core/s2smanager.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 23 Jul 2012 17:32:33 +0100
changeset 4993 5243b74a4cbb
parent 4930 42ac50c0382e
child 4994 d4e6a07a7c33
permissions -rw-r--r--
Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
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;
4831
da2c49a9ab99 s2smanager: Clean up unused imports.
Kim Alvefur <zash@zash.se>
parents: 4684
diff changeset
    12
local tostring, pairs, ipairs, getmetatable, newproxy, setmetatable
da2c49a9ab99 s2smanager: Clean up unused imports.
Kim Alvefur <zash@zash.se>
parents: 4684
diff changeset
    13
    = tostring, pairs, ipairs, getmetatable, newproxy, setmetatable;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
3436
9c454c029c26 s2smanager: Remove dependency on eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 3432
diff changeset
    15
local fire_event = prosody.events.fire_event;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local logger_init = require "util.logger".init;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
local log = logger_init("s2smanager");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
1962
3e7231c6d6a9 s2smanager: Fix access of 'config' global without requiring configmanager
Matthew Wild <mwild1@gmail.com>
parents: 1936
diff changeset
    20
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
    21
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
    22
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
    23
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
    24
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
    25
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
    26
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
module "s2smanager"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
local open_sessions = 0;
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
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
    32
	local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	if true then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		session.trace = newproxy(true);
583
5821eaa80baa Remove print()s from sessionmanager and s2smanager
Matthew Wild <mwild1@gmail.com>
parents: 559
diff changeset
    35
		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
    36
	end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	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
    38
	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
    39
	incoming_s2s[session] = true;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	return session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
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
    43
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
    44
	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
    45
		               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
    46
	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
    47
	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
    48
	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
    49
	return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
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
    52
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
    53
	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
    54
		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
    55
		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
    56
			session:close({
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    57
				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
    58
				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
    59
				       ..((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
    60
			});
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    61
		end
0dc7b5ceaf8f s2smanager: Make require_s2s_encryption do what it says on the tin
Matthew Wild <mwild1@gmail.com>
parents: 2787
diff changeset
    62
	end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	if session.type == "s2sout_unauthed" then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
		session.type = "s2sout";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	elseif session.type == "s2sin_unauthed" then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		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
    67
		if host then
3650
2b80450bd7ae s2smanager: Compatibility hack for when not using dialback
Paul Aurich <paul@darkrain42.org>
parents: 3540
diff changeset
    68
			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
    69
			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
    70
		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
    71
	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
    72
		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
    73
		session.hosts[host].authed = true;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	else
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		return false;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	end
4493
cad62a675b60 s2smanager: Log the host when marking it authenticated
Paul Aurich <paul@darkrain42.org>
parents: 4329
diff changeset
    77
	session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host or "(unknown)", session.to_host or "(unknown)", host);
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
	mark_connected(session);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
	return true;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
1891
fd725a28141d s2smanager: Small comment
Matthew Wild <mwild1@gmail.com>
parents: 1890
diff changeset
    84
-- Stream is authorised, and ready for normal stanzas
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
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
    86
	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
    87
	
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
    88
	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
    89
	
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 4930
diff changeset
    90
	session.log("info", "%s s2s connection %s->%s complete", session.direction, from, to);
4566
afd09bfa0884 s2smanager: remove send_to_host.
Florian Zeitz <florob@babelmonkeys.de>
parents: 4555
diff changeset
    91
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
    92
	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
    93
	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
    94
		prosody.events.fire_event("s2sout-established", event_data);
4822
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
    95
		hosts[from].events.fire_event("s2sout-established", event_data);
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
    96
	else
4822
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
    97
		local host_session = hosts[to];
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
    98
		session.send = function(stanza)
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
    99
			host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
   100
		end;
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
   101
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   102
		prosody.events.fire_event("s2sin-established", event_data);
4822
5ef05f32bc42 mod_s2s, s2smanager, mod_dialback: Move addition of session.send() on s2sin to after they are authenticated (thus from mod_s2s to s2smanager). Update mod_dialback to fire route/remote directly, as session.send() is no longer available for s2sin_unauthed. Fixes #291.
Matthew Wild <mwild1@gmail.com>
parents: 4684
diff changeset
   103
		hosts[to].events.fire_event("s2sin-established", event_data);
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
   104
	end
186
bfa8a30ea488 sends2s -> s2s_session.send(), s2s_session.send() -> s2s_session.sends2s()
Matthew Wild <mwild1@gmail.com>
parents: 179
diff changeset
   105
	
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
   106
	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
   107
		if sendq then
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 4930
diff changeset
   108
			session.log("debug", "sending %d queued stanzas across new outgoing connection to %s", #sendq, 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
   109
			for i, data in ipairs(sendq) do
631
6957fe7b0313 Bounce stanza errors on failed s2s
Waqas Hussain <waqas20@gmail.com>
parents: 621
diff changeset
   110
				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
   111
				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
   112
			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
   113
			session.sendq = nil;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
		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
   115
		
4576
79c813c4ecc0 s2smanager, mod_s2s: clear up ip_hosts after s2s is marked as established, remove useless space from mod_s2s code
Marco Cirillo <maranda@lightwitch.org>
parents: 4566
diff changeset
   116
		session.ip_hosts = nil;
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
   117
		session.srv_hosts = nil;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
	end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
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
   121
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
   122
		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
   123
		type = "s2s_destroyed";
2748
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   124
		open_stream = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   125
			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
   126
		end;
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   127
		close = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
   128
			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
   129
		end;
3459
543f31cdde19 sessionmanager, s2smanager: Give resting sessions a pass-through filter, fixes #202
Matthew Wild <mwild1@gmail.com>
parents: 3436
diff changeset
   130
		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
   131
	}; 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
   132
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   133
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
   134
	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
   135
	for k in pairs(session) do
4930
42ac50c0382e s2smanager: Fix a traceback when we close a s2s connection ourselves (thanks for the testing Zash).
Waqas Hussain <waqas20@gmail.com>
parents: 4833
diff changeset
   136
		if k ~= "trace" and k ~= "log" and k ~= "id" and k ~= "conn" then
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
   137
			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
   138
		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
   139
	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
   140
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   141
	session.destruction_reason = reason;
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
   142
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
   143
	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
   144
	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
   145
	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
   146
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
   147
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
   148
function destroy_session(session, reason)
2749
8dc5f3651501 s2smanager: Don't re-destroy destroyed sessions
Matthew Wild <mwild1@gmail.com>
parents: 2748
diff changeset
   149
	if session.destroyed then return; end
4262
fd4d1cf6d63e s2smanager: Log reason when destroying a session.
Waqas Hussain <waqas20@gmail.com>
parents: 4200
diff changeset
   150
	(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
   151
	
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   152
	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
   153
		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
   154
		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
   155
	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
   156
		incoming_s2s[session] = nil;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   157
	end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 327
diff changeset
   158
	
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   159
	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
   160
	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
   161
		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
   162
		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
   163
			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
   164
		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
   165
	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
   166
		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
   167
		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
   168
			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
   169
		end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   170
	end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
   171
	
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
   172
	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
   173
	return true;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   174
end
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
   175
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
   176
return _M;