mod_admin_adhoc: Implement global module reloading
[prosody.git] / plugins / mod_s2s / s2sout.lib.lua
index 94445ea21dfef6efc2ee7b808145030d1735a17d..076239680f41b7d6a69f6017780672d23d9417b0 100644 (file)
@@ -25,6 +25,7 @@ local s2s_destroy_session = require "core.s2smanager".destroy_session;
 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);
@@ -93,15 +94,15 @@ function s2sout.attempt_connection(host_session, err)
                handle = adns.lookup(function (answer)
                        handle = nil;
                        host_session.connecting = nil;
-                       if answer then
-                               log("debug", to_host.." has SRV records, handling...");
+                       if answer and #answer > 0 then
+                               log("debug", "%s has SRV records, handling...", to_host);
                                local srv_hosts = {};
                                host_session.srv_hosts = srv_hosts;
                                for _, record in ipairs(answer) do
                                        t_insert(srv_hosts, record.srv);
                                end
                                if #srv_hosts == 1 and srv_hosts[1].target == "." then
-                                       log("debug", to_host.." does not provide a XMPP service");
+                                       log("debug", "%s does not provide a XMPP service", to_host);
                                        s2s_destroy_session(host_session, err); -- Nothing to see here
                                        return;
                                end
@@ -114,7 +115,7 @@ function s2sout.attempt_connection(host_session, err)
                                        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", "%s has no SRV records, falling back to A/AAAA", to_host);
                        end
                        -- Try with SRV, or just the plain hostname if no SRV
                        local ok, err = s2sout.try_connect(host_session, connect_host, connect_port);
@@ -169,84 +170,91 @@ function s2sout.try_connect(host_session, connect_host, connect_port, err)
                local IPs = {};
                host_session.ip_hosts = IPs;
                local handle4, handle6;
-               local has_other = false;
-
-               handle4 = adns.lookup(function (reply, err)
-                       handle4 = nil;
-
-                       -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
-                       if not (reply and reply[#reply] and reply[#reply].a) then
-                               local count = max_dns_depth;
-                               reply = dns.peek(connect_host, "CNAME", "IN");
-                               while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
-                                       log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
-                                       reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
-                                       count = count - 1;
+               local have_other_result = not(has_ipv4) or not(has_ipv6) or false;
+
+               if has_ipv4 then
+                       handle4 = adns.lookup(function (reply, err)
+                               handle4 = nil;
+
+                               -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
+                               if not (reply and reply[#reply] and reply[#reply].a) then
+                                       local count = max_dns_depth;
+                                       reply = dns.peek(connect_host, "CNAME", "IN");
+                                       while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
+                                               log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
+                                               reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
+                                               count = count - 1;
+                                       end
                                end
-                       end
-                       -- end of CNAME resolving
+                               -- end of CNAME resolving
 
-                       if reply and reply[#reply] and reply[#reply].a then
-                               for _, ip in ipairs(reply) do
-                                       log("debug", "DNS reply for %s gives us %s", connect_host, ip.a);
-                                       IPs[#IPs+1] = new_ip(ip.a, "IPv4");
+                               if reply and reply[#reply] and reply[#reply].a then
+                                       for _, ip in ipairs(reply) do
+                                               log("debug", "DNS reply for %s gives us %s", connect_host, ip.a);
+                                               IPs[#IPs+1] = new_ip(ip.a, "IPv4");
+                                       end
                                end
-                       end
 
-                       if has_other then
-                               if #IPs > 0 then
-                                       rfc3484_dest(host_session.ip_hosts, sources);
-                                       for i = 1, #IPs do
-                                               IPs[i] = {ip = IPs[i], port = connect_port};
+                               if have_other_result then
+                                       if #IPs > 0 then
+                                               rfc3484_dest(host_session.ip_hosts, sources);
+                                               for i = 1, #IPs do
+                                                       IPs[i] = {ip = IPs[i], port = connect_port};
+                                               end
+                                               host_session.ip_choice = 0;
+                                               s2sout.try_next_ip(host_session);
+                                       else
+                                               log("debug", "DNS lookup failed to get a response for %s", connect_host);
+                                               host_session.ip_hosts = nil;
+                                               if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
+                                                       log("debug", "No other records to try for %s - destroying", host_session.to_host);
+                                                       err = err and (": "..err) or "";
+                                                       s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
+                                               end
                                        end
-                                       host_session.ip_choice = 0;
-                                       s2sout.try_next_ip(host_session);
                                else
-                                       log("debug", "DNS lookup failed to get a response for %s", connect_host);
-                                       host_session.ip_hosts = nil;
-                                       if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
-                                               log("debug", "No other records to try for %s - destroying", host_session.to_host);
-                                               err = err and (": "..err) or "";
-                                               s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
-                                       end
+                                       have_other_result = true;
                                end
-                       else
-                               has_other = true;
-                       end
-               end, connect_host, "A", "IN");
+                       end, connect_host, "A", "IN");
+               else
+                       have_other_result = true;
+               end
 
-               handle6 = adns.lookup(function (reply, err)
-                       handle6 = nil;
+               if has_ipv6 then
+                       handle6 = adns.lookup(function (reply, err)
+                               handle6 = nil;
 
-                       if reply and reply[#reply] and reply[#reply].aaaa then
-                               for _, ip in ipairs(reply) do
-                                       log("debug", "DNS reply for %s gives us %s", connect_host, ip.aaaa);
-                                       IPs[#IPs+1] = new_ip(ip.aaaa, "IPv6");
+                               if reply and reply[#reply] and reply[#reply].aaaa then
+                                       for _, ip in ipairs(reply) do
+                                               log("debug", "DNS reply for %s gives us %s", connect_host, ip.aaaa);
+                                               IPs[#IPs+1] = new_ip(ip.aaaa, "IPv6");
+                                       end
                                end
-                       end
 
-                       if has_other then
-                               if #IPs > 0 then
-                                       rfc3484_dest(host_session.ip_hosts, sources);
-                                       for i = 1, #IPs do
-                                               IPs[i] = {ip = IPs[i], port = connect_port};
+                               if have_other_result then
+                                       if #IPs > 0 then
+                                               rfc3484_dest(host_session.ip_hosts, sources);
+                                               for i = 1, #IPs do
+                                                       IPs[i] = {ip = IPs[i], port = connect_port};
+                                               end
+                                               host_session.ip_choice = 0;
+                                               s2sout.try_next_ip(host_session);
+                                       else
+                                               log("debug", "DNS lookup failed to get a response for %s", connect_host);
+                                               host_session.ip_hosts = nil;
+                                               if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
+                                                       log("debug", "No other records to try for %s - destroying", host_session.to_host);
+                                                       err = err and (": "..err) or "";
+                                                       s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
+                                               end
                                        end
-                                       host_session.ip_choice = 0;
-                                       s2sout.try_next_ip(host_session);
                                else
-                                       log("debug", "DNS lookup failed to get a response for %s", connect_host);
-                                       host_session.ip_hosts = nil;
-                                       if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
-                                               log("debug", "No other records to try for %s - destroying", host_session.to_host);
-                                               err = err and (": "..err) or "";
-                                               s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
-                                       end
+                                       have_other_result = true;
                                end
-                       else
-                               has_other = true;
-                       end
-               end, connect_host, "AAAA", "IN");
-
+                       end, connect_host, "AAAA", "IN");
+               else
+                       have_other_result = 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
                s2sout.try_next_ip(host_session);
@@ -273,10 +281,6 @@ function s2sout.make_connect(host_session, connect_host, connect_port)
        if connect_host.proto == "IPv4" then
                conn, handler = socket.tcp();
        else
-               if not socket.tcp6 then
-                       log("warn", "Could not connect to "..to_host..". Your version of lua-socket does not support IPv6");
-                       return false, "no-ipv6";
-               end
                conn, handler = socket.tcp6();
        end
        
@@ -347,6 +351,13 @@ module:hook_global("service-added", function (event)
                        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;