diff options
author | Coren[m] <Break@Ocean> | 2013-11-14 19:05:36 +0100 |
---|---|---|
committer | Coren[m] <Break@Ocean> | 2013-11-14 19:05:53 +0100 |
commit | 0d8329b3a9b16cd6089810e61ce958fde00046b8 (patch) | |
tree | a86bc471393905c16e8a0d0151a7494784136d63 /toxcore/DHT.c | |
parent | 3af83a6aecdbc719ef38986cba2f238eab3ba3ec (diff) |
Significantly trimmed down version of an ID<=>IP cache.
Besides acknowledging timeouts, the module isn't trying to do anything fancy with the data besides storing and retrieving.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 81 |
1 files changed, 69 insertions, 12 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index eb50cc43..9730771b 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -28,27 +28,20 @@ | |||
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "DHT.h" | 30 | #include "DHT.h" |
31 | #include "network.h" | 31 | #include "assoc.h" |
32 | #include "ping.h" | 32 | #include "ping.h" |
33 | |||
34 | #include "network.h" | ||
35 | #include "LAN_discovery.h" | ||
33 | #include "misc_tools.h" | 36 | #include "misc_tools.h" |
34 | #include "util.h" | 37 | #include "util.h" |
35 | #include "LAN_discovery.h" | ||
36 | |||
37 | /* The number of seconds for a non responsive node to become bad. */ | ||
38 | #define BAD_NODE_TIMEOUT 70 | ||
39 | 38 | ||
40 | /* The max number of nodes to send with send nodes. */ | 39 | /* The max number of nodes to send with send nodes. */ |
41 | #define MAX_SENT_NODES 8 | 40 | #define MAX_SENT_NODES 8 |
42 | 41 | ||
43 | /* Ping timeout in seconds */ | ||
44 | #define PING_TIMEOUT 5 | ||
45 | |||
46 | /* The timeout after which a node is discarded completely. */ | 42 | /* The timeout after which a node is discarded completely. */ |
47 | #define KILL_NODE_TIMEOUT 300 | 43 | #define KILL_NODE_TIMEOUT 300 |
48 | 44 | ||
49 | /* Ping interval in seconds for each node in our lists. */ | ||
50 | #define PING_INTERVAL 60 | ||
51 | |||
52 | /* Ping interval in seconds for each random sending of a get nodes request. */ | 45 | /* Ping interval in seconds for each random sending of a get nodes request. */ |
53 | #define GET_NODE_INTERVAL 5 | 46 | #define GET_NODE_INTERVAL 5 |
54 | 47 | ||
@@ -850,6 +843,14 @@ static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, | |||
850 | /* store the address the *request* was sent to */ | 843 | /* store the address the *request* was sent to */ |
851 | addto_lists(dht, dht->send_nodes[send_nodes_index - 1].ip_port, packet + 1); | 844 | addto_lists(dht, dht->send_nodes[send_nodes_index - 1].ip_port, packet + 1); |
852 | 845 | ||
846 | if (dht->assoc) { | ||
847 | IPPTs ippts; | ||
848 | |||
849 | ippts.ip_port = dht->send_nodes[send_nodes_index - 1].ip_port; | ||
850 | ippts.timestamp = dht->send_nodes[send_nodes_index - 1].timestamp; | ||
851 | Assoc_add_entry(dht->assoc, packet + 1, &ippts, &source); | ||
852 | } | ||
853 | |||
853 | *num_nodes_out = num_nodes; | 854 | *num_nodes_out = num_nodes; |
854 | 855 | ||
855 | return 0; | 856 | return 0; |
@@ -879,6 +880,13 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
879 | 880 | ||
880 | send_ping_request(dht->ping, ipp, nodes4_list[i].client_id); | 881 | send_ping_request(dht->ping, ipp, nodes4_list[i].client_id); |
881 | returnedip_ports(dht, ipp, nodes4_list[i].client_id, packet + 1); | 882 | returnedip_ports(dht, ipp, nodes4_list[i].client_id, packet + 1); |
883 | |||
884 | if (dht->assoc) { | ||
885 | IPPTs ippts; | ||
886 | ippts.ip_port = ipp; | ||
887 | ippts.timestamp = 0; | ||
888 | Assoc_add_entry(dht->assoc, nodes4_list[i].client_id, &ippts, NULL); | ||
889 | } | ||
882 | } | 890 | } |
883 | 891 | ||
884 | return 0; | 892 | return 0; |
@@ -902,6 +910,13 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
902 | if (ipport_isset(&nodes_list[i].ip_port)) { | 910 | if (ipport_isset(&nodes_list[i].ip_port)) { |
903 | send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id); | 911 | send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id); |
904 | returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 912 | returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
913 | |||
914 | if (dht->assoc) { | ||
915 | IPPTs ippts; | ||
916 | ippts.ip_port = nodes_list[i].ip_port; | ||
917 | ippts.timestamp = 0; | ||
918 | Assoc_add_entry(dht->assoc, nodes_list[i].client_id, &ippts, NULL); | ||
919 | } | ||
905 | } | 920 | } |
906 | 921 | ||
907 | return 0; | 922 | return 0; |
@@ -950,7 +965,38 @@ int DHT_addfriend(DHT *dht, uint8_t *client_id) | |||
950 | 965 | ||
951 | dht->friends_list[dht->num_friends].nat.NATping_id = ((uint64_t)random_int() << 32) + random_int(); | 966 | dht->friends_list[dht->num_friends].nat.NATping_id = ((uint64_t)random_int() << 32) + random_int(); |
952 | ++dht->num_friends; | 967 | ++dht->num_friends; |
953 | get_bunchnodes(dht, dht->close_clientlist, LCLIENT_LIST, MAX_FRIEND_CLIENTS, client_id);/*TODO: make this better?*/ | 968 | |
969 | if (dht->assoc) { | ||
970 | /* get up to MAX_FRIEND_CLIENTS connectable nodes */ | ||
971 | DHT_Friend *friend = &dht->friends_list[dht->num_friends - 1]; | ||
972 | |||
973 | Assoc_close_entries close_entries; | ||
974 | memset(&close_entries, 0, sizeof(close_entries)); | ||
975 | close_entries.wanted_id = client_id; | ||
976 | close_entries.count_good = MAX_FRIEND_CLIENTS / 2; | ||
977 | close_entries.count = MAX_FRIEND_CLIENTS; | ||
978 | close_entries.result = calloc(MAX_FRIEND_CLIENTS, sizeof(*close_entries.result)); | ||
979 | |||
980 | uint8_t i, found = Assoc_get_close_entries(dht->assoc, &close_entries); | ||
981 | |||
982 | for (i = 0; i < found; i++) | ||
983 | memcpy(&friend->client_list[i], close_entries.result[i], sizeof(*close_entries.result[i])); | ||
984 | |||
985 | if (found) { | ||
986 | /* send getnodes to the "best" entry */ | ||
987 | Client_data *client = &friend->client_list[0]; | ||
988 | |||
989 | if (ipport_isset(&client->assoc4.ip_port)) | ||
990 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend->client_id); | ||
991 | |||
992 | if (ipport_isset(&client->assoc6.ip_port)) | ||
993 | getnodes(dht, client->assoc6.ip_port, client->client_id, friend->client_id); | ||
994 | } | ||
995 | } | ||
996 | |||
997 | /*TODO: make this better?*/ | ||
998 | get_bunchnodes(dht, dht->close_clientlist, LCLIENT_LIST, MAX_FRIEND_CLIENTS, client_id); | ||
999 | |||
954 | return 0; | 1000 | return 0; |
955 | } | 1001 | } |
956 | 1002 | ||
@@ -1085,6 +1131,13 @@ static void do_Close(DHT *dht) | |||
1085 | 1131 | ||
1086 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) | 1132 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) |
1087 | { | 1133 | { |
1134 | if (dht->assoc) { | ||
1135 | IPPTs ippts; | ||
1136 | ippts.ip_port = ip_port; | ||
1137 | ippts.timestamp = 0; | ||
1138 | Assoc_add_entry(dht->assoc, public_key, &ippts, NULL); | ||
1139 | } | ||
1140 | |||
1088 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); | 1141 | getnodes(dht, ip_port, public_key, dht->c->self_public_key); |
1089 | } | 1142 | } |
1090 | int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, | 1143 | int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, |
@@ -1556,6 +1609,9 @@ DHT *new_DHT(Net_Crypto *c) | |||
1556 | init_cryptopackets(dht); | 1609 | init_cryptopackets(dht); |
1557 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); | 1610 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); |
1558 | 1611 | ||
1612 | /* dhtassoc is not mandatory for now */ | ||
1613 | dht->assoc = new_Assoc(dht); | ||
1614 | |||
1559 | return dht; | 1615 | return dht; |
1560 | } | 1616 | } |
1561 | 1617 | ||
@@ -1570,6 +1626,7 @@ void do_DHT(DHT *dht) | |||
1570 | } | 1626 | } |
1571 | void kill_DHT(DHT *dht) | 1627 | void kill_DHT(DHT *dht) |
1572 | { | 1628 | { |
1629 | kill_Assoc(dht->assoc); | ||
1573 | kill_ping(dht->ping); | 1630 | kill_ping(dht->ping); |
1574 | free(dht->friends_list); | 1631 | free(dht->friends_list); |
1575 | free(dht); | 1632 | free(dht); |