2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local tostring, pairs, ipairs, getmetatable, newproxy, setmetatable
13 = tostring, pairs, ipairs, getmetatable, newproxy, setmetatable;
15 local fire_event = prosody.events.fire_event;
16 local logger_init = require "util.logger".init;
18 local log = logger_init("s2smanager");
20 local config = require "core.configmanager";
22 local prosody = _G.prosody;
24 prosody.incoming_s2s = incoming_s2s;
25 local incoming_s2s = incoming_s2s;
29 local open_sessions = 0;
31 function new_incoming(conn)
32 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
34 session.trace = newproxy(true);
35 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
37 open_sessions = open_sessions + 1;
38 session.log = logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$"));
39 incoming_s2s[session] = true;
43 function new_outgoing(from_host, to_host, connect)
44 local host_session = { to_host = to_host, from_host = from_host, host = from_host,
45 notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
46 hosts[from_host].s2sout[to_host] = host_session;
47 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
48 host_session.log = logger_init(conn_name);
52 function make_authenticated(session, host)
53 if not session.secure then
54 local local_host = session.direction == "incoming" and session.to_host or session.from_host;
55 if config.get(local_host, "core", "s2s_require_encryption") then
57 condition = "policy-violation",
58 text = "Encrypted server-to-server communication is required but was not "
59 ..((session.direction == "outgoing" and "offered") or "used")
63 if session.type == "s2sout_unauthed" then
64 session.type = "s2sout";
65 elseif session.type == "s2sin_unauthed" then
66 session.type = "s2sin";
68 if not session.hosts[host] then session.hosts[host] = {}; end
69 session.hosts[host].authed = true;
71 elseif session.type == "s2sin" and host then
72 if not session.hosts[host] then session.hosts[host] = {}; end
73 session.hosts[host].authed = true;
77 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
79 mark_connected(session);
84 -- Stream is authorised, and ready for normal stanzas
85 function mark_connected(session)
86 local sendq, send = session.sendq, session.sends2s;
88 local from, to = session.from_host, session.to_host;
90 session.log("info", "%s s2s connection %s->%s complete", session.direction, from, to);
92 local event_data = { session = session };
93 if session.type == "s2sout" then
94 prosody.events.fire_event("s2sout-established", event_data);
95 hosts[from].events.fire_event("s2sout-established", event_data);
97 local host_session = hosts[to];
98 session.send = function(stanza)
99 host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
102 prosody.events.fire_event("s2sin-established", event_data);
103 hosts[to].events.fire_event("s2sin-established", event_data);
106 if session.direction == "outgoing" then
108 session.log("debug", "sending %d queued stanzas across new outgoing connection to %s", #sendq, session.to_host);
109 for i, data in ipairs(sendq) do
116 session.ip_hosts = nil;
117 session.srv_hosts = nil;
121 local resting_session = { -- Resting, not dead
123 type = "s2s_destroyed";
124 open_stream = function (session)
125 session.log("debug", "Attempt to open stream on resting session");
127 close = function (session)
128 session.log("debug", "Attempt to close already-closed session");
130 filter = function (type, data) return data; end;
131 }; resting_session.__index = resting_session;
133 function retire_session(session, reason)
134 local log = session.log or log;
135 for k in pairs(session) do
136 if k ~= "trace" and k ~= "log" and k ~= "id" and k ~= "conn" then
141 session.destruction_reason = reason;
143 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
144 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
145 return setmetatable(session, resting_session);
148 function destroy_session(session, reason)
149 if session.destroyed then return; end
150 (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
152 if session.direction == "outgoing" then
153 hosts[session.from_host].s2sout[session.to_host] = nil;
154 session:bounce_sendq(reason);
155 elseif session.direction == "incoming" then
156 incoming_s2s[session] = nil;
159 local event_data = { session = session, reason = reason };
160 if session.type == "s2sout" then
161 prosody.events.fire_event("s2sout-destroyed", event_data);
162 if hosts[session.from_host] then
163 hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
165 elseif session.type == "s2sin" then
166 prosody.events.fire_event("s2sin-destroyed", event_data);
167 if hosts[session.to_host] then
168 hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
172 retire_session(session, reason); -- Clean session until it is GC'd