Bind to interface instead of implicitly binding to first interface

This commit is contained in:
Muaz Ahmad 2024-05-18 13:53:12 +05:00
parent 952253104e
commit 69bfc60261

View file

@ -8,6 +8,7 @@ const MDNSError = error{
NotResponse,
NoMatchingAddress,
AddressBadFormat,
SocketBindFail,
};
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) {
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;
}
@ -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 {
const target_addr: []const u8 = switch (ip_info.version) {
.IPv4 => "224.0.0.251",
.IPv6 => blk: {
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];
},
.IPv6 => "ff02::fb",
else => unreachable,
};
return std.net.Address.resolveIp(target_addr, 5353);