Annotate

plugins/mod_s2s/s2sout.lib.lua @ 9413:127208a6107c

mod_s2s: Silence luacheck warnings in s2sout module
author Kim Alvefur <zash@zash.se>
date Sun, 30 Sep 2018 17:52:30 +0200
parent 8911:cea6165a03d0
child 9414:a48579a7b709
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
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
4 --
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
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
9413
127208a6107c mod_s2s: Silence luacheck warnings in s2sout module
Kim Alvefur <zash@zash.se>
parents: 8911
diff changeset
11 -- luacheck: ignore 432/err
127208a6107c mod_s2s: Silence luacheck warnings in s2sout module
Kim Alvefur <zash@zash.se>
parents: 8911
diff changeset
12
4602
ba898af15de5 mod_s2s: Collect s2s sources from portmanager and get local address if necessary
Florian Zeitz <florob@babelmonkeys.de>
parents: 4599
diff changeset
13 local portmanager = require "core.portmanager";
4569
34b1122126f6 s2sout.lib: import utils/functionality once in s2smanager.
Florian Zeitz <florob@babelmonkeys.de>
parents: 4555
diff changeset
14 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
15 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
16 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
17 local new_ip = require "util.ip".new_ip;
5552
40e7a6cf15ff util.rfc{3484,6724}: Update to RFC 6724
Florian Zeitz <florob@babelmonkeys.de>
parents: 5388
diff changeset
18 local rfc6724_dest = require "util.rfc6724".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
19 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
20 local adns = require "net.adns";
4573
09255a10fdfa Merge Zash with Florob and Maranda
Matthew Wild <mwild1@gmail.com>
parents: 4564 4570
diff changeset
21 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs;
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
22 local local_addresses = require "util.net".local_addresses;
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
23
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 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
25
7561
ab8324d3b899 mod_s2s: Set read mode for s2sout sockets to the same as s2sins
Kim Alvefur <zash@zash.se>
parents: 7098
diff changeset
26 local default_mode = module:get_option("network_default_read_size", 4096);
ab8324d3b899 mod_s2s: Set read mode for s2sout sockets to the same as s2sins
Kim Alvefur <zash@zash.se>
parents: 7098
diff changeset
27
4756
f8ddaaa08a8e mod_s2s/s2sout.lib: Fix imports and some undefined variables
Matthew Wild <mwild1@gmail.com>
parents: 4629
diff changeset
28 local log = module._log;
f8ddaaa08a8e mod_s2s/s2sout.lib: Fix imports and some undefined variables
Matthew Wild <mwild1@gmail.com>
parents: 4629
diff changeset
29
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
30 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
31 local has_ipv4, has_ipv6;
4569
34b1122126f6 s2sout.lib: import utils/functionality once in s2smanager.
Florian Zeitz <florob@babelmonkeys.de>
parents: 4555
diff changeset
32
4832
6b3aec1e0d9f s2smanager, mod_s2s: Move checking DNS timeout option to mod_s2s
Kim Alvefur <zash@zash.se>
parents: 4814
diff changeset
33 local dns_timeout = module:get_option_number("dns_timeout", 15);
8896
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
34 local resolvers = module:get_option_set("s2s_dns_resolvers")
4599
f9f5230d7a9c s2smanager, mod_s2s: Move import of dns_max_depth to mod_s2s
Kim Alvefur <zash@zash.se>
parents: 4573
diff changeset
35
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
36 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
37
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
38 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
39
4563
75bff73391fb s2sout.lib: Add locals for source interfaces
Kim Alvefur <zash@zash.se>
parents: 4562
diff changeset
40
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
41 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
42 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
43 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
44
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 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
46 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
47 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
48
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 function s2sout.initiate_connection(host_session)
8911
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
50 local log = host_session.log or log;
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
51
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
52 initialize_filters(host_session);
5351
901ed253bbf7 mod_s2s, mod_saslauth, mod_compression: Refactor to have common code for opening streams
Kim Alvefur <zash@zash.se>
parents: 5112
diff changeset
53 host_session.version = 1;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
54
8267
42fad8465537 mod_s2s: Use a separate resolver object for each outgoing session
Matthew Wild <mwild1@gmail.com>
parents: 7685
diff changeset
55 host_session.resolver = adns.resolver();
8899
c72a6226ee31 mod_s2s: Fix DNS timeout setting for per-session resolvers (fixes #1167)
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
56 host_session.resolver._resolver:settimeout(dns_timeout);
8896
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
57 if resolvers then
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
58 for resolver in resolvers do
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
59 host_session.resolver._resolver:addnameserver(resolver);
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
60 end
739deb514140 mod_s2s: Add setting for overriding DNS resolvers (and avoid reading /etc/resolv.conf every time)
Kim Alvefur <zash@zash.se>
parents: 8483
diff changeset
61 end
8267
42fad8465537 mod_s2s: Use a separate resolver object for each outgoing session
Matthew Wild <mwild1@gmail.com>
parents: 7685
diff changeset
62
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
63 -- 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
64 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
65 -- 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
66 -- 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
67 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
68 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
69
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
70 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
71 -- 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
72 -- 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
73 -- 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
74 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
75 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
76 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
77 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
78 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
79 end
8483
6d47b74926dd mod_s2s: Remove tostring() in logging since this is handled by util.format now
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
80 log("debug", "Buffering data on unconnected s2sout to %s", 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
81 buffer[#buffer+1] = data;
8483
6d47b74926dd mod_s2s: Remove tostring() in logging since this is handled by util.format now
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
82 log("debug", "Buffered item %d: %s", #buffer, data);
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
83 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
84 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
85 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
86
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 function s2sout.attempt_connection(host_session, err)
5703
ea00fc524b95 mod_s2s/s2sout.lib: Remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5702
diff changeset
88 local to_host = 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
89 local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
8911
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
90 local log = host_session.log or log;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
91
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
92 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
93 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
94 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
95
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
96 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
97 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
98 host_session.connecting = true;
8267
42fad8465537 mod_s2s: Use a separate resolver object for each outgoing session
Matthew Wild <mwild1@gmail.com>
parents: 7685
diff changeset
99 host_session.resolver:lookup(function (answer)
6917
65344c3bae7a mod_s2s/s2sout: Keep SRV lookup response around even if it's empty
Kim Alvefur <zash@zash.se>
parents: 6685
diff changeset
100 local srv_hosts = { answer = answer };
65344c3bae7a mod_s2s/s2sout: Keep SRV lookup response around even if it's empty
Kim Alvefur <zash@zash.se>
parents: 6685
diff changeset
101 host_session.srv_hosts = srv_hosts;
65344c3bae7a mod_s2s/s2sout: Keep SRV lookup response around even if it's empty
Kim Alvefur <zash@zash.se>
parents: 6685
diff changeset
102 host_session.srv_choice = 0;
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
103 host_session.connecting = nil;
5112
8b94a8d92cf3 mod_s2s: Check that an SRV reply isn't empty.
Kim Alvefur <zash@zash.se>
parents: 4987
diff changeset
104 if answer and #answer > 0 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 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
106 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
107 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
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 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
110 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
111 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
112 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
113 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
114 t_sort(srv_hosts, compare_srv_priorities);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
115
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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 -- 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
126 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
127 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
128 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
129 -- 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
130 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
131 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
132 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
133 end, "_xmpp-server._tcp."..connect_host..".", "SRV");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
134
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
135 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
136 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
137 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
138 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
139 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
140 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
141 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
8483
6d47b74926dd mod_s2s: Remove tostring() in logging since this is handled by util.format now
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
142 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", err, host_session.srv_choice, connect_host, connect_port);
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
143 else
8483
6d47b74926dd mod_s2s: Remove tostring() in logging since this is handled by util.format now
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
144 host_session.log("info", "Failed in all attempts to connect to %s", 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
145 -- 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
146 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
147 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
148
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
149 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
150 -- Likely we couldn't resolve DNS
8483
6d47b74926dd mod_s2s: Remove tostring() in logging since this is handled by util.format now
Kim Alvefur <zash@zash.se>
parents: 8267
diff changeset
151 log("warn", "Hmm, we're without a host (%s) and port (%s) to connect to for %s, giving up :(", connect_host, connect_port, 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
152 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
153 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
154
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 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
156 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
157
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 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
159 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
160 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
161 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
162 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
163 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
164 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
165 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
166 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
167 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
168 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
169 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
170
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 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
172 host_session.connecting = true;
8911
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
173 local log = host_session.log or log;
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
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
175 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
176 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
177 host_session.ip_hosts = IPs;
9413
127208a6107c mod_s2s: Silence luacheck warnings in s2sout module
Kim Alvefur <zash@zash.se>
parents: 8911
diff changeset
178 :make
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
179 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
180 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
181
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
182 if has_ipv4 then
8267
42fad8465537 mod_s2s: Use a separate resolver object for each outgoing session
Matthew Wild <mwild1@gmail.com>
parents: 7685
diff changeset
183 handle4 = host_session.resolver:lookup(function (reply, err)
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
184 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
185
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
186 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
187 for _, ip in ipairs(reply) do
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
188 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
189 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
190 end
7685
8e43a05e2374 mod_s2s: Log error message from DNS lookup
Kim Alvefur <zash@zash.se>
parents: 7561
diff changeset
191 elseif err then
8e43a05e2374 mod_s2s: Log error message from DNS lookup
Kim Alvefur <zash@zash.se>
parents: 7561
diff changeset
192 log("debug", "Error in DNS lookup: %s", err);
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
193 end
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
194
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
195 if have_other_result then
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
196 if #IPs > 0 then
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
197 rfc6724_dest(host_session.ip_hosts, sources);
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
198 for i = 1, #IPs do
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
199 IPs[i] = {ip = IPs[i], port = connect_port};
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
200 end
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
201 host_session.ip_choice = 0;
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
202 s2sout.try_next_ip(host_session);
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
203 else
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
204 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
205 host_session.ip_hosts = nil;
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
206 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
207 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
208 err = err and (": "..err) or "";
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
209 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
210 end
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
211 end
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
212 else
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
213 have_other_result = true;
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 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
216 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
217 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
218 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
219
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
220 if has_ipv6 then
8267
42fad8465537 mod_s2s: Use a separate resolver object for each outgoing session
Matthew Wild <mwild1@gmail.com>
parents: 7685
diff changeset
221 handle6 = host_session.resolver:lookup(function (reply, err)
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
222 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
223
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
224 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
225 for _, ip in ipairs(reply) do
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
226 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
227 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
228 end
7685
8e43a05e2374 mod_s2s: Log error message from DNS lookup
Kim Alvefur <zash@zash.se>
parents: 7561
diff changeset
229 elseif err then
8e43a05e2374 mod_s2s: Log error message from DNS lookup
Kim Alvefur <zash@zash.se>
parents: 7561
diff changeset
230 log("debug", "Error in DNS lookup: %s", err);
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
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
232
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
233 if have_other_result then
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
234 if #IPs > 0 then
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
235 rfc6724_dest(host_session.ip_hosts, sources);
4959
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
236 for i = 1, #IPs do
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
237 IPs[i] = {ip = IPs[i], port = connect_port};
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
238 end
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
239 host_session.ip_choice = 0;
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
240 s2sout.try_next_ip(host_session);
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
241 else
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
242 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
243 host_session.ip_hosts = nil;
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
244 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
245 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
246 err = err and (": "..err) or "";
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
247 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
248 end
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 else
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
251 have_other_result = true;
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
252 end
ab696f5394cf mod_s2s/s2sout.lib: Fix indentation
Matthew Wild <mwild1@gmail.com>
parents: 4957
diff changeset
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 else
8910
28ec1e024379 mod_s2s: Log a message before trying the next SRV record
Kim Alvefur <zash@zash.se>
parents: 8902
diff changeset
261 log("debug", "Out of IP addresses, trying next SRV record (if any)");
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
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)
8911
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
275 local log = host_session.log or log;
cea6165a03d0 mod_s2s: Use session logger to ease debugging
Kim Alvefur <zash@zash.se>
parents: 8910
diff changeset
276 log("debug", "Beginning new connection attempt to %s ([%s]:%d)", host_session.to_host, connect_host.addr, connect_port);
5703
ea00fc524b95 mod_s2s/s2sout.lib: Remove unused variables and imports
Matthew Wild <mwild1@gmail.com>
parents: 5702
diff changeset
277
5388
f833d2188c16 mod_s2s: Reset secure flag on new connection attempt
Kim Alvefur <zash@zash.se>
parents: 5386
diff changeset
278 -- Reset secure flag in case this is another
f833d2188c16 mod_s2s: Reset secure flag on new connection attempt
Kim Alvefur <zash@zash.se>
parents: 5386
diff changeset
279 -- connection attempt after a failed STARTTLS
f833d2188c16 mod_s2s: Reset secure flag on new connection attempt
Kim Alvefur <zash@zash.se>
parents: 5386
diff changeset
280 host_session.secure = nil;
5859
e327f2d4e09f mod_c2s, mod_s2s: Set session.encrypted as session.secure does not allways mean encrypted (eg consider_bosh_secure)
Kim Alvefur <zash@zash.se>
parents: 5799
diff changeset
281 host_session.encrypted = nil;
5388
f833d2188c16 mod_s2s: Reset secure flag on new connection attempt
Kim Alvefur <zash@zash.se>
parents: 5386
diff changeset
282
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
283 local conn, handler;
5700
94081567ea9e mod_s2s/s2sout.lib: Only attempt to create an IPv6 socket if LuaSocket supports IPv6
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
284 local proto = connect_host.proto;
94081567ea9e mod_s2s/s2sout.lib: Only attempt to create an IPv6 socket if LuaSocket supports IPv6
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
285 if proto == "IPv4" then
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
286 conn, handler = socket.tcp();
5700
94081567ea9e mod_s2s/s2sout.lib: Only attempt to create an IPv6 socket if LuaSocket supports IPv6
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
287 elseif proto == "IPv6" and socket.tcp6 then
94081567ea9e mod_s2s/s2sout.lib: Only attempt to create an IPv6 socket if LuaSocket supports IPv6
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
288 conn, handler = socket.tcp6();
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
289 else
5700
94081567ea9e mod_s2s/s2sout.lib: Only attempt to create an IPv6 socket if LuaSocket supports IPv6
Matthew Wild <mwild1@gmail.com>
parents: 5552
diff changeset
290 handler = "Unsupported protocol: "..tostring(proto);
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
291 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
292
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
293 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
294 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
295 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
296 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
297
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 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
299 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
300 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
301 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
302 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
303 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
304
7561
ab8324d3b899 mod_s2s: Set read mode for s2sout sockets to the same as s2sins
Kim Alvefur <zash@zash.se>
parents: 7098
diff changeset
305 conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, default_mode);
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
306 host_session.conn = conn;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
307
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
308 -- 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
309 -- 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
310 s2s_listener.register_outgoing(conn, host_session);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5703
diff changeset
311
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
312 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
313 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
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
4602
ba898af15de5 mod_s2s: Collect s2s sources from portmanager and get local address if necessary
Florian Zeitz <florob@babelmonkeys.de>
parents: 4599
diff changeset
316 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
317 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
318
ba898af15de5 mod_s2s: Collect s2s sources from portmanager and get local address if necessary
Florian Zeitz <florob@babelmonkeys.de>
parents: 4599
diff changeset
319 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
320 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
321 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
322 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
323 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
324 for source, _ in pairs(s2s_sources) do
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
325 if source == "*" or source == "0.0.0.0" then
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
326 for _, addr in ipairs(local_addresses("ipv4", true)) do
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
327 sources[#sources + 1] = new_ip(addr, "IPv4");
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
328 end
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
329 elseif source == "::" then
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
330 for _, addr in ipairs(local_addresses("ipv6", true)) do
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
331 sources[#sources + 1] = new_ip(addr, "IPv6");
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
332 end
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
333 else
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
334 sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
335 end
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
336 end
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
337 for i = 1,#sources do
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
338 if sources[i].proto == "IPv6" then
6682
63f5870f9afe mod_s2s/s2sout: Use the local address assigned to UDP sockets instead of util.net to enumerate possible source addresses
Kim Alvefur <zash@zash.se>
parents: 6680
diff changeset
339 has_ipv6 = true;
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
340 elseif sources[i].proto == "IPv4" then
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
341 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
342 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
343 end
7092
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
344 if not (has_ipv4 or has_ipv6) then
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
345 module:log("warn", "No local IPv4 or IPv6 addresses detected, outgoing connections may fail");
bee63de49663 Backout 63f5870f9afe, no longer needed since Windows is currently unsupported
Kim Alvefur <zash@zash.se>
parents: 6682
diff changeset
346 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
347 end);
ba898af15de5 mod_s2s: Collect s2s sources from portmanager and get local address if necessary
Florian Zeitz <florob@babelmonkeys.de>
parents: 4599
diff changeset
348
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
349 return s2sout;