2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-14 21:51:01 +01:00
|
|
|
%%% Purpose : Main router
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 12:41:41 +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.
|
2009-01-12 15:44:42 +01: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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_router).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2017-02-14 13:39:57 +01:00
|
|
|
-ifndef(GEN_SERVER).
|
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-endif.
|
|
|
|
-behaviour(?GEN_SERVER).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%% API
|
2017-02-16 09:00:26 +01:00
|
|
|
-export([route/1,
|
|
|
|
route_error/2,
|
2017-11-10 16:02:22 +01:00
|
|
|
route_iq/2,
|
|
|
|
route_iq/3,
|
|
|
|
route_iq/4,
|
2004-11-05 22:14:31 +01:00
|
|
|
register_route/2,
|
2016-03-13 09:38:40 +01:00
|
|
|
register_route/3,
|
2017-03-15 08:27:22 +01:00
|
|
|
register_route/4,
|
2005-04-17 20:08:34 +02:00
|
|
|
register_routes/1,
|
2016-03-13 09:38:40 +01:00
|
|
|
host_of_route/1,
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq/1,
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route/1,
|
2017-03-28 15:31:37 +02:00
|
|
|
unregister_route/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
unregister_routes/1,
|
2017-01-11 14:25:43 +01:00
|
|
|
get_all_routes/0,
|
2016-12-28 07:47:11 +01:00
|
|
|
is_my_route/1,
|
2017-01-11 14:25:43 +01:00
|
|
|
is_my_host/1,
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache/1,
|
|
|
|
config_reloaded/0,
|
2017-01-11 14:25:43 +01:00
|
|
|
get_backend/0]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-export([start_link/0]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
%% Deprecated functions
|
|
|
|
-export([route/3, route_error/4]).
|
|
|
|
-deprecated([{route, 3}, {route_error, 4}]).
|
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
%% This value is used in SIP and Megaco for a transaction lifetime.
|
|
|
|
-define(IQ_TIMEOUT, 32000).
|
2019-07-03 09:39:03 +02:00
|
|
|
-define(CALL_TIMEOUT, timer:minutes(10)).
|
2017-11-10 16:02:22 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2017-01-11 14:25:43 +01:00
|
|
|
-include("ejabberd_router.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2002-11-29 21:55:12 +01:00
|
|
|
|
2017-01-11 14:25:43 +01:00
|
|
|
-callback init() -> any().
|
|
|
|
-callback register_route(binary(), binary(), local_hint(),
|
2017-03-15 08:27:22 +01:00
|
|
|
undefined | pos_integer(), pid()) -> ok | {error, term()}.
|
2017-03-28 15:31:37 +02:00
|
|
|
-callback unregister_route(binary(), undefined | pos_integer(), pid()) -> ok | {error, term()}.
|
2017-04-14 12:57:52 +02:00
|
|
|
-callback find_routes(binary()) -> {ok, [#route{}]} | {error, any()}.
|
|
|
|
-callback get_all_routes() -> {ok, [binary()]} | {error, any()}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-03 09:39:03 +02:00
|
|
|
-record(state, {route_monitors = #{} :: #{{binary(), pid()} => reference()}}).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2017-02-14 13:39:57 +01:00
|
|
|
?GEN_SERVER:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2004-11-05 22:14:31 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route(stanza()) -> ok.
|
|
|
|
route(Packet) ->
|
|
|
|
try do_route(Packet)
|
2019-07-07 21:12:14 +02:00
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
2019-06-25 23:05:41 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to route packet:~n~ts~n** ~ts",
|
2019-07-07 21:12:14 +02:00
|
|
|
[xmpp:pp(Packet),
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)])
|
2017-02-16 09:00:26 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec route(jid(), jid(), xmlel() | stanza()) -> ok.
|
2017-01-09 15:02:17 +01:00
|
|
|
route(#jid{} = From, #jid{} = To, #xmlel{} = El) ->
|
|
|
|
try xmpp:decode(El, ?NS_CLIENT, [ignore_els]) of
|
2017-01-11 14:25:43 +01:00
|
|
|
Pkt -> route(From, To, Pkt)
|
2017-01-09 15:02:17 +01:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Failed to decode xml element ~p when "
|
2019-09-23 14:17:20 +02:00
|
|
|
"routing from ~ts to ~ts: ~ts",
|
2017-02-26 08:07:12 +01:00
|
|
|
[El, jid:encode(From), jid:encode(To),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:format_error(Why)])
|
|
|
|
end;
|
|
|
|
route(#jid{} = From, #jid{} = To, Packet) ->
|
2019-07-07 21:12:14 +02:00
|
|
|
route(xmpp:set_from_to(Packet, From, To)).
|
2002-11-29 21:55:12 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route_error(stanza(), stanza_error()) -> ok.
|
|
|
|
route_error(Packet, Err) ->
|
|
|
|
Type = xmpp:get_type(Packet),
|
|
|
|
if Type == error; Type == result ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
route(xmpp:make_error(Packet, Err))
|
|
|
|
end.
|
|
|
|
|
2010-04-20 20:31:42 +02:00
|
|
|
%% Route the error packet only if the originating packet is not an error itself.
|
|
|
|
%% RFC3920 9.3.1
|
2016-07-25 12:50:30 +02:00
|
|
|
-spec route_error(jid(), jid(), xmlel(), xmlel()) -> ok;
|
2016-09-08 16:08:48 +02:00
|
|
|
(jid(), jid(), stanza(), stanza_error()) -> ok.
|
2016-07-25 12:50:30 +02:00
|
|
|
route_error(From, To, #xmlel{} = ErrPacket, #xmlel{} = OrigPacket) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{attrs = Attrs} = OrigPacket,
|
2016-02-03 19:03:17 +01:00
|
|
|
case <<"error">> == fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> route(From, To, ErrPacket);
|
|
|
|
true -> ok
|
2016-07-25 12:50:30 +02:00
|
|
|
end;
|
2016-09-08 16:08:48 +02:00
|
|
|
route_error(From, To, Packet, #stanza_error{} = Err) ->
|
2016-07-25 12:50:30 +02:00
|
|
|
Type = xmpp:get_type(Packet),
|
|
|
|
if Type == error; Type == result ->
|
|
|
|
ok;
|
|
|
|
true ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route(From, To, xmpp:make_error(Packet, Err))
|
2010-04-20 20:31:42 +02:00
|
|
|
end.
|
|
|
|
|
2017-11-11 07:33:42 +01:00
|
|
|
-spec route_iq(iq(), fun((iq() | timeout) -> any())) -> ok.
|
|
|
|
route_iq(IQ, Fun) ->
|
|
|
|
route_iq(IQ, Fun, undefined, ?IQ_TIMEOUT).
|
2017-11-10 16:02:22 +01:00
|
|
|
|
|
|
|
-spec route_iq(iq(), term(), pid() | atom()) -> ok.
|
|
|
|
route_iq(IQ, State, Proc) ->
|
|
|
|
route_iq(IQ, State, Proc, ?IQ_TIMEOUT).
|
|
|
|
|
|
|
|
-spec route_iq(iq(), term(), pid() | atom(), undefined | non_neg_integer()) -> ok.
|
|
|
|
route_iq(IQ, State, Proc, undefined) ->
|
|
|
|
route_iq(IQ, State, Proc, ?IQ_TIMEOUT);
|
|
|
|
route_iq(IQ, State, Proc, Timeout) ->
|
|
|
|
ejabberd_iq:route(IQ, Proc, State, Timeout).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec register_route(binary(), binary()) -> ok.
|
2016-03-13 09:38:40 +01:00
|
|
|
register_route(Domain, ServerHost) ->
|
|
|
|
register_route(Domain, ServerHost, undefined).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec register_route(binary(), binary(), local_hint() | undefined) -> ok.
|
2016-03-13 09:38:40 +01:00
|
|
|
register_route(Domain, ServerHost, LocalHint) ->
|
2017-03-15 08:27:22 +01:00
|
|
|
register_route(Domain, ServerHost, LocalHint, self()).
|
|
|
|
|
|
|
|
-spec register_route(binary(), binary(), local_hint() | undefined, pid()) -> ok.
|
|
|
|
register_route(Domain, ServerHost, LocalHint, Pid) ->
|
2016-03-13 09:38:40 +01:00
|
|
|
case {jid:nameprep(Domain), jid:nameprep(ServerHost)} of
|
2017-01-11 14:25:43 +01:00
|
|
|
{error, _} ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
{_, error} ->
|
|
|
|
erlang:error({invalid_domain, ServerHost});
|
|
|
|
{LDomain, LServerHost} ->
|
|
|
|
Mod = get_backend(),
|
|
|
|
case Mod:register_route(LDomain, LServerHost, LocalHint,
|
2017-03-15 08:27:22 +01:00
|
|
|
get_component_number(LDomain), Pid) of
|
2017-01-11 14:25:43 +01:00
|
|
|
ok ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Route registered: ~ts", [LDomain]),
|
2019-07-03 09:39:03 +02:00
|
|
|
monitor_route(LDomain, Pid),
|
2017-05-12 15:27:09 +02:00
|
|
|
ejabberd_hooks:run(route_registered, [LDomain]),
|
2017-04-14 12:57:52 +02:00
|
|
|
delete_cache(Mod, LDomain);
|
2017-01-11 14:25:43 +01:00
|
|
|
{error, Err} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to register route ~ts: ~p",
|
2017-01-11 14:25:43 +01:00
|
|
|
[LDomain, Err])
|
|
|
|
end
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2016-03-13 09:38:40 +01:00
|
|
|
-spec register_routes([{binary(), binary()}]) -> ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
register_routes(Domains) ->
|
2016-03-13 09:38:40 +01:00
|
|
|
lists:foreach(fun ({Domain, ServerHost}) -> register_route(Domain, ServerHost)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Domains).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec unregister_route(binary()) -> ok.
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route(Domain) ->
|
2017-03-28 15:31:37 +02:00
|
|
|
unregister_route(Domain, self()).
|
|
|
|
|
|
|
|
-spec unregister_route(binary(), pid()) -> ok.
|
|
|
|
unregister_route(Domain, Pid) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nameprep(Domain) of
|
2017-01-11 14:25:43 +01:00
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
|
|
|
Mod = get_backend(),
|
2017-03-28 15:31:37 +02:00
|
|
|
case Mod:unregister_route(
|
|
|
|
LDomain, get_component_number(LDomain), Pid) of
|
2017-01-11 14:25:43 +01:00
|
|
|
ok ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Route unregistered: ~ts", [LDomain]),
|
2019-07-03 09:39:03 +02:00
|
|
|
demonitor_route(LDomain, Pid),
|
2017-05-12 15:27:09 +02:00
|
|
|
ejabberd_hooks:run(route_unregistered, [LDomain]),
|
2017-04-14 12:57:52 +02:00
|
|
|
delete_cache(Mod, LDomain);
|
2017-01-11 14:25:43 +01:00
|
|
|
{error, Err} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to unregister route ~ts: ~p",
|
2017-01-11 14:25:43 +01:00
|
|
|
[LDomain, Err])
|
|
|
|
end
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec unregister_routes([binary()]) -> ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
unregister_routes(Domains) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (Domain) -> unregister_route(Domain)
|
|
|
|
end,
|
|
|
|
Domains).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec find_routes(binary()) -> [#route{}].
|
|
|
|
find_routes(Domain) ->
|
2017-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
2017-04-14 12:57:52 +02:00
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
case ets_cache:lookup(
|
|
|
|
?ROUTES_CACHE, {route, Domain},
|
|
|
|
fun() ->
|
|
|
|
case Mod:find_routes(Domain) of
|
|
|
|
{ok, Rs} when Rs /= [] ->
|
|
|
|
{ok, Rs};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end) of
|
|
|
|
{ok, Rs} -> Rs;
|
|
|
|
error -> []
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case Mod:find_routes(Domain) of
|
|
|
|
{ok, Rs} -> Rs;
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end.
|
2003-12-14 21:51:01 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec get_all_routes() -> [binary()].
|
|
|
|
get_all_routes() ->
|
2017-03-28 18:34:04 +02:00
|
|
|
Mod = get_backend(),
|
2017-04-14 12:57:52 +02:00
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
case ets_cache:lookup(
|
|
|
|
?ROUTES_CACHE, routes,
|
|
|
|
fun() ->
|
|
|
|
case Mod:get_all_routes() of
|
|
|
|
{ok, Rs} when Rs /= [] ->
|
|
|
|
{ok, Rs};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end) of
|
|
|
|
{ok, Rs} -> Rs;
|
|
|
|
error -> []
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case Mod:get_all_routes() of
|
|
|
|
{ok, Rs} -> Rs;
|
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end.
|
2017-03-28 18:34:04 +02:00
|
|
|
|
2016-03-13 09:38:40 +01:00
|
|
|
-spec host_of_route(binary()) -> binary().
|
|
|
|
host_of_route(Domain) ->
|
|
|
|
case jid:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
2017-04-14 12:57:52 +02:00
|
|
|
case find_routes(LDomain) of
|
|
|
|
[#route{server_host = ServerHost}|_] ->
|
|
|
|
ServerHost;
|
|
|
|
_ ->
|
|
|
|
erlang:error({unregistered_route, Domain})
|
2016-03-13 09:38:40 +01:00
|
|
|
end
|
|
|
|
end.
|
2003-12-14 21:51:01 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec is_my_route(binary()) -> boolean().
|
|
|
|
is_my_route(Domain) ->
|
|
|
|
case jid:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
2017-04-14 19:34:00 +02:00
|
|
|
find_routes(LDomain) /= []
|
2016-12-28 07:47:11 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec is_my_host(binary()) -> boolean().
|
|
|
|
is_my_host(Domain) ->
|
|
|
|
case jid:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
2017-04-14 12:57:52 +02:00
|
|
|
case find_routes(LDomain) of
|
|
|
|
[#route{server_host = LDomain}|_] -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
2016-12-28 07:47:11 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec process_iq(iq()) -> any().
|
2018-05-09 09:30:00 +02:00
|
|
|
process_iq(IQ) ->
|
|
|
|
gen_iq_handler:handle(IQ).
|
2016-07-18 14:01:32 +02:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec config_reloaded() -> ok.
|
|
|
|
config_reloaded() ->
|
|
|
|
Mod = get_backend(),
|
|
|
|
init_cache(Mod).
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
init([]) ->
|
2017-04-14 12:57:52 +02:00
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 50),
|
2017-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
2017-04-14 12:57:52 +02:00
|
|
|
init_cache(Mod),
|
2017-01-11 14:25:43 +01:00
|
|
|
Mod:init(),
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache(),
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2019-07-03 09:39:03 +02:00
|
|
|
handle_call({monitor, Domain, Pid}, _From, State) ->
|
|
|
|
MRefs = State#state.route_monitors,
|
|
|
|
MRefs1 = case maps:is_key({Domain, Pid}, MRefs) of
|
|
|
|
true -> MRefs;
|
|
|
|
false ->
|
|
|
|
MRef = erlang:monitor(process, Pid),
|
|
|
|
MRefs#{{Domain, Pid} => MRef}
|
|
|
|
end,
|
|
|
|
{reply, ok, State#state{route_monitors = MRefs1}};
|
|
|
|
handle_call({demonitor, Domain, Pid}, _From, State) ->
|
|
|
|
MRefs = State#state.route_monitors,
|
|
|
|
MRefs1 = case maps:find({Domain, Pid}, MRefs) of
|
|
|
|
{ok, MRef} ->
|
|
|
|
erlang:demonitor(MRef, [flush]),
|
|
|
|
maps:remove({Domain, Pid}, MRefs);
|
|
|
|
error ->
|
|
|
|
MRefs
|
|
|
|
end,
|
|
|
|
{reply, ok, State#state{route_monitors = MRefs1}};
|
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2019-07-03 09:39:03 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-01-11 14:25:43 +01:00
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, Packet}, State) ->
|
|
|
|
route(Packet),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2019-07-03 09:39:03 +02:00
|
|
|
handle_info({'DOWN', MRef, _, Pid, Info}, State) ->
|
|
|
|
MRefs = maps:filter(
|
|
|
|
fun({Domain, P}, M) when P == Pid, M == MRef ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Process ~p with route registered to ~ts "
|
2019-07-03 09:39:03 +02:00
|
|
|
"has terminated unexpectedly with reason: ~p",
|
|
|
|
[P, Domain, Info]),
|
2019-07-18 20:48:53 +02:00
|
|
|
try unregister_route(Domain, Pid)
|
|
|
|
catch _:_ -> ok
|
|
|
|
end,
|
2019-07-03 09:39:03 +02:00
|
|
|
false;
|
|
|
|
(_, _) ->
|
|
|
|
true
|
|
|
|
end, State#state.route_monitors),
|
|
|
|
{noreply, State#state{route_monitors = MRefs}};
|
2017-01-16 09:34:49 +01:00
|
|
|
handle_info(Info, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Unexpected info: ~p", [Info]),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2019-07-29 09:46:20 +02:00
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 50).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(stanza()) -> ok.
|
|
|
|
do_route(OrigPacket) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Route:~n~ts", [xmpp:pp(OrigPacket)]),
|
2017-02-16 09:00:26 +01:00
|
|
|
case ejabberd_hooks:run_fold(filter_packet, OrigPacket, []) of
|
|
|
|
drop ->
|
|
|
|
ok;
|
|
|
|
Packet ->
|
2017-11-10 16:02:22 +01:00
|
|
|
case ejabberd_iq:dispatch(Packet) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
To = xmpp:get_to(Packet),
|
|
|
|
LDstDomain = To#jid.lserver,
|
|
|
|
case find_routes(LDstDomain) of
|
|
|
|
[] ->
|
|
|
|
ejabberd_s2s:route(Packet);
|
|
|
|
[Route] ->
|
|
|
|
do_route(Packet, Route);
|
|
|
|
Routes ->
|
|
|
|
From = xmpp:get_from(Packet),
|
|
|
|
balancing_route(From, To, Packet, Routes)
|
|
|
|
end,
|
|
|
|
ok
|
|
|
|
end
|
2006-01-29 05:38:31 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(stanza(), #route{}) -> any().
|
|
|
|
do_route(Pkt, #route{local_hint = LocalHint,
|
|
|
|
pid = Pid}) when is_pid(Pid) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
case LocalHint of
|
|
|
|
{apply, Module, Function} when node(Pid) == node() ->
|
2017-02-16 09:00:26 +01:00
|
|
|
Module:Function(Pkt);
|
2017-01-09 15:02:17 +01:00
|
|
|
_ ->
|
2019-07-03 08:42:18 +02:00
|
|
|
ejabberd_cluster:send(Pid, {route, Pkt})
|
2016-07-18 14:01:32 +02:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(_Pkt, _Route) ->
|
|
|
|
ok.
|
2016-07-18 14:01:32 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec balancing_route(jid(), jid(), stanza(), [#route{}]) -> any().
|
|
|
|
balancing_route(From, To, Packet, Rs) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case get_domain_balancing(From, To, To#jid.lserver) of
|
2017-01-09 15:02:17 +01:00
|
|
|
undefined ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Value = erlang:system_time(),
|
2017-01-09 15:02:17 +01:00
|
|
|
case [R || R <- Rs, node(R#route.pid) == node()] of
|
|
|
|
[] ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(Rs)), Rs),
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Packet, R);
|
2017-01-09 15:02:17 +01:00
|
|
|
LRs ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(LRs)), LRs),
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Packet, R)
|
2017-01-09 15:02:17 +01:00
|
|
|
end;
|
2019-06-14 11:33:26 +02:00
|
|
|
Value ->
|
2017-01-09 15:02:17 +01:00
|
|
|
SRs = lists:ukeysort(#route.local_hint, Rs),
|
|
|
|
R = lists:nth(erlang:phash(Value, length(SRs)), SRs),
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Packet, R)
|
2017-01-09 15:02:17 +01:00
|
|
|
end.
|
2016-07-27 09:45:08 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_component_number(binary()) -> pos_integer() | undefined.
|
2007-01-19 05:46:44 +01:00
|
|
|
get_component_number(LDomain) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
M = ejabberd_option:domain_balancing(),
|
|
|
|
case maps:get(LDomain, M, undefined) of
|
|
|
|
undefined -> undefined;
|
|
|
|
Opts -> maps:get(component_number, Opts)
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec get_domain_balancing(jid(), jid(), binary()) -> integer() | ljid() | undefined.
|
2016-07-18 14:01:32 +02:00
|
|
|
get_domain_balancing(From, To, LDomain) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
M = ejabberd_option:domain_balancing(),
|
|
|
|
case maps:get(LDomain, M, undefined) of
|
|
|
|
undefined -> undefined;
|
|
|
|
Opts ->
|
|
|
|
case maps:get(type, Opts, random) of
|
|
|
|
random -> erlang:system_time();
|
|
|
|
source -> jid:tolower(From);
|
|
|
|
destination -> jid:tolower(To);
|
|
|
|
bare_source -> jid:remove_resource(jid:tolower(From));
|
|
|
|
bare_destination -> jid:remove_resource(jid:tolower(To))
|
|
|
|
end
|
2016-07-18 14:01:32 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-03 09:39:03 +02:00
|
|
|
-spec monitor_route(binary(), pid()) -> ok.
|
|
|
|
monitor_route(Domain, Pid) ->
|
|
|
|
?GEN_SERVER:call(?MODULE, {monitor, Domain, Pid}, ?CALL_TIMEOUT).
|
|
|
|
|
|
|
|
-spec demonitor_route(binary(), pid()) -> ok.
|
|
|
|
demonitor_route(Domain, Pid) ->
|
2019-07-18 20:48:53 +02:00
|
|
|
case whereis(?MODULE) == self() of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
?GEN_SERVER:call(?MODULE, {demonitor, Domain, Pid}, ?CALL_TIMEOUT)
|
|
|
|
end.
|
2019-07-03 09:39:03 +02:00
|
|
|
|
2017-01-11 14:25:43 +01:00
|
|
|
-spec get_backend() -> module().
|
|
|
|
get_backend() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
DBType = ejabberd_option:router_db_type(),
|
2019-06-15 14:14:45 +02:00
|
|
|
list_to_existing_atom("ejabberd_router_" ++ atom_to_list(DBType)).
|
2003-01-04 21:09:25 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec cache_nodes(module()) -> [node()].
|
|
|
|
cache_nodes(Mod) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 0) of
|
|
|
|
true -> Mod:cache_nodes();
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec use_cache(module()) -> boolean().
|
|
|
|
use_cache(Mod) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 0) of
|
|
|
|
true -> Mod:use_cache();
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> ejabberd_option:router_use_cache()
|
2017-04-14 12:57:52 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec delete_cache(module(), binary()) -> ok.
|
|
|
|
delete_cache(Mod, Domain) ->
|
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?ROUTES_CACHE, {route, Domain}, cache_nodes(Mod)),
|
|
|
|
ets_cache:delete(?ROUTES_CACHE, routes, cache_nodes(Mod));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec init_cache(module()) -> ok.
|
|
|
|
init_cache(Mod) ->
|
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(?ROUTES_CACHE, cache_opts());
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?ROUTES_CACHE)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_opts() -> [proplists:property()].
|
|
|
|
cache_opts() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = ejabberd_option:router_cache_size(),
|
|
|
|
CacheMissed = ejabberd_option:router_cache_missed(),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = ejabberd_option:router_cache_life_time(),
|
2017-04-14 12:57:52 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec clean_cache(node()) -> non_neg_integer().
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache(Node) ->
|
|
|
|
ets_cache:filter(
|
|
|
|
?ROUTES_CACHE,
|
|
|
|
fun(_, error) ->
|
|
|
|
false;
|
|
|
|
(routes, _) ->
|
|
|
|
false;
|
|
|
|
({route, _}, {ok, Rs}) ->
|
|
|
|
not lists:any(
|
|
|
|
fun(#route{pid = Pid}) ->
|
|
|
|
node(Pid) == Node
|
|
|
|
end, Rs)
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec clean_cache() -> ok.
|
|
|
|
clean_cache() ->
|
|
|
|
ejabberd_cluster:eval_everywhere(?MODULE, clean_cache, [node()]).
|