summaryrefslogtreecommitdiff
path: root/toxcore/TCP_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r--toxcore/TCP_client.c135
1 files changed, 90 insertions, 45 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 824e5dd1..641760ae 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -148,11 +148,13 @@ static int socks5_read_handshake_response(TCP_Client_Connection *TCP_conn)
148 uint8_t data[2]; 148 uint8_t data[2];
149 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); 149 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data));
150 150
151 if (ret == -1) 151 if (ret == -1) {
152 return 0; 152 return 0;
153 }
153 154
154 if (data[0] == 5 && data[1] == 0) // FIXME magic numbers 155 if (data[0] == 5 && data[1] == 0) { // FIXME magic numbers
155 return 1; 156 return 1;
157 }
156 158
157 return -1; 159 return -1;
158} 160}
@@ -193,21 +195,25 @@ static int proxy_socks5_read_connection_response(TCP_Client_Connection *TCP_conn
193 uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; 195 uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)];
194 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); 196 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data));
195 197
196 if (ret == -1) 198 if (ret == -1) {
197 return 0; 199 return 0;
200 }
198 201
199 if (data[0] == 5 && data[1] == 0) 202 if (data[0] == 5 && data[1] == 0) {
200 return 1; 203 return 1;
204 }
201 205
202 } else { 206 } else {
203 uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)]; 207 uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)];
204 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); 208 int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data));
205 209
206 if (ret == -1) 210 if (ret == -1) {
207 return 0; 211 return 0;
212 }
208 213
209 if (data[0] == 5 && data[1] == 0) 214 if (data[0] == 5 && data[1] == 0) {
210 return 1; 215 return 1;
216 }
211 } 217 }
212 218
213 return -1; 219 return -1;
@@ -227,8 +233,9 @@ static int generate_handshake(TCP_Client_Connection *TCP_conn)
227 int len = encrypt_data_symmetric(TCP_conn->shared_key, TCP_conn->last_packet + crypto_box_PUBLICKEYBYTES, plain, 233 int len = encrypt_data_symmetric(TCP_conn->shared_key, TCP_conn->last_packet + crypto_box_PUBLICKEYBYTES, plain,
228 sizeof(plain), TCP_conn->last_packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 234 sizeof(plain), TCP_conn->last_packet + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
229 235
230 if (len != sizeof(plain) + crypto_box_MACBYTES) 236 if (len != sizeof(plain) + crypto_box_MACBYTES) {
231 return -1; 237 return -1;
238 }
232 239
233 TCP_conn->last_packet_length = crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(plain) + crypto_box_MACBYTES; 240 TCP_conn->last_packet_length = crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + sizeof(plain) + crypto_box_MACBYTES;
234 TCP_conn->last_packet_sent = 0; 241 TCP_conn->last_packet_sent = 0;
@@ -246,8 +253,9 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data
246 int len = decrypt_data_symmetric(TCP_conn->shared_key, data, data + crypto_box_NONCEBYTES, 253 int len = decrypt_data_symmetric(TCP_conn->shared_key, data, data + crypto_box_NONCEBYTES,
247 TCP_SERVER_HANDSHAKE_SIZE - crypto_box_NONCEBYTES, plain); 254 TCP_SERVER_HANDSHAKE_SIZE - crypto_box_NONCEBYTES, plain);
248 255
249 if (len != sizeof(plain)) 256 if (len != sizeof(plain)) {
250 return -1; 257 return -1;
258 }
251 259
252 memcpy(TCP_conn->recv_nonce, plain + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); 260 memcpy(TCP_conn->recv_nonce, plain + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES);
253 encrypt_precompute(plain, TCP_conn->temp_secret_key, TCP_conn->shared_key); 261 encrypt_precompute(plain, TCP_conn->temp_secret_key, TCP_conn->shared_key);
@@ -267,8 +275,9 @@ static int send_pending_data_nonpriority(TCP_Client_Connection *con)
267 uint16_t left = con->last_packet_length - con->last_packet_sent; 275 uint16_t left = con->last_packet_length - con->last_packet_sent;
268 int len = send(con->sock, con->last_packet + con->last_packet_sent, left, MSG_NOSIGNAL); 276 int len = send(con->sock, con->last_packet + con->last_packet_sent, left, MSG_NOSIGNAL);
269 277
270 if (len <= 0) 278 if (len <= 0) {
271 return -1; 279 return -1;
280 }
272 281
273 if (len == left) { 282 if (len == left) {
274 con->last_packet_length = 0; 283 con->last_packet_length = 0;
@@ -365,8 +374,9 @@ static void wipe_priority_list(TCP_Client_Connection *con)
365static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length, 374static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length,
366 _Bool priority) 375 _Bool priority)
367{ 376{
368 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) 377 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) {
369 return -1; 378 return -1;
379 }
370 380
371 _Bool sendpriority = 1; 381 _Bool sendpriority = 1;
372 382
@@ -384,8 +394,9 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
384 memcpy(packet, &c_length, sizeof(uint16_t)); 394 memcpy(packet, &c_length, sizeof(uint16_t));
385 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 395 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
386 396
387 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) 397 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) {
388 return -1; 398 return -1;
399 }
389 400
390 if (priority) { 401 if (priority) {
391 len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; 402 len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0;
@@ -405,13 +416,15 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
405 416
406 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); 417 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL);
407 418
408 if (len <= 0) 419 if (len <= 0) {
409 return 0; 420 return 0;
421 }
410 422
411 increment_nonce(con->sent_nonce); 423 increment_nonce(con->sent_nonce);
412 424
413 if ((unsigned int)len == sizeof(packet)) 425 if ((unsigned int)len == sizeof(packet)) {
414 return 1; 426 return 1;
427 }
415 428
416 memcpy(con->last_packet, packet, sizeof(packet)); 429 memcpy(con->last_packet, packet, sizeof(packet));
417 con->last_packet_length = sizeof(packet); 430 con->last_packet_length = sizeof(packet);
@@ -454,14 +467,17 @@ static int send_ping_request(TCP_Client_Connection *con);
454 */ 467 */
455int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length) 468int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length)
456{ 469{
457 if (con_id >= NUM_CLIENT_CONNECTIONS) 470 if (con_id >= NUM_CLIENT_CONNECTIONS) {
458 return -1; 471 return -1;
472 }
459 473
460 if (con->connections[con_id].status != 2) 474 if (con->connections[con_id].status != 2) {
461 return -1; 475 return -1;
476 }
462 477
463 if (send_ping_response(con) == 0 || send_ping_request(con) == 0) 478 if (send_ping_response(con) == 0 || send_ping_request(con) == 0) {
464 return 0; 479 return 0;
480 }
465 481
466 uint8_t packet[1 + length]; 482 uint8_t packet[1 + length];
467 packet[0] = con_id + NUM_RESERVED_PORTS; 483 packet[0] = con_id + NUM_RESERVED_PORTS;
@@ -475,8 +491,9 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
475 */ 491 */
476int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length) 492int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length)
477{ 493{
478 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) 494 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) {
479 return -1; 495 return -1;
496 }
480 497
481 uint8_t packet[1 + crypto_box_PUBLICKEYBYTES + length]; 498 uint8_t packet[1 + crypto_box_PUBLICKEYBYTES + length];
482 packet[0] = TCP_PACKET_OOB_SEND; 499 packet[0] = TCP_PACKET_OOB_SEND;
@@ -495,11 +512,13 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const
495 */ 512 */
496int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32_t number) 513int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32_t number)
497{ 514{
498 if (con_id >= NUM_CLIENT_CONNECTIONS) 515 if (con_id >= NUM_CLIENT_CONNECTIONS) {
499 return -1; 516 return -1;
517 }
500 518
501 if (con->connections[con_id].status == 0) 519 if (con->connections[con_id].status == 0) {
502 return -1; 520 return -1;
521 }
503 522
504 con->connections[con_id].number = number; 523 con->connections[con_id].number = number;
505 return 0; 524 return 0;
@@ -537,8 +556,9 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id)
537 */ 556 */
538static int send_ping_request(TCP_Client_Connection *con) 557static int send_ping_request(TCP_Client_Connection *con)
539{ 558{
540 if (!con->ping_request_id) 559 if (!con->ping_request_id) {
541 return 1; 560 return 1;
561 }
542 562
543 uint8_t packet[1 + sizeof(uint64_t)]; 563 uint8_t packet[1 + sizeof(uint64_t)];
544 packet[0] = TCP_PACKET_PING; 564 packet[0] = TCP_PACKET_PING;
@@ -558,8 +578,9 @@ static int send_ping_request(TCP_Client_Connection *con)
558 */ 578 */
559static int send_ping_response(TCP_Client_Connection *con) 579static int send_ping_response(TCP_Client_Connection *con)
560{ 580{
561 if (!con->ping_response_id) 581 if (!con->ping_response_id) {
562 return 1; 582 return 1;
583 }
563 584
564 uint8_t packet[1 + sizeof(uint64_t)]; 585 uint8_t packet[1 + sizeof(uint64_t)];
565 packet[0] = TCP_PACKET_PONG; 586 packet[0] = TCP_PACKET_PONG;
@@ -579,8 +600,9 @@ static int send_ping_response(TCP_Client_Connection *con)
579 */ 600 */
580int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id) 601int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id)
581{ 602{
582 if (con_id >= NUM_CLIENT_CONNECTIONS) 603 if (con_id >= NUM_CLIENT_CONNECTIONS) {
583 return -1; 604 return -1;
605 }
584 606
585 con->connections[con_id].status = 0; 607 con->connections[con_id].status = 0;
586 con->connections[con_id].number = 0; 608 con->connections[con_id].number = 0;
@@ -615,8 +637,9 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
615 return NULL; 637 return NULL;
616 } 638 }
617 639
618 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) 640 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) {
619 return NULL; 641 return NULL;
642 }
620 643
621 uint8_t family = ip_port.ip.family; 644 uint8_t family = ip_port.ip.family;
622 645
@@ -694,80 +717,95 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
694 */ 717 */
695static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length, void *userdata) 718static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length, void *userdata)
696{ 719{
697 if (length <= 1) 720 if (length <= 1) {
698 return -1; 721 return -1;
722 }
699 723
700 switch (data[0]) { 724 switch (data[0]) {
701 case TCP_PACKET_ROUTING_RESPONSE: { 725 case TCP_PACKET_ROUTING_RESPONSE: {
702 if (length != 1 + 1 + crypto_box_PUBLICKEYBYTES) 726 if (length != 1 + 1 + crypto_box_PUBLICKEYBYTES) {
703 return -1; 727 return -1;
728 }
704 729
705 if (data[1] < NUM_RESERVED_PORTS) 730 if (data[1] < NUM_RESERVED_PORTS) {
706 return 0; 731 return 0;
732 }
707 733
708 uint8_t con_id = data[1] - NUM_RESERVED_PORTS; 734 uint8_t con_id = data[1] - NUM_RESERVED_PORTS;
709 735
710 if (conn->connections[con_id].status != 0) 736 if (conn->connections[con_id].status != 0) {
711 return 0; 737 return 0;
738 }
712 739
713 conn->connections[con_id].status = 1; 740 conn->connections[con_id].status = 1;
714 conn->connections[con_id].number = ~0; 741 conn->connections[con_id].number = ~0;
715 memcpy(conn->connections[con_id].public_key, data + 2, crypto_box_PUBLICKEYBYTES); 742 memcpy(conn->connections[con_id].public_key, data + 2, crypto_box_PUBLICKEYBYTES);
716 743
717 if (conn->response_callback) 744 if (conn->response_callback) {
718 conn->response_callback(conn->response_callback_object, con_id, conn->connections[con_id].public_key); 745 conn->response_callback(conn->response_callback_object, con_id, conn->connections[con_id].public_key);
746 }
719 747
720 return 0; 748 return 0;
721 } 749 }
722 750
723 case TCP_PACKET_CONNECTION_NOTIFICATION: { 751 case TCP_PACKET_CONNECTION_NOTIFICATION: {
724 if (length != 1 + 1) 752 if (length != 1 + 1) {
725 return -1; 753 return -1;
754 }
726 755
727 if (data[1] < NUM_RESERVED_PORTS) 756 if (data[1] < NUM_RESERVED_PORTS) {
728 return -1; 757 return -1;
758 }
729 759
730 uint8_t con_id = data[1] - NUM_RESERVED_PORTS; 760 uint8_t con_id = data[1] - NUM_RESERVED_PORTS;
731 761
732 if (conn->connections[con_id].status != 1) 762 if (conn->connections[con_id].status != 1) {
733 return 0; 763 return 0;
764 }
734 765
735 conn->connections[con_id].status = 2; 766 conn->connections[con_id].status = 2;
736 767
737 if (conn->status_callback) 768 if (conn->status_callback) {
738 conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id, 769 conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id,
739 conn->connections[con_id].status); 770 conn->connections[con_id].status);
771 }
740 772
741 return 0; 773 return 0;
742 } 774 }
743 775
744 case TCP_PACKET_DISCONNECT_NOTIFICATION: { 776 case TCP_PACKET_DISCONNECT_NOTIFICATION: {
745 if (length != 1 + 1) 777 if (length != 1 + 1) {
746 return -1; 778 return -1;
779 }
747 780
748 if (data[1] < NUM_RESERVED_PORTS) 781 if (data[1] < NUM_RESERVED_PORTS) {
749 return -1; 782 return -1;
783 }
750 784
751 uint8_t con_id = data[1] - NUM_RESERVED_PORTS; 785 uint8_t con_id = data[1] - NUM_RESERVED_PORTS;
752 786
753 if (conn->connections[con_id].status == 0) 787 if (conn->connections[con_id].status == 0) {
754 return 0; 788 return 0;
789 }
755 790
756 if (conn->connections[con_id].status != 2) 791 if (conn->connections[con_id].status != 2) {
757 return 0; 792 return 0;
793 }
758 794
759 conn->connections[con_id].status = 1; 795 conn->connections[con_id].status = 1;
760 796
761 if (conn->status_callback) 797 if (conn->status_callback) {
762 conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id, 798 conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id,
763 conn->connections[con_id].status); 799 conn->connections[con_id].status);
800 }
764 801
765 return 0; 802 return 0;
766 } 803 }
767 804
768 case TCP_PACKET_PING: { 805 case TCP_PACKET_PING: {
769 if (length != 1 + sizeof(uint64_t)) 806 if (length != 1 + sizeof(uint64_t)) {
770 return -1; 807 return -1;
808 }
771 809
772 uint64_t ping_id; 810 uint64_t ping_id;
773 memcpy(&ping_id, data + 1, sizeof(uint64_t)); 811 memcpy(&ping_id, data + 1, sizeof(uint64_t));
@@ -777,8 +815,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
777 } 815 }
778 816
779 case TCP_PACKET_PONG: { 817 case TCP_PACKET_PONG: {
780 if (length != 1 + sizeof(uint64_t)) 818 if (length != 1 + sizeof(uint64_t)) {
781 return -1; 819 return -1;
820 }
782 821
783 uint64_t ping_id; 822 uint64_t ping_id;
784 memcpy(&ping_id, data + 1, sizeof(uint64_t)); 823 memcpy(&ping_id, data + 1, sizeof(uint64_t));
@@ -795,12 +834,14 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
795 } 834 }
796 835
797 case TCP_PACKET_OOB_RECV: { 836 case TCP_PACKET_OOB_RECV: {
798 if (length <= 1 + crypto_box_PUBLICKEYBYTES) 837 if (length <= 1 + crypto_box_PUBLICKEYBYTES) {
799 return -1; 838 return -1;
839 }
800 840
801 if (conn->oob_data_callback) 841 if (conn->oob_data_callback) {
802 conn->oob_data_callback(conn->oob_data_callback_object, data + 1, data + 1 + crypto_box_PUBLICKEYBYTES, 842 conn->oob_data_callback(conn->oob_data_callback_object, data + 1, data + 1 + crypto_box_PUBLICKEYBYTES,
803 length - (1 + crypto_box_PUBLICKEYBYTES), userdata); 843 length - (1 + crypto_box_PUBLICKEYBYTES), userdata);
844 }
804 845
805 return 0; 846 return 0;
806 } 847 }
@@ -811,14 +852,16 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
811 } 852 }
812 853
813 default: { 854 default: {
814 if (data[0] < NUM_RESERVED_PORTS) 855 if (data[0] < NUM_RESERVED_PORTS) {
815 return -1; 856 return -1;
857 }
816 858
817 uint8_t con_id = data[0] - NUM_RESERVED_PORTS; 859 uint8_t con_id = data[0] - NUM_RESERVED_PORTS;
818 860
819 if (conn->data_callback) 861 if (conn->data_callback) {
820 conn->data_callback(conn->data_callback_object, conn->connections[con_id].number, con_id, data + 1, length - 1, 862 conn->data_callback(conn->data_callback_object, conn->connections[con_id].number, con_id, data + 1, length - 1,
821 userdata); 863 userdata);
864 }
822 } 865 }
823 } 866 }
824 867
@@ -837,8 +880,9 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata)
837 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) { 880 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
838 uint64_t ping_id = random_64b(); 881 uint64_t ping_id = random_64b();
839 882
840 if (!ping_id) 883 if (!ping_id) {
841 ++ping_id; 884 ++ping_id;
885 }
842 886
843 conn->ping_request_id = conn->ping_id = ping_id; 887 conn->ping_request_id = conn->ping_id = ping_id;
844 send_ping_request(conn); 888 send_ping_request(conn);
@@ -958,8 +1002,9 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata)
958 */ 1002 */
959void kill_TCP_connection(TCP_Client_Connection *TCP_connection) 1003void kill_TCP_connection(TCP_Client_Connection *TCP_connection)
960{ 1004{
961 if (TCP_connection == NULL) 1005 if (TCP_connection == NULL) {
962 return; 1006 return;
1007 }
963 1008
964 wipe_priority_list(TCP_connection); 1009 wipe_priority_list(TCP_connection);
965 kill_sock(TCP_connection->sock); 1010 kill_sock(TCP_connection->sock);