Skip to content

Commit

Permalink
Resolve scope IDs using IPv6 sockets
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
The-King-of-Toasters authored and andrewrk committed Sep 24, 2021
1 parent 1e7009a commit a032fd0
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions lib/std/x/os/net.zig
Expand Up @@ -27,7 +27,7 @@ pub fn resolveScopeId(name: []const u8) !u32 {
return rc;
}

const fd = try os.socket(os.AF.UNIX, os.SOCK.DGRAM, 0);
const fd = try os.socket(os.AF.INET, os.SOCK.DGRAM, 0);
defer os.closeSocket(fd);

var f: os.ifreq = undefined;
Expand Down Expand Up @@ -566,21 +566,17 @@ test "ipv6: parse & format" {

test "ipv6: parse & format addresses with scope ids" {
if (!have_ifnamesize) return error.SkipZigTest;

const inputs = [_][]const u8{
"FF01::FB%lo",
};

const outputs = [_][]const u8{
"ff01::fb%1",
const iface = if (native_os.tag == .linux)
"lo"
else
"lo0";
const input = "FF01::FB%" ++ iface;
const output = "ff01::fb%1";

const parsed = IPv6.parse(input) catch |err| switch (err) {
error.InterfaceNotFound => return,
else => return err,
};

for (inputs) |input, i| {
const parsed = IPv6.parse(input) catch |err| switch (err) {
error.InterfaceNotFound => continue,
else => return err,
};

try testing.expectFmt(outputs[i], "{}", .{parsed});
}
try testing.expectFmt(output, "{}", .{parsed});
}

0 comments on commit a032fd0

Please sign in to comment.