Annotate

plugins/mod_s2s/s2sout.lib.lua @ 5024:d25e1b9332cc

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