-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHost.js
69 lines (57 loc) · 1.52 KB
/
Host.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const config = require('./config.json');
const dgram = require('dgram');
module.exports = class Host
{
outSocket;
config;
client;
constructor(outSocket)
{
this.outSocket = outSocket;
this.config = config;
this.client = dgram.createSocket('udp4');
}
OnFrameReceived(frame, buffer)
{
//TODO: organize protocols into a stack, including the bottom layer (DL?) that routes to ARP, IP, etc.
console.log("host received a frame");
let reply;
if (frame.type.name === "ARP")
{
console.log(buffer);
reply = this.processARPMessage(frame.payload, buffer);
}
if (reply)
{
frame.ConvertToReply(config.me.MAC, buffer);
console.log("sending reply: " );
console.log(frame);
setTimeout(this.sendBuffer.bind(this), 250, buffer);
}
}
sendBuffer(buffer)
{
console.log("sending buffer:");
console.log(buffer);
this.client.send(buffer.buffer, this.config.udpsockets.out, '0.0.0.0');
}
processARPMessage(message, buffer)
{
//console.log("host received a frame. Op=" + message.operation.name);
if (message.operation.name === "Request")
{
//look in the cache and send the response if one exists
let targetMAC = config.arpCache[message.target.IP]
if (targetMAC)
{
console.log("ARP request target " + message.target.IP + " in cache as " + targetMAC + ". Replying...");
message.ConvertToReply(targetMAC, message.target.IP, buffer);
return message;
}
else
{
console.log("ARP request target " + message.target.IP + " not in cache. Not responding");
}
}
}
}