diff options
author | irungentoo <irungentoo@gmail.com> | 2015-06-07 20:30:36 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-06-07 20:30:36 -0400 |
commit | 14afb3b9b75fe7639688711467caf6405330740f (patch) | |
tree | 53e6b758812f47e6574124460a8b6cdc2c7b068d /toxcore | |
parent | f4d0b852847b850deba45958e05a4b9212c91709 (diff) |
Save DHT friend nodes too.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 62 |
1 files changed, 42 insertions, 20 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index dd7c0acd..0590aec9 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -2245,8 +2245,8 @@ DHT *new_DHT(Networking_Core *net) | |||
2245 | void do_DHT(DHT *dht) | 2245 | void do_DHT(DHT *dht) |
2246 | { | 2246 | { |
2247 | // Load friends/clients if first call to do_DHT | 2247 | // Load friends/clients if first call to do_DHT |
2248 | if (dht->has_loaded_friends_clients == 0) { | 2248 | if (dht->has_loaded_friends_clients) { |
2249 | dht->has_loaded_friends_clients = 1; | 2249 | dht->has_loaded_friends_clients = 0; |
2250 | DHT_connect_after_load(dht); | 2250 | DHT_connect_after_load(dht); |
2251 | } | 2251 | } |
2252 | 2252 | ||
@@ -2296,12 +2296,20 @@ void kill_DHT(DHT *dht) | |||
2296 | /* Get the size of the DHT (for saving). */ | 2296 | /* Get the size of the DHT (for saving). */ |
2297 | uint32_t DHT_size(const DHT *dht) | 2297 | uint32_t DHT_size(const DHT *dht) |
2298 | { | 2298 | { |
2299 | uint32_t num = 0, i; | 2299 | uint32_t num = 0, i, j; |
2300 | 2300 | ||
2301 | for (i = 0; i < LCLIENT_LIST; ++i) { | 2301 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2302 | num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); | 2302 | num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); |
2303 | } | 2303 | } |
2304 | 2304 | ||
2305 | for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { | ||
2306 | DHT_Friend *fr = &dht->friends_list[i]; | ||
2307 | |||
2308 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | ||
2309 | num += (fr->client_list[j].assoc4.timestamp != 0) + (fr->client_list[j].assoc6.timestamp != 0); | ||
2310 | } | ||
2311 | } | ||
2312 | |||
2305 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; | 2313 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; |
2306 | return size32 | 2314 | return size32 |
2307 | + sizesubhead + sizeof(Node_format) * num; | 2315 | + sizesubhead + sizeof(Node_format) * num; |
@@ -2324,34 +2332,48 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
2324 | *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; | 2332 | *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; |
2325 | data += sizeof(uint32_t); | 2333 | data += sizeof(uint32_t); |
2326 | 2334 | ||
2327 | uint32_t num = 0, i; | 2335 | uint32_t num, i, j; |
2328 | 2336 | ||
2329 | //TODO: don't only save close nodes. | 2337 | uint8_t *old_data = data; |
2330 | for (i = 0; i < LCLIENT_LIST; ++i) { | 2338 | |
2331 | num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); | 2339 | /* get right offset. we write the actual header later. */ |
2332 | } | 2340 | data = z_state_save_subheader(data, 0, 0); |
2333 | 2341 | ||
2334 | len = num * sizeof(Node_format); | 2342 | Node_format *clients = (Node_format *)data; |
2335 | type = DHT_STATE_TYPE_NODES; | ||
2336 | data = z_state_save_subheader(data, len, type); | ||
2337 | 2343 | ||
2338 | if (num) { | 2344 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { |
2339 | Node_format *clients = (Node_format *)data; | 2345 | if (dht->close_clientlist[i].assoc4.timestamp != 0) { |
2346 | memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); | ||
2347 | clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port; | ||
2348 | ++num; | ||
2349 | } | ||
2350 | |||
2351 | if (dht->close_clientlist[i].assoc6.timestamp != 0) { | ||
2352 | memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); | ||
2353 | clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port; | ||
2354 | ++num; | ||
2355 | } | ||
2356 | } | ||
2340 | 2357 | ||
2341 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { | 2358 | for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { |
2342 | if (dht->close_clientlist[i].assoc4.timestamp != 0) { | 2359 | DHT_Friend *fr = &dht->friends_list[i]; |
2343 | memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); | 2360 | |
2344 | clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port; | 2361 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
2362 | if (fr->client_list[j].assoc4.timestamp != 0) { | ||
2363 | memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES); | ||
2364 | clients[num].ip_port = fr->client_list[j].assoc4.ip_port; | ||
2345 | ++num; | 2365 | ++num; |
2346 | } | 2366 | } |
2347 | 2367 | ||
2348 | if (dht->close_clientlist[i].assoc6.timestamp != 0) { | 2368 | if (fr->client_list[j].assoc6.timestamp != 0) { |
2349 | memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); | 2369 | memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES); |
2350 | clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port; | 2370 | clients[num].ip_port = fr->client_list[j].assoc6.ip_port; |
2351 | ++num; | 2371 | ++num; |
2352 | } | 2372 | } |
2353 | } | 2373 | } |
2354 | } | 2374 | } |
2375 | |||
2376 | z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES); | ||
2355 | } | 2377 | } |
2356 | 2378 | ||
2357 | /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ | 2379 | /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ |