diff options
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r-- | toxcore/TCP_client.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 408ec489..1405d9b2 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c | |||
@@ -151,12 +151,12 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *tcp_con | |||
151 | * return 0 if no data received. | 151 | * return 0 if no data received. |
152 | * return -1 on failure (connection refused). | 152 | * return -1 on failure (connection refused). |
153 | */ | 153 | */ |
154 | static int proxy_http_read_connection_response(TCP_Client_Connection *tcp_conn) | 154 | static int proxy_http_read_connection_response(const Logger *logger, TCP_Client_Connection *tcp_conn) |
155 | { | 155 | { |
156 | char success[] = "200"; | 156 | char success[] = "200"; |
157 | uint8_t data[16]; // draining works the best if the length is a power of 2 | 157 | uint8_t data[16]; // draining works the best if the length is a power of 2 |
158 | 158 | ||
159 | int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data) - 1); | 159 | int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data) - 1); |
160 | 160 | ||
161 | if (ret == -1) { | 161 | if (ret == -1) { |
162 | return 0; | 162 | return 0; |
@@ -170,7 +170,7 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *tcp_conn) | |||
170 | 170 | ||
171 | if (data_left) { | 171 | if (data_left) { |
172 | VLA(uint8_t, temp_data, data_left); | 172 | VLA(uint8_t, temp_data, data_left); |
173 | read_TCP_packet(tcp_conn->sock, temp_data, data_left); | 173 | read_TCP_packet(logger, tcp_conn->sock, temp_data, data_left); |
174 | } | 174 | } |
175 | 175 | ||
176 | return 1; | 176 | return 1; |
@@ -193,10 +193,10 @@ static void proxy_socks5_generate_handshake(TCP_Client_Connection *tcp_conn) | |||
193 | * return 0 if no data received. | 193 | * return 0 if no data received. |
194 | * return -1 on failure (connection refused). | 194 | * return -1 on failure (connection refused). |
195 | */ | 195 | */ |
196 | static int socks5_read_handshake_response(TCP_Client_Connection *tcp_conn) | 196 | static int socks5_read_handshake_response(const Logger *logger, TCP_Client_Connection *tcp_conn) |
197 | { | 197 | { |
198 | uint8_t data[2]; | 198 | uint8_t data[2]; |
199 | int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data)); | 199 | int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data)); |
200 | 200 | ||
201 | if (ret == -1) { | 201 | if (ret == -1) { |
202 | return 0; | 202 | return 0; |
@@ -239,11 +239,11 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *tcp_ | |||
239 | * return 0 if no data received. | 239 | * return 0 if no data received. |
240 | * return -1 on failure (connection refused). | 240 | * return -1 on failure (connection refused). |
241 | */ | 241 | */ |
242 | static int proxy_socks5_read_connection_response(TCP_Client_Connection *tcp_conn) | 242 | static int proxy_socks5_read_connection_response(const Logger *logger, TCP_Client_Connection *tcp_conn) |
243 | { | 243 | { |
244 | if (net_family_is_ipv4(tcp_conn->ip_port.ip.family)) { | 244 | if (net_family_is_ipv4(tcp_conn->ip_port.ip.family)) { |
245 | uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; | 245 | uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; |
246 | int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data)); | 246 | int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data)); |
247 | 247 | ||
248 | if (ret == -1) { | 248 | if (ret == -1) { |
249 | return 0; | 249 | return 0; |
@@ -254,7 +254,7 @@ static int proxy_socks5_read_connection_response(TCP_Client_Connection *tcp_conn | |||
254 | } | 254 | } |
255 | } else { | 255 | } else { |
256 | uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)]; | 256 | uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)]; |
257 | int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data)); | 257 | int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data)); |
258 | 258 | ||
259 | if (ret == -1) { | 259 | if (ret == -1) { |
260 | return 0; | 260 | return 0; |
@@ -900,10 +900,10 @@ static int handle_TCP_client_packet(TCP_Client_Connection *conn, const uint8_t * | |||
900 | return 0; | 900 | return 0; |
901 | } | 901 | } |
902 | 902 | ||
903 | static bool tcp_process_packet(TCP_Client_Connection *conn, void *userdata) | 903 | static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn, void *userdata) |
904 | { | 904 | { |
905 | uint8_t packet[MAX_PACKET_SIZE]; | 905 | uint8_t packet[MAX_PACKET_SIZE]; |
906 | const int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, | 906 | const int len = read_packet_TCP_secure_connection(logger, conn->sock, &conn->next_packet_length, conn->shared_key, |
907 | conn->recv_nonce, packet, sizeof(packet)); | 907 | conn->recv_nonce, packet, sizeof(packet)); |
908 | 908 | ||
909 | if (len == 0) { | 909 | if (len == 0) { |
@@ -923,7 +923,8 @@ static bool tcp_process_packet(TCP_Client_Connection *conn, void *userdata) | |||
923 | return true; | 923 | return true; |
924 | } | 924 | } |
925 | 925 | ||
926 | static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_time, void *userdata) | 926 | static int do_confirmed_TCP(const Logger *logger, TCP_Client_Connection *conn, const Mono_Time *mono_time, |
927 | void *userdata) | ||
927 | { | 928 | { |
928 | client_send_pending_data(conn); | 929 | client_send_pending_data(conn); |
929 | tcp_send_ping_response(conn); | 930 | tcp_send_ping_response(conn); |
@@ -947,7 +948,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_t | |||
947 | return 0; | 948 | return 0; |
948 | } | 949 | } |
949 | 950 | ||
950 | while (tcp_process_packet(conn, userdata)) { | 951 | while (tcp_process_packet(logger, conn, userdata)) { |
951 | // Keep reading until error or out of data. | 952 | // Keep reading until error or out of data. |
952 | continue; | 953 | continue; |
953 | } | 954 | } |
@@ -957,7 +958,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_t | |||
957 | 958 | ||
958 | /* Run the TCP connection | 959 | /* Run the TCP connection |
959 | */ | 960 | */ |
960 | void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, void *userdata) | 961 | void do_TCP_connection(const Logger *logger, Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, |
962 | void *userdata) | ||
961 | { | 963 | { |
962 | if (tcp_connection->status == TCP_CLIENT_DISCONNECTED) { | 964 | if (tcp_connection->status == TCP_CLIENT_DISCONNECTED) { |
963 | return; | 965 | return; |
@@ -965,7 +967,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti | |||
965 | 967 | ||
966 | if (tcp_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { | 968 | if (tcp_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { |
967 | if (client_send_pending_data(tcp_connection) == 0) { | 969 | if (client_send_pending_data(tcp_connection) == 0) { |
968 | int ret = proxy_http_read_connection_response(tcp_connection); | 970 | int ret = proxy_http_read_connection_response(logger, tcp_connection); |
969 | 971 | ||
970 | if (ret == -1) { | 972 | if (ret == -1) { |
971 | tcp_connection->kill_at = 0; | 973 | tcp_connection->kill_at = 0; |
@@ -981,7 +983,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti | |||
981 | 983 | ||
982 | if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { | 984 | if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { |
983 | if (client_send_pending_data(tcp_connection) == 0) { | 985 | if (client_send_pending_data(tcp_connection) == 0) { |
984 | int ret = socks5_read_handshake_response(tcp_connection); | 986 | int ret = socks5_read_handshake_response(logger, tcp_connection); |
985 | 987 | ||
986 | if (ret == -1) { | 988 | if (ret == -1) { |
987 | tcp_connection->kill_at = 0; | 989 | tcp_connection->kill_at = 0; |
@@ -997,7 +999,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti | |||
997 | 999 | ||
998 | if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { | 1000 | if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { |
999 | if (client_send_pending_data(tcp_connection) == 0) { | 1001 | if (client_send_pending_data(tcp_connection) == 0) { |
1000 | int ret = proxy_socks5_read_connection_response(tcp_connection); | 1002 | int ret = proxy_socks5_read_connection_response(logger, tcp_connection); |
1001 | 1003 | ||
1002 | if (ret == -1) { | 1004 | if (ret == -1) { |
1003 | tcp_connection->kill_at = 0; | 1005 | tcp_connection->kill_at = 0; |
@@ -1019,7 +1021,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti | |||
1019 | 1021 | ||
1020 | if (tcp_connection->status == TCP_CLIENT_UNCONFIRMED) { | 1022 | if (tcp_connection->status == TCP_CLIENT_UNCONFIRMED) { |
1021 | uint8_t data[TCP_SERVER_HANDSHAKE_SIZE]; | 1023 | uint8_t data[TCP_SERVER_HANDSHAKE_SIZE]; |
1022 | int len = read_TCP_packet(tcp_connection->sock, data, sizeof(data)); | 1024 | int len = read_TCP_packet(logger, tcp_connection->sock, data, sizeof(data)); |
1023 | 1025 | ||
1024 | if (sizeof(data) == len) { | 1026 | if (sizeof(data) == len) { |
1025 | if (handle_handshake(tcp_connection, data) == 0) { | 1027 | if (handle_handshake(tcp_connection, data) == 0) { |
@@ -1033,7 +1035,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti | |||
1033 | } | 1035 | } |
1034 | 1036 | ||
1035 | if (tcp_connection->status == TCP_CLIENT_CONFIRMED) { | 1037 | if (tcp_connection->status == TCP_CLIENT_CONFIRMED) { |
1036 | do_confirmed_TCP(tcp_connection, mono_time, userdata); | 1038 | do_confirmed_TCP(logger, tcp_connection, mono_time, userdata); |
1037 | } | 1039 | } |
1038 | 1040 | ||
1039 | if (tcp_connection->kill_at <= mono_time_get(mono_time)) { | 1041 | if (tcp_connection->kill_at <= mono_time_get(mono_time)) { |