1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : mod_ping.erl |
3 |
|
%%% Author : Piotr Nosek <piotr.nosek@erlang-solutions.com> |
4 |
|
%%% Purpose : XEP-0199 XMPP Ping implementation |
5 |
|
%%% Created : 14 Nov 2019 by Piotr Nosek <piotr.nosek@erlang-solutions.com> |
6 |
|
%%%---------------------------------------------------------------------- |
7 |
|
|
8 |
|
-module(mod_ping). |
9 |
|
-author('piotr.nosek@erlang-solutions.com'). |
10 |
|
|
11 |
|
-behavior(gen_mod). |
12 |
|
-xep([{xep, 199}, {version, "2.0.1"}]). |
13 |
|
|
14 |
|
-include("jlib.hrl"). |
15 |
|
-include("mongoose_logger.hrl"). |
16 |
|
-include("mongoose_config_spec.hrl"). |
17 |
|
|
18 |
|
-define(DEFAULT_SEND_PINGS, false). % bool() |
19 |
|
-define(DEFAULT_PING_INTERVAL, (60*1000)). % 60 seconds |
20 |
|
-define(DEFAULT_PING_REQ_TIMEOUT, (32*1000)).% 32 seconds |
21 |
|
|
22 |
|
%% gen_mod callbacks |
23 |
|
-export([start/2, |
24 |
|
stop/1, |
25 |
|
config_spec/0, |
26 |
|
supported_features/0]). |
27 |
|
|
28 |
|
%% Hook callbacks |
29 |
|
-export([user_send_packet/3, |
30 |
|
user_send_iq/3, |
31 |
|
user_ping_response/3, |
32 |
|
iq_ping/5]). |
33 |
|
|
34 |
|
%% Record that will be stored in the c2s state when the server pings the client, |
35 |
|
%% in order to indentify the possible client's answer. |
36 |
|
-record(ping_handler, {id :: binary(), time :: integer()}). |
37 |
|
|
38 |
|
%%==================================================================== |
39 |
|
%% Info Handler |
40 |
|
%%==================================================================== |
41 |
|
|
42 |
|
hooks(HostType) -> |
43 |
:-( |
[{user_ping_response, HostType, fun ?MODULE:user_ping_response/3, #{}, 100} |
44 |
|
| c2s_hooks(HostType)]. |
45 |
|
|
46 |
|
-spec c2s_hooks(mongooseim:host_type()) -> gen_hook:hook_list(mongoose_c2s_hooks:fn()). |
47 |
|
c2s_hooks(HostType) -> |
48 |
:-( |
[ |
49 |
|
{user_send_packet, HostType, fun ?MODULE:user_send_packet/3, #{}, 100}, |
50 |
|
{user_send_iq, HostType, fun ?MODULE:user_send_iq/3, #{}, 100} |
51 |
|
]. |
52 |
|
|
53 |
|
ensure_metrics(HostType) -> |
54 |
:-( |
mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response], spiral), |
55 |
:-( |
mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response_timeout], spiral), |
56 |
:-( |
mongoose_metrics:ensure_metric(HostType, [mod_ping, ping_response_time], histogram). |
57 |
|
|
58 |
|
%%==================================================================== |
59 |
|
%% gen_mod callbacks |
60 |
|
%%==================================================================== |
61 |
|
|
62 |
|
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
63 |
|
start(HostType, #{send_pings := SendPings, iqdisc := IQDisc}) -> |
64 |
:-( |
ensure_metrics(HostType), |
65 |
:-( |
gen_iq_handler:add_iq_handler_for_domain( |
66 |
|
HostType, ?NS_PING, ejabberd_sm, fun ?MODULE:iq_ping/5, #{}, IQDisc), |
67 |
:-( |
gen_iq_handler:add_iq_handler_for_domain( |
68 |
|
HostType, ?NS_PING, ejabberd_local, fun ?MODULE:iq_ping/5, #{}, IQDisc), |
69 |
:-( |
maybe_add_hooks_handlers(HostType, SendPings). |
70 |
|
|
71 |
|
-spec maybe_add_hooks_handlers(mongooseim:host_type(), boolean()) -> ok. |
72 |
|
maybe_add_hooks_handlers(Host, true) -> |
73 |
:-( |
gen_hook:add_handlers(hooks(Host)); |
74 |
|
maybe_add_hooks_handlers(_, _) -> |
75 |
:-( |
ok. |
76 |
|
|
77 |
|
-spec stop(mongooseim:host_type()) -> ok. |
78 |
|
stop(HostType) -> |
79 |
|
%% a word of warning: timers are installed in c2s processes, so stopping mod_ping |
80 |
|
%% won't stop currently running timers. They'll run one more time, and then stop. |
81 |
:-( |
gen_hook:delete_handlers(hooks(HostType)), |
82 |
:-( |
gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_local), |
83 |
:-( |
gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_PING, ejabberd_sm), |
84 |
:-( |
ok. |
85 |
|
|
86 |
|
-spec config_spec() -> mongoose_config_spec:config_section(). |
87 |
|
config_spec() -> |
88 |
8 |
#section{ |
89 |
|
items = #{<<"send_pings">> => #option{type = boolean}, |
90 |
|
<<"ping_interval">> => #option{type = integer, |
91 |
|
validate = positive, |
92 |
|
process = fun timer:seconds/1}, |
93 |
|
<<"timeout_action">> => #option{type = atom, |
94 |
|
validate = {enum, [none, kill]}}, |
95 |
|
<<"ping_req_timeout">> => #option{type = integer, |
96 |
|
validate = positive, |
97 |
|
process = fun timer:seconds/1}, |
98 |
|
<<"iqdisc">> => mongoose_config_spec:iqdisc() |
99 |
|
}, |
100 |
|
defaults = #{<<"send_pings">> => ?DEFAULT_SEND_PINGS, |
101 |
|
<<"ping_interval">> => ?DEFAULT_PING_INTERVAL, |
102 |
|
<<"timeout_action">> => none, |
103 |
|
<<"ping_req_timeout">> => ?DEFAULT_PING_REQ_TIMEOUT, |
104 |
|
<<"iqdisc">> => no_queue |
105 |
|
} |
106 |
|
}. |
107 |
|
|
108 |
:-( |
supported_features() -> [dynamic_domains]. |
109 |
|
|
110 |
|
%%==================================================================== |
111 |
|
%% IQ handlers |
112 |
|
%%==================================================================== |
113 |
|
iq_ping(Acc, _From, _To, #iq{type = get, sub_el = #xmlel{name = <<"ping">>}} = IQ, _) -> |
114 |
:-( |
{Acc, IQ#iq{type = result, sub_el = []}}; |
115 |
|
iq_ping(Acc, _From, _To, #iq{sub_el = SubEl} = IQ, _) -> |
116 |
:-( |
NewSubEl = [SubEl, mongoose_xmpp_errors:feature_not_implemented()], |
117 |
:-( |
{Acc, IQ#iq{type = error, sub_el = NewSubEl}}. |
118 |
|
|
119 |
|
%%==================================================================== |
120 |
|
%% Hook callbacks |
121 |
|
%%==================================================================== |
122 |
|
|
123 |
|
-spec user_send_iq(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) -> |
124 |
|
mongoose_c2s_hooks:result(). |
125 |
|
user_send_iq(Acc, #{c2s_data := StateData}, #{host_type := HostType}) -> |
126 |
:-( |
case {mongoose_acc:stanza_type(Acc), |
127 |
|
mongoose_c2s:get_mod_state(StateData, ?MODULE)} of |
128 |
|
{<<"result">>, {ok, #ping_handler{id = PingId, time = T0}}} -> |
129 |
:-( |
IqResponse = mongoose_acc:element(Acc), |
130 |
:-( |
IqId = exml_query:attr(IqResponse, <<"id">>), |
131 |
:-( |
case {IqId, PingId} of |
132 |
|
{Id, Id} -> |
133 |
:-( |
Jid = mongoose_c2s:get_jid(StateData), |
134 |
:-( |
TDelta = erlang:monotonic_time(millisecond) - T0, |
135 |
:-( |
mongoose_hooks:user_ping_response(HostType, #{}, Jid, IqResponse, TDelta), |
136 |
:-( |
Action = {{timeout, ping_timeout}, cancel}, |
137 |
:-( |
{stop, mongoose_c2s_acc:to_acc(Acc, actions, Action)}; |
138 |
|
_ -> |
139 |
:-( |
{ok, Acc} |
140 |
|
end; |
141 |
|
_ -> |
142 |
:-( |
{ok, Acc} |
143 |
|
end. |
144 |
|
|
145 |
|
-spec user_send_packet(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) -> |
146 |
|
mongoose_c2s_hooks:result(). |
147 |
|
user_send_packet(Acc, _Params, #{host_type := HostType}) -> |
148 |
:-( |
Interval = gen_mod:get_module_opt(HostType, ?MODULE, ping_interval), |
149 |
:-( |
Action = {{timeout, ping}, Interval, fun ping_c2s_handler/2}, |
150 |
:-( |
{ok, mongoose_c2s_acc:to_acc(Acc, actions, Action)}. |
151 |
|
|
152 |
|
-spec ping_c2s_handler(atom(), mongoose_c2s:data()) -> mongoose_c2s_acc:t(). |
153 |
|
ping_c2s_handler(ping, StateData) -> |
154 |
:-( |
HostType = mongoose_c2s:get_host_type(StateData), |
155 |
:-( |
Interval = gen_mod:get_module_opt(HostType, ?MODULE, ping_req_timeout), |
156 |
:-( |
Actions = [{{timeout, send_ping}, Interval, fun ping_c2s_handler/2}], |
157 |
:-( |
mongoose_c2s_acc:new(#{actions => Actions}); |
158 |
|
ping_c2s_handler(send_ping, StateData) -> |
159 |
:-( |
PingId = mongoose_bin:gen_from_crypto(), |
160 |
:-( |
IQ = ping_get(PingId), |
161 |
:-( |
HostType = mongoose_c2s:get_host_type(StateData), |
162 |
:-( |
LServer = mongoose_c2s:get_lserver(StateData), |
163 |
:-( |
Jid = mongoose_c2s:get_jid(StateData), |
164 |
:-( |
FromServer = jid:make_noprep(<<>>, LServer, <<>>), |
165 |
:-( |
Interval = gen_mod:get_module_opt(HostType, ?MODULE, ping_req_timeout), |
166 |
:-( |
Actions = [{{timeout, ping_timeout}, Interval, fun ping_c2s_handler/2}], |
167 |
:-( |
T0 = erlang:monotonic_time(millisecond), |
168 |
:-( |
Params = #{host_type => HostType, lserver => LServer, location => ?LOCATION, |
169 |
|
from_jid => FromServer, to_jid => Jid, element => IQ}, |
170 |
:-( |
Acc = mongoose_acc:new(Params), |
171 |
:-( |
mongoose_c2s_acc:new(#{state_mod => #{?MODULE => #ping_handler{id = PingId, time = T0}}, |
172 |
|
actions => Actions, route => [Acc]}); |
173 |
|
ping_c2s_handler(ping_timeout, StateData) -> |
174 |
:-( |
Jid = mongoose_c2s:get_jid(StateData), |
175 |
:-( |
HostType = mongoose_c2s:get_host_type(StateData), |
176 |
:-( |
mongoose_hooks:user_ping_response(HostType, #{}, Jid, timeout, 0), |
177 |
:-( |
case gen_mod:get_module_opt(HostType, ?MODULE, timeout_action) of |
178 |
:-( |
kill -> mongoose_c2s_acc:new(#{stop => {shutdown, ping_timeout}}); |
179 |
:-( |
_ -> mongoose_c2s_acc:new() |
180 |
|
end. |
181 |
|
|
182 |
|
-spec user_ping_response(Acc, Params, Extra) -> {ok, Acc} when |
183 |
|
Acc :: mongoose_acc:t(), |
184 |
|
Params :: #{response := timeout | jlib:iq(), time_delta := non_neg_integer()}, |
185 |
|
Extra :: #{host_type := mongooseim:host_type()}. |
186 |
|
user_ping_response(Acc, #{response := timeout}, #{host_type := HostType}) -> |
187 |
:-( |
mongoose_metrics:update(HostType, [mod_ping, ping_response_timeout], 1), |
188 |
:-( |
{ok, Acc}; |
189 |
|
user_ping_response(Acc, #{time_delta := TDelta}, #{host_type := HostType}) -> |
190 |
:-( |
mongoose_metrics:update(HostType, [mod_ping, ping_response_time], TDelta), |
191 |
:-( |
mongoose_metrics:update(HostType, [mod_ping, ping_response], 1), |
192 |
:-( |
{ok, Acc}. |
193 |
|
|
194 |
|
%%==================================================================== |
195 |
|
%% Stanzas |
196 |
|
%%==================================================================== |
197 |
|
|
198 |
|
-spec ping_get(binary()) -> exml:element(). |
199 |
|
ping_get(Id) -> |
200 |
:-( |
#xmlel{name = <<"iq">>, |
201 |
|
attrs = [{<<"type">>, <<"get">>}, {<<"id">>, Id}], |
202 |
|
children = [#xmlel{name = <<"ping">>, attrs = [{<<"xmlns">>, ?NS_PING}]}]}. |