Use active broadcast address
This commit is contained in:
parent
e5f95105af
commit
d095be762e
1 changed files with 15 additions and 29 deletions
|
|
@ -1,39 +1,25 @@
|
|||
const os = require('os')
|
||||
|
||||
function getBroadcastAddress () {
|
||||
const networkInterfaces = os.networkInterfaces()
|
||||
let broadcastAddress = null
|
||||
function getBroadcastAddress() {
|
||||
const interfaces = os.networkInterfaces();
|
||||
|
||||
for (const interfaceName in networkInterfaces) {
|
||||
const interfaces = networkInterfaces[interfaceName]
|
||||
for (const iface of interfaces) {
|
||||
for (const interfaceName in interfaces) {
|
||||
for (const iface of interfaces[interfaceName]) {
|
||||
// Only consider IPv4, non-internal (non-loopback) addresses
|
||||
if (iface.family === 'IPv4' && !iface.internal) {
|
||||
const ipAddress = iface.address
|
||||
const netmask = iface.netmask
|
||||
const ip = iface.address.split('.').map(Number);
|
||||
const netmask = iface.netmask.split('.').map(Number);
|
||||
const broadcast = ip.map((octet, i) => (octet | (~netmask[i] & 255)));
|
||||
|
||||
broadcastAddress = calculateBroadcastAddress(ipAddress, netmask)
|
||||
console.log(`Active Interface: ${interfaceName}`);
|
||||
console.log(`IP Address: ${iface.address}`);
|
||||
console.log(`Netmask: ${iface.netmask}`);
|
||||
console.log(`Broadcast Address: ${broadcast.join('.')}`);
|
||||
|
||||
return broadcast.join('.'); // Return the broadcast address
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return broadcastAddress
|
||||
}
|
||||
|
||||
function calculateBroadcastAddress (ipAddress, netmask) {
|
||||
const ipParts = ipAddress.split('.').map(Number)
|
||||
const maskParts = netmask.split('.').map(Number)
|
||||
|
||||
const ip = (ipParts[0] << 24) | (ipParts[1] << 16) | (ipParts[2] << 8) | ipParts[3]
|
||||
const mask = (maskParts[0] << 24) | (maskParts[1] << 16) | (maskParts[2] << 8) | maskParts[3]
|
||||
|
||||
const broadcast = ip | (~mask >>> 0)
|
||||
|
||||
return [
|
||||
(broadcast >>> 24) & 0xff,
|
||||
(broadcast >>> 16) & 0xff,
|
||||
(broadcast >>> 8) & 0xff,
|
||||
broadcast & 0xff
|
||||
].join('.')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue