Skip to content

Commit

Permalink
Handle TLS handshake timing issue during upgrade (Nordix#11)
Browse files Browse the repository at this point in the history
Make sure  we handle setopt result
  • Loading branch information
bjosv authored Jun 13, 2020
1 parent 68120d6 commit b3b34fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 1 addition & 2 deletions include/eredis.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

-define(NL, "\r\n").

%% initial active opt should be 'false' before a possible upgrade to ssl
-define(SOCKET_MODE, binary).
-define(SOCKET_OPTS, [{active, false}, {packet, raw}, {reuseaddr, false},
-define(SOCKET_OPTS, [{active, once}, {packet, raw}, {reuseaddr, false},
{keepalive, false}, {send_timeout, ?SEND_TIMEOUT}]).

-define(RECV_TIMEOUT, 5000).
Expand Down
18 changes: 12 additions & 6 deletions src/eredis_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ connect(State) ->
{ok, Socket} ->
case maybe_upgrade_to_tls(Socket, State) of
{ok, NewSocket} ->
%% Enter `{active, once}' mode. NOTE: tls/ssl doesn't support `{active, N}'
ok = setopts(NewSocket, State#state.transport, [{active, once}]),
case authenticate(NewSocket, State#state.transport, State#state.password) of
ok ->
case select_database(NewSocket, State#state.transport, State#state.database) of
Expand All @@ -382,13 +380,21 @@ connect(State) ->
end.

maybe_upgrade_to_tls(Socket, #state{transport = tls} = State) ->
ssl:connect(Socket, State#state.tls_options, State#state.connect_timeout);
%% initial active opt should be 'false' before a possible upgrade to ssl
inet:setopts(Socket, [{active, false}]),
case ssl:connect(Socket, State#state.tls_options, State#state.connect_timeout) of
{ok, NewSocket} ->
%% Enter `{active, once}' mode. NOTE: tls/ssl doesn't support `{active, N}'
case ssl:setopts(NewSocket, [{active, once}]) of
ok -> {ok, NewSocket};
Reason -> Reason
end;
Reason -> Reason
end;

maybe_upgrade_to_tls(Socket, _State) ->
{ok, Socket}.

setopts(Socket, tls, Opts) -> ssl:setopts(Socket, Opts);
setopts(Socket, _, Opts) -> inet:setopts(Socket, Opts).

get_addr({local, Path}) ->
{ok, {local, {local, Path}}};
get_addr(Hostname) ->
Expand Down

0 comments on commit b3b34fe

Please sign in to comment.