summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c36
-rw-r--r--toxcore/DHT.h4
2 files changed, 22 insertions, 18 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 0590aec9..ca87ad90 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2244,18 +2244,17 @@ DHT *new_DHT(Networking_Core *net)
2244 2244
2245void do_DHT(DHT *dht) 2245void do_DHT(DHT *dht)
2246{ 2246{
2247 // Load friends/clients if first call to do_DHT
2248 if (dht->has_loaded_friends_clients) {
2249 dht->has_loaded_friends_clients = 0;
2250 DHT_connect_after_load(dht);
2251 }
2252
2253 unix_time_update(); 2247 unix_time_update();
2254 2248
2255 if (dht->last_run == unix_time()) { 2249 if (dht->last_run == unix_time()) {
2256 return; 2250 return;
2257 } 2251 }
2258 2252
2253 // Load friends/clients if first call to do_DHT
2254 if (dht->loaded_num_nodes) {
2255 DHT_connect_after_load(dht);
2256 }
2257
2259 do_Close(dht); 2258 do_Close(dht);
2260 do_DHT_friends(dht); 2259 do_DHT_friends(dht);
2261 do_NAT(dht); 2260 do_NAT(dht);
@@ -2287,7 +2286,7 @@ void kill_DHT(DHT *dht)
2287} 2286}
2288 2287
2289/* new DHT format for load/save, more robust and forward compatible */ 2288/* new DHT format for load/save, more robust and forward compatible */
2290 2289//TODO: Move this closer to Messenger.
2291#define DHT_STATE_COOKIE_GLOBAL 0x159000d 2290#define DHT_STATE_COOKIE_GLOBAL 0x159000d
2292 2291
2293#define DHT_STATE_COOKIE_TYPE 0x11ce 2292#define DHT_STATE_COOKIE_TYPE 0x11ce
@@ -2376,6 +2375,9 @@ void DHT_save(DHT *dht, uint8_t *data)
2376 z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES); 2375 z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES);
2377} 2376}
2378 2377
2378/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */
2379#define SAVE_BOOTSTAP_FREQUENCY 8
2380
2379/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ 2381/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */
2380int DHT_connect_after_load(DHT *dht) 2382int DHT_connect_after_load(DHT *dht)
2381{ 2383{
@@ -2385,17 +2387,22 @@ int DHT_connect_after_load(DHT *dht)
2385 if (!dht->loaded_nodes_list) 2387 if (!dht->loaded_nodes_list)
2386 return -1; 2388 return -1;
2387 2389
2390 /* DHT is connected, stop. */
2391 if (DHT_non_lan_connected(dht)) {
2392 free(dht->loaded_nodes_list);
2393 dht->loaded_nodes_list = NULL;
2394 dht->loaded_num_nodes = 0;
2395 return 0;
2396 }
2397
2388 unsigned int i; 2398 unsigned int i;
2389 2399
2390 for (i = 0; i < dht->loaded_num_nodes; ++i) { 2400 for (i = 0; i < dht->loaded_num_nodes && i < SAVE_BOOTSTAP_FREQUENCY; ++i) {
2391 DHT_bootstrap(dht, dht->loaded_nodes_list[i].ip_port, dht->loaded_nodes_list[i].public_key); 2401 unsigned int index = dht->loaded_nodes_index % dht->loaded_num_nodes;
2402 DHT_bootstrap(dht, dht->loaded_nodes_list[index].ip_port, dht->loaded_nodes_list[index].public_key);
2403 ++dht->loaded_nodes_index;
2392 } 2404 }
2393 2405
2394 // Loaded lists were allocd, free them
2395 free(dht->loaded_nodes_list);
2396 dht->loaded_nodes_list = NULL;
2397 dht->loaded_num_nodes = 0;
2398
2399 return 0; 2406 return 0;
2400} 2407}
2401 2408
@@ -2422,7 +2429,6 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2422 2429
2423 dht->loaded_num_nodes = num; 2430 dht->loaded_num_nodes = num;
2424 2431
2425 dht->has_loaded_friends_clients = 1;
2426 } /* localize declarations */ 2432 } /* localize declarations */
2427 2433
2428 break; 2434 break;
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 981728a1..c612c287 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -211,11 +211,9 @@ typedef struct {
211 DHT_Friend *friends_list; 211 DHT_Friend *friends_list;
212 uint16_t num_friends; 212 uint16_t num_friends;
213 213
214 // Used after loading of file (tox_load), but no longer needed after connect (tox_connect)
215 // Unsure if friends_list and num_friends could just be used instead?
216 int has_loaded_friends_clients; // Whether or not we have loaded on the first do_DHT
217 Node_format *loaded_nodes_list; 214 Node_format *loaded_nodes_list;
218 uint32_t loaded_num_nodes; 215 uint32_t loaded_num_nodes;
216 unsigned int loaded_nodes_index;
219 217
220 Shared_Keys shared_keys_recv; 218 Shared_Keys shared_keys_recv;
221 Shared_Keys shared_keys_sent; 219 Shared_Keys shared_keys_sent;