diff options
author | iphydf <iphydf@users.noreply.github.com> | 2017-06-04 12:59:34 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2017-06-04 17:48:23 +0000 |
commit | 1e8fa85aadf602bdca3a540de09a8184f7139a6c (patch) | |
tree | 4caab80957f7f9a2098fb97e776be763defc44ab /toxcore/TCP_client.c | |
parent | 03f99bde2ecee3722d47a5b6a1031fc88e7210b7 (diff) |
Add a monolith_test that includes all toxcore sources.
This requires that every symbol, even if static (file-scope), is unique.
The idea is that we can easily run "whole" program static analysis on
programs that include monolith.h ("whole" is in quotes, as we don't
include dependencies like libsodium in this static analysis).
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r-- | toxcore/TCP_client.c | 64 |
1 files changed, 32 insertions, 32 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 3e6de14a..e584b265 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c | |||
@@ -244,7 +244,7 @@ static int handle_handshake(TCP_Client_Connection *TCP_conn, const uint8_t *data | |||
244 | /* return 0 if pending data was sent completely | 244 | /* return 0 if pending data was sent completely |
245 | * return -1 if it wasn't | 245 | * return -1 if it wasn't |
246 | */ | 246 | */ |
247 | static int send_pending_data_nonpriority(TCP_Client_Connection *con) | 247 | static int client_send_pending_data_nonpriority(TCP_Client_Connection *con) |
248 | { | 248 | { |
249 | if (con->last_packet_length == 0) { | 249 | if (con->last_packet_length == 0) { |
250 | return 0; | 250 | return 0; |
@@ -270,10 +270,10 @@ static int send_pending_data_nonpriority(TCP_Client_Connection *con) | |||
270 | /* return 0 if pending data was sent completely | 270 | /* return 0 if pending data was sent completely |
271 | * return -1 if it wasn't | 271 | * return -1 if it wasn't |
272 | */ | 272 | */ |
273 | static int send_pending_data(TCP_Client_Connection *con) | 273 | static int client_send_pending_data(TCP_Client_Connection *con) |
274 | { | 274 | { |
275 | /* finish sending current non-priority packet */ | 275 | /* finish sending current non-priority packet */ |
276 | if (send_pending_data_nonpriority(con) == -1) { | 276 | if (client_send_pending_data_nonpriority(con) == -1) { |
277 | return -1; | 277 | return -1; |
278 | } | 278 | } |
279 | 279 | ||
@@ -309,7 +309,7 @@ static int send_pending_data(TCP_Client_Connection *con) | |||
309 | /* return 0 on failure (only if malloc fails) | 309 | /* return 0 on failure (only if malloc fails) |
310 | * return 1 on success | 310 | * return 1 on success |
311 | */ | 311 | */ |
312 | static bool add_priority(TCP_Client_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) | 312 | static bool client_add_priority(TCP_Client_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) |
313 | { | 313 | { |
314 | TCP_Priority_List *p = con->priority_queue_end; | 314 | TCP_Priority_List *p = con->priority_queue_end; |
315 | TCP_Priority_List *new_list = (TCP_Priority_List *)malloc(sizeof(TCP_Priority_List) + size); | 315 | TCP_Priority_List *new_list = (TCP_Priority_List *)malloc(sizeof(TCP_Priority_List) + size); |
@@ -348,7 +348,7 @@ static void wipe_priority_list(TCP_Client_Connection *con) | |||
348 | * return 0 if could not send packet. | 348 | * return 0 if could not send packet. |
349 | * return -1 on failure (connection must be killed). | 349 | * return -1 on failure (connection must be killed). |
350 | */ | 350 | */ |
351 | static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length, | 351 | static int write_packet_TCP_client_secure_connection(TCP_Client_Connection *con, const uint8_t *data, uint16_t length, |
352 | bool priority) | 352 | bool priority) |
353 | { | 353 | { |
354 | if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE) { | 354 | if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE) { |
@@ -357,7 +357,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const | |||
357 | 357 | ||
358 | bool sendpriority = 1; | 358 | bool sendpriority = 1; |
359 | 359 | ||
360 | if (send_pending_data(con) == -1) { | 360 | if (client_send_pending_data(con) == -1) { |
361 | if (priority) { | 361 | if (priority) { |
362 | sendpriority = 0; | 362 | sendpriority = 0; |
363 | } else { | 363 | } else { |
@@ -388,7 +388,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const | |||
388 | return 1; | 388 | return 1; |
389 | } | 389 | } |
390 | 390 | ||
391 | return add_priority(con, packet, SIZEOF_VLA(packet), len); | 391 | return client_add_priority(con, packet, SIZEOF_VLA(packet), len); |
392 | } | 392 | } |
393 | 393 | ||
394 | len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); | 394 | len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); |
@@ -418,7 +418,7 @@ int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key) | |||
418 | uint8_t packet[1 + CRYPTO_PUBLIC_KEY_SIZE]; | 418 | uint8_t packet[1 + CRYPTO_PUBLIC_KEY_SIZE]; |
419 | packet[0] = TCP_PACKET_ROUTING_REQUEST; | 419 | packet[0] = TCP_PACKET_ROUTING_REQUEST; |
420 | memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 420 | memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
421 | return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1); | 421 | return write_packet_TCP_client_secure_connection(con, packet, sizeof(packet), 1); |
422 | } | 422 | } |
423 | 423 | ||
424 | void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, | 424 | void routing_response_handler(TCP_Client_Connection *con, int (*response_callback)(void *object, uint8_t connection_id, |
@@ -435,8 +435,8 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v | |||
435 | con->status_callback_object = object; | 435 | con->status_callback_object = object; |
436 | } | 436 | } |
437 | 437 | ||
438 | static int send_ping_response(TCP_Client_Connection *con); | 438 | static int tcp_send_ping_response(TCP_Client_Connection *con); |
439 | static int send_ping_request(TCP_Client_Connection *con); | 439 | static int tcp_send_ping_request(TCP_Client_Connection *con); |
440 | 440 | ||
441 | /* return 1 on success. | 441 | /* return 1 on success. |
442 | * return 0 if could not send packet. | 442 | * return 0 if could not send packet. |
@@ -452,14 +452,14 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u | |||
452 | return -1; | 452 | return -1; |
453 | } | 453 | } |
454 | 454 | ||
455 | if (send_ping_response(con) == 0 || send_ping_request(con) == 0) { | 455 | if (tcp_send_ping_response(con) == 0 || tcp_send_ping_request(con) == 0) { |
456 | return 0; | 456 | return 0; |
457 | } | 457 | } |
458 | 458 | ||
459 | VLA(uint8_t, packet, 1 + length); | 459 | VLA(uint8_t, packet, 1 + length); |
460 | packet[0] = con_id + NUM_RESERVED_PORTS; | 460 | packet[0] = con_id + NUM_RESERVED_PORTS; |
461 | memcpy(packet + 1, data, length); | 461 | memcpy(packet + 1, data, length); |
462 | return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0); | 462 | return write_packet_TCP_client_secure_connection(con, packet, SIZEOF_VLA(packet), 0); |
463 | } | 463 | } |
464 | 464 | ||
465 | /* return 1 on success. | 465 | /* return 1 on success. |
@@ -476,7 +476,7 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const | |||
476 | packet[0] = TCP_PACKET_OOB_SEND; | 476 | packet[0] = TCP_PACKET_OOB_SEND; |
477 | memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 477 | memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
478 | memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); | 478 | memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); |
479 | return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0); | 479 | return write_packet_TCP_client_secure_connection(con, packet, SIZEOF_VLA(packet), 0); |
480 | } | 480 | } |
481 | 481 | ||
482 | 482 | ||
@@ -519,19 +519,19 @@ void oob_data_handler(TCP_Client_Connection *con, int (*oob_data_callback)(void | |||
519 | * return 0 if could not send packet. | 519 | * return 0 if could not send packet. |
520 | * return -1 on failure (connection must be killed). | 520 | * return -1 on failure (connection must be killed). |
521 | */ | 521 | */ |
522 | static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id) | 522 | static int client_send_disconnect_notification(TCP_Client_Connection *con, uint8_t id) |
523 | { | 523 | { |
524 | uint8_t packet[1 + 1]; | 524 | uint8_t packet[1 + 1]; |
525 | packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; | 525 | packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; |
526 | packet[1] = id; | 526 | packet[1] = id; |
527 | return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1); | 527 | return write_packet_TCP_client_secure_connection(con, packet, sizeof(packet), 1); |
528 | } | 528 | } |
529 | 529 | ||
530 | /* return 1 on success. | 530 | /* return 1 on success. |
531 | * return 0 if could not send packet. | 531 | * return 0 if could not send packet. |
532 | * return -1 on failure (connection must be killed). | 532 | * return -1 on failure (connection must be killed). |
533 | */ | 533 | */ |
534 | static int send_ping_request(TCP_Client_Connection *con) | 534 | static int tcp_send_ping_request(TCP_Client_Connection *con) |
535 | { | 535 | { |
536 | if (!con->ping_request_id) { | 536 | if (!con->ping_request_id) { |
537 | return 1; | 537 | return 1; |
@@ -542,7 +542,7 @@ static int send_ping_request(TCP_Client_Connection *con) | |||
542 | memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); | 542 | memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); |
543 | int ret; | 543 | int ret; |
544 | 544 | ||
545 | if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) { | 545 | if ((ret = write_packet_TCP_client_secure_connection(con, packet, sizeof(packet), 1)) == 1) { |
546 | con->ping_request_id = 0; | 546 | con->ping_request_id = 0; |
547 | } | 547 | } |
548 | 548 | ||
@@ -553,7 +553,7 @@ static int send_ping_request(TCP_Client_Connection *con) | |||
553 | * return 0 if could not send packet. | 553 | * return 0 if could not send packet. |
554 | * return -1 on failure (connection must be killed). | 554 | * return -1 on failure (connection must be killed). |
555 | */ | 555 | */ |
556 | static int send_ping_response(TCP_Client_Connection *con) | 556 | static int tcp_send_ping_response(TCP_Client_Connection *con) |
557 | { | 557 | { |
558 | if (!con->ping_response_id) { | 558 | if (!con->ping_response_id) { |
559 | return 1; | 559 | return 1; |
@@ -564,7 +564,7 @@ static int send_ping_response(TCP_Client_Connection *con) | |||
564 | memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); | 564 | memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); |
565 | int ret; | 565 | int ret; |
566 | 566 | ||
567 | if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet), 1)) == 1) { | 567 | if ((ret = write_packet_TCP_client_secure_connection(con, packet, sizeof(packet), 1)) == 1) { |
568 | con->ping_response_id = 0; | 568 | con->ping_response_id = 0; |
569 | } | 569 | } |
570 | 570 | ||
@@ -583,7 +583,7 @@ int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id) | |||
583 | 583 | ||
584 | con->connections[con_id].status = 0; | 584 | con->connections[con_id].status = 0; |
585 | con->connections[con_id].number = 0; | 585 | con->connections[con_id].number = 0; |
586 | return send_disconnect_notification(con, con_id + NUM_RESERVED_PORTS); | 586 | return client_send_disconnect_notification(con, con_id + NUM_RESERVED_PORTS); |
587 | } | 587 | } |
588 | 588 | ||
589 | /* return 1 on success. | 589 | /* return 1 on success. |
@@ -595,7 +595,7 @@ int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t | |||
595 | VLA(uint8_t, packet, 1 + length); | 595 | VLA(uint8_t, packet, 1 + length); |
596 | packet[0] = TCP_PACKET_ONION_REQUEST; | 596 | packet[0] = TCP_PACKET_ONION_REQUEST; |
597 | memcpy(packet + 1, data, length); | 597 | memcpy(packet + 1, data, length); |
598 | return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0); | 598 | return write_packet_TCP_client_secure_connection(con, packet, SIZEOF_VLA(packet), 0); |
599 | } | 599 | } |
600 | 600 | ||
601 | void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, | 601 | void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, |
@@ -692,7 +692,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public | |||
692 | /* return 0 on success | 692 | /* return 0 on success |
693 | * return -1 on failure | 693 | * return -1 on failure |
694 | */ | 694 | */ |
695 | static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length, void *userdata) | 695 | static int handle_TCP_client_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length, void *userdata) |
696 | { | 696 | { |
697 | if (length <= 1) { | 697 | if (length <= 1) { |
698 | return -1; | 698 | return -1; |
@@ -787,7 +787,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u | |||
787 | uint64_t ping_id; | 787 | uint64_t ping_id; |
788 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); | 788 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); |
789 | conn->ping_response_id = ping_id; | 789 | conn->ping_response_id = ping_id; |
790 | send_ping_response(conn); | 790 | tcp_send_ping_response(conn); |
791 | return 0; | 791 | return 0; |
792 | } | 792 | } |
793 | 793 | ||
@@ -847,9 +847,9 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u | |||
847 | 847 | ||
848 | static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata) | 848 | static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata) |
849 | { | 849 | { |
850 | send_pending_data(conn); | 850 | client_send_pending_data(conn); |
851 | send_ping_response(conn); | 851 | tcp_send_ping_response(conn); |
852 | send_ping_request(conn); | 852 | tcp_send_ping_request(conn); |
853 | 853 | ||
854 | uint8_t packet[MAX_PACKET_SIZE]; | 854 | uint8_t packet[MAX_PACKET_SIZE]; |
855 | int len; | 855 | int len; |
@@ -862,7 +862,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata) | |||
862 | } | 862 | } |
863 | 863 | ||
864 | conn->ping_request_id = conn->ping_id = ping_id; | 864 | conn->ping_request_id = conn->ping_id = ping_id; |
865 | send_ping_request(conn); | 865 | tcp_send_ping_request(conn); |
866 | conn->last_pinged = unix_time(); | 866 | conn->last_pinged = unix_time(); |
867 | } | 867 | } |
868 | 868 | ||
@@ -878,7 +878,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata) | |||
878 | break; | 878 | break; |
879 | } | 879 | } |
880 | 880 | ||
881 | if (handle_TCP_packet(conn, packet, len, userdata) == -1) { | 881 | if (handle_TCP_client_packet(conn, packet, len, userdata) == -1) { |
882 | conn->status = TCP_CLIENT_DISCONNECTED; | 882 | conn->status = TCP_CLIENT_DISCONNECTED; |
883 | break; | 883 | break; |
884 | } | 884 | } |
@@ -898,7 +898,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata) | |||
898 | } | 898 | } |
899 | 899 | ||
900 | if (TCP_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { | 900 | if (TCP_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { |
901 | if (send_pending_data(TCP_connection) == 0) { | 901 | if (client_send_pending_data(TCP_connection) == 0) { |
902 | int ret = proxy_http_read_connection_response(TCP_connection); | 902 | int ret = proxy_http_read_connection_response(TCP_connection); |
903 | 903 | ||
904 | if (ret == -1) { | 904 | if (ret == -1) { |
@@ -914,7 +914,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata) | |||
914 | } | 914 | } |
915 | 915 | ||
916 | if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { | 916 | if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { |
917 | if (send_pending_data(TCP_connection) == 0) { | 917 | if (client_send_pending_data(TCP_connection) == 0) { |
918 | int ret = socks5_read_handshake_response(TCP_connection); | 918 | int ret = socks5_read_handshake_response(TCP_connection); |
919 | 919 | ||
920 | if (ret == -1) { | 920 | if (ret == -1) { |
@@ -930,7 +930,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata) | |||
930 | } | 930 | } |
931 | 931 | ||
932 | if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { | 932 | if (TCP_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { |
933 | if (send_pending_data(TCP_connection) == 0) { | 933 | if (client_send_pending_data(TCP_connection) == 0) { |
934 | int ret = proxy_socks5_read_connection_response(TCP_connection); | 934 | int ret = proxy_socks5_read_connection_response(TCP_connection); |
935 | 935 | ||
936 | if (ret == -1) { | 936 | if (ret == -1) { |
@@ -946,7 +946,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata) | |||
946 | } | 946 | } |
947 | 947 | ||
948 | if (TCP_connection->status == TCP_CLIENT_CONNECTING) { | 948 | if (TCP_connection->status == TCP_CLIENT_CONNECTING) { |
949 | if (send_pending_data(TCP_connection) == 0) { | 949 | if (client_send_pending_data(TCP_connection) == 0) { |
950 | TCP_connection->status = TCP_CLIENT_UNCONFIRMED; | 950 | TCP_connection->status = TCP_CLIENT_UNCONFIRMED; |
951 | } | 951 | } |
952 | } | 952 | } |