From 463951a4f1493ae2e45bc948cca90b2de7aa91d4 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 14 Aug 2024 19:23:01 -0400 Subject: [PATCH] Track/show the origin of each broadcast (discovery/room/peer exchange). --- apps/ssb.json | 2 +- apps/ssb/tf-tab-connections.js | 7 +++++++ src/ssb.c | 10 ++++++---- src/ssb.h | 9 ++++++++- src/ssb.js.c | 14 +++++++++++++- src/ssb.tests.c | 4 ++-- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/apps/ssb.json b/apps/ssb.json index 95c75517..5b36f263 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🐌", - "previous": "&2xK//SIpjFb0+uT5I7MSAGJ3d1FKuI/rlzhcCQd3NME=.sha256" + "previous": "&xsmsLytB3VvoHphiFHZGGEvrCfTEVGXrGwobGTIYFPQ=.sha256" } diff --git a/apps/ssb/tf-tab-connections.js b/apps/ssb/tf-tab-connections.js index eb43803d..b6a4c282 100644 --- a/apps/ssb/tf-tab-connections.js +++ b/apps/ssb/tf-tab-connections.js @@ -17,6 +17,12 @@ class TfTabConnectionsElement extends LitElement { static styles = styles; + static k_broadcast_emojis = { + discovery: '🏓', + room: '🚪', + peer_exchange: '🕸', + }; + constructor() { super(); let self = this; @@ -92,6 +98,7 @@ class TfTabConnectionsElement extends LitElement { Connect
+ ${TfTabConnectionsElement.k_broadcast_emojis[connection.origin]} ${this.render_connection_summary(connection)}
diff --git a/src/ssb.c b/src/ssb.c index fda94c0d..9a530ba5 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -121,6 +121,7 @@ typedef struct _tf_ssb_broadcast_t time_t mtime; time_t expires_at; char host[256]; + tf_ssb_broadcast_origin_t origin; struct sockaddr_in addr; tf_ssb_connection_t* tunnel_connection; uint8_t pub[crypto_sign_PUBLICKEYBYTES]; @@ -2948,7 +2949,7 @@ static void _tf_ssb_update_seeds_after_work(tf_ssb_t* ssb, int status, void* use seeds_t* seeds = user_data; for (int i = 0; i < seeds->seeds_count; i++) { - tf_ssb_broadcast_t broadcast = { 0 }; + tf_ssb_broadcast_t broadcast = { .origin = k_tf_ssb_broadcast_origin_peer_exchange }; if (_tf_ssb_parse_broadcast(seeds->seeds[i], &broadcast)) { _tf_ssb_add_broadcast(ssb, &broadcast, k_seed_expire_seconds); @@ -3163,7 +3164,7 @@ static void _tf_ssb_on_broadcast_listener_recv(uv_udp_t* handle, ssize_t nread, char* entry = strtok_r(buf->base, k_delim, &state); while (entry) { - tf_ssb_broadcast_t broadcast = { 0 }; + tf_ssb_broadcast_t broadcast = { .origin = k_tf_ssb_broadcast_origin_discovery }; if (_tf_ssb_parse_broadcast(entry, &broadcast)) { _tf_ssb_add_broadcast(ssb, &broadcast, k_udp_discovery_expires_seconds); @@ -3174,7 +3175,7 @@ static void _tf_ssb_on_broadcast_listener_recv(uv_udp_t* handle, ssize_t nread, } void tf_ssb_visit_broadcasts( - tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data) + tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data) { time_t now = time(NULL); tf_ssb_broadcast_t* next = NULL; @@ -3184,7 +3185,7 @@ void tf_ssb_visit_broadcasts( if (node->mtime - now < 60) { tf_trace_begin(ssb->trace, "broadcast"); - callback(node->host, &node->addr, node->tunnel_connection, node->pub, user_data); + callback(node->host, &node->addr, node->origin, node->tunnel_connection, node->pub, user_data); tf_trace_end(ssb->trace); } } @@ -3610,6 +3611,7 @@ void tf_ssb_notify_blob_want_added(tf_ssb_t* ssb, const char* id) void tf_ssb_connection_add_room_attendant(tf_ssb_connection_t* connection, const char* id) { tf_ssb_broadcast_t broadcast = { + .origin = k_tf_ssb_broadcast_origin_room, .tunnel_connection = connection, }; tf_ssb_id_str_to_bin(broadcast.pub, id); diff --git a/src/ssb.h b/src/ssb.h index 4cfbe956..37ca0cd9 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -41,6 +41,13 @@ typedef enum _tf_ssb_change_t k_tf_ssb_change_update, } tf_ssb_change_t; +typedef enum _tf_ssb_broadcast_origin_t +{ + k_tf_ssb_broadcast_origin_discovery, + k_tf_ssb_broadcast_origin_room, + k_tf_ssb_broadcast_origin_peer_exchange, +} tf_ssb_broadcast_origin_t; + /** ** Flags describing the structure of a message. */ @@ -300,7 +307,7 @@ bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size); ** @param user_data User data for the callback. */ void tf_ssb_visit_broadcasts( - tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data); + tf_ssb_t* ssb, void (*callback)(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data), void* user_data); /** ** Get the identities of all active connections. diff --git a/src/ssb.js.c b/src/ssb.js.c index d49cac45..29220eae 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -1450,12 +1450,24 @@ typedef struct _broadcasts_t int length; } broadcasts_t; -static void _tf_ssb_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) +static void _tf_ssb_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) { broadcasts_t* broadcasts = user_data; JSValue entry = JS_NewObject(broadcasts->context); char pubkey[k_id_base64_len]; tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub); + switch (origin) + { + case k_tf_ssb_broadcast_origin_discovery: + JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "discovery")); + break; + case k_tf_ssb_broadcast_origin_room: + JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "room")); + break; + case k_tf_ssb_broadcast_origin_peer_exchange: + JS_SetPropertyStr(broadcasts->context, entry, "origin", JS_NewString(broadcasts->context, "peer_exchange")); + break; + } if (tunnel) { JS_SetPropertyStr(broadcasts->context, entry, "tunnel", JS_DupValue(broadcasts->context, tf_ssb_connection_get_object(tunnel))); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 975098d8..86c1a1db 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -323,7 +323,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) uv_loop_close(&loop); } -static void _broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) +static void _broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) { int* count = user_data; (*count)++; @@ -747,7 +747,7 @@ static void _break_in_a_bit(tf_ssb_t* ssb, tf_ssb_connection_t* connection, cons uv_timer_start(&data->timer, _close_callback, 3000, 0); } -static void _ssb_test_room_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) +static void _ssb_test_room_broadcasts_visit(const char* host, const struct sockaddr_in* addr, tf_ssb_broadcast_origin_t origin, tf_ssb_connection_t* tunnel, const uint8_t* pub, void* user_data) { tf_ssb_t* ssb = user_data; char id[k_id_base64_len] = { 0 };