diff options
-rw-r--r-- | toxcore/DHT.c | 78 | ||||
-rw-r--r-- | toxcore/DHT.h | 8 | ||||
-rw-r--r-- | toxcore/Makefile.inc | 2 | ||||
-rw-r--r-- | toxcore/onion_client.c | 8 |
4 files changed, 94 insertions, 2 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 341973a4..454047db 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -2034,6 +2034,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2034 | Client_data *list = dht->close_clientlist; | 2034 | Client_data *list = dht->close_clientlist; |
2035 | 2035 | ||
2036 | uint32_t i; | 2036 | uint32_t i; |
2037 | |||
2037 | for (i = LCLIENT_LIST; i != 0; --i) { | 2038 | for (i = LCLIENT_LIST; i != 0; --i) { |
2038 | IPPTsPng *assoc = NULL; | 2039 | IPPTsPng *assoc = NULL; |
2039 | 2040 | ||
@@ -2060,6 +2061,83 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2060 | return count; | 2061 | return count; |
2061 | } | 2062 | } |
2062 | 2063 | ||
2064 | /* Put a random node from list of list_size in node. LAN_ok is 1 if LAN ips are ok, 0 if we don't want them. */ | ||
2065 | static int random_node_fromlist(Client_data *list, uint16_t list_size, Node_format *node, uint8_t LAN_ok) | ||
2066 | { | ||
2067 | uint32_t i; | ||
2068 | uint32_t num_nodes = 0; | ||
2069 | Client_data *client_list[list_size * 2]; | ||
2070 | IPPTsPng *assoc_list[list_size * 2]; | ||
2071 | |||
2072 | for (i = 0; i < list_size; i++) { | ||
2073 | /* If node is not dead. */ | ||
2074 | Client_data *client = &list[i]; | ||
2075 | IPPTsPng *assoc; | ||
2076 | uint32_t a; | ||
2077 | |||
2078 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { | ||
2079 | /* If node is good. */ | ||
2080 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | ||
2081 | if (!LAN_ok) { | ||
2082 | if (LAN_ip(assoc->ip_port.ip) == 0) | ||
2083 | continue; | ||
2084 | } | ||
2085 | |||
2086 | client_list[num_nodes] = client; | ||
2087 | assoc_list[num_nodes] = assoc; | ||
2088 | ++num_nodes; | ||
2089 | } | ||
2090 | } | ||
2091 | } | ||
2092 | |||
2093 | if (num_nodes == 0) | ||
2094 | return -1; | ||
2095 | |||
2096 | uint32_t rand_node = rand() % num_nodes; | ||
2097 | node->ip_port = assoc_list[rand_node]->ip_port; | ||
2098 | memcpy(node->client_id, client_list[rand_node]->client_id, CLIENT_ID_SIZE); | ||
2099 | return 0; | ||
2100 | } | ||
2101 | |||
2102 | /* Put up to max_num random nodes in nodes. | ||
2103 | * | ||
2104 | * return the number of nodes. | ||
2105 | * | ||
2106 | * NOTE:this is used to pick nodes for paths. | ||
2107 | */ | ||
2108 | uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num) | ||
2109 | { | ||
2110 | if (max_num == 0) | ||
2111 | return 0; | ||
2112 | |||
2113 | uint16_t count = 0; | ||
2114 | Client_data *list = NULL; | ||
2115 | uint16_t list_size = 0; | ||
2116 | uint32_t i; | ||
2117 | |||
2118 | for (i = 0; i < max_num; ++i) { | ||
2119 | uint16_t rand_num = rand() % dht->num_friends + 1; | ||
2120 | |||
2121 | if (rand_num == dht->num_friends) { | ||
2122 | list = dht->close_clientlist; | ||
2123 | list_size = LCLIENT_LIST; | ||
2124 | } else { | ||
2125 | list = dht->friends_list[rand_num].client_list; | ||
2126 | list_size = MAX_FRIEND_CLIENTS; | ||
2127 | } | ||
2128 | |||
2129 | uint8_t LAN_ok = 1; | ||
2130 | |||
2131 | if (count != 0 && LAN_ip(nodes[0].ip_port.ip) != 0) | ||
2132 | LAN_ok = 0; | ||
2133 | |||
2134 | if (random_node_fromlist(list, list_size, &nodes[count], LAN_ok) == 0) | ||
2135 | ++count; | ||
2136 | } | ||
2137 | |||
2138 | return count; | ||
2139 | } | ||
2140 | |||
2063 | void do_hardening(DHT *dht) | 2141 | void do_hardening(DHT *dht) |
2064 | { | 2142 | { |
2065 | uint32_t i; | 2143 | uint32_t i; |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index f572c1fc..5afc8ce8 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -227,6 +227,14 @@ int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_fa | |||
227 | */ | 227 | */ |
228 | uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num); | 228 | uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num); |
229 | 229 | ||
230 | /* Put up to max_num random nodes in nodes. | ||
231 | * | ||
232 | * return the number of nodes. | ||
233 | * | ||
234 | * NOTE:this is used to pick nodes for paths. | ||
235 | */ | ||
236 | uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num); | ||
237 | |||
230 | /* Run this function at least a couple times per second (It's the main loop). */ | 238 | /* Run this function at least a couple times per second (It's the main loop). */ |
231 | void do_DHT(DHT *dht); | 239 | void do_DHT(DHT *dht); |
232 | 240 | ||
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 56114553..51b4c20b 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc | |||
@@ -33,6 +33,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ | |||
33 | ../toxcore/onion.c \ | 33 | ../toxcore/onion.c \ |
34 | ../toxcore/onion_announce.h \ | 34 | ../toxcore/onion_announce.h \ |
35 | ../toxcore/onion_announce.c \ | 35 | ../toxcore/onion_announce.c \ |
36 | ../toxcore/onion_client.h \ | ||
37 | ../toxcore/onion_client.c \ | ||
36 | ../toxcore/misc_tools.h | 38 | ../toxcore/misc_tools.h |
37 | 39 | ||
38 | libtoxcore_la_CFLAGS = -I$(top_srcdir) \ | 40 | libtoxcore_la_CFLAGS = -I$(top_srcdir) \ |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9363bbd7..27823f0d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -447,6 +447,8 @@ static int send_fakeid_announce(Onion_Client *onion_c, uint16_t friend_num) | |||
447 | uint16_t num_nodes = closelist_nodes(onion_c->dht, nodes, MAX_SENT_NODES); | 447 | uint16_t num_nodes = closelist_nodes(onion_c->dht, nodes, MAX_SENT_NODES); |
448 | memcpy(data + FAKEID_DATA_MIN_LENGTH, nodes, sizeof(Node_format) * num_nodes); | 448 | memcpy(data + FAKEID_DATA_MIN_LENGTH, nodes, sizeof(Node_format) * num_nodes); |
449 | return send_onion_data(onion_c, friend_num, data, FAKEID_DATA_MIN_LENGTH + sizeof(Node_format) * num_nodes); | 449 | return send_onion_data(onion_c, friend_num, data, FAKEID_DATA_MIN_LENGTH + sizeof(Node_format) * num_nodes); |
450 | //TODO: somehow make this function send our DHT client id directly to the other if we know theirs but they don't | ||
451 | //seem to know ours. | ||
450 | } | 452 | } |
451 | 453 | ||
452 | /* Get the friend_num of a friend. | 454 | /* Get the friend_num of a friend. |
@@ -583,8 +585,10 @@ int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port) | |||
583 | */ | 585 | */ |
584 | int random_path(Onion_Client *onion_c, Node_format *nodes) | 586 | int random_path(Onion_Client *onion_c, Node_format *nodes) |
585 | { | 587 | { |
586 | //TODO | 588 | if (random_nodes_path(onion_c->dht, nodes, 3) != 3) |
587 | return -1; | 589 | return -1; |
590 | |||
591 | return 0; | ||
588 | } | 592 | } |
589 | 593 | ||
590 | #define ANNOUNCE_FRIEND 30 | 594 | #define ANNOUNCE_FRIEND 30 |