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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2003-01-07 20:10:35 +01:00
|
|
|
-export([route/3,
|
2010-04-20 20:31:42 +02:00
|
|
|
route_error/4,
|
2004-11-05 22:14:31 +01:00
|
|
|
register_route/2,
|
2016-03-13 09:38:40 +01:00
|
|
|
register_route/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
register_routes/1,
|
2016-03-13 09:38:40 +01:00
|
|
|
host_of_route/1,
|
2016-07-18 14:01:32 +02:00
|
|
|
process_iq/3,
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route/1,
|
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,
|
|
|
|
get_backend/0]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2017-01-11 14:25:43 +01:00
|
|
|
-export([start/0, 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,
|
2015-06-01 14:38:27 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3, opt_type/1]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
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").
|
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(),
|
|
|
|
undefined | pos_integer()) -> ok | {error, term()}.
|
|
|
|
-callback unregister_route(binary(), undefined | pos_integer()) -> ok | {error, term()}.
|
|
|
|
-callback find_routes(binary()) -> [#route{}].
|
|
|
|
-callback host_of_route(binary()) -> {ok, binary()} | error.
|
|
|
|
-callback is_my_route(binary()) -> boolean().
|
|
|
|
-callback is_my_host(binary()) -> boolean().
|
|
|
|
-callback get_all_routes() -> [binary()].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2017-01-11 14:25:43 +01:00
|
|
|
start() ->
|
|
|
|
ChildSpec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
transient, 1000, worker, [?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2006-01-29 05:38:31 +01:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2004-11-05 22:14:31 +01:00
|
|
|
|
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} ->
|
|
|
|
?ERROR_MSG("failed to decode xml element ~p when "
|
|
|
|
"routing from ~s to ~s: ~s",
|
|
|
|
[El, jid:to_string(From), jid:to_string(To),
|
|
|
|
xmpp:format_error(Why)])
|
|
|
|
end;
|
|
|
|
route(#jid{} = From, #jid{} = To, Packet) ->
|
|
|
|
case catch do_route(From, To, xmpp:set_from_to(Packet, From, To)) of
|
2004-11-05 22:14:31 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-11-29 21:55:12 +01:00
|
|
|
|
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 ->
|
|
|
|
ejabberd_router:route(From, To, xmpp:make_error(Packet, Err))
|
2010-04-20 20:31:42 +02:00
|
|
|
end.
|
|
|
|
|
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).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec register_route(binary(), binary(), local_hint()) -> ok.
|
2016-03-13 09:38:40 +01:00
|
|
|
register_route(Domain, ServerHost, LocalHint) ->
|
|
|
|
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,
|
|
|
|
get_component_number(LDomain)) of
|
|
|
|
ok ->
|
|
|
|
?DEBUG("Route registered: ~s", [LDomain]);
|
|
|
|
{error, Err} ->
|
|
|
|
?ERROR_MSG("Failed to register route ~s: ~p",
|
|
|
|
[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) ->
|
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(),
|
|
|
|
case Mod:unregister_route(LDomain, get_component_number(LDomain)) of
|
|
|
|
ok ->
|
|
|
|
?DEBUG("Route unregistered: ~s", [LDomain]);
|
|
|
|
{error, Err} ->
|
|
|
|
?ERROR_MSG("Failed to unregister route ~s: ~p",
|
|
|
|
[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-01-11 14:25:43 +01:00
|
|
|
-spec get_all_routes() -> [binary()].
|
|
|
|
get_all_routes() ->
|
|
|
|
Mod = get_backend(),
|
|
|
|
Mod:get_all_routes().
|
2003-12-14 21:51:01 +01: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-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
|
|
|
case Mod:host_of_route(LDomain) of
|
|
|
|
{ok, ServerHost} -> ServerHost;
|
|
|
|
error -> 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-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
|
|
|
Mod:is_my_route(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-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
|
|
|
Mod:is_my_host(LDomain)
|
2016-12-28 07:47:11 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-16 14:40:11 +01:00
|
|
|
-spec process_iq(jid(), jid(), iq()) -> any().
|
2016-07-18 14:01:32 +02:00
|
|
|
process_iq(From, To, #iq{} = IQ) ->
|
|
|
|
if To#jid.luser == <<"">> ->
|
|
|
|
ejabberd_local:process_iq(From, To, IQ);
|
|
|
|
true ->
|
|
|
|
ejabberd_sm:process_iq(From, To, IQ)
|
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
init([]) ->
|
2017-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
|
|
|
Mod:init(),
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
2017-01-11 14:25:43 +01:00
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-01-11 14:25:43 +01:00
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
2017-01-16 09:34:49 +01:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?ERROR_MSG("unexpected info: ~p", [Info]),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec do_route(jid(), jid(), stanza()) -> any().
|
2006-01-29 05:38:31 +01:00
|
|
|
do_route(OrigFrom, OrigTo, OrigPacket) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
?DEBUG("route:~n~s", [xmpp:pp(OrigPacket)]),
|
2006-08-14 21:46:14 +02:00
|
|
|
case ejabberd_hooks:run_fold(filter_packet,
|
2017-01-09 15:02:17 +01:00
|
|
|
{OrigFrom, OrigTo, OrigPacket}, []) of
|
|
|
|
{From, To, Packet} ->
|
|
|
|
LDstDomain = To#jid.lserver,
|
2017-01-11 14:25:43 +01:00
|
|
|
Mod = get_backend(),
|
|
|
|
case Mod:find_routes(LDstDomain) of
|
2017-01-09 15:02:17 +01:00
|
|
|
[] ->
|
|
|
|
ejabberd_s2s:route(From, To, Packet);
|
|
|
|
[Route] ->
|
|
|
|
do_route(From, To, Packet, Route);
|
|
|
|
Routes ->
|
|
|
|
balancing_route(From, To, Packet, Routes)
|
|
|
|
end;
|
|
|
|
drop ->
|
|
|
|
ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec do_route(jid(), jid(), stanza(), #route{}) -> any().
|
|
|
|
do_route(From, To, Pkt, #route{local_hint = LocalHint,
|
|
|
|
pid = Pid}) when is_pid(Pid) ->
|
|
|
|
case LocalHint of
|
|
|
|
{apply, Module, Function} when node(Pid) == node() ->
|
|
|
|
Module:Function(From, To, Pkt);
|
|
|
|
_ ->
|
|
|
|
Pid ! {route, From, To, Pkt}
|
2016-07-18 14:01:32 +02:00
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
do_route(_From, _To, _Pkt, _Route) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
drop.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec balancing_route(jid(), jid(), stanza(), [#route{}]) -> any().
|
|
|
|
balancing_route(From, To, Packet, Rs) ->
|
|
|
|
LDstDomain = To#jid.lserver,
|
|
|
|
Value = get_domain_balancing(From, To, LDstDomain),
|
|
|
|
case get_component_number(LDstDomain) of
|
|
|
|
undefined ->
|
|
|
|
case [R || R <- Rs, node(R#route.pid) == node()] of
|
|
|
|
[] ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(Rs)), Rs),
|
|
|
|
do_route(From, To, Packet, R);
|
|
|
|
LRs ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(LRs)), LRs),
|
|
|
|
do_route(From, To, Packet, R)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
SRs = lists:ukeysort(#route.local_hint, Rs),
|
|
|
|
R = lists:nth(erlang:phash(Value, length(SRs)), SRs),
|
|
|
|
do_route(From, To, Packet, R)
|
|
|
|
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) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
|
|
|
{domain_balancing_component_number, LDomain},
|
|
|
|
fun(N) when is_integer(N), N > 1 -> N end,
|
|
|
|
undefined).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_domain_balancing(jid(), jid(), binary()) -> any().
|
|
|
|
get_domain_balancing(From, To, LDomain) ->
|
|
|
|
case ejabberd_config:get_option(
|
|
|
|
{domain_balancing, LDomain}, fun(D) when is_atom(D) -> D end) of
|
|
|
|
undefined -> p1_time_compat:monotonic_time();
|
|
|
|
random -> p1_time_compat:monotonic_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.
|
|
|
|
|
2017-01-11 14:25:43 +01:00
|
|
|
-spec get_backend() -> module().
|
|
|
|
get_backend() ->
|
|
|
|
DBType = case ejabberd_config:get_option(
|
|
|
|
router_db_type,
|
|
|
|
fun(T) -> ejabberd_config:v_db(?MODULE, T) end) of
|
|
|
|
undefined ->
|
|
|
|
ejabberd_config:default_ram_db(?MODULE);
|
|
|
|
T ->
|
|
|
|
T
|
|
|
|
end,
|
|
|
|
list_to_atom("ejabberd_router_" ++ atom_to_list(DBType)).
|
2003-01-04 21:09:25 +01:00
|
|
|
|
2016-01-15 12:32:10 +01:00
|
|
|
opt_type(domain_balancing) ->
|
|
|
|
fun (random) -> random;
|
|
|
|
(source) -> source;
|
|
|
|
(destination) -> destination;
|
|
|
|
(bare_source) -> bare_source;
|
|
|
|
(bare_destination) -> bare_destination
|
|
|
|
end;
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(domain_balancing_component_number) ->
|
|
|
|
fun (N) when is_integer(N), N > 1 -> N end;
|
2017-01-11 14:25:43 +01:00
|
|
|
opt_type(router_db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
|
|
|
opt_type(_) ->
|
|
|
|
[domain_balancing, domain_balancing_component_number,
|
|
|
|
router_db_type].
|