authorgravatar for git@l4.pmLuna <git@l4.pm> 2020-03-29 17:17:40-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-02 14:56:06-04:00
logf02f4c08808c4f9f171825baf4a1b0d032f1e482
treec795137404f171eace9990536baf0a4613c470b6
parent9c200035f346c9687ccf9b490a704803a652138e

Fix typo and add if_nametoindex


1 files changed, 20 insertions(+), 1 deletions(-)

lib/std/net.zig+20-1
......@@ -75,6 +75,8 @@ pub const Address = extern union {
7575 }
7676 }
7777
78 /// Parse a given IPv6 address string into an Address.
79 /// Assumes the Scope ID of the address is fully numeric.
7880 pub fn parseIp6(buf: []const u8, port: u16) !Address {
7981 var result = Address{
8082 .in6 = os.sockaddr_in6{
......@@ -274,7 +276,7 @@ pub const Address = extern union {
274276 const resolved_scope_id = std.fmt.parseInt(u32, scope_id_value, 10) catch |err| blk: {
275277 if (err != err.InvalidCharacter) return err;
276278 break :blk if_nametoindex(scope_id_value);
277 }
279 };
278280
279281 result.in6.scope_id = resolved_scope_id;
280282
......@@ -515,6 +517,23 @@ pub fn connectUnixSocket(path: []const u8) !fs.File {
515517 };
516518}
517519
520fn if_nametoindex(name: []const u8) !u32 {
521 var ifr: os.ifreq = undefined;
522 var sockfd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM | os.SOCK_CLOEXEC, 0);
523 defer os.close(sockfd);
524
525 std.mem.copy(u8, ifr.ifr_ifrn.name, &name);
526
527 const rc = os.system.syscall3(
528 os.linux.SYS_ioctl,
529 @bitCast(usize, @as(isize, sockfd)),
530 os.linux.SIOCGIFINDEX,
531 @ptrToInt(&ifr),
532 );
533
534 return ifr.ifr_ifru.ifru_ivalue;
535}
536
518537pub const AddressList = struct {
519538 arena: std.heap.ArenaAllocator,
520539 addrs: []Address,