2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s_out.erl
|
2007-07-24 11:04:09 +02:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Manage outgoing server-to-server connections
|
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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
|
|
|
%%%
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s_out).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-07-24 11:04:09 +02:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2007-09-14 16:21:25 +02:00
|
|
|
-behaviour(p1_fsm).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%% External exports
|
2005-11-12 04:42:12 +01:00
|
|
|
-export([start/3,
|
|
|
|
start_link/3,
|
2007-07-18 18:26:50 +02:00
|
|
|
start_connection/1,
|
2008-04-18 14:19:45 +02:00
|
|
|
terminate_if_waiting_delay/2,
|
2013-08-12 14:25:05 +02:00
|
|
|
stop_connection/1,
|
2015-10-07 00:06:58 +02:00
|
|
|
transform_options/1]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([init/1, open_socket/2, wait_for_stream/2,
|
|
|
|
wait_for_validation/2, wait_for_features/2,
|
|
|
|
wait_for_auth_result/2, wait_for_starttls_proceed/2,
|
|
|
|
relay_to_bridge/2, reopen_socket/2, wait_before_retry/2,
|
|
|
|
stream_established/2, handle_event/3,
|
|
|
|
handle_sync_event/4, handle_info/3, terminate/3,
|
|
|
|
print_state/1, code_change/4, test_get_addr_port/1,
|
|
|
|
get_addr_port/1, opt_type/1]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-27 09:45:08 +02:00
|
|
|
-include("xmpp.hrl").
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{socket :: ejabberd_socket:socket_state(),
|
|
|
|
streamid = <<"">> :: binary(),
|
2016-01-11 12:22:17 +01:00
|
|
|
remote_streamid = <<"">> :: binary(),
|
2013-03-14 10:33:02 +01:00
|
|
|
use_v10 = true :: boolean(),
|
|
|
|
tls = false :: boolean(),
|
|
|
|
tls_required = false :: boolean(),
|
2014-04-28 01:42:02 +02:00
|
|
|
tls_certverify = false :: boolean(),
|
2013-03-14 10:33:02 +01:00
|
|
|
tls_enabled = false :: boolean(),
|
|
|
|
tls_options = [connect] :: list(),
|
|
|
|
authenticated = false :: boolean(),
|
|
|
|
db_enabled = true :: boolean(),
|
|
|
|
try_auth = true :: boolean(),
|
|
|
|
myname = <<"">> :: binary(),
|
|
|
|
server = <<"">> :: binary(),
|
2015-02-11 12:08:16 +01:00
|
|
|
queue = queue:new() :: ?TQUEUE,
|
2013-03-14 10:33:02 +01:00
|
|
|
delay_to_retry = undefined_delay :: undefined_delay | non_neg_integer(),
|
2016-01-11 12:22:17 +01:00
|
|
|
new = false :: boolean(),
|
2013-03-14 10:33:02 +01:00
|
|
|
verify = false :: false | {pid(), binary(), binary()},
|
|
|
|
bridge :: {atom(), atom()},
|
|
|
|
timer = make_ref() :: reference()}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-type state_name() :: open_socket | wait_for_stream |
|
|
|
|
wait_for_validation | wait_for_features |
|
|
|
|
wait_for_auth_result | wait_for_starttls_proceed |
|
|
|
|
relay_to_bridge | reopen_socket | wait_before_retry |
|
|
|
|
stream_established.
|
|
|
|
-type state() :: #state{}.
|
|
|
|
-type fsm_stop() :: {stop, normal, state()}.
|
|
|
|
-type fsm_next() :: {next_state, state_name(), state(), non_neg_integer()} |
|
|
|
|
{next_state, state_name(), state()}.
|
|
|
|
-type fsm_transition() :: fsm_stop() | fsm_next().
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
%%-define(DBGFSM, true).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-endif.
|
|
|
|
|
2008-05-16 17:29:33 +02:00
|
|
|
-define(FSMTIMEOUT, 30000).
|
2007-07-24 11:04:09 +02:00
|
|
|
|
2009-01-08 21:49:16 +01:00
|
|
|
%% We do not block on send anymore.
|
|
|
|
-define(TCP_SEND_TIMEOUT, 15000).
|
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
%% Maximum delay to wait before retrying to connect after a failed attempt.
|
|
|
|
%% Specified in miliseconds. Default value is 5 minutes.
|
|
|
|
-define(MAX_RETRY_DELAY, 300000).
|
|
|
|
|
2008-12-26 12:45:08 +01:00
|
|
|
-define(SOCKET_DEFAULT_RESULT, {error, badarg}).
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(From, Host, Type) ->
|
2015-11-04 16:24:35 +01:00
|
|
|
supervisor:start_child(ejabberd_s2s_out_sup,
|
|
|
|
[From, Host, Type]).
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(From, Host, Type) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:start_link(ejabberd_s2s_out, [From, Host, Type],
|
2013-03-14 10:33:02 +01:00
|
|
|
fsm_limit_opts() ++ (?FSMOPTS)).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
start_connection(Pid) -> p1_fsm:send_event(Pid, init).
|
2005-11-12 04:42:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
stop_connection(Pid) -> p1_fsm:send_event(Pid, closed).
|
2007-07-18 18:26:50 +02:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2007-09-14 16:21:25 +02:00
|
|
|
%%% Callback functions from p1_fsm
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-01-04 21:09:25 +01:00
|
|
|
init([From, Server, Type]) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
process_flag(trap_exit, true),
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("started: ~p", [{From, Server, Type}]),
|
2014-04-28 01:42:02 +02:00
|
|
|
{TLS, TLSRequired, TLSCertverify} =
|
|
|
|
case ejabberd_config:get_option(
|
|
|
|
s2s_use_starttls,
|
|
|
|
fun(true) -> true;
|
|
|
|
(false) -> false;
|
|
|
|
(optional) -> optional;
|
|
|
|
(required) -> required;
|
|
|
|
(required_trusted) -> required_trusted
|
|
|
|
end)
|
|
|
|
of
|
|
|
|
UseTls
|
|
|
|
when (UseTls == undefined) or (UseTls == false) ->
|
|
|
|
{false, false, false};
|
|
|
|
UseTls
|
|
|
|
when (UseTls == true) or (UseTls == optional) ->
|
|
|
|
{true, false, false};
|
|
|
|
required ->
|
|
|
|
{true, true, false};
|
|
|
|
required_trusted ->
|
|
|
|
{true, true, true}
|
|
|
|
end,
|
2005-10-25 03:08:37 +02:00
|
|
|
UseV10 = TLS,
|
2013-07-17 14:28:23 +02:00
|
|
|
TLSOpts1 = case
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
s2s_certfile, fun iolist_to_binary/1)
|
|
|
|
of
|
|
|
|
undefined -> [connect];
|
|
|
|
CertFile -> [{certfile, CertFile}, connect]
|
2005-10-25 03:08:37 +02:00
|
|
|
end,
|
2013-11-28 18:39:11 +01:00
|
|
|
TLSOpts2 = case ejabberd_config:get_option(
|
|
|
|
s2s_ciphers, fun iolist_to_binary/1) of
|
|
|
|
undefined -> TLSOpts1;
|
|
|
|
Ciphers -> [{ciphers, Ciphers} | TLSOpts1]
|
|
|
|
end,
|
2014-04-01 21:57:33 +02:00
|
|
|
TLSOpts3 = case ejabberd_config:get_option(
|
|
|
|
s2s_protocol_options,
|
|
|
|
fun (Options) ->
|
|
|
|
[_|O] = lists:foldl(
|
|
|
|
fun(X, Acc) -> X ++ Acc end, [],
|
|
|
|
[["|" | binary_to_list(Opt)] || Opt <- Options, is_binary(Opt)]
|
|
|
|
),
|
|
|
|
iolist_to_binary(O)
|
|
|
|
end) of
|
|
|
|
undefined -> TLSOpts2;
|
|
|
|
ProtocolOpts -> [{protocol_options, ProtocolOpts} | TLSOpts2]
|
|
|
|
end,
|
2015-05-26 21:06:04 +02:00
|
|
|
TLSOpts4 = case ejabberd_config:get_option(
|
|
|
|
s2s_dhfile, fun iolist_to_binary/1) of
|
|
|
|
undefined -> TLSOpts3;
|
|
|
|
DHFile -> [{dhfile, DHFile} | TLSOpts3]
|
|
|
|
end,
|
2013-08-12 14:25:05 +02:00
|
|
|
TLSOpts = case ejabberd_config:get_option(
|
2013-07-17 14:28:23 +02:00
|
|
|
{s2s_tls_compression, From},
|
|
|
|
fun(true) -> true;
|
|
|
|
(false) -> false
|
2016-04-11 22:50:11 +02:00
|
|
|
end, false) of
|
2015-05-26 21:06:04 +02:00
|
|
|
false -> [compression_none | TLSOpts4];
|
|
|
|
true -> TLSOpts4
|
2013-07-17 14:28:23 +02:00
|
|
|
end,
|
2002-12-13 21:58:27 +01:00
|
|
|
{New, Verify} = case Type of
|
2016-01-11 12:22:17 +01:00
|
|
|
new -> {true, false};
|
2013-03-14 10:33:02 +01:00
|
|
|
{verify, Pid, Key, SID} ->
|
|
|
|
start_connection(self()), {false, {Pid, Key, SID}}
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, open_socket,
|
|
|
|
#state{use_v10 = UseV10, tls = TLS,
|
2014-04-28 01:42:02 +02:00
|
|
|
tls_required = TLSRequired, tls_certverify = TLSCertverify,
|
|
|
|
tls_options = TLSOpts, queue = queue:new(), myname = From,
|
|
|
|
server = Server, new = New, verify = Verify, timer = Timer}}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
open_socket(init, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
log_s2s_out(StateData#state.new, StateData#state.myname,
|
|
|
|
StateData#state.server, StateData#state.tls),
|
|
|
|
?DEBUG("open_socket: ~p",
|
|
|
|
[{StateData#state.myname, StateData#state.server,
|
|
|
|
StateData#state.new, StateData#state.verify}]),
|
|
|
|
AddrList = case
|
2015-09-02 15:02:46 +02:00
|
|
|
ejabberd_idna:domain_utf8_to_ascii(StateData#state.server)
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
false -> [];
|
|
|
|
ASCIIAddr -> get_addr_port(ASCIIAddr)
|
2006-12-31 01:38:11 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:foldl(fun ({Addr, Port}, Acc) ->
|
2007-12-07 00:15:04 +01:00
|
|
|
case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Socket} -> {ok, Socket};
|
|
|
|
_ -> open_socket1(Addr, Port)
|
2007-12-07 00:15:04 +01:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
?SOCKET_DEFAULT_RESULT, AddrList)
|
|
|
|
of
|
|
|
|
{ok, Socket} ->
|
2016-09-23 11:30:33 +02:00
|
|
|
Version = if StateData#state.use_v10 -> {1,0};
|
|
|
|
true -> undefined
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
NewStateData = StateData#state{socket = Socket,
|
|
|
|
tls_enabled = false,
|
|
|
|
streamid = new_id()},
|
2016-07-27 09:45:08 +02:00
|
|
|
send_header(NewStateData, Version),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, wait_for_stream, NewStateData,
|
|
|
|
?FSMTIMEOUT};
|
2016-11-21 09:52:29 +01:00
|
|
|
{error, Reason} ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("s2s connection: ~s -> ~s (remote server "
|
2016-11-21 09:52:29 +01:00
|
|
|
"not found: ~p)",
|
|
|
|
[StateData#state.myname, StateData#state.server, Reason]),
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_hooks:run_fold(find_s2s_bridge, undefined,
|
|
|
|
[StateData#state.myname,
|
|
|
|
StateData#state.server])
|
|
|
|
of
|
|
|
|
{Mod, Fun, Type} ->
|
|
|
|
?INFO_MSG("found a bridge to ~s for: ~s -> ~s",
|
|
|
|
[Type, StateData#state.myname,
|
|
|
|
StateData#state.server]),
|
|
|
|
NewStateData = StateData#state{bridge = {Mod, Fun}},
|
|
|
|
{next_state, relay_to_bridge, NewStateData};
|
|
|
|
_ -> wait_before_reconnect(StateData)
|
|
|
|
end
|
2006-03-14 05:26:15 +01:00
|
|
|
end;
|
2016-07-27 09:45:08 +02:00
|
|
|
open_socket(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, open_socket, StateData).
|
2006-03-14 05:26:15 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
open_socket1({_, _, _, _} = Addr, Port) ->
|
2008-12-26 12:45:08 +01:00
|
|
|
open_socket2(inet, Addr, Port);
|
|
|
|
%% IPv6
|
2013-03-14 10:33:02 +01:00
|
|
|
open_socket1({_, _, _, _, _, _, _, _} = Addr, Port) ->
|
2008-12-26 12:45:08 +01:00
|
|
|
open_socket2(inet6, Addr, Port);
|
|
|
|
%% Hostname
|
|
|
|
open_socket1(Host, Port) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (_Family, {ok, _Socket} = R) -> R;
|
|
|
|
(Family, _) ->
|
2008-12-26 12:45:08 +01:00
|
|
|
Addrs = get_addrs(Host, Family),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (_Addr, {ok, _Socket} = R) -> R;
|
|
|
|
(Addr, _) -> open_socket1(Addr, Port)
|
|
|
|
end,
|
|
|
|
?SOCKET_DEFAULT_RESULT, Addrs)
|
|
|
|
end,
|
|
|
|
?SOCKET_DEFAULT_RESULT, outgoing_s2s_families()).
|
2008-12-26 12:45:08 +01:00
|
|
|
|
|
|
|
open_socket2(Type, Addr, Port) ->
|
|
|
|
?DEBUG("s2s_out: connecting to ~p:~p~n", [Addr, Port]),
|
|
|
|
Timeout = outgoing_s2s_timeout(),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch ejabberd_socket:connect(Addr, Port,
|
|
|
|
[binary, {packet, 0},
|
|
|
|
{send_timeout, ?TCP_SEND_TIMEOUT},
|
2015-04-16 23:51:16 +02:00
|
|
|
{send_timeout_close, true},
|
|
|
|
{active, false}, Type],
|
2013-03-14 10:33:02 +01:00
|
|
|
Timeout)
|
|
|
|
of
|
|
|
|
{ok, _Socket} = R -> R;
|
|
|
|
{error, Reason} = R ->
|
|
|
|
?DEBUG("s2s_out: connect return ~p~n", [Reason]), R;
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?DEBUG("s2s_out: connect crashed ~p~n", [Reason]),
|
|
|
|
{error, Reason}
|
2006-03-14 05:26:15 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
wait_for_stream({xmlstreamstart, Name, Attrs}, StateData0) ->
|
|
|
|
{CertCheckRes, CertCheckMsg, StateData} =
|
|
|
|
if StateData0#state.tls_certverify, StateData0#state.tls_enabled ->
|
|
|
|
{Res, Msg} =
|
|
|
|
ejabberd_s2s:check_peer_certificate(ejabberd_socket,
|
|
|
|
StateData0#state.socket,
|
|
|
|
StateData0#state.server),
|
|
|
|
?DEBUG("Certificate verification result for ~s: ~s",
|
|
|
|
[StateData0#state.server, Msg]),
|
|
|
|
{Res, Msg, StateData0#state{tls_certverify = false}};
|
2014-04-28 01:42:02 +02:00
|
|
|
true ->
|
2016-07-27 09:45:08 +02:00
|
|
|
{no_verify, <<"Not verified">>, StateData0}
|
2014-04-28 01:42:02 +02:00
|
|
|
end,
|
2016-07-27 09:45:08 +02:00
|
|
|
try xmpp:decode(#xmlel{name = Name, attrs = Attrs}) of
|
|
|
|
_ when CertCheckRes == error ->
|
|
|
|
send_element(StateData,
|
|
|
|
xmpp:serr_policy_violation(CertCheckMsg, ?MYLANG)),
|
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (~s)",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
CertCheckMsg]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
#stream_start{xmlns = NS_SERVER, stream_xmlns = NS_STREAM}
|
|
|
|
when NS_SERVER /= ?NS_SERVER; NS_STREAM /= ?NS_STREAM ->
|
|
|
|
send_element(StateData, xmpp:serr_invalid_namespace()),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
#stream_start{db_xmlns = ?NS_SERVER_DIALBACK, id = ID,
|
2016-09-23 11:30:33 +02:00
|
|
|
version = V} when V /= {1,0} ->
|
2016-07-27 09:45:08 +02:00
|
|
|
send_db_request(StateData#state{remote_streamid = ID});
|
|
|
|
#stream_start{db_xmlns = ?NS_SERVER_DIALBACK, id = ID}
|
|
|
|
when StateData#state.use_v10 ->
|
|
|
|
{next_state, wait_for_features,
|
|
|
|
StateData#state{remote_streamid = ID}, ?FSMTIMEOUT};
|
|
|
|
#stream_start{db_xmlns = ?NS_SERVER_DIALBACK, id = ID}
|
|
|
|
when not StateData#state.use_v10 ->
|
|
|
|
%% Handle Tigase's workaround for an old ejabberd bug:
|
|
|
|
send_db_request(StateData#state{remote_streamid = ID});
|
|
|
|
#stream_start{id = ID} when StateData#state.use_v10 ->
|
|
|
|
{next_state, wait_for_features,
|
|
|
|
StateData#state{db_enabled = false, remote_streamid = ID},
|
|
|
|
?FSMTIMEOUT};
|
|
|
|
#stream_start{} ->
|
|
|
|
send_element(StateData, xmpp:serr_invalid_namespace()),
|
2016-09-23 11:30:33 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
_ ->
|
|
|
|
send_element(StateData, xmpp:serr_invalid_xml()),
|
|
|
|
{stop, normal, StateData}
|
2016-07-27 09:45:08 +02:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Txt = xmpp:format_error(Why),
|
2016-09-23 11:30:33 +02:00
|
|
|
send_element(StateData, xmpp:serr_invalid_xml(Txt, ?MYLANG)),
|
2016-07-27 09:45:08 +02:00
|
|
|
{stop, normal, StateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
end;
|
2016-07-27 09:45:08 +02:00
|
|
|
wait_for_stream(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, wait_for_stream, StateData).
|
|
|
|
|
|
|
|
wait_for_validation({xmlstreamelement, El}, StateData) ->
|
|
|
|
decode_element(El, wait_for_validation, StateData);
|
|
|
|
wait_for_validation(#db_result{to = To, from = From, type = Type}, StateData) ->
|
|
|
|
?DEBUG("recv result: ~p", [{From, To, Type}]),
|
|
|
|
case {Type, StateData#state.tls_enabled, StateData#state.tls_required} of
|
|
|
|
{valid, Enabled, Required} when (Enabled == true) or (Required == false) ->
|
|
|
|
send_queue(StateData, StateData#state.queue),
|
|
|
|
?INFO_MSG("Connection established: ~s -> ~s with "
|
|
|
|
"TLS=~p",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateData#state.tls_enabled]),
|
|
|
|
ejabberd_hooks:run(s2s_connect_hook,
|
|
|
|
[StateData#state.myname,
|
|
|
|
StateData#state.server]),
|
|
|
|
{next_state, stream_established, StateData#state{queue = queue:new()}};
|
|
|
|
{valid, Enabled, Required} when (Enabled == false) and (Required == true) ->
|
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (TLS "
|
|
|
|
"is required but unavailable)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (invalid "
|
|
|
|
"dialback key result)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
wait_for_validation(#db_verify{to = To, from = From, id = Id, type = Type},
|
2013-03-14 10:33:02 +01:00
|
|
|
StateData) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
?DEBUG("recv verify: ~p", [{From, To, Id, Type}]),
|
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
|
|
|
NextState = wait_for_validation,
|
|
|
|
{next_state, NextState, StateData, get_timeout_interval(NextState)};
|
|
|
|
{Pid, _Key, _SID} ->
|
|
|
|
case Type of
|
|
|
|
valid ->
|
|
|
|
p1_fsm:send_event(Pid,
|
|
|
|
{valid, StateData#state.server,
|
|
|
|
StateData#state.myname});
|
|
|
|
_ ->
|
|
|
|
p1_fsm:send_event(Pid,
|
|
|
|
{invalid, StateData#state.server,
|
|
|
|
StateData#state.myname})
|
|
|
|
end,
|
|
|
|
if StateData#state.verify == false ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
|
|
|
NextState = wait_for_validation,
|
|
|
|
{next_state, NextState, StateData, get_timeout_interval(NextState)}
|
|
|
|
end
|
2002-12-06 21:59:19 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_validation(timeout,
|
|
|
|
#state{verify = {VPid, VKey, SID}} = StateData)
|
2016-07-27 09:45:08 +02:00
|
|
|
when is_pid(VPid) and is_binary(VKey) and is_binary(SID) ->
|
|
|
|
?DEBUG("wait_for_validation: ~s -> ~s (timeout in verify connection)",
|
2008-04-29 18:59:03 +02:00
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
2016-07-27 09:45:08 +02:00
|
|
|
wait_for_validation(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, wait_for_validation, StateData).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_features({xmlstreamelement, El}, StateData) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
decode_element(El, wait_for_features, StateData);
|
|
|
|
wait_for_features(#stream_features{sub_els = Els}, StateData) ->
|
|
|
|
{SASLEXT, StartTLS, StartTLSRequired} =
|
|
|
|
lists:foldl(
|
|
|
|
fun(#sasl_mechanisms{list = Mechs}, {_, STLS, STLSReq}) ->
|
|
|
|
{lists:member(<<"EXTERNAL">>, Mechs), STLS, STLSReq};
|
|
|
|
(#starttls{required = Required}, {SEXT, _, _}) ->
|
|
|
|
{SEXT, true, Required};
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, {false, false, false}, Els),
|
|
|
|
if not SASLEXT and not StartTLS and StateData#state.authenticated ->
|
|
|
|
send_queue(StateData, StateData#state.queue),
|
|
|
|
?INFO_MSG("Connection established: ~s -> ~s with "
|
|
|
|
"SASL EXTERNAL and TLS=~p",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateData#state.tls_enabled]),
|
|
|
|
ejabberd_hooks:run(s2s_connect_hook,
|
|
|
|
[StateData#state.myname,
|
|
|
|
StateData#state.server]),
|
|
|
|
{next_state, stream_established,
|
|
|
|
StateData#state{queue = queue:new()}};
|
|
|
|
SASLEXT and StateData#state.try_auth and
|
|
|
|
(StateData#state.new /= false) and
|
|
|
|
(StateData#state.tls_enabled or
|
|
|
|
not StateData#state.tls_required) ->
|
|
|
|
send_element(StateData,
|
|
|
|
#sasl_auth{mechanism = <<"EXTERNAL">>,
|
|
|
|
text = StateData#state.myname}),
|
|
|
|
{next_state, wait_for_auth_result,
|
|
|
|
StateData#state{try_auth = false}, ?FSMTIMEOUT};
|
|
|
|
StartTLS and StateData#state.tls and
|
|
|
|
not StateData#state.tls_enabled ->
|
|
|
|
send_element(StateData, #starttls{}),
|
|
|
|
{next_state, wait_for_starttls_proceed, StateData, ?FSMTIMEOUT};
|
|
|
|
StartTLSRequired and not StateData#state.tls ->
|
|
|
|
?DEBUG("restarted: ~p",
|
|
|
|
[{StateData#state.myname, StateData#state.server}]),
|
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
|
|
|
{next_state, reopen_socket,
|
|
|
|
StateData#state{socket = undefined, use_v10 = false},
|
|
|
|
?FSMTIMEOUT};
|
|
|
|
StateData#state.db_enabled ->
|
|
|
|
send_db_request(StateData);
|
|
|
|
true ->
|
|
|
|
?DEBUG("restarted: ~p",
|
|
|
|
[{StateData#state.myname, StateData#state.server}]),
|
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
|
|
|
{next_state, reopen_socket,
|
|
|
|
StateData#state{socket = undefined, use_v10 = false}, ?FSMTIMEOUT}
|
2005-10-25 03:08:37 +02:00
|
|
|
end;
|
2016-07-27 09:45:08 +02:00
|
|
|
wait_for_features(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, wait_for_features, StateData).
|
|
|
|
|
|
|
|
wait_for_auth_result({xmlstreamelement, El}, StateData) ->
|
|
|
|
decode_element(El, wait_for_auth_result, StateData);
|
|
|
|
wait_for_auth_result(#sasl_success{}, StateData) ->
|
|
|
|
?DEBUG("auth: ~p", [{StateData#state.myname, StateData#state.server}]),
|
|
|
|
ejabberd_socket:reset_stream(StateData#state.socket),
|
2016-09-23 11:30:33 +02:00
|
|
|
send_header(StateData, {1,0}),
|
2016-07-27 09:45:08 +02:00
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(), authenticated = true},
|
|
|
|
?FSMTIMEOUT};
|
|
|
|
wait_for_auth_result(#sasl_failure{}, StateData) ->
|
|
|
|
?DEBUG("restarted: ~p", [{StateData#state.myname, StateData#state.server}]),
|
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
|
|
|
{next_state, reopen_socket,
|
|
|
|
StateData#state{socket = undefined}, ?FSMTIMEOUT};
|
|
|
|
wait_for_auth_result(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, wait_for_auth_result, StateData).
|
|
|
|
|
|
|
|
wait_for_starttls_proceed({xmlstreamelement, El}, StateData) ->
|
|
|
|
decode_element(El, wait_for_starttls_proceed, StateData);
|
|
|
|
wait_for_starttls_proceed(#starttls_proceed{}, StateData) ->
|
|
|
|
?DEBUG("starttls: ~p", [{StateData#state.myname, StateData#state.server}]),
|
|
|
|
Socket = StateData#state.socket,
|
|
|
|
TLSOpts = case ejabberd_config:get_option(
|
|
|
|
{domain_certfile, StateData#state.myname},
|
|
|
|
fun iolist_to_binary/1) of
|
|
|
|
undefined -> StateData#state.tls_options;
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile}
|
|
|
|
| lists:keydelete(certfile, 1,
|
|
|
|
StateData#state.tls_options)]
|
|
|
|
end,
|
|
|
|
TLSSocket = ejabberd_socket:starttls(Socket, TLSOpts),
|
|
|
|
NewStateData = StateData#state{socket = TLSSocket,
|
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true,
|
|
|
|
tls_options = TLSOpts},
|
2016-09-23 11:30:33 +02:00
|
|
|
send_header(NewStateData, {1,0}),
|
2016-07-27 09:45:08 +02:00
|
|
|
{next_state, wait_for_stream, NewStateData, ?FSMTIMEOUT};
|
|
|
|
wait_for_starttls_proceed(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, wait_for_starttls_proceed, StateData).
|
2005-10-25 03:08:37 +02:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
reopen_socket({xmlstreamelement, _El}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2007-12-07 00:15:04 +01:00
|
|
|
reopen_socket({xmlstreamend, _Name}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket({xmlstreamerror, _}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket(timeout, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("reopen socket: timeout", []),
|
2006-02-06 06:12:54 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
reopen_socket(closed, StateData) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(self(), init),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, open_socket, StateData, ?FSMTIMEOUT}.
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2007-09-14 16:18:32 +02:00
|
|
|
%% This state is use to avoid reconnecting to often to bad sockets
|
2007-12-07 00:15:04 +01:00
|
|
|
wait_before_retry(_Event, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_before_retry, StateData, ?FSMTIMEOUT}.
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2010-10-11 10:40:57 +02:00
|
|
|
relay_to_bridge(stop, StateData) ->
|
|
|
|
wait_before_reconnect(StateData);
|
|
|
|
relay_to_bridge(closed, StateData) ->
|
|
|
|
?INFO_MSG("relay to bridge: ~s -> ~s (closed)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
relay_to_bridge(_Event, StateData) ->
|
|
|
|
{next_state, relay_to_bridge, StateData}.
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
decode_element(El, stream_established, StateData);
|
|
|
|
stream_established(#db_verify{to = VTo, from = VFrom, id = VId, type = VType},
|
|
|
|
StateData) ->
|
|
|
|
?DEBUG("recv verify: ~p", [{VFrom, VTo, VId, VType}]),
|
|
|
|
case StateData#state.verify of
|
|
|
|
{VPid, _VKey, _SID} ->
|
|
|
|
case VType of
|
|
|
|
valid ->
|
|
|
|
p1_fsm:send_event(VPid,
|
|
|
|
{valid, StateData#state.server,
|
|
|
|
StateData#state.myname});
|
|
|
|
_ ->
|
|
|
|
p1_fsm:send_event(VPid,
|
|
|
|
{invalid, StateData#state.server,
|
|
|
|
StateData#state.myname})
|
|
|
|
end;
|
|
|
|
_ -> ok
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, StateData};
|
2016-07-27 09:45:08 +02:00
|
|
|
stream_established(Event, StateData) ->
|
|
|
|
handle_unexpected_event(Event, stream_established, StateData).
|
|
|
|
|
|
|
|
-spec handle_unexpected_event(term(), state_name(), state()) -> fsm_transition().
|
|
|
|
handle_unexpected_event(Event, StateName, StateData) ->
|
|
|
|
case Event of
|
|
|
|
{xmlstreamerror, _} ->
|
|
|
|
send_element(StateData, xmpp:serr_not_well_formed()),
|
|
|
|
?INFO_MSG("Closing s2s connection ~s -> ~s in state ~s: "
|
|
|
|
"got invalid XML from peer",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateName]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
{xmlstreamend, _} ->
|
|
|
|
?INFO_MSG("Closing s2s connection ~s -> ~s in state ~s: "
|
|
|
|
"XML stream closed by peer",
|
2016-09-23 11:30:33 +02:00
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateName]),
|
2016-07-27 09:45:08 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
timeout ->
|
|
|
|
send_element(StateData, xmpp:serr_connection_timeout()),
|
|
|
|
?INFO_MSG("Closing s2s connection ~s -> ~s in state ~s: "
|
|
|
|
"timed out during establishing an XML stream",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateName]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
closed ->
|
|
|
|
?INFO_MSG("Closing s2s connection ~s -> ~s in state ~s: "
|
|
|
|
"connection socket closed",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateName]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
Pkt when StateName == wait_for_stream;
|
|
|
|
StateName == wait_for_features;
|
|
|
|
StateName == wait_for_auth_result;
|
|
|
|
StateName == wait_for_starttls_proceed ->
|
|
|
|
send_element(StateData, xmpp:serr_bad_format()),
|
|
|
|
?INFO_MSG("Closing s2s connection ~s -> ~s in state ~s: "
|
|
|
|
"got unexpected event ~p",
|
|
|
|
[StateData#state.myname, StateData#state.server,
|
|
|
|
StateName, Pkt]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
_ ->
|
|
|
|
{next_state, StateName, StateData, get_timeout_interval(StateName)}
|
|
|
|
end.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
%%state_name(Event, From, StateData) ->
|
|
|
|
%% Reply = ok,
|
|
|
|
%% {reply, Reply, state_name, StateData}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, StateName, StateData,
|
|
|
|
get_timeout_interval(StateName)}.
|
|
|
|
|
|
|
|
handle_sync_event(get_state_infos, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
{Addr, Port} = try
|
|
|
|
ejabberd_socket:peername(StateData#state.socket)
|
|
|
|
of
|
|
|
|
{ok, {A, P}} -> {A, P};
|
|
|
|
{error, _} -> {unknown, unknown}
|
|
|
|
catch
|
|
|
|
_:_ -> {unknown, unknown}
|
|
|
|
end,
|
|
|
|
Infos = [{direction, out}, {statename, StateName},
|
|
|
|
{addr, Addr}, {port, Port},
|
2009-05-19 11:38:17 +02:00
|
|
|
{streamid, StateData#state.streamid},
|
|
|
|
{use_v10, StateData#state.use_v10},
|
|
|
|
{tls, StateData#state.tls},
|
|
|
|
{tls_required, StateData#state.tls_required},
|
|
|
|
{tls_enabled, StateData#state.tls_enabled},
|
|
|
|
{tls_options, StateData#state.tls_options},
|
|
|
|
{authenticated, StateData#state.authenticated},
|
|
|
|
{db_enabled, StateData#state.db_enabled},
|
|
|
|
{try_auth, StateData#state.try_auth},
|
|
|
|
{myname, StateData#state.myname},
|
|
|
|
{server, StateData#state.server},
|
|
|
|
{delay_to_retry, StateData#state.delay_to_retry},
|
2013-03-14 10:33:02 +01:00
|
|
|
{verify, StateData#state.verify}],
|
2009-05-19 11:38:17 +02:00
|
|
|
Reply = {state_infos, Infos},
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply, StateName, StateData};
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply, StateName, StateData,
|
|
|
|
get_timeout_interval(StateName)}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text),
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, StateName, StateData#state{timer = Timer},
|
|
|
|
get_timeout_interval(StateName)};
|
2002-12-07 21:27:26 +01:00
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
|
|
|
case StateName of
|
2013-03-14 10:33:02 +01:00
|
|
|
stream_established ->
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
|
|
|
send_element(StateData, El),
|
|
|
|
{next_state, StateName, StateData#state{timer = Timer}};
|
|
|
|
%% In this state we bounce all message: We are waiting before
|
|
|
|
%% trying to reconnect
|
|
|
|
wait_before_retry ->
|
2016-07-27 09:45:08 +02:00
|
|
|
bounce_element(El, xmpp:err_remote_server_not_found()),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
relay_to_bridge ->
|
|
|
|
{Mod, Fun} = StateData#state.bridge,
|
|
|
|
?DEBUG("relaying stanza via ~p:~p/1", [Mod, Fun]),
|
|
|
|
case catch Mod:Fun(El) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Error while relaying to bridge: ~p",
|
|
|
|
[Reason]),
|
2016-07-27 09:45:08 +02:00
|
|
|
bounce_element(El, xmpp:err_internal_server_error()),
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_before_reconnect(StateData);
|
|
|
|
_ -> {next_state, StateName, StateData}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Q = queue:in(El, StateData#state.queue),
|
|
|
|
{next_state, StateName, StateData#state{queue = Q},
|
|
|
|
get_timeout_interval(StateName)}
|
2002-12-07 21:27:26 +01:00
|
|
|
end;
|
2007-09-14 16:18:32 +02:00
|
|
|
handle_info({timeout, Timer, _}, wait_before_retry,
|
|
|
|
#state{timer = Timer} = StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Reconnect delay expired: Will now retry "
|
|
|
|
"to connect to ~s when needed.",
|
|
|
|
[StateData#state.server]),
|
2007-09-14 16:18:32 +02:00
|
|
|
{stop, normal, StateData};
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_info({timeout, Timer, _}, _StateName,
|
2003-12-06 20:58:49 +01:00
|
|
|
#state{timer = Timer} = StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Closing connection with ~s: timeout",
|
|
|
|
[StateData#state.server]),
|
2003-12-06 20:58:49 +01:00
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(terminate_if_waiting_before_retry,
|
|
|
|
wait_before_retry, StateData) ->
|
2008-04-18 14:19:45 +02:00
|
|
|
{stop, normal, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(terminate_if_waiting_before_retry,
|
|
|
|
StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData,
|
|
|
|
get_timeout_interval(StateName)};
|
2003-12-06 20:58:49 +01:00
|
|
|
handle_info(_, StateName, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, StateName, StateData,
|
|
|
|
get_timeout_interval(StateName)}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
terminate(Reason, StateName, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("terminated: ~p", [{Reason, StateName}]),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.new of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> ok;
|
2016-01-11 12:22:17 +01:00
|
|
|
true ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_s2s:remove_connection({StateData#state.myname,
|
|
|
|
StateData#state.server},
|
2016-01-11 12:22:17 +01:00
|
|
|
self())
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2016-07-27 09:45:08 +02:00
|
|
|
bounce_queue(StateData#state.queue, xmpp:err_remote_server_not_found()),
|
|
|
|
bounce_messages(xmpp:err_remote_server_not_found()),
|
2002-12-07 21:27:26 +01:00
|
|
|
case StateData#state.socket of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined -> ok;
|
2016-07-27 09:45:08 +02:00
|
|
|
_Socket ->
|
|
|
|
catch send_trailer(StateData),
|
|
|
|
ejabberd_socket:close(StateData#state.socket)
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2002-12-06 21:59:19 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
print_state(State) -> State.
|
2010-04-09 19:25:00 +02:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec send_text(state(), iodata()) -> ok.
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text) ->
|
2016-09-23 11:30:33 +02:00
|
|
|
?DEBUG("Send Text on stream = ~s", [Text]),
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:send(StateData#state.socket, Text).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec send_element(state(), xmpp_element()) -> ok.
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El) ->
|
2016-09-24 13:17:21 +02:00
|
|
|
El1 = xmpp:encode(El, ?NS_SERVER),
|
2016-07-27 09:45:08 +02:00
|
|
|
send_text(StateData, fxml:element_to_binary(El1)).
|
|
|
|
|
2016-09-23 11:30:33 +02:00
|
|
|
-spec send_header(state(), undefined | {integer(), integer()}) -> ok.
|
2016-07-27 09:45:08 +02:00
|
|
|
send_header(StateData, Version) ->
|
2016-09-23 11:30:33 +02:00
|
|
|
Header = xmpp:encode(
|
|
|
|
#stream_start{xmlns = ?NS_SERVER,
|
|
|
|
stream_xmlns = ?NS_STREAM,
|
|
|
|
db_xmlns = ?NS_SERVER_DIALBACK,
|
|
|
|
from = jid:make(StateData#state.myname),
|
|
|
|
to = jid:make(StateData#state.server),
|
|
|
|
version = Version}),
|
|
|
|
send_text(StateData, fxml:element_to_header(Header)).
|
2016-07-27 09:45:08 +02:00
|
|
|
|
|
|
|
-spec send_trailer(state()) -> ok.
|
|
|
|
send_trailer(StateData) ->
|
|
|
|
send_text(StateData, <<"</stream:stream>">>).
|
|
|
|
|
|
|
|
-spec send_queue(state(), queue:queue()) -> ok.
|
2005-10-25 03:08:37 +02:00
|
|
|
send_queue(StateData, Q) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
case queue:out(Q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{{value, El}, Q1} ->
|
|
|
|
send_element(StateData, El), send_queue(StateData, Q1);
|
|
|
|
{empty, _Q1} -> ok
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2007-09-14 16:18:32 +02:00
|
|
|
%% Bounce a single message (xmlelement)
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec bounce_element(stanza(), stanza_error()) -> ok.
|
2007-09-14 16:18:32 +02:00
|
|
|
bounce_element(El, Error) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
From = xmpp:get_from(El),
|
|
|
|
To = xmpp:get_to(El),
|
|
|
|
ejabberd_router:route_error(To, From, El, Error).
|
2007-09-14 16:18:32 +02:00
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec bounce_queue(queue:queue(), stanza_error()) -> ok.
|
2003-12-06 20:58:49 +01:00
|
|
|
bounce_queue(Q, Error) ->
|
|
|
|
case queue:out(Q) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{{value, El}, Q1} ->
|
|
|
|
bounce_element(El, Error), bounce_queue(Q1, Error);
|
|
|
|
{empty, _} -> ok
|
2003-12-06 20:58:49 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec new_id() -> binary().
|
2013-03-14 10:33:02 +01:00
|
|
|
new_id() -> randoms:get_string().
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec cancel_timer(reference()) -> ok.
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
2013-03-14 10:33:02 +01:00
|
|
|
receive {timeout, Timer, _} -> ok after 0 -> ok end.
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec bounce_messages(stanza_error()) -> ok.
|
2003-05-09 21:32:59 +02:00
|
|
|
bounce_messages(Error) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{send_element, El} ->
|
|
|
|
bounce_element(El, Error), bounce_messages(Error)
|
|
|
|
after 0 -> ok
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec send_db_request(state()) -> fsm_transition().
|
2005-10-25 03:08:37 +02:00
|
|
|
send_db_request(StateData) ->
|
|
|
|
Server = StateData#state.server,
|
|
|
|
New = case StateData#state.new of
|
2016-01-11 12:22:17 +01:00
|
|
|
false ->
|
|
|
|
ejabberd_s2s:try_register({StateData#state.myname, Server});
|
|
|
|
true ->
|
|
|
|
true
|
2005-10-25 03:08:37 +02:00
|
|
|
end,
|
2010-03-04 04:36:57 +01:00
|
|
|
NewStateData = StateData#state{new = New},
|
2013-03-14 10:33:02 +01:00
|
|
|
try case New of
|
2016-01-11 12:22:17 +01:00
|
|
|
false -> ok;
|
|
|
|
true ->
|
|
|
|
Key1 = ejabberd_s2s:make_key(
|
|
|
|
{StateData#state.myname, Server},
|
|
|
|
StateData#state.remote_streamid),
|
2013-03-14 10:33:02 +01:00
|
|
|
send_element(StateData,
|
2016-08-09 09:56:32 +02:00
|
|
|
#db_result{from = StateData#state.myname,
|
|
|
|
to = Server,
|
2016-07-27 09:45:08 +02:00
|
|
|
key = Key1})
|
2010-03-04 04:36:57 +01:00
|
|
|
end,
|
|
|
|
case StateData#state.verify of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> ok;
|
|
|
|
{_Pid, Key2, SID} ->
|
|
|
|
send_element(StateData,
|
2016-08-09 09:56:32 +02:00
|
|
|
#db_verify{from = StateData#state.myname,
|
2016-07-27 09:45:08 +02:00
|
|
|
to = StateData#state.server,
|
|
|
|
id = SID,
|
|
|
|
key = Key2})
|
2010-03-04 04:36:57 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, wait_for_validation, NewStateData,
|
|
|
|
(?FSMTIMEOUT) * 6}
|
2010-03-04 04:36:57 +01:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
_:_ -> {stop, normal, NewStateData}
|
2010-03-04 04:36:57 +01:00
|
|
|
end.
|
2005-10-25 03:08:37 +02:00
|
|
|
|
2003-02-04 21:45:23 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2007-12-07 00:15:04 +01:00
|
|
|
%% SRV support
|
2003-02-04 21:45:23 +01:00
|
|
|
|
|
|
|
-include_lib("kernel/include/inet.hrl").
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_addr_port(binary()) -> [{binary(), inet:port_number()}].
|
2003-02-04 21:45:23 +01:00
|
|
|
get_addr_port(Server) ->
|
2009-07-30 14:34:59 +02:00
|
|
|
Res = srv_lookup(Server),
|
2003-10-12 20:21:16 +02:00
|
|
|
case Res of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("srv lookup of '~s' failed: ~p~n",
|
|
|
|
[Server, Reason]),
|
|
|
|
[{Server, outgoing_s2s_port()}];
|
|
|
|
{ok, HEnt} ->
|
|
|
|
?DEBUG("srv lookup of '~s': ~p~n",
|
|
|
|
[Server, HEnt#hostent.h_addr_list]),
|
|
|
|
AddrList = HEnt#hostent.h_addr_list,
|
|
|
|
case catch lists:map(fun ({Priority, Weight, Port,
|
|
|
|
Host}) ->
|
|
|
|
N = case Weight of
|
|
|
|
0 -> 0;
|
|
|
|
_ ->
|
2016-10-18 07:17:21 +02:00
|
|
|
(Weight + 1) * randoms:uniform()
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
{Priority * 65536 - N, Host, Port}
|
|
|
|
end,
|
|
|
|
AddrList)
|
|
|
|
of
|
|
|
|
SortedList = [_ | _] ->
|
|
|
|
List = lists:map(fun ({_, Host, Port}) ->
|
|
|
|
{list_to_binary(Host), Port}
|
|
|
|
end,
|
|
|
|
lists:keysort(1, SortedList)),
|
|
|
|
?DEBUG("srv lookup of '~s': ~p~n", [Server, List]),
|
|
|
|
List;
|
|
|
|
_ -> [{Server, outgoing_s2s_port()}]
|
|
|
|
end
|
2003-02-04 21:45:23 +01:00
|
|
|
end.
|
|
|
|
|
2009-07-30 14:34:59 +02:00
|
|
|
srv_lookup(Server) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
TimeoutMs = timer:seconds(
|
|
|
|
ejabberd_config:get_option(
|
|
|
|
s2s_dns_timeout,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
10)),
|
|
|
|
Retries = ejabberd_config:get_option(
|
|
|
|
s2s_dns_retries,
|
|
|
|
fun(I) when is_integer(I), I>=0 -> I end,
|
|
|
|
2),
|
2013-03-14 10:33:02 +01:00
|
|
|
srv_lookup(binary_to_list(Server), TimeoutMs, Retries).
|
2009-07-30 14:34:59 +02:00
|
|
|
|
|
|
|
%% XXX - this behaviour is suboptimal in the case that the domain
|
|
|
|
%% has a "_xmpp-server._tcp." but not a "_jabber._tcp." record and
|
|
|
|
%% we don't get a DNS reply for the "_xmpp-server._tcp." lookup. In this
|
|
|
|
%% case we'll give up when we get the "_jabber._tcp." nxdomain reply.
|
2013-03-14 10:33:02 +01:00
|
|
|
srv_lookup(_Server, _Timeout, Retries)
|
|
|
|
when Retries < 1 ->
|
2009-07-30 14:34:59 +02:00
|
|
|
{error, timeout};
|
|
|
|
srv_lookup(Server, Timeout, Retries) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case inet_res:getbyname("_xmpp-server._tcp." ++ Server,
|
|
|
|
srv, Timeout)
|
|
|
|
of
|
|
|
|
{error, _Reason} ->
|
|
|
|
case inet_res:getbyname("_jabber._tcp." ++ Server, srv,
|
|
|
|
Timeout)
|
|
|
|
of
|
|
|
|
{error, timeout} ->
|
|
|
|
?ERROR_MSG("The DNS servers~n ~p~ntimed out on "
|
|
|
|
"request for ~p IN SRV. You should check "
|
|
|
|
"your DNS configuration.",
|
|
|
|
[inet_db:res_option(nameserver), Server]),
|
|
|
|
srv_lookup(Server, Timeout, Retries - 1);
|
|
|
|
R -> R
|
|
|
|
end;
|
|
|
|
{ok, _HEnt} = R -> R
|
2009-07-30 14:34:59 +02:00
|
|
|
end.
|
|
|
|
|
2006-03-14 05:26:15 +01:00
|
|
|
test_get_addr_port(Server) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (_, Acc) ->
|
|
|
|
[HostPort | _] = get_addr_port(Server),
|
|
|
|
case lists:keysearch(HostPort, 1, Acc) of
|
|
|
|
false -> [{HostPort, 1} | Acc];
|
|
|
|
{value, {_, Num}} ->
|
|
|
|
lists:keyreplace(HostPort, 1, Acc,
|
|
|
|
{HostPort, Num + 1})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], lists:seq(1, 100000)).
|
2007-09-14 16:16:20 +02:00
|
|
|
|
2008-12-26 12:45:08 +01:00
|
|
|
get_addrs(Host, Family) ->
|
|
|
|
Type = case Family of
|
2013-03-14 10:33:02 +01:00
|
|
|
inet4 -> inet;
|
|
|
|
ipv4 -> inet;
|
|
|
|
inet6 -> inet6;
|
|
|
|
ipv6 -> inet6
|
2008-12-26 12:45:08 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
case inet:gethostbyname(binary_to_list(Host), Type) of
|
|
|
|
{ok, #hostent{h_addr_list = Addrs}} ->
|
|
|
|
?DEBUG("~s of ~s resolved to: ~p~n",
|
|
|
|
[Type, Host, Addrs]),
|
|
|
|
Addrs;
|
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("~s lookup of '~s' failed: ~p~n",
|
|
|
|
[Type, Host, Reason]),
|
|
|
|
[]
|
2008-12-26 12:45:08 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec outgoing_s2s_port() -> pos_integer().
|
2007-12-29 15:22:38 +01:00
|
|
|
outgoing_s2s_port() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
outgoing_s2s_port,
|
|
|
|
fun(I) when is_integer(I), I > 0, I =< 65536 -> I end,
|
|
|
|
5269).
|
2007-09-14 16:18:17 +02:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec outgoing_s2s_families() -> [ipv4 | ipv6].
|
2008-12-26 12:45:08 +01:00
|
|
|
outgoing_s2s_families() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
|
|
|
outgoing_s2s_families,
|
|
|
|
fun(Families) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
true = lists:all(
|
|
|
|
fun(ipv4) -> true;
|
|
|
|
(ipv6) -> true
|
|
|
|
end, Families),
|
|
|
|
Families
|
|
|
|
end, [ipv4, ipv6]).
|
2008-12-26 12:45:08 +01:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec outgoing_s2s_timeout() -> pos_integer().
|
2008-12-26 12:45:08 +01:00
|
|
|
outgoing_s2s_timeout() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd_config:get_option(
|
|
|
|
outgoing_s2s_timeout,
|
|
|
|
fun(TimeOut) when is_integer(TimeOut), TimeOut > 0 ->
|
2013-03-14 10:33:02 +01:00
|
|
|
TimeOut;
|
2013-08-12 14:25:05 +02:00
|
|
|
(infinity) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
infinity
|
|
|
|
end, 10000).
|
2008-12-26 12:45:08 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_options(Opts) ->
|
|
|
|
lists:foldl(fun transform_options/2, [], Opts).
|
|
|
|
|
|
|
|
transform_options({outgoing_s2s_options, Families, Timeout}, Opts) ->
|
|
|
|
?WARNING_MSG("Option 'outgoing_s2s_options' is deprecated. "
|
|
|
|
"The option is still supported "
|
|
|
|
"but it is better to fix your config: "
|
|
|
|
"use 'outgoing_s2s_timeout' and "
|
|
|
|
"'outgoing_s2s_families' instead.", []),
|
|
|
|
[{outgoing_s2s_families, Families},
|
|
|
|
{outgoing_s2s_timeout, Timeout}
|
|
|
|
| Opts];
|
|
|
|
transform_options({s2s_dns_options, S2SDNSOpts}, AllOpts) ->
|
|
|
|
?WARNING_MSG("Option 's2s_dns_options' is deprecated. "
|
|
|
|
"The option is still supported "
|
|
|
|
"but it is better to fix your config: "
|
|
|
|
"use 's2s_dns_timeout' and "
|
|
|
|
"'s2s_dns_retries' instead", []),
|
|
|
|
lists:foldr(
|
|
|
|
fun({timeout, T}, AccOpts) ->
|
|
|
|
[{s2s_dns_timeout, T}|AccOpts];
|
|
|
|
({retries, R}, AccOpts) ->
|
|
|
|
[{s2s_dns_retries, R}|AccOpts];
|
|
|
|
(_, AccOpts) ->
|
|
|
|
AccOpts
|
|
|
|
end, AllOpts, S2SDNSOpts);
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
|
|
|
|
2007-09-14 16:16:20 +02:00
|
|
|
%% Human readable S2S logging: Log only new outgoing connections as INFO
|
|
|
|
%% Do not log dialback
|
2010-07-23 00:25:43 +02:00
|
|
|
log_s2s_out(false, _, _, _) -> ok;
|
2007-09-14 16:16:20 +02:00
|
|
|
%% Log new outgoing connections:
|
2010-07-23 00:25:43 +02:00
|
|
|
log_s2s_out(_, Myname, Server, Tls) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?INFO_MSG("Trying to open s2s connection: ~s -> "
|
|
|
|
"~s with TLS=~p",
|
|
|
|
[Myname, Server, Tls]).
|
2007-09-14 16:18:32 +02:00
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
%% Calculate timeout depending on which state we are in:
|
2007-09-14 16:18:32 +02:00
|
|
|
%% Can return integer > 0 | infinity
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec get_timeout_interval(state_name()) -> pos_integer() | infinity.
|
2007-09-14 16:18:32 +02:00
|
|
|
get_timeout_interval(StateName) ->
|
|
|
|
case StateName of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% Validation implies dialback: Networking can take longer:
|
|
|
|
wait_for_validation -> (?FSMTIMEOUT) * 6;
|
|
|
|
%% When stream is established, we only rely on S2S Timeout timer:
|
|
|
|
stream_established -> infinity;
|
2016-07-27 09:45:08 +02:00
|
|
|
relay_to_bridge -> infinity;
|
|
|
|
open_socket -> infinity;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ?FSMTIMEOUT
|
2007-09-14 16:18:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% This function is intended to be called at the end of a state
|
|
|
|
%% function that want to wait for a reconnect delay before stopping.
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec wait_before_reconnect(state()) -> fsm_next().
|
2008-04-18 14:19:45 +02:00
|
|
|
wait_before_reconnect(StateData) ->
|
2016-07-27 09:45:08 +02:00
|
|
|
bounce_queue(StateData#state.queue, xmpp:err_remote_server_not_found()),
|
|
|
|
bounce_messages(xmpp:err_remote_server_not_found()),
|
2007-09-14 16:18:32 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2008-04-18 14:19:45 +02:00
|
|
|
Delay = case StateData#state.delay_to_retry of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined_delay ->
|
2015-12-07 16:08:57 +01:00
|
|
|
{_, _, MicroSecs} = p1_time_compat:timestamp(), MicroSecs rem 14000 + 1000;
|
2013-03-14 10:33:02 +01:00
|
|
|
D1 -> lists:min([D1 * 2, get_max_retry_delay()])
|
2008-04-18 14:19:45 +02:00
|
|
|
end,
|
2007-09-14 16:18:32 +02:00
|
|
|
Timer = erlang:start_timer(Delay, self(), []),
|
2013-03-14 10:33:02 +01:00
|
|
|
{next_state, wait_before_retry,
|
|
|
|
StateData#state{timer = Timer, delay_to_retry = Delay,
|
|
|
|
queue = queue:new()}}.
|
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec get_max_retry_delay() -> pos_integer().
|
2008-04-18 14:19:45 +02:00
|
|
|
get_max_retry_delay() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
s2s_max_retry_delay,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I end) of
|
|
|
|
undefined -> ?MAX_RETRY_DELAY;
|
|
|
|
Seconds -> Seconds * 1000
|
2008-04-18 14:19:45 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Terminate s2s_out connections that are in state wait_before_retry
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec terminate_if_waiting_delay(binary(), binary()) -> ok.
|
2008-04-18 14:19:45 +02:00
|
|
|
terminate_if_waiting_delay(From, To) ->
|
|
|
|
FromTo = {From, To},
|
|
|
|
Pids = ejabberd_s2s:get_connections_pids(FromTo),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (Pid) ->
|
|
|
|
Pid ! terminate_if_waiting_before_retry
|
|
|
|
end,
|
|
|
|
Pids).
|
2009-10-07 16:24:09 +02:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec fsm_limit_opts() -> [{max_queue, pos_integer()}].
|
2009-10-07 16:24:09 +02:00
|
|
|
fsm_limit_opts() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
max_fsm_queue,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I end) of
|
|
|
|
undefined -> [];
|
|
|
|
N -> [{max_queue, N}]
|
2009-10-07 16:24:09 +02:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-27 09:45:08 +02:00
|
|
|
-spec decode_element(xmlel(), state_name(), state()) -> fsm_next().
|
|
|
|
decode_element(#xmlel{} = El, StateName, StateData) ->
|
2016-09-24 13:17:21 +02:00
|
|
|
Opts = if StateName == stream_established ->
|
|
|
|
[ignore_els];
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
try xmpp:decode(El, ?NS_SERVER, Opts) of
|
2016-07-27 09:45:08 +02:00
|
|
|
Pkt -> ?MODULE:StateName(Pkt, StateData)
|
|
|
|
catch error:{xmpp_codec, Why} ->
|
|
|
|
Type = xmpp:get_type(El),
|
|
|
|
case xmpp:is_stanza(El) of
|
|
|
|
true when Type /= <<"result">>, Type /= <<"error">> ->
|
|
|
|
Lang = xmpp:get_lang(El),
|
|
|
|
Txt = xmpp:format_error(Why),
|
|
|
|
Err = xmpp:make_error(El, xmpp:err_bad_request(Txt, Lang)),
|
|
|
|
send_element(StateData, Err);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{next_state, StateName, StateData, get_timeout_interval(StateName)}
|
|
|
|
end.
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(domain_certfile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(max_fsm_queue) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
opt_type(outgoing_s2s_families) ->
|
|
|
|
fun (Families) ->
|
|
|
|
true = lists:all(fun (ipv4) -> true;
|
|
|
|
(ipv6) -> true
|
|
|
|
end,
|
|
|
|
Families),
|
|
|
|
Families
|
|
|
|
end;
|
|
|
|
opt_type(outgoing_s2s_port) ->
|
|
|
|
fun (I) when is_integer(I), I > 0, I =< 65536 -> I end;
|
|
|
|
opt_type(outgoing_s2s_timeout) ->
|
|
|
|
fun (TimeOut) when is_integer(TimeOut), TimeOut > 0 ->
|
|
|
|
TimeOut;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end;
|
|
|
|
opt_type(s2s_certfile) -> fun iolist_to_binary/1;
|
|
|
|
opt_type(s2s_ciphers) -> fun iolist_to_binary/1;
|
2015-06-16 15:18:34 +02:00
|
|
|
opt_type(s2s_dhfile) -> fun iolist_to_binary/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(s2s_dns_retries) ->
|
|
|
|
fun (I) when is_integer(I), I >= 0 -> I end;
|
|
|
|
opt_type(s2s_dns_timeout) ->
|
|
|
|
fun (I) when is_integer(I), I >= 0 -> I end;
|
|
|
|
opt_type(s2s_max_retry_delay) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
opt_type(s2s_protocol_options) ->
|
|
|
|
fun (Options) ->
|
|
|
|
[_ | O] = lists:foldl(fun (X, Acc) -> X ++ Acc end, [],
|
|
|
|
[["|" | binary_to_list(Opt)]
|
|
|
|
|| Opt <- Options, is_binary(Opt)]),
|
|
|
|
iolist_to_binary(O)
|
|
|
|
end;
|
|
|
|
opt_type(s2s_tls_compression) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false
|
|
|
|
end;
|
|
|
|
opt_type(s2s_use_starttls) ->
|
|
|
|
fun (true) -> true;
|
|
|
|
(false) -> false;
|
|
|
|
(optional) -> optional;
|
|
|
|
(required) -> required;
|
|
|
|
(required_trusted) -> required_trusted
|
|
|
|
end;
|
|
|
|
opt_type(_) ->
|
|
|
|
[domain_certfile, max_fsm_queue, outgoing_s2s_families,
|
|
|
|
outgoing_s2s_port, outgoing_s2s_timeout, s2s_certfile,
|
2015-06-20 00:14:54 +02:00
|
|
|
s2s_ciphers, s2s_dhfile, s2s_dns_retries, s2s_dns_timeout,
|
2015-06-01 14:38:27 +02:00
|
|
|
s2s_max_retry_delay, s2s_protocol_options,
|
|
|
|
s2s_tls_compression, s2s_use_starttls].
|