Software /
code /
prosody
Annotate
net/xmppserver_listener.lua @ 434:0d7ba3742f7a
(Basic) IDNA and SRV fallback support
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 26 Nov 2008 23:44:30 +0000 |
parent | 431:3c89a073db53 |
child | 451:e9f269e5204e |
rev | line source |
---|---|
148 | 1 |
2 local logger = require "logger"; | |
3 local lxp = require "lxp" | |
4 local init_xmlhandlers = require "core.xmlhandlers" | |
5 local sm_new_session = require "core.sessionmanager".new_session; | |
6 local s2s_new_incoming = require "core.s2smanager".new_incoming; | |
7 local s2s_streamopened = require "core.s2smanager".streamopened; | |
342
52f75260a22d
Incorrect function set as callback
Matthew Wild <mwild1@gmail.com>
parents:
333
diff
changeset
|
8 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
|
9 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
|
10 local s2s_attempt_connect = require "core.s2smanager".attempt_connection; |
331 | 11 local stream_callbacks = { streamopened = s2s_streamopened, streamclosed = s2s_streamclosed }; |
12 | |
148 | 13 local connlisteners_register = require "net.connlisteners".register; |
14 | |
15 local t_insert = table.insert; | |
16 local t_concat = table.concat; | |
17 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 | |
18 local m_random = math.random; | |
19 local format = string.format; | |
20 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); | |
21 local st = stanza; | |
22 | |
23 local sessions = {}; | |
24 local xmppserver = { default_port = 5269 }; | |
25 | |
26 -- These are session methods -- | |
27 | |
28 local function session_reset_stream(session) | |
29 -- Reset stream | |
331 | 30 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "|"); |
148 | 31 session.parser = parser; |
32 | |
33 session.notopen = true; | |
34 | |
35 function session.data(conn, data) | |
36 parser:parse(data); | |
37 end | |
38 return true; | |
39 end | |
40 | |
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
|
41 |
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
42 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; |
333
8d15b073fdbe
session:disconnect() -> session:close() for consistency with other Lua APIs
Matthew Wild <mwild1@gmail.com>
parents:
331
diff
changeset
|
43 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
|
44 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
|
45 if session.conn 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
|
46 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
|
47 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
|
48 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, reason); |
331 | 49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 log("info", "Disconnecting %s[%s], <stream:error> is: %s", session.host or "(unknown host)", session.type, tostring(stanza)); |
331 | 60 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
|
61 elseif reason.name then -- a stanza |
331 | 62 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)); |
63 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
|
64 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
|
65 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
|
66 end |
331 | 67 session.sends2s("</stream:stream>"); |
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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 |
d9d4c1de16ce
s2s sessions can now be disconnected, with or without a stream error. Fixes #8
Matthew Wild <mwild1@gmail.com>
parents:
232
diff
changeset
|
73 |
148 | 74 -- End of session methods -- |
75 | |
76 function xmppserver.listener(conn, data) | |
77 local session = sessions[conn]; | |
78 if not session then | |
79 session = s2s_new_incoming(conn); | |
80 sessions[conn] = session; | |
81 | |
82 -- Logging functions -- | |
83 | |
84 local mainlog, log = log; | |
85 do | |
86 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$"); | |
87 log = logger.init(conn_name); | |
88 end | |
89 local print = function (...) log("info", t_concatall({...}, "\t")); end | |
90 session.log = log; | |
91 | |
92 print("Incoming s2s connection"); | |
93 | |
94 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
|
95 session.close = session_close; |
148 | 96 |
97 session_reset_stream(session); -- Initialise, ready for use | |
98 | |
99 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
100 -- this will avoid the useless indirection we have atm | |
101 -- (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
|
102 |
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
|
103 -- Debug version -- |
232
20745f8f4cf1
Actually show error and position when we show a traceback :)
Matthew Wild <mwild1@gmail.com>
parents:
226
diff
changeset
|
104 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
|
105 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
148 | 106 end |
107 if data then | |
108 session.data(conn, data); | |
109 end | |
110 end | |
111 | |
434
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
112 function xmppserver.disconnect(conn, err) |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
113 local session = sessions[conn]; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
114 if session then |
434
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
115 if err and err ~= "closed" and session.srv_hosts then |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
116 if s2s_attempt_connect(session, err) then |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
117 return; -- Session lives for now |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
118 end |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
119 end |
0d7ba3742f7a
(Basic) IDNA and SRV fallback support
Matthew Wild <mwild1@gmail.com>
parents:
431
diff
changeset
|
120 (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
|
121 s2s_destroy_session(session); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
122 sessions[conn] = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
123 session = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
124 collectgarbage("collect"); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
125 end |
148 | 126 end |
127 | |
128 function xmppserver.register_outgoing(conn, session) | |
129 session.direction = "outgoing"; | |
130 sessions[conn] = session; | |
131 | |
132 session.reset_stream = session_reset_stream; | |
133 session_reset_stream(session); -- Initialise, ready for use | |
134 | |
135 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
136 -- this will avoid the useless indirection we have atm | |
137 -- (I'm on a mission, no time to fix now) | |
431
3c89a073db53
Fix a waqas copy/paste error. It was my fault again apparently.
Matthew Wild <mwild1@gmail.com>
parents:
426
diff
changeset
|
138 local function handleerr(err) print("Traceback:", err, debug.traceback()); end |
426
3d8778059e90
Wrapped a core_process_stanza call in an xpcall call
Waqas Hussain <waqas20@gmail.com>
parents:
342
diff
changeset
|
139 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
148 | 140 end |
141 | |
142 connlisteners_register("xmppserver", xmppserver); | |
143 | |
144 | |
145 -- We need to perform some initialisation when a connection is created | |
146 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) | |
147 | |
148 -- ...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
|
149 -- ...and record all sessions associated with connections |