Skip to content

Commit 98e171d

Browse files
authored
Merge branch 'main' into host.v1.nat
2 parents 30e5fb7 + 45d498f commit 98e171d

File tree

5 files changed

+74
-58
lines changed

5 files changed

+74
-58
lines changed

inc_internal/internal_model.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ XX(tls, model_string, none, tls, __VA_ARGS__)
3333

3434
#define ZITI_EDGE_ROUTER_MODEL(XX, ...)\
3535
XX(name, model_string, none, name, __VA_ARGS__)\
36-
XX(hostname, model_string, none, hostname, __VA_ARGS__) \
3736
XX(protocols, ziti_er_protocols, none, supportedProtocols, __VA_ARGS__)
3837

3938
#define ZITI_SERVICE_EDGE_ROUTERS_MODEL(XX, ...) \

inc_internal/zt_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,15 @@ bool ziti_channel_is_connected(ziti_channel_t *ch);
357357

358358
uint64_t ziti_channel_latency(ziti_channel_t *ch);
359359

360+
void ziti_channel_set_url(ziti_channel_t *ch, const char *url);
361+
360362
int ziti_channel_force_connect(ziti_channel_t *ch);
361363

362364
int ziti_channel_update_token(ziti_channel_t *ch, const char *token);
363365

364366
int ziti_channel_update_posture(ziti_channel_t *ch, const uint8_t *data, size_t len);
365367

366-
int ziti_channel_connect(ziti_context ztx, const char *name, const char *url);
368+
int ziti_channel_connect(ziti_context ztx, const ziti_edge_router *er);
367369

368370
int ziti_channel_prepare(ziti_channel_t *ch);
369371

library/channel.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ void ziti_channel_free(ziti_channel_t *ch) {
190190
}
191191

192192
int ziti_close_channels(struct ziti_ctx *ztx, int err) {
193-
const char *url;
193+
const char *er_id;
194194
model_list ch_ids = {0};
195195
MODEL_MAP_FOR(it, ztx->channels) {
196196
model_list_append(&ch_ids, model_map_it_key(it));
197197
}
198198

199199
MODEL_LIST_FOR(it, ch_ids) {
200-
url = model_list_it_element(it);
201-
ziti_channel_t *ch = model_map_get(&ztx->channels, url);
200+
er_id = model_list_it_element(it);
201+
ziti_channel_t *ch = model_map_get(&ztx->channels, er_id);
202202
if (ch != NULL) {
203-
ZTX_LOG(DEBUG, "closing channel[%s]: %s", url, ziti_errorstr(err));
203+
ZTX_LOG(DEBUG, "closing channel[%s]: %s", er_id, ziti_errorstr(err));
204204
ziti_channel_close(ch, err);
205205
}
206206
}
@@ -257,21 +257,15 @@ uint64_t ziti_channel_latency(ziti_channel_t *ch) {
257257
return ch->latency;
258258
}
259259

260-
static ziti_channel_t *new_ziti_channel(ziti_context ztx, const char *ch_name, const char *url) {
260+
static ziti_channel_t *new_ziti_channel(ziti_context ztx, const ziti_edge_router *er) {
261261
ziti_channel_t *ch = calloc(1, sizeof(ziti_channel_t));
262262
ziti_channel_init(ztx, ch, channel_counter++);
263263
const ziti_identity *identity = ziti_get_identity(ztx);
264-
ch->name = strdup(ch_name);
265-
ch->url = strdup(url);
264+
ch->name = strdup(er->name);
266265
CH_LOG(INFO, "(%s) new channel for ztx[%d] identity[%s]", ch->name, ztx->id, identity->name);
267266

268-
struct tlsuv_url_s ingress;
269-
tlsuv_parse_url(&ingress, url);
270-
271-
ch->host = calloc(1, ingress.hostname_len + 1);
272-
snprintf(ch->host, ingress.hostname_len + 1, "%.*s", (int) ingress.hostname_len, ingress.hostname);
273-
ch->port = ingress.port;
274-
model_map_set(&ztx->channels, url, ch);
267+
ziti_channel_set_url(ch, er->protocols.tls);
268+
model_map_set(&ztx->channels, er->name, ch);
275269
return ch;
276270
}
277271

@@ -312,6 +306,26 @@ static void token_update_cb(void *ctx, message *m, int status) {
312306
}
313307
}
314308

309+
void ziti_channel_set_url(ziti_channel_t *ch, const char *url) {
310+
assert(ch != NULL);
311+
assert(url != NULL);
312+
313+
if (ch->url && strcmp(ch->url, url) == 0) {
314+
return;
315+
}
316+
CH_LOG(DEBUG, "setting channel[%s] url[%s]", ch->name, url);
317+
318+
FREE(ch->url);
319+
FREE(ch->host);
320+
ch->url = strdup(url);
321+
322+
struct tlsuv_url_s ingress;
323+
tlsuv_parse_url(&ingress, ch->url);
324+
ch->host = calloc(1, ingress.hostname_len + 1);
325+
snprintf(ch->host, ingress.hostname_len + 1, "%.*s", (int) ingress.hostname_len, ingress.hostname);
326+
ch->port = ingress.port;
327+
}
328+
315329
int ziti_channel_update_token(ziti_channel_t *ch, const char *token) {
316330
if (ch == NULL) {
317331
return ZITI_INVALID_STATE;
@@ -348,14 +362,20 @@ int ziti_channel_force_connect(ziti_channel_t *ch) {
348362
return ZITI_OK;
349363
}
350364

351-
int ziti_channel_connect(ziti_context ztx, const char *ch_name, const char *url) {
352-
ziti_channel_t *ch = model_map_get(&ztx->channels, url);
365+
int ziti_channel_connect(ziti_context ztx, const ziti_edge_router* er) {
366+
const char *url = er->protocols.tls;
367+
if (url == NULL) {
368+
ZTX_LOG(ERROR, "er[%s] does not have TLS edge listener", er->name);
369+
return ZITI_INVALID_CONFIG;
370+
}
371+
372+
ziti_channel_t *ch = model_map_get(&ztx->channels, er->name);
353373

354374
if (ch != NULL) {
355375
ZTX_LOG(DEBUG, "existing ch[%d](%s) found for ingress[%s]", ch->id, ch_state_str(ch), url);
356376
}
357377
else {
358-
ch = new_ziti_channel(ztx, ch_name, url);
378+
ch = new_ziti_channel(ztx, er);
359379
ch->notify_cb(ch, EdgeRouterAdded, ch->notify_ctx);
360380
}
361381

library/connect.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,19 @@ static bool ziti_connect(struct ziti_ctx *ztx, ziti_session *session, struct zit
388388

389389

390390
MODEL_LIST_FOREACH(er, session->edge_routers) {
391-
const char *tls = er->protocols.tls;
392-
393-
if (tls) {
394-
ch = model_map_get(&ztx->channels, tls);
395-
if (ch == NULL) continue;
396-
397-
if (ch->state == Connected) {
398-
uint64_t latency = ziti_channel_latency(ch);
399-
if (latency < best_latency) {
400-
best_ch = ch;
401-
best_latency = latency;
402-
}
391+
ch = model_map_get(&ztx->channels, er->name);
392+
if (ch == NULL) continue;
393+
394+
if (ch->state == Connected) {
395+
uint64_t latency = ziti_channel_latency(ch);
396+
if (latency < best_latency) {
397+
best_ch = ch;
398+
best_latency = latency;
403399
}
400+
}
404401

405-
if (ch->state == Disconnected) {
402+
if (ch->state == Disconnected) {
406403
model_list_append(&disconnected, ch);
407-
}
408404
}
409405
}
410406

library/ziti.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ void ziti_set_fully_authenticated(ziti_context ztx, const char *session_token) {
389389
ziti_ctrl_set_token(ztx_get_controller(ztx), session_token);
390390
ziti_ctrl_list_controllers(ctrl, ctrl_list_cb, ztx);
391391

392-
const char* url;
392+
const char* er_name;
393393
ziti_channel_t *ch;
394-
MODEL_MAP_FOREACH(url, ch, &ztx->channels) {
394+
MODEL_MAP_FOREACH(er_name, ch, &ztx->channels) {
395395
ziti_channel_update_token(ch, session_token);
396396
}
397397
}
@@ -907,11 +907,11 @@ void ziti_dump(ziti_context ztx, int (*printer)(void *arg, const char *fmt, ...)
907907

908908
printer(ctx, "\n==================\nChannels:\n");
909909
ziti_channel_t *ch;
910-
const char *url;
911-
MODEL_MAP_FOREACH(url, ch, &ztx->channels) {
910+
const char *er_id;
911+
MODEL_MAP_FOREACH(er_id, ch, &ztx->channels) {
912912
printer(ctx, "ch[%d] %s\n", ch->id, ch->name);
913913
printer(ctx, "\tconnected[%c] version[%s] address[%s]",
914-
ziti_channel_is_connected(ch) ? 'Y' : 'N', ch->version, url);
914+
ziti_channel_is_connected(ch) ? 'Y' : 'N', ch->version, ch->url);
915915
if (ziti_channel_is_connected(ch)) {
916916
printer(ctx, " latency[%" PRIu64 "]\n", ziti_channel_latency(ch));
917917
} else {
@@ -1450,26 +1450,28 @@ static void edge_routers_cb(ziti_edge_router_array ers, const ziti_error *err, v
14501450
}
14511451

14521452
model_map curr_routers = {0};
1453-
const char *er_url;
1453+
const char *er_name;
14541454
ziti_channel_t *ch;
1455-
MODEL_MAP_FOREACH(er_url, ch, &ztx->channels) {
1456-
model_map_set(&curr_routers, er_url, (void *) er_url);
1455+
MODEL_MAP_FOREACH(er_name, ch, &ztx->channels) {
1456+
model_map_set(&curr_routers, er_name, (void *) er_name);
14571457
}
14581458

14591459
ziti_edge_router **erp = ers;
14601460
while (*erp) {
14611461
ziti_edge_router *er = *erp;
1462-
const char *tls = er->protocols.tls;
14631462

1464-
if (tls) {
1465-
// check if it is already in the list
1466-
if (model_map_remove(&curr_routers, tls) == NULL) {
1467-
ZTX_LOG(TRACE, "connecting to %s(%s)", er->name, tls);
1468-
ziti_channel_connect(ztx, er->name, tls);
1463+
// check if it is already in the list
1464+
if (model_map_remove(&curr_routers, er->name) == NULL) {
1465+
if (ziti_channel_connect(ztx, er) == ZITI_OK) {
14691466
ers_changed = true;
1467+
ZTX_LOG(TRACE, "connecting to %s(%s)", er->name, er->protocols.tls);
14701468
}
1471-
} else {
1472-
ZTX_LOG(DEBUG, "edge router %s does not have TLS edge listener", er->name);
1469+
} else if(er->protocols.tls != NULL) {
1470+
// N.B.: if protocols.tls is NULL,
1471+
// controller may not have refreshed the ER model leave the channel as is
1472+
// otherwise update the url
1473+
ch = model_map_get(&ztx->channels, er->name);
1474+
ziti_channel_set_url(ch, er->protocols.tls);
14731475
}
14741476

14751477
free_ziti_edge_router(er);
@@ -1480,9 +1482,9 @@ static void edge_routers_cb(ziti_edge_router_array ers, const ziti_error *err, v
14801482

14811483
model_map_iter it = model_map_iterator(&curr_routers);
14821484
while (it != NULL) {
1483-
er_url = model_map_it_key(it);
1484-
ch = model_map_remove(&ztx->channels, er_url);
1485-
ZTX_LOG(INFO, "removing channel[%s@%s]: no longer available", ch->name, er_url);
1485+
er_name = model_map_it_key(it);
1486+
ch = model_map_remove(&ztx->channels, er_name);
1487+
ZTX_LOG(INFO, "removing channel[%s@%s]: no longer available", ch->name, ch->url);
14861488
ziti_channel_close(ch, ZITI_GATEWAY_UNAVAILABLE);
14871489
it = model_map_it_remove(it);
14881490
ers_changed = true;
@@ -1734,7 +1736,7 @@ void ziti_on_channel_event(ziti_channel_t *ch, ziti_router_status status, ziti_c
17341736
ziti_send_event(ztx, &ev);
17351737

17361738
if (status == EdgeRouterRemoved) {
1737-
model_map_remove(&ztx->channels, ch->url);
1739+
model_map_remove(&ztx->channels, ch->name);
17381740
if (ztx->closing) {
17391741
shutdown_and_free(ztx);
17401742
}
@@ -2034,12 +2036,9 @@ ziti_channel_t * ztx_get_channel(ziti_context ztx, const ziti_edge_router *er) {
20342036
assert(ztx);
20352037
assert(er);
20362038

2037-
model_string url = er->protocols.tls;
2038-
if (url == NULL) return NULL;
2039-
2040-
ziti_channel_t *ch = (ziti_channel_t *) model_map_get(&ztx->channels, url);
2039+
ziti_channel_t *ch = (ziti_channel_t *) model_map_get(&ztx->channels, er->name);
20412040
if (ch == NULL) {
2042-
ziti_channel_connect(ztx, er->name, url);
2041+
ziti_channel_connect(ztx, er);
20432042
}
20442043
return ch;
20452044
}

0 commit comments

Comments
 (0)