Skip to content

Commit a5b4596

Browse files
committed
Fix prefix len when parsing a network
1 parent 3acd713 commit a5b4596

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/net.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ pub const Network = struct {
2323
};
2424
}
2525

26+
const ip = try std.net.Address.parseIp(s, 0);
2627
return .{
27-
.ip = try std.net.Address.parseIp(s, 0),
28+
.ip = ip,
29+
.prefix_len = switch (ip.any.family) {
30+
std.posix.AF.INET => 32,
31+
std.posix.AF.INET6 => 128,
32+
else => unreachable,
33+
},
2834
};
2935
}
3036

@@ -102,8 +108,11 @@ test "Network.parse" {
102108
const got_v6 = try std.fmt.bufPrint(&buf, "{f}", .{v6});
103109
try std.testing.expectEqualStrings("2001:0db8:0000:0000:0000:0000:0000:0000/32", got_v6);
104110

105-
const no_cidr = try Network.parse("10.0.0.1");
106-
try std.testing.expectEqual(0, no_cidr.prefix_len);
111+
const no_cidr_v4 = try Network.parse("10.0.0.1");
112+
try std.testing.expectEqual(32, no_cidr_v4.prefix_len);
113+
114+
const no_cidr_v6 = try Network.parse("2001:db8::1");
115+
try std.testing.expectEqual(128, no_cidr_v6.prefix_len);
107116
}
108117

109118
// Represents IPv4 or IPv6 bytes.

0 commit comments

Comments
 (0)