net/ip
lib/net/ip.vklib/net/ip.vk Pure-Veko IPv4 & IPv6 address parser, canonical formatter, CIDR subnet matcher, and loopback/private address classification. Zero capabilities required.
IpAddr = struct { version i64, octets bytes }
Parsed IPv4 (version = 4, 4 octets) or IPv6 (version = 6, 16 octets) address.
Cidr = struct { addr IpAddr, prefix_len i64 }
Parsed CIDR network prefix (addr/prefix_len, e.g. 10.0.0.0/8 or fc00::/7).
parse = fn [s str] IpAddr
Parses an IPv4 or IPv6 address string into an IpAddr.
Executable Example
use [ip = net/ip]
a = ip/parse '192.168.1.10'
b = ip/parse '2001:db8::1'
if (not (ip/v4? a)) or (not (ip/private? a)) or (not (ip/v6? b)) [ err 'ip parse' ] else [ .ok ]
format = fn [a IpAddr] str
Formats an IpAddr into its canonical string representation (RFC 5952 compression for IPv6).
Executable Example
use [ip = net/ip]
if (ip/format (ip/parse '2001:0db8:0000:0000:0000:0000:0000:0001')) != '2001:db8::1' [ err 'ip fmt' ] else [ .ok ]
v4? = fn [a IpAddr] bool
v6? = fn [a IpAddr] bool
loopback? = fn [a IpAddr] bool
Returns true if a is a loopback address (127.0.0.0/8 or ::1).
private? = fn [a IpAddr] bool
Returns true if a is an RFC 1918 / link-local / ULA private address.
parse-cidr = fn [s str] Cidr
Parses a CIDR prefix string ('10.0.0.0/8' or '2001:db8::/32').
contains? = fn [cidr Cidr, target IpAddr] bool
Returns true if cidr contains target.
Executable Example
use [ip = net/ip]
net10 = ip/parse-cidr '10.20.0.0/16'
if (not (ip/contains? net10 (ip/parse '10.20.5.9'))) or (ip/contains? net10 (ip/parse '10.21.0.1')) [
err 'cidr match'
] else [ .ok ]