function cidrv4FirstUsableAddress
cidrv4FirstUsableAddress(cidr: Cidrv4): number

Returns the first assignable address of a CIDR block.

This is the cidrv4NetworkAddress plus one, because the network address names the subnet rather than a machine on it (RFC 1812 section 5.3.5).

Two prefix lengths are exceptions and yield the network address itself:

  • /31 — a point-to-point link, where RFC 3021 assigns both addresses to the two ends and there is no broadcast address to reserve.
  • /32 — a host route, which is a single address and nothing else.

Every IPv4 block therefore has at least one assignable address, so this never returns a value outside the block and never has to signal "none".

Examples

First assignable address of a subnet

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

const gateway = cidrv4FirstUsableAddress(parseCidrv4("192.168.1.0/24"));
assertEquals(stringifyAddressv4(gateway), "192.168.1.1");

RFC 3021 /31 and /32 keep the network address

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

assertEquals(
  stringifyAddressv4(cidrv4FirstUsableAddress(parseCidrv4("10.0.0.0/31"))),
  "10.0.0.0",
);
assertEquals(
  stringifyAddressv4(cidrv4FirstUsableAddress(parseCidrv4("10.0.0.7/32"))),
  "10.0.0.7",
);

Parameters

cidr: Cidrv4

The CIDR block

Return Type

number

The first assignable address as a 32-bit unsigned integer

Usage

import { cidrv4FirstUsableAddress } from "mod.ts";