function cidrv4LastUsableAddress
cidrv4LastUsableAddress(cidr: Cidrv4): number

Returns the last assignable address of a CIDR block.

This is the cidrv4BroadcastAddress minus one, because the broadcast address addresses the whole subnet rather than a machine on it (RFC 1812 section 5.3.5).

Two prefix lengths are exceptions and yield the broadcast 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.

Examples

Last assignable address of a subnet

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

const last = cidrv4LastUsableAddress(parseCidrv4("192.168.1.0/24"));
assertEquals(stringifyAddressv4(last), "192.168.1.254");

RFC 3021 /31 and /32 keep the broadcast address

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

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

Parameters

cidr: Cidrv4

The CIDR block

Return Type

number

The last assignable address as a 32-bit unsigned integer

Usage

import { cidrv4LastUsableAddress } from "cidrv4.ts";