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

Closed
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
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