Skip to content

Commit

Permalink
ssl: Remove unnecessary code generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
IngelaAndin committed Dec 11, 2024
1 parent c838c6c commit d8de66f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 79 deletions.
12 changes: 6 additions & 6 deletions lib/ssl/src/dtls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ accept({Listener,_}, #config{}, _Timeout) ->
{error, Reason}
end.

connect(Address, Port, #config{transport_info = {Transport, _, _, _, _} = CbInfo,
connection_cb = ConnectionCb,
connect(Host, Port, #config{transport_info = CbInfo,
ssl = SslOpts,
emulated = EmOpts,
inet_ssl = SocketOpts,
tab = _Tab
}, Timeout) ->
Transport = element(1, CbInfo),
case Transport:open(0, SocketOpts ++ internal_inet_values()) of
{ok, Socket} ->
ssl_gen_statem:connect(ConnectionCb, Address, Port, {{Address, Port},Socket},
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
dtls_gen_connection:start_fsm(client, Host, Port, {{Host, Port}, Socket},
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{error, _} = Error->
Error
end.
Expand Down
29 changes: 17 additions & 12 deletions lib/ssl/src/ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2173,15 +2173,16 @@ connect(Host, Port, TLSOptions, infinity).

connect(TCPSocket, TLSOptions0, Timeout)
when is_list(TLSOptions0), ?IS_TIMEOUT(Timeout) ->

try
tls_gen_connection = connection_cb(TLSOptions0),
CbInfo = handle_option_cb_info(TLSOptions0, tls),
Transport = element(1, CbInfo),
{ok, Config} = handle_options(Transport, TCPSocket, TLSOptions0, client, undefined),
tls_socket:upgrade(TCPSocket, Config, Timeout)
catch
_:{error, Reason} ->
{error, Reason}
error:{badmatch, _} ->
{error, {dtls_upgrade, notsup}};
throw:Error = {error, _Reason} -> Error
end;
connect(Host, Port, TLSOptions)
when is_integer(Port), is_list(TLSOptions) ->
Expand Down Expand Up @@ -2245,8 +2246,10 @@ connect(Host, Port, Options, Timeout)
dtls_socket:connect(Host,Port,Config,Timeout)
end
catch
throw:Error ->
Error
exit:{noproc, _} ->
{error, ssl_not_started};
throw:Error ->
Error
end.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -2448,21 +2451,23 @@ handshake(#sslsocket{socket_handle = {Controller,_}, connection_cb = dtls_gen_co
handshake(Socket, SslOptions, Timeout)
when is_list(SslOptions), ?IS_TIMEOUT(Timeout) ->
try
tls_gen_connection = connection_cb(SslOptions),
CbInfo = handle_option_cb_info(SslOptions, tls),
Transport = element(1, CbInfo),
ConnetionCb = connection_cb(SslOptions),
{ok, #config{transport_info = CbInfo, ssl = SslOpts, emulated = EmOpts}} =
handle_options(Transport, Socket, SslOptions, server, undefined),
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),
ssl_gen_statem:handshake(ConnetionCb, Port, Socket,
{SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{}),
[{session_id_tracker, SessionIdHandle}]},
self(), CbInfo, Timeout)
tls_gen_connection:start_fsm(server, "localhost", Port, Socket,
{SslOpts,
tls_socket:emulated_socket_options(EmOpts, #socket_options{}),
[{session_id_tracker, SessionIdHandle}]},
self(), CbInfo, Timeout)
catch
Error = {error, _Reason} -> Error
error:{badmatch, _} ->
{error, {dtls_upgrade, notsup}};
throw:Error = {error, _Reason} -> Error
end.

%%--------------------------------------------------------------------
Expand Down
39 changes: 0 additions & 39 deletions lib/ssl/src/ssl_gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
-export([opposite_role/1,
init_ssl_config/3,
ssl_config/3,
connect/8,
handshake/7,
handshake/2,
handshake/3,
handshake_continue/3,
Expand Down Expand Up @@ -269,43 +267,6 @@ ssl_config(Opts, Role, #state{static_env = InitStatEnv0,
connection_env = CEnv#connection_env{cert_key_alts = CertKeyAlts},
ssl_options = Opts}.

%%--------------------------------------------------------------------
-spec connect(tls_gen_connection | dtls_gen_connection,
ssl:host(), inet:port_number(),
port() | {tuple(), port()}, %% TLS | DTLS
{ssl_options(), #socket_options{},
%% Tracker only needed on server side
undefined},
pid(), tuple(), timeout()) ->
{ok, #sslsocket{}} | {error, ssl:reason()}.
%%
%% Description: Connect to an ssl server.
%%--------------------------------------------------------------------
connect(Connection, Host, Port, Socket, Options, User, CbInfo, Timeout) ->
try Connection:start_fsm(client, Host, Port, Socket, Options, User, CbInfo,
Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.
%%--------------------------------------------------------------------
-spec handshake(tls_gen_connection | dtls_gen_connection,
inet:port_number(), port(),
{ssl_options(), #socket_options{}, list()},
pid(), tuple(), timeout()) ->
{ok, #sslsocket{}} | {error, ssl:reason()}.
%%
%% Description: Performs accept on an ssl listen socket. e.i. performs
%% ssl handshake.
%%--------------------------------------------------------------------
handshake(Connection, Port, Socket, Opts, User, CbInfo, Timeout) ->
try Connection:start_fsm(server, "localhost", Port, Socket, Opts, User,
CbInfo, Timeout)
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end.

%%--------------------------------------------------------------------
-spec handshake(#sslsocket{}, timeout()) -> {ok, #sslsocket{}} |
{ok, #sslsocket{}, map()}| {error, ssl:reason()}.
Expand Down
2 changes: 1 addition & 1 deletion lib/ssl/src/ssl_trace.erl
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ trace_profiles() ->
[{ssl,
[{listen,2}, {connect,3}, {handshake,2}, {close, 1}]},
{ssl_gen_statem,
[{connect, 8}, {close, 2}, {terminate_alert, 1}]},
[{close, 2}, {terminate_alert, 1}]},
{tls_client_connection,
[{initial_hello, 3}]},
{tls_server_connection,
Expand Down
38 changes: 25 additions & 13 deletions lib/ssl/src/tls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,41 @@ accept(ListenSocket, #config{transport_info = {Transport,_,_,_,_} = CbInfo,

upgrade(Socket, #config{transport_info = {Transport,_,_,_,_}= CbInfo,
ssl = SslOptions,
emulated = EmOpts, connection_cb = ConnectionCb}, Timeout) ->
emulated = EmOpts}, Timeout) ->
ok = setopts(Transport, Socket, tls_socket:internal_inet_values()),
case peername(Transport, Socket) of
{ok, {Address, Port}} ->
ssl_gen_statem:connect(ConnectionCb, Address, Port, Socket,
{SslOptions,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
{ok, {Host, Port}} ->
try tls_gen_connection:start_fsm(client, Host, Port, Socket,
{SslOptions,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout) of
Result ->
Result
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end;
{error, Error} ->
{error, Error}
end.

connect(Address, Port,
connect(Host, Port,
#config{transport_info = CbInfo, inet_user = UserOpts, ssl = SslOpts,
emulated = EmOpts, inet_ssl = SocketOpts, connection_cb = ConnetionCb},
emulated = EmOpts, inet_ssl = SocketOpts},
Timeout) ->
{Transport, _, _, _, _} = CbInfo,
try Transport:connect(Address, Port, SocketOpts, Timeout) of
try Transport:connect(Host, Port, SocketOpts, Timeout) of
{ok, Socket} ->
ssl_gen_statem:connect(ConnetionCb, Address, Port, Socket,
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout);
try tls_gen_connection:start_fsm(client, Host, Port, Socket,
{SslOpts,
emulated_socket_options(EmOpts, #socket_options{}), undefined},
self(), CbInfo, Timeout) of
Result ->
Result
catch
exit:{noproc, _} ->
{error, ssl_not_started}
end;
{error, Reason} ->
{error, Reason}
catch
Expand Down
8 changes: 4 additions & 4 deletions lib/ssl/test/dtls_api_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ client_restarts(Config) ->

ct:sleep(250),
?CT_LOG("Client second connect: ~p ~p~n", [Socket, CbInfo]),
{ok, NewSocket} = ssl_gen_statem:connect(ConnectionCb, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{ok, NewSocket} = dtls_gen_connection:start_fsm(client, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{replace, NewSocket}
end,

Expand Down Expand Up @@ -535,8 +535,8 @@ client_restarts_multiple_acceptors(Config) ->
SslOpts = {SslOpts0, #socket_options{}, undefined},
ct:sleep(250),
?CT_LOG("Client second connect: ~p ~p~n", [Socket, CbInfo]),
{ok, NewSocket} = ssl_gen_statem:connect(ConnectionCb, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{ok, NewSocket} = dtls_gen_connection:start_fsm(client, Address, CPort, IntSocket,
SslOpts, self(), CbInfo, infinity),
{replace, NewSocket}
end,

Expand Down
5 changes: 1 addition & 4 deletions lib/ssl/test/ssl_trace_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ tc_api_profile(Config) ->
tls_server_connection, initial_hello},
{" (client) <- tls_client_connection:initial_hello/3 returned",
tls_client_connection, initial_hello},
{" (client) <- ssl_gen_statem:connect/8 returned",
ssl_gen_statem, connect},
{" (client) <- ssl:connect/3 returned", ssl, connect},
{" (server) <- ssl:handshake/2 returned", ssl, handshake},
{" (client) <- tls_sender:init/3 returned", tls_sender, init},
Expand All @@ -161,8 +159,7 @@ tc_api_profile(Config) ->
"rle ('?') -> ssl:listen/2 (*server) Args",
"rle ('?') -> ssl:connect/3 (*client) Args",
"rle ('?') -> tls_sender:init/3 (*server)",
"rle ('?') -> tls_sender:init/3 (*client)",
"api (client) -> ssl_gen_statem:connect/8"]},
"rle ('?') -> tls_sender:init/3 (*client)"]},
TracesAfterDisconnect =
#{
call =>
Expand Down

0 comments on commit d8de66f

Please sign in to comment.