Class: Cidr

Cidr()

CIDR library

PURPOSE: Provides methods for working with CIDRs.

The key methods it provides are:

  • doSubnetsOverlap() - Pass it two subnet strings, and it tells you whether they overlap
  • sortCidrByBinary() - Use this as your sort function's callback and you can sort subnets by their binary representation

Another public method some may find useful is:

  • getBinaryRepresentation() - Returns the binary representation of a CIDR string.

A CIDR is a Classless Inter-Domain Routing address. See https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing.

In a bit more detail, a CIDR is an IP address with an optional range suffix appended after a forward slash. The range is the number of digits (bits, counted from left to right) in the binary representation of the IP address to count as part of that CIDR's prefix. When the range suffix is present, only the prefix (and not the rest of the IP address's) digits are considered to be significant. The result is the prefix represents a range of IP addresses, which is called a subnet.

USAGE:

To use this library, import and instantiate the Cidr class, then call thedoSubnetsOverlap method with two CIDRs, like this:

import { Cidr } from './cidr.js';

let cidr = new Cidr();

let overlaps = cidr.doSubnetsOverlap('11.1.1.2/21', '11.1.1.1/20');

Note: In the future I might use a different library in the app for which I wrote this one. A couple far more full-featured libraries are below:

Constructor

new Cidr()

Constructor

Source:

Classes

Cidr

Methods

_getBinaryPrefix(cidr, prefix_length) → {string}

Convert one subnet to the binary representation of its IP address, truncated to its CIDR prefix length. If the cidr is incomplete or specifies no prefix length, assume it is the left-most portion of the CIDR, and consider its binary length to be its prefix_length.

Parameters:
Name Type Description
cidr string

The CIDR whose binary prefix should be returned

prefix_length number

The integer length of the CIDR's binary prefix

Source:
Returns:

The CIDR's binary prefix

Type
string

_getClasses(cidr) → {array}

Get the CIDR's classes as an array of classes

Parameters:
Name Type Description
cidr string

The CIDR whose classes should be returned

Source:
Returns:

An array of the classes in the CIDR

Type
array

_getPrefixesOfShortestEqualLength(first_cidr, second_cidr) → {array}

Convert subnets to their IP addresses' binary representations, truncated to the shortest of their two CIDR prefix lengths

Parameters:
Name Type Description
first_cidr string

The first CIDR from which to get a prefix

second_cidr string

The second CIDR from which to get a prefix

Source:
Returns:

An array containing the binary representations of the two input CIDRs, truncated to the shortest of their two prefix lengths

Type
array

_getPrefixLength(cidr) → {number}

Get the CIDR's prefix length as an integer. Handle incomplete cidrs by counting their binary length as their prefix_length.

Parameters:
Name Type Description
cidr string

The CIDR whose prefix length should be returned

Source:
Returns:

The CIDR's prefix length as an integer

Type
number

doSubnetsOverlap(first_cidr, second_cidr) → {boolean}

Determine if the new subnet overlaps the existing subnet

Parameters:
Name Type Description
first_cidr string

The first CIDR to compare

second_cidr string

The second CIDR to compare

Source:
Returns:
Type
boolean

getBinaryRepresentation(cidr) → {string}

Get the CIDR's binary representation including both its prefix and suffix

Parameters:
Name Type Description
cidr string

The CIDR whose binary representation should be returned

Source:
Returns:

The CIDR's binary representation

Type
string

sortCidrByBinary(a, b) → {number}

Sort CIDRs by their binary representation

Parameters:
Name Type Description
a string

The first CIDR to sort

b string

The second CIDR to sort

Source:
Returns:

1 means sort a before b; 0 means they sort to the same level; -1 means sort a after b

Type
number