cidrv4Intersect(): Cidrv4 | null
Returns the intersection of two IPv4 CIDR blocks.
Since CIDR blocks are power-of-2-aligned, two overlapping blocks always have a containment relationship -- the intersection is the more specific (longer prefix) block with its canonical network address.
The result matches the dialect of the inputs. When they disagree, it is a MaskedCidrv4 (ADR 0006).
Find overlap between allocations
Find overlap between allocations
import { assertEquals } from "@std/assert"; import { cidrv4Intersect, parseCidrv4, stringifyCidrv4 } from "@hertzg/ip/cidrv4"; const result = cidrv4Intersect( parseCidrv4("192.168.1.0/24"), parseCidrv4("192.168.1.0/28"), ); assertEquals(result && stringifyCidrv4(result), "192.168.1.0/28");
No overlap returns null
No overlap returns null
import { assertEquals } from "@std/assert"; import { cidrv4Intersect, parseCidrv4 } from "@hertzg/ip/cidrv4"; assertEquals(cidrv4Intersect( parseCidrv4("10.0.0.0/8"), parseCidrv4("172.16.0.0/12"), ), null);
Mixed dialects intersect to a masked block
Mixed dialects intersect to a masked block
import { assertEquals } from "@std/assert"; import { cidrv4Intersect, parseCidrv4 } from "@hertzg/ip/cidrv4"; const result = cidrv4Intersect( parseCidrv4("192.168.1.0/24"), { address: 3232235776, mask: 0xFFFFFFF0 }, ); assertEquals(result, { address: 3232235776, mask: 0xFFFFFFF0 });
Cidrv4 | null
The overlapping CIDR with canonical network address, or null if disjoint