Commit 8a83fc7eba

Zenomat <ae@groundzeno.net>
2025-04-04 13:40:44
std.net: Implement if_nametoindex for windows (#22555)
1 parent 155b34b
Changed files (2)
lib
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;
     }
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");
 }