-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (69 loc) · 1.94 KB
/
index.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
70
71
72
73
74
const udp = require('dgram');
const EventEmitter = require('events');
const Packet = require('./packet');
const SERVER_PORT = 67;
const CLIENT_PORT = 68;
const INADDR_ANY = '0.0.0.0';
const INADDR_BROADCAST = '255.255.255.255';
/**
* DHCP Client
* @rfc: https://tools.ietf.org/html/rfc2132
*/
class DHCP extends udp.Socket {
constructor(options) {
options = Object.assign({
type: 'udp4'
}, options);
super(options.type);
this.on('message', message => {
const packet = Packet.parse(message);
this.emit(packet.options[Packet.OPTION_TYPES.MessageType], packet);
});
this.on(Packet.TYPES.DHCPACK, this.handleAck.bind(this));
this.on(Packet.TYPES.DHCPOFFER, this.handleOffer.bind(this));
return this;
}
handleOffer(offer) {
this.emit('offer', offer);
}
handleAck(message) {
this.emit('ack', message);
}
listen(port = CLIENT_PORT, address) {
this.socket.bind(port, address);
return this;
}
discover(mac) {
const packet = new Packet();
packet.op = Packet.OPCODE.BOOTREQUEST;
packet.hlen = 6;
packet.chaddr = mac;
return this.send(packet);
}
request(lease) {
const packet = new Packet();
packet.op = BOOTREQUEST;
packet.chaddr = this.mac;
packet.ciaddr = INADDR_ANY;
packet.yiaddr = INADDR_ANY;
packet.giaddr = INADDR_ANY;
packet.options[Packet.OPTION_TYPES.MessageType] = Packet.TYPES.DHCPREQUEST;
packet.options[Packet.OPTION_TYPES.ClientIdentifier] = this.mac;
packet.options[Packet.OPTION_TYPES.RequestedIPAddress] = lease.address;
this.send(packet.toBuffer());
}
send(data) {
if (data instanceof Packet)
data = data.toBuffer();
super.send(data, CLIENT_PORT, INADDR_BROADCAST, (err) => {
console.log('send -> ', err);
});
return this;
}
}
DHCP.Packet = Packet;
DHCP.Server = require('./server');
DHCP.createServer = function (options) {
return new DHCP.Server(options);
};
module.exports = DHCP;