From d32999f178c3b5058cd919df5919a9a74b2ae85a Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 8 Aug 2024 18:50:54 -0400 Subject: [PATCH] Decouple DNS-based seed discovery from the broadcast timer. --- src/ssb.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index 2ac55fb4..fda94c0d 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -77,6 +77,9 @@ enum k_tf_ssb_rpc_message_body_length_max = 1 * 1024 * 1024, k_debug_close_message_count = 256, k_debug_close_connection_count = 32, + k_seed_expire_seconds = 10 * 60, + k_seed_check_interval_seconds = 5 * 50, + k_udp_discovery_expires_seconds = 10, }; typedef struct _tf_ssb_broadcast_t tf_ssb_broadcast_t; @@ -116,6 +119,7 @@ typedef struct _tf_ssb_broadcast_t tf_ssb_broadcast_t* next; time_t ctime; time_t mtime; + time_t expires_at; char host[256]; struct sockaddr_in addr; tf_ssb_connection_t* tunnel_connection; @@ -262,6 +266,7 @@ typedef struct _tf_ssb_t bool is_room; char* room_name; char seeds_host[256]; + time_t last_seed_check; tf_ssb_timer_t** timers; int timers_count; @@ -357,7 +362,7 @@ static JSClassID _connection_class_id; static int s_connection_index; static int s_tunnel_index; -static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broadcast); +static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broadcast, int expires_seconds); static void _tf_ssb_connection_client_send_hello(tf_ssb_connection_t* connection); static void _tf_ssb_connection_close(tf_ssb_connection_t* connection, const char* reason); static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason); @@ -2946,7 +2951,7 @@ static void _tf_ssb_update_seeds_after_work(tf_ssb_t* ssb, int status, void* use tf_ssb_broadcast_t broadcast = { 0 }; if (_tf_ssb_parse_broadcast(seeds->seeds[i], &broadcast)) { - _tf_ssb_add_broadcast(ssb, &broadcast); + _tf_ssb_add_broadcast(ssb, &broadcast, k_seed_expire_seconds); } tf_free(seeds->seeds[i]); } @@ -2971,11 +2976,12 @@ static void _tf_ssb_broadcast_timer(uv_timer_t* timer) uv_free_interface_addresses(info, count); } - seeds_t* seeds = tf_malloc(sizeof(seeds_t)); - *seeds = (seeds_t) { 0 }; - - if (*ssb->seeds_host) + time_t now = time(NULL); + if (*ssb->seeds_host && now - ssb->last_seed_check > k_seed_check_interval_seconds) { + seeds_t* seeds = tf_malloc(sizeof(seeds_t)); + *seeds = (seeds_t) { 0 }; + ssb->last_seed_check = now; tf_ssb_run_work(ssb, _tf_ssb_update_seeds_work, _tf_ssb_update_seeds_after_work, seeds); } } @@ -3090,7 +3096,7 @@ static void _tf_ssb_notify_broadcasts_changed(tf_ssb_t* ssb) } } -static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broadcast) +static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broadcast, int expires_seconds) { if (memcmp(broadcast->pub, ssb->pub, sizeof(ssb->pub)) == 0) { @@ -3104,6 +3110,7 @@ static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broad if (node->tunnel_connection == broadcast->tunnel_connection && memcmp(node->pub, broadcast->pub, sizeof(node->pub)) == 0) { node->mtime = time(NULL); + node->expires_at = node->mtime + expires_seconds; return; } } @@ -3116,6 +3123,7 @@ static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broad node->addr.sin_addr.s_addr == broadcast->addr.sin_addr.s_addr && memcmp(node->pub, broadcast->pub, sizeof(node->pub)) == 0) { node->mtime = time(NULL); + node->expires_at = node->mtime + expires_seconds; return; } } @@ -3132,6 +3140,7 @@ static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broad node->next = ssb->broadcasts; node->ctime = time(NULL); node->mtime = node->ctime; + node->expires_at = node->mtime + expires_seconds; ssb->broadcasts = node; ssb->broadcasts_count++; @@ -3157,7 +3166,7 @@ static void _tf_ssb_on_broadcast_listener_recv(uv_udp_t* handle, ssize_t nread, tf_ssb_broadcast_t broadcast = { 0 }; if (_tf_ssb_parse_broadcast(entry, &broadcast)) { - _tf_ssb_add_broadcast(ssb, &broadcast); + _tf_ssb_add_broadcast(ssb, &broadcast, k_udp_discovery_expires_seconds); } entry = strtok_r(NULL, k_delim, &state); } @@ -3188,7 +3197,7 @@ static void _tf_ssb_broadcast_cleanup_timer(uv_timer_t* timer) time_t now = time(NULL); for (tf_ssb_broadcast_t** it = &ssb->broadcasts; *it;) { - if (!(*it)->tunnel_connection && (*it)->mtime < now - 10) + if (!(*it)->tunnel_connection && now > (*it)->expires_at) { tf_ssb_broadcast_t* node = *it; *it = node->next; @@ -3604,7 +3613,7 @@ void tf_ssb_connection_add_room_attendant(tf_ssb_connection_t* connection, const .tunnel_connection = connection, }; tf_ssb_id_str_to_bin(broadcast.pub, id); - _tf_ssb_add_broadcast(connection->ssb, &broadcast); + _tf_ssb_add_broadcast(connection->ssb, &broadcast, 0); } void tf_ssb_connection_remove_room_attendant(tf_ssb_connection_t* connection, const char* id)