What is ip-cidr?
The ip-cidr npm package provides utilities for working with IP addresses and CIDR (Classless Inter-Domain Routing) notations. It allows you to perform various operations such as checking if an IP address is within a CIDR range, converting CIDR notations to IP ranges, and more.
What are ip-cidr's main functionalities?
Check if an IP is within a CIDR range
This feature allows you to check if a given IP address is within a specified CIDR range. In this example, '192.168.1.5' is checked against the CIDR range '192.168.1.0/24'.
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const isInRange = cidr.contains('192.168.1.5');
console.log(isInRange); // true
Convert CIDR to IP range
This feature converts a CIDR notation to its corresponding IP range. In this example, '192.168.1.0/24' is converted to the range ['192.168.1.0', '192.168.1.255'].
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const range = cidr.toRange();
console.log(range); // ['192.168.1.0', '192.168.1.255']
Get the first and last IP addresses in a CIDR range
This feature retrieves the first and last IP addresses within a given CIDR range. In this example, '192.168.1.0/24' is used to get the first and last IP addresses.
const IPCIDR = require('ip-cidr');
const cidr = new IPCIDR('192.168.1.0/24');
const firstIp = cidr.start({ type: 'addressObject' });
const lastIp = cidr.end({ type: 'addressObject' });
console.log(firstIp); // { address: '192.168.1.0', subnetMask: 24 }
console.log(lastIp); // { address: '192.168.1.255', subnetMask: 24 }
Other packages similar to ip-cidr
ip
The 'ip' package provides a set of utilities for working with IP addresses, including parsing, formatting, and converting between different representations. It also supports CIDR notation but is more general-purpose compared to ip-cidr.
cidr-js
The 'cidr-js' package offers similar functionalities to ip-cidr, such as checking if an IP is within a CIDR range and converting CIDR to IP ranges. It is a lightweight alternative with a focus on CIDR operations.
netmask
The 'netmask' package is another utility for working with IP addresses and subnet masks. It provides methods for parsing and manipulating IP ranges and CIDR notations, similar to ip-cidr.
Install
npm install ip-cidr
About
Module for working with CIDR (v4, v6). Based on ip-address.
Example
const IPCIDR = require("ip-cidr");
const BigInteger = require("jsbn").BigInteger;
const address = "50.165.190.0/23";
if(!IPCIDR.isValidAddress(address)) {
return;
}
const cidr = new IPCIDR(address);
cidr.start();
cidr.end({ type: "bigInteger" });
cidr.loop(ip => console.log(ip), { type: "addressObject" });
cidr.toArray({ type: "bigInteger" });
cidr.toArray({ from: 1, limit: new BigInteger('2') });
cidr.toArray({ from: new BigInteger('1'), to: 3 });
cidr.toArray({ from: '50.165.190.1', to: '50.165.190.3' });
cidr.toRange();
Client side
Load /dist/ip-cidr.js as a script and you can get the library in window.IPCIDR
API
.formatIP(address, [options])
to return an "ip-address" module object in the necessary format
.isValidAddress(address)
to check the address is valid or not
.createAddress(address)
to create an object address from the string
.prototype.contains(address)
to check the address belongs to the range
.prototype.start([options])
to get the start ip address
.prototype.end([options])
to get the end ip address
.prototype.toString()
to convert the cidr to a string like "50.165.190.0/23"
.prototype.toRange([options])
to convert the cidr to an array with start and end ip addresses [startIp, endIp]
.prototype.toObject([options])
to convert the cidr to an object with start and end ip addresses {start: startIp, end: endIp}
.prototype.toArray([options], [results])
to convert the cidr to an array with all ip addresses in the range
you can get information by chunks using options.from/options.limit or options.from/options.to
you can pass the second argument "results" (object) to get all chunk pagination information
.prototype.loop(fn, [options], [results])
to run fn for each element of the range
you can use the same chunk options as in .toArray()