summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src/toxcore/TCP_connection.c
diff options
context:
space:
mode:
authordartraiden <wowemuh@gmail.com>2021-12-09 19:45:20 +0300
committerdartraiden <wowemuh@gmail.com>2021-12-09 19:45:20 +0300
commitb40119891c2f73c4726ab64ac2a2f327cf781ee2 (patch)
treef06adb7527462996a95160921693b7361f2f5d87 /protocols/Tox/libtox/src/toxcore/TCP_connection.c
parentf4f1858d08a927e48cf05169b53dd79f19f21794 (diff)
Update libtox to 0.2.13
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/TCP_connection.c')
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_connection.c108
1 files changed, 49 insertions, 59 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_connection.c b/protocols/Tox/libtox/src/toxcore/TCP_connection.c
index adef31751f..ac933cc3cc 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_connection.c
+++ b/protocols/Tox/libtox/src/toxcore/TCP_connection.c
@@ -125,7 +125,7 @@ static bool connections_number_is_valid(const TCP_Connections *tcp_c, int connec
*/
static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tcp_connections_number)
{
- if ((unsigned int)tcp_connections_number >= tcp_c->tcp_connections_length) {
+ if ((uint32_t)tcp_connections_number >= tcp_c->tcp_connections_length) {
return false;
}
@@ -173,9 +173,7 @@ static int create_connection(TCP_Connections *tcp_c)
*/
static int create_tcp_connection(TCP_Connections *tcp_c)
{
- uint32_t i;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
if (tcp_c->tcp_connections[i].status == TCP_CONN_NONE) {
return i;
}
@@ -231,9 +229,10 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe
return -1;
}
- uint32_t i;
memset(&tcp_c->tcp_connections[tcp_connections_number], 0, sizeof(TCP_con));
+ uint32_t i;
+
for (i = tcp_c->tcp_connections_length; i != 0; --i) {
if (tcp_c->tcp_connections[i - 1].status != TCP_CONN_NONE) {
break;
@@ -375,7 +374,7 @@ int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c)
* return 0 on success.
* return -1 on failure.
*/
-int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data,
+int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_number, const uint8_t *data,
uint16_t length)
{
if (tcp_connections_number >= tcp_c->tcp_connections_length) {
@@ -474,9 +473,7 @@ static int find_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_
*/
static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *relay_pk)
{
- unsigned int i;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
@@ -695,9 +692,9 @@ static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, unsigned int t
*/
static unsigned int online_tcp_connection_from_conn(TCP_Connection_to *con_to)
{
- unsigned int i, count = 0;
+ unsigned int count = 0;
- for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
+ for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection) {
if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) {
++count;
@@ -1089,9 +1086,9 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
return -1;
}
- unsigned int i, sent = 0;
+ unsigned int sent = 0;
- for (i = 0; i < tcp_c->connections_length; ++i) {
+ for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);
if (con_to) {
@@ -1314,9 +1311,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
}
if (status) {
- unsigned int i;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
@@ -1334,7 +1329,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) {
unsigned int wakeup = NUM_ONION_TCP_CONNECTIONS - tcp_c->onion_num_conns;
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
@@ -1351,9 +1346,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
tcp_c->onion_status = 1;
} else {
- unsigned int i;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
@@ -1398,48 +1391,48 @@ 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;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
- if (tcp_con->status != TCP_CONN_SLEEPING) {
- do_TCP_connection(tcp_c->mono_time, tcp_con->connection, userdata);
+ if (tcp_con == nullptr) {
+ continue;
+ }
- /* callbacks can change TCP connection address. */
- tcp_con = get_tcp_connection(tcp_c, i);
+ if (tcp_con->status != TCP_CONN_SLEEPING) {
+ do_TCP_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata);
- // Make sure the TCP connection wasn't dropped in any of the callbacks.
- assert(tcp_con != nullptr);
+ /* callbacks can change TCP connection address. */
+ tcp_con = get_tcp_connection(tcp_c, i);
- if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
- if (tcp_con->status == TCP_CONN_CONNECTED) {
- reconnect_tcp_relay_connection(tcp_c, i);
- } else {
- kill_tcp_relay_connection(tcp_c, i);
- }
+ // Make sure the TCP connection wasn't dropped in any of the callbacks.
+ assert(tcp_con != nullptr);
- continue;
+ if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
+ if (tcp_con->status == TCP_CONN_CONNECTED) {
+ reconnect_tcp_relay_connection(tcp_c, i);
+ } else {
+ kill_tcp_relay_connection(tcp_c, i);
}
- if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
- tcp_relay_on_online(tcp_c, i);
- }
+ continue;
+ }
- 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);
- }
+ if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
+ tcp_relay_on_online(tcp_c, i);
}
- if (tcp_con->status == TCP_CONN_SLEEPING && tcp_con->unsleep) {
- unsleep_tcp_relay_connection(tcp_c, i);
+ 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);
}
}
+
+ if (tcp_con->status == TCP_CONN_SLEEPING && tcp_con->unsleep) {
+ unsleep_tcp_relay_connection(tcp_c, i);
+ }
}
}
@@ -1449,12 +1442,11 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
return;
}
- unsigned int i;
- unsigned int num_online = 0;
- unsigned int num_kill = 0;
+ uint32_t num_online = 0;
+ uint32_t num_kill = 0;
VLA(unsigned int, to_kill, tcp_c->tcp_connections_length);
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ 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) {
@@ -1474,28 +1466,26 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
return;
}
- unsigned int n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
+ uint32_t n = num_online - RECOMMENDED_FRIEND_TCP_CONNECTIONS;
if (n < num_kill) {
num_kill = n;
}
- for (i = 0; i < num_kill; ++i) {
+ for (uint32_t i = 0; i < num_kill; ++i) {
kill_tcp_relay_connection(tcp_c, to_kill[i]);
}
}
-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);
}
void kill_tcp_connections(TCP_Connections *tcp_c)
{
- unsigned int i;
-
- for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
+ for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
kill_TCP_connection(tcp_c->tcp_connections[i].connection);
}