Skip to content

Commit 78515a3

Browse files
committed
Send broadcast messages to all interfaces
Look up all available interfaces and build an array of destinations for each for broadcast.
1 parent 0563d99 commit 78515a3

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

lib/network.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var dgram = require('dgram'),
22
crypto = require('crypto'),
33
os = require('os'),
4+
broadcastAddress = require("broadcast-address"),
45
EventEmitter = require('events').EventEmitter,
56
util = require('util'),
67
uuid = require('uuid/v4'),
@@ -9,8 +10,8 @@ var dgram = require('dgram'),
910
var procUuid = uuid();
1011
var hostName = process.env.DISCOVERY_HOSTNAME || os.hostname();
1112

12-
module.exports = Network;
13-
13+
module.exports = Network;
14+
1415
function Network (options) {
1516
if (!(this instanceof Network)) {
1617
return new Network(options, callback);
@@ -96,7 +97,29 @@ Network.prototype.start = function (callback) {
9697
self.socket.setBroadcast(true);
9798

9899
//TODO: get the default broadcast address from os.networkInterfaces() (not currently returned)
99-
self.destination = [self.broadcast || "255.255.255.255"];
100+
101+
if (self.broadcast) {
102+
self.destination = Array.isArray( self.broadcast) ? self.broadcast.slice(0) : [self.broadcast];
103+
} else {
104+
self.destination = [];
105+
var networkInterfaces = os.networkInterfaces();
106+
for (var interfaceName in networkInterfaces) {
107+
if (!networkInterfaces.hasOwnProperty(interfaceName)) {
108+
continue;
109+
}
110+
for (var i = 0; i < networkInterfaces[interfaceName].length; i++) {
111+
var iface = networkInterfaces[interfaceName][i];
112+
if (iface.internal || iface.family !== "IPv4") {
113+
continue;
114+
}
115+
116+
var broadcast = broadcastAddress(interfaceName, iface.address);
117+
if (broadcast) {
118+
self.destination.push(broadcast);
119+
}
120+
}
121+
}
122+
}
100123
}
101124
else {
102125
try {
@@ -149,7 +172,7 @@ Network.prototype.send = function (event) {
149172
}
150173

151174
var msg = Buffer.from(contents);
152-
175+
153176
self.destination.forEach(function (destination) {
154177
self.socket.send(
155178
msg

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"node": ">=0.4.1 <0.5.0 || >=0.6.9"
2020
},
2121
"dependencies": {
22+
"broadcast-address": "^1.0.2",
2223
"uuid": "^3.3.2"
2324
},
2425
"bugs": {

0 commit comments

Comments
 (0)