forked from apaprocki/node-dhcpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-dhcpinform.js
68 lines (63 loc) · 2.36 KB
/
example-dhcpinform.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
// Copyright (c) 2014 Andrew Paprocki
var dhcpjs = require('dhcpjs');
var util = require('util');
var os = require('os');
var client = new dhcpjs.Client();
client.on('message', function(pkt) {
console.log('message:', util.inspect(pkt, false, 3));
});
client.on('dhcpOffer', function(pkt) {
console.log('dhcpOffer:', util.inspect(pkt, false, 3));
});
client.on('dhcpAck', function(pkt) {
console.log('dhcpAck:', util.inspect(pkt, false, 3));
});
client.on('dhcpNak', function(pkt) {
console.log('dhcpNak:', util.inspect(pkt, false, 3));
});
client.on('listening', function(addr) {
console.log('listening on', addr);
});
client.bind('0.0.0.0', 68, function() {
console.log('bound to 0.0.0.0:68');
});
// Configure a DHCPINFORM packet:
// xid Transaction ID. This is a counter that the DHCP
// client should maintain and increment every time
// a packet is broadcast.
//
// chaddr Ethernet address of the interface.
//
// ciaddr Pre-configured client network address.
//
// options Object containing keys that map to DHCP options.
//
// dhcpMessageType Option indicating a DHCP protocol message (as
// opposed to a plain BOOTP protocol message).
//
// clientIdentifier Option indicating a client-configured unique name
// to be used to disambiguate the lease on the server.
var xid = 1;
var interfaces = os.getNetworkInterfaces();
for (var interface in interfaces) {
var addresses = interfaces[interface];
for (var address in addresses) {
if (addresses[address].family === 'IPv4' &&
!addresses[address].internal) {
console.log(util.inspect(addresses[address], false));
var pkt = {
xid: xid++,
chaddr: addresses[address].mac,
ciaddr: addresses[address].address,
options: {
dhcpMessageType: dhcpjs.Protocol.DHCPMessageType.DHCPINFORM,
clientIdentifier: 'MyMachine',
}
}
var discover = client.createDiscoverPacket(pkt);
client.broadcastPacket(discover, undefined, function() {
console.log('dhcpInform ['+interface+': '+pkt.ciaddr+']: sent');
});
}
}
}