Skip to content

Commit

Permalink
ssl: Refactor sslsocket to ease maintenance and extension
Browse files Browse the repository at this point in the history
Backwards compatibility of this internal data structure is
no longer needed and nowadays only makes it hard to understand the
intent of the code. So this is a refactor long overdue.
  • Loading branch information
IngelaAndin committed Sep 11, 2024
1 parent 3651d52 commit 1ac86be
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 184 deletions.
45 changes: 26 additions & 19 deletions lib/ssl/src/dtls_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ listen(Port, #config{inet_ssl = SockOpts,
Error
end.

accept(dtls, #config{transport_info = {Transport,_,_,_,_},
connection_cb = ConnectionCb,
dtls_handler = {Listener, _}}, _Timeout) ->
accept({Listener,_}, #config{transport_info = Info,
connection_cb = ConnectionCb}, _Timeout) ->
Transport = element(1, Info),
case dtls_packet_demux:accept(Listener, self()) of
{ok, Pid, Socket} ->
{ok, socket([Pid], Transport, {Listener, Socket}, ConnectionCb)};
Expand All @@ -94,8 +94,8 @@ connect(Address, Port, #config{transport_info = {Transport, _, _, _, _} = CbInfo
Error
end.

close_listen(#sslsocket{pid = {dtls, #config{dtls_handler = {Pid, Port0},
inet_ssl = SockOpts}}}, Timeout) ->
close_listen(#sslsocket{socket_handle = {Pid, Port0},
listener_config = #config{inet_ssl = SockOpts}}, Timeout) ->
IP = proplists:get_value(ip, SockOpts, default_ip(SockOpts)),
Port = get_real_port(Pid, Port0),
dtls_listener_sup:register_listener({undefined, Pid}, IP, Port),
Expand Down Expand Up @@ -124,16 +124,21 @@ close(gen_udp, {_Client, _Socket}) ->
close(Transport, {_Client, Socket}) ->
Transport:close(Socket).

socket(Pids, gen_udp = Transport,
socket([Pid], gen_udp = Transport,
PeerAndSock = {{_Host, _Port}, _Socket}, ConnectionCb) ->
#sslsocket{pid = Pids,
%% "The name "fd" is kept for backwards compatibility
fd = {Transport, PeerAndSock, ConnectionCb}};
socket(Pids, Transport, Socket, ConnectionCb) ->
#sslsocket{pid = Pids,
%% "The name "fd" is kept for backwards compatibility
fd = {Transport, Socket, ConnectionCb}}.
setopts(_, Socket = #sslsocket{pid = {dtls, #config{dtls_handler = {ListenPid, _}}}}, Options) ->
#sslsocket{socket_handle = PeerAndSock,
connection_handler = Pid,
payload_sender = Pid,
transport_cb = Transport,
connection_cb = ConnectionCb};
socket([Pid], Transport, Socket, ConnectionCb) ->
#sslsocket{socket_handle = Socket,
connection_handler = Pid,
payload_sender = Pid,
transport_cb = Transport,
connection_cb = ConnectionCb}.
setopts(_, Socket = #sslsocket{socket_handle = {ListenPid, _},
listener_config = #config{}}, Options) ->
SplitOpts = {_, EmOpts} = tls_socket:split_options(Options),
check_active_n(EmOpts, Socket),
dtls_packet_demux:set_sock_opts(ListenPid, SplitOpts);
Expand All @@ -143,7 +148,8 @@ setopts(gen_udp, Socket, Options) ->
setopts(Transport, Socket, Options) ->
Transport:setopts(Socket, Options).

check_active_n(EmulatedOpts, Socket = #sslsocket{pid = {dtls, #config{dtls_handler = {ListenPid, _}}}}) ->
check_active_n(EmulatedOpts, Socket = #sslsocket{socket_handle = {ListenPid, _},
listener_config = #config{}}) ->
%% We check the resulting options to send an ssl_passive message if necessary.
case proplists:lookup(active, EmulatedOpts) of
%% The provided value is out of bound.
Expand All @@ -169,15 +175,15 @@ check_active_n(EmulatedOpts, Socket = #sslsocket{pid = {dtls, #config{dtls_handl
ok
end.

getopts(_, #sslsocket{pid = {dtls, #config{dtls_handler = {ListenPid, _}}}}, Options) ->
getopts(_, #sslsocket{socket_handle = {ListenPid, _}, listener_config =#config{}}, Options) ->
SplitOpts = tls_socket:split_options(Options),
dtls_packet_demux:get_sock_opts(ListenPid, SplitOpts);
getopts(gen_udp, #sslsocket{pid = {Socket, #config{emulated = EmOpts}}}, Options) ->
getopts(gen_udp, #sslsocket{socket_handle = {Socket, #config{emulated = EmOpts}}}, Options) ->
{SockOptNames, EmulatedOptNames} = tls_socket:split_options(Options),
EmulatedOpts = get_emulated_opts(EmOpts, EmulatedOptNames),
SocketOpts = tls_socket:get_socket_opts(Socket, SockOptNames, inet),
{ok, EmulatedOpts ++ SocketOpts};
getopts(_Transport, #sslsocket{pid = {Socket, #config{emulated = EmOpts}}}, Options) ->
getopts(_Transport, #sslsocket{socket_handle = {Socket, #config{emulated = EmOpts}}}, Options) ->
{SockOptNames, EmulatedOptNames} = tls_socket:split_options(Options),
EmulatedOpts = get_emulated_opts(EmOpts, EmulatedOptNames),
SocketOpts = tls_socket:get_socket_opts(Socket, SockOptNames, inet),
Expand Down Expand Up @@ -340,7 +346,8 @@ start_dtls_packet_demux(#config{
create_dtls_socket(#config{emulated = EmOpts} = Config,
Listener, Port) ->
Socket = #sslsocket{
pid = {dtls, Config#config{dtls_handler = {Listener, Port}}}},
socket_handle = {Listener, Port},
listener_config = Config},
check_active_n(EmOpts, Socket),
Socket.

8 changes: 4 additions & 4 deletions lib/ssl/src/inet_tls_dist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ hs_data_inet_tcp(Driver, Socket) ->
}
end}.

hs_data_ssl(Family, #sslsocket{pid = [_, DistCtrl|_]} = SslSocket) ->
hs_data_ssl(Family, #sslsocket{payload_sender = DistCtrl} = SslSocket) ->
{ok, Address} =
maybe
{error, einval} ?= ssl:peername(SslSocket),
Expand Down Expand Up @@ -347,7 +347,7 @@ accept_one(Family, Socket, NetKernel) ->
net_kernel:connecttime())
of
{ok, SslSocket} ->
Receiver = hd(SslSocket#sslsocket.pid),
Receiver = SslSocket#sslsocket.connection_handler,
case KTLS of
true ->
{ok, KtlsInfo} = ssl_gen_statem:ktls_handover(Receiver),
Expand Down Expand Up @@ -512,7 +512,7 @@ do_accept(
Timer = dist_util:start_timer(SetupTime),
{HSData0, NewAllowed} =
case DistSocket of
SslSocket = #sslsocket{pid = [_Receiver, Sender| _]} ->
SslSocket = #sslsocket{payload_sender = Sender} ->
link(Sender),
{hs_data_ssl(Family, SslSocket),
allowed_nodes(SslSocket, Allowed)};
Expand Down Expand Up @@ -647,7 +647,7 @@ do_setup(
KTLS = proplists:get_value(ktls, Opts, false),
dist_util:reset_timer(Timer),
maybe
{ok, #sslsocket{pid = [Receiver, Sender| _]} = SslSocket} ?=
{ok, #sslsocket{connection_handler = Receiver, payload_sender = Sender} = SslSocket} ?=
ssl:connect(Ip, PortNum, Opts, net_kernel:connecttime()),
HSData =
case KTLS of
Expand Down
18 changes: 3 additions & 15 deletions lib/ssl/src/ssl.appup.src
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
%% -*- erlang -*-
{"%VSN%",
[
[
{<<"11\\..*">>, [{restart_application, ssl}]},
{<<"10\\..*">>, [{restart_application, ssl}]},
{<<"9\\..*">>, [{restart_application, ssl}]},
{<<"8\\..*">>, [{restart_application, ssl}]},
{<<"7\\..*">>, [{restart_application, ssl}]},
{<<"6\\..*">>, [{restart_application, ssl}]},
{<<"5\\..*">>, [{restart_application, ssl}]},
{<<"4\\..*">>, [{restart_application, ssl}]},
{<<"3\\..*">>, [{restart_application, ssl}]}
{<<"9\\..*">>, [{restart_application, ssl}]}
],
[
{<<"11\\..*">>, [{restart_application, ssl}]},
{<<"10\\..*">>, [{restart_application, ssl}]},
{<<"9\\..*">>, [{restart_application, ssl}]},
{<<"8\\..*">>, [{restart_application, ssl}]},
{<<"7\\..*">>, [{restart_application, ssl}]},
{<<"6\\..*">>, [{restart_application, ssl}]},
{<<"5\\..*">>, [{restart_application, ssl}]},
{<<"4\\..*">>, [{restart_application, ssl}]},
{<<"3\\..*">>, [{restart_application, ssl}]}
{<<"9\\..*">>, [{restart_application, ssl}]}
]
}.
Loading

0 comments on commit 1ac86be

Please sign in to comment.