zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit a2ea4b02bc35d0cbde1716705daad2e0a395fa12 (tree)
parent dac350f7c839891c1165b1fa6f8d08f44f047743
Author: Zenomat <ae@groundzeno.net>
Date:   Fri,  4 Apr 2025 13:40:44 +0200

std.net: Implement if_nametoindex for windows (#22555)


Diffstat:
Mlib/std/net.zig | 13+++++++++++++
Mlib/std/net/test.zig | 9+++++----
2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/lib/std/net.zig b/lib/std/net.zig @@ -785,6 +785,19 @@ fn if_nametoindex(name: []const u8) IPv6InterfaceError!u32 { return @as(u32, @bitCast(index)); } + if (native_os == .windows) { + if (name.len >= posix.IFNAMESIZE) + return error.NameTooLong; + + var interface_name: [posix.IFNAMESIZE:0]u8 = undefined; + @memcpy(interface_name[0..name.len], name); + interface_name[name.len] = 0; + const index = std.os.windows.ws2_32.if_nametoindex(@as([*:0]const u8, &interface_name)); + if (index == 0) + return error.InterfaceNotFound; + return index; + } + @compileError("std.net.if_nametoindex unimplemented for this OS"); } diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig @@ -129,15 +129,16 @@ test "parse and render IPv6 addresses" { try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0)); try testing.expectError(error.Incomplete, net.Address.parseIp6("1", 0)); // TODO Make this test pass on other operating systems. - if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin()) { + if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin() or builtin.os.tag == .windows) { try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0)); - try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%wlp3s0s0s0s0s0s0s0s0", 0)); + // Assumes IFNAMESIZE will always be a multiple of 2 + try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0)); try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%12345678901234", 0)); } } test "invalid but parseable IPv6 scope ids" { - if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin()) { + if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin() and builtin.os.tag != .windows) { // Currently, resolveIp6 with alphanumerical scope IDs only works on Linux. // TODO Make this test pass on other operating systems. return error.SkipZigTest; @@ -261,7 +262,7 @@ test "listen on a port, send bytes, receive bytes" { } test "listen on an in use port" { - if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin()) { + if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin() and builtin.os.tag != .windows) { // TODO build abstractions for other operating systems return error.SkipZigTest; }