2017-11-10 16:02:22 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
%%% File : ejabberd_iq.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose :
|
2017-11-10 16:11:04 +01:00
|
|
|
%%% Created : 10 Nov 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2017-11-10 16:11:04 +01: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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
2017-11-10 16:02:22 +01:00
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
-module(ejabberd_iq).
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/0, route/4, dispatch/1]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
|
|
|
-include("xmpp.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
-record(state, {expire = infinity :: timeout()}).
|
2017-11-10 16:11:04 +01:00
|
|
|
-type state() :: #state{}.
|
2017-11-10 16:02:22 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec route(iq(), atom() | pid(), term(), non_neg_integer()) -> ok.
|
2017-11-10 16:02:22 +01:00
|
|
|
route(#iq{type = T} = IQ, Proc, Ctx, Timeout) when T == set; T == get ->
|
|
|
|
Expire = current_time() + Timeout,
|
|
|
|
Rnd = randoms:get_string(),
|
|
|
|
ID = encode_id(Expire, Rnd),
|
|
|
|
ets:insert(?MODULE, {{Expire, Rnd}, Proc, Ctx}),
|
|
|
|
gen_server:cast(?MODULE, {restart_timer, Expire}),
|
|
|
|
ejabberd_router:route(IQ#iq{id = ID}).
|
|
|
|
|
|
|
|
-spec dispatch(iq()) -> boolean().
|
|
|
|
dispatch(#iq{type = T, id = ID} = IQ) when T == error; T == result ->
|
|
|
|
case decode_id(ID) of
|
|
|
|
{ok, Expire, Rnd, Node} ->
|
|
|
|
ejabberd_cluster:send({?MODULE, Node}, {route, IQ, {Expire, Rnd}});
|
|
|
|
error ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
dispatch(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
|
|
|
init([]) ->
|
|
|
|
ets:new(?MODULE, [named_table, ordered_set, public]),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
{stop, {unexpected_call, Request, From}, State}.
|
|
|
|
|
|
|
|
handle_cast({restart_timer, Expire}, State) ->
|
|
|
|
State1 = State#state{expire = min(Expire, State#state.expire)},
|
|
|
|
noreply(State1);
|
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("unexpected cast: ~p", [Msg]),
|
|
|
|
noreply(State).
|
|
|
|
|
|
|
|
handle_info({route, IQ, Key}, State) ->
|
|
|
|
case ets:lookup(?MODULE, Key) of
|
|
|
|
[{_, Proc, Ctx}] ->
|
|
|
|
callback(Proc, IQ, Ctx),
|
|
|
|
ets:delete(?MODULE, Key);
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
noreply(State);
|
|
|
|
handle_info(timeout, State) ->
|
|
|
|
Expire = clean(ets:first(?MODULE)),
|
|
|
|
noreply(State#state{expire = Expire});
|
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("unexpected info: ~p", [Info]),
|
|
|
|
noreply(State).
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec current_time() -> non_neg_integer().
|
2017-11-10 16:02:22 +01:00
|
|
|
current_time() ->
|
|
|
|
p1_time_compat:system_time(milli_seconds).
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec clean({non_neg_integer(), binary()} | '$end_of_table')
|
|
|
|
-> non_neg_integer() | infinity.
|
2017-11-10 16:02:22 +01:00
|
|
|
clean({Expire, _} = Key) ->
|
|
|
|
case current_time() of
|
|
|
|
Time when Time >= Expire ->
|
|
|
|
case ets:lookup(?MODULE, Key) of
|
|
|
|
[{_, Proc, Ctx}] ->
|
|
|
|
callback(Proc, timeout, Ctx),
|
|
|
|
ets:delete(?MODULE, Key);
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
clean(ets:next(?MODULE, Key));
|
|
|
|
_ ->
|
|
|
|
Expire
|
|
|
|
end;
|
|
|
|
clean('$end_of_table') ->
|
|
|
|
infinity.
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec noreply(state()) -> {noreply, state()} | {noreply, state(), non_neg_integer()}.
|
2017-11-10 16:02:22 +01:00
|
|
|
noreply(#state{expire = Expire} = State) ->
|
|
|
|
case Expire of
|
|
|
|
infinity ->
|
|
|
|
{noreply, State};
|
|
|
|
_ ->
|
|
|
|
Timeout = max(0, Expire - current_time()),
|
|
|
|
{noreply, State, Timeout}
|
|
|
|
end.
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec encode_id(non_neg_integer(), binary()) -> binary().
|
2017-11-10 16:02:22 +01:00
|
|
|
encode_id(Expire, Rnd) ->
|
|
|
|
ExpireBin = integer_to_binary(Expire),
|
2018-02-18 09:54:40 +01:00
|
|
|
Node = ejabberd_cluster:node_id(),
|
2017-11-10 16:02:22 +01:00
|
|
|
CheckSum = calc_checksum(<<ExpireBin/binary, Rnd/binary, Node/binary>>),
|
|
|
|
<<"rr-", ExpireBin/binary, $-, Rnd/binary, $-, CheckSum/binary, $-, Node/binary>>.
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec decode_id(binary()) -> {ok, non_neg_integer(), binary(), atom()} | error.
|
2017-11-10 16:02:22 +01:00
|
|
|
decode_id(<<"rr-", ID/binary>>) ->
|
|
|
|
try
|
|
|
|
[ExpireBin, Tail] = binary:split(ID, <<"-">>),
|
|
|
|
[Rnd, Rest] = binary:split(Tail, <<"-">>),
|
|
|
|
[CheckSum, NodeBin] = binary:split(Rest, <<"-">>),
|
|
|
|
CheckSum = calc_checksum(<<ExpireBin/binary, Rnd/binary, NodeBin/binary>>),
|
2018-02-18 09:54:40 +01:00
|
|
|
Node = ejabberd_cluster:get_node_by_id(NodeBin),
|
2017-11-10 16:02:22 +01:00
|
|
|
Expire = binary_to_integer(ExpireBin),
|
|
|
|
{ok, Expire, Rnd, Node}
|
|
|
|
catch _:{badmatch, _} ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
decode_id(_) ->
|
|
|
|
error.
|
|
|
|
|
2017-11-10 16:11:04 +01:00
|
|
|
-spec calc_checksum(binary()) -> binary().
|
2017-11-10 16:02:22 +01:00
|
|
|
calc_checksum(Data) ->
|
|
|
|
Key = ejabberd_config:get_option(shared_key),
|
|
|
|
base64:encode(crypto:hash(sha, <<Data/binary, Key/binary>>)).
|
|
|
|
|
2017-11-11 07:33:42 +01:00
|
|
|
-spec callback(atom() | pid(), #iq{} | timeout, term()) -> any().
|
2017-11-10 16:02:22 +01:00
|
|
|
callback(undefined, IQRes, Fun) ->
|
|
|
|
Fun(IQRes);
|
|
|
|
callback(Proc, IQRes, Ctx) ->
|
|
|
|
Proc ! {iq_reply, IQRes, Ctx}.
|