authorgravatar for dev@sgregoratto.meStephen Gregoratto <dev@sgregoratto.me> 2021-09-18 23:26:55+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-24 13:50:18-04:00
loga032fd01e88be2c6e8d0cfb0ccd3d9859c9dffdc
tree56a13b671117c0d3c815c5b2c7c0b55f8c4dbec4
parent1e7009a9d982faa466517063452ed7d299b66966

Resolve scope IDs using IPv6 sockets

On certain systems (Solaris), resolving the scope id from an interface name can only be done on AF_INET-domain sockets. While we're here, simplify the test while we're here, since there's only one address. Also note that the loopback interface name is not stable across OSs. BSDs and Solaris use `lo0` whilst Linux uses `l0`.

1 files changed, 12 insertions(+), 16 deletions(-)

lib/std/x/os/net.zig+12-16
...@@ -27,7 +27,7 @@ pub fn resolveScopeId(name: []const u8) !u32 {...@@ -27,7 +27,7 @@ pub fn resolveScopeId(name: []const u8) !u32 {
27 return rc;27 return rc;
28 }28 }
2929
30 const fd = try os.socket(os.AF.UNIX, os.SOCK.DGRAM, 0);30 const fd = try os.socket(os.AF.INET, os.SOCK.DGRAM, 0);
31 defer os.closeSocket(fd);31 defer os.closeSocket(fd);
3232
33 var f: os.ifreq = undefined;33 var f: os.ifreq = undefined;
...@@ -566,21 +566,17 @@ test "ipv6: parse & format" {...@@ -566,21 +566,17 @@ test "ipv6: parse & format" {
566566
567test "ipv6: parse & format addresses with scope ids" {567test "ipv6: parse & format addresses with scope ids" {
568 if (!have_ifnamesize) return error.SkipZigTest;568 if (!have_ifnamesize) return error.SkipZigTest;
569569 const iface = if (native_os.tag == .linux)
570 const inputs = [_][]const u8{570 "lo"
571 "FF01::FB%lo",571 else
572 };572 "lo0";
573573 const input = "FF01::FB%" ++ iface;
574 const outputs = [_][]const u8{574 const output = "ff01::fb%1";
575 "ff01::fb%1",575
576 const parsed = IPv6.parse(input) catch |err| switch (err) {
577 error.InterfaceNotFound => return,
578 else => return err,
576 };579 };
577580
578 for (inputs) |input, i| {581 try testing.expectFmt(output, "{}", .{parsed});
579 const parsed = IPv6.parse(input) catch |err| switch (err) {
580 error.InterfaceNotFound => continue,
581 else => return err,
582 };
583
584 try testing.expectFmt(outputs[i], "{}", .{parsed});
585 }
586}582}