function cidrv4Overlaps
cidrv4Overlaps(): boolean

Checks if two IPv4 CIDR blocks overlap (share at least one address).

Two CIDRs overlap when one contains at least one address of the other. This is equivalent to checking containment using the shorter prefix. The check is symmetric: cidrv4Overlaps(a, b) === cidrv4Overlaps(b, a).

Examples

Overlapping CIDRs

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

assert(cidrv4Overlaps(parseCidrv4("10.0.0.0/8"), parseCidrv4("10.1.0.0/16")));
assert(cidrv4Overlaps(parseCidrv4("10.1.0.0/16"), parseCidrv4("10.0.0.0/8")));
assert(cidrv4Overlaps(parseCidrv4("192.168.1.0/24"), parseCidrv4("192.168.1.0/24")));
assertEquals(cidrv4Overlaps(parseCidrv4("10.0.0.0/8"), parseCidrv4("172.16.0.0/12")), false);

/0 overlaps everything

import { assert } from "@std/assert";
import { cidrv4Overlaps, parseCidrv4 } from "@hertzg/ip/cidrv4";

const all = parseCidrv4("0.0.0.0/0");
assert(cidrv4Overlaps(all, parseCidrv4("192.168.1.0/24")));
assert(cidrv4Overlaps(all, parseCidrv4("10.0.0.1/32")));

Adjacent but non-overlapping

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

assertEquals(cidrv4Overlaps(parseCidrv4("192.168.0.0/24"), parseCidrv4("192.168.1.0/24")), false);

Parameters

The first CIDR block

The second CIDR block

Return Type

boolean

true if the two CIDR blocks share at least one address

Usage

import { cidrv4Overlaps } from "cidrv4.ts";