Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use interfaces of current machine for example client #4

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
53 changes: 0 additions & 53 deletions example-client.js

This file was deleted.

67 changes: 67 additions & 0 deletions example/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env node

// Copyright (c) 2014 Andrew Paprocki

var os = require('os')

var util = require('util');

var dhcpjs = require('..');


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');
});


var interfaces = os.getNetworkInterfaces()

for(var name in interfaces)
{
// Configure a DHCPDISCOVER packet:
// xid 0x01 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 being configured
//
// 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 discover = client.createDiscoverPacket(
{
xid: 0x01,
chaddr: interfaces[name][0].mac,
options:
{
dhcpMessageType: dhcpjs.Protocol.DHCPMessageType.DHCPDISCOVER,
clientIdentifier: 'MyMachine',
}
});

client.broadcastPacket(discover, undefined, function()
{
console.log('dhcpDiscover ('+name+'): sent');
});
}