summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c8
-rw-r--r--toxcore/TCP_client.c38
-rw-r--r--toxcore/TCP_client.h3
-rw-r--r--toxcore/TCP_connection.c8
-rw-r--r--toxcore/TCP_connection.h3
-rw-r--r--toxcore/TCP_server.c39
-rw-r--r--toxcore/TCP_server.h12
-rw-r--r--toxcore/friend_connection.c7
-rw-r--r--toxcore/friend_connection.h2
-rw-r--r--toxcore/net_crypto.c2
-rw-r--r--toxcore/onion_client.c4
-rw-r--r--toxcore/onion_client.h2
-rw-r--r--toxcore/util.c4
13 files changed, 71 insertions, 61 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 556acc27..32c89867 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1977,8 +1977,8 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig
1977 1977
1978 m->onion = new_onion(m->mono_time, m->dht); 1978 m->onion = new_onion(m->mono_time, m->dht);
1979 m->onion_a = new_onion_announce(m->mono_time, m->dht); 1979 m->onion_a = new_onion_announce(m->mono_time, m->dht);
1980 m->onion_c = new_onion_client(m->mono_time, m->net_crypto); 1980 m->onion_c = new_onion_client(m->log, m->mono_time, m->net_crypto);
1981 m->fr_c = new_friend_connections(m->mono_time, m->onion_c, options->local_discovery_enabled); 1981 m->fr_c = new_friend_connections(m->log, m->mono_time, m->onion_c, options->local_discovery_enabled);
1982 1982
1983 if (!(m->onion && m->onion_a && m->onion_c && m->fr_c)) { 1983 if (!(m->onion && m->onion_a && m->onion_c && m->fr_c)) {
1984 kill_friend_connections(m->fr_c); 1984 kill_friend_connections(m->fr_c);
@@ -1995,8 +1995,8 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig
1995 } 1995 }
1996 1996
1997 if (options->tcp_server_port) { 1997 if (options->tcp_server_port) {
1998 m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, dht_get_self_secret_key(m->dht), 1998 m->tcp_server = new_TCP_server(m->log, options->ipv6enabled, 1, &options->tcp_server_port,
1999 m->onion); 1999 dht_get_self_secret_key(m->dht), m->onion);
2000 2000
2001 if (m->tcp_server == nullptr) { 2001 if (m->tcp_server == nullptr) {
2002 kill_friend_connections(m->fr_c); 2002 kill_friend_connections(m->fr_c);
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 */
154static int proxy_http_read_connection_response(TCP_Client_Connection *tcp_conn) 154static 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 */
196static int socks5_read_handshake_response(TCP_Client_Connection *tcp_conn) 196static 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 */
242static int proxy_socks5_read_connection_response(TCP_Client_Connection *tcp_conn) 242static 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
903static bool tcp_process_packet(TCP_Client_Connection *conn, void *userdata) 903static 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
926static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_time, void *userdata) 926static 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 */
960void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, void *userdata) 961void 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)) {
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index cecf5a6d..ed4304cb 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -54,7 +54,8 @@ TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip
54 54
55/* Run the TCP connection 55/* Run the TCP connection
56 */ 56 */
57void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, void *userdata); 57void do_TCP_connection(const Logger *logger, Mono_Time *mono_time, TCP_Client_Connection *tcp_connection,
58 void *userdata);
58 59
59/* Kill the TCP connection 60/* Kill the TCP connection
60 */ 61 */
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index adef3175..411589bf 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -1398,7 +1398,7 @@ TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret
1398 return temp; 1398 return temp;
1399} 1399}
1400 1400
1401static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata) 1401static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *userdata)
1402{ 1402{
1403 unsigned int i; 1403 unsigned int i;
1404 1404
@@ -1407,7 +1407,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1407 1407
1408 if (tcp_con) { 1408 if (tcp_con) {
1409 if (tcp_con->status != TCP_CONN_SLEEPING) { 1409 if (tcp_con->status != TCP_CONN_SLEEPING) {
1410 do_TCP_connection(tcp_c->mono_time, tcp_con->connection, userdata); 1410 do_TCP_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata);
1411 1411
1412 /* callbacks can change TCP connection address. */ 1412 /* callbacks can change TCP connection address. */
1413 tcp_con = get_tcp_connection(tcp_c, i); 1413 tcp_con = get_tcp_connection(tcp_c, i);
@@ -1485,9 +1485,9 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1485 } 1485 }
1486} 1486}
1487 1487
1488void do_tcp_connections(TCP_Connections *tcp_c, void *userdata) 1488void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata)
1489{ 1489{
1490 do_tcp_conns(tcp_c, userdata); 1490 do_tcp_conns(logger, tcp_c, userdata);
1491 kill_nonused_tcp(tcp_c); 1491 kill_nonused_tcp(tcp_c);
1492} 1492}
1493 1493
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h
index a8e522d3..1bd5b85a 100644
--- a/toxcore/TCP_connection.h
+++ b/toxcore/TCP_connection.h
@@ -206,7 +206,8 @@ uint32_t tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_rela
206 */ 206 */
207TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, TCP_Proxy_Info *proxy_info); 207TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, TCP_Proxy_Info *proxy_info);
208 208
209void do_tcp_connections(TCP_Connections *tcp_c, void *userdata); 209void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata);
210
210void kill_tcp_connections(TCP_Connections *tcp_c); 211void kill_tcp_connections(TCP_Connections *tcp_c);
211 212
212#endif 213#endif
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
68struct TCP_Server { 67struct 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 */
278uint16_t read_TCP_length(Socket sock) 278uint16_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 */
308int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) 308int 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 */
330int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key, 330int 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 */
620static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key) 620static 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
1058TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, 1058TCP_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) {
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 5862a473..46dd0330 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -69,8 +69,8 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server);
69 69
70/* Create new TCP server instance. 70/* Create new TCP server instance.
71 */ 71 */
72TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, 72TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
73 Onion *onion); 73 const uint8_t *secret_key, Onion *onion);
74 74
75/* Run the TCP_server 75/* Run the TCP_server
76 */ 76 */
@@ -87,21 +87,21 @@ void kill_TCP_server(TCP_Server *tcp_server);
87 * return 0 if nothing has been read from socket. 87 * return 0 if nothing has been read from socket.
88 * return -1 on failure. 88 * return -1 on failure.
89 */ 89 */
90uint16_t read_TCP_length(Socket sock); 90uint16_t read_TCP_length(const Logger *logger, Socket sock);
91 91
92/* Read length bytes from socket. 92/* Read length bytes from socket.
93 * 93 *
94 * return length on success 94 * return length on success
95 * return -1 on failure/no data in buffer. 95 * return -1 on failure/no data in buffer.
96 */ 96 */
97int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length); 97int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length);
98 98
99/* return length of received packet on success. 99/* return length of received packet on success.
100 * return 0 if could not read any packet. 100 * return 0 if could not read any packet.
101 * return -1 on failure (connection must be killed). 101 * return -1 on failure (connection must be killed).
102 */ 102 */
103int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key, 103int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length,
104 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len); 104 const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
105 105
106 106
107#endif 107#endif
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index af5d09e3..fd1fa71c 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -12,7 +12,6 @@
12 12
13#include "friend_connection.h" 13#include "friend_connection.h"
14 14
15#include <stdio.h>
16#include <stdlib.h> 15#include <stdlib.h>
17#include <string.h> 16#include <string.h>
18 17
@@ -60,6 +59,7 @@ typedef struct Friend_Conn {
60 59
61struct Friend_Connections { 60struct Friend_Connections {
62 const Mono_Time *mono_time; 61 const Mono_Time *mono_time;
62 const Logger *logger;
63 Net_Crypto *net_crypto; 63 Net_Crypto *net_crypto;
64 DHT *dht; 64 DHT *dht;
65 Onion_Client *onion_c; 65 Onion_Client *onion_c;
@@ -348,7 +348,7 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
348 348
349 if (friend_con->dht_lock) { 349 if (friend_con->dht_lock) {
350 if (dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) { 350 if (dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) {
351 printf("a. Could not delete dht peer. Please report this.\n"); 351 LOGGER_ERROR(fr_c->logger, "a. Could not delete dht peer. Please report this.");
352 return; 352 return;
353 } 353 }
354 354
@@ -848,7 +848,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
848} 848}
849 849
850/* Create new friend_connections instance. */ 850/* Create new friend_connections instance. */
851Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Client *onion_c, 851Friend_Connections *new_friend_connections(const Logger *logger, const Mono_Time *mono_time, Onion_Client *onion_c,
852 bool local_discovery_enabled) 852 bool local_discovery_enabled)
853{ 853{
854 if (onion_c == nullptr) { 854 if (onion_c == nullptr) {
@@ -862,6 +862,7 @@ Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Cli
862 } 862 }
863 863
864 temp->mono_time = mono_time; 864 temp->mono_time = mono_time;
865 temp->logger = logger;
865 temp->dht = onion_get_dht(onion_c); 866 temp->dht = onion_get_dht(onion_c);
866 temp->net_crypto = onion_get_net_crypto(onion_c); 867 temp->net_crypto = onion_get_net_crypto(onion_c);
867 temp->onion_c = onion_c; 868 temp->onion_c = onion_c;
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h
index 92e38703..2263c8c3 100644
--- a/toxcore/friend_connection.h
+++ b/toxcore/friend_connection.h
@@ -148,7 +148,7 @@ typedef int fr_request_cb(void *object, const uint8_t *source_pubkey, const uint
148void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object); 148void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object);
149 149
150/* Create new friend_connections instance. */ 150/* Create new friend_connections instance. */
151Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Client *onion_c, 151Friend_Connections *new_friend_connections(const Logger *logger, const Mono_Time *mono_time, Onion_Client *onion_c,
152 bool local_discovery_enabled); 152 bool local_discovery_enabled);
153 153
154/* main friend_connections loop. */ 154/* main friend_connections loop. */
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 2851053b..f57886a8 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2281,7 +2281,7 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u
2281static void do_tcp(Net_Crypto *c, void *userdata) 2281static void do_tcp(Net_Crypto *c, void *userdata)
2282{ 2282{
2283 pthread_mutex_lock(&c->tcp_mutex); 2283 pthread_mutex_lock(&c->tcp_mutex);
2284 do_tcp_connections(c->tcp_c, userdata); 2284 do_tcp_connections(c->log, c->tcp_c, userdata);
2285 pthread_mutex_unlock(&c->tcp_mutex); 2285 pthread_mutex_unlock(&c->tcp_mutex);
2286 2286
2287 uint32_t i; 2287 uint32_t i;
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index e161928b..60a80bc6 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -97,6 +97,7 @@ typedef struct Onion_Data_Handler {
97 97
98struct Onion_Client { 98struct Onion_Client {
99 Mono_Time *mono_time; 99 Mono_Time *mono_time;
100 const Logger *logger;
100 101
101 DHT *dht; 102 DHT *dht;
102 Net_Crypto *c; 103 Net_Crypto *c;
@@ -1849,7 +1850,7 @@ void do_onion_client(Onion_Client *onion_c)
1849 onion_c->last_run = mono_time_get(onion_c->mono_time); 1850 onion_c->last_run = mono_time_get(onion_c->mono_time);
1850} 1851}
1851 1852
1852Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c) 1853Onion_Client *new_onion_client(const Logger *logger, Mono_Time *mono_time, Net_Crypto *c)
1853{ 1854{
1854 if (c == nullptr) { 1855 if (c == nullptr) {
1855 return nullptr; 1856 return nullptr;
@@ -1869,6 +1870,7 @@ Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c)
1869 } 1870 }
1870 1871
1871 onion_c->mono_time = mono_time; 1872 onion_c->mono_time = mono_time;
1873 onion_c->logger = logger;
1872 onion_c->dht = nc_get_dht(c); 1874 onion_c->dht = nc_get_dht(c);
1873 onion_c->net = dht_get_net(onion_c->dht); 1875 onion_c->net = dht_get_net(onion_c->dht);
1874 onion_c->c = c; 1876 onion_c->c = c;
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index 5b6bea65..88e97dd6 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -171,7 +171,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
171 171
172void do_onion_client(Onion_Client *onion_c); 172void do_onion_client(Onion_Client *onion_c);
173 173
174Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c); 174Onion_Client *new_onion_client(const Logger *logger, Mono_Time *mono_time, Net_Crypto *c);
175 175
176void kill_onion_client(Onion_Client *onion_c); 176void kill_onion_client(Onion_Client *onion_c);
177 177
diff --git a/toxcore/util.c b/toxcore/util.c
index 58b75763..2f00adac 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -17,12 +17,12 @@
17 17
18#include "util.h" 18#include "util.h"
19 19
20#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */
21
22#include <stdlib.h> 20#include <stdlib.h>
23#include <string.h> 21#include <string.h>
24#include <time.h> 22#include <time.h>
25 23
24#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */
25
26 26
27/* id functions */ 27/* id functions */
28bool id_equal(const uint8_t *dest, const uint8_t *src) 28bool id_equal(const uint8_t *dest, const uint8_t *src)