Concise and correct way to calculate the broadcast address for a particular network interface. You can calculate the broadcast address by doing a bitwise OR over the bit complements of the subnet mask and the selected IP address.
$ npm install --save broadcast-address
broadcastAddress
takes two parameters of which one is optional (address
).
interface
of type {string}: define for which network interface you'd like a broadcast address.address
of type {string}: define for which specific address on thatinterface
you'd like to calculate the broadcast address from. Picks the first IPv4 address on the network interface if none is given.
The function can also throw errors, namely:
- Unknown network interface (xyz). of type {Error}: when an unknown network interface was given.
- No address info found. Specify a valid address. of type {Error}: when no address info could be selected on that interface.
const broadcastAddress = require('broadcast-address');
broadcastAddress('lo0'); //-> 127.255.255.255
broadcastAddress('lo0', '127.0.0.1'); //-> 127.255.255.255
broadcastAddress('en1'); //-> 192.168.178.255
Note: the output is just exemplary.
Included is an index.d.ts
file, Typescript should automatically pick this file up and apply the typings across your codebase.
import * as broadcastAddress from "broadcast-address";
broadcastAddress("lo0"); //-> 127.255.255.255
broadcastAddress("lo0", "127.0.0.1"); //-> 127.255.255.255
broadcastAddress("en1"); //-> 192.168.178.255
Note: the output is just exemplary.
Copyright © 2018, Alex Burghardt. Made available under the MIT license.