diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 29e6baa3..ca815844 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -717,6 +717,12 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *publ | |||
717 | } | 717 | } |
718 | } | 718 | } |
719 | 719 | ||
720 | static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) | ||
721 | { | ||
722 | memcpy(cmp_public_key, comp_public_key, crypto_box_PUBLICKEYBYTES); | ||
723 | qsort(list, length, sizeof(Client_data), cmp_dht_entry); | ||
724 | } | ||
725 | |||
720 | /* Replace a first bad (or empty) node with this one | 726 | /* Replace a first bad (or empty) node with this one |
721 | * or replace a possibly bad node (tests failed or not done yet) | 727 | * or replace a possibly bad node (tests failed or not done yet) |
722 | * that is further than any other in the list | 728 | * that is further than any other in the list |
@@ -740,8 +746,7 @@ static int replace_all( Client_data *list, | |||
740 | return 0; | 746 | return 0; |
741 | 747 | ||
742 | if (store_node_ok(&list[1], public_key, comp_public_key) || store_node_ok(&list[0], public_key, comp_public_key)) { | 748 | if (store_node_ok(&list[1], public_key, comp_public_key) || store_node_ok(&list[0], public_key, comp_public_key)) { |
743 | memcpy(cmp_public_key, comp_public_key, crypto_box_PUBLICKEYBYTES); | 749 | sort_client_list(list, length, comp_public_key); |
744 | qsort(list, length, sizeof(Client_data), cmp_dht_entry); | ||
745 | 750 | ||
746 | IPPTsPng *ipptp_write = NULL; | 751 | IPPTsPng *ipptp_write = NULL; |
747 | IPPTsPng *ipptp_clear = NULL; | 752 | IPPTsPng *ipptp_clear = NULL; |
@@ -1391,6 +1396,8 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1391 | uint32_t num_nodes = 0; | 1396 | uint32_t num_nodes = 0; |
1392 | Client_data *client_list[list_count * 2]; | 1397 | Client_data *client_list[list_count * 2]; |
1393 | IPPTsPng *assoc_list[list_count * 2]; | 1398 | IPPTsPng *assoc_list[list_count * 2]; |
1399 | unsigned int sort = 0; | ||
1400 | _Bool sort_ok = 0; | ||
1394 | 1401 | ||
1395 | for (i = 0; i < list_count; i++) { | 1402 | for (i = 0; i < list_count; i++) { |
1396 | /* If node is not dead. */ | 1403 | /* If node is not dead. */ |
@@ -1400,6 +1407,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1400 | 1407 | ||
1401 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1408 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) |
1402 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1409 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1410 | sort = 0; | ||
1403 | not_kill++; | 1411 | not_kill++; |
1404 | 1412 | ||
1405 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { | 1413 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { |
@@ -1413,9 +1421,20 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1413 | assoc_list[num_nodes] = assoc; | 1421 | assoc_list[num_nodes] = assoc; |
1414 | ++num_nodes; | 1422 | ++num_nodes; |
1415 | } | 1423 | } |
1424 | } else { | ||
1425 | ++sort; | ||
1426 | |||
1427 | /* Timed out should be at beginning, if they are not, sort the list. */ | ||
1428 | if (sort > 1 && sort < (((i + 1) * 2) - 1)) { | ||
1429 | sort_ok = 1; | ||
1430 | } | ||
1416 | } | 1431 | } |
1417 | } | 1432 | } |
1418 | 1433 | ||
1434 | if (sort_ok) { | ||
1435 | sort_client_list(list, list_count, public_key); | ||
1436 | } | ||
1437 | |||
1419 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { | 1438 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { |
1420 | uint32_t rand_node = rand() % (num_nodes * 2); | 1439 | uint32_t rand_node = rand() % (num_nodes * 2); |
1421 | 1440 | ||