From dc809cb002a2b8a583e0bed7b1f6ce80f4eebc74 Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Thu, 12 May 2016 01:24:30 +0100 Subject: [PATCH 1/6] begin implementation of com port options --- lib/telnet.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/telnet.js b/lib/telnet.js index c2e53d5..7fd36e9 100644 --- a/lib/telnet.js +++ b/lib/telnet.js @@ -676,6 +676,48 @@ Client.prototype.terminal_type = function(cmd) { return i; }; +Client.prototype.com_port_option = function(cmd) { + var data = cmd.data; + var i = 0; + if (data.length < 7) return -1; + var iac1 = data.readUInt8(i); + i += 1; + var sb = data.readUInt8(i); + i += 1; + var option = data.readUInt8(i); + i += 1; + var command = data.readUInt8(i); + i += 1; + var commands = []; + commands[1] = { length: 4, name: "baudrate" }; + commands[2] = { length: 1, name: "datasize" }; + commands[3] = { length: 1, name: "parity" }; + commands[4] = { length: 1, name: "stopsize" }; + commands[5] = { length: 1, name: "control" }; + if(typeof commands[command] === "undefined") return -1; + var value; + if(commands[command].length === 4) + value = data.readUInt32BE(i); + else if (commands[command].length === 1) + value = data.readUInt8(i); + i += commands[command].length; + var iac2 = data.readUInt8(i); + i += 1; + var se = data.readUInt8(i); + i += 1; + assert(iac1 === COMMANDS.IAC); + assert(sb === COMMANDS.SB); + assert(option === OPTIONS.COM_PORT_OPTION); + assert(Number.isInteger(value) && value >= 0); + assert(iac2 === COMMANDS.IAC); + assert(se === COMMANDS.SE); + this.emit("com port option", { + command: commands[command].name, + value: value + }); + return i; +}; + Client.prototype._setRawMode = function(mode) { this.isRaw = mode; if (!this.writable) return; From bd7aa21253b43b2ce16d4125daf7c3e10237ef9b Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Thu, 12 May 2016 10:15:37 +0100 Subject: [PATCH 2/6] refactor --- lib/telnet.js | 70 ++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/lib/telnet.js b/lib/telnet.js index 7fd36e9..05e671b 100644 --- a/lib/telnet.js +++ b/lib/telnet.js @@ -116,11 +116,7 @@ var OPTIONS = { EXOPL: 255 // http://tools.ietf.org/html/rfc861 }; -var OPTION_NAMES = Object.keys(OPTIONS).reduce(function(out, key) { - var value = OPTIONS[key]; - out[value] = key.toLowerCase(); - return out; -}, {}); +var OPTION_NAMES = generateReverseLookup(OPTIONS); var SUB = { IS: 0, @@ -132,6 +128,23 @@ var SUB = { USER_VARIABLE: 3 }; +var COM_PORT_OPTIONS = { + BAUDRATE: 1, + DATASIZE: 2, + PARITY: 3, + STOPSIZE: 4, + CONTROL: 5 +}; + +var COM_PORT_OPTION_NAMES = generateReverseLookup(COM_PORT_OPTIONS); + +function generateReverseLookup(table) { + return Object.keys(table).reduce(function(out, key) { + var value = table[key]; + out[value] = key.toLowerCase(); + return out; + }, {}); +} /** * Client */ @@ -680,31 +693,26 @@ Client.prototype.com_port_option = function(cmd) { var data = cmd.data; var i = 0; if (data.length < 7) return -1; - var iac1 = data.readUInt8(i); - i += 1; - var sb = data.readUInt8(i); - i += 1; - var option = data.readUInt8(i); - i += 1; - var command = data.readUInt8(i); - i += 1; - var commands = []; - commands[1] = { length: 4, name: "baudrate" }; - commands[2] = { length: 1, name: "datasize" }; - commands[3] = { length: 1, name: "parity" }; - commands[4] = { length: 1, name: "stopsize" }; - commands[5] = { length: 1, name: "control" }; - if(typeof commands[command] === "undefined") return -1; - var value; - if(commands[command].length === 4) - value = data.readUInt32BE(i); - else if (commands[command].length === 1) - value = data.readUInt8(i); - i += commands[command].length; - var iac2 = data.readUInt8(i); - i += 1; - var se = data.readUInt8(i); - i += 1; + var iac1 = data.readUInt8(i++); + var sb = data.readUInt8(i++); + var option = data.readUInt8(i++); + var command = data.readUInt8(i++); + var valueBytes = ""; + for (var s = i; i < data.length; i++) { + if (data[i] === COMMANDS.IAC) { + valueBytes = data.toString('binary', s, i); + break; + } + } + if(valueBytes.length === 0 || valueBytes.length > 4) + return -1; + if(typeof COM_PORT_OPTION_NAMES[command] === "undefined") + return -1; + // pad to 4 bytes + valueBytes = ("\x00\x00\x00\x00" + valueBytes).slice(-4); + var value = Buffer(valueBytes).readUInt32BE(); + var iac2 = data.readUInt8(i++); + var se = data.readUInt8(i++); assert(iac1 === COMMANDS.IAC); assert(sb === COMMANDS.SB); assert(option === OPTIONS.COM_PORT_OPTION); @@ -712,7 +720,7 @@ Client.prototype.com_port_option = function(cmd) { assert(iac2 === COMMANDS.IAC); assert(se === COMMANDS.SE); this.emit("com port option", { - command: commands[command].name, + name: COM_PORT_OPTION_NAMES[command], value: value }); return i; From 2bebe00b432f48ec6b804515726f5a0057c7f46a Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Thu, 12 May 2016 11:03:59 +0100 Subject: [PATCH 3/6] update readme --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 64249cc..fdde140 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ telnet features. | Echo | `'echo'` | [RFC857](http://tools.ietf.org/html/rfc857) | Suppress Go Ahead | `'suppress go ahead'` | [RFC858](http://tools.ietf.org/html/rfc858) | Window Size | `'window size'` | [RFC1073](http://tools.ietf.org/html/rfc1073) - +| Com Port Option | `'com port option'` | [RFC1073](http://tools.ietf.org/html/rfc2217) Installation ------------ @@ -49,6 +49,11 @@ telnet.createServer(function (client) { } }) + // listen for com port option events from the client + client.on('com port option', function (c) { + console.log('com port set %s to %d', c.name, c.value) + }) + // listen for the actual data from the client client.on('data', function (b) { client.write(b) From b895023e409a22d6f7ac3eb334d8c61708277b39 Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Thu, 12 May 2016 11:16:08 +0100 Subject: [PATCH 4/6] cp error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fdde140..ffb19a2 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ telnet features. | Echo | `'echo'` | [RFC857](http://tools.ietf.org/html/rfc857) | Suppress Go Ahead | `'suppress go ahead'` | [RFC858](http://tools.ietf.org/html/rfc858) | Window Size | `'window size'` | [RFC1073](http://tools.ietf.org/html/rfc1073) -| Com Port Option | `'com port option'` | [RFC1073](http://tools.ietf.org/html/rfc2217) +| Com Port Option | `'com port option'` | [RFC2217](http://tools.ietf.org/html/rfc2217) Installation ------------ From 0fecf0eb0a7677851f66a203b849defe90621cbf Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Thu, 12 May 2016 11:40:52 +0100 Subject: [PATCH 5/6] investigate ci fail --- test/test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 29b66cc..f17afb5 100644 --- a/test/test.js +++ b/test/test.js @@ -24,7 +24,7 @@ describe('telnet', function () { done(); }); }); - + /* after(function (done) { client.end(function () { client = null; @@ -34,7 +34,7 @@ describe('telnet', function () { }); }); }); - + */ it('should be listening on port ' + port, function (done) { client = net.connect({port: port}, function () { done(); From f6fc76b2a0f71c1d67d5af4558033efcd87190ee Mon Sep 17 00:00:00 2001 From: Andy Bartelsmeier Date: Fri, 13 May 2016 16:13:36 +0100 Subject: [PATCH 6/6] fix padding --- lib/telnet.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/telnet.js b/lib/telnet.js index 05e671b..b0be9c8 100644 --- a/lib/telnet.js +++ b/lib/telnet.js @@ -700,7 +700,7 @@ Client.prototype.com_port_option = function(cmd) { var valueBytes = ""; for (var s = i; i < data.length; i++) { if (data[i] === COMMANDS.IAC) { - valueBytes = data.toString('binary', s, i); + valueBytes = data.slice(s, i); break; } } @@ -709,8 +709,8 @@ Client.prototype.com_port_option = function(cmd) { if(typeof COM_PORT_OPTION_NAMES[command] === "undefined") return -1; // pad to 4 bytes - valueBytes = ("\x00\x00\x00\x00" + valueBytes).slice(-4); - var value = Buffer(valueBytes).readUInt32BE(); + valueBytes = Buffer.concat([Buffer([0,0,0]), valueBytes]).slice(-4); + var value = valueBytes.readUInt32BE(); var iac2 = data.readUInt8(i++); var se = data.readUInt8(i++); assert(iac1 === COMMANDS.IAC);