Skip to content

Commit

Permalink
cleanup binding termination (#564)
Browse files Browse the repository at this point in the history
* cleanup binding termination

* fix un-encrypted client conn: cannot opportunistically turn on encryption after Dial request was sent

* do not log innocuous shutdown error
  • Loading branch information
ekoby authored Oct 17, 2023
1 parent 889cec7 commit e196b9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
20 changes: 17 additions & 3 deletions library/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,24 @@ void on_unbind(void *ctx, message *m, int code) {
}

static void stop_binding(struct binding_s *b) {

// stop accepting incoming requests
ziti_channel_rem_receiver(b->ch, b->conn->conn_id);
if (b->waiter) {
ziti_channel_remove_waiter(b->ch, b->waiter);
b->waiter = NULL;
}

// no need to send unbind message
if (!ziti_channel_is_connected(b->ch)) {
return;
}

ziti_net_session *s = b->conn->server.session;
if (s == NULL) {
return;
}

int32_t conn_id = htole32(b->conn->conn_id);
hdr_t headers[] = {
{
Expand All @@ -495,9 +512,6 @@ static void stop_binding(struct binding_s *b) {
.value = (uint8_t *) &conn_id
},
};
if (b->waiter) {
ziti_channel_remove_waiter(b->ch, b->waiter);
}
b->waiter = ziti_channel_send_for_reply(b->ch, ContentTypeUnbind,
headers, 1,
s->token, strlen(s->token),
Expand Down
4 changes: 3 additions & 1 deletion library/conn_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ static void close_bridge(struct ziti_bridge_s *br) {
}

static void on_shutdown(uv_shutdown_t *sr, int status) {
if (status != 0) {
// ignore UV_ECANCELED, it just means that stream was closed
// before shutdown was processed
if (status != 0 && status != UV_ECANCELED) {
struct ziti_bridge_s *br = sr->handle->data;
BR_LOG(WARN, "shutdown failed: %d(%s)", status, uv_strerror(status));
close_bridge(sr->handle->data);
Expand Down
19 changes: 13 additions & 6 deletions library/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,11 @@ static void connect_timeout(uv_timer_t *timer) {

if (conn->state == Connecting) {
if (ch == NULL) {
CONN_LOG(WARN, "connect timeout: no suitable edge router");
CONN_LOG(WARN, "connect timeout: no suitable edge router for service[%s]", conn->service);
} else {
CONN_LOG(WARN, "failed to establish connection in %dms on ch[%d]", conn->timeout, ch->id);

CONN_LOG(WARN, "failed to establish connection to service[%s] in %dms on ch[%d]",
conn->service, conn->timeout, ch->id);
}
complete_conn_req(conn, ZITI_TIMEOUT);
ziti_disconnect(conn);
Expand Down Expand Up @@ -667,6 +669,9 @@ static void crypto_wr_cb(ziti_connection conn, ssize_t status, void *ctx) {
}

int establish_crypto(ziti_connection conn, message *msg) {
if (!conn->encrypted) {
return ZITI_OK;
}

if (conn->state != Connecting && conn->state != Accepting) {
CONN_LOG(ERROR, "cannot establish crypto in state[%s]", ziti_conn_state(conn));
Expand All @@ -687,7 +692,6 @@ int establish_crypto(ziti_connection conn, message *msg) {
return ZITI_OK;
}
}
conn->encrypted = true;

int rc = init_crypto(&conn->key_ex, &conn->key_pair, peer_key, conn->state == Accepting);

Expand Down Expand Up @@ -927,9 +931,12 @@ void connect_reply_cb(void *ctx, message *msg, int err) {
case ContentTypeStateConnected:
if (conn->state == Connecting) {
CONN_LOG(TRACE, "connected");
int rc = establish_crypto(conn, msg);
if (rc == ZITI_OK && conn->encrypted) {
send_crypto_header(conn);
int rc = ZITI_OK;
if (conn->encrypted) {
rc = establish_crypto(conn, msg);
if (rc == ZITI_OK) {
send_crypto_header(conn);
}
}
conn_set_state(conn, rc == ZITI_OK ? Connected : Disconnected);
complete_conn_req(conn, rc);
Expand Down

0 comments on commit e196b9c

Please sign in to comment.