From 834ffee47dab5a03c0a0f8d08809c377f7b8ef7c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 27 Sep 2014 08:00:42 -0400 Subject: Make the ip port from DHT timeout after a while. Some small fixes. --- toxcore/DHT.c | 26 ++++++++++++++++++++------ toxcore/Messenger.c | 9 +++++++++ toxcore/Messenger.h | 4 ++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index f7d460d8..db03def1 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -708,29 +708,43 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id) } else used++; + DHT_Friend *friend_foundip = 0; + for (i = 0; i < dht->num_friends; ++i) { if (!client_or_ip_port_in_list(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, dht->friends_list[i].client_id)) { + DHT_Friend *friend = &dht->friends_list[i]; if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) { - uint32_t j; - - for (j = 0; j < friend->lock_count; ++j) { - if (friend->callbacks[j].ip_callback) - friend->callbacks[j].ip_callback(friend->callbacks[j].data, friend->callbacks[j].number, ip_port); - } + friend_foundip = friend; } used++; } } else { + DHT_Friend *friend = &dht->friends_list[i]; + + if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) { + friend_foundip = friend; + } + used++; } } + if (friend_foundip) { + uint32_t j; + + for (j = 0; j < friend_foundip->lock_count; ++j) { + if (friend_foundip->callbacks[j].ip_callback) + friend_foundip->callbacks[j].ip_callback(friend_foundip->callbacks[j].data, friend_foundip->callbacks[j].number, + ip_port); + } + } + #ifdef ENABLE_ASSOC_DHT if (dht->assoc) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index df736ebe..8faa7f03 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -183,6 +183,7 @@ static void dht_ip_callback(void *data, int32_t number, IP_Port ip_port) set_direct_ip_port(m->net_crypto, m->friendlist[number].crypt_connection_id, ip_port); m->friendlist[number].dht_ip_port = ip_port; + m->friendlist[number].dht_ip_port_lastrecv = unix_time(); } /* Callback for dht public key changes. */ @@ -1629,10 +1630,14 @@ static int handle_new_connections(void *object, New_Connection *n_c) if (n_c->source.ip.family != AF_INET && n_c->source.ip.family != AF_INET6) { set_direct_ip_port(m->net_crypto, m->friendlist[friend_id].crypt_connection_id, m->friendlist[friend_id].dht_ip_port); + } else { + m->friendlist[friend_id].dht_ip_port = n_c->source; + m->friendlist[friend_id].dht_ip_port_lastrecv = unix_time(); } dht_pk_callback(m, friend_id, n_c->dht_public_key); + nc_dht_pk_callback(m->net_crypto, id, &dht_pk_callback, m, friend_id); return 0; } @@ -2410,6 +2415,10 @@ void do_friends(Messenger *m) m->friendlist[i].dht_lock = 0; } } + + if (m->friendlist[i].dht_ip_port_lastrecv + FRIEND_DHT_TIMEOUT < temp_time) { + m->friendlist[i].dht_ip_port.ip.family = 0; + } } if (friend_new_connection(m, i, m->friendlist[i].client_id) == 0) { diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 36da8425..60d00225 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -120,7 +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. */ +#define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT /* Time before friend is removed from the DHT after last hearing about him. */ /* USERSTATUS - * Represents userstatuses someone can have. @@ -201,7 +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; + uint64_t dht_ping_lastrecv, dht_ip_port_lastrecv; uint32_t onion_friendnum; int crypt_connection_id; -- cgit v1.2.3