From 95b5f7f07308faf0d8acd7b27bac2d8de8dbc486 Mon Sep 17 00:00:00 2001 From: Keyvan Fatehi Date: Sat, 16 Jan 2021 09:31:47 -0800 Subject: [PATCH 1/3] Add "refresh" feature --- dist/index.d.ts | 13 +++++++++++++ dist/index.js | 20 ++++++++++++++++++++ lib/index.ts | 23 +++++++++++++++++++++++ test.js | 1 + 4 files changed, 57 insertions(+) diff --git a/dist/index.d.ts b/dist/index.d.ts index e2f1b34..15714dc 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -22,4 +22,17 @@ export declare class NodeMndp extends events.EventEmitter { * Initalize Listeners */ private registerListeners(); + /** + * Instigate responses from neighbors. + * + * According to Wireshark on Windows, pressing the Refresh button on Mikrotik's + * WinBox tool causes it to send three packets: + * 1. to 255.255.255.255 + * 2. to your subnet's broadcast address, e.g. 192.168.1.255 + * 3. to 239.255.255.255 + * The first one works but it causes the server to pick up the packet. + * The second one I am not sure how to correctly acquire the IP for, so I skipped. + * The third seems to work best because it works without causing our server to react. + */ + refresh(portOverride?: null): void; } diff --git a/dist/index.js b/dist/index.js index c3e1a78..4f947e2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -55,5 +55,25 @@ class NodeMndp extends events.EventEmitter { this.server.setBroadcast(true); }); } + /** + * Instigate responses from neighbors. + * + * According to Wireshark on Windows, pressing the Refresh button on Mikrotik's + * WinBox tool causes it to send three packets: + * 1. to 255.255.255.255 + * 2. to your subnet's broadcast address, e.g. 192.168.1.255 + * 3. to 239.255.255.255 + * The first one works but it causes the server to pick up the packet. + * The second one I am not sure how to correctly acquire the IP for, so I skipped. + * The third seems to work best because it works without causing our server to react. + */ + refresh(portOverride = null) { + if (!this.started) + return; + let port = portOverride || this.port; + let buf = Buffer.alloc(4); + buf.fill(0); + this.server.send(buf, 0, 4, port, '239.255.255.255'); + } } exports.NodeMndp = NodeMndp; diff --git a/lib/index.ts b/lib/index.ts index 69fbf48..8b06ea4 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -78,4 +78,27 @@ export class NodeMndp extends events.EventEmitter { this.server.setBroadcast(true); }) } + + /** + * Instigate responses from neighbors. + * + * According to Wireshark on Windows, pressing the Refresh button on Mikrotik's + * WinBox tool causes it to send three packets: + * 1. to 255.255.255.255 + * 2. to your subnet's broadcast address, e.g. 192.168.1.255 + * 3. to 239.255.255.255 + * The first one works but it causes the server to pick up the packet. + * The second one I am not sure how to correctly acquire the IP for, so I skipped. + * The third seems to work best because it works without causing our server to react. + */ + refresh(portOverride=null): void + { + if (!this.started) + return; + + let port = portOverride || this.port; + let buf = Buffer.alloc(4); + buf.fill(0); + this.server.send(buf, 0, 4, port, '239.255.255.255'); + } } \ No newline at end of file diff --git a/test.js b/test.js index fd64e64..f57088f 100644 --- a/test.js +++ b/test.js @@ -11,6 +11,7 @@ test.on('deviceFound', (output) => { test.on('started', (output) => { console.log(output); + test.refresh(); }); test.on('error', (output) => { From 06dd30fe45a3f85db78a38df5aba5243cfc59844 Mon Sep 17 00:00:00 2001 From: Keyvan Fatehi Date: Sat, 16 Jan 2021 09:57:25 -0800 Subject: [PATCH 2/3] Add missing attributes Thanks to the wireshark dissector https://gitlab.com/wireshark/wireshark/-/blob/master/epan/dissectors/packet-mndp.c --- dist/Discovery.js | 14 +++++++++++++- dist/interfaces/device.d.ts | 3 +++ lib/Discovery.ts | 14 +++++++++++++- lib/interfaces/device.ts | 3 +++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dist/Discovery.js b/dist/Discovery.js index 41e9481..c1214e2 100644 --- a/dist/Discovery.js +++ b/dist/Discovery.js @@ -25,7 +25,10 @@ class Discovery { version: '', platform: '', uptime: 0, - board: '' + softwareId: '', + board: '', + unpack: 0, + interfaceName: '' }; /** * Mikrotik FirstByte starts at 8 @@ -57,9 +60,18 @@ class Discovery { case 10: // uptime device.uptime = Buffer.from(this.msg.slice(offset, offset + attrLength)).readUInt32LE(0); break; + case 11: // software id + device.softwareId = format_1.bin2String(this.msg.subarray(offset, offset + attrLength)); + break; case 12: // board device.board = format_1.bin2String(this.msg.subarray(offset, offset + attrLength)); break; + case 14: // unpack (discovery packet compresson type) (none|simple|uncompressed-headers|uncompressed-all) + device.unpack = Buffer.from(this.msg.slice(offset, offset + attrLength)).readInt8(0); + break; + case 16: // interface name + device.interfaceName = format_1.bin2String(this.msg.subarray(offset, offset + attrLength)); + break; default: // unknown type console.debug('unknown mndp message type', attrType); break; diff --git a/dist/interfaces/device.d.ts b/dist/interfaces/device.d.ts index a38c680..bb38388 100644 --- a/dist/interfaces/device.d.ts +++ b/dist/interfaces/device.d.ts @@ -5,5 +5,8 @@ export interface Device { identity: string; platform: string; uptime: number; + softwareId: string; board: string; + unpack: number; + interfaceName: string; } diff --git a/lib/Discovery.ts b/lib/Discovery.ts index d6e51f6..be73656 100644 --- a/lib/Discovery.ts +++ b/lib/Discovery.ts @@ -32,7 +32,10 @@ export class Discovery { version: '', platform: '', uptime: 0, - board: '' + softwareId: '', + board: '', + unpack: 0, + interfaceName: '' }; /** @@ -67,9 +70,18 @@ export class Discovery { case 10: // uptime device.uptime = Buffer.from(this.msg.slice(offset, offset + attrLength)).readUInt32LE(0); break; + case 11: // software id + device.softwareId = bin2String(this.msg.subarray(offset, offset + attrLength)); + break; case 12: // board device.board = bin2String(this.msg.subarray(offset, offset + attrLength)); break; + case 14: // unpack (discovery packet compresson type) (none|simple|uncompressed-headers|uncompressed-all) + device.unpack = Buffer.from(this.msg.slice(offset, offset + attrLength)).readInt8(0); + break; + case 16: // interface name + device.interfaceName = bin2String(this.msg.subarray(offset, offset + attrLength)); + break; default: // unknown type console.debug('unknown mndp message type', attrType); break; diff --git a/lib/interfaces/device.ts b/lib/interfaces/device.ts index 65d4130..abdb69d 100644 --- a/lib/interfaces/device.ts +++ b/lib/interfaces/device.ts @@ -5,5 +5,8 @@ export interface Device { identity: string; platform: string; uptime: number; + softwareId: string; board: string; + unpack: number; + interfaceName: string; } \ No newline at end of file From c9b69b3fdd95035513a817dc721f9024b825817c Mon Sep 17 00:00:00 2001 From: Keyvan Fatehi Date: Sat, 16 Jan 2021 09:57:44 -0800 Subject: [PATCH 3/3] Improve the README --- readme.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index c6b660a..a684aef 100644 --- a/readme.md +++ b/readme.md @@ -3,11 +3,12 @@ This is an implementation written in Node +NOTE: The library does **not** automatically send a "refresh" packet to cause neighbors to announce themselves, but the function is implemented and available in the API as `refresh()`. ### Usage Example ``` -var NodeMndp = require('node-mndp'); -var discovery = new NodeMndp({ +var mndp = require('node-mndp').NodeMndp; +var discovery = new mndp({ port: 5678 }); @@ -20,8 +21,8 @@ discovery.start(); Ipv6 Example ``` -var NodeMndp = require('node-mndp'); -var discovery = new NodeMndp({ +var mndp = require('node-mndp').NodeMndp; +var discovery = new mndp({ port: 5678, host: "::", version: "udp6" @@ -35,8 +36,8 @@ discovery.start(); ``` ### API ``` -var NodeMndp = require('node-mndp'); -var discovery = new NodeMndp({ +var mndp = require('node-mndp').NodeMndp; +var discovery = new mndp({ port: 5678 }); ``` @@ -54,6 +55,8 @@ options { #### discovery.stop() -> void +#### discovery.refresh() -> void + #### Event: 'deviceFound' ``` @@ -63,6 +66,12 @@ Output: "macAddress":"aabbccddeeff", "identity":"Mikrotik", "version":"6.41.2 (stable)" + "platform":"MikroTik", + "uptime":12190, + "softwareId":"8C0S-DDXE", + "board":"RB2011UiAS-2HnD", + "unpack":0, + "interfaceName":"LAN_Bridge/ether2" } ```