Skip to content

Commit

Permalink
feat: bring back async_handshake_1
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Dec 10, 2024
1 parent 79f5055 commit f878a31
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
75 changes: 38 additions & 37 deletions c_src/quicer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ async_accept2(ErlNifEnv *env,
const ERL_NIF_TERM argv[])
{
ERL_NIF_TERM listener = argv[0];
// @NOTE: since 0.2, we ignore argv[1]
QuicerListenerCTX *l_ctx = NULL;
if (!enif_get_resource(env, listener, ctx_listener_t, (void **)&l_ctx))
{
Expand Down Expand Up @@ -1061,51 +1062,31 @@ get_conn_rid1(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}

QUIC_STATUS
continue_connection_handshake(QuicerConnCTX *c_ctx, QUIC_SETTINGS *Settings)
continue_connection_handshake(QuicerConnCTX *c_ctx)
{
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;

if (!c_ctx)
{
return QUIC_STATUS_INTERNAL_ERROR;
}

if (!c_ctx->Connection)
{
return QUIC_STATUS_INVALID_STATE;
}

if (QUIC_FAILED(Status = MsQuic->ConnectionSetConfiguration(
c_ctx->Connection, c_ctx->config_ctx->Configuration)))
{
return Status;
}
CXPLAT_FRE_ASSERT(c_ctx);
CXPLAT_FRE_ASSERT(c_ctx->Connection);

// Apply connection owners' option overrides
Status = MsQuic->SetParam(c_ctx->Connection,
QUIC_PARAM_CONN_SETTINGS,
sizeof(QUIC_SETTINGS),
Settings);
Status = MsQuic->ConnectionSetConfiguration(
c_ctx->Connection, c_ctx->config_ctx->Configuration);
return Status;
}

ERL_NIF_TERM
async_handshake_2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
async_handshake_X(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])

{
QuicerConnCTX *c_ctx;
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
ERL_NIF_TERM res = ATOM_OK;
CXPLAT_FRE_ASSERT(argc == 2);
CXPLAT_FRE_ASSERT(argc == 1 || argc == 2);
ERL_NIF_TERM econn = argv[0];
ERL_NIF_TERM econn_opts = argv[1];

QUIC_SETTINGS Settings = { 0 };
ERL_NIF_TERM active_val = ATOM_TRUE;

// Set parm active is optional
enif_get_map_value(
env, econn_opts, ATOM_QUIC_STREAM_OPTS_ACTIVE, &active_val);

if (!enif_get_resource(env, econn, ctx_connection_t, (void **)&c_ctx))
{
return ERROR_TUPLE_2(ATOM_BADARG);
Expand All @@ -1118,21 +1099,41 @@ async_handshake_2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
return ERROR_TUPLE_2(ATOM_CLOSED);
}

if (!create_settings(env, &econn_opts, &Settings))
if (argc > 1)
{
return ERROR_TUPLE_2(ATOM_PARAM_ERROR);
}
ERL_NIF_TERM econn_opts = argv[1];
// Set parm active is optional
enif_get_map_value(
env, econn_opts, ATOM_QUIC_STREAM_OPTS_ACTIVE, &active_val);

if (QUIC_FAILED(Status = continue_connection_handshake(c_ctx, &Settings)))
{
res = ERROR_TUPLE_2(ATOM_STATUS(Status));
if (!create_settings(env, &econn_opts, &Settings))
{
res = ERROR_TUPLE_2(ATOM_PARAM_ERROR);
goto exit;
}

if (!set_owner_recv_mode(c_ctx->owner, env, active_val))
{
res = ERROR_TUPLE_2(ATOM_BADARG);
goto exit;
}

// Apply connection owners' option overrides
if (QUIC_FAILED(Status = MsQuic->SetParam(c_ctx->Connection,
QUIC_PARAM_CONN_SETTINGS,
sizeof(QUIC_SETTINGS),
&Settings)))
{
res = ERROR_TUPLE_2(ATOM_STATUS(Status));
goto exit;
}
}

if (!set_owner_recv_mode(c_ctx->owner, env, active_val))
if (QUIC_FAILED(Status = continue_connection_handshake(c_ctx)))
{
return ERROR_TUPLE_2(ATOM_BADARG);
res = ERROR_TUPLE_2(ATOM_STATUS(Status));
}

exit:
put_conn_handle(c_ctx);
return res;
}
Expand Down
5 changes: 2 additions & 3 deletions c_src/quicer_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ ERL_NIF_TERM
get_conn_rid1(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

ERL_NIF_TERM
async_handshake_2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
async_handshake_X(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);

QUIC_STATUS continue_connection_handshake(QuicerConnCTX *c_ctx,
QUIC_SETTINGS *settings);
QUIC_STATUS continue_connection_handshake(QuicerConnCTX *c_ctx);

ERL_NIF_TERM
peercert1(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
Expand Down
3 changes: 2 additions & 1 deletion c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,8 @@ static ErlNifFunc nif_funcs[] = {
{ "open_connection", 1, open_connectionX, 0},
{ "async_connect", 3, async_connect3, 0},
{ "async_accept", 2, async_accept2, 0},
{ "async_handshake", 2, async_handshake_2, 0},
{ "async_handshake", 1, async_handshake_X, 0},
{ "async_handshake", 2, async_handshake_X, 0},
{ "async_shutdown_connection", 3, shutdown_connection3, 0},
{ "async_accept_stream", 2, async_accept_stream2, 0},
{ "start_stream", 2, async_start_stream2, 0},
Expand Down
25 changes: 22 additions & 3 deletions src/quicer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,22 @@ async_connect(Host, Port, Opts) when is_map(Opts) ->
{ok, connection_handle()}
| {error, any()}.
handshake(Conn) ->
handshake(Conn, #{}, 5000).
handshake(Conn, 5000).

-spec handshake(connection_handle(), timeout()) ->
{ok, connection_handle()} | {error, any()}.
handshake(Conn, Timeout) ->
handshake(Conn, #{}, Timeout).
case async_handshake(Conn) of
{error, _} = E ->
E;
ok ->
receive
{quic, connected, Conn, _} -> {ok, Conn};
{quic, closed, Conn, _Flags} -> {error, closed}
after Timeout ->
{error, timeout}
end
end.

%% @doc Complete TLS handshake after accepted a Connection
%% @see handshake/2
Expand All @@ -477,11 +487,20 @@ handshake(Conn, ConnOpts, Timeout) ->
%% Caller should expect to receive ```{quic, connected, connection_handle()}'''
%%
%% @see handshake/2
%% @see async_handshake/2
-spec async_handshake(connection_handle()) -> ok | {error, any()}.
async_handshake(Conn) ->
quicer_nif:async_handshake(Conn, #{}).
quicer_nif:async_handshake(Conn).

%% @doc Complete TLS handshake after accepted a Connection.
%% also set connection options which override the default listener options.
%% Caller should expect to receive ```{quic, connected, connection_handle()}'''
%%
%% @see handshake/2
%% @see async_handshake/1
-spec async_handshake(connection_handle(), conn_opts()) -> ok | {error, any()}.
async_handshake(Conn, ConnOpts) when is_list(ConnOpts) ->
async_handshake(Conn, maps:from_list(ConnOpts));
async_handshake(Conn, ConnOpts) ->
quicer_nif:async_handshake(Conn, ConnOpts).

Expand Down
5 changes: 5 additions & 0 deletions src/quicer_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
close_listener/1,
async_connect/3,
async_accept/2,
async_handshake/1,
async_handshake/2,
async_shutdown_connection/3,
async_accept_stream/2,
Expand Down Expand Up @@ -272,6 +273,10 @@ async_connect(_Host, _Port, _Opts) ->
async_accept(_Listener, _Opts) ->
erlang:nif_error(nif_library_not_loaded).

-spec async_handshake(connection_handle()) ->
ok | {error, badarg | atom_reason()}.
async_handshake(_Connection) ->
erlang:nif_error(nif_library_not_loaded).
-spec async_handshake(connection_handle(), conn_opts()) ->
ok | {error, badarg | atom_reason()}.
async_handshake(_Connection, _ConnOpts) ->
Expand Down
2 changes: 1 addition & 1 deletion test/quicer_snb_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ tc_slow_conn(Config) ->
},
#{
?snk_kind := debug,
function := "async_handshake_2",
function := "async_handshake_X",
tag := "start",
mark := 0,
resource_id := _Rid
Expand Down

0 comments on commit f878a31

Please sign in to comment.