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

    Class Ipv6Cidr

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

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

    import { ipv6 } from 'cidr-block';

    // Recommended: use the ipv6 namespace shorthand
    const cidr1 = ipv6.cidr("2001:db8::/32");
    const cidr2 = ipv6.cidr({ address: "2001:db8::", range: 32 });
    const cidr3 = ipv6.cidr(["2001:db8::", 32]);
    import { ipv6 } from 'cidr-block';

    const cidr = ipv6.cidr("2001:db8::/32");
    cidr.baseAddress().toString(); // "2001:db8::"
    cidr.range(); // 32
    cidr.netmask().toString(); // "ffff:ffff::"
    cidr.addressCount(); // 79228162514264337593543950336n
    import { ipv6 } from 'cidr-block';

    const cidr = ipv6.cidr("2001:db8::/126");
    cidr.getFirstUsableAddress()?.toString(); // "2001:db8::1"
    cidr.getLastUsableAddress()?.toString(); // "2001:db8::2"
    import { ipv6 } from 'cidr-block';

    const cidr = ipv6.cidr("2001:db8::/32");
    cidr.includes(ipv6.address("2001:db8::1")); // true
    cidr.includes(ipv6.address("2001:db9::1")); // false
    cidr.overlaps("2001:db8::/48"); // true
    cidr.overlaps("2001:db9::/32"); // false
    import { ipv6 } from 'cidr-block';

    const cidr = ipv6.cidr("2001:db8::/32");

    // Split into equal /48 subnets
    cidr.subnet(34).map(s => s.toString());
    // ["2001:db8::/34", "2001:db8:4000::/34", "2001:db8:8000::/34", "2001:db8:c000::/34"]
    import { ipv6 } from 'cidr-block';

    const cidr = ipv6.cidr("2001:db8::/32");
    cidr.nextCIDR()?.toString(); // "2001:db9::/32"
    cidr.previousCIDR()?.toString(); // "2001:db7::/32"
    Index

    Constructors

    Methods

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

      Returns bigint

      The total number of addresses in the CIDR range as a BigInt.

      import { ipv6 } from 'cidr-block';

      ipv6.cidr("2001:db8::/64").addressCount(); // 18446744073709551616n
      ipv6.cidr("2001:db8::/128").addressCount(); // 1n
      ipv6.cidr("2001:db8::/126").addressCount(); // 4n
    • Generates IPv6 addresses within the CIDR range. Note: For large CIDR ranges, this may generate an extremely large number of addresses. Use with caution and consider using a limit parameter.

      Parameters

      • Optionallimit: bigint

        Optional maximum number of addresses to generate (defaults to all addresses).

      Returns Generator<Ipv6Address>

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

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/126");
      for (const addr of cidr.addresses()) {
      console.log(addr.toString());
      }
      // Output: "2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"
    • Gets the base IPv6 address of the CIDR.

      Returns Ipv6Address

      The base IPv6 address.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.baseAddress().toString(); // "2001:db8::"
    • 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 { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.equals("2001:db8::/32"); // true
      cidr.equals({ address: "2001:db8::", range: 32 }); // true
      cidr.equals("2001:db8::/48"); // false
    • Gets the first usable address in the CIDR range.

      Returns Ipv6Address | undefined

      The first usable IPv6 address, or undefined if the range is /128.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/64");
      cidr.getFirstUsableAddress()?.toString(); // "2001:db8::1"

      const hostCidr = ipv6.cidr("2001:db8::1/128");
      hostCidr.getFirstUsableAddress(); // undefined (no usable addresses in /128)
    • Gets the last usable address in the CIDR range.

      Returns Ipv6Address | undefined

      The last usable IPv6 address, or undefined if the range is /128.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/126");
      cidr.getLastUsableAddress()?.toString(); // "2001:db8::2"

      const hostCidr = ipv6.cidr("2001:db8::1/128");
      hostCidr.getLastUsableAddress(); // undefined (no usable addresses in /128)
    • Checks if there is a next sequential CIDR.

      Returns boolean

      true if there is a next CIDR.

      import { ipv6 } from 'cidr-block';

      ipv6.cidr("2001:db8::/32").hasNextCIDR(); // true
      ipv6.cidr("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/120").hasNextCIDR(); // false
    • Checks if there is a previous sequential CIDR.

      Returns boolean

      true if there is a previous CIDR.

      import { ipv6 } from 'cidr-block';

      ipv6.cidr("2001:db8::/32").hasPreviousCIDR(); // true
      ipv6.cidr("::/32").hasPreviousCIDR(); // false
    • Checks if the given IPv6 address is within the CIDR range.

      Parameters

      Returns boolean

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

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.includes(ipv6.address("2001:db8::1")); // true
      cidr.includes(ipv6.address("2001:db9::1")); // false
    • Calculates the netmask for the CIDR range.

      Returns Ipv6Address

      The netmask as an Ipv6Address.

      import { ipv6 } from 'cidr-block';

      ipv6.cidr("2001:db8::/32").netmask().toString(); // "ffff:ffff::"
      ipv6.cidr("2001:db8::/64").netmask().toString(); // "ffff:ffff:ffff:ffff::"
      ipv6.cidr("2001:db8::/128").netmask().toString(); // "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
    • Gets the next sequential CIDR.

      Returns Ipv6Cidr | undefined

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

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.nextCIDR()?.toString(); // "2001:db9::/32"
    • Checks if this CIDR overlaps with another CIDR.

      Parameters

      Returns boolean

      True if the CIDRs overlap; otherwise, false.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.overlaps("2001:db8::/48"); // true (subset)
      cidr.overlaps("2001:db8:8000::/33"); // true (partial overlap)
      cidr.overlaps("2001:db9::/32"); // false (no overlap)
    • Gets the previous sequential CIDR.

      Returns Ipv6Cidr | undefined

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

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.previousCIDR()?.toString(); // "2001:db7::/32"

      const firstCidr = ipv6.cidr("::/32");
      firstCidr.previousCIDR(); // undefined
    • Gets the CIDR range.

      Returns number

      The CIDR range as a number.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      cidr.range(); // 32
    • Gets the address and range parts of the IPv6 CIDR.

      Returns [Ipv6Address, number]

      A tuple containing the IPv6 address and the CIDR range.

      import { ipv6 } from 'cidr-block';

      const [address, range] = ipv6.cidr("2001:db8::/32").rangeParts();
      address.toString(); // "2001:db8::"
      range; // 32
    • Splits the CIDR into smaller subranges of the specified new range.

      Parameters

      • newRange: number

        The new CIDR range for the subnets.

      Returns Ipv6Cidr[]

      An array of Ipv6Cidr instances representing the subnets.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      const subnets = cidr.subnet(34);
      subnets.map(s => s.toString());
      // ["2001:db8::/34", "2001:db8:4000::/34", "2001:db8:8000::/34", "2001:db8:c000::/34"]

      InvalidIpv6CidrRangeError 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., [48, 36, 48]).

      Returns Ipv6Cidr[]

      An array of Ipv6Cidr instances representing the subnets.

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr("2001:db8::/32");
      const subnets = cidr.subnetBy([48, 36, 48]);
      subnets.map(s => s.toString());
      // ["2001:db8::/48", "2001:db8:1::/36", "2001:db8:1100::/48"]

      InvalidIpv6CidrRangeError if any range is less than the current range or greater than 128.

    • Returns the string representation of the IPv6 CIDR.

      Returns string

      The IPv6 CIDR as a string (example: "2001:db8::/32").

      import { ipv6 } from 'cidr-block';

      const cidr = ipv6.cidr({ address: "2001:db8::", range: 32 });
      cidr.toString(); // "2001:db8::/32"