diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-08-31 19:12:19 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-08-31 20:04:16 +0100 |
commit | 633da98ae69866efb195e00d9a3a22ace6bada66 (patch) | |
tree | 875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxcore/TCP_server.c | |
parent | 6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff) |
Add braces to all if statements.
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r-- | toxcore/TCP_server.c | 168 |
1 files changed, 112 insertions, 56 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index d4944aed..d689e817 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -80,8 +80,9 @@ static int realloc_connection(TCP_Server *TCP_server, uint32_t num) | |||
80 | TCP_Secure_Connection *new_connections = realloc(TCP_server->accepted_connection_array, | 80 | TCP_Secure_Connection *new_connections = realloc(TCP_server->accepted_connection_array, |
81 | num * sizeof(TCP_Secure_Connection)); | 81 | num * sizeof(TCP_Secure_Connection)); |
82 | 82 | ||
83 | if (new_connections == NULL) | 83 | if (new_connections == NULL) { |
84 | return -1; | 84 | return -1; |
85 | } | ||
85 | 86 | ||
86 | if (num > TCP_server->size_accepted_connections) { | 87 | if (num > TCP_server->size_accepted_connections) { |
87 | uint32_t old_size = TCP_server->size_accepted_connections; | 88 | uint32_t old_size = TCP_server->size_accepted_connections; |
@@ -120,8 +121,9 @@ static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con | |||
120 | } | 121 | } |
121 | 122 | ||
122 | if (TCP_server->size_accepted_connections == TCP_server->num_accepted_connections) { | 123 | if (TCP_server->size_accepted_connections == TCP_server->num_accepted_connections) { |
123 | if (realloc_connection(TCP_server, TCP_server->size_accepted_connections + 4) == -1) | 124 | if (realloc_connection(TCP_server, TCP_server->size_accepted_connections + 4) == -1) { |
124 | return -1; | 125 | return -1; |
126 | } | ||
125 | 127 | ||
126 | index = TCP_server->num_accepted_connections; | 128 | index = TCP_server->num_accepted_connections; |
127 | } else { | 129 | } else { |
@@ -140,8 +142,9 @@ static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con | |||
140 | return -1; | 142 | return -1; |
141 | } | 143 | } |
142 | 144 | ||
143 | if (!bs_list_add(&TCP_server->accepted_key_list, con->public_key, index)) | 145 | if (!bs_list_add(&TCP_server->accepted_key_list, con->public_key, index)) { |
144 | return -1; | 146 | return -1; |
147 | } | ||
145 | 148 | ||
146 | memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection)); | 149 | memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection)); |
147 | TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; | 150 | TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; |
@@ -160,20 +163,24 @@ static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con | |||
160 | */ | 163 | */ |
161 | static int del_accepted(TCP_Server *TCP_server, int index) | 164 | static int del_accepted(TCP_Server *TCP_server, int index) |
162 | { | 165 | { |
163 | if ((uint32_t)index >= TCP_server->size_accepted_connections) | 166 | if ((uint32_t)index >= TCP_server->size_accepted_connections) { |
164 | return -1; | 167 | return -1; |
168 | } | ||
165 | 169 | ||
166 | if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) | 170 | if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) { |
167 | return -1; | 171 | return -1; |
172 | } | ||
168 | 173 | ||
169 | if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index)) | 174 | if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index)) { |
170 | return -1; | 175 | return -1; |
176 | } | ||
171 | 177 | ||
172 | sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection)); | 178 | sodium_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection)); |
173 | --TCP_server->num_accepted_connections; | 179 | --TCP_server->num_accepted_connections; |
174 | 180 | ||
175 | if (TCP_server->num_accepted_connections == 0) | 181 | if (TCP_server->num_accepted_connections == 0) { |
176 | realloc_connection(TCP_server, 0); | 182 | realloc_connection(TCP_server, 0); |
183 | } | ||
177 | 184 | ||
178 | return 0; | 185 | return 0; |
179 | } | 186 | } |
@@ -259,30 +266,35 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, | |||
259 | if (*next_packet_length == 0) { | 266 | if (*next_packet_length == 0) { |
260 | uint16_t len = read_TCP_length(sock); | 267 | uint16_t len = read_TCP_length(sock); |
261 | 268 | ||
262 | if (len == (uint16_t)~0) | 269 | if (len == (uint16_t)~0) { |
263 | return -1; | 270 | return -1; |
271 | } | ||
264 | 272 | ||
265 | if (len == 0) | 273 | if (len == 0) { |
266 | return 0; | 274 | return 0; |
275 | } | ||
267 | 276 | ||
268 | *next_packet_length = len; | 277 | *next_packet_length = len; |
269 | } | 278 | } |
270 | 279 | ||
271 | if (max_len + crypto_box_MACBYTES < *next_packet_length) | 280 | if (max_len + crypto_box_MACBYTES < *next_packet_length) { |
272 | return -1; | 281 | return -1; |
282 | } | ||
273 | 283 | ||
274 | uint8_t data_encrypted[*next_packet_length]; | 284 | uint8_t data_encrypted[*next_packet_length]; |
275 | int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length); | 285 | int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length); |
276 | 286 | ||
277 | if (len_packet != *next_packet_length) | 287 | if (len_packet != *next_packet_length) { |
278 | return 0; | 288 | return 0; |
289 | } | ||
279 | 290 | ||
280 | *next_packet_length = 0; | 291 | *next_packet_length = 0; |
281 | 292 | ||
282 | int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data); | 293 | int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data); |
283 | 294 | ||
284 | if (len + crypto_box_MACBYTES != len_packet) | 295 | if (len + crypto_box_MACBYTES != len_packet) { |
285 | return -1; | 296 | return -1; |
297 | } | ||
286 | 298 | ||
287 | increment_nonce(recv_nonce); | 299 | increment_nonce(recv_nonce); |
288 | 300 | ||
@@ -301,8 +313,9 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con) | |||
301 | uint16_t left = con->last_packet_length - con->last_packet_sent; | 313 | uint16_t left = con->last_packet_length - con->last_packet_sent; |
302 | int len = send(con->sock, con->last_packet + con->last_packet_sent, left, MSG_NOSIGNAL); | 314 | int len = send(con->sock, con->last_packet + con->last_packet_sent, left, MSG_NOSIGNAL); |
303 | 315 | ||
304 | if (len <= 0) | 316 | if (len <= 0) { |
305 | return -1; | 317 | return -1; |
318 | } | ||
306 | 319 | ||
307 | if (len == left) { | 320 | if (len == left) { |
308 | con->last_packet_length = 0; | 321 | con->last_packet_length = 0; |
@@ -388,8 +401,9 @@ static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uin | |||
388 | static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, | 401 | static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, |
389 | _Bool priority) | 402 | _Bool priority) |
390 | { | 403 | { |
391 | if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) | 404 | if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) { |
392 | return -1; | 405 | return -1; |
406 | } | ||
393 | 407 | ||
394 | _Bool sendpriority = 1; | 408 | _Bool sendpriority = 1; |
395 | 409 | ||
@@ -407,8 +421,9 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const | |||
407 | memcpy(packet, &c_length, sizeof(uint16_t)); | 421 | memcpy(packet, &c_length, sizeof(uint16_t)); |
408 | int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); | 422 | int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); |
409 | 423 | ||
410 | if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) | 424 | if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) { |
411 | return -1; | 425 | return -1; |
426 | } | ||
412 | 427 | ||
413 | if (priority) { | 428 | if (priority) { |
414 | len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; | 429 | len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; |
@@ -428,13 +443,15 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const | |||
428 | 443 | ||
429 | len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); | 444 | len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); |
430 | 445 | ||
431 | if (len <= 0) | 446 | if (len <= 0) { |
432 | return 0; | 447 | return 0; |
448 | } | ||
433 | 449 | ||
434 | increment_nonce(con->sent_nonce); | 450 | increment_nonce(con->sent_nonce); |
435 | 451 | ||
436 | if ((unsigned int)len == sizeof(packet)) | 452 | if ((unsigned int)len == sizeof(packet)) { |
437 | return 1; | 453 | return 1; |
454 | } | ||
438 | 455 | ||
439 | memcpy(con->last_packet, packet, sizeof(packet)); | 456 | memcpy(con->last_packet, packet, sizeof(packet)); |
440 | con->last_packet_length = sizeof(packet); | 457 | con->last_packet_length = sizeof(packet); |
@@ -459,8 +476,9 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co | |||
459 | */ | 476 | */ |
460 | static int kill_accepted(TCP_Server *TCP_server, int index) | 477 | static int kill_accepted(TCP_Server *TCP_server, int index) |
461 | { | 478 | { |
462 | if ((uint32_t)index >= TCP_server->size_accepted_connections) | 479 | if ((uint32_t)index >= TCP_server->size_accepted_connections) { |
463 | return -1; | 480 | return -1; |
481 | } | ||
464 | 482 | ||
465 | uint32_t i; | 483 | uint32_t i; |
466 | 484 | ||
@@ -470,8 +488,9 @@ static int kill_accepted(TCP_Server *TCP_server, int index) | |||
470 | 488 | ||
471 | sock_t sock = TCP_server->accepted_connection_array[index].sock; | 489 | sock_t sock = TCP_server->accepted_connection_array[index].sock; |
472 | 490 | ||
473 | if (del_accepted(TCP_server, index) != 0) | 491 | if (del_accepted(TCP_server, index) != 0) { |
474 | return -1; | 492 | return -1; |
493 | } | ||
475 | 494 | ||
476 | kill_sock(sock); | 495 | kill_sock(sock); |
477 | return 0; | 496 | return 0; |
@@ -483,11 +502,13 @@ static int kill_accepted(TCP_Server *TCP_server, int index) | |||
483 | static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, | 502 | static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, |
484 | const uint8_t *self_secret_key) | 503 | const uint8_t *self_secret_key) |
485 | { | 504 | { |
486 | if (length != TCP_CLIENT_HANDSHAKE_SIZE) | 505 | if (length != TCP_CLIENT_HANDSHAKE_SIZE) { |
487 | return -1; | 506 | return -1; |
507 | } | ||
488 | 508 | ||
489 | if (con->status != TCP_STATUS_CONNECTED) | 509 | if (con->status != TCP_STATUS_CONNECTED) { |
490 | return -1; | 510 | return -1; |
511 | } | ||
491 | 512 | ||
492 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 513 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
493 | encrypt_precompute(data, self_secret_key, shared_key); | 514 | encrypt_precompute(data, self_secret_key, shared_key); |
@@ -495,8 +516,9 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, | |||
495 | int len = decrypt_data_symmetric(shared_key, data + crypto_box_PUBLICKEYBYTES, | 516 | int len = decrypt_data_symmetric(shared_key, data + crypto_box_PUBLICKEYBYTES, |
496 | data + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES, plain); | 517 | data + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES, plain); |
497 | 518 | ||
498 | if (len != TCP_HANDSHAKE_PLAIN_SIZE) | 519 | if (len != TCP_HANDSHAKE_PLAIN_SIZE) { |
499 | return -1; | 520 | return -1; |
521 | } | ||
500 | 522 | ||
501 | memcpy(con->public_key, data, crypto_box_PUBLICKEYBYTES); | 523 | memcpy(con->public_key, data, crypto_box_PUBLICKEYBYTES); |
502 | uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; | 524 | uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; |
@@ -512,11 +534,13 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, | |||
512 | len = encrypt_data_symmetric(shared_key, response, resp_plain, TCP_HANDSHAKE_PLAIN_SIZE, | 534 | len = encrypt_data_symmetric(shared_key, response, resp_plain, TCP_HANDSHAKE_PLAIN_SIZE, |
513 | response + crypto_box_NONCEBYTES); | 535 | response + crypto_box_NONCEBYTES); |
514 | 536 | ||
515 | if (len != TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES) | 537 | if (len != TCP_HANDSHAKE_PLAIN_SIZE + crypto_box_MACBYTES) { |
516 | return -1; | 538 | return -1; |
539 | } | ||
517 | 540 | ||
518 | if (TCP_SERVER_HANDSHAKE_SIZE != send(con->sock, response, TCP_SERVER_HANDSHAKE_SIZE, MSG_NOSIGNAL)) | 541 | if (TCP_SERVER_HANDSHAKE_SIZE != send(con->sock, response, TCP_SERVER_HANDSHAKE_SIZE, MSG_NOSIGNAL)) { |
519 | return -1; | 542 | return -1; |
543 | } | ||
520 | 544 | ||
521 | encrypt_precompute(plain, temp_secret_key, con->shared_key); | 545 | encrypt_precompute(plain, temp_secret_key, con->shared_key); |
522 | con->status = TCP_STATUS_UNCONFIRMED; | 546 | con->status = TCP_STATUS_UNCONFIRMED; |
@@ -584,8 +608,9 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const | |||
584 | 608 | ||
585 | /* If person tries to cennect to himself we deny the request*/ | 609 | /* If person tries to cennect to himself we deny the request*/ |
586 | if (public_key_cmp(con->public_key, public_key) == 0) { | 610 | if (public_key_cmp(con->public_key, public_key) == 0) { |
587 | if (send_routing_response(con, 0, public_key) == -1) | 611 | if (send_routing_response(con, 0, public_key) == -1) { |
588 | return -1; | 612 | return -1; |
613 | } | ||
589 | 614 | ||
590 | return 0; | 615 | return 0; |
591 | } | 616 | } |
@@ -605,19 +630,22 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const | |||
605 | } | 630 | } |
606 | 631 | ||
607 | if (index == (uint32_t)~0) { | 632 | if (index == (uint32_t)~0) { |
608 | if (send_routing_response(con, 0, public_key) == -1) | 633 | if (send_routing_response(con, 0, public_key) == -1) { |
609 | return -1; | 634 | return -1; |
635 | } | ||
610 | 636 | ||
611 | return 0; | 637 | return 0; |
612 | } | 638 | } |
613 | 639 | ||
614 | int ret = send_routing_response(con, index + NUM_RESERVED_PORTS, public_key); | 640 | int ret = send_routing_response(con, index + NUM_RESERVED_PORTS, public_key); |
615 | 641 | ||
616 | if (ret == 0) | 642 | if (ret == 0) { |
617 | return 0; | 643 | return 0; |
644 | } | ||
618 | 645 | ||
619 | if (ret == -1) | 646 | if (ret == -1) { |
620 | return -1; | 647 | return -1; |
648 | } | ||
621 | 649 | ||
622 | con->connections[index].status = 1; | 650 | con->connections[index].status = 1; |
623 | memcpy(con->connections[index].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 651 | memcpy(con->connections[index].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
@@ -657,8 +685,9 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const | |||
657 | static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key, const uint8_t *data, | 685 | static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key, const uint8_t *data, |
658 | uint16_t length) | 686 | uint16_t length) |
659 | { | 687 | { |
660 | if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) | 688 | if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) { |
661 | return -1; | 689 | return -1; |
690 | } | ||
662 | 691 | ||
663 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; | 692 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; |
664 | 693 | ||
@@ -683,8 +712,9 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui | |||
683 | */ | 712 | */ |
684 | static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) | 713 | static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) |
685 | { | 714 | { |
686 | if (con_number >= NUM_CLIENT_CONNECTIONS) | 715 | if (con_number >= NUM_CLIENT_CONNECTIONS) { |
687 | return -1; | 716 | return -1; |
717 | } | ||
688 | 718 | ||
689 | if (con->connections[con_number].status) { | 719 | if (con->connections[con_number].status) { |
690 | uint32_t index = con->connections[con_number].index; | 720 | uint32_t index = con->connections[con_number].index; |
@@ -692,8 +722,9 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co | |||
692 | 722 | ||
693 | if (con->connections[con_number].status == 2) { | 723 | if (con->connections[con_number].status == 2) { |
694 | 724 | ||
695 | if (index >= TCP_server->size_accepted_connections) | 725 | if (index >= TCP_server->size_accepted_connections) { |
696 | return -1; | 726 | return -1; |
727 | } | ||
697 | 728 | ||
698 | TCP_server->accepted_connection_array[index].connections[other_id].other_id = 0; | 729 | TCP_server->accepted_connection_array[index].connections[other_id].other_id = 0; |
699 | TCP_server->accepted_connection_array[index].connections[other_id].index = 0; | 730 | TCP_server->accepted_connection_array[index].connections[other_id].index = 0; |
@@ -716,20 +747,23 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, | |||
716 | TCP_Server *TCP_server = object; | 747 | TCP_Server *TCP_server = object; |
717 | uint32_t index = dest.ip.ip6.uint32[0]; | 748 | uint32_t index = dest.ip.ip6.uint32[0]; |
718 | 749 | ||
719 | if (index >= TCP_server->size_accepted_connections) | 750 | if (index >= TCP_server->size_accepted_connections) { |
720 | return 1; | 751 | return 1; |
752 | } | ||
721 | 753 | ||
722 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[index]; | 754 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[index]; |
723 | 755 | ||
724 | if (con->identifier != dest.ip.ip6.uint64[1]) | 756 | if (con->identifier != dest.ip.ip6.uint64[1]) { |
725 | return 1; | 757 | return 1; |
758 | } | ||
726 | 759 | ||
727 | uint8_t packet[1 + length]; | 760 | uint8_t packet[1 + length]; |
728 | memcpy(packet + 1, data, length); | 761 | memcpy(packet + 1, data, length); |
729 | packet[0] = TCP_PACKET_ONION_RESPONSE; | 762 | packet[0] = TCP_PACKET_ONION_RESPONSE; |
730 | 763 | ||
731 | if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1) | 764 | if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1) { |
732 | return 1; | 765 | return 1; |
766 | } | ||
733 | 767 | ||
734 | return 0; | 768 | return 0; |
735 | } | 769 | } |
@@ -739,36 +773,41 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, | |||
739 | */ | 773 | */ |
740 | static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *data, uint16_t length) | 774 | static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *data, uint16_t length) |
741 | { | 775 | { |
742 | if (length == 0) | 776 | if (length == 0) { |
743 | return -1; | 777 | return -1; |
778 | } | ||
744 | 779 | ||
745 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; | 780 | TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; |
746 | 781 | ||
747 | switch (data[0]) { | 782 | switch (data[0]) { |
748 | case TCP_PACKET_ROUTING_REQUEST: { | 783 | case TCP_PACKET_ROUTING_REQUEST: { |
749 | if (length != 1 + crypto_box_PUBLICKEYBYTES) | 784 | if (length != 1 + crypto_box_PUBLICKEYBYTES) { |
750 | return -1; | 785 | return -1; |
786 | } | ||
751 | 787 | ||
752 | return handle_TCP_routing_req(TCP_server, con_id, data + 1); | 788 | return handle_TCP_routing_req(TCP_server, con_id, data + 1); |
753 | } | 789 | } |
754 | 790 | ||
755 | case TCP_PACKET_CONNECTION_NOTIFICATION: { | 791 | case TCP_PACKET_CONNECTION_NOTIFICATION: { |
756 | if (length != 2) | 792 | if (length != 2) { |
757 | return -1; | 793 | return -1; |
794 | } | ||
758 | 795 | ||
759 | break; | 796 | break; |
760 | } | 797 | } |
761 | 798 | ||
762 | case TCP_PACKET_DISCONNECT_NOTIFICATION: { | 799 | case TCP_PACKET_DISCONNECT_NOTIFICATION: { |
763 | if (length != 2) | 800 | if (length != 2) { |
764 | return -1; | 801 | return -1; |
802 | } | ||
765 | 803 | ||
766 | return rm_connection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); | 804 | return rm_connection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); |
767 | } | 805 | } |
768 | 806 | ||
769 | case TCP_PACKET_PING: { | 807 | case TCP_PACKET_PING: { |
770 | if (length != 1 + sizeof(uint64_t)) | 808 | if (length != 1 + sizeof(uint64_t)) { |
771 | return -1; | 809 | return -1; |
810 | } | ||
772 | 811 | ||
773 | uint8_t response[1 + sizeof(uint64_t)]; | 812 | uint8_t response[1 + sizeof(uint64_t)]; |
774 | response[0] = TCP_PACKET_PONG; | 813 | response[0] = TCP_PACKET_PONG; |
@@ -778,8 +817,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
778 | } | 817 | } |
779 | 818 | ||
780 | case TCP_PACKET_PONG: { | 819 | case TCP_PACKET_PONG: { |
781 | if (length != 1 + sizeof(uint64_t)) | 820 | if (length != 1 + sizeof(uint64_t)) { |
782 | return -1; | 821 | return -1; |
822 | } | ||
783 | 823 | ||
784 | uint64_t ping_id; | 824 | uint64_t ping_id; |
785 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); | 825 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); |
@@ -796,8 +836,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
796 | } | 836 | } |
797 | 837 | ||
798 | case TCP_PACKET_OOB_SEND: { | 838 | case TCP_PACKET_OOB_SEND: { |
799 | if (length <= 1 + crypto_box_PUBLICKEYBYTES) | 839 | if (length <= 1 + crypto_box_PUBLICKEYBYTES) { |
800 | return -1; | 840 | return -1; |
841 | } | ||
801 | 842 | ||
802 | return handle_TCP_oob_send(TCP_server, con_id, data + 1, data + 1 + crypto_box_PUBLICKEYBYTES, | 843 | return handle_TCP_oob_send(TCP_server, con_id, data + 1, data + 1 + crypto_box_PUBLICKEYBYTES, |
803 | length - (1 + crypto_box_PUBLICKEYBYTES)); | 844 | length - (1 + crypto_box_PUBLICKEYBYTES)); |
@@ -805,8 +846,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
805 | 846 | ||
806 | case TCP_PACKET_ONION_REQUEST: { | 847 | case TCP_PACKET_ONION_REQUEST: { |
807 | if (TCP_server->onion) { | 848 | if (TCP_server->onion) { |
808 | if (length <= 1 + crypto_box_NONCEBYTES + ONION_SEND_BASE * 2) | 849 | if (length <= 1 + crypto_box_NONCEBYTES + ONION_SEND_BASE * 2) { |
809 | return -1; | 850 | return -1; |
851 | } | ||
810 | 852 | ||
811 | IP_Port source; | 853 | IP_Port source; |
812 | source.port = 0; // dummy initialise | 854 | source.port = 0; // dummy initialise |
@@ -826,19 +868,23 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
826 | } | 868 | } |
827 | 869 | ||
828 | default: { | 870 | default: { |
829 | if (data[0] < NUM_RESERVED_PORTS) | 871 | if (data[0] < NUM_RESERVED_PORTS) { |
830 | return -1; | 872 | return -1; |
873 | } | ||
831 | 874 | ||
832 | uint8_t c_id = data[0] - NUM_RESERVED_PORTS; | 875 | uint8_t c_id = data[0] - NUM_RESERVED_PORTS; |
833 | 876 | ||
834 | if (c_id >= NUM_CLIENT_CONNECTIONS) | 877 | if (c_id >= NUM_CLIENT_CONNECTIONS) { |
835 | return -1; | 878 | return -1; |
879 | } | ||
836 | 880 | ||
837 | if (con->connections[c_id].status == 0) | 881 | if (con->connections[c_id].status == 0) { |
838 | return -1; | 882 | return -1; |
883 | } | ||
839 | 884 | ||
840 | if (con->connections[c_id].status != 2) | 885 | if (con->connections[c_id].status != 2) { |
841 | return 0; | 886 | return 0; |
887 | } | ||
842 | 888 | ||
843 | uint32_t index = con->connections[c_id].index; | 889 | uint32_t index = con->connections[c_id].index; |
844 | uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS; | 890 | uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS; |
@@ -847,8 +893,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
847 | new_data[0] = other_c_id; | 893 | new_data[0] = other_c_id; |
848 | int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); | 894 | int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); |
849 | 895 | ||
850 | if (ret == -1) | 896 | if (ret == -1) { |
851 | return -1; | 897 | return -1; |
898 | } | ||
852 | 899 | ||
853 | return 0; | 900 | return 0; |
854 | } | 901 | } |
@@ -883,8 +930,9 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection | |||
883 | */ | 930 | */ |
884 | static int accept_connection(TCP_Server *TCP_server, sock_t sock) | 931 | static int accept_connection(TCP_Server *TCP_server, sock_t sock) |
885 | { | 932 | { |
886 | if (!sock_valid(sock)) | 933 | if (!sock_valid(sock)) { |
887 | return -1; | 934 | return -1; |
935 | } | ||
888 | 936 | ||
889 | if (!set_socket_nonblock(sock)) { | 937 | if (!set_socket_nonblock(sock)) { |
890 | kill_sock(sock); | 938 | kill_sock(sock); |
@@ -900,8 +948,9 @@ static int accept_connection(TCP_Server *TCP_server, sock_t sock) | |||
900 | 948 | ||
901 | TCP_Secure_Connection *conn = &TCP_server->incomming_connection_queue[index]; | 949 | TCP_Secure_Connection *conn = &TCP_server->incomming_connection_queue[index]; |
902 | 950 | ||
903 | if (conn->status != TCP_STATUS_NO_STATUS) | 951 | if (conn->status != TCP_STATUS_NO_STATUS) { |
904 | kill_TCP_connection(conn); | 952 | kill_TCP_connection(conn); |
953 | } | ||
905 | 954 | ||
906 | conn->status = TCP_STATUS_CONNECTED; | 955 | conn->status = TCP_STATUS_CONNECTED; |
907 | conn->sock = sock; | 956 | conn->sock = sock; |
@@ -942,8 +991,9 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port) | |||
942 | TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, | 991 | TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, |
943 | Onion *onion) | 992 | Onion *onion) |
944 | { | 993 | { |
945 | if (num_sockets == 0 || ports == NULL) | 994 | if (num_sockets == 0 || ports == NULL) { |
946 | return NULL; | 995 | return NULL; |
996 | } | ||
947 | 997 | ||
948 | if (networking_at_startup() != 0) { | 998 | if (networking_at_startup() != 0) { |
949 | return NULL; | 999 | return NULL; |
@@ -951,8 +1001,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin | |||
951 | 1001 | ||
952 | TCP_Server *temp = calloc(1, sizeof(TCP_Server)); | 1002 | TCP_Server *temp = calloc(1, sizeof(TCP_Server)); |
953 | 1003 | ||
954 | if (temp == NULL) | 1004 | if (temp == NULL) { |
955 | return NULL; | 1005 | return NULL; |
1006 | } | ||
956 | 1007 | ||
957 | temp->socks_listening = calloc(num_sockets, sizeof(sock_t)); | 1008 | temp->socks_listening = calloc(num_sockets, sizeof(sock_t)); |
958 | 1009 | ||
@@ -1040,8 +1091,9 @@ static void do_TCP_accept_new(TCP_Server *TCP_server) | |||
1040 | 1091 | ||
1041 | static int do_incoming(TCP_Server *TCP_server, uint32_t i) | 1092 | static int do_incoming(TCP_Server *TCP_server, uint32_t i) |
1042 | { | 1093 | { |
1043 | if (TCP_server->incomming_connection_queue[i].status != TCP_STATUS_CONNECTED) | 1094 | if (TCP_server->incomming_connection_queue[i].status != TCP_STATUS_CONNECTED) { |
1044 | return -1; | 1095 | return -1; |
1096 | } | ||
1045 | 1097 | ||
1046 | int ret = read_connection_handshake(&TCP_server->incomming_connection_queue[i], TCP_server->secret_key); | 1098 | int ret = read_connection_handshake(&TCP_server->incomming_connection_queue[i], TCP_server->secret_key); |
1047 | 1099 | ||
@@ -1052,8 +1104,9 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i) | |||
1052 | TCP_Secure_Connection *conn_old = &TCP_server->incomming_connection_queue[i]; | 1104 | TCP_Secure_Connection *conn_old = &TCP_server->incomming_connection_queue[i]; |
1053 | TCP_Secure_Connection *conn_new = &TCP_server->unconfirmed_connection_queue[index_new]; | 1105 | TCP_Secure_Connection *conn_new = &TCP_server->unconfirmed_connection_queue[index_new]; |
1054 | 1106 | ||
1055 | if (conn_new->status != TCP_STATUS_NO_STATUS) | 1107 | if (conn_new->status != TCP_STATUS_NO_STATUS) { |
1056 | kill_TCP_connection(conn_new); | 1108 | kill_TCP_connection(conn_new); |
1109 | } | ||
1057 | 1110 | ||
1058 | memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection)); | 1111 | memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection)); |
1059 | sodium_memzero(conn_old, sizeof(TCP_Secure_Connection)); | 1112 | sodium_memzero(conn_old, sizeof(TCP_Secure_Connection)); |
@@ -1069,8 +1122,9 @@ static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i) | |||
1069 | { | 1122 | { |
1070 | TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i]; | 1123 | TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i]; |
1071 | 1124 | ||
1072 | if (conn->status != TCP_STATUS_UNCONFIRMED) | 1125 | if (conn->status != TCP_STATUS_UNCONFIRMED) { |
1073 | return -1; | 1126 | return -1; |
1127 | } | ||
1074 | 1128 | ||
1075 | uint8_t packet[MAX_PACKET_SIZE]; | 1129 | uint8_t packet[MAX_PACKET_SIZE]; |
1076 | int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce, | 1130 | int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce, |
@@ -1139,16 +1193,18 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) | |||
1139 | for (i = 0; i < TCP_server->size_accepted_connections; ++i) { | 1193 | for (i = 0; i < TCP_server->size_accepted_connections; ++i) { |
1140 | TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i]; | 1194 | TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i]; |
1141 | 1195 | ||
1142 | if (conn->status != TCP_STATUS_CONFIRMED) | 1196 | if (conn->status != TCP_STATUS_CONFIRMED) { |
1143 | continue; | 1197 | continue; |
1198 | } | ||
1144 | 1199 | ||
1145 | if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { | 1200 | if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { |
1146 | uint8_t ping[1 + sizeof(uint64_t)]; | 1201 | uint8_t ping[1 + sizeof(uint64_t)]; |
1147 | ping[0] = TCP_PACKET_PING; | 1202 | ping[0] = TCP_PACKET_PING; |
1148 | uint64_t ping_id = random_64b(); | 1203 | uint64_t ping_id = random_64b(); |
1149 | 1204 | ||
1150 | if (!ping_id) | 1205 | if (!ping_id) { |
1151 | ++ping_id; | 1206 | ++ping_id; |
1207 | } | ||
1152 | 1208 | ||
1153 | memcpy(ping + 1, &ping_id, sizeof(uint64_t)); | 1209 | memcpy(ping + 1, &ping_id, sizeof(uint64_t)); |
1154 | int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1); | 1210 | int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1); |