diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 79 |
1 files changed, 62 insertions, 17 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 6bd23081..ac43371e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -2333,6 +2333,58 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
2333 | } | 2333 | } |
2334 | } | 2334 | } |
2335 | 2335 | ||
2336 | static void DHT_bootstrap_loaded_clients(DHT *dht) | ||
2337 | { | ||
2338 | uint32_t i; | ||
2339 | |||
2340 | Client_data *client_list = dht->loaded_clients_list; | ||
2341 | uint32_t client_count = dht->loaded_num_clients; | ||
2342 | |||
2343 | for (i = 0; i < client_count; ++i) { | ||
2344 | if (client_list[i].assoc4.timestamp != 0) | ||
2345 | DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); | ||
2346 | |||
2347 | if (client_list[i].assoc6.timestamp != 0) | ||
2348 | DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); | ||
2349 | } | ||
2350 | } | ||
2351 | |||
2352 | static void getnodes_of_loaded_friend_clients(DHT *dht) | ||
2353 | { | ||
2354 | uint32_t i, j; | ||
2355 | |||
2356 | DHT_Friend *friend_list = dht->loaded_friends_list; | ||
2357 | uint32_t friend_count = dht->loaded_num_friends; | ||
2358 | |||
2359 | for (i = 0; i < friend_count; ++i) { | ||
2360 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | ||
2361 | Client_data *client = &friend_list[i].client_list[j]; | ||
2362 | |||
2363 | if (client->assoc4.timestamp != 0) | ||
2364 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); | ||
2365 | |||
2366 | if (client->assoc6.timestamp != 0) | ||
2367 | getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); | ||
2368 | } | ||
2369 | } | ||
2370 | } | ||
2371 | |||
2372 | /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ | ||
2373 | int DHT_connect_after_load(DHT *dht) | ||
2374 | { | ||
2375 | if(dht == NULL || dht->loaded_friends_list == NULL || dht->loaded_clients_list == NULL) | ||
2376 | return -1; | ||
2377 | |||
2378 | getnodes_of_loaded_friend_clients(dht); | ||
2379 | DHT_bootstrap_loaded_clients(dht); | ||
2380 | |||
2381 | // Loaded lists were allocd, free them | ||
2382 | free(dht->loaded_friends_list); | ||
2383 | free(dht->loaded_clients_list); | ||
2384 | |||
2385 | return 0; | ||
2386 | } | ||
2387 | |||
2336 | static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) | 2388 | static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) |
2337 | { | 2389 | { |
2338 | DHT *dht = outer; | 2390 | DHT *dht = outer; |
@@ -2347,18 +2399,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2347 | DHT_Friend *friend_list = (DHT_Friend *)data; | 2399 | DHT_Friend *friend_list = (DHT_Friend *)data; |
2348 | num = length / sizeof(DHT_Friend); | 2400 | num = length / sizeof(DHT_Friend); |
2349 | 2401 | ||
2350 | for (i = 0; i < num; ++i) { | 2402 | // Copy to loaded_friends_list |
2351 | 2403 | dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend)); | |
2352 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 2404 | for(i = 0; i < num; i++) |
2353 | Client_data *client = &friend_list[i].client_list[j]; | 2405 | memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend)); |
2354 | 2406 | dht->loaded_num_friends = num; | |
2355 | if (client->assoc4.timestamp != 0) | ||
2356 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); | ||
2357 | 2407 | ||
2358 | if (client->assoc6.timestamp != 0) | ||
2359 | getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); | ||
2360 | } | ||
2361 | } | ||
2362 | } /* localize declarations */ | 2408 | } /* localize declarations */ |
2363 | 2409 | ||
2364 | break; | 2410 | break; |
@@ -2371,13 +2417,12 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2371 | num = length / sizeof(Client_data); | 2417 | num = length / sizeof(Client_data); |
2372 | Client_data *client_list = (Client_data *)data; | 2418 | Client_data *client_list = (Client_data *)data; |
2373 | 2419 | ||
2374 | for (i = 0; i < num; ++i) { | 2420 | // Copy to loaded_clients_list |
2375 | if (client_list[i].assoc4.timestamp != 0) | 2421 | dht->loaded_clients_list = calloc(num, sizeof(Client_data)); |
2376 | DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); | 2422 | for(i = 0; i < num; i++) |
2423 | memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data)); | ||
2424 | dht->loaded_num_clients = num; | ||
2377 | 2425 | ||
2378 | if (client_list[i].assoc6.timestamp != 0) | ||
2379 | DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); | ||
2380 | } | ||
2381 | } /* localize declarations */ | 2426 | } /* localize declarations */ |
2382 | 2427 | ||
2383 | break; | 2428 | break; |