summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index f38ce3a5..894458a3 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -134,7 +134,7 @@ static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
134 * 134 *
135 * return True(1) or False(0) 135 * return True(1) or False(0)
136 */ 136 */
137static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) 137static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port)
138{ 138{
139 uint32_t i; 139 uint32_t i;
140 uint64_t temp_time = unix_time(); 140 uint64_t temp_time = unix_time();
@@ -299,10 +299,14 @@ static int replace_bad( Client_data *list,
299 uint8_t *client_id, 299 uint8_t *client_id,
300 IP_Port ip_port ) 300 IP_Port ip_port )
301{ 301{
302 uint32_t i; 302 uint32_t i = 0;
303 uint64_t temp_time = unix_time(); 303 uint64_t temp_time = unix_time();
304 304
305 for (i = 0; i < length; ++i) { 305 /* reserve the lower end to IPv4 for now */
306 if (ip_port.ip.family == AF_INET6)
307 i = length / 2;
308
309 for (; i < length; ++i) {
306 /* If node is bad */ 310 /* If node is bad */
307 if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) { 311 if (is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) {
308 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 312 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
@@ -347,11 +351,15 @@ static int replace_good( Client_data *list,
347 IP_Port ip_port, 351 IP_Port ip_port,
348 uint8_t *comp_client_id ) 352 uint8_t *comp_client_id )
349{ 353{
350 uint32_t i; 354 uint32_t i = 0;
351 uint64_t temp_time = unix_time(); 355 uint64_t temp_time = unix_time();
352 sort_list(list, length, comp_client_id); 356 sort_list(list, length, comp_client_id);
353 357
354 for (i = 0; i < length; ++i) 358 /* reserve the lower end to IPv4 for now */
359 if (ip_port.ip.family == AF_INET6)
360 i = length / 2;
361
362 for (; i < length; ++i)
355 if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) { 363 if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) {
356 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 364 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
357 list[i].ip_port = ip_port; 365 list[i].ip_port = ip_port;
@@ -372,36 +380,32 @@ void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
372{ 380{
373 uint32_t i; 381 uint32_t i;
374 382
383 /* convert IPv4-in-IPv6 to IPv4 */
384 if ((ip_port.ip.family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&ip_port.ip.ip6)) {
385 ip_port.ip.family = AF_INET;
386 ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3];
387 }
388
375 /* NOTE: Current behavior if there are two clients with the same id is 389 /* NOTE: Current behavior if there are two clients with the same id is
376 * to replace the first ip by the second. 390 * to replace the first ip by the second.
377 */ 391 */
378 if (!client_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 392 if (!client_or_ip_port_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
379 if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 393 if (replace_bad(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) {
380 /* If we can't replace bad nodes we try replacing good ones. */ 394 /* If we can't replace bad nodes we try replacing good ones. */
381 replace_good( dht->close_clientlist, 395 replace_good(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port,
382 LCLIENT_LIST, 396 dht->c->self_public_key);
383 client_id,
384 ip_port,
385 dht->c->self_public_key );
386 } 397 }
387 } 398 }
388 399
389 for (i = 0; i < dht->num_friends; ++i) { 400 for (i = 0; i < dht->num_friends; ++i) {
390 if (!client_in_list( dht->friends_list[i].client_list, 401 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list,
391 MAX_FRIEND_CLIENTS, 402 MAX_FRIEND_CLIENTS, client_id, ip_port)) {
392 client_id, 403
393 ip_port )) { 404 if (replace_bad(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
394 405 client_id, ip_port)) {
395 if (replace_bad( dht->friends_list[i].client_list,
396 MAX_FRIEND_CLIENTS,
397 client_id,
398 ip_port )) {
399 /* If we can't replace bad nodes we try replacing good ones. */ 406 /* If we can't replace bad nodes we try replacing good ones. */
400 replace_good( dht->friends_list[i].client_list, 407 replace_good(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
401 MAX_FRIEND_CLIENTS, 408 client_id, ip_port, dht->friends_list[i].client_id);
402 client_id,
403 ip_port,
404 dht->friends_list[i].client_id );
405 } 409 }
406 } 410 }
407 } 411 }