diff options
| author | John Schmidt <john.schmidt.h@gmail.com> | 2022-01-11 17:38:49 +0100 |
|---|---|---|
| committer | John Schmidt <john.schmidt.h@gmail.com> | 2022-01-12 19:32:46 +0100 |
| commit | 66fe5bb0d8c5e6279e2b68459b6b37d9244210bc (patch) | |
| tree | 0de1bbb4cef77af19e3fb20abf32f20fffd76ef3 /lib/std/net.zig | |
| parent | c4681b4889652d5228a84ac7af5ad5e17ac39055 (diff) | |
| download | zig-66fe5bb0d8c5e6279e2b68459b6b37d9244210bc.tar.gz zig-66fe5bb0d8c5e6279e2b68459b6b37d9244210bc.zip | |
Use libc if_nametoindex if available when parsing IPs
Fixes https://github.com/ziglang/zig/issues/10521 and makes a couple of
additional tests pass when linking libc.
Diffstat (limited to 'lib/std/net.zig')
| -rw-r--r-- | lib/std/net.zig | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig index 1165ac6073..78d59ae9c0 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -636,17 +636,35 @@ pub fn connectUnixSocket(path: []const u8) !Stream { } fn if_nametoindex(name: []const u8) !u32 { - var ifr: os.ifreq = undefined; - var sockfd = try os.socket(os.AF.UNIX, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0); - defer os.closeSocket(sockfd); + if (builtin.target.os.tag == .linux) { + var ifr: os.ifreq = undefined; + var sockfd = try os.socket(os.AF.UNIX, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0); + defer os.closeSocket(sockfd); + + std.mem.copy(u8, &ifr.ifrn.name, name); + ifr.ifrn.name[name.len] = 0; + + // TODO investigate if this needs to be integrated with evented I/O. + try os.ioctl_SIOCGIFINDEX(sockfd, &ifr); - std.mem.copy(u8, &ifr.ifrn.name, name); - ifr.ifrn.name[name.len] = 0; + return @bitCast(u32, ifr.ifru.ivalue); + } + + if (builtin.link_libc) { + if (name.len >= os.IFNAMESIZE) + return error.NameTooLong; - // TODO investigate if this needs to be integrated with evented I/O. - try os.ioctl_SIOCGIFINDEX(sockfd, &ifr); + var if_name: [os.IFNAMESIZE:0]u8 = undefined; + std.mem.copy(u8, &if_name, name); + if_name[name.len] = 0; + const if_slice = if_name[0..name.len :0]; + const index = os.system.if_nametoindex(if_slice); + if (index == 0) + return error.InterfaceNotFound; + return @bitCast(u32, index); + } - return @bitCast(u32, ifr.ifru.ivalue); + @compileError("std.net.if_nametoindex unimplemented for this OS"); } pub const AddressList = struct { |
