Annotate

net/xmppserver_listener.lua @ 280:516f4c901991

Rewrote SASL Digest-MD5 responce generating code, fixed some realm related issue and tested it successfully with Psi. Thanks to dwd, remko and jake.
author Tobias Markmann <tm@ayena.de>
date Wed, 12 Nov 2008 21:38:46 +0100
parent 232:20745f8f4cf1
child 330:d9d4c1de16ce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local logger = require "logger";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local lxp = require "lxp"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local init_xmlhandlers = require "core.xmlhandlers"
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local sm_new_session = require "core.sessionmanager".new_session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local s2s_new_incoming = require "core.s2smanager".new_incoming;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local s2s_streamopened = require "core.s2smanager".streamopened;
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
8 local s2s_destroy_session = require "core.s2smanager".destroy_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local connlisteners_register = require "net.connlisteners".register;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local t_insert = table.insert;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local t_concat = table.concat;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 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
15 local m_random = math.random;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local format = string.format;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local st = stanza;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local sessions = {};
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local xmppserver = { default_port = 5269 };
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- These are session methods --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function session_reset_stream(session)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Reset stream
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 session.parser = parser;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 session.notopen = true;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function session.data(conn, data)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 parser:parse(data);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return true;
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
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 -- End of session methods --
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 function xmppserver.listener(conn, data)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local session = sessions[conn];
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not session then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 session = s2s_new_incoming(conn);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 sessions[conn] = session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- Logging functions --
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local mainlog, log = log;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 do
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 log = logger.init(conn_name);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local print = function (...) log("info", t_concatall({...}, "\t")); end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 session.log = log;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 print("Incoming s2s connection");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 session.reset_stream = session_reset_stream;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 session_reset_stream(session); -- Initialise, ready for use
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- this will avoid the useless indirection we have atm
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 -- (I'm on a mission, no time to fix now)
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
65
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
66 -- Debug version --
232
20745f8f4cf1 Actually show error and position when we show a traceback :)
Matthew Wild <mwild1@gmail.com>
parents: 226
diff changeset
67 local function handleerr(err) print("Traceback:", err, debug.traceback()); end
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
68 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end
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
69
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
70 -- session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if data then
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 session.data(conn, data);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
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
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 function xmppserver.disconnect(conn)
163
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
79 local session = sessions[conn];
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
80 if session then
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
81 s2s_destroy_session(session);
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
82 sessions[conn] = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
83 session = nil;
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
84 collectgarbage("collect");
3fec9b512d4e Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents: 148
diff changeset
85 end
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 function xmppserver.register_outgoing(conn, session)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 session.direction = "outgoing";
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 sessions[conn] = session;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 session.reset_stream = session_reset_stream;
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 session_reset_stream(session); -- Initialise, ready for use
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call,
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 -- this will avoid the useless indirection we have atm
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 -- (I'm on a mission, no time to fix now)
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 connlisteners_register("xmppserver", xmppserver);
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 -- We need to perform some initialisation when a connection is created
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 -- 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
106
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 -- ...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
108 -- ...and record all sessions associated with connections