summaryrefslogtreecommitdiff
path: root/toxcore/TCP_connection.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/TCP_connection.c
parent6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff)
Add braces to all if statements.
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r--toxcore/TCP_connection.c222
1 files changed, 148 insertions, 74 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index d564762a..786cb164 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -41,14 +41,17 @@
41 */ 41 */
42static _Bool connections_number_not_valid(const TCP_Connections *tcp_c, int connections_number) 42static _Bool connections_number_not_valid(const TCP_Connections *tcp_c, int connections_number)
43{ 43{
44 if ((unsigned int)connections_number >= tcp_c->connections_length) 44 if ((unsigned int)connections_number >= tcp_c->connections_length) {
45 return 1; 45 return 1;
46 }
46 47
47 if (tcp_c->connections == NULL) 48 if (tcp_c->connections == NULL) {
48 return 1; 49 return 1;
50 }
49 51
50 if (tcp_c->connections[connections_number].status == TCP_CONN_NONE) 52 if (tcp_c->connections[connections_number].status == TCP_CONN_NONE) {
51 return 1; 53 return 1;
54 }
52 55
53 return 0; 56 return 0;
54} 57}
@@ -58,14 +61,17 @@ static _Bool connections_number_not_valid(const TCP_Connections *tcp_c, int conn
58 */ 61 */
59static _Bool tcp_connections_number_not_valid(const TCP_Connections *tcp_c, int tcp_connections_number) 62static _Bool tcp_connections_number_not_valid(const TCP_Connections *tcp_c, int tcp_connections_number)
60{ 63{
61 if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length) 64 if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length) {
62 return 1; 65 return 1;
66 }
63 67
64 if (tcp_c->tcp_connections == NULL) 68 if (tcp_c->tcp_connections == NULL) {
65 return 1; 69 return 1;
70 }
66 71
67 if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_NONE) 72 if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_NONE) {
68 return 1; 73 return 1;
74 }
69 75
70 return 0; 76 return 0;
71} 77}
@@ -80,8 +86,9 @@ static int create_connection(TCP_Connections *tcp_c)
80 uint32_t i; 86 uint32_t i;
81 87
82 for (i = 0; i < tcp_c->connections_length; ++i) { 88 for (i = 0; i < tcp_c->connections_length; ++i) {
83 if (tcp_c->connections[i].status == TCP_CONN_NONE) 89 if (tcp_c->connections[i].status == TCP_CONN_NONE) {
84 return i; 90 return i;
91 }
85 } 92 }
86 93
87 int id = -1; 94 int id = -1;
@@ -108,8 +115,9 @@ static int create_tcp_connection(TCP_Connections *tcp_c)
108 uint32_t i; 115 uint32_t i;
109 116
110 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 117 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
111 if (tcp_c->tcp_connections[i].status == TCP_CONN_NONE) 118 if (tcp_c->tcp_connections[i].status == TCP_CONN_NONE) {
112 return i; 119 return i;
120 }
113 } 121 }
114 122
115 int id = -1; 123 int id = -1;
@@ -132,15 +140,17 @@ static int create_tcp_connection(TCP_Connections *tcp_c)
132 */ 140 */
133static int wipe_connection(TCP_Connections *tcp_c, int connections_number) 141static int wipe_connection(TCP_Connections *tcp_c, int connections_number)
134{ 142{
135 if (connections_number_not_valid(tcp_c, connections_number)) 143 if (connections_number_not_valid(tcp_c, connections_number)) {
136 return -1; 144 return -1;
145 }
137 146
138 uint32_t i; 147 uint32_t i;
139 memset(&(tcp_c->connections[connections_number]), 0 , sizeof(TCP_Connection_to)); 148 memset(&(tcp_c->connections[connections_number]), 0 , sizeof(TCP_Connection_to));
140 149
141 for (i = tcp_c->connections_length; i != 0; --i) { 150 for (i = tcp_c->connections_length; i != 0; --i) {
142 if (tcp_c->connections[i - 1].status != TCP_CONN_NONE) 151 if (tcp_c->connections[i - 1].status != TCP_CONN_NONE) {
143 break; 152 break;
153 }
144 } 154 }
145 155
146 if (tcp_c->connections_length != i) { 156 if (tcp_c->connections_length != i) {
@@ -159,15 +169,17 @@ static int wipe_connection(TCP_Connections *tcp_c, int connections_number)
159 */ 169 */
160static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_number) 170static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_number)
161{ 171{
162 if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) 172 if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) {
163 return -1; 173 return -1;
174 }
164 175
165 uint32_t i; 176 uint32_t i;
166 memset(&(tcp_c->tcp_connections[tcp_connections_number]), 0 , sizeof(TCP_con)); 177 memset(&(tcp_c->tcp_connections[tcp_connections_number]), 0 , sizeof(TCP_con));
167 178
168 for (i = tcp_c->tcp_connections_length; i != 0; --i) { 179 for (i = tcp_c->tcp_connections_length; i != 0; --i) {
169 if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_NONE) 180 if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_NONE) {
170 break; 181 break;
182 }
171 } 183 }
172 184
173 if (tcp_c->tcp_connections_length != i) { 185 if (tcp_c->tcp_connections_length != i) {
@@ -181,16 +193,18 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe
181 193
182static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int connections_number) 194static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int connections_number)
183{ 195{
184 if (connections_number_not_valid(tcp_c, connections_number)) 196 if (connections_number_not_valid(tcp_c, connections_number)) {
185 return 0; 197 return 0;
198 }
186 199
187 return &tcp_c->connections[connections_number]; 200 return &tcp_c->connections[connections_number];
188} 201}
189 202
190static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connections_number) 203static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connections_number)
191{ 204{
192 if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) 205 if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) {
193 return 0; 206 return 0;
207 }
194 208
195 return &tcp_c->tcp_connections[tcp_connections_number]; 209 return &tcp_c->tcp_connections[tcp_connections_number];
196} 210}
@@ -312,8 +326,9 @@ int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_
312 if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) { 326 if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) {
313 int ret = send_onion_request(tcp_c->tcp_connections[tcp_connections_number].connection, data, length); 327 int ret = send_onion_request(tcp_c->tcp_connections[tcp_connections_number].connection, data, length);
314 328
315 if (ret == 1) 329 if (ret == 1) {
316 return 0; 330 return 0;
331 }
317 } 332 }
318 333
319 return -1; 334 return -1;
@@ -329,16 +344,19 @@ int tcp_send_oob_packet(TCP_Connections *tcp_c, unsigned int tcp_connections_num
329{ 344{
330 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 345 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
331 346
332 if (!tcp_con) 347 if (!tcp_con) {
333 return -1; 348 return -1;
349 }
334 350
335 if (tcp_con->status != TCP_CONN_CONNECTED) 351 if (tcp_con->status != TCP_CONN_CONNECTED) {
336 return -1; 352 return -1;
353 }
337 354
338 int ret = send_oob_packet(tcp_con->connection, public_key, packet, length); 355 int ret = send_oob_packet(tcp_con->connection, public_key, packet, length);
339 356
340 if (ret == 1) 357 if (ret == 1) {
341 return 0; 358 return 0;
359 }
342 360
343 return -1; 361 return -1;
344} 362}
@@ -432,13 +450,15 @@ static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *rela
432 */ 450 */
433int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int id) 451int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int id)
434{ 452{
435 if (find_tcp_connection_to(tcp_c, public_key) != -1) 453 if (find_tcp_connection_to(tcp_c, public_key) != -1) {
436 return -1; 454 return -1;
455 }
437 456
438 int connections_number = create_connection(tcp_c); 457 int connections_number = create_connection(tcp_c);
439 458
440 if (connections_number == -1) 459 if (connections_number == -1) {
441 return -1; 460 return -1;
461 }
442 462
443 TCP_Connection_to *con_to = &tcp_c->connections[connections_number]; 463 TCP_Connection_to *con_to = &tcp_c->connections[connections_number];
444 464
@@ -456,8 +476,9 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
456{ 476{
457 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 477 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
458 478
459 if (!con_to) 479 if (!con_to) {
460 return -1; 480 return -1;
481 }
461 482
462 unsigned int i; 483 unsigned int i;
463 484
@@ -466,8 +487,9 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
466 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1; 487 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
467 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 488 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
468 489
469 if (!tcp_con) 490 if (!tcp_con) {
470 continue; 491 continue;
492 }
471 493
472 if (tcp_con->status == TCP_CONN_CONNECTED) { 494 if (tcp_con->status == TCP_CONN_CONNECTED) {
473 send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id); 495 send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id);
@@ -500,13 +522,15 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
500{ 522{
501 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 523 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
502 524
503 if (!con_to) 525 if (!con_to) {
504 return -1; 526 return -1;
527 }
505 528
506 if (status) { 529 if (status) {
507 /* Conection is unsleeping. */ 530 /* Conection is unsleeping. */
508 if (con_to->status != TCP_CONN_SLEEPING) 531 if (con_to->status != TCP_CONN_SLEEPING) {
509 return -1; 532 return -1;
533 }
510 534
511 unsigned int i; 535 unsigned int i;
512 536
@@ -515,8 +539,9 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
515 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1; 539 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
516 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 540 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
517 541
518 if (!tcp_con) 542 if (!tcp_con) {
519 continue; 543 continue;
544 }
520 545
521 if (tcp_con->status == TCP_CONN_SLEEPING) { 546 if (tcp_con->status == TCP_CONN_SLEEPING) {
522 tcp_con->unsleep = 1; 547 tcp_con->unsleep = 1;
@@ -528,8 +553,9 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
528 return 0; 553 return 0;
529 } else { 554 } else {
530 /* Conection is going to sleep. */ 555 /* Conection is going to sleep. */
531 if (con_to->status != TCP_CONN_VALID) 556 if (con_to->status != TCP_CONN_VALID) {
532 return -1; 557 return -1;
558 }
533 559
534 unsigned int i; 560 unsigned int i;
535 561
@@ -538,8 +564,9 @@ int set_tcp_connection_to_status(TCP_Connections *tcp_c, int connections_number,
538 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1; 564 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
539 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 565 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
540 566
541 if (!tcp_con) 567 if (!tcp_con) {
542 continue; 568 continue;
569 }
543 570
544 if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { 571 if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) {
545 ++tcp_con->sleep_count; 572 ++tcp_con->sleep_count;
@@ -572,8 +599,9 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, unsigned int tc
572{ 599{
573 unsigned int i; 600 unsigned int i;
574 601
575 if (tcp_connection_in_conn(con_to, tcp_connections_number)) 602 if (tcp_connection_in_conn(con_to, tcp_connections_number)) {
576 return -1; 603 return -1;
604 }
577 605
578 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { 606 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
579 if (con_to->connections[i].tcp_connection == 0) { 607 if (con_to->connections[i].tcp_connection == 0) {
@@ -657,8 +685,9 @@ static int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections
657{ 685{
658 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 686 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
659 687
660 if (!tcp_con) 688 if (!tcp_con) {
661 return -1; 689 return -1;
690 }
662 691
663 unsigned int i; 692 unsigned int i;
664 693
@@ -683,11 +712,13 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
683{ 712{
684 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 713 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
685 714
686 if (!tcp_con) 715 if (!tcp_con) {
687 return -1; 716 return -1;
717 }
688 718
689 if (tcp_con->status == TCP_CONN_SLEEPING) 719 if (tcp_con->status == TCP_CONN_SLEEPING) {
690 return -1; 720 return -1;
721 }
691 722
692 IP_Port ip_port = tcp_con->connection->ip_port; 723 IP_Port ip_port = tcp_con->connection->ip_port;
693 uint8_t relay_pk[crypto_box_PUBLICKEYBYTES]; 724 uint8_t relay_pk[crypto_box_PUBLICKEYBYTES];
@@ -729,14 +760,17 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
729{ 760{
730 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 761 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
731 762
732 if (!tcp_con) 763 if (!tcp_con) {
733 return -1; 764 return -1;
765 }
734 766
735 if (tcp_con->status != TCP_CONN_CONNECTED) 767 if (tcp_con->status != TCP_CONN_CONNECTED) {
736 return -1; 768 return -1;
769 }
737 770
738 if (tcp_con->lock_count != tcp_con->sleep_count) 771 if (tcp_con->lock_count != tcp_con->sleep_count) {
739 return -1; 772 return -1;
773 }
740 774
741 tcp_con->ip_port = tcp_con->connection->ip_port; 775 tcp_con->ip_port = tcp_con->connection->ip_port;
742 memcpy(tcp_con->relay_pk, tcp_con->connection->public_key, crypto_box_PUBLICKEYBYTES); 776 memcpy(tcp_con->relay_pk, tcp_con->connection->public_key, crypto_box_PUBLICKEYBYTES);
@@ -772,11 +806,13 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti
772{ 806{
773 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 807 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
774 808
775 if (!tcp_con) 809 if (!tcp_con) {
776 return -1; 810 return -1;
811 }
777 812
778 if (tcp_con->status != TCP_CONN_SLEEPING) 813 if (tcp_con->status != TCP_CONN_SLEEPING) {
779 return -1; 814 return -1;
815 }
780 816
781 tcp_con->connection = new_TCP_connection(tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key, 817 tcp_con->connection = new_TCP_connection(tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key,
782 tcp_c->self_secret_key, &tcp_c->proxy_info); 818 tcp_c->self_secret_key, &tcp_c->proxy_info);
@@ -803,14 +839,17 @@ static int send_tcp_relay_routing_request(TCP_Connections *tcp_c, int tcp_connec
803{ 839{
804 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 840 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
805 841
806 if (!tcp_con) 842 if (!tcp_con) {
807 return -1; 843 return -1;
844 }
808 845
809 if (tcp_con->status == TCP_CONN_SLEEPING) 846 if (tcp_con->status == TCP_CONN_SLEEPING) {
810 return -1; 847 return -1;
848 }
811 849
812 if (send_routing_request(tcp_con->connection, public_key) != 1) 850 if (send_routing_request(tcp_con->connection, public_key) != 1) {
813 return -1; 851 return -1;
852 }
814 853
815 return 0; 854 return 0;
816} 855}
@@ -823,21 +862,25 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint
823 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 862 unsigned int tcp_connections_number = TCP_client_con->custom_uint;
824 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 863 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
825 864
826 if (!tcp_con) 865 if (!tcp_con) {
827 return -1; 866 return -1;
867 }
828 868
829 int connections_number = find_tcp_connection_to(tcp_c, public_key); 869 int connections_number = find_tcp_connection_to(tcp_c, public_key);
830 870
831 if (connections_number == -1) 871 if (connections_number == -1) {
832 return -1; 872 return -1;
873 }
833 874
834 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 875 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
835 876
836 if (con_to == NULL) 877 if (con_to == NULL) {
837 return -1; 878 return -1;
879 }
838 880
839 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) 881 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) {
840 return -1; 882 return -1;
883 }
841 884
842 set_tcp_connection_number(tcp_con->connection, connection_id, connections_number); 885 set_tcp_connection_number(tcp_con->connection, connection_id, connections_number);
843 886
@@ -853,12 +896,14 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection
853 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 896 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
854 TCP_Connection_to *con_to = get_connection(tcp_c, number); 897 TCP_Connection_to *con_to = get_connection(tcp_c, number);
855 898
856 if (!con_to || !tcp_con) 899 if (!con_to || !tcp_con) {
857 return -1; 900 return -1;
901 }
858 902
859 if (status == 1) { 903 if (status == 1) {
860 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) 904 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) {
861 return -1; 905 return -1;
906 }
862 907
863 --tcp_con->lock_count; 908 --tcp_con->lock_count;
864 909
@@ -866,8 +911,9 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection
866 --tcp_con->sleep_count; 911 --tcp_con->sleep_count;
867 } 912 }
868 } else if (status == 2) { 913 } else if (status == 2) {
869 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, connection_id) == -1) 914 if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, connection_id) == -1) {
870 return -1; 915 return -1;
916 }
871 917
872 ++tcp_con->lock_count; 918 ++tcp_con->lock_count;
873 919
@@ -883,8 +929,9 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i
883 void *userdata) 929 void *userdata)
884{ 930{
885 931
886 if (length == 0) 932 if (length == 0) {
887 return -1; 933 return -1;
934 }
888 935
889 TCP_Client_Connection *TCP_client_con = object; 936 TCP_Client_Connection *TCP_client_con = object;
890 TCP_Connections *tcp_c = TCP_client_con->custom_object; 937 TCP_Connections *tcp_c = TCP_client_con->custom_object;
@@ -892,16 +939,19 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i
892 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 939 unsigned int tcp_connections_number = TCP_client_con->custom_uint;
893 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 940 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
894 941
895 if (!tcp_con) 942 if (!tcp_con) {
896 return -1; 943 return -1;
944 }
897 945
898 TCP_Connection_to *con_to = get_connection(tcp_c, number); 946 TCP_Connection_to *con_to = get_connection(tcp_c, number);
899 947
900 if (!con_to) 948 if (!con_to) {
901 return -1; 949 return -1;
950 }
902 951
903 if (tcp_c->tcp_data_callback) 952 if (tcp_c->tcp_data_callback) {
904 tcp_c->tcp_data_callback(tcp_c->tcp_data_callback_object, con_to->id, data, length, userdata); 953 tcp_c->tcp_data_callback(tcp_c->tcp_data_callback_object, con_to->id, data, length, userdata);
954 }
905 955
906 return 0; 956 return 0;
907} 957}
@@ -909,8 +959,9 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i
909static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, 959static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length,
910 void *userdata) 960 void *userdata)
911{ 961{
912 if (length == 0) 962 if (length == 0) {
913 return -1; 963 return -1;
964 }
914 965
915 TCP_Client_Connection *TCP_client_con = object; 966 TCP_Client_Connection *TCP_client_con = object;
916 TCP_Connections *tcp_c = TCP_client_con->custom_object; 967 TCP_Connections *tcp_c = TCP_client_con->custom_object;
@@ -918,8 +969,9 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8
918 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 969 unsigned int tcp_connections_number = TCP_client_con->custom_uint;
919 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 970 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
920 971
921 if (!tcp_con) 972 if (!tcp_con) {
922 return -1; 973 return -1;
974 }
923 975
924 /* TODO: optimize */ 976 /* TODO: optimize */
925 int connections_number = find_tcp_connection_to(tcp_c, public_key); 977 int connections_number = find_tcp_connection_to(tcp_c, public_key);
@@ -929,8 +981,9 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8
929 if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) { 981 if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) {
930 return tcp_data_callback(object, connections_number, 0, data, length, userdata); 982 return tcp_data_callback(object, connections_number, 0, data, length, userdata);
931 } else { 983 } else {
932 if (tcp_c->tcp_oob_callback) 984 if (tcp_c->tcp_oob_callback) {
933 tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length); 985 tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length);
986 }
934 } 987 }
935 988
936 return 0; 989 return 0;
@@ -940,8 +993,9 @@ static int tcp_onion_callback(void *object, const uint8_t *data, uint16_t length
940{ 993{
941 TCP_Connections *tcp_c = object; 994 TCP_Connections *tcp_c = object;
942 995
943 if (tcp_c->tcp_onion_callback) 996 if (tcp_c->tcp_onion_callback) {
944 tcp_c->tcp_onion_callback(tcp_c->tcp_onion_callback_object, data, length, userdata); 997 tcp_c->tcp_onion_callback(tcp_c->tcp_onion_callback_object, data, length, userdata);
998 }
945 999
946 return 0; 1000 return 0;
947} 1001}
@@ -955,8 +1009,9 @@ static int tcp_relay_set_callbacks(TCP_Connections *tcp_c, int tcp_connections_n
955{ 1009{
956 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1010 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
957 1011
958 if (!tcp_con) 1012 if (!tcp_con) {
959 return -1; 1013 return -1;
1014 }
960 1015
961 TCP_Client_Connection *con = tcp_con->connection; 1016 TCP_Client_Connection *con = tcp_con->connection;
962 1017
@@ -975,8 +1030,9 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
975{ 1030{
976 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1031 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
977 1032
978 if (!tcp_con) 1033 if (!tcp_con) {
979 return -1; 1034 return -1;
1035 }
980 1036
981 unsigned int i, sent = 0; 1037 unsigned int i, sent = 0;
982 1038
@@ -1018,13 +1074,15 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const
1018 ip_port.ip.family = AF_INET6; 1074 ip_port.ip.family = AF_INET6;
1019 } 1075 }
1020 1076
1021 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) 1077 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) {
1022 return -1; 1078 return -1;
1079 }
1023 1080
1024 int tcp_connections_number = create_tcp_connection(tcp_c); 1081 int tcp_connections_number = create_tcp_connection(tcp_c);
1025 1082
1026 if (tcp_connections_number == -1) 1083 if (tcp_connections_number == -1) {
1027 return -1; 1084 return -1;
1085 }
1028 1086
1029 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; 1087 TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];
1030 1088
@@ -1032,8 +1090,9 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const
1032 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, 1090 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key,
1033 &tcp_c->proxy_info); 1091 &tcp_c->proxy_info);
1034 1092
1035 if (!tcp_con->connection) 1093 if (!tcp_con->connection) {
1036 return -1; 1094 return -1;
1095 }
1037 1096
1038 tcp_con->status = TCP_CONN_VALID; 1097 tcp_con->status = TCP_CONN_VALID;
1039 1098
@@ -1049,11 +1108,13 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
1049{ 1108{
1050 int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk); 1109 int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk);
1051 1110
1052 if (tcp_connections_number != -1) 1111 if (tcp_connections_number != -1) {
1053 return -1; 1112 return -1;
1113 }
1054 1114
1055 if (add_tcp_relay_instance(tcp_c, ip_port, relay_pk) == -1) 1115 if (add_tcp_relay_instance(tcp_c, ip_port, relay_pk) == -1) {
1056 return -1; 1116 return -1;
1117 }
1057 1118
1058 return 0; 1119 return 0;
1059} 1120}
@@ -1067,20 +1128,23 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb
1067{ 1128{
1068 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 1129 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
1069 1130
1070 if (!con_to) 1131 if (!con_to) {
1071 return -1; 1132 return -1;
1133 }
1072 1134
1073 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1135 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
1074 1136
1075 if (!tcp_con) 1137 if (!tcp_con) {
1076 return -1; 1138 return -1;
1139 }
1077 1140
1078 if (con_to->status != TCP_CONN_SLEEPING && tcp_con->status == TCP_CONN_SLEEPING) { 1141 if (con_to->status != TCP_CONN_SLEEPING && tcp_con->status == TCP_CONN_SLEEPING) {
1079 tcp_con->unsleep = 1; 1142 tcp_con->unsleep = 1;
1080 } 1143 }
1081 1144
1082 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) 1145 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) {
1083 return -1; 1146 return -1;
1147 }
1084 1148
1085 if (tcp_con->status == TCP_CONN_CONNECTED) { 1149 if (tcp_con->status == TCP_CONN_CONNECTED) {
1086 if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) { 1150 if (send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key) == 0) {
@@ -1102,8 +1166,9 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
1102{ 1166{
1103 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 1167 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
1104 1168
1105 if (!con_to) 1169 if (!con_to) {
1106 return -1; 1170 return -1;
1171 }
1107 1172
1108 int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk); 1173 int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk);
1109 1174
@@ -1118,8 +1183,9 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
1118 1183
1119 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1184 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
1120 1185
1121 if (!tcp_con) 1186 if (!tcp_con) {
1122 return -1; 1187 return -1;
1188 }
1123 1189
1124 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { 1190 if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) {
1125 return -1; 1191 return -1;
@@ -1136,8 +1202,9 @@ unsigned int tcp_connection_to_online_tcp_relays(TCP_Connections *tcp_c, int con
1136{ 1202{
1137 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); 1203 TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
1138 1204
1139 if (!con_to) 1205 if (!con_to) {
1140 return 0; 1206 return 0;
1207 }
1141 1208
1142 return online_tcp_connection_from_conn(con_to); 1209 return online_tcp_connection_from_conn(con_to);
1143} 1210}
@@ -1185,8 +1252,9 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_
1185 */ 1252 */
1186int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status) 1253int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status)
1187{ 1254{
1188 if (tcp_c->onion_status == status) 1255 if (tcp_c->onion_status == status) {
1189 return -1; 1256 return -1;
1257 }
1190 1258
1191 if (status) { 1259 if (status) {
1192 unsigned int i; 1260 unsigned int i;
@@ -1201,8 +1269,9 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status)
1201 } 1269 }
1202 } 1270 }
1203 1271
1204 if (tcp_c->onion_num_conns >= NUM_ONION_TCP_CONNECTIONS) 1272 if (tcp_c->onion_num_conns >= NUM_ONION_TCP_CONNECTIONS) {
1205 break; 1273 break;
1274 }
1206 } 1275 }
1207 1276
1208 if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { 1277 if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) {
@@ -1217,8 +1286,9 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status)
1217 } 1286 }
1218 } 1287 }
1219 1288
1220 if (!wakeup) 1289 if (!wakeup) {
1221 break; 1290 break;
1291 }
1222 } 1292 }
1223 } 1293 }
1224 1294
@@ -1252,13 +1322,15 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status)
1252 */ 1322 */
1253TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info) 1323TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info)
1254{ 1324{
1255 if (secret_key == NULL) 1325 if (secret_key == NULL) {
1256 return NULL; 1326 return NULL;
1327 }
1257 1328
1258 TCP_Connections *temp = calloc(1, sizeof(TCP_Connections)); 1329 TCP_Connections *temp = calloc(1, sizeof(TCP_Connections));
1259 1330
1260 if (temp == NULL) 1331 if (temp == NULL) {
1261 return NULL; 1332 return NULL;
1333 }
1262 1334
1263 memcpy(temp->self_secret_key, secret_key, crypto_box_SECRETKEYBYTES); 1335 memcpy(temp->self_secret_key, secret_key, crypto_box_SECRETKEYBYTES);
1264 crypto_scalarmult_curve25519_base(temp->self_public_key, temp->self_secret_key); 1336 crypto_scalarmult_curve25519_base(temp->self_public_key, temp->self_secret_key);
@@ -1311,8 +1383,9 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1311 1383
1312static void kill_nonused_tcp(TCP_Connections *tcp_c) 1384static void kill_nonused_tcp(TCP_Connections *tcp_c)
1313{ 1385{
1314 if (tcp_c->tcp_connections_length == 0) 1386 if (tcp_c->tcp_connections_length == 0) {
1315 return; 1387 return;
1388 }
1316 1389
1317 unsigned int i, num_online = 0, num_kill = 0, to_kill[tcp_c->tcp_connections_length]; 1390 unsigned int i, num_online = 0, num_kill = 0, to_kill[tcp_c->tcp_connections_length];
1318 1391
@@ -1336,8 +1409,9 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1336 } else { 1409 } else {
1337 unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS; 1410 unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
1338 1411
1339 if (n < num_kill) 1412 if (n < num_kill) {
1340 num_kill = n; 1413 num_kill = n;
1414 }
1341 } 1415 }
1342 1416
1343 for (i = 0; i < num_kill; ++i) { 1417 for (i = 0; i < num_kill; ++i) {