Skip to content

Commit

Permalink
Merge pull request #181 from emqx/1013-fix-port-command-for-otp-26
Browse files Browse the repository at this point in the history
fix(esockd_transport): make it work with OTP 26
  • Loading branch information
zmstone committed Oct 13, 2023
2 parents e794498 + 4b85060 commit e150e09
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 61 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/run_test_case.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
otp:
- erlang:23.0.3
- erlang:22.3
- erlang:21.3
- erlang:26
- erlang:25
- erlang:24

container:
image: ${{ matrix.otp }}
Expand Down
8 changes: 7 additions & 1 deletion include/esockd.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@

-define(IS_PROXY(Sock), is_record(Sock, proxy_socket)).

-endif.
-if(?OTP_RELEASE >= 26).
-type ssl_option() :: ssl:tls_option().
-else.
-type ssl_option() :: ssl:ssl_option().
-endif. % OTP_RELEASE

-endif. % ESOCKD_HRL
34 changes: 17 additions & 17 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@

{src_dirs, ["src"]}.

{extra_src_dirs, ["examples/client",
"examples/active_n",
"examples/async_recv",
"examples/client",
"examples/dtls",
"examples/dtls_psk",
"examples/gen_server",
"examples/plain",
"examples/proxy_protocol",
"examples/simple",
"examples/tls",
"examples/tcp_window",
"examples/udp"
]}.

{cover_enabled, true}.
{cover_opts, [verbose]}.
{cover_export_enabled, true}.

{dialyzer, [{warnings, [unmatched_returns,
error_handling,
race_conditions]}]}.
error_handling]}]}.

{profiles,
[{test,
[{plugins, [{coveralls, {git, "https://github.com/emqx/coveralls-erl", {branch, "github"}}}]},
{deps, [{meck, "0.8.13"}]},
{erl_opts, [debug_info]}
{erl_opts, [debug_info]},
{extra_src_dirs,
["examples/client",
"examples/active_n",
"examples/async_recv",
"examples/client",
"examples/dtls",
"examples/dtls_psk",
"examples/gen_server",
"examples/plain",
"examples/proxy_protocol",
"examples/simple",
"examples/tls",
"examples/tcp_window",
"examples/udp"
]}
]}
]}.
2 changes: 1 addition & 1 deletion src/esockd.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{vsn, "5.9.7"},
{modules, []},
{registered, []},
{applications, [kernel, stdlib, sasl, ssl]},
{applications, [kernel, stdlib, sasl, ssl, public_key]},
{mod, {esockd_app, []}},
{env, []},
{licenses, ["Apache-2.0"]},
Expand Down
19 changes: 15 additions & 4 deletions src/esockd.appup.src
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
%%-*- mode: erlang; -*-

{"5.9.7",
[{"5.9.6",
{"5.9.8",
[
{"5.9.7",
[ {load_module,esockd_transport,brutal_purge,soft_purge,[]}
]
},
{"5.9.6",
[ {load_module,esockd_proxy_protocol,brutal_purge,soft_purge,[]}
, {load_module,esockd_connection_sup,brutal_purge,soft_purge,[]}
, {load_module,esockd_transport,brutal_purge,soft_purge,[]}
]
},
{"5.9.5",
Expand Down Expand Up @@ -57,9 +62,15 @@
},
{<<".*">>, []}
],
[{"5.9.6",
[
{"5.9.7",
[ {load_module,esockd_transport,brutal_purge,soft_purge,[]}
]
},
{"5.9.6",
[ {load_module,esockd_proxy_protocol,brutal_purge,soft_purge,[]}
, {load_module,esockd_connection_sup,brutal_purge,soft_purge,[]}
, {load_module,esockd_transport,brutal_purge,soft_purge,[]}
]
},
{"5.9.5",
Expand Down
4 changes: 2 additions & 2 deletions src/esockd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@

-type(host() :: inet:ip_address() | string()).
-type(listen_on() :: inet:port_number() | {host(), inet:port_number()}).
-type ssl_options() :: [{handshake_timeout, pos_integer()} | ssl:ssl_option()].
-type dtls_options() :: [{handshake_timeout, pos_integer()} | ssl:ssl_option()].
-type ssl_options() :: [{handshake_timeout, pos_integer()} | ssl_option()].
-type dtls_options() :: [{handshake_timeout, pos_integer()} | ssl_option()].

%%--------------------------------------------------------------------
%% APIs
Expand Down
3 changes: 1 addition & 2 deletions src/esockd_generic_limiter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-module(esockd_generic_limiter).

-export([create/1, consume/2, delete/1]).
-export_type([limiter/0, create_options/0, consume_result/0]).

-type pause_time() :: non_neg_integer().

Expand All @@ -40,8 +41,6 @@

-callback delete(limiter()) -> ok.

-export_type([limiter/0, create_options/0, consume_result/0]).

%%--------------------------------------------------------------------
%% Callbacks
%%--------------------------------------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions src/esockd_limiter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,9 @@ pause_time(Name, Now) ->
max(1, LastTime + (Interval * 1000) - Now)
end.

%% The dialyzer cannot support this type specification
-dialyzer({nowarn_function, delete/1}).
-spec delete(esockd_generic_limiter:limter()) -> ok;
(bucket_name()) -> ok.
-spec delete(esockd_generic_limiter:limiter() | bucket_name()) -> ok.
delete(#{name := Name}) ->
delete(Name);

delete(Name) ->
gen_server:cast(?SERVER, {delete, Name}).

Expand Down
40 changes: 30 additions & 10 deletions src/esockd_transport.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,41 @@ async_send(Sock, Data) ->
-spec(async_send(socket(), iodata(), [force|nosuspend]) -> ok | {error, Reason} when
Reason :: close | timeout | inet:posix()).
async_send(Sock, Data, Options) when is_port(Sock) ->
do_port_command(Sock, Data, Options);
async_send(Sock = #ssl_socket{ssl = SslSock}, Data, _Options) ->
case ssl:send(SslSock, Data) of
ok ->
%% TODO: get rid of this message send
self() ! {inet_reply, Sock, ok},
ok;
Error ->
Error
end;
async_send(#proxy_socket{socket = Sock}, Data, _Options) ->
async_send(Sock, Data).

-if(?OTP_RELEASE >= 26).
do_port_command(Sock, Data, _Options) ->
case gen_tcp:send(Sock, Data) of
ok ->
%% TODO: get rid of this message send
self() ! {inet_reply, Sock, ok},
ok;
{error, Reason} ->
{error, Reason}
end.
-else.
do_port_command(Sock, Data, Options) ->
try erlang:port_command(Sock, Data, Options) of
true -> ok;
true ->
ok;
false -> %% nosuspend option and port busy
{error, busy}
catch
error:_Error ->
{error, einval}
end;
async_send(Sock = #ssl_socket{ssl = SslSock}, Data, _Options) ->
case ssl:send(SslSock, Data) of
ok -> self() ! {inet_reply, Sock, ok}, ok;
Error -> Error
end;
async_send(#proxy_socket{socket = Sock}, Data, _Options) ->
async_send(Sock, Data).
end.
-endif.

-spec(recv(socket(), non_neg_integer())
-> {ok, iodata()} | {error, closed | inet:posix()}).
Expand Down Expand Up @@ -358,7 +378,7 @@ shutdown(#proxy_socket{socket = Sock}, How) ->
shutdown(Sock, How).

%% @doc TCP/DTLS socket -> #ssl_socket{}
-spec(ssl_upgrade_fun([ssl:ssl_option()]) -> esockd:sock_fun()).
-spec(ssl_upgrade_fun([ssl_option()]) -> esockd:sock_fun()).
ssl_upgrade_fun(SslOpts) ->
{Timeout, SslOpts1} = take_handshake_timeout(SslOpts),
{GCAfterHandshake, SslOpts2} = take_gc_after_handshake(SslOpts1),
Expand Down
22 changes: 14 additions & 8 deletions test/esockd_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ t_open_dtls(Config) ->
DtlsOpts = [{mode, binary},
{reuseaddr, true},
{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)}
{keyfile, esockd_ct:keyfile(Config)},
{verify, verify_none}
],
{ok, _} = esockd:open_dtls(echo, 5000, [{dtls_options, DtlsOpts}],
{dtls_echo_server, start_link, []}),
{ok, Sock} = ssl:connect({127,0,0,1}, 5000, [binary,
{protocol, dtls},
{active, false}
{active, false},
{verify, verify_none}
], 5000),
ok = ssl:send(Sock, <<"Hi">>),
{ok, <<"Hi">>} = ssl:recv(Sock, 0, 3000),
Expand Down Expand Up @@ -242,11 +244,13 @@ t_get_current_connections(Config) ->
DtlsOpts = [{mode, binary},
{reuseaddr, true},
{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)}
{keyfile, esockd_ct:keyfile(Config)},
{verify, verify_none}
],
ClientOpts = [binary, {protocol, dtls}, {verify, verify_none}],
{ok, _LSup1} = esockd:open_dtls(dtls_echo, 7000, [{dtls_options, DtlsOpts}], {dtls_echo_server, start_link, []}),
{ok, DtlsSock1} = ssl:connect({127,0,0,1}, 7000, [binary, {protocol, dtls}], 5000),
{ok, DtlsSock2} = ssl:connect({127,0,0,1}, 7000, [binary, {protocol, dtls}], 5000),
{ok, DtlsSock1} = ssl:connect({127,0,0,1}, 7000, ClientOpts, 5000),
{ok, DtlsSock2} = ssl:connect({127,0,0,1}, 7000, ClientOpts, 5000),
timer:sleep(10),
?assertEqual(2, esockd:get_current_connections({dtls_echo, 7000})),
ok = ssl:close(DtlsSock1),
Expand Down Expand Up @@ -277,11 +281,13 @@ t_get_shutdown_count(Config) ->
DtlsOpts = [{mode, binary},
{reuseaddr, true},
{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)}
{keyfile, esockd_ct:keyfile(Config)},
{verify, verify_none}
],
ClientOpts = [binary, {protocol, dtls}, {verify, verify_none}],
{ok, _LSup1} = esockd:open_dtls(dtls_echo, 7000, [{dtls_options, DtlsOpts}], {dtls_echo_server, start_link, []}),
{ok, DtlsSock1} = ssl:connect({127,0,0,1}, 7000, [binary, {protocol, dtls}], 5000),
{ok, DtlsSock2} = ssl:connect({127,0,0,1}, 7000, [binary, {protocol, dtls}], 5000),
{ok, DtlsSock1} = ssl:connect({127,0,0,1}, 7000, ClientOpts, 5000),
{ok, DtlsSock2} = ssl:connect({127,0,0,1}, 7000, ClientOpts, 5000),
ok = ssl:close(DtlsSock1),
ok = ssl:close(DtlsSock2),
timer:sleep(200),
Expand Down
9 changes: 7 additions & 2 deletions test/esockd_dtls_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ t_dtls_server(Config) ->
DtlsOpts = [{mode, binary},
{reuseaddr, true},
{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)}
{keyfile, esockd_ct:keyfile(Config)},
{verify, verify_none}
],
Options = [{acceptors, 4},
{max_connections, 1000},
{max_conn_rate, 10},
{dtls_options, DtlsOpts}],

ClientOpts = [binary,
{protocol, dtls},
{active, false},
{verify, verify_none}],
{ok, _} = esockd:open_dtls('echo/dtls', 9876, Options, {?MODULE, dtls_echo_init, []}),
{ok, Sock} = ssl:connect({127,0,0,1}, 9876, [binary, {protocol, dtls}, {active, false}], 5000),
{ok, Sock} = ssl:connect({127,0,0,1}, 9876, ClientOpts, 5000),
ok = ssl:send(Sock, <<"hello">>),
{ok, <<"hello">>} = ssl:recv(Sock, 5, 3000),
ok = ssl:send(Sock, <<"world">>),
Expand Down
19 changes: 13 additions & 6 deletions test/esockd_transport_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ t_send_recv_tcp(_) ->
t_send_ssl(Config) ->
ssl:start(),
SslOpts = [{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)}],
{keyfile, esockd_ct:keyfile(Config)},
{verify, verify_none}
],
{ok, _} = esockd:open(echo, 8883, [{ssl_options, SslOpts}], {echo_server, start_link, []}),
{ok, SslSock} = ssl:connect({127,0,0,1}, 8883, [], 3000),
{ok, SslSock} = ssl:connect({127,0,0,1}, 8883, [{verify, verify_none}], 3000),
ok = esockd_transport:send(#ssl_socket{ssl = SslSock}, <<"Hello">>),
ok = esockd_transport:close(#ssl_socket{ssl = SslSock}),
ok = esockd:close(echo, 8883).
Expand All @@ -101,9 +103,11 @@ t_send_ssl_gc_after_handshake(Config) ->
ssl:start(),
SslOpts = [{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)},
{gc_after_handshake, true}],
{gc_after_handshake, true},
{verify, verify_none}
],
{ok, _} = esockd:open(echo, 8883, [{ssl_options, SslOpts}], {echo_server, start_link, []}),
{ok, SslSock} = ssl:connect({127,0,0,1}, 8883, [], 3000),
{ok, SslSock} = ssl:connect({127,0,0,1}, 8883, [{verify, verify_none}], 3000),
ok = esockd_transport:send(#ssl_socket{ssl = SslSock}, <<"Hello">>),
ok = esockd_transport:close(#ssl_socket{ssl = SslSock}),
ok = esockd:close(echo, 8883).
Expand Down Expand Up @@ -220,9 +224,12 @@ t_peersni_ssl_disabled_sni(Config) ->
ssl:start(),
SslOpts = [{certfile, esockd_ct:certfile(Config)},
{keyfile, esockd_ct:keyfile(Config)},
{gc_after_handshake, true}],
{gc_after_handshake, true},
{verify, verify_none}
],
ClientSslOpts = [{server_name_indication, disable}, {verify, verify_none}],
{ok, _} = esockd:open(echo, 8883, [{ssl_options, SslOpts}], {?MODULE, start_link_peersni, [disable]}),
{ok, SslSock} = ssl:connect("localhost", 8883, [{server_name_indication, disable}], 3000),
{ok, SslSock} = ssl:connect("localhost", 8883, ClientSslOpts, 3000),
ok = ssl:send(SslSock, <<"Hello">>),
receive
{ssl, _, "Hello"} -> ok
Expand Down

0 comments on commit e150e09

Please sign in to comment.