function cidrv4UsableAddresses
cidrv4UsableAddresses(cidr: Cidrv4): Generator<number>

Generates every assignable address in a CIDR block, in ascending order.

Yields cidrv4FirstUsableAddress through cidrv4LastUsableAddress inclusive — cidrv4UsableSize addresses in total, with the network and broadcast addresses skipped except at /31 and /32 where the whole block is assignable.

Lazy: nothing is materialized, so a short prefix costs nothing until the caller iterates it.

Unlike cidrv4Addresses this takes no offset, count, or step — its extent is fixed by the block. Reach for cidrv4Addresses to slice a block on any other terms.

Examples

Assign addresses out of a pool

import { assertEquals } from "@std/assert";
import { cidrv4UsableAddresses, parseCidrv4 } from "@hertzg/ip/cidrv4";
import { stringifyAddressv4 } from "@hertzg/ip/addressv4";

const pool = Array.from(cidrv4UsableAddresses(parseCidrv4("10.0.1.0/29")));
assertEquals(pool.map(stringifyAddressv4), [
  "10.0.1.1",
  "10.0.1.2",
  "10.0.1.3",
  "10.0.1.4",
  "10.0.1.5",
  "10.0.1.6",
]);

RFC 3021 /31 yields both ends of the link

import { assertEquals } from "@std/assert";
import { cidrv4UsableAddresses, parseCidrv4 } from "@hertzg/ip/cidrv4";
import { stringifyAddressv4 } from "@hertzg/ip/addressv4";

const link = Array.from(cidrv4UsableAddresses(parseCidrv4("10.0.0.0/31")));
assertEquals(link.map(stringifyAddressv4), ["10.0.0.0", "10.0.0.1"]);

const route = Array.from(cidrv4UsableAddresses(parseCidrv4("10.0.0.7/32")));
assertEquals(route.map(stringifyAddressv4), ["10.0.0.7"]);

Lazy — a /8 costs nothing until iterated

import { assertEquals } from "@std/assert";
import { cidrv4UsableAddresses, parseCidrv4 } from "@hertzg/ip/cidrv4";
import { stringifyAddressv4 } from "@hertzg/ip/addressv4";

const addresses = cidrv4UsableAddresses(parseCidrv4("10.0.0.0/8"));
assertEquals(stringifyAddressv4(addresses.next().value as number), "10.0.0.1");
addresses.return(undefined);

Parameters

cidr: Cidrv4

The CIDR block to enumerate

Return Type

Generator<number>

A generator yielding assignable addresses as 32-bit unsigned integers