Skip to content

Commit

Permalink
Merge pull request RackHD#18 from tldavies/addingtests
Browse files Browse the repository at this point in the history
Adding unit tests to parser and packet.
  • Loading branch information
benbp committed Dec 18, 2015
2 parents b8e7631 + 9fd1800 commit 959ab31
Show file tree
Hide file tree
Showing 6 changed files with 1,010 additions and 302 deletions.
227 changes: 196 additions & 31 deletions lib/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function createPacketBuffer(pkt) {
if (fname.length > 128) {
throw new Error('fname field too long (128 bytes max): ' + fname.toString());
}

var hw;
// If this is coming from a client, it's a string
if (typeof pkt.chaddr === 'string') {
Expand All @@ -129,9 +130,27 @@ function createPacketBuffer(pkt) {
var p = new Buffer(1500);
var i = 0;

p.writeUInt8(pkt.op || 0, i);

if(typeof pkt.op === 'object'){
p.writeUInt8(pkt.op.value, i);
}
else{
p.writeUInt8(pkt.op || 0, i);
}
i += 1;
p.writeUInt8(pkt.htype || 0, i);
//packet Type may be pkt.type or pkt.chaddr.type
if(typeof pkt.htype === 'object'){
p.writeUInt8(pkt.htype.value, i);
}
else if(typeof pkt.chaddr.type === 'object'){
p.writeUInt8(pkt.chaddr.type.value, i);
}
else if(typeof pkt.chaddr.type === undefined) {
p.writeUInt8(pkt.htype || 0, i);
}
else{
p.writeUInt8(pkt.chaddr.type, i);
}
i += 1;
p.writeUInt8(pkt.hlen || 0, i);
i += 1;
Expand Down Expand Up @@ -175,17 +194,24 @@ function createPacketBuffer(pkt) {
p.writeUInt32BE(0x63825363, i);
i += 4;

if (pkt.options && pkt.options.subnetMask) {
if (pkt.options && pkt.options.subnetMask) { // option 1
p.writeUInt8(1, i);
i += 1; // option 1
var subnetMask = new Buffer(
new v4.Address(pkt.options.subnetMask).toArray());
i += 1;
var subnetMask = new Buffer(new v4.Address(pkt.options.subnetMask).toArray());
p.writeUInt8(subnetMask.length, i);
i += 1;
subnetMask.copy(p, i);
i += subnetMask.length;
}
if (pkt.options && pkt.options.routerOptions) {
if (pkt.options && pkt.options.timeOffset) { // option 2
p.writeUInt8(2, i);
i += 1; // option 2
p.writeUInt8(4, i);
i += 1; // length
p.writeUInt32BE(pkt.options.timeOffset, i);
i += 4;
}
if (pkt.options && pkt.options.routerOptions) { // option 3
p.writeUInt8(3, i);
i += 1; // option 3
// If routerOptions is not an array by human error,
Expand All @@ -205,7 +231,64 @@ function createPacketBuffer(pkt) {
i += routerBuffer.length;
});
}
if (pkt.options && pkt.options.broadcastAddress) {
if(pkt.options && pkt.options.timeServerOption) { //option 4
p.writeUInt8(4, i);
i += 1; // option 4
// If timeServerOption is not an array by human error,
// it's probably a string and we should just make it an array.
var timeServerOptions = pkt.options.timeServerOption;
if (typeof timeServerOptions !== 'object') {
timeServerOptions = [timeServerOptions];
}
// Let's hope timeServerOptions.length * 4 can fit in one byte :)
// To quote Joe: "probably OSPF would blow up before that!"
p.writeUInt8(timeServerOptions.length * 4, i);
i += 1;
timeServerOptions.forEach(function (timeServer) {
var timeServerBuffer = new Buffer(
new v4.Address(timeServer).toArray());
timeServerBuffer.copy(p, i);
i += timeServerBuffer.length;
});
}
if(pkt.options && pkt.options.domainNameServerOption) { //option 6
p.writeUInt8(6, i);
i += 1; // option 6
// If domainNameServerOption is not an array by human error,
// it's probably a string and we should just make it an array.
var domainNameServerOptions = pkt.options.domainNameServerOption;
if (typeof domainNameServerOptions !== 'object') {
domainNameServerOptions = [domainNameServerOptions];
}
// Let's hope domainNameServerOptions.length * 4 can fit in one byte :)
p.writeUInt8(domainNameServerOptions.length * 4, i);
i += 1;
domainNameServerOptions.forEach(function (domainNameServer) {
var domainNameServerBuffer = new Buffer(
new v4.Address(domainNameServer).toArray());
domainNameServerBuffer.copy(p, i);
i += domainNameServerBuffer.length;
});
}
if(pkt.options && pkt.options.hostName) { //option 12
p.writeUInt8(12, i);
i += 1; // option 12
var hostName = new Buffer(pkt.options.hostName);
p.writeUInt8(hostName.length, i);
i += 1;
hostName.copy(p, i);
i += hostName.length;
}
if(pkt.options && pkt.options.domainName) { //option 15
p.writeUInt8(15, i);
i += 1; // option 15
var domainName = new Buffer(pkt.options.domainName);
p.writeUInt8(domainName.length, i);
i += 1;
domainName.copy(p, i);
i += domainName.length;
}
if (pkt.options && pkt.options.broadcastAddress) { // option 28
p.writeUInt8(28, i);
i += 1; // option 28
var broadcastAddress = new Buffer(
Expand All @@ -215,7 +298,34 @@ function createPacketBuffer(pkt) {
broadcastAddress.copy(p, i);
i += broadcastAddress.length;
}
if (pkt.options && pkt.options.requestedIpAddress) {
if (pkt.options && pkt.options.vendorOptions) { // option 43
p.writeUInt8(43, i);
i += 1; // option 43
var vendorOptions = pkt.options.vendorOptions;

//Find the length of the venderoption buffer
var venLength = 0;
for (var key in vendorOptions) {
if (vendorOptions.hasOwnProperty(key)) {
venLength += 2 + vendorOptions[key].length;
}
}

p.writeUInt8(venLength, i);
i += 1;
for (key in vendorOptions) {
if (vendorOptions.hasOwnProperty(key)) {
p.writeUInt8(parseInt(key), i); //write key
i += 1;
var valueBuf = new Buffer(vendorOptions[key]);
p.writeUInt8(valueBuf.length, i); //write value length
i += 1;
valueBuf.copy(p, i); //write value
i += valueBuf.length;
}
}
}
if (pkt.options && pkt.options.requestedIpAddress) { // option 50
p.writeUInt8(50, i);
i += 1; // option 50
var requestedIpAddress = new Buffer(
Expand All @@ -225,23 +335,23 @@ function createPacketBuffer(pkt) {
requestedIpAddress.copy(p, i);
i += requestedIpAddress.length;
}
if (pkt.options && pkt.options.ipAddressLeaseTime) {
if (pkt.options && pkt.options.ipAddressLeaseTime) { // option 51
p.writeUInt8(51, i);
i += 1; // option 51
p.writeUInt8(4, i);
i += 1; // length
p.writeUInt32BE(pkt.options.ipAddressLeaseTime, i);
i += 4;
}
if (pkt.options && pkt.options.optionOverload) {
if (pkt.options && pkt.options.optionOverload) { // option 52
p.writeUInt8(52, i);
i += 1; // option 52
p.writeUInt8(1, i);
i += 1; // length
p.writeUInt8(pkt.options.optionOverload, i);
i += 1;
}
if (pkt.options && pkt.options.dhcpMessageType) {
if (pkt.options && pkt.options.dhcpMessageType) { // option 53
p.writeUInt8(53, i);
i += 1; // option 53
p.writeUInt8(1, i);
Expand All @@ -255,7 +365,7 @@ function createPacketBuffer(pkt) {
}
i += 1;
}
if (pkt.options && pkt.options.serverIdentifier) {
if (pkt.options && pkt.options.serverIdentifier) { // option 54
p.writeUInt8(54, i);
i += 1; // option 54
var serverIdentifier = new Buffer(
Expand All @@ -265,7 +375,7 @@ function createPacketBuffer(pkt) {
serverIdentifier.copy(p, i);
i += serverIdentifier.length;
}
if (pkt.options && pkt.options.parameterRequestList) {
if (pkt.options && pkt.options.parameterRequestList) { // option 55
p.writeUInt8(55, i);
i += 1; // option 55
var parameterRequestList = new Buffer(pkt.options.parameterRequestList);
Expand All @@ -277,48 +387,65 @@ function createPacketBuffer(pkt) {
parameterRequestList.copy(p, i);
i += parameterRequestList.length;
}
if (pkt.options && pkt.options.renewalTimeValue) {
if (pkt.options && pkt.options.maximumMessageSize) { // option 57
p.writeUInt8(57, i);
i += 1; // option 57
p.writeUInt8(2, i);
i += 1; // length
p.writeUInt16BE(pkt.options.maximumMessageSize, i);
i += 2;
}
if (pkt.options && pkt.options.renewalTimeValue) { // option 58
p.writeUInt8(58, i);
i += 1; // option 58
p.writeUInt8(4, i);
i += 1; // length
p.writeUInt32BE(pkt.options.renewalTimeValue, i);
i += 4;
}
if (pkt.options && pkt.options.rebindingTimeValue) {
if (pkt.options && pkt.options.rebindingTimeValue) { // option 59
p.writeUInt8(59, i);
i += 1; // option 59
p.writeUInt8(4, i);
i += 1; // length
p.writeUInt32BE(pkt.options.rebindingTimeValue, i);
i += 4;
}
if (pkt.options && pkt.options.vendorClassIdentifier) {
if (pkt.options && pkt.options.vendorClassIdentifier) { // option 60
p.writeUInt8(60, i);
i += 1; // option 60
var vendorClassIdentifier =
new Buffer(pkt.options.vendorClassIdentifier);
var vendorClassIdentifier = new Buffer(pkt.options.vendorClassIdentifier);
p.writeUInt8(vendorClassIdentifier.length, i);
i += 1;
vendorClassIdentifier.copy(p, i);
i += vendorClassIdentifier.length;
}
if (pkt.options && pkt.options.clientIdentifier) {
var clientIdentifier = new Buffer(pkt.options.clientIdentifier);
var optionLength = 1 + clientIdentifier.length;
if (optionLength > 0xff) {
throw new Error('pkt.options.clientIdentifier malformed');
}
if (pkt.options && pkt.options.clientIdentifier) { // option 61
p.writeUInt8(61, i);
i += 1; // option 61
p.writeUInt8(optionLength, i);
var add = pkt.options.clientIdentifier.address;
var addBuf = new Buffer(add.split(':').map(function (part) {
return parseInt(part, 16);
}));
p.writeUInt8(addBuf.length+1, i); //add length of buffer
i += 1; // length
p.writeUInt8(0, i);
i += 1; // hardware type 0
clientIdentifier.copy(p, i);
i += clientIdentifier.length;
if (addBuf.length !== 6) {
throw new Error('pkt.options.clientIdentifier.address malformed, only ' +
add.length + ' bytes');
}
//Add type to buffer
if(typeof pkt.options.clientIdentifier.type !== 'object'){
p.writeUInt8(pkt.options.clientIdentifier.type, i);
}
else {
p.writeUInt8(pkt.options.clientIdentifier.type.value, i);

}
i += 1; // hardware type 0
addBuf.copy(p, i);
i += addBuf.length;
}
if (pkt.options && pkt.options.bootFileName) {
if (pkt.options && pkt.options.bootFileName) { // option 67
p.writeUInt8(67, i);
i += 1; // option 67
var bootFileName = new Buffer(pkt.options.bootFileName);
Expand All @@ -327,6 +454,44 @@ function createPacketBuffer(pkt) {
bootFileName.copy(p, i);
i += bootFileName.length;
}
if (pkt.options && pkt.options.userClass) { // option 77
p.writeUInt8(77, i);
i += 1; // option 60
var userClass = new Buffer(pkt.options.userClass);
p.writeUInt8(userClass.length, i);
i += 1;
userClass.copy(p, i);
i += userClass.length;
}
if (pkt.options && pkt.options.fullyQualifiedDomainName) { // option 81
p.writeUInt8(81, i);
i += 1; // option 81
var name = new Buffer(pkt.options.fullyQualifiedDomainName.name); //name
p.writeUInt8(name.length+3, i);
i += 1;
p.writeUInt8(pkt.options.fullyQualifiedDomainName.flags, i); //flag
i += 3;
name.copy(p, i);
i += name.length;
}
if (pkt.options && pkt.options.archType) { // option 93
p.writeUInt8(93, i);
i += 1; // option 57
p.writeUInt8(2, i);
i += 1; // length
p.writeUInt16BE(pkt.options.archType, i);
i += 2;
}
if (pkt.options && pkt.options.subnetAddress) { // option 118
p.writeUInt8(118, i);
i += 1; // option 118
var subnetAddress = new Buffer(
new v4.Address(pkt.options.subnetAddress).toArray());
p.writeUInt8(subnetAddress.length, i);
i += 1;
subnetAddress.copy(p, i);
i += subnetAddress.length;
}

// option 255 - end
p.writeUInt8(0xff, i);
Expand Down
Loading

0 comments on commit 959ab31

Please sign in to comment.