9 |
9 |
10 |
10 |
11 local hosts = hosts; |
11 local hosts = hosts; |
12 local sessions = sessions; |
12 local sessions = sessions; |
13 local core_process_stanza = function(a, b) core_process_stanza(a, b); end |
13 local core_process_stanza = function(a, b) core_process_stanza(a, b); end |
|
14 local add_task = require "util.timer".add_task; |
14 local socket = require "socket"; |
15 local socket = require "socket"; |
15 local format = string.format; |
16 local format = string.format; |
16 local t_insert, t_sort = table.insert, table.sort; |
17 local t_insert, t_sort = table.insert, table.sort; |
17 local get_traceback = debug.traceback; |
18 local get_traceback = debug.traceback; |
18 local tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber |
19 local tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber |
35 local sha256_hash = require "util.hashes".sha256; |
36 local sha256_hash = require "util.hashes".sha256; |
36 |
37 |
37 local dialback_secret = sha256_hash(tostring{} .. math.random() .. socket.gettime(), true); |
38 local dialback_secret = sha256_hash(tostring{} .. math.random() .. socket.gettime(), true); |
38 |
39 |
39 local adns = require "net.adns"; |
40 local adns = require "net.adns"; |
|
41 |
|
42 local dns_timeout = config.get("*", "core", "dns_timeout") or 60; |
40 |
43 |
41 incoming_s2s = {}; |
44 incoming_s2s = {}; |
42 local incoming_s2s = incoming_s2s; |
45 local incoming_s2s = incoming_s2s; |
43 |
46 |
44 module "s2smanager" |
47 module "s2smanager" |
167 local connect_host, connect_port = idna_to_ascii(to_host), 5269; |
170 local connect_host, connect_port = idna_to_ascii(to_host), 5269; |
168 |
171 |
169 if not err then -- This is our first attempt |
172 if not err then -- This is our first attempt |
170 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host); |
173 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host); |
171 host_session.connecting = true; |
174 host_session.connecting = true; |
172 local answer = |
175 local answer, handle; |
173 adns.lookup(function (answer) |
176 handle = adns.lookup(function (answer) |
|
177 handle = nil; |
174 host_session.connecting = nil; |
178 host_session.connecting = nil; |
175 if answer then |
179 if answer then |
176 log("debug", to_host.." has SRV records, handling..."); |
180 log("debug", to_host.." has SRV records, handling..."); |
177 local srv_hosts = {}; |
181 local srv_hosts = {}; |
178 host_session.srv_hosts = srv_hosts; |
182 host_session.srv_hosts = srv_hosts; |
191 log("debug", to_host.." has no SRV records, falling back to A"); |
195 log("debug", to_host.." has no SRV records, falling back to A"); |
192 end |
196 end |
193 -- Try with SRV, or just the plain hostname if no SRV |
197 -- Try with SRV, or just the plain hostname if no SRV |
194 return try_connect(host_session, connect_host, connect_port); |
198 return try_connect(host_session, connect_host, connect_port); |
195 end, "_xmpp-server._tcp."..connect_host..".", "SRV"); |
199 end, "_xmpp-server._tcp."..connect_host..".", "SRV"); |
|
200 |
|
201 -- Set handler for DNS timeout |
|
202 add_task(dns_timeout, function () |
|
203 if handle then |
|
204 adns.cancel(handle, true); |
|
205 end |
|
206 end); |
|
207 |
196 log("debug", "DNS lookup for %s sent, waiting for response before we can connect", to_host); |
208 log("debug", "DNS lookup for %s sent, waiting for response before we can connect", to_host); |
197 return true; -- Attempt in progress |
209 return true; -- Attempt in progress |
198 elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV |
210 elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV |
199 host_session.srv_choice = host_session.srv_choice + 1; |
211 host_session.srv_choice = host_session.srv_choice + 1; |
200 local srv_choice = host_session.srv_hosts[host_session.srv_choice]; |
212 local srv_choice = host_session.srv_hosts[host_session.srv_choice]; |