mod_s2s: Only do AAAA lookup if IPv6 is available, and A if IPv4 is available.
authorKim Alvefur <zash@zash.se>
Tue, 29 May 2012 18:02:48 +0200
changeset 4922 d1fdc545f8b2
parent 4921 9a01c6bc435e
child 4923 760a1f367f02
mod_s2s: Only do AAAA lookup if IPv6 is available, and A if IPv4 is available.
plugins/mod_s2s/s2sout.lib.lua
--- a/plugins/mod_s2s/s2sout.lib.lua	Tue May 29 17:20:02 2012 +0200
+++ b/plugins/mod_s2s/s2sout.lib.lua	Tue May 29 18:02:48 2012 +0200
@@ -25,6 +25,7 @@
 local log = module._log;
 
 local sources = {};
+local has_ipv4, has_ipv6;
 
 local dns_timeout = module:get_option_number("dns_timeout", 15);
 dns.settimeout(dns_timeout);
@@ -114,7 +115,7 @@
 					log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
 				end
 			else
-				log("debug", to_host.." has no SRV records, falling back to A");
+				log("debug", to_host.." has no SRV records, falling back to A/AAAA");
 			end
 			-- Try with SRV, or just the plain hostname if no SRV
 			local ok, err = s2sout.try_connect(host_session, connect_host, connect_port);
@@ -171,6 +172,7 @@
 		local handle4, handle6;
 		local has_other = false;
 
+		if has_ipv4 then
 		handle4 = adns.lookup(function (reply, err)
 			handle4 = nil;
 
@@ -214,7 +216,11 @@
 				has_other = true;
 			end
 		end, connect_host, "A", "IN");
+		else
+			has_other = true;
+		end
 
+		if has_ipv6 then
 		handle6 = adns.lookup(function (reply, err)
 			handle6 = nil;
 
@@ -246,6 +252,9 @@
 				has_other = true;
 			end
 		end, connect_host, "AAAA", "IN");
+		else
+			has_other = true;
+		end
 
 		return true;
 	elseif host_session.ip_hosts and #host_session.ip_hosts > host_session.ip_choice then -- Not our first attempt, and we also have IPs left to try
@@ -347,6 +356,13 @@
 			sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
 		end
 	end
+	for i = 1,#sources do
+		if sources[i].proto == "IPv6" then
+			has_ipv6 = true;
+		elseif sources[i].proto == "IPv4" then
+			has_ipv4 = true;
+		end
+	end
 end);
 
 return s2sout;