summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src
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
parentf4f1858d08a927e48cf05169b53dd79f19f21794 (diff)
Update libtox to 0.2.13
Diffstat (limited to 'protocols/Tox/libtox/src')
-rw-r--r--protocols/Tox/libtox/src/toxcore/DHT.c126
-rw-r--r--protocols/Tox/libtox/src/toxcore/DHT.h16
-rw-r--r--protocols/Tox/libtox/src/toxcore/LAN_discovery.c8
-rw-r--r--protocols/Tox/libtox/src/toxcore/Messenger.c166
-rw-r--r--protocols/Tox/libtox/src/toxcore/Messenger.h6
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_client.c38
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_client.h3
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_connection.c108
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_connection.h7
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_server.c39
-rw-r--r--protocols/Tox/libtox/src/toxcore/TCP_server.h12
-rw-r--r--protocols/Tox/libtox/src/toxcore/ccompat.h3
-rw-r--r--protocols/Tox/libtox/src/toxcore/crypto_core.c55
-rw-r--r--protocols/Tox/libtox/src/toxcore/friend_connection.c9
-rw-r--r--protocols/Tox/libtox/src/toxcore/friend_connection.h4
-rw-r--r--protocols/Tox/libtox/src/toxcore/group.c37
-rw-r--r--protocols/Tox/libtox/src/toxcore/net_crypto.c50
-rw-r--r--protocols/Tox/libtox/src/toxcore/net_crypto.h4
-rw-r--r--protocols/Tox/libtox/src/toxcore/network.c8
-rw-r--r--protocols/Tox/libtox/src/toxcore/onion_announce.c3
-rw-r--r--protocols/Tox/libtox/src/toxcore/onion_client.c47
-rw-r--r--protocols/Tox/libtox/src/toxcore/onion_client.h4
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox.api.h2
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox.c3
-rw-r--r--protocols/Tox/libtox/src/toxcore/tox.h2
-rw-r--r--protocols/Tox/libtox/src/toxcore/util.c4
-rw-r--r--protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.api.h4
-rw-r--r--protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c54
-rw-r--r--protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.h4
29 files changed, 422 insertions, 404 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.c b/protocols/Tox/libtox/src/toxcore/DHT.c
index b3017259b2..267d46881e 100644
--- a/protocols/Tox/libtox/src/toxcore/DHT.c
+++ b/protocols/Tox/libtox/src/toxcore/DHT.c
@@ -173,9 +173,9 @@ const uint8_t *dht_get_friend_public_key(const DHT *dht, uint32_t friend_num)
return dht->friends_list[friend_num].public_key;
}
-static bool assoc_timeout(const Mono_Time *mono_time, const IPPTsPng *assoc)
+static bool assoc_timeout(uint64_t cur_time, const IPPTsPng *assoc)
{
- return mono_time_is_timeout(mono_time, assoc->timestamp, BAD_NODE_TIMEOUT);
+ return (assoc->timestamp + BAD_NODE_TIMEOUT) <= cur_time;
}
/* Compares pk1 and pk2 with pk.
@@ -295,7 +295,7 @@ void dht_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi
get_shared_key(dht->mono_time, &dht->shared_keys_sent, shared_key, dht->self_secret_key, public_key);
}
-#define CRYPTO_SIZE 1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE
+#define CRYPTO_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE * 2 + CRYPTO_NONCE_SIZE)
/* Create a request to peer.
* send_public_key and send_secret_key are the pub/secret keys of the sender.
@@ -583,7 +583,8 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_
int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data,
uint16_t length, bool tcp_enabled)
{
- uint32_t num = 0, len_processed = 0;
+ uint32_t num = 0;
+ uint32_t len_processed = 0;
while (num < max_num_nodes && len_processed < length) {
const int ipp_size = unpack_ip_port(&nodes[num].ip_port, data + len_processed, length - len_processed, tcp_enabled);
@@ -787,7 +788,7 @@ static uint8_t hardening_correct(const Hardening *h)
/*
* helper for get_close_nodes(). argument list is a monster :D
*/
-static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *public_key, Node_format *nodes_list,
+static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
uint32_t *num_nodes_ptr, bool is_LAN, uint8_t want_good)
{
@@ -818,7 +819,7 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub
}
/* node not in a good condition? */
- if (assoc_timeout(mono_time, ipptp)) {
+ if (assoc_timeout(cur_time, ipptp)) {
continue;
}
@@ -856,7 +857,7 @@ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, N
Family sa_family, bool is_LAN, uint8_t want_good)
{
uint32_t num_nodes = 0;
- get_close_nodes_inner(dht->mono_time, public_key, nodes_list, sa_family,
+ get_close_nodes_inner(dht->last_run, public_key, nodes_list, sa_family,
dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN, 0);
/* TODO(irungentoo): uncomment this when hardening is added to close friend clients */
@@ -871,7 +872,7 @@ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, N
#endif
for (uint32_t i = 0; i < dht->num_friends; ++i) {
- get_close_nodes_inner(dht->mono_time, public_key, nodes_list, sa_family,
+ get_close_nodes_inner(dht->last_run, public_key, nodes_list, sa_family,
dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
&num_nodes, is_LAN, 0);
}
@@ -887,7 +888,7 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node
}
typedef struct DHT_Cmp_data {
- const Mono_Time *mono_time;
+ uint64_t cur_time;
const uint8_t *base_public_key;
Client_data entry;
} DHT_Cmp_data;
@@ -899,15 +900,16 @@ static bool incorrect_hardening(const IPPTsPng *assoc)
static int cmp_dht_entry(const void *a, const void *b)
{
- DHT_Cmp_data cmp1, cmp2;
+ DHT_Cmp_data cmp1;
+ DHT_Cmp_data cmp2;
memcpy(&cmp1, a, sizeof(DHT_Cmp_data));
memcpy(&cmp2, b, sizeof(DHT_Cmp_data));
const Client_data entry1 = cmp1.entry;
const Client_data entry2 = cmp2.entry;
const uint8_t *cmp_public_key = cmp1.base_public_key;
- bool t1 = assoc_timeout(cmp1.mono_time, &entry1.assoc4) && assoc_timeout(cmp1.mono_time, &entry1.assoc6);
- bool t2 = assoc_timeout(cmp2.mono_time, &entry2.assoc4) && assoc_timeout(cmp2.mono_time, &entry2.assoc6);
+ bool t1 = assoc_timeout(cmp1.cur_time, &entry1.assoc4) && assoc_timeout(cmp1.cur_time, &entry1.assoc6);
+ bool t2 = assoc_timeout(cmp2.cur_time, &entry2.assoc4) && assoc_timeout(cmp2.cur_time, &entry2.assoc6);
if (t1 && t2) {
return 0;
@@ -950,15 +952,15 @@ static int cmp_dht_entry(const void *a, const void *b)
* return 0 if node can't be stored.
* return 1 if it can.
*/
-static unsigned int store_node_ok(const Client_data *client, const Mono_Time *mono_time, const uint8_t *public_key,
+static unsigned int store_node_ok(const Client_data *client, uint64_t cur_time, const uint8_t *public_key,
const uint8_t *comp_public_key)
{
- return (assoc_timeout(mono_time, &client->assoc4)
- && assoc_timeout(mono_time, &client->assoc6))
+ return (assoc_timeout(cur_time, &client->assoc4)
+ && assoc_timeout(cur_time, &client->assoc6))
|| id_closest(comp_public_key, client->public_key, public_key) == 2;
}
-static void sort_client_list(Client_data *list, const Mono_Time *mono_time, unsigned int length,
+static void sort_client_list(Client_data *list, uint64_t cur_time, unsigned int length,
const uint8_t *comp_public_key)
{
// Pass comp_public_key to qsort with each Client_data entry, so the
@@ -966,7 +968,7 @@ static void sort_client_list(Client_data *list, const Mono_Time *mono_time, unsi
VLA(DHT_Cmp_data, cmp_list, length);
for (uint32_t i = 0; i < length; ++i) {
- cmp_list[i].mono_time = mono_time;
+ cmp_list[i].cur_time = cur_time;
cmp_list[i].base_public_key = comp_public_key;
cmp_list[i].entry = list[i];
}
@@ -1015,7 +1017,7 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl
* than public_key.
*
* returns true when the item was stored, false otherwise */
-static bool replace_all(const Mono_Time *mono_time,
+static bool replace_all(const DHT *dht,
Client_data *list,
uint16_t length,
const uint8_t *public_key,
@@ -1026,17 +1028,17 @@ static bool replace_all(const Mono_Time *mono_time,
return false;
}
- if (!store_node_ok(&list[1], mono_time, public_key, comp_public_key) &&
- !store_node_ok(&list[0], mono_time, public_key, comp_public_key)) {
+ if (!store_node_ok(&list[1], dht->last_run, public_key, comp_public_key) &&
+ !store_node_ok(&list[0], dht->last_run, public_key, comp_public_key)) {
return false;
}
- sort_client_list(list, mono_time, length, comp_public_key);
+ sort_client_list(list, dht->last_run, length, comp_public_key);
Client_data *const client = &list[0];
id_copy(client->public_key, public_key);
- update_client_with_reset(mono_time, client, &ip_port);
+ update_client_with_reset(dht->mono_time, client, &ip_port);
return true;
}
@@ -1060,8 +1062,8 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo
* index is left as >= LCLIENT_LENGTH */
Client_data *const client = &dht->close_clientlist[(index * LCLIENT_NODES) + i];
- if (!assoc_timeout(dht->mono_time, &client->assoc4) ||
- !assoc_timeout(dht->mono_time, &client->assoc6)) {
+ if (!assoc_timeout(dht->last_run, &client->assoc4) ||
+ !assoc_timeout(dht->last_run, &client->assoc6)) {
continue;
}
@@ -1084,7 +1086,7 @@ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_
return add_to_close(dht, public_key, ip_port, 1) == 0;
}
-static bool is_pk_in_client_list(const Client_data *list, unsigned int client_list_length, const Mono_Time *mono_time,
+static bool is_pk_in_client_list(const Client_data *list, unsigned int client_list_length, uint64_t cur_time,
const uint8_t *public_key, IP_Port ip_port)
{
const uint32_t index = index_of_client_pk(list, client_list_length, public_key);
@@ -1097,7 +1099,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li
? &list[index].assoc4
: &list[index].assoc6;
- return !assoc_timeout(mono_time, assoc);
+ return !assoc_timeout(cur_time, assoc);
}
static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port)
@@ -1108,7 +1110,7 @@ static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_
index = LCLIENT_LENGTH - 1;
}
- return is_pk_in_client_list(dht->close_clientlist + index * LCLIENT_NODES, LCLIENT_NODES, dht->mono_time, public_key,
+ return is_pk_in_client_list(dht->close_clientlist + index * LCLIENT_NODES, LCLIENT_NODES, dht->last_run, public_key,
ip_port);
}
@@ -1148,17 +1150,17 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_P
bool store_ok = false;
- if (store_node_ok(&dht_friend->client_list[1], dht->mono_time, public_key, dht_friend->public_key)) {
+ if (store_node_ok(&dht_friend->client_list[1], dht->last_run, public_key, dht_friend->public_key)) {
store_ok = true;
}
- if (store_node_ok(&dht_friend->client_list[0], dht->mono_time, public_key, dht_friend->public_key)) {
+ if (store_node_ok(&dht_friend->client_list[0], dht->last_run, public_key, dht_friend->public_key)) {
store_ok = true;
}
unsigned int *const friend_num = &dht_friend->num_to_bootstrap;
const uint32_t index = index_of_node_pk(dht_friend->to_bootstrap, *friend_num, public_key);
- const bool pk_in_list = is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, dht->mono_time, public_key,
+ const bool pk_in_list = is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, dht->last_run, public_key,
ip_port);
if (store_ok && index == UINT32_MAX && !pk_in_list) {
@@ -1212,7 +1214,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
/* replace_all should be called only if !in_list (don't extract to variable) */
if (in_list
- || replace_all(dht->mono_time, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key, ip_port,
+ || replace_all(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key, ip_port,
dht->friends_list[i].public_key)) {
DHT_Friend *dht_friend = &dht->friends_list[i];
@@ -1292,7 +1294,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke
}
/* Send a getnodes request.
- sendback_node is the node that it will send back the response to (set to NULL to disable this) */
+ * sendback_node is the node that it will send back the response to (set to NULL to disable this) */
static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *client_id,
const Node_format *sendback_node)
{
@@ -1425,7 +1427,7 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet,
}
/* return false if no
- return true if yes */
+ * return true if yes */
static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port node_ip_port, uint64_t ping_id,
Node_format *sendback_node)
{
@@ -1676,7 +1678,7 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
for (const IPPTsPng * const *it = assocs; *it; ++it) {
const IPPTsPng *const assoc = *it;
- if (!assoc_timeout(dht->mono_time, assoc)) {
+ if (!assoc_timeout(dht->last_run, assoc)) {
*ip_port = assoc->ip_port;
return 1;
}
@@ -1717,7 +1719,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
}
/* If node is good. */
- if (!assoc_timeout(dht->mono_time, assoc)) {
+ if (!assoc_timeout(dht->last_run, assoc)) {
client_list[num_nodes] = client;
assoc_list[num_nodes] = assoc;
++num_nodes;
@@ -1734,7 +1736,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
}
if (sortable && sort_ok) {
- sort_client_list(list, dht->mono_time, list_count, public_key);
+ sort_client_list(list, dht->last_run, list_count, public_key);
}
if ((num_nodes != 0) && (mono_time_is_timeout(dht->mono_time, *lastgetnode, GET_NODE_INTERVAL)
@@ -1918,8 +1920,8 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n
}
if (id_equal(client->public_key, dht_friend->public_key)) {
- if (!assoc_timeout(dht->mono_time, &client->assoc6)
- || !assoc_timeout(dht->mono_time, &client->assoc4)) {
+ if (!assoc_timeout(dht->last_run, &client->assoc6)
+ || !assoc_timeout(dht->last_run, &client->assoc4)) {
return 0; /* direct connectivity */
}
}
@@ -2389,7 +2391,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num)
const IPPTsPng *const temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family);
if (temp) {
- if (!assoc_timeout(dht->mono_time, temp)) {
+ if (!assoc_timeout(dht->last_run, temp)) {
++counter;
}
}
@@ -2417,7 +2419,8 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
return 1;
}
- Node_format node, tocheck_node;
+ Node_format node;
+ Node_format tocheck_node;
node.ip_port = source;
memcpy(node.public_key, source_pubkey, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(&tocheck_node, packet + 1, sizeof(Node_format));
@@ -2444,7 +2447,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
length_nodes, 0);
/* TODO(irungentoo): MAX_SENT_NODES nodes should be returned at all times
- (right now we have a small network size so it could cause problems for testing and etc..) */
+ * (right now we have a small network size so it could cause problems for testing and etc..) */
if (num_nodes <= 0) {
return 1;
}
@@ -2508,7 +2511,7 @@ static Node_format random_node(DHT *dht, Family sa_family)
*
* return the number of nodes.
*/
-static uint16_t list_nodes(Client_data *list, size_t length, const Mono_Time *mono_time, Node_format *nodes,
+static uint16_t list_nodes(Client_data *list, size_t length, uint64_t cur_time, Node_format *nodes,
uint16_t max_num)
{
if (max_num == 0) {
@@ -2520,11 +2523,11 @@ static uint16_t list_nodes(Client_data *list, size_t length, const Mono_Time *mo
for (size_t i = length; i != 0; --i) {
const IPPTsPng *assoc = nullptr;
- if (!assoc_timeout(mono_time, &list[i - 1].assoc4)) {
+ if (!assoc_timeout(cur_time, &list[i - 1].assoc4)) {
assoc = &list[i - 1].assoc4;
}
- if (!assoc_timeout(mono_time, &list[i - 1].assoc6)) {
+ if (!assoc_timeout(cur_time, &list[i - 1].assoc6)) {
if (assoc == nullptr) {
assoc = &list[i - 1].assoc6;
} else if (random_u08() % 2) {
@@ -2560,7 +2563,7 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
const uint32_t r = random_u32();
for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
- count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, dht->mono_time,
+ count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, dht->last_run,
nodes + count, max_num - count);
if (count >= max_num) {
@@ -2577,7 +2580,7 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
*/
uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
{
- return list_nodes(dht->close_clientlist, LCLIENT_LIST, dht->mono_time, nodes, max_num);
+ return list_nodes(dht->close_clientlist, LCLIENT_LIST, dht->last_run, nodes, max_num);
}
#if DHT_HARDENING
@@ -2698,6 +2701,7 @@ DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool
}
dht->mono_time = mono_time;
+ dht->last_run = mono_time_get(mono_time);
dht->log = log;
dht->net = net;
@@ -2743,10 +2747,14 @@ DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool
void do_dht(DHT *dht)
{
- if (dht->last_run == mono_time_get(dht->mono_time)) {
+ const uint64_t cur_time = mono_time_get(dht->mono_time);
+
+ if (dht->last_run == cur_time) {
return;
}
+ dht->last_run = cur_time;
+
// Load friends/clients if first call to do_dht
if (dht->loaded_num_nodes) {
dht_connect_after_load(dht);
@@ -2759,7 +2767,6 @@ void do_dht(DHT *dht)
#if DHT_HARDENING
do_hardening(dht);
#endif
- dht->last_run = mono_time_get(dht->mono_time);
}
void kill_dht(DHT *dht)
@@ -2827,7 +2834,12 @@ void dht_save(const DHT *dht, uint8_t *data)
/* get right offset. we write the actual header later. */
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);
- Node_format clients[MAX_SAVED_DHT_NODES];
+ Node_format *clients = (Node_format *)malloc(MAX_SAVED_DHT_NODES * sizeof(Node_format));
+
+ if (clients == nullptr) {
+ LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
+ return;
+ }
uint32_t num = 0;
@@ -2870,6 +2882,8 @@ void dht_save(const DHT *dht, uint8_t *data)
state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(data, sizeof(Node_format) * num, clients, num),
DHT_STATE_TYPE_NODES);
+
+ free(clients);
}
/* Bootstrap from this number of nodes every time dht_connect_after_load() is called */
@@ -2917,6 +2931,12 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat
// Copy to loaded_clients_list
dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format));
+ if (dht->loaded_nodes_list == nullptr) {
+ LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
+ dht->loaded_num_nodes = 0;
+ break;
+ }
+
const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, 0);
if (num > 0) {
@@ -2967,8 +2987,8 @@ bool dht_isconnected(const DHT *dht)
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *const client = &dht->close_clientlist[i];
- if (!assoc_timeout(dht->mono_time, &client->assoc4) ||
- !assoc_timeout(dht->mono_time, &client->assoc6)) {
+ if (!assoc_timeout(dht->last_run, &client->assoc4) ||
+ !assoc_timeout(dht->last_run, &client->assoc6)) {
return true;
}
}
@@ -2984,12 +3004,12 @@ bool dht_non_lan_connected(const DHT *dht)
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *const client = &dht->close_clientlist[i];
- if (!assoc_timeout(dht->mono_time, &client->assoc4)
+ if (!assoc_timeout(dht->last_run, &client->assoc4)
&& !ip_is_lan(client->assoc4.ip_port.ip)) {
return true;
}
- if (!assoc_timeout(dht->mono_time, &client->assoc6)
+ if (!assoc_timeout(dht->last_run, &client->assoc6)
&& !ip_is_lan(client->assoc6.ip_port.ip)) {
return true;
}
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.h b/protocols/Tox/libtox/src/toxcore/DHT.h
index 13f7ce20ed..fcc67251f6 100644
--- a/protocols/Tox/libtox/src/toxcore/DHT.h
+++ b/protocols/Tox/libtox/src/toxcore/DHT.h
@@ -17,10 +17,14 @@
#include <stdbool.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/* Maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8
-#define LCLIENT_NODES (MAX_FRIEND_CLIENTS)
+#define LCLIENT_NODES MAX_FRIEND_CLIENTS
#define LCLIENT_LENGTH 128
/* A list of the clients mathematically closest to ours. */
@@ -69,9 +73,9 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke
const uint8_t *recv_public_key, const uint8_t *data, uint32_t length, uint8_t request_id);
/* puts the senders public key in the request in public_key, the data from the request
- in data if a friend or ping request was sent to us and returns the length of the data.
- packet is the request packet and length is its length
- return -1 if not valid request. */
+ * in data if a friend or ping request was sent to us and returns the length of the data.
+ * packet is the request packet and length is its length
+ * return -1 if not valid request. */
int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
uint8_t *request_id, const uint8_t *packet, uint16_t length);
@@ -405,4 +409,8 @@ bool dht_non_lan_connected(const DHT *dht);
uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key);
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
#endif
diff --git a/protocols/Tox/libtox/src/toxcore/LAN_discovery.c b/protocols/Tox/libtox/src/toxcore/LAN_discovery.c
index c3d060145f..3f6951fcc7 100644
--- a/protocols/Tox/libtox/src/toxcore/LAN_discovery.c
+++ b/protocols/Tox/libtox/src/toxcore/LAN_discovery.c
@@ -70,14 +70,16 @@ static void fetch_broadcast_info(uint16_t port)
IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
while (pAdapter) {
- IP gateway = {0}, subnet_mask = {0};
+ IP gateway = {0};
+ IP subnet_mask = {0};
if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) {
IP_Port *ip_port = &ip_ports[count];
ip_port->ip.family = net_family_ipv4;
- uint32_t gateway_ip = net_ntohl(gateway.ip.v4.uint32), subnet_ip = net_ntohl(subnet_mask.ip.v4.uint32);
+ uint32_t gateway_ip = net_ntohl(gateway.ip.v4.uint32);
+ uint32_t subnet_ip = net_ntohl(subnet_mask.ip.v4.uint32);
uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1;
ip_port->ip.ip.v4.uint32 = net_htonl(broadcast_ip);
ip_port->port = port;
@@ -329,7 +331,7 @@ bool ip_is_lan(IP ip)
if (net_family_is_ipv6(ip.family)) {
/* autogenerated for each interface: `FE80::*` (up to `FEBF::*`)
- `FF02::1` is - according to RFC 4291 - multicast all-nodes link-local */
+ * `FF02::1` is - according to RFC 4291 - multicast all-nodes link-local */
if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) ||
((ip.ip.v6.uint8[0] == 0xFE) && ((ip.ip.v6.uint8[1] & 0xC0) == 0x80))) {
return true;
diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.c b/protocols/Tox/libtox/src/toxcore/Messenger.c
index 556acc272f..22c3e9e856 100644
--- a/protocols/Tox/libtox/src/toxcore/Messenger.c
+++ b/protocols/Tox/libtox/src/toxcore/Messenger.c
@@ -239,7 +239,8 @@ int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, u
return FAERR_BADCHECKSUM;
}
- uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
+ uint16_t check;
+ uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
memcpy(&check, address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), sizeof(check));
if (check != checksum) {
@@ -443,26 +444,31 @@ int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
return -1;
}
- if (m->friendlist[friendnumber].status == FRIEND_ONLINE) {
- bool direct_connected = 0;
- unsigned int num_online_relays = 0;
- int crypt_conn_id = friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id);
+ if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
+ return CONNECTION_NONE;
+ }
- // FIXME(sudden6): handle return value
- crypto_connection_status(m->net_crypto, crypt_conn_id, &direct_connected, &num_online_relays);
+ bool direct_connected = 0;
+ unsigned int num_online_relays = 0;
+ int crypt_conn_id = friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id);
- if (direct_connected) {
- return CONNECTION_UDP;
- }
+ if (!crypto_connection_status(m->net_crypto, crypt_conn_id, &direct_connected, &num_online_relays)) {
+ return CONNECTION_NONE;
+ }
- if (num_online_relays) {
- return CONNECTION_TCP;
- }
+ if (direct_connected) {
+ return CONNECTION_UDP;
+ }
- return CONNECTION_UNKNOWN;
+ if (num_online_relays) {
+ return CONNECTION_TCP;
}
- return CONNECTION_NONE;
+ /* if we have a valid friend connection but do not have an established connection
+ * we leave the connection status unchanged until the friend connection is either
+ * established or dropped.
+ */
+ return m->friendlist[friendnumber].last_connection_udp_tcp;
}
int m_friend_exists(const Messenger *m, int32_t friendnumber)
@@ -898,21 +904,13 @@ static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber, void *userd
return;
}
- if (ret == CONNECTION_UNKNOWN) {
- if (last_connection_udp_tcp == CONNECTION_UDP) {
- return;
- }
-
- ret = CONNECTION_TCP;
- }
-
if (last_connection_udp_tcp != ret) {
if (m->friend_connectionstatuschange) {
m->friend_connectionstatuschange(m, friendnumber, ret, userdata);
}
}
- m->friendlist[friendnumber].last_connection_udp_tcp = ret;
+ m->friendlist[friendnumber].last_connection_udp_tcp = (Connection_Status)ret;
}
static void break_files(const Messenger *m, int32_t friendnumber);
@@ -1046,7 +1044,8 @@ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, u
}
uint32_t temp_filenum;
- uint8_t send_receive, file_number;
+ uint8_t send_receive;
+ uint8_t file_number;
if (filenumber >= (1 << 16)) {
send_receive = 1;
@@ -1154,8 +1153,6 @@ long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_
ft->requested = 0;
- ft->slots_allocated = 0;
-
ft->paused = FILE_PAUSE_NOT;
memcpy(ft->id, file_id, FILE_ID_LENGTH);
@@ -1208,7 +1205,8 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber,
}
uint32_t temp_filenum;
- uint8_t send_receive, file_number;
+ uint8_t send_receive;
+ uint8_t file_number;
if (filenumber >= (1 << 16)) {
send_receive = 1;
@@ -1435,10 +1433,6 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
// TODO(irungentoo): record packet ids to check if other received complete file.
ft->transferred += length;
- if (ft->slots_allocated) {
- --ft->slots_allocated;
- }
-
if (length != MAX_FILE_DATA_SIZE || ft->size == ft->transferred) {
ft->status = FILESTATUS_FINISHED;
ft->last_packet_number = ret;
@@ -1461,52 +1455,46 @@ int file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
* @param userdata The client userdata to pass along to chunk request callbacks.
* @param free_slots A pointer to the number of free send queue slots in the
* crypto connection.
+ * @return true if there's still work to do, false otherwise.
*
- * @return true if there are still file transfers ongoing, false if all file
- * transfers are complete.
*/
static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userdata, uint32_t *free_slots)
{
Friend *const friendcon = &m->friendlist[friendnumber];
- uint32_t num = friendcon->num_sending_files;
-
- bool any_active_fts = false;
- // Iterate over all file transfers, including inactive ones. I.e. we always
- // iterate exactly MAX_CONCURRENT_FILE_PIPES times.
+ // Iterate over file transfers as long as we're sending files
for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
- struct File_Transfers *const ft = &friendcon->file_sending[i];
+ if (friendcon->num_sending_files == 0) {
+ // no active file transfers anymore
+ return false;
+ }
- // Any status other than NONE means the file transfer is active.
- if (ft->status != FILESTATUS_NONE) {
- any_active_fts = true;
- --num;
+ if (*free_slots == 0) {
+ // send buffer full enough
+ return false;
+ }
- // If the file transfer is complete, we request a chunk of size 0.
- if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
- if (m->file_reqchunk) {
- m->file_reqchunk(m, friendnumber, i, ft->transferred, 0, userdata);
- }
+ if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
+ m->fr_c, friendcon->friendcon_id))) {
+ LOGGER_TRACE(m->log, "Maximum connection speed reached");
+ // connection doesn't support any more data
+ return false;
+ }
- // Now it's inactive, we're no longer sending this.
- ft->status = FILESTATUS_NONE;
- --friendcon->num_sending_files;
+ struct File_Transfers *const ft = &friendcon->file_sending[i];
+
+ // If the file transfer is complete, we request a chunk of size 0.
+ if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
+ if (m->file_reqchunk) {
+ m->file_reqchunk(m, friendnumber, i, ft->transferred, 0, userdata);
}
- // Decrease free slots by the number of slots this FT uses.
- *free_slots = max_s32(0, (int32_t) * free_slots - ft->slots_allocated);
+ // Now it's inactive, we're no longer sending this.
+ ft->status = FILESTATUS_NONE;
+ --friendcon->num_sending_files;
}
if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) {
- if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
- m->fr_c, friendcon->friendcon_id))) {
- *free_slots = 0;
- }
-
- if (*free_slots == 0) {
- continue;
- }
-
if (ft->size == 0) {
/* Send 0 data to friend if file is 0 length. */
file_data(m, friendnumber, i, 0, nullptr, 0);
@@ -1518,9 +1506,6 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
continue;
}
- // Allocate 1 slot to this file transfer.
- ++ft->slots_allocated;
-
const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE);
const uint64_t position = ft->requested;
ft->requested += length;
@@ -1532,13 +1517,9 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
// The allocated slot is no longer free.
--*free_slots;
}
-
- if (num == 0) {
- continue;
- }
}
- return any_active_fts;
+ return true;
}
static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata)
@@ -1560,19 +1541,22 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
// transfers might block other traffic for a long time.
free_slots = max_s32(0, (int32_t)free_slots - MIN_SLOTS_FREE);
- bool any_active_fts = true;
- uint32_t loop_counter = 0;
// Maximum number of outer loops below. If the client doesn't send file
// chunks from within the chunk request callback handler, we never realise
// that the file transfer has finished and may end up in an infinite loop.
//
- // TODO(zoff99): Fix this to exit the loop properly when we're done
- // requesting all chunks for all file transfers.
+ // Request up to that number of chunks per file from the client
const uint32_t max_ft_loops = 16;
- while (((free_slots > 0) || loop_counter == 0) && any_active_fts && (loop_counter < max_ft_loops)) {
- any_active_fts = do_all_filetransfers(m, friendnumber, userdata, &free_slots);
- ++loop_counter;
+ for (uint32_t i = 0; i < max_ft_loops; ++i) {
+ if (!do_all_filetransfers(m, friendnumber, userdata, &free_slots)) {
+ break;
+ }
+
+ if (free_slots == 0) {
+ // stop when the buffer is full enough
+ break;
+ }
}
}
@@ -1977,8 +1961,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 +1979,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);
@@ -2507,7 +2491,7 @@ static char *id_to_string(const uint8_t *pk, char *id_str, size_t length)
}
/* Minimum messenger run interval in ms
- TODO(mannol): A/V */
+ * TODO(mannol): A/V */
#define MIN_RUN_INTERVAL 50
/* Return the time in milliseconds before do_messenger() should be called again
@@ -2567,9 +2551,9 @@ void do_messenger(Messenger *m, void *userdata)
if (mono_time_get(m->mono_time) > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
m->lastdump = mono_time_get(m->mono_time);
- uint32_t client, last_pinged;
+ uint32_t last_pinged;
- for (client = 0; client < LCLIENT_LIST; ++client) {
+ for (uint32_t client = 0; client < LCLIENT_LIST; ++client) {
const Client_data *cptr = dht_get_close_client(m->dht, client);
const IPPTsPng *const assocs[] = { &cptr->assoc4, &cptr->assoc6, nullptr };
@@ -2594,14 +2578,12 @@ void do_messenger(Messenger *m, void *userdata)
}
- uint32_t friend_idx, dhtfriend;
-
/* dht contains additional "friends" (requests) */
uint32_t num_dhtfriends = dht_get_num_friends(m->dht);
VLA(int32_t, m2dht, num_dhtfriends);
VLA(int32_t, dht2m, num_dhtfriends);
- for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
+ for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
m2dht[friend_idx] = -1;
dht2m[friend_idx] = -1;
@@ -2609,7 +2591,7 @@ void do_messenger(Messenger *m, void *userdata)
continue;
}
- for (dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
+ for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) {
m2dht[friend_idx] = dhtfriend;
break;
@@ -2617,7 +2599,7 @@ void do_messenger(Messenger *m, void *userdata)
}
}
- for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
+ for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
if (m2dht[friend_idx] >= 0) {
dht2m[m2dht[friend_idx]] = friend_idx;
}
@@ -2630,7 +2612,7 @@ void do_messenger(Messenger *m, void *userdata)
Friend *msgfptr;
DHT_Friend *dhtfptr;
- for (friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
+ for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
if (dht2m[friend_idx] >= 0) {
msgfptr = &m->friendlist[dht2m[friend_idx]];
} else {
@@ -2650,7 +2632,7 @@ void do_messenger(Messenger *m, void *userdata)
id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str)));
}
- for (client = 0; client < MAX_FRIEND_CLIENTS; ++client) {
+ for (uint32_t client = 0; client < MAX_FRIEND_CLIENTS; ++client) {
const Client_data *cptr = dht_friend_client(dhtfptr, client);
const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6};
diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.h b/protocols/Tox/libtox/src/toxcore/Messenger.h
index 31526b631d..673fd425a2 100644
--- a/protocols/Tox/libtox/src/toxcore/Messenger.h
+++ b/protocols/Tox/libtox/src/toxcore/Messenger.h
@@ -109,7 +109,6 @@ typedef enum Connection_Status {
CONNECTION_NONE,
CONNECTION_TCP,
CONNECTION_UDP,
- CONNECTION_UNKNOWN,
} Connection_Status;
/* USERSTATUS -
@@ -132,7 +131,6 @@ struct File_Transfers {
uint8_t paused; /* 0: not paused, 1 = paused by us, 2 = paused by other, 3 = paused by both. */
uint32_t last_packet_number; /* number of the last packet sent. */
uint64_t requested; /* total data requested by the request chunk callback */
- unsigned int slots_allocated; /* number of slots allocated to this transfer. */
uint8_t id[FILE_ID_LENGTH];
};
typedef enum Filestatus {
@@ -225,7 +223,7 @@ typedef struct Friend {
uint32_t message_id; // a semi-unique id used in read receipts.
uint32_t friendrequest_nospam; // The nospam number used in the friend request.
uint64_t last_seen_time;
- uint8_t last_connection_udp_tcp;
+ Connection_Status last_connection_udp_tcp;
struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES];
uint32_t num_sending_files;
struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES];
@@ -360,7 +358,7 @@ int getfriendcon_id(const Messenger *m, int32_t friendnumber);
*/
int m_delfriend(Messenger *m, int32_t friendnumber);
-/* Checks friend's connecting status.
+/* Checks friend's connection status.
*
* return CONNECTION_UDP (2) if friend is directly connected to us (Online UDP).
* return CONNECTION_TCP (1) if friend is connected to us (Online TCP).
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_client.c b/protocols/Tox/libtox/src/toxcore/TCP_client.c
index 408ec4891a..1405d9b2c3 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_client.c
+++ b/protocols/Tox/libtox/src/toxcore/TCP_client.c
@@ -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)) {
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_client.h b/protocols/Tox/libtox/src/toxcore/TCP_client.h
index cecf5a6d5f..ed4304cb00 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_client.h
+++ b/protocols/Tox/libtox/src/toxcore/TCP_client.h
@@ -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
*/
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);
}
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_connection.h b/protocols/Tox/libtox/src/toxcore/TCP_connection.h
index a8e522d343..0ae3304541 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_connection.h
+++ b/protocols/Tox/libtox/src/toxcore/TCP_connection.h
@@ -27,10 +27,10 @@
#define MAX_FRIEND_TCP_CONNECTIONS 6
/* Time until connection to friend gets killed (if it doesn't get locked within that time) */
-#define TCP_CONNECTION_ANNOUNCE_TIMEOUT (TCP_CONNECTION_TIMEOUT)
+#define TCP_CONNECTION_ANNOUNCE_TIMEOUT TCP_CONNECTION_TIMEOUT
/* The amount of recommended connections for each friend
- NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */
+ * NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */
#define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2)
/* Number of TCP connections used for onion purposes. */
@@ -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
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_server.c b/protocols/Tox/libtox/src/toxcore/TCP_server.c
index b22bdb8084..79a9e560c3 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_server.c
+++ b/protocols/Tox/libtox/src/toxcore/TCP_server.c
@@ -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) {
diff --git a/protocols/Tox/libtox/src/toxcore/TCP_server.h b/protocols/Tox/libtox/src/toxcore/TCP_server.h
index 5862a4733f..46dd03304f 100644
--- a/protocols/Tox/libtox/src/toxcore/TCP_server.h
+++ b/protocols/Tox/libtox/src/toxcore/TCP_server.h
@@ -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
diff --git a/protocols/Tox/libtox/src/toxcore/ccompat.h b/protocols/Tox/libtox/src/toxcore/ccompat.h
index 8ff6563d9e..34def21bf9 100644
--- a/protocols/Tox/libtox/src/toxcore/ccompat.h
+++ b/protocols/Tox/libtox/src/toxcore/ccompat.h
@@ -42,6 +42,9 @@
#if !defined(__cplusplus) || __cplusplus < 201103L
#define nullptr NULL
+#ifndef static_assert
+#define static_assert(cond, msg) extern int unused_for_static_assert
+#endif
#endif
#ifdef __GNUC__
diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.c b/protocols/Tox/libtox/src/toxcore/crypto_core.c
index 2fa4750ff4..35e3a60307 100644
--- a/protocols/Tox/libtox/src/toxcore/crypto_core.c
+++ b/protocols/Tox/libtox/src/toxcore/crypto_core.c
@@ -33,41 +33,26 @@
#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
#endif
-#if CRYPTO_PUBLIC_KEY_SIZE != crypto_box_PUBLICKEYBYTES
-#error "CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES"
-#endif
-
-#if CRYPTO_SECRET_KEY_SIZE != crypto_box_SECRETKEYBYTES
-#error "CRYPTO_SECRET_KEY_SIZE should be equal to crypto_box_SECRETKEYBYTES"
-#endif
-
-#if CRYPTO_SHARED_KEY_SIZE != crypto_box_BEFORENMBYTES
-#error "CRYPTO_SHARED_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES"
-#endif
-
-#if CRYPTO_SYMMETRIC_KEY_SIZE != crypto_box_BEFORENMBYTES
-#error "CRYPTO_SYMMETRIC_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES"
-#endif
-
-#if CRYPTO_MAC_SIZE != crypto_box_MACBYTES
-#error "CRYPTO_MAC_SIZE should be equal to crypto_box_MACBYTES"
-#endif
-
-#if CRYPTO_NONCE_SIZE != crypto_box_NONCEBYTES
-#error "CRYPTO_NONCE_SIZE should be equal to crypto_box_NONCEBYTES"
-#endif
-
-#if CRYPTO_SHA256_SIZE != crypto_hash_sha256_BYTES
-#error "CRYPTO_SHA256_SIZE should be equal to crypto_hash_sha256_BYTES"
-#endif
-
-#if CRYPTO_SHA512_SIZE != crypto_hash_sha512_BYTES
-#error "CRYPTO_SHA512_SIZE should be equal to crypto_hash_sha512_BYTES"
-#endif
-
-#if CRYPTO_PUBLIC_KEY_SIZE != 32
-#error "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_cmp to work,"
-#endif
+//!TOKSTYLE-
+static_assert(CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES,
+ "CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES");
+static_assert(CRYPTO_SECRET_KEY_SIZE == crypto_box_SECRETKEYBYTES,
+ "CRYPTO_SECRET_KEY_SIZE should be equal to crypto_box_SECRETKEYBYTES");
+static_assert(CRYPTO_SHARED_KEY_SIZE == crypto_box_BEFORENMBYTES,
+ "CRYPTO_SHARED_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES");
+static_assert(CRYPTO_SYMMETRIC_KEY_SIZE == crypto_box_BEFORENMBYTES,
+ "CRYPTO_SYMMETRIC_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES");
+static_assert(CRYPTO_MAC_SIZE == crypto_box_MACBYTES,
+ "CRYPTO_MAC_SIZE should be equal to crypto_box_MACBYTES");
+static_assert(CRYPTO_NONCE_SIZE == crypto_box_NONCEBYTES,
+ "CRYPTO_NONCE_SIZE should be equal to crypto_box_NONCEBYTES");
+static_assert(CRYPTO_SHA256_SIZE == crypto_hash_sha256_BYTES,
+ "CRYPTO_SHA256_SIZE should be equal to crypto_hash_sha256_BYTES");
+static_assert(CRYPTO_SHA512_SIZE == crypto_hash_sha512_BYTES,
+ "CRYPTO_SHA512_SIZE should be equal to crypto_hash_sha512_BYTES");
+static_assert(CRYPTO_PUBLIC_KEY_SIZE == 32,
+ "CRYPTO_PUBLIC_KEY_SIZE is required to be 32 bytes for public_key_cmp to work");
+//!TOKSTYLE+
static uint8_t *crypto_malloc(size_t bytes)
{
diff --git a/protocols/Tox/libtox/src/toxcore/friend_connection.c b/protocols/Tox/libtox/src/toxcore/friend_connection.c
index af5d09e370..496b83b505 100644
--- a/protocols/Tox/libtox/src/toxcore/friend_connection.c
+++ b/protocols/Tox/libtox/src/toxcore/friend_connection.c
@@ -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;
@@ -273,7 +273,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
for (int i = 0; i < n; ++i) {
/* Associated the relays being sent with this connection.
- On receiving the peer will do the same which will establish the connection. */
+ * On receiving the peer will do the same which will establish the connection. */
friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key);
}
@@ -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;
diff --git a/protocols/Tox/libtox/src/toxcore/friend_connection.h b/protocols/Tox/libtox/src/toxcore/friend_connection.h
index 92e3870345..fecd745565 100644
--- a/protocols/Tox/libtox/src/toxcore/friend_connection.h
+++ b/protocols/Tox/libtox/src/toxcore/friend_connection.h
@@ -34,7 +34,7 @@
#define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4)
/* Max number of tcp relays sent to friends */
-#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS)
+#define MAX_SHARED_RELAYS RECOMMENDED_FRIEND_TCP_CONNECTIONS
/* Interval between the sending of tcp relay information */
#define SHARE_RELAYS_INTERVAL (5 * 60)
@@ -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. */
diff --git a/protocols/Tox/libtox/src/toxcore/group.c b/protocols/Tox/libtox/src/toxcore/group.c
index a946e7b8e7..4b331f986e 100644
--- a/protocols/Tox/libtox/src/toxcore/group.c
+++ b/protocols/Tox/libtox/src/toxcore/group.c
@@ -242,7 +242,8 @@ static int get_peer_index(const Group_c *g, uint16_t peer_number)
static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
{
- uint64_t cmp1 = 0, cmp2 = 0;
+ uint64_t cmp1 = 0;
+ uint64_t cmp2 = 0;
for (size_t i = 0; i < sizeof(uint64_t); ++i) {
cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
@@ -1541,6 +1542,10 @@ static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t fri
{
Group_c *g = get_group_c(g_c, groupnumber);
+ if (g == nullptr) {
+ return false;
+ }
+
const bool member = (g->status == GROUPCHAT_STATUS_CONNECTED);
VLA(uint8_t, response, member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE);
@@ -1932,7 +1937,8 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
return;
}
- uint16_t other_groupnum, groupnum;
+ uint16_t other_groupnum;
+ uint16_t groupnum;
net_unpack_u16(data + 1, &other_groupnum);
net_unpack_u16(data + 1 + sizeof(uint16_t), &groupnum);
@@ -1979,7 +1985,8 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
return;
}
- uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE], temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
addpeer(g_c, groupnum, real_pk, temp_pk, peer_number, userdata, true, true);
@@ -2097,7 +2104,8 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->type, g->id);
if (g->connections[index].reasons & GROUPCHAT_CONNECTION_REASON_INTRODUCING) {
- uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE], temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
const int peer_index = peer_in_group(g, real_pk);
@@ -2129,7 +2137,8 @@ static int handle_packet_rejoin(Group_Chats *g_c, int friendcon_id, const uint8_
return -1;
}
- uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE], temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
uint16_t peer_number;
@@ -2366,7 +2375,9 @@ static unsigned int send_lossy_all_connections(const Group_Chats *g_c, const Gro
uint16_t length,
int receiver)
{
- unsigned int sent = 0, num_connected_closest = 0, connected_closest[DESIRED_CLOSEST];
+ unsigned int sent = 0;
+ unsigned int num_connected_closest = 0;
+ unsigned int connected_closest[DESIRED_CLOSEST];
for (unsigned int i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->connections[i].type != GROUPCHAT_CONNECTION_ONLINE) {
@@ -2656,6 +2667,10 @@ static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber,
return;
}
+ uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
+ get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[connection_index].number);
+ const bool direct_from_sender = id_equal(g->group[index].real_pk, real_pk);
+
switch (message_id) {
case GROUP_MESSAGE_PING_ID:
break;
@@ -2752,11 +2767,7 @@ static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber,
* back. When the sender only has one group connection (e.g. because there
* are only two peers in the group), this is the only way for them to
* receive their own message. */
- uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
- get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, g->connections[connection_index].number);
- bool relay_back = id_equal(g->group[index].real_pk, real_pk);
-
- send_message_all_connections(g_c, g, data, length, relay_back ? -1 : connection_index);
+ send_message_all_connections(g_c, g, data, length, direct_from_sender ? -1 : connection_index);
}
static int g_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
@@ -2877,7 +2888,9 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin
return -1;
}
- uint16_t groupnumber, peer_number, message_number;
+ uint16_t groupnumber;
+ uint16_t peer_number;
+ uint16_t message_number;
memcpy(&groupnumber, data + 1, sizeof(uint16_t));
memcpy(&peer_number, data + 1 + sizeof(uint16_t), sizeof(uint16_t));
memcpy(&message_number, data + 1 + sizeof(uint16_t) * 2, sizeof(uint16_t));
diff --git a/protocols/Tox/libtox/src/toxcore/net_crypto.c b/protocols/Tox/libtox/src/toxcore/net_crypto.c
index 2851053bbf..97383a059a 100644
--- a/protocols/Tox/libtox/src/toxcore/net_crypto.c
+++ b/protocols/Tox/libtox/src/toxcore/net_crypto.c
@@ -617,7 +617,8 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id)
}
const uint64_t current_time = mono_time_get(c->mono_time);
- bool v6 = 0, v4 = 0;
+ bool v6 = 0;
+ bool v4 = 0;
if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) {
v4 = 1;
@@ -932,9 +933,9 @@ static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t le
return cur_len;
}
- uint32_t i, n = 1;
+ uint32_t n = 1;
- for (i = recv_array->buffer_start; i != recv_array->buffer_end; ++i) {
+ for (uint32_t i = recv_array->buffer_start; i != recv_array->buffer_end; ++i) {
uint32_t num = i % CRYPTO_PACKET_BUFFER_SIZE;
if (!recv_array->buffer[num]) {
@@ -1118,7 +1119,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
}
/* If last packet send failed, try to send packet again.
- If sending it fails we won't be able to send the new packet. */
+ * If sending it fails we won't be able to send the new packet. */
if (conn->maximum_speed_reached) {
Packet_Data *dt = nullptr;
const uint32_t packet_num = conn->send_array.buffer_end - 1;
@@ -1156,7 +1157,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
}
/* If last packet send failed, try to send packet again.
- If sending it fails we won't be able to send the new packet. */
+ * If sending it fails we won't be able to send the new packet. */
reset_max_speed_reached(c, crypt_connection_id);
if (conn->maximum_speed_reached && congestion_control) {
@@ -1290,9 +1291,10 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32
}
const uint64_t temp_time = current_time_monotonic(c->mono_time);
- uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array);
+ const uint32_t array_size = num_packets_array(&conn->send_array);
+ uint32_t num_sent = 0;
- for (i = 0; i < array_size; ++i) {
+ for (uint32_t i = 0; i < array_size; ++i) {
Packet_Data *dt;
const uint32_t packet_num = i + conn->send_array.buffer_start;
const int ret = get_data_pointer(c->log, &conn->send_array, &dt, packet_num);
@@ -1510,7 +1512,8 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
return -1;
}
- uint32_t buffer_start, num;
+ uint32_t buffer_start;
+ uint32_t num;
memcpy(&buffer_start, data, sizeof(uint32_t));
memcpy(&num, data + sizeof(uint32_t), sizeof(uint32_t));
buffer_start = net_ntohl(buffer_start);
@@ -2281,12 +2284,10 @@ 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;
-
- for (i = 0; i < c->crypto_connections_length; ++i) {
+ for (uint32_t i = 0; i < c->crypto_connections_length; ++i) {
Crypto_Connection *conn = get_crypto_connection(c, i);
if (conn == nullptr) {
@@ -2297,21 +2298,15 @@ static void do_tcp(Net_Crypto *c, void *userdata)
continue;
}
- bool direct_connected = 0;
+ bool direct_connected = false;
if (!crypto_connection_status(c, i, &direct_connected, nullptr)) {
continue;
}
- if (direct_connected) {
- pthread_mutex_lock(&c->tcp_mutex);
- set_tcp_connection_to_status(c->tcp_c, conn->connection_number_tcp, 0);
- pthread_mutex_unlock(&c->tcp_mutex);
- } else {
- pthread_mutex_lock(&c->tcp_mutex);
- set_tcp_connection_to_status(c->tcp_c, conn->connection_number_tcp, 1);
- pthread_mutex_unlock(&c->tcp_mutex);
- }
+ pthread_mutex_lock(&c->tcp_mutex);
+ set_tcp_connection_to_status(c->tcp_c, conn->connection_number_tcp, !direct_connected);
+ pthread_mutex_unlock(&c->tcp_mutex);
}
}
@@ -2477,7 +2472,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
}
/* The dT for the average packet receiving rate calculations.
- Also used as the */
+ * Also used as the */
#define PACKET_COUNTER_AVERAGE_INTERVAL 50
/* Ratio of recv queue size / recv packet rate (in seconds) times
@@ -2563,7 +2558,7 @@ static void send_crypto_packets(Net_Crypto *c)
conn->packets_resent = 0;
/* conjestion control
- calculate a new value of conn->packet_send_rate based on some data
+ * calculate a new value of conn->packet_send_rate based on some data
*/
unsigned int pos = conn->last_sendqueue_counter % CONGESTION_QUEUE_ARRAY_SIZE;
@@ -2586,7 +2581,8 @@ static void send_crypto_packets(Net_Crypto *c)
/* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */
if (!(direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time)) {
- long signed int total_sent = 0, total_resent = 0;
+ long signed int total_sent = 0;
+ long signed int total_resent = 0;
// TODO(irungentoo): use real delay
unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5);
@@ -2925,9 +2921,7 @@ bool crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool
if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) {
*direct_connected = 1;
- }
-
- if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) {
+ } else if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) {
*direct_connected = 1;
}
}
diff --git a/protocols/Tox/libtox/src/toxcore/net_crypto.h b/protocols/Tox/libtox/src/toxcore/net_crypto.h
index 3885a1ba5f..33275708af 100644
--- a/protocols/Tox/libtox/src/toxcore/net_crypto.h
+++ b/protocols/Tox/libtox/src/toxcore/net_crypto.h
@@ -87,7 +87,7 @@
#define CRYPTO_SEND_PACKET_INTERVAL 1000
/* The maximum number of times we try to send the cookie request and handshake
- before giving up. */
+ * before giving up. */
#define MAX_NUM_SENDPACKET_TRIES 8
/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
@@ -100,7 +100,7 @@
#define CRYPTO_MAX_PADDING 8
/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken
- at the dT defined in net_crypto.c */
+ * at the dT defined in net_crypto.c */
#define CONGESTION_QUEUE_ARRAY_SIZE 12
#define CONGESTION_LAST_SENT_ARRAY_SIZE (CONGESTION_QUEUE_ARRAY_SIZE * 2)
diff --git a/protocols/Tox/libtox/src/toxcore/network.c b/protocols/Tox/libtox/src/toxcore/network.c
index e5a5833109..3efa3283e9 100644
--- a/protocols/Tox/libtox/src/toxcore/network.c
+++ b/protocols/Tox/libtox/src/toxcore/network.c
@@ -310,7 +310,7 @@ IP6 get_ip6_loopback(void)
}
#ifndef OS_WIN32
-#define INVALID_SOCKET -1
+#define INVALID_SOCKET (-1)
#endif
const Socket net_invalid_socket = { (int)INVALID_SOCKET };
@@ -1532,7 +1532,8 @@ size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
{
const uint8_t *p = bytes;
- uint16_t lo, hi;
+ uint16_t hi;
+ uint16_t lo;
p += net_unpack_u16(p, &hi);
p += net_unpack_u16(p, &lo);
*v = ((uint32_t)hi << 16) | lo;
@@ -1542,7 +1543,8 @@ size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
{
const uint8_t *p = bytes;
- uint32_t lo, hi;
+ uint32_t hi;
+ uint32_t lo;
p += net_unpack_u32(p, &hi);
p += net_unpack_u32(p, &lo);
*v = ((uint64_t)hi << 32) | lo;
diff --git a/protocols/Tox/libtox/src/toxcore/onion_announce.c b/protocols/Tox/libtox/src/toxcore/onion_announce.c
index 6d38012aa7..0a04a4b834 100644
--- a/protocols/Tox/libtox/src/toxcore/onion_announce.c
+++ b/protocols/Tox/libtox/src/toxcore/onion_announce.c
@@ -259,7 +259,8 @@ typedef struct Cmp_data {
static int cmp_entry(const void *a, const void *b)
{
- Cmp_data cmp1, cmp2;
+ Cmp_data cmp1;
+ Cmp_data cmp2;
memcpy(&cmp1, a, sizeof(Cmp_data));
memcpy(&cmp2, b, sizeof(Cmp_data));
Onion_Announce_Entry entry1 = cmp1.entry;
diff --git a/protocols/Tox/libtox/src/toxcore/onion_client.c b/protocols/Tox/libtox/src/toxcore/onion_client.c
index e161928bae..d3a56af73b 100644
--- a/protocols/Tox/libtox/src/toxcore/onion_client.c
+++ b/protocols/Tox/libtox/src/toxcore/onion_client.c
@@ -21,7 +21,7 @@
#include "util.h"
/* defines for the array size and
- timeout for onion announce packets. */
+ * timeout for onion announce packets. */
#define ANNOUNCE_ARRAY_SIZE 256
#define ANNOUNCE_TIMEOUT 10
@@ -97,6 +97,7 @@ typedef struct Onion_Data_Handler {
struct Onion_Client {
Mono_Time *mono_time;
+ const Logger *logger;
DHT *dht;
Net_Crypto *c;
@@ -610,7 +611,8 @@ typedef struct Onion_Client_Cmp_data {
static int onion_client_cmp_entry(const void *a, const void *b)
{
- Onion_Client_Cmp_data cmp1, cmp2;
+ Onion_Client_Cmp_data cmp1;
+ Onion_Client_Cmp_data cmp2;
memcpy(&cmp1, a, sizeof(Onion_Client_Cmp_data));
memcpy(&cmp2, b, sizeof(Onion_Client_Cmp_data));
Onion_Node entry1 = cmp1.entry;
@@ -700,7 +702,8 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
sort_onion_node_list(list_nodes, list_length, onion_c->mono_time, reference_id);
- int index = -1, stored = 0;
+ int index = -1;
+ int stored = 0;
unsigned int i;
if (onion_node_timed_out(&list_nodes[0], onion_c->mono_time)
@@ -1045,10 +1048,12 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
return -1;
}
- unsigned int i, good_nodes[MAX_ONION_CLIENTS], num_good = 0, num_nodes = 0;
+ unsigned int good_nodes[MAX_ONION_CLIENTS];
+ unsigned int num_good = 0;
+ unsigned int num_nodes = 0;
Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;
- for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
+ for (unsigned int i = 0; i < MAX_ONION_CLIENTS; ++i) {
if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) {
continue;
}
@@ -1080,7 +1085,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
unsigned int good = 0;
- for (i = 0; i < num_good; ++i) {
+ for (unsigned int i = 0; i < num_good; ++i) {
Onion_Path path;
if (random_path(onion_c, &onion_c->onion_paths_friends, -1, &path) == -1) {
@@ -1209,7 +1214,8 @@ static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8
}
}
- int num1 = -1, num2 = -1;
+ int num1 = -1;
+ int num2 = -1;
if (onion_dht_both != 1) {
num1 = send_onion_data(onion_c, friend_num, data, DHTPK_DATA_MIN_LENGTH + nodes_len);
@@ -1288,9 +1294,9 @@ int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key)
return num;
}
- unsigned int i, index = -1;
+ unsigned int index = -1;
- for (i = 0; i < onion_c->num_friends; ++i) {
+ for (unsigned int i = 0; i < onion_c->num_friends; ++i) {
if (onion_c->friends_list[i].status == 0) {
index = i;
break;
@@ -1658,10 +1664,10 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
static void do_announce(Onion_Client *onion_c)
{
- unsigned int i, count = 0;
+ unsigned int count = 0;
Onion_Node *list_nodes = onion_c->clients_announce_list;
- for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
+ for (unsigned int i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) {
continue;
}
@@ -1734,7 +1740,7 @@ static void do_announce(Onion_Client *onion_c)
if (count <= random_u32() % MAX_ONION_CLIENTS_ANNOUNCE) {
if (num_nodes != 0) {
- for (i = 0; i < (MAX_ONION_CLIENTS_ANNOUNCE / 2); ++i) {
+ for (unsigned int i = 0; i < (MAX_ONION_CLIENTS_ANNOUNCE / 2); ++i) {
const uint32_t num = random_u32() % num_nodes;
client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].public_key, nullptr, -1);
}
@@ -1748,7 +1754,8 @@ static void do_announce(Onion_Client *onion_c)
*/
static int onion_isconnected(const Onion_Client *onion_c)
{
- unsigned int i, num = 0, announced = 0;
+ unsigned int num = 0;
+ unsigned int announced = 0;
if (mono_time_is_timeout(onion_c->mono_time, onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) {
return 0;
@@ -1758,7 +1765,7 @@ static int onion_isconnected(const Onion_Client *onion_c)
return 0;
}
- for (i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
+ for (unsigned int i = 0; i < MAX_ONION_CLIENTS_ANNOUNCE; ++i) {
if (!onion_node_timed_out(&onion_c->clients_announce_list[i], onion_c->mono_time)) {
++num;
@@ -1775,7 +1782,7 @@ static int onion_isconnected(const Onion_Client *onion_c)
}
/* Consider ourselves online if we are announced to half or more nodes
- we are connected to */
+ * we are connected to */
if (num && announced) {
if ((num / 2) <= announced && (pnodes / 2) <= num) {
return 1;
@@ -1827,15 +1834,12 @@ void do_onion_client(Onion_Client *onion_c)
}
}
- bool udp_connected = dht_non_lan_connected(onion_c->dht);
+ onion_c->udp_connected = dht_non_lan_connected(onion_c->dht);
if (mono_time_is_timeout(onion_c->mono_time, onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) {
- set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !udp_connected);
+ set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !onion_c->udp_connected);
}
- onion_c->udp_connected = udp_connected
- || get_random_tcp_onion_conn_number(nc_get_tcp_c(onion_c->c)) == -1; /* Check if connected to any TCP relays. */
-
if (onion_connection_status(onion_c)) {
for (unsigned i = 0; i < onion_c->num_friends; ++i) {
do_friend(onion_c, i);
@@ -1849,7 +1853,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 +1873,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;
diff --git a/protocols/Tox/libtox/src/toxcore/onion_client.h b/protocols/Tox/libtox/src/toxcore/onion_client.h
index 5b6bea659a..c0f8f52c8b 100644
--- a/protocols/Tox/libtox/src/toxcore/onion_client.h
+++ b/protocols/Tox/libtox/src/toxcore/onion_client.h
@@ -26,7 +26,7 @@
#define NUMBER_ONION_PATHS 6
/* The timeout the first time the path is added and
- then for all the next consecutive times */
+ * then for all the next consecutive times */
#define ONION_PATH_FIRST_TIMEOUT 4
#define ONION_PATH_TIMEOUT 10
#define ONION_PATH_MAX_LIFETIME 1200
@@ -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);
diff --git a/protocols/Tox/libtox/src/toxcore/tox.api.h b/protocols/Tox/libtox/src/toxcore/tox.api.h
index 3655617181..90a6ffc8f8 100644
--- a/protocols/Tox/libtox/src/toxcore/tox.api.h
+++ b/protocols/Tox/libtox/src/toxcore/tox.api.h
@@ -169,7 +169,7 @@ const VERSION_MINOR = 2;
* The patch or revision number. Incremented when bugfixes are applied without
* changing any functionality or API or ABI.
*/
-const VERSION_PATCH = 12;
+const VERSION_PATCH = 13;
/**
* A macro to check at preprocessing time whether the client code is compatible
diff --git a/protocols/Tox/libtox/src/toxcore/tox.c b/protocols/Tox/libtox/src/toxcore/tox.c
index d2d3ed344f..780d913667 100644
--- a/protocols/Tox/libtox/src/toxcore/tox.c
+++ b/protocols/Tox/libtox/src/toxcore/tox.c
@@ -400,7 +400,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
Messenger_Options m_options = {0};
- bool load_savedata_sk = false, load_savedata_tox = false;
+ bool load_savedata_sk = false;
+ bool load_savedata_tox = false;
struct Tox_Options *default_options = nullptr;
diff --git a/protocols/Tox/libtox/src/toxcore/tox.h b/protocols/Tox/libtox/src/toxcore/tox.h
index c9c0967a7f..ab71986e9e 100644
--- a/protocols/Tox/libtox/src/toxcore/tox.h
+++ b/protocols/Tox/libtox/src/toxcore/tox.h
@@ -170,7 +170,7 @@ uint32_t tox_version_minor(void);
* The patch or revision number. Incremented when bugfixes are applied without
* changing any functionality or API or ABI.
*/
-#define TOX_VERSION_PATCH 12
+#define TOX_VERSION_PATCH 13
uint32_t tox_version_patch(void);
diff --git a/protocols/Tox/libtox/src/toxcore/util.c b/protocols/Tox/libtox/src/toxcore/util.c
index 58b75763e5..2f00adacb1 100644
--- a/protocols/Tox/libtox/src/toxcore/util.c
+++ b/protocols/Tox/libtox/src/toxcore/util.c
@@ -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)
diff --git a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.api.h b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.api.h
index b1068c5079..df5d1136f3 100644
--- a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.api.h
+++ b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.api.h
@@ -10,6 +10,8 @@
#ifndef C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H
#define C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H
+//!TOKSTYLE-
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -303,5 +305,7 @@ typedef TOX_ERR_ENCRYPTION Tox_Err_Encryption;
typedef TOX_ERR_DECRYPTION Tox_Err_Decryption;
typedef TOX_ERR_GET_SALT Tox_Err_Get_Salt;
+//!TOKSTYLE+
+
#endif // C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H
%}
diff --git a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c
index 70aa3746e4..be4bcaf26c 100644
--- a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c
+++ b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c
@@ -28,17 +28,15 @@
#include <stdlib.h>
#include <string.h>
-#if TOX_PASS_SALT_LENGTH != crypto_pwhash_scryptsalsa208sha256_SALTBYTES
-#error TOX_PASS_SALT_LENGTH is assumed to be equal to crypto_pwhash_scryptsalsa208sha256_SALTBYTES
-#endif
-
-#if TOX_PASS_KEY_LENGTH != CRYPTO_SHARED_KEY_SIZE
-#error TOX_PASS_KEY_LENGTH is assumed to be equal to CRYPTO_SHARED_KEY_SIZE
-#endif
-
-#if TOX_PASS_ENCRYPTION_EXTRA_LENGTH != (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH)
-#error TOX_PASS_ENCRYPTION_EXTRA_LENGTH is assumed to be equal to (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH)
-#endif
+//!TOKSTYLE-
+static_assert(TOX_PASS_SALT_LENGTH == crypto_pwhash_scryptsalsa208sha256_SALTBYTES,
+ "TOX_PASS_SALT_LENGTH is assumed to be equal to crypto_pwhash_scryptsalsa208sha256_SALTBYTES");
+static_assert(TOX_PASS_KEY_LENGTH == CRYPTO_SHARED_KEY_SIZE,
+ "TOX_PASS_KEY_LENGTH is assumed to be equal to CRYPTO_SHARED_KEY_SIZE");
+static_assert(TOX_PASS_ENCRYPTION_EXTRA_LENGTH == (crypto_box_MACBYTES + crypto_box_NONCEBYTES +
+ crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH),
+ "TOX_PASS_ENCRYPTION_EXTRA_LENGTH is assumed to be equal to (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH)");
+//!TOKSTYLE+
uint32_t tox_pass_salt_length(void)
{
@@ -109,7 +107,7 @@ Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t pplength,
Tox_Err_Key_Derivation *error)
{
uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
- random_bytes(salt, sizeof salt);
+ random_bytes(salt, sizeof(salt));
return tox_pass_key_derive_with_salt(passphrase, pplength, salt, error);
}
@@ -129,10 +127,10 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp
uint8_t key[CRYPTO_SHARED_KEY_SIZE];
- /* Derive a key from the password */
- /* http://doc.libsodium.org/key_derivation/README.html */
- /* note that, according to the documentation, a generic pwhash interface will be created
- * once the pwhash competition (https://password-hashing.net/) is over */
+ // Derive a key from the password
+ // http://doc.libsodium.org/key_derivation/README.html
+ // note that, according to the documentation, a generic pwhash interface will be created
+ // once the pwhash competition (https://password-hashing.net/) is over */
if (crypto_pwhash_scryptsalsa208sha256(
key, sizeof(key), (char *)passkey, sizeof(passkey), salt,
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 2, /* slightly stronger */
@@ -157,7 +155,8 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp
return out_key;
}
-/* Encrypt arbitrary with a key produced by tox_derive_key_*. The output
+/**
+ * Encrypt arbitrary with a key produced by `tox_derive_key_*`. The output
* array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long.
* key must be TOX_PASS_KEY_LENGTH bytes.
* If you already have a symmetric key from somewhere besides this module, simply
@@ -173,13 +172,12 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t d
return 0;
}
- /* the output data consists of, in order:
- * salt, nonce, mac, enc_data
- * where the mac is automatically prepended by the encrypt()
- * the salt+nonce is called the prefix
- * I'm not sure what else I'm supposed to do with the salt and nonce, since we
- * need them to decrypt the data
- */
+ // the output data consists of, in order:
+ // salt, nonce, mac, enc_data
+ // where the mac is automatically prepended by the encrypt()
+ // the salt+nonce is called the prefix
+ // I'm not sure what else I'm supposed to do with the salt and nonce, since we
+ // need them to decrypt the data
/* first add the magic number */
memcpy(out, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH);
@@ -214,13 +212,13 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t d
bool tox_pass_encrypt(const uint8_t *data, size_t data_len, const uint8_t *passphrase, size_t pplength, uint8_t *out,
Tox_Err_Encryption *error)
{
- Tox_Err_Key_Derivation _error;
- Tox_Pass_Key *key = tox_pass_key_derive(passphrase, pplength, &_error);
+ Tox_Err_Key_Derivation err;
+ Tox_Pass_Key *key = tox_pass_key_derive(passphrase, pplength, &err);
if (!key) {
- if (_error == TOX_ERR_KEY_DERIVATION_NULL) {
+ if (err == TOX_ERR_KEY_DERIVATION_NULL) {
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_NULL);
- } else if (_error == TOX_ERR_KEY_DERIVATION_FAILED) {
+ } else if (err == TOX_ERR_KEY_DERIVATION_FAILED) {
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED);
}
diff --git a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.h b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.h
index 34287a1ac9..4b58165e7d 100644
--- a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.h
+++ b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.h
@@ -9,6 +9,8 @@
#ifndef C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H
#define C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H
+//!TOKSTYLE-
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -365,4 +367,6 @@ typedef TOX_ERR_ENCRYPTION Tox_Err_Encryption;
typedef TOX_ERR_DECRYPTION Tox_Err_Decryption;
typedef TOX_ERR_GET_SALT Tox_Err_Get_Salt;
+//!TOKSTYLE+
+
#endif // C_TOXCORE_TOXENCRYPTSAVE_TOXENCRYPTSAVE_H