From 2773c46aa5507b1069f7ac426423ca751938c9c2 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 30 Oct 2014 12:05:24 +0100 Subject: net.adns: Preserve error from setpeername --- net/adns.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'net') diff --git a/net/adns.lua b/net/adns.lua index 2a3fa8ad..da7981ba 100644 --- a/net/adns.lua +++ b/net/adns.lua @@ -76,7 +76,7 @@ function new_async_socket(sock, resolver) handler.settimeout = function () end handler.setsockname = function (_, ...) return sock:setsockname(...); end - handler.setpeername = function (_, ...) peername = (...); local ret = sock:setpeername(...); _:set_send(dummy_send); return ret; end + handler.setpeername = function (_, ...) peername = (...); local ret, err = sock:setpeername(...); _:set_send(dummy_send); return ret, err; end handler.connect = function (_, ...) return sock:connect(...) end --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end handler.send = function (_, data) -- cgit v1.2.3 From aac0915942b7d95b572e1d3a9e369ea84e935e2d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 30 Oct 2014 12:08:05 +0100 Subject: net.adns: Log peername recorded from wrapped setpeername instead of calling sock:getpeername, it exists and throws an error on unconnected sockets (thanks wirehack7) --- net/adns.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'net') diff --git a/net/adns.lua b/net/adns.lua index da7981ba..3fc958f4 100644 --- a/net/adns.lua +++ b/net/adns.lua @@ -80,8 +80,7 @@ function new_async_socket(sock, resolver) handler.connect = function (_, ...) return sock:connect(...) end --handler.send = function (_, data) _:write(data); return _.sendbuffer and _.sendbuffer(); end handler.send = function (_, data) - local getpeername = sock.getpeername; - log("debug", "Sending DNS query to %s", (getpeername and getpeername(sock)) or ""); + log("debug", "Sending DNS query to %s", peername); return sock:send(data); end return handler; -- cgit v1.2.3 From a5b90711b7e2638c4e735ec6545bea5b4462e9c4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 30 Oct 2014 12:10:15 +0100 Subject: net.dns: Return new socket from servfail --- net/dns.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'net') diff --git a/net/dns.lua b/net/dns.lua index dc2da1b6..55622fc8 100644 --- a/net/dns.lua +++ b/net/dns.lua @@ -787,7 +787,7 @@ function resolver:servfail(sock) local num = self.socketset[sock] -- Socket is dead now - self:voidsocket(sock); + sock = self:voidsocket(sock); -- Find all requests to the down server, and retry on the next server self.time = socket.gettime(); @@ -804,8 +804,8 @@ function resolver:servfail(sock) --print('timeout'); queries[question] = nil; else - local _a = self:getsocket(o.server); - if _a then _a:send(o.packet); end + sock = self:getsocket(o.server); + if sock then sock:send(o.packet); end end end end @@ -821,6 +821,7 @@ function resolver:servfail(sock) self.best_server = 1; end end + return sock; end function resolver:settimeout(seconds) -- cgit v1.2.3 From ce6a2af78f81381026b4aee05638a624ec8eba5e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 30 Oct 2014 12:28:07 +0100 Subject: net.dns: Try next server if peer name can not be set (thanks wirehack7) --- net/dns.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'net') diff --git a/net/dns.lua b/net/dns.lua index 55622fc8..13417cee 100644 --- a/net/dns.lua +++ b/net/dns.lua @@ -622,7 +622,7 @@ function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket local sock = self.socket[servernum]; if sock then return sock; end - local err; + local ok, err; sock, err = socket.udp(); if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end if not sock then @@ -630,10 +630,14 @@ function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket end sock:settimeout(0); -- todo: attempt to use a random port, fallback to 0 - sock:setsockname('*', 0); - sock:setpeername(self.server[servernum], 53); self.socket[servernum] = sock; self.socketset[sock] = servernum; + -- set{sock,peer}name can fail, eg because of local routing table + -- if so, try the next server + ok, err = sock:setsockname('*', 0); + if not ok then return self:servfail(sock, err); end + ok, err = sock:setpeername(self.server[servernum], 53); + if not ok then return self:servfail(sock, err); end return sock; end @@ -781,7 +785,7 @@ function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query return true; end -function resolver:servfail(sock) +function resolver:servfail(sock, err) -- Resend all queries for this server local num = self.socketset[sock] @@ -804,7 +808,7 @@ function resolver:servfail(sock) --print('timeout'); queries[question] = nil; else - sock = self:getsocket(o.server); + sock, err = self:getsocket(o.server); if sock then sock:send(o.packet); end end end @@ -821,7 +825,7 @@ function resolver:servfail(sock) self.best_server = 1; end end - return sock; + return sock, err; end function resolver:settimeout(seconds) -- cgit v1.2.3