diff --git a/lib/network.js b/lib/network.js index 22f5221..433a5df 100644 --- a/lib/network.js +++ b/lib/network.js @@ -94,8 +94,39 @@ 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"]; + 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 = broadcastAddresses; + } } else { try {