• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 1258

12 Dec 2025 03:57PM UTC coverage: 33.638% (-0.006%) from 33.644%
1258

push

github

badlop
Container: Apply commit a22c88a

ejabberdctl.template: Show meaningful error when ERL_DIST_PORT is in use

15554 of 46240 relevant lines covered (33.64%)

1078.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.3
/src/mod_ping.erl
1
%%%----------------------------------------------------------------------
2
%%% File    : mod_ping.erl
3
%%% Author  : Brian Cully <bjc@kublai.com>
4
%%% Purpose : Support XEP-0199 XMPP Ping and periodic keepalives
5
%%% Created : 11 Jul 2009 by Brian Cully <bjc@kublai.com>
6
%%%
7
%%%
8
%%% ejabberd, Copyright (C) 2002-2025   ProcessOne
9
%%%
10
%%% This program is free software; you can redistribute it and/or
11
%%% modify it under the terms of the GNU General Public License as
12
%%% published by the Free Software Foundation; either version 2 of the
13
%%% License, or (at your option) any later version.
14
%%%
15
%%% This program is distributed in the hope that it will be useful,
16
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
%%% General Public License for more details.
19
%%%
20
%%% You should have received a copy of the GNU General Public License along
21
%%% with this program; if not, write to the Free Software Foundation, Inc.,
22
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
%%%
24
%%%----------------------------------------------------------------------
25

26
-module(mod_ping).
27

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

30
-protocol({xep, 199, '2.0.1', '2.1.0', "complete", ""}).
31

32
-behaviour(gen_mod).
33

34
-behaviour(gen_server).
35

36
-include("logger.hrl").
37

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

40
-include("translate.hrl").
41

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

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

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

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

55
-record(state,
56
        {host                :: binary(),
57
         send_pings          :: boolean(),
58
         ping_interval       :: pos_integer(),
59
         timeout_action      :: none | kill,
60
         timers              :: timers()}).
61

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

64
%%====================================================================
65
%% API
66
%%====================================================================
67
-spec start_ping(binary(), jid()) -> ok.
68
start_ping(Host, JID) ->
69
    Proc = gen_mod:get_module_proc(Host, ?MODULE),
×
70
    gen_server:cast(Proc, {start_ping, JID}).
×
71

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

77
%%====================================================================
78
%% gen_mod callbacks
79
%%====================================================================
80
start(Host, Opts) ->
81
    gen_mod:start_child(?MODULE, Host, Opts).
99✔
82

83
stop(Host) ->
84
    gen_mod:stop_child(?MODULE, Host).
99✔
85

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

90
%%====================================================================
91
%% gen_server callbacks
92
%%====================================================================
93
init([Host|_]) ->
94
    process_flag(trap_exit, true),
99✔
95
    Opts = gen_mod:get_module_opts(Host, ?MODULE),
99✔
96
    State = init_state(Host, Opts),
99✔
97
    register_iq_handlers(Host),
99✔
98
    case State#state.send_pings of
99✔
99
        true -> register_hooks(Host);
×
100
        false -> ok
99✔
101
    end,
102
    {ok, State}.
99✔
103

104
terminate(_Reason, #state{host = Host}) ->
105
    unregister_hooks(Host),
99✔
106
    unregister_iq_handlers(Host).
99✔
107

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

114
handle_cast({reload, Host, NewOpts, _OldOpts},
115
            #state{timers = Timers} = OldState) ->
116
    NewState = init_state(Host, NewOpts),
×
117
    case {NewState#state.send_pings, OldState#state.send_pings} of
×
118
        {true, false} -> register_hooks(Host);
×
119
        {false, true} -> unregister_hooks(Host);
×
120
        _ -> ok
×
121
    end,
122
    {noreply, NewState#state{timers = Timers}};
×
123
handle_cast({start_ping, JID}, State) ->
124
    Timers = add_timer(JID, State#state.ping_interval,
×
125
                       State#state.timers),
126
    {noreply, State#state{timers = Timers}};
×
127
handle_cast({stop_ping, JID}, State) ->
128
    Timers = del_timer(JID, State#state.timers),
×
129
    {noreply, State#state{timers = Timers}};
×
130
handle_cast(Msg, State) ->
131
    ?WARNING_MSG("Unexpected cast: ~p", [Msg]),
×
132
    {noreply, State}.
×
133

134
handle_info({iq_reply, #iq{type = error} = IQ, JID}, State) ->
135
    Timers = case xmpp:get_error(IQ) of
×
136
                 #stanza_error{type=cancel, reason='service-unavailable'} ->
137
                     del_timer(JID, State#state.timers);
×
138
                 _ ->
139
                     State#state.timers
×
140
             end,
141
    {noreply, State#state{timers = Timers}};
×
142
handle_info({iq_reply, #iq{}, _JID}, State) ->
143
    {noreply, State};
×
144
handle_info({iq_reply, timeout, JID}, State) ->
145
    ejabberd_hooks:run(user_ping_timeout, State#state.host,
×
146
                       [JID]),
147
    Timers = case State#state.timeout_action of
×
148
                 kill ->
149
                     #jid{user = User, server = Server,
×
150
                          resource = Resource} =
151
                         JID,
152
                     case ejabberd_sm:get_session_pid(User, Server, Resource) of
×
153
                         Pid when is_pid(Pid) ->
154
                             ejabberd_c2s:close(Pid, ping_timeout);
×
155
                         _ ->
156
                             ok
×
157
                     end,
158
                     del_timer(JID, State#state.timers);
×
159
                 _ ->
160
                     State#state.timers
×
161
             end,
162
    {noreply, State#state{timers = Timers}};
×
163
handle_info({timeout, _TRef, {ping, JID}}, State) ->
164
    Timers = case ejabberd_sm:get_session_pid(JID#jid.luser,
×
165
                                              JID#jid.lserver,
166
                                              JID#jid.lresource) of
167
                 none ->
168
                     del_timer(JID, State#state.timers);
×
169
                 Pid ->
170
                     ejabberd_c2s:cast(Pid, send_ping),
×
171
                     add_timer(JID, State#state.ping_interval,
×
172
                               State#state.timers)
173
             end,
174
    {noreply, State#state{timers = Timers}};
×
175
handle_info(Info, State) ->
176
    ?WARNING_MSG("Unexpected info: ~p", [Info]),
×
177
    {noreply, State}.
×
178

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

181
%%====================================================================
182
%% Hook callbacks
183
%%====================================================================
184
-spec iq_ping(iq()) -> iq().
185
iq_ping(#iq{type = get, sub_els = [#ping{}]} = IQ) ->
186
    xmpp:make_iq_result(IQ);
9✔
187
iq_ping(#iq{lang = Lang} = IQ) ->
188
    Txt = ?T("Ping query is incorrect"),
×
189
    xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang)).
×
190

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

195
-spec user_offline(ejabberd_sm:sid(), jid(), ejabberd_sm:info()) -> ok.
196
user_offline(_SID, JID, _Info) ->
197
    case ejabberd_sm:get_session_pid(JID#jid.luser,
×
198
                                     JID#jid.lserver,
199
                                     JID#jid.lresource) of
200
        PID when PID =:= none; node(PID) /= node() ->
201
            stop_ping(JID#jid.lserver, JID);
×
202
        _ ->
203
            ok
×
204
    end.
205

206
-spec user_send({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
207
user_send({Packet, #{jid := JID} = C2SState}) ->
208
    start_ping(JID#jid.lserver, JID),
×
209
    {Packet, C2SState}.
×
210

211
-spec c2s_handle_cast(ejabberd_c2s:state(), send_ping | term())
212
      -> ejabberd_c2s:state() | {stop, ejabberd_c2s:state()}.
213
c2s_handle_cast(#{lserver := Host, jid := JID} = C2SState, send_ping) ->
214
    From = jid:make(Host),
×
215
    IQ = #iq{from = From, to = JID, type = get, sub_els = [#ping{}]},
×
216
    Proc = gen_mod:get_module_proc(Host, ?MODULE),
×
217
    PingAckTimeout = mod_ping_opt:ping_ack_timeout(Host),
×
218
    ejabberd_router:route_iq(IQ, JID, Proc, PingAckTimeout),
×
219
    {stop, C2SState};
×
220
c2s_handle_cast(C2SState, _Msg) ->
221
    C2SState.
×
222

223
%%====================================================================
224
%% Internal functions
225
%%====================================================================
226
init_state(Host, Opts) ->
227
    SendPings = mod_ping_opt:send_pings(Opts),
99✔
228
    PingInterval = mod_ping_opt:ping_interval(Opts),
99✔
229
    TimeoutAction = mod_ping_opt:timeout_action(Opts),
99✔
230
    #state{host = Host,
99✔
231
           send_pings = SendPings,
232
           ping_interval = PingInterval,
233
           timeout_action = TimeoutAction,
234
           timers = #{}}.
235

236
register_hooks(Host) ->
237
    ejabberd_hooks:add(sm_register_connection_hook, Host,
×
238
                       ?MODULE, user_online, 100),
239
    ejabberd_hooks:add(sm_remove_connection_hook, Host,
×
240
                       ?MODULE, user_offline, 100),
241
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
×
242
                       user_send, 100),
243
    ejabberd_hooks:add(c2s_handle_cast, Host, ?MODULE,
×
244
                       c2s_handle_cast, 99).
245

246
unregister_hooks(Host) ->
247
    ejabberd_hooks:delete(sm_remove_connection_hook, Host,
99✔
248
                          ?MODULE, user_offline, 100),
249
    ejabberd_hooks:delete(sm_register_connection_hook, Host,
99✔
250
                          ?MODULE, user_online, 100),
251
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
99✔
252
                          user_send, 100),
253
    ejabberd_hooks:delete(c2s_handle_cast, Host, ?MODULE,
99✔
254
                          c2s_handle_cast, 99).
255

256
register_iq_handlers(Host) ->
257
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_PING,
99✔
258
                                  ?MODULE, iq_ping),
259
    gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_PING,
99✔
260
                                  ?MODULE, iq_ping).
261

262
unregister_iq_handlers(Host) ->
263
    gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_PING),
99✔
264
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PING).
99✔
265

266
-spec add_timer(jid(), pos_integer(), timers()) -> timers().
267
add_timer(JID, Interval, Timers) ->
268
    LJID = jid:tolower(JID),
×
269
    NewTimers = case maps:find(LJID, Timers) of
×
270
                    {ok, OldTRef} ->
271
                        misc:cancel_timer(OldTRef),
×
272
                        maps:remove(LJID, Timers);
×
273
                    _ -> Timers
×
274
                end,
275
    TRef = erlang:start_timer(Interval, self(), {ping, JID}),
×
276
    maps:put(LJID, TRef, NewTimers).
×
277

278
-spec del_timer(jid(), timers()) -> timers().
279
del_timer(JID, Timers) ->
280
    LJID = jid:tolower(JID),
×
281
    case maps:find(LJID, Timers) of
×
282
        {ok, TRef} ->
283
            misc:cancel_timer(TRef),
×
284
            maps:remove(LJID, Timers);
×
285
        _ -> Timers
×
286
    end.
287

288
depends(_Host, _Opts) ->
289
    [].
117✔
290

291
mod_opt_type(ping_interval) ->
292
    econf:timeout(second);
117✔
293
mod_opt_type(ping_ack_timeout) ->
294
    econf:timeout(second);
117✔
295
mod_opt_type(send_pings) ->
296
    econf:bool();
117✔
297
mod_opt_type(timeout_action) ->
298
    econf:enum([none, kill]).
117✔
299

300
mod_options(_Host) ->
301
    [{ping_interval, timer:minutes(1)},
117✔
302
     {ping_ack_timeout, undefined},
303
     {send_pings, false},
304
     {timeout_action, none}].
305

306
mod_doc() ->
307
    #{desc =>
×
308
          ?T("This module implements support for "
309
             "https://xmpp.org/extensions/xep-0199.html"
310
             "[XEP-0199: XMPP Ping] and periodic keepalives. "
311
             "When this module is enabled ejabberd responds "
312
             "correctly to ping requests, as defined by the protocol."),
313
      opts =>
314
          [{ping_interval,
315
            #{value => "timeout()",
316
              desc =>
317
                  ?T("How often to send pings to connected clients, "
318
                     "if option 'send_pings' is set to 'true'. If a client "
319
                     "connection does not send or receive any stanza "
320
                     "within this interval, a ping request is sent to "
321
                     "the client. The default value is '1' minute.")}},
322
           {ping_ack_timeout,
323
            #{value => "timeout()",
324
              desc =>
325
                  ?T("How long to wait before deeming that a client "
326
                     "has not answered a given server ping request. NOTE: when "
327
                     "_`mod_stream_mgmt`_ is loaded and stream management is "
328
                     "enabled by a client, this value is ignored, and the "
329
                     "`ack_timeout` applies instead. "
330
                     "The default value is 'undefined'.")}},
331
           {send_pings,
332
            #{value => "true | false",
333
              desc =>
334
                  ?T("If this option is set to 'true', the server "
335
                     "sends pings to connected clients that are not "
336
                     "active in a given interval defined in 'ping_interval' "
337
                     "option. This is useful to keep client connections "
338
                     "alive or checking availability. "
339
                     "The default value is 'false'.")}},
340
           {timeout_action,
341
            #{value => "none | kill",
342
              desc =>
343
                  ?T("What to do when a client does not answer to a "
344
                     "server ping request in less than period defined "
345
                     "in 'ping_ack_timeout' option: "
346
                     "'kill' means destroying the underlying connection, "
347
                     "'none' means to do nothing. NOTE: when _`mod_stream_mgmt`_ "
348
                     "is loaded and stream management is enabled by "
349
                     "a client, killing the client connection doesn't mean "
350
                     "killing the client session - the session will be kept "
351
                     "alive in order to give the client a chance to resume it. "
352
                     "The default value is 'none'.")}}],
353
      example =>
354
          ["modules:",
355
           "  mod_ping:",
356
           "    send_pings: true",
357
           "    ping_interval: 4 min",
358
           "    timeout_action: kill"]}.
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc