From fb810ec2dae1af88d8d6ff7b6ac608e3cb2f3300 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 1 Feb 2022 20:49:42 +0000 Subject: [PATCH] fix: Fix bootstrap on emscripten/wasm. Also added a whole bunch of logging that I needed while debugging the issue. The solution in the end is that bootstrap needs to resolve IPs, and getaddrinfo fails in the browser. Most of the time we bootstrap against IPs anyway, so trying to parse as IP address first will shortcut that. --- auto_tests/TCP_test.c | 131 ++++++++------- other/bootstrap_daemon/docker/Dockerfile | 2 + .../docker/tox-bootstrapd.sha256 | 2 +- other/bootstrap_daemon/docker/update-sha256 | 2 +- toxcore/TCP_client.c | 88 +++++----- toxcore/TCP_client.h | 16 +- toxcore/TCP_common.c | 59 ++++--- toxcore/TCP_common.h | 12 +- toxcore/TCP_connection.c | 26 +-- toxcore/TCP_connection.h | 3 +- toxcore/TCP_server.c | 157 +++++++++++------- toxcore/net_crypto.c | 2 +- toxcore/network.c | 56 +++++-- toxcore/network.h | 78 ++++----- toxcore/tox.c | 2 + 15 files changed, 369 insertions(+), 267 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 79a6c99c6d..2781351a3b 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -20,7 +20,7 @@ #define net_family_ipv6 net_family_ipv4 #endif -static inline IP get_loopback(void) +static IP get_loopback(void) { IP ip; #if USE_IPV6 @@ -57,14 +57,15 @@ static void test_basic(void) "Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS); Socket sock = {0}; + IP_Port localhost; + localhost.ip = get_loopback(); + localhost.port = 0; // Check all opened ports for connectivity. for (uint8_t i = 0; i < NUM_PORTS; i++) { sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); - IP_Port ip_port_loopback; - ip_port_loopback.ip = get_loopback(); - ip_port_loopback.port = net_htons(ports[i]); - int ret = net_connect(sock, ip_port_loopback); + localhost.port = net_htons(ports[i]); + int ret = net_connect(logger, sock, localhost); ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d.", ports[i]); // Leave open one connection for the next test. @@ -96,12 +97,13 @@ static void test_basic(void) "encrypt_data() call failed."); // Sending the handshake - ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, + localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "An attempt to send the initial handshake minus last byte failed."); do_TCP_server_delay(tcp_s, mono_time, 50); - ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, + ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, localhost) == 1, "The attempt to send the last byte of handshake failed."); do_TCP_server_delay(tcp_s, mono_time, 50); @@ -109,7 +111,7 @@ static void test_basic(void) // Receiving server response and decrypting it uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; - ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, + ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, localhost) == TCP_SERVER_HANDSHAKE_SIZE, "Could/did not receive a server response to the initial handshake."); ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE, TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); @@ -138,7 +140,7 @@ static void test_basic(void) msg_length = sizeof(r_req) - i; } - ck_assert_msg(net_send(sock, r_req + i, msg_length) == msg_length, + ck_assert_msg(net_send(logger, sock, r_req + i, msg_length, localhost) == msg_length, "Failed to send request after completing the handshake."); i += msg_length; @@ -149,7 +151,7 @@ static void test_basic(void) // Receiving the second response and verifying its validity uint8_t packet_resp[4096]; - int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); + int recv_data_len = net_recv(logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, localhost); ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Failed to receive server response to request. %d", recv_data_len); memcpy(&size, packet_resp, 2); @@ -182,17 +184,17 @@ struct sec_TCP_con { uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; }; -static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s, Mono_Time *mono_time) +static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s, Mono_Time *mono_time) { struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); ck_assert(sec_c != nullptr); Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); - IP_Port ip_port_loopback; - ip_port_loopback.ip = get_loopback(); - ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); + IP_Port localhost; + localhost.ip = get_loopback(); + localhost.port = net_htons(ports[random_u32() % NUM_PORTS]); - int ret = net_connect(sock, ip_port_loopback); + int ret = net_connect(logger, sock, localhost); ck_assert_msg(ret == 0, "Failed to connect to the test TCP relay server."); uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -212,19 +214,20 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s, Mono_Time *mono_time) ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), "Failed to encrypt the outgoing handshake."); - ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, + localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "Failed to send the first portion of the handshake to the TCP relay server."); do_TCP_server_delay(tcp_s, mono_time, 50); - ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, + ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, localhost) == 1, "Failed to send last byte of handshake."); do_TCP_server_delay(tcp_s, mono_time, 50); uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; - ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, + ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, localhost) == TCP_SERVER_HANDSHAKE_SIZE, "Failed to receive server handshake response."); ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE, TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); @@ -241,7 +244,8 @@ static void kill_TCP_con(struct sec_TCP_con *con) free(con); } -static int write_packet_TCP_test_connection(struct sec_TCP_con *con, const uint8_t *data, uint16_t length) +static int write_packet_TCP_test_connection(const Logger *logger, struct sec_TCP_con *con, const uint8_t *data, + uint16_t length) { VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); @@ -255,13 +259,22 @@ static int write_packet_TCP_test_connection(struct sec_TCP_con *con, const uint8 increment_nonce(con->sent_nonce); - ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "Failed to send a packet."); + IP_Port localhost; + localhost.ip = get_loopback(); + localhost.port = 0; + + ck_assert_msg(net_send(logger, con->sock, packet, SIZEOF_VLA(packet), localhost) == SIZEOF_VLA(packet), + "Failed to send a packet."); return 0; } -static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length) +static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, uint8_t *data, uint16_t length) { - int rlen = net_recv(con->sock, data, length); + IP_Port localhost; + localhost.ip = get_loopback(); + localhost.port = 0; + + int rlen = net_recv(logger, con->sock, data, length, localhost); ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen); rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data); ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server."); @@ -281,31 +294,31 @@ static void test_some(void) ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports."); - struct sec_TCP_con *con1 = new_TCP_con(tcp_s, mono_time); - struct sec_TCP_con *con2 = new_TCP_con(tcp_s, mono_time); - struct sec_TCP_con *con3 = new_TCP_con(tcp_s, mono_time); + struct sec_TCP_con *con1 = new_TCP_con(logger, tcp_s, mono_time); + struct sec_TCP_con *con2 = new_TCP_con(logger, tcp_s, mono_time); + struct sec_TCP_con *con3 = new_TCP_con(logger, tcp_s, mono_time); uint8_t requ_p[1 + CRYPTO_PUBLIC_KEY_SIZE]; requ_p[0] = TCP_PACKET_ROUTING_REQUEST; // Sending wrong public keys to test server response. memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE); - write_packet_TCP_test_connection(con1, requ_p, sizeof(requ_p)); + write_packet_TCP_test_connection(logger, con1, requ_p, sizeof(requ_p)); memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE); - write_packet_TCP_test_connection(con3, requ_p, sizeof(requ_p)); + write_packet_TCP_test_connection(logger, con3, requ_p, sizeof(requ_p)); do_TCP_server_delay(tcp_s, mono_time, 50); // Testing response from connection 1 uint8_t data[2048]; - int len = read_packet_sec_TCP(con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); + int len = read_packet_sec_TCP(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong."); // Connection 3 - len = read_packet_sec_TCP(con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); @@ -313,55 +326,55 @@ static void test_some(void) uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? - write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet)); - write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet)); - write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con3, test_packet, sizeof(test_packet)); do_TCP_server_delay(tcp_s, mono_time, 50); - len = read_packet_sec_TCP(con1, data, 2 + 2 + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con1, data, 2 + 2 + CRYPTO_MAC_SIZE); ck_assert_msg(len == 2, "wrong len %d", len); ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]); ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); - len = read_packet_sec_TCP(con3, data, 2 + 2 + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con3, data, 2 + 2 + CRYPTO_MAC_SIZE); ck_assert_msg(len == 2, "wrong len %d", len); ck_assert_msg(data[0] == TCP_PACKET_CONNECTION_NOTIFICATION, "wrong packet id %u", data[0]); ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); - len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet)); - write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet)); - write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet)); + write_packet_TCP_test_connection(logger, con1, test_packet, sizeof(test_packet)); do_TCP_server_delay(tcp_s, mono_time, 50); - len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); - len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len); ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); uint8_t ping_packet[1 + sizeof(uint64_t)] = {TCP_PACKET_PING, 8, 6, 9, 67}; - write_packet_TCP_test_connection(con1, ping_packet, sizeof(ping_packet)); + write_packet_TCP_test_connection(logger, con1, ping_packet, sizeof(ping_packet)); do_TCP_server_delay(tcp_s, mono_time, 50); - len = read_packet_sec_TCP(con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); + len = read_packet_sec_TCP(logger, con1, data, 2 + sizeof(ping_packet) + CRYPTO_MAC_SIZE); ck_assert_msg(len == sizeof(ping_packet), "wrong len %d", len); ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]); ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); @@ -477,8 +490,8 @@ static void test_client(void) ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); - TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, - nullptr); + TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f_public_key, + f_secret_key, nullptr); do_TCP_connection(logger, mono_time, conn, nullptr); c_sleep(50); @@ -512,7 +525,7 @@ static void test_client(void) uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(f2_public_key, f2_secret_key); ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); - TCP_Client_Connection *conn2 = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f2_public_key, + TCP_Client_Connection *conn2 = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, nullptr); // The client should call this function (defined earlier) during the routing process. @@ -538,9 +551,9 @@ static void test_client(void) uint8_t data[5] = {1, 2, 3, 4, 5}; memcpy(oob_pubkey, f2_public_key, CRYPTO_PUBLIC_KEY_SIZE); - send_oob_packet(conn2, f_public_key, data, 5); - send_routing_request(conn, f2_public_key); - send_routing_request(conn2, f_public_key); + send_oob_packet(logger, conn2, f_public_key, data, 5); + send_routing_request(logger, conn, f2_public_key); + send_routing_request(logger, conn2, f_public_key); do_TCP_server_delay(tcp_s, mono_time, 50); @@ -558,7 +571,7 @@ static void test_client(void) do_TCP_server_delay(tcp_s, mono_time, 50); - ck_assert_msg(send_data(conn2, 0, data, 5) == 1, "Failed a send_data() call."); + ck_assert_msg(send_data(logger, conn2, 0, data, 5) == 1, "Failed a send_data() call."); do_TCP_server_delay(tcp_s, mono_time, 50); @@ -566,7 +579,7 @@ static void test_client(void) do_TCP_connection(logger, mono_time, conn2, nullptr); ck_assert_msg(data_callback_good == 1, "Data callback was not called."); status_callback_good = 0; - send_disconnect_request(conn2, 0); + send_disconnect_request(logger, conn2, 0); do_TCP_server_delay(tcp_s, mono_time, 50); @@ -601,8 +614,8 @@ static void test_client_invalid(void) ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); - TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, - nullptr); + TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f_public_key, + f_secret_key, nullptr); // Run the client's main loop but not the server. mono_time_update(mono_time); @@ -672,11 +685,11 @@ static void test_tcp_connection(void) TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); + TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); + TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); IP_Port ip_port_tcp_s; @@ -780,11 +793,11 @@ static void test_tcp_connection2(void) TCP_Proxy_Info proxy_info; proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_1 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); + TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key"); crypto_new_keypair(self_public_key, self_secret_key); - TCP_Connections *tc_2 = new_tcp_connections(mono_time, self_secret_key, &proxy_info); + TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info); ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key"); IP_Port ip_port_tcp_s; diff --git a/other/bootstrap_daemon/docker/Dockerfile b/other/bootstrap_daemon/docker/Dockerfile index f5450e68ce..67e7df5c04 100644 --- a/other/bootstrap_daemon/docker/Dockerfile +++ b/other/bootstrap_daemon/docker/Dockerfile @@ -32,6 +32,8 @@ COPY CMakeLists.txt so.version ./ RUN cmake -B_build -H. \ -GNinja \ + -DCMAKE_C_FLAGS=-DTCP_SERVER_USE_EPOLL \ + -DMIN_LOGGER_LEVEL=DEBUG \ -DCMAKE_BUILD_TYPE=Release \ -DFULLY_STATIC=ON \ -DBUILD_TOXAV=OFF \ diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index d7464923d5..9e540e60fc 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -33548aa46a757ac403a823dbd02151580ead0fe51ea4f334cfeddaefb5391150 /usr/local/bin/tox-bootstrapd +43f09f0e054355d37a774a57beded8b371de085ae321d4208b46785f430613c1 /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/docker/update-sha256 b/other/bootstrap_daemon/docker/update-sha256 index 32eb4e32b6..8ec1d4b44e 100755 --- a/other/bootstrap_daemon/docker/update-sha256 +++ b/other/bootstrap_daemon/docker/update-sha256 @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh set -eux diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 91f57ea94b..731f19c228 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -95,14 +95,14 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) /** return 1 on success * return 0 on failure */ -static int connect_sock_to(Socket sock, IP_Port ip_port, const TCP_Proxy_Info *proxy_info) +static int connect_sock_to(const Logger *logger, Socket sock, IP_Port ip_port, const TCP_Proxy_Info *proxy_info) { if (proxy_info->proxy_type != TCP_PROXY_NONE) { ip_port = proxy_info->ip_port; } /* nonblocking socket, connect will never return success */ - net_connect(sock, ip_port); + net_connect(logger, sock, ip_port); return 1; } @@ -145,7 +145,7 @@ static int proxy_http_read_connection_response(const Logger *logger, const TCP_C char success[] = "200"; uint8_t data[16]; // draining works the best if the length is a power of 2 - int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data) - 1); + int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data) - 1, tcp_conn->con.ip_port); if (ret == -1) { return 0; @@ -159,7 +159,7 @@ static int proxy_http_read_connection_response(const Logger *logger, const TCP_C if (data_left) { VLA(uint8_t, temp_data, data_left); - read_TCP_packet(logger, tcp_conn->con.sock, temp_data, data_left); + read_TCP_packet(logger, tcp_conn->con.sock, temp_data, data_left, tcp_conn->con.ip_port); } return 1; @@ -194,7 +194,7 @@ static void proxy_socks5_generate_greetings(TCP_Client_Connection *tcp_conn) static int socks5_read_handshake_response(const Logger *logger, const TCP_Client_Connection *tcp_conn) { uint8_t data[2]; - int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data)); + int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data), tcp_conn->con.ip_port); if (ret == -1) { return 0; @@ -241,7 +241,7 @@ static int proxy_socks5_read_connection_response(const Logger *logger, const TCP { if (net_family_is_ipv4(tcp_conn->ip_port.ip.family)) { uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; - int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data)); + int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data), tcp_conn->con.ip_port); if (ret == -1) { return 0; @@ -252,7 +252,7 @@ static int proxy_socks5_read_connection_response(const Logger *logger, const TCP } } else { uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)]; - int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data)); + int ret = read_TCP_packet(logger, tcp_conn->con.sock, data, sizeof(data), tcp_conn->con.ip_port); if (ret == -1) { return 0; @@ -314,12 +314,12 @@ static int handle_handshake(TCP_Client_Connection *tcp_conn, const uint8_t *data * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_routing_request(TCP_Client_Connection *con, const uint8_t *public_key) +int send_routing_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key) { uint8_t packet[1 + CRYPTO_PUBLIC_KEY_SIZE]; packet[0] = TCP_PACKET_ROUTING_REQUEST; memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); - return write_packet_TCP_secure_connection(&con->con, packet, sizeof(packet), 1); + return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); } void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object) @@ -334,14 +334,14 @@ void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *s con->status_callback_object = object; } -static int tcp_send_ping_response(TCP_Client_Connection *con); -static int tcp_send_ping_request(TCP_Client_Connection *con); +static int tcp_send_ping_response(const Logger *logger, TCP_Client_Connection *con); +static int tcp_send_ping_request(const Logger *logger, TCP_Client_Connection *con); /** return 1 on success. * return 0 if could not send packet. * return -1 on failure. */ -int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length) +int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length) { if (con_id >= NUM_CLIENT_CONNECTIONS) { return -1; @@ -351,21 +351,22 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u return -1; } - if (tcp_send_ping_response(con) == 0 || tcp_send_ping_request(con) == 0) { + if (tcp_send_ping_response(logger, con) == 0 || tcp_send_ping_request(logger, con) == 0) { return 0; } VLA(uint8_t, packet, 1 + length); packet[0] = con_id + NUM_RESERVED_PORTS; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(&con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure. */ -int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length) +int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, + uint16_t length) { if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) { return -1; @@ -375,7 +376,7 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const packet[0] = TCP_PACKET_OOB_SEND; memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); - return write_packet_TCP_secure_connection(&con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); } @@ -416,19 +417,19 @@ void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_call * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int client_send_disconnect_notification(TCP_Client_Connection *con, uint8_t id) +static int client_send_disconnect_notification(const Logger *logger, TCP_Client_Connection *con, uint8_t id) { uint8_t packet[1 + 1]; packet[0] = TCP_PACKET_DISCONNECT_NOTIFICATION; packet[1] = id; - return write_packet_TCP_secure_connection(&con->con, packet, sizeof(packet), 1); + return write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int tcp_send_ping_request(TCP_Client_Connection *con) +static int tcp_send_ping_request(const Logger *logger, TCP_Client_Connection *con) { if (!con->ping_request_id) { return 1; @@ -437,7 +438,7 @@ static int tcp_send_ping_request(TCP_Client_Connection *con) uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PING; memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t)); - const int ret = write_packet_TCP_secure_connection(&con->con, packet, sizeof(packet), 1); + const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); if (ret == 1) { con->ping_request_id = 0; @@ -450,7 +451,7 @@ static int tcp_send_ping_request(TCP_Client_Connection *con) * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int tcp_send_ping_response(TCP_Client_Connection *con) +static int tcp_send_ping_response(const Logger *logger, TCP_Client_Connection *con) { if (!con->ping_response_id) { return 1; @@ -459,7 +460,7 @@ static int tcp_send_ping_response(TCP_Client_Connection *con) uint8_t packet[1 + sizeof(uint64_t)]; packet[0] = TCP_PACKET_PONG; memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t)); - const int ret = write_packet_TCP_secure_connection(&con->con, packet, sizeof(packet), 1); + const int ret = write_packet_TCP_secure_connection(logger, &con->con, packet, sizeof(packet), 1); if (ret == 1) { con->ping_response_id = 0; @@ -472,7 +473,7 @@ static int tcp_send_ping_response(TCP_Client_Connection *con) * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id) +int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id) { if (con_id >= NUM_CLIENT_CONNECTIONS) { return -1; @@ -480,19 +481,19 @@ int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id) con->connections[con_id].status = 0; con->connections[con_id].number = 0; - return client_send_disconnect_notification(con, con_id + NUM_RESERVED_PORTS); + return client_send_disconnect_notification(logger, con, con_id + NUM_RESERVED_PORTS); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length) +int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length) { VLA(uint8_t, packet, 1 + length); packet[0] = TCP_PACKET_ONION_REQUEST; memcpy(packet + 1, data, length); - return write_packet_TCP_secure_connection(&con->con, packet, SIZEOF_VLA(packet), 0); + return write_packet_TCP_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), 0); } void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object) @@ -503,8 +504,9 @@ void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *o /** Create new TCP connection to ip_port/public_key */ -TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip_port, const uint8_t *public_key, - const uint8_t *self_public_key, const uint8_t *self_secret_key, const TCP_Proxy_Info *proxy_info) +TCP_Client_Connection *new_TCP_connection(const Logger *logger, const Mono_Time *mono_time, IP_Port ip_port, + const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, + const TCP_Proxy_Info *proxy_info) { if (networking_at_startup() != 0) { return nullptr; @@ -537,7 +539,7 @@ TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip return nullptr; } - if (!(set_socket_nonblock(sock) && connect_sock_to(sock, ip_port, proxy_info))) { + if (!(set_socket_nonblock(sock) && connect_sock_to(logger, sock, ip_port, proxy_info))) { kill_sock(sock); return nullptr; } @@ -550,6 +552,7 @@ TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip } temp->con.sock = sock; + temp->con.ip_port = ip_port; memcpy(temp->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(temp->self_public_key, self_public_key, CRYPTO_PUBLIC_KEY_SIZE); encrypt_precompute(temp->public_key, self_secret_key, temp->con.shared_key); @@ -590,7 +593,8 @@ TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip /** return 0 on success * return -1 on failure */ -static int handle_TCP_client_packet(TCP_Client_Connection *conn, const uint8_t *data, uint16_t length, void *userdata) +static int handle_TCP_client_packet(const Logger *logger, TCP_Client_Connection *conn, const uint8_t *data, + uint16_t length, void *userdata) { if (length <= 1) { return -1; @@ -685,7 +689,7 @@ static int handle_TCP_client_packet(TCP_Client_Connection *conn, const uint8_t * uint64_t ping_id; memcpy(&ping_id, data + 1, sizeof(uint64_t)); conn->ping_response_id = ping_id; - tcp_send_ping_response(conn); + tcp_send_ping_response(logger, conn); return 0; } @@ -747,7 +751,7 @@ static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn { uint8_t packet[MAX_PACKET_SIZE]; const int len = read_packet_TCP_secure_connection(logger, conn->con.sock, &conn->next_packet_length, - conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet)); + conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), conn->ip_port); if (len == 0) { return false; @@ -758,7 +762,7 @@ static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn return false; } - if (handle_TCP_client_packet(conn, packet, len, userdata) == -1) { + if (handle_TCP_client_packet(logger, conn, packet, len, userdata) == -1) { conn->status = TCP_CLIENT_DISCONNECTED; return false; } @@ -769,9 +773,9 @@ static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn static int do_confirmed_TCP(const Logger *logger, TCP_Client_Connection *conn, const Mono_Time *mono_time, void *userdata) { - send_pending_data(&conn->con); - tcp_send_ping_response(conn); - tcp_send_ping_request(conn); + send_pending_data(logger, &conn->con); + tcp_send_ping_response(logger, conn); + tcp_send_ping_request(logger, conn); if (mono_time_is_timeout(mono_time, conn->last_pinged, TCP_PING_FREQUENCY)) { uint64_t ping_id = random_u64(); @@ -782,7 +786,7 @@ static int do_confirmed_TCP(const Logger *logger, TCP_Client_Connection *conn, c conn->ping_request_id = ping_id; conn->ping_id = ping_id; - tcp_send_ping_request(conn); + tcp_send_ping_request(logger, conn); conn->last_pinged = mono_time_get(mono_time); } @@ -809,7 +813,7 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time, } if (tcp_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) { - if (send_pending_data(&tcp_connection->con) == 0) { + if (send_pending_data(logger, &tcp_connection->con) == 0) { int ret = proxy_http_read_connection_response(logger, tcp_connection); if (ret == -1) { @@ -825,7 +829,7 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time, } if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) { - if (send_pending_data(&tcp_connection->con) == 0) { + if (send_pending_data(logger, &tcp_connection->con) == 0) { int ret = socks5_read_handshake_response(logger, tcp_connection); if (ret == -1) { @@ -841,7 +845,7 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time, } if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) { - if (send_pending_data(&tcp_connection->con) == 0) { + if (send_pending_data(logger, &tcp_connection->con) == 0) { int ret = proxy_socks5_read_connection_response(logger, tcp_connection); if (ret == -1) { @@ -857,14 +861,14 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time, } if (tcp_connection->status == TCP_CLIENT_CONNECTING) { - if (send_pending_data(&tcp_connection->con) == 0) { + if (send_pending_data(logger, &tcp_connection->con) == 0) { tcp_connection->status = TCP_CLIENT_UNCONFIRMED; } } if (tcp_connection->status == TCP_CLIENT_UNCONFIRMED) { uint8_t data[TCP_SERVER_HANDSHAKE_SIZE]; - int len = read_TCP_packet(logger, tcp_connection->con.sock, data, sizeof(data)); + int len = read_TCP_packet(logger, tcp_connection->con.sock, data, sizeof(data), tcp_connection->con.ip_port); if (sizeof(data) == len) { if (handle_handshake(tcp_connection, data) == 0) { diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 75d5b44134..817521f2f2 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -49,8 +49,9 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value); /** Create new TCP connection to ip_port/public_key */ -TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip_port, const uint8_t *public_key, - const uint8_t *self_public_key, const uint8_t *self_secret_key, const TCP_Proxy_Info *proxy_info); +TCP_Client_Connection *new_TCP_connection(const Logger *logger, const Mono_Time *mono_time, IP_Port ip_port, + const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, + const TCP_Proxy_Info *proxy_info); /** Run the TCP connection */ @@ -67,7 +68,7 @@ typedef int tcp_onion_response_cb(void *object, const uint8_t *data, uint16_t le * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length); +int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length); void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object); typedef int tcp_routing_response_cb(void *object, uint8_t connection_id, const uint8_t *public_key); @@ -77,7 +78,7 @@ typedef int tcp_routing_status_cb(void *object, uint32_t number, uint8_t connect * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_routing_request(TCP_Client_Connection *con, const uint8_t *public_key); +int send_routing_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key); void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object); void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *status_callback, void *object); @@ -85,7 +86,7 @@ void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *s * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id); +int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id); /** Set the number that will be used as an argument in the callbacks related to con_id. * @@ -103,7 +104,7 @@ typedef int tcp_routing_data_cb(void *object, uint32_t number, uint8_t connectio * return 0 if could not send packet. * return -1 on failure. */ -int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length); +int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length); void routing_data_handler(TCP_Client_Connection *con, tcp_routing_data_cb *data_callback, void *object); typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, @@ -113,7 +114,8 @@ typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8 * return 0 if could not send packet. * return -1 on failure. */ -int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length); +int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, + uint16_t length); void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_callback, void *object); diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index 0eddd17d7c..e0afea7639 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -21,14 +21,14 @@ void wipe_priority_list(TCP_Priority_List *p) /** return 0 if pending data was sent completely * return -1 if it wasn't */ -int send_pending_data_nonpriority(TCP_Connection *con) +int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con) { if (con->last_packet_length == 0) { return 0; } const uint16_t left = con->last_packet_length - con->last_packet_sent; - const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left); + const int len = net_send(logger, con->sock, con->last_packet + con->last_packet_sent, left, con->ip_port); if (len <= 0) { return -1; @@ -47,10 +47,10 @@ int send_pending_data_nonpriority(TCP_Connection *con) /** return 0 if pending data was sent completely * return -1 if it wasn't */ -int send_pending_data(TCP_Connection *con) +int send_pending_data(const Logger *logger, TCP_Connection *con) { /* finish sending current non-priority packet */ - if (send_pending_data_nonpriority(con) == -1) { + if (send_pending_data_nonpriority(logger, con) == -1) { return -1; } @@ -58,7 +58,7 @@ int send_pending_data(TCP_Connection *con) while (p) { const uint16_t left = p->size - p->sent; - const int len = net_send(con->sock, p->data + p->sent, left); + const int len = net_send(logger, con->sock, p->data + p->sent, left, con->ip_port); if (len != left) { if (len > 0) { @@ -122,7 +122,8 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, uint16_t length, bool priority) +int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length, + bool priority) { if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE) { return -1; @@ -130,7 +131,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, bool sendpriority = 1; - if (send_pending_data(con) == -1) { + if (send_pending_data(logger, con) == -1) { if (priority) { sendpriority = 0; } else { @@ -149,7 +150,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, } if (priority) { - len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0; + len = sendpriority ? net_send(logger, con->sock, packet, SIZEOF_VLA(packet), con->ip_port) : 0; if (len <= 0) { len = 0; @@ -164,7 +165,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, return add_priority(con, packet, SIZEOF_VLA(packet), len); } - len = net_send(con->sock, packet, SIZEOF_VLA(packet)); + len = net_send(logger, con->sock, packet, SIZEOF_VLA(packet), con->ip_port); if (len <= 0) { return 0; @@ -187,22 +188,23 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, * return length on success * return -1 on failure/no data in buffer. */ -int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length) +int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length, IP_Port ip_port) { const uint16_t count = net_socket_data_recv_buffer(sock); - if (count >= length) { - const int len = net_recv(sock, data, length); + if (count < length) { + LOGGER_INFO(logger, "recv buffer has %d bytes, but requested %d bytes", count, length); + return -1; + } - if (len != length) { - LOGGER_ERROR(logger, "FAIL recv packet"); - return -1; - } + const int len = net_recv(logger, sock, data, length, ip_port); - return len; + if (len != length) { + LOGGER_ERROR(logger, "FAIL recv packet"); + return -1; } - return -1; + return len; } /** Read the next two bytes in TCP stream then convert them to @@ -212,22 +214,24 @@ int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t l * return 0 if nothing has been read from socket. * return -1 on failure. */ -static uint16_t read_TCP_length(const Logger *logger, Socket sock) +static uint16_t read_TCP_length(const Logger *logger, Socket sock, IP_Port ip_port) { const uint16_t count = net_socket_data_recv_buffer(sock); if (count >= sizeof(uint16_t)) { - uint16_t length; - const int len = net_recv(sock, &length, sizeof(uint16_t)); + uint8_t length_buf[sizeof(uint16_t)]; + const int len = net_recv(logger, sock, length_buf, sizeof(length_buf), ip_port); if (len != sizeof(uint16_t)) { LOGGER_ERROR(logger, "FAIL recv packet"); return 0; } - length = net_ntohs(length); + uint16_t length; + net_unpack_u16(length_buf, &length); if (length > MAX_PACKET_SIZE) { + LOGGER_WARNING(logger, "TCP packet too large: %d > %d", length, MAX_PACKET_SIZE); return -1; } @@ -242,10 +246,10 @@ static uint16_t read_TCP_length(const Logger *logger, Socket sock) * return -1 on failure (connection must be killed). */ int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length, - const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len) + const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len, IP_Port ip_port) { if (*next_packet_length == 0) { - uint16_t len = read_TCP_length(logger, sock); + const uint16_t len = read_TCP_length(logger, sock, ip_port); if (len == (uint16_t) -1) { return -1; @@ -259,21 +263,24 @@ int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_ } if (max_len + CRYPTO_MAC_SIZE < *next_packet_length) { + LOGGER_DEBUG(logger, "packet too large"); return -1; } VLA(uint8_t, data_encrypted, *next_packet_length); - int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length); + const int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length, ip_port); if (len_packet != *next_packet_length) { + LOGGER_WARNING(logger, "invalid packet length: %d, expected %d", len_packet, *next_packet_length); return 0; } *next_packet_length = 0; - int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data); + const int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data); if (len + CRYPTO_MAC_SIZE != len_packet) { + LOGGER_WARNING(logger, "decrypted length %d does not match expected length %d", len + CRYPTO_MAC_SIZE, len_packet); return -1; } diff --git a/toxcore/TCP_common.h b/toxcore/TCP_common.h index 528ab0f100..47aac96ce5 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -46,6 +46,7 @@ void wipe_priority_list(TCP_Priority_List *p); typedef struct TCP_Connection { Socket sock; + IP_Port ip_port; // for debugging. uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; uint8_t last_packet[2 + MAX_PACKET_SIZE]; @@ -59,12 +60,12 @@ typedef struct TCP_Connection { /** return 0 if pending data was sent completely * return -1 if it wasn't */ -int send_pending_data_nonpriority(TCP_Connection *con); +int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con); /** return 0 if pending data was sent completely * return -1 if it wasn't */ -int send_pending_data(TCP_Connection *con); +int send_pending_data(const Logger *logger, TCP_Connection *con); /** return 0 on failure (only if calloc fails) * return 1 on success @@ -75,20 +76,21 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, uint16_t length, bool priority); +int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length, + bool priority); /** Read length bytes from socket. * * return length on success * return -1 on failure/no data in buffer. */ -int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length); +int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length, IP_Port ip_port); /** return length of received packet on success. * return 0 if could not read any packet. * return -1 on failure (connection must be killed). */ int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length, - const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len); + const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len, IP_Port ip_port); #endif diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 90c833ab46..60b926c157 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -17,6 +17,7 @@ #include "util.h" struct TCP_Connections { + const Logger *logger; Mono_Time *mono_time; DHT *dht; @@ -317,7 +318,7 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num continue; } - ret = send_data(tcp_con->connection, connection_id, packet, length); + ret = send_data(tcp_c->logger, tcp_con->connection, connection_id, packet, length); if (ret == 0) { limit_reached = 1; @@ -349,7 +350,7 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num continue; } - if (send_oob_packet(tcp_con->connection, con_to->public_key, packet, length) == 1) { + if (send_oob_packet(tcp_c->logger, tcp_con->connection, con_to->public_key, packet, length) == 1) { ret += 1; } } @@ -401,7 +402,8 @@ int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_numb } if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) { - int ret = send_onion_request(tcp_c->tcp_connections[tcp_connections_number].connection, data, length); + const int ret = send_onion_request(tcp_c->logger, tcp_c->tcp_connections[tcp_connections_number].connection, data, + length); if (ret == 1) { return 0; @@ -429,7 +431,7 @@ int tcp_send_oob_packet(const TCP_Connections *tcp_c, unsigned int tcp_connectio return -1; } - int ret = send_oob_packet(tcp_con->connection, public_key, packet, length); + const int ret = send_oob_packet(tcp_c->logger, tcp_con->connection, public_key, packet, length); if (ret == 1) { return 0; @@ -578,7 +580,7 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number) } if (tcp_con->status == TCP_CONN_CONNECTED) { - send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id); + send_disconnect_request(tcp_c->logger, tcp_con->connection, con_to->connections[i].connection_id); } if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { @@ -797,7 +799,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); kill_TCP_connection(tcp_con->connection); - tcp_con->connection = new_TCP_connection(tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key, + tcp_con->connection = new_TCP_connection(tcp_c->logger, tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); if (!tcp_con->connection) { @@ -883,8 +885,8 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti return -1; } - tcp_con->connection = new_TCP_connection(tcp_c->mono_time, tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key, - tcp_c->self_secret_key, &tcp_c->proxy_info); + tcp_con->connection = new_TCP_connection(tcp_c->logger, tcp_c->mono_time, tcp_con->ip_port, tcp_con->relay_pk, + tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); if (!tcp_con->connection) { kill_tcp_relay_connection(tcp_c, tcp_connections_number); @@ -917,7 +919,7 @@ static int send_tcp_relay_routing_request(const TCP_Connections *tcp_c, int tcp_ return -1; } - if (send_routing_request(tcp_con->connection, public_key) != 1) { + if (send_routing_request(tcp_c->logger, tcp_con->connection, public_key) != 1) { return -1; } @@ -1155,7 +1157,7 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; - tcp_con->connection = new_TCP_connection(tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key, + tcp_con->connection = new_TCP_connection(tcp_c->logger, tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); if (!tcp_con->connection) { @@ -1390,7 +1392,8 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status) * * Returns NULL on failure. */ -TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info) +TCP_Connections *new_tcp_connections(const Logger *logger, Mono_Time *mono_time, const uint8_t *secret_key, + const TCP_Proxy_Info *proxy_info) { if (secret_key == nullptr) { return nullptr; @@ -1402,6 +1405,7 @@ TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret return nullptr; } + temp->logger = logger; temp->mono_time = mono_time; memcpy(temp->self_secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 3cf1288791..9a23442f6c 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -218,7 +218,8 @@ uint32_t tcp_copy_connected_relays(const TCP_Connections *tcp_c, Node_format *tc * * Returns NULL on failure. */ -TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info); +TCP_Connections *new_tcp_connections(const Logger *logger, Mono_Time *mono_time, const uint8_t *secret_key, + const TCP_Proxy_Info *proxy_info); int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number); diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 8155d922cc..42fb51948a 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -286,14 +286,16 @@ static int kill_accepted(TCP_Server *tcp_server, int index) /** return 1 if everything went well. * return -1 if the connection must be killed. */ -static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, +static int handle_TCP_handshake(const Logger *logger, TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, const uint8_t *self_secret_key) { if (length != TCP_CLIENT_HANDSHAKE_SIZE) { + LOGGER_WARNING(logger, "invalid handshake length: %d != %d", length, TCP_CLIENT_HANDSHAKE_SIZE); return -1; } if (con->status != TCP_STATUS_CONNECTED) { + LOGGER_WARNING(logger, "TCP connection %u not connected", (unsigned int)con->identifier); return -1; } @@ -304,6 +306,7 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, data + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE, plain); if (len != TCP_HANDSHAKE_PLAIN_SIZE) { + LOGGER_WARNING(logger, "invalid TCP handshake decrypted length: %d != %d", len, TCP_HANDSHAKE_PLAIN_SIZE); crypto_memzero(shared_key, sizeof(shared_key)); return -1; } @@ -327,7 +330,9 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, return -1; } - if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE)) { + if (TCP_SERVER_HANDSHAKE_SIZE != net_send(logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, (IP_Port) { + 0 +})) { crypto_memzero(shared_key, sizeof(shared_key)); return -1; } @@ -347,47 +352,49 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, static int read_connection_handshake(const Logger *logger, TCP_Secure_Connection *con, const uint8_t *self_secret_key) { uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE]; - const int len = read_TCP_packet(logger, con->con.sock, data, TCP_CLIENT_HANDSHAKE_SIZE); + const int len = read_TCP_packet(logger, con->con.sock, data, TCP_CLIENT_HANDSHAKE_SIZE, con->con.ip_port); - if (len != -1) { - return handle_TCP_handshake(con, data, len, self_secret_key); + if (len == -1) { + LOGGER_WARNING(logger, "connection handshake is not ready yet"); + return 0; } - return 0; + return handle_TCP_handshake(logger, con, data, len, self_secret_key); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const uint8_t *public_key) +static int send_routing_response(const Logger *logger, TCP_Secure_Connection *con, uint8_t rpid, + const uint8_t *public_key) { uint8_t data[1 + 1 + CRYPTO_PUBLIC_KEY_SIZE]; data[0] = TCP_PACKET_ROUTING_RESPONSE; data[1] = rpid; memcpy(data + 2, public_key, CRYPTO_PUBLIC_KEY_SIZE); - return write_packet_TCP_secure_connection(&con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) +static int send_connect_notification(const Logger *logger, TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, (uint8_t)(id + NUM_RESERVED_PORTS)}; - return write_packet_TCP_secure_connection(&con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); } /** return 1 on success. * return 0 if could not send packet. * return -1 on failure (connection must be killed). */ -static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id) +static int send_disconnect_notification(const Logger *logger, TCP_Secure_Connection *con, uint8_t id) { uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, (uint8_t)(id + NUM_RESERVED_PORTS)}; - return write_packet_TCP_secure_connection(&con->con, data, sizeof(data), 1); + return write_packet_TCP_secure_connection(logger, &con->con, data, sizeof(data), 1); } /** return 0 on success. @@ -400,7 +407,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const /* If person tries to cennect to himself we deny the request*/ if (public_key_cmp(con->public_key, public_key) == 0) { - if (send_routing_response(con, 0, public_key) == -1) { + if (send_routing_response(tcp_server->logger, con, 0, public_key) == -1) { return -1; } @@ -410,7 +417,7 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const for (uint32_t i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { if (con->connections[i].status != 0) { if (public_key_cmp(public_key, con->connections[i].public_key) == 0) { - if (send_routing_response(con, i + NUM_RESERVED_PORTS, public_key) == -1) { + if (send_routing_response(tcp_server->logger, con, i + NUM_RESERVED_PORTS, public_key) == -1) { return -1; } @@ -422,14 +429,14 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const } if (index == (uint32_t) -1) { - if (send_routing_response(con, 0, public_key) == -1) { + if (send_routing_response(tcp_server->logger, con, 0, public_key) == -1) { return -1; } return 0; } - int ret = send_routing_response(con, index + NUM_RESERVED_PORTS, public_key); + int ret = send_routing_response(tcp_server->logger, con, index + NUM_RESERVED_PORTS, public_key); if (ret == 0) { return 0; @@ -463,8 +470,8 @@ static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const other_conn->connections[other_id].index = con_id; other_conn->connections[other_id].other_id = index; // TODO(irungentoo): return values? - send_connect_notification(con, index); - send_connect_notification(other_conn, other_id); + send_connect_notification(tcp_server->logger, con, index); + send_connect_notification(tcp_server->logger, other_conn, other_id); } } @@ -490,8 +497,8 @@ static int handle_TCP_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui resp_packet[0] = TCP_PACKET_OOB_RECV; memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); - write_packet_TCP_secure_connection(&tcp_server->accepted_connection_array[other_index].con, resp_packet, - SIZEOF_VLA(resp_packet), 0); + write_packet_TCP_secure_connection(tcp_server->logger, &tcp_server->accepted_connection_array[other_index].con, + resp_packet, SIZEOF_VLA(resp_packet), 0); } return 0; @@ -521,7 +528,7 @@ static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *co tcp_server->accepted_connection_array[index].connections[other_id].index = 0; tcp_server->accepted_connection_array[index].connections[other_id].status = 1; // TODO(irungentoo): return values? - send_disconnect_notification(&tcp_server->accepted_connection_array[index], other_id); + send_disconnect_notification(tcp_server->logger, &tcp_server->accepted_connection_array[index], other_id); } con->connections[con_number].index = 0; @@ -552,7 +559,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, memcpy(packet + 1, data, length); packet[0] = TCP_PACKET_ONION_RESPONSE; - if (write_packet_TCP_secure_connection(&con->con, packet, SIZEOF_VLA(packet), 0) != 1) { + if (write_packet_TCP_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), 0) != 1) { return 1; } @@ -568,7 +575,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } - TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id]; + TCP_Secure_Connection *const con = &tcp_server->accepted_connection_array[con_id]; switch (data[0]) { case TCP_PACKET_ROUTING_REQUEST: { @@ -576,6 +583,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling routing request for %d", con_id); return handle_TCP_routing_req(tcp_server, con_id, data + 1); } @@ -584,6 +592,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling connection notification for %d", con_id); break; } @@ -592,6 +601,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling disconnect notification for %d", con_id); return rm_connection_index(tcp_server, con, data[1] - NUM_RESERVED_PORTS); } @@ -600,10 +610,12 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling ping for %d", con_id); + uint8_t response[1 + sizeof(uint64_t)]; response[0] = TCP_PACKET_PONG; memcpy(response + 1, data + 1, sizeof(uint64_t)); - write_packet_TCP_secure_connection(&con->con, response, sizeof(response), 1); + write_packet_TCP_secure_connection(tcp_server->logger, &con->con, response, sizeof(response), 1); return 0; } @@ -612,6 +624,8 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling pong for %d", con_id); + uint64_t ping_id; memcpy(&ping_id, data + 1, sizeof(uint64_t)); @@ -631,11 +645,15 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling oob send for %d", con_id); + return handle_TCP_oob_send(tcp_server, con_id, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_PUBLIC_KEY_SIZE)); } case TCP_PACKET_ONION_REQUEST: { + LOGGER_DEBUG(tcp_server->logger, "handling onion request for %d", con_id); + if (tcp_server->onion) { if (length <= 1 + CRYPTO_NONCE_SIZE + ONION_SEND_BASE * 2) { return -1; @@ -655,6 +673,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint } case TCP_PACKET_ONION_RESPONSE: { + LOGGER_DEBUG(tcp_server->logger, "handling onion response for %d", con_id); return -1; } @@ -663,7 +682,8 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return -1; } - uint8_t c_id = data[0] - NUM_RESERVED_PORTS; + const uint8_t c_id = data[0] - NUM_RESERVED_PORTS; + LOGGER_DEBUG(tcp_server->logger, "handling packet id %d for %d", c_id, con_id); if (c_id >= NUM_CLIENT_CONNECTIONS) { return -1; @@ -677,12 +697,13 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint return 0; } - uint32_t index = con->connections[c_id].index; - uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS; + const uint32_t index = con->connections[c_id].index; + const uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS; VLA(uint8_t, new_data, length); memcpy(new_data, data, length); new_data[0] = other_c_id; - int ret = write_packet_TCP_secure_connection(&tcp_server->accepted_connection_array[index].con, new_data, length, 0); + const int ret = write_packet_TCP_secure_connection(tcp_server->logger, + &tcp_server->accepted_connection_array[index].con, new_data, length, 0); if (ret == -1) { return -1; @@ -697,12 +718,12 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint static int confirm_TCP_connection(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_Secure_Connection *con, - const uint8_t *data, - uint16_t length) + const uint8_t *data, uint16_t length) { - int index = add_accepted(tcp_server, mono_time, con); + const int index = add_accepted(tcp_server, mono_time, con); if (index == -1) { + LOGGER_DEBUG(tcp_server->logger, "dropping connection %u: not accepted", (unsigned int)con->identifier); kill_TCP_secure_connection(con); return -1; } @@ -710,6 +731,8 @@ static int confirm_TCP_connection(TCP_Server *tcp_server, const Mono_Time *mono_ wipe_secure_connection(con); if (handle_TCP_packet(tcp_server, index, data, length) == -1) { + LOGGER_DEBUG(tcp_server->logger, "dropping connection %u: data packet (len=%d) not handled", + (unsigned int)con->identifier, length); kill_accepted(tcp_server, index); return -1; } @@ -736,11 +759,12 @@ static int accept_connection(TCP_Server *tcp_server, Socket sock) return -1; } - uint16_t index = tcp_server->incoming_connection_queue_index % MAX_INCOMING_CONNECTIONS; + const uint16_t index = tcp_server->incoming_connection_queue_index % MAX_INCOMING_CONNECTIONS; TCP_Secure_Connection *conn = &tcp_server->incoming_connection_queue[index]; if (conn->status != TCP_STATUS_NO_STATUS) { + LOGGER_DEBUG(tcp_server->logger, "connection %d dropped before accepting", index); kill_TCP_secure_connection(conn); } @@ -874,44 +898,54 @@ static void do_TCP_accept_new(TCP_Server *tcp_server) static int do_incoming(TCP_Server *tcp_server, uint32_t i) { - if (tcp_server->incoming_connection_queue[i].status != TCP_STATUS_CONNECTED) { + TCP_Secure_Connection *const conn = &tcp_server->incoming_connection_queue[i]; + + if (conn->status != TCP_STATUS_CONNECTED) { return -1; } - int ret = read_connection_handshake(tcp_server->logger, &tcp_server->incoming_connection_queue[i], - tcp_server->secret_key); + LOGGER_DEBUG(tcp_server->logger, "handling incoming TCP connection %d", i); + + const int ret = read_connection_handshake(tcp_server->logger, conn, tcp_server->secret_key); if (ret == -1) { - kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[i]); - } else if (ret == 1) { - int index_new = tcp_server->unconfirmed_connection_queue_index % MAX_INCOMING_CONNECTIONS; - TCP_Secure_Connection *conn_old = &tcp_server->incoming_connection_queue[i]; - TCP_Secure_Connection *conn_new = &tcp_server->unconfirmed_connection_queue[index_new]; - - if (conn_new->status != TCP_STATUS_NO_STATUS) { - kill_TCP_secure_connection(conn_new); - } + LOGGER_DEBUG(tcp_server->logger, "incoming connection %d dropped due to failed handshake", i); + kill_TCP_secure_connection(conn); + return -1; + } + + if (ret != 1) { + return -1; + } - move_secure_connection(conn_new, conn_old); - ++tcp_server->unconfirmed_connection_queue_index; + const int index_new = tcp_server->unconfirmed_connection_queue_index % MAX_INCOMING_CONNECTIONS; + TCP_Secure_Connection *conn_old = conn; + TCP_Secure_Connection *conn_new = &tcp_server->unconfirmed_connection_queue[index_new]; - return index_new; + if (conn_new->status != TCP_STATUS_NO_STATUS) { + LOGGER_WARNING(tcp_server->logger, "incoming connection %d would overwrite existing", i); + kill_TCP_secure_connection(conn_new); } - return -1; + move_secure_connection(conn_new, conn_old); + ++tcp_server->unconfirmed_connection_queue_index; + + return index_new; } static int do_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_time, uint32_t i) { - TCP_Secure_Connection *conn = &tcp_server->unconfirmed_connection_queue[i]; + TCP_Secure_Connection *const conn = &tcp_server->unconfirmed_connection_queue[i]; if (conn->status != TCP_STATUS_UNCONFIRMED) { return -1; } + LOGGER_DEBUG(tcp_server->logger, "handling unconfirmed TCP connection %d", i); + uint8_t packet[MAX_PACKET_SIZE]; - int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->con.sock, &conn->next_packet_length, - conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet)); + const int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->con.sock, &conn->next_packet_length, + conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), conn->con.ip_port); if (len == 0) { return -1; @@ -930,8 +964,9 @@ static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i) TCP_Secure_Connection *const conn = &tcp_server->accepted_connection_array[i]; uint8_t packet[MAX_PACKET_SIZE]; - int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->con.sock, &conn->next_packet_length, - conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet)); + const int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->con.sock, &conn->next_packet_length, + conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), conn->con.ip_port); + LOGGER_DEBUG(tcp_server->logger, "processing packet for %d: %d", i, len); if (len == 0) { return false; @@ -943,6 +978,7 @@ static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i) } if (handle_TCP_packet(tcp_server, i, packet, len) == -1) { + LOGGER_DEBUG(tcp_server->logger, "dropping connection %d: data packet (len=%d) not handled", i, len); kill_accepted(tcp_server, i); return false; } @@ -1002,7 +1038,7 @@ static void do_TCP_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time) } memcpy(ping + 1, &ping_id, sizeof(uint64_t)); - int ret = write_packet_TCP_secure_connection(&conn->con, ping, sizeof(ping), 1); + int ret = write_packet_TCP_secure_connection(tcp_server->logger, &conn->con, ping, sizeof(ping), 1); if (ret == 1) { conn->last_pinged = mono_time_get(mono_time); @@ -1020,7 +1056,7 @@ static void do_TCP_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time) continue; } - send_pending_data(&conn->con); + send_pending_data(tcp_server->logger, &conn->con); #ifndef TCP_SERVER_USE_EPOLL @@ -1047,20 +1083,24 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time switch (status) { case TCP_SOCKET_LISTENING: { // should never happen + LOGGER_ERROR(tcp_server->logger, "connection %d was in listening state", index); break; } case TCP_SOCKET_INCOMING: { + LOGGER_DEBUG(tcp_server->logger, "incoming connection %d dropped", index); kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[index]); break; } case TCP_SOCKET_UNCONFIRMED: { + LOGGER_DEBUG(tcp_server->logger, "unconfirmed connection %d dropped", index); kill_TCP_secure_connection(&tcp_server->unconfirmed_connection_queue[index]); break; } case TCP_SOCKET_CONFIRMED: { + LOGGER_DEBUG(tcp_server->logger, "confirmed connection %d dropped", index); kill_accepted(tcp_server, index); break; } @@ -1084,7 +1124,7 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time break; } - int index_new = accept_connection(tcp_server, sock_new); + const int index_new = accept_connection(tcp_server, sock_new); if (index_new == -1) { continue; @@ -1097,6 +1137,7 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time ev.data.u64 = sock_new.socket | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40); if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, sock_new.socket, &ev) == -1) { + LOGGER_DEBUG(tcp_server->logger, "new connection %d was dropped due to epoll error %d", index, net_error()); kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[index_new]); continue; } @@ -1109,10 +1150,12 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time const int index_new = do_incoming(tcp_server, index); if (index_new != -1) { + LOGGER_DEBUG(tcp_server->logger, "incoming connection %d was accepted as %d", index, index_new); events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40); if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { + LOGGER_DEBUG(tcp_server->logger, "incoming connection %d was dropped due to epoll error %d", index, net_error()); kill_TCP_secure_connection(&tcp_server->unconfirmed_connection_queue[index_new]); break; } @@ -1125,11 +1168,13 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time const int index_new = do_unconfirmed(tcp_server, mono_time, index); if (index_new != -1) { + LOGGER_DEBUG(tcp_server->logger, "unconfirmed connection %d was confirmed as %d", index, index_new); events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40); if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { // remove from confirmed connections + LOGGER_DEBUG(tcp_server->logger, "unconfirmed connection %d was dropped due to epoll error %d", index, net_error()); kill_accepted(tcp_server, index_new); break; } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index e3cf7fd30c..e51099b7a4 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2994,7 +2994,7 @@ Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, co temp->log = log; temp->mono_time = mono_time; - temp->tcp_c = new_tcp_connections(mono_time, dht_get_self_secret_key(dht), proxy_info); + temp->tcp_c = new_tcp_connections(log, mono_time, dht_get_self_secret_key(dht), proxy_info); if (temp->tcp_c == nullptr) { free(temp); diff --git a/toxcore/network.c b/toxcore/network.c index 2d4e9aae41..bae8b4f6b9 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -493,21 +493,21 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu if (res < 0) { /* Windows doesn't necessarily know `%zu` */ int error = net_error(); char *strerror = net_new_strerror(error); - LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", + LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", buffer[0], message, min_u16(buflen, 999), 'E', ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error, - strerror, data_0(buflen, buffer), data_1(buflen, buffer)); + strerror, data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); net_kill_strerror(strerror); } else if ((res > 0) && ((size_t)res <= buflen)) { - LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", + LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", buffer[0], message, min_u16(res, 999), (size_t)res < buflen ? '<' : '=', ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer)); + data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); } else { /* empty or overwrite */ - LOGGER_TRACE(log, "[%2u] %s %lu%c%u %s:%u (%u: %s) | %04x%04x", + LOGGER_TRACE(log, "[%2u] %s %lu%c%u %s:%u (%u: %s) | %08x%08x...%02x", buffer[0], message, res, !res ? '!' : '>', buflen, ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer)); + data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); } } @@ -1401,7 +1401,7 @@ bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) return true; } -int net_connect(Socket sock, IP_Port ip_port) +int net_connect(const Logger *log, Socket sock, IP_Port ip_port) { struct sockaddr_storage addr = {0}; size_t addrsize; @@ -1427,6 +1427,7 @@ int net_connect(Socket sock, IP_Port ip_port) #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION return 0; #else + LOGGER_DEBUG(log, "connecting socket %d", (int)sock.socket); return connect(sock.socket, (struct sockaddr *)&addr, addrsize); #endif } @@ -1442,8 +1443,24 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) return 1; #else + // Try parsing as IP address first. + IP_Port parsed = {0}; + + if (addr_parse_ip(node, &parsed.ip)) { + IP_Port *tmp = (IP_Port *)calloc(1, sizeof(IP_Port)); + + if (tmp == nullptr) { + return -1; + } + + tmp[0] = parsed; + *res = tmp; + return 1; + } + + // It's not an IP address, so now we try doing a DNS lookup. struct addrinfo *infos; - int ret = getaddrinfo(node, nullptr, nullptr, &infos); + const int ret = getaddrinfo(node, nullptr, nullptr, &infos); *res = nullptr; if (ret != 0) { @@ -1452,11 +1469,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) // Used to avoid calloc parameter overflow const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); - int type = make_socktype(tox_type); - struct addrinfo *cur; + const int type = make_socktype(tox_type); size_t count = 0; - for (cur = infos; count < max_count && cur != nullptr; cur = cur->ai_next) { + for (struct addrinfo *cur = infos; count < max_count && cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { continue; } @@ -1484,7 +1500,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) IP_Port *ip_port = *res; - for (cur = infos; cur != nullptr; cur = cur->ai_next) { + for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { continue; } @@ -1565,22 +1581,26 @@ Socket net_socket(Family domain, int type, int protocol) #endif } -int net_send(Socket sock, const void *buf, size_t len) +int net_send(const Logger *log, Socket sock, const uint8_t *buf, size_t len, IP_Port ip_port) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - return fuzz_send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL); + int res = fuzz_send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL); #else - return send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL); + int res = send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL); #endif + loglogdata(log, "T=>", buf, len, ip_port, res); + return res; } -int net_recv(Socket sock, void *buf, size_t len) +int net_recv(const Logger *log, Socket sock, uint8_t *buf, size_t len, IP_Port ip_port) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - return fuzz_recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL); + int res = fuzz_recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL); #else - return recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL); + int res = recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL); #endif + loglogdata(log, "=>T", buf, len, ip_port, res); + return res; } int net_listen(Socket sock, int backlog) diff --git a/toxcore/network.h b/toxcore/network.h index 36bf399666..82a94d7768 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -43,44 +43,6 @@ extern const Family net_family_tcp_ipv6; extern const Family net_family_tox_tcp_ipv4; extern const Family net_family_tox_tcp_ipv6; -typedef struct Socket { - int socket; -} Socket; - -Socket net_socket(Family domain, int type, int protocol); - -/** - * Check if socket is valid. - * - * @return true if valid, false otherwise. - */ -bool sock_valid(Socket sock); - -extern const Socket net_invalid_socket; - -/** - * Calls send(sockfd, buf, len, MSG_NOSIGNAL). - */ -int net_send(Socket sock, const void *buf, size_t len); -/** - * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). - */ -int net_recv(Socket sock, void *buf, size_t len); -/** - * Calls listen(sockfd, backlog). - */ -int net_listen(Socket sock, int backlog); -/** - * Calls accept(sockfd, nullptr, nullptr). - */ -Socket net_accept(Socket sock); - -/** - * return the size of data in the tcp recv buffer. - * return 0 on failure. - */ -uint16_t net_socket_data_recv_buffer(Socket sock); - #define MAX_UDP_PACKET_SIZE 2048 typedef enum Net_Packet_Type { @@ -184,6 +146,44 @@ typedef struct IP_Port { uint16_t port; } IP_Port; +typedef struct Socket { + int socket; +} Socket; + +Socket net_socket(Family domain, int type, int protocol); + +/** + * Check if socket is valid. + * + * @return true if valid, false otherwise. + */ +bool sock_valid(Socket sock); + +extern const Socket net_invalid_socket; + +/** + * Calls send(sockfd, buf, len, MSG_NOSIGNAL). + */ +int net_send(const Logger *log, Socket sock, const uint8_t *buf, size_t len, IP_Port ip_port); +/** + * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). + */ +int net_recv(const Logger *log, Socket sock, uint8_t *buf, size_t len, IP_Port ip_port); +/** + * Calls listen(sockfd, backlog). + */ +int net_listen(Socket sock, int backlog); +/** + * Calls accept(sockfd, nullptr, nullptr). + */ +Socket net_accept(Socket sock); + +/** + * return the size of data in the tcp recv buffer. + * return 0 on failure. + */ +uint16_t net_socket_data_recv_buffer(Socket sock); + /** Convert values between host and network byte order. */ uint32_t net_htonl(uint32_t hostlong); @@ -402,7 +402,7 @@ void networking_poll(const Networking_Core *net, void *userdata); * Return 0 on success. * Return -1 on failure. */ -int net_connect(Socket sock, IP_Port ip_port); +int net_connect(const Logger *log, Socket sock, IP_Port ip_port); /** High-level getaddrinfo implementation. * Given node, which identifies an Internet host, net_getipport() fills an array diff --git a/toxcore/tox.c b/toxcore/tox.c index 7299f3b141..be57abb8d2 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -692,6 +692,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub const int32_t count = net_getipport(host, &root, TOX_SOCK_DGRAM); if (count == -1) { + LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host); net_freeipport(root); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; @@ -718,6 +719,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub return 1; } + LOGGER_DEBUG(tox->m->log, "bootstrap node '%s' resolved to 0 IP_Ports", host); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; }