Skip to content

Commit

Permalink
impl: global registration context (#220)
Browse files Browse the repository at this point in the history
* feat(reg): intro G_r_ctx for global registration

* fix: thread safe msquic API table and global registration

* fix: cacertifle leak in listener neg cases
  • Loading branch information
qzhuyan authored Sep 27, 2023
1 parent 3e8f713 commit 42e896d
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 163 deletions.
6 changes: 3 additions & 3 deletions c_src/quicer_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ limitations under the License.
#include "quicer_tls.h"
#include <msquichelper.h>

extern BOOLEAN isRegistered;
extern QuicerRegistrationCTX *G_r_ctx;

static ERL_NIF_TERM get_stream_opt(ErlNifEnv *env,
QuicerStreamCTX *s_ctx,
Expand Down Expand Up @@ -217,7 +217,7 @@ ServerLoadConfiguration(ErlNifEnv *env,
{
QUIC_SETTINGS Settings = { 0 };

if (!isRegistered && (Registration == GRegistration))
if (!G_r_ctx)
{
return ATOM_REG_FAILED;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ ClientLoadConfiguration(ErlNifEnv *env,
QUIC_SETTINGS Settings = { 0 };
ERL_NIF_TERM ret = ATOM_OK;

if (!isRegistered && (Registration == GRegistration))
if (!G_r_ctx)
{
return ATOM_REG_FAILED;
}
Expand Down
38 changes: 34 additions & 4 deletions c_src/quicer_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ limitations under the License.
#include <openssl/x509.h>
#include <unistd.h>

extern QuicerRegistrationCTX *G_r_ctx;

#ifdef DEBUG
extern inline void
EncodeHexBuffer(uint8_t *Buffer, uint8_t BufferLen, char *HexString);
Expand Down Expand Up @@ -505,7 +507,14 @@ open_connectionX(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])

if (argc == 0)
{
registration = GRegistration;
if (G_r_ctx)
{
registration = G_r_ctx->Registration;
}
else
{
return ERROR_TUPLE_2(ATOM_QUIC_REGISTRATION);
}
r_ctx = NULL;
}
else
Expand All @@ -522,7 +531,14 @@ open_connectionX(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
else
{
registration = GRegistration;
if (G_r_ctx)
{
registration = G_r_ctx->Registration;
}
else
{
return ERROR_TUPLE_2(ATOM_QUIC_REGISTRATION);
}
}
}

Expand Down Expand Up @@ -619,7 +635,14 @@ async_connect3(ErlNifEnv *env,
}
else
{
Registration = GRegistration;
if (G_r_ctx)
{
Registration = G_r_ctx->Registration;
}
else
{
return ERROR_TUPLE_2(ATOM_REG_FAILED);
}
}
}
else
Expand Down Expand Up @@ -649,7 +672,14 @@ async_connect3(ErlNifEnv *env,
{
// quic_registration is not set, use global registration
// msquic should reject if global registration is NULL (closed)
Registration = GRegistration;
if (G_r_ctx && G_r_ctx->Registration)
{
Registration = G_r_ctx->Registration;
}
else
{
Registration = NULL;
}
}

if ((c_ctx->owner = AcceptorAlloc()) == NULL)
Expand Down
3 changes: 2 additions & 1 deletion c_src/quicer_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#include "quicer_ctx.h"

// alloc/dealloc ctx should be done in the callbacks.
extern QuicerRegistrationCTX *G_r_ctx;

QuicerRegistrationCTX *
init_r_ctx()
Expand Down Expand Up @@ -81,7 +82,7 @@ deinit_l_ctx(QuicerListenerCTX *l_ctx)
{
destroy_config_ctx(l_ctx->config_resource);
}
if (l_ctx->r_ctx && l_ctx->r_ctx->Registration != GRegistration)
if (l_ctx->r_ctx && l_ctx->r_ctx != G_r_ctx)
{
enif_release_resource(l_ctx->r_ctx);
}
Expand Down
1 change: 1 addition & 0 deletions c_src/quicer_eterms.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern ERL_NIF_TERM ATOM_BAD_MON;
extern ERL_NIF_TERM ATOM_LISTENER_OPEN_ERROR;
extern ERL_NIF_TERM ATOM_LISTENER_START_ERROR;
extern ERL_NIF_TERM ATOM_BADARG;
extern ERL_NIF_TERM ATOM_LIB_UNINITIALIZED;
extern ERL_NIF_TERM ATOM_CONN_OPEN_ERROR;
extern ERL_NIF_TERM ATOM_CONN_START_ERROR;
extern ERL_NIF_TERM ATOM_STREAM_OPEN_ERROR;
Expand Down
15 changes: 14 additions & 1 deletion c_src/quicer_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ limitations under the License.
#include <openssl/pem.h>
#include <openssl/x509.h>

extern QuicerRegistrationCTX *G_r_ctx;

BOOLEAN parse_registration(ErlNifEnv *env,
ERL_NIF_TERM options,
QuicerRegistrationCTX **r_ctx);
Expand Down Expand Up @@ -274,6 +276,7 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
if (!parse_cacertfile_option(env, options, &cacertfile))
{
// TLS opt error not file content error
free(cacertfile);
return ERROR_TUPLE_2(ATOM_CACERTFILE);
}

Expand All @@ -282,6 +285,7 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])

if (!l_ctx)
{
free(cacertfile);
return ERROR_TUPLE_2(ATOM_ERROR_NOT_ENOUGH_MEMORY);
}

Expand Down Expand Up @@ -325,7 +329,15 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
{
// quic_registration is not set, use global registration
// msquic should reject if global registration is NULL (closed)
Registration = GRegistration;
if (G_r_ctx)
{
Registration = G_r_ctx->Registration;
}
else
{
ret = ERROR_TUPLE_2(ATOM_QUIC_REGISTRATION);
goto exit;
}
}

// Now load server config
Expand Down Expand Up @@ -400,6 +412,7 @@ listen2(ErlNifEnv *env, __unused_parm__ int argc, const ERL_NIF_TERM argv[])
return OK_TUPLE_2(listenHandle);

exit: // errors..
free(cacertfile);
free_certificate(&CredConfig);
destroy_l_ctx(l_ctx);
return ret;
Expand Down
Loading

0 comments on commit 42e896d

Please sign in to comment.