Skip to content

Commit

Permalink
Merge pull request #211 from qzhuyan/dev/william/listener-registration
Browse files Browse the repository at this point in the history
Support listener registration
  • Loading branch information
qzhuyan authored Sep 5, 2023
2 parents 525ca33 + e49cc16 commit b6d3507
Show file tree
Hide file tree
Showing 17 changed files with 604 additions and 188 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ jobs:
- name: gdb bt
if: failure()
run: |
set -x
which gdb || sudo apt install gdb
corefile=$(find _build/test -name core)
if [ -n $corefile ]; then
corefile=$(find _build/test -name core.*)
if [ -n "$corefile" ]; then
echo "found corefile: $corefile";
gdb -ex bt $(erl -noshell -eval 'io:format(code:root_dir()),halt()')/erts-*/bin/beam.smp "${corefile}"
else
echo "No coredump found"
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ set(SOURCES
c_src/quicer_ctx.h
c_src/quicer_listener.c
c_src/quicer_listener.h
c_src/quicer_tls.c
c_src/quicer_tls.h
c_src/quicer_connection.c
c_src/quicer_connection.h
c_src/quicer_stream.c
Expand Down
112 changes: 88 additions & 24 deletions c_src/quicer_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,13 @@ atom_cipher_suite(QUIC_CIPHER_SUITE suite)
ERL_NIF_TERM
ServerLoadConfiguration(ErlNifEnv *env,
const ERL_NIF_TERM *option,
HQUIC Registration,
HQUIC *Configuration,
QUIC_CREDENTIAL_CONFIG *CredConfig)
{
QUIC_SETTINGS Settings = { 0 };

if (!isRegistered)
if (!isRegistered && (Registration == GRegistration))
{
return ATOM_REG_FAILED;
}
Expand All @@ -238,7 +239,7 @@ ServerLoadConfiguration(ErlNifEnv *env,
// and settings.
//
QUIC_STATUS Status = QUIC_STATUS_SUCCESS;
if (QUIC_FAILED(Status = MsQuic->ConfigurationOpen(GRegistration,
if (QUIC_FAILED(Status = MsQuic->ConfigurationOpen(Registration,
alpn_buffers,
alpn_buffer_length,
&Settings,
Expand Down Expand Up @@ -1173,15 +1174,33 @@ bool
parse_listen_on(ErlNifEnv *env, ERL_NIF_TERM elisten_on, QUIC_ADDR *Address)
{
char listen_on[INET6_ADDRSTRLEN + 6] = { 0 };
if (enif_get_string(
env, elisten_on, listen_on, INET6_ADDRSTRLEN + 6, ERL_NIF_LATIN1)
> 0)
int UdpPort = 0;

ErlNifTermType type = enif_term_type(env, elisten_on);
switch (type)
{
if ((QuicAddr4FromString(listen_on, Address)
|| QuicAddr6FromString(listen_on, Address)))
case ERL_NIF_TERM_TYPE_LIST:
if (enif_get_string(
env, elisten_on, listen_on, INET6_ADDRSTRLEN + 6, ERL_NIF_LATIN1)
> 0)
{
if ((QuicAddr4FromString(listen_on, Address)
|| QuicAddr6FromString(listen_on, Address)))
{
return TRUE;
}
}
break;
case ERL_NIF_TERM_TYPE_INTEGER:
if (enif_get_int(env, elisten_on, &UdpPort) && UdpPort >= 0)
{
QuicAddrSetFamily(Address, QUIC_ADDRESS_FAMILY_UNSPEC);
QuicAddrSetPort(Address, (uint16_t)UdpPort);
return TRUE;
}
break;
default:
break;
}
return FALSE;
}
Expand Down Expand Up @@ -2414,6 +2433,7 @@ set_config_opt(ErlNifEnv *env,
return res;
}

// @deprecated
int
get_str_from_map(ErlNifEnv *env,
ERL_NIF_TERM key,
Expand Down Expand Up @@ -2441,36 +2461,80 @@ get_str_from_map(ErlNifEnv *env,
return enif_get_string(env, tmp_term, buff, tmp_len + 1, ERL_NIF_LATIN1);
}

BOOLEAN
build_trustedstore(const char *cacertfile, X509_STORE **trusted_store)
/*
** Fill str_buffer with string value of key in map.
** In case str_buffer is NULL, then new memory will be allocated,
** and caller should free it after use.
**
** Returns NULL on error.
*/
char *
str_from_map(ErlNifEnv *env,
ERL_NIF_TERM key,
const ERL_NIF_TERM *map,
char *str_buffer,
unsigned int max_len)
{
X509_STORE *store = NULL;
X509_LOOKUP *lookup = NULL;
unsigned int len = 0;
ERL_NIF_TERM tmp_term;
BOOLEAN is_alloc = str_buffer == NULL;

if (!enif_get_map_value(env, *map, key, &tmp_term))
{
goto exit;
}

if (cacertfile == NULL)
if (ERL_NIF_TERM_TYPE_LIST != enif_term_type(env, tmp_term))
{
return FALSE;
goto exit;
}

store = X509_STORE_new();
if (store == NULL)
if ((!str_buffer && !enif_get_list_length(env, tmp_term, &len))
|| len > max_len)
{
return FALSE;
goto exit;
}
else
{
len = max_len;
}

lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
if (lookup == NULL)
if (is_alloc)
{
X509_STORE_free(store);
return FALSE;
str_buffer = (char *)malloc(len + 1);
}

if (!X509_LOOKUP_load_file(lookup, cacertfile, X509_FILETYPE_PEM))
if (enif_get_string(env, tmp_term, str_buffer, len + 1, ERL_NIF_LATIN1))
{
X509_STORE_free(store);
return FALSE;
return str_buffer;
}
else if (is_alloc)
{
free(str_buffer);
}

exit:
return NULL;
}

/*
* parse optional quic_registration, and store it in r_ctx
* return TRUE if quic_registration is present and valid or not present
* */
BOOLEAN
parse_registration(ErlNifEnv *env,
ERL_NIF_TERM options,
QuicerRegistrationCTX **r_ctx)
{
ERL_NIF_TERM tmp_term;
assert(*r_ctx == NULL);
if (enif_get_map_value(env, options, ATOM_QUIC_REGISTRATION, &tmp_term))
{
if (!enif_get_resource(env, tmp_term, ctx_reg_t, (void **)r_ctx))
{
return FALSE;
}
}

*trusted_store = store;
return TRUE;
}
8 changes: 6 additions & 2 deletions c_src/quicer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ QUIC_STATUS UpdateCredConfig(ErlNifEnv *env,

ERL_NIF_TERM ServerLoadConfiguration(ErlNifEnv *env,
const ERL_NIF_TERM *option,
HQUIC Registration,
HQUIC *Configuration,
QUIC_CREDENTIAL_CONFIG *Config);
ERL_NIF_TERM ClientLoadConfiguration(ErlNifEnv *env,
Expand Down Expand Up @@ -101,6 +102,11 @@ int get_str_from_map(ErlNifEnv *env,
const ERL_NIF_TERM *map,
char *buff,
unsigned max_len);
char *str_from_map(ErlNifEnv *env,
ERL_NIF_TERM key,
const ERL_NIF_TERM *map,
char *string_buffer,
unsigned int max_len);

ERL_NIF_TERM getopt3(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM setopt4(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
Expand All @@ -118,6 +124,4 @@ ERL_NIF_TERM set_connection_opt(ErlNifEnv *env,
ERL_NIF_TERM optval,
ERL_NIF_TERM elevel);

BOOLEAN build_trustedstore(const char *cacertfile, X509_STORE **trusted_store);

#endif // __QUICER_CONFIG_H_
1 change: 1 addition & 0 deletions c_src/quicer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
-------------------------------------------------------------------*/
#include "quicer_connection.h"
#include "quicer_ctx.h"
#include "quicer_tls.h"
#include <assert.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
Expand Down
12 changes: 12 additions & 0 deletions c_src/quicer_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ init_l_ctx()
l_ctx->trusted_store = NULL;
l_ctx->is_closed = TRUE;
l_ctx->allow_insecure = FALSE;
l_ctx->r_ctx = NULL;
return l_ctx;
}

Expand All @@ -80,6 +81,10 @@ deinit_l_ctx(QuicerListenerCTX *l_ctx)
{
destroy_config_ctx(l_ctx->config_resource);
}
if (l_ctx->r_ctx && l_ctx->r_ctx->Registration != GRegistration)
{
enif_release_resource(l_ctx->r_ctx);
}
enif_mutex_destroy(l_ctx->lock);
enif_free_env(l_ctx->env);
}
Expand All @@ -90,7 +95,14 @@ destroy_l_ctx(QuicerListenerCTX *l_ctx)
// @note, Destroy config asap as it holds rundown
// ref count in registration
destroy_config_ctx(l_ctx->config_resource);

if (l_ctx->r_ctx)
{
enif_release_resource(l_ctx->r_ctx);
l_ctx->r_ctx = NULL;
}
l_ctx->config_resource = NULL;
enif_demonitor_process(l_ctx->env, l_ctx, &l_ctx->owner_mon);
enif_release_resource(l_ctx);
}

Expand Down
2 changes: 2 additions & 0 deletions c_src/quicer_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ typedef struct QuicerListenerCTX
{
// config_resource is allocated in 'init_l_ctx'
QuicerConfigCTX *config_resource;
QuicerRegistrationCTX *r_ctx;
HQUIC Listener;
QUICER_ACCEPTOR_QUEUE *acceptor_queue;
ErlNifPid listenerPid;
ErlNifMonitor owner_mon;
ErlNifEnv *env;
ErlNifMutex *lock;
char *cacertfile;
Expand Down
Loading

0 comments on commit b6d3507

Please sign in to comment.