Software /
code /
prosody
Comparison
plugins/mod_s2s/s2sout.lib.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 5703:ea00fc524b95 |
child | 5798:c610b5fb53f3 |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 --- Module containing all the logic for connecting to a remote server | 9 --- Module containing all the logic for connecting to a remote server |
45 end | 45 end |
46 | 46 |
47 function s2sout.initiate_connection(host_session) | 47 function s2sout.initiate_connection(host_session) |
48 initialize_filters(host_session); | 48 initialize_filters(host_session); |
49 host_session.version = 1; | 49 host_session.version = 1; |
50 | 50 |
51 -- Kick the connection attempting machine into life | 51 -- Kick the connection attempting machine into life |
52 if not s2sout.attempt_connection(host_session) then | 52 if not s2sout.attempt_connection(host_session) then |
53 -- Intentionally not returning here, the | 53 -- Intentionally not returning here, the |
54 -- session is needed, connected or not | 54 -- session is needed, connected or not |
55 s2s_destroy_session(host_session); | 55 s2s_destroy_session(host_session); |
56 end | 56 end |
57 | 57 |
58 if not host_session.sends2s then | 58 if not host_session.sends2s then |
59 -- A sends2s which buffers data (until the stream is opened) | 59 -- A sends2s which buffers data (until the stream is opened) |
60 -- note that data in this buffer will be sent before the stream is authed | 60 -- note that data in this buffer will be sent before the stream is authed |
61 -- and will not be ack'd in any way, successful or otherwise | 61 -- and will not be ack'd in any way, successful or otherwise |
62 local buffer; | 62 local buffer; |
73 end | 73 end |
74 | 74 |
75 function s2sout.attempt_connection(host_session, err) | 75 function s2sout.attempt_connection(host_session, err) |
76 local to_host = host_session.to_host; | 76 local to_host = host_session.to_host; |
77 local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269; | 77 local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269; |
78 | 78 |
79 if not connect_host then | 79 if not connect_host then |
80 return false; | 80 return false; |
81 end | 81 end |
82 | 82 |
83 if not err then -- This is our first attempt | 83 if not err then -- This is our first attempt |
84 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host); | 84 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host); |
85 host_session.connecting = true; | 85 host_session.connecting = true; |
86 local handle; | 86 local handle; |
87 handle = adns.lookup(function (answer) | 87 handle = adns.lookup(function (answer) |
98 log("debug", "%s does not provide a XMPP service", to_host); | 98 log("debug", "%s does not provide a XMPP service", to_host); |
99 s2s_destroy_session(host_session, err); -- Nothing to see here | 99 s2s_destroy_session(host_session, err); -- Nothing to see here |
100 return; | 100 return; |
101 end | 101 end |
102 t_sort(srv_hosts, compare_srv_priorities); | 102 t_sort(srv_hosts, compare_srv_priorities); |
103 | 103 |
104 local srv_choice = srv_hosts[1]; | 104 local srv_choice = srv_hosts[1]; |
105 host_session.srv_choice = 1; | 105 host_session.srv_choice = 1; |
106 if srv_choice then | 106 if srv_choice then |
107 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; | 107 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; |
108 log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); | 108 log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); |
117 -- No more attempts will be made | 117 -- No more attempts will be made |
118 s2s_destroy_session(host_session, err); | 118 s2s_destroy_session(host_session, err); |
119 end | 119 end |
120 end | 120 end |
121 end, "_xmpp-server._tcp."..connect_host..".", "SRV"); | 121 end, "_xmpp-server._tcp."..connect_host..".", "SRV"); |
122 | 122 |
123 return true; -- Attempt in progress | 123 return true; -- Attempt in progress |
124 elseif host_session.ip_hosts then | 124 elseif host_session.ip_hosts then |
125 return s2sout.try_connect(host_session, connect_host, connect_port, err); | 125 return s2sout.try_connect(host_session, connect_host, connect_port, err); |
126 elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV | 126 elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV |
127 host_session.srv_choice = host_session.srv_choice + 1; | 127 host_session.srv_choice = host_session.srv_choice + 1; |
131 else | 131 else |
132 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host)); | 132 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host)); |
133 -- We're out of options | 133 -- We're out of options |
134 return false; | 134 return false; |
135 end | 135 end |
136 | 136 |
137 if not (connect_host and connect_port) then | 137 if not (connect_host and connect_port) then |
138 -- Likely we couldn't resolve DNS | 138 -- Likely we couldn't resolve DNS |
139 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)); | 139 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)); |
140 return false; | 140 return false; |
141 end | 141 end |
278 elseif proto == "IPv6" and socket.tcp6 then | 278 elseif proto == "IPv6" and socket.tcp6 then |
279 conn, handler = socket.tcp6(); | 279 conn, handler = socket.tcp6(); |
280 else | 280 else |
281 handler = "Unsupported protocol: "..tostring(proto); | 281 handler = "Unsupported protocol: "..tostring(proto); |
282 end | 282 end |
283 | 283 |
284 if not conn then | 284 if not conn then |
285 log("warn", "Failed to create outgoing connection, system error: %s", handler); | 285 log("warn", "Failed to create outgoing connection, system error: %s", handler); |
286 return false, handler; | 286 return false, handler; |
287 end | 287 end |
288 | 288 |
290 local success, err = conn:connect(connect_host.addr, connect_port); | 290 local success, err = conn:connect(connect_host.addr, connect_port); |
291 if not success and err ~= "timeout" then | 291 if not success and err ~= "timeout" then |
292 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host.addr, connect_port, err); | 292 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host.addr, connect_port, err); |
293 return false, err; | 293 return false, err; |
294 end | 294 end |
295 | 295 |
296 conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, "*a"); | 296 conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, "*a"); |
297 host_session.conn = conn; | 297 host_session.conn = conn; |
298 | 298 |
299 local filter = initialize_filters(host_session); | 299 local filter = initialize_filters(host_session); |
300 local w, log = conn.write, host_session.log; | 300 local w, log = conn.write, host_session.log; |
301 host_session.sends2s = function (t) | 301 host_session.sends2s = function (t) |
302 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?")); | 302 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?")); |
303 if t.name then | 303 if t.name then |
308 if t then | 308 if t then |
309 return w(conn, tostring(t)); | 309 return w(conn, tostring(t)); |
310 end | 310 end |
311 end | 311 end |
312 end | 312 end |
313 | 313 |
314 -- Register this outgoing connection so that xmppserver_listener knows about it | 314 -- Register this outgoing connection so that xmppserver_listener knows about it |
315 -- otherwise it will assume it is a new incoming connection | 315 -- otherwise it will assume it is a new incoming connection |
316 s2s_listener.register_outgoing(conn, host_session); | 316 s2s_listener.register_outgoing(conn, host_session); |
317 | 317 |
318 log("debug", "Connection attempt in progress..."); | 318 log("debug", "Connection attempt in progress..."); |
319 return true; | 319 return true; |
320 end | 320 end |
321 | 321 |
322 module:hook_global("service-added", function (event) | 322 module:hook_global("service-added", function (event) |