Skip to content

Commit

Permalink
add HA controller details to ZitiCtxEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoby committed Jun 13, 2024
1 parent f093369 commit e09306d
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 165 deletions.
9 changes: 7 additions & 2 deletions inc_internal/ziti_ctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extern const char* const PC_ENDPOINT_STATE_TYPE;

typedef void (*ziti_ctrl_redirect_cb)(const char *new_address, void *ctx);

typedef void (*ziti_ctrl_change_cb)(void *ctx, const model_map *endpoints);

typedef void (*ctrl_version_cb)(const ziti_version *, const ziti_error *, void *);

typedef void(*routers_cb)(ziti_service_routers *srv_routers, const ziti_error *, void *);
Expand All @@ -58,8 +60,9 @@ typedef struct ziti_controller_s {
bool has_token;
char *instance_id;

ziti_ctrl_change_cb change_cb;
ziti_ctrl_redirect_cb redirect_cb;
void *redirect_ctx;
void *cb_ctx;
} ziti_controller;

int ziti_ctrl_init(uv_loop_t *loop, ziti_controller *ctrl, model_list *urls, tls_context *tls);
Expand All @@ -70,7 +73,9 @@ int ziti_ctrl_cancel(ziti_controller *ctrl);

void ziti_ctrl_set_page_size(ziti_controller *ctrl, unsigned int size);

void ziti_ctrl_set_redirect_cb(ziti_controller *ctrl, ziti_ctrl_redirect_cb cb, void *ctx);
void ziti_ctrl_set_callbacks(ziti_controller *ctrl, void *ctx,
ziti_ctrl_redirect_cb redirect_cb,
ziti_ctrl_change_cb change_cb);

int ziti_ctrl_close(ziti_controller *ctrl);

Expand Down
14 changes: 3 additions & 11 deletions inc_internal/zt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

#include <sodium.h>

//#define SIZEOF(arr) (sizeof(arr) / sizeof((arr)[0]))

#if !defined(UUID_STR_LEN)
#define UUID_STR_LEN 37
#endif
Expand All @@ -52,10 +50,6 @@ typedef struct ziti_channel ziti_channel_t;

typedef void (*reply_cb)(void *ctx, message *m, int err);

typedef void (*send_cb)(int status, void *ctx);

typedef void (*ch_connect_cb)(ziti_channel_t *ch, void *ctx, int status);

typedef void (*ch_notify_state)(ziti_channel_t *ch, ziti_router_status status, void *ctx);

typedef int ch_state;
Expand Down Expand Up @@ -108,7 +102,7 @@ typedef struct ziti_channel {

ch_notify_state notify_cb;
void *notify_ctx;
} ziti_channel_t;
};

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux x86_64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux x86_64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux x86_64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux x86_64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM64

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM

useless storage class specifier in empty declaration

Check warning on line 105 in inc_internal/zt_internal.h

View workflow job for this annotation

GitHub Actions / Linux ARM

useless storage class specifier in empty declaration

struct ziti_write_req_s {
struct ziti_conn *conn;
Expand Down Expand Up @@ -320,8 +314,6 @@ extern "C" {

ziti_controller *ztx_get_controller(ziti_context ztx);

bool ziti_is_session_valid(ziti_context ztx, ziti_session *session, const char *service_id, ziti_session_type type);

void ziti_invalidate_session(ziti_context ztx, const char *service_id, ziti_session_type type);

void ziti_on_channel_event(ziti_channel_t *ch, ziti_router_status status, ziti_context ztx);
Expand All @@ -346,9 +338,9 @@ int ziti_channel_prepare(ziti_channel_t *ch);

int ziti_channel_close(ziti_channel_t *ch, int err);

void ziti_channel_add_receiver(ziti_channel_t *ch, int id, void *receiver, void (*receive_f)(void *, message *, int));
void ziti_channel_add_receiver(ziti_channel_t *ch, uint32_t id, void *receiver, void (*receive_f)(void *, message *, int));

void ziti_channel_rem_receiver(ziti_channel_t *ch, int id);
void ziti_channel_rem_receiver(ziti_channel_t *ch, uint32_t id);

int ziti_channel_send_message(ziti_channel_t *ch, message *msg, struct ziti_write_req_s *ziti_write);

Expand Down
2 changes: 1 addition & 1 deletion includes/ziti/model_collections.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ ZITI_FUNC void *model_list_it_element(model_list_iter it);

#define MODEL_LIST_FOR(it, m) for(model_list_iter it = model_list_iterator(&(m)); (it) != NULL; (it) = model_list_it_next(it))

#define MODEL_LIST_FOREACH(el, list) for(model_list_iter it = model_list_iterator(&(list)); \
#define MODEL_LIST_FOREACH(el, list) for(model_list_iter it = model_list_iterator((model_list*)&(list)); \
(it) != NULL && (((el) = model_list_it_element(it)), true); \
(it) = model_list_it_next(it))

Expand Down
11 changes: 10 additions & 1 deletion includes/ziti/ziti_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ typedef enum {
struct ziti_context_event {
int ctrl_status;
const char *err;
size_t ctrl_count;
struct ctrl_detail_s *ctrl_details;
};

struct ctrl_detail_s {
const char *id;
const char *url;
bool online;
bool active;
};

struct ziti_api_event {
Expand Down Expand Up @@ -120,7 +129,7 @@ typedef struct ziti_event_s {
struct ziti_service_event service;
struct ziti_mfa_auth_event mfa_auth_event;
struct ziti_api_event api;
} event;
};
} ziti_event_t;

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion library/auth_queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void ziti_auth_query_mfa_process(ziti_mfa_auth_ctx *mfa_auth_ctx) {

ziti_event_t ev = {
.type = ZitiMfaAuthEvent,
.event.mfa_auth_event = {
.mfa_auth_event = {
.auth_query_mfa = mfa_auth_ctx->auth_query_mfa,
}
};
Expand Down
2 changes: 1 addition & 1 deletion library/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ static void bind_reply_cb(void *ctx, message *msg, int code) {
if (code == ZITI_OK && msg->header.content == ContentTypeStateConnected) {
CONN_LOG(TRACE, "received msg ct[%X] code[%d]", msg->header.content, code);
CONN_LOG(DEBUG, "bound successfully over ch[%s]", b->ch->url);
ziti_channel_add_receiver(b->ch, (int)conn->conn_id, b,
ziti_channel_add_receiver(b->ch, conn->conn_id, b,
(void (*)(void *, message *, int)) on_message);
b->bound = true;
} else {
Expand Down
51 changes: 24 additions & 27 deletions library/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ int ziti_channel_close(ziti_channel_t *ch, int err) {
return 0;
}

void ziti_channel_add_receiver(ziti_channel_t *ch, int id, void *receiver, void (*receive_f)(void *, message *, int)) {
void ziti_channel_add_receiver(ziti_channel_t *ch, uint32_t id, void *receiver, void (*receive_f)(void *, message *, int)) {
NEWP(r, struct msg_receiver);
r->id = id;
r->receiver = receiver;
Expand All @@ -246,7 +246,7 @@ void ziti_channel_add_receiver(ziti_channel_t *ch, int id, void *receiver, void
CH_LOG(DEBUG, "added receiver[%d]", id);
}

void ziti_channel_rem_receiver(ziti_channel_t *ch, int id) {
void ziti_channel_rem_receiver(ziti_channel_t *ch, uint32_t id) {
struct msg_receiver *r = model_map_removel(&ch->receivers, id);

if (r) {
Expand Down Expand Up @@ -328,12 +328,8 @@ int ziti_channel_update_token(ziti_channel_t *ch) {
}

const char* token = ziti_get_api_session_token(ch->ztx);

uint8_t true_val = 1;
ziti_channel_send_for_reply(ch, ContentTypeUpdateTokenType,
NULL, 0, token, strlen(token), token_update_cb, ch);


return ZITI_OK;
}

Expand Down Expand Up @@ -696,9 +692,9 @@ static void hello_reply_cb(void *ctx, message *msg, int err) {
}

if (success) {
uint8_t *erVersion = "<unknown>";
const char *erVersion = "<unknown>";
size_t erVersionLen = strlen(erVersion);
message_get_bytes_header(msg, HelloVersionHeader, &erVersion, &erVersionLen);
message_get_bytes_header(msg, HelloVersionHeader, (uint8_t **) &erVersion, &erVersionLen);
CH_LOG(INFO, "connected. EdgeRouter version: %.*s", (int) erVersionLen, erVersion);
ch->state = Connected;
FREE(ch->version);
Expand Down Expand Up @@ -891,30 +887,31 @@ static void on_channel_data(uv_stream_t *s, ssize_t len, const uv_buf_t *buf) {
tlsuv_stream_t *ssl = (tlsuv_stream_t *) s;
ziti_channel_t *ch = ssl->data;

if (len == UV_ENOBUFS) {
tlsuv_stream_read_stop(ssl);
CH_LOG(VERBOSE, "blocked until messages are processed");
return;
}

if (len < 0) {
free(buf->base);
switch (len) {
case UV_ENOBUFS:
tlsuv_stream_read_stop(ssl);
CH_LOG(VERBOSE, "blocked until messages are processed");
return;

default:
CH_LOG(INFO, "channel disconnected [%zd/%s]", len, uv_strerror(len));
// propagate close
on_channel_close(ch, ZITI_CONNABORT, len);
close_connection(ch);
break;
}
} else if (len == 0) {
CH_LOG(INFO, "channel disconnected [%zd/%s]", len, uv_strerror(len));
// propagate close
on_channel_close(ch, ZITI_CONNABORT, len);
close_connection(ch);
return;
}

if (len == 0) {
// sometimes SSL message has no payload
free(buf->base);
} else {
CH_LOG(TRACE, "on_data [len=%zd]", len);
ch->last_read = uv_now(ch->loop);
buffer_append(ch->incoming, buf->base, (uint32_t) len);
process_inbound(ch);
return;
}

CH_LOG(TRACE, "on_data [len=%zd]", len);
ch->last_read = uv_now(ch->loop);
buffer_append(ch->incoming, buf->base, (uint32_t) len);
process_inbound(ch);
}

static void on_channel_connect_internal(uv_connect_t *req, int status) {
Expand Down
7 changes: 4 additions & 3 deletions library/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,10 @@ static bool ziti_connect(struct ziti_ctx *ztx, ziti_session *session, struct zit
if (ch == NULL) continue;

if (ch->state == Connected) {
if (ch->latency < best_latency) {
uint64_t latency = ziti_channel_latency(ch);
if (latency < best_latency) {
best_ch = ch;
best_latency = ch->latency;
best_latency = latency;
}
}

Expand All @@ -388,7 +389,7 @@ static bool ziti_connect(struct ziti_ctx *ztx, ziti_session *session, struct zit

if (best_ch) {
CONN_LOG(DEBUG, "selected ch[%s@%s] for best latency(%llu ms)", best_ch->name, best_ch->url,
(unsigned long long) best_ch->latency);
(unsigned long long) best_latency);
ziti_channel_start_connection(conn, best_ch, session);
result = true;
} else {
Expand Down
2 changes: 1 addition & 1 deletion library/legacy_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int legacy_auth_refresh(ziti_auth_method_t *self) {
assert(auth->ctx);
assert(auth->cb);

uv_timer_start(&auth->timer, auth_timer_cb, 0, 0);
return uv_timer_start(&auth->timer, auth_timer_cb, 0, 0);
}


Expand Down
1 change: 1 addition & 0 deletions library/oidc.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ int oidc_client_close(oidc_client_t *clt, oidc_close_cb cb) {
tlsuv_http_close(&clt->http, http_close_cb);
uv_close((uv_handle_t *) clt->timer, (uv_close_cb) free);
clt->timer = NULL;
return 0;
}

static void oidc_client_set_tokens(oidc_client_t *clt, json_object *tok_json) {
Expand Down
Loading

0 comments on commit e09306d

Please sign in to comment.