diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index eaf417e2..5f4aa228 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -257,17 +257,29 @@ static int friend_number(DHT *dht, uint8_t *client_id) | |||
257 | return -1; | 257 | return -1; |
258 | } | 258 | } |
259 | 259 | ||
260 | /*TODO: change this to 7 when done*/ | ||
261 | #define HARDENING_ALL_OK 2 | ||
262 | /* return 0 if not. | ||
263 | * return 1 if route request are ok | ||
264 | * return 2 if it responds to send node packets correctly | ||
265 | * return 4 if it can test other nodes correctly | ||
266 | * return HARDENING_ALL_OK if all ok. | ||
267 | */ | ||
268 | static uint8_t hardening_correct(Hardening *h) | ||
269 | { | ||
270 | return h->routes_requests_ok + (h->send_nodes_ok << 1) + (h->testing_requests << 2); | ||
271 | } | ||
260 | /* | 272 | /* |
261 | * helper for get_close_nodes(). argument list is a monster :D | 273 | * helper for get_close_nodes(). argument list is a monster :D |
262 | */ | 274 | */ |
263 | static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nodes_list, | 275 | static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nodes_list, |
264 | sa_family_t sa_family, Client_data *client_list, uint32_t client_list_length, | 276 | sa_family_t sa_family, Client_data *client_list, uint32_t client_list_length, |
265 | int *num_nodes_ptr, uint8_t is_LAN) | 277 | uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) |
266 | { | 278 | { |
267 | if ((sa_family != AF_INET) && (sa_family != AF_INET6)) | 279 | if ((sa_family != AF_INET) && (sa_family != AF_INET6)) |
268 | return; | 280 | return; |
269 | 281 | ||
270 | int num_nodes = *num_nodes_ptr; | 282 | uint32_t num_nodes = *num_nodes_ptr; |
271 | int ipv46x, j, closest; | 283 | int ipv46x, j, closest; |
272 | uint32_t i; | 284 | uint32_t i; |
273 | 285 | ||
@@ -317,6 +329,9 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
317 | if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) | 329 | if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) |
318 | continue; | 330 | continue; |
319 | 331 | ||
332 | if (want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK && !id_equal(client_id, client->client_id)) | ||
333 | continue; | ||
334 | |||
320 | if (num_nodes < MAX_SENT_NODES) { | 335 | if (num_nodes < MAX_SENT_NODES) { |
321 | memcpy(nodes_list[num_nodes].client_id, | 336 | memcpy(nodes_list[num_nodes].client_id, |
322 | client->client_id, | 337 | client->client_id, |
@@ -355,17 +370,20 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
355 | * | 370 | * |
356 | * TODO: For the love of based <your favorite deity, in doubt use "love"> make | 371 | * TODO: For the love of based <your favorite deity, in doubt use "love"> make |
357 | * this function cleaner and much more efficient. | 372 | * this function cleaner and much more efficient. |
373 | * | ||
374 | * want_good : do we want only good nodes as checked with the hardening returned or not? | ||
358 | */ | 375 | */ |
359 | static int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, uint8_t is_LAN) | 376 | static uint32_t get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, |
377 | uint8_t is_LAN, uint8_t want_good) | ||
360 | { | 378 | { |
361 | int num_nodes = 0, i; | 379 | uint32_t num_nodes = 0, i; |
362 | get_close_nodes_inner(dht, client_id, nodes_list, sa_family, | 380 | get_close_nodes_inner(dht, client_id, nodes_list, sa_family, |
363 | dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN); | 381 | dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN, want_good); |
364 | 382 | ||
365 | for (i = 0; i < dht->num_friends; ++i) | 383 | for (i = 0; i < dht->num_friends; ++i) |
366 | get_close_nodes_inner(dht, client_id, nodes_list, sa_family, | 384 | get_close_nodes_inner(dht, client_id, nodes_list, sa_family, |
367 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, | 385 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, |
368 | &num_nodes, is_LAN); | 386 | &num_nodes, is_LAN, want_good); |
369 | 387 | ||
370 | return num_nodes; | 388 | return num_nodes; |
371 | } | 389 | } |
@@ -651,7 +669,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
651 | + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 669 | + Node4_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
652 | 670 | ||
653 | Node_format nodes_list[MAX_SENT_NODES]; | 671 | Node_format nodes_list[MAX_SENT_NODES]; |
654 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0); | 672 | uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0, 1); |
655 | 673 | ||
656 | if (num_nodes == 0) | 674 | if (num_nodes == 0) |
657 | return 0; | 675 | return 0; |
@@ -718,7 +736,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
718 | + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; | 736 | + Node_format_size * MAX_SENT_NODES + NODES_ENCRYPTED_MESSAGE_LENGTH + crypto_box_MACBYTES]; |
719 | 737 | ||
720 | Node_format nodes_list[MAX_SENT_NODES]; | 738 | Node_format nodes_list[MAX_SENT_NODES]; |
721 | int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0); | 739 | uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0, 1); |
722 | 740 | ||
723 | if (num_nodes == 0) | 741 | if (num_nodes == 0) |
724 | return 0; | 742 | return 0; |
@@ -1606,10 +1624,11 @@ static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *qu | |||
1606 | static int have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num, sa_family_t sa_family) | 1624 | static int have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num, sa_family_t sa_family) |
1607 | { | 1625 | { |
1608 | Node_format nodes_list[MAX_SENT_NODES]; | 1626 | Node_format nodes_list[MAX_SENT_NODES]; |
1609 | int num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1); | 1627 | uint32_t num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1, 1); |
1610 | 1628 | ||
1611 | if (num_nodes < 1) | 1629 | if (num_nodes < num) { |
1612 | return -1; | 1630 | num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1, 0); |
1631 | } | ||
1613 | 1632 | ||
1614 | int counter = 0; | 1633 | int counter = 0; |
1615 | uint32_t i, j; | 1634 | uint32_t i, j; |
@@ -1735,9 +1754,9 @@ Node_format random_node(DHT *dht, sa_family_t sa_family) | |||
1735 | 1754 | ||
1736 | Node_format nodes_list[MAX_SENT_NODES]; | 1755 | Node_format nodes_list[MAX_SENT_NODES]; |
1737 | memset(nodes_list, 0, sizeof(nodes_list)); | 1756 | memset(nodes_list, 0, sizeof(nodes_list)); |
1738 | int num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1); | 1757 | uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); |
1739 | 1758 | ||
1740 | if (num_nodes < 1) | 1759 | if (num_nodes == 0) |
1741 | return nodes_list[0]; | 1760 | return nodes_list[0]; |
1742 | else | 1761 | else |
1743 | return nodes_list[rand() % num_nodes]; | 1762 | return nodes_list[rand() % num_nodes]; |