Universal IP address classification.
This module provides a single classifyAddress function that accepts
both IPv4 (number) and IPv6 (bigint) addresses and returns the
appropriate classification with version information and parsed value.
For version-specific classifiers, see:
classifyv4: classifyAddressv4, isAddressv4Private, etc.classifyv6: classifyAddressv6, isAddressv6Loopback, etc.
Classify any IP address
Classify any IP address
import { assertEquals } from "@std/assert"; import { classifyAddress } from "@hertzg/ip/classify"; import { parseAddressv4 } from "@hertzg/ip/addressv4"; import { parseAddressv6 } from "@hertzg/ip/addressv6"; // IPv4 from parsed value const v4 = classifyAddress(parseAddressv4("192.168.1.1").address); assertEquals(v4.kind, "ipv4"); assertEquals(v4.value, 3232235777); assertEquals(v4.classification, "private"); // IPv6 from parsed value const v6 = classifyAddress(parseAddressv6("::1").address); assertEquals(v6.kind, "ipv6"); assertEquals(v6.value, 1n); assertEquals(v6.classification, "loopback"); // From string directly const str4 = classifyAddress("127.0.0.1"); assertEquals(str4.kind, "ipv4"); assertEquals(str4.classification, "loopback"); const str6 = classifyAddress("2001:db8::1"); assertEquals(str6.kind, "ipv6"); assertEquals(str6.classification, "documentation");
Classifies an IPv4 address into its well-known range.
A plain IP address of either IP version.
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).
An IPv6 address as a 128-bit unsigned bigint, 0n to 2n ** 128n - 1n.
The primitive type is what carries the version: a bigint is IPv6, a
number is IPv4 (ADR 0001).
| "this-network"
| "loopback"
| "link-local"
| "documentation"
| "benchmarking"
| "cg-nat"
| "private"
| "multicast"
| "reserved"
| "public"
All possible IPv4 address classification labels.
Result of classifying an IP address with version information and parsed value.
Usage
import * as mod from "classify.ts";