Software /
code /
prosody
Annotate
core/s2smanager.lua @ 10278:94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 29 Sep 2019 14:50:04 +0200 |
parent | 10244:483cc47f15f8 |
child | 10301:3123a13cf577 |
rev | line source |
---|---|
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 |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5459
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
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 | 10 |
5366
c1357b7fbca3
s2smanager: Access prosody.hosts instead of hosts global directly
Matthew Wild <mwild1@gmail.com>
parents:
5362
diff
changeset
|
11 local hosts = prosody.hosts; |
10103
18cab054364a
core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents:
10102
diff
changeset
|
12 local pairs, setmetatable = pairs, setmetatable; |
148 | 13 |
14 local logger_init = require "util.logger".init; | |
9936
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
15 local sessionlib = require "util.session"; |
148 | 16 |
17 local log = logger_init("s2smanager"); | |
18 | |
3476
193bb0936a4e
s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents:
3459
diff
changeset
|
19 local prosody = _G.prosody; |
8675
d3d74e923e4e
s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
20 local incoming_s2s = {}; |
d3d74e923e4e
s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
21 _G.incoming_s2s = 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
|
22 prosody.incoming_s2s = incoming_s2s; |
5349
0d11e393201f
s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents:
5306
diff
changeset
|
23 local fire_event = prosody.events.fire_event; |
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
|
24 |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
25 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7950
diff
changeset
|
26 -- luacheck: std none |
148 | 27 |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
28 local function new_incoming(conn) |
9936
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
29 local host_session = sessionlib.new("s2sin"); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
30 sessionlib.set_id(host_session); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
31 sessionlib.set_logger(host_session); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
32 sessionlib.set_conn(host_session, conn); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
33 host_session.direction = "incoming"; |
10243
b36765ab0ae3
core.s2smanager: Add [direction] boolean flags to s2s connections
Kim Alvefur <zash@zash.se>
parents:
10103
diff
changeset
|
34 host_session.incoming = true; |
9938
4e8ba156738b
core.s2smanager: Fix previous commit (Thanks Martin)
Kim Alvefur <zash@zash.se>
parents:
9936
diff
changeset
|
35 host_session.hosts = {}; |
9934
69982753fe4b
core.s2smanager: Rename variable to be same in two functions
Kim Alvefur <zash@zash.se>
parents:
9787
diff
changeset
|
36 incoming_s2s[host_session] = true; |
69982753fe4b
core.s2smanager: Rename variable to be same in two functions
Kim Alvefur <zash@zash.se>
parents:
9787
diff
changeset
|
37 return host_session; |
148 | 38 end |
39 | |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
40 local function new_outgoing(from_host, to_host) |
9936
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
41 local host_session = sessionlib.new("s2sout"); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
42 sessionlib.set_id(host_session); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
43 sessionlib.set_logger(host_session); |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
44 host_session.to_host = to_host; |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
45 host_session.from_host = from_host; |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
46 host_session.host = from_host; |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
47 host_session.notopen = true; |
8695b50368cd
core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents:
9935
diff
changeset
|
48 host_session.direction = "outgoing"; |
10243
b36765ab0ae3
core.s2smanager: Add [direction] boolean flags to s2s connections
Kim Alvefur <zash@zash.se>
parents:
10103
diff
changeset
|
49 host_session.outgoing = true; |
10244
483cc47f15f8
core.s2smanager: Add map of names authenticate for remote on s2sout for parity with s2sin
Kim Alvefur <zash@zash.se>
parents:
10243
diff
changeset
|
50 host_session.hosts = {}; |
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
|
51 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
|
52 return host_session; |
148 | 53 end |
54 | |
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
|
55 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
|
56 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
|
57 type = "s2s_destroyed"; |
2748
85a242cd1bc4
s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents:
2747
diff
changeset
|
58 open_stream = function (session) |
85a242cd1bc4
s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents:
2747
diff
changeset
|
59 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
|
60 end; |
85a242cd1bc4
s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents:
2747
diff
changeset
|
61 close = function (session) |
85a242cd1bc4
s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents:
2747
diff
changeset
|
62 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
|
63 end; |
9787
6625efab91e2
core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents:
8675
diff
changeset
|
64 reset_stream = function (session) |
6625efab91e2
core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents:
8675
diff
changeset
|
65 session.log("debug", "Attempt to reset stream of already-closed session"); |
6625efab91e2
core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents:
8675
diff
changeset
|
66 end; |
6663
d3023dd07cb6
portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
67 filter = function (type, data) return data; end; --luacheck: ignore 212/type |
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
|
68 }; 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
|
69 |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
70 local function retire_session(session, reason) |
6663
d3023dd07cb6
portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents:
5776
diff
changeset
|
71 local log = session.log or log; --luacheck: ignore 431/log |
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
|
72 for k in pairs(session) do |
5447
92b88476873a
sessionmanager, s2smanager: Remove open_session tracing
Matthew Wild <mwild1@gmail.com>
parents:
5367
diff
changeset
|
73 if 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
|
74 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
|
75 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
|
76 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
|
77 |
4018
5061c8d41d89
s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents:
4017
diff
changeset
|
78 session.destruction_reason = reason; |
5061c8d41d89
s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents:
4017
diff
changeset
|
79 |
10102
77a21eef243b
core.s2smanager: Remove use of tostring in logging
Kim Alvefur <zash@zash.se>
parents:
9938
diff
changeset
|
80 function session.send(data) log("debug", "Discarding data sent to resting session: %s", data); end |
77a21eef243b
core.s2smanager: Remove use of tostring in logging
Kim Alvefur <zash@zash.se>
parents:
9938
diff
changeset
|
81 function session.data(data) log("debug", "Discarding data received from resting session: %s", data); end |
7452
d916703d5e18
s2smanager: Include a stub thread on destroyed sessions (thanks Link Mauve)
Kim Alvefur <zash@zash.se>
parents:
6779
diff
changeset
|
82 session.thread = { run = function (_, data) return session.data(data) end }; |
6691
c6c996410064
s2smanager: Make sure destroyed sessions have a sends2s method
Kim Alvefur <zash@zash.se>
parents:
5459
diff
changeset
|
83 session.sends2s = session.send; |
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
|
84 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
|
85 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
|
86 |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
87 local function destroy_session(session, reason) |
2749
8dc5f3651501
s2smanager: Don't re-destroy destroyed sessions
Matthew Wild <mwild1@gmail.com>
parents:
2748
diff
changeset
|
88 if session.destroyed then return; end |
10103
18cab054364a
core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents:
10102
diff
changeset
|
89 local log = session.log or log; |
18cab054364a
core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents:
10102
diff
changeset
|
90 log("debug", "Destroying %s session %s->%s%s%s", session.direction, session.from_host, session.to_host, reason and ": " or "", reason or ""); |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5459
diff
changeset
|
91 |
164
8dc1faa5b1df
other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 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
|
96 incoming_s2s[session] = nil; |
164
8dc1faa5b1df
other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
97 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5459
diff
changeset
|
98 |
3488
4f3fc5f9d944
s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents:
3476
diff
changeset
|
99 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
|
100 if session.type == "s2sout" then |
5349
0d11e393201f
s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents:
5306
diff
changeset
|
101 fire_event("s2sout-destroyed", event_data); |
3488
4f3fc5f9d944
s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents:
3476
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 elseif session.type == "s2sin" then |
5349
0d11e393201f
s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents:
5306
diff
changeset
|
106 fire_event("s2sin-destroyed", event_data); |
3488
4f3fc5f9d944
s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents:
3476
diff
changeset
|
107 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
|
108 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
|
109 end |
4f3fc5f9d944
s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents:
3476
diff
changeset
|
110 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5459
diff
changeset
|
111 |
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
|
112 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
|
113 return true; |
164
8dc1faa5b1df
other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
114 end |
8dc1faa5b1df
other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
115 |
6779
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
116 return { |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
117 incoming_s2s = incoming_s2s; |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
118 new_incoming = new_incoming; |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
119 new_outgoing = new_outgoing; |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
120 retire_session = retire_session; |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
121 destroy_session = destroy_session; |
6236668da30a
core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents:
6692
diff
changeset
|
122 }; |