Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend error reason in case of failed handshake #345

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions erlang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ERLANG_MK_FILENAME := $(realpath $(lastword $(MAKEFILE_LIST)))
export ERLANG_MK_FILENAME

ERLANG_MK_VERSION = bb811a8
ERLANG_MK_VERSION = 16d60fa
ERLANG_MK_WITHOUT =

# Make 3.81 and 3.82 are deprecated.
Expand Down Expand Up @@ -3565,7 +3565,7 @@ REBAR_DEPS_DIR = $(DEPS_DIR)
export REBAR_DEPS_DIR

REBAR3_GIT ?= https://github.com/erlang/rebar3
REBAR3_COMMIT ?= 3f563feaf1091a1980241adefa83a32dd2eebf7c # 3.20.0
REBAR3_COMMIT ?= 06aaecd51b0ce828b66bb65a74d3c1fd7833a4ba # 3.22.1 + OTP-27 fixes

CACHE_DEPS ?= 0

Expand Down Expand Up @@ -4665,7 +4665,6 @@ define makedep.erl
end,
MakeDepend = fun
(F, Fd, Mod, StartLocation) ->
{ok, Filename} = file:pid2name(Fd),
case io:parse_erl_form(Fd, undefined, StartLocation) of
{ok, AbsData, EndLocation} ->
case AbsData of
Expand Down
23 changes: 13 additions & 10 deletions src/ranch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ handshake(Ref, Opts) ->

handshake1(Ref, Opts) ->
receive {handshake, Ref, Transport, CSocket, Timeout} ->
PeerInfo = get_peer_info(Transport, CSocket, undefined),
Handshake = handshake_transport(Transport, handshake, CSocket, Opts, Timeout),
handshake_result(Handshake, Ref, Transport, CSocket, Timeout)
handshake_result(Handshake, Ref, Transport, CSocket, PeerInfo, Timeout)
end.

-spec handshake_continue(ref()) -> {ok, ranch_transport:socket()}.
Expand All @@ -301,31 +302,27 @@ handshake_continue(Ref, Opts) ->

handshake_continue1(Ref, Opts) ->
receive {handshake_continue, Ref, Transport, CSocket, Timeout} ->
PeerInfo = get_peer_info(Transport, CSocket, undefined),
Handshake = handshake_transport(Transport, handshake_continue, CSocket, Opts, Timeout),
handshake_result(Handshake, Ref, Transport, CSocket, Timeout)
handshake_result(Handshake, Ref, Transport, CSocket, PeerInfo, Timeout)
end.

handshake_transport(Transport, Fun, CSocket, undefined, Timeout) ->
Transport:Fun(CSocket, Timeout);
handshake_transport(Transport, Fun, CSocket, {opts, Opts}, Timeout) ->
Transport:Fun(CSocket, Opts, Timeout).

handshake_result(Result, Ref, Transport, CSocket, Timeout) ->
handshake_result(Result, Ref, Transport, CSocket, PeerInfo0, Timeout) ->
case Result of
OK = {ok, _} ->
OK;
{ok, CSocket2, Info} ->
self() ! {handshake_continue, Ref, Transport, CSocket2, Timeout},
{continue, Info};
{error, {tls_alert, _}} ->
ok = Transport:close(CSocket),
exit(normal);
{error, Reason} when Reason =:= timeout; Reason =:= closed ->
ok = Transport:close(CSocket),
exit(normal);
{error, Reason} ->
PeerInfo = get_peer_info(Transport, CSocket, PeerInfo0),
ok = Transport:close(CSocket),
error(Reason)
exit({shutdown, {Reason, PeerInfo}})
end.

-spec handshake_cancel(ref()) -> ok.
Expand All @@ -334,6 +331,12 @@ handshake_cancel(Ref) ->
Transport:handshake_cancel(CSocket)
end.

get_peer_info(Transport, Socket, Default) ->
case Transport:peername(Socket) of
{ok, Peer} -> Peer;
{error, _} -> Default
end.

%% Unlike handshake/2 this function always return errors because
%% the communication between the proxy and the server are expected
%% to be reliable. If there is a problem while receiving the proxy
Expand Down
27 changes: 27 additions & 0 deletions test/acceptor_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ groups() ->
ssl_local_echo,
ssl_graceful,
ssl_handshake,
ssl_handshake_error,
ssl_sni_echo,
ssl_sni_fail,
ssl_tls_psk,
Expand Down Expand Up @@ -834,6 +835,32 @@ ssl_handshake(_) ->
{'EXIT', _} = begin catch ranch:get_port(Name) end,
ok.

ssl_handshake_error(_) ->
doc("Acceptor must not crash if client disconnects in the middle of SSL handshake."),
Name = name(),
Opts = ct_helper:get_certs_from_ets(),
{ok, _} = ranch:start_listener(Name,
ranch_ssl, #{num_acceptors => 1, socket_opts => Opts},
echo_protocol, []),
Port = ranch:get_port(Name),
{ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
receive after 500 -> ok end,
[ConnPid] = ranch:procs(Name, connections),
ConnMon = monitor(process, ConnPid),
ok = gen_tcp:send(Socket, <<"GARBAGE">>),
ok = gen_tcp:close(Socket),
receive
{'DOWN', ConnMon, process, ConnPid, R} ->
{shutdown, {_, _}} = R
after 1000 ->
error(timeout)
end,
receive after 500 -> ok end,
ok = ranch:stop_listener(Name),
%% Make sure the listener stopped.
{'EXIT', _} = begin catch ranch:get_port(Name) end,
ok.

ssl_local_echo(_) ->
case do_os_supports_local_sockets() of
true ->
Expand Down
Loading