Something went wrong. Try again.
An allocation-free Zig library for parsing, validating, formatting and classifying IPv4 and IPv6 addresses, networks, ranges and endpoints.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! An IP network of either version.
const std = @import("std");const json = @import("json.zig");const Writer = std.Io.Writer;
const Address = @import("address.zig").Address;const Family = @import("address.zig").Family;const Ipv4Network = @import("Ipv4Network.zig");const Ipv6Network = @import("Ipv6Network.zig");
/// An IPv4 or IPv6 network in CIDR notation, for code that handles both/// without caring which it has.pub const Network = union(Family) { ipv4: Ipv4Network, ipv6: Ipv6Network,
/// The IPv4 default route, `0.0.0.0/0`. pub const default_route_ipv4: Network = .{ .ipv4 = .default_route }; /// The IPv6 default route, `::/0`. pub const default_route_ipv6: Network = .{ .ipv6 = .default_route };
pub const ParseError = Ipv4Network.ParseError || Ipv6Network.ParseError;
/// Options accepted by `parseWithOptions`. The IPv4 form is the only one /// with a second spelling to allow or refuse, so these are /// `Ipv4Network.ParseOptions`; an IPv6 network reads the options that /// apply to it and ignores `allow_netmask`, which no IPv6 text has ever /// used. pub const ParseOptions = Ipv4Network.ParseOptions;
/// Parses a network of either version. Text holding a `:` before the `/` /// is read as IPv6 and everything else as IPv4. pub fn parse(text: []const u8) ParseError!Network { return parseWithOptions(text, .{}); }
/// Like `parse`, but clears any host bits instead of rejecting them. pub fn parseTruncate(text: []const u8) ParseError!Network { return parseWithOptions(text, .{ .truncate_host_bits = true }); }
/// Like `parse`, but reading the text form that `options` describes, such /// as the network of an SPF `ip4:` or `ip6:` mechanism: /// /// ``` /// const network = try Network.parseWithOptions(term[4..], .spf); /// ``` /// /// Which version is read is still decided by the text rather than by the /// mechanism it came from, so a caller that must hold `ip4:` to IPv4 and /// `ip6:` to IPv6 -- as RFC 7208 §5.6 does -- asks the version's own type /// instead. pub fn parseWithOptions(text: []const u8, options: ParseOptions) ParseError!Network { if (isIpv6Text(text)) return .{ .ipv6 = try .parseWithOptions(text, .{ .truncate_host_bits = options.truncate_host_bits, }) }; return .{ .ipv4 = try .parseWithOptions(text, options) }; }
fn isIpv6Text(text: []const u8) bool { const address_text = text[0 .. std.mem.findScalar(u8, text, '/') orelse text.len]; return std.mem.findScalar(u8, address_text, ':') != null; }
/// Whether `text` is a valid network of either version with its host bits /// clear, discarding the reason when it is not. pub fn isValid(text: []const u8) bool { _ = parse(text) catch return false; return true; }
/// The `/32` or `/128` network holding exactly `address`. pub fn fromHost(address: Address) Network { return switch (address) { .ipv4 => |v4| .{ .ipv4 = .fromHost(v4) }, .ipv6 => |v6| .{ .ipv6 = .fromHost(v6) }, }; }
pub fn family(n: Network) Family { return n; }
/// Length of the routing prefix in bits. pub fn prefixLen(n: Network) u8 { return switch (n) { inline else => |network| network.prefix_len, }; }
/// The first address in the network. pub fn networkAddress(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.networkAddress() }, .ipv6 => |v6| .{ .ipv6 = v6.networkAddress() }, }; }
/// The last address in the network: the broadcast address of an IPv4 /// network, and simply the highest address of an IPv6 one. pub fn lastAddress(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.broadcastAddress() }, .ipv6 => |v6| .{ .ipv6 = v6.lastAddress() }, }; }
pub fn netmask(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.netmask() }, .ipv6 => |v6| .{ .ipv6 = v6.netmask() }, }; }
pub fn hostmask(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.hostmask() }, .ipv6 => |v6| .{ .ipv6 = v6.hostmask() }, }; }
/// The number of addresses in the network. The result is 129 bits wide /// because an IPv6 `/0` holds 2^128 addresses. pub fn addressCount(n: Network) u129 { return switch (n) { .ipv4 => |v4| v4.addressCount(), .ipv6 => |v6| v6.addressCount(), }; }
/// The part of this network that is left once `other` is taken out of /// it, as a sequence of whole networks. See `Ipv4Network.exclude` for /// what comes out and in what order. /// /// A network of the other version overlaps nothing here, so excluding one /// removes nothing and this network comes back whole. pub fn exclude(n: Network, other: Network) ExcludeIterator { return switch (n) { .ipv4 => |v4| .{ .ipv4 = switch (other) { .ipv4 => |o| v4.exclude(o), .ipv6 => .initWhole(v4), } }, .ipv6 => |v6| .{ .ipv6 = switch (other) { .ipv4 => .initWhole(v6), .ipv6 => |o| v6.exclude(o), } }, }; }
pub const ExcludeIterator = union(Family) { ipv4: Ipv4Network.ExcludeIterator, ipv6: Ipv6Network.ExcludeIterator,
pub fn next(it: *ExcludeIterator) ?Network { return switch (it.*) { .ipv4 => |*v4| if (v4.next()) |network| .{ .ipv4 = network } else null, .ipv6 => |*v6| if (v6.next()) |network| .{ .ipv6 = network } else null, }; }
test next { var it = (try Network.parse("2001:db8::/32")).exclude(try .parse("2001:db8::/34")); try testing.expect(it.next().?.eql(try .parse("2001:db8:8000::/33"))); try testing.expect(it.next().?.eql(try .parse("2001:db8:4000::/34"))); try testing.expectEqual(null, it.next()); } };
/// Whether the network holds exactly one address: a `/32` for IPv4 or a /// `/128` for IPv6. See `Ipv4Network.isSingleAddress` for why it is a /// question worth asking directly. pub fn isSingleAddress(n: Network) bool { return switch (n) { inline else => |network| network.isSingleAddress(), }; }
/// The number of usable host addresses. What the two versions hold back /// differs — IPv4 reserves its network and broadcast addresses, IPv6 only /// the Subnet-Router anycast address — so see `Ipv4Network.hostCount` and /// `Ipv6Network.hostCount` for which addresses these are. pub fn hostCount(n: Network) u129 { return switch (n) { .ipv4 => |v4| v4.hostCount(), .ipv6 => |v6| v6.hostCount(), }; }
/// The first usable host address. pub fn firstHost(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.firstHost() }, .ipv6 => |v6| .{ .ipv6 = v6.firstHost() }, }; }
/// The last usable host address. pub fn lastHost(n: Network) Address { return switch (n) { .ipv4 => |v4| .{ .ipv4 = v4.lastHost() }, .ipv6 => |v6| .{ .ipv6 = v6.lastHost() }, }; }
/// Whether `address` falls inside the network. An address of the other /// version never does, even when it is the IPv4-mapped form of one that /// would. pub fn contains(n: Network, address: Address) bool { return switch (n) { .ipv4 => |v4| address == .ipv4 and v4.contains(address.ipv4), .ipv6 => |v6| address == .ipv6 and v6.contains(address.ipv6), }; }
/// Whether every address of `other` also falls inside `n`. Networks of /// different versions never contain one another. pub fn containsNetwork(n: Network, other: Network) bool { return switch (n) { .ipv4 => |v4| other == .ipv4 and v4.containsNetwork(other.ipv4), .ipv6 => |v6| other == .ipv6 and v6.containsNetwork(other.ipv6), }; }
/// Whether the two networks share any address. Networks of different /// versions never overlap. pub fn overlaps(n: Network, other: Network) bool { return switch (n) { .ipv4 => |v4| other == .ipv4 and v4.overlaps(other.ipv4), .ipv6 => |v6| other == .ipv6 and v6.overlaps(other.ipv6), }; }
/// The longest text a network of either version can format to. pub const max_text_len = @max(Ipv4Network.max_text_len, Ipv6Network.max_text_len);
/// Writes the network in CIDR notation. Use the `{f}` specifier. pub fn format(n: Network, w: *Writer) Writer.Error!void { switch (n) { inline else => |network| try network.format(w), } }
/// Formats into `buffer`, returning the written slice. The buffer must be /// at least `max_text_len` bytes. pub fn bufPrint(n: Network, buffer: []u8) error{NoSpaceLeft}![]u8 { var w: Writer = .fixed(buffer); n.format(&w) catch return error.NoSpaceLeft; return w.buffered(); }
/// Sorts `networks` in place and folds it down to the smallest set /// covering exactly the same addresses. See `Ipv4Network.merge`. /// /// A mixed slice is fine. Sorting puts every IPv4 network ahead of every /// IPv6 one and nothing folds across the boundary, so the two versions /// are merged independently and come back in that order. pub fn merge(networks: []Network) []Network { // Unstable, because under this comparator two networks that compare // equal are the same network and there is no order between them to // preserve. std.mem.sortUnstable(Network, networks, {}, lessThan); var kept: usize = 0; for (networks) |candidate| { var network = candidate; while (kept > 0) { const top = networks[kept - 1]; if (top.containsNetwork(network)) break; network = join(top, network) orelse break; kept -= 1; } if (kept > 0 and networks[kept - 1].containsNetwork(network)) continue; networks[kept] = network; kept += 1; } return networks[0..kept]; }
fn lessThan(_: void, a: Network, b: Network) bool { return a.order(b) == .lt; }
/// The one network that covers both exactly, when they are the two halves /// of it, and null otherwise. Two networks of different versions never /// are. See `Ipv4Network.join`. pub fn join(a: Network, b: Network) ?Network { return switch (a) { .ipv4 => |v4| if (b == .ipv4) if (v4.join(b.ipv4)) |network| .{ .ipv4 = network } else null else null, .ipv6 => |v6| if (b == .ipv6) if (v6.join(b.ipv6)) |network| .{ .ipv6 = network } else null else null, }; }
pub fn eql(n: Network, other: Network) bool { return switch (n) { .ipv4 => |v4| other == .ipv4 and v4.eql(other.ipv4), .ipv6 => |v6| other == .ipv6 and v6.eql(other.ipv6), }; }
/// Orders two networks, with every IPv4 network before every IPv6 one. pub fn order(n: Network, other: Network) std.math.Order { return switch (n) { .ipv4 => |v4| switch (other) { .ipv4 => |o| v4.order(o), .ipv6 => .lt, }, .ipv6 => |v6| switch (other) { .ipv4 => .gt, .ipv6 => |o| v6.order(o), }, }; }
/// Writes the value as a JSON string. See `json.stringify`. pub fn jsonStringify(value: Network, jws: anytype) !void { return json.stringify(value, jws); }
/// Reads a JSON string with `parse`. See `json.parse` for what happens to the /// reason when the text is rejected. pub fn jsonParse( allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions, ) !Network { _ = options; return json.parse(Network, allocator, source); }
test parse { try testing.expect((try Network.parse("192.168.1.0/24")) == .ipv4); try testing.expect((try Network.parse("2001:db8::/32")) == .ipv6); // A `:` before the `/` is what marks the text as IPv6. try testing.expectError(error.HasHostBits, Network.parse("192.168.1.42/24")); }
test parseTruncate { // The host bits are cleared rather than rejected, which is how a // network is read out of an address that is on it. try testing.expect((try Network.parseTruncate("192.168.1.42/24")) .eql(try Network.parse("192.168.1.0/24"))); try testing.expect((try Network.parseTruncate("2001:db8::1/32")) .eql(try Network.parse("2001:db8::/32"))); }
test parseWithOptions { // The network of an SPF `ip4:` or `ip6:` mechanism, which compares // only the prefix and so allows host bits. try testing.expect((try Network.parseWithOptions("192.0.2.130/24", .spf)) .eql(try Network.parse("192.0.2.0/24"))); try testing.expect((try Network.parseWithOptions("2001:db8::1/32", .spf)) .eql(try Network.parse("2001:db8::/32"))); }
test isValid { try testing.expect(Network.isValid("192.168.1.0/24")); try testing.expect(Network.isValid("2001:db8::/32")); // Host bits are what separate a network from an interface. try testing.expect(!Network.isValid("192.168.1.42/24")); }
test fromHost { const network = Network.fromHost(try .parse("2001:db8::1")); try testing.expectEqual(@as(u8, 128), network.prefixLen()); try testing.expectEqual(@as(u129, 1), network.addressCount()); try testing.expectEqual(@as(u8, 32), Network.fromHost(try .parse("192.0.2.1")).prefixLen()); }
test family { try testing.expectEqual(.ipv4, (try Network.parse("192.168.1.0/24")).family()); try testing.expectEqual(.ipv6, (try Network.parse("2001:db8::/32")).family()); }
test prefixLen { try testing.expectEqual(@as(u8, 24), (try Network.parse("192.168.1.0/24")).prefixLen()); try testing.expectEqual(@as(u8, 32), (try Network.parse("2001:db8::/32")).prefixLen()); }
test networkAddress { try testing.expect((try Network.parse("192.168.1.0/24")) .networkAddress().eql(try .parse("192.168.1.0"))); }
test lastAddress { // The broadcast address of an IPv4 network, and simply the highest // address of an IPv6 one. try testing.expect((try Network.parse("192.168.1.0/24")) .lastAddress().eql(try .parse("192.168.1.255"))); try testing.expect((try Network.parse("2001:db8::/112")) .lastAddress().eql(try .parse("2001:db8::ffff"))); }
test netmask { try testing.expect((try Network.parse("192.168.1.0/24")) .netmask().eql(try .parse("255.255.255.0"))); try testing.expect((try Network.parse("2001:db8::/32")) .netmask().eql(try .parse("ffff:ffff::"))); }
test hostmask { try testing.expect((try Network.parse("192.168.1.0/24")) .hostmask().eql(try .parse("0.0.0.255"))); }
test addressCount { try testing.expectEqual(@as(u129, 256), (try Network.parse("192.168.1.0/24")).addressCount()); // 129 bits wide, because an IPv6 `/0` holds 2^128 addresses. try testing.expectEqual(@as(u129, 1) << 128, (try Network.parse("::/0")).addressCount()); }
test exclude { var it = (try Network.parse("192.0.2.0/24")).exclude(try .parse("192.0.2.0/26")); try testing.expect(it.next().?.eql(try .parse("192.0.2.128/25"))); try testing.expect(it.next().?.eql(try .parse("192.0.2.64/26"))); try testing.expectEqual(null, it.next());
// Excluding a network of the other version removes nothing. var across = (try Network.parse("192.0.2.0/24")).exclude(try .parse("2001:db8::/32")); try testing.expect(across.next().?.eql(try .parse("192.0.2.0/24"))); try testing.expectEqual(null, across.next()); }
test isSingleAddress { try testing.expect((try Network.parse("192.0.2.1/32")).isSingleAddress()); try testing.expect((try Network.parse("2001:db8::1/128")).isSingleAddress()); try testing.expect(!(try Network.parse("192.0.2.0/24")).isSingleAddress()); }
test hostCount { // IPv4 holds back two addresses of the four, IPv6 only one. try testing.expectEqual(@as(u129, 2), (try Network.parse("192.0.2.0/30")).hostCount()); try testing.expectEqual(@as(u129, 3), (try Network.parse("2001:db8::/126")).hostCount()); }
test firstHost { try testing.expect((try Network.parse("192.0.2.0/24")).firstHost() .eql(try .parse("192.0.2.1"))); try testing.expect((try Network.parse("2001:db8::/64")).firstHost() .eql(try .parse("2001:db8::1"))); }
test lastHost { // One short of the broadcast address for IPv4; the last address // itself for IPv6. try testing.expect((try Network.parse("192.0.2.0/24")).lastHost() .eql(try .parse("192.0.2.254"))); try testing.expect((try Network.parse("2001:db8::/126")).lastHost() .eql(try .parse("2001:db8::3"))); }
test contains { const network = try Network.parse("192.168.1.0/24"); try testing.expect(network.contains(try .parse("192.168.1.42"))); try testing.expect(!network.contains(try .parse("192.168.2.42"))); // An address of the other version never falls inside, even as the // IPv4-mapped form of one that would. try testing.expect(!network.contains(try .parse("::ffff:192.168.1.42"))); }
test containsNetwork { const outer = try Network.parse("192.168.0.0/16"); try testing.expect(outer.containsNetwork(try Network.parse("192.168.1.0/24"))); // Networks of different versions never contain one another. try testing.expect(!outer.containsNetwork(try Network.parse("2001:db8::/32"))); }
test overlaps { const network = try Network.parse("192.168.1.0/24"); try testing.expect(network.overlaps(try Network.parse("192.168.0.0/16"))); try testing.expect(!network.overlaps(try Network.parse("192.168.2.0/24"))); // Networks of different versions never overlap. try testing.expect(!network.overlaps(try Network.parse("2001:db8::/32"))); }
test jsonStringify { var buffer: [128]u8 = undefined; var w: Writer = .fixed(&buffer); try std.json.Stringify.value(try Network.parse("192.0.2.0/24"), .{}, &w); // A JSON string holding exactly what `format` would have written. try testing.expectEqualStrings("\"192.0.2.0/24\"", w.buffered()); }
test jsonParse { // The hook is driven directly rather than through `parseFromSlice`, so // that this tests the function it is named after and nothing else; // `root.zig` has the end-to-end one. var scanner = std.json.Scanner.initCompleteInput(testing.allocator, "\"192.0.2.0/24\""); defer scanner.deinit(); const value = try Network.jsonParse(testing.allocator, &scanner, .{}); try testing.expect(value.eql(try Network.parse("192.0.2.0/24")));
// As strict inside a JSON document as anywhere else, though the reason is // flattened to `InvalidCharacter` on the way out. var bad = std.json.Scanner.initCompleteInput(testing.allocator, "\"192.0.2.1/24\""); defer bad.deinit(); try testing.expectError( error.InvalidCharacter, Network.jsonParse(testing.allocator, &bad, .{}), );
// Anything that is not a string is the wrong shape rather than a bad // address. var number = std.json.Scanner.initCompleteInput(testing.allocator, "42"); defer number.deinit(); try testing.expectError( error.UnexpectedToken, Network.jsonParse(testing.allocator, &number, .{}), ); }
test format { try testing.expectFmt("192.168.1.0/24", "{f}", .{try Network.parse("192.168.1.0/24")}); try testing.expectFmt("2001:db8::/32", "{f}", .{try Network.parse("2001:0db8::/32")}); }
test bufPrint { var buffer: [Network.max_text_len]u8 = undefined; try testing.expectEqualStrings( "2001:db8::/32", try (try Network.parse("2001:db8::/32")).bufPrint(&buffer), );
var small: [4]u8 = undefined; try testing.expectError(error.NoSpaceLeft, (try Network.parse("2001:db8::/32")).bufPrint(&small)); }
test merge { // A mixed slice: each version folds on its own, and IPv4 comes first. var list = [_]Network{ try .parse("2001:db8:8000::/33"), try .parse("192.0.2.64/26"), try .parse("2001:db8::/33"), try .parse("192.0.2.0/26"), try .parse("192.0.2.128/25"), }; const merged = Network.merge(&list); try testing.expectEqual(@as(usize, 2), merged.len); try testing.expect(merged[0].eql(try .parse("192.0.2.0/24"))); try testing.expect(merged[1].eql(try .parse("2001:db8::/32"))); }
test lessThan { try testing.expect(lessThan({}, try .parse("192.0.2.0/24"), try .parse("::/0"))); try testing.expect(!lessThan({}, try .parse("::/0"), try .parse("192.0.2.0/24"))); }
test join { try testing.expect(Network.join( try .parse("192.0.2.0/25"), try .parse("192.0.2.128/25"), ).?.eql(try .parse("192.0.2.0/24"))); // Nothing joins across the versions, however the halves look. try testing.expectEqual(null, Network.join( try .parse("192.0.2.0/25"), try .parse("2001:db8::/33"), )); }
test eql { try testing.expect((try Network.parse("2001:db8::/32")).eql(try Network.parse("2001:0db8::/32"))); // The two versions are never equal, and the prefix length is part of // the network. try testing.expect(!(try Network.parse("192.168.1.0/24")).eql(try Network.parse("192.168.1.0/25"))); }
test order { // Every IPv4 network sorts before every IPv6 one, and a containing // network before the networks inside it. try testing.expectEqual(.lt, (try Network.parse("0.0.0.0/0")).order(try Network.parse("::/0"))); try testing.expectEqual(.lt, (try Network.parse("192.168.0.0/16")) .order(try Network.parse("192.168.0.0/24"))); try testing.expectEqual(.eq, (try Network.parse("::/0")).order(try Network.parse("::/0"))); }
test isIpv6Text { // A `:` before the `/` is what marks the text as IPv6; anything after // the slash is a prefix length and says nothing about the version. try testing.expect(isIpv6Text("2001:db8::/32")); try testing.expect(isIpv6Text("::1")); try testing.expect(!isIpv6Text("192.168.1.0/24")); try testing.expect(!isIpv6Text("192.0.2.1")); }};
const testing = std.testing;
test "parse dispatches on version" { try testing.expect((try Network.parse("192.168.1.0/24")) == .ipv4); try testing.expect((try Network.parse("2001:db8::/32")) == .ipv6); try testing.expect((try Network.parse("::/0")) == .ipv6); try testing.expect((try Network.parse("10.0.0.0/255.0.0.0")) == .ipv4); try testing.expectEqual(.ipv6, (try Network.parse("2001:db8::/32")).family());
try testing.expectError(error.HasHostBits, Network.parse("192.168.1.1/24")); try testing.expectError(error.HasHostBits, Network.parse("2001:db8::1/32")); try testing.expectError(error.InvalidPrefixLength, Network.parse("2001:db8::/129")); try testing.expect(Network.isValid("10.0.0.0/8")); try testing.expect(!Network.isValid("10.0.0.1/8"));
try testing.expect((try Network.parseTruncate("192.168.1.130/24")).eql(try Network.parse("192.168.1.0/24"))); try testing.expect((try Network.parseTruncate("2001:db8::1/32")).eql(try Network.parse("2001:db8::/32")));}
test "the SPF options reach both versions" { try testing.expect((try Network.parseWithOptions("192.0.2.130/24", .spf)) .eql(try Network.parse("192.0.2.0/24"))); try testing.expect((try Network.parseWithOptions("2001:db8::1/32", .spf)) .eql(try Network.parse("2001:db8::/32"))); try testing.expectError( error.InvalidPrefix, Network.parseWithOptions("10.0.0.0/255.0.0.0", .spf), ); // `allow_netmask` is the IPv4 form's alone, and asking for it either way // changes nothing about an IPv6 network. try testing.expect((try Network.parseWithOptions("2001:db8::/32", .{ .allow_netmask = false })) .eql(try Network.parse("2001:db8::/32")));}
test "properties" { const v4 = try Network.parse("192.168.1.0/24"); try testing.expectEqual(@as(u8, 24), v4.prefixLen()); try testing.expectFmt("192.168.1.0", "{f}", .{v4.networkAddress()}); try testing.expectFmt("192.168.1.255", "{f}", .{v4.lastAddress()}); try testing.expectFmt("255.255.255.0", "{f}", .{v4.netmask()}); try testing.expectFmt("0.0.0.255", "{f}", .{v4.hostmask()}); try testing.expectEqual(@as(u129, 256), v4.addressCount());
const v6 = try Network.parse("2001:db8::/126"); try testing.expectFmt("2001:db8::", "{f}", .{v6.networkAddress()}); try testing.expectFmt("2001:db8::3", "{f}", .{v6.lastAddress()}); try testing.expectEqual(@as(u129, 4), v6.addressCount()); try testing.expectEqual(@as(u129, 1) << 128, Network.default_route_ipv6.addressCount());}
test "containment never crosses versions" { const v4 = try Network.parse("0.0.0.0/0"); const v6 = try Network.parse("::/0"); try testing.expect(v4.contains(try .parse("192.0.2.1"))); try testing.expect(!v4.contains(try .parse("::ffff:192.0.2.1"))); try testing.expect(v6.contains(try .parse("2001:db8::1"))); try testing.expect(!v6.contains(try .parse("192.0.2.1"))); try testing.expect(!v4.containsNetwork(v6)); try testing.expect(!v4.overlaps(v6)); try testing.expect(v4.containsNetwork(try .parse("10.0.0.0/8"))); try testing.expect(v6.overlaps(try .parse("2001:db8::/32")));}
test "format and ordering" { try testing.expectFmt("192.168.1.0/24", "{f}", .{try Network.parse("192.168.1.0/24")}); try testing.expectFmt("2001:db8::/32", "{f}", .{try Network.parse("2001:db8::/32")});
var buffer: [Network.max_text_len]u8 = undefined; try testing.expectEqualStrings("::/0", try Network.default_route_ipv6.bufPrint(&buffer));
try testing.expectEqual(.lt, (try Network.parse("255.0.0.0/8")).order(try Network.parse("::/0"))); try testing.expectEqual(.lt, (try Network.parse("10.0.0.0/8")).order(try Network.parse("10.0.0.0/16"))); try testing.expect(Network.fromHost(try .parse("192.0.2.1")).eql(try Network.parse("192.0.2.1/32"))); try testing.expect(Network.fromHost(try .parse("::1")).eql(try Network.parse("::1/128")));}