Add read timeout for failed address

This commit is contained in:
Muaz Ahmad 2024-05-13 02:21:30 +05:00
parent db1fbeb92a
commit fef2821364

View file

@ -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;