summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-12-15 14:13:28 -0500
committerirungentoo <irungentoo@gmail.com>2015-12-15 14:13:28 -0500
commit836e18075748722a64d77ad4a7dd0a9b8903bc5b (patch)
treec6717293e7b638058c0df3c9ab6ec04264e73490 /toxcore
parent5e6bd7bfeef268cd5a232641ed9e20236ad3eb94 (diff)
More efficient DHT searching.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c80
-rw-r--r--toxcore/DHT.h3
2 files changed, 71 insertions, 12 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index ca815844..a958938d 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -63,7 +63,7 @@
63#define NAT_PING_RESPONSE 1 63#define NAT_PING_RESPONSE 1
64 64
65/* Number of get node requests to send to quickly find close nodes. */ 65/* Number of get node requests to send to quickly find close nodes. */
66#define MAX_BOOTSTRAP_TIMES 20 66#define MAX_BOOTSTRAP_TIMES 0
67 67
68/* Compares pk1 and pk2 with pk. 68/* Compares pk1 and pk2 with pk.
69 * 69 *
@@ -778,35 +778,83 @@ static int replace_all( Client_data *list,
778 return 0; 778 return 0;
779} 779}
780 780
781static _Bool is_pk_in_client_list(Client_data *list, unsigned int client_list_length, const uint8_t *public_key,
782 IP_Port ip_port)
783{
784 unsigned int i;
785
786 for (i = 0; i < client_list_length; ++i) {
787 if ((ip_port.ip.family == AF_INET && !is_timeout(list[i].assoc4.timestamp, BAD_NODE_TIMEOUT))
788 || (ip_port.ip.family == AF_INET6 && !is_timeout(list[i].assoc6.timestamp, BAD_NODE_TIMEOUT))) {
789 if (memcmp(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
790 return 1;
791 }
792 }
793 }
794
795 return 0;
796}
797
781/* Check if the node obtained with a get_nodes with public_key should be pinged. 798/* Check if the node obtained with a get_nodes with public_key should be pinged.
782 * NOTE: for best results call it after addto_lists; 799 * NOTE: for best results call it after addto_lists;
783 * 800 *
784 * return 0 if the node should not be pinged. 801 * return 0 if the node should not be pinged.
785 * return 1 if it should. 802 * return 1 if it should.
786 */ 803 */
787static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key) 804static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port)
788{ 805{
806 _Bool ret = 0;
807
789 if (store_node_ok(&dht->close_clientlist[1], public_key, dht->self_public_key)) { 808 if (store_node_ok(&dht->close_clientlist[1], public_key, dht->self_public_key)) {
790 return 1; 809 ret = 1;
791 } 810 }
792 811
793 if (store_node_ok(&dht->close_clientlist[0], public_key, dht->self_public_key)) { 812 if (store_node_ok(&dht->close_clientlist[0], public_key, dht->self_public_key)) {
794 return 1; 813 ret = 1;
814 }
815
816 if (ret && !client_in_nodelist(dht->to_bootstrap, dht->num_to_bootstrap, public_key)
817 && !is_pk_in_client_list(dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) {
818 if (dht->num_to_bootstrap < MAX_SENT_NODES) {
819 memcpy(dht->to_bootstrap[dht->num_to_bootstrap].public_key, public_key, crypto_box_PUBLICKEYBYTES);
820 dht->to_bootstrap[dht->num_to_bootstrap].ip_port = ip_port;
821 ++dht->num_to_bootstrap;
822 } else {
823 //TODO: ipv6 vs v4
824 add_to_list(dht->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht->self_public_key);
825 }
795 } 826 }
796 827
797 unsigned int i; 828 unsigned int i;
798 829
799 for (i = 0; i < dht->num_friends; ++i) { 830 for (i = 0; i < dht->num_friends; ++i) {
800 if (store_node_ok(&dht->friends_list[i].client_list[1], public_key, dht->friends_list[i].public_key)) { 831 _Bool store_ok = 0;
801 return 1; 832
833 DHT_Friend *friend = &dht->friends_list[i];
834
835 if (store_node_ok(&friend->client_list[1], public_key, friend->public_key)) {
836 store_ok = 1;
802 } 837 }
803 838
804 if (store_node_ok(&dht->friends_list[i].client_list[0], public_key, dht->friends_list[i].public_key)) { 839 if (store_node_ok(&friend->client_list[0], public_key, friend->public_key)) {
805 return 1; 840 store_ok = 1;
841 }
842
843 if (store_ok && !client_in_nodelist(friend->to_bootstrap, friend->num_to_bootstrap, public_key)
844 && !is_pk_in_client_list(friend->client_list, MAX_FRIEND_CLIENTS, public_key, ip_port)) {
845 if (friend->num_to_bootstrap < MAX_SENT_NODES) {
846 memcpy(friend->to_bootstrap[friend->num_to_bootstrap].public_key, public_key, crypto_box_PUBLICKEYBYTES);
847 friend->to_bootstrap[friend->num_to_bootstrap].ip_port = ip_port;
848 ++friend->num_to_bootstrap;
849 } else {
850 add_to_list(friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, friend->public_key);
851 }
852
853 ret = 1;
806 } 854 }
807 } 855 }
808 856
809 return 0; 857 return ret;
810} 858}
811 859
812/* Attempt to add client with ip_port and public_key to the friends client list 860/* Attempt to add client with ip_port and public_key to the friends client list
@@ -1197,9 +1245,9 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa
1197 uint32_t i; 1245 uint32_t i;
1198 1246
1199 for (i = 0; i < num_nodes; i++) { 1247 for (i = 0; i < num_nodes; i++) {
1200 if (ipport_isset(&plain_nodes[i].ip_port) && (LAN_ip(plain_nodes[i].ip_port.ip) == 0 1248
1201 || ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key))) { 1249 if (ipport_isset(&plain_nodes[i].ip_port)) {
1202 send_ping_request(dht->ping, plain_nodes[i].ip_port, plain_nodes[i].public_key); 1250 ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, plain_nodes[i].ip_port);
1203 returnedip_ports(dht, plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1); 1251 returnedip_ports(dht, plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1);
1204 } 1252 }
1205 } 1253 }
@@ -1491,6 +1539,14 @@ static void do_DHT_friends(DHT *dht)
1491 */ 1539 */
1492static void do_Close(DHT *dht) 1540static void do_Close(DHT *dht)
1493{ 1541{
1542 unsigned int i;
1543
1544 for (i = 0; i < dht->num_to_bootstrap; ++i) {
1545 getnodes(dht, dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key, NULL);
1546 }
1547
1548 dht->num_to_bootstrap = 0;
1549
1494 uint8_t not_killed = do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->self_public_key, 1550 uint8_t not_killed = do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->self_public_key,
1495 dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times); 1551 dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times);
1496 1552
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index c83ca073..4ae99049 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -231,6 +231,9 @@ typedef struct {
231 uint64_t last_run; 231 uint64_t last_run;
232 232
233 Cryptopacket_Handles cryptopackethandlers[256]; 233 Cryptopacket_Handles cryptopackethandlers[256];
234
235 Node_format to_bootstrap[MAX_SENT_NODES];
236 unsigned int num_to_bootstrap;
234} DHT; 237} DHT;
235/*----------------------------------------------------------------------------------*/ 238/*----------------------------------------------------------------------------------*/
236 239