diff options
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r-- | toxcore/TCP_server.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index b22bdb80..79a9e560 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -12,7 +12,6 @@ | |||
12 | 12 | ||
13 | #include "TCP_server.h" | 13 | #include "TCP_server.h" |
14 | 14 | ||
15 | #include <stdio.h> | ||
16 | #include <stdlib.h> | 15 | #include <stdlib.h> |
17 | #include <string.h> | 16 | #include <string.h> |
18 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) | 17 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) |
@@ -66,6 +65,7 @@ typedef struct TCP_Secure_Connection { | |||
66 | 65 | ||
67 | 66 | ||
68 | struct TCP_Server { | 67 | struct TCP_Server { |
68 | const Logger *logger; | ||
69 | Onion *onion; | 69 | Onion *onion; |
70 | 70 | ||
71 | #ifdef TCP_SERVER_USE_EPOLL | 71 | #ifdef TCP_SERVER_USE_EPOLL |
@@ -220,7 +220,7 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_ | |||
220 | } | 220 | } |
221 | 221 | ||
222 | if (index == -1) { | 222 | if (index == -1) { |
223 | fprintf(stderr, "FAIL index is -1\n"); | 223 | LOGGER_ERROR(tcp_server->logger, "FAIL index is -1"); |
224 | return -1; | 224 | return -1; |
225 | } | 225 | } |
226 | 226 | ||
@@ -275,7 +275,7 @@ static int del_accepted(TCP_Server *tcp_server, int index) | |||
275 | * return 0 if nothing has been read from socket. | 275 | * return 0 if nothing has been read from socket. |
276 | * return -1 on failure. | 276 | * return -1 on failure. |
277 | */ | 277 | */ |
278 | uint16_t read_TCP_length(Socket sock) | 278 | uint16_t read_TCP_length(const Logger *logger, Socket sock) |
279 | { | 279 | { |
280 | const unsigned int count = net_socket_data_recv_buffer(sock); | 280 | const unsigned int count = net_socket_data_recv_buffer(sock); |
281 | 281 | ||
@@ -284,7 +284,7 @@ uint16_t read_TCP_length(Socket sock) | |||
284 | const int len = net_recv(sock, &length, sizeof(uint16_t)); | 284 | const int len = net_recv(sock, &length, sizeof(uint16_t)); |
285 | 285 | ||
286 | if (len != sizeof(uint16_t)) { | 286 | if (len != sizeof(uint16_t)) { |
287 | fprintf(stderr, "FAIL recv packet\n"); | 287 | LOGGER_ERROR(logger, "FAIL recv packet"); |
288 | return 0; | 288 | return 0; |
289 | } | 289 | } |
290 | 290 | ||
@@ -305,7 +305,7 @@ uint16_t read_TCP_length(Socket sock) | |||
305 | * return length on success | 305 | * return length on success |
306 | * return -1 on failure/no data in buffer. | 306 | * return -1 on failure/no data in buffer. |
307 | */ | 307 | */ |
308 | int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) | 308 | int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length) |
309 | { | 309 | { |
310 | unsigned int count = net_socket_data_recv_buffer(sock); | 310 | unsigned int count = net_socket_data_recv_buffer(sock); |
311 | 311 | ||
@@ -313,7 +313,7 @@ int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) | |||
313 | const int len = net_recv(sock, data, length); | 313 | const int len = net_recv(sock, data, length); |
314 | 314 | ||
315 | if (len != length) { | 315 | if (len != length) { |
316 | fprintf(stderr, "FAIL recv packet\n"); | 316 | LOGGER_ERROR(logger, "FAIL recv packet"); |
317 | return -1; | 317 | return -1; |
318 | } | 318 | } |
319 | 319 | ||
@@ -327,11 +327,11 @@ int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) | |||
327 | * return 0 if could not read any packet. | 327 | * return 0 if could not read any packet. |
328 | * return -1 on failure (connection must be killed). | 328 | * return -1 on failure (connection must be killed). |
329 | */ | 329 | */ |
330 | int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key, | 330 | int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length, |
331 | uint8_t *recv_nonce, uint8_t *data, uint16_t max_len) | 331 | const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len) |
332 | { | 332 | { |
333 | if (*next_packet_length == 0) { | 333 | if (*next_packet_length == 0) { |
334 | uint16_t len = read_TCP_length(sock); | 334 | uint16_t len = read_TCP_length(logger, sock); |
335 | 335 | ||
336 | if (len == (uint16_t) -1) { | 336 | if (len == (uint16_t) -1) { |
337 | return -1; | 337 | return -1; |
@@ -349,7 +349,7 @@ int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, | |||
349 | } | 349 | } |
350 | 350 | ||
351 | VLA(uint8_t, data_encrypted, *next_packet_length); | 351 | VLA(uint8_t, data_encrypted, *next_packet_length); |
352 | int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length); | 352 | int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length); |
353 | 353 | ||
354 | if (len_packet != *next_packet_length) { | 354 | if (len_packet != *next_packet_length) { |
355 | return 0; | 355 | return 0; |
@@ -617,10 +617,10 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, | |||
617 | * return 0 if we didn't get it yet. | 617 | * return 0 if we didn't get it yet. |
618 | * return -1 if the connection must be killed. | 618 | * return -1 if the connection must be killed. |
619 | */ | 619 | */ |
620 | static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key) | 620 | static int read_connection_handshake(const Logger *logger, TCP_Secure_Connection *con, const uint8_t *self_secret_key) |
621 | { | 621 | { |
622 | uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE]; | 622 | uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE]; |
623 | const int len = read_TCP_packet(con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE); | 623 | const int len = read_TCP_packet(logger, con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE); |
624 | 624 | ||
625 | if (len != -1) { | 625 | if (len != -1) { |
626 | return handle_TCP_handshake(con, data, len, self_secret_key); | 626 | return handle_TCP_handshake(con, data, len, self_secret_key); |
@@ -1055,8 +1055,8 @@ static Socket new_listening_TCP_socket(Family family, uint16_t port) | |||
1055 | return sock; | 1055 | return sock; |
1056 | } | 1056 | } |
1057 | 1057 | ||
1058 | TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, | 1058 | TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, |
1059 | Onion *onion) | 1059 | const uint8_t *secret_key, Onion *onion) |
1060 | { | 1060 | { |
1061 | if (num_sockets == 0 || ports == nullptr) { | 1061 | if (num_sockets == 0 || ports == nullptr) { |
1062 | return nullptr; | 1062 | return nullptr; |
@@ -1072,6 +1072,8 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin | |||
1072 | return nullptr; | 1072 | return nullptr; |
1073 | } | 1073 | } |
1074 | 1074 | ||
1075 | temp->logger = logger; | ||
1076 | |||
1075 | temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket)); | 1077 | temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket)); |
1076 | 1078 | ||
1077 | if (temp->socks_listening == nullptr) { | 1079 | if (temp->socks_listening == nullptr) { |
@@ -1156,7 +1158,8 @@ static int do_incoming(TCP_Server *tcp_server, uint32_t i) | |||
1156 | return -1; | 1158 | return -1; |
1157 | } | 1159 | } |
1158 | 1160 | ||
1159 | int ret = read_connection_handshake(&tcp_server->incoming_connection_queue[i], tcp_server->secret_key); | 1161 | int ret = read_connection_handshake(tcp_server->logger, &tcp_server->incoming_connection_queue[i], |
1162 | tcp_server->secret_key); | ||
1160 | 1163 | ||
1161 | if (ret == -1) { | 1164 | if (ret == -1) { |
1162 | kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[i]); | 1165 | kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[i]); |
@@ -1187,8 +1190,8 @@ static int do_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_time, ui | |||
1187 | } | 1190 | } |
1188 | 1191 | ||
1189 | uint8_t packet[MAX_PACKET_SIZE]; | 1192 | uint8_t packet[MAX_PACKET_SIZE]; |
1190 | int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce, | 1193 | int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->sock, &conn->next_packet_length, conn->shared_key, |
1191 | packet, sizeof(packet)); | 1194 | conn->recv_nonce, packet, sizeof(packet)); |
1192 | 1195 | ||
1193 | if (len == 0) { | 1196 | if (len == 0) { |
1194 | return -1; | 1197 | return -1; |
@@ -1207,7 +1210,7 @@ static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i) | |||
1207 | TCP_Secure_Connection *const conn = &tcp_server->accepted_connection_array[i]; | 1210 | TCP_Secure_Connection *const conn = &tcp_server->accepted_connection_array[i]; |
1208 | 1211 | ||
1209 | uint8_t packet[MAX_PACKET_SIZE]; | 1212 | uint8_t packet[MAX_PACKET_SIZE]; |
1210 | int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, | 1213 | int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->sock, &conn->next_packet_length, conn->shared_key, |
1211 | conn->recv_nonce, packet, sizeof(packet)); | 1214 | conn->recv_nonce, packet, sizeof(packet)); |
1212 | 1215 | ||
1213 | if (len == 0) { | 1216 | if (len == 0) { |