netaddr.js
Node.js utility package for working with IP addresses and CIDR ranges.
Installation
$ npm install netaddr --save
Usage
var Addr = Addr;// Create a fixed IP address from a stringvar localhost = ;// Create CIDR range from a stringvar subnet = ;// Create a CIDR range from an integer and prefixvar subnet = ;// Convert an IP address to an integervar intval = ;// Get the network addressvar network = subnet;// Get the broadcast addressvar broadcast = subnet;// Mask the address with a new prefixvar masked = subnet;// Check if one CIDR contains another CIDR or IPsubnet; // true// Check where two CIDRs intersectsubnet;// Increment an address (non-mutating)localhost;// Decrement an address (non-mutating)localhost;// Find the next adjacent subnetsubnetnextSibling;// Find the previous adjacent subnetsubnet;
Attempts to create an invalid Addr
will throw an Error
.
Limitations
Currently only IPv4 is supported, though there are plans to add IPv6 support. This is complicated by the fact JavaScript does not support 64-bit integers, let alone 128-bit integers.
Copyright & License
Copyright © 2015 Chris Corbyn. See the LICENSE file for details.