mod_admin_adhoc: Implement global module reloading
[prosody.git] / plugins / mod_s2s / s2sout.lib.lua
index 48a49036b43b577652a1804352d027b87f45fac0..076239680f41b7d6a69f6017780672d23d9417b0 100644 (file)
@@ -94,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
@@ -115,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/AAAA");
+                               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);
@@ -170,92 +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;
+               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;
+                       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
-                       has_other = true;
+                       have_other_result = true;
                end
 
                if has_ipv6 then
-               handle6 = adns.lookup(function (reply, err)
-                       handle6 = nil;
+                       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
-                       has_other = true;
+                       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);