Annotate

net/xmppserver_listener.lua @ 1951:632039101699

xmppserver_listener: More forcefully close s2s connections (fixes fd leak)
author Matthew Wild <mwild1@gmail.com>
date Wed, 14 Oct 2009 14:07:50 +0100
parent 1617:c6e175a0d83b
child 1953:64538c620450
child 1954:4ef18b2dcc21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1040
diff changeset
1 -- Prosody IM
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
3 -- Copyright (C) 2008-2009 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: 625
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 625
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 logger = require "logger";
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 990
diff changeset
12 local log = logger.init("xmppserver_listener");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local lxp = require "lxp"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local init_xmlhandlers = require "core.xmlhandlers"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local s2s_new_incoming = require "core.s2smanager".new_incoming;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local s2s_streamopened = require "core.s2smanager".streamopened;
342
52f75260a22d Incorrect function set as callback
Matthew Wild <mwild1@gmail.com>
parents: 333
diff changeset
17 local s2s_streamclosed = require "core.s2smanager".streamclosed;
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
18 local s2s_destroy_session = require "core.s2smanager".destroy_session;
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
19 local s2s_attempt_connect = require "core.s2smanager".attempt_connection;
900
686e3e4a7e15 net.xmppserver_listener: Set default namespace to jabber:server
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
20 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream",
686e3e4a7e15 net.xmppserver_listener: Set default namespace to jabber:server
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
21 default_ns = "jabber:server",
686e3e4a7e15 net.xmppserver_listener: Set default namespace to jabber:server
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
22 streamopened = s2s_streamopened, streamclosed = s2s_streamclosed, handlestanza = core_process_stanza };
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
23
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
24 function stream_callbacks.error(session, error, data)
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
25 if error == "no-stream" then
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
26 session:close("invalid-namespace");
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
27 else
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
28 session.log("debug", "Server-to-server XML parse error: %s", tostring(error));
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
29 session:close("xml-not-well-formed");
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
30 end
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
31 end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
32
611
7bb91fcddaf8 Fix blank tracebacks for c2s/s2s connections
Matthew Wild <mwild1@gmail.com>
parents: 604
diff changeset
33 local function handleerr(err) log("error", "Traceback[s2s]: %s: %s", tostring(err), debug.traceback()); end
585
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
34 function stream_callbacks.handlestanza(a, b)
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
35 xpcall(function () core_process_stanza(a, b) end, handleerr);
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
36 end
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
37
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local connlisteners_register = require "net.connlisteners".register;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local t_insert = table.insert;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local t_concat = table.concat;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local m_random = math.random;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local format = string.format;
1040
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 990
diff changeset
45 local sessionmanager = require "core.sessionmanager";
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 990
diff changeset
46 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
4c79b28bce64 xmpp{server,client}_listener: Fix global accesses
Matthew Wild <mwild1@gmail.com>
parents: 990
diff changeset
47 local st = require "util.stanza";
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local sessions = {};
451
e9f269e5204e No more reading 1 byte at a time from sockets
Matthew Wild <mwild1@gmail.com>
parents: 434
diff changeset
50 local xmppserver = { default_port = 5269, default_mode = "*a" };
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- These are session methods --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local function session_reset_stream(session)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 -- Reset stream
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
56 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 session.parser = parser;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 session.notopen = true;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 function session.data(conn, data)
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
62 local ok, err = parser:parse(data);
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
63 if ok then return; end
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
64 session:close("xml-not-well-formed");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
557
c9b3ffb08fe3 Disconnect with stream errors on bad XML, or invalid stream namespace
Matthew Wild <mwild1@gmail.com>
parents: 545
diff changeset
66
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 return true;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
70
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
71 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
72 local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:gsub("%|[^|]+$", ""), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
333
8d15b073fdbe session:disconnect() -> session:close() for consistency with other Lua APIs
Matthew Wild <mwild1@gmail.com>
parents: 331
diff changeset
73 local function session_close(session, reason)
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
74 local log = session.log or log;
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
75 if session.conn then
1617
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
76 if session.notopen then
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
77 session.sends2s("<?xml version='1.0'?>");
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
78 session.sends2s(st.stanza("stream:stream", default_stream_attr):top_tag());
c6e175a0d83b xmpp{client,server,component]_listener: Open stream if sending an error and it isn't already open. Fixes #120
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
79 end
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
80 if reason then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
81 if type(reason) == "string" then -- assume stream error
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
82 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason);
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
83 session.sends2s(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
84 elseif type(reason) == "table" then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
85 if reason.condition then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
86 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
87 if reason.text then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
88 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
89 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
90 if reason.extra then
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
91 stanza:add_child(reason.extra);
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
92 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
93 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza));
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
94 session.sends2s(stanza);
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
95 elseif reason.name then -- a stanza
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
96 log("info", "Disconnecting %s->%s[%s], <stream:error> is: %s", session.from_host or "(unknown host)", session.to_host or "(unknown host)", session.type, tostring(reason));
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
97 session.sends2s(reason);
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
98 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
99 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
100 end
331
830fd67f9378 Quite some changes, to:
Matthew Wild <mwild1@gmail.com>
parents: 330
diff changeset
101 session.sends2s("</stream:stream>");
1951
632039101699 xmppserver_listener: More forcefully close s2s connections (fixes fd leak)
Matthew Wild <mwild1@gmail.com>
parents: 1617
diff changeset
102 if sesson.notopen or not session.conn.close() then
632039101699 xmppserver_listener: More forcefully close s2s connections (fixes fd leak)
Matthew Wild <mwild1@gmail.com>
parents: 1617
diff changeset
103 session.conn.close(true); -- Force FIXME: timer?
632039101699 xmppserver_listener: More forcefully close s2s connections (fixes fd leak)
Matthew Wild <mwild1@gmail.com>
parents: 1617
diff changeset
104 end
330
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
105 session.conn.close();
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
106 xmppserver.disconnect(session.conn, "stream error");
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
107 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
108 end
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
109
d9d4c1de16ce s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents: 232
diff changeset
110
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 -- End of session methods --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 function xmppserver.listener(conn, data)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local session = sessions[conn];
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 if not session then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 session = s2s_new_incoming(conn);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 sessions[conn] = session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 -- Logging functions --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
585
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
121
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
122 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$");
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
123 session.log = logger.init(conn_name);
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
124
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
125 session.log("info", "Incoming s2s connection");
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 session.reset_stream = session_reset_stream;
333
8d15b073fdbe session:disconnect() -> session:close() for consistency with other Lua APIs
Matthew Wild <mwild1@gmail.com>
parents: 331
diff changeset
128 session.close = session_close;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 session_reset_stream(session); -- Initialise, ready for use
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
604
b6a31e97d018 stanza_dispatch != dispatch_stanza
Matthew Wild <mwild1@gmail.com>
parents: 598
diff changeset
132 session.dispatch_stanza = stream_callbacks.handlestanza;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if data then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 session.data(conn, data);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
139 function xmppserver.disconnect(conn, err)
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
140 local session = sessions[conn];
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
141 if session then
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
142 if err and err ~= "closed" and session.srv_hosts then
974
Matthew Wild <mwild1@gmail.com>
parents: 900
diff changeset
143 (session.log or log)("debug", "s2s connection closed unexpectedly");
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
144 if s2s_attempt_connect(session, err) then
974
Matthew Wild <mwild1@gmail.com>
parents: 900
diff changeset
145 (session.log or log)("debug", "...so we're going to try again");
434
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
146 return; -- Session lives for now
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
147 end
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
148 end
0d7ba3742f7a (Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents: 431
diff changeset
149 (session.log or log)("info", "s2s disconnected: %s->%s (%s)", tostring(session.from_host), tostring(session.to_host), tostring(err));
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
150 s2s_destroy_session(session);
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
151 sessions[conn] = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
152 session = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
153 collectgarbage("collect");
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
154 end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 function xmppserver.register_outgoing(conn, session)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 session.direction = "outgoing";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 sessions[conn] = session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160
990
235abebc896e xmppserver_listener: Add session:close() method to outgoing s2s connections too
Matthew Wild <mwild1@gmail.com>
parents: 974
diff changeset
161 session.reset_stream = session_reset_stream;
235abebc896e xmppserver_listener: Add session:close() method to outgoing s2s connections too
Matthew Wild <mwild1@gmail.com>
parents: 974
diff changeset
162 session.close = session_close;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 session_reset_stream(session); -- Initialise, ready for use
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
585
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
165 --local function handleerr(err) print("Traceback:", err, debug.traceback()); end
033817e12ddb Code tidying for xmpp{client,server}_listeners
Matthew Wild <mwild1@gmail.com>
parents: 557
diff changeset
166 --session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 connlisteners_register("xmppserver", xmppserver);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 -- We need to perform some initialisation when a connection is created
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 -- We also need to perform that same initialisation at other points (SASL, TLS, ...)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 -- ...and we need to handle data
226
ba4711c4e8d2 Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents: 163
diff changeset
176 -- ...and record all sessions associated with connections