Skip to content

Commit

Permalink
feat: conn with local addr specified
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Oct 10, 2021
1 parent 38f8f80 commit 2bbffed
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
7 changes: 1 addition & 6 deletions c_src/quicer_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ static ERL_NIF_TERM get_connection_opt(ErlNifEnv *env,
QuicerConnCTX *c_ctx,
ERL_NIF_TERM optname,
ERL_NIF_TERM elevel);
static ERL_NIF_TERM set_connection_opt(ErlNifEnv *env,
QuicerConnCTX *c_ctx,
ERL_NIF_TERM optname,
ERL_NIF_TERM optval,
ERL_NIF_TERM elevel);

static ERL_NIF_TERM get_listener_opt(ErlNifEnv *env,
QuicerListenerCTX *l_ctx,
Expand Down Expand Up @@ -1454,7 +1449,7 @@ get_connection_opt(ErlNifEnv *env,
return res;
}

static ERL_NIF_TERM
ERL_NIF_TERM
set_connection_opt(ErlNifEnv *env,
QuicerConnCTX *c_ctx,
ERL_NIF_TERM optname,
Expand Down
6 changes: 6 additions & 0 deletions c_src/quicer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ bool create_settings(ErlNifEnv *env,
bool
parse_listen_on(ErlNifEnv *env, ERL_NIF_TERM elisten_on, QUIC_ADDR *Address);

ERL_NIF_TERM set_connection_opt(ErlNifEnv *env,
QuicerConnCTX *c_ctx,
ERL_NIF_TERM optname,
ERL_NIF_TERM optval,
ERL_NIF_TERM elevel);

#endif // __QUICER_CONFIG_H_
16 changes: 16 additions & 0 deletions c_src/quicer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,22 @@ async_connect3(ErlNifEnv *env,
}
}

ERL_NIF_TERM evalue;
if (enif_get_map_value(
env, eoptions, ATOM_QUIC_PARAM_CONN_LOCAL_ADDRESS, &evalue))
{
if (!IS_SAME_TERM(ATOM_OK,
set_connection_opt(env,
c_ctx,
ATOM_QUIC_PARAM_CONN_LOCAL_ADDRESS,
evalue,
ATOM_FALSE)))
{
destroy_c_ctx(c_ctx);
return ERROR_TUPLE_2(ATOM_CONN_OPEN_ERROR);
}
}

if (QUIC_FAILED(Status = MsQuic->ConnectionStart(c_ctx->Connection,
c_ctx->Configuration,
QUIC_ADDRESS_FAMILY_UNSPEC,
Expand Down
21 changes: 21 additions & 0 deletions test/quicer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
, tc_conn_basic_slow_start/1
, tc_conn_double_close/1
, tc_conn_other_port/1
, tc_conn_with_localaddr/1
, tc_conn_controlling_process/1

, tc_stream_client_init/1
Expand Down Expand Up @@ -373,6 +374,26 @@ tc_conn_other_port(Config)->
ct:fail("timeout")
end.

tc_conn_with_localaddr(Config)->
Port = 5568,
Owner = self(),
{SPid, Ref} = spawn_monitor(fun() -> simple_conn_server(Owner, Config, Port) end),

{ok, CPort0} = gen_udp:open(0, [{ip, {127, 0, 0, 1}}]),
{ok, {{127, 0, 0, 1}, PortX}} = inet:sockname(CPort0),
ok = gen_udp:close(CPort0),
receive
listener_ready ->
{ok, Conn} = quicer:connect("127.0.0.1", Port, [{param_conn_local_address, "127.0.0.1:" ++ integer_to_list(PortX)}
| default_conn_opts()], 5000),
?assertEqual({ok, {{127,0,0,1}, PortX}}, quicer:sockname(Conn)),
ok = quicer:close_connection(Conn),
SPid ! done,
ok = ensure_server_exit_normal(Ref)
after 1000 ->
ct:fail("timeout")
end.

tc_stream_client_init(Config) ->
Port = 4568,
Owner = self(),
Expand Down

0 comments on commit 2bbffed

Please sign in to comment.