From bba2027e7b9f1d2d02af3ecc9c6d8df9674ea4b0 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 13 Feb 2014 17:54:09 -0500 Subject: [PATCH] Onion now eventualy deletes friends from the DHT search. This should improve long term bandwidth usage by a bit. --- toxcore/onion_client.c | 25 +++++++++++++++++++++++++ toxcore/onion_client.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index e55e92f3..c03dfcea 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -390,6 +390,8 @@ static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t crypto_box_PUBLICKEYBYTES) != 0) { DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id); + onion_c->friends_list[friend_num].last_seen = unix_time(); + if (DHT_addfriend(onion_c->dht, data + 1 + sizeof(uint64_t)) == 1) { return 1; } @@ -712,6 +714,9 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on if ((uint32_t)friend_num >= onion_c->num_friends) return -1; + if (is_online == 0 && onion_c->friends_list[friend_num].is_online == 1) + onion_c->friends_list[friend_num].last_seen = unix_time(); + onion_c->friends_list[friend_num].is_online = is_online; /* This should prevent some clock related issues */ @@ -788,6 +793,25 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) } } + +/* Timeout before which a peer is considered dead and removed from the DHT search. */ +#define DEAD_ONION_TIMEOUT (10 * 60) + +static void cleanup_friend(Onion_Client *onion_c, uint16_t friendnum) +{ + if (friendnum >= onion_c->num_friends) + return; + + if (onion_c->friends_list[friendnum].status == 0) + return; + + if (onion_c->friends_list[friendnum].is_fake_clientid && !onion_c->friends_list[friendnum].is_online + && is_timeout(onion_c->friends_list[friendnum].last_seen, DEAD_ONION_TIMEOUT)) { + onion_c->friends_list[friendnum].is_fake_clientid = 0; + DHT_delfriend(onion_c->dht, onion_c->friends_list[friendnum].fake_client_id); + } +} + /* Function to call when onion data packet with contents beginning with byte is received. */ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object) { @@ -845,6 +869,7 @@ void do_onion_client(Onion_Client *onion_c) for (i = 0; i < onion_c->num_friends; ++i) { do_friend(onion_c, i); + cleanup_friend(onion_c, i); } onion_c->last_run = unix_time(); diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 708d9093..36b5b5c3 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -61,6 +61,8 @@ typedef struct { uint64_t last_fakeid_dht_sent; uint64_t last_noreplay; + + uint64_t last_seen; } Onion_Friend; typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len);