Diff

net/dns.lua @ 6511:5f9389af5115

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sun, 09 Nov 2014 20:32:33 +0100
parent 6510:8273236a995f
child 6632:855085439f7f
line wrap: on
line diff
--- a/net/dns.lua	Sun Oct 26 20:58:02 2014 +0100
+++ b/net/dns.lua	Sun Nov 09 20:32:33 2014 +0100
@@ -624,7 +624,7 @@
 	local sock = self.socket[servernum];
 	if sock then return sock; end
 
-	local err;
+	local ok, err;
 	local peer = self.server[servernum];
 	if peer:find(":") then
 		sock, err = socket.udp6();
@@ -637,10 +637,14 @@
 	end
 	sock:settimeout(0);
 	-- todo: attempt to use a random port, fallback to 0
-	sock:setsockname('*', 0);
-	sock:setpeername(peer, 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(peer, 53);
+	if not ok then return self:servfail(sock, err); end
 	return sock;
 end
 
@@ -788,13 +792,13 @@
 	return true;
 end
 
-function resolver:servfail(sock)
+function resolver:servfail(sock, err)
 	-- Resend all queries for this server
 
 	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();
@@ -811,8 +815,8 @@
 					--print('timeout');
 					queries[question] = nil;
 				else
-					local _a = self:getsocket(o.server);
-					if _a then _a:send(o.packet); end
+					sock, err = self:getsocket(o.server);
+					if sock then sock:send(o.packet); end
 				end
 			end
 		end
@@ -828,6 +832,7 @@
 			self.best_server = 1;
 		end
 	end
+	return sock, err;
 end
 
 function resolver:settimeout(seconds)