From d8de66f08da9ed0e6b3c8d2d369b66dc9d1c7a68 Mon Sep 17 00:00:00 2001 From: Ingela Anderton Andin Date: Fri, 6 Dec 2024 11:33:02 +0100 Subject: [PATCH] ssl: Remove unnecessary code generalization --- lib/ssl/src/dtls_socket.erl | 12 +++++----- lib/ssl/src/ssl.erl | 29 ++++++++++++++---------- lib/ssl/src/ssl_gen_statem.erl | 39 -------------------------------- lib/ssl/src/ssl_trace.erl | 2 +- lib/ssl/src/tls_socket.erl | 38 ++++++++++++++++++++----------- lib/ssl/test/dtls_api_SUITE.erl | 8 +++---- lib/ssl/test/ssl_trace_SUITE.erl | 5 +--- 7 files changed, 54 insertions(+), 79 deletions(-) diff --git a/lib/ssl/src/dtls_socket.erl b/lib/ssl/src/dtls_socket.erl index 02ef6cea3c08..44d1426e00d3 100644 --- a/lib/ssl/src/dtls_socket.erl +++ b/lib/ssl/src/dtls_socket.erl @@ -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. diff --git a/lib/ssl/src/ssl.erl b/lib/ssl/src/ssl.erl index 249d2c6194f4..c3021e11c70e 100644 --- a/lib/ssl/src/ssl.erl +++ b/lib/ssl/src/ssl.erl @@ -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) -> @@ -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. %%-------------------------------------------------------------------- @@ -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. %%-------------------------------------------------------------------- diff --git a/lib/ssl/src/ssl_gen_statem.erl b/lib/ssl/src/ssl_gen_statem.erl index b46c7640654a..0323d8f517c3 100644 --- a/lib/ssl/src/ssl_gen_statem.erl +++ b/lib/ssl/src/ssl_gen_statem.erl @@ -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, @@ -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()}. diff --git a/lib/ssl/src/ssl_trace.erl b/lib/ssl/src/ssl_trace.erl index 38650092b17b..3f1e91b0cbce 100644 --- a/lib/ssl/src/ssl_trace.erl +++ b/lib/ssl/src/ssl_trace.erl @@ -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, diff --git a/lib/ssl/src/tls_socket.erl b/lib/ssl/src/tls_socket.erl index 18f4ea288189..e558dd934e7a 100644 --- a/lib/ssl/src/tls_socket.erl +++ b/lib/ssl/src/tls_socket.erl @@ -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 diff --git a/lib/ssl/test/dtls_api_SUITE.erl b/lib/ssl/test/dtls_api_SUITE.erl index aecea455c82b..0fea7d64f78a 100644 --- a/lib/ssl/test/dtls_api_SUITE.erl +++ b/lib/ssl/test/dtls_api_SUITE.erl @@ -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, @@ -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, diff --git a/lib/ssl/test/ssl_trace_SUITE.erl b/lib/ssl/test/ssl_trace_SUITE.erl index 91cdbf5179ae..7b48e11b97e3 100644 --- a/lib/ssl/test/ssl_trace_SUITE.erl +++ b/lib/ssl/test/ssl_trace_SUITE.erl @@ -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}, @@ -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 =>