IPv4 CIDR Range Matcher

Convert IPv4 addresses to integers and test membership in CIDR blocks for allowlists, proxies and rate limits.

TypescriptSecurity Boundaries

Code

typescriptsnippet
36 lines
export interface ParsedCidr {
  network: number;
  mask: number;
  prefix: number;
}

export function ipv4ToInt(address: string): number {
  const parts = address.split(".");
  if (parts.length !== 4) throw new TypeError("invalid IPv4 address: " + address);
  let value = 0;
  for (const part of parts) {
    if (!/^\d{1,3}$/.test(part)) throw new TypeError("invalid IPv4 octet: " + part);
    if (part.length > 1 && part.startsWith("0")) throw new RangeError("leading zeros are not allowed");
    const octet = Number(part);
    if (octet > 255) throw new RangeError("IPv4 octet out of range: " + part);
    value = value * 256 + octet;
  }
  return value >>> 0;
}

export function parseCidr(cidr: string): ParsedCidr {
  const parts = cidr.split("/");
  if (parts.length !== 2) throw new SyntaxError("CIDR must contain one slash");
  const address = parts[0]!;
  const prefix = Number(parts[1]!);
  if (!Number.isInteger(prefix) || prefix < 0 || prefix > 32) {
    throw new RangeError("CIDR prefix must be an integer between 0 and 32");
  }
  const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0;
  return { network: (ipv4ToInt(address) & mask) >>> 0, mask, prefix };
}

export function isIpInCidr(address: string, cidr: string): boolean {
  const { network, mask } = parseCidr(cidr);
  return ((ipv4ToInt(address) & mask) >>> 0) === network;
}

How it works

The working surface is the functions ipv4ToInt, parseCidr and isIpInCidr — everything else is wiring.

Repetition is confined to for (const part of parts) — the rest of the file is straight-line. TypeError, RangeError and SyntaxError is the guard’s signal — thrown at the branch, before any partial work happens.

The result comes back as an object carrying mask.

What to watch for

  • The guard parts.length !== 4 short-circuits the flow with an early throw when its condition holds.

Applying it

The primary signature is export function ipv4ToInt(address: string)

The surface worth importing is ipv4ToInt; the helpers around it are private to the snippet.

The call returns mask.

Statements that matter

if (part.length > 1 && part.startsWith("0")) throw new RangeError("leading zeros are not allowed");
if (parts.length !== 4) throw new TypeError("invalid IPv4 address: " + address);
if (!/^\d{1,3}$/.test(part)) throw new TypeError("invalid IPv4 octet: " + part);

Interactive Tools for This Topic

FAQ

Does `ipv4ToInt` modify its input?
No — a new value is constructed for the result and the arguments are never written to.
How are precondition violations signaled?
Validation comes first — `TypeError`, `RangeError` and `SyntaxError` marks the failure at `parts.length !== 4`, before any partial work happens.
Does the code pull in any external packages?
No external packages are involved; every call resolves to a built-in API. The file defines `ipv4ToInt`, `parseCidr`, `isIpInCidr` at load time.