Skip to content

Commit

Permalink
ssl: Some code polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
IngelaAndin committed Jan 8, 2025
1 parent 195733d commit 23f2d56
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 3 additions & 9 deletions lib/ssl/src/ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,7 @@ connect(TCPSocket, TLSOptions, Timeout)
try
tls_gen_connection = connection_cb(TLSOptions),
{ok, Config} = ssl_config:handle_options(TCPSocket, TLSOptions, client, undefined),
tls_socket:upgrade(TCPSocket, Config, Timeout)
tls_socket:upgrade(client, TCPSocket, Config, Timeout)
catch
error:{badmatch, _} ->
{error, {dtls_upgrade, notsup}};
Expand Down Expand Up @@ -2448,15 +2448,9 @@ handshake(Socket, SslOptions, Timeout)
when is_list(SslOptions), ?IS_TIMEOUT(Timeout) ->
try
tls_gen_connection = connection_cb(SslOptions),
{ok, #config{transport_info = CbInfo, ssl = SslOpts, emulated = EmOpts}} =
{ok, Config} =
ssl_config:handle_options(Socket, SslOptions, server, undefined),
Transport = element(1, CbInfo),
ok = tls_socket:setopts(Transport, Socket, tls_socket:internal_inet_values()),
{ok, Port} = tls_socket:port(Transport, Socket),
{ok, SessionIdHandle} = tls_socket:session_id_tracker(ssl_unknown_listener, SslOpts),
Trackers = [{session_id_tracker, SessionIdHandle}],
{ok, SSocket} = tls_socket:start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo),
ssl_gen_statem:handshake(SSocket, Timeout)
tls_socket:upgrade(server, Socket, Config, Timeout)
catch
error:{badmatch, _} ->
{error, {dtls_upgrade, notsup}};
Expand Down
4 changes: 2 additions & 2 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
%%% Initial Erlang process setup
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
-spec tls_start_link(client| server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
-spec tls_start_link(client | server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
{ok, pid()} | ignore | {error, ssl:reason()}.
%%
%% Description: Creates a process which calls Module:init/1 to
Expand All @@ -121,7 +121,7 @@ tls_start_link(Role, Host, Port, Socket, {SslOpts, _, _} = Options, User, CbInfo
{ok, Pid}.

%%--------------------------------------------------------------------
-spec dtls_start_link(atom(), ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
-spec dtls_start_link(client | server, ssl:host(), inet:port_number(), port(), tuple(), pid(), tuple()) ->
{ok, pid()} | ignore | {error, ssl:reason()}.
%%
%% Description: Creates a gen_statem process which calls Module:init/1 to
Expand Down
27 changes: 18 additions & 9 deletions lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
accept/3,
socket/6,
connect/4,
upgrade/3,
upgrade/4,
setopts/3,
getopts/3,
getstat/3,
Expand All @@ -41,8 +41,7 @@
close/2]).

-export([split_options/1,
get_socket_opts/3,
start_tls_server_connection/6]).
get_socket_opts/3]).

-export([emulated_options/0,
emulated_options/1,
Expand All @@ -52,7 +51,6 @@
start_link/3,
terminate/2,
inherit_tracker/3,
session_id_tracker/2,
emulated_socket_options/2,
get_emulated_opts/1,
set_emulated_opts/2,
Expand Down Expand Up @@ -113,16 +111,27 @@ accept(ListenSocket, #config{transport_info = {Transport,_,_,_,_} = CbInfo,
{error, Reason}
end.

upgrade(Socket, #config{transport_info = {Transport,_,_,_,_}= CbInfo,
ssl = SslOptions,
emulated = EmOpts}, Timeout) ->
ok = setopts(Transport, Socket, tls_socket:internal_inet_values()),
upgrade(client, Socket, #config{transport_info = CbInfo,
ssl = SslOptions,
emulated = EmOpts}, Timeout) ->
Transport = element(1, CbInfo),
ok = setopts(Transport, Socket, internal_inet_values()),
case peername(Transport, Socket) of
{ok, {Host, Port}} ->
start_tls_client_connection(Host, Port, Socket, SslOptions, EmOpts, CbInfo, Timeout);
{error, Error} ->
{error, Error}
end.
end;
upgrade(server, Socket, #config{transport_info = CbInfo,
ssl = SslOpts,
emulated = EmOpts}, Timeout) ->
Transport = element(1, CbInfo),
ok = setopts(Transport, Socket, internal_inet_values()),
{ok, Port} = port(Transport, Socket),
{ok, SessionIdHandle} = session_id_tracker(ssl_unknown_listener, SslOpts),
Trackers = [{session_id_tracker, SessionIdHandle}],
{ok, SSocket} = start_tls_server_connection(SslOpts, Port, Socket, EmOpts, Trackers, CbInfo),
ssl_gen_statem:handshake(SSocket, Timeout).

connect(Host, Port,
#config{transport_info = CbInfo, inet_user = UserOpts, ssl = SslOpts,
Expand Down

0 comments on commit 23f2d56

Please sign in to comment.