net.http.server: Properly handle persistent connections
[prosody.git] / plugins / mod_s2s / s2sout.lib.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 --- Module containing all the logic for connecting to a remote server
10
11 local portmanager = require "core.portmanager";
12 local wrapclient = require "net.server".wrapclient;
13 local initialize_filters = require "util.filters".initialize;
14 local idna_to_ascii = require "util.encodings".idna.to_ascii;
15 local new_ip = require "util.ip".new_ip;
16 local rfc3484_dest = require "util.rfc3484".destination;
17 local socket = require "socket";
18 local adns = require "net.adns";
19 local dns = require "net.dns";
20 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs;
21 local st = require "util.stanza";
22
23 local s2s_destroy_session = require "core.s2smanager".destroy_session;
24
25 local log = module._log;
26
27 local sources = {};
28 local has_ipv4, has_ipv6;
29
30 local dns_timeout = module:get_option_number("dns_timeout", 15);
31 dns.settimeout(dns_timeout);
32 local max_dns_depth = module:get_option_number("dns_max_depth", 3);
33
34 local s2sout = {};
35
36 local s2s_listener;
37
38
39 function s2sout.set_listener(listener)
40         s2s_listener = listener;
41 end
42
43 local function compare_srv_priorities(a,b)
44         return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
45 end
46
47 local function session_open_stream(session, from, to)
48         session.sends2s(st.stanza("stream:stream", {
49                 xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback',
50                 ["xmlns:stream"]='http://etherx.jabber.org/streams',
51                 from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag());
52 end
53
54 function s2sout.initiate_connection(host_session)
55         initialize_filters(host_session);
56         host_session.open_stream = session_open_stream;
57         
58         -- Kick the connection attempting machine into life
59         if not s2sout.attempt_connection(host_session) then
60                 -- Intentionally not returning here, the
61                 -- session is needed, connected or not
62                 s2s_destroy_session(host_session);
63         end
64         
65         if not host_session.sends2s then
66                 -- A sends2s which buffers data (until the stream is opened)
67                 -- note that data in this buffer will be sent before the stream is authed
68                 -- and will not be ack'd in any way, successful or otherwise
69                 local buffer;
70                 function host_session.sends2s(data)
71                         if not buffer then
72                                 buffer = {};
73                                 host_session.send_buffer = buffer;
74                         end
75                         log("debug", "Buffering data on unconnected s2sout to %s", tostring(host_session.to_host));
76                         buffer[#buffer+1] = data;
77                         log("debug", "Buffered item %d: %s", #buffer, tostring(data));
78                 end
79         end
80 end
81
82 function s2sout.attempt_connection(host_session, err)
83         local from_host, to_host = host_session.from_host, host_session.to_host;
84         local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
85         
86         if not connect_host then
87                 return false;
88         end
89         
90         if not err then -- This is our first attempt
91                 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host);
92                 host_session.connecting = true;
93                 local handle;
94                 handle = adns.lookup(function (answer)
95                         handle = nil;
96                         host_session.connecting = nil;
97                         if answer and #answer > 0 then
98                                 log("debug", "%s has SRV records, handling...", to_host);
99                                 local srv_hosts = {};
100                                 host_session.srv_hosts = srv_hosts;
101                                 for _, record in ipairs(answer) do
102                                         t_insert(srv_hosts, record.srv);
103                                 end
104                                 if #srv_hosts == 1 and srv_hosts[1].target == "." then
105                                         log("debug", "%s does not provide a XMPP service", to_host);
106                                         s2s_destroy_session(host_session, err); -- Nothing to see here
107                                         return;
108                                 end
109                                 t_sort(srv_hosts, compare_srv_priorities);
110                                 
111                                 local srv_choice = srv_hosts[1];
112                                 host_session.srv_choice = 1;
113                                 if srv_choice then
114                                         connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
115                                         log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
116                                 end
117                         else
118                                 log("debug", "%s has no SRV records, falling back to A/AAAA", to_host);
119                         end
120                         -- Try with SRV, or just the plain hostname if no SRV
121                         local ok, err = s2sout.try_connect(host_session, connect_host, connect_port);
122                         if not ok then
123                                 if not s2sout.attempt_connection(host_session, err) then
124                                         -- No more attempts will be made
125                                         s2s_destroy_session(host_session, err);
126                                 end
127                         end
128                 end, "_xmpp-server._tcp."..connect_host..".", "SRV");
129                 
130                 return true; -- Attempt in progress
131         elseif host_session.ip_hosts then
132                 return s2sout.try_connect(host_session, connect_host, connect_port, err);
133         elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV
134                 host_session.srv_choice = host_session.srv_choice + 1;
135                 local srv_choice = host_session.srv_hosts[host_session.srv_choice];
136                 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
137                 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port);
138         else
139                 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host));
140                 -- We're out of options
141                 return false;
142         end
143         
144         if not (connect_host and connect_port) then
145                 -- Likely we couldn't resolve DNS
146                 log("warn", "Hmm, we're without a host (%s) and port (%s) to connect to for %s, giving up :(", tostring(connect_host), tostring(connect_port), tostring(to_host));
147                 return false;
148         end
149
150         return s2sout.try_connect(host_session, connect_host, connect_port);
151 end
152
153 function s2sout.try_next_ip(host_session)
154         host_session.connecting = nil;
155         host_session.ip_choice = host_session.ip_choice + 1;
156         local ip = host_session.ip_hosts[host_session.ip_choice];
157         local ok, err= s2sout.make_connect(host_session, ip.ip, ip.port);
158         if not ok then
159                 if not s2sout.attempt_connection(host_session, err or "closed") then
160                         err = err and (": "..err) or "";
161                         s2s_destroy_session(host_session, "Connection failed"..err);
162                 end
163         end
164 end
165
166 function s2sout.try_connect(host_session, connect_host, connect_port, err)
167         host_session.connecting = true;
168
169         if not err then
170                 local IPs = {};
171                 host_session.ip_hosts = IPs;
172                 local handle4, handle6;
173                 local have_other_result = not(has_ipv4) or not(has_ipv6) or false;
174
175                 if has_ipv4 then
176                         handle4 = adns.lookup(function (reply, err)
177                                 handle4 = nil;
178
179                                 -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
180                                 if not (reply and reply[#reply] and reply[#reply].a) then
181                                         local count = max_dns_depth;
182                                         reply = dns.peek(connect_host, "CNAME", "IN");
183                                         while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
184                                                 log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
185                                                 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
186                                                 count = count - 1;
187                                         end
188                                 end
189                                 -- end of CNAME resolving
190
191                                 if reply and reply[#reply] and reply[#reply].a then
192                                         for _, ip in ipairs(reply) do
193                                                 log("debug", "DNS reply for %s gives us %s", connect_host, ip.a);
194                                                 IPs[#IPs+1] = new_ip(ip.a, "IPv4");
195                                         end
196                                 end
197
198                                 if have_other_result then
199                                         if #IPs > 0 then
200                                                 rfc3484_dest(host_session.ip_hosts, sources);
201                                                 for i = 1, #IPs do
202                                                         IPs[i] = {ip = IPs[i], port = connect_port};
203                                                 end
204                                                 host_session.ip_choice = 0;
205                                                 s2sout.try_next_ip(host_session);
206                                         else
207                                                 log("debug", "DNS lookup failed to get a response for %s", connect_host);
208                                                 host_session.ip_hosts = nil;
209                                                 if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
210                                                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
211                                                         err = err and (": "..err) or "";
212                                                         s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
213                                                 end
214                                         end
215                                 else
216                                         have_other_result = true;
217                                 end
218                         end, connect_host, "A", "IN");
219                 else
220                         have_other_result = true;
221                 end
222
223                 if has_ipv6 then
224                         handle6 = adns.lookup(function (reply, err)
225                                 handle6 = nil;
226
227                                 if reply and reply[#reply] and reply[#reply].aaaa then
228                                         for _, ip in ipairs(reply) do
229                                                 log("debug", "DNS reply for %s gives us %s", connect_host, ip.aaaa);
230                                                 IPs[#IPs+1] = new_ip(ip.aaaa, "IPv6");
231                                         end
232                                 end
233
234                                 if have_other_result then
235                                         if #IPs > 0 then
236                                                 rfc3484_dest(host_session.ip_hosts, sources);
237                                                 for i = 1, #IPs do
238                                                         IPs[i] = {ip = IPs[i], port = connect_port};
239                                                 end
240                                                 host_session.ip_choice = 0;
241                                                 s2sout.try_next_ip(host_session);
242                                         else
243                                                 log("debug", "DNS lookup failed to get a response for %s", connect_host);
244                                                 host_session.ip_hosts = nil;
245                                                 if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
246                                                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
247                                                         err = err and (": "..err) or "";
248                                                         s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
249                                                 end
250                                         end
251                                 else
252                                         have_other_result = true;
253                                 end
254                         end, connect_host, "AAAA", "IN");
255                 else
256                         have_other_result = true;
257                 end
258                 return true;
259         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
260                 s2sout.try_next_ip(host_session);
261         else
262                 host_session.ip_hosts = nil;
263                 if not s2sout.attempt_connection(host_session, "out of IP addresses") then -- Retry if we can
264                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
265                         err = err and (": "..err) or "";
266                         s2s_destroy_session(host_session, "Connecting failed"..err); -- End of the line, we can't
267                         return false;
268                 end
269         end
270
271         return true;
272 end
273
274 function s2sout.make_connect(host_session, connect_host, connect_port)
275         (host_session.log or log)("info", "Beginning new connection attempt to %s ([%s]:%d)", host_session.to_host, connect_host.addr, connect_port);
276         -- Ok, we're going to try to connect
277         
278         local from_host, to_host = host_session.from_host, host_session.to_host;
279         
280         local conn, handler;
281         if connect_host.proto == "IPv4" then
282                 conn, handler = socket.tcp();
283         else
284                 conn, handler = socket.tcp6();
285         end
286         
287         if not conn then
288                 log("warn", "Failed to create outgoing connection, system error: %s", handler);
289                 return false, handler;
290         end
291
292         conn:settimeout(0);
293         local success, err = conn:connect(connect_host.addr, connect_port);
294         if not success and err ~= "timeout" then
295                 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host.addr, connect_port, err);
296                 return false, err;
297         end
298         
299         conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, "*a");
300         host_session.conn = conn;
301         
302         local filter = initialize_filters(host_session);
303         local w, log = conn.write, host_session.log;
304         host_session.sends2s = function (t)
305                 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?"));
306                 if t.name then
307                         t = filter("stanzas/out", t);
308                 end
309                 if t then
310                         t = filter("bytes/out", tostring(t));
311                         if t then
312                                 return w(conn, tostring(t));
313                         end
314                 end
315         end
316         
317         -- Register this outgoing connection so that xmppserver_listener knows about it
318         -- otherwise it will assume it is a new incoming connection
319         s2s_listener.register_outgoing(conn, host_session);
320         
321         log("debug", "Connection attempt in progress...");
322         return true;
323 end
324
325 module:hook_global("service-added", function (event)
326         if event.name ~= "s2s" then return end
327
328         local s2s_sources = portmanager.get_active_services():get("s2s");
329         if not s2s_sources then
330                 module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
331                 return;
332         end
333         for source, _ in pairs(s2s_sources) do
334                 if source == "*" or source == "0.0.0.0" then
335                         if not socket.local_addresses then
336                                 sources[#sources + 1] = new_ip("0.0.0.0", "IPv4");
337                         else
338                                 for _, addr in ipairs(socket.local_addresses("ipv4", true)) do
339                                         sources[#sources + 1] = new_ip(addr, "IPv4");
340                                 end
341                         end
342                 elseif source == "::" then
343                         if not socket.local_addresses then
344                                 sources[#sources + 1] = new_ip("::", "IPv6");
345                         else
346                                 for _, addr in ipairs(socket.local_addresses("ipv6", true)) do
347                                         sources[#sources + 1] = new_ip(addr, "IPv6");
348                                 end
349                         end
350                 else
351                         sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
352                 end
353         end
354         for i = 1,#sources do
355                 if sources[i].proto == "IPv6" then
356                         has_ipv6 = true;
357                 elseif sources[i].proto == "IPv4" then
358                         has_ipv4 = true;
359                 end
360         end
361 end);
362
363 return s2sout;