diff options
| author | Luna <git@l4.pm> | 2020-03-29 17:17:40 -0300 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-06-02 14:56:06 -0400 |
| commit | f02f4c08808c4f9f171825baf4a1b0d032f1e482 (patch) | |
| tree | c795137404f171eace9990536baf0a4613c470b6 /lib/std/net.zig | |
| parent | 9c200035f346c9687ccf9b490a704803a652138e (diff) | |
| download | zig-f02f4c08808c4f9f171825baf4a1b0d032f1e482.tar.gz zig-f02f4c08808c4f9f171825baf4a1b0d032f1e482.zip | |
Fix typo and add if_nametoindex
Diffstat (limited to 'lib/std/net.zig')
| -rw-r--r-- | lib/std/net.zig | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig index f757585f34..768fbab2b2 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -75,6 +75,8 @@ pub const Address = extern union { } } + /// Parse a given IPv6 address string into an Address. + /// Assumes the Scope ID of the address is fully numeric. pub fn parseIp6(buf: []const u8, port: u16) !Address { var result = Address{ .in6 = os.sockaddr_in6{ @@ -274,7 +276,7 @@ pub const Address = extern union { const resolved_scope_id = std.fmt.parseInt(u32, scope_id_value, 10) catch |err| blk: { if (err != err.InvalidCharacter) return err; break :blk if_nametoindex(scope_id_value); - } + }; result.in6.scope_id = resolved_scope_id; @@ -515,6 +517,23 @@ pub fn connectUnixSocket(path: []const u8) !fs.File { }; } +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.close(sockfd); + + std.mem.copy(u8, ifr.ifr_ifrn.name, &name); + + const rc = os.system.syscall3( + os.linux.SYS_ioctl, + @bitCast(usize, @as(isize, sockfd)), + os.linux.SIOCGIFINDEX, + @ptrToInt(&ifr), + ); + + return ifr.ifr_ifru.ifru_ivalue; +} + pub const AddressList = struct { arena: std.heap.ArenaAllocator, addrs: []Address, |
