From 99722a43d8e1ff5427bd0fd59ae239bc3e893759 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 21:18:55 +0200 Subject: [PATCH 1/7] Added a method to parse option82 suboptions for cisco devices --- lib/seqbuffer.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/seqbuffer.js b/lib/seqbuffer.js index 3eb3547..1069e5a 100644 --- a/lib/seqbuffer.js +++ b/lib/seqbuffer.js @@ -278,7 +278,11 @@ SeqBuffer.prototype = { let len = this.getUInt8(); if (opt in Options) { - options[opt] = this['get' + Options[opt].type](len); + if (opt == 82) { + options[opt] = this['get' + Options[opt].type](Options[opt].default); + } else { + options[opt] = this['get' + Options[opt].type](len); + } } else { this._r += len; console.error('Option ' + opt + ' not known'); @@ -287,6 +291,41 @@ SeqBuffer.prototype = { } return options; }, + getSubOptions(vendor) { + + const sub_options = {}; + const buf = this._data; + + while (this._r < buf.length - 1) { + let opt = this.getUInt8(); + let len = this.getUInt8(); + + // Agent Circuit ID Sub-option + if (opt == 0x01) { + switch(vendor) { + default: + sub_options["circuitId"] = this.getASCII(len); + } + } + + // Agent Remote ID Sub-option + if (opt == 0x02) { + switch(vendor) { + case "cisco": + // Cisco Remote ID Type + let sub_type = this.getUInt8(); + // Cisco Remote ID Length + let sub_type_length = this.getUInt8(); + sub_options["remoteId"] = this.getASCII(sub_type_length); + break; + default: + sub_options["remoteId"] = this.getASCII(len); + break; + } + } + } + return sub_options; + }, // addUInt8s: function(arr) { From 019e790af0eec8c112659b39f5d080fa83e06290 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 21:19:37 +0200 Subject: [PATCH 2/7] Added a test for option82 sub-options --- tests/segbuffer.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/segbuffer.test.js b/tests/segbuffer.test.js index 0f58852..fec519e 100644 --- a/tests/segbuffer.test.js +++ b/tests/segbuffer.test.js @@ -429,4 +429,19 @@ describe('Segbuffer', function() { }); + it('should get option82 sub-options correctly for vendor cisco', function() { + + var vendor = "cisco"; + + // Option82 with Sub-Option 1 first + var sb_1 = new SeqBuffer(new Buffer([0x52, 0x19, 0x01, 0x0a, 0x43, 0x49, 0x52, 0x43, 0x55, 0x49, 0x54, 0x5f, 0x49, 0x44, 0x02, 0x0b, 0x01, 0x09, 0x52, 0x45, 0x4d, 0x4f, 0x54, 0x45, 0x5f, 0x49, 0x44])); + + sb_1.getSubOptions(vendor).should.be.deepEqual({ circuitId: 'CIRCUIT_ID', remoteId: 'REMOTE_ID' }); + + // Option82 with Sub-Option 2 first + var sb_2 = new SeqBuffer(new Buffer([0x52, 0x19, 0x02, 0x0b, 0x01, 0x09, 0x52, 0x45, 0x4d, 0x4f, 0x54, 0x45, 0x5f, 0x49, 0x44, 0x01, 0x0a, 0x43, 0x49, 0x52, 0x43, 0x55, 0x49, 0x54, 0x5f, 0x49, 0x44])); + + sb_2.getSubOptions(vendor).should.be.deepEqual({ circuitId: 'CIRCUIT_ID', remoteId: 'REMOTE_ID' }); + }); + }); From 09b7b6e54fb4195fe46c8e371dadfc0ef9f7e8f6 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 21:20:17 +0200 Subject: [PATCH 3/7] Added option82 to the options object --- lib/options.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/options.js b/lib/options.js index b5f597a..88cd359 100644 --- a/lib/options.js +++ b/lib/options.js @@ -459,10 +459,14 @@ const opts = { name: 'Rapid Commit', type: 'Bool', attr: 'rapidCommit' - }, /* - 82: { // RFC 3046, relayAgentInformation - - },*/ + }, + 82: { // TODO: Add support for more vendors + // RFC 3046, relayAgentInformation + name: "Relay Agent Information", + type: "SubOptions", + config: "relayAgentInformation", + default: "cisco" + }, 95: { name: 'LDAP Servers', type: 'IPs', From e0bd6fdf17058bdc77fb8234392a3fa168b98e85 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 23:08:35 +0200 Subject: [PATCH 4/7] Changed default to vendor --- lib/seqbuffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/seqbuffer.js b/lib/seqbuffer.js index 1069e5a..a0cf2de 100644 --- a/lib/seqbuffer.js +++ b/lib/seqbuffer.js @@ -279,7 +279,7 @@ SeqBuffer.prototype = { if (opt in Options) { if (opt == 82) { - options[opt] = this['get' + Options[opt].type](Options[opt].default); + options[opt] = this['get' + Options[opt].type](Options[opt].vendor); } else { options[opt] = this['get' + Options[opt].type](len); } From 3a590a9fbbe2256e57cd3f8503619cf6f70ea79c Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 23:09:19 +0200 Subject: [PATCH 5/7] Added example configuration to the dhcp server configuration --- examples/server.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/server.js b/examples/server.js index 9820d70..a6d0c3b 100644 --- a/examples/server.js +++ b/examples/server.js @@ -37,7 +37,19 @@ var s = dhcpd.createServer({ } else { return 'x64linux.0'; } - } + }, + + // Allocate an ip based on a static Option82 Circuit-ID binding + // relayAgentInformation: { + // "fa0/1": "192.168.3.101" + // }, + // Allocate an ip based on a callback function + // relayAgentInformation: function() { + // return function(remoteId, circuitId) { + // if (remoteId == "als01" && circuitId == "fa0/1") + // return "192.168.3.102"; + // } + // }, }); s.on('message', function(data) { From a48c8cae20475df13ffdbec692e4d7a0416b84f4 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 23:10:07 +0200 Subject: [PATCH 6/7] added configuration to bind ip based on option82 --- lib/dhcp.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/dhcp.js b/lib/dhcp.js index e009e81..94f34f6 100644 --- a/lib/dhcp.js +++ b/lib/dhcp.js @@ -288,6 +288,19 @@ Server.prototype = { return _static[clientMAC]; } + + // Is there a option82 binding? + const _relayAgentInformation = this.config('relayAgentInformation'); + console.log("Option82: %s", typeof _relayAgentInformation[req.options["82"]["remoteId"]]); + if (typeof _relayAgentInformation === "function") { + const relayAgentInformationResult = _relayAgentInformation(req.options["82"]["remoteId"], req.options["82"]["circuitId"]); + if (relayAgentInformationResult) + return relayAgentInformationResult; + } else if (typeof _relayAgentInformation[req.options["82"]["remoteId"]] === "string") { + return _relayAgentInformation[req.options["82"]["circuitId"]]; + } else if (typeof _relayAgentInformation[req.options["82"]["remoteId"]] === "object") { + return _relayAgentInformation[req.options["82"]["remoteId"][req.options["82"]["circuitId"]]]; + } const randIP = this.config('randomIP'); const _tmp = this.config('range'); From 07f0cca477b954ab2a43b19e0d6e9a90de83c002 Mon Sep 17 00:00:00 2001 From: Emil Haugbergsmyr Date: Fri, 11 Jun 2021 23:10:36 +0200 Subject: [PATCH 7/7] changed default to vendor in options configuration --- lib/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/options.js b/lib/options.js index 88cd359..509164e 100644 --- a/lib/options.js +++ b/lib/options.js @@ -465,7 +465,7 @@ const opts = { name: "Relay Agent Information", type: "SubOptions", config: "relayAgentInformation", - default: "cisco" + vendor: "cisco" }, 95: { name: 'LDAP Servers',