summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 0695fa59..1c6e767f 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2297,6 +2297,8 @@ static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t typ
2297 return data; 2297 return data;
2298} 2298}
2299 2299
2300#define MAX_SAVED_DHT_NODES ((DHT_FAKE_FRIEND_NUMBER * MAX_FRIEND_CLIENTS) + LCLIENT_LIST)
2301
2300/* Save the DHT in data where data is an array of size DHT_size(). */ 2302/* Save the DHT in data where data is an array of size DHT_size(). */
2301void DHT_save(DHT *dht, uint8_t *data) 2303void DHT_save(DHT *dht, uint8_t *data)
2302{ 2304{
@@ -2310,7 +2312,7 @@ void DHT_save(DHT *dht, uint8_t *data)
2310 /* get right offset. we write the actual header later. */ 2312 /* get right offset. we write the actual header later. */
2311 data = z_state_save_subheader(data, 0, 0); 2313 data = z_state_save_subheader(data, 0, 0);
2312 2314
2313 Node_format *clients = (Node_format *)data; 2315 Node_format clients[MAX_SAVED_DHT_NODES];
2314 2316
2315 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { 2317 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) {
2316 if (dht->close_clientlist[i].assoc4.timestamp != 0) { 2318 if (dht->close_clientlist[i].assoc4.timestamp != 0) {
@@ -2344,7 +2346,7 @@ void DHT_save(DHT *dht, uint8_t *data)
2344 } 2346 }
2345 } 2347 }
2346 2348
2347 z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES); 2349 z_state_save_subheader(old_data, pack_nodes(data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
2348} 2350}
2349 2351
2350/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */ 2352/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */
@@ -2381,25 +2383,27 @@ int DHT_connect_after_load(DHT *dht)
2381static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) 2383static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
2382{ 2384{
2383 DHT *dht = outer; 2385 DHT *dht = outer;
2384 uint32_t num, i; 2386 int i;
2385 2387
2386 switch (type) { 2388 switch (type) {
2387 case DHT_STATE_TYPE_NODES: 2389 case DHT_STATE_TYPE_NODES:
2388 if ((length % sizeof(Node_format)) != 0) 2390 if ((length % sizeof(Node_format)) != 0)
2389 break; 2391 break;
2390 2392
2391 { /* localize declarations */ 2393 {
2392 num = length / sizeof(Node_format);
2393 Node_format *client_list = (Node_format *)data;
2394
2395 free(dht->loaded_nodes_list); 2394 free(dht->loaded_nodes_list);
2396 // Copy to loaded_clients_list 2395 // Copy to loaded_clients_list
2397 dht->loaded_nodes_list = calloc(num, sizeof(Node_format)); 2396 dht->loaded_nodes_list = calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format));
2398 2397
2399 for (i = 0; i < num; i++) 2398 int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, NULL, data, length, 0);
2400 memcpy(&(dht->loaded_nodes_list[i]), &(client_list[i]), sizeof(Node_format));
2401 2399
2402 dht->loaded_num_nodes = num; 2400 Node_format *client_list = (Node_format *)data;
2401
2402 if (num > 0) {
2403 dht->loaded_num_nodes = num;
2404 } else {
2405 dht->loaded_num_nodes = 0;
2406 }
2403 2407
2404 } /* localize declarations */ 2408 } /* localize declarations */
2405 2409