Add logging to TCP and onion client.

This commit is contained in:
iphydf 2020-05-04 00:40:59 +01:00
parent 4efe541814
commit 02a5bdc60c
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
18 changed files with 128 additions and 105 deletions

View File

@ -48,12 +48,13 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
START_TEST(test_basic)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
// Attempt to create a new TCP_Server instance.
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS,
"Failed to bind a TCP relay server to all %d attempted ports.", NUM_PORTS);
@ -170,6 +171,7 @@ START_TEST(test_basic)
kill_sock(sock);
kill_TCP_server(tcp_s);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST
@ -272,11 +274,12 @@ static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t
START_TEST(test_some)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
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.");
@ -371,6 +374,7 @@ START_TEST(test_some)
kill_TCP_con(con2);
kill_TCP_con(con3);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST
@ -459,11 +463,12 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
START_TEST(test_client)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(tcp_s != nullptr, "Failed to create a TCP relay server.");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind the relay server to all ports.");
@ -477,7 +482,7 @@ START_TEST(test_client)
TCP_Client_Connection *conn = new_TCP_connection(mono_time, ip_port_tcp_s, self_public_key, f_public_key, f_secret_key,
nullptr);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
c_sleep(50);
// The connection status should be unconfirmed here because we have finished
@ -491,7 +496,7 @@ START_TEST(test_client)
for (uint8_t i = 0; i < LOOP_SIZE; i++) {
mono_time_update(mono_time);
do_TCP_connection(mono_time, conn, nullptr); // Run the connection loop.
do_TCP_connection(logger, mono_time, conn, nullptr); // Run the connection loop.
// The status of the connection should continue to be TCP_CLIENT_CONFIRMED after multiple subsequent do_TCP_connection() calls.
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong connection status. Expected: %d, is: %d",
@ -525,13 +530,13 @@ START_TEST(test_client)
// These integers will increment per successful callback.
oob_data_callback_good = response_callback_good = status_callback_good = data_callback_good = 0;
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(mono_time, conn2, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn2, nullptr);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(mono_time, conn2, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn2, nullptr);
c_sleep(50);
uint8_t data[5] = {1, 2, 3, 4, 5};
@ -542,8 +547,8 @@ START_TEST(test_client)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(mono_time, conn2, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn2, nullptr);
// All callback methods save data should have run during the above network prodding.
ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called");
@ -560,16 +565,16 @@ START_TEST(test_client)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(mono_time, conn2, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
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);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(mono_time, conn2, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn2, nullptr);
ck_assert_msg(status_callback_good == 1, "Status callback not called");
ck_assert_msg(status_callback_status == 1, "Wrong status callback status.");
@ -578,6 +583,7 @@ START_TEST(test_client)
kill_TCP_connection(conn);
kill_TCP_connection(conn2);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST
@ -586,6 +592,7 @@ END_TEST
START_TEST(test_client_invalid)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@ -603,7 +610,7 @@ START_TEST(test_client_invalid)
// Run the client's main loop but not the server.
mono_time_update(mono_time);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
c_sleep(50);
// After 50ms of no response...
@ -612,18 +619,19 @@ START_TEST(test_client_invalid)
// After 5s...
c_sleep(5000);
mono_time_update(mono_time);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %d, is: %d.",
TCP_CLIENT_CONNECTING, tcp_con_status(conn));
// 11s... (Should wait for 10 before giving up.)
c_sleep(6000);
mono_time_update(mono_time);
do_TCP_connection(mono_time, conn, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %d, is: %d.",
TCP_CLIENT_DISCONNECTED, tcp_con_status(conn));
kill_TCP_connection(conn);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST
@ -657,12 +665,13 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
START_TEST(test_tcp_connection)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
tcp_data_callback_called = 0;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
TCP_Proxy_Info proxy_info;
@ -696,18 +705,18 @@ START_TEST(test_tcp_connection)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
int ret = send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6);
ck_assert_msg(ret == 0, "could not send packet.");
@ -715,8 +724,8 @@ START_TEST(test_tcp_connection)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
ck_assert_msg(tcp_connection_to_online_tcp_relays(tc_1, 0) == 1, "Wrong number of connected relays");
@ -724,8 +733,8 @@ START_TEST(test_tcp_connection)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
ck_assert_msg(send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6) == -1, "could send packet.");
ck_assert_msg(kill_tcp_connection_to(tc_2, 0) == 0, "could not kill connection to\n");
@ -734,6 +743,7 @@ START_TEST(test_tcp_connection)
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST
@ -762,6 +772,7 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
START_TEST(test_tcp_connection2)
{
Mono_Time *mono_time = mono_time_new();
Logger *logger = logger_new();
tcp_oobdata_callback_called = 0;
tcp_data_callback_called = 0;
@ -769,7 +780,7 @@ START_TEST(test_tcp_connection2)
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
TCP_Proxy_Info proxy_info;
@ -797,18 +808,18 @@ START_TEST(test_tcp_connection2)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
int ret = send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6);
ck_assert_msg(ret == 0, "could not send packet.");
@ -817,15 +828,15 @@ START_TEST(test_tcp_connection2)
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
ck_assert_msg(tcp_oobdata_callback_called, "could not recv packet.");
do_TCP_server_delay(tcp_s, mono_time, 50);
do_tcp_connections(tc_1, nullptr);
do_tcp_connections(tc_2, nullptr);
do_tcp_connections(logger, tc_1, nullptr);
do_tcp_connections(logger, tc_2, nullptr);
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
@ -834,6 +845,7 @@ START_TEST(test_tcp_connection2)
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);
logger_kill(logger);
mono_time_free(mono_time);
}
END_TEST

View File

@ -390,7 +390,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
}
TCP_Proxy_Info inf = {{{{0}}}};
on->onion_c = new_onion_client(on->mono_time, new_net_crypto(on->log, on->mono_time, dht, &inf));
on->onion_c = new_onion_client(on->log, on->mono_time, new_net_crypto(on->log, on->mono_time, dht, &inf));
if (!on->onion_c) {
kill_onion_announce(on->onion_a);

View File

@ -163,7 +163,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion);
TCP_Server *tcp_s = new_TCP_server(logger, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion);
if (tcp_s == nullptr) {
printf("TCP server failed to initialize.\n");

View File

@ -1 +1 @@
79c4f7416951d024f95d08e7fc48e127181b7465935cc75b9b3e0ab001bd43b4 /usr/local/bin/tox-bootstrapd
6f719e76aedd7b386c40ef096412e0336dbc608e69112329932a2c7b60dd8652 /usr/local/bin/tox-bootstrapd

View File

@ -406,7 +406,8 @@ int main(int argc, char *argv[])
return 1;
}
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht_get_self_secret_key(dht), onion);
tcp_server = new_TCP_server(logger, enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht_get_self_secret_key(dht),
onion);
free(tcp_relay_ports);

View File

@ -1977,8 +1977,8 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig
m->onion = new_onion(m->mono_time, m->dht);
m->onion_a = new_onion_announce(m->mono_time, m->dht);
m->onion_c = new_onion_client(m->mono_time, m->net_crypto);
m->fr_c = new_friend_connections(m->mono_time, m->onion_c, options->local_discovery_enabled);
m->onion_c = new_onion_client(m->log, m->mono_time, m->net_crypto);
m->fr_c = new_friend_connections(m->log, m->mono_time, m->onion_c, options->local_discovery_enabled);
if (!(m->onion && m->onion_a && m->onion_c && m->fr_c)) {
kill_friend_connections(m->fr_c);
@ -1995,8 +1995,8 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig
}
if (options->tcp_server_port) {
m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, dht_get_self_secret_key(m->dht),
m->onion);
m->tcp_server = new_TCP_server(m->log, options->ipv6enabled, 1, &options->tcp_server_port,
dht_get_self_secret_key(m->dht), m->onion);
if (m->tcp_server == nullptr) {
kill_friend_connections(m->fr_c);

View File

@ -151,12 +151,12 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *tcp_con
* return 0 if no data received.
* return -1 on failure (connection refused).
*/
static int proxy_http_read_connection_response(TCP_Client_Connection *tcp_conn)
static int proxy_http_read_connection_response(const Logger *logger, TCP_Client_Connection *tcp_conn)
{
char success[] = "200";
uint8_t data[16]; // draining works the best if the length is a power of 2
int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data) - 1);
int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data) - 1);
if (ret == -1) {
return 0;
@ -170,7 +170,7 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *tcp_conn)
if (data_left) {
VLA(uint8_t, temp_data, data_left);
read_TCP_packet(tcp_conn->sock, temp_data, data_left);
read_TCP_packet(logger, tcp_conn->sock, temp_data, data_left);
}
return 1;
@ -193,10 +193,10 @@ static void proxy_socks5_generate_handshake(TCP_Client_Connection *tcp_conn)
* return 0 if no data received.
* return -1 on failure (connection refused).
*/
static int socks5_read_handshake_response(TCP_Client_Connection *tcp_conn)
static int socks5_read_handshake_response(const Logger *logger, TCP_Client_Connection *tcp_conn)
{
uint8_t data[2];
int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data));
int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data));
if (ret == -1) {
return 0;
@ -239,11 +239,11 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *tcp_
* return 0 if no data received.
* return -1 on failure (connection refused).
*/
static int proxy_socks5_read_connection_response(TCP_Client_Connection *tcp_conn)
static int proxy_socks5_read_connection_response(const Logger *logger, TCP_Client_Connection *tcp_conn)
{
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(tcp_conn->sock, data, sizeof(data));
int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data));
if (ret == -1) {
return 0;
@ -254,7 +254,7 @@ static int proxy_socks5_read_connection_response(TCP_Client_Connection *tcp_conn
}
} else {
uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)];
int ret = read_TCP_packet(tcp_conn->sock, data, sizeof(data));
int ret = read_TCP_packet(logger, tcp_conn->sock, data, sizeof(data));
if (ret == -1) {
return 0;
@ -900,10 +900,10 @@ static int handle_TCP_client_packet(TCP_Client_Connection *conn, const uint8_t *
return 0;
}
static bool tcp_process_packet(TCP_Client_Connection *conn, void *userdata)
static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn, void *userdata)
{
uint8_t packet[MAX_PACKET_SIZE];
const int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
const int len = read_packet_TCP_secure_connection(logger, conn->sock, &conn->next_packet_length, conn->shared_key,
conn->recv_nonce, packet, sizeof(packet));
if (len == 0) {
@ -923,7 +923,8 @@ static bool tcp_process_packet(TCP_Client_Connection *conn, void *userdata)
return true;
}
static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_time, void *userdata)
static int do_confirmed_TCP(const Logger *logger, TCP_Client_Connection *conn, const Mono_Time *mono_time,
void *userdata)
{
client_send_pending_data(conn);
tcp_send_ping_response(conn);
@ -947,7 +948,7 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_t
return 0;
}
while (tcp_process_packet(conn, userdata)) {
while (tcp_process_packet(logger, conn, userdata)) {
// Keep reading until error or out of data.
continue;
}
@ -957,7 +958,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn, const Mono_Time *mono_t
/* Run the TCP connection
*/
void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, void *userdata)
void do_TCP_connection(const Logger *logger, Mono_Time *mono_time, TCP_Client_Connection *tcp_connection,
void *userdata)
{
if (tcp_connection->status == TCP_CLIENT_DISCONNECTED) {
return;
@ -965,7 +967,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti
if (tcp_connection->status == TCP_CLIENT_PROXY_HTTP_CONNECTING) {
if (client_send_pending_data(tcp_connection) == 0) {
int ret = proxy_http_read_connection_response(tcp_connection);
int ret = proxy_http_read_connection_response(logger, tcp_connection);
if (ret == -1) {
tcp_connection->kill_at = 0;
@ -981,7 +983,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti
if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) {
if (client_send_pending_data(tcp_connection) == 0) {
int ret = socks5_read_handshake_response(tcp_connection);
int ret = socks5_read_handshake_response(logger, tcp_connection);
if (ret == -1) {
tcp_connection->kill_at = 0;
@ -997,7 +999,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti
if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) {
if (client_send_pending_data(tcp_connection) == 0) {
int ret = proxy_socks5_read_connection_response(tcp_connection);
int ret = proxy_socks5_read_connection_response(logger, tcp_connection);
if (ret == -1) {
tcp_connection->kill_at = 0;
@ -1019,7 +1021,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti
if (tcp_connection->status == TCP_CLIENT_UNCONFIRMED) {
uint8_t data[TCP_SERVER_HANDSHAKE_SIZE];
int len = read_TCP_packet(tcp_connection->sock, data, sizeof(data));
int len = read_TCP_packet(logger, tcp_connection->sock, data, sizeof(data));
if (sizeof(data) == len) {
if (handle_handshake(tcp_connection, data) == 0) {
@ -1033,7 +1035,7 @@ void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connecti
}
if (tcp_connection->status == TCP_CLIENT_CONFIRMED) {
do_confirmed_TCP(tcp_connection, mono_time, userdata);
do_confirmed_TCP(logger, tcp_connection, mono_time, userdata);
}
if (tcp_connection->kill_at <= mono_time_get(mono_time)) {

View File

@ -54,7 +54,8 @@ TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip
/* Run the TCP connection
*/
void do_TCP_connection(Mono_Time *mono_time, TCP_Client_Connection *tcp_connection, void *userdata);
void do_TCP_connection(const Logger *logger, Mono_Time *mono_time, TCP_Client_Connection *tcp_connection,
void *userdata);
/* Kill the TCP connection
*/

View File

@ -1398,7 +1398,7 @@ TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret
return temp;
}
static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *userdata)
{
unsigned int i;
@ -1407,7 +1407,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
if (tcp_con) {
if (tcp_con->status != TCP_CONN_SLEEPING) {
do_TCP_connection(tcp_c->mono_time, tcp_con->connection, userdata);
do_TCP_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata);
/* callbacks can change TCP connection address. */
tcp_con = get_tcp_connection(tcp_c, i);
@ -1485,9 +1485,9 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
}
}
void do_tcp_connections(TCP_Connections *tcp_c, void *userdata)
void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata)
{
do_tcp_conns(tcp_c, userdata);
do_tcp_conns(logger, tcp_c, userdata);
kill_nonused_tcp(tcp_c);
}

View File

@ -206,7 +206,8 @@ uint32_t tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_rela
*/
TCP_Connections *new_tcp_connections(Mono_Time *mono_time, const uint8_t *secret_key, TCP_Proxy_Info *proxy_info);
void do_tcp_connections(TCP_Connections *tcp_c, void *userdata);
void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata);
void kill_tcp_connections(TCP_Connections *tcp_c);
#endif

View File

@ -12,7 +12,6 @@
#include "TCP_server.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
@ -66,6 +65,7 @@ typedef struct TCP_Secure_Connection {
struct TCP_Server {
const Logger *logger;
Onion *onion;
#ifdef TCP_SERVER_USE_EPOLL
@ -220,7 +220,7 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_
}
if (index == -1) {
fprintf(stderr, "FAIL index is -1\n");
LOGGER_ERROR(tcp_server->logger, "FAIL index is -1");
return -1;
}
@ -275,7 +275,7 @@ static int del_accepted(TCP_Server *tcp_server, int index)
* return 0 if nothing has been read from socket.
* return -1 on failure.
*/
uint16_t read_TCP_length(Socket sock)
uint16_t read_TCP_length(const Logger *logger, Socket sock)
{
const unsigned int count = net_socket_data_recv_buffer(sock);
@ -284,7 +284,7 @@ uint16_t read_TCP_length(Socket sock)
const int len = net_recv(sock, &length, sizeof(uint16_t));
if (len != sizeof(uint16_t)) {
fprintf(stderr, "FAIL recv packet\n");
LOGGER_ERROR(logger, "FAIL recv packet");
return 0;
}
@ -305,7 +305,7 @@ uint16_t read_TCP_length(Socket sock)
* return length on success
* return -1 on failure/no data in buffer.
*/
int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length)
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length)
{
unsigned int count = net_socket_data_recv_buffer(sock);
@ -313,7 +313,7 @@ int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length)
const int len = net_recv(sock, data, length);
if (len != length) {
fprintf(stderr, "FAIL recv packet\n");
LOGGER_ERROR(logger, "FAIL recv packet");
return -1;
}
@ -327,11 +327,11 @@ int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length)
* return 0 if could not read any packet.
* return -1 on failure (connection must be killed).
*/
int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key,
uint8_t *recv_nonce, uint8_t *data, uint16_t max_len)
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)
{
if (*next_packet_length == 0) {
uint16_t len = read_TCP_length(sock);
uint16_t len = read_TCP_length(logger, sock);
if (len == (uint16_t) -1) {
return -1;
@ -349,7 +349,7 @@ int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length,
}
VLA(uint8_t, data_encrypted, *next_packet_length);
int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length);
int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length);
if (len_packet != *next_packet_length) {
return 0;
@ -617,10 +617,10 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data,
* return 0 if we didn't get it yet.
* return -1 if the connection must be killed.
*/
static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key)
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(con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE);
const int len = read_TCP_packet(logger, con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE);
if (len != -1) {
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)
return sock;
}
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
Onion *onion)
TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
const uint8_t *secret_key, Onion *onion)
{
if (num_sockets == 0 || ports == nullptr) {
return nullptr;
@ -1072,6 +1072,8 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
return nullptr;
}
temp->logger = logger;
temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket));
if (temp->socks_listening == nullptr) {
@ -1156,7 +1158,8 @@ static int do_incoming(TCP_Server *tcp_server, uint32_t i)
return -1;
}
int ret = read_connection_handshake(&tcp_server->incoming_connection_queue[i], tcp_server->secret_key);
int ret = read_connection_handshake(tcp_server->logger, &tcp_server->incoming_connection_queue[i],
tcp_server->secret_key);
if (ret == -1) {
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
}
uint8_t packet[MAX_PACKET_SIZE];
int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce,
packet, sizeof(packet));
int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->sock, &conn->next_packet_length, conn->shared_key,
conn->recv_nonce, packet, sizeof(packet));
if (len == 0) {
return -1;
@ -1207,7 +1210,7 @@ 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(conn->sock, &conn->next_packet_length, conn->shared_key,
int len = read_packet_TCP_secure_connection(tcp_server->logger, conn->sock, &conn->next_packet_length, conn->shared_key,
conn->recv_nonce, packet, sizeof(packet));
if (len == 0) {

View File

@ -69,8 +69,8 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server);
/* Create new TCP server instance.
*/
TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key,
Onion *onion);
TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
const uint8_t *secret_key, Onion *onion);
/* Run the TCP_server
*/
@ -87,21 +87,21 @@ void kill_TCP_server(TCP_Server *tcp_server);
* return 0 if nothing has been read from socket.
* return -1 on failure.
*/
uint16_t read_TCP_length(Socket sock);
uint16_t read_TCP_length(const Logger *logger, Socket sock);
/* Read length bytes from socket.
*
* return length on success
* return -1 on failure/no data in buffer.
*/
int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length);
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length);
/* 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(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key,
uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
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);
#endif

View File

@ -12,7 +12,6 @@
#include "friend_connection.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -60,6 +59,7 @@ typedef struct Friend_Conn {
struct Friend_Connections {
const Mono_Time *mono_time;
const Logger *logger;
Net_Crypto *net_crypto;
DHT *dht;
Onion_Client *onion_c;
@ -348,7 +348,7 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
if (friend_con->dht_lock) {
if (dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock) != 0) {
printf("a. Could not delete dht peer. Please report this.\n");
LOGGER_ERROR(fr_c->logger, "a. Could not delete dht peer. Please report this.");
return;
}
@ -848,7 +848,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
}
/* Create new friend_connections instance. */
Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Client *onion_c,
Friend_Connections *new_friend_connections(const Logger *logger, const Mono_Time *mono_time, Onion_Client *onion_c,
bool local_discovery_enabled)
{
if (onion_c == nullptr) {
@ -862,6 +862,7 @@ Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Cli
}
temp->mono_time = mono_time;
temp->logger = logger;
temp->dht = onion_get_dht(onion_c);
temp->net_crypto = onion_get_net_crypto(onion_c);
temp->onion_c = onion_c;

View File

@ -148,7 +148,7 @@ typedef int fr_request_cb(void *object, const uint8_t *source_pubkey, const uint
void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_request_callback, void *object);
/* Create new friend_connections instance. */
Friend_Connections *new_friend_connections(const Mono_Time *mono_time, Onion_Client *onion_c,
Friend_Connections *new_friend_connections(const Logger *logger, const Mono_Time *mono_time, Onion_Client *onion_c,
bool local_discovery_enabled);
/* main friend_connections loop. */

View File

@ -2281,7 +2281,7 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u
static void do_tcp(Net_Crypto *c, void *userdata)
{
pthread_mutex_lock(&c->tcp_mutex);
do_tcp_connections(c->tcp_c, userdata);
do_tcp_connections(c->log, c->tcp_c, userdata);
pthread_mutex_unlock(&c->tcp_mutex);
uint32_t i;

View File

@ -97,6 +97,7 @@ typedef struct Onion_Data_Handler {
struct Onion_Client {
Mono_Time *mono_time;
const Logger *logger;
DHT *dht;
Net_Crypto *c;
@ -1849,7 +1850,7 @@ void do_onion_client(Onion_Client *onion_c)
onion_c->last_run = mono_time_get(onion_c->mono_time);
}
Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c)
Onion_Client *new_onion_client(const Logger *logger, Mono_Time *mono_time, Net_Crypto *c)
{
if (c == nullptr) {
return nullptr;
@ -1869,6 +1870,7 @@ Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c)
}
onion_c->mono_time = mono_time;
onion_c->logger = logger;
onion_c->dht = nc_get_dht(c);
onion_c->net = dht_get_net(onion_c->dht);
onion_c->c = c;

View File

@ -171,7 +171,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
void do_onion_client(Onion_Client *onion_c);
Onion_Client *new_onion_client(Mono_Time *mono_time, Net_Crypto *c);
Onion_Client *new_onion_client(const Logger *logger, Mono_Time *mono_time, Net_Crypto *c);
void kill_onion_client(Onion_Client *onion_c);

View File

@ -17,12 +17,12 @@
#include "util.h"
#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */
/* id functions */
bool id_equal(const uint8_t *dest, const uint8_t *src)