From 1a764103aa99eff0941169b4a28ebb3d74071eb0 Mon Sep 17 00:00:00 2001 From: Vladislav Bodnya Date: Fri, 14 Oct 2016 21:27:33 +0300 Subject: [PATCH 1/2] Resolve issue with default broadcast addresses --- lib/network.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/network.js b/lib/network.js index 22f5221..65e0523 100644 --- a/lib/network.js +++ b/lib/network.js @@ -94,8 +94,34 @@ Network.prototype.start = function (callback) { //Default to using broadcast if multicast address is not specified. self.socket.setBroadcast(true); - //TODO: get the default broadcast address from os.networkInterfaces() (not currently returned) - self.destination = [self.broadcast || "255.255.255.255"]; + /** + * Get broadcast addresses for all network interfaces instead 255.255.255.255 + * because of issues with broadcast using this method on Windows + */ + var networkInterfaces = os.networkInterfaces(), + broadcastAddresses = []; + + Object.keys(networkInterfaces).forEach(function (ifname) { + networkInterfaces[ifname].forEach(function (iface) { + if ('IPv4' !== iface.family || iface.internal !== false) { + // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses + return; + } + + var tabytes = (iface.address).split("."), + tsbytes = (iface.netmask).split("."); + + // Calculate Broadcast address + var tbaddr = ((tabytes[0] & tsbytes[0]) | (255 ^ tsbytes[0])) + "." + + ((tabytes[1] & tsbytes[1]) | (255 ^ tsbytes[1])) + "." + + ((tabytes[2] & tsbytes[2]) | (255 ^ tsbytes[2])) + "." + + ((tabytes[3] & tsbytes[3]) | (255 ^ tsbytes[3])); + + broadcastAddresses.push(tbaddr); + }); + }); + + self.destination = self.broadcast ? self.broadcast : broadcastAddrs; } else { try { From 0b4fa0c437656be252a69e94e655cf8cb17aa074 Mon Sep 17 00:00:00 2001 From: Vladislav Bodnya Date: Sat, 15 Oct 2016 21:36:34 +0300 Subject: [PATCH 2/2] Resolve issue with default broadcast addresses --- lib/network.js | 57 +++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/lib/network.js b/lib/network.js index 65e0523..433a5df 100644 --- a/lib/network.js +++ b/lib/network.js @@ -94,34 +94,39 @@ Network.prototype.start = function (callback) { //Default to using broadcast if multicast address is not specified. self.socket.setBroadcast(true); - /** - * Get broadcast addresses for all network interfaces instead 255.255.255.255 - * because of issues with broadcast using this method on Windows - */ - var networkInterfaces = os.networkInterfaces(), - broadcastAddresses = []; - - Object.keys(networkInterfaces).forEach(function (ifname) { - networkInterfaces[ifname].forEach(function (iface) { - if ('IPv4' !== iface.family || iface.internal !== false) { - // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses - return; - } - - var tabytes = (iface.address).split("."), - tsbytes = (iface.netmask).split("."); - - // Calculate Broadcast address - var tbaddr = ((tabytes[0] & tsbytes[0]) | (255 ^ tsbytes[0])) + "." - + ((tabytes[1] & tsbytes[1]) | (255 ^ tsbytes[1])) + "." - + ((tabytes[2] & tsbytes[2]) | (255 ^ tsbytes[2])) + "." - + ((tabytes[3] & tsbytes[3]) | (255 ^ tsbytes[3])); - - broadcastAddresses.push(tbaddr); + if (self.broadcast) { + self.destination = self.broadcast + } + else { + /** + * Get broadcast addresses for all network interfaces instead 255.255.255.255 + * because of issues with broadcast using this method on Windows + */ + var networkInterfaces = os.networkInterfaces(), + broadcastAddresses = []; + + Object.keys(networkInterfaces).forEach(function (ifname) { + networkInterfaces[ifname].forEach(function (iface) { + if ('IPv4' !== iface.family || iface.internal !== false) { + // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses + return; + } + + var tabytes = (iface.address).split("."), + tsbytes = (iface.netmask).split("."); + + // Calculate Broadcast address + var tbaddr = ((tabytes[0] & tsbytes[0]) | (255 ^ tsbytes[0])) + "." + + ((tabytes[1] & tsbytes[1]) | (255 ^ tsbytes[1])) + "." + + ((tabytes[2] & tsbytes[2]) | (255 ^ tsbytes[2])) + "." + + ((tabytes[3] & tsbytes[3]) | (255 ^ tsbytes[3])); + + broadcastAddresses.push(tbaddr); + }); }); - }); - self.destination = self.broadcast ? self.broadcast : broadcastAddrs; + self.destination = broadcastAddresses; + } } else { try {