Created
April 26, 2015 04:25
-
-
Save John-Lin/97f8622d3a52bbba1ecd to your computer and use it in GitHub Desktop.
Decode DHCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EthernetAddr = require("./ethernet_addr"); | |
var IPv4Addr = require("./ipv4_addr"); | |
// DHCP packet parser | |
// RFC 2131 | |
// DHCP packet format | |
// 0 1 2 3 | |
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | op (1) | htype (1) | hlen (1) | hops (1) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | xid (4) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | secs (2) | flags (2) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | ciaddr (4) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | yiaddr (4) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | siaddr (4) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | giaddr (4) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | | | |
// | chaddr (16) | | |
// | | | |
// | | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | | | |
// | sname (64) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | | | |
// | file (128) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
// | | | |
// | options (variable) | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
function DHCP(emitter) { | |
this.emitter = emitter; | |
this.op = undefined; | |
this.htype = undefined; | |
this.hlen = undefined; | |
this.hops = undefined; | |
this.xid = undefined; | |
this.secs = undefined; | |
this.flags = undefined; | |
this.ciaddr = undefined; | |
this.yiaddr = undefined; | |
this.siaddr = undefined; | |
this.giaddr = undefined; | |
this.chaddr = undefined; | |
this.sname = undefined; | |
this.bootfile = undefined; | |
this.options = undefined; | |
} | |
DHCP.prototype.decode = function (raw_packet, offset) { | |
this.op = raw_packet[offset]; | |
this.htype = raw_packet[offset + 1]; | |
this.hlen = raw_packet[offset + 2]; | |
this.hops = raw_packet[offset + 3]; | |
this.xid = raw_packet.readUInt32BE(offset + 4); | |
this.secs = raw_packet.readUInt16BE(offset + 8); | |
this.flags = raw_packet.readUInt16BE(offset + 10); | |
this.ciaddr = new IPv4Addr().decode(raw_packet, offset + 12); | |
this.yiaddr = new IPv4Addr().decode(raw_packet, offset + 16); | |
this.siaddr = new IPv4Addr().decode(raw_packet, offset + 20); | |
this.giaddr = new IPv4Addr().decode(raw_packet, offset + 24); | |
this.chaddr = new EthernetAddr(raw_packet, offset + 28); | |
this.sname = raw_packet.readUInt32BE(offset + 32); | |
this.bootfile = raw_packet.readUInt32BE(offset + 36); | |
this.options = raw_packet.readUInt32BE(offset + 40); | |
if(this.emitter) { this.emitter.emit("dhcp", this); } | |
return this; | |
}; | |
DHCP.prototype.decoderName = "dhcp"; | |
DHCP.prototype.eventsOnDecode = true; | |
module.exports = DHCP; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment