Save DHT friend nodes too.

This commit is contained in:
irungentoo 2015-06-07 20:30:36 -04:00
parent f4d0b85284
commit 14afb3b9b7
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98

View File

@ -2245,8 +2245,8 @@ DHT *new_DHT(Networking_Core *net)
void do_DHT(DHT *dht) void do_DHT(DHT *dht)
{ {
// Load friends/clients if first call to do_DHT // Load friends/clients if first call to do_DHT
if (dht->has_loaded_friends_clients == 0) { if (dht->has_loaded_friends_clients) {
dht->has_loaded_friends_clients = 1; dht->has_loaded_friends_clients = 0;
DHT_connect_after_load(dht); DHT_connect_after_load(dht);
} }
@ -2296,12 +2296,20 @@ void kill_DHT(DHT *dht)
/* Get the size of the DHT (for saving). */ /* Get the size of the DHT (for saving). */
uint32_t DHT_size(const DHT *dht) uint32_t DHT_size(const DHT *dht)
{ {
uint32_t num = 0, i; uint32_t num = 0, i, j;
for (i = 0; i < LCLIENT_LIST; ++i) { for (i = 0; i < LCLIENT_LIST; ++i) {
num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0);
} }
for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) {
DHT_Friend *fr = &dht->friends_list[i];
for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
num += (fr->client_list[j].assoc4.timestamp != 0) + (fr->client_list[j].assoc6.timestamp != 0);
}
}
uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
return size32 return size32
+ sizesubhead + sizeof(Node_format) * num; + sizesubhead + sizeof(Node_format) * num;
@ -2324,34 +2332,48 @@ void DHT_save(DHT *dht, uint8_t *data)
*(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL;
data += sizeof(uint32_t); data += sizeof(uint32_t);
uint32_t num = 0, i; uint32_t num, i, j;
//TODO: don't only save close nodes. uint8_t *old_data = data;
for (i = 0; i < LCLIENT_LIST; ++i) {
num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); /* get right offset. we write the actual header later. */
data = z_state_save_subheader(data, 0, 0);
Node_format *clients = (Node_format *)data;
for (num = 0, i = 0; i < LCLIENT_LIST; ++i) {
if (dht->close_clientlist[i].assoc4.timestamp != 0) {
memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES);
clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port;
++num;
}
if (dht->close_clientlist[i].assoc6.timestamp != 0) {
memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES);
clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port;
++num;
}
} }
len = num * sizeof(Node_format); for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) {
type = DHT_STATE_TYPE_NODES; DHT_Friend *fr = &dht->friends_list[i];
data = z_state_save_subheader(data, len, type);
if (num) { for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
Node_format *clients = (Node_format *)data; if (fr->client_list[j].assoc4.timestamp != 0) {
memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES);
for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { clients[num].ip_port = fr->client_list[j].assoc4.ip_port;
if (dht->close_clientlist[i].assoc4.timestamp != 0) {
memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES);
clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port;
++num; ++num;
} }
if (dht->close_clientlist[i].assoc6.timestamp != 0) { if (fr->client_list[j].assoc6.timestamp != 0) {
memcpy(&clients[num].public_key, &dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES);
clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port; clients[num].ip_port = fr->client_list[j].assoc6.ip_port;
++num; ++num;
} }
} }
} }
z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES);
} }
/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */