infusion / node-dhcp

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

windows 10, basic example of dhcpd: needs to specify server ip #43

Closed ccoenen closed 5 years ago

ccoenen commented 5 years ago

I'm not sure if this is expected or not, on windows 10 with the example dhcpd from the readme, it will fail

.....\node_modules\dhcp\lib\dhcp.js:180
    throw new Error('Required option ' + Options.opts[req].config + ' does not have a value set');
    ^

Error: Required option server does not have a value set
    at Server._getOptions (.....\node_modules\dhcp\lib\dhcp.js:180:17)
    at Server.sendOffer (.....\node_modules\dhcp\lib\dhcp.js:370:21)
    at Server.handleDiscover (.....\node_modules\dhcp\lib\dhcp.js:349:10)
    at Socket.<anonymous> (.....\node_modules\dhcp\lib\dhcp.js:91:16)
    at Socket.emit (events.js:182:13)
    at UDP.onMessage [as onmessage] (dgram.js:678:8)

The error goes away if I specify my computer's IP in createServer:

var s = dhcp.createServer({
    server: '192.168.100.20', // my computer's local IP
    // rest unchanged

I went to dhcp.js around line 345 and tried reading config('server'), but it was simply 0. I didn't find where and if this should be set manually under normal circumstances.

If this is expected, I would like to request adding it to the readme. If this is unexpected, I would like find and squish that bug.

ccoenen commented 5 years ago

for reference: I'm using node.js 11.0.7 (current version at time of writing). NPM 6.4.1 and dhcp 0.2.18 from npm packages.

infusion commented 5 years ago

I'm reading the source to figure out what could go wrong on windows that it's not finding an entry on the object. This is really strange and you did nothing wrong, so it should be a bug somehow.

infusion commented 5 years ago

Does this also happen with examples/server.js? You should be able to execute it without any problems. Here for me on OSX with node 11.5.0:

➜  examples git:(master) ✗ sudo node server.js
Server Listening: 0.0.0.0:67
ccoenen commented 5 years ago

interestingly, i can run the server example with no problem at all right from master. It also completes a full cycle until "BOUND" when I reset wifi on my smartphone.

infusion commented 5 years ago

Okay, then the example in the readme seems to be broken somehow. I'll look into that.

ccoenen commented 5 years ago

I verified that I have the same problem with master.

I just saw, server.js has also set the server config:

https://github.com/infusion/node-dhcp/blob/e6c7792bc59d4771acbac6ffb5be5b208bbe47b3/examples/server.js#L26

perhaps it's just missing from the minimal readme example.

ccoenen commented 5 years ago

funnily, I didn't alter that (so on my network this would be the wrong IP) yet it didn't create any problem as far as I can tell right now.

infusion commented 5 years ago

This does not throw an error since that option is sent to the client in the dhcp package only. The server is bound on INADDR_ANY per default. I updated the readme, sorry for that mistake.

ccoenen commented 5 years ago

not a problem. I have a fine dhcpd running in JS right now. Many thanks for that :-)

I added server and also broadcast and dns to my script and it works spectacularly!

Next up: auto configure new clients (think: ssh-copy-id and setting serialised hostnames for a bunch of raspberry-pis). I'll let you know how it goes!

infusion commented 5 years ago

You're welcome!

I was too lazy to document all options. You can have a look into the options.js file if you need more things you can push to your PI's :)

But configuring a PI was also my initial motivation for this project. I also loved the idea of setting the hostname, but this is/was a bit tricky with the standard dhcp client running on the PI. I found 1000 sites on the net that did not work to set the hostname and ultimately found one that did. But don't ask which :D