diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c98b9d95..521562b8 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -193,6 +193,8 @@ static void dht_pk_callback(void *data, int32_t number, const uint8_t *dht_publi if (friend_not_valid(m, number)) return; + m->friendlist[number].dht_ping_lastrecv = unix_time(); + if (memcmp(m->friendlist[number].dht_temp_pk, dht_public_key, crypto_box_PUBLICKEYBYTES) == 0) return; @@ -1765,6 +1767,8 @@ static int handle_status(void *object, int i, uint8_t status) } else { /* Went offline. */ m->friendlist[i].crypt_connection_id = -1; + m->friendlist[i].dht_ping_lastrecv = temp_time; + if (m->friendlist[i].status == FRIEND_ONLINE) { set_friend_status(m, i, FRIEND_CONFIRMED); } @@ -2400,15 +2404,23 @@ void do_friends(Messenger *m) */ check_friend_request_timed_out(m, i, temp_time); - if (m->friendlist[i].dht_lock) - set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk, - current_time_monotonic()); - - set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_ip_port); - + } else { + if (m->friendlist[i].dht_ping_lastrecv + FRIEND_DHT_TIMEOUT < temp_time) { + if (m->friendlist[i].dht_lock) { + DHT_delfriend(m->dht, m->friendlist[i].dht_temp_pk, m->friendlist[i].dht_lock); + m->friendlist[i].dht_lock = 0; + } + } } friend_new_connection(m, i, m->friendlist[i].client_id); + + if (m->friendlist[i].dht_lock) + set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_temp_pk, + current_time_monotonic()); + + set_direct_ip_port(m->net_crypto, m->friendlist[i].crypt_connection_id, m->friendlist[i].dht_ip_port); + } if (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 2cbc57e5..36da8425 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -120,6 +120,7 @@ enum { #define AVATAR_DATA_TRANSFER_LIMIT (10*AVATAR_MAX_DATA_LENGTH) #define AVATAR_DATA_TRANSFER_TIMEOUT (60) /* 164kB every 60 seconds is not a lot */ +#define FRIEND_DHT_TIMEOUT (5 * 60) /* Time before friend is removed from the DHT after last hearing about him. */ /* USERSTATUS - * Represents userstatuses someone can have. @@ -200,6 +201,7 @@ typedef struct { uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; uint16_t dht_lock; IP_Port dht_ip_port; + uint64_t dht_ping_lastrecv; uint32_t onion_friendnum; int crypt_connection_id; diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 97afeb79..8ba1fdc5 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1204,23 +1204,6 @@ 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, 0); - } -} /* 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) @@ -1293,7 +1276,6 @@ void do_onion_client(Onion_Client *onion_c) if (onion_isconnected(onion_c)) { for (i = 0; i < onion_c->num_friends; ++i) { do_friend(onion_c, i); - cleanup_friend(onion_c, i); } }