authorgravatar for ae@groundzeno.netZenomat <ae@groundzeno.net> 2025-04-04 13:40:44+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-04-04 11:40:44+00:00
log8a83fc7eba630458262148547c134821024b5142
tree7df1f63e34f0cc75c45a2dbc371d498b140e2a5c
parent155b34ba0569d264650e341eab58c1f704c35bfd
signaturebadge-check Signed by PGP key B5690EEEBB952194

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


2 files changed, 18 insertions(+), 4 deletions(-)

lib/std/net.zig+13
...@@ -785,6 +785,19 @@ fn if_nametoindex(name: []const u8) IPv6InterfaceError!u32 {...@@ -785,6 +785,19 @@ fn if_nametoindex(name: []const u8) IPv6InterfaceError!u32 {
785 return @as(u32, @bitCast(index));785 return @as(u32, @bitCast(index));
786 }786 }
787787
788 if (native_os == .windows) {
789 if (name.len >= posix.IFNAMESIZE)
790 return error.NameTooLong;
791
792 var interface_name: [posix.IFNAMESIZE:0]u8 = undefined;
793 @memcpy(interface_name[0..name.len], name);
794 interface_name[name.len] = 0;
795 const index = std.os.windows.ws2_32.if_nametoindex(@as([*:0]const u8, &interface_name));
796 if (index == 0)
797 return error.InterfaceNotFound;
798 return index;
799 }
800
788 @compileError("std.net.if_nametoindex unimplemented for this OS");801 @compileError("std.net.if_nametoindex unimplemented for this OS");
789}802}
790803
lib/std/net/test.zig+5-4
...@@ -129,15 +129,16 @@ test "parse and render IPv6 addresses" {...@@ -129,15 +129,16 @@ test "parse and render IPv6 addresses" {
129 try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0));129 try testing.expectError(error.InvalidIpv4Mapping, net.Address.parseIp6("::123.123.123.123", 0));
130 try testing.expectError(error.Incomplete, net.Address.parseIp6("1", 0));130 try testing.expectError(error.Incomplete, net.Address.parseIp6("1", 0));
131 // TODO Make this test pass on other operating systems.131 // TODO Make this test pass on other operating systems.
132 if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin()) {132 if (builtin.os.tag == .linux or comptime builtin.os.tag.isDarwin() or builtin.os.tag == .windows) {
133 try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0));133 try testing.expectError(error.Incomplete, net.Address.resolveIp6("ff01::fb%", 0));
134 try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%wlp3s0s0s0s0s0s0s0s0", 0));134 // Assumes IFNAMESIZE will always be a multiple of 2
135 try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%wlp3" ++ "s0" ** @divExact(std.posix.IFNAMESIZE - 4, 2), 0));
135 try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%12345678901234", 0));136 try testing.expectError(error.Overflow, net.Address.resolveIp6("ff01::fb%12345678901234", 0));
136 }137 }
137}138}
138139
139test "invalid but parseable IPv6 scope ids" {140test "invalid but parseable IPv6 scope ids" {
140 if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin()) {141 if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin() and builtin.os.tag != .windows) {
141 // Currently, resolveIp6 with alphanumerical scope IDs only works on Linux.142 // Currently, resolveIp6 with alphanumerical scope IDs only works on Linux.
142 // TODO Make this test pass on other operating systems.143 // TODO Make this test pass on other operating systems.
143 return error.SkipZigTest;144 return error.SkipZigTest;
...@@ -261,7 +262,7 @@ test "listen on a port, send bytes, receive bytes" {...@@ -261,7 +262,7 @@ test "listen on a port, send bytes, receive bytes" {
261}262}
262263
263test "listen on an in use port" {264test "listen on an in use port" {
264 if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin()) {265 if (builtin.os.tag != .linux and comptime !builtin.os.tag.isDarwin() and builtin.os.tag != .windows) {
265 // TODO build abstractions for other operating systems266 // TODO build abstractions for other operating systems
266 return error.SkipZigTest;267 return error.SkipZigTest;
267 }268 }