diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 88 |
1 files changed, 8 insertions, 80 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index d28c1ecf..deb71864 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -164,12 +164,17 @@ void to_net_family(IP *ip) | |||
164 | ip->family = TOX_AF_INET6; | 164 | ip->family = TOX_AF_INET6; |
165 | } | 165 | } |
166 | 166 | ||
167 | void to_host_family(IP *ip) | 167 | int to_host_family(IP *ip) |
168 | { | 168 | { |
169 | if (ip->family == TOX_AF_INET) | 169 | if (ip->family == TOX_AF_INET) { |
170 | ip->family = AF_INET; | 170 | ip->family = AF_INET; |
171 | else if (ip->family == TOX_AF_INET6) | 171 | return 0; |
172 | } else if (ip->family == TOX_AF_INET6) { | ||
172 | ip->family = AF_INET6; | 173 | ip->family = AF_INET6; |
174 | return 0; | ||
175 | } else { | ||
176 | return -1; | ||
177 | } | ||
173 | } | 178 | } |
174 | 179 | ||
175 | /* Pack number of nodes into data of maxlength length. | 180 | /* Pack number of nodes into data of maxlength length. |
@@ -2082,82 +2087,6 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2082 | return count; | 2087 | return count; |
2083 | } | 2088 | } |
2084 | 2089 | ||
2085 | /* 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. */ | ||
2086 | static int random_node_fromlist(Client_data *list, uint16_t list_size, Node_format *node, uint8_t LAN_ok) | ||
2087 | { | ||
2088 | uint32_t i; | ||
2089 | uint32_t num_nodes = 0; | ||
2090 | Client_data *client_list[list_size * 2]; | ||
2091 | IPPTsPng *assoc_list[list_size * 2]; | ||
2092 | |||
2093 | for (i = 0; i < list_size; i++) { | ||
2094 | /* If node is not dead. */ | ||
2095 | Client_data *client = &list[i]; | ||
2096 | IPPTsPng *assoc; | ||
2097 | uint32_t a; | ||
2098 | |||
2099 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { | ||
2100 | /* If node is good. */ | ||
2101 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | ||
2102 | if (!LAN_ok) { | ||
2103 | if (LAN_ip(assoc->ip_port.ip) == 0) | ||
2104 | continue; | ||
2105 | } | ||
2106 | |||
2107 | client_list[num_nodes] = client; | ||
2108 | assoc_list[num_nodes] = assoc; | ||
2109 | ++num_nodes; | ||
2110 | } | ||
2111 | } | ||
2112 | } | ||
2113 | |||
2114 | if (num_nodes == 0) | ||
2115 | return -1; | ||
2116 | |||
2117 | uint32_t rand_node = rand() % num_nodes; | ||
2118 | node->ip_port = assoc_list[rand_node]->ip_port; | ||
2119 | memcpy(node->client_id, client_list[rand_node]->client_id, CLIENT_ID_SIZE); | ||
2120 | return 0; | ||
2121 | } | ||
2122 | |||
2123 | /* Put up to max_num random nodes in nodes. | ||
2124 | * | ||
2125 | * return the number of nodes. | ||
2126 | * | ||
2127 | * NOTE:this is used to pick nodes for paths. | ||
2128 | * | ||
2129 | * TODO: remove the LAN stuff from this. | ||
2130 | */ | ||
2131 | uint16_t random_nodes_path(const DHT *dht, Node_format *nodes, uint16_t max_num) | ||
2132 | { | ||
2133 | if (max_num == 0) | ||
2134 | return 0; | ||
2135 | |||
2136 | if (dht->num_friends == 0) | ||
2137 | return 0; | ||
2138 | |||
2139 | uint16_t count = 0; | ||
2140 | uint16_t list_size = 0; | ||
2141 | uint32_t i; | ||
2142 | |||
2143 | for (i = 0; i < max_num; ++i) { | ||
2144 | Client_data *list = NULL; | ||
2145 | uint16_t rand_num = rand() % (dht->num_friends); | ||
2146 | list = dht->friends_list[rand_num].client_list; | ||
2147 | list_size = MAX_FRIEND_CLIENTS; | ||
2148 | |||
2149 | uint8_t LAN_ok = 1; | ||
2150 | |||
2151 | if (count != 0 && LAN_ip(nodes[0].ip_port.ip) != 0) | ||
2152 | LAN_ok = 0; | ||
2153 | |||
2154 | if (random_node_fromlist(list, list_size, &nodes[count], LAN_ok) == 0) | ||
2155 | ++count; | ||
2156 | } | ||
2157 | |||
2158 | return count; | ||
2159 | } | ||
2160 | |||
2161 | void do_hardening(DHT *dht) | 2090 | void do_hardening(DHT *dht) |
2162 | { | 2091 | { |
2163 | uint32_t i; | 2092 | uint32_t i; |
@@ -2331,7 +2260,6 @@ void kill_DHT(DHT *dht) | |||
2331 | kill_Assoc(dht->assoc); | 2260 | kill_Assoc(dht->assoc); |
2332 | #endif | 2261 | #endif |
2333 | networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); | 2262 | networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); |
2334 | networking_registerhandler(dht->net, NET_PACKET_SEND_NODES, NULL, NULL); | ||
2335 | networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); | 2263 | networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); |
2336 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); | 2264 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); |
2337 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); | 2265 | cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); |