summaryrefslogtreecommitdiff
path: root/toxcore/onion_client.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 19:12:19 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-31 20:04:16 +0100
commit633da98ae69866efb195e00d9a3a22ace6bada66 (patch)
tree875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxcore/onion_client.c
parent6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff)
Add braces to all if statements.
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r--toxcore/onion_client.c351
1 files changed, 234 insertions, 117 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 717737ad..c6528c97 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -40,14 +40,16 @@
40 */ 40 */
41int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) 41int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key)
42{ 42{
43 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) 43 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) {
44 return -1; 44 return -1;
45 }
45 46
46 unsigned int i; 47 unsigned int i;
47 48
48 for (i = 0; i < MAX_PATH_NODES; ++i) { 49 for (i = 0; i < MAX_PATH_NODES; ++i) {
49 if (public_key_cmp(public_key, onion_c->path_nodes_bs[i].public_key) == 0) 50 if (public_key_cmp(public_key, onion_c->path_nodes_bs[i].public_key) == 0) {
50 return -1; 51 return -1;
52 }
51 } 53 }
52 54
53 onion_c->path_nodes_bs[onion_c->path_nodes_index_bs % MAX_PATH_NODES].ip_port = ip_port; 55 onion_c->path_nodes_bs[onion_c->path_nodes_index_bs % MAX_PATH_NODES].ip_port = ip_port;
@@ -57,8 +59,9 @@ int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t
57 uint16_t last = onion_c->path_nodes_index_bs; 59 uint16_t last = onion_c->path_nodes_index_bs;
58 ++onion_c->path_nodes_index_bs; 60 ++onion_c->path_nodes_index_bs;
59 61
60 if (onion_c->path_nodes_index_bs < last) 62 if (onion_c->path_nodes_index_bs < last) {
61 onion_c->path_nodes_index_bs = MAX_PATH_NODES + 1; 63 onion_c->path_nodes_index_bs = MAX_PATH_NODES + 1;
64 }
62 65
63 return 0; 66 return 0;
64} 67}
@@ -70,14 +73,16 @@ int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t
70 */ 73 */
71static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) 74static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key)
72{ 75{
73 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) 76 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) {
74 return -1; 77 return -1;
78 }
75 79
76 unsigned int i; 80 unsigned int i;
77 81
78 for (i = 0; i < MAX_PATH_NODES; ++i) { 82 for (i = 0; i < MAX_PATH_NODES; ++i) {
79 if (public_key_cmp(public_key, onion_c->path_nodes[i].public_key) == 0) 83 if (public_key_cmp(public_key, onion_c->path_nodes[i].public_key) == 0) {
80 return -1; 84 return -1;
85 }
81 } 86 }
82 87
83 onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES].ip_port = ip_port; 88 onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES].ip_port = ip_port;
@@ -87,8 +92,9 @@ static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uin
87 uint16_t last = onion_c->path_nodes_index; 92 uint16_t last = onion_c->path_nodes_index;
88 ++onion_c->path_nodes_index; 93 ++onion_c->path_nodes_index;
89 94
90 if (onion_c->path_nodes_index < last) 95 if (onion_c->path_nodes_index < last) {
91 onion_c->path_nodes_index = MAX_PATH_NODES + 1; 96 onion_c->path_nodes_index = MAX_PATH_NODES + 1;
97 }
92 98
93 return 0; 99 return 0;
94} 100}
@@ -101,16 +107,19 @@ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uin
101{ 107{
102 unsigned int i; 108 unsigned int i;
103 109
104 if (!max_num) 110 if (!max_num) {
105 return 0; 111 return 0;
112 }
106 113
107 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; 114 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
108 115
109 if (num_nodes == 0) 116 if (num_nodes == 0) {
110 return 0; 117 return 0;
118 }
111 119
112 if (num_nodes < max_num) 120 if (num_nodes < max_num) {
113 max_num = num_nodes; 121 max_num = num_nodes;
122 }
114 123
115 for (i = 0; i < max_num; ++i) { 124 for (i = 0; i < max_num; ++i) {
116 nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; 125 nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes];
@@ -127,15 +136,17 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
127{ 136{
128 unsigned int i; 137 unsigned int i;
129 138
130 if (!max_num) 139 if (!max_num) {
131 return 0; 140 return 0;
141 }
132 142
133 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; 143 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
134 144
135 //if (DHT_non_lan_connected(onion_c->dht)) { 145 //if (DHT_non_lan_connected(onion_c->dht)) {
136 if (DHT_isconnected(onion_c->dht)) { 146 if (DHT_isconnected(onion_c->dht)) {
137 if (num_nodes == 0) 147 if (num_nodes == 0) {
138 return 0; 148 return 0;
149 }
139 150
140 for (i = 0; i < max_num; ++i) { 151 for (i = 0; i < max_num; ++i) {
141 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 152 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
@@ -158,8 +169,9 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
158 unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : 169 unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs :
159 MAX_PATH_NODES; 170 MAX_PATH_NODES;
160 171
161 if (num_nodes_bs == 0) 172 if (num_nodes_bs == 0) {
162 return 0; 173 return 0;
174 }
163 175
164 nodes[0].ip_port.ip.family = TCP_FAMILY; 176 nodes[0].ip_port.ip.family = TCP_FAMILY;
165 nodes[0].ip_port.ip.ip4.uint32 = random_tcp; 177 nodes[0].ip_port.ip.ip4.uint32 = random_tcp;
@@ -229,14 +241,16 @@ static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_pa
229 if (path_timed_out(onion_paths, pathnum)) { 241 if (path_timed_out(onion_paths, pathnum)) {
230 Node_format nodes[ONION_PATH_LENGTH]; 242 Node_format nodes[ONION_PATH_LENGTH];
231 243
232 if (random_nodes_path_onion(onion_c, nodes, ONION_PATH_LENGTH) != ONION_PATH_LENGTH) 244 if (random_nodes_path_onion(onion_c, nodes, ONION_PATH_LENGTH) != ONION_PATH_LENGTH) {
233 return -1; 245 return -1;
246 }
234 247
235 int n = is_path_used(onion_paths, nodes); 248 int n = is_path_used(onion_paths, nodes);
236 249
237 if (n == -1) { 250 if (n == -1) {
238 if (create_onion_path(onion_c->dht, &onion_paths->paths[pathnum], nodes) == -1) 251 if (create_onion_path(onion_c->dht, &onion_paths->paths[pathnum], nodes) == -1) {
239 return -1; 252 return -1;
253 }
240 254
241 onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT; 255 onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT;
242 onion_paths->path_creation_time[pathnum] = unix_time(); 256 onion_paths->path_creation_time[pathnum] = unix_time();
@@ -262,8 +276,9 @@ static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_pa
262/* Does path with path_num exist. */ 276/* Does path with path_num exist. */
263static _Bool path_exists(Onion_Client_Paths *onion_paths, uint32_t path_num) 277static _Bool path_exists(Onion_Client_Paths *onion_paths, uint32_t path_num)
264{ 278{
265 if (path_timed_out(onion_paths, path_num)) 279 if (path_timed_out(onion_paths, path_num)) {
266 return 0; 280 return 0;
281 }
267 282
268 return onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num; 283 return onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num;
269} 284}
@@ -273,8 +288,9 @@ static _Bool path_exists(Onion_Client_Paths *onion_paths, uint32_t path_num)
273 */ 288 */
274static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t path_num) 289static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t path_num)
275{ 290{
276 if (num > onion_c->num_friends) 291 if (num > onion_c->num_friends) {
277 return -1; 292 return -1;
293 }
278 294
279 Onion_Client_Paths *onion_paths; 295 Onion_Client_Paths *onion_paths;
280 296
@@ -316,19 +332,22 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa
316 uint8_t packet[ONION_MAX_PACKET_SIZE]; 332 uint8_t packet[ONION_MAX_PACKET_SIZE];
317 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); 333 int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
318 334
319 if (len == -1) 335 if (len == -1) {
320 return -1; 336 return -1;
337 }
321 338
322 if (sendpacket(onion_c->net, path->ip_port1, packet, len) != len) 339 if (sendpacket(onion_c->net, path->ip_port1, packet, len) != len) {
323 return -1; 340 return -1;
341 }
324 342
325 return 0; 343 return 0;
326 } else if (path->ip_port1.ip.family == TCP_FAMILY) { 344 } else if (path->ip_port1.ip.family == TCP_FAMILY) {
327 uint8_t packet[ONION_MAX_PACKET_SIZE]; 345 uint8_t packet[ONION_MAX_PACKET_SIZE];
328 int len = create_onion_packet_tcp(packet, sizeof(packet), path, dest, data, length); 346 int len = create_onion_packet_tcp(packet, sizeof(packet), path, dest, data, length);
329 347
330 if (len == -1) 348 if (len == -1) {
331 return -1; 349 return -1;
350 }
332 351
333 return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len); 352 return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len);
334 } else { 353 } else {
@@ -361,8 +380,9 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *publ
361 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port), &path_num, sizeof(uint32_t)); 380 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port), &path_num, sizeof(uint32_t));
362 *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data)); 381 *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data));
363 382
364 if (*sendback == 0) 383 if (*sendback == 0) {
365 return -1; 384 return -1;
385 }
366 386
367 return 0; 387 return 0;
368} 388}
@@ -384,8 +404,9 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u
384 memcpy(&sback, sendback, sizeof(uint64_t)); 404 memcpy(&sback, sendback, sizeof(uint64_t));
385 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + sizeof(uint32_t)]; 405 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + sizeof(uint32_t)];
386 406
387 if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data)) 407 if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data)) {
388 return ~0; 408 return ~0;
409 }
389 410
390 memcpy(ret_pubkey, data + sizeof(uint32_t), crypto_box_PUBLICKEYBYTES); 411 memcpy(ret_pubkey, data + sizeof(uint32_t), crypto_box_PUBLICKEYBYTES);
391 memcpy(ret_ip_port, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port)); 412 memcpy(ret_ip_port, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
@@ -399,27 +420,32 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u
399static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_Port dest, const uint8_t *dest_pubkey, 420static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_Port dest, const uint8_t *dest_pubkey,
400 const uint8_t *ping_id, uint32_t pathnum) 421 const uint8_t *ping_id, uint32_t pathnum)
401{ 422{
402 if (num > onion_c->num_friends) 423 if (num > onion_c->num_friends) {
403 return -1; 424 return -1;
425 }
404 426
405 uint64_t sendback; 427 uint64_t sendback;
406 Onion_Path path; 428 Onion_Path path;
407 429
408 if (num == 0) { 430 if (num == 0) {
409 if (random_path(onion_c, &onion_c->onion_paths_self, pathnum, &path) == -1) 431 if (random_path(onion_c, &onion_c->onion_paths_self, pathnum, &path) == -1) {
410 return -1; 432 return -1;
433 }
411 } else { 434 } else {
412 if (random_path(onion_c, &onion_c->onion_paths_friends, pathnum, &path) == -1) 435 if (random_path(onion_c, &onion_c->onion_paths_friends, pathnum, &path) == -1) {
413 return -1; 436 return -1;
437 }
414 } 438 }
415 439
416 if (new_sendback(onion_c, num, dest_pubkey, dest, path.path_num, &sendback) == -1) 440 if (new_sendback(onion_c, num, dest_pubkey, dest, path.path_num, &sendback) == -1) {
417 return -1; 441 return -1;
442 }
418 443
419 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0}; 444 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0};
420 445
421 if (ping_id == NULL) 446 if (ping_id == NULL) {
422 ping_id = zero_ping_id; 447 ping_id = zero_ping_id;
448 }
423 449
424 uint8_t request[ONION_ANNOUNCE_REQUEST_SIZE]; 450 uint8_t request[ONION_ANNOUNCE_REQUEST_SIZE];
425 int len; 451 int len;
@@ -450,22 +476,27 @@ static int cmp_entry(const void *a, const void *b)
450 int t1 = is_timeout(entry1.timestamp, ONION_NODE_TIMEOUT); 476 int t1 = is_timeout(entry1.timestamp, ONION_NODE_TIMEOUT);
451 int t2 = is_timeout(entry2.timestamp, ONION_NODE_TIMEOUT); 477 int t2 = is_timeout(entry2.timestamp, ONION_NODE_TIMEOUT);
452 478
453 if (t1 && t2) 479 if (t1 && t2) {
454 return 0; 480 return 0;
481 }
455 482
456 if (t1) 483 if (t1) {
457 return -1; 484 return -1;
485 }
458 486
459 if (t2) 487 if (t2) {
460 return 1; 488 return 1;
489 }
461 490
462 int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); 491 int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key);
463 492
464 if (close == 1) 493 if (close == 1) {
465 return 1; 494 return 1;
495 }
466 496
467 if (close == 2) 497 if (close == 2) {
468 return -1; 498 return -1;
499 }
469 500
470 return 0; 501 return 0;
471} 502}
@@ -473,8 +504,9 @@ static int cmp_entry(const void *a, const void *b)
473static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, 504static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
474 uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num) 505 uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num)
475{ 506{
476 if (num > onion_c->num_friends) 507 if (num > onion_c->num_friends) {
477 return -1; 508 return -1;
509 }
478 510
479 Onion_Node *list_nodes = NULL; 511 Onion_Node *list_nodes = NULL;
480 uint8_t *reference_id = NULL; 512 uint8_t *reference_id = NULL;
@@ -490,8 +522,9 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
490 } 522 }
491 523
492 } else { 524 } else {
493 if (is_stored >= 2) 525 if (is_stored >= 2) {
494 return -1; 526 return -1;
527 }
495 528
496 list_nodes = onion_c->friends_list[num - 1].clients_list; 529 list_nodes = onion_c->friends_list[num - 1].clients_list;
497 reference_id = onion_c->friends_list[num - 1].real_public_key; 530 reference_id = onion_c->friends_list[num - 1].real_public_key;
@@ -517,8 +550,9 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
517 } 550 }
518 } 551 }
519 552
520 if (index == -1) 553 if (index == -1) {
521 return 0; 554 return 0;
555 }
522 556
523 memcpy(list_nodes[index].public_key, public_key, crypto_box_PUBLICKEYBYTES); 557 memcpy(list_nodes[index].public_key, public_key, crypto_box_PUBLICKEYBYTES);
524 list_nodes[index].ip_port = ip_port; 558 list_nodes[index].ip_port = ip_port;
@@ -535,8 +569,9 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
535 list_nodes[index].is_stored = is_stored; 569 list_nodes[index].is_stored = is_stored;
536 list_nodes[index].timestamp = unix_time(); 570 list_nodes[index].timestamp = unix_time();
537 571
538 if (!stored) 572 if (!stored) {
539 list_nodes[index].last_pinged = 0; 573 list_nodes[index].last_pinged = 0;
574 }
540 575
541 list_nodes[index].path_used = set_path_timeouts(onion_c, num, path_num); 576 list_nodes[index].path_used = set_path_timeouts(onion_c, num, path_num);
542 return 0; 577 return 0;
@@ -547,9 +582,11 @@ static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, co
547 unsigned int i; 582 unsigned int i;
548 583
549 for (i = 0; i < MAX_STORED_PINGED_NODES; ++i) { 584 for (i = 0; i < MAX_STORED_PINGED_NODES; ++i) {
550 if (!is_timeout(last_pinged[i].timestamp, MIN_NODE_PING_TIME)) 585 if (!is_timeout(last_pinged[i].timestamp, MIN_NODE_PING_TIME)) {
551 if (public_key_cmp(last_pinged[i].public_key, public_key) == 0) 586 if (public_key_cmp(last_pinged[i].public_key, public_key) == 0) {
552 return 0; 587 return 0;
588 }
589 }
553 } 590 }
554 591
555 memcpy(last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].public_key, public_key, crypto_box_PUBLICKEYBYTES); 592 memcpy(last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].public_key, public_key, crypto_box_PUBLICKEYBYTES);
@@ -561,11 +598,13 @@ static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, co
561static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_format *nodes, uint16_t num_nodes, 598static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_format *nodes, uint16_t num_nodes,
562 IP_Port source) 599 IP_Port source)
563{ 600{
564 if (num > onion_c->num_friends) 601 if (num > onion_c->num_friends) {
565 return -1; 602 return -1;
603 }
566 604
567 if (num_nodes == 0) 605 if (num_nodes == 0) {
568 return 0; 606 return 0;
607 }
569 608
570 Onion_Node *list_nodes = NULL; 609 Onion_Node *list_nodes = NULL;
571 uint8_t *reference_id = NULL; 610 uint8_t *reference_id = NULL;
@@ -593,9 +632,11 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for
593 632
594 for (i = 0; i < num_nodes; ++i) { 633 for (i = 0; i < num_nodes; ++i) {
595 634
596 if (!lan_ips_accepted) 635 if (!lan_ips_accepted) {
597 if (LAN_ip(nodes[i].ip_port.ip) == 0) 636 if (LAN_ip(nodes[i].ip_port.ip) == 0) {
598 continue; 637 continue;
638 }
639 }
599 640
600 if (is_timeout(list_nodes[0].timestamp, ONION_NODE_TIMEOUT) 641 if (is_timeout(list_nodes[0].timestamp, ONION_NODE_TIMEOUT)
601 || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2 642 || id_closest(reference_id, list_nodes[0].public_key, nodes[i].public_key) == 2
@@ -622,8 +663,9 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
622{ 663{
623 Onion_Client *onion_c = object; 664 Onion_Client *onion_c = object;
624 665
625 if (length < ONION_ANNOUNCE_RESPONSE_MIN_SIZE || length > ONION_ANNOUNCE_RESPONSE_MAX_SIZE) 666 if (length < ONION_ANNOUNCE_RESPONSE_MIN_SIZE || length > ONION_ANNOUNCE_RESPONSE_MAX_SIZE) {
626 return 1; 667 return 1;
668 }
627 669
628 uint16_t len_nodes = length - ONION_ANNOUNCE_RESPONSE_MIN_SIZE; 670 uint16_t len_nodes = length - ONION_ANNOUNCE_RESPONSE_MIN_SIZE;
629 671
@@ -632,8 +674,9 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
632 uint32_t path_num; 674 uint32_t path_num;
633 uint32_t num = check_sendback(onion_c, packet + 1, public_key, &ip_port, &path_num); 675 uint32_t num = check_sendback(onion_c, packet + 1, public_key, &ip_port, &path_num);
634 676
635 if (num > onion_c->num_friends) 677 if (num > onion_c->num_friends) {
636 return 1; 678 return 1;
679 }
637 680
638 uint8_t plain[1 + ONION_PING_ID_SIZE + len_nodes]; 681 uint8_t plain[1 + ONION_PING_ID_SIZE + len_nodes];
639 int len = -1; 682 int len = -1;
@@ -643,8 +686,9 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
643 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES, 686 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES,
644 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain); 687 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain);
645 } else { 688 } else {
646 if (onion_c->friends_list[num - 1].status == 0) 689 if (onion_c->friends_list[num - 1].status == 0) {
647 return 1; 690 return 1;
691 }
648 692
649 len = decrypt_data(public_key, onion_c->friends_list[num - 1].temp_secret_key, 693 len = decrypt_data(public_key, onion_c->friends_list[num - 1].temp_secret_key,
650 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH, 694 packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
@@ -652,21 +696,25 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
652 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain); 696 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES), plain);
653 } 697 }
654 698
655 if ((uint32_t)len != sizeof(plain)) 699 if ((uint32_t)len != sizeof(plain)) {
656 return 1; 700 return 1;
701 }
657 702
658 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1, path_num) == -1) 703 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1, path_num) == -1) {
659 return 1; 704 return 1;
705 }
660 706
661 if (len_nodes != 0) { 707 if (len_nodes != 0) {
662 Node_format nodes[MAX_SENT_NODES]; 708 Node_format nodes[MAX_SENT_NODES];
663 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes, 0); 709 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes, 0);
664 710
665 if (num_nodes <= 0) 711 if (num_nodes <= 0) {
666 return 1; 712 return 1;
713 }
667 714
668 if (client_ping_nodes(onion_c, num, nodes, num_nodes, source) == -1) 715 if (client_ping_nodes(onion_c, num, nodes, num_nodes, source) == -1) {
669 return 1; 716 return 1;
717 }
670 } 718 }
671 719
672 //TODO: LAN vs non LAN ips?, if we are connected only to LAN, are we offline? 720 //TODO: LAN vs non LAN ips?, if we are connected only to LAN, are we offline?
@@ -680,29 +728,34 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac
680{ 728{
681 Onion_Client *onion_c = object; 729 Onion_Client *onion_c = object;
682 730
683 if (length <= (ONION_DATA_RESPONSE_MIN_SIZE + DATA_IN_RESPONSE_MIN_SIZE)) 731 if (length <= (ONION_DATA_RESPONSE_MIN_SIZE + DATA_IN_RESPONSE_MIN_SIZE)) {
684 return 1; 732 return 1;
733 }
685 734
686 if (length > MAX_DATA_REQUEST_SIZE) 735 if (length > MAX_DATA_REQUEST_SIZE) {
687 return 1; 736 return 1;
737 }
688 738
689 uint8_t temp_plain[length - ONION_DATA_RESPONSE_MIN_SIZE]; 739 uint8_t temp_plain[length - ONION_DATA_RESPONSE_MIN_SIZE];
690 int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_c->temp_secret_key, packet + 1, 740 int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_c->temp_secret_key, packet + 1,
691 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, 741 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
692 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), temp_plain); 742 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), temp_plain);
693 743
694 if ((uint32_t)len != sizeof(temp_plain)) 744 if ((uint32_t)len != sizeof(temp_plain)) {
695 return 1; 745 return 1;
746 }
696 747
697 uint8_t plain[sizeof(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE]; 748 uint8_t plain[sizeof(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE];
698 len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + crypto_box_PUBLICKEYBYTES, 749 len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + crypto_box_PUBLICKEYBYTES,
699 sizeof(temp_plain) - crypto_box_PUBLICKEYBYTES, plain); 750 sizeof(temp_plain) - crypto_box_PUBLICKEYBYTES, plain);
700 751
701 if ((uint32_t)len != sizeof(plain)) 752 if ((uint32_t)len != sizeof(plain)) {
702 return 1; 753 return 1;
754 }
703 755
704 if (!onion_c->Onion_Data_Handlers[plain[0]].function) 756 if (!onion_c->Onion_Data_Handlers[plain[0]].function) {
705 return 1; 757 return 1;
758 }
706 759
707 return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain, 760 return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain,
708 sizeof(plain)); 761 sizeof(plain));
@@ -714,29 +767,34 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
714{ 767{
715 Onion_Client *onion_c = object; 768 Onion_Client *onion_c = object;
716 769
717 if (length < DHTPK_DATA_MIN_LENGTH) 770 if (length < DHTPK_DATA_MIN_LENGTH) {
718 return 1; 771 return 1;
772 }
719 773
720 if (length > DHTPK_DATA_MAX_LENGTH) 774 if (length > DHTPK_DATA_MAX_LENGTH) {
721 return 1; 775 return 1;
776 }
722 777
723 int friend_num = onion_friend_num(onion_c, source_pubkey); 778 int friend_num = onion_friend_num(onion_c, source_pubkey);
724 779
725 if (friend_num == -1) 780 if (friend_num == -1) {
726 return 1; 781 return 1;
782 }
727 783
728 uint64_t no_replay; 784 uint64_t no_replay;
729 memcpy(&no_replay, data + 1, sizeof(uint64_t)); 785 memcpy(&no_replay, data + 1, sizeof(uint64_t));
730 net_to_host((uint8_t *) &no_replay, sizeof(no_replay)); 786 net_to_host((uint8_t *) &no_replay, sizeof(no_replay));
731 787
732 if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) 788 if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) {
733 return 1; 789 return 1;
790 }
734 791
735 onion_c->friends_list[friend_num].last_noreplay = no_replay; 792 onion_c->friends_list[friend_num].last_noreplay = no_replay;
736 793
737 if (onion_c->friends_list[friend_num].dht_pk_callback) 794 if (onion_c->friends_list[friend_num].dht_pk_callback) {
738 onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object, 795 onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object,
739 onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t)); 796 onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t));
797 }
740 798
741 onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t)); 799 onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t));
742 onion_c->friends_list[friend_num].last_seen = unix_time(); 800 onion_c->friends_list[friend_num].last_seen = unix_time();
@@ -748,8 +806,9 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
748 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, 806 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES,
749 len_nodes, 1); 807 len_nodes, 1);
750 808
751 if (num_nodes <= 0) 809 if (num_nodes <= 0) {
752 return 1; 810 return 1;
811 }
753 812
754 int i; 813 int i;
755 814
@@ -773,8 +832,9 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
773 832
774static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, void *userdata) 833static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, void *userdata)
775{ 834{
776 if (length == 0) 835 if (length == 0) {
777 return 1; 836 return 1;
837 }
778 838
779 IP_Port ip_port = {{0}}; 839 IP_Port ip_port = {{0}};
780 ip_port.ip.family = TCP_FAMILY; 840 ip_port.ip.family = TCP_FAMILY;
@@ -798,21 +858,25 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length,
798 */ 858 */
799int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length) 859int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length)
800{ 860{
801 if ((uint32_t)friend_num >= onion_c->num_friends) 861 if ((uint32_t)friend_num >= onion_c->num_friends) {
802 return -1; 862 return -1;
863 }
803 864
804 if (length + DATA_IN_RESPONSE_MIN_SIZE > MAX_DATA_REQUEST_SIZE) 865 if (length + DATA_IN_RESPONSE_MIN_SIZE > MAX_DATA_REQUEST_SIZE) {
805 return -1; 866 return -1;
867 }
806 868
807 if (length == 0) 869 if (length == 0) {
808 return -1; 870 return -1;
871 }
809 872
810 unsigned int i, good_nodes[MAX_ONION_CLIENTS], num_good = 0, num_nodes = 0; 873 unsigned int i, good_nodes[MAX_ONION_CLIENTS], num_good = 0, num_nodes = 0;
811 Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list; 874 Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;
812 875
813 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 876 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
814 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) 877 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) {
815 continue; 878 continue;
879 }
816 880
817 ++num_nodes; 881 ++num_nodes;
818 882
@@ -822,8 +886,9 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
822 } 886 }
823 } 887 }
824 888
825 if (num_good < (num_nodes / 4) + 1) 889 if (num_good < (num_nodes / 4) + 1) {
826 return -1; 890 return -1;
891 }
827 892
828 uint8_t nonce[crypto_box_NONCEBYTES]; 893 uint8_t nonce[crypto_box_NONCEBYTES];
829 random_nonce(nonce); 894 random_nonce(nonce);
@@ -833,26 +898,30 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
833 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, 898 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data,
834 length, packet + crypto_box_PUBLICKEYBYTES); 899 length, packet + crypto_box_PUBLICKEYBYTES);
835 900
836 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES != sizeof(packet)) 901 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES != sizeof(packet)) {
837 return -1; 902 return -1;
903 }
838 904
839 unsigned int good = 0; 905 unsigned int good = 0;
840 906
841 for (i = 0; i < num_good; ++i) { 907 for (i = 0; i < num_good; ++i) {
842 Onion_Path path; 908 Onion_Path path;
843 909
844 if (random_path(onion_c, &onion_c->onion_paths_friends, ~0, &path) == -1) 910 if (random_path(onion_c, &onion_c->onion_paths_friends, ~0, &path) == -1) {
845 continue; 911 continue;
912 }
846 913
847 uint8_t o_packet[ONION_MAX_PACKET_SIZE]; 914 uint8_t o_packet[ONION_MAX_PACKET_SIZE];
848 len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key, 915 len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key,
849 list_nodes[good_nodes[i]].data_public_key, nonce, packet, sizeof(packet)); 916 list_nodes[good_nodes[i]].data_public_key, nonce, packet, sizeof(packet));
850 917
851 if (len == -1) 918 if (len == -1) {
852 continue; 919 continue;
920 }
853 921
854 if (send_onion_packet_tcp_udp(onion_c, &path, list_nodes[good_nodes[i]].ip_port, o_packet, len) == 0) 922 if (send_onion_packet_tcp_udp(onion_c, &path, list_nodes[good_nodes[i]].ip_port, o_packet, len) == 0) {
855 ++good; 923 ++good;
924 }
856 } 925 }
857 926
858 return good; 927 return good;
@@ -867,11 +936,13 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
867 */ 936 */
868static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length) 937static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length)
869{ 938{
870 if ((uint32_t)friend_num >= onion_c->num_friends) 939 if ((uint32_t)friend_num >= onion_c->num_friends) {
871 return -1; 940 return -1;
941 }
872 942
873 if (!onion_c->friends_list[friend_num].know_dht_public_key) 943 if (!onion_c->friends_list[friend_num].know_dht_public_key) {
874 return -1; 944 return -1;
945 }
875 946
876 uint8_t nonce[crypto_box_NONCEBYTES]; 947 uint8_t nonce[crypto_box_NONCEBYTES];
877 new_nonce(nonce); 948 new_nonce(nonce);
@@ -882,15 +953,17 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin
882 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, 953 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data,
883 length, temp + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 954 length, temp + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
884 955
885 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES != sizeof(temp)) 956 if ((uint32_t)len + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES != sizeof(temp)) {
886 return -1; 957 return -1;
958 }
887 959
888 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 960 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
889 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet, 961 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet,
890 onion_c->friends_list[friend_num].dht_public_key, temp, sizeof(temp), CRYPTO_PACKET_DHTPK); 962 onion_c->friends_list[friend_num].dht_public_key, temp, sizeof(temp), CRYPTO_PACKET_DHTPK);
891 963
892 if (len == -1) 964 if (len == -1) {
893 return -1; 965 return -1;
966 }
894 967
895 return route_tofriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, packet, len); 968 return route_tofriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, packet, len);
896} 969}
@@ -900,22 +973,26 @@ static int handle_dht_dhtpk(void *object, IP_Port source, const uint8_t *source_
900{ 973{
901 Onion_Client *onion_c = object; 974 Onion_Client *onion_c = object;
902 975
903 if (length < DHTPK_DATA_MIN_LENGTH + DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES) 976 if (length < DHTPK_DATA_MIN_LENGTH + DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES) {
904 return 1; 977 return 1;
978 }
905 979
906 if (length > DHTPK_DATA_MAX_LENGTH + DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES) 980 if (length > DHTPK_DATA_MAX_LENGTH + DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES) {
907 return 1; 981 return 1;
982 }
908 983
909 uint8_t plain[DHTPK_DATA_MAX_LENGTH]; 984 uint8_t plain[DHTPK_DATA_MAX_LENGTH];
910 int len = decrypt_data(packet, onion_c->c->self_secret_key, packet + crypto_box_PUBLICKEYBYTES, 985 int len = decrypt_data(packet, onion_c->c->self_secret_key, packet + crypto_box_PUBLICKEYBYTES,
911 packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, 986 packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES,
912 length - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), plain); 987 length - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), plain);
913 988
914 if (len != length - (DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES)) 989 if (len != length - (DATA_IN_RESPONSE_MIN_SIZE + crypto_box_NONCEBYTES)) {
915 return 1; 990 return 1;
991 }
916 992
917 if (public_key_cmp(source_pubkey, plain + 1 + sizeof(uint64_t)) != 0) 993 if (public_key_cmp(source_pubkey, plain + 1 + sizeof(uint64_t)) != 0) {
918 return 1; 994 return 1;
995 }
919 996
920 return handle_dhtpk_announce(onion_c, packet, plain, len); 997 return handle_dhtpk_announce(onion_c, packet, plain, len);
921} 998}
@@ -930,8 +1007,9 @@ static int handle_dht_dhtpk(void *object, IP_Port source, const uint8_t *source_
930 */ 1007 */
931static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8_t onion_dht_both) 1008static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8_t onion_dht_both)
932{ 1009{
933 if (friend_num >= onion_c->num_friends) 1010 if (friend_num >= onion_c->num_friends) {
934 return -1; 1011 return -1;
1012 }
935 1013
936 uint8_t data[DHTPK_DATA_MAX_LENGTH]; 1014 uint8_t data[DHTPK_DATA_MAX_LENGTH];
937 data[0] = ONION_DATA_DHTPK; 1015 data[0] = ONION_DATA_DHTPK;
@@ -949,23 +1027,28 @@ static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8
949 nodes_len = pack_nodes(data + DHTPK_DATA_MIN_LENGTH, DHTPK_DATA_MAX_LENGTH - DHTPK_DATA_MIN_LENGTH, nodes, 1027 nodes_len = pack_nodes(data + DHTPK_DATA_MIN_LENGTH, DHTPK_DATA_MAX_LENGTH - DHTPK_DATA_MIN_LENGTH, nodes,
950 num_nodes); 1028 num_nodes);
951 1029
952 if (nodes_len <= 0) 1030 if (nodes_len <= 0) {
953 return -1; 1031 return -1;
1032 }
954 } 1033 }
955 1034
956 int num1 = -1, num2 = -1; 1035 int num1 = -1, num2 = -1;
957 1036
958 if (onion_dht_both != 1) 1037 if (onion_dht_both != 1) {
959 num1 = send_onion_data(onion_c, friend_num, data, DHTPK_DATA_MIN_LENGTH + nodes_len); 1038 num1 = send_onion_data(onion_c, friend_num, data, DHTPK_DATA_MIN_LENGTH + nodes_len);
1039 }
960 1040
961 if (onion_dht_both != 0) 1041 if (onion_dht_both != 0) {
962 num2 = send_dht_dhtpk(onion_c, friend_num, data, DHTPK_DATA_MIN_LENGTH + nodes_len); 1042 num2 = send_dht_dhtpk(onion_c, friend_num, data, DHTPK_DATA_MIN_LENGTH + nodes_len);
1043 }
963 1044
964 if (num1 == -1) 1045 if (num1 == -1) {
965 return num2; 1046 return num2;
1047 }
966 1048
967 if (num2 == -1) 1049 if (num2 == -1) {
968 return num1; 1050 return num1;
1051 }
969 1052
970 return num1 + num2; 1053 return num1 + num2;
971} 1054}
@@ -980,11 +1063,13 @@ int onion_friend_num(const Onion_Client *onion_c, const uint8_t *public_key)
980 unsigned int i; 1063 unsigned int i;
981 1064
982 for (i = 0; i < onion_c->num_friends; ++i) { 1065 for (i = 0; i < onion_c->num_friends; ++i) {
983 if (onion_c->friends_list[i].status == 0) 1066 if (onion_c->friends_list[i].status == 0) {
984 continue; 1067 continue;
1068 }
985 1069
986 if (public_key_cmp(public_key, onion_c->friends_list[i].real_public_key) == 0) 1070 if (public_key_cmp(public_key, onion_c->friends_list[i].real_public_key) == 0) {
987 return i; 1071 return i;
1072 }
988 } 1073 }
989 1074
990 return -1; 1075 return -1;
@@ -1005,8 +1090,9 @@ static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num)
1005 1090
1006 Onion_Friend *newonion_friends = realloc(onion_c->friends_list, num * sizeof(Onion_Friend)); 1091 Onion_Friend *newonion_friends = realloc(onion_c->friends_list, num * sizeof(Onion_Friend));
1007 1092
1008 if (newonion_friends == NULL) 1093 if (newonion_friends == NULL) {
1009 return -1; 1094 return -1;
1095 }
1010 1096
1011 onion_c->friends_list = newonion_friends; 1097 onion_c->friends_list = newonion_friends;
1012 return 0; 1098 return 0;
@@ -1021,8 +1107,9 @@ int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key)
1021{ 1107{
1022 int num = onion_friend_num(onion_c, public_key); 1108 int num = onion_friend_num(onion_c, public_key);
1023 1109
1024 if (num != -1) 1110 if (num != -1) {
1025 return num; 1111 return num;
1112 }
1026 1113
1027 unsigned int i, index = ~0; 1114 unsigned int i, index = ~0;
1028 1115
@@ -1034,8 +1121,9 @@ int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key)
1034 } 1121 }
1035 1122
1036 if (index == (uint32_t)~0) { 1123 if (index == (uint32_t)~0) {
1037 if (realloc_onion_friends(onion_c, onion_c->num_friends + 1) == -1) 1124 if (realloc_onion_friends(onion_c, onion_c->num_friends + 1) == -1) {
1038 return -1; 1125 return -1;
1126 }
1039 1127
1040 index = onion_c->num_friends; 1128 index = onion_c->num_friends;
1041 memset(&(onion_c->friends_list[onion_c->num_friends]), 0, sizeof(Onion_Friend)); 1129 memset(&(onion_c->friends_list[onion_c->num_friends]), 0, sizeof(Onion_Friend));
@@ -1055,8 +1143,9 @@ int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key)
1055 */ 1143 */
1056int onion_delfriend(Onion_Client *onion_c, int friend_num) 1144int onion_delfriend(Onion_Client *onion_c, int friend_num)
1057{ 1145{
1058 if ((uint32_t)friend_num >= onion_c->num_friends) 1146 if ((uint32_t)friend_num >= onion_c->num_friends) {
1059 return -1; 1147 return -1;
1148 }
1060 1149
1061 //if (onion_c->friends_list[friend_num].know_dht_public_key) 1150 //if (onion_c->friends_list[friend_num].know_dht_public_key)
1062 // DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, 0); 1151 // DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, 0);
@@ -1065,8 +1154,9 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num)
1065 unsigned int i; 1154 unsigned int i;
1066 1155
1067 for (i = onion_c->num_friends; i != 0; --i) { 1156 for (i = onion_c->num_friends; i != 0; --i) {
1068 if (onion_c->friends_list[i - 1].status != 0) 1157 if (onion_c->friends_list[i - 1].status != 0) {
1069 break; 1158 break;
1159 }
1070 } 1160 }
1071 1161
1072 if (onion_c->num_friends != i) { 1162 if (onion_c->num_friends != i) {
@@ -1088,8 +1178,9 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num)
1088int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, 1178int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object,
1089 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number) 1179 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number)
1090{ 1180{
1091 if ((uint32_t)friend_num >= onion_c->num_friends) 1181 if ((uint32_t)friend_num >= onion_c->num_friends) {
1092 return -1; 1182 return -1;
1183 }
1093 1184
1094 onion_c->friends_list[friend_num].tcp_relay_node_callback = tcp_relay_node_callback; 1185 onion_c->friends_list[friend_num].tcp_relay_node_callback = tcp_relay_node_callback;
1095 onion_c->friends_list[friend_num].tcp_relay_node_callback_object = object; 1186 onion_c->friends_list[friend_num].tcp_relay_node_callback_object = object;
@@ -1108,8 +1199,9 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela
1108int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number, 1199int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number,
1109 const uint8_t *dht_public_key), void *object, uint32_t number) 1200 const uint8_t *dht_public_key), void *object, uint32_t number)
1110{ 1201{
1111 if ((uint32_t)friend_num >= onion_c->num_friends) 1202 if ((uint32_t)friend_num >= onion_c->num_friends) {
1112 return -1; 1203 return -1;
1204 }
1113 1205
1114 onion_c->friends_list[friend_num].dht_pk_callback = function; 1206 onion_c->friends_list[friend_num].dht_pk_callback = function;
1115 onion_c->friends_list[friend_num].dht_pk_callback_object = object; 1207 onion_c->friends_list[friend_num].dht_pk_callback_object = object;
@@ -1124,11 +1216,13 @@ int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function
1124 */ 1216 */
1125int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key) 1217int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key)
1126{ 1218{
1127 if ((uint32_t)friend_num >= onion_c->num_friends) 1219 if ((uint32_t)friend_num >= onion_c->num_friends) {
1128 return -1; 1220 return -1;
1221 }
1129 1222
1130 if (onion_c->friends_list[friend_num].status == 0) 1223 if (onion_c->friends_list[friend_num].status == 0) {
1131 return -1; 1224 return -1;
1225 }
1132 1226
1133 if (onion_c->friends_list[friend_num].know_dht_public_key) { 1227 if (onion_c->friends_list[friend_num].know_dht_public_key) {
1134 if (public_key_cmp(dht_key, onion_c->friends_list[friend_num].dht_public_key) == 0) { 1228 if (public_key_cmp(dht_key, onion_c->friends_list[friend_num].dht_public_key) == 0) {
@@ -1152,14 +1246,17 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin
1152 */ 1246 */
1153unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key) 1247unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key)
1154{ 1248{
1155 if ((uint32_t)friend_num >= onion_c->num_friends) 1249 if ((uint32_t)friend_num >= onion_c->num_friends) {
1156 return 0; 1250 return 0;
1251 }
1157 1252
1158 if (onion_c->friends_list[friend_num].status == 0) 1253 if (onion_c->friends_list[friend_num].status == 0) {
1159 return 0; 1254 return 0;
1255 }
1160 1256
1161 if (!onion_c->friends_list[friend_num].know_dht_public_key) 1257 if (!onion_c->friends_list[friend_num].know_dht_public_key) {
1162 return 0; 1258 return 0;
1259 }
1163 1260
1164 memcpy(dht_key, onion_c->friends_list[friend_num].dht_public_key, crypto_box_PUBLICKEYBYTES); 1261 memcpy(dht_key, onion_c->friends_list[friend_num].dht_public_key, crypto_box_PUBLICKEYBYTES);
1165 return 1; 1262 return 1;
@@ -1176,8 +1273,9 @@ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_p
1176{ 1273{
1177 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; 1274 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES];
1178 1275
1179 if (onion_getfriend_DHT_pubkey(onion_c, friend_num, dht_public_key) == 0) 1276 if (onion_getfriend_DHT_pubkey(onion_c, friend_num, dht_public_key) == 0) {
1180 return -1; 1277 return -1;
1278 }
1181 1279
1182 return DHT_getfriendip(onion_c->dht, dht_public_key, ip_port); 1280 return DHT_getfriendip(onion_c->dht, dht_public_key, ip_port);
1183} 1281}
@@ -1194,11 +1292,13 @@ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_p
1194 */ 1292 */
1195int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_online) 1293int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_online)
1196{ 1294{
1197 if ((uint32_t)friend_num >= onion_c->num_friends) 1295 if ((uint32_t)friend_num >= onion_c->num_friends) {
1198 return -1; 1296 return -1;
1297 }
1199 1298
1200 if (is_online == 0 && onion_c->friends_list[friend_num].is_online == 1) 1299 if (is_online == 0 && onion_c->friends_list[friend_num].is_online == 1) {
1201 onion_c->friends_list[friend_num].last_seen = unix_time(); 1300 onion_c->friends_list[friend_num].last_seen = unix_time();
1301 }
1202 1302
1203 onion_c->friends_list[friend_num].is_online = is_online; 1303 onion_c->friends_list[friend_num].is_online = is_online;
1204 1304
@@ -1244,24 +1344,28 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c)
1244 1344
1245static void do_friend(Onion_Client *onion_c, uint16_t friendnum) 1345static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1246{ 1346{
1247 if (friendnum >= onion_c->num_friends) 1347 if (friendnum >= onion_c->num_friends) {
1248 return; 1348 return;
1349 }
1249 1350
1250 if (onion_c->friends_list[friendnum].status == 0) 1351 if (onion_c->friends_list[friendnum].status == 0) {
1251 return; 1352 return;
1353 }
1252 1354
1253 unsigned int interval = ANNOUNCE_FRIEND; 1355 unsigned int interval = ANNOUNCE_FRIEND;
1254 1356
1255 if (onion_c->friends_list[friendnum].run_count < RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING) 1357 if (onion_c->friends_list[friendnum].run_count < RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING) {
1256 interval = ANNOUNCE_FRIEND_BEGINNING; 1358 interval = ANNOUNCE_FRIEND_BEGINNING;
1359 }
1257 1360
1258 unsigned int i, count = 0; 1361 unsigned int i, count = 0;
1259 Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; 1362 Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list;
1260 1363
1261 if (!onion_c->friends_list[friendnum].is_online) { 1364 if (!onion_c->friends_list[friendnum].is_online) {
1262 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 1365 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
1263 if (is_timeout(list_nodes[i].timestamp, FRIEND_ONION_NODE_TIMEOUT)) 1366 if (is_timeout(list_nodes[i].timestamp, FRIEND_ONION_NODE_TIMEOUT)) {
1264 continue; 1367 continue;
1368 }
1265 1369
1266 ++count; 1370 ++count;
1267 1371
@@ -1283,8 +1387,9 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1283 1387
1284 unsigned int n = num_nodes; 1388 unsigned int n = num_nodes;
1285 1389
1286 if (num_nodes > (MAX_ONION_CLIENTS / 2)) 1390 if (num_nodes > (MAX_ONION_CLIENTS / 2)) {
1287 n = (MAX_ONION_CLIENTS / 2); 1391 n = (MAX_ONION_CLIENTS / 2);
1392 }
1288 1393
1289 if (num_nodes != 0) { 1394 if (num_nodes != 0) {
1290 unsigned int j; 1395 unsigned int j;
@@ -1302,13 +1407,17 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1302 } 1407 }
1303 1408
1304 /* send packets to friend telling them our DHT public key. */ 1409 /* send packets to friend telling them our DHT public key. */
1305 if (is_timeout(onion_c->friends_list[friendnum].last_dht_pk_onion_sent, ONION_DHTPK_SEND_INTERVAL)) 1410 if (is_timeout(onion_c->friends_list[friendnum].last_dht_pk_onion_sent, ONION_DHTPK_SEND_INTERVAL)) {
1306 if (send_dhtpk_announce(onion_c, friendnum, 0) >= 1) 1411 if (send_dhtpk_announce(onion_c, friendnum, 0) >= 1) {
1307 onion_c->friends_list[friendnum].last_dht_pk_onion_sent = unix_time(); 1412 onion_c->friends_list[friendnum].last_dht_pk_onion_sent = unix_time();
1413 }
1414 }
1308 1415
1309 if (is_timeout(onion_c->friends_list[friendnum].last_dht_pk_dht_sent, DHT_DHTPK_SEND_INTERVAL)) 1416 if (is_timeout(onion_c->friends_list[friendnum].last_dht_pk_dht_sent, DHT_DHTPK_SEND_INTERVAL)) {
1310 if (send_dhtpk_announce(onion_c, friendnum, 1) >= 1) 1417 if (send_dhtpk_announce(onion_c, friendnum, 1) >= 1) {
1311 onion_c->friends_list[friendnum].last_dht_pk_dht_sent = unix_time(); 1418 onion_c->friends_list[friendnum].last_dht_pk_dht_sent = unix_time();
1419 }
1420 }
1312 1421
1313 } 1422 }
1314} 1423}
@@ -1330,8 +1439,9 @@ static void do_announce(Onion_Client *onion_c)
1330 Onion_Node *list_nodes = onion_c->clients_announce_list; 1439 Onion_Node *list_nodes = onion_c->clients_announce_list;
1331 1440
1332 for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) { 1441 for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
1333 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) 1442 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) {
1334 continue; 1443 continue;
1444 }
1335 1445
1336 ++count; 1446 ++count;
1337 1447
@@ -1385,11 +1495,13 @@ static int onion_isconnected(const Onion_Client *onion_c)
1385{ 1495{
1386 unsigned int i, num = 0, announced = 0; 1496 unsigned int i, num = 0, announced = 0;
1387 1497
1388 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) 1498 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) {
1389 return 0; 1499 return 0;
1500 }
1390 1501
1391 if (onion_c->path_nodes_index == 0) 1502 if (onion_c->path_nodes_index == 0) {
1392 return 0; 1503 return 0;
1504 }
1393 1505
1394 for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) { 1506 for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
1395 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) { 1507 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) {
@@ -1410,8 +1522,9 @@ static int onion_isconnected(const Onion_Client *onion_c)
1410 /* Consider ourselves online if we are announced to half or more nodes 1522 /* Consider ourselves online if we are announced to half or more nodes
1411 we are connected to */ 1523 we are connected to */
1412 if (num && announced) { 1524 if (num && announced) {
1413 if ((num / 2) <= announced && (pnodes / 2) <= num) 1525 if ((num / 2) <= announced && (pnodes / 2) <= num) {
1414 return 1; 1526 return 1;
1527 }
1415 } 1528 }
1416 1529
1417 return 0; 1530 return 0;
@@ -1440,8 +1553,9 @@ void do_onion_client(Onion_Client *onion_c)
1440{ 1553{
1441 unsigned int i; 1554 unsigned int i;
1442 1555
1443 if (onion_c->last_run == unix_time()) 1556 if (onion_c->last_run == unix_time()) {
1444 return; 1557 return;
1558 }
1445 1559
1446 if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS)) { 1560 if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS)) {
1447 populate_path_nodes(onion_c); 1561 populate_path_nodes(onion_c);
@@ -1485,13 +1599,15 @@ void do_onion_client(Onion_Client *onion_c)
1485 1599
1486Onion_Client *new_onion_client(Net_Crypto *c) 1600Onion_Client *new_onion_client(Net_Crypto *c)
1487{ 1601{
1488 if (c == NULL) 1602 if (c == NULL) {
1489 return NULL; 1603 return NULL;
1604 }
1490 1605
1491 Onion_Client *onion_c = calloc(1, sizeof(Onion_Client)); 1606 Onion_Client *onion_c = calloc(1, sizeof(Onion_Client));
1492 1607
1493 if (onion_c == NULL) 1608 if (onion_c == NULL) {
1494 return NULL; 1609 return NULL;
1610 }
1495 1611
1496 if (ping_array_init(&onion_c->announce_ping_array, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT) != 0) { 1612 if (ping_array_init(&onion_c->announce_ping_array, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT) != 0) {
1497 free(onion_c); 1613 free(onion_c);
@@ -1514,8 +1630,9 @@ Onion_Client *new_onion_client(Net_Crypto *c)
1514 1630
1515void kill_onion_client(Onion_Client *onion_c) 1631void kill_onion_client(Onion_Client *onion_c)
1516{ 1632{
1517 if (onion_c == NULL) 1633 if (onion_c == NULL) {
1518 return; 1634 return;
1635 }
1519 1636
1520 ping_array_free_all(&onion_c->announce_ping_array); 1637 ping_array_free_all(&onion_c->announce_ping_array);
1521 realloc_onion_friends(onion_c, 0); 1638 realloc_onion_friends(onion_c, 0);