From fef2821364110f4a5f382315f10b7205646842ba Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 13 May 2024 02:21:30 +0500 Subject: [PATCH] Add read timeout for failed address --- src/mdns.zig | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mdns.zig b/src/mdns.zig index 15343fe..0c42fcc 100644 --- a/src/mdns.zig +++ b/src/mdns.zig @@ -10,6 +10,7 @@ const MDNSError = error{ NotResponse, NoMatchingAddress, AddressBadFormat, + SocketSetTimeoutFail, }; pub fn get_mdns(domain: util.Domain, ip_info: util.IPInfo) !util.IP { @@ -28,6 +29,11 @@ fn get_mdns_socket(ip_info: util.IPInfo) !socket { if (sock == -1) { return MDNSError.SocketInitFail; } + const timeout: std.c.timeval = .{ .tv_sec = 1, .tv_usec = 0 }; + if (std.c.setsockopt(sock, std.c.SOL.SOCKET, std.c.SO.RCVTIMEO, &timeout, @intCast(@sizeOf(std.c.timeval))) == -1) { + std.debug.print("{d}\n", .{std.c._errno().*}); + return MDNSError.SocketSetTimeoutFail; + } return sock; } @@ -85,7 +91,7 @@ fn send_query(domain: util.Domain, ip_info: util.IPInfo) !socket { var buff = [_]u8{0x00} ** MSG_BUFF_SIZE; const n = try construct_mdns_query(domain, ip_info, &buff); - if (std.c.sendto(sock, &buff, n, 0, &addr.any, addr.getOsSockLen()) == -1) { + if (std.c.sendto(sock, &buff, n, std.c.MSG.DONTWAIT, &addr.any, addr.getOsSockLen()) == -1) { return MDNSError.UDPSendFail; } return sock;