cidr-block - v2.1.1
    Preparing search index...

    Class Ipv4Cidr

    Represents an IPv4 CIDR block with utility methods for subnet operations.

    While you can instantiate this class directly, it is recommended to use the ipv4.cidr shorthand method from the ipv4 namespace instead.

    import { ipv4 } from 'cidr-block';

    // Recommended: use the ipv4 namespace shorthand
    const cidr1 = ipv4.cidr("192.168.0.0/24");
    const cidr2 = ipv4.cidr({ address: "10.0.0.0", range: 8 });
    const cidr3 = ipv4.cidr([[172, 16, 0, 0], 12]);
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.0.0/24");
    cidr.baseAddress().toString(); // "192.168.0.0"
    cidr.range(); // 24
    cidr.netmask().toString(); // "255.255.255.0"
    cidr.addressCount(); // 256
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.0.0/24");
    cidr.getFirstUsableAddress()?.toString(); // "192.168.0.1"
    cidr.getLastUsableAddress()?.toString(); // "192.168.0.254"
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.1.0/30");
    for (const addr of cidr.addresses()) {
    console.log(addr.toString());
    }
    // "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.0.0/24");
    cidr.includes(ipv4.address("192.168.0.100")); // true
    cidr.includes(ipv4.address("192.168.1.1")); // false
    cidr.overlaps("192.168.0.0/25"); // true
    cidr.overlaps("10.0.0.0/8"); // false
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.0.0/24");

    // Split into equal /26 subnets
    cidr.subnet(26).map(s => s.toString());
    // ["192.168.0.0/26", "192.168.0.64/26", "192.168.0.128/26", "192.168.0.192/26"]

    // Split by specific ranges
    cidr.subnetBy([25, 26, 27, 27]).map(s => s.toString());
    // ["192.168.0.0/25", "192.168.0.128/26", "192.168.0.192/27", "192.168.0.224/27"]
    import { ipv4 } from 'cidr-block';

    const cidr = ipv4.cidr("192.168.0.0/24");
    cidr.nextCIDR()?.toString(); // "192.168.1.0/24"
    cidr.previousCIDR()?.toString(); // "192.167.255.0/24"
    Index

    Constructors

    Methods

    • Calculates the total number of addresses in the CIDR range.

      Returns number

      The total number of addresses in the CIDR range.

      import { ipv4 } from 'cidr-block';

      ipv4.cidr("192.168.0.0/24").addressCount(); // 256
      ipv4.cidr("10.0.0.0/8").addressCount(); // 16777216
      ipv4.cidr("192.168.1.0/32").addressCount(); // 1
    • Generates all IPv4 addresses within the CIDR range.

      Returns Generator<Ipv4Address>

      A generator that yields each IPv4 address in the CIDR range.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.1.0/30");
      for (const addr of cidr.addresses()) {
      console.log(addr.toString());
      }
      // Output: "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3"
    • Gets the base IPv4 address of the CIDR.

      Returns Ipv4Address

      The base IPv4 address.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.baseAddress().toString(); // "192.168.0.0"
    • Checks if this CIDR is equal to another CIDR.

      Parameters

      Returns boolean

      True if both CIDRs have the same base address and range; otherwise, false.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.equals("192.168.0.0/24"); // true
      cidr.equals({ address: "192.168.0.0", range: 24 }); // true
      cidr.equals("10.0.0.0/8"); // false
    • Gets the first usable address in the CIDR range.

      Returns Ipv4Address | undefined

      The first usable IPv4 address, or undefined if the range is /32.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.getFirstUsableAddress()?.toString(); // "192.168.0.1"

      const hostCidr = ipv4.cidr("192.168.1.1/32");
      hostCidr.getFirstUsableAddress(); // undefined (no usable addresses in /32)
    • Gets the last usable address in the CIDR range.

      Returns Ipv4Address | undefined

      The last usable IPv4 address, or undefined if the range is /32.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.getLastUsableAddress()?.toString(); // "192.168.0.254"

      const hostCidr = ipv4.cidr("192.168.1.1/32");
      hostCidr.getLastUsableAddress(); // undefined (no usable addresses in /32)
    • Checks if there is a next sequential CIDR.

      Returns boolean

      true if there is a next CIDR.

      import { ipv4 } from 'cidr-block';

      ipv4.cidr("192.168.0.0/24").hasNextCIDR(); // true
      ipv4.cidr("255.255.255.0/24").hasNextCIDR(); // false
    • Checks if there is a previous sequential CIDR.

      Returns boolean

      true if there is a previous CIDR.

      import { ipv4 } from 'cidr-block';

      ipv4.cidr("192.168.1.0/24").hasPreviousCIDR(); // true
      ipv4.cidr("0.0.0.0/24").hasPreviousCIDR(); // false
    • Checks if the given IPv4 address is within the CIDR range.

      Parameters

      Returns boolean

      True if the address is within the CIDR range; otherwise, false.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.includes(ipv4.address("192.168.0.100")); // true
      cidr.includes(ipv4.address("192.168.1.1")); // false
    • Calculates the netmask for the CIDR range.

      Returns Ipv4Address

      The netmask as an Ipv4Address.

      import { ipv4 } from 'cidr-block';

      ipv4.cidr("192.168.0.0/24").netmask().toString(); // "255.255.255.0"
      ipv4.cidr("10.0.0.0/8").netmask().toString(); // "255.0.0.0"
      ipv4.cidr("172.16.0.0/16").netmask().toString(); // "255.255.0.0"
    • Gets the next sequential CIDR.

      Returns Ipv4Cidr | undefined

      The next CIDR, or undefined if there is no next CIDR.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.nextCIDR()?.toString(); // "192.168.1.0/24"

      const lastCidr = ipv4.cidr("255.255.255.0/24");
      lastCidr.nextCIDR(); // undefined
    • Checks if this CIDR overlaps with another CIDR.

      Parameters

      Returns boolean

      True if the CIDRs overlap; otherwise, false.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.overlaps("192.168.0.0/25"); // true (subset)
      cidr.overlaps("192.168.0.128/25"); // true (partial overlap)
      cidr.overlaps("10.0.0.0/8"); // false (no overlap)
    • Gets the previous sequential CIDR.

      Returns Ipv4Cidr | undefined

      The previous CIDR, or undefined if there is no previous CIDR.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.1.0/24");
      cidr.previousCIDR()?.toString(); // "192.168.0.0/24"

      const firstCidr = ipv4.cidr("0.0.0.0/24");
      firstCidr.previousCIDR(); // undefined
    • Gets the CIDR range.

      Returns number

      The CIDR range as a number.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      cidr.range(); // 24
    • Gets the address and range parts of the IPv4 CIDR.

      Returns [Ipv4Address, number]

      A tuple containing the IPv4 address and the CIDR range.

      import { ipv4 } from 'cidr-block';

      const [address, range] = ipv4.cidr("192.168.0.0/24").rangeParts();
      address.toString(); // "192.168.0.0"
      range; // 24
    • Splits the CIDR into smaller subranges of the specified new range.

      Parameters

      • newRange: number

        The new CIDR range for the subnets.

      Returns Ipv4Cidr[]

      An array of Ipv4Cidr instances representing the subnets.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("192.168.0.0/24");
      const subnets = cidr.subnet(26);
      subnets.map(s => s.toString());
      // ["192.168.0.0/26", "192.168.0.64/26", "192.168.0.128/26", "192.168.0.192/26"]

      InvalidIpv4CidrRangeError if the new range is less than the current range or greater than the maximum range.

    • Splits the CIDR into sequential subranges with the specified CIDR ranges. Each range in the input array creates a subrange starting where the previous one ended.

      Parameters

      • ranges: number[]

        An array of CIDR range values (e.g., [24, 20, 24]).

      Returns Ipv4Cidr[]

      An array of Ipv4Cidr instances representing the subnets.

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr("10.0.0.0/16");
      const subnets = cidr.subnetBy([24, 20, 24]);
      subnets.map(s => s.toString());
      // ["10.0.0.0/24", "10.0.1.0/20", "10.0.17.0/24"]

      InvalidIpv4CidrRangeError if any range is less than the current range or greater than 32.

    • Returns the string representation of the IPv4 CIDR.

      Returns `${number}.${number}.${number}.${number}/${number}`

      The IPv4 CIDR as a string (example: "192.0.0.0/24").

      import { ipv4 } from 'cidr-block';

      const cidr = ipv4.cidr({ address: "10.0.0.0", range: 8 });
      cidr.toString(); // "10.0.0.0/8"