From de67c4067cc5eb31d0b4415b2567c9a59863dfe8 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Wed, 23 Feb 2022 20:10:23 -0500 Subject: [PATCH] Fix issue with friend finding taking too long after disconnects This forces the onion to consider all friends as new after a 10 second period of the onion being disconnected. new friends are aggressively searched for in the DHT, whereas old friends are only searched for in intervals spanning minutes. --- toxcore/onion_client.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 04bd5888..4d9743b4 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -111,6 +111,7 @@ struct Onion_Client { uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; uint64_t last_run; uint64_t first_run; + uint64_t last_time_connected; uint8_t temp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -1811,7 +1812,20 @@ static int onion_isconnected(const Onion_Client *onion_c) return 0; } +non_null() +static void reset_friend_run_counts(Onion_Client *onion_c) +{ + for (uint16_t i = 0; i < onion_c->num_friends; ++i) { + Onion_Friend *o_friend = &onion_c->friends_list[i]; + + if (o_friend->is_valid) { + o_friend->run_count = 0; + } + } +} + #define ONION_CONNECTION_SECONDS 3 +#define ONION_CONNECTED_TIMEOUT 10 Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c) { @@ -1838,6 +1852,12 @@ void do_onion_client(Onion_Client *onion_c) } if (onion_isconnected(onion_c)) { + if (mono_time_is_timeout(onion_c->mono_time, onion_c->last_time_connected, ONION_CONNECTED_TIMEOUT)) { + reset_friend_run_counts(onion_c); + } + + onion_c->last_time_connected = mono_time_get(onion_c->mono_time); + if (onion_c->onion_connected < ONION_CONNECTION_SECONDS * 2) { ++onion_c->onion_connected; }