authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-13 17:58:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:49-07:00
log923a7bdd7e390c5dbb0fcf2686998180ba41cc88
tree2b0b8b6e9aca0a3116d7a585d6bb3b39a10a3e45
parente0e463bcf711f660cf8df5e8d31e82f855c0a967

std.Io.net: fix parsing IPv4-mapped IPv6 addresses


2 files changed, 28 insertions(+), 45 deletions(-)

lib/std/Io/net.zig+8-1
......@@ -438,6 +438,13 @@ pub const Ip6Address = struct {
438438
439439 pub fn parse(text: []const u8) Parsed {
440440 if (text.len < 2) return .unexpected_end;
441 if (std.ascii.startsWithIgnoreCase(text, "::ffff:")) ip4_mapped: {
442 const a4 = (Ip4Address.parse(text["::ffff:".len..], 0) catch break :ip4_mapped).bytes;
443 return .{ .success = .{
444 .bytes = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a4[0], a4[1], a4[2], a4[3] },
445 .interface_name = null,
446 } };
447 }
441448 // Has to be u16 elements to handle 3-digit hex numbers from compression.
442449 var parts: [8]u16 = @splat(0);
443450 var parts_i: u8 = 0;
......@@ -623,7 +630,7 @@ pub const Ip6Address = struct {
623630
624631 /// This is a pure function but it cannot handle IPv6 addresses that have
625632 /// scope ids ("%foo" at the end). To also handle those, `resolve` must be
626 /// called instead.
633 /// called instead, or the lower level `Unresolved` API may be used.
627634 pub fn parse(buffer: []const u8, port: u16) ParseError!Ip6Address {
628635 switch (Unresolved.parse(buffer)) {
629636 .success => |p| return .{
lib/std/Io/net/test.zig+20-44
......@@ -46,46 +46,26 @@ test "parse IPv6 address, check raw bytes" {
4646}
4747
4848test "parse and render IPv6 addresses" {
49 // TODO make this test parsing and rendering only, then it doesn't need I/O
50 const io = testing.io;
49 try testParseAndRenderIp6Address("FF01:0:0:0:0:0:0:FB", "ff01::fb");
50 try testParseAndRenderIp6Address("FF01::Fb", "ff01::fb");
51 try testParseAndRenderIp6Address("::1", "::1");
52 try testParseAndRenderIp6Address("::", "::");
53 try testParseAndRenderIp6Address("1::", "1::");
54 try testParseAndRenderIp6Address("2001:db8::", "2001:db8::");
55 try testParseAndRenderIp6Address("::1234:5678", "::1234:5678");
56 try testParseAndRenderIp6Address("2001:db8::1234:5678", "2001:db8::1234:5678");
57 try testParseAndRenderIp6Address("FF01::FB%1234", "ff01::fb%1234");
58 try testParseAndRenderIp6Address("::ffff:123.5.123.5", "::ffff:123.5.123.5");
59}
5160
61fn testParseAndRenderIp6Address(input: []const u8, expected_output: []const u8) !void {
5262 var buffer: [100]u8 = undefined;
53 const ips = [_][]const u8{
54 "FF01:0:0:0:0:0:0:FB",
55 "FF01::Fb",
56 "::1",
57 "::",
58 "1::",
59 "2001:db8::",
60 "::1234:5678",
61 "2001:db8::1234:5678",
62 "FF01::FB%1234",
63 "::ffff:123.5.123.5",
64 };
65 const printed = [_][]const u8{
66 "ff01::fb",
67 "ff01::fb",
68 "::1",
69 "::",
70 "1::",
71 "2001:db8::",
72 "::1234:5678",
73 "2001:db8::1234:5678",
74 "ff01::fb%1234",
75 "::ffff:123.5.123.5",
76 };
77 for (ips, 0..) |ip, i| {
78 const addr = net.IpAddress.parseIp6(ip, 0) catch unreachable;
79 var newIp = std.fmt.bufPrint(buffer[0..], "{f}", .{addr}) catch unreachable;
80 try testing.expect(std.mem.eql(u8, printed[i], newIp[1 .. newIp.len - 3]));
81
82 if (builtin.os.tag == .linux) {
83 const addr_via_resolve = net.IpAddress.resolveIp6(io, ip, 0) catch unreachable;
84 var newResolvedIp = std.fmt.bufPrint(buffer[0..], "{f}", .{addr_via_resolve}) catch unreachable;
85 try testing.expect(std.mem.eql(u8, printed[i], newResolvedIp[1 .. newResolvedIp.len - 3]));
86 }
87 }
63 const parsed = net.Ip6Address.Unresolved.parse(input);
64 const actual_printed = try std.fmt.bufPrint(&buffer, "{f}", .{parsed.success});
65 try testing.expectEqualStrings(expected_output, actual_printed);
66}
8867
68test "IPv6 address parse failures" {
8969 try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6(":::", 0));
9070 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("FF001::FB", 0));
9171 try testing.expectError(error.InvalidCharacter, net.IpAddress.parseIp6("FF01::Fb:zig", 0));
......@@ -93,13 +73,9 @@ test "parse and render IPv6 addresses" {
9373 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("FF01:", 0));
9474 try testing.expectError(error.InvalidIpv4Mapping, net.IpAddress.parseIp6("::123.123.123.123", 0));
9575 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("1", 0));
96 // TODO Make this test pass on other operating systems.
97 if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin() or builtin.os.tag == .windows) {
98 try testing.expectError(error.Incomplete, net.IpAddress.resolveIp6(io, "ff01::fb%", 0));
99 // Assumes IFNAMESIZE will always be a multiple of 2
100 try testing.expectError(error.Overflow, net.IpAddress.resolveIp6(io, "ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0));
101 try testing.expectError(error.Overflow, net.IpAddress.resolveIp6(io, "ff01::fb%12345678901234", 0));
102 }
76 try testing.expectError(error.Incomplete, net.IpAddress.parseIp6("ff01::fb%", 0));
77 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0));
78 try testing.expectError(error.Overflow, net.IpAddress.parseIp6("ff01::fb%12345678901234", 0));
10379}
10480
10581test "invalid but parseable IPv6 scope ids" {