From 69bfc6026141b494df834704f8c8e08d06973ae0 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Sat, 18 May 2024 13:53:12 +0500 Subject: [PATCH] Bind to interface instead of implicitly binding to first interface --- src/mdns.zig | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/mdns.zig b/src/mdns.zig index 020dcc3..3b9b88c 100644 --- a/src/mdns.zig +++ b/src/mdns.zig @@ -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);