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 {
2727 return rc;
2828 }
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);
3131 defer os.closeSocket(fd);
3232
3333 var f: os.ifreq = undefined;
......@@ -566,21 +566,17 @@ test "ipv6: parse & format" {
566566
567567test "ipv6: parse & format addresses with scope ids" {
568568 if (!have_ifnamesize) return error.SkipZigTest;
569
570 const inputs = [_][]const u8{
571 "FF01::FB%lo",
572 };
573
574 const outputs = [_][]const u8{
575 "ff01::fb%1",
569 const iface = if (native_os.tag == .linux)
570 "lo"
571 else
572 "lo0";
573 const input = "FF01::FB%" ++ iface;
574 const output = "ff01::fb%1";
575
576 const parsed = IPv6.parse(input) catch |err| switch (err) {
577 error.InterfaceNotFound => return,
578 else => return err,
576579 };
577580
578 for (inputs) |input, i| {
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 }
581 try testing.expectFmt(output, "{}", .{parsed});
586582}