summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2015-08-09 12:39:21 +0200
committermannol <eniz_vukovic@hotmail.com>2015-08-09 12:39:21 +0200
commitc641b0fceb9b1fe0ca7181af1ddbb65200c33599 (patch)
tree286a870e03e3b43878753eb57e2d354df7c53f99 /toxcore
parent0be0e88f3ec0cd81147a1418aa0afe61c24112b7 (diff)
parent2ab3b14731061cc04d3ccc50a35093c11d018298 (diff)
Updated with upstream
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c460
-rw-r--r--toxcore/DHT.h81
-rw-r--r--toxcore/Messenger.c122
-rw-r--r--toxcore/TCP_connection.c19
-rw-r--r--toxcore/TCP_connection.h12
-rw-r--r--toxcore/TCP_server.c6
-rw-r--r--toxcore/TCP_server.h4
-rw-r--r--toxcore/assoc.c36
-rw-r--r--toxcore/net_crypto.c17
-rw-r--r--toxcore/onion_client.c7
-rw-r--r--toxcore/ping.c60
-rw-r--r--toxcore/ping.h6
-rw-r--r--toxcore/tox.h4
-rw-r--r--toxcore/util.c21
-rw-r--r--toxcore/util.h1
15 files changed, 457 insertions, 399 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 2236fe61..ae823302 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1,6 +1,6 @@
1/* DHT.c 1/* DHT.c
2 * 2 *
3 * An implementation of the DHT as seen in http://wiki.tox.im/index.php/DHT 3 * An implementation of the DHT as seen in docs/updates/DHT.md
4 * 4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 * 6 *
@@ -65,21 +65,21 @@
65/* Number of get node requests to send to quickly find close nodes. */ 65/* Number of get node requests to send to quickly find close nodes. */
66#define MAX_BOOTSTRAP_TIMES 10 66#define MAX_BOOTSTRAP_TIMES 10
67 67
68/* Compares client_id1 and client_id2 with client_id. 68/* Compares pk1 and pk2 with pk.
69 * 69 *
70 * return 0 if both are same distance. 70 * return 0 if both are same distance.
71 * return 1 if client_id1 is closer. 71 * return 1 if pk1 is closer.
72 * return 2 if client_id2 is closer. 72 * return 2 if pk2 is closer.
73 */ 73 */
74int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2) 74int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2)
75{ 75{
76 size_t i; 76 size_t i;
77 uint8_t distance1, distance2; 77 uint8_t distance1, distance2;
78 78
79 for (i = 0; i < CLIENT_ID_SIZE; ++i) { 79 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
80 80
81 distance1 = id[i] ^ id1[i]; 81 distance1 = pk[i] ^ pk1[i];
82 distance2 = id[i] ^ id2[i]; 82 distance2 = pk[i] ^ pk2[i];
83 83
84 if (distance1 < distance2) 84 if (distance1 < distance2)
85 return 1; 85 return 1;
@@ -97,15 +97,15 @@ int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2)
97 * If shared key is already in shared_keys, copy it to shared_key. 97 * If shared key is already in shared_keys, copy it to shared_key.
98 * else generate it into shared_key and copy it to shared_keys 98 * else generate it into shared_key and copy it to shared_keys
99 */ 99 */
100void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *client_id) 100void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *public_key)
101{ 101{
102 uint32_t i, num = ~0, curr = 0; 102 uint32_t i, num = ~0, curr = 0;
103 103
104 for (i = 0; i < MAX_KEYS_PER_SLOT; ++i) { 104 for (i = 0; i < MAX_KEYS_PER_SLOT; ++i) {
105 int index = client_id[30] * MAX_KEYS_PER_SLOT + i; 105 int index = public_key[30] * MAX_KEYS_PER_SLOT + i;
106 106
107 if (shared_keys->keys[index].stored) { 107 if (shared_keys->keys[index].stored) {
108 if (memcmp(client_id, shared_keys->keys[index].client_id, CLIENT_ID_SIZE) == 0) { 108 if (memcmp(public_key, shared_keys->keys[index].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
109 memcpy(shared_key, shared_keys->keys[index].shared_key, crypto_box_BEFORENMBYTES); 109 memcpy(shared_key, shared_keys->keys[index].shared_key, crypto_box_BEFORENMBYTES);
110 ++shared_keys->keys[index].times_requested; 110 ++shared_keys->keys[index].times_requested;
111 shared_keys->keys[index].time_last_requested = unix_time(); 111 shared_keys->keys[index].time_last_requested = unix_time();
@@ -129,31 +129,31 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t
129 } 129 }
130 } 130 }
131 131
132 encrypt_precompute(client_id, secret_key, shared_key); 132 encrypt_precompute(public_key, secret_key, shared_key);
133 133
134 if (num != (uint32_t)~0) { 134 if (num != (uint32_t)~0) {
135 shared_keys->keys[curr].stored = 1; 135 shared_keys->keys[curr].stored = 1;
136 shared_keys->keys[curr].times_requested = 1; 136 shared_keys->keys[curr].times_requested = 1;
137 memcpy(shared_keys->keys[curr].client_id, client_id, CLIENT_ID_SIZE); 137 memcpy(shared_keys->keys[curr].public_key, public_key, crypto_box_PUBLICKEYBYTES);
138 memcpy(shared_keys->keys[curr].shared_key, shared_key, crypto_box_BEFORENMBYTES); 138 memcpy(shared_keys->keys[curr].shared_key, shared_key, crypto_box_BEFORENMBYTES);
139 shared_keys->keys[curr].time_last_requested = unix_time(); 139 shared_keys->keys[curr].time_last_requested = unix_time();
140 } 140 }
141} 141}
142 142
143/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key 143/* Copy shared_key to encrypt/decrypt DHT packet from public_key into shared_key
144 * for packets that we receive. 144 * for packets that we receive.
145 */ 145 */
146void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *client_id) 146void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *public_key)
147{ 147{
148 get_shared_key(&dht->shared_keys_recv, shared_key, dht->self_secret_key, client_id); 148 get_shared_key(&dht->shared_keys_recv, shared_key, dht->self_secret_key, public_key);
149} 149}
150 150
151/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key 151/* Copy shared_key to encrypt/decrypt DHT packet from public_key into shared_key
152 * for packets that we send. 152 * for packets that we send.
153 */ 153 */
154void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *client_id) 154void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *public_key)
155{ 155{
156 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, client_id); 156 get_shared_key(&dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key);
157} 157}
158 158
159void to_net_family(IP *ip) 159void to_net_family(IP *ip)
@@ -177,6 +177,28 @@ int to_host_family(IP *ip)
177 } 177 }
178} 178}
179 179
180#define PACKED_NODE_SIZE_IP4 (1 + SIZE_IP4 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES)
181#define PACKED_NODE_SIZE_IP6 (1 + SIZE_IP6 + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES)
182
183/* Return packet size of packed node with ip_family on success.
184 * Return -1 on failure.
185 */
186int packed_node_size(uint8_t ip_family)
187{
188 if (ip_family == AF_INET) {
189 return PACKED_NODE_SIZE_IP4;
190 } else if (ip_family == TCP_INET) {
191 return PACKED_NODE_SIZE_IP4;
192 } else if (ip_family == AF_INET6) {
193 return PACKED_NODE_SIZE_IP6;
194 } else if (ip_family == TCP_INET6) {
195 return PACKED_NODE_SIZE_IP6;
196 } else {
197 return -1;
198 }
199}
200
201
180/* Pack number of nodes into data of maxlength length. 202/* Pack number of nodes into data of maxlength length.
181 * 203 *
182 * return length of packed nodes on success. 204 * return length of packed nodes on success.
@@ -207,26 +229,26 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_
207 } 229 }
208 230
209 if (ipv6 == 0) { 231 if (ipv6 == 0) {
210 uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES; 232 uint32_t size = PACKED_NODE_SIZE_IP4;
211 233
212 if (packed_length + size > length) 234 if (packed_length + size > length)
213 return -1; 235 return -1;
214 236
215 data[packed_length] = net_family; 237 data[packed_length] = net_family;
216 memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, sizeof(IP4)); 238 memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, SIZE_IP4);
217 memcpy(data + packed_length + 1 + sizeof(IP4), &nodes[i].ip_port.port, sizeof(uint16_t)); 239 memcpy(data + packed_length + 1 + SIZE_IP4, &nodes[i].ip_port.port, sizeof(uint16_t));
218 memcpy(data + packed_length + 1 + sizeof(IP4) + sizeof(uint16_t), nodes[i].public_key, crypto_box_PUBLICKEYBYTES); 240 memcpy(data + packed_length + 1 + SIZE_IP4 + sizeof(uint16_t), nodes[i].public_key, crypto_box_PUBLICKEYBYTES);
219 packed_length += size; 241 packed_length += size;
220 } else if (ipv6 == 1) { 242 } else if (ipv6 == 1) {
221 uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES; 243 uint32_t size = PACKED_NODE_SIZE_IP6;
222 244
223 if (packed_length + size > length) 245 if (packed_length + size > length)
224 return -1; 246 return -1;
225 247
226 data[packed_length] = net_family; 248 data[packed_length] = net_family;
227 memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, sizeof(IP6)); 249 memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, SIZE_IP6);
228 memcpy(data + packed_length + 1 + sizeof(IP6), &nodes[i].ip_port.port, sizeof(uint16_t)); 250 memcpy(data + packed_length + 1 + SIZE_IP6, &nodes[i].ip_port.port, sizeof(uint16_t));
229 memcpy(data + packed_length + 1 + sizeof(IP6) + sizeof(uint16_t), nodes[i].public_key, crypto_box_PUBLICKEYBYTES); 251 memcpy(data + packed_length + 1 + SIZE_IP6 + sizeof(uint16_t), nodes[i].public_key, crypto_box_PUBLICKEYBYTES);
230 packed_length += size; 252 packed_length += size;
231 } else { 253 } else {
232 return -1; 254 return -1;
@@ -275,27 +297,27 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
275 } 297 }
276 298
277 if (ipv6 == 0) { 299 if (ipv6 == 0) {
278 uint32_t size = 1 + sizeof(IP4) + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES; 300 uint32_t size = PACKED_NODE_SIZE_IP4;
279 301
280 if (len_processed + size > length) 302 if (len_processed + size > length)
281 return -1; 303 return -1;
282 304
283 nodes[num].ip_port.ip.family = host_family; 305 nodes[num].ip_port.ip.family = host_family;
284 memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, sizeof(IP4)); 306 memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, SIZE_IP4);
285 memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP4), sizeof(uint16_t)); 307 memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + SIZE_IP4, sizeof(uint16_t));
286 memcpy(nodes[num].public_key, data + len_processed + 1 + sizeof(IP4) + sizeof(uint16_t), crypto_box_PUBLICKEYBYTES); 308 memcpy(nodes[num].public_key, data + len_processed + 1 + SIZE_IP4 + sizeof(uint16_t), crypto_box_PUBLICKEYBYTES);
287 len_processed += size; 309 len_processed += size;
288 ++num; 310 ++num;
289 } else if (ipv6 == 1) { 311 } else if (ipv6 == 1) {
290 uint32_t size = 1 + sizeof(IP6) + sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES; 312 uint32_t size = PACKED_NODE_SIZE_IP6;
291 313
292 if (len_processed + size > length) 314 if (len_processed + size > length)
293 return -1; 315 return -1;
294 316
295 nodes[num].ip_port.ip.family = host_family; 317 nodes[num].ip_port.ip.family = host_family;
296 memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, sizeof(IP6)); 318 memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, SIZE_IP6);
297 memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + sizeof(IP6), sizeof(uint16_t)); 319 memcpy(&nodes[num].ip_port.port, data + len_processed + 1 + SIZE_IP6, sizeof(uint16_t));
298 memcpy(nodes[num].public_key, data + len_processed + 1 + sizeof(IP6) + sizeof(uint16_t), crypto_box_PUBLICKEYBYTES); 320 memcpy(nodes[num].public_key, data + len_processed + 1 + SIZE_IP6 + sizeof(uint16_t), crypto_box_PUBLICKEYBYTES);
299 len_processed += size; 321 len_processed += size;
300 ++num; 322 ++num;
301 } else { 323 } else {
@@ -311,21 +333,21 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
311 333
312 334
313 335
314/* Check if client with client_id is already in list of length length. 336/* Check if client with public_key is already in list of length length.
315 * If it is then set its corresponding timestamp to current time. 337 * If it is then set its corresponding timestamp to current time.
316 * If the id is already in the list with a different ip_port, update it. 338 * If the id is already in the list with a different ip_port, update it.
317 * TODO: Maybe optimize this. 339 * TODO: Maybe optimize this.
318 * 340 *
319 * return True(1) or False(0) 341 * return True(1) or False(0)
320 */ 342 */
321static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const uint8_t *client_id, IP_Port ip_port) 343static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port)
322{ 344{
323 uint32_t i; 345 uint32_t i;
324 uint64_t temp_time = unix_time(); 346 uint64_t temp_time = unix_time();
325 347
326 /* if client_id is in list, find it and maybe overwrite ip_port */ 348 /* if public_key is in list, find it and maybe overwrite ip_port */
327 for (i = 0; i < length; ++i) 349 for (i = 0; i < length; ++i)
328 if (id_equal(list[i].client_id, client_id)) { 350 if (id_equal(list[i].public_key, public_key)) {
329 /* Refresh the client timestamp. */ 351 /* Refresh the client timestamp. */
330 if (ip_port.ip.family == AF_INET) { 352 if (ip_port.ip.family == AF_INET) {
331 353
@@ -360,18 +382,18 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
360 return 1; 382 return 1;
361 } 383 }
362 384
363 /* client_id not in list yet: see if we can find an identical ip_port, in 385 /* public_key not in list yet: see if we can find an identical ip_port, in
364 * that case we kill the old client_id by overwriting it with the new one 386 * that case we kill the old public_key by overwriting it with the new one
365 * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list 387 * TODO: maybe we SHOULDN'T do that if that public_key is in a friend_list
366 * and the one who is the actual friend's client_id/address set? */ 388 * and the one who is the actual friend's public_key/address set? */
367 for (i = 0; i < length; ++i) { 389 for (i = 0; i < length; ++i) {
368 /* MAYBE: check the other address, if valid, don't nuke? */ 390 /* MAYBE: check the other address, if valid, don't nuke? */
369 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 391 if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
370 /* Initialize client timestamp. */ 392 /* Initialize client timestamp. */
371 list[i].assoc4.timestamp = temp_time; 393 list[i].assoc4.timestamp = temp_time;
372 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 394 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
373 395
374 LOGGER_DEBUG("coipil[%u]: switching client_id (ipv4)", i); 396 LOGGER_DEBUG("coipil[%u]: switching public_key (ipv4)", i);
375 397
376 /* kill the other address, if it was set */ 398 /* kill the other address, if it was set */
377 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); 399 memset(&list[i].assoc6, 0, sizeof(list[i].assoc6));
@@ -379,9 +401,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
379 } else if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { 401 } else if ((ip_port.ip.family == AF_INET6) && ipport_equal(&list[i].assoc6.ip_port, &ip_port)) {
380 /* Initialize client timestamp. */ 402 /* Initialize client timestamp. */
381 list[i].assoc6.timestamp = temp_time; 403 list[i].assoc6.timestamp = temp_time;
382 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 404 memcpy(list[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
383 405
384 LOGGER_DEBUG("coipil[%u]: switching client_id (ipv6)", i); 406 LOGGER_DEBUG("coipil[%u]: switching public_key (ipv6)", i);
385 407
386 /* kill the other address, if it was set */ 408 /* kill the other address, if it was set */
387 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); 409 memset(&list[i].assoc4, 0, sizeof(list[i].assoc4));
@@ -392,7 +414,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
392 return 0; 414 return 0;
393} 415}
394 416
395/* Check if client with client_id is already in node format list of length length. 417/* Check if client with public_key is already in node format list of length length.
396 * 418 *
397 * return 1 if true. 419 * return 1 if true.
398 * return 0 if false. 420 * return 0 if false.
@@ -409,15 +431,15 @@ static int client_in_nodelist(const Node_format *list, uint16_t length, const ui
409 return 0; 431 return 0;
410} 432}
411 433
412/* return friend number from the client_id. 434/* return friend number from the public_key.
413 * return -1 if a failure occurs. 435 * return -1 if a failure occurs.
414 */ 436 */
415static int friend_number(const DHT *dht, const uint8_t *client_id) 437static int friend_number(const DHT *dht, const uint8_t *public_key)
416{ 438{
417 uint32_t i; 439 uint32_t i;
418 440
419 for (i = 0; i < dht->num_friends; ++i) { 441 for (i = 0; i < dht->num_friends; ++i) {
420 if (id_equal(dht->friends_list[i].client_id, client_id)) 442 if (id_equal(dht->friends_list[i].public_key, public_key))
421 return i; 443 return i;
422 } 444 }
423 445
@@ -439,7 +461,7 @@ static uint8_t hardening_correct(const Hardening *h)
439/* 461/*
440 * helper for get_close_nodes(). argument list is a monster :D 462 * helper for get_close_nodes(). argument list is a monster :D
441 */ 463 */
442static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_list, 464static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_list,
443 sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length, 465 sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length,
444 uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) 466 uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good)
445{ 467{
@@ -454,7 +476,7 @@ static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_l
454 const Client_data *client = &client_list[i]; 476 const Client_data *client = &client_list[i];
455 477
456 /* node already in list? */ 478 /* node already in list? */
457 if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->client_id)) 479 if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->public_key))
458 continue; 480 continue;
459 481
460 const IPPTsPng *ipptp = NULL; 482 const IPPTsPng *ipptp = NULL;
@@ -480,30 +502,30 @@ static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_l
480 continue; 502 continue;
481 503
482 if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK 504 if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK
483 && !id_equal(client_id, client->client_id)) 505 && !id_equal(public_key, client->public_key))
484 continue; 506 continue;
485 507
486 if (num_nodes < MAX_SENT_NODES) { 508 if (num_nodes < MAX_SENT_NODES) {
487 memcpy(nodes_list[num_nodes].public_key, 509 memcpy(nodes_list[num_nodes].public_key,
488 client->client_id, 510 client->public_key,
489 crypto_box_PUBLICKEYBYTES ); 511 crypto_box_PUBLICKEYBYTES );
490 512
491 nodes_list[num_nodes].ip_port = ipptp->ip_port; 513 nodes_list[num_nodes].ip_port = ipptp->ip_port;
492 num_nodes++; 514 num_nodes++;
493 } else { 515 } else {
494 /* see if node_list contains a client_id that's "further away" 516 /* see if node_list contains a public_key that's "further away"
495 * compared to the one we're looking at at the moment, if there 517 * compared to the one we're looking at at the moment, if there
496 * is, replace it 518 * is, replace it
497 */ 519 */
498 for (j = 0; j < MAX_SENT_NODES; ++j) { 520 for (j = 0; j < MAX_SENT_NODES; ++j) {
499 closest = id_closest( client_id, 521 closest = id_closest( public_key,
500 nodes_list[j].public_key, 522 nodes_list[j].public_key,
501 client->client_id ); 523 client->public_key );
502 524
503 /* second client_id is closer than current: change to it */ 525 /* second public_key is closer than current: change to it */
504 if (closest == 2) { 526 if (closest == 2) {
505 memcpy( nodes_list[j].public_key, 527 memcpy( nodes_list[j].public_key,
506 client->client_id, 528 client->public_key,
507 crypto_box_PUBLICKEYBYTES); 529 crypto_box_PUBLICKEYBYTES);
508 530
509 nodes_list[j].ip_port = ipptp->ip_port; 531 nodes_list[j].ip_port = ipptp->ip_port;
@@ -516,7 +538,7 @@ static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_l
516 *num_nodes_ptr = num_nodes; 538 *num_nodes_ptr = num_nodes;
517} 539}
518 540
519/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: 541/* Find MAX_SENT_NODES nodes closest to the public_key for the send nodes request:
520 * put them in the nodes_list and return how many were found. 542 * put them in the nodes_list and return how many were found.
521 * 543 *
522 * TODO: For the love of based <your favorite deity, in doubt use "love"> make 544 * TODO: For the love of based <your favorite deity, in doubt use "love"> make
@@ -524,28 +546,28 @@ static void get_close_nodes_inner(const uint8_t *client_id, Node_format *nodes_l
524 * 546 *
525 * want_good : do we want only good nodes as checked with the hardening returned or not? 547 * want_good : do we want only good nodes as checked with the hardening returned or not?
526 */ 548 */
527static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, 549static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
528 sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good) 550 sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good)
529{ 551{
530 uint32_t num_nodes = 0, i; 552 uint32_t num_nodes = 0, i;
531 get_close_nodes_inner(client_id, nodes_list, sa_family, 553 get_close_nodes_inner(public_key, nodes_list, sa_family,
532 dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN, want_good); 554 dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN, want_good);
533 555
534 /*TODO uncomment this when hardening is added to close friend clients 556 /*TODO uncomment this when hardening is added to close friend clients
535 for (i = 0; i < dht->num_friends; ++i) 557 for (i = 0; i < dht->num_friends; ++i)
536 get_close_nodes_inner(dht, client_id, nodes_list, sa_family, 558 get_close_nodes_inner(dht, public_key, nodes_list, sa_family,
537 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, 559 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
538 &num_nodes, is_LAN, want_good); 560 &num_nodes, is_LAN, want_good);
539 */ 561 */
540 for (i = 0; i < dht->num_friends; ++i) 562 for (i = 0; i < dht->num_friends; ++i)
541 get_close_nodes_inner(client_id, nodes_list, sa_family, 563 get_close_nodes_inner(public_key, nodes_list, sa_family,
542 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, 564 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
543 &num_nodes, is_LAN, 0); 565 &num_nodes, is_LAN, 0);
544 566
545 return num_nodes; 567 return num_nodes;
546} 568}
547 569
548int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, 570int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, sa_family_t sa_family,
549 uint8_t is_LAN, uint8_t want_good) 571 uint8_t is_LAN, uint8_t want_good)
550{ 572{
551 memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); 573 memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
@@ -553,7 +575,7 @@ int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes
553 575
554 if (!dht->assoc) 576 if (!dht->assoc)
555#endif 577#endif
556 return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); 578 return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good);
557 579
558#ifdef ENABLE_ASSOC_DHT 580#ifdef ENABLE_ASSOC_DHT
559 //TODO: assoc, sa_family 0 (don't care if ipv4 or ipv6) support. 581 //TODO: assoc, sa_family 0 (don't care if ipv4 or ipv6) support.
@@ -564,14 +586,14 @@ int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes
564 request.count = MAX_SENT_NODES; 586 request.count = MAX_SENT_NODES;
565 request.count_good = MAX_SENT_NODES - 2; /* allow 2 'indirect' nodes */ 587 request.count_good = MAX_SENT_NODES - 2; /* allow 2 'indirect' nodes */
566 request.result = result; 588 request.result = result;
567 request.wanted_id = client_id; 589 request.wanted_id = public_key;
568 request.flags = (is_LAN ? LANOk : 0) + (sa_family == AF_INET ? ProtoIPv4 : ProtoIPv6); 590 request.flags = (is_LAN ? LANOk : 0) + (sa_family == AF_INET ? ProtoIPv4 : ProtoIPv6);
569 591
570 uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); 592 uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request);
571 593
572 if (!num_found) { 594 if (!num_found) {
573 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned zero nodes"); 595 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned zero nodes");
574 return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); 596 return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good);
575 } 597 }
576 598
577 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes", 599 LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes",
@@ -583,7 +605,7 @@ int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes
583 Client_data *client = result[i]; 605 Client_data *client = result[i];
584 606
585 if (client) { 607 if (client) {
586 id_copy(nodes_list[num_returned].public_key, client->client_id); 608 id_copy(nodes_list[num_returned].public_key, client->public_key);
587 609
588 if (sa_family == AF_INET) 610 if (sa_family == AF_INET)
589 if (ipport_isset(&client->assoc4.ip_port)) { 611 if (ipport_isset(&client->assoc4.ip_port)) {
@@ -636,7 +658,7 @@ static int cmp_dht_entry(const void *a, const void *b)
636 return 1; 658 return 1;
637 } 659 }
638 660
639 int close = id_closest(cmp_public_key, entry1.client_id, entry2.client_id); 661 int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key);
640 662
641 if (close == 1) 663 if (close == 1)
642 return 1; 664 return 1;
@@ -647,15 +669,15 @@ static int cmp_dht_entry(const void *a, const void *b)
647 return 0; 669 return 0;
648} 670}
649 671
650/* Is it ok to store node with client_id in client. 672/* Is it ok to store node with public_key in client.
651 * 673 *
652 * return 0 if node can't be stored. 674 * return 0 if node can't be stored.
653 * return 1 if it can. 675 * return 1 if it can.
654 */ 676 */
655static unsigned int store_node_ok(const Client_data *client, const uint8_t *client_id, const uint8_t *comp_client_id) 677static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key)
656{ 678{
657 if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 679 if ((is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT))
658 || (id_closest(comp_client_id, client->client_id, client_id) == 2)) { 680 || (id_closest(comp_public_key, client->public_key, public_key) == 2)) {
659 return 1; 681 return 1;
660 } else { 682 } else {
661 return 0; 683 return 0;
@@ -665,31 +687,31 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *clie
665/* Replace a first bad (or empty) node with this one 687/* Replace a first bad (or empty) node with this one
666 * or replace a possibly bad node (tests failed or not done yet) 688 * or replace a possibly bad node (tests failed or not done yet)
667 * that is further than any other in the list 689 * that is further than any other in the list
668 * from the comp_client_id 690 * from the comp_public_key
669 * or replace a good node that is further 691 * or replace a good node that is further
670 * than any other in the list from the comp_client_id 692 * than any other in the list from the comp_public_key
671 * and further than client_id. 693 * and further than public_key.
672 * 694 *
673 * Do not replace any node if the list has no bad or possibly bad nodes 695 * Do not replace any node if the list has no bad or possibly bad nodes
674 * and all nodes in the list are closer to comp_client_id 696 * and all nodes in the list are closer to comp_public_key
675 * than client_id. 697 * than public_key.
676 * 698 *
677 * returns True(1) when the item was stored, False(0) otherwise */ 699 * returns True(1) when the item was stored, False(0) otherwise */
678static int replace_all( Client_data *list, 700static int replace_all( Client_data *list,
679 uint16_t length, 701 uint16_t length,
680 const uint8_t *client_id, 702 const uint8_t *public_key,
681 IP_Port ip_port, 703 IP_Port ip_port,
682 const uint8_t *comp_client_id ) 704 const uint8_t *comp_public_key )
683{ 705{
684 if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) 706 if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6))
685 return 0; 707 return 0;
686 708
687 memcpy(cmp_public_key, comp_client_id, crypto_box_PUBLICKEYBYTES); 709 memcpy(cmp_public_key, comp_public_key, crypto_box_PUBLICKEYBYTES);
688 qsort(list, length, sizeof(Client_data), cmp_dht_entry); 710 qsort(list, length, sizeof(Client_data), cmp_dht_entry);
689 711
690 Client_data *client = &list[0]; 712 Client_data *client = &list[0];
691 713
692 if (store_node_ok(client, client_id, comp_client_id)) { 714 if (store_node_ok(client, public_key, comp_public_key)) {
693 IPPTsPng *ipptp_write = NULL; 715 IPPTsPng *ipptp_write = NULL;
694 IPPTsPng *ipptp_clear = NULL; 716 IPPTsPng *ipptp_clear = NULL;
695 717
@@ -701,7 +723,7 @@ static int replace_all( Client_data *list,
701 ipptp_clear = &client->assoc4; 723 ipptp_clear = &client->assoc4;
702 } 724 }
703 725
704 id_copy(client->client_id, client_id); 726 id_copy(client->public_key, public_key);
705 ipptp_write->ip_port = ip_port; 727 ipptp_write->ip_port = ip_port;
706 ipptp_write->timestamp = unix_time(); 728 ipptp_write->timestamp = unix_time();
707 729
@@ -718,22 +740,22 @@ static int replace_all( Client_data *list,
718 return 0; 740 return 0;
719} 741}
720 742
721/* Check if the node obtained with a get_nodes with client_id should be pinged. 743/* Check if the node obtained with a get_nodes with public_key should be pinged.
722 * NOTE: for best results call it after addto_lists; 744 * NOTE: for best results call it after addto_lists;
723 * 745 *
724 * return 0 if the node should not be pinged. 746 * return 0 if the node should not be pinged.
725 * return 1 if it should. 747 * return 1 if it should.
726 */ 748 */
727static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *client_id) 749static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key)
728{ 750{
729 if (store_node_ok(&dht->close_clientlist[0], client_id, dht->self_public_key)) { 751 if (store_node_ok(&dht->close_clientlist[0], public_key, dht->self_public_key)) {
730 return 1; 752 return 1;
731 } 753 }
732 754
733 unsigned int i; 755 unsigned int i;
734 756
735 for (i = 0; i < dht->num_friends; ++i) { 757 for (i = 0; i < dht->num_friends; ++i) {
736 if (store_node_ok(&dht->friends_list[i].client_list[0], client_id, dht->self_public_key)) { 758 if (store_node_ok(&dht->friends_list[i].client_list[0], public_key, dht->self_public_key)) {
737 return 1; 759 return 1;
738 } 760 }
739 } 761 }
@@ -741,12 +763,12 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *client_i
741 return 0; 763 return 0;
742} 764}
743 765
744/* Attempt to add client with ip_port and client_id to the friends client list 766/* Attempt to add client with ip_port and public_key to the friends client list
745 * and close_clientlist. 767 * and close_clientlist.
746 * 768 *
747 * returns 1+ if the item is used in any list, 0 else 769 * returns 1+ if the item is used in any list, 0 else
748 */ 770 */
749int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id) 771int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
750{ 772{
751 uint32_t i, used = 0; 773 uint32_t i, used = 0;
752 774
@@ -759,8 +781,8 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id)
759 /* NOTE: Current behavior if there are two clients with the same id is 781 /* NOTE: Current behavior if there are two clients with the same id is
760 * to replace the first ip by the second. 782 * to replace the first ip by the second.
761 */ 783 */
762 if (!client_or_ip_port_in_list(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) { 784 if (!client_or_ip_port_in_list(dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) {
763 if (replace_all(dht->close_clientlist, LCLIENT_LIST, client_id, ip_port, dht->self_public_key)) 785 if (replace_all(dht->close_clientlist, LCLIENT_LIST, public_key, ip_port, dht->self_public_key))
764 used++; 786 used++;
765 } else 787 } else
766 used++; 788 used++;
@@ -769,13 +791,13 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id)
769 791
770 for (i = 0; i < dht->num_friends; ++i) { 792 for (i = 0; i < dht->num_friends; ++i) {
771 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list, 793 if (!client_or_ip_port_in_list(dht->friends_list[i].client_list,
772 MAX_FRIEND_CLIENTS, client_id, ip_port)) { 794 MAX_FRIEND_CLIENTS, public_key, ip_port)) {
773 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, 795 if (replace_all(dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
774 client_id, ip_port, dht->friends_list[i].client_id)) { 796 public_key, ip_port, dht->friends_list[i].public_key)) {
775 797
776 DHT_Friend *friend = &dht->friends_list[i]; 798 DHT_Friend *friend = &dht->friends_list[i];
777 799
778 if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) { 800 if (memcmp(public_key, friend->public_key, crypto_box_PUBLICKEYBYTES) == 0) {
779 friend_foundip = friend; 801 friend_foundip = friend;
780 } 802 }
781 803
@@ -784,7 +806,7 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id)
784 } else { 806 } else {
785 DHT_Friend *friend = &dht->friends_list[i]; 807 DHT_Friend *friend = &dht->friends_list[i];
786 808
787 if (memcmp(client_id, friend->client_id, CLIENT_ID_SIZE) == 0) { 809 if (memcmp(public_key, friend->public_key, crypto_box_PUBLICKEYBYTES) == 0) {
788 friend_foundip = friend; 810 friend_foundip = friend;
789 } 811 }
790 812
@@ -810,17 +832,17 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id)
810 ippts.ip_port = ip_port; 832 ippts.ip_port = ip_port;
811 ippts.timestamp = unix_time(); 833 ippts.timestamp = unix_time();
812 834
813 Assoc_add_entry(dht->assoc, client_id, &ippts, NULL, used ? 1 : 0); 835 Assoc_add_entry(dht->assoc, public_key, &ippts, NULL, used ? 1 : 0);
814 } 836 }
815 837
816#endif 838#endif
817 return used; 839 return used;
818} 840}
819 841
820/* If client_id is a friend or us, update ret_ip_port 842/* If public_key is a friend or us, update ret_ip_port
821 * nodeclient_id is the id of the node that sent us this info. 843 * nodepublic_key is the id of the node that sent us this info.
822 */ 844 */
823static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *client_id, const uint8_t *nodeclient_id) 845static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key)
824{ 846{
825 uint32_t i, j; 847 uint32_t i, j;
826 uint64_t temp_time = unix_time(); 848 uint64_t temp_time = unix_time();
@@ -833,9 +855,9 @@ static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *client_id,
833 ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; 855 ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3];
834 } 856 }
835 857
836 if (id_equal(client_id, dht->self_public_key)) { 858 if (id_equal(public_key, dht->self_public_key)) {
837 for (i = 0; i < LCLIENT_LIST; ++i) { 859 for (i = 0; i < LCLIENT_LIST; ++i) {
838 if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) { 860 if (id_equal(nodepublic_key, dht->close_clientlist[i].public_key)) {
839 if (ip_port.ip.family == AF_INET) { 861 if (ip_port.ip.family == AF_INET) {
840 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; 862 dht->close_clientlist[i].assoc4.ret_ip_port = ip_port;
841 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; 863 dht->close_clientlist[i].assoc4.ret_timestamp = temp_time;
@@ -850,9 +872,9 @@ static int returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *client_id,
850 } 872 }
851 } else { 873 } else {
852 for (i = 0; i < dht->num_friends; ++i) { 874 for (i = 0; i < dht->num_friends; ++i) {
853 if (id_equal(client_id, dht->friends_list[i].client_id)) { 875 if (id_equal(public_key, dht->friends_list[i].public_key)) {
854 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 876 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
855 if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) { 877 if (id_equal(nodepublic_key, dht->friends_list[i].client_list[j].public_key)) {
856 if (ip_port.ip.family == AF_INET) { 878 if (ip_port.ip.family == AF_INET) {
857 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; 879 dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port;
858 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; 880 dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time;
@@ -878,7 +900,7 @@ end:
878 ippts.timestamp = temp_time; 900 ippts.timestamp = temp_time;
879 /* this is only a hear-say entry, so ret-ipp is NULL, but used is required 901 /* this is only a hear-say entry, so ret-ipp is NULL, but used is required
880 * to decide how valuable it is ("used" may throw an "unused" entry out) */ 902 * to decide how valuable it is ("used" may throw an "unused" entry out) */
881 Assoc_add_entry(dht->assoc, client_id, &ippts, NULL, used ? 1 : 0); 903 Assoc_add_entry(dht->assoc, public_key, &ippts, NULL, used ? 1 : 0);
882 } 904 }
883 905
884#endif 906#endif
@@ -897,7 +919,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
897 uint8_t plain_message[sizeof(Node_format) * 2] = {0}; 919 uint8_t plain_message[sizeof(Node_format) * 2] = {0};
898 920
899 Node_format receiver; 921 Node_format receiver;
900 memcpy(receiver.public_key, public_key, CLIENT_ID_SIZE); 922 memcpy(receiver.public_key, public_key, crypto_box_PUBLICKEYBYTES);
901 receiver.ip_port = ip_port; 923 receiver.ip_port = ip_port;
902 memcpy(plain_message, &receiver, sizeof(receiver)); 924 memcpy(plain_message, &receiver, sizeof(receiver));
903 925
@@ -913,12 +935,12 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
913 if (ping_id == 0) 935 if (ping_id == 0)
914 return -1; 936 return -1;
915 937
916 uint8_t plain[CLIENT_ID_SIZE + sizeof(ping_id)]; 938 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)];
917 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; 939 uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES];
918 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(encrypt)]; 940 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(encrypt)];
919 941
920 memcpy(plain, client_id, CLIENT_ID_SIZE); 942 memcpy(plain, client_id, crypto_box_PUBLICKEYBYTES);
921 memcpy(plain + CLIENT_ID_SIZE, &ping_id, sizeof(ping_id)); 943 memcpy(plain + crypto_box_PUBLICKEYBYTES, &ping_id, sizeof(ping_id));
922 944
923 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 945 uint8_t shared_key[crypto_box_BEFORENMBYTES];
924 DHT_get_shared_key_sent(dht, shared_key, public_key); 946 DHT_get_shared_key_sent(dht, shared_key, public_key);
@@ -936,9 +958,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
936 return -1; 958 return -1;
937 959
938 data[0] = NET_PACKET_GET_NODES; 960 data[0] = NET_PACKET_GET_NODES;
939 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 961 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
940 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 962 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
941 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 963 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
942 964
943 return sendpacket(dht->net, ip_port, data, sizeof(data)); 965 return sendpacket(dht->net, ip_port, data, sizeof(data));
944} 966}
@@ -955,7 +977,7 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
955 return -1; 977 return -1;
956 978
957 size_t Node_format_size = sizeof(Node_format); 979 size_t Node_format_size = sizeof(Node_format);
958 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES 980 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES
959 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES]; 981 + Node_format_size * MAX_SENT_NODES + length + crypto_box_MACBYTES];
960 982
961 Node_format nodes_list[MAX_SENT_NODES]; 983 Node_format nodes_list[MAX_SENT_NODES];
@@ -986,16 +1008,17 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
986 return -1; 1008 return -1;
987 1009
988 data[0] = NET_PACKET_SEND_NODES_IPV6; 1010 data[0] = NET_PACKET_SEND_NODES_IPV6;
989 memcpy(data + 1, dht->self_public_key, CLIENT_ID_SIZE); 1011 memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
990 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 1012 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES);
991 memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); 1013 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, encrypt, len);
992 1014
993 return sendpacket(dht->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 1015 return sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len);
994} 1016}
995 1017
996static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length) 1018static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length)
997{ 1019{
998 if (length != (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES)) 1020 if (length != (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof(
1021 uint64_t) + crypto_box_MACBYTES))
999 return 1; 1022 return 1;
1000 1023
1001 DHT *dht = object; 1024 DHT *dht = object;
@@ -1004,20 +1027,20 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
1004 if (id_equal(packet + 1, dht->self_public_key)) 1027 if (id_equal(packet + 1, dht->self_public_key))
1005 return 1; 1028 return 1;
1006 1029
1007 uint8_t plain[CLIENT_ID_SIZE + sizeof(uint64_t)]; 1030 uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)];
1008 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 1031 uint8_t shared_key[crypto_box_BEFORENMBYTES];
1009 1032
1010 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 1033 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
1011 int len = decrypt_data_symmetric( shared_key, 1034 int len = decrypt_data_symmetric( shared_key,
1012 packet + 1 + CLIENT_ID_SIZE, 1035 packet + 1 + crypto_box_PUBLICKEYBYTES,
1013 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1036 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1014 CLIENT_ID_SIZE + sizeof(uint64_t) + crypto_box_MACBYTES, 1037 crypto_box_PUBLICKEYBYTES + sizeof(uint64_t) + crypto_box_MACBYTES,
1015 plain ); 1038 plain );
1016 1039
1017 if (len != CLIENT_ID_SIZE + sizeof(uint64_t)) 1040 if (len != crypto_box_PUBLICKEYBYTES + sizeof(uint64_t))
1018 return 1; 1041 return 1;
1019 1042
1020 sendnodes_ipv6(dht, source, packet + 1, plain, plain + CLIENT_ID_SIZE, sizeof(uint64_t), shared_key); 1043 sendnodes_ipv6(dht, source, packet + 1, plain, plain + crypto_box_PUBLICKEYBYTES, sizeof(uint64_t), shared_key);
1021 1044
1022 add_to_ping(dht->ping, packet + 1, source); 1045 add_to_ping(dht->ping, packet + 1, source);
1023 1046
@@ -1025,7 +1048,7 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
1025} 1048}
1026/* return 0 if no 1049/* return 0 if no
1027 return 1 if yes */ 1050 return 1 if yes */
1028static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *client_id, IP_Port node_ip_port, uint64_t ping_id, 1051static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port node_ip_port, uint64_t ping_id,
1029 Node_format *sendback_node) 1052 Node_format *sendback_node)
1030{ 1053{
1031 uint8_t data[sizeof(Node_format) * 2]; 1054 uint8_t data[sizeof(Node_format) * 2];
@@ -1041,7 +1064,7 @@ static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *client_id, IP_Port
1041 Node_format test; 1064 Node_format test;
1042 memcpy(&test, data, sizeof(Node_format)); 1065 memcpy(&test, data, sizeof(Node_format));
1043 1066
1044 if (!ipport_equal(&test.ip_port, &node_ip_port) || memcmp(test.public_key, client_id, CLIENT_ID_SIZE) != 0) 1067 if (!ipport_equal(&test.ip_port, &node_ip_port) || memcmp(test.public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0)
1045 return 0; 1068 return 0;
1046 1069
1047 return 1; 1070 return 1;
@@ -1055,7 +1078,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1055 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) 1078 Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
1056{ 1079{
1057 DHT *dht = object; 1080 DHT *dht = object;
1058 uint32_t cid_size = 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES; 1081 uint32_t cid_size = 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES;
1059 1082
1060 if (length <= cid_size) /* too short */ 1083 if (length <= cid_size) /* too short */
1061 return 1; 1084 return 1;
@@ -1073,8 +1096,8 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1073 DHT_get_shared_key_sent(dht, shared_key, packet + 1); 1096 DHT_get_shared_key_sent(dht, shared_key, packet + 1);
1074 int len = decrypt_data_symmetric( 1097 int len = decrypt_data_symmetric(
1075 shared_key, 1098 shared_key,
1076 packet + 1 + CLIENT_ID_SIZE, 1099 packet + 1 + crypto_box_PUBLICKEYBYTES,
1077 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 1100 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
1078 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES, 1101 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES,
1079 plain); 1102 plain);
1080 1103
@@ -1141,34 +1164,10 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa
1141/*----------------------------------------------------------------------------------*/ 1164/*----------------------------------------------------------------------------------*/
1142/*------------------------END of packet handling functions--------------------------*/ 1165/*------------------------END of packet handling functions--------------------------*/
1143 1166
1144/* 1167int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port),
1145 * Send get nodes requests with client_id to max_num peers in list of length length
1146 */
1147/*
1148static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_t max_num, uint8_t *client_id)
1149{
1150 uint32_t i, num = 0;
1151
1152 for (i = 0; i < length; ++i) {
1153 IPPTsPng *assoc;
1154 uint32_t a;
1155
1156 for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4)
1157 if (ipport_isset(&(assoc->ip_port)) &&
1158 !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {
1159 getnodes(dht, assoc->ip_port, list[i].client_id, client_id, NULL);
1160 ++num;
1161
1162 if (num >= max_num)
1163 return;
1164 }
1165 }
1166}
1167*/
1168int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *data, int32_t number, IP_Port),
1169 void *data, int32_t number, uint16_t *lock_count) 1168 void *data, int32_t number, uint16_t *lock_count)
1170{ 1169{
1171 int friend_num = friend_number(dht, client_id); 1170 int friend_num = friend_number(dht, public_key);
1172 1171
1173 uint16_t lock_num; 1172 uint16_t lock_num;
1174 1173
@@ -1199,7 +1198,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *
1199 dht->friends_list = temp; 1198 dht->friends_list = temp;
1200 DHT_Friend *friend = &dht->friends_list[dht->num_friends]; 1199 DHT_Friend *friend = &dht->friends_list[dht->num_friends];
1201 memset(friend, 0, sizeof(DHT_Friend)); 1200 memset(friend, 0, sizeof(DHT_Friend));
1202 memcpy(friend->client_id, client_id, CLIENT_ID_SIZE); 1201 memcpy(friend->public_key, public_key, crypto_box_PUBLICKEYBYTES);
1203 1202
1204 friend->nat.NATping_id = random_64b(); 1203 friend->nat.NATping_id = random_64b();
1205 ++dht->num_friends; 1204 ++dht->num_friends;
@@ -1221,7 +1220,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *
1221 1220
1222 Assoc_close_entries close_entries; 1221 Assoc_close_entries close_entries;
1223 memset(&close_entries, 0, sizeof(close_entries)); 1222 memset(&close_entries, 0, sizeof(close_entries));
1224 close_entries.wanted_id = client_id; 1223 close_entries.wanted_id = public_key;
1225 close_entries.count_good = MAX_FRIEND_CLIENTS / 2; 1224 close_entries.count_good = MAX_FRIEND_CLIENTS / 2;
1226 close_entries.count = MAX_FRIEND_CLIENTS; 1225 close_entries.count = MAX_FRIEND_CLIENTS;
1227 close_entries.result = calloc(MAX_FRIEND_CLIENTS, sizeof(*close_entries.result)); 1226 close_entries.result = calloc(MAX_FRIEND_CLIENTS, sizeof(*close_entries.result));
@@ -1236,23 +1235,21 @@ int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *
1236 Client_data *client = &friend->client_list[0]; 1235 Client_data *client = &friend->client_list[0];
1237 1236
1238 if (ipport_isset(&client->assoc4.ip_port)) 1237 if (ipport_isset(&client->assoc4.ip_port))
1239 getnodes(dht, client->assoc4.ip_port, client->client_id, friend->client_id, NULL); 1238 getnodes(dht, client->assoc4.ip_port, client->public_key, friend->public_key, NULL);
1240 1239
1241 if (ipport_isset(&client->assoc6.ip_port)) 1240 if (ipport_isset(&client->assoc6.ip_port))
1242 getnodes(dht, client->assoc6.ip_port, client->client_id, friend->client_id, NULL); 1241 getnodes(dht, client->assoc6.ip_port, client->public_key, friend->public_key, NULL);
1243 } 1242 }
1244 } 1243 }
1245 1244
1246#endif 1245#endif
1247 /*this isn't really useful anymore.
1248 get_bunchnodes(dht, dht->close_clientlist, LCLIENT_LIST, MAX_FRIEND_CLIENTS, client_id);*/
1249 1246
1250 return 0; 1247 return 0;
1251} 1248}
1252 1249
1253int DHT_delfriend(DHT *dht, const uint8_t *client_id, uint16_t lock_count) 1250int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count)
1254{ 1251{
1255 int friend_num = friend_number(dht, client_id); 1252 int friend_num = friend_number(dht, public_key);
1256 1253
1257 if (friend_num == -1) { 1254 if (friend_num == -1) {
1258 return -1; 1255 return -1;
@@ -1295,7 +1292,7 @@ int DHT_delfriend(DHT *dht, const uint8_t *client_id, uint16_t lock_count)
1295} 1292}
1296 1293
1297/* TODO: Optimize this. */ 1294/* TODO: Optimize this. */
1298int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port) 1295int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
1299{ 1296{
1300 uint32_t i, j; 1297 uint32_t i, j;
1301 1298
@@ -1304,11 +1301,11 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port)
1304 1301
1305 for (i = 0; i < dht->num_friends; ++i) { 1302 for (i = 0; i < dht->num_friends; ++i) {
1306 /* Equal */ 1303 /* Equal */
1307 if (id_equal(dht->friends_list[i].client_id, client_id)) { 1304 if (id_equal(dht->friends_list[i].public_key, public_key)) {
1308 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 1305 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
1309 Client_data *client = &dht->friends_list[i].client_list[j]; 1306 Client_data *client = &dht->friends_list[i].client_list[j];
1310 1307
1311 if (id_equal(client->client_id, client_id)) { 1308 if (id_equal(client->public_key, public_key)) {
1312 IPPTsPng *assoc = NULL; 1309 IPPTsPng *assoc = NULL;
1313 uint32_t a; 1310 uint32_t a;
1314 1311
@@ -1328,7 +1325,7 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port)
1328} 1325}
1329 1326
1330/* returns number of nodes not in kill-timeout */ 1327/* returns number of nodes not in kill-timeout */
1331static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, const uint8_t *client_id, 1328static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, const uint8_t *public_key,
1332 Client_data *list, uint32_t list_count, uint32_t *bootstrap_times) 1329 Client_data *list, uint32_t list_count, uint32_t *bootstrap_times)
1333{ 1330{
1334 uint32_t i; 1331 uint32_t i;
@@ -1350,7 +1347,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1350 not_kill++; 1347 not_kill++;
1351 1348
1352 if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { 1349 if (is_timeout(assoc->last_pinged, PING_INTERVAL)) {
1353 send_ping_request(dht->ping, assoc->ip_port, client->client_id ); 1350 send_ping_request(dht->ping, assoc->ip_port, client->public_key );
1354 assoc->last_pinged = temp_time; 1351 assoc->last_pinged = temp_time;
1355 } 1352 }
1356 1353
@@ -1365,8 +1362,8 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1365 1362
1366 if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { 1363 if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) {
1367 uint32_t rand_node = rand() % num_nodes; 1364 uint32_t rand_node = rand() % num_nodes;
1368 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->client_id, 1365 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key,
1369 client_id, NULL); 1366 public_key, NULL);
1370 *lastgetnode = temp_time; 1367 *lastgetnode = temp_time;
1371 ++*bootstrap_times; 1368 ++*bootstrap_times;
1372 } 1369 }
@@ -1382,7 +1379,7 @@ static void do_DHT_friends(DHT *dht)
1382 uint32_t i; 1379 uint32_t i;
1383 1380
1384 for (i = 0; i < dht->num_friends; ++i) 1381 for (i = 0; i < dht->num_friends; ++i)
1385 do_ping_and_sendnode_requests(dht, &dht->friends_list[i].lastgetnode, dht->friends_list[i].client_id, 1382 do_ping_and_sendnode_requests(dht, &dht->friends_list[i].lastgetnode, dht->friends_list[i].public_key,
1386 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &dht->friends_list[i].bootstrap_times); 1383 dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &dht->friends_list[i].bootstrap_times);
1387} 1384}
1388 1385
@@ -1463,16 +1460,16 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable
1463 return 0; 1460 return 0;
1464} 1461}
1465 1462
1466/* Send the given packet to node with client_id 1463/* Send the given packet to node with public_key
1467 * 1464 *
1468 * return -1 if failure. 1465 * return -1 if failure.
1469 */ 1466 */
1470int route_packet(const DHT *dht, const uint8_t *client_id, const uint8_t *packet, uint16_t length) 1467int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length)
1471{ 1468{
1472 uint32_t i; 1469 uint32_t i;
1473 1470
1474 for (i = 0; i < LCLIENT_LIST; ++i) { 1471 for (i = 0; i < LCLIENT_LIST; ++i) {
1475 if (id_equal(client_id, dht->close_clientlist[i].client_id)) { 1472 if (id_equal(public_key, dht->close_clientlist[i].public_key)) {
1476 const Client_data *client = &dht->close_clientlist[i]; 1473 const Client_data *client = &dht->close_clientlist[i];
1477 1474
1478 if (ip_isset(&client->assoc6.ip_port.ip)) 1475 if (ip_isset(&client->assoc6.ip_port.ip))
@@ -1521,7 +1518,7 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n
1521 ++num_ipv6s; 1518 ++num_ipv6s;
1522 } 1519 }
1523 1520
1524 if (id_equal(client->client_id, friend->client_id)) 1521 if (id_equal(client->public_key, friend->public_key))
1525 if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT)) 1522 if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT))
1526 return 0; /* direct connectivity */ 1523 return 0; /* direct connectivity */
1527 } 1524 }
@@ -1796,7 +1793,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1796 IP_Port pinging; 1793 IP_Port pinging;
1797 ip_copy(&pinging.ip, &ip); 1794 ip_copy(&pinging.ip, &ip);
1798 pinging.port = htons(firstport); 1795 pinging.port = htons(firstport);
1799 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); 1796 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
1800 } else { 1797 } else {
1801 for (i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) { 1798 for (i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) {
1802 /* TODO: Improve port guessing algorithm. */ 1799 /* TODO: Improve port guessing algorithm. */
@@ -1804,7 +1801,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1804 IP_Port pinging; 1801 IP_Port pinging;
1805 ip_copy(&pinging.ip, &ip); 1802 ip_copy(&pinging.ip, &ip);
1806 pinging.port = htons(port); 1803 pinging.port = htons(port);
1807 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); 1804 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
1808 } 1805 }
1809 1806
1810 dht->friends_list[friend_num].nat.punching_index = i; 1807 dht->friends_list[friend_num].nat.punching_index = i;
@@ -1818,7 +1815,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1818 1815
1819 for (i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) { 1816 for (i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) {
1820 pinging.port = htons(port + i); 1817 pinging.port = htons(port + i);
1821 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); 1818 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
1822 } 1819 }
1823 1820
1824 dht->friends_list[friend_num].nat.punching_index2 = i - (MAX_PUNCHING_PORTS / 2); 1821 dht->friends_list[friend_num].nat.punching_index2 = i - (MAX_PUNCHING_PORTS / 2);
@@ -1841,7 +1838,7 @@ static void do_NAT(DHT *dht)
1841 continue; 1838 continue;
1842 1839
1843 if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) { 1840 if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) {
1844 send_NATping(dht, dht->friends_list[i].client_id, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST); 1841 send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST);
1845 dht->friends_list[i].nat.NATping_timestamp = temp_time; 1842 dht->friends_list[i].nat.NATping_timestamp = temp_time;
1846 } 1843 }
1847 1844
@@ -1897,10 +1894,10 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8
1897/* Send a get node hardening request */ 1894/* Send a get node hardening request */
1898static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id) 1895static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format *node_totest, uint8_t *search_id)
1899{ 1896{
1900 uint8_t data[sizeof(Node_format) + CLIENT_ID_SIZE]; 1897 uint8_t data[sizeof(Node_format) + crypto_box_PUBLICKEYBYTES];
1901 memcpy(data, node_totest, sizeof(Node_format)); 1898 memcpy(data, node_totest, sizeof(Node_format));
1902 memcpy(data + sizeof(Node_format), search_id, CLIENT_ID_SIZE); 1899 memcpy(data + sizeof(Node_format), search_id, crypto_box_PUBLICKEYBYTES);
1903 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + CLIENT_ID_SIZE); 1900 return send_hardening_req(dht, dest, CHECK_TYPE_GETNODE_REQ, data, sizeof(Node_format) + crypto_box_PUBLICKEYBYTES);
1904} 1901}
1905 1902
1906/* Send a get node hardening response */ 1903/* Send a get node hardening response */
@@ -1911,10 +1908,10 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto,
1911 return -1; 1908 return -1;
1912 1909
1913 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 1910 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
1914 uint8_t data[1 + CLIENT_ID_SIZE + nodes_data_length]; 1911 uint8_t data[1 + crypto_box_PUBLICKEYBYTES + nodes_data_length];
1915 data[0] = CHECK_TYPE_GETNODE_RES; 1912 data[0] = CHECK_TYPE_GETNODE_RES;
1916 memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); 1913 memcpy(data + 1, queried_client_id, crypto_box_PUBLICKEYBYTES);
1917 memcpy(data + 1 + CLIENT_ID_SIZE, nodes_data, nodes_data_length); 1914 memcpy(data + 1 + crypto_box_PUBLICKEYBYTES, nodes_data, nodes_data_length);
1918 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, 1915 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data,
1919 sizeof(data), CRYPTO_PACKET_HARDENING); 1916 sizeof(data), CRYPTO_PACKET_HARDENING);
1920 1917
@@ -1925,12 +1922,12 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto,
1925} 1922}
1926 1923
1927/* TODO: improve */ 1924/* TODO: improve */
1928static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *client_id, sa_family_t sa_family) 1925static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_family_t sa_family)
1929{ 1926{
1930 uint32_t i; 1927 uint32_t i;
1931 1928
1932 for (i = 0; i < LCLIENT_LIST; ++i) { 1929 for (i = 0; i < LCLIENT_LIST; ++i) {
1933 if (memcmp(dht->close_clientlist[i].client_id, client_id, CLIENT_ID_SIZE) != 0) 1930 if (memcmp(dht->close_clientlist[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0)
1934 continue; 1931 continue;
1935 1932
1936 if (sa_family == AF_INET) 1933 if (sa_family == AF_INET)
@@ -2000,15 +1997,15 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2000 } 1997 }
2001 1998
2002 case CHECK_TYPE_GETNODE_RES: { 1999 case CHECK_TYPE_GETNODE_RES: {
2003 if (length <= CLIENT_ID_SIZE + 1) 2000 if (length <= crypto_box_PUBLICKEYBYTES + 1)
2004 return 1; 2001 return 1;
2005 2002
2006 if (length > 1 + CLIENT_ID_SIZE + sizeof(Node_format) * MAX_SENT_NODES) 2003 if (length > 1 + crypto_box_PUBLICKEYBYTES + sizeof(Node_format) * MAX_SENT_NODES)
2007 return 1; 2004 return 1;
2008 2005
2009 uint16_t length_nodes = length - 1 - CLIENT_ID_SIZE; 2006 uint16_t length_nodes = length - 1 - crypto_box_PUBLICKEYBYTES;
2010 Node_format nodes[MAX_SENT_NODES]; 2007 Node_format nodes[MAX_SENT_NODES];
2011 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CLIENT_ID_SIZE, length_nodes, 0); 2008 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + crypto_box_PUBLICKEYBYTES, length_nodes, 0);
2012 2009
2013 /* TODO: MAX_SENT_NODES nodes should be returned at all times 2010 /* TODO: MAX_SENT_NODES nodes should be returned at all times
2014 (right now we have a small network size so it could cause problems for testing and etc..) */ 2011 (right now we have a small network size so it could cause problems for testing and etc..) */
@@ -2027,7 +2024,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2027 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) 2024 if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL))
2028 return 1; 2025 return 1;
2029 2026
2030 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, CLIENT_ID_SIZE) != 0) 2027 if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, crypto_box_PUBLICKEYBYTES) != 0)
2031 return 1; 2028 return 1;
2032 2029
2033 /* If Nodes look good and the request checks out */ 2030 /* If Nodes look good and the request checks out */
@@ -2044,10 +2041,10 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2044 */ 2041 */
2045Node_format random_node(DHT *dht, sa_family_t sa_family) 2042Node_format random_node(DHT *dht, sa_family_t sa_family)
2046{ 2043{
2047 uint8_t id[CLIENT_ID_SIZE]; 2044 uint8_t id[crypto_box_PUBLICKEYBYTES];
2048 uint32_t i; 2045 uint32_t i;
2049 2046
2050 for (i = 0; i < CLIENT_ID_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ 2047 for (i = 0; i < crypto_box_PUBLICKEYBYTES / 4; ++i) { /* populate the id with pseudorandom bytes.*/
2051 uint32_t t = rand(); 2048 uint32_t t = rand();
2052 memcpy(id + i * sizeof(t), &t, sizeof(t)); 2049 memcpy(id + i * sizeof(t), &t, sizeof(t));
2053 } 2050 }
@@ -2090,7 +2087,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
2090 } 2087 }
2091 2088
2092 if (assoc != NULL) { 2089 if (assoc != NULL) {
2093 memcpy(nodes[count].public_key, list[i - 1].client_id, CLIENT_ID_SIZE); 2090 memcpy(nodes[count].public_key, list[i - 1].public_key, crypto_box_PUBLICKEYBYTES);
2094 nodes[count].ip_port = assoc->ip_port; 2091 nodes[count].ip_port = assoc->ip_port;
2095 ++count; 2092 ++count;
2096 2093
@@ -2109,7 +2106,7 @@ void do_hardening(DHT *dht)
2109 for (i = 0; i < LCLIENT_LIST * 2; ++i) { 2106 for (i = 0; i < LCLIENT_LIST * 2; ++i) {
2110 IPPTsPng *cur_iptspng; 2107 IPPTsPng *cur_iptspng;
2111 sa_family_t sa_family; 2108 sa_family_t sa_family;
2112 uint8_t *client_id = dht->close_clientlist[i / 2].client_id; 2109 uint8_t *public_key = dht->close_clientlist[i / 2].public_key;
2113 2110
2114 if (i % 2 == 0) { 2111 if (i % 2 == 0) {
2115 cur_iptspng = &dht->close_clientlist[i / 2].assoc4; 2112 cur_iptspng = &dht->close_clientlist[i / 2].assoc4;
@@ -2129,12 +2126,12 @@ void do_hardening(DHT *dht)
2129 if (!ipport_isset(&rand_node.ip_port)) 2126 if (!ipport_isset(&rand_node.ip_port))
2130 continue; 2127 continue;
2131 2128
2132 if (id_equal(client_id, rand_node.public_key)) 2129 if (id_equal(public_key, rand_node.public_key))
2133 continue; 2130 continue;
2134 2131
2135 Node_format to_test; 2132 Node_format to_test;
2136 to_test.ip_port = cur_iptspng->ip_port; 2133 to_test.ip_port = cur_iptspng->ip_port;
2137 memcpy(to_test.public_key, client_id, crypto_box_PUBLICKEYBYTES); 2134 memcpy(to_test.public_key, public_key, crypto_box_PUBLICKEYBYTES);
2138 2135
2139 //TODO: The search id should maybe not be ours? 2136 //TODO: The search id should maybe not be ours?
2140 if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->self_public_key) > 0) { 2137 if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->self_public_key) > 0) {
@@ -2234,7 +2231,7 @@ DHT *new_DHT(Networking_Core *net)
2234 uint32_t i; 2231 uint32_t i;
2235 2232
2236 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { 2233 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
2237 uint8_t random_key_bytes[CLIENT_ID_SIZE]; 2234 uint8_t random_key_bytes[crypto_box_PUBLICKEYBYTES];
2238 randombytes(random_key_bytes, sizeof(random_key_bytes)); 2235 randombytes(random_key_bytes, sizeof(random_key_bytes));
2239 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0); 2236 DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0);
2240 } 2237 }
@@ -2292,41 +2289,46 @@ void kill_DHT(DHT *dht)
2292#define DHT_STATE_COOKIE_TYPE 0x11ce 2289#define DHT_STATE_COOKIE_TYPE 0x11ce
2293#define DHT_STATE_TYPE_NODES 4 2290#define DHT_STATE_TYPE_NODES 4
2294 2291
2292#define MAX_SAVED_DHT_NODES (((DHT_FAKE_FRIEND_NUMBER * MAX_FRIEND_CLIENTS) + LCLIENT_LIST) * 2)
2293
2295/* Get the size of the DHT (for saving). */ 2294/* Get the size of the DHT (for saving). */
2296uint32_t DHT_size(const DHT *dht) 2295uint32_t DHT_size(const DHT *dht)
2297{ 2296{
2298 uint32_t num = 0, i, j; 2297 uint32_t numv4 = 0, numv6 = 0, i, j;
2299 2298
2300 for (i = 0; i < LCLIENT_LIST; ++i) { 2299 for (i = 0; i < LCLIENT_LIST; ++i) {
2301 num += (dht->close_clientlist[i].assoc4.timestamp != 0) + (dht->close_clientlist[i].assoc6.timestamp != 0); 2300 numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0);
2301 numv6 += (dht->close_clientlist[i].assoc6.timestamp != 0);
2302 } 2302 }
2303 2303
2304 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { 2304 for (i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) {
2305 DHT_Friend *fr = &dht->friends_list[i]; 2305 DHT_Friend *fr = &dht->friends_list[i];
2306 2306
2307 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 2307 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2308 num += (fr->client_list[j].assoc4.timestamp != 0) + (fr->client_list[j].assoc6.timestamp != 0); 2308 numv4 += (fr->client_list[j].assoc4.timestamp != 0);
2309 numv6 += (fr->client_list[j].assoc6.timestamp != 0);
2309 } 2310 }
2310 } 2311 }
2311 2312
2312 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; 2313 uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
2313 return size32 2314
2314 + sizesubhead + sizeof(Node_format) * num; 2315 return size32 + sizesubhead + (packed_node_size(AF_INET) * numv4) + (packed_node_size(AF_INET6) * numv6);
2315} 2316}
2316 2317
2317static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 2318static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
2318{ 2319{
2319 uint32_t *data32 = (uint32_t *)data; 2320 host_to_lendian32(data, len);
2320 data32[0] = len; 2321 data += sizeof(uint32_t);
2321 data32[1] = (DHT_STATE_COOKIE_TYPE << 16) | type; 2322 host_to_lendian32(data, (host_tolendian16(DHT_STATE_COOKIE_TYPE) << 16) | host_tolendian16(type));
2322 data += sizeof(uint32_t) * 2; 2323 data += sizeof(uint32_t);
2323 return data; 2324 return data;
2324} 2325}
2325 2326
2327
2326/* Save the DHT in data where data is an array of size DHT_size(). */ 2328/* Save the DHT in data where data is an array of size DHT_size(). */
2327void DHT_save(DHT *dht, uint8_t *data) 2329void DHT_save(DHT *dht, uint8_t *data)
2328{ 2330{
2329 *(uint32_t *)data = DHT_STATE_COOKIE_GLOBAL; 2331 host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL);
2330 data += sizeof(uint32_t); 2332 data += sizeof(uint32_t);
2331 2333
2332 uint32_t num, i, j; 2334 uint32_t num, i, j;
@@ -2336,17 +2338,17 @@ void DHT_save(DHT *dht, uint8_t *data)
2336 /* get right offset. we write the actual header later. */ 2338 /* get right offset. we write the actual header later. */
2337 data = z_state_save_subheader(data, 0, 0); 2339 data = z_state_save_subheader(data, 0, 0);
2338 2340
2339 Node_format *clients = (Node_format *)data; 2341 Node_format clients[MAX_SAVED_DHT_NODES];
2340 2342
2341 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) { 2343 for (num = 0, i = 0; i < LCLIENT_LIST; ++i) {
2342 if (dht->close_clientlist[i].assoc4.timestamp != 0) { 2344 if (dht->close_clientlist[i].assoc4.timestamp != 0) {
2343 memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); 2345 memcpy(clients[num].public_key, dht->close_clientlist[i].public_key, crypto_box_PUBLICKEYBYTES);
2344 clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port; 2346 clients[num].ip_port = dht->close_clientlist[i].assoc4.ip_port;
2345 ++num; 2347 ++num;
2346 } 2348 }
2347 2349
2348 if (dht->close_clientlist[i].assoc6.timestamp != 0) { 2350 if (dht->close_clientlist[i].assoc6.timestamp != 0) {
2349 memcpy(clients[num].public_key, dht->close_clientlist[i].client_id, crypto_box_PUBLICKEYBYTES); 2351 memcpy(clients[num].public_key, dht->close_clientlist[i].public_key, crypto_box_PUBLICKEYBYTES);
2350 clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port; 2352 clients[num].ip_port = dht->close_clientlist[i].assoc6.ip_port;
2351 ++num; 2353 ++num;
2352 } 2354 }
@@ -2357,20 +2359,20 @@ void DHT_save(DHT *dht, uint8_t *data)
2357 2359
2358 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 2360 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
2359 if (fr->client_list[j].assoc4.timestamp != 0) { 2361 if (fr->client_list[j].assoc4.timestamp != 0) {
2360 memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES); 2362 memcpy(clients[num].public_key, fr->client_list[j].public_key, crypto_box_PUBLICKEYBYTES);
2361 clients[num].ip_port = fr->client_list[j].assoc4.ip_port; 2363 clients[num].ip_port = fr->client_list[j].assoc4.ip_port;
2362 ++num; 2364 ++num;
2363 } 2365 }
2364 2366
2365 if (fr->client_list[j].assoc6.timestamp != 0) { 2367 if (fr->client_list[j].assoc6.timestamp != 0) {
2366 memcpy(clients[num].public_key, fr->client_list[j].client_id, crypto_box_PUBLICKEYBYTES); 2368 memcpy(clients[num].public_key, fr->client_list[j].public_key, crypto_box_PUBLICKEYBYTES);
2367 clients[num].ip_port = fr->client_list[j].assoc6.ip_port; 2369 clients[num].ip_port = fr->client_list[j].assoc6.ip_port;
2368 ++num; 2370 ++num;
2369 } 2371 }
2370 } 2372 }
2371 } 2373 }
2372 2374
2373 z_state_save_subheader(old_data, num * sizeof(Node_format), DHT_STATE_TYPE_NODES); 2375 z_state_save_subheader(old_data, pack_nodes(data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
2374} 2376}
2375 2377
2376/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */ 2378/* Bootstrap from this number of nodes every time DHT_connect_after_load() is called */
@@ -2407,25 +2409,26 @@ int DHT_connect_after_load(DHT *dht)
2407static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) 2409static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
2408{ 2410{
2409 DHT *dht = outer; 2411 DHT *dht = outer;
2410 uint32_t num, i;
2411 2412
2412 switch (type) { 2413 switch (type) {
2413 case DHT_STATE_TYPE_NODES: 2414 case DHT_STATE_TYPE_NODES:
2414 if ((length % sizeof(Node_format)) != 0) 2415 if (length == 0)
2415 break; 2416 break;
2416 2417
2417 { /* localize declarations */ 2418 {
2418 num = length / sizeof(Node_format);
2419 Node_format *client_list = (Node_format *)data;
2420
2421 free(dht->loaded_nodes_list); 2419 free(dht->loaded_nodes_list);
2422 // Copy to loaded_clients_list 2420 // Copy to loaded_clients_list
2423 dht->loaded_nodes_list = calloc(num, sizeof(Node_format)); 2421 dht->loaded_nodes_list = calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format));
2422
2423 int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, NULL, data, length, 0);
2424 2424
2425 for (i = 0; i < num; i++) 2425 Node_format *client_list = (Node_format *)data;
2426 memcpy(&(dht->loaded_nodes_list[i]), &(client_list[i]), sizeof(Node_format));
2427 2426
2428 dht->loaded_num_nodes = num; 2427 if (num > 0) {
2428 dht->loaded_num_nodes = num;
2429 } else {
2430 dht->loaded_num_nodes = 0;
2431 }
2429 2432
2430 } /* localize declarations */ 2433 } /* localize declarations */
2431 2434
@@ -2453,9 +2456,10 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length)
2453 uint32_t cookie_len = sizeof(uint32_t); 2456 uint32_t cookie_len = sizeof(uint32_t);
2454 2457
2455 if (length > cookie_len) { 2458 if (length > cookie_len) {
2456 uint32_t *data32 = (uint32_t *)data; 2459 uint32_t data32;
2460 lendian_to_host32(&data32, data);
2457 2461
2458 if (data32[0] == DHT_STATE_COOKIE_GLOBAL) 2462 if (data32 == DHT_STATE_COOKIE_GLOBAL)
2459 return load_state(dht_load_state_callback, dht, data + cookie_len, 2463 return load_state(dht_load_state_callback, dht, data + cookie_len,
2460 length - cookie_len, DHT_STATE_COOKIE_TYPE); 2464 length - cookie_len, DHT_STATE_COOKIE_TYPE);
2461 } 2465 }
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index c612c287..50ab92dc 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -1,6 +1,6 @@
1/* DHT.h 1/* DHT.h
2 * 2 *
3 * An implementation of the DHT as seen in http://wiki.tox.im/index.php/DHT 3 * An implementation of the DHT as seen in docs/updates/DHT.md
4 * 4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 * 6 *
@@ -28,9 +28,6 @@
28#include "network.h" 28#include "network.h"
29#include "ping_array.h" 29#include "ping_array.h"
30 30
31/* Size of the client_id in bytes. */
32#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
33
34/* Maximum number of clients stored per friend. */ 31/* Maximum number of clients stored per friend. */
35#define MAX_FRIEND_CLIENTS 8 32#define MAX_FRIEND_CLIENTS 8
36 33
@@ -79,17 +76,17 @@ typedef struct {
79 uint8_t routes_requests_ok; 76 uint8_t routes_requests_ok;
80 /* Time which we last checked this.*/ 77 /* Time which we last checked this.*/
81 uint64_t routes_requests_timestamp; 78 uint64_t routes_requests_timestamp;
82 uint8_t routes_requests_pingedid[CLIENT_ID_SIZE]; 79 uint8_t routes_requests_pingedid[crypto_box_PUBLICKEYBYTES];
83 /* Node sends correct send_node (true (1) or false/didn't check (0)) */ 80 /* Node sends correct send_node (true (1) or false/didn't check (0)) */
84 uint8_t send_nodes_ok; 81 uint8_t send_nodes_ok;
85 /* Time which we last checked this.*/ 82 /* Time which we last checked this.*/
86 uint64_t send_nodes_timestamp; 83 uint64_t send_nodes_timestamp;
87 uint8_t send_nodes_pingedid[CLIENT_ID_SIZE]; 84 uint8_t send_nodes_pingedid[crypto_box_PUBLICKEYBYTES];
88 /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */ 85 /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */
89 uint8_t testing_requests; 86 uint8_t testing_requests;
90 /* Time which we last checked this.*/ 87 /* Time which we last checked this.*/
91 uint64_t testing_timestamp; 88 uint64_t testing_timestamp;
92 uint8_t testing_pingedid[CLIENT_ID_SIZE]; 89 uint8_t testing_pingedid[crypto_box_PUBLICKEYBYTES];
93} Hardening; 90} Hardening;
94 91
95typedef struct { 92typedef struct {
@@ -104,7 +101,7 @@ typedef struct {
104} IPPTsPng; 101} IPPTsPng;
105 102
106typedef struct { 103typedef struct {
107 uint8_t client_id[CLIENT_ID_SIZE]; 104 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
108 IPPTsPng assoc4; 105 IPPTsPng assoc4;
109 IPPTsPng assoc6; 106 IPPTsPng assoc6;
110} Client_data; 107} Client_data;
@@ -127,7 +124,7 @@ typedef struct {
127#define DHT_FRIEND_MAX_LOCKS 32 124#define DHT_FRIEND_MAX_LOCKS 32
128 125
129typedef struct { 126typedef struct {
130 uint8_t client_id[CLIENT_ID_SIZE]; 127 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
131 Client_data client_list[MAX_FRIEND_CLIENTS]; 128 Client_data client_list[MAX_FRIEND_CLIENTS];
132 129
133 /* Time at which the last get_nodes request was sent. */ 130 /* Time at which the last get_nodes request was sent. */
@@ -153,6 +150,11 @@ typedef struct {
153} 150}
154Node_format; 151Node_format;
155 152
153/* Return packet size of packed node with ip_family on success.
154 * Return -1 on failure.
155 */
156int packed_node_size(uint8_t ip_family);
157
156/* Pack number of nodes into data of maxlength length. 158/* Pack number of nodes into data of maxlength length.
157 * 159 *
158 * return length of packed nodes on success. 160 * return length of packed nodes on success.
@@ -177,7 +179,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
177#define KEYS_TIMEOUT 600 179#define KEYS_TIMEOUT 600
178typedef struct { 180typedef struct {
179 struct { 181 struct {
180 uint8_t client_id[CLIENT_ID_SIZE]; 182 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
181 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 183 uint8_t shared_key[crypto_box_BEFORENMBYTES];
182 uint32_t times_requested; 184 uint32_t times_requested;
183 uint8_t stored; /* 0 if not, 1 if is */ 185 uint8_t stored; /* 0 if not, 1 if is */
@@ -236,22 +238,23 @@ typedef struct {
236 * If shared key is already in shared_keys, copy it to shared_key. 238 * If shared key is already in shared_keys, copy it to shared_key.
237 * else generate it into shared_key and copy it to shared_keys 239 * else generate it into shared_key and copy it to shared_keys
238 */ 240 */
239void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key, const uint8_t *client_id); 241void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t *secret_key,
242 const uint8_t *public_key);
240 243
241/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key 244/* Copy shared_key to encrypt/decrypt DHT packet from public_key into shared_key
242 * for packets that we receive. 245 * for packets that we receive.
243 */ 246 */
244void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *client_id); 247void DHT_get_shared_key_recv(DHT *dht, uint8_t *shared_key, const uint8_t *public_key);
245 248
246/* Copy shared_key to encrypt/decrypt DHT packet from client_id into shared_key 249/* Copy shared_key to encrypt/decrypt DHT packet from public_key into shared_key
247 * for packets that we send. 250 * for packets that we send.
248 */ 251 */
249void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *client_id); 252void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *public_key);
250 253
251void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); 254void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id);
252 255
253/* Add a new friend to the friends list. 256/* Add a new friend to the friends list.
254 * client_id must be CLIENT_ID_SIZE bytes long. 257 * public_key must be crypto_box_PUBLICKEYBYTES bytes long.
255 * 258 *
256 * ip_callback is the callback of a function that will be called when the ip address 259 * ip_callback is the callback of a function that will be called when the ip address
257 * is found along with arguments data and number. 260 * is found along with arguments data and number.
@@ -262,47 +265,39 @@ void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, con
262 * return 0 if success. 265 * return 0 if success.
263 * return -1 if failure (friends list is full). 266 * return -1 if failure (friends list is full).
264 */ 267 */
265int DHT_addfriend(DHT *dht, const uint8_t *client_id, void (*ip_callback)(void *data, int32_t number, IP_Port), 268int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port),
266 void *data, int32_t number, uint16_t *lock_count); 269 void *data, int32_t number, uint16_t *lock_count);
267 270
268/* Delete a friend from the friends list. 271/* Delete a friend from the friends list.
269 * client_id must be CLIENT_ID_SIZE bytes long. 272 * public_key must be crypto_box_PUBLICKEYBYTES bytes long.
270 * 273 *
271 * return 0 if success. 274 * return 0 if success.
272 * return -1 if failure (client_id not in friends list). 275 * return -1 if failure (public_key not in friends list).
273 */ 276 */
274int DHT_delfriend(DHT *dht, const uint8_t *client_id, uint16_t lock_count); 277int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count);
275 278
276/* Get ip of friend. 279/* Get ip of friend.
277 * client_id must be CLIENT_ID_SIZE bytes long. 280 * public_key must be crypto_box_PUBLICKEYBYTES bytes long.
278 * ip must be 4 bytes long. 281 * ip must be 4 bytes long.
279 * port must be 2 bytes long. 282 * port must be 2 bytes long.
280 * 283 *
281 * !!! Signature changed !!! 284 * int DHT_getfriendip(DHT *dht, uint8_t *public_key, IP_Port *ip_port);
282 *
283 * OLD: IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id);
284 *
285 * return ip if success.
286 * return ip of 0 if failure (This means the friend is either offline or we have not found him yet).
287 * return ip of 1 if friend is not in list.
288 *
289 * NEW: int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port);
290 * 285 *
291 * return -1, -- if client_id does NOT refer to a friend 286 * return -1, -- if public_key does NOT refer to a friend
292 * return 0, -- if client_id refers to a friend and we failed to find the friend (yet) 287 * return 0, -- if public_key refers to a friend and we failed to find the friend (yet)
293 * return 1, ip if client_id refers to a friend and we found him 288 * return 1, ip if public_key refers to a friend and we found him
294 */ 289 */
295int DHT_getfriendip(const DHT *dht, const uint8_t *client_id, IP_Port *ip_port); 290int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port);
296 291
297/* Compares client_id1 and client_id2 with client_id. 292/* Compares pk1 and pk2 with pk.
298 * 293 *
299 * return 0 if both are same distance. 294 * return 0 if both are same distance.
300 * return 1 if client_id1 is closer. 295 * return 1 if pk1 is closer.
301 * return 2 if client_id2 is closer. 296 * return 2 if pk2 is closer.
302 */ 297 */
303int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2); 298int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2);
304 299
305/* Get the (maximum MAX_SENT_NODES) closest nodes to client_id we know 300/* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
306 * and put them in nodes_list (must be MAX_SENT_NODES big). 301 * and put them in nodes_list (must be MAX_SENT_NODES big).
307 * 302 *
308 * sa_family = family (IPv4 or IPv6) (0 if we don't care)? 303 * sa_family = family (IPv4 or IPv6) (0 if we don't care)?
@@ -312,7 +307,7 @@ int id_closest(const uint8_t *id, const uint8_t *id1, const uint8_t *id2);
312 * return the number of nodes returned. 307 * return the number of nodes returned.
313 * 308 *
314 */ 309 */
315int get_close_nodes(const DHT *dht, const uint8_t *client_id, Node_format *nodes_list, sa_family_t sa_family, 310int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, sa_family_t sa_family,
316 uint8_t is_LAN, uint8_t want_good); 311 uint8_t is_LAN, uint8_t want_good);
317 312
318 313
@@ -355,11 +350,11 @@ int DHT_connect_after_load(DHT *dht);
355 350
356/* ROUTING FUNCTIONS */ 351/* ROUTING FUNCTIONS */
357 352
358/* Send the given packet to node with client_id. 353/* Send the given packet to node with public_key.
359 * 354 *
360 * return -1 if failure. 355 * return -1 if failure.
361 */ 356 */
362int route_packet(const DHT *dht, const uint8_t *client_id, const uint8_t *packet, uint16_t length); 357int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length);
363 358
364/* Send the following packet to everyone who tells us they are connected to friend_id. 359/* Send the following packet to everyone who tells us they are connected to friend_id.
365 * 360 *
@@ -402,7 +397,7 @@ int DHT_isconnected(const DHT *dht);
402int DHT_non_lan_connected(const DHT *dht); 397int DHT_non_lan_connected(const DHT *dht);
403 398
404 399
405int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *client_id); 400int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key);
406 401
407#endif 402#endif
408 403
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 381ee737..4277f16a 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -381,6 +381,9 @@ int m_delfriend(Messenger *m, int32_t friendnumber)
381 if (friend_not_valid(m, friendnumber)) 381 if (friend_not_valid(m, friendnumber))
382 return -1; 382 return -1;
383 383
384 if (m->friend_connectionstatuschange_internal)
385 m->friend_connectionstatuschange_internal(m, friendnumber, 0, m->friend_connectionstatuschange_internal_userdata);
386
384 clear_receipts(m, friendnumber); 387 clear_receipts(m, friendnumber);
385 remove_request_received(&(m->fr), m->friendlist[friendnumber].real_pk); 388 remove_request_received(&(m->fr), m->friendlist[friendnumber].real_pk);
386 friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0); 389 friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0);
@@ -1329,9 +1332,6 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
1329 if (ft->status != FILESTATUS_TRANSFERRING) 1332 if (ft->status != FILESTATUS_TRANSFERRING)
1330 return -4; 1333 return -4;
1331 1334
1332 if (ft->paused != FILE_PAUSE_NOT)
1333 return -4;
1334
1335 if (length > MAX_FILE_DATA_SIZE) 1335 if (length > MAX_FILE_DATA_SIZE)
1336 return -5; 1336 return -5;
1337 1337
@@ -1343,7 +1343,7 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
1343 return -5; 1343 return -5;
1344 } 1344 }
1345 1345
1346 if (position != ft->transferred) { 1346 if (position != ft->transferred || (ft->requested <= position && ft->size != 0)) {
1347 return -7; 1347 return -7;
1348 } 1348 }
1349 1349
@@ -1469,11 +1469,12 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber)
1469 1469
1470 ++ft->slots_allocated; 1470 ++ft->slots_allocated;
1471 1471
1472 if (m->file_reqchunk) 1472 uint64_t position = ft->requested;
1473 (*m->file_reqchunk)(m, friendnumber, i, ft->requested, length, m->file_reqchunk_userdata);
1474
1475 ft->requested += length; 1473 ft->requested += length;
1476 1474
1475 if (m->file_reqchunk)
1476 (*m->file_reqchunk)(m, friendnumber, i, position, length, m->file_reqchunk_userdata);
1477
1477 --free_slots; 1478 --free_slots;
1478 1479
1479 } 1480 }
@@ -1805,24 +1806,23 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
1805 kill_onion(m->onion); 1806 kill_onion(m->onion);
1806 kill_onion_announce(m->onion_a); 1807 kill_onion_announce(m->onion_a);
1807 kill_onion_client(m->onion_c); 1808 kill_onion_client(m->onion_c);
1808 kill_DHT(m->dht);
1809 kill_net_crypto(m->net_crypto); 1809 kill_net_crypto(m->net_crypto);
1810 kill_DHT(m->dht);
1810 kill_networking(m->net); 1811 kill_networking(m->net);
1811 free(m); 1812 free(m);
1812 return NULL; 1813 return NULL;
1813 } 1814 }
1814 1815
1815 if (options->tcp_server_port) { 1816 if (options->tcp_server_port) {
1816 m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_public_key, 1817 m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_secret_key, m->onion);
1817 m->dht->self_secret_key, m->onion);
1818 1818
1819 if (m->tcp_server == NULL) { 1819 if (m->tcp_server == NULL) {
1820 kill_friend_connections(m->fr_c); 1820 kill_friend_connections(m->fr_c);
1821 kill_onion(m->onion); 1821 kill_onion(m->onion);
1822 kill_onion_announce(m->onion_a); 1822 kill_onion_announce(m->onion_a);
1823 kill_onion_client(m->onion_c); 1823 kill_onion_client(m->onion_c);
1824 kill_DHT(m->dht);
1825 kill_net_crypto(m->net_crypto); 1824 kill_net_crypto(m->net_crypto);
1825 kill_DHT(m->dht);
1826 kill_networking(m->net); 1826 kill_networking(m->net);
1827 free(m); 1827 free(m);
1828 1828
@@ -2340,7 +2340,7 @@ void do_messenger(Messenger *m)
2340 2340
2341 LOGGER_TRACE("C[%2u] %s:%u [%3u] %s", 2341 LOGGER_TRACE("C[%2u] %s:%u [%3u] %s",
2342 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), 2342 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
2343 last_pinged, ID2String(cptr->client_id)); 2343 last_pinged, ID2String(cptr->public_key));
2344 } 2344 }
2345 } 2345 }
2346 2346
@@ -2360,7 +2360,7 @@ void do_messenger(Messenger *m)
2360 continue; 2360 continue;
2361 2361
2362 for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++) 2362 for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++)
2363 if (id_equal(m->friendlist[friend].real_pk, m->dht->friends_list[dhtfriend].client_id)) { 2363 if (id_equal(m->friendlist[friend].real_pk, m->dht->friends_list[dhtfriend].public_key)) {
2364 m2dht[friend] = dhtfriend; 2364 m2dht[friend] = dhtfriend;
2365 break; 2365 break;
2366 } 2366 }
@@ -2390,7 +2390,7 @@ void do_messenger(Messenger *m)
2390 dht2m[friend], friend, msgfptr->name, 2390 dht2m[friend], friend, msgfptr->name,
2391 ID2String(msgfptr->real_pk)); 2391 ID2String(msgfptr->real_pk));
2392 } else { 2392 } else {
2393 LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); 2393 LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->public_key));
2394 } 2394 }
2395 2395
2396 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { 2396 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
@@ -2408,7 +2408,7 @@ void do_messenger(Messenger *m)
2408 LOGGER_TRACE("F[%2u] => C[%2u] %s:%u [%3u] %s", 2408 LOGGER_TRACE("F[%2u] => C[%2u] %s:%u [%3u] %s",
2409 friend, client, ip_ntoa(&assoc->ip_port.ip), 2409 friend, client, ip_ntoa(&assoc->ip_port.ip),
2410 ntohs(assoc->ip_port.port), last_pinged, 2410 ntohs(assoc->ip_port.port), last_pinged,
2411 ID2String(cptr->client_id)); 2411 ID2String(cptr->public_key));
2412 } 2412 }
2413 } 2413 }
2414 } 2414 }
@@ -2430,6 +2430,7 @@ void do_messenger(Messenger *m)
2430#define MESSENGER_STATE_TYPE_STATUS 6 2430#define MESSENGER_STATE_TYPE_STATUS 6
2431#define MESSENGER_STATE_TYPE_TCP_RELAY 10 2431#define MESSENGER_STATE_TYPE_TCP_RELAY 10
2432#define MESSENGER_STATE_TYPE_PATH_NODE 11 2432#define MESSENGER_STATE_TYPE_PATH_NODE 11
2433#define MESSENGER_STATE_TYPE_END 255
2433 2434
2434#define SAVED_FRIEND_REQUEST_SIZE 1024 2435#define SAVED_FRIEND_REQUEST_SIZE 1024
2435#define NUM_SAVED_PATH_NODES 8 2436#define NUM_SAVED_PATH_NODES 8
@@ -2545,9 +2546,9 @@ uint32_t messenger_size(const Messenger *m)
2545 + sizesubhead + m->name_length // Own nickname. 2546 + sizesubhead + m->name_length // Own nickname.
2546 + sizesubhead + m->statusmessage_length // status message 2547 + sizesubhead + m->statusmessage_length // status message
2547 + sizesubhead + 1 // status 2548 + sizesubhead + 1 // status
2548 + sizesubhead + NUM_SAVED_TCP_RELAYS * sizeof(Node_format) //TCP relays 2549 + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(TCP_INET6) //TCP relays
2549 + sizesubhead + NUM_SAVED_PATH_NODES * sizeof(Node_format) //saved path nodes 2550 + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(TCP_INET6) //saved path nodes
2550 ; 2551 + sizesubhead;
2551} 2552}
2552 2553
2553static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type) 2554static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
@@ -2562,14 +2563,16 @@ static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t typ
2562/* Save the messenger in data of size Messenger_size(). */ 2563/* Save the messenger in data of size Messenger_size(). */
2563void messenger_save(const Messenger *m, uint8_t *data) 2564void messenger_save(const Messenger *m, uint8_t *data)
2564{ 2565{
2566 memset(data, 0, messenger_size(m));
2567
2565 uint32_t len; 2568 uint32_t len;
2566 uint16_t type; 2569 uint16_t type;
2567 uint32_t *data32, size32 = sizeof(uint32_t); 2570 uint32_t size32 = sizeof(uint32_t);
2568 2571
2569 data32 = (uint32_t *)data; 2572 memset(data, 0, size32);
2570 data32[0] = 0; 2573 data += size32;
2571 data32[1] = MESSENGER_STATE_COOKIE_GLOBAL; 2574 host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL);
2572 data += size32 * 2; 2575 data += size32;
2573 2576
2574#ifdef DEBUG 2577#ifdef DEBUG
2575 assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t)); 2578 assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t));
@@ -2581,12 +2584,6 @@ void messenger_save(const Messenger *m, uint8_t *data)
2581 save_keys(m->net_crypto, data + size32); 2584 save_keys(m->net_crypto, data + size32);
2582 data += len; 2585 data += len;
2583 2586
2584 len = DHT_size(m->dht);
2585 type = MESSENGER_STATE_TYPE_DHT;
2586 data = z_state_save_subheader(data, len, type);
2587 DHT_save(m->dht, data);
2588 data += len;
2589
2590 len = saved_friendslist_size(m); 2587 len = saved_friendslist_size(m);
2591 type = MESSENGER_STATE_TYPE_FRIENDS; 2588 type = MESSENGER_STATE_TYPE_FRIENDS;
2592 data = z_state_save_subheader(data, len, type); 2589 data = z_state_save_subheader(data, len, type);
@@ -2611,22 +2608,40 @@ void messenger_save(const Messenger *m, uint8_t *data)
2611 *data = m->userstatus; 2608 *data = m->userstatus;
2612 data += len; 2609 data += len;
2613 2610
2614 Node_format relays[NUM_SAVED_TCP_RELAYS]; 2611 len = DHT_size(m->dht);
2615 len = sizeof(relays); 2612 type = MESSENGER_STATE_TYPE_DHT;
2616 type = MESSENGER_STATE_TYPE_TCP_RELAY;
2617 data = z_state_save_subheader(data, len, type); 2613 data = z_state_save_subheader(data, len, type);
2618 memset(relays, 0, len); 2614 DHT_save(m->dht, data);
2619 copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS);
2620 memcpy(data, relays, len);
2621 data += len; 2615 data += len;
2622 2616
2617 Node_format relays[NUM_SAVED_TCP_RELAYS];
2618 type = MESSENGER_STATE_TYPE_TCP_RELAY;
2619 uint8_t *temp_data = data;
2620 data = z_state_save_subheader(temp_data, 0, type);
2621 unsigned int num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS);
2622 int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(TCP_INET6), relays, num);
2623
2624 if (l > 0) {
2625 len = l;
2626 data = z_state_save_subheader(temp_data, len, type);
2627 data += len;
2628 }
2629
2623 Node_format nodes[NUM_SAVED_PATH_NODES]; 2630 Node_format nodes[NUM_SAVED_PATH_NODES];
2624 len = sizeof(nodes);
2625 type = MESSENGER_STATE_TYPE_PATH_NODE; 2631 type = MESSENGER_STATE_TYPE_PATH_NODE;
2626 data = z_state_save_subheader(data, len, type); 2632 temp_data = data;
2627 memset(nodes, 0, len); 2633 data = z_state_save_subheader(data, 0, type);
2628 onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES); 2634 memset(nodes, 0, sizeof(nodes));
2629 memcpy(data, nodes, len); 2635 num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
2636 l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(TCP_INET6), nodes, num);
2637
2638 if (l > 0) {
2639 len = l;
2640 data = z_state_save_subheader(temp_data, len, type);
2641 data += len;
2642 }
2643
2644 z_state_save_subheader(data, 0, MESSENGER_STATE_TYPE_END);
2630} 2645}
2631 2646
2632static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) 2647static int messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type)
@@ -2677,11 +2692,11 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
2677 break; 2692 break;
2678 2693
2679 case MESSENGER_STATE_TYPE_TCP_RELAY: { 2694 case MESSENGER_STATE_TYPE_TCP_RELAY: {
2680 if (length != sizeof(m->loaded_relays)) { 2695 if (length == 0) {
2681 return -1; 2696 break;
2682 } 2697 }
2683 2698
2684 memcpy(m->loaded_relays, data, length); 2699 unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, 0, data, length, 1);
2685 m->has_added_relays = 0; 2700 m->has_added_relays = 0;
2686 2701
2687 break; 2702 break;
@@ -2690,20 +2705,28 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
2690 case MESSENGER_STATE_TYPE_PATH_NODE: { 2705 case MESSENGER_STATE_TYPE_PATH_NODE: {
2691 Node_format nodes[NUM_SAVED_PATH_NODES]; 2706 Node_format nodes[NUM_SAVED_PATH_NODES];
2692 2707
2693 if (length != sizeof(nodes)) { 2708 if (length == 0) {
2694 return -1; 2709 break;
2695 } 2710 }
2696 2711
2697 memcpy(nodes, data, length); 2712 int i, num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, 0, data, length, 0);
2698 uint32_t i;
2699 2713
2700 for (i = 0; i < NUM_SAVED_PATH_NODES; ++i) { 2714 for (i = 0; i < num; ++i) {
2701 onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].public_key); 2715 onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].public_key);
2702 } 2716 }
2703 2717
2704 break; 2718 break;
2705 } 2719 }
2706 2720
2721 case MESSENGER_STATE_TYPE_END: {
2722 if (length != 0) {
2723 return -1;
2724 }
2725
2726 return -2;
2727 break;
2728 }
2729
2707#ifdef DEBUG 2730#ifdef DEBUG
2708 2731
2709 default: 2732 default:
@@ -2725,7 +2748,8 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
2725 if (length < cookie_len) 2748 if (length < cookie_len)
2726 return -1; 2749 return -1;
2727 2750
2728 memcpy(data32, data, sizeof(data32)); 2751 memcpy(data32, data, sizeof(uint32_t));
2752 lendian_to_host32(data32 + 1, data + sizeof(uint32_t));
2729 2753
2730 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) 2754 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL))
2731 return load_state(messenger_load_state_callback, m, data + cookie_len, 2755 return load_state(messenger_load_state_callback, m, data + cookie_len,
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index d26a60de..5f38b5dd 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -423,6 +423,8 @@ static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *rela
423 423
424/* Create a new TCP connection to public_key. 424/* Create a new TCP connection to public_key.
425 * 425 *
426 * public_key must be the counterpart to the secret key that the other peer used with new_tcp_connections().
427 *
426 * id is the id in the callbacks for that connection. 428 * id is the id in the callbacks for that connection.
427 * 429 *
428 * return connections_number on success. 430 * return connections_number on success.
@@ -1006,7 +1008,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
1006 return 0; 1008 return 0;
1007} 1009}
1008 1010
1009static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk) 1011static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk)
1010{ 1012{
1011 if (ip_port.ip.family == TCP_INET) { 1013 if (ip_port.ip.family == TCP_INET) {
1012 ip_port.ip.family = AF_INET; 1014 ip_port.ip.family = AF_INET;
@@ -1036,7 +1038,7 @@ static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
1036 return tcp_connections_number; 1038 return tcp_connections_number;
1037} 1039}
1038 1040
1039/* Add a TCP relay to the instance. 1041/* Add a TCP relay to the TCP_Connections instance.
1040 * 1042 *
1041 * return 0 on success. 1043 * return 0 on success.
1042 * return -1 on failure. 1044 * return -1 on failure.
@@ -1048,7 +1050,7 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
1048 if (tcp_connections_number != -1) 1050 if (tcp_connections_number != -1)
1049 return -1; 1051 return -1;
1050 1052
1051 if (add_tcp_relay(tcp_c, ip_port, relay_pk) == -1) 1053 if (add_tcp_relay_instance(tcp_c, ip_port, relay_pk) == -1)
1052 return -1; 1054 return -1;
1053 1055
1054 return 0; 1056 return 0;
@@ -1089,6 +1091,8 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb
1089 1091
1090/* Add a TCP relay tied to a connection. 1092/* Add a TCP relay tied to a connection.
1091 * 1093 *
1094 * This should be called with the same relay by two peers who want to create a TCP connection with each other.
1095 *
1092 * return 0 on success. 1096 * return 0 on success.
1093 * return -1 on failure. 1097 * return -1 on failure.
1094 */ 1098 */
@@ -1108,7 +1112,7 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
1108 return -1; 1112 return -1;
1109 } 1113 }
1110 1114
1111 int tcp_connections_number = add_tcp_relay(tcp_c, ip_port, relay_pk); 1115 int tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk);
1112 1116
1113 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1117 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
1114 1118
@@ -1237,6 +1241,13 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status)
1237 return 0; 1241 return 0;
1238} 1242}
1239 1243
1244/* Returns a new TCP_Connections object associated with the secret_key.
1245 *
1246 * In order for others to connect to this instance new_tcp_connection_to() must be called with the
1247 * public_key associated with secret_key.
1248 *
1249 * Returns NULL on failure.
1250 */
1240TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info) 1251TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info)
1241{ 1252{
1242 if (secret_key == NULL) 1253 if (secret_key == NULL)
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h
index 140d6de3..7bc34f86 100644
--- a/toxcore/TCP_connection.h
+++ b/toxcore/TCP_connection.h
@@ -165,6 +165,8 @@ void set_oob_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_oo
165 165
166/* Create a new TCP connection to public_key. 166/* Create a new TCP connection to public_key.
167 * 167 *
168 * public_key must be the counterpart to the secret key that the other peer used with new_tcp_connections().
169 *
168 * id is the id in the callbacks for that connection. 170 * id is the id in the callbacks for that connection.
169 * 171 *
170 * return connections_number on success. 172 * return connections_number on success.
@@ -206,6 +208,8 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb
206 208
207/* Add a TCP relay tied to a connection. 209/* Add a TCP relay tied to a connection.
208 * 210 *
211 * This should be called with the same relay by two peers who want to create a TCP connection with each other.
212 *
209 * return 0 on success. 213 * return 0 on success.
210 * return -1 on failure. 214 * return -1 on failure.
211 */ 215 */
@@ -226,7 +230,15 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
226 */ 230 */
227unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num); 231unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num);
228 232
233/* Returns a new TCP_Connections object associated with the secret_key.
234 *
235 * In order for others to connect to this instance new_tcp_connection_to() must be called with the
236 * public_key associated with secret_key.
237 *
238 * Returns NULL on failure.
239 */
229TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info); 240TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info);
241
230void do_tcp_connections(TCP_Connections *tcp_c); 242void do_tcp_connections(TCP_Connections *tcp_c);
231void kill_tcp_connections(TCP_Connections *tcp_c); 243void kill_tcp_connections(TCP_Connections *tcp_c);
232 244
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index f07df2c6..cf997d34 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -939,8 +939,8 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port)
939 return sock; 939 return sock;
940} 940}
941 941
942TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *public_key, 942TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
943 const uint8_t *secret_key, Onion *onion) 943 Onion *onion)
944{ 944{
945 if (num_sockets == 0 || ports == NULL) 945 if (num_sockets == 0 || ports == NULL)
946 return NULL; 946 return NULL;
@@ -1015,8 +1015,8 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1015 set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp); 1015 set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp);
1016 } 1016 }
1017 1017
1018 memcpy(temp->public_key, public_key, crypto_box_PUBLICKEYBYTES);
1019 memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES); 1018 memcpy(temp->secret_key, secret_key, crypto_box_SECRETKEYBYTES);
1019 crypto_scalarmult_curve25519_base(temp->public_key, temp->secret_key);
1020 1020
1021 bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES, 8); 1021 bs_list_init(&temp->accepted_key_list, crypto_box_PUBLICKEYBYTES, 8);
1022 1022
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index ba3e7308..3f6b18ae 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -143,8 +143,8 @@ typedef struct {
143 143
144/* Create new TCP server instance. 144/* Create new TCP server instance.
145 */ 145 */
146TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *public_key, 146TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
147 const uint8_t *secret_key, Onion *onion); 147 Onion *onion);
148 148
149/* Run the TCP_server 149/* Run the TCP_server
150 */ 150 */
diff --git a/toxcore/assoc.c b/toxcore/assoc.c
index 2e03dabe..44c4cc30 100644
--- a/toxcore/assoc.c
+++ b/toxcore/assoc.c
@@ -87,7 +87,7 @@ typedef struct candidates_bucket {
87 87
88struct Assoc { 88struct Assoc {
89 hash_t self_hash; /* hash of self_client_id */ 89 hash_t self_hash; /* hash of self_client_id */
90 uint8_t self_client_id[CLIENT_ID_SIZE]; /* don't store entries for this */ 90 uint8_t self_client_id[crypto_box_PUBLICKEYBYTES]; /* don't store entries for this */
91 91
92 /* association centralization: clients not in use */ 92 /* association centralization: clients not in use */
93 size_t candidates_bucket_bits; 93 size_t candidates_bucket_bits;
@@ -101,7 +101,7 @@ struct Assoc {
101/* HELPER FUNCTIONS */ 101/* HELPER FUNCTIONS */
102/*****************************************************************************/ 102/*****************************************************************************/
103 103
104/* the complete distance would be CLIENT_ID_SIZE long... 104/* the complete distance would be crypto_box_PUBLICKEYBYTES long...
105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */ 105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */
106static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test) 106static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test)
107{ 107{
@@ -157,13 +157,13 @@ static Client_entry *dist_index_entry(Assoc *assoc, uint64_t dist_ind)
157 return NULL; 157 return NULL;
158} 158}
159 159
160/* get actual entry's client_id to a distance_index */ 160/* get actual entry's public_key to a distance_index */
161static uint8_t *dist_index_id(Assoc *assoc, uint64_t dist_ind) 161static uint8_t *dist_index_id(Assoc *assoc, uint64_t dist_ind)
162{ 162{
163 Client_entry *entry = dist_index_entry(assoc, dist_ind); 163 Client_entry *entry = dist_index_entry(assoc, dist_ind);
164 164
165 if (entry) 165 if (entry)
166 return entry->client.client_id; 166 return entry->client.public_key;
167 167
168 return NULL; 168 return NULL;
169} 169}
@@ -200,7 +200,7 @@ static hash_t id_hash(const Assoc *assoc, const uint8_t *id)
200{ 200{
201 uint32_t i, res = 0x19a64e82; 201 uint32_t i, res = 0x19a64e82;
202 202
203 for (i = 0; i < CLIENT_ID_SIZE; i++) 203 for (i = 0; i < crypto_box_PUBLICKEYBYTES; i++)
204 res = ((res << 1) ^ id[i]) + (res >> 31); 204 res = ((res << 1) ^ id[i]) + (res >> 31);
205 205
206 /* can't have zero as hash, a) marks an unused spot, 206 /* can't have zero as hash, a) marks an unused spot,
@@ -356,7 +356,7 @@ static uint8_t candidates_search(const Assoc *assoc, const uint8_t *id, hash_t h
356 Client_entry *entry = &cnd_bckt->list[pos]; 356 Client_entry *entry = &cnd_bckt->list[pos];
357 357
358 if (entry->hash == hash) 358 if (entry->hash == hash)
359 if (id_equal(entry->client.client_id, id)) { 359 if (id_equal(entry->client.public_key, id)) {
360 *entryptr = entry; 360 *entryptr = entry;
361 return 1; 361 return 1;
362 } 362 }
@@ -476,7 +476,7 @@ static uint8_t candidates_create_new(const Assoc *assoc, hash_t hash, const uint
476 return 0; 476 return 0;
477 477
478 entry->hash = hash; 478 entry->hash = hash;
479 id_copy(entry->client.client_id, id); 479 id_copy(entry->client.public_key, id);
480 480
481 if (used) 481 if (used)
482 entry->used_at = unix_time(); 482 entry->used_at = unix_time();
@@ -536,7 +536,7 @@ static void client_id_self_update(Assoc *assoc)
536 Client_entry *entry = &cnd_bckt->list[pos]; 536 Client_entry *entry = &cnd_bckt->list[pos];
537 537
538 if (entry->hash == assoc->self_hash) 538 if (entry->hash == assoc->self_hash)
539 if (id_equal(entry->client.client_id, assoc->self_client_id)) 539 if (id_equal(entry->client.public_key, assoc->self_client_id))
540 entry->hash = 0; 540 entry->hash = 0;
541 } 541 }
542} 542}
@@ -648,7 +648,7 @@ uint8_t Assoc_get_close_entries(Assoc *assoc, Assoc_close_entries *state)
648 continue; 648 continue;
649 } 649 }
650 650
651 uint64_t dist = state->distance_absolute_func(assoc, state->custom_data, state->wanted_id, entry->client.client_id); 651 uint64_t dist = state->distance_absolute_func(assoc, state->custom_data, state->wanted_id, entry->client.public_key);
652 uint32_t index = b * assoc->candidates_bucket_size + i; 652 uint32_t index = b * assoc->candidates_bucket_size + i;
653 dist_list[index] = (dist << DISTANCE_INDEX_INDEX_BITS) | index; 653 dist_list[index] = (dist << DISTANCE_INDEX_INDEX_BITS) | index;
654 } 654 }
@@ -914,7 +914,7 @@ void do_Assoc(Assoc *assoc, DHT *dht)
914 continue; 914 continue;
915 915
916 if (!target_id) 916 if (!target_id)
917 target_id = entry->client.client_id; 917 target_id = entry->client.public_key;
918 918
919 if (entry->seen_at) { 919 if (entry->seen_at) {
920 if (!seen) 920 if (!seen)
@@ -940,9 +940,9 @@ void do_Assoc(Assoc *assoc, DHT *dht)
940 IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6; 940 IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6;
941 941
942 LOGGER_DEBUG("[%u] => S[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), 942 LOGGER_DEBUG("[%u] => S[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count),
943 idpart2str(seen->client.client_id, 8), ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port)); 943 idpart2str(seen->client.public_key, 8), ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port));
944 944
945 DHT_getnodes(dht, &ippts->ip_port, seen->client.client_id, target_id); 945 DHT_getnodes(dht, &ippts->ip_port, seen->client.public_key, target_id);
946 seen->getnodes = unix_time(); 946 seen->getnodes = unix_time();
947 } 947 }
948 948
@@ -950,9 +950,9 @@ void do_Assoc(Assoc *assoc, DHT *dht)
950 IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6; 950 IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6;
951 951
952 LOGGER_DEBUG("[%u] => H[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), 952 LOGGER_DEBUG("[%u] => H[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count),
953 idpart2str(heard->client.client_id, 8), ip_ntoa(&ipp->ip), htons(ipp->port)); 953 idpart2str(heard->client.public_key, 8), ip_ntoa(&ipp->ip), htons(ipp->port));
954 954
955 DHT_getnodes(dht, ipp, heard->client.client_id, target_id); 955 DHT_getnodes(dht, ipp, heard->client.public_key, target_id);
956 heard->getnodes = unix_time(); 956 heard->getnodes = unix_time();
957 } 957 }
958 958
@@ -976,11 +976,11 @@ void kill_Assoc(Assoc *assoc)
976 976
977#ifdef LOGGING 977#ifdef LOGGING
978 978
979static char buffer[CLIENT_ID_SIZE * 2 + 1]; 979static char buffer[crypto_box_PUBLICKEYBYTES * 2 + 1];
980static char *idpart2str(uint8_t *id, size_t len) 980static char *idpart2str(uint8_t *id, size_t len)
981{ 981{
982 if (len > CLIENT_ID_SIZE) 982 if (len > crypto_box_PUBLICKEYBYTES)
983 len = CLIENT_ID_SIZE; 983 len = crypto_box_PUBLICKEYBYTES;
984 984
985 size_t i; 985 size_t i;
986 986
@@ -1012,7 +1012,7 @@ void Assoc_status(const Assoc *assoc)
1012 total++; 1012 total++;
1013 1013
1014 LOGGER_TRACE("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", 1014 LOGGER_TRACE("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n",
1015 (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), 1015 (int)bid, (int)cid, entry->hash, idpart2str(entry->client.public_key, 8),
1016 entry->used_at ? (int)(unix_time() - entry->used_at) : 0, 1016 entry->used_at ? (int)(unix_time() - entry->used_at) : 0,
1017 entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, 1017 entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0,
1018 entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', 1018 entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?',
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index a826f77a..628666c0 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1,7 +1,6 @@
1/* net_crypto.c 1/* net_crypto.c
2 * 2 *
3 * Functions for the core network crypto. 3 * Functions for the core network crypto.
4 * See also: http://wiki.tox.im/index.php/DHT
5 * 4 *
6 * NOTE: This code has to be perfect. We don't mess around with encryption. 5 * NOTE: This code has to be perfect. We don't mess around with encryption.
7 * 6 *
@@ -700,13 +699,15 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
700 n = 0; 699 n = 0;
701 ++requested; 700 ++requested;
702 } else { 701 } else {
703 uint64_t sent_time = send_array->buffer[num]->sent_time; 702 if (send_array->buffer[num]) {
703 uint64_t sent_time = send_array->buffer[num]->sent_time;
704 704
705 if (l_sent_time < sent_time) 705 if (l_sent_time < sent_time)
706 l_sent_time = sent_time; 706 l_sent_time = sent_time;
707 707
708 free(send_array->buffer[num]); 708 free(send_array->buffer[num]);
709 send_array->buffer[num] = NULL; 709 send_array->buffer[num] = NULL;
710 }
710 } 711 }
711 712
712 if (n == 255) { 713 if (n == 255) {
@@ -2033,7 +2034,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2033#define REQUEST_PACKETS_COMPARE_CONSTANT (0.125 * 100.0) 2034#define REQUEST_PACKETS_COMPARE_CONSTANT (0.125 * 100.0)
2034 2035
2035/* Multiplier for maximum allowed resends. */ 2036/* Multiplier for maximum allowed resends. */
2036#define PACKET_RESEND_MULTIPLIER 3 2037#define PACKET_RESEND_MULTIPLIER 3.5
2037 2038
2038/* Timeout for increasing speed after congestion event (in ms). */ 2039/* Timeout for increasing speed after congestion event (in ms). */
2039#define CONGESTION_EVENT_TIMEOUT 2000 2040#define CONGESTION_EVENT_TIMEOUT 2000
@@ -2145,7 +2146,7 @@ static void send_crypto_packets(Net_Crypto *c)
2145 int ret = send_requested_packets(c, i, conn->packets_left * PACKET_RESEND_MULTIPLIER); 2146 int ret = send_requested_packets(c, i, conn->packets_left * PACKET_RESEND_MULTIPLIER);
2146 2147
2147 if (ret != -1) { 2148 if (ret != -1) {
2148 if (ret < conn->packets_left) { 2149 if ((unsigned int)ret < conn->packets_left) {
2149 conn->packets_left -= ret; 2150 conn->packets_left -= ret;
2150 } else { 2151 } else {
2151 conn->last_congestion_event = temp_time; 2152 conn->last_congestion_event = temp_time;
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 4f049fbc..8609c6b6 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -1452,12 +1452,15 @@ void do_onion_client(Onion_Client *onion_c)
1452 } 1452 }
1453 } 1453 }
1454 1454
1455 onion_c->UDP_connected = DHT_non_lan_connected(onion_c->dht); 1455 _Bool UDP_connected = DHT_non_lan_connected(onion_c->dht);
1456 1456
1457 if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS)) { 1457 if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS)) {
1458 set_tcp_onion_status(onion_c->c->tcp_c, !onion_c->UDP_connected); 1458 set_tcp_onion_status(onion_c->c->tcp_c, !UDP_connected);
1459 } 1459 }
1460 1460
1461 onion_c->UDP_connected = UDP_connected
1462 || get_random_tcp_onion_conn_number(onion_c->c->tcp_c) == -1; /* Check if connected to any TCP relays. */
1463
1461 if (onion_connection_status(onion_c)) { 1464 if (onion_connection_status(onion_c)) {
1462 for (i = 0; i < onion_c->num_friends; ++i) { 1465 for (i = 0; i < onion_c->num_friends; ++i) {
1463 do_friend(onion_c, i); 1466 do_friend(onion_c, i);
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 2decca0a..877aaca2 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -55,26 +55,26 @@ struct PING {
55 55
56 56
57#define PING_PLAIN_SIZE (1 + sizeof(uint64_t)) 57#define PING_PLAIN_SIZE (1 + sizeof(uint64_t))
58#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES) 58#define DHT_PING_SIZE (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + PING_PLAIN_SIZE + crypto_box_MACBYTES)
59#define PING_DATA_SIZE (CLIENT_ID_SIZE + sizeof(IP_Port)) 59#define PING_DATA_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(IP_Port))
60 60
61int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id) 61int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key)
62{ 62{
63 uint8_t pk[DHT_PING_SIZE]; 63 uint8_t pk[DHT_PING_SIZE];
64 int rc; 64 int rc;
65 uint64_t ping_id; 65 uint64_t ping_id;
66 66
67 if (id_equal(client_id, ping->dht->self_public_key)) 67 if (id_equal(public_key, ping->dht->self_public_key))
68 return 1; 68 return 1;
69 69
70 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 70 uint8_t shared_key[crypto_box_BEFORENMBYTES];
71 71
72 // generate key to encrypt ping_id with recipient privkey 72 // generate key to encrypt ping_id with recipient privkey
73 DHT_get_shared_key_sent(ping->dht, shared_key, client_id); 73 DHT_get_shared_key_sent(ping->dht, shared_key, public_key);
74 // Generate random ping_id. 74 // Generate random ping_id.
75 uint8_t data[PING_DATA_SIZE]; 75 uint8_t data[PING_DATA_SIZE];
76 id_copy(data, client_id); 76 id_copy(data, public_key);
77 memcpy(data + CLIENT_ID_SIZE, &ipp, sizeof(IP_Port)); 77 memcpy(data + crypto_box_PUBLICKEYBYTES, &ipp, sizeof(IP_Port));
78 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data)); 78 ping_id = ping_array_add(&ping->ping_array, data, sizeof(data));
79 79
80 if (ping_id == 0) 80 if (ping_id == 0)
@@ -86,13 +86,13 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id)
86 86
87 pk[0] = NET_PACKET_PING_REQUEST; 87 pk[0] = NET_PACKET_PING_REQUEST;
88 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 88 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
89 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce 89 new_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
90 90
91 91
92 rc = encrypt_data_symmetric(shared_key, 92 rc = encrypt_data_symmetric(shared_key,
93 pk + 1 + CLIENT_ID_SIZE, 93 pk + 1 + crypto_box_PUBLICKEYBYTES,
94 ping_plain, sizeof(ping_plain), 94 ping_plain, sizeof(ping_plain),
95 pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES); 95 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
96 96
97 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) 97 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES)
98 return 1; 98 return 1;
@@ -100,13 +100,13 @@ int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id)
100 return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); 100 return sendpacket(ping->dht->net, ipp, pk, sizeof(pk));
101} 101}
102 102
103static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *client_id, uint64_t ping_id, 103static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id,
104 uint8_t *shared_encryption_key) 104 uint8_t *shared_encryption_key)
105{ 105{
106 uint8_t pk[DHT_PING_SIZE]; 106 uint8_t pk[DHT_PING_SIZE];
107 int rc; 107 int rc;
108 108
109 if (id_equal(client_id, ping->dht->self_public_key)) 109 if (id_equal(public_key, ping->dht->self_public_key))
110 return 1; 110 return 1;
111 111
112 uint8_t ping_plain[PING_PLAIN_SIZE]; 112 uint8_t ping_plain[PING_PLAIN_SIZE];
@@ -115,13 +115,13 @@ static int send_ping_response(PING *ping, IP_Port ipp, const uint8_t *client_id,
115 115
116 pk[0] = NET_PACKET_PING_RESPONSE; 116 pk[0] = NET_PACKET_PING_RESPONSE;
117 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey 117 id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey
118 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce 118 new_nonce(pk + 1 + crypto_box_PUBLICKEYBYTES); // Generate new nonce
119 119
120 // Encrypt ping_id using recipient privkey 120 // Encrypt ping_id using recipient privkey
121 rc = encrypt_data_symmetric(shared_encryption_key, 121 rc = encrypt_data_symmetric(shared_encryption_key,
122 pk + 1 + CLIENT_ID_SIZE, 122 pk + 1 + crypto_box_PUBLICKEYBYTES,
123 ping_plain, sizeof(ping_plain), 123 ping_plain, sizeof(ping_plain),
124 pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES ); 124 pk + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES );
125 125
126 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES) 126 if (rc != PING_PLAIN_SIZE + crypto_box_MACBYTES)
127 return 1; 127 return 1;
@@ -148,8 +148,8 @@ static int handle_ping_request(void *_dht, IP_Port source, const uint8_t *packet
148 // Decrypt ping_id 148 // Decrypt ping_id
149 DHT_get_shared_key_recv(dht, shared_key, packet + 1); 149 DHT_get_shared_key_recv(dht, shared_key, packet + 1);
150 rc = decrypt_data_symmetric(shared_key, 150 rc = decrypt_data_symmetric(shared_key,
151 packet + 1 + CLIENT_ID_SIZE, 151 packet + 1 + crypto_box_PUBLICKEYBYTES,
152 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 152 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
153 PING_PLAIN_SIZE + crypto_box_MACBYTES, 153 PING_PLAIN_SIZE + crypto_box_MACBYTES,
154 ping_plain ); 154 ping_plain );
155 155
@@ -189,8 +189,8 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
189 uint8_t ping_plain[PING_PLAIN_SIZE]; 189 uint8_t ping_plain[PING_PLAIN_SIZE];
190 // Decrypt ping_id 190 // Decrypt ping_id
191 rc = decrypt_data_symmetric(shared_key, 191 rc = decrypt_data_symmetric(shared_key,
192 packet + 1 + CLIENT_ID_SIZE, 192 packet + 1 + crypto_box_PUBLICKEYBYTES,
193 packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, 193 packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
194 PING_PLAIN_SIZE + crypto_box_MACBYTES, 194 PING_PLAIN_SIZE + crypto_box_MACBYTES,
195 ping_plain); 195 ping_plain);
196 196
@@ -211,7 +211,7 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
211 return 1; 211 return 1;
212 212
213 IP_Port ipp; 213 IP_Port ipp;
214 memcpy(&ipp, data + CLIENT_ID_SIZE, sizeof(IP_Port)); 214 memcpy(&ipp, data + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
215 215
216 if (!ipport_equal(&ipp, &source)) 216 if (!ipport_equal(&ipp, &source))
217 return 1; 217 return 1;
@@ -220,17 +220,17 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
220 return 0; 220 return 0;
221} 221}
222 222
223/* Check if client_id with ip_port is in the list. 223/* Check if public_key with ip_port is in the list.
224 * 224 *
225 * return 1 if it is. 225 * return 1 if it is.
226 * return 0 if it isn't. 226 * return 0 if it isn't.
227 */ 227 */
228static int in_list(const Client_data *list, uint16_t length, const uint8_t *client_id, IP_Port ip_port) 228static int in_list(const Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port)
229{ 229{
230 uint32_t i; 230 uint32_t i;
231 231
232 for (i = 0; i < length; ++i) { 232 for (i = 0; i < length; ++i) {
233 if (id_equal(list[i].client_id, client_id)) { 233 if (id_equal(list[i].public_key, public_key)) {
234 const IPPTsPng *ipptp; 234 const IPPTsPng *ipptp;
235 235
236 if (ip_port.ip.family == AF_INET) { 236 if (ip_port.ip.family == AF_INET) {
@@ -250,31 +250,31 @@ static int in_list(const Client_data *list, uint16_t length, const uint8_t *clie
250/* Add nodes to the to_ping list. 250/* Add nodes to the to_ping list.
251 * All nodes in this list are pinged every TIME_TO_PING seconds 251 * All nodes in this list are pinged every TIME_TO_PING seconds
252 * and are then removed from the list. 252 * and are then removed from the list.
253 * If the list is full the nodes farthest from our client_id are replaced. 253 * If the list is full the nodes farthest from our public_key are replaced.
254 * The purpose of this list is to enable quick integration of new nodes into the 254 * The purpose of this list is to enable quick integration of new nodes into the
255 * network while preventing amplification attacks. 255 * network while preventing amplification attacks.
256 * 256 *
257 * return 0 if node was added. 257 * return 0 if node was added.
258 * return -1 if node was not added. 258 * return -1 if node was not added.
259 */ 259 */
260int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port) 260int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
261{ 261{
262 if (!ip_isset(&ip_port.ip)) 262 if (!ip_isset(&ip_port.ip))
263 return -1; 263 return -1;
264 264
265 if (in_list(ping->dht->close_clientlist, LCLIENT_LIST, client_id, ip_port)) 265 if (in_list(ping->dht->close_clientlist, LCLIENT_LIST, public_key, ip_port))
266 return -1; 266 return -1;
267 267
268 uint32_t i; 268 uint32_t i;
269 269
270 for (i = 0; i < MAX_TO_PING; ++i) { 270 for (i = 0; i < MAX_TO_PING; ++i) {
271 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { 271 if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
272 memcpy(ping->to_ping[i].public_key, client_id, CLIENT_ID_SIZE); 272 memcpy(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
273 ipport_copy(&ping->to_ping[i].ip_port, &ip_port); 273 ipport_copy(&ping->to_ping[i].ip_port, &ip_port);
274 return 0; 274 return 0;
275 } 275 }
276 276
277 if (memcmp(ping->to_ping[i].public_key, client_id, CLIENT_ID_SIZE) == 0) { 277 if (memcmp(ping->to_ping[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
278 return -1; 278 return -1;
279 } 279 }
280 } 280 }
@@ -282,8 +282,8 @@ int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port)
282 uint32_t r = rand(); 282 uint32_t r = rand();
283 283
284 for (i = 0; i < MAX_TO_PING; ++i) { 284 for (i = 0; i < MAX_TO_PING; ++i) {
285 if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, client_id) == 2) { 285 if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key) == 2) {
286 memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, client_id, CLIENT_ID_SIZE); 286 memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key, crypto_box_PUBLICKEYBYTES);
287 ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port); 287 ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port);
288 return 0; 288 return 0;
289 } 289 }
diff --git a/toxcore/ping.h b/toxcore/ping.h
index c19c912a..904ad844 100644
--- a/toxcore/ping.h
+++ b/toxcore/ping.h
@@ -29,19 +29,19 @@ typedef struct PING PING;
29/* Add nodes to the to_ping list. 29/* Add nodes to the to_ping list.
30 * All nodes in this list are pinged every TIME_TOPING seconds 30 * All nodes in this list are pinged every TIME_TOPING seconds
31 * and are then removed from the list. 31 * and are then removed from the list.
32 * If the list is full the nodes farthest from our client_id are replaced. 32 * If the list is full the nodes farthest from our public_key are replaced.
33 * The purpose of this list is to enable quick integration of new nodes into the 33 * The purpose of this list is to enable quick integration of new nodes into the
34 * network while preventing amplification attacks. 34 * network while preventing amplification attacks.
35 * 35 *
36 * return 0 if node was added. 36 * return 0 if node was added.
37 * return -1 if node was not added. 37 * return -1 if node was not added.
38 */ 38 */
39int add_to_ping(PING *ping, const uint8_t *client_id, IP_Port ip_port); 39int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port);
40void do_to_ping(PING *ping); 40void do_to_ping(PING *ping);
41 41
42PING *new_ping(DHT *dht); 42PING *new_ping(DHT *dht);
43void kill_ping(PING *ping); 43void kill_ping(PING *ping);
44 44
45int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *client_id); 45int send_ping_request(PING *ping, IP_Port ipp, const uint8_t *public_key);
46 46
47#endif /* __PING_H__ */ 47#endif /* __PING_H__ */
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 3b2da2f7..1655d9c1 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -1283,7 +1283,7 @@ void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback, void *user
1283size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error); 1283size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error);
1284 1284
1285/** 1285/**
1286 * Write the name of the friend designated by the given friend number to a byte 1286 * Write the status message of the friend designated by the given friend number to a byte
1287 * array. 1287 * array.
1288 * 1288 *
1289 * Call tox_friend_get_status_message_size to determine the allocation size for the `status_name` 1289 * Call tox_friend_get_status_message_size to determine the allocation size for the `status_name`
@@ -1292,7 +1292,7 @@ size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number
1292 * The data written to `status_message` is equal to the data received by the last 1292 * The data written to `status_message` is equal to the data received by the last
1293 * `friend_status_message` callback. 1293 * `friend_status_message` callback.
1294 * 1294 *
1295 * @param name A valid memory region large enough to store the friend's name. 1295 * @param status_message A valid memory region large enough to store the friend's status message.
1296 */ 1296 */
1297bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8_t *status_message, 1297bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8_t *status_message,
1298 TOX_ERR_FRIEND_QUERY *error); 1298 TOX_ERR_FRIEND_QUERY *error);
diff --git a/toxcore/util.c b/toxcore/util.c
index d6db946d..5865a172 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -28,8 +28,8 @@
28 28
29#include <time.h> 29#include <time.h>
30 30
31/* for CLIENT_ID_SIZE */ 31/* for crypto_box_PUBLICKEYBYTES */
32#include "DHT.h" 32#include "crypto_core.h"
33 33
34#include "util.h" 34#include "util.h"
35 35
@@ -60,13 +60,13 @@ int is_timeout(uint64_t timestamp, uint64_t timeout)
60/* id functions */ 60/* id functions */
61bool id_equal(const uint8_t *dest, const uint8_t *src) 61bool id_equal(const uint8_t *dest, const uint8_t *src)
62{ 62{
63 return memcmp(dest, src, CLIENT_ID_SIZE) == 0; 63 return memcmp(dest, src, crypto_box_PUBLICKEYBYTES) == 0;
64} 64}
65 65
66uint32_t id_copy(uint8_t *dest, const uint8_t *src) 66uint32_t id_copy(uint8_t *dest, const uint8_t *src)
67{ 67{
68 memcpy(dest, src, CLIENT_ID_SIZE); 68 memcpy(dest, src, crypto_box_PUBLICKEYBYTES);
69 return CLIENT_ID_SIZE; 69 return crypto_box_PUBLICKEYBYTES;
70} 70}
71 71
72void host_to_net(uint8_t *num, uint16_t numbytes) 72void host_to_net(uint8_t *num, uint16_t numbytes)
@@ -153,8 +153,15 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
153 153
154 type = lendian_to_host16(cookie_type & 0xFFFF); 154 type = lendian_to_host16(cookie_type & 0xFFFF);
155 155
156 if (-1 == load_state_callback(outer, data, length_sub, type)) 156 int ret = load_state_callback(outer, data, length_sub, type);
157
158 if (ret == -1) {
157 return -1; 159 return -1;
160 }
161
162 /* -2 means end of save. */
163 if (ret == -2)
164 return 0;
158 165
159 data += length_sub; 166 data += length_sub;
160 length -= length_sub; 167 length -= length_sub;
@@ -257,4 +264,4 @@ void rb_free(RingBuffer *b)
257 free(b->data); 264 free(b->data);
258 free(b); 265 free(b);
259 } 266 }
260} \ No newline at end of file 267}
diff --git a/toxcore/util.h b/toxcore/util.h
index 6c3d3b38..7670a80f 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -48,6 +48,7 @@ uint16_t lendian_to_host16(uint16_t lendian);
48#define host_tolendian16(x) lendian_to_host16(x) 48#define host_tolendian16(x) lendian_to_host16(x)
49 49
50void host_to_lendian32(uint8_t *dest, uint32_t num); 50void host_to_lendian32(uint8_t *dest, uint32_t num);
51void lendian_to_host32(uint32_t *dest, const uint8_t *lendian);
51 52
52/* state load/save */ 53/* state load/save */
53typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type); 54typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type);