Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down