IPv4 CIDR notation parsing and utilities.
This module provides CIDR parsing, network calculations, and IP range checking for IPv4 networks. Works with number representations to enable efficient IP assignment workflows.
A Cidrv4 stores whichever dialect it was written in, a prefix
length (10.0.0.0/8) or a network mask (10.0.0.0/255.0.0.0), and every
operation here accepts both.
Both dialects parse, and write back the way they came
Both dialects parse, and write back the way they came
import { assertEquals } from "@std/assert"; import { cidrv4Size, parseCidrv4, stringifyCidrv4 } from "@hertzg/ip/cidrv4"; const prefixed = parseCidrv4("10.0.0.0/8"); const masked = parseCidrv4("10.0.0.0/255.0.0.0"); assertEquals(cidrv4Size(prefixed), cidrv4Size(masked)); assertEquals(stringifyCidrv4(prefixed), "10.0.0.0/8"); assertEquals(stringifyCidrv4(masked), "10.0.0.0/255.0.0.0");
CIDR operations
CIDR operations
import { assert, assertEquals } from "@std/assert"; import { cidrv4Contains, parseCidrv4 } from "@hertzg/ip/cidrv4"; import { parseAddressv4 } from "@hertzg/ip/addressv4"; const cidr = parseCidrv4("192.168.1.0/24"); assert(cidrv4Contains(cidr, parseAddressv4("192.168.1.1").address)); assertEquals(cidrv4Contains(cidr, parseAddressv4("192.168.2.1").address), false);
Handing out assignable addresses
Handing out assignable addresses
import { assertEquals } from "@std/assert"; import { cidrv4UsableAddresses, cidrv4UsableSize, parseCidrv4, } from "@hertzg/ip/cidrv4"; import { stringifyAddressv4 } from "@hertzg/ip/addressv4"; const pool = parseCidrv4("192.168.1.0/24"); const assigned = Array.from(cidrv4UsableAddresses(pool), stringifyAddressv4); assertEquals(assigned.length, cidrv4UsableSize(pool)); assertEquals(assigned[0], "192.168.1.1"); assertEquals(assigned.at(-1), "192.168.1.254");
Generates a range of IP addresses from a CIDR block.
Returns the directed broadcast address of a CIDR block.
Checks if an IPv4 address is contained within a CIDR block.
Checks if one IPv4 CIDR block fully contains another.
Returns the first address of a CIDR block (network address).
Returns the first assignable address of a CIDR block.
Returns the intersection of two IPv4 CIDR blocks.
Returns the last address of a CIDR block (broadcast address for IPv4).
Returns the last assignable address of a CIDR block.
Creates a network mask from an IPv4 prefix length.
Merges IPv4 CIDR blocks into the minimal covering set.
Returns the network address of a CIDR block.
Checks if two IPv4 CIDR blocks overlap (share at least one address).
Recovers the prefix length from an IPv4 network mask given as a 32-bit unsigned integer.
Returns the total number of IP addresses in a CIDR block.
Subtracts one IPv4 CIDR block from another.
Generates every assignable address in a CIDR block, in ascending order.
Returns the number of assignable addresses in a CIDR block.
Compares two IPv4 CIDR blocks for sorting.
Parses IPv4 CIDR notation, in either dialect and with an optional zone ID, to a ParsedCidrv4.
Stringifies an IPv4 CIDR block, or an address, to CIDR notation.
An IPv4 address as a 32-bit unsigned integer, 0 to 4294967295. The
primitive type is what carries the version: a number is IPv4, a
bigint is IPv6 (ADR 0001).
Represents an IPv4 CIDR block.
An IPv4 CIDR block written with a network mask, as in
10.0.0.0/255.0.0.0.
-
address: Addressv4
The IPv4 address from the CIDR notation
-
mask: Maskv4
The network mask, stored as given
-
prefixLength: never
Absent: the prefix length dialect is PrefixedCidrv4
An IPv4 network mask as a 32-bit unsigned integer, e.g. 0xFFFFFF00
for /24.
What parseAddressv4 returns and what stringifyAddressv4
accepts: the address, plus the zone ID if the notation had one. Read
.address for the bare Addressv4; the zone never touches the
value, and no operation in this package reads it.
-
address: Addressv4
The address as a 32-bit unsigned integer
-
zoneId: ZoneId
The zone ID after
%, verbatim, when the notation had one
What parseCidrv4 returns and what stringifyCidrv4
accepts: a Cidrv4 in the dialect it was written in, plus the zone
ID if the notation had one (fe80::%ether1/64 has one; RouterOS emits
that form for connected routes). Assignable to Cidrv4, so a parse
result goes straight into every cidrv4* operation; none of them reads
the zone.
An IPv4 CIDR block written with a prefix length, as in 10.0.0.0/8.
-
address: Addressv4
The IPv4 address from the CIDR notation
-
mask: never
Absent: the mask dialect is MaskedCidrv4
-
prefixLength: PrefixLengthv4
The prefix length (0-32)
An IPv4 prefix length, the 24 in /24. The range is 0 to 32; it is
documented rather than encoded in the type, so prefixLength + 1 stays
a PrefixLengthv4 (ADR 0002).
The zone ID of an address, the interface tail after % in
fe80::1%eth0 (RFC 4007 section 11). Carried verbatim by the Parsed*
types; never percent-decoded, so %25eth0 is the zone 25eth0.
Usage
import * as mod from "cidrv4.ts";