Skip to content

Commit

Permalink
make sure ziti_init happens on the event loop (#201)
Browse files Browse the repository at this point in the history
* make sure ziti_init happens on the event loop
  • Loading branch information
ekoby authored Dec 9, 2020
1 parent 516f22b commit 08f0480
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
3 changes: 0 additions & 3 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ struct ziti_ctx {
model_map channels;
LIST_HEAD(conns, ziti_conn) connections;

uv_async_t connect_async;
uint32_t conn_seq;

/* options */
Expand All @@ -198,8 +197,6 @@ struct ziti_ctx {
extern "C" {
#endif

int ziti_process_connect_reqs(ziti_context ztx);

int ziti_close_channels(ziti_context ztx);

int ziti_channel_connect(ziti_context ztx, const char *name, const char *url, ch_connect_cb, void *ctx);
Expand Down
6 changes: 0 additions & 6 deletions library/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,12 +1025,6 @@ int ziti_accept(ziti_connection conn, ziti_conn_cb cb, ziti_data_cb data_cb) {
return ZITI_OK;
}

int ziti_process_connect_reqs(ziti_context ztx) {
ZITI_LOG(WARN, "TODO");

return ZITI_OK;
}

static int send_fin_message(ziti_connection conn) {
ziti_channel_t *ch = conn->channel;
int32_t conn_id = htole32(conn->conn_id);
Expand Down
64 changes: 40 additions & 24 deletions library/ziti.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static void version_cb(ziti_version *v, ziti_error *err, void *ctx);

static void session_cb(ziti_session *session, ziti_error *err, void *ctx);

static void ziti_init_async(uv_async_t *ar);

static void grim_reaper(uv_prepare_t *p);

#define CONN_STATES(XX) \
Expand Down Expand Up @@ -130,7 +132,6 @@ static int parse_getopt(const char *q, const char *opt, char *out, size_t maxout

static void async_connects(uv_async_t *ar) {
ziti_context ztx = ar->data;
ziti_process_connect_reqs(ztx);
}

int load_tls(ziti_config *cfg, tls_context **ctx) {
Expand Down Expand Up @@ -179,16 +180,6 @@ int ziti_init_opts(ziti_options *options, uv_loop_t *loop, void *init_ctx) {
ziti_log_init(loop, ZITI_LOG_DEFAULT_LEVEL, NULL);
metrics_init(loop, 5);

uv_timeval64_t start_time;
uv_gettimeofday(&start_time);

char time_str[32];
ziti_fmt_time(time_str, sizeof(time_str), &start_time);

ZITI_LOG(INFO, "Ziti C SDK version %s @%s(%s) starting at (%s.%03d)",
ziti_get_build_version(false), ziti_git_commit(), ziti_git_branch(),
time_str, start_time.tv_usec / 1000);

PREP(ziti);

if (options->config == NULL && (options->controller == NULL || options->tls == NULL)) {
Expand Down Expand Up @@ -216,10 +207,44 @@ int ziti_init_opts(ziti_options *options, uv_loop_t *loop, void *init_ctx) {
ctx->loop = loop;
ctx->ziti_timeout = ZITI_DEFAULT_TIMEOUT;

uv_async_init(loop, &ctx->connect_async, async_connects);
uv_unref((uv_handle_t *) &ctx->connect_async);
NEWP(init_req, struct ziti_init_req);
init_req->init_cb = options->init_cb;
init_req->init_ctx = init_ctx;
init_req->ztx = ctx;

NEWP(async_init_req, uv_async_t);
uv_async_init(loop, async_init_req, ziti_init_async);
async_init_req->data = init_req;

uv_async_send(async_init_req);

CATCH(ziti) {
return ERR(ziti);
}

return ZITI_OK;
}

static void ziti_init_async(uv_async_t *ar) {
struct ziti_init_req *init_req = ar->data;

ziti_context ctx = init_req->ztx;
uv_loop_t *loop = ar->loop;

uv_close((uv_handle_t *) ar, (uv_close_cb) free);

uv_timeval64_t start_time;
uv_gettimeofday(&start_time);

char time_str[32];
ziti_fmt_time(time_str, sizeof(time_str), &start_time);

ZITI_LOG(INFO, "Ziti C SDK version %s @%s(%s) starting at (%s.%03d)",
ziti_get_build_version(false), ziti_git_commit(), ziti_git_branch(),
time_str, start_time.tv_usec / 1000);


ziti_ctrl_init(loop, &ctx->controller, options->controller, ctx->tlsCtx);
ziti_ctrl_init(loop, &ctx->controller, ctx->opts->controller, ctx->tlsCtx);
ziti_ctrl_get_version(&ctx->controller, version_cb, ctx);

uv_timer_init(loop, &ctx->session_timer);
Expand All @@ -237,17 +262,7 @@ int ziti_init_opts(ziti_options *options, uv_loop_t *loop, void *init_ctx) {
uv_unref((uv_handle_t *) &ctx->reaper);
uv_prepare_start(&ctx->reaper, grim_reaper);

NEWP(init_req, struct ziti_init_req);
init_req->init_cb = options->init_cb;
init_req->init_ctx = init_ctx;
init_req->ztx = ctx;
ziti_ctrl_login(&ctx->controller, ctx->opts->config_types, session_cb, init_req);

CATCH(ziti) {
return ERR(ziti);
}

return ZITI_OK;
}

int ziti_init(const char *config, uv_loop_t *loop, ziti_init_cb init_cb, void *init_ctx) {
Expand Down Expand Up @@ -290,6 +305,7 @@ int ziti_shutdown(ziti_context ztx) {
ZITI_LOG(INFO, "Ziti is shutting down");

free_ziti_session(ztx->session);
free(ztx->session);
ztx->session = NULL;

uv_timer_stop(&ztx->refresh_timer);
Expand Down
5 changes: 0 additions & 5 deletions library/ziti_enroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ static void well_known_certs_cb(char *base64_encoded_pkcs7, ziti_error *err, voi

static void enroll_cb(ziti_enrollment_resp *er, ziti_error *err, void *ctx);

static void async_connects(uv_async_t *ar) {
ziti_context ztx = ar->data;
ziti_process_connect_reqs(ztx);
}

int verify_controller_jwt(tls_cert cert, void *ctx) {
ZITI_LOG(INFO, "verifying JWT signature");

Expand Down

0 comments on commit 08f0480

Please sign in to comment.