authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-04 23:11:52+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-05 12:05:34-05:00
loga26e9fa7238e972301f761db80434fa0205d1016
tree200bd4175a09ae63a7f862aa3546683cc7ed6f37
parent71e209521a0675a3ec06aba133ce8837e2ee9806

add special formatting for ipv4-mapped ipv6 addresses


2 files changed, 22 insertions(+), 6 deletions(-)

lib/std/net.zig+20-4
...@@ -117,10 +117,12 @@ pub const IpAddress = extern union {...@@ -117,10 +117,12 @@ pub const IpAddress = extern union {
117 ip_slice[10] = 0xff;117 ip_slice[10] = 0xff;
118 ip_slice[11] = 0xff;118 ip_slice[11] = 0xff;
119119
120 ip_slice[12] = @truncate(u8, addr >> 24 & 0xff);120 const ptr = @sliceToBytes((*const [1]u32)(&addr)[0..]);
121 ip_slice[13] = @truncate(u8, addr >> 16 & 0xff);121
122 ip_slice[14] = @truncate(u8, addr >> 8 & 0xff);122 ip_slice[12] = ptr[0];
123 ip_slice[15] = @truncate(u8, addr & 0xff);123 ip_slice[13] = ptr[1];
124 ip_slice[14] = ptr[2];
125 ip_slice[15] = ptr[3];
124 return result;126 return result;
125 } else {127 } else {
126 const digit = try std.fmt.charToDigit(c, 16);128 const digit = try std.fmt.charToDigit(c, 16);
...@@ -269,6 +271,20 @@ pub const IpAddress = extern union {...@@ -269,6 +271,20 @@ pub const IpAddress = extern union {
269 },271 },
270 os.AF_INET6 => {272 os.AF_INET6 => {
271 const port = mem.bigToNative(u16, self.in6.port);273 const port = mem.bigToNative(u16, self.in6.port);
274 if (mem.eql(u8, self.in6.addr[0..12], [_]u8{0,0,0,0,0,0,0,0,0,0,0xff,0xff})) {
275 try std.fmt.format(
276 context,
277 Errors,
278 output,
279 "[::ffff:{}.{}.{}.{}]:{}",
280 self.in6.addr[12],
281 self.in6.addr[13],
282 self.in6.addr[14],
283 self.in6.addr[15],
284 port,
285 );
286 return;
287 }
272 const big_endian_parts = @ptrCast(*align(1) const [8]u16, &self.in6.addr);288 const big_endian_parts = @ptrCast(*align(1) const [8]u16, &self.in6.addr);
273 const native_endian_parts = switch (builtin.endian) {289 const native_endian_parts = switch (builtin.endian) {
274 .Big => big_endian_parts.*,290 .Big => big_endian_parts.*,
lib/std/net/test.zig+2-2
...@@ -14,7 +14,7 @@ test "parse and render IPv6 addresses" {...@@ -14,7 +14,7 @@ test "parse and render IPv6 addresses" {
14 "::1234:5678",14 "::1234:5678",
15 "2001:db8::1234:5678",15 "2001:db8::1234:5678",
16 "FF01::FB%1234",16 "FF01::FB%1234",
17 "::ffff:123.123.123.123",17 "::ffff:123.5.123.5",
18 };18 };
19 const printed = [_][]const u8{19 const printed = [_][]const u8{
20 "ff01::fb",20 "ff01::fb",
...@@ -25,7 +25,7 @@ test "parse and render IPv6 addresses" {...@@ -25,7 +25,7 @@ test "parse and render IPv6 addresses" {
25 "::1234:5678",25 "::1234:5678",
26 "2001:db8::1234:5678",26 "2001:db8::1234:5678",
27 "ff01::fb",27 "ff01::fb",
28 "::ffff:7b7b:7b7b",28 "::ffff:123.5.123.5",
29 };29 };
30 for (ips) |ip, i| {30 for (ips) |ip, i| {
31 var addr = net.IpAddress.parseIp6(ip, 0) catch unreachable;31 var addr = net.IpAddress.parseIp6(ip, 0) catch unreachable;