From 2c641c93da1babed3d53bf705f3d6a84414512e7 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 29 Mar 2020 17:45:34 -0300 Subject: Only resolve scope id when needed --- lib/std/net.zig | 16 +++++++++------- lib/std/os/bits/linux.zig | 6 +++--- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'lib/std') diff --git a/lib/std/net.zig b/lib/std/net.zig index 768fbab2b2..5cc9a8f740 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -184,7 +184,6 @@ pub const Address = extern union { } pub fn resolveIp6(buf: []const u8, port: u16) !Address { - // FIXME: implement if_nametoindex // FIXME: this is a very bad implementation, since it's only a copy // of parseIp6 with alphanumerical scope id support var result = Address{ @@ -205,7 +204,7 @@ pub const Address = extern union { var abbrv = false; var scope_id = false; - var scope_id_value: [32]u8 = undefined; + var scope_id_value: [16]u8 = undefined; var scope_id_index: usize = 0; for (buf) |c, i| { @@ -273,10 +272,13 @@ pub const Address = extern union { return error.Incomplete; } - 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); - }; + var resolved_scope_id: u32 = 0; + if (std.mem.len(scope_id_value) > 0) { + resolved_scope_id = std.fmt.parseInt(u32, &scope_id_value, 10) catch |err| blk: { + if (err != error.InvalidCharacter) return err; + break :blk try if_nametoindex(&scope_id_value); + }; + } result.in6.scope_id = resolved_scope_id; @@ -522,7 +524,7 @@ fn if_nametoindex(name: []const u8) !u32 { 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); + std.mem.copy(u8, &ifr.ifr_ifrn.name, name); const rc = os.system.syscall3( os.linux.SYS_ioctl, diff --git a/lib/std/os/bits/linux.zig b/lib/std/os/bits/linux.zig index ccb04d30a2..773a61b3ce 100644 --- a/lib/std/os/bits/linux.zig +++ b/lib/std/os/bits/linux.zig @@ -1720,7 +1720,7 @@ pub const ifmap = struct { pub const ifreq = extern union { ifr_ifrn: struct { - ifrn_name: [IFNAMESIZE]u8, + name: [IFNAMESIZE]u8, }, ifr_ifru: struct { ifru_addr: sockaddr, @@ -1729,8 +1729,8 @@ pub const ifreq = extern union { ifru_netmask: sockaddr, ifru_hwaddr: sockaddr, ifru_flags: i16, - ifru_ivalue: i16, - ifru_mtu: i16, + ifru_ivalue: i32, + ifru_mtu: i32, ifru_map: ifmap, ifru_slave: [IFNAMESIZE]u8, ifru_newname: [IFNAMESIZE]u8, -- cgit v1.2.3