2015-04-08 14:01:16 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router_multicast.erl
|
|
|
|
%%% Author : Badlop <badlop@process-one.net>
|
|
|
|
%%% Purpose : Multicast router
|
|
|
|
%%% Created : 11 Aug 2007 by Badlop <badlop@process-one.net>
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2015-10-07 00:06:58 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
2015-04-08 14:01:16 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_router_multicast).
|
|
|
|
-author('alexey@process-one.net').
|
|
|
|
-author('badlop@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([route_multicast/4,
|
|
|
|
register_route/1,
|
|
|
|
unregister_route/1
|
|
|
|
]).
|
|
|
|
|
2017-02-24 10:05:47 +01:00
|
|
|
-export([start_link/0]).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-record(route_multicast, {domain = <<"">> :: binary() | '_',
|
2016-08-04 10:49:17 +02:00
|
|
|
pid = self() :: pid()}).
|
2015-04-08 14:01:16 +02:00
|
|
|
-record(state, {}).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec route_multicast(jid(), binary(), [jid()], stanza()) -> ok.
|
2015-04-08 14:01:16 +02:00
|
|
|
route_multicast(From, Domain, Destinations, Packet) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
case catch do_route(Domain, Destinations, xmpp:set_from(Packet, From)) of
|
2015-04-08 14:01:16 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, Domain, Destinations, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec register_route(binary()) -> any().
|
2015-04-08 14:01:16 +02:00
|
|
|
register_route(Domain) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nameprep(Domain) of
|
2015-04-08 14:01:16 +02:00
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
|
|
|
Pid = self(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#route_multicast{domain = LDomain,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec unregister_route(binary()) -> any().
|
2015-04-08 14:01:16 +02:00
|
|
|
unregister_route(Domain) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nameprep(Domain) of
|
2015-04-08 14:01:16 +02:00
|
|
|
error ->
|
|
|
|
erlang:error({invalid_domain, Domain});
|
|
|
|
LDomain ->
|
|
|
|
Pid = self(),
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:select(route_multicast,
|
|
|
|
[{#route_multicast{pid = Pid, domain = LDomain, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]) of
|
|
|
|
[R] -> mnesia:delete_object(R);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([]) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, route_multicast,
|
2015-04-08 14:01:16 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, route_multicast)}]),
|
|
|
|
mnesia:subscribe({table, route_multicast, simple}),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Pid) ->
|
|
|
|
erlang:monitor(process, Pid)
|
|
|
|
end,
|
|
|
|
mnesia:dirty_select(route_multicast, [{{route_multicast, '_', '$1'}, [], ['$1']}])),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2015-04-08 14:01:16 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route_multicast, Domain, Destinations, Packet}, State) ->
|
|
|
|
case catch do_route(Domain, Destinations, Packet) of
|
2015-04-08 14:01:16 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
2017-02-16 09:00:26 +01:00
|
|
|
[Reason, {Domain, Destinations, Packet}]);
|
2015-04-08 14:01:16 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({mnesia_table_event, {write, #route_multicast{pid = Pid}, _ActivityId}},
|
|
|
|
State) ->
|
|
|
|
erlang:monitor(process, Pid),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({'DOWN', _Ref, _Type, Pid, _Info}, State) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
route_multicast,
|
|
|
|
[{#route_multicast{pid = Pid, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(
|
|
|
|
fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
{noreply, State};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2015-04-08 14:01:16 +02:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% From = #jid
|
|
|
|
%% Destinations = [#jid]
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(binary(), [jid()], stanza()) -> any().
|
|
|
|
do_route(Domain, Destinations, Packet) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Route multicast:~n~ts~nDomain: ~ts~nDestinations: ~ts~n",
|
2017-02-16 09:00:26 +01:00
|
|
|
[xmpp:pp(Packet), Domain,
|
2017-02-26 08:07:12 +01:00
|
|
|
str:join([jid:encode(To) || To <- Destinations], <<", ">>)]),
|
2015-04-08 14:01:16 +02:00
|
|
|
%% Try to find an appropriate multicast service
|
|
|
|
case mnesia:dirty_read(route_multicast, Domain) of
|
|
|
|
|
|
|
|
%% If no multicast service is available in this server, send manually
|
2017-02-16 09:00:26 +01:00
|
|
|
[] -> do_route_normal(Destinations, Packet);
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2015-06-04 12:13:10 +02:00
|
|
|
%% If some is available, send the packet using multicast service
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
Pid = pick_multicast_pid(Rs),
|
2017-02-16 09:00:26 +01:00
|
|
|
Pid ! {route_trusted, Destinations, Packet}
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec pick_multicast_pid([#route_multicast{}]) -> pid().
|
2015-06-04 12:13:10 +02:00
|
|
|
pick_multicast_pid(Rs) ->
|
|
|
|
List = case [R || R <- Rs, node(R#route_multicast.pid) == node()] of
|
|
|
|
[] -> Rs;
|
|
|
|
RLocals -> RLocals
|
|
|
|
end,
|
|
|
|
(hd(List))#route_multicast.pid.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route_normal([jid()], stanza()) -> any().
|
|
|
|
do_route_normal(Destinations, Packet) ->
|
|
|
|
[ejabberd_router:route(xmpp:set_to(Packet, To)) || To <- Destinations].
|