apaprocki / node-dhcpjs

dhcpjs provides native DHCP support in Node.js
MIT License
61 stars 21 forks source link

crash when handling malformed dhcp packet #24

Closed gregorybrzeski closed 7 years ago

gregorybrzeski commented 7 years ago

If packet is malformed node-dhcpjs crashes, do you have some thoughts how to handle this ?

# node ./test.js 
listening on 0.0.0.0:67
events.js:182
      throw er; // Unhandled 'error' event
      ^

RangeError: Index out of range
    at checkOffset (buffer.js:934:11)
    at Uint8Array.Buffer.readUInt32BE (buffer.js:1008:5)
    at Object.module.exports.parse (/root/dhcp-test/node_modules/dhcpjs/lib/parser.js:63:18)
    at Socket.<anonymous> (/root/dhcp-test/node_modules/dhcpjs/lib/server.js:22:29)
    at emitTwo (events.js:125:13)
    at Socket.emit (events.js:213:7)
    at UDP.onMessage [as onmessage] (dgram.js:623:8)

Malformed packet is sent like this (x.x.x.x is the ip of the dhcp server):

dgram.createSocket('udp4').send("foobar", 67, "x.x.x.x", console.log)
apaprocki commented 7 years ago

The little dummy server program does this:

      try {
          var data = parser.parse(msg, rinfo);
          self.emit('message', data);
      } catch (e) {
          if (!self.emit('error', e)) {
              throw e;
          }
      }

So you're supposed to handle the error event and do with it what you please.