From 154120cb3b1e89e31f87b580711727f761554dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Tue, 19 May 2015 20:18:59 +0200 Subject: [PATCH] Use interfaces of current machine for example client --- .gitignore | 1 + example-client.js | 53 ------------------------------------- example/client.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 .gitignore delete mode 100644 example-client.js create mode 100755 example/client.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/example-client.js b/example-client.js deleted file mode 100644 index 888d3e5..0000000 --- a/example-client.js +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) 2014 Andrew Paprocki - -var dhcpjs = require('dhcpjs'); -var util = require('util'); - -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 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 pkt = { - xid: 0x01, - chaddr: '00:01:02:03:04:05', - options: { - dhcpMessageType: dhcpjs.Protocol.DHCPMessageType.DHCPDISCOVER, - clientIdentifier: 'MyMachine', - } -} - -var discover = client.createDiscoverPacket(pkt); -client.broadcastPacket(discover, undefined, function() { - console.log('dhcpDiscover: sent'); -}); diff --git a/example/client.js b/example/client.js new file mode 100755 index 0000000..f53840d --- /dev/null +++ b/example/client.js @@ -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'); + }); +}