infusion / node-dhcp

A DHCP server and client written in pure JavaScript
https://raw.org/article/a-pure-javascript-dhcp-implementation/
MIT License
301 stars 75 forks source link

Interface binding #37

Closed evacomm-mmacadams closed 6 years ago

evacomm-mmacadams commented 6 years ago

For a DHCP server, it would be ideal to be able to bind an instance to an interface . (or perhaps a group of interfaces). If you were building a router, you only wan DHCP offered on LAN ports, not the WAN ports.

Thanks for taking the time to build this project, it's been very helpful!

nistorj commented 6 years ago

+1

infusion commented 6 years ago

Did you guys try to pass a net you want to listen on to the listen-method like so?

// dhcp.listen(port, net, callback)
dhcp.listen(null, '192.168.3.0');
nistorj commented 6 years ago

Ah yes, thanks!

I edited lib/node_modules/dhcp/bin/dhcpd-cli.js and ended up with: //server.listen(); server.listen(null,'10.10.10.1');

root@guestshell ~/Python_DHCP: netstat -an | grep :67 udp 0 0 0.0.0.0:67 0.0.0.0: root@guestshell ~/Python_DHCP: netstat -an | grep :67 udp 0 0 10.10.10.1:67 0.0.0.0: root@guestshell ~/Python_DHCP:

thanks!

infusion commented 6 years ago

Yeah! You're welcome :) Do you have any suggestions to make things better for others too?

nistorj commented 6 years ago

Passing it on the CLI at startup under the options list would be ideal, unless I somehow missed that completely. --interface or --bind .

FYI, In my situation I'm running this within a guest shell on a Nexus 9000 switch since NXOS doesn't provide the ability to run a DHCP server locally.

infusion commented 6 years ago

I quickly added the bind option to the CLI daemon. Could you check it out, if that's the way you would like to have it? (with --bind 10.10.10.1)

nistorj commented 6 years ago

slick. Works well for IP addr yes, thanks!

For binding to an interface name (such as Vlan400, or Eth1/x) it doesn't but that's ok for me, I can work with it. Thanks so much!

infusion commented 6 years ago

It's typically IP based, since its the only way to make it work across platforms, especially Windows. Under Linux you need to use SO_BINDTODEVICE, under OSX you need to use IP_BOUND_IF, but as far as I know there is no option to pass this info to the OS using nodejs.

There is a hacky solution using ffi: https://github.com/nodejs/node/issues/3625