core/s2smanager.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 23 Jan 2012 16:28:20 +0000
changeset 4555 3dce04129693
parent 4461 a81d045e7d16
child 4566 afd09bfa0884
permissions -rw-r--r--
s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1492
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2889
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2889
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
     9
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local hosts = hosts;
631
6957fe7b0313 Bounce stanza errors on failed s2s
Waqas Hussain <waqas20@gmail.com>
parents: 621
diff changeset
    12
local core_process_stanza = function(a, b) core_process_stanza(a, b); end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
local format = string.format;
337
4a1dd1c2c219 We have SRV resolving \o/
Matthew Wild <mwild1@gmail.com>
parents: 333
diff changeset
    14
local t_insert, t_sort = table.insert, table.sort;
255
43a9683bcd19 Fix for detecting when we are routing a stanza to ourself (I'm sure this has something to do with you, waqas...)
Matthew Wild <mwild1@gmail.com>
parents: 254
diff changeset
    15
local get_traceback = debug.traceback;
4461
a81d045e7d16 s2smanager: Fix missing import of 'type' (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 4458
diff changeset
    16
local tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable
a81d045e7d16 s2smanager: Fix missing import of 'type' (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 4458
diff changeset
    17
    = tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
3146
aaaea4cdbf10 s2smanager: Add filters for outgoing bytes and stanzas
Matthew Wild <mwild1@gmail.com>
parents: 2951
diff changeset
    19
local initialize_filters = require "util.filters".initialize;
739
1def06cd9311 Port to new server.lua, quite some changes, but I believe everything to be working
Matthew Wild <mwild1@gmail.com>
parents: 631
diff changeset
    20
local wrapclient = require "net.server".wrapclient;
244
0e3bda34f958 Missed importing a function in last commit
Matthew Wild <mwild1@gmail.com>
parents: 243
diff changeset
    21
local st = require "stanza";
0e3bda34f958 Missed importing a function in last commit
Matthew Wild <mwild1@gmail.com>
parents: 243
diff changeset
    22
local stanza = st.stanza;
931
4514ed5ee943 Fixed: s2smanager: Apply nameprep on hostnames passed in stream tag (part of issue #57)
Waqas Hussain <waqas20@gmail.com>
parents: 896
diff changeset
    23
local nameprep = require "util.encodings".stringprep.nameprep;
3733
26571a99f6e6 core.s2smanager, mod_console, mod_saslauth, util.certverification: rename util.certverification to util.x509
Kim Alvefur <zash@zash.se>
parents: 3663
diff changeset
    24
local cert_verify_identity = require "util.x509".verify_identity;
4424
971a66627f7a s2smanager, xmppserver_listener: Support for connecting to other servers via IPv6
Florian Zeitz <florob@babelmonkeys.de>
parents: 4329
diff changeset
    25
local new_ip = require "util.ip".new_ip;
971a66627f7a s2smanager, xmppserver_listener: Support for connecting to other servers via IPv6
Florian Zeitz <florob@babelmonkeys.de>
parents: 4329
diff changeset
    26
local rfc3484_dest = require "util.rfc3484".destination;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
3436
9c454c029c26 s2smanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 3432
diff changeset
    28
local fire_event = prosody.events.fire_event;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local uuid_gen = require "util.uuid".generate;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
local logger_init = require "util.logger".init;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
local log = logger_init("s2smanager");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
448
2623519b25b0 Switched from md5 to sha256 for dialback key generation
Waqas Hussain <waqas20@gmail.com>
parents: 435
diff changeset
    35
local sha256_hash = require "util.hashes".sha256;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
1793
1fc6c2822e6b s2smanager: Compatibility with domains which use CNAMEs
Matthew Wild <mwild1@gmail.com>
parents: 1791
diff changeset
    37
local adns, dns = require "net.adns", require "net.dns";
1962
3e7231c6d6a9 s2smanager: Fix access of 'config' global without requiring configmanager
Matthew Wild <mwild1@gmail.com>
parents: 1936
diff changeset
    38
local config = require "core.configmanager";
3329
9adafeeadecb s2smanager: Lower default DNS timeout to 15s (it's now a per-DNS-server timeout, rather than total timeout), use net.dns's new timeout system, and remove our custom timeout handlers
Matthew Wild <mwild1@gmail.com>
parents: 3312
diff changeset
    39
local dns_timeout = config.get("*", "core", "dns_timeout") or 15;
1805
7e41ad68fe3c core.s2smanager: Don't recurse CNAMEs infinitely :)
Matthew Wild <mwild1@gmail.com>
parents: 1796
diff changeset
    40
local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
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: 4461
diff changeset
    41
local cfg_sources = config.get("*", "core", "s2s_interface")
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
    42
	or config.get("*", "core", "interface");
4424
971a66627f7a s2smanager, xmppserver_listener: Support for connecting to other servers via IPv6
Florian Zeitz <florob@babelmonkeys.de>
parents: 4329
diff changeset
    43
local sources;
1204
dea89234e545 s2smanager: Timeout DNS requests after 60 seconds (or dns_timeout in config)
Matthew Wild <mwild1@gmail.com>
parents: 1199
diff changeset
    44
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: 4461
diff changeset
    45
--FIXME: s2sout should create its own resolver w/ timeout
3329
9adafeeadecb s2smanager: Lower default DNS timeout to 15s (it's now a per-DNS-server timeout, rather than total timeout), use net.dns's new timeout system, and remove our custom timeout handlers
Matthew Wild <mwild1@gmail.com>
parents: 3312
diff changeset
    46
dns.settimeout(dns_timeout);
9adafeeadecb s2smanager: Lower default DNS timeout to 15s (it's now a per-DNS-server timeout, rather than total timeout), use net.dns's new timeout system, and remove our custom timeout handlers
Matthew Wild <mwild1@gmail.com>
parents: 3312
diff changeset
    47
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
    48
local prosody = _G.prosody;
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    49
incoming_s2s = {};
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
    50
prosody.incoming_s2s = incoming_s2s;
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    51
local incoming_s2s = incoming_s2s;
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    52
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
module "s2smanager"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
local open_sessions = 0;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
function new_incoming(conn)
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    58
	local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
	if true then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
		session.trace = newproxy(true);
583
5821eaa80baa Remove print()s from sessionmanager and s2smanager
Matthew Wild <mwild1@gmail.com>
parents: 559
diff changeset
    61
		getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
	end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	open_sessions = open_sessions + 1;
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: 4461
diff changeset
    64
	session.log = logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    65
	incoming_s2s[session] = true;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
	return session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
2422
affeb565b050 s2smanager: Allow new_outgoing() to create sessions without automatically connecting them (set 3rd parameters to false)
Matthew Wild <mwild1@gmail.com>
parents: 2421
diff changeset
    69
function new_outgoing(from_host, to_host, connect)
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: 4461
diff changeset
    70
	local host_session = { to_host = to_host, from_host = from_host, host = from_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: 4461
diff changeset
    71
		               notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
    72
	hosts[from_host].s2sout[to_host] = 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: 4461
diff changeset
    73
	local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
    74
	host_session.log = logger_init(conn_name);
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
    75
	return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    78
function make_authenticated(session, host)
2799
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    79
	if not session.secure then
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    80
		local local_host = session.direction == "incoming" and session.to_host or session.from_host;
2801
0323bdb1fcfa s2smanager: Fix syntax error)
Matthew Wild <mwild1@gmail.com>
parents: 2800
diff changeset
    81
		if config.get(local_host, "core", "s2s_require_encryption") then
2799
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    82
			session:close({
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    83
				condition = "policy-violation",
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    84
				text = "Encrypted server-to-server communication is required but was not "
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    85
				       ..((session.direction == "outgoing" and "offered") or "used")
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    86
			});
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    87
		end
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    88
	end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
	if session.type == "s2sout_unauthed" then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
		session.type = "s2sout";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
	elseif session.type == "s2sin_unauthed" then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
		session.type = "s2sin";
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    93
		if host then
3650
2b80450bd7ae s2smanager: Compatibility hack for when not using dialback
Paul Aurich <paul@darkrain42.org>
parents: 3540
diff changeset
    94
			if not session.hosts[host] then session.hosts[host] = {}; end
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    95
			session.hosts[host].authed = true;
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    96
		end
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    97
	elseif session.type == "s2sin" and host then
3650
2b80450bd7ae s2smanager: Compatibility hack for when not using dialback
Paul Aurich <paul@darkrain42.org>
parents: 3540
diff changeset
    98
		if not session.hosts[host] then session.hosts[host] = {}; end
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
    99
		session.hosts[host].authed = true;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
	else
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
		return false;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
	end
1075
831c84cbf5fa s2smanager: Miscellaneous logging improvements, changing levels, improving messages and using session loggers where possible
Matthew Wild <mwild1@gmail.com>
parents: 1007
diff changeset
   103
	session.log("debug", "connection %s->%s is now authenticated", session.from_host or "(unknown)", session.to_host or "(unknown)");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
	mark_connected(session);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
	
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
	return true;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
1891
fd725a28141d s2smanager: Small comment
Matthew Wild <mwild1@gmail.com>
parents: 1890
diff changeset
   110
-- Stream is authorised, and ready for normal stanzas
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
function mark_connected(session)
186
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   112
	local sendq, send = session.sendq, session.sends2s;
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   113
	
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   114
	local from, to = session.from_host, session.to_host;
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   115
	
1075
831c84cbf5fa s2smanager: Miscellaneous logging improvements, changing levels, improving messages and using session loggers where possible
Matthew Wild <mwild1@gmail.com>
parents: 1007
diff changeset
   116
	session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete");
186
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   117
	
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   118
	local send_to_host = send_to_host;
4017
2cb5bb26b831 s2smanager: session.send(): Return the result of send_to_host() (and incidentally make this a tail call \o/)
Matthew Wild <mwild1@gmail.com>
parents: 4016
diff changeset
   119
	function session.send(data) return send_to_host(to, from, data); end
190
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   120
	
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   121
	local event_data = { session = session };
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   122
	if session.type == "s2sout" then
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   123
		prosody.events.fire_event("s2sout-established", event_data);
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   124
		hosts[session.from_host].events.fire_event("s2sout-established", event_data);
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   125
	else
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   126
		prosody.events.fire_event("s2sin-established", event_data);
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   127
		hosts[session.to_host].events.fire_event("s2sin-established", event_data);
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   128
	end
186
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   129
	
190
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   130
	if session.direction == "outgoing" then
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   131
		if sendq then
269
3cfac0e5e6ca Log how many queued stanzas we send
Waqas Hussain <waqas20@gmail.com>
parents: 266
diff changeset
   132
			session.log("debug", "sending "..#sendq.." queued stanzas across new outgoing connection to "..session.to_host);
190
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   133
			for i, data in ipairs(sendq) do
631
6957fe7b0313 Bounce stanza errors on failed s2s
Waqas Hussain <waqas20@gmail.com>
parents: 621
diff changeset
   134
				send(data[1]);
190
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   135
				sendq[i] = nil;
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   136
			end
1e993b7deae7 General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
Matthew Wild <mwild1@gmail.com>
parents: 186
diff changeset
   137
			session.sendq = nil;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
		end
1345
35b5686d73ea s2smanager: Remove srv_hosts from session when connected, this fixes attempting to reconnect s2s sessions when they are closed during shutdown
Matthew Wild <mwild1@gmail.com>
parents: 1308
diff changeset
   139
		
35b5686d73ea s2smanager: Remove srv_hosts from session when connected, this fixes attempting to reconnect s2s sessions when they are closed during shutdown
Matthew Wild <mwild1@gmail.com>
parents: 1308
diff changeset
   140
		session.srv_hosts = nil;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
	end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   144
local resting_session = { -- Resting, not dead
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   145
		destroyed = true;
2915
f47bd0f7e2e6 sessionmanager, s2smanager: Add type of ?2s_destroyed to resting sessions (fixes a logging traceback, thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2892
diff changeset
   146
		type = "s2s_destroyed";
2748
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   147
		open_stream = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   148
			session.log("debug", "Attempt to open stream on resting session");
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   149
		end;
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   150
		close = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   151
			session.log("debug", "Attempt to close already-closed session");
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   152
		end;
3459
543f31cdde19 sessionmanager, s2smanager: Give resting sessions a pass-through filter, fixes #202
Matthew Wild <mwild1@gmail.com>
parents: 3436
diff changeset
   153
		filter = function (type, data) return data; end;
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   154
	}; resting_session.__index = resting_session;
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   155
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   156
function retire_session(session, reason)
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   157
	local log = session.log or log;
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   158
	for k in pairs(session) do
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   159
		if k ~= "trace" and k ~= "log" and k ~= "id" then
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   160
			session[k] = nil;
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   161
		end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   162
	end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   163
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   164
	session.destruction_reason = reason;
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   165
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   166
	function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   167
	function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   168
	return setmetatable(session, resting_session);
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
   169
end
2857
6036c4b75235 sessionmanager, s2smanager: Give sessions dummy data handlers that log when data is received by a destroyed session
Matthew Wild <mwild1@gmail.com>
parents: 2712
diff changeset
   170
2781
4b5881b6bbfc s2smanager: Have both destroy_session and bounce_sendq accept a reason string to include in bounced stanzas
Matthew Wild <mwild1@gmail.com>
parents: 2780
diff changeset
   171
function destroy_session(session, reason)
2749
8dc5f3651501 s2smanager: Don't re-destroy destroyed sessions
Matthew Wild <mwild1@gmail.com>
parents: 2748
diff changeset
   172
	if session.destroyed then return; end
4262
fd4d1cf6d63e s2smanager: Log reason when destroying a session.
Waqas Hussain <waqas20@gmail.com>
parents: 4200
diff changeset
   173
	(session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 327
diff changeset
   174
	
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   175
	if session.direction == "outgoing" then
260
182f0c895676 Now outgoing s2s sessions are associated with their from_host, fixes #15
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
   176
		hosts[session.from_host].s2sout[session.to_host] = 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: 4461
diff changeset
   177
		session:bounce_sendq(reason);
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   178
	elseif session.direction == "incoming" then
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
   179
		incoming_s2s[session] = nil;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   180
	end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 327
diff changeset
   181
	
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   182
	local event_data = { session = session, reason = reason };
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   183
	if session.type == "s2sout" then
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   184
		prosody.events.fire_event("s2sout-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   185
		if hosts[session.from_host] then
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   186
			hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   187
		end
3489
1b76d18e8045 s2smanager: Don't fire s2sin-destroyed for sessions that were never fully established (thanks Thomas)
Matthew Wild <mwild1@gmail.com>
parents: 3488
diff changeset
   188
	elseif session.type == "s2sin" then
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   189
		prosody.events.fire_event("s2sin-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   190
		if hosts[session.to_host] then
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   191
			hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   192
		end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   193
	end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   194
	
4019
80aa47c009f0 s2smanager: destroy_session(): Pass reason to retire_session() and return true on successful destruction
Matthew Wild <mwild1@gmail.com>
parents: 4018
diff changeset
   195
	retire_session(session, reason); -- Clean session until it is GC'd
80aa47c009f0 s2smanager: destroy_session(): Pass reason to retire_session() and return true on successful destruction
Matthew Wild <mwild1@gmail.com>
parents: 4018
diff changeset
   196
	return true;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   197
end
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   198
225
bbbd169b326b Just committing this warning, because I want to know if the problem really affects us
Matthew Wild <mwild1@gmail.com>
parents: 199
diff changeset
   199
return _M;