Only resolve scope id when needed

master
Luna 2020-03-29 17:45:34 -03:00 committed by Andrew Kelley
parent f02f4c0880
commit 2c641c93da
2 changed files with 12 additions and 10 deletions

View File

@ -184,7 +184,6 @@ pub const Address = extern union {
} }
pub fn resolveIp6(buf: []const u8, port: u16) !Address { 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 // FIXME: this is a very bad implementation, since it's only a copy
// of parseIp6 with alphanumerical scope id support // of parseIp6 with alphanumerical scope id support
var result = Address{ var result = Address{
@ -205,7 +204,7 @@ pub const Address = extern union {
var abbrv = false; var abbrv = false;
var scope_id = 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; var scope_id_index: usize = 0;
for (buf) |c, i| { for (buf) |c, i| {
@ -273,10 +272,13 @@ pub const Address = extern union {
return error.Incomplete; return error.Incomplete;
} }
const resolved_scope_id = std.fmt.parseInt(u32, scope_id_value, 10) catch |err| blk: { var resolved_scope_id: u32 = 0;
if (err != err.InvalidCharacter) return err; if (std.mem.len(scope_id_value) > 0) {
break :blk if_nametoindex(scope_id_value); 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; 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); var sockfd = try os.socket(os.AF_UNIX, os.SOCK_DGRAM | os.SOCK_CLOEXEC, 0);
defer os.close(sockfd); 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( const rc = os.system.syscall3(
os.linux.SYS_ioctl, os.linux.SYS_ioctl,

View File

@ -1720,7 +1720,7 @@ pub const ifmap = struct {
pub const ifreq = extern union { pub const ifreq = extern union {
ifr_ifrn: struct { ifr_ifrn: struct {
ifrn_name: [IFNAMESIZE]u8, name: [IFNAMESIZE]u8,
}, },
ifr_ifru: struct { ifr_ifru: struct {
ifru_addr: sockaddr, ifru_addr: sockaddr,
@ -1729,8 +1729,8 @@ pub const ifreq = extern union {
ifru_netmask: sockaddr, ifru_netmask: sockaddr,
ifru_hwaddr: sockaddr, ifru_hwaddr: sockaddr,
ifru_flags: i16, ifru_flags: i16,
ifru_ivalue: i16, ifru_ivalue: i32,
ifru_mtu: i16, ifru_mtu: i32,
ifru_map: ifmap, ifru_map: ifmap,
ifru_slave: [IFNAMESIZE]u8, ifru_slave: [IFNAMESIZE]u8,
ifru_newname: [IFNAMESIZE]u8, ifru_newname: [IFNAMESIZE]u8,