2009-07-30 13:45:54 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% 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>
|
2009-07-30 10:58:21 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2009-07-30 10:58:21 +02:00
|
|
|
%%%
|
2009-07-30 13:45:54 +02:00
|
|
|
%%% 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.
|
2009-07-30 10:58:21 +02:00
|
|
|
%%%
|
2009-07-30 13:45:54 +02:00
|
|
|
%%% 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.
|
2009-07-30 10:58:21 +02:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2009-07-30 13:45:54 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
-module(mod_ping).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
-author('bjc@kublai.com').
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 199, '2.0'}).
|
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
-behavior(gen_mod).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
-behavior(gen_server).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
|
|
|
-define(SUPERVISOR, ejabberd_sup).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-define(DEFAULT_SEND_PINGS, false).
|
|
|
|
|
|
|
|
-define(DEFAULT_PING_INTERVAL, 60).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
-define(DICT, dict).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/2, start_ping/2, stop_ping/2]).
|
|
|
|
|
|
|
|
%% gen_mod callbacks
|
|
|
|
-export([start/2, stop/1]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, terminate/2, handle_call/3,
|
|
|
|
handle_cast/2, handle_info/2, code_change/3]).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([iq_ping/3, user_online/3, user_offline/3,
|
2015-06-22 15:56:08 +02:00
|
|
|
user_send/4, mod_opt_type/1]).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{host = <<"">>,
|
|
|
|
send_pings = ?DEFAULT_SEND_PINGS :: boolean(),
|
|
|
|
ping_interval = ?DEFAULT_PING_INTERVAL :: non_neg_integer(),
|
2015-10-22 14:48:40 +02:00
|
|
|
ping_ack_timeout = undefined :: non_neg_integer(),
|
2013-03-14 10:33:02 +01:00
|
|
|
timeout_action = none :: none | kill,
|
2015-02-11 12:08:16 +01:00
|
|
|
timers = (?DICT):new() :: ?TDICT}).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
start_ping(Host, JID) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {start_ping, JID}).
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
PingSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
|
2013-03-14 10:33:02 +01:00
|
|
|
transient, 2000, worker, [?MODULE]},
|
2009-07-30 10:58:21 +02:00
|
|
|
supervisor:start_child(?SUPERVISOR, PingSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?MODULE),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:delete_child(?SUPERVISOR, Proc).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
init([Host, Opts]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
SendPings = gen_mod:get_opt(send_pings, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
?DEFAULT_SEND_PINGS),
|
|
|
|
PingInterval = gen_mod:get_opt(ping_interval, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
?DEFAULT_PING_INTERVAL),
|
2015-10-22 14:48:40 +02:00
|
|
|
PingAckTimeout = gen_mod:get_opt(ping_ack_timeout, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I * 1000 end,
|
|
|
|
undefined),
|
2013-03-14 10:33:02 +01:00
|
|
|
TimeoutAction = gen_mod:get_opt(timeout_action, Opts,
|
|
|
|
fun(none) -> none;
|
|
|
|
(kill) -> kill
|
|
|
|
end, none),
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
no_queue),
|
2009-07-30 10:58:21 +02:00
|
|
|
mod_disco:register_feature(Host, ?NS_PING),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_PING, ?MODULE, iq_ping, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_PING, ?MODULE, iq_ping, IQDisc),
|
2009-07-30 10:58:21 +02:00
|
|
|
case SendPings of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
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);
|
|
|
|
_ -> ok
|
2009-07-30 10:58:21 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok,
|
|
|
|
#state{host = Host, send_pings = SendPings,
|
|
|
|
ping_interval = PingInterval,
|
|
|
|
timeout_action = TimeoutAction,
|
2015-10-22 14:48:40 +02:00
|
|
|
ping_ack_timeout = PingAckTimeout,
|
2013-03-14 10:33:02 +01:00
|
|
|
timers = (?DICT):new()}}.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
terminate(_Reason, #state{host = 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),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send, 100),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_PING),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_PING),
|
2009-07-30 10:58:21 +02:00
|
|
|
mod_disco:unregister_feature(Host, ?NS_PING).
|
|
|
|
|
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
{stop, normal, ok, State};
|
|
|
|
handle_call(_Req, _From, State) ->
|
|
|
|
{reply, {error, badarg}, State}.
|
|
|
|
|
|
|
|
handle_cast({start_ping, JID}, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Timers = add_timer(JID, State#state.ping_interval,
|
|
|
|
State#state.timers),
|
2009-07-30 10:58:21 +02:00
|
|
|
{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({iq_pong, JID, timeout}, State) ->
|
|
|
|
Timers = del_timer(JID, State#state.timers),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run(user_ping_timeout, State#state.host,
|
|
|
|
[JID]),
|
2009-07-30 12:25:54 +02:00
|
|
|
case State#state.timeout_action of
|
2013-03-14 10:33:02 +01:00
|
|
|
kill ->
|
|
|
|
#jid{user = User, server = Server,
|
|
|
|
resource = Resource} =
|
|
|
|
JID,
|
|
|
|
case ejabberd_sm:get_session_pid(User, Server, Resource)
|
|
|
|
of
|
2015-10-17 23:15:31 +02:00
|
|
|
Pid when is_pid(Pid) -> ejabberd_c2s:close(Pid);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ -> ok
|
2009-07-30 12:25:54 +02:00
|
|
|
end,
|
2009-07-30 10:58:21 +02:00
|
|
|
{noreply, State#state{timers = Timers}};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
handle_info({timeout, _TRef, {ping, JID}}, State) ->
|
|
|
|
IQ = #iq{type = get,
|
2013-03-14 10:33:02 +01:00
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"ping">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_PING}], children = []}]},
|
2009-07-30 10:58:21 +02:00
|
|
|
Pid = self(),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun (Response) ->
|
2009-07-30 10:58:21 +02:00
|
|
|
gen_server:cast(Pid, {iq_pong, JID, Response})
|
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
From = jid:make(<<"">>, State#state.host, <<"">>),
|
2015-10-22 14:48:40 +02:00
|
|
|
ejabberd_local:route_iq(From, JID, IQ, F, State#state.ping_ack_timeout),
|
2013-03-14 10:33:02 +01:00
|
|
|
Timers = add_timer(JID, State#state.ping_interval,
|
|
|
|
State#state.timers),
|
2009-07-30 10:58:21 +02:00
|
|
|
{noreply, State#state{timers = Timers}};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% Hook callbacks
|
|
|
|
%%====================================================================
|
2013-03-14 10:33:02 +01:00
|
|
|
iq_ping(_From, _To,
|
|
|
|
#iq{type = Type, sub_el = SubEl} = IQ) ->
|
2009-07-30 10:58:21 +02:00
|
|
|
case {Type, SubEl} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{get, #xmlel{name = <<"ping">>}} ->
|
|
|
|
IQ#iq{type = result, sub_el = []};
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_FEATURE_NOT_IMPLEMENTED]}
|
2009-07-30 10:58:21 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
user_online(_SID, JID, _Info) ->
|
|
|
|
start_ping(JID#jid.lserver, JID).
|
|
|
|
|
|
|
|
user_offline(_SID, JID, _Info) ->
|
|
|
|
stop_ping(JID#jid.lserver, JID).
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
user_send(Packet, _C2SState, JID, _From) ->
|
|
|
|
start_ping(JID#jid.lserver, JID),
|
|
|
|
Packet.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%%====================================================================
|
|
|
|
add_timer(JID, Interval, Timers) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewTimers = case (?DICT):find(LJID, Timers) of
|
|
|
|
{ok, OldTRef} ->
|
|
|
|
cancel_timer(OldTRef), (?DICT):erase(LJID, Timers);
|
|
|
|
_ -> Timers
|
2009-07-30 10:58:21 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
TRef = erlang:start_timer(Interval * 1000, self(),
|
|
|
|
{ping, JID}),
|
|
|
|
(?DICT):store(LJID, TRef, NewTimers).
|
2009-07-30 10:58:21 +02:00
|
|
|
|
|
|
|
del_timer(JID, Timers) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
case (?DICT):find(LJID, Timers) of
|
|
|
|
{ok, TRef} ->
|
|
|
|
cancel_timer(TRef), (?DICT):erase(LJID, Timers);
|
|
|
|
_ -> Timers
|
2009-07-30 10:58:21 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
cancel_timer(TRef) ->
|
|
|
|
case erlang:cancel_timer(TRef) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
receive {timeout, TRef, _} -> ok after 0 -> ok end;
|
|
|
|
_ -> ok
|
2009-07-30 10:58:21 +02:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(ping_interval) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2015-11-18 13:25:01 +01:00
|
|
|
mod_opt_type(ping_ack_timeout) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(send_pings) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(timeout_action) ->
|
|
|
|
fun (none) -> none;
|
|
|
|
(kill) -> kill
|
|
|
|
end;
|
|
|
|
mod_opt_type(_) ->
|
2015-11-18 13:25:01 +01:00
|
|
|
[iqdisc, ping_interval, ping_ack_timeout, send_pings, timeout_action].
|