authorgravatar for git@l4.pmLuna <git@l4.pm> 2020-03-29 17:45:34-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-02 14:56:06-04:00
log2c641c93da1babed3d53bf705f3d6a84414512e7
treec8adebe80256b5207dce05cb59999c10cf7148db
parentf02f4c08808c4f9f171825baf4a1b0d032f1e482

Only resolve scope id when needed


2 files changed, 12 insertions(+), 10 deletions(-)

lib/std/net.zig+9-7
...@@ -184,7 +184,6 @@ pub const Address = extern union {...@@ -184,7 +184,6 @@ pub const Address = extern union {
184 }184 }
185185
186 pub fn resolveIp6(buf: []const u8, port: u16) !Address {186 pub fn resolveIp6(buf: []const u8, port: u16) !Address {
187 // FIXME: implement if_nametoindex
188 // FIXME: this is a very bad implementation, since it's only a copy187 // FIXME: this is a very bad implementation, since it's only a copy
189 // of parseIp6 with alphanumerical scope id support188 // of parseIp6 with alphanumerical scope id support
190 var result = Address{189 var result = Address{
...@@ -205,7 +204,7 @@ pub const Address = extern union {...@@ -205,7 +204,7 @@ pub const Address = extern union {
205 var abbrv = false;204 var abbrv = false;
206205
207 var scope_id = false;206 var scope_id = false;
208 var scope_id_value: [32]u8 = undefined;207 var scope_id_value: [16]u8 = undefined;
209 var scope_id_index: usize = 0;208 var scope_id_index: usize = 0;
210209
211 for (buf) |c, i| {210 for (buf) |c, i| {
...@@ -273,10 +272,13 @@ pub const Address = extern union {...@@ -273,10 +272,13 @@ pub const Address = extern union {
273 return error.Incomplete;272 return error.Incomplete;
274 }273 }
275274
276 const resolved_scope_id = std.fmt.parseInt(u32, scope_id_value, 10) catch |err| blk: {275 var resolved_scope_id: u32 = 0;
277 if (err != err.InvalidCharacter) return err;276 if (std.mem.len(scope_id_value) > 0) {
278 break :blk if_nametoindex(scope_id_value);277 resolved_scope_id = std.fmt.parseInt(u32, &scope_id_value, 10) catch |err| blk: {
279 };278 if (err != error.InvalidCharacter) return err;
279 break :blk try if_nametoindex(&scope_id_value);
280 };
281 }
280282
281 result.in6.scope_id = resolved_scope_id;283 result.in6.scope_id = resolved_scope_id;
282284
...@@ -522,7 +524,7 @@ fn if_nametoindex(name: []const u8) !u32 {...@@ -522,7 +524,7 @@ fn if_nametoindex(name: []const u8) !u32 {
522 var sockfd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM | os.SOCK_CLOEXEC, 0);524 var sockfd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM | os.SOCK_CLOEXEC, 0);
523 defer os.close(sockfd);525 defer os.close(sockfd);
524526
525 std.mem.copy(u8, ifr.ifr_ifrn.name, &name);527 std.mem.copy(u8, &ifr.ifr_ifrn.name, name);
526528
527 const rc = os.system.syscall3(529 const rc = os.system.syscall3(
528 os.linux.SYS_ioctl,530 os.linux.SYS_ioctl,
lib/std/os/bits/linux.zig+3-3
...@@ -1720,7 +1720,7 @@ pub const ifmap = struct {...@@ -1720,7 +1720,7 @@ pub const ifmap = struct {
17201720
1721pub const ifreq = extern union {1721pub const ifreq = extern union {
1722 ifr_ifrn: struct {1722 ifr_ifrn: struct {
1723 ifrn_name: [IFNAMESIZE]u8,1723 name: [IFNAMESIZE]u8,
1724 },1724 },
1725 ifr_ifru: struct {1725 ifr_ifru: struct {
1726 ifru_addr: sockaddr,1726 ifru_addr: sockaddr,
...@@ -1729,8 +1729,8 @@ pub const ifreq = extern union {...@@ -1729,8 +1729,8 @@ pub const ifreq = extern union {
1729 ifru_netmask: sockaddr,1729 ifru_netmask: sockaddr,
1730 ifru_hwaddr: sockaddr,1730 ifru_hwaddr: sockaddr,
1731 ifru_flags: i16,1731 ifru_flags: i16,
1732 ifru_ivalue: i16,1732 ifru_ivalue: i32,
1733 ifru_mtu: i16,1733 ifru_mtu: i32,
1734 ifru_map: ifmap,1734 ifru_map: ifmap,
1735 ifru_slave: [IFNAMESIZE]u8,1735 ifru_slave: [IFNAMESIZE]u8,
1736 ifru_newname: [IFNAMESIZE]u8,1736 ifru_newname: [IFNAMESIZE]u8,