Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Created August 31, 2017 21:15
Show Gist options
  • Save d3x0r/315c87e8d2d65543d8689356d80f4c76 to your computer and use it in GitHub Desktop.
Save d3x0r/315c87e8d2d65543d8689356d80f4c76 to your computer and use it in GitHub Desktop.
Test udp subnet broadcast
const dgram = require( "dgram" );
var os = require("os");
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
calculateBroadcast(address);
addresses.push(address);
//console.log( "pushed an address from networkInterfaces..." );
}
}
const port = 3213;
var Servers = [];
var connecting = 0;
addresses.forEach( (addr)=>{
if( addr.family == 'IPv6' ) return;
var listener = dgram.createSocket({
type: 'udp4',
reuseAddr: true
});
listener.address = addr;
console.log( new Date(), "Bind to:", addr.address );
listener.bind({
address: addr.address,
port: port,
exclusive: false
});
connecting++;
listener.on( 'message', (msg,rinfo)=>{
console.log( "received message:", msg, "from", rinfo.address );
} );
listener.on( 'listening', ()=>{
console.log( new Date(), "listening...", listener.address.broadcast );
listener.setBroadcast(true);
connecting--;
if( !connecting ) ping();
} );
Servers.push( listener );
} );
var pings = 0;
function ping() {
pings++;
if( pings == 4 )
process.exit(0)
Servers.forEach((server) => {
console.log("Send to ", server.address.broadcast)
var msg = "Message Data";
server.send(msg, 0, msg.length, port, server.address.broadcast);
});
setTimeout( ping, 1000 );
}
function calculateBroadcast(addr) {
if (addr.family == "IPv4") {
var mask = [];
var addrNum = [];
var stub = [255, 255, 255, 255];
var addrPart = [];
var addrPartNot = [];
var d = addr.netmask.split('.');
mask[0] = Number(d[0])
mask[1] = Number(d[1])
mask[2] = Number(d[2])
mask[3] = Number(d[3])
var d = addr.address.split('.');
addrNum[0] = Number(d[0])
addrNum[1] = Number(d[1])
addrNum[2] = Number(d[2])
addrNum[3] = Number(d[3])
addrPart[0] = addrNum[0] & mask[0];
addrPart[1] = addrNum[1] & mask[1];
addrPart[2] = addrNum[2] & mask[2];
addrPart[3] = addrNum[3] & mask[3];
stub[0] &= ~mask[0];
stub[1] &= ~mask[1];
stub[2] &= ~mask[2];
stub[3] &= ~mask[3];
addrPart[0] |= stub[0];
addrPart[1] |= stub[1];
addrPart[2] |= stub[2];
addrPart[3] |= stub[3];
addr.broadcast = `${addrPart[0]}.${addrPart[1]}.${addrPart[2]}.${addrPart[3]}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment