2016-12-28 07:47:11 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% Created : 16 Dec 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2022-02-10 17:21:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2022 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-08 21:49:16 +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
|
|
|
%%%
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
2002-12-06 21:59:19 +01:00
|
|
|
-module(ejabberd_s2s_out).
|
2016-12-28 07:47:11 +01:00
|
|
|
-behaviour(xmpp_stream_out).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%% xmpp_stream_out callbacks
|
|
|
|
-export([tls_options/1, tls_required/1, tls_verify/1, tls_enabled/1,
|
2020-09-21 22:18:46 +02:00
|
|
|
connect_options/3, connect_timeout/1, address_families/1, default_port/1,
|
2017-01-09 15:02:17 +01:00
|
|
|
dns_retries/1, dns_timeout/1,
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_auth_success/2, handle_auth_failure/3, handle_packet/2,
|
2016-12-29 22:00:36 +01:00
|
|
|
handle_stream_end/2, handle_stream_downgraded/2,
|
2017-01-09 15:02:17 +01:00
|
|
|
handle_recv/3, handle_send/3, handle_cdata/2,
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_stream_established/1, handle_timeout/1]).
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
%% Hooks
|
|
|
|
-export([process_auth_result/2, process_closed/2, handle_unexpected_info/2,
|
2016-12-29 22:00:36 +01:00
|
|
|
handle_unexpected_cast/2, process_downgraded/2]).
|
2016-12-28 07:47:11 +01:00
|
|
|
%% API
|
2020-04-01 14:35:49 +02:00
|
|
|
-export([start/3, start_link/3, connect/1, close/1, close/2, stop_async/1, send/2,
|
2017-02-23 08:12:19 +01:00
|
|
|
route/2, establish/1, update_state/2, host_up/1, host_down/1]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-12-28 07:47:11 +01:00
|
|
|
-include("logger.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2019-06-22 09:19:17 +02:00
|
|
|
-type state() :: xmpp_stream_out:state().
|
2016-12-28 07:47:11 +01:00
|
|
|
-export_type([state/0]).
|
2008-12-26 12:45:08 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
2002-12-06 21:59:19 +01:00
|
|
|
%%% API
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
|
|
|
start(From, To, Opts) ->
|
2017-01-11 08:56:04 +01:00
|
|
|
case proplists:get_value(supervisor, Opts, true) of
|
2017-06-22 15:58:46 +02:00
|
|
|
true ->
|
|
|
|
case supervisor:start_child(ejabberd_s2s_out_sup,
|
|
|
|
[From, To, Opts]) of
|
|
|
|
{ok, undefined} -> ignore;
|
|
|
|
Res -> Res
|
|
|
|
end;
|
|
|
|
_ ->
|
2018-06-25 14:28:02 +02:00
|
|
|
xmpp_stream_out:start(?MODULE, [From, To, Opts],
|
2017-01-11 08:56:04 +01:00
|
|
|
ejabberd_config:fsm_limit_opts([]))
|
|
|
|
end.
|
2005-10-25 03:08:37 +02:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
start_link(From, To, Opts) ->
|
2018-06-25 14:28:02 +02:00
|
|
|
xmpp_stream_out:start_link(?MODULE, [From, To, Opts],
|
2016-12-28 07:47:11 +01:00
|
|
|
ejabberd_config:fsm_limit_opts([])).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec connect(pid()) -> ok.
|
2016-12-28 07:47:11 +01:00
|
|
|
connect(Ref) ->
|
|
|
|
xmpp_stream_out:connect(Ref).
|
|
|
|
|
2017-02-18 07:36:27 +01:00
|
|
|
-spec close(pid()) -> ok;
|
|
|
|
(state()) -> state().
|
2016-12-28 07:47:11 +01:00
|
|
|
close(Ref) ->
|
|
|
|
xmpp_stream_out:close(Ref).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec close(pid(), atom()) -> ok.
|
2017-04-15 07:30:41 +02:00
|
|
|
close(Ref, Reason) ->
|
|
|
|
xmpp_stream_out:close(Ref, Reason).
|
|
|
|
|
2020-04-01 14:35:49 +02:00
|
|
|
-spec stop_async(pid()) -> ok.
|
|
|
|
stop_async(Pid) ->
|
|
|
|
xmpp_stream_out:stop_async(Pid).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec send(pid(), xmpp_element()) -> ok;
|
|
|
|
(state(), xmpp_element()) -> state().
|
|
|
|
send(Stream, Pkt) ->
|
|
|
|
xmpp_stream_out:send(Stream, Pkt).
|
|
|
|
|
|
|
|
-spec route(pid(), xmpp_element()) -> ok.
|
|
|
|
route(Ref, Pkt) ->
|
2017-02-18 07:36:27 +01:00
|
|
|
Ref ! {route, Pkt},
|
|
|
|
ok.
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec establish(state()) -> state().
|
|
|
|
establish(State) ->
|
|
|
|
xmpp_stream_out:establish(State).
|
|
|
|
|
|
|
|
-spec update_state(pid(), fun((state()) -> state()) |
|
|
|
|
{module(), atom(), list()}) -> ok.
|
|
|
|
update_state(Ref, Callback) ->
|
|
|
|
xmpp_stream_out:cast(Ref, {update_state, Callback}).
|
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec host_up(binary()) -> ok.
|
|
|
|
host_up(Host) ->
|
|
|
|
ejabberd_hooks:add(s2s_out_auth_result, Host, ?MODULE,
|
|
|
|
process_auth_result, 100),
|
|
|
|
ejabberd_hooks:add(s2s_out_closed, Host, ?MODULE,
|
|
|
|
process_closed, 100),
|
|
|
|
ejabberd_hooks:add(s2s_out_handle_info, Host, ?MODULE,
|
|
|
|
handle_unexpected_info, 100),
|
|
|
|
ejabberd_hooks:add(s2s_out_handle_cast, Host, ?MODULE,
|
|
|
|
handle_unexpected_cast, 100),
|
|
|
|
ejabberd_hooks:add(s2s_out_downgraded, Host, ?MODULE,
|
|
|
|
process_downgraded, 100).
|
|
|
|
|
|
|
|
-spec host_down(binary()) -> ok.
|
|
|
|
host_down(Host) ->
|
|
|
|
ejabberd_hooks:delete(s2s_out_auth_result, Host, ?MODULE,
|
|
|
|
process_auth_result, 100),
|
|
|
|
ejabberd_hooks:delete(s2s_out_closed, Host, ?MODULE,
|
|
|
|
process_closed, 100),
|
|
|
|
ejabberd_hooks:delete(s2s_out_handle_info, Host, ?MODULE,
|
|
|
|
handle_unexpected_info, 100),
|
|
|
|
ejabberd_hooks:delete(s2s_out_handle_cast, Host, ?MODULE,
|
|
|
|
handle_unexpected_cast, 100),
|
|
|
|
ejabberd_hooks:delete(s2s_out_downgraded, Host, ?MODULE,
|
|
|
|
process_downgraded, 100).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Hooks
|
|
|
|
%%%===================================================================
|
|
|
|
process_auth_result(#{server := LServer, remote_server := RServer} = State,
|
2017-01-09 15:02:17 +01:00
|
|
|
{false, Reason}) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
Delay = get_delay(),
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to establish outbound s2s connection ~ts -> ~ts: "
|
2018-09-19 22:12:14 +02:00
|
|
|
"authentication failed; bouncing for ~p seconds",
|
2019-07-17 21:15:56 +02:00
|
|
|
[LServer, RServer, Delay div 1000]),
|
2017-01-09 15:02:17 +01:00
|
|
|
State1 = State#{on_route => bounce, stop_reason => Reason},
|
2016-12-29 22:00:36 +01:00
|
|
|
State2 = close(State1),
|
|
|
|
State3 = bounce_queue(State2),
|
2019-07-17 21:15:56 +02:00
|
|
|
xmpp_stream_out:set_timeout(State3, Delay);
|
2016-12-28 07:47:11 +01:00
|
|
|
process_auth_result(State, true) ->
|
|
|
|
State.
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
process_closed(#{server := LServer, remote_server := RServer,
|
|
|
|
on_route := send} = State,
|
|
|
|
Reason) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Closing outbound s2s connection ~ts -> ~ts: ~ts",
|
2017-03-10 18:21:04 +01:00
|
|
|
[LServer, RServer, format_error(Reason)]),
|
2020-04-01 14:35:49 +02:00
|
|
|
stop_async(self()),
|
|
|
|
State;
|
2016-12-28 07:47:11 +01:00
|
|
|
process_closed(#{server := LServer, remote_server := RServer} = State,
|
2016-12-29 22:00:36 +01:00
|
|
|
Reason) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
Delay = get_delay(),
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("Failed to establish outbound s2s connection ~ts -> ~ts: ~ts; "
|
2018-09-19 22:12:14 +02:00
|
|
|
"bouncing for ~p seconds",
|
2019-07-17 21:15:56 +02:00
|
|
|
[LServer, RServer, format_error(Reason), Delay div 1000]),
|
2016-12-29 22:00:36 +01:00
|
|
|
State1 = State#{on_route => bounce},
|
|
|
|
State2 = bounce_queue(State1),
|
2019-07-17 21:15:56 +02:00
|
|
|
xmpp_stream_out:set_timeout(State2, Delay).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
handle_unexpected_info(State, Info) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2016-12-28 07:47:11 +01:00
|
|
|
State.
|
|
|
|
|
|
|
|
handle_unexpected_cast(State, Msg) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2016-12-28 07:47:11 +01:00
|
|
|
State.
|
|
|
|
|
2016-12-29 22:00:36 +01:00
|
|
|
process_downgraded(State, _StreamStart) ->
|
|
|
|
send(State, xmpp:serr_unsupported_version()).
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
2017-02-14 08:25:08 +01:00
|
|
|
%%% xmpp_stream_out callbacks
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
2019-09-06 21:20:07 +02:00
|
|
|
tls_options(#{server := LServer, server_host := ServerHost}) ->
|
|
|
|
ejabberd_s2s:tls_options(LServer, ServerHost, []).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
tls_required(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_s2s:tls_required(ServerHost).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
tls_verify(#{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_tls_verify, ServerHost, true, [State]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
tls_enabled(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_s2s:tls_enabled(ServerHost).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2020-09-21 22:18:46 +02:00
|
|
|
connect_options(Addr, Opts, #{server_host := ServerHost}) ->
|
2020-11-04 13:12:06 +01:00
|
|
|
BindAddr = case get_addr_type(Addr) of
|
|
|
|
inet ->
|
|
|
|
ejabberd_option:outgoing_s2s_ipv4_address(ServerHost);
|
|
|
|
inet6 ->
|
|
|
|
ejabberd_option:outgoing_s2s_ipv6_address(ServerHost)
|
|
|
|
end,
|
|
|
|
case BindAddr of
|
|
|
|
undefined ->
|
|
|
|
Opts;
|
|
|
|
_ ->
|
|
|
|
[{ip, BindAddr} | Opts]
|
2020-09-21 22:18:46 +02:00
|
|
|
end.
|
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
connect_timeout(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_option:outgoing_s2s_timeout(ServerHost).
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
default_port(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_option:outgoing_s2s_port(ServerHost).
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
address_families(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_option:outgoing_s2s_families(ServerHost).
|
2010-10-11 10:40:57 +02:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
dns_retries(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_option:s2s_dns_retries(ServerHost).
|
2016-07-27 09:45:08 +02:00
|
|
|
|
2019-06-23 18:37:54 +02:00
|
|
|
dns_timeout(#{server_host := ServerHost}) ->
|
|
|
|
ejabberd_option:s2s_dns_timeout(ServerHost).
|
2017-01-09 15:02:17 +01:00
|
|
|
|
2017-12-26 16:55:57 +01:00
|
|
|
handle_auth_success(Mech, #{socket := Socket, ip := IP,
|
2016-12-28 07:47:11 +01:00
|
|
|
remote_server := RServer,
|
2017-02-22 17:45:12 +01:00
|
|
|
server_host := ServerHost,
|
2016-12-28 07:47:11 +01:00
|
|
|
server := LServer} = State) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("(~ts) Accepted outbound s2s ~ts authentication ~ts -> ~ts (~ts)",
|
2017-12-26 16:55:57 +01:00
|
|
|
[xmpp_socket:pp(Socket), Mech, LServer, RServer,
|
2017-04-11 12:13:58 +02:00
|
|
|
ejabberd_config:may_hide_data(misc:ip_to_list(IP))]),
|
2017-02-22 17:45:12 +01:00
|
|
|
ejabberd_hooks:run_fold(s2s_out_auth_result, ServerHost, State, [true]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
handle_auth_failure(Mech, Reason,
|
2017-12-26 16:55:57 +01:00
|
|
|
#{socket := Socket, ip := IP,
|
2016-12-28 07:47:11 +01:00
|
|
|
remote_server := RServer,
|
2017-02-22 17:45:12 +01:00
|
|
|
server_host := ServerHost,
|
2016-12-28 07:47:11 +01:00
|
|
|
server := LServer} = State) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?WARNING_MSG("(~ts) Failed outbound s2s ~ts authentication ~ts -> ~ts (~ts): ~ts",
|
2018-09-19 22:12:14 +02:00
|
|
|
[xmpp_socket:pp(Socket), Mech, LServer, RServer,
|
|
|
|
ejabberd_config:may_hide_data(misc:ip_to_list(IP)),
|
|
|
|
xmpp_stream_out:format_error(Reason)]),
|
2017-02-22 17:45:12 +01:00
|
|
|
ejabberd_hooks:run_fold(s2s_out_auth_result, ServerHost, State, [{false, Reason}]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_packet(Pkt, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_packet, ServerHost, State, [Pkt]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_stream_end(Reason, #{server_host := ServerHost} = State) ->
|
2016-12-29 22:00:36 +01:00
|
|
|
State1 = State#{stop_reason => Reason},
|
2017-02-22 17:45:12 +01:00
|
|
|
ejabberd_hooks:run_fold(s2s_out_closed, ServerHost, State1, [Reason]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_stream_downgraded(StreamStart, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_downgraded, ServerHost, State, [StreamStart]).
|
2016-12-29 22:00:36 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
handle_stream_established(State) ->
|
|
|
|
State1 = State#{on_route => send},
|
|
|
|
State2 = resend_queue(State1),
|
|
|
|
set_idle_timeout(State2).
|
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_cdata(Data, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_cdata, ServerHost, State, [Data]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_recv(El, Pkt, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_recv, ServerHost, State, [El, Pkt]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_send(El, Pkt, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_send, ServerHost, State, [El, Pkt]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2018-01-28 09:10:22 +01:00
|
|
|
handle_timeout(#{on_route := Action, lang := Lang} = State) ->
|
2016-12-28 07:47:11 +01:00
|
|
|
case Action of
|
2020-04-01 14:35:49 +02:00
|
|
|
bounce ->
|
|
|
|
stop_async(self()),
|
|
|
|
State;
|
2018-01-28 09:10:22 +01:00
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Idle connection"),
|
2018-01-28 09:10:22 +01:00
|
|
|
send(State, xmpp:serr_connection_timeout(Txt, Lang))
|
2016-07-27 09:45:08 +02:00
|
|
|
end.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
init([#{server := LServer, remote_server := RServer} = State, Opts]) ->
|
2017-02-22 17:45:12 +01:00
|
|
|
ServerHost = ejabberd_router:host_of_route(LServer),
|
2019-06-23 18:37:54 +02:00
|
|
|
QueueType = ejabberd_s2s:queue_type(ServerHost),
|
2017-03-10 13:12:43 +01:00
|
|
|
QueueLimit = case lists:keyfind(
|
|
|
|
max_queue, 1, ejabberd_config:fsm_limit_opts([])) of
|
|
|
|
{_, N} -> N;
|
|
|
|
false -> unlimited
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
Timeout = ejabberd_option:negotiation_timeout(),
|
2016-12-28 07:47:11 +01:00
|
|
|
State1 = State#{on_route => queue,
|
2017-03-10 13:12:43 +01:00
|
|
|
queue => p1_queue:new(QueueType, QueueLimit),
|
2016-12-28 07:47:11 +01:00
|
|
|
xmlns => ?NS_SERVER,
|
2019-06-14 11:33:26 +02:00
|
|
|
lang => ejabberd_option:language(),
|
2017-02-22 17:45:12 +01:00
|
|
|
server_host => ServerHost,
|
2016-12-28 07:47:11 +01:00
|
|
|
shaper => none},
|
2018-02-20 09:38:00 +01:00
|
|
|
State2 = xmpp_stream_out:set_timeout(State1, Timeout),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Outbound s2s connection started: ~ts -> ~ts",
|
2016-12-28 07:47:11 +01:00
|
|
|
[LServer, RServer]),
|
2018-02-20 09:38:00 +01:00
|
|
|
ejabberd_hooks:run_fold(s2s_out_init, ServerHost, {ok, State2}, [Opts]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_call(Request, From, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_call, ServerHost, State, [Request, From]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
handle_cast({update_state, Fun}, State) ->
|
|
|
|
case Fun of
|
|
|
|
{M, F, A} -> erlang:apply(M, F, [State|A]);
|
|
|
|
_ when is_function(Fun) -> Fun(State)
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_cast(Msg, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_cast, ServerHost, State, [Msg]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
handle_info({route, Pkt}, #{queue := Q, on_route := Action} = State) ->
|
|
|
|
case Action of
|
2017-03-10 13:12:43 +01:00
|
|
|
queue ->
|
|
|
|
try State#{queue => p1_queue:in(Pkt, Q)}
|
|
|
|
catch error:full ->
|
2017-03-10 18:21:04 +01:00
|
|
|
Q1 = p1_queue:set_limit(Q, unlimited),
|
|
|
|
Q2 = p1_queue:in(Pkt, Q1),
|
|
|
|
handle_stream_end(queue_full, State#{queue => Q2})
|
2017-03-10 13:12:43 +01:00
|
|
|
end;
|
2016-12-28 07:47:11 +01:00
|
|
|
bounce -> bounce_packet(Pkt, State);
|
|
|
|
send -> set_idle_timeout(send(State, Pkt))
|
2002-12-07 21:27:26 +01:00
|
|
|
end;
|
2017-02-22 17:45:12 +01:00
|
|
|
handle_info(Info, #{server_host := ServerHost} = State) ->
|
|
|
|
ejabberd_hooks:run_fold(s2s_out_handle_info, ServerHost, State, [Info]).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
terminate(Reason, #{server := LServer,
|
|
|
|
remote_server := RServer} = State) ->
|
|
|
|
State1 = case Reason of
|
|
|
|
normal -> State;
|
2016-12-29 22:00:36 +01:00
|
|
|
_ -> State#{stop_reason => internal_failure}
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
State2 = bounce_queue(State1),
|
2019-06-26 10:56:25 +02:00
|
|
|
bounce_message_queue({LServer, RServer}, State2).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2010-04-09 19:25:00 +02:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
2002-12-06 21:59:19 +01:00
|
|
|
%%% Internal functions
|
2016-12-28 07:47:11 +01:00
|
|
|
%%%===================================================================
|
2020-09-21 22:18:46 +02:00
|
|
|
-spec get_addr_type(inet:ip_address()) -> inet:address_family().
|
|
|
|
get_addr_type({_, _, _, _}) -> inet;
|
|
|
|
get_addr_type({_, _, _, _, _, _, _, _}) -> inet6.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec resend_queue(state()) -> state().
|
2017-03-10 13:12:43 +01:00
|
|
|
resend_queue(State) ->
|
|
|
|
queue_fold(
|
2016-12-28 07:47:11 +01:00
|
|
|
fun(Pkt, AccState) ->
|
|
|
|
send(AccState, Pkt)
|
2017-03-10 13:12:43 +01:00
|
|
|
end, State).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec bounce_queue(state()) -> state().
|
2017-03-10 13:12:43 +01:00
|
|
|
bounce_queue(State) ->
|
|
|
|
queue_fold(
|
2016-12-28 07:47:11 +01:00
|
|
|
fun(Pkt, AccState) ->
|
|
|
|
bounce_packet(Pkt, AccState)
|
2017-03-10 13:12:43 +01:00
|
|
|
end, State).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
2019-06-26 10:56:25 +02:00
|
|
|
-spec bounce_message_queue({binary(), binary()}, state()) -> state().
|
2021-11-16 10:25:03 +01:00
|
|
|
bounce_message_queue(FromTo, State) ->
|
|
|
|
receive {route, Pkt} ->
|
|
|
|
State1 = bounce_packet(Pkt, State),
|
|
|
|
bounce_message_queue(FromTo, State1)
|
|
|
|
after 0 ->
|
|
|
|
State
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec bounce_packet(xmpp_element(), state()) -> state().
|
|
|
|
bounce_packet(Pkt, State) when ?is_stanza(Pkt) ->
|
|
|
|
Lang = xmpp:get_lang(Pkt),
|
|
|
|
Err = mk_bounce_error(Lang, State),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Pkt, Err),
|
2016-12-28 07:47:11 +01:00
|
|
|
State;
|
|
|
|
bounce_packet(_, State) ->
|
|
|
|
State.
|
|
|
|
|
|
|
|
-spec mk_bounce_error(binary(), state()) -> stanza_error().
|
2016-12-29 22:00:36 +01:00
|
|
|
mk_bounce_error(Lang, #{stop_reason := Why}) ->
|
2017-03-10 18:21:04 +01:00
|
|
|
Reason = format_error(Why),
|
2016-12-29 22:00:36 +01:00
|
|
|
case Why of
|
|
|
|
internal_failure ->
|
2017-03-10 18:21:04 +01:00
|
|
|
xmpp:err_internal_server_error(Reason, Lang);
|
2017-03-10 13:12:43 +01:00
|
|
|
queue_full ->
|
2017-03-10 18:21:04 +01:00
|
|
|
xmpp:err_resource_constraint(Reason, Lang);
|
2016-12-29 22:00:36 +01:00
|
|
|
{dns, _} ->
|
|
|
|
xmpp:err_remote_server_not_found(Reason, Lang);
|
2019-07-29 21:45:38 +02:00
|
|
|
{idna, _} ->
|
|
|
|
xmpp:err_remote_server_not_found(Reason, Lang);
|
|
|
|
_ ->
|
2016-12-29 22:00:36 +01:00
|
|
|
xmpp:err_remote_server_timeout(Reason, Lang)
|
2019-07-29 21:45:38 +02:00
|
|
|
end;
|
2016-12-29 22:00:36 +01:00
|
|
|
mk_bounce_error(_Lang, _State) ->
|
|
|
|
%% We should not be here. Probably :)
|
|
|
|
xmpp:err_remote_server_not_found().
|
2008-12-26 12:45:08 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
-spec get_delay() -> non_neg_integer().
|
|
|
|
get_delay() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxDelay = ejabberd_option:s2s_max_retry_delay(),
|
2018-07-05 10:51:49 +02:00
|
|
|
p1_rand:uniform(MaxDelay).
|
2016-12-28 07:47:11 +01:00
|
|
|
|
|
|
|
-spec set_idle_timeout(state()) -> state().
|
2019-06-23 18:37:54 +02:00
|
|
|
set_idle_timeout(#{on_route := send, server_host := ServerHost} = State) ->
|
|
|
|
Timeout = ejabberd_s2s:get_idle_timeout(ServerHost),
|
2016-12-28 07:47:11 +01:00
|
|
|
xmpp_stream_out:set_timeout(State, Timeout);
|
|
|
|
set_idle_timeout(State) ->
|
|
|
|
State.
|
2008-12-26 12:45:08 +01:00
|
|
|
|
2017-03-10 18:21:04 +01:00
|
|
|
-spec queue_fold(fun((xmpp_element(), state()) -> state()), state()) -> state().
|
2017-03-10 13:12:43 +01:00
|
|
|
queue_fold(F, #{queue := Q} = State) ->
|
|
|
|
case p1_queue:out(Q) of
|
|
|
|
{{value, Pkt}, Q1} ->
|
|
|
|
State1 = F(Pkt, State#{queue => Q1}),
|
|
|
|
queue_fold(F, State1);
|
|
|
|
{empty, Q1} ->
|
|
|
|
State#{queue => Q1}
|
|
|
|
end.
|
|
|
|
|
2017-03-10 18:21:04 +01:00
|
|
|
format_error(internal_failure) ->
|
|
|
|
<<"Internal server error">>;
|
|
|
|
format_error(queue_full) ->
|
|
|
|
<<"Stream queue is overloaded">>;
|
|
|
|
format_error(Reason) ->
|
|
|
|
xmpp_stream_out:format_error(Reason).
|