diff options
-rw-r--r-- | auto_tests/tox_test.c | 16 | ||||
-rw-r--r-- | toxcore/DHT.c | 149 | ||||
-rw-r--r-- | toxcore/DHT.h | 6 |
3 files changed, 59 insertions, 112 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index e7cc237a..189240c3 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c | |||
@@ -478,6 +478,12 @@ START_TEST(test_few_clients) | |||
478 | 478 | ||
479 | printf("tox clients messaging succeeded\n"); | 479 | printf("tox clients messaging succeeded\n"); |
480 | 480 | ||
481 | unsigned int save_size1 = tox_get_savedata_size(tox2); | ||
482 | ck_assert_msg(save_size1 != 0 && save_size1 < 4096, "save is invalid size %u", save_size1); | ||
483 | printf("%u\n", save_size1); | ||
484 | uint8_t save1[save_size1]; | ||
485 | tox_get_savedata(tox2, save1); | ||
486 | |||
481 | tox_callback_friend_name(tox3, print_nickchange, &to_compare); | 487 | tox_callback_friend_name(tox3, print_nickchange, &to_compare); |
482 | TOX_ERR_SET_INFO err_n; | 488 | TOX_ERR_SET_INFO err_n; |
483 | bool succ = tox_self_set_name(tox2, (uint8_t *)"Gentoo", sizeof("Gentoo"), &err_n); | 489 | bool succ = tox_self_set_name(tox2, (uint8_t *)"Gentoo", sizeof("Gentoo"), &err_n); |
@@ -798,10 +804,10 @@ START_TEST(test_many_clients) | |||
798 | unsigned int num_f = 0; | 804 | unsigned int num_f = 0; |
799 | 805 | ||
800 | for (i = 0; i < NUM_TOXES; ++i) { | 806 | for (i = 0; i < NUM_TOXES; ++i) { |
801 | num_f += tox_self_get_friend_list_size(); | 807 | num_f += tox_self_get_friend_list_size(toxes[i]); |
802 | } | 808 | } |
803 | 809 | ||
804 | ck_assert_msg(num_f == NUM_FRIENDS * 2, "bad num friends"); | 810 | ck_assert_msg(num_f == 0, "bad num friends: %u", num_f); |
805 | 811 | ||
806 | for (i = 0; i < NUM_FRIENDS; ++i) { | 812 | for (i = 0; i < NUM_FRIENDS; ++i) { |
807 | loop_top: | 813 | loop_top: |
@@ -825,6 +831,12 @@ loop_top: | |||
825 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); | 831 | ck_assert_msg(num != UINT32_MAX && test == TOX_ERR_FRIEND_ADD_OK, "Failed to add friend error code: %i", test); |
826 | } | 832 | } |
827 | 833 | ||
834 | for (i = 0; i < NUM_TOXES; ++i) { | ||
835 | num_f += tox_self_get_friend_list_size(toxes[i]); | ||
836 | } | ||
837 | |||
838 | ck_assert_msg(num_f == NUM_FRIENDS * 2, "bad num friends: %u", num_f); | ||
839 | |||
828 | while (1) { | 840 | while (1) { |
829 | uint16_t counter = 0; | 841 | uint16_t counter = 0; |
830 | 842 | ||
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 8cd8852c..dd7c0acd 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -2282,8 +2282,7 @@ void kill_DHT(DHT *dht) | |||
2282 | ping_array_free_all(&dht->dht_harden_ping_array); | 2282 | ping_array_free_all(&dht->dht_harden_ping_array); |
2283 | kill_ping(dht->ping); | 2283 | kill_ping(dht->ping); |
2284 | free(dht->friends_list); | 2284 | free(dht->friends_list); |
2285 | free(dht->loaded_friends_list); | 2285 | free(dht->loaded_nodes_list); |
2286 | free(dht->loaded_clients_list); | ||
2287 | free(dht); | 2286 | free(dht); |
2288 | } | 2287 | } |
2289 | 2288 | ||
@@ -2292,23 +2291,20 @@ void kill_DHT(DHT *dht) | |||
2292 | #define DHT_STATE_COOKIE_GLOBAL 0x159000d | 2291 | #define DHT_STATE_COOKIE_GLOBAL 0x159000d |
2293 | 2292 | ||
2294 | #define DHT_STATE_COOKIE_TYPE 0x11ce | 2293 | #define DHT_STATE_COOKIE_TYPE 0x11ce |
2295 | #define DHT_STATE_TYPE_FRIENDS_ASSOC46 3 | 2294 | #define DHT_STATE_TYPE_NODES 4 |
2296 | #define DHT_STATE_TYPE_CLIENTS_ASSOC46 4 | ||
2297 | 2295 | ||
2298 | /* Get the size of the DHT (for saving). */ | 2296 | /* Get the size of the DHT (for saving). */ |
2299 | uint32_t DHT_size(const DHT *dht) | 2297 | uint32_t DHT_size(const DHT *dht) |
2300 | { | 2298 | { |
2301 | uint32_t num = 0, i; | 2299 | uint32_t num = 0, i; |
2302 | 2300 | ||
2303 | for (i = 0; i < LCLIENT_LIST; ++i) | 2301 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2304 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | 2302 | num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); |
2305 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | 2303 | } |
2306 | num++; | ||
2307 | 2304 | ||
2308 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; | 2305 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; |
2309 | return size32 | 2306 | return size32 |
2310 | + sizesubhead + sizeof(DHT_Friend) * dht->num_friends | 2307 | + sizesubhead + sizeof(Node_format) * num; |
2311 | + sizesubhead + sizeof(Client_data) * num; | ||
2312 | } | 2308 | } |
2313 | 2309 | ||
2314 | static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) | 2310 | static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) |
@@ -2328,71 +2324,32 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
2328 | *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; | 2324 | *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; |
2329 | data += sizeof(uint32_t); | 2325 | data += sizeof(uint32_t); |
2330 | 2326 | ||
2331 | len = sizeof(DHT_Friend) * dht->num_friends; | ||
2332 | type = DHT_STATE_TYPE_FRIENDS_ASSOC46; | ||
2333 | data = z_state_save_subheader(data, len, type); | ||
2334 | memcpy(data, dht->friends_list, len); | ||
2335 | data += len; | ||
2336 | |||
2337 | uint32_t num = 0, i; | 2327 | uint32_t num = 0, i; |
2338 | 2328 | ||
2339 | for (i = 0; i < LCLIENT_LIST; ++i) | 2329 | //TODO: don't only save close nodes. |
2340 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | 2330 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2341 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | 2331 | num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); |
2342 | num++; | 2332 | } |
2343 | 2333 | ||
2344 | len = num * sizeof(Client_data); | 2334 | len = num * sizeof(Node_format); |
2345 | type = DHT_STATE_TYPE_CLIENTS_ASSOC46; | 2335 | type = DHT_STATE_TYPE_NODES; |
2346 | data = z_state_save_subheader(data, len, type); | 2336 | data = z_state_save_subheader(data, len, type); |
2347 | 2337 | ||
2348 | if (num) { | 2338 | if (num) { |
2349 | Client_data *clients = (Client_data *)data; | 2339 | Node_format *clients = (Node_format *)data; |
2350 | |||
2351 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) | ||
2352 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | ||
2353 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | ||
2354 | memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); | ||
2355 | } | ||
2356 | } | ||
2357 | |||
2358 | static void DHT_bootstrap_loaded_clients(DHT *dht) | ||
2359 | { | ||
2360 | if (!dht->loaded_clients_list) | ||
2361 | return; | ||
2362 | |||
2363 | uint32_t i; | ||
2364 | |||
2365 | Client_data *client_list = dht->loaded_clients_list; | ||
2366 | uint32_t client_count = dht->loaded_num_clients; | ||
2367 | |||
2368 | for (i = 0; i < client_count; ++i) { | ||
2369 | if (client_list[i].assoc4.timestamp != 0) | ||
2370 | DHT_bootstrap(dht, client_list[i].assoc4.ip_port, client_list[i].client_id); | ||
2371 | |||
2372 | if (client_list[i].assoc6.timestamp != 0) | ||
2373 | DHT_bootstrap(dht, client_list[i].assoc6.ip_port, client_list[i].client_id); | ||
2374 | } | ||
2375 | } | ||
2376 | |||
2377 | static void getnodes_of_loaded_friend_clients(DHT *dht) | ||
2378 | { | ||
2379 | if (!dht->loaded_friends_list) | ||
2380 | return; | ||
2381 | |||
2382 | uint32_t i, j; | ||
2383 | |||
2384 | DHT_Friend *friend_list = dht->loaded_friends_list; | ||
2385 | uint32_t friend_count = dht->loaded_num_friends; | ||
2386 | 2340 | ||
2387 | for (i = 0; i < friend_count; ++i) { | 2341 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { |
2388 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 2342 | if (dht->close_clientlist[i].assoc4.timestamp != 0) { |
2389 | Client_data *client = &friend_list[i].client_list[j]; | 2343 | memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); |
2390 | 2344 | clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port; | |
2391 | if (client->assoc4.timestamp != 0) | 2345 | ++num; |
2392 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id, NULL); | 2346 | } |
2393 | 2347 | ||
2394 | if (client->assoc6.timestamp != 0) | 2348 | if (dht->close_clientlist[i].assoc6.timestamp != 0) { |
2395 | getnodes(dht, client->assoc6.ip_port, client->client_id, friend_list[i].client_id, NULL); | 2349 | memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); |
2350 | clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port; | ||
2351 | ++num; | ||
2352 | } | ||
2396 | } | 2353 | } |
2397 | } | 2354 | } |
2398 | } | 2355 | } |
@@ -2403,17 +2360,19 @@ int DHT_connect_after_load(DHT *dht) | |||
2403 | if (dht == NULL) | 2360 | if (dht == NULL) |
2404 | return -1; | 2361 | return -1; |
2405 | 2362 | ||
2406 | getnodes_of_loaded_friend_clients(dht); | 2363 | if (!dht->loaded_nodes_list) |
2407 | DHT_bootstrap_loaded_clients(dht); | 2364 | return -1; |
2365 | |||
2366 | unsigned int i; | ||
2408 | 2367 | ||
2409 | // Loaded lists were allocd, free them | 2368 | for (i = 0; i < dht->loaded_num_nodes; ++i) { |
2410 | free(dht->loaded_friends_list); | 2369 | DHT_bootstrap(dht, dht->loaded_nodes_list[i].ip_port, dht->loaded_nodes_list[i].public_key); |
2411 | dht->loaded_friends_list = NULL; | 2370 | } |
2412 | dht->loaded_num_friends = 0; | ||
2413 | 2371 | ||
2414 | free(dht->loaded_clients_list); | 2372 | // Loaded lists were allocd, free them |
2415 | dht->loaded_clients_list = NULL; | 2373 | free(dht->loaded_nodes_list); |
2416 | dht->loaded_num_clients = 0; | 2374 | dht->loaded_nodes_list = NULL; |
2375 | dht->loaded_num_nodes = 0; | ||
2417 | 2376 | ||
2418 | return 0; | 2377 | return 0; |
2419 | } | 2378 | } |
@@ -2424,44 +2383,22 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2424 | uint32_t num, i; | 2383 | uint32_t num, i; |
2425 | 2384 | ||
2426 | switch (type) { | 2385 | switch (type) { |
2427 | case DHT_STATE_TYPE_FRIENDS_ASSOC46: | 2386 | case DHT_STATE_TYPE_NODES: |
2428 | if (length % sizeof(DHT_Friend) != 0) | 2387 | if ((length % sizeof(Node_format)) != 0) |
2429 | break; | ||
2430 | |||
2431 | { /* localize declarations */ | ||
2432 | DHT_Friend *friend_list = (DHT_Friend *)data; | ||
2433 | num = length / sizeof(DHT_Friend); | ||
2434 | |||
2435 | free(dht->loaded_friends_list); | ||
2436 | // Copy to loaded_friends_list | ||
2437 | dht->loaded_friends_list = calloc(num, sizeof(DHT_Friend)); | ||
2438 | |||
2439 | for (i = 0; i < num; i++) | ||
2440 | memcpy(&(dht->loaded_friends_list[i]), &(friend_list[i]), sizeof(DHT_Friend)); | ||
2441 | |||
2442 | dht->loaded_num_friends = num; | ||
2443 | |||
2444 | dht->has_loaded_friends_clients = 1; | ||
2445 | } /* localize declarations */ | ||
2446 | |||
2447 | break; | ||
2448 | |||
2449 | case DHT_STATE_TYPE_CLIENTS_ASSOC46: | ||
2450 | if ((length % sizeof(Client_data)) != 0) | ||
2451 | break; | 2388 | break; |
2452 | 2389 | ||
2453 | { /* localize declarations */ | 2390 | { /* localize declarations */ |
2454 | num = length / sizeof(Client_data); | 2391 | num = length / sizeof(Node_format); |
2455 | Client_data *client_list = (Client_data *)data; | 2392 | Node_format *client_list = (Node_format *)data; |
2456 | 2393 | ||
2457 | free(dht->loaded_clients_list); | 2394 | free(dht->loaded_nodes_list); |
2458 | // Copy to loaded_clients_list | 2395 | // Copy to loaded_clients_list |
2459 | dht->loaded_clients_list = calloc(num, sizeof(Client_data)); | 2396 | dht->loaded_nodes_list = calloc(num, sizeof(Node_format)); |
2460 | 2397 | ||
2461 | for (i = 0; i < num; i++) | 2398 | for (i = 0; i < num; i++) |
2462 | memcpy(&(dht->loaded_clients_list[i]), &(client_list[i]), sizeof(Client_data)); | 2399 | memcpy(&(dht->loaded_nodes_list[i]), &(client_list[i]), sizeof(Node_format)); |
2463 | 2400 | ||
2464 | dht->loaded_num_clients = num; | 2401 | dht->loaded_num_nodes = num; |
2465 | 2402 | ||
2466 | dht->has_loaded_friends_clients = 1; | 2403 | dht->has_loaded_friends_clients = 1; |
2467 | } /* localize declarations */ | 2404 | } /* localize declarations */ |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 318a6002..981728a1 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -214,10 +214,8 @@ typedef struct { | |||
214 | // Used after loading of file (tox_load), but no longer needed after connect (tox_connect) | 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? | 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 | 216 | int has_loaded_friends_clients; // Whether or not we have loaded on the first do_DHT |
217 | DHT_Friend *loaded_friends_list; | 217 | Node_format *loaded_nodes_list; |
218 | uint32_t loaded_num_friends; | 218 | uint32_t loaded_num_nodes; |
219 | Client_data *loaded_clients_list; | ||
220 | uint32_t loaded_num_clients; | ||
221 | 219 | ||
222 | Shared_Keys shared_keys_recv; | 220 | Shared_Keys shared_keys_recv; |
223 | Shared_Keys shared_keys_sent; | 221 | Shared_Keys shared_keys_sent; |