Skip to content

Commit

Permalink
merge tcp_connection changes from new groupchats fork
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Dec 14, 2021
1 parent 5c1796d commit fafce4e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 26 deletions.
83 changes: 59 additions & 24 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include <stdlib.h>
#include <string.h>

#include "TCP_client.h"
#include "mono_time.h"
#include "util.h"


struct TCP_Connections {
Mono_Time *mono_time;
DHT *dht;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions toxcore/TCP_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef C_TOXCORE_TOXCORE_TCP_CONNECTION_H
#define C_TOXCORE_TOXCORE_TCP_CONNECTION_H

#include <stdbool.h>
#include "TCP_client.h"

#define TCP_CONN_NONE 0
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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

0 comments on commit fafce4e

Please sign in to comment.