Compare commits

...

2 commits

2 changed files with 14 additions and 10 deletions

View file

@ -8,6 +8,7 @@ const MDNSError = error{
NotResponse, NotResponse,
NoMatchingAddress, NoMatchingAddress,
AddressBadFormat, AddressBadFormat,
SocketBindFail,
}; };
pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP { pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP {
@ -43,6 +44,9 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket {
if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.RCVTIMEO, &timeout, @intCast(@sizeOf(std.c.timeval))) == -1) { if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.RCVTIMEO, &timeout, @intCast(@sizeOf(std.c.timeval))) == -1) {
return MDNSError.SocketInitFail; return MDNSError.SocketInitFail;
} }
if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.BINDTOIFINDEX, &ip_info.interface, @sizeOf(c_int)) == -1) {
return MDNSError.SocketBindFail;
}
return sock; return sock;
} }
@ -81,13 +85,7 @@ fn construct_mdns_query(domain: util.Domain, ip_info: util.IPInfo, buff: []u8) !
fn get_target_address(ip_info: util.IPInfo) !std.net.Address { fn get_target_address(ip_info: util.IPInfo) !std.net.Address {
const target_addr: []const u8 = switch (ip_info.version) { const target_addr: []const u8 = switch (ip_info.version) {
.IPv4 => "224.0.0.251", .IPv4 => "224.0.0.251",
.IPv6 => blk: { .IPv6 => "ff02::fb",
var buf: [50]u8 = undefined;
var byte_buf = std.io.fixedBufferStream(&buf);
const writer = byte_buf.writer();
try std.fmt.format(writer, "ff02::fb%{d}", .{ip_info.interface});
break :blk buf[0..writer.context.pos];
},
else => unreachable, else => unreachable,
}; };
return std.net.Address.resolveIp(target_addr, 5353); return std.net.Address.resolveIp(target_addr, 5353);

View file

@ -64,9 +64,15 @@ pub fn get_input() !struct {
const ip_ver_str = args.next() orelse return ArgError.NotEnoughArgs; const ip_ver_str = args.next() orelse return ArgError.NotEnoughArgs;
const ip_ver = std.meta.intToEnum(IP_VER_ENUM, std.fmt.parseInt(u3, ip_ver_str, 10) catch return ArgError.InvalidAddressVer) catch return ArgError.InvalidAddressVer; const ip_ver = std.meta.intToEnum(IP_VER_ENUM, std.fmt.parseInt(u3, ip_ver_str, 10) catch return ArgError.InvalidAddressVer) catch return ArgError.InvalidAddressVer;
const iface = try std.fmt.parseInt(c_int, args.next() orelse return ArgError.NotEnoughArgs, 10); var iface: c_int = undefined;
if (iface < 0) { if (args.next()) |iface_str| {
return ArgError.InvalidInterface; iface = std.fmt.parseInt(c_int, iface_str, 10) catch |err| blk: {
if (err == std.fmt.ParseIntError.Overflow) return err;
break :blk std.c.if_nametoindex(iface_str);
};
if (iface <= 0) return ArgError.InvalidInterface;
} else {
return ArgError.NotEnoughArgs;
} }
return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } }; return .{ domain, IPInfo{ .version = ip_ver, .interface = iface } };
} }