diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 68190794704..18655529b9b 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -12,10 +12,10 @@ #include #include +#include "TCP_client.h" #include "mono_time.h" #include "util.h" - struct TCP_Connections { Mono_Time *mono_time; DHT *dht; @@ -51,6 +51,11 @@ const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c) } +uint32_t tcp_connections_count(const TCP_Connections *tcp_c) +{ + return tcp_c->tcp_connections_length; +} + /* Set the size of the array to num. * * return -1 if realloc fails. @@ -261,6 +266,26 @@ static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connect return &tcp_c->tcp_connections[tcp_connections_number]; } +/* Returns the number of connected TCP relays */ +uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c) +{ + uint32_t count = 0; + + for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) { + const TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + + if (tcp_con == nullptr) { + continue; + } + + if (tcp_con->status == TCP_CONN_CONNECTED) { + ++count; + } + } + + return count; +} + /* Send a packet to the TCP connection. * * return -1 on failure. @@ -415,6 +440,25 @@ int tcp_send_oob_packet(TCP_Connections *tcp_c, unsigned int tcp_connections_num return -1; } +static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk); + +/* Send an oob packet via the TCP relay corresponding to relay_pk. + * + * return 0 on success. + * return -1 on failure. + */ +int tcp_send_oob_packet_using_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk, const uint8_t *public_key, + const uint8_t *packet, uint16_t length) +{ + int tcp_con_number = find_tcp_connection_relay(tcp_c, relay_pk); + + if (tcp_con_number < 0) { + return -1; + } + + return tcp_send_oob_packet(tcp_c, tcp_con_number, public_key, packet, length); +} + /* Set the callback for TCP data packets. */ void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_data_cb *tcp_data_callback, void *object) @@ -439,7 +483,6 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_ tcp_c->tcp_onion_callback_object = object; } - /* Find the TCP connection with public_key. * * return connections_number on success. @@ -704,8 +747,8 @@ static unsigned int online_tcp_connection_from_conn(TCP_Connection_to *con_to) /* return index on success. * return -1 on failure. */ -static int set_tcp_connection_status(TCP_Connection_to *con_to, unsigned int tcp_connections_number, - unsigned int status, uint8_t connection_id) +static int set_tcp_connection_status(TCP_Connections *tcp_c, TCP_Connection_to *con_to, + unsigned int tcp_connections_number, unsigned int status, uint8_t connection_id) { unsigned int i; @@ -730,7 +773,7 @@ static int set_tcp_connection_status(TCP_Connection_to *con_to, unsigned int tcp * return 0 on success. * return -1 on failure. */ -static int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number) +int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number) { TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); @@ -787,7 +830,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to) { - set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_NONE, 0); + set_tcp_connection_status(tcp_c, con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_NONE, 0); } } @@ -833,7 +876,7 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection TCP_Connection_to *con_to = get_connection(tcp_c, i); if (con_to) { - set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_NONE, 0); + set_tcp_connection_status(tcp_c, con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_NONE, 0); } } @@ -927,7 +970,8 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint return -1; } - if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) { + if (set_tcp_connection_status(tcp_c, con_to, tcp_connections_number, + TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) { return -1; } @@ -950,7 +994,8 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection } if (status == 1) { - if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) { + if (set_tcp_connection_status(tcp_c, con_to, tcp_connections_number, + TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) { return -1; } @@ -960,7 +1005,8 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection --tcp_con->sleep_count; } } else if (status == 2) { - if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, connection_id) == -1) { + if (set_tcp_connection_status(tcp_c, con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_ONLINE, + connection_id) == -1) { return -1; } @@ -1419,7 +1465,8 @@ static void do_tcp_conns(const Logger *logger, TCP_Connections *tcp_c, void *use tcp_relay_on_online(tcp_c, i); } - if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion && tcp_con->lock_count + if (tcp_con->status == TCP_CONN_CONNECTED + && !tcp_con->onion && tcp_con->lock_count && tcp_con->lock_count == tcp_con->sleep_count && mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { sleep_tcp_relay_connection(tcp_c, i); @@ -1438,19 +1485,7 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) return; } - uint32_t num_online = 0; - - for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, i); - - if (tcp_con == nullptr) { - continue; - } - - if (tcp_con->status == TCP_CONN_CONNECTED) { - ++num_online; - } - } + uint32_t num_online = tcp_connected_relays_count(tcp_c); if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { return; diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 0ae33045417..7451d920142 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -9,6 +9,7 @@ #ifndef C_TOXCORE_TOXCORE_TCP_CONNECTION_H #define C_TOXCORE_TOXCORE_TCP_CONNECTION_H +#include #include "TCP_client.h" #define TCP_CONN_NONE 0 @@ -69,6 +70,11 @@ typedef struct TCP_Connections TCP_Connections; const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c); +uint32_t tcp_connections_count(const TCP_Connections *tcp_c); + +/* Returns the number of connected TCP relays */ +uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c); + /* Send a packet to the TCP connection. * * return -1 on failure. @@ -113,6 +119,9 @@ int tcp_send_oob_packet(TCP_Connections *tcp_c, unsigned int tcp_connections_num typedef int tcp_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); +int tcp_send_oob_packet_using_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk, const uint8_t *public_key, + const uint8_t *packet, uint16_t length); + /* Set the callback for TCP data packets. */ void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_data_cb *tcp_data_callback, void *object); @@ -182,6 +191,7 @@ int add_tcp_number_relay_connection(TCP_Connections *tcp_c, int connections_numb */ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_Port ip_port, const uint8_t *relay_pk); + /* Add a TCP relay to the instance. * * return 0 on success. @@ -206,9 +216,9 @@ 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(const Logger *logger, TCP_Connections *tcp_c, void *userdata); +int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number); +void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata); void kill_tcp_connections(TCP_Connections *tcp_c); #endif -