aboutsummaryrefslogtreecommitdiff
path: root/src/mod_ping.erl
blob: 825acf2cd8ce88781214658895d21dff992b19ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
%%%----------------------------------------------------------------------
%%% File    : mod_ping.erl
%%% Author  : Brian Cully <bjc@kublai.com>
%%% Purpose : Support XEP-0199 XMPP Ping and periodic keepalives
%%% Created : 11 Jul 2009 by Brian Cully <bjc@kublai.com>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2022   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------

-module(mod_ping).

-author('bjc@kublai.com').

-protocol({xep, 199, '2.0'}).

-behaviour(gen_mod).

-behaviour(gen_server).

-include("logger.hrl").

-include_lib("xmpp/include/xmpp.hrl").

-include("translate.hrl").

%% API
-export([start_ping/2, stop_ping/2]).

%% gen_mod callbacks
-export([start/2, stop/1, reload/3]).

%% gen_server callbacks
-export([init/1, terminate/2, handle_call/3,
	 handle_cast/2, handle_info/2, code_change/3]).

-export([iq_ping/1, user_online/3, user_offline/3, mod_doc/0,
	 user_send/1, mod_opt_type/1, mod_options/1, depends/2]).

-record(state,
	{host                :: binary(),
         send_pings          :: boolean(),
	 ping_interval       :: pos_integer(),
	 ping_ack_timeout    :: undefined | non_neg_integer(),
	 timeout_action      :: none | kill,
         timers              :: timers()}).

-type timers() :: #{ljid() => reference()}.

%%====================================================================
%% API
%%====================================================================
-spec start_ping(binary(), jid()) -> ok.
start_ping(Host, JID) ->
    Proc = gen_mod:get_module_proc(Host, ?MODULE),
    gen_server:cast(Proc, {start_ping, JID}).

-spec stop_ping(binary(), jid()) -> ok.
stop_ping(Host, JID) ->
    Proc = gen_mod:get_module_proc(Host, ?MODULE),
    gen_server:cast(Proc, {stop_ping, JID}).

%%====================================================================
%% gen_mod callbacks
%%====================================================================
start(Host, Opts) ->
    gen_mod:start_child(?MODULE, Host, Opts).

stop(Host) ->
    gen_mod:stop_child(?MODULE, Host).

reload(Host, NewOpts, OldOpts) ->
    Proc = gen_mod:get_module_proc(Host, ?MODULE),
    gen_server:cast(Proc, {reload, Host, NewOpts, OldOpts}).

%%====================================================================
%% gen_server callbacks
%%====================================================================
init([Host|_]) ->
    process_flag(trap_exit, true),
    Opts = gen_mod:get_module_opts(Host, ?MODULE),
    State = init_state(Host, Opts),
    register_iq_handlers(Host),
    case State#state.send_pings of
	true -> register_hooks(Host);
	false -> ok
    end,
    {ok, State}.

terminate(_Reason, #state{host = Host}) ->
    unregister_hooks(Host),
    unregister_iq_handlers(Host).

handle_call(stop, _From, State) ->
    {stop, normal, ok, State};
handle_call(Request, From, State) ->
    ?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
    {noreply, State}.

handle_cast({reload, Host, NewOpts, _OldOpts},
	    #state{timers = Timers} = OldState) ->
    NewState = init_state(Host, NewOpts),
    case {NewState#state.send_pings, OldState#state.send_pings} of
	{true, false} -> register_hooks(Host);
	{false, true} -> unregister_hooks(Host);
	_ -> ok
    end,
    {noreply, NewState#state{timers = Timers}};
handle_cast({start_ping, JID}, State) ->
    Timers = add_timer(JID, State#state.ping_interval,
		       State#state.timers),
    {noreply, State#state{timers = Timers}};
handle_cast({stop_ping, JID}, State) ->
    Timers = del_timer(JID, State#state.timers),
    {noreply, State#state{timers = Timers}};
handle_cast(Msg, State) ->
    ?WARNING_MSG("Unexpected cast: ~p", [Msg]),
    {noreply, State}.

handle_info({iq_reply, #iq{type = error} = IQ, JID}, State) ->
    Timers = case xmpp:get_error(IQ) of
		 #stanza_error{type=cancel, reason='service-unavailable'} ->
		     del_timer(JID, State#state.timers);
		 _ ->
		     State#state.timers
	     end,
    {noreply, State#state{timers = Timers}};
handle_info({iq_reply, #iq{}, _JID}, State) ->
    {noreply, State};
handle_info({iq_reply, timeout, JID}, State) ->
    ejabberd_hooks:run(user_ping_timeout, State#state.host,
		       [JID]),
    Timers = case State#state.timeout_action of
		 kill ->
		     #jid{user = User, server = Server,
			  resource = Resource} =
			 JID,
		     case ejabberd_sm:get_session_pid(User, Server, Resource) of
			 Pid when is_pid(Pid) ->
			     ejabberd_c2s:close(Pid, ping_timeout);
			 _ ->
			     ok
		     end,
		     del_timer(JID, State#state.timers);
		 _ ->
		     State#state.timers
	     end,
    {noreply, State#state{timers = Timers}};
handle_info({timeout, _TRef, {ping, JID}}, State) ->
    Timers = case ejabberd_sm:get_session_pid(JID#jid.luser,
					      JID#jid.lserver,
					      JID#jid.lresource) of
		 none ->
		     del_timer(JID, State#state.timers);
		 _ ->
		     Host = State#state.host,
		     From = jid:make(Host),
		     IQ = #iq{from = From, to = JID, type = get, sub_els = [#ping{}]},
		     ejabberd_router:route_iq(IQ, JID,
					      gen_mod:get_module_proc(Host, ?MODULE),
					      State#state.ping_ack_timeout),
		     add_timer(JID, State#state.ping_interval,
			       State#state.timers)
	     end,
    {noreply, State#state{timers = Timers}};
handle_info(Info, State) ->
    ?WARNING_MSG("Unexpected info: ~p", [Info]),
    {noreply, State}.

code_change(_OldVsn, State, _Extra) -> {ok, State}.

%%====================================================================
%% Hook callbacks
%%====================================================================
-spec iq_ping(iq()) -> iq().
iq_ping(#iq{type = get, sub_els = [#ping{}]} = IQ) ->
    xmpp:make_iq_result(IQ);
iq_ping(#iq{lang = Lang} = IQ) ->
    Txt = ?T("Ping query is incorrect"),
    xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang)).

-spec user_online(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> ok.
user_online(_SID, JID, _Info) ->
    start_ping(JID#jid.lserver, JID).

-spec user_offline(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> ok.
user_offline(_SID, JID, _Info) ->
    case ejabberd_sm:get_session_pid(JID#jid.luser,
                                     JID#jid.lserver,
                                     JID#jid.lresource) of
        PID when PID =:= none; node(PID) /= node() ->
            stop_ping(JID#jid.lserver, JID);
        _ ->
            ok
    end.

-spec user_send({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
user_send({Packet, #{jid := JID} = C2SState}) ->
    start_ping(JID#jid.lserver, JID),
    {Packet, C2SState}.

%%====================================================================
%% Internal functions
%%====================================================================
init_state(Host, Opts) ->
    SendPings = mod_ping_opt:send_pings(Opts),
    PingInterval = mod_ping_opt:ping_interval(Opts),
    PingAckTimeout = mod_ping_opt:ping_ack_timeout(Opts),
    TimeoutAction = mod_ping_opt:timeout_action(Opts),
    #state{host = Host,
	   send_pings = SendPings,
	   ping_interval = PingInterval,
	   timeout_action = TimeoutAction,
	   ping_ack_timeout = PingAckTimeout,
	   timers = #{}}.

register_hooks(Host) ->
    ejabberd_hooks:add(sm_register_connection_hook, Host,
		       ?MODULE, user_online, 100),
    ejabberd_hooks:add(sm_remove_connection_hook, Host,
		       ?MODULE, user_offline, 100),
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
		       user_send, 100).

unregister_hooks(Host) ->
    ejabberd_hooks:delete(sm_remove_connection_hook, Host,
			  ?MODULE, user_offline, 100),
    ejabberd_hooks:delete(sm_register_connection_hook, Host,
			  ?MODULE, user_online, 100),
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
			  user_send, 100).

register_iq_handlers(Host) ->
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PING,
				  ?MODULE, iq_ping),
    gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_PING,
				  ?MODULE, iq_ping).

unregister_iq_handlers(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_PING),
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PING).

-spec add_timer(jid(), pos_integer(), timers()) -> timers().
add_timer(JID, Interval, Timers) ->
    LJID = jid:tolower(JID),
    NewTimers = case maps:find(LJID, Timers) of
		    {ok, OldTRef} ->
			misc:cancel_timer(OldTRef),
			maps:remove(LJID, Timers);
		    _ -> Timers
		end,
    TRef = erlang:start_timer(Interval, self(), {ping, JID}),
    maps:put(LJID, TRef, NewTimers).

-spec del_timer(jid(), timers()) -> timers().
del_timer(JID, Timers) ->
    LJID = jid:tolower(JID),
    case maps:find(LJID, Timers) of
	{ok, TRef} ->
	    misc:cancel_timer(TRef),
	    maps:remove(LJID, Timers);
	_ -> Timers
    end.

depends(_Host, _Opts) ->
    [].

mod_opt_type(ping_interval) ->
    econf:timeout(second);
mod_opt_type(ping_ack_timeout) ->
    econf:timeout(second);
mod_opt_type(send_pings) ->
    econf:bool();
mod_opt_type(timeout_action) ->
    econf:enum([none, kill]).

mod_options(_Host) ->
    [{ping_interval, timer:minutes(1)},
     {ping_ack_timeout, undefined},
     {send_pings, false},
     {timeout_action, none}].

mod_doc() ->
    #{desc =>
          ?T("This module implements support for "
             "https://xmpp.org/extensions/xep-0199.html"
             "[XEP-0199: XMPP Ping] and periodic keepalives. "
             "When this module is enabled ejabberd responds "
             "correctly to ping requests, as defined by the protocol."),
      opts =>
          [{ping_interval,
            #{value => "timeout()",
              desc =>
                  ?T("How often to send pings to connected clients, "
                     "if option 'send_pings' is set to 'true'. If a client "
                     "connection does not send or receive any stanza "
                     "within this interval, a ping request is sent to "
                     "the client. The default value is '1' minute.")}},
           {ping_ack_timeout,
            #{value => "timeout()",
              desc =>
                  ?T("How long to wait before deeming that a client "
                     "has not answered a given server ping request. "
                     "The default value is 'undefined'.")}},
           {send_pings,
            #{value => "true | false",
              desc =>
                  ?T("If this option is set to 'true', the server "
                     "sends pings to connected clients that are not "
                     "active in a given interval defined in 'ping_interval' "
                     "option. This is useful to keep client connections "
                     "alive or checking availability. "
                     "The default value is 'false'.")}},
           {timeout_action,
            #{value => "none | kill",
              desc =>
                  ?T("What to do when a client does not answer to a "
                     "server ping request in less than period defined "
                     "in 'ping_ack_timeout' option: "
                     "'kill' means destroying the underlying connection, "
                     "'none' means to do nothing. NOTE: when _`mod_stream_mgmt`_ "
                     "is loaded and stream management is enabled by "
                     "a client, killing the client connection doesn't mean "
                     "killing the client session - the session will be kept "
                     "alive in order to give the client a chance to resume it. "
                     "The default value is 'none'.")}}],
      example =>
          ["modules:",
           "  ...",
           "  mod_ping:",
           "    send_pings: true",
           "    ping_interval: 4 min",
           "    timeout_action: kill",
           "  ..."]}.