Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ssl:peername/1 (DTLS client sockets) #2786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/ssl/src/dtls_socket.erl
Expand Up @@ -111,10 +111,11 @@ close(gen_udp, {_Client, _Socket}) ->
close(Transport, {_Client, Socket}) ->
Transport:close(Socket).

socket(Pids, gen_udp = Transport, {{_, _}, Socket}, ConnectionCb) ->
socket(Pids, gen_udp = Transport,
PeerAndSock = {{_Host, _Port}, _Socket}, ConnectionCb) ->
#sslsocket{pid = Pids,
%% "The name "fd" is keept for backwards compatibility
fd = {Transport, Socket, ConnectionCb}};
fd = {Transport, PeerAndSock, ConnectionCb}};
socket(Pids, Transport, Socket, ConnectionCb) ->
#sslsocket{pid = Pids,
%% "The name "fd" is keept for backwards compatibility
Expand Down Expand Up @@ -179,14 +180,19 @@ getstat(gen_udp, Pid, Options) when is_pid(Pid) ->
dtls_packet_demux:getstat(Pid, Options);
getstat(gen_udp, {_,{_, Socket}}, Options) ->
inet:getstat(Socket, Options);
getstat(gen_udp, {_, Socket}, Options) ->
inet:getstat(Socket, Options);
getstat(gen_udp, Socket, Options) ->
inet:getstat(Socket, Options);
getstat(Transport, Socket, Options) ->
Transport:getstat(Socket, Options).

peername(_, undefined) ->
{error, enotconn};
peername(gen_udp, {_, {Client, _Socket}}) ->
{ok, Client};
peername(gen_udp, {Client, _Socket}) ->
{ok, Client};
peername(Transport, Socket) ->
Transport:peername(Socket).
sockname(gen_udp, {_, {_,Socket}}) ->
Expand Down
4 changes: 2 additions & 2 deletions lib/ssl/src/ssl_connection.erl
Expand Up @@ -193,10 +193,10 @@ socket_control(tls_connection = Connection, Socket, [Pid|_] = Pids, Transport, T
{error, Reason} ->
{error, Reason}
end;
socket_control(dtls_connection = Connection, {_, Socket}, [Pid|_] = Pids, Transport, Trackers) ->
socket_control(dtls_connection = Connection, {PeerAddrPort, Socket}, [Pid|_] = Pids, Transport, Trackers) ->
case Transport:controlling_process(Socket, Pid) of
ok ->
{ok, Connection:socket(Pids, Transport, Socket, Trackers)};
{ok, Connection:socket(Pids, Transport, {PeerAddrPort, Socket}, Trackers)};
{error, Reason} ->
{error, Reason}
end.
Expand Down
43 changes: 43 additions & 0 deletions lib/ssl/test/ssl_api_SUITE.erl
Expand Up @@ -66,6 +66,8 @@
hibernate_right_away/1,
listen_socket/0,
listen_socket/1,
peername/0,
peername/1,
recv_active/0,
recv_active/1,
recv_active_once/0,
Expand Down Expand Up @@ -250,6 +252,7 @@ gen_api_tests() ->
hibernate,
hibernate_right_away,
listen_socket,
peername,
recv_active,
recv_active_once,
recv_active_n,
Expand Down Expand Up @@ -1089,6 +1092,46 @@ listen_socket(Config) ->

ok = ssl:close(ListenSocket).

%%--------------------------------------------------------------------
peername() ->
[{doc,"Test API function peername/1"}].

peername(Config) when is_list(Config) ->
ClientOpts = ssl_test_lib:ssl_options(client_rsa_opts, Config),
ServerOpts = ssl_test_lib:ssl_options(server_rsa_opts, Config),
{ClientNode, ServerNode, Hostname} = ssl_test_lib:run_where(Config),

Server = ssl_test_lib:start_server(
[
{node, ServerNode}, {port, 0},
{from, self()},
{options, ServerOpts}]),
Port = ssl_test_lib:inet_port(Server),
{Client, CSocket} = ssl_test_lib:start_client(
[return_socket,
{node, ClientNode}, {port, Port},
{host, Hostname},
{from, self()},
{options, ClientOpts}]),

ct:log("Testcase ~p, Client ~p Server ~p ~n",
[self(), Client, Server]),

Server ! get_socket,
SSocket =
receive
{Server, {socket, Socket}} ->
Socket
end,

{ok, ServerPeer} = ssl:peername(SSocket),
ct:log("Server's peer: ~p~n", [ServerPeer]),
{ok, ClientPeer} = ssl:peername(CSocket),
ct:log("Client's peer: ~p~n", [ClientPeer]),

ssl_test_lib:close(Server),
ssl_test_lib:close(Client).

%%--------------------------------------------------------------------

recv_active() ->
Expand Down