diff --git a/modules/dhcp.js b/modules/dhcp.js new file mode 100644 index 0000000..ce98d7c --- /dev/null +++ b/modules/dhcp.js @@ -0,0 +1,272 @@ +/* +Copyright 2021 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +* @description Mini DHCP Client Module, to fetch configuration data +* @author Bryan Roe & Ylian Saint-Hilaire +*/ + +// DHCP Information +var promise = require('promise'); +function promise_default(res, rej) +{ + this._res = res; + this._rej = rej; +} + + +function buf2addr(buf) +{ + return (buf[0] + '.' + buf[1] + '.' + buf[2] + '.' + buf[3]); +} +function parseDHCP(buffer) +{ + var i; + var packet = Buffer.alloc(buffer.length); + for (i = 0; i < buffer.length; ++i) { packet[i] = buffer[i]; } + + var ret = { op: packet[0] == 0 ? 'REQ' : 'RES', hlen: packet[2] }; // OP Code + ret.xid = packet.readUInt32BE(4); // Transaction ID + ret.ciaddr = buf2addr(packet.slice(12, 16)); + ret.yiaddr = buf2addr(packet.slice(16, 20)); + ret.siaddr = buf2addr(packet.slice(20, 24)); + ret.giaddr = buf2addr(packet.slice(24, 28)); + ret.chaddr = packet.slice(28, 28 + ret.hlen).toString('hex:'); + if (packet[236] == 99 && packet[237] == 130 && packet[238] == 83 && packet[239] == 99) + { + // Magic Cookie Validated + ret.magic = true; + ret.options = {}; + + i = 240; + while(i