diff options
author | aunsane <aunsane@gmail.com> | 2018-03-03 16:35:09 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-03-03 16:35:33 +0300 |
commit | fa58f69fe117640e29cefb1b699bede4d045bc2f (patch) | |
tree | a1bf8406d63e2b246bebdc572ceccfaabcffa359 /protocols | |
parent | 0e8f5a3aa5f5e73b413c5646444751783e367f2b (diff) |
Tox:
- updated toxcore due to release v0.2
- removed message correction
- reworked nodes update
Diffstat (limited to 'protocols')
42 files changed, 1832 insertions, 1425 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.c b/protocols/Tox/libtox/src/toxcore/DHT.c index a42515181c..4acfa1617d 100644 --- a/protocols/Tox/libtox/src/toxcore/DHT.c +++ b/protocols/Tox/libtox/src/toxcore/DHT.c @@ -59,6 +59,92 @@ #define ASSOC_COUNT 2 +struct DHT { + Logger *log; + Networking_Core *net; + + bool hole_punching_enabled; + + Client_data close_clientlist[LCLIENT_LIST]; + uint64_t close_lastgetnodes; + uint32_t close_bootstrap_times; + + /* DHT keypair */ + uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; + + DHT_Friend *friends_list; + uint16_t num_friends; + + Node_format *loaded_nodes_list; + uint32_t loaded_num_nodes; + unsigned int loaded_nodes_index; + + Shared_Keys shared_keys_recv; + Shared_Keys shared_keys_sent; + + struct Ping *ping; + Ping_Array *dht_ping_array; + Ping_Array *dht_harden_ping_array; + uint64_t last_run; + + Cryptopacket_Handles cryptopackethandlers[256]; + + Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES]; + unsigned int num_to_bootstrap; +}; + +const uint8_t *dht_get_self_public_key(const DHT *dht) +{ + return dht->self_public_key; +} +const uint8_t *dht_get_self_secret_key(const DHT *dht) +{ + return dht->self_secret_key; +} + +void dht_set_self_public_key(DHT *dht, const uint8_t *key) +{ + memcpy(dht->self_public_key, key, CRYPTO_PUBLIC_KEY_SIZE); +} +void dht_set_self_secret_key(DHT *dht, const uint8_t *key) +{ + memcpy(dht->self_secret_key, key, CRYPTO_SECRET_KEY_SIZE); +} + +Networking_Core *dht_get_net(const DHT *dht) +{ + return dht->net; +} +struct Ping *dht_get_ping(const DHT *dht) +{ + return dht->ping; +} +const Client_data *dht_get_close_clientlist(const DHT *dht) +{ + return dht->close_clientlist; +} +const Client_data *dht_get_close_client(const DHT *dht, uint32_t client_num) +{ + assert(client_num < sizeof(dht->close_clientlist) / sizeof(dht->close_clientlist[0])); + return &dht->close_clientlist[client_num]; +} +uint16_t dht_get_num_friends(const DHT *dht) +{ + return dht->num_friends; +} + +DHT_Friend *dht_get_friend(DHT *dht, uint32_t friend_num) +{ + assert(friend_num < dht->num_friends); + return &dht->friends_list[friend_num]; +} +const uint8_t *dht_get_friend_public_key(const DHT *dht, uint32_t friend_num) +{ + assert(friend_num < dht->num_friends); + return dht->friends_list[friend_num].public_key; +} + /* Compares pk1 and pk2 with pk. * * return 0 if both are same distance. @@ -68,8 +154,8 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) { for (size_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) { - uint8_t distance1 = pk[i] ^ pk1[i]; - uint8_t distance2 = pk[i] ^ pk2[i]; + const uint8_t distance1 = pk[i] ^ pk1[i]; + const uint8_t distance2 = pk[i] ^ pk2[i]; if (distance1 < distance2) { return 1; @@ -96,7 +182,7 @@ static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2) } for (j = 0; j < 8; ++j) { - uint8_t mask = 1 << (7 - j); + const uint8_t mask = 1 << (7 - j); if ((pk1[i] & mask) != (pk2[i] & mask)) { break; @@ -121,8 +207,8 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t uint32_t curr = 0; for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { - int index = public_key[30] * MAX_KEYS_PER_SLOT + i; - Shared_Key *key = &shared_keys->keys[index]; + const int index = public_key[30] * MAX_KEYS_PER_SLOT + i; + Shared_Key *const key = &shared_keys->keys[index]; if (key->stored) { if (id_equal(public_key, key->public_key)) { @@ -149,8 +235,8 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t encrypt_precompute(public_key, secret_key, shared_key); - if (num != (uint32_t)~0) { - Shared_Key *key = &shared_keys->keys[curr]; + if (num != UINT32_MAX) { + Shared_Key *const key = &shared_keys->keys[curr]; key->stored = 1; key->times_requested = 1; memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -198,13 +284,13 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke return -1; } - uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; + uint8_t *const nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; random_nonce(nonce); uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; memcpy(temp + 1, data, length); temp[0] = request_id; - int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, - CRYPTO_SIZE + packet); + const int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, + CRYPTO_SIZE + packet); if (len == -1) { crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); @@ -241,7 +327,7 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke } memcpy(public_key, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_PUBLIC_KEY_SIZE); - const uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; + const uint8_t *const nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + CRYPTO_SIZE, length - CRYPTO_SIZE, temp); @@ -288,7 +374,7 @@ int packed_node_size(uint8_t ip_family) */ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) { - if (data == NULL) { + if (data == nullptr) { return -1; } @@ -313,25 +399,25 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) } if (is_ipv4) { - uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); + const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); if (size > length) { return -1; } data[0] = net_family; - memcpy(data + 1, &ip_port->ip.ip4, SIZE_IP4); + memcpy(data + 1, &ip_port->ip.ip.v4, SIZE_IP4); memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); return size; } else { - uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); + const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); if (size > length) { return -1; } data[0] = net_family; - memcpy(data + 1, &ip_port->ip.ip6, SIZE_IP6); + memcpy(data + 1, &ip_port->ip.ip.v6, SIZE_IP6); memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t)); return size; } @@ -345,7 +431,7 @@ static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], random_nonce(nonce); - int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted); + const int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted); if (encrypted_length == -1) { return -1; @@ -366,7 +452,7 @@ static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], */ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled) { - if (data == NULL) { + if (data == nullptr) { return -1; } @@ -398,25 +484,25 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 } if (is_ipv4) { - uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); + const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); if (size > length) { return -1; } ip_port->ip.family = host_family; - memcpy(&ip_port->ip.ip4, data + 1, SIZE_IP4); + memcpy(&ip_port->ip.ip.v4, data + 1, SIZE_IP4); memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); return size; } else { - uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); + const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); if (size > length) { return -1; } ip_port->ip.family = host_family; - memcpy(&ip_port->ip.ip6, data + 1, SIZE_IP6); + memcpy(&ip_port->ip.ip.v6, data + 1, SIZE_IP6); memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); return size; } @@ -432,7 +518,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ uint32_t packed_length = 0; for (uint32_t i = 0; i < number && packed_length < length; ++i) { - int ipp_size = pack_ip_port(data + packed_length, length - packed_length, &nodes[i].ip_port); + const int ipp_size = pack_ip_port(data + packed_length, length - packed_length, &nodes[i].ip_port); if (ipp_size == -1) { return -1; @@ -447,7 +533,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ memcpy(data + packed_length, nodes[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); packed_length += CRYPTO_PUBLIC_KEY_SIZE; - uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; + const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); } @@ -467,7 +553,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed uint32_t num = 0, len_processed = 0; while (num < max_num_nodes && len_processed < length) { - int ipp_size = unpack_ip_port(&nodes[num].ip_port, data + len_processed, length - len_processed, tcp_enabled); + const int ipp_size = unpack_ip_port(&nodes[num].ip_port, data + len_processed, length - len_processed, tcp_enabled); if (ipp_size == -1) { return -1; @@ -483,7 +569,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed len_processed += CRYPTO_PUBLIC_KEY_SIZE; ++num; - uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; + const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); } @@ -529,8 +615,8 @@ static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, const IP_Port *ip_port) { for (uint32_t i = 0; i < size; ++i) { - if (ip_port->ip.family == TOX_AF_INET && ipport_equal(&array[i].assoc4.ip_port, ip_port) || - ip_port->ip.family == TOX_AF_INET6 && ipport_equal(&array[i].assoc6.ip_port, ip_port)) { + if ((ip_port->ip.family == TOX_AF_INET && ipport_equal(&array[i].assoc4.ip_port, ip_port)) || + (ip_port->ip.family == TOX_AF_INET6 && ipport_equal(&array[i].assoc6.ip_port, ip_port))) { return i; } } @@ -583,7 +669,7 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port) { - uint64_t temp_time = unix_time(); + const uint64_t temp_time = unix_time(); uint32_t index = index_of_client_pk(list, length, public_key); /* if public_key is in list, find it and maybe overwrite ip_port */ @@ -677,14 +763,14 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ uint32_t num_nodes = *num_nodes_ptr; for (uint32_t i = 0; i < client_list_length; i++) { - const Client_data *client = &client_list[i]; + const Client_data *const client = &client_list[i]; /* node already in list? */ if (index_of_node_pk(nodes_list, MAX_SENT_NODES, client->public_key) != UINT32_MAX) { continue; } - const IPPTsPng *ipptp = NULL; + const IPPTsPng *ipptp = nullptr; if (sa_family == TOX_AF_INET) { ipptp = &client->assoc4; @@ -775,8 +861,8 @@ static int cmp_dht_entry(const void *a, const void *b) DHT_Cmp_data cmp1, cmp2; memcpy(&cmp1, a, sizeof(DHT_Cmp_data)); memcpy(&cmp2, b, sizeof(DHT_Cmp_data)); - Client_data entry1 = cmp1.entry; - Client_data entry2 = cmp2.entry; + const Client_data entry1 = cmp1.entry; + const Client_data entry2 = cmp2.entry; const uint8_t *cmp_public_key = cmp1.base_public_key; #define ASSOC_TIMEOUT(assoc) is_timeout((assoc).timestamp, BAD_NODE_TIMEOUT) @@ -809,7 +895,7 @@ static int cmp_dht_entry(const void *a, const void *b) return 1; } - int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); + const int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); if (close == 1) { return 1; @@ -829,9 +915,9 @@ static int cmp_dht_entry(const void *a, const void *b) */ static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key) { - return is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && - is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) || - id_closest(comp_public_key, client->public_key, public_key) == 2; + return (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) + && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) + || id_closest(comp_public_key, client->public_key, public_key) == 2; } static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) @@ -854,8 +940,8 @@ static void sort_client_list(Client_data *list, unsigned int length, const uint8 static void update_client_with_reset(Client_data *client, const IP_Port *ip_port) { - IPPTsPng *ipptp_write = NULL; - IPPTsPng *ipptp_clear = NULL; + IPPTsPng *ipptp_write = nullptr; + IPPTsPng *ipptp_clear = nullptr; if (ip_port->ip.family == TOX_AF_INET) { ipptp_write = &client->assoc4; @@ -888,29 +974,29 @@ static void update_client_with_reset(Client_data *client, const IP_Port *ip_port * and all nodes in the list are closer to comp_public_key * than public_key. * - * returns True(1) when the item was stored, False(0) otherwise */ -static int replace_all(Client_data *list, - uint16_t length, - const uint8_t *public_key, - IP_Port ip_port, - const uint8_t *comp_public_key) + * returns true when the item was stored, false otherwise */ +static bool replace_all(Client_data *list, + uint16_t length, + const uint8_t *public_key, + IP_Port ip_port, + const uint8_t *comp_public_key) { if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { - return 0; + return false; } if (!store_node_ok(&list[1], public_key, comp_public_key) && !store_node_ok(&list[0], public_key, comp_public_key)) { - return 0; + return false; } sort_client_list(list, length, comp_public_key); - Client_data *client = &list[0]; + Client_data *const client = &list[0]; id_copy(client->public_key, public_key); update_client_with_reset(client, &ip_port); - return 1; + return true; } /* Add node to close list. @@ -931,7 +1017,7 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo for (uint32_t i = 0; i < LCLIENT_NODES; ++i) { /* TODO(iphydf): write bounds checking test to catch the case that * index is left as >= LCLIENT_LENGTH */ - Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; + Client_data *const client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { @@ -957,18 +1043,18 @@ 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(Client_data *list, unsigned int client_list_length, const uint8_t *public_key, +static bool is_pk_in_client_list(const Client_data *list, unsigned int client_list_length, const uint8_t *public_key, IP_Port ip_port) { - uint32_t index = index_of_client_pk(list, client_list_length, public_key); + const uint32_t index = index_of_client_pk(list, client_list_length, public_key); if (index == UINT32_MAX) { return 0; } - const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET ? - &list[index].assoc4 : - &list[index].assoc6; + const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET + ? &list[index].assoc4 + : &list[index].assoc6; return !is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT); } @@ -987,52 +1073,54 @@ static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_ /* Check if the node obtained with a get_nodes with public_key should be pinged. * NOTE: for best results call it after addto_lists; * - * return 0 if the node should not be pinged. - * return 1 if it should. + * return false if the node should not be pinged. + * return true if it should. */ -static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) +static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) { - bool ret = 0; + bool ret = false; if (add_to_close(dht, public_key, ip_port, 1) == 0) { - ret = 1; + ret = true; } - unsigned int *num = &dht->num_to_bootstrap; - uint32_t index = index_of_node_pk(dht->to_bootstrap, *num, public_key); - bool in_close_list = is_pk_in_close_list(dht, public_key, ip_port); + { + unsigned int *const num = &dht->num_to_bootstrap; + const uint32_t index = index_of_node_pk(dht->to_bootstrap, *num, public_key); + const bool in_close_list = is_pk_in_close_list(dht, public_key, ip_port); - if (ret && index == UINT32_MAX && !in_close_list) { - if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) { - memcpy(dht->to_bootstrap[*num].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - dht->to_bootstrap[*num].ip_port = ip_port; - ++*num; - } else { - // TODO(irungentoo): ipv6 vs v4 - add_to_list(dht->to_bootstrap, MAX_CLOSE_TO_BOOTSTRAP_NODES, public_key, ip_port, dht->self_public_key); + if (ret && index == UINT32_MAX && !in_close_list) { + if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) { + memcpy(dht->to_bootstrap[*num].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); + dht->to_bootstrap[*num].ip_port = ip_port; + ++*num; + } else { + // TODO(irungentoo): ipv6 vs v4 + add_to_list(dht->to_bootstrap, MAX_CLOSE_TO_BOOTSTRAP_NODES, public_key, ip_port, dht->self_public_key); + } } } for (uint32_t i = 0; i < dht->num_friends; ++i) { - bool store_ok = 0; - DHT_Friend *dht_friend = &dht->friends_list[i]; + bool store_ok = false; + if (store_node_ok(&dht_friend->client_list[1], public_key, dht_friend->public_key)) { - store_ok = 1; + store_ok = true; } if (store_node_ok(&dht_friend->client_list[0], public_key, dht_friend->public_key)) { - store_ok = 1; + store_ok = true; } - unsigned int *friend_num = &dht_friend->num_to_bootstrap; + 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, public_key, ip_port); if (store_ok && index == UINT32_MAX && !pk_in_list) { if (*friend_num < MAX_SENT_NODES) { - Node_format *format = &dht_friend->to_bootstrap[*friend_num]; + Node_format *const format = &dht_friend->to_bootstrap[*friend_num]; memcpy(format->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); format->ip_port = ip_port; ++*friend_num; @@ -1040,7 +1128,7 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key); } - ret = 1; + ret = true; } } @@ -1057,9 +1145,9 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) uint32_t used = 0; /* convert IPv4-in-IPv6 to IPv4 */ - if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { ip_port.ip.family = TOX_AF_INET; - ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; + ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } /* NOTE: Current behavior if there are two clients with the same id is @@ -1073,7 +1161,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) used++; } - DHT_Friend *friend_foundip = 0; + DHT_Friend *friend_foundip = nullptr; for (uint32_t i = 0; i < dht->num_friends; ++i) { const bool in_list = client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list, @@ -1108,14 +1196,14 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, const uint8_t *pk) { - uint64_t temp_time = unix_time(); - uint32_t index = index_of_client_pk(array, size, pk); + const uint64_t temp_time = unix_time(); + const uint32_t index = index_of_client_pk(array, size, pk); if (index == UINT32_MAX) { return false; } - Client_data *data = &array[index]; + Client_data *const data = &array[index]; IPPTsPng *assoc; if (ip_port.ip.family == TOX_AF_INET) { @@ -1137,9 +1225,9 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) { /* convert IPv4-in-IPv6 to IPv4 */ - if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { ip_port.ip.family = TOX_AF_INET; - ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; + ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } if (id_equal(public_key, dht->self_public_key)) { @@ -1149,7 +1237,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke for (uint32_t i = 0; i < dht->num_friends; ++i) { if (id_equal(public_key, dht->friends_list[i].public_key)) { - Client_data *client_list = dht->friends_list[i].client_list; + Client_data *const client_list = dht->friends_list[i].client_list; if (update_client_data(client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key)) { return; @@ -1177,7 +1265,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint64_t ping_id = 0; - if (sendback_node != NULL) { + if (sendback_node != nullptr) { memcpy(plain_message + sizeof(receiver), sendback_node, sizeof(Node_format)); ping_id = ping_array_add(dht->dht_harden_ping_array, plain_message, sizeof(plain_message)); } else { @@ -1197,8 +1285,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; DHT_get_shared_key_sent(dht, shared_key, public_key); - int len = DHT_create_packet(dht->self_public_key, shared_key, NET_PACKET_GET_NODES, - plain, sizeof(plain), data); + const int len = DHT_create_packet(dht->self_public_key, shared_key, NET_PACKET_GET_NODES, + plain, sizeof(plain), data); if (len != sizeof(data)) { return -1; @@ -1220,17 +1308,17 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public return -1; } - size_t Node_format_size = sizeof(Node_format); + const size_t node_format_size = sizeof(Node_format); Node_format nodes_list[MAX_SENT_NODES]; - uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); + const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); - VLA(uint8_t, plain, 1 + Node_format_size * MAX_SENT_NODES + length); + VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); int nodes_length = 0; if (num_nodes) { - nodes_length = pack_nodes(plain + 1, Node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); + nodes_length = pack_nodes(plain + 1, node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); if (nodes_length <= 0) { return -1; @@ -1243,8 +1331,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE; VLA(uint8_t, data, 1 + nodes_length + length + crypto_size); - int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, - plain, 1 + nodes_length + length, data); + const int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, + plain, 1 + nodes_length + length, data); if (len != SIZEOF_VLA(data)) { return -1; @@ -1258,41 +1346,42 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) { if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) { - return 1; + return true; } - DHT *dht = (DHT *)object; + DHT *const dht = (DHT *)object; /* Check if packet is from ourself. */ if (id_equal(packet + 1, dht->self_public_key)) { - return 1; + return true; } uint8_t plain[CRYPTO_NODE_SIZE]; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; DHT_get_shared_key_recv(dht, shared_key, packet + 1); - int len = decrypt_data_symmetric( - shared_key, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, - CRYPTO_NODE_SIZE + CRYPTO_MAC_SIZE, - plain); + const int len = decrypt_data_symmetric( + shared_key, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, + CRYPTO_NODE_SIZE + CRYPTO_MAC_SIZE, + plain); if (len != CRYPTO_NODE_SIZE) { - return 1; + return true; } sendnodes_ipv6(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key); ping_add(dht->ping, packet + 1, source); - return 0; + return false; } -/* return 0 if no - return 1 if yes */ -static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port node_ip_port, uint64_t ping_id, - Node_format *sendback_node) + +/* return false if no + 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) { uint8_t data[sizeof(Node_format) * 2]; @@ -1301,17 +1390,17 @@ static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port } else if (ping_array_check(dht->dht_harden_ping_array, data, sizeof(data), ping_id) == sizeof(data)) { memcpy(sendback_node, data + sizeof(Node_format), sizeof(Node_format)); } else { - return 0; + return false; } Node_format test; memcpy(&test, data, sizeof(Node_format)); if (!ipport_equal(&test.ip_port, &node_ip_port) || !id_equal(test.public_key, public_key)) { - return 0; + return false; } - return 1; + return true; } /* Function is needed in following functions. */ @@ -1321,14 +1410,14 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *packet, uint16_t length, Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) { - DHT *dht = (DHT *)object; - uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE; + DHT *const dht = (DHT *)object; + const uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE; if (length < cid_size) { /* too short */ return 1; } - uint32_t data_size = length - cid_size; + const uint32_t data_size = length - cid_size; if (data_size == 0) { return 1; @@ -1341,12 +1430,12 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t)); uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; DHT_get_shared_key_sent(dht, shared_key, packet + 1); - int len = decrypt_data_symmetric( - shared_key, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, - packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, - 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, - plain); + const int len = decrypt_data_symmetric( + shared_key, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, + packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, + 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, + plain); if ((unsigned int)len != SIZEOF_VLA(plain)) { return 1; @@ -1366,7 +1455,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa } uint16_t length_nodes = 0; - int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); + const int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); if (length_nodes != data_size) { return 1; @@ -1391,7 +1480,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) { - DHT *dht = (DHT *)object; + DHT *const dht = (DHT *)object; Node_format plain_nodes[MAX_SENT_NODES]; uint32_t num_nodes; @@ -1419,12 +1508,12 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), void *data, int32_t number, uint16_t *lock_count) { - uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); + const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); uint16_t lock_num; if (friend_num != UINT32_MAX) { /* Is friend already in DHT? */ - DHT_Friend *dht_friend = &dht->friends_list[friend_num]; + DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; if (dht_friend->lock_count == DHT_FRIEND_MAX_LOCKS) { return -1; @@ -1443,14 +1532,14 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void return 0; } - DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); + DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); - if (temp == NULL) { + if (temp == nullptr) { return -1; } dht->friends_list = temp; - DHT_Friend *dht_friend = &dht->friends_list[dht->num_friends]; + DHT_Friend *const dht_friend = &dht->friends_list[dht->num_friends]; memset(dht_friend, 0, sizeof(DHT_Friend)); memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -1474,19 +1563,19 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) { - uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); + const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); if (friend_num == UINT32_MAX) { return -1; } - DHT_Friend *dht_friend = &dht->friends_list[friend_num]; + DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; --dht_friend->lock_count; if (dht_friend->lock_count && lock_count) { /* DHT friend is still in use.*/ --lock_count; - dht_friend->callbacks[lock_count].ip_callback = NULL; - dht_friend->callbacks[lock_count].data = NULL; + dht_friend->callbacks[lock_count].ip_callback = nullptr; + dht_friend->callbacks[lock_count].data = nullptr; dht_friend->callbacks[lock_count].number = 0; return 0; } @@ -1501,13 +1590,13 @@ int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) if (dht->num_friends == 0) { free(dht->friends_list); - dht->friends_list = NULL; + dht->friends_list = nullptr; return 0; } - DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); + DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); - if (temp == NULL) { + if (temp == nullptr) { return -1; } @@ -1521,24 +1610,24 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) ip_reset(&ip_port->ip); ip_port->port = 0; - uint32_t friend_index = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); + const uint32_t friend_index = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); if (friend_index == UINT32_MAX) { return -1; } - DHT_Friend *frnd = &dht->friends_list[friend_index]; - uint32_t client_index = index_of_client_pk(frnd->client_list, MAX_FRIEND_CLIENTS, public_key); + DHT_Friend *const frnd = &dht->friends_list[friend_index]; + const uint32_t client_index = index_of_client_pk(frnd->client_list, MAX_FRIEND_CLIENTS, public_key); if (client_index == -1) { return 0; } - Client_data *client = &frnd->client_list[client_index]; - IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + const Client_data *const client = &frnd->client_list[client_index]; + const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; for (size_t i = 0; i < ASSOC_COUNT; i++) { - IPPTsPng *assoc = assocs[i]; + const IPPTsPng *const assoc = assocs[i]; if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { *ip_port = assoc->ip_port; @@ -1554,13 +1643,13 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co Client_data *list, uint32_t list_count, uint32_t *bootstrap_times, bool sortable) { uint8_t not_kill = 0; - uint64_t temp_time = unix_time(); + const uint64_t temp_time = unix_time(); uint32_t num_nodes = 0; VLA(Client_data *, client_list, list_count * 2); VLA(IPPTsPng *, assoc_list, list_count * 2); unsigned int sort = 0; - bool sort_ok = 0; + bool sort_ok = false; for (uint32_t i = 0; i < list_count; i++) { /* If node is not dead. */ @@ -1568,15 +1657,15 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; - for (size_t i = 0; i < ASSOC_COUNT; i++) { - IPPTsPng *assoc = assocs[i]; + for (size_t j = 0; j < ASSOC_COUNT; j++) { + IPPTsPng *assoc = assocs[j]; if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { sort = 0; not_kill++; if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { - getnodes(dht, assoc->ip_port, client->public_key, public_key, NULL); + getnodes(dht, assoc->ip_port, client->public_key, public_key, nullptr); assoc->last_pinged = temp_time; } @@ -1590,8 +1679,8 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co ++sort; /* Timed out should be at beginning, if they are not, sort the list. */ - if (sort > 1 && sort < (((i + 1) * 2) - 1)) { - sort_ok = 1; + if (sort > 1 && sort < (((j + 1) * 2) - 1)) { + sort_ok = true; } } } @@ -1602,13 +1691,13 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co } if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { - uint32_t rand_node = rand() % (num_nodes); + uint32_t rand_node = rand() % num_nodes; if ((num_nodes - 1) != rand_node) { rand_node += rand() % (num_nodes - (rand_node + 1)); } - getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, NULL); + getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, nullptr); *lastgetnode = temp_time; ++*bootstrap_times; @@ -1623,11 +1712,11 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co static void do_DHT_friends(DHT *dht) { for (size_t i = 0; i < dht->num_friends; ++i) { - DHT_Friend *dht_friend = &dht->friends_list[i]; + DHT_Friend *const dht_friend = &dht->friends_list[i]; for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) { getnodes(dht, dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key, - NULL); + nullptr); } dht_friend->num_to_bootstrap = 0; @@ -1644,34 +1733,37 @@ static void do_DHT_friends(DHT *dht) static void do_Close(DHT *dht) { for (size_t i = 0; i < dht->num_to_bootstrap; ++i) { - getnodes(dht, dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key, NULL); + getnodes(dht, dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key, nullptr); } dht->num_to_bootstrap = 0; - uint8_t not_killed = do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->self_public_key, - dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times, 0); + uint8_t not_killed = do_ping_and_sendnode_requests( + dht, &dht->close_lastgetnodes, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times, + 0); + + if (not_killed != 0) { + return; + } - if (!not_killed) { - /* all existing nodes are at least KILL_NODE_TIMEOUT, - * which means we are mute, as we only send packets to - * nodes NOT in KILL_NODE_TIMEOUT - * - * so: reset all nodes to be BAD_NODE_TIMEOUT, but not - * KILL_NODE_TIMEOUT, so we at least keep trying pings */ - uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; + /* all existing nodes are at least KILL_NODE_TIMEOUT, + * which means we are mute, as we only send packets to + * nodes NOT in KILL_NODE_TIMEOUT + * + * so: reset all nodes to be BAD_NODE_TIMEOUT, but not + * KILL_NODE_TIMEOUT, so we at least keep trying pings */ + const uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; - for (size_t i = 0; i < LCLIENT_LIST; i++) { - Client_data *client = &dht->close_clientlist[i]; + for (size_t i = 0; i < LCLIENT_LIST; i++) { + Client_data *const client = &dht->close_clientlist[i]; - IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; - for (size_t j = 0; j < ASSOC_COUNT; j++) { - IPPTsPng *assoc = assocs[j]; + for (size_t j = 0; j < ASSOC_COUNT; j++) { + IPPTsPng *const assoc = assocs[j]; - if (assoc->timestamp) { - assoc->timestamp = badonly; - } + if (assoc->timestamp) { + assoc->timestamp = badonly; } } } @@ -1679,18 +1771,18 @@ static void do_Close(DHT *dht) void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id) { - getnodes(dht, *from_ipp, from_id, which_id, NULL); + getnodes(dht, *from_ipp, from_id, which_id, nullptr); } void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key) { - getnodes(dht, ip_port, public_key, dht->self_public_key, NULL); + getnodes(dht, ip_port, public_key, dht->self_public_key, nullptr); } int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, uint16_t port, const uint8_t *public_key) { IP_Port ip_port_v64; - IP *ip_extra = NULL; + IP *ip_extra = nullptr; IP_Port ip_port_v4; ip_init(&ip_port_v64.ip, ipv6enabled); @@ -1705,7 +1797,7 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable ip_port_v64.port = port; DHT_bootstrap(dht, ip_port_v64, public_key); - if ((ip_extra != NULL) && ip_isset(ip_extra)) { + if ((ip_extra != nullptr) && ip_isset(ip_extra)) { ip_port_v4.port = port; DHT_bootstrap(dht, ip_port_v4, public_key); } @@ -1724,11 +1816,11 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe { for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { if (id_equal(public_key, dht->close_clientlist[i].public_key)) { - const Client_data *client = &dht->close_clientlist[i]; - const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + const Client_data *const client = &dht->close_clientlist[i]; + const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; for (size_t j = 0; j < ASSOC_COUNT; j++) { - const IPPTsPng *assoc = assocs[j]; + const IPPTsPng *const assoc = assocs[j]; if (ip_isset(&assoc->ip_port.ip)) { return sendpacket(dht->net, assoc->ip_port, packet, length); @@ -1755,15 +1847,14 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n return -1; } - DHT_Friend *dht_friend = &dht->friends_list[friend_num]; - Client_data *client; + const DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; IP_Port ipv4s[MAX_FRIEND_CLIENTS]; int num_ipv4s = 0; IP_Port ipv6s[MAX_FRIEND_CLIENTS]; int num_ipv6s = 0; for (size_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - client = &dht_friend->client_list[i]; + const Client_data *const client = &dht_friend->client_list[i]; /* If ip is not zero and node is good. */ if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { @@ -1824,7 +1915,7 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n */ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) { - uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); + const uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); if (num == UINT32_MAX) { return 0; @@ -1834,14 +1925,13 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0}; IP_Port ip_list[MAX_FRIEND_CLIENTS]; - int ip_num = friend_iplist(dht, ip_list, num); + const int ip_num = friend_iplist(dht, ip_list, num); if (ip_num < (MAX_FRIEND_CLIENTS / 4)) { return 0; /* Reason for that? */ } - DHT_Friend *dht_friend = &dht->friends_list[num]; - Client_data *client; + const DHT_Friend *const dht_friend = &dht->friends_list[num]; /* extra legwork, because having the outside allocating the space for us * is *usually* good(tm) (bites us in the behind in this case though) */ @@ -1851,16 +1941,15 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack continue; } - client = &dht_friend->client_list[i]; - - const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; + const Client_data *const client = &dht_friend->client_list[i]; + const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; for (size_t j = 0; j < ASSOC_COUNT; j++) { - const IPPTsPng *assoc = assocs[j]; + const IPPTsPng *const assoc = assocs[j]; /* If ip is not zero and node is good. */ if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { - int retval = sendpacket(dht->net, assoc->ip_port, packet, length); + const int retval = sendpacket(dht->net, assoc->ip_port, packet, length); if ((unsigned int)retval == length) { ++sent; @@ -1879,14 +1968,13 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack */ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) { - uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); + const uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); if (num == UINT32_MAX) { return 0; } - DHT_Friend *dht_friend = &dht->friends_list[num]; - Client_data *client; + const DHT_Friend *const dht_friend = &dht->friends_list[num]; IP_Port ip_list[MAX_FRIEND_CLIENTS * 2]; int n = 0; @@ -1895,9 +1983,8 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * * is *usually* good(tm) (bites us in the behind in this case though) */ for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - client = &dht_friend->client_list[i]; - - const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; + const Client_data *const client = &dht_friend->client_list[i]; + const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; for (size_t j = 0; j < ASSOC_COUNT; j++) { const IPPTsPng *assoc = assocs[j]; @@ -1914,7 +2001,7 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * return 0; } - int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); + const int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); if ((unsigned int)retval == length) { return 1; @@ -1936,8 +2023,9 @@ static int send_NATping(DHT *dht, const uint8_t *public_key, uint64_t ping_id, u data[0] = type; memcpy(data + 1, &ping_id, sizeof(uint64_t)); /* 254 is NAT ping request packet id */ - int len = create_request(dht->self_public_key, dht->self_secret_key, packet, public_key, data, - sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); + const int len = create_request( + dht->self_public_key, dht->self_secret_key, packet, public_key, data, + sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); if (len == -1) { return -1; @@ -1964,7 +2052,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu return 1; } - DHT *dht = (DHT *)object; + DHT *const dht = (DHT *)object; uint64_t ping_id; memcpy(&ping_id, packet + 1, sizeof(uint64_t)); @@ -1974,7 +2062,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu return 1; } - DHT_Friend *dht_friend = &dht->friends_list[friendnumber]; + DHT_Friend *const dht_friend = &dht->friends_list[friendnumber]; if (packet[0] == NAT_PING_REQUEST) { /* 1 is reply */ @@ -2056,7 +2144,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, return; } - uint16_t first_port = port_list[0]; + const uint16_t first_port = port_list[0]; uint32_t i; for (i = 0; i < numports; ++i) { @@ -2073,11 +2161,11 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, } else { for (i = 0; i < MAX_PUNCHING_PORTS; ++i) { /* TODO(irungentoo): Improve port guessing algorithm. */ - uint32_t it = i + dht->friends_list[friend_num].nat.punching_index; - int8_t sign = (it % 2) ? -1 : 1; - uint32_t delta = sign * (it / (2 * numports)); - uint32_t index = (it / 2) % numports; - uint16_t port = port_list[index] + delta; + const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index; + const int8_t sign = (it % 2) ? -1 : 1; + const uint32_t delta = sign * (it / (2 * numports)); + const uint32_t index = (it / 2) % numports; + const uint16_t port = port_list[index] + delta; IP_Port pinging; ip_copy(&pinging.ip, &ip); pinging.port = net_htons(port); @@ -2088,7 +2176,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, } if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { - uint16_t port = 1024; + const uint16_t port = 1024; IP_Port pinging; ip_copy(&pinging.ip, &ip); @@ -2106,11 +2194,11 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, static void do_NAT(DHT *dht) { - uint64_t temp_time = unix_time(); + const uint64_t temp_time = unix_time(); for (uint32_t i = 0; i < dht->num_friends; ++i) { IP_Port ip_list[MAX_FRIEND_CLIENTS]; - int num = friend_iplist(dht, ip_list, i); + const int num = friend_iplist(dht, ip_list, i); /* If already connected or friend is not online don't try to hole punch. */ if (num < MAX_FRIEND_CLIENTS / 2) { @@ -2126,7 +2214,7 @@ static void do_NAT(DHT *dht) dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time && dht->friends_list[i].nat.recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { - IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); + const IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); if (!ip_isset(&ip)) { continue; @@ -2139,7 +2227,7 @@ static void do_NAT(DHT *dht) } uint16_t port_list[MAX_FRIEND_CLIENTS]; - uint16_t numports = NAT_getports(port_list, ip_list, num, ip); + const uint16_t numports = NAT_getports(port_list, ip_list, num, ip); punch_holes(dht, ip, port_list, numports, i); dht->friends_list[i].nat.punching_timestamp = temp_time; @@ -2151,14 +2239,17 @@ static void do_NAT(DHT *dht) /*----------------------------------------------------------------------------------*/ /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ +#define DHT_HARDENING 0 #define HARDREQ_DATA_SIZE 384 /* Attempt to prevent amplification/other attacks*/ -#define CHECK_TYPE_ROUTE_REQ 0 -#define CHECK_TYPE_ROUTE_RES 1 -#define CHECK_TYPE_GETNODE_REQ 2 -#define CHECK_TYPE_GETNODE_RES 3 -#define CHECK_TYPE_TEST_REQ 4 -#define CHECK_TYPE_TEST_RES 5 +enum { + CHECK_TYPE_ROUTE_REQ = 0, + CHECK_TYPE_ROUTE_RES = 1, + CHECK_TYPE_GETNODE_REQ = 2, + CHECK_TYPE_GETNODE_RES = 3, + CHECK_TYPE_TEST_REQ = 4, + CHECK_TYPE_TEST_RES = 5, +}; #if DHT_HARDENING static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) @@ -2171,8 +2262,9 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8 uint8_t data[HARDREQ_DATA_SIZE] = {0}; data[0] = type; memcpy(data + 1, contents, length); - int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, - sizeof(data), CRYPTO_PACKET_HARDENING); + const int len = create_request( + dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, + data, sizeof(data), CRYPTO_PACKET_HARDENING); if (len == -1) { return -1; @@ -2204,8 +2296,9 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, data[0] = CHECK_TYPE_GETNODE_RES; memcpy(data + 1, queried_client_id, CRYPTO_PUBLIC_KEY_SIZE); memcpy(data + 1 + CRYPTO_PUBLIC_KEY_SIZE, nodes_data, nodes_data_length); - int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, - SIZEOF_VLA(data), CRYPTO_PACKET_HARDENING); + const int len = create_request( + dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, + data, SIZEOF_VLA(data), CRYPTO_PACKET_HARDENING); if (len == -1) { return -1; @@ -2231,7 +2324,7 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Fam } } - return NULL; + return nullptr; } /* @@ -2248,7 +2341,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num) continue; } - IPPTsPng *temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family); + const IPPTsPng *const temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family); if (temp) { if (!is_timeout(temp->timestamp, BAD_NODE_TIMEOUT)) { @@ -2262,13 +2355,12 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num) /* Interval in seconds between hardening checks */ #define HARDENING_INTERVAL 120 -#define HARDEN_TIMEOUT 1200 /* Handle a received hardening packet */ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length, void *userdata) { - DHT *dht = (DHT *)object; + DHT *const dht = (DHT *)object; if (length < 2) { return 1; @@ -2303,7 +2395,8 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ uint16_t length_nodes = length - 1 - CRYPTO_PUBLIC_KEY_SIZE; Node_format nodes[MAX_SENT_NODES]; - int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length_nodes, 0); + const int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, + 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..) */ @@ -2316,9 +2409,9 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ return 1; } - IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); + IPPTsPng *const temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); - if (temp == NULL) { + if (temp == nullptr) { return 1; } @@ -2340,6 +2433,8 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ } #if DHT_HARDENING +#define HARDEN_TIMEOUT 1200 + /* Return a random node from all the nodes we are connected to. * TODO(irungentoo): improve this function. */ @@ -2348,13 +2443,13 @@ static Node_format random_node(DHT *dht, Family sa_family) uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ - uint32_t t = rand(); + const uint32_t t = rand(); memcpy(id + i * sizeof(t), &t, sizeof(t)); } Node_format nodes_list[MAX_SENT_NODES]; memset(nodes_list, 0, sizeof(nodes_list)); - uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); + const uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); if (num_nodes == 0) { return nodes_list[0]; @@ -2377,21 +2472,21 @@ static uint16_t list_nodes(Client_data *list, size_t length, Node_format *nodes, uint16_t count = 0; for (size_t i = length; i != 0; --i) { - IPPTsPng *assoc = NULL; + const IPPTsPng *assoc = nullptr; if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { assoc = &list[i - 1].assoc4; } if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { - if (assoc == NULL) { + if (assoc == nullptr) { assoc = &list[i - 1].assoc6; } else if (rand() % 2) { assoc = &list[i - 1].assoc6; } } - if (assoc != NULL) { + if (assoc != nullptr) { memcpy(nodes[count].public_key, list[i - 1].public_key, CRYPTO_PUBLIC_KEY_SIZE); nodes[count].ip_port = assoc->ip_port; ++count; @@ -2416,7 +2511,7 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) } uint16_t count = 0; - unsigned int r = rand(); + const unsigned int r = rand(); 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, nodes + count, @@ -2443,9 +2538,9 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) static void do_hardening(DHT *dht) { for (uint32_t i = 0; i < LCLIENT_LIST * 2; ++i) { - IPPTsPng *cur_iptspng; + IPPTsPng *cur_iptspng; Family sa_family; - uint8_t *public_key = dht->close_clientlist[i / 2].public_key; + const uint8_t *const public_key = dht->close_clientlist[i / 2].public_key; if (i % 2 == 0) { cur_iptspng = &dht->close_clientlist[i / 2].assoc4; @@ -2502,7 +2597,7 @@ void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_c static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) { - DHT *dht = (DHT *)object; + DHT *const dht = (DHT *)object; assert(packet[0] == NET_PACKET_CRYPTO); @@ -2516,7 +2611,8 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t data[MAX_CRYPTO_REQUEST_SIZE]; uint8_t number; - int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length); + const int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, + data, &number, packet, length); if (len == -1 || len == 0) { return 1; @@ -2526,12 +2622,13 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack return 1; } - return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, - data, len, userdata); + return dht->cryptopackethandlers[number].function( + dht->cryptopackethandlers[number].object, source, public_key, + data, len, userdata); } /* If request is not for us, try routing it. */ - int retval = route_packet(dht, packet + 1, packet, length); + const int retval = route_packet(dht, packet + 1, packet, length); if ((unsigned int)retval == length) { return 0; @@ -2547,14 +2644,14 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) /* init time */ unix_time_update(); - if (net == NULL) { - return NULL; + if (net == nullptr) { + return nullptr; } - DHT *dht = (DHT *)calloc(1, sizeof(DHT)); + DHT *const dht = (DHT *)calloc(1, sizeof(DHT)); - if (dht == NULL) { - return NULL; + if (dht == nullptr) { + return nullptr; } dht->log = log; @@ -2564,9 +2661,9 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) dht->ping = ping_new(dht); - if (dht->ping == NULL) { + if (dht->ping == nullptr) { kill_DHT(dht); - return NULL; + return nullptr; } networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); @@ -2575,7 +2672,6 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, &handle_hardening, dht); - new_symmetric_key(dht->secret_symmetric_key); crypto_new_keypair(dht->self_public_key, dht->self_secret_key); dht->dht_ping_array = ping_array_new(DHT_PING_ARRAY_SIZE, PING_TIMEOUT); @@ -2585,9 +2681,9 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) uint8_t random_key_bytes[CRYPTO_PUBLIC_KEY_SIZE]; random_bytes(random_key_bytes, sizeof(random_key_bytes)); - if (DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0) != 0) { + if (DHT_addfriend(dht, random_key_bytes, nullptr, nullptr, 0, nullptr) != 0) { kill_DHT(dht); - return NULL; + return nullptr; } } @@ -2616,12 +2712,13 @@ void do_DHT(DHT *dht) #endif dht->last_run = unix_time(); } + void kill_DHT(DHT *dht) { - networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); - networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); - cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); - cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); + networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr); + networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, nullptr, nullptr); + cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr); + cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, nullptr, nullptr); ping_array_kill(dht->dht_ping_array); ping_array_kill(dht->dht_harden_ping_array); ping_kill(dht->ping); @@ -2642,7 +2739,8 @@ void kill_DHT(DHT *dht) /* Get the size of the DHT (for saving). */ uint32_t DHT_size(const DHT *dht) { - uint32_t numv4 = 0, numv6 = 0; + uint32_t numv4 = 0; + uint32_t numv6 = 0; for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0); @@ -2650,7 +2748,7 @@ uint32_t DHT_size(const DHT *dht) } for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { - DHT_Friend *fr = &dht->friends_list[i]; + const DHT_Friend *const fr = &dht->friends_list[i]; for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { numv4 += (fr->client_list[j].assoc4.timestamp != 0); @@ -2658,7 +2756,8 @@ uint32_t DHT_size(const DHT *dht) } } - uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; + const uint32_t size32 = sizeof(uint32_t); + const uint32_t sizesubhead = size32 * 2; return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * numv6); } @@ -2674,12 +2773,12 @@ static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type) /* Save the DHT in data where data is an array of size DHT_size(). */ -void DHT_save(DHT *dht, uint8_t *data) +void DHT_save(const DHT *dht, uint8_t *data) { host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL); data += sizeof(uint32_t); - uint8_t *old_data = data; + uint8_t *const old_data = data; /* get right offset. we write the actual header later. */ data = DHT_save_subheader(data, 0, 0); @@ -2703,7 +2802,7 @@ void DHT_save(DHT *dht, uint8_t *data) } for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { - DHT_Friend *fr = &dht->friends_list[i]; + const DHT_Friend *const fr = &dht->friends_list[i]; for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { if (fr->client_list[j].assoc4.timestamp != 0) { @@ -2729,7 +2828,7 @@ void DHT_save(DHT *dht, uint8_t *data) /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ int DHT_connect_after_load(DHT *dht) { - if (dht == NULL) { + if (dht == nullptr) { return -1; } @@ -2740,13 +2839,13 @@ int DHT_connect_after_load(DHT *dht) /* DHT is connected, stop. */ if (DHT_non_lan_connected(dht)) { free(dht->loaded_nodes_list); - dht->loaded_nodes_list = NULL; + dht->loaded_nodes_list = nullptr; dht->loaded_num_nodes = 0; return 0; } for (uint32_t i = 0; i < dht->loaded_num_nodes && i < SAVE_BOOTSTAP_FREQUENCY; ++i) { - unsigned int index = dht->loaded_nodes_index % dht->loaded_num_nodes; + const unsigned int index = dht->loaded_nodes_index % dht->loaded_num_nodes; DHT_bootstrap(dht, dht->loaded_nodes_list[index].ip_port, dht->loaded_nodes_list[index].public_key); ++dht->loaded_nodes_index; } @@ -2759,26 +2858,25 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le DHT *dht = (DHT *)outer; switch (type) { - case DHT_STATE_TYPE_NODES: + case DHT_STATE_TYPE_NODES: { if (length == 0) { break; } - { - free(dht->loaded_nodes_list); - // Copy to loaded_clients_list - dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); + free(dht->loaded_nodes_list); + // Copy to loaded_clients_list + dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); - int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, NULL, data, length, 0); + const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, 0); - if (num > 0) { - dht->loaded_num_nodes = num; - } else { - dht->loaded_num_nodes = 0; - } - } /* localize declarations */ + if (num > 0) { + dht->loaded_num_nodes = num; + } else { + dht->loaded_num_nodes = 0; + } break; + } default: LOGGER_ERROR(dht->log, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", @@ -2796,7 +2894,7 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le */ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) { - uint32_t cookie_len = sizeof(uint32_t); + const uint32_t cookie_len = sizeof(uint32_t); if (length > cookie_len) { uint32_t data32; @@ -2811,43 +2909,43 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) return -1; } -/* return 0 if we are not connected to the DHT. - * return 1 if we are. +/* return false if we are not connected to the DHT. + * return true if we are. */ -int DHT_isconnected(const DHT *dht) +bool DHT_isconnected(const DHT *dht) { unix_time_update(); for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { - const Client_data *client = &dht->close_clientlist[i]; + const Client_data *const client = &dht->close_clientlist[i]; if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { - return 1; + return true; } } - return 0; + return false; } -/* return 0 if we are not connected or only connected to lan peers with the DHT. - * return 1 if we are. +/* return false if we are not connected or only connected to lan peers with the DHT. + * return true if we are. */ -int DHT_non_lan_connected(const DHT *dht) +bool DHT_non_lan_connected(const DHT *dht) { unix_time_update(); for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { - const Client_data *client = &dht->close_clientlist[i]; + const Client_data *const client = &dht->close_clientlist[i]; if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc4.ip_port.ip) == -1) { - return 1; + return true; } if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc6.ip_port.ip) == -1) { - return 1; + return true; } } - return 0; + return false; } diff --git a/protocols/Tox/libtox/src/toxcore/DHT.h b/protocols/Tox/libtox/src/toxcore/DHT.h index 8fdd80b04d..284aa8c02f 100644 --- a/protocols/Tox/libtox/src/toxcore/DHT.h +++ b/protocols/Tox/libtox/src/toxcore/DHT.h @@ -240,42 +240,22 @@ typedef struct { } Cryptopacket_Handles; #define DHT_DEFINED -typedef struct DHT { - Logger *log; - Networking_Core *net; +typedef struct DHT DHT; - bool hole_punching_enabled; +const uint8_t *dht_get_self_public_key(const DHT *dht); +const uint8_t *dht_get_self_secret_key(const DHT *dht); +void dht_set_self_public_key(DHT *dht, const uint8_t *key); +void dht_set_self_secret_key(DHT *dht, const uint8_t *key); - Client_data close_clientlist[LCLIENT_LIST]; - uint64_t close_lastgetnodes; - uint32_t close_bootstrap_times; +Networking_Core *dht_get_net(const DHT *dht); +struct Ping *dht_get_ping(const DHT *dht); +const Client_data *dht_get_close_clientlist(const DHT *dht); +const Client_data *dht_get_close_client(const DHT *dht, uint32_t client_num); +uint16_t dht_get_num_friends(const DHT *dht); - /* Note: this key should not be/is not used to transmit any sensitive materials */ - uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; - /* DHT keypair */ - uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; +DHT_Friend *dht_get_friend(DHT *dht, uint32_t friend_num); +const uint8_t *dht_get_friend_public_key(const DHT *dht, uint32_t friend_num); - DHT_Friend *friends_list; - uint16_t num_friends; - - Node_format *loaded_nodes_list; - uint32_t loaded_num_nodes; - unsigned int loaded_nodes_index; - - Shared_Keys shared_keys_recv; - Shared_Keys shared_keys_sent; - - struct Ping *ping; - Ping_Array *dht_ping_array; - Ping_Array *dht_harden_ping_array; - uint64_t last_run; - - Cryptopacket_Handles cryptopackethandlers[256]; - - Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES]; - unsigned int num_to_bootstrap; -} DHT; /*----------------------------------------------------------------------------------*/ /* Shared key generations are costly, it is therefor smart to store commonly used @@ -433,7 +413,7 @@ void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_c uint32_t DHT_size(const DHT *dht); /* Save the DHT in data where data is an array of size DHT_size(). */ -void DHT_save(DHT *dht, uint8_t *data); +void DHT_save(const DHT *dht, uint8_t *data); /* Load the DHT from data of size size. * @@ -447,15 +427,15 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled); void kill_DHT(DHT *dht); -/* return 0 if we are not connected to the DHT. - * return 1 if we are. +/* return false if we are not connected to the DHT. + * return true if we are. */ -int DHT_isconnected(const DHT *dht); +bool DHT_isconnected(const DHT *dht); -/* return 0 if we are not connected or only connected to lan peers with the DHT. - * return 1 if we are. +/* return false if we are not connected or only connected to lan peers with the DHT. + * return true if we are. */ -int DHT_non_lan_connected(const DHT *dht); +bool DHT_non_lan_connected(const DHT *dht); uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key); diff --git a/protocols/Tox/libtox/src/toxcore/LAN_discovery.c b/protocols/Tox/libtox/src/toxcore/LAN_discovery.c index b5cf2c525b..70b71fea02 100644 --- a/protocols/Tox/libtox/src/toxcore/LAN_discovery.c +++ b/protocols/Tox/libtox/src/toxcore/LAN_discovery.c @@ -29,12 +29,6 @@ #include "util.h" -/* Used for get_broadcast(). */ -#ifdef __linux -#include <linux/netdevice.h> -#include <sys/ioctl.h> -#endif - #define MAX_INTERFACES 16 @@ -52,7 +46,7 @@ static void fetch_broadcast_info(uint16_t port) IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO); - if (pAdapterInfo == NULL) { + if (pAdapterInfo == nullptr) { return; } @@ -60,7 +54,7 @@ static void fetch_broadcast_info(uint16_t port) free(pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen); - if (pAdapterInfo == NULL) { + if (pAdapterInfo == nullptr) { return; } } @@ -85,9 +79,9 @@ static void fetch_broadcast_info(uint16_t port) if (gateway.family == TOX_AF_INET && subnet_mask.family == TOX_AF_INET) { IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = TOX_AF_INET; - uint32_t gateway_ip = net_ntohl(gateway.ip4.uint32), subnet_ip = net_ntohl(subnet_mask.ip4.uint32); + uint32_t gateway_ip = net_ntohl(gateway.ip.v4.uint32), subnet_ip = net_ntohl(subnet_mask.ip.v4.uint32); uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1; - ip_port->ip.ip4.uint32 = net_htonl(broadcast_ip); + ip_port->ip.ip.v4.uint32 = net_htonl(broadcast_ip); ip_port->port = port; count++; @@ -112,7 +106,17 @@ static void fetch_broadcast_info(uint16_t port) } } -#elif defined(__linux__) +#elif defined(__linux__) || defined(__FreeBSD__) + +#ifdef __linux__ +#include <linux/netdevice.h> +#endif + +#ifdef __FreeBSD__ +#include <net/if.h> +#endif + +#include <sys/ioctl.h> static void fetch_broadcast_info(uint16_t port) { @@ -121,9 +125,9 @@ static void fetch_broadcast_info(uint16_t port) * Definitely won't work like this on Windows... */ broadcast_count = 0; - Socket sock = 0; + const Socket sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0); - if ((sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0)) < 0) { + if (sock < 0) { return; } @@ -152,9 +156,9 @@ static void fetch_broadcast_info(uint16_t port) * a larger array, not done (640kB and 16 interfaces shall be * enough, for everybody!) */ - int i, n = ifconf.ifc_len / sizeof(struct ifreq); + int n = ifconf.ifc_len / sizeof(struct ifreq); - for (i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { /* there are interfaces with are incapable of broadcast */ if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { continue; @@ -173,9 +177,9 @@ static void fetch_broadcast_info(uint16_t port) IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = TOX_AF_INET; - ip_port->ip.ip4.uint32 = sock4->sin_addr.s_addr; + ip_port->ip.ip.v4.uint32 = sock4->sin_addr.s_addr; - if (ip_port->ip.ip4.uint32 == 0) { + if (ip_port->ip.ip.v4.uint32 == 0) { continue; } @@ -217,9 +221,7 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8 return 0; } - int i; - - for (i = 0; i < broadcast_count; i++) { + for (int i = 0; i < broadcast_count; i++) { sendpacket(net, broadcast_ip_ports[i], data, length); } @@ -238,17 +240,17 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ /* FE80::*: MUST be exact, for that we would need to look over all * interfaces and check in which status they are */ - ip.ip6.uint8[ 0] = 0xFF; - ip.ip6.uint8[ 1] = 0x02; - ip.ip6.uint8[15] = 0x01; + ip.ip.v6.uint8[ 0] = 0xFF; + ip.ip.v6.uint8[ 1] = 0x02; + ip.ip.v6.uint8[15] = 0x01; } else if (family_broadcast == TOX_AF_INET) { ip.family = TOX_AF_INET6; - ip.ip6 = IP6_BROADCAST; + ip.ip.v6 = IP6_BROADCAST; } } else if (family_socket == TOX_AF_INET) { if (family_broadcast == TOX_AF_INET) { ip.family = TOX_AF_INET; - ip.ip4 = IP4_BROADCAST; + ip.ip.v4 = IP4_BROADCAST; } } @@ -259,7 +261,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) bool ip_is_local(IP ip) { if (ip.family == TOX_AF_INET) { - IP4 ip4 = ip.ip4; + IP4 ip4 = ip.ip.v4; /* Loopback. */ if (ip4.uint8[0] == 127) { @@ -267,15 +269,15 @@ bool ip_is_local(IP ip) } } else { /* embedded IPv4-in-IPv6 */ - if (IPV6_IPV4_IN_V6(ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; ip4.family = TOX_AF_INET; - ip4.ip4.uint32 = ip.ip6.uint32[3]; + ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_local(ip4); } /* localhost in IPv6 (::1) */ - if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == net_htonl(1)) { + if (ip.ip.v6.uint64[0] == 0 && ip.ip.v6.uint32[2] == 0 && ip.ip.v6.uint32[3] == net_htonl(1)) { return 1; } } @@ -293,7 +295,7 @@ int ip_is_lan(IP ip) } if (ip.family == TOX_AF_INET) { - IP4 ip4 = ip.ip4; + IP4 ip4 = ip.ip.v4; /* 10.0.0.0 to 10.255.255.255 range. */ if (ip4.uint8[0] == 10) { @@ -325,16 +327,16 @@ int ip_is_lan(IP ip) /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ - if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) || - ((ip.ip6.uint8[0] == 0xFE) && ((ip.ip6.uint8[1] & 0xC0) == 0x80))) { + 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 0; } /* embedded IPv4-in-IPv6 */ - if (IPV6_IPV4_IN_V6(ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; ip4.family = TOX_AF_INET; - ip4.ip4.uint32 = ip.ip6.uint32[3]; + ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_lan(ip4); } } @@ -346,6 +348,9 @@ static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *pack { DHT *dht = (DHT *)object; + char ip_str[IP_NTOA_LEN] = { 0 }; + ip_ntoa(&source.ip, ip_str, sizeof(ip_str)); + if (ip_is_lan(source.ip) == -1) { return 1; } @@ -354,10 +359,6 @@ static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *pack return 1; } - char ip_str[IP_NTOA_LEN] = { 0 }; - ip_ntoa(&source.ip, ip_str, sizeof(ip_str)); - LOGGER_DEBUG(dht->log, "Found node in LAN: %s", ip_str); - DHT_bootstrap(dht, source, packet + 1); return 0; } @@ -367,30 +368,30 @@ int lan_discovery_send(uint16_t port, DHT *dht) { uint8_t data[CRYPTO_PUBLIC_KEY_SIZE + 1]; data[0] = NET_PACKET_LAN_DISCOVERY; - id_copy(data + 1, dht->self_public_key); + id_copy(data + 1, dht_get_self_public_key(dht)); - send_broadcasts(dht->net, port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE); + send_broadcasts(dht_get_net(dht), port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE); int res = -1; IP_Port ip_port; ip_port.port = port; /* IPv6 multicast */ - if (net_family(dht->net) == TOX_AF_INET6) { + if (net_family(dht_get_net(dht)) == TOX_AF_INET6) { ip_port.ip = broadcast_ip(TOX_AF_INET6, TOX_AF_INET6); if (ip_isset(&ip_port.ip)) { - if (sendpacket(dht->net, ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) { + if (sendpacket(dht_get_net(dht), ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) { res = 1; } } } /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is TOX_AF_INET6 */ - ip_port.ip = broadcast_ip(net_family(dht->net), TOX_AF_INET); + ip_port.ip = broadcast_ip(net_family(dht_get_net(dht)), TOX_AF_INET); if (ip_isset(&ip_port.ip)) { - if (sendpacket(dht->net, ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) { + if (sendpacket(dht_get_net(dht), ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) { res = 1; } } @@ -401,10 +402,10 @@ int lan_discovery_send(uint16_t port, DHT *dht) void lan_discovery_init(DHT *dht) { - networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); + networking_registerhandler(dht_get_net(dht), NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); } void lan_discovery_kill(DHT *dht) { - networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL); + networking_registerhandler(dht_get_net(dht), NET_PACKET_LAN_DISCOVERY, nullptr, nullptr); } diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.c b/protocols/Tox/libtox/src/toxcore/Messenger.c index fcee455ccc..6e209f4db3 100644 --- a/protocols/Tox/libtox/src/toxcore/Messenger.c +++ b/protocols/Tox/libtox/src/toxcore/Messenger.c @@ -33,7 +33,6 @@ #include <assert.h> -static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata); static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, uint8_t congestion_control); @@ -57,13 +56,13 @@ static int realloc_friendlist(Messenger *m, uint32_t num) { if (num == 0) { free(m->friendlist); - m->friendlist = NULL; + m->friendlist = nullptr; return 0; } Friend *newfriendlist = (Friend *)realloc(m->friendlist, num * sizeof(Friend)); - if (newfriendlist == NULL) { + if (newfriendlist == nullptr) { return -1; } @@ -322,8 +321,8 @@ static int clear_receipts(Messenger *m, int32_t friendnumber) receipts = temp_r; } - m->friendlist[friendnumber].receipts_start = NULL; - m->friendlist[friendnumber].receipts_end = NULL; + m->friendlist[friendnumber].receipts_start = nullptr; + m->friendlist[friendnumber].receipts_end = nullptr; return 0; } @@ -349,7 +348,7 @@ static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, } m->friendlist[friendnumber].receipts_end = new_receipts; - new_receipts->next = NULL; + new_receipts->next = nullptr; return 0; } /* @@ -393,7 +392,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) } if (!m->friendlist[friendnumber].receipts_start) { - m->friendlist[friendnumber].receipts_end = NULL; + m->friendlist[friendnumber].receipts_end = nullptr; } return 0; @@ -416,7 +415,8 @@ int m_delfriend(Messenger *m, int32_t friendnumber) clear_receipts(m, friendnumber); remove_request_received(m->fr, m->friendlist[friendnumber].real_pk); - friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, 0, 0, 0, 0, 0); + friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, nullptr, + nullptr, nullptr, nullptr, 0); if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) { send_offline_packet(m, m->friendlist[friendnumber].friendcon_id); @@ -488,8 +488,7 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber) int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length, uint32_t *message_id) { - /* MESSAGE_LAST itself is incorrect value */ - if (type >= MESSAGE_LAST) { + if (type > MESSAGE_ACTION) { return -5; } @@ -601,7 +600,7 @@ int setname(Messenger *m, const uint8_t *name, uint16_t length) */ uint16_t getself_name(const Messenger *m, uint8_t *name) { - if (name == NULL) { + if (name == nullptr) { return 0; } @@ -949,7 +948,7 @@ static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, ui } } -void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata) +static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata) { check_friend_connectionstatus(m, friendnumber, status, userdata); m->friendlist[friendnumber].status = status; @@ -1283,7 +1282,7 @@ int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, } } - if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, 0, 0)) { + if (send_file_control_packet(m, friendnumber, send_receive, file_number, control, nullptr, 0)) { if (control == FILECONTROL_KILL) { ft->status = FILESTATUS_NONE; @@ -1482,119 +1481,159 @@ uint64_t file_dataremaining(const Messenger *m, int32_t friendnumber, uint8_t fi return 0; } + const struct File_Transfers *const sending = &m->friendlist[friendnumber].file_sending[filenumber]; + if (send_receive == 0) { - if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) { + if (sending->status == FILESTATUS_NONE) { return 0; } - return m->friendlist[friendnumber].file_sending[filenumber].size - - m->friendlist[friendnumber].file_sending[filenumber].transferred; + return sending->size - sending->transferred; } - if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) { + const struct File_Transfers *const receiving = &m->friendlist[friendnumber].file_receiving[filenumber]; + + if (receiving->status == FILESTATUS_NONE) { return 0; } - return m->friendlist[friendnumber].file_receiving[filenumber].size - - m->friendlist[friendnumber].file_receiving[filenumber].transferred; + return receiving->size - receiving->transferred; } -static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata) +/** + * Iterate over all file transfers and request chunks (from the client) for each + * of them. + * + * The free_slots parameter is updated by this function. + * + * @param m Our messenger object. + * @param friendnumber The friend we're sending files to. + * @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 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) { - if (!m->friendlist[friendnumber].num_sending_files) { - return; - } + Friend *const friendcon = &m->friendlist[friendnumber]; + uint32_t num = friendcon->num_sending_files; - int free_slots = crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id)); + bool any_active_fts = false; - if (free_slots < MIN_SLOTS_FREE) { - free_slots = 0; - } else { - free_slots -= MIN_SLOTS_FREE; - } - - unsigned int i, num = m->friendlist[friendnumber].num_sending_files; - - for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { - struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i]; + // Iterate over all file transfers, including inactive ones. I.e. we always + // iterate exactly MAX_CONCURRENT_FILE_PIPES times. + for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { + struct File_Transfers *const ft = &friendcon->file_sending[i]; + // Any status other than NONE means the file transfer is active. if (ft->status != FILESTATUS_NONE) { + any_active_fts = true; --num; - if (ft->status == FILESTATUS_FINISHED) { - /* Check if file was entirely sent. */ - if (friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) { - if (m->file_reqchunk) { - (*m->file_reqchunk)(m, friendnumber, i, ft->transferred, 0, userdata); - } - - ft->status = FILESTATUS_NONE; - --m->friendlist[friendnumber].num_sending_files; + // 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); } - } - /* TODO(irungentoo): if file is too slow, switch to the next. */ - if (ft->slots_allocated > (unsigned int)free_slots) { - free_slots = 0; - } else { - free_slots -= ft->slots_allocated; + // Now it's inactive, we're no longer sending this. + ft->status = FILESTATUS_NONE; + --friendcon->num_sending_files; } + + // Decrease free slots by the number of slots this FT uses. + *free_slots = max_s32(0, (int32_t) * free_slots - ft->slots_allocated); } - while (ft->status == FILESTATUS_TRANSFERRING && (ft->paused == FILE_PAUSE_NOT)) { - if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, - m->friendlist[friendnumber].friendcon_id))) { - free_slots = 0; + 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) { - break; + if (*free_slots == 0) { + continue; } - uint16_t length = MAX_FILE_DATA_SIZE; - if (ft->size == 0) { /* Send 0 data to friend if file is 0 length. */ - file_data(m, friendnumber, i, 0, 0, 0); - break; + file_data(m, friendnumber, i, 0, nullptr, 0); + continue; } if (ft->size == ft->requested) { - break; - } - - if (ft->size - ft->requested < length) { - length = ft->size - ft->requested; + // This file transfer is done. + continue; } - ++ft->slots_allocated; + // Allocate 1 slot to this file transfer. + ft->slots_allocated++; - uint64_t position = ft->requested; + const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE); + const uint64_t position = ft->requested; ft->requested += length; if (m->file_reqchunk) { - (*m->file_reqchunk)(m, friendnumber, i, position, length, userdata); + m->file_reqchunk(m, friendnumber, i, position, length, userdata); } - --free_slots; + // The allocated slot is no longer free. + --*free_slots; } if (num == 0) { - break; + continue; } } + + return any_active_fts; +} + +static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata) +{ + // We're not currently doing any file transfers. + if (m->friendlist[friendnumber].num_sending_files == 0) { + return; + } + + // The number of packet slots left in the sendbuffer. + // This is a per friend count (CRYPTO_PACKET_BUFFER_SIZE). + uint32_t free_slots = crypto_num_free_sendqueue_slots( + m->net_crypto, + friend_connection_crypt_connection_id( + m->fr_c, + m->friendlist[friendnumber].friendcon_id)); + + // We keep MIN_SLOTS_FREE slots free for other packets, otherwise file + // 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. + 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++; + } } + /* Run this when the friend disconnects. * Kill all current file transfers. */ static void break_files(const Messenger *m, int32_t friendnumber) { - uint32_t i; - // TODO(irungentoo): Inform the client which file transfers get killed with a callback? - for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { + for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) { if (m->friendlist[friendnumber].file_sending[i].status != FILESTATUS_NONE) { m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_NONE; } @@ -1619,7 +1658,7 @@ static struct File_Transfers *get_file_transfer(uint8_t receive_send, uint8_t fi } if (ft->status == FILESTATUS_NONE) { - return NULL; + return nullptr; } return ft; @@ -1639,10 +1678,10 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv uint32_t real_filenumber; struct File_Transfers *ft = get_file_transfer(receive_send, filenumber, &real_filenumber, &m->friendlist[friendnumber]); - if (ft == NULL) { + if (ft == nullptr) { LOGGER_DEBUG(m->log, "file control (friend %d, file %d): file transfer does not exist; telling the other to kill it", friendnumber, filenumber); - send_file_control_packet(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, 0, 0); + send_file_control_packet(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, nullptr, 0); return -1; } @@ -1785,13 +1824,13 @@ static int m_handle_custom_lossy_packet(void *object, int friend_num, const uint return 1; } -void custom_lossy_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m, +void custom_lossy_packet_registerhandler(Messenger *m, void (*lossy_packethandler)(Messenger *m, uint32_t friendnumber, const uint8_t *data, size_t len, void *object)) { - m->lossy_packethandler = packet_handler_callback; + m->lossy_packethandler = lossy_packethandler; } -int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*packet_handler_callback)(Messenger *m, +int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int (*function)(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t len, void *object), void *object) { if (friend_not_valid(m, friendnumber)) { @@ -1806,8 +1845,7 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, int return -1; } - m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = - packet_handler_callback; + m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].function = function; m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_LOSSY_AV_RESERVED].object = object; return 0; } @@ -1867,10 +1905,10 @@ static int handle_custom_lossless_packet(void *object, int friend_num, const uin return 1; } -void custom_lossless_packet_registerhandler(Messenger *m, void (*packet_handler_callback)(Messenger *m, +void custom_lossless_packet_registerhandler(Messenger *m, void (*lossless_packethandler)(Messenger *m, uint32_t friendnumber, const uint8_t *data, size_t len, void *object)) { - m->lossless_packethandler = packet_handler_callback; + m->lossless_packethandler = lossless_packethandler; } int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length) @@ -1919,7 +1957,7 @@ static int friend_already_added(const uint8_t *real_pk, void *data) Messenger *new_messenger(Messenger_Options *options, unsigned int *error) { if (!options) { - return NULL; + return nullptr; } if (error) { @@ -1929,22 +1967,22 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); if (!m) { - return NULL; + return nullptr; } m->fr = friendreq_new(); if (!m->fr) { free(m); - return NULL; + return nullptr; } - Logger *log = NULL; + Logger *log = nullptr; if (options->log_callback) { log = logger_new(); - if (log != NULL) { + if (log != nullptr) { logger_callback_log(log, options->log_callback, m, options->log_user_data); } } @@ -1961,34 +1999,37 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err); } - if (m->net == NULL) { + if (m->net == nullptr) { friendreq_kill(m->fr); + logger_kill(m->log); free(m); if (error && net_err == 1) { *error = MESSENGER_ERROR_PORT; } - return NULL; + return nullptr; } m->dht = new_DHT(m->log, m->net, options->hole_punching_enabled); - if (m->dht == NULL) { + if (m->dht == nullptr) { kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } m->net_crypto = new_net_crypto(m->log, m->dht, &options->proxy_info); - if (m->net_crypto == NULL) { + if (m->net_crypto == nullptr) { kill_networking(m->net); kill_DHT(m->dht); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } m->onion = new_onion(m->dht); @@ -2005,14 +2046,16 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_DHT(m->dht); kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); - return NULL; + return nullptr; } if (options->tcp_server_port) { - m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, m->dht->self_secret_key, m->onion); + m->tcp_server = new_TCP_server(options->ipv6enabled, 1, &options->tcp_server_port, dht_get_self_secret_key(m->dht), + m->onion); - if (m->tcp_server == NULL) { + if (m->tcp_server == nullptr) { kill_friend_connections(m->fr_c); kill_onion(m->onion); kill_onion_announce(m->onion_a); @@ -2021,13 +2064,14 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) kill_DHT(m->dht); kill_networking(m->net); friendreq_kill(m->fr); + logger_kill(m->log); free(m); if (error) { *error = MESSENGER_ERROR_TCP_SERVER; } - return NULL; + return nullptr; } } @@ -2214,8 +2258,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le } case PACKET_ID_MESSAGE: // fall-through - case PACKET_ID_ACTION: - case PACKET_ID_CORRECTION: { + case PACKET_ID_ACTION: { if (data_length == 0) { break; } @@ -2257,10 +2300,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[0]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + uint64_t filesize; uint32_t file_type; uint16_t filename_length = data_length - head_length; @@ -2287,7 +2334,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH); VLA(uint8_t, filename_terminated, filename_length + 1); - uint8_t *filename = NULL; + uint8_t *filename = nullptr; if (filename_length) { /* Force NULL terminate file name. */ @@ -2317,10 +2364,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[1]; uint8_t control_type = data[2]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) { // TODO(iphydf): Do something different here? Right now, this // check is pointless. @@ -2337,10 +2388,14 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le uint8_t filenumber = data[0]; +#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES + if (filenumber >= MAX_CONCURRENT_FILE_PIPES) { break; } +#endif + struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber]; if (ft->status != FILESTATUS_TRANSFERRING) { @@ -2355,7 +2410,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le const uint8_t *file_data; if (file_data_length == 0) { - file_data = NULL; + file_data = nullptr; } else { file_data = data + 1; } @@ -2373,7 +2428,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le if (file_data_length && (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE)) { file_data_length = 0; - file_data = NULL; + file_data = nullptr; position = ft->transferred; /* Full file received. */ @@ -2467,7 +2522,7 @@ static void do_friends(Messenger *m, void *userdata) do_receipts(m, i, userdata); do_reqchunk_filecb(m, i, userdata); - m->friendlist[i].last_seen_time = (uint64_t) time(NULL); + m->friendlist[i].last_seen_time = (uint64_t) time(nullptr); } } } @@ -2543,7 +2598,7 @@ void do_messenger(Messenger *m, void *userdata) IP_Port local_ip_port; local_ip_port.port = m->options.tcp_server_port; local_ip_port.ip.family = TOX_AF_INET; - local_ip_port.ip.ip4 = get_ip4_loopback(); + local_ip_port.ip.ip.v4 = get_ip4_loopback(); add_tcp_relay(m->net_crypto, local_ip_port, tcp_server_public_key(m->tcp_server)); } @@ -2571,8 +2626,8 @@ void do_messenger(Messenger *m, void *userdata) uint32_t client, last_pinged; for (client = 0; client < LCLIENT_LIST; client++) { - Client_data *cptr = &m->dht->close_clientlist[client]; - IPPTsPng *assoc = NULL; + const Client_data *cptr = dht_get_close_client(m->dht, client); + const IPPTsPng *assoc = nullptr; uint32_t a; for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) { @@ -2597,7 +2652,7 @@ void do_messenger(Messenger *m, void *userdata) uint32_t friend_idx, dhtfriend; /* dht contains additional "friends" (requests) */ - uint32_t num_dhtfriends = m->dht->num_friends; + uint32_t num_dhtfriends = dht_get_num_friends(m->dht); VLA(int32_t, m2dht, num_dhtfriends); VLA(int32_t, dht2m, num_dhtfriends); @@ -2609,8 +2664,8 @@ void do_messenger(Messenger *m, void *userdata) continue; } - for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++) { - if (id_equal(m->friendlist[friend_idx].real_pk, m->dht->friends_list[dhtfriend].public_key)) { + for (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; } @@ -2623,8 +2678,8 @@ void do_messenger(Messenger *m, void *userdata) } } - if (m->numfriends != m->dht->num_friends) { - LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); + if (m->numfriends != dht_get_num_friends(m->dht)) { + LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u\n", dht_get_num_friends(m->dht), m->numfriends); } Friend *msgfptr; @@ -2634,10 +2689,10 @@ void do_messenger(Messenger *m, void *userdata) if (dht2m[friend_idx] >= 0) { msgfptr = &m->friendlist[dht2m[friend_idx]]; } else { - msgfptr = NULL; + msgfptr = nullptr; } - dhtfptr = &m->dht->friends_list[friend_idx]; + dhtfptr = dht_get_friend(m->dht, friend_idx); if (msgfptr) { char id_str[IDSTRING_LEN]; @@ -2652,7 +2707,7 @@ void do_messenger(Messenger *m, void *userdata) for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { Client_data *cptr = &dhtfptr->client_list[client]; - IPPTsPng *assoc = NULL; + IPPTsPng *assoc = nullptr; uint32_t a; for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6) { @@ -2708,13 +2763,13 @@ struct SAVED_FRIEND { uint64_t last_seen_time; }; -static uint32_t friend_size() +static uint32_t friend_size(void) { uint32_t data = 0; - const struct SAVED_FRIEND temp = { 0 }; + const struct SAVED_FRIEND *const temp = nullptr; -#define VALUE_MEMBER(NAME) data += sizeof(temp.NAME) -#define ARRAY_MEMBER(NAME) data += sizeof(temp.NAME) +#define VALUE_MEMBER(NAME) data += sizeof(temp->NAME) +#define ARRAY_MEMBER(NAME) data += sizeof(temp->NAME) // Exactly the same in friend_load, friend_save, and friend_size VALUE_MEMBER(status); @@ -3061,7 +3116,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 break; } - unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, 0, data, length, 1); + unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, 1); m->has_added_relays = 0; break; @@ -3074,7 +3129,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3 break; } - int i, num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, 0, data, length, 0); + int i, num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, nullptr, data, length, 0); for (i = 0; i < num; ++i) { onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].public_key); diff --git a/protocols/Tox/libtox/src/toxcore/Messenger.h b/protocols/Tox/libtox/src/toxcore/Messenger.h index a261a507db..402eb91650 100644 --- a/protocols/Tox/libtox/src/toxcore/Messenger.h +++ b/protocols/Tox/libtox/src/toxcore/Messenger.h @@ -37,14 +37,16 @@ /* This cannot be bigger than 256 */ #define MAX_CONCURRENT_FILE_PIPES 256 +#if !defined(__SPLINT__) && MAX_CONCURRENT_FILE_PIPES > UINT8_MAX + 1 +#error "uint8_t cannot represent all file transfer numbers" +#endif + #define FRIEND_ADDRESS_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) enum { MESSAGE_NORMAL, - MESSAGE_ACTION, - MESSAGE_CORRECTION, - MESSAGE_LAST + MESSAGE_ACTION }; /* NOTE: Packet ids below 24 must never be used. */ @@ -56,7 +58,6 @@ enum { #define PACKET_ID_TYPING 51 #define PACKET_ID_MESSAGE 64 #define PACKET_ID_ACTION (PACKET_ID_MESSAGE + MESSAGE_ACTION) /* 65 */ -#define PACKET_ID_CORRECTION (PACKET_ID_MESSAGE + MESSAGE_CORRECTION) /* 66 */ #define PACKET_ID_MSI 69 #define PACKET_ID_FILE_SENDREQUEST 80 #define PACKET_ID_FILE_CONTROL 81 @@ -73,13 +74,13 @@ enum { #define PACKET_LOSSY_AV_RESERVED 8 /* Number of lossy packet types at start of range reserved for A/V. */ typedef struct { - uint8_t ipv6enabled; - uint8_t udp_disabled; + bool ipv6enabled; + bool udp_disabled; TCP_Proxy_Info proxy_info; uint16_t port_range[2]; uint16_t tcp_server_port; - uint8_t hole_punching_enabled; + bool hole_punching_enabled; bool local_discovery_enabled; logger_cb *log_callback; @@ -205,7 +206,7 @@ typedef struct { uint64_t last_seen_time; uint8_t last_connection_udp_tcp; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; - unsigned int num_sending_files; + uint32_t num_sending_files; struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; struct { diff --git a/protocols/Tox/libtox/src/toxcore/TCP_client.c b/protocols/Tox/libtox/src/toxcore/TCP_client.c index b4092f803d..a6c32f9c64 100644 --- a/protocols/Tox/libtox/src/toxcore/TCP_client.c +++ b/protocols/Tox/libtox/src/toxcore/TCP_client.c @@ -110,9 +110,9 @@ void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object) { con->custom_object = object; } -void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint) +void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) { - con->custom_uint = uint; + con->custom_uint = value; } /* return 1 on success @@ -230,12 +230,12 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *TCP_ if (TCP_conn->ip_port.ip.family == TOX_AF_INET) { TCP_conn->last_packet[3] = 1; /* IPv4 address */ ++length; - memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip4.uint8, sizeof(IP4)); + memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip.v4.uint8, sizeof(IP4)); length += sizeof(IP4); } else { TCP_conn->last_packet[3] = 4; /* IPv6 address */ ++length; - memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip6.uint8, sizeof(IP6)); + memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip.v6.uint8, sizeof(IP6)); length += sizeof(IP6); } @@ -382,7 +382,7 @@ static int client_send_pending_data(TCP_Client_Connection *con) con->priority_queue_start = p; if (!p) { - con->priority_queue_end = NULL; + con->priority_queue_end = nullptr; return 0; } @@ -401,7 +401,7 @@ static bool client_add_priority(TCP_Client_Connection *con, const uint8_t *packe return 0; } - new_list->next = NULL; + new_list->next = nullptr; new_list->size = size; new_list->sent = sent; memcpy(new_list->data, packet, size); @@ -694,16 +694,16 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public const uint8_t *self_secret_key, TCP_Proxy_Info *proxy_info) { if (networking_at_startup() != 0) { - return NULL; + return nullptr; } if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { - return NULL; + return nullptr; } TCP_Proxy_Info default_proxyinfo; - if (proxy_info == NULL) { + if (proxy_info == nullptr) { default_proxyinfo.proxy_type = TCP_PROXY_NONE; proxy_info = &default_proxyinfo; } @@ -717,24 +717,24 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { - return NULL; + return nullptr; } if (!set_socket_nosigpipe(sock)) { kill_sock(sock); - return 0; + return nullptr; } if (!(set_socket_nonblock(sock) && connect_sock_to(sock, ip_port, proxy_info))) { kill_sock(sock); - return NULL; + return nullptr; } TCP_Client_Connection *temp = (TCP_Client_Connection *)calloc(sizeof(TCP_Client_Connection), 1); - if (temp == NULL) { + if (temp == nullptr) { kill_sock(sock); - return NULL; + return nullptr; } temp->sock = sock; @@ -761,7 +761,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public if (generate_handshake(temp) == -1) { kill_sock(sock); free(temp); - return NULL; + return nullptr; } break; @@ -1062,7 +1062,7 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection, void *userdata) */ void kill_TCP_connection(TCP_Client_Connection *TCP_connection) { - if (TCP_connection == NULL) { + if (TCP_connection == nullptr) { return; } diff --git a/protocols/Tox/libtox/src/toxcore/TCP_connection.c b/protocols/Tox/libtox/src/toxcore/TCP_connection.c index a43069da68..d10114f625 100644 --- a/protocols/Tox/libtox/src/toxcore/TCP_connection.c +++ b/protocols/Tox/libtox/src/toxcore/TCP_connection.c @@ -72,13 +72,28 @@ const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c) * return -1 if realloc fails. * return 0 if it succeeds. */ -#define realloc_tox_array(array, element_type, num, temp_pointer) \ - (num \ - ? (temp_pointer = (element_type *)realloc( \ - array, \ - (num) * sizeof(element_type)), \ - temp_pointer ? (array = temp_pointer, 0) : -1) \ - : (free(array), array = NULL, 0)) +#define MAKE_REALLOC(T) \ +static int realloc_##T(T **array, size_t num) \ +{ \ + if (!num) { \ + free(*array); \ + *array = nullptr; \ + return 0; \ + } \ + \ + T *temp_pointer = (T *)realloc(*array, num * sizeof(T)); \ + \ + if (!temp_pointer) { \ + return -1; \ + } \ + \ + *array = temp_pointer; \ + \ + return 0; \ +} + +MAKE_REALLOC(TCP_Connection_to) +MAKE_REALLOC(TCP_con) /* return 1 if the connections_number is not valid. @@ -90,7 +105,7 @@ static bool connections_number_not_valid(const TCP_Connections *tcp_c, int conne return 1; } - if (tcp_c->connections == NULL) { + if (tcp_c->connections == nullptr) { return 1; } @@ -110,7 +125,7 @@ static bool tcp_connections_number_not_valid(const TCP_Connections *tcp_c, int t return 1; } - if (tcp_c->tcp_connections == NULL) { + if (tcp_c->tcp_connections == nullptr) { return 1; } @@ -138,10 +153,7 @@ static int create_connection(TCP_Connections *tcp_c) int id = -1; - TCP_Connection_to *temp_pointer; - - if (realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length + 1, - temp_pointer) == 0) { + if (realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length + 1) == 0) { id = tcp_c->connections_length; ++tcp_c->connections_length; memset(&tcp_c->connections[id], 0, sizeof(TCP_Connection_to)); @@ -167,9 +179,7 @@ static int create_tcp_connection(TCP_Connections *tcp_c) int id = -1; - TCP_con *temp_pointer; - - if (realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length + 1, temp_pointer) == 0) { + if (realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length + 1) == 0) { id = tcp_c->tcp_connections_length; ++tcp_c->tcp_connections_length; memset(&tcp_c->tcp_connections[id], 0, sizeof(TCP_con)); @@ -200,8 +210,7 @@ static int wipe_connection(TCP_Connections *tcp_c, int connections_number) if (tcp_c->connections_length != i) { tcp_c->connections_length = i; - TCP_Connection_to *temp_pointer; - realloc_tox_array(tcp_c->connections, TCP_Connection_to, tcp_c->connections_length, temp_pointer); + realloc_TCP_Connection_to(&tcp_c->connections, tcp_c->connections_length); } return 0; @@ -229,8 +238,7 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe if (tcp_c->tcp_connections_length != i) { tcp_c->tcp_connections_length = i; - TCP_con *temp_pointer; - realloc_tox_array(tcp_c->tcp_connections, TCP_con, tcp_c->tcp_connections_length, temp_pointer); + realloc_TCP_con(&tcp_c->tcp_connections, tcp_c->tcp_connections_length); } return 0; @@ -239,7 +247,7 @@ static int wipe_tcp_connection(TCP_Connections *tcp_c, int tcp_connections_numbe static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int connections_number) { if (connections_number_not_valid(tcp_c, connections_number)) { - return 0; + return nullptr; } return &tcp_c->connections[connections_number]; @@ -248,7 +256,7 @@ static TCP_Connection_to *get_connection(const TCP_Connections *tcp_c, int conne static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connections_number) { if (tcp_connections_number_not_valid(tcp_c, tcp_connections_number)) { - return 0; + return nullptr; } return &tcp_c->tcp_connections[tcp_connections_number]; @@ -824,7 +832,7 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection memcpy(tcp_con->relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); kill_TCP_connection(tcp_con->connection); - tcp_con->connection = NULL; + tcp_con->connection = nullptr; unsigned int i; @@ -922,7 +930,7 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); - if (con_to == NULL) { + if (con_to == nullptr) { return -1; } @@ -1368,14 +1376,14 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status) */ TCP_Connections *new_tcp_connections(const uint8_t *secret_key, TCP_Proxy_Info *proxy_info) { - if (secret_key == NULL) { - return NULL; + if (secret_key == nullptr) { + return nullptr; } TCP_Connections *temp = (TCP_Connections *)calloc(1, sizeof(TCP_Connections)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } memcpy(temp->self_secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); @@ -1400,7 +1408,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata) tcp_con = get_tcp_connection(tcp_c, i); // Make sure the TCP connection wasn't dropped in any of the callbacks. - assert(tcp_con != NULL); + assert(tcp_con != nullptr); if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) { if (tcp_con->status == TCP_CONN_CONNECTED) { diff --git a/protocols/Tox/libtox/src/toxcore/TCP_server.c b/protocols/Tox/libtox/src/toxcore/TCP_server.c index e86776e59e..4541fce0ce 100644 --- a/protocols/Tox/libtox/src/toxcore/TCP_server.c +++ b/protocols/Tox/libtox/src/toxcore/TCP_server.c @@ -98,9 +98,11 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server) /* This is needed to compile on Android below API 21 */ +#ifdef TCP_SERVER_USE_EPOLL #ifndef EPOLLRDHUP #define EPOLLRDHUP 0x2000 #endif +#endif /* Set the size of the connection list to numfriends. * @@ -111,7 +113,7 @@ static int realloc_connection(TCP_Server *TCP_server, uint32_t num) { if (num == 0) { free(TCP_server->accepted_connection_array); - TCP_server->accepted_connection_array = NULL; + TCP_server->accepted_connection_array = nullptr; TCP_server->size_accepted_connections = 0; return 0; } @@ -124,7 +126,7 @@ static int realloc_connection(TCP_Server *TCP_server, uint32_t num) TCP_server->accepted_connection_array, num * sizeof(TCP_Secure_Connection)); - if (new_connections == NULL) { + if (new_connections == nullptr) { return -1; } @@ -403,7 +405,7 @@ static int send_pending_data(TCP_Secure_Connection *con) con->priority_queue_start = p; if (!p) { - con->priority_queue_end = NULL; + con->priority_queue_end = nullptr; return 0; } @@ -422,7 +424,7 @@ static bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint return 0; } - new_list->next = NULL; + new_list->next = nullptr; new_list->size = size; new_list->sent = sent; memcpy(new_list->data, packet, size); @@ -788,7 +790,7 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, uint16_t length) { TCP_Server *TCP_server = (TCP_Server *)object; - uint32_t index = dest.ip.ip6.uint32[0]; + uint32_t index = dest.ip.ip.v6.uint32[0]; if (index >= TCP_server->size_accepted_connections) { return 1; @@ -796,7 +798,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[index]; - if (con->identifier != dest.ip.ip6.uint64[1]) { + if (con->identifier != dest.ip.ip.v6.uint64[1]) { return 1; } @@ -896,9 +898,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint IP_Port source; source.port = 0; // dummy initialise source.ip.family = TCP_ONION_FAMILY; - source.ip.ip6.uint32[0] = con_id; - source.ip.ip6.uint32[1] = 0; - source.ip.ip6.uint64[1] = con->identifier; + source.ip.ip.v6.uint32[0] = con_id; + source.ip.ip.v6.uint32[1] = 0; + source.ip.ip.v6.uint64[1] = con->identifier; onion_send_1(TCP_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), source, data + 1); } @@ -1034,25 +1036,25 @@ static Socket new_listening_TCP_socket(int family, uint16_t port) TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, const uint8_t *secret_key, Onion *onion) { - if (num_sockets == 0 || ports == NULL) { - return NULL; + if (num_sockets == 0 || ports == nullptr) { + return nullptr; } if (networking_at_startup() != 0) { - return NULL; + return nullptr; } TCP_Server *temp = (TCP_Server *)calloc(1, sizeof(TCP_Server)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket)); - if (temp->socks_listening == NULL) { + if (temp->socks_listening == nullptr) { free(temp); - return NULL; + return nullptr; } #ifdef TCP_SERVER_USE_EPOLL @@ -1061,7 +1063,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin if (temp->efd == -1) { free(temp->socks_listening); free(temp); - return NULL; + return nullptr; } #endif @@ -1101,7 +1103,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin if (temp->num_listening_socks == 0) { free(temp->socks_listening); free(temp); - return NULL; + return nullptr; } if (onion) { @@ -1429,7 +1431,7 @@ void kill_TCP_server(TCP_Server *TCP_server) } if (TCP_server->onion) { - set_callback_handle_recv_1(TCP_server->onion, NULL, NULL); + set_callback_handle_recv_1(TCP_server->onion, nullptr, nullptr); } bs_list_free(&TCP_server->accepted_key_list); diff --git a/protocols/Tox/libtox/src/toxcore/ccompat.h b/protocols/Tox/libtox/src/toxcore/ccompat.h index e72e66ae58..1ceb5a5a87 100644 --- a/protocols/Tox/libtox/src/toxcore/ccompat.h +++ b/protocols/Tox/libtox/src/toxcore/ccompat.h @@ -4,13 +4,6 @@ #ifndef CCOMPAT_H #define CCOMPAT_H -// Marking GNU extensions to avoid warnings. -#if defined(__GNUC__) -#define GNU_EXTENSION __extension__ -#else -#define GNU_EXTENSION -#endif - // Variable length arrays. // VLA(type, name, size) allocates a variable length array with automatic // storage duration. VLA_SIZE(name) evaluates to the runtime size of that array @@ -20,7 +13,7 @@ // "function") is used. Note the semantic difference: alloca'd memory does not // get freed at the end of the declaration's scope. Do not use VLA() in loops or // you may run out of stack space. -#if !defined(_MSC_VER) && __STDC_VERSION__ >= 199901L +#if !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 VLAs. #define VLA(type, name, size) type name[size] #define SIZEOF_VLA sizeof @@ -29,6 +22,11 @@ // Emulation using alloca. #ifdef _WIN32 #include <malloc.h> +#elif defined(__FreeBSD__) +#include <stdlib.h> +#if !defined(alloca) && defined(__GNUC__) +#define alloca __builtin_alloca +#endif #else #include <alloca.h> #endif @@ -40,4 +38,14 @@ #endif +#ifndef __cplusplus +#define nullptr NULL +#endif + +#ifdef __GNUC__ +#define GNU_PRINTF __attribute__((__format__(__printf__, 6, 7))) +#else +#define GNU_PRINTF +#endif + #endif /* CCOMPAT_H */ diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.api.h b/protocols/Tox/libtox/src/toxcore/crypto_core.api.h index 0ba28c0c1b..bb2c0a13b8 100644 --- a/protocols/Tox/libtox/src/toxcore/crypto_core.api.h +++ b/protocols/Tox/libtox/src/toxcore/crypto_core.api.h @@ -28,6 +28,10 @@ #include <stdbool.h> #include <stdint.h> #include <stdlib.h> + +#ifdef __cplusplus +extern "C" { +#endif %} /** @@ -251,5 +255,9 @@ static void increment_nonce_number(uint8_t[CRYPTO_NONCE_SIZE] nonce, uint32_t ho static void new_symmetric_key(uint8_t[CRYPTO_SYMMETRIC_KEY_SIZE] key); %{ +#ifdef __cplusplus +} // extern "C" +#endif + #endif /* CRYPTO_CORE_H */ %} diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.c b/protocols/Tox/libtox/src/toxcore/crypto_core.c index d5333e6760..26589219ed 100644 --- a/protocols/Tox/libtox/src/toxcore/crypto_core.c +++ b/protocols/Tox/libtox/src/toxcore/crypto_core.c @@ -219,7 +219,7 @@ void increment_nonce(uint8_t *nonce) static uint32_t host_to_network(uint32_t x) { -#if BYTE_ORDER == LITTLE_ENDIAN +#if !defined(BYTE_ORDER) || BYTE_ORDER == LITTLE_ENDIAN return ((x >> 24) & 0x000000FF) | // move byte 3 to byte 0 ((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1 diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core.h b/protocols/Tox/libtox/src/toxcore/crypto_core.h index d2742a8b72..2c83fd255a 100644 --- a/protocols/Tox/libtox/src/toxcore/crypto_core.h +++ b/protocols/Tox/libtox/src/toxcore/crypto_core.h @@ -28,6 +28,10 @@ #include <stdint.h> #include <stdlib.h> +#ifdef __cplusplus +extern "C" { +#endif + /** * The number of bytes in a Tox public key. */ @@ -236,4 +240,8 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num); */ void new_symmetric_key(uint8_t *key); +#ifdef __cplusplus +} // extern "C" +#endif + #endif /* CRYPTO_CORE_H */ diff --git a/protocols/Tox/libtox/src/toxcore/crypto_core_test.cpp b/protocols/Tox/libtox/src/toxcore/crypto_core_test.cpp new file mode 100644 index 0000000000..8f91dce842 --- /dev/null +++ b/protocols/Tox/libtox/src/toxcore/crypto_core_test.cpp @@ -0,0 +1,96 @@ +#include "crypto_core.h" + +#include <algorithm> + +#include <gtest/gtest.h> + +namespace +{ + +enum { + /** + * The size of the arrays to compare. This was chosen to take around 2000 + * CPU clocks on x86_64. + */ + CRYPTO_TEST_MEMCMP_SIZE = 1024 * 1024, // 1 MiB + /** + * The number of times we run memcmp in the test. + * + * We compute the median time taken to reduce error margins. + */ + CRYPTO_TEST_MEMCMP_ITERATIONS = 500, + /** + * The margin of error (in clocks) we allow for this test. + * + * Should be within 0.5% of ~2000 CPU clocks. In reality, the code is much + * more precise and is usually within 1 CPU clock. + */ + CRYPTO_TEST_MEMCMP_EPS = 10, +}; + +clock_t memcmp_time(void *a, void *b, size_t len) +{ + clock_t start = clock(); + crypto_memcmp(a, b, len); + return clock() - start; +} + +/** + * This function performs the actual timing. It interleaves comparison of + * equal and non-equal arrays to reduce the influence of external effects + * such as the machine being a little more busy 1 second later. + */ +void memcmp_median(void *src, void *same, void *not_same, size_t len, + clock_t *same_median, clock_t *not_same_median) +{ + clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; + clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; + + for (size_t i = 0; i < CRYPTO_TEST_MEMCMP_ITERATIONS; i++) { + same_results[i] = memcmp_time(src, same, len); + not_same_results[i] = memcmp_time(src, not_same, len); + } + + std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); + *same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; + std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); + *not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; +} + +/** + * This test checks whether crypto_memcmp takes the same time for equal and + * non-equal chunks of memory. + */ +TEST(CryptoCore, MemcmpTimingIsDataIndependent) +{ + // A random piece of memory. + uint8_t *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE); + + // A separate piece of memory containing the same data. + uint8_t *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE); + + // Another piece of memory containing different data. + uint8_t *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; + random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE); + + clock_t same_median; + clock_t not_same_median; + memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, ¬_same_median); + + delete[] not_same; + delete[] same; + delete[] src; + + clock_t const delta = same_median > not_same_median + ? same_median - not_same_median + : not_same_median - same_median; + + EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) + << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" + << "Time of the same data comparation: " << same_median << " clocks\n" + << "Time of the different data comparation: " << not_same_median << " clocks"; +} + +} // namespace diff --git a/protocols/Tox/libtox/src/toxcore/friend_connection.c b/protocols/Tox/libtox/src/toxcore/friend_connection.c index 2ebc0d9bc0..6f5685fb5d 100644 --- a/protocols/Tox/libtox/src/toxcore/friend_connection.c +++ b/protocols/Tox/libtox/src/toxcore/friend_connection.c @@ -88,48 +88,38 @@ Net_Crypto *friendconn_net_crypto(const Friend_Connections *fr_c) } -/* return 1 if the friendcon_id is not valid. - * return 0 if the friendcon_id is valid. +/* return true if the friendcon_id is valid. + * return false if the friendcon_id is not valid. */ -static uint8_t friendconn_id_not_valid(const Friend_Connections *fr_c, int friendcon_id) +static bool friendconn_id_valid(const Friend_Connections *fr_c, int friendcon_id) { - if ((unsigned int)friendcon_id >= fr_c->num_cons) { - return 1; - } - - if (fr_c->conns == NULL) { - return 1; - } - - if (fr_c->conns[friendcon_id].status == FRIENDCONN_STATUS_NONE) { - return 1; - } - - return 0; + return (unsigned int)friendcon_id < fr_c->num_cons && + fr_c->conns != nullptr && + fr_c->conns[friendcon_id].status != FRIENDCONN_STATUS_NONE; } /* Set the size of the friend connections list to num. * - * return -1 if realloc fails. - * return 0 if it succeeds. + * return false if realloc fails. + * return true if it succeeds. */ -static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num) +static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) { if (num == 0) { free(fr_c->conns); - fr_c->conns = NULL; - return 0; + fr_c->conns = nullptr; + return true; } Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); - if (newgroup_cons == NULL) { - return -1; + if (newgroup_cons == nullptr) { + return false; } fr_c->conns = newgroup_cons; - return 0; + return true; } /* Create a new empty friend connection. @@ -139,22 +129,20 @@ static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num) */ static int create_friend_conn(Friend_Connections *fr_c) { - uint32_t i; - - for (i = 0; i < fr_c->num_cons; ++i) { + for (uint32_t i = 0; i < fr_c->num_cons; ++i) { if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) { return i; } } - int id = -1; - - if (realloc_friendconns(fr_c, fr_c->num_cons + 1) == 0) { - id = fr_c->num_cons; - ++fr_c->num_cons; - memset(&fr_c->conns[id], 0, sizeof(Friend_Conn)); + if (!realloc_friendconns(fr_c, fr_c->num_cons + 1)) { + return -1; } + const int id = fr_c->num_cons; + ++fr_c->num_cons; + memset(&fr_c->conns[id], 0, sizeof(Friend_Conn)); + return id; } @@ -165,13 +153,14 @@ static int create_friend_conn(Friend_Connections *fr_c) */ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) { - if (friendconn_id_not_valid(fr_c, friendcon_id)) { + if (!friendconn_id_valid(fr_c, friendcon_id)) { return -1; } - uint32_t i; memset(&fr_c->conns[friendcon_id], 0, sizeof(Friend_Conn)); + uint32_t i; + for (i = fr_c->num_cons; i != 0; --i) { if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) { break; @@ -188,8 +177,8 @@ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) { - if (friendconn_id_not_valid(fr_c, friendcon_id)) { - return 0; + if (!friendconn_id_valid(fr_c, friendcon_id)) { + return nullptr; } return &fr_c->conns[friendcon_id]; @@ -200,9 +189,7 @@ static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) */ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) { - uint32_t i; - - for (i = 0; i < fr_c->num_cons; ++i) { + for (uint32_t i = 0; i < fr_c->num_cons; ++i) { Friend_Conn *friend_con = get_conn(fr_c, i); if (friend_con) { @@ -222,7 +209,7 @@ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) */ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -237,11 +224,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ } } - unsigned int i; - - uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; + const uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; - for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { + for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { if (friend_con->tcp_relays[i].ip_port.ip.family != 0 && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); @@ -258,16 +243,14 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ /* Connect to number saved relays for friend. */ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return; } - unsigned int i; - - for (i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { - uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; + for (unsigned i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { + const uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; if (friend_con->tcp_relays[index].ip_port.ip.family) { if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, @@ -280,7 +263,7 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return 0; @@ -288,19 +271,16 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) Node_format nodes[MAX_SHARED_RELAYS]; uint8_t data[1024]; - int n, length; - - n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); - int i; + const int n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); - for (i = 0; i < n; ++i) { + 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. */ friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); } - length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); + int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); if (length <= 0) { return 0; @@ -321,7 +301,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) { Friend_Connections *fr_c = (Friend_Connections *)object; - Friend_Conn *friend_con = get_conn(fr_c, number); + const Friend_Conn *friend_con = get_conn(fr_c, number); if (!friend_con) { return -1; @@ -338,8 +318,8 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id); /* Callback for DHT ip_port changes. */ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) { - Friend_Connections *fr_c = (Friend_Connections *)object; - Friend_Conn *friend_con = get_conn(fr_c, number); + Friend_Connections *const fr_c = (Friend_Connections *)object; + Friend_Conn *const friend_con = get_conn(fr_c, number); if (!friend_con) { return; @@ -361,7 +341,7 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return; @@ -384,8 +364,8 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint static int handle_status(void *object, int number, uint8_t status, void *userdata) { - Friend_Connections *fr_c = (Friend_Connections *)object; - Friend_Conn *friend_con = get_conn(fr_c, number); + Friend_Connections *const fr_c = (Friend_Connections *)object; + Friend_Conn *const friend_con = get_conn(fr_c, number); if (!friend_con) { return -1; @@ -429,8 +409,8 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat /* Callback for dht public key changes. */ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata) { - Friend_Connections *fr_c = (Friend_Connections *)object; - Friend_Conn *friend_con = get_conn(fr_c, number); + Friend_Connections *const fr_c = (Friend_Connections *)object; + Friend_Conn *const friend_con = get_conn(fr_c, number); if (!friend_con) { return; @@ -459,7 +439,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t return -1; } - Friend_Connections *fr_c = (Friend_Connections *)object; + Friend_Connections *const fr_c = (Friend_Connections *)object; Friend_Conn *friend_con = get_conn(fr_c, number); if (!friend_con) { @@ -481,24 +461,20 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t if (data[0] == PACKET_ID_SHARE_RELAYS) { Node_format nodes[MAX_SHARED_RELAYS]; - int n; + const int n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1); - if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1) { + if (n == -1) { return -1; } - int j; - - for (j = 0; j < n; j++) { + for (int j = 0; j < n; j++) { friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); } return 0; } - unsigned int i; - - for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { + for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { if (friend_con->callbacks[i].data_callback) { friend_con->callbacks[i].data_callback( friend_con->callbacks[i].callback_object, @@ -521,16 +497,14 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui return -1; } - Friend_Connections *fr_c = (Friend_Connections *)object; - Friend_Conn *friend_con = get_conn(fr_c, number); + Friend_Connections *const fr_c = (Friend_Connections *)object; + const Friend_Conn *friend_con = get_conn(fr_c, number); if (!friend_con) { return -1; } - unsigned int i; - - for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { + for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { if (friend_con->callbacks[i].lossy_data_callback) { friend_con->callbacks[i].lossy_data_callback( friend_con->callbacks[i].callback_object, @@ -549,48 +523,47 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui static int handle_new_connections(void *object, New_Connection *n_c) { - Friend_Connections *fr_c = (Friend_Connections *)object; - int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key); - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Connections *const fr_c = (Friend_Connections *)object; + const int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); - if (friend_con) { - - if (friend_con->crypt_connection_id != -1) { - return -1; - } + if (!friend_con) { + return -1; + } - int id = accept_crypto_connection(fr_c->net_crypto, n_c); + if (friend_con->crypt_connection_id != -1) { + return -1; + } - if (id == -1) { - return -1; - } + const int id = accept_crypto_connection(fr_c->net_crypto, n_c); - connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); - connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); - connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); - friend_con->crypt_connection_id = id; + if (id == -1) { + return -1; + } - if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { - set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); - } else { - friend_con->dht_ip_port = n_c->source; - friend_con->dht_ip_port_lastrecv = unix_time(); - } + connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); + connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); + connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); + friend_con->crypt_connection_id = id; - if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { - change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); - } + if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { + set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); + } else { + friend_con->dht_ip_port = n_c->source; + friend_con->dht_ip_port_lastrecv = unix_time(); + } - nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); - return 0; + if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { + change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); } - return -1; + nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); + return 0; } static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -605,7 +578,7 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) return -1; } - int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); + const int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); if (id == -1) { return -1; @@ -622,14 +595,14 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) static int send_ping(const Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; } - uint8_t ping = PACKET_ID_ALIVE; - int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); + const uint8_t ping = PACKET_ID_ALIVE; + const int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); if (ret != -1) { friend_con->ping_lastsent = unix_time(); @@ -646,7 +619,7 @@ static int send_ping(const Friend_Connections *fr_c, int friendcon_id) */ int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -662,7 +635,7 @@ int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) */ unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return 0; @@ -678,7 +651,7 @@ unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) */ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -714,7 +687,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), void *object, int number) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -741,7 +714,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi */ int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -771,13 +744,13 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k return -1; } - int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); + const int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); if (onion_friendnum == -1) { return -1; } - Friend_Conn *friend_con = &fr_c->conns[friendcon_id]; + Friend_Conn *const friend_con = &fr_c->conns[friendcon_id]; friend_con->crypt_connection_id = -1; friend_con->status = FRIENDCONN_STATUS_CONNECTING; @@ -797,7 +770,7 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k */ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id) { - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -844,7 +817,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 return -1; } - Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { return -1; @@ -860,7 +833,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 } packet[0] = CRYPTO_PACKET_FRIEND_REQ; - int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet)); + const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet)); if (num <= 0) { return -1; @@ -872,14 +845,14 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 /* Create new friend_connections instance. */ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled) { - if (!onion_c) { - return NULL; + if (onion_c == nullptr) { + return nullptr; } - Friend_Connections *temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); + Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } temp->dht = onion_get_dht(onion_c); @@ -923,11 +896,10 @@ static void LANdiscovery(Friend_Connections *fr_c) /* main friend_connections loop. */ void do_friend_connections(Friend_Connections *fr_c, void *userdata) { - uint32_t i; - uint64_t temp_time = unix_time(); + const uint64_t temp_time = unix_time(); - for (i = 0; i < fr_c->num_cons; ++i) { - Friend_Conn *friend_con = get_conn(fr_c, i); + for (uint32_t i = 0; i < fr_c->num_cons; ++i) { + Friend_Conn *const friend_con = get_conn(fr_c, i); if (friend_con) { if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { @@ -980,9 +952,7 @@ void kill_friend_connections(Friend_Connections *fr_c) return; } - uint32_t i; - - for (i = 0; i < fr_c->num_cons; ++i) { + for (uint32_t i = 0; i < fr_c->num_cons; ++i) { kill_friend_connection(fr_c, i); } diff --git a/protocols/Tox/libtox/src/toxcore/friend_requests.c b/protocols/Tox/libtox/src/toxcore/friend_requests.c index 4a06654deb..13c89eae13 100644 --- a/protocols/Tox/libtox/src/toxcore/friend_requests.c +++ b/protocols/Tox/libtox/src/toxcore/friend_requests.c @@ -67,6 +67,7 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const fr->handle_friendrequest_isset = 1; fr->handle_friendrequest_object = object; } + /* Set the function used to check if a friend request should be displayed to the user or not. */ void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata) { @@ -87,20 +88,18 @@ static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) /* Check if a friend request was already received. * - * return 0 if it did not. - * return 1 if it did. + * return false if it did not. + * return true if it did. */ -static int request_received(Friend_Requests *fr, const uint8_t *real_pk) +static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) { - uint32_t i; - - for (i = 0; i < MAX_RECEIVED_STORED; ++i) { + for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { if (id_equal(fr->received_requests[i], real_pk)) { - return 1; + return true; } } - return 0; + return false; } /* Remove real pk from received_requests list. @@ -110,9 +109,7 @@ static int request_received(Friend_Requests *fr, const uint8_t *real_pk) */ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) { - uint32_t i; - - for (i = 0; i < MAX_RECEIVED_STORED; ++i) { + for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { if (id_equal(fr->received_requests[i], real_pk)) { crypto_memzero(fr->received_requests[i], CRYPTO_PUBLIC_KEY_SIZE); return 0; @@ -126,7 +123,7 @@ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length, void *userdata) { - Friend_Requests *fr = (Friend_Requests *)object; + Friend_Requests *const fr = (Friend_Requests *)object; if (length <= 1 + sizeof(fr->nospam) || length > ONION_CLIENT_MAX_DATA_SIZE) { return 1; @@ -148,19 +145,19 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co } if (fr->filter_function) { - if ((*fr->filter_function)(source_pubkey, fr->filter_function_userdata) != 0) { + if (fr->filter_function(source_pubkey, fr->filter_function_userdata) != 0) { return 1; } } addto_receivedlist(fr, source_pubkey); - uint32_t message_len = length - sizeof(fr->nospam); + const uint32_t message_len = length - sizeof(fr->nospam); VLA(uint8_t, message, message_len + 1); memcpy(message, packet + sizeof(fr->nospam), message_len); message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */ - (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); + fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); return 0; } diff --git a/protocols/Tox/libtox/src/toxcore/group.c b/protocols/Tox/libtox/src/toxcore/group.c index 30f241ade5..37b4437d79 100644 --- a/protocols/Tox/libtox/src/toxcore/group.c +++ b/protocols/Tox/libtox/src/toxcore/group.c @@ -32,13 +32,13 @@ /* return 1 if the groupnumber is not valid. * return 0 if the groupnumber is valid. */ -static uint8_t groupnumber_not_valid(const Group_Chats *g_c, int groupnumber) +static uint8_t groupnumber_not_valid(const Group_Chats *g_c, uint32_t groupnumber) { - if ((unsigned int)groupnumber >= g_c->num_chats) { + if (groupnumber >= g_c->num_chats) { return 1; } - if (g_c->chats == NULL) { + if (g_c->chats == nullptr) { return 1; } @@ -59,13 +59,13 @@ static int realloc_groupchats(Group_Chats *g_c, uint32_t num) { if (num == 0) { free(g_c->chats); - g_c->chats = NULL; + g_c->chats = nullptr; return 0; } Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c)); - if (newgroup_chats == NULL) { + if (newgroup_chats == nullptr) { return -1; } @@ -106,7 +106,7 @@ static int create_group_chat(Group_Chats *g_c) * return -1 on failure. * return 0 on success. */ -static int wipe_group_chat(Group_Chats *g_c, int groupnumber) +static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber) { if (groupnumber_not_valid(g_c, groupnumber)) { return -1; @@ -129,10 +129,10 @@ static int wipe_group_chat(Group_Chats *g_c, int groupnumber) return 0; } -static Group_c *get_group_c(const Group_Chats *g_c, int groupnumber) +static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber) { if (groupnumber_not_valid(g_c, groupnumber)) { - return 0; + return nullptr; } return &g_c->chats[groupnumber]; @@ -224,9 +224,10 @@ enum { }; static int friend_in_close(Group_c *g, int friendcon_id); -static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, int groupnumber, uint8_t closest, uint8_t lock); +static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t groupnumber, uint8_t closest, + uint8_t lock); -static int add_to_closest(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk) +static int add_to_closest(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk) { Group_c *g = get_group_c(g_c, groupnumber); @@ -328,7 +329,7 @@ static unsigned int pk_in_closest_peers(Group_c *g, uint8_t *real_pk) static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier); -static int connect_to_closest(Group_Chats *g_c, int groupnumber, void *userdata) +static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *userdata) { Group_c *g = get_group_c(g_c, groupnumber); @@ -408,7 +409,7 @@ static int connect_to_closest(Group_Chats *g_c, int groupnumber, void *userdata) * return peer_index if success or peer already in chat. * return -1 if error. */ -static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk, +static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk, uint16_t peer_number, void *userdata, bool do_gc_callback) { Group_c *g = get_group_c(g_c, groupnumber); @@ -438,7 +439,7 @@ static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, co Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1)); - if (temp == NULL) { + if (temp == nullptr) { return -1; } @@ -454,8 +455,8 @@ static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, co add_to_closest(g_c, groupnumber, real_pk, temp_pk); - if (do_gc_callback && g_c->group_namelistchange) { - g_c->group_namelistchange(g_c->m, groupnumber, g->numpeers - 1, CHAT_CHANGE_PEER_ADD, userdata); + if (do_gc_callback && g_c->peer_list_changed_callback) { + g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata); } if (g->peer_on_join) { @@ -465,7 +466,7 @@ static int addpeer(Group_Chats *g_c, int groupnumber, const uint8_t *real_pk, co return (g->numpeers - 1); } -static int remove_close_conn(Group_Chats *g_c, int groupnumber, int friendcon_id) +static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id) { Group_c *g = get_group_c(g_c, groupnumber); @@ -497,7 +498,7 @@ static int remove_close_conn(Group_Chats *g_c, int groupnumber, int friendcon_id * return 0 if success * return -1 if error. */ -static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index, void *userdata) +static int delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void *userdata) { Group_c *g = get_group_c(g_c, groupnumber); @@ -527,7 +528,7 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index, void *user if (g->numpeers == 0) { free(g->group); - g->group = NULL; + g->group = nullptr; } else { if (g->numpeers != (uint32_t)peer_index) { memcpy(&g->group[peer_index], &g->group[g->numpeers], sizeof(Group_Peer)); @@ -535,15 +536,15 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index, void *user Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers)); - if (temp == NULL) { + if (temp == nullptr) { return -1; } g->group = temp; } - if (g_c->group_namelistchange) { - g_c->group_namelistchange(g_c->m, groupnumber, peer_index, CHAT_CHANGE_PEER_DEL, userdata); + if (g_c->peer_list_changed_callback) { + g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata); } if (g->peer_on_leave) { @@ -562,7 +563,7 @@ static int delpeer(Group_Chats *g_c, int groupnumber, int peer_index, void *user * return 0 on success. * return -1 if error. */ -static int setnick(Group_Chats *g_c, int groupnumber, int peer_index, const uint8_t *nick, uint16_t nick_len, +static int setnick(Group_Chats *g_c, uint32_t groupnumber, int peer_index, const uint8_t *nick, uint16_t nick_len, void *userdata, bool do_gc_callback) { if (nick_len > MAX_NAME_LENGTH) { @@ -588,14 +589,14 @@ static int setnick(Group_Chats *g_c, int groupnumber, int peer_index, const uint g->group[peer_index].nick_len = nick_len; - if (do_gc_callback && g_c->group_namelistchange) { - g_c->group_namelistchange(g_c->m, groupnumber, peer_index, CHAT_CHANGE_PEER_NAME, userdata); + if (do_gc_callback && g_c->peer_name_callback) { + g_c->peer_name_callback(g_c->m, groupnumber, peer_index, nick, nick_len, userdata); } return 0; } -static int settitle(Group_Chats *g_c, int groupnumber, int peer_index, const uint8_t *title, uint8_t title_len, +static int settitle(Group_Chats *g_c, uint32_t groupnumber, int peer_index, const uint8_t *title, uint8_t title_len, void *userdata) { if (title_len > MAX_NAME_LENGTH || title_len == 0) { @@ -623,7 +624,7 @@ static int settitle(Group_Chats *g_c, int groupnumber, int peer_index, const uin return 0; } -static void set_conns_type_close(Group_Chats *g_c, int groupnumber, int friendcon_id, uint8_t type) +static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id, uint8_t type) { Group_c *g = get_group_c(g_c, groupnumber); @@ -681,7 +682,8 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin * return close index on success * return -1 on failure. */ -static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, int groupnumber, uint8_t closest, uint8_t lock) +static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t groupnumber, uint8_t closest, + uint8_t lock) { Group_c *g = get_group_c(g_c, groupnumber); @@ -744,24 +746,24 @@ int add_groupchat(Group_Chats *g_c, uint8_t type) g->identifier[0] = type; g->peer_number = 0; /* Founder is peer 0. */ memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); - int peer_index = addpeer(g_c, groupnumber, g->real_pk, g_c->m->dht->self_public_key, 0, NULL, false); + int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), 0, nullptr, false); if (peer_index == -1) { return -1; } - setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length, NULL, false); + setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length, nullptr, false); return groupnumber; } -static int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_num); +static int group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, uint16_t peer_num); /* Delete a groupchat from the chats array. * * return 0 on success. * return -1 if groupnumber is invalid. */ -int del_groupchat(Group_Chats *g_c, int groupnumber) +int del_groupchat(Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -804,7 +806,7 @@ int del_groupchat(Group_Chats *g_c, int groupnumber) * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peer_pubkey(const Group_Chats *g_c, int groupnumber, int peernumber, uint8_t *pk) +int group_peer_pubkey(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *pk) { Group_c *g = get_group_c(g_c, groupnumber); @@ -826,7 +828,7 @@ int group_peer_pubkey(const Group_Chats *g_c, int groupnumber, int peernumber, u * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peername_size(const Group_Chats *g_c, int groupnumber, int peernumber) +int group_peername_size(const Group_Chats *g_c, uint32_t groupnumber, int peernumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -839,7 +841,7 @@ int group_peername_size(const Group_Chats *g_c, int groupnumber, int peernumber) } if (g->group[peernumber].nick_len == 0) { - return 8; + return 0; } return g->group[peernumber].nick_len; @@ -852,7 +854,7 @@ int group_peername_size(const Group_Chats *g_c, int groupnumber, int peernumber) * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint8_t *name) +int group_peername(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *name) { Group_c *g = get_group_c(g_c, groupnumber); @@ -865,8 +867,7 @@ int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint } if (g->group[peernumber].nick_len == 0) { - memcpy(name, "Tox User", 8); - return 8; + return 0; } memcpy(name, g->group[peernumber].nick, g->group[peernumber].nick_len); @@ -883,7 +884,7 @@ int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint * * return -1 on failure. */ -int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], +int group_names(const Group_Chats *g_c, uint32_t groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], uint16_t length) { Group_c *g = get_group_c(g_c, groupnumber); @@ -904,7 +905,7 @@ int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAM /* Return the number of peers in the group chat on success. * return -1 if groupnumber is invalid. */ -int group_number_peers(const Group_Chats *g_c, int groupnumber) +int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -921,7 +922,7 @@ int group_number_peers(const Group_Chats *g_c, int groupnumber) * return -2 if peernumber is invalid. * return -3 if we are not connected to the group chat. */ -int group_peernumber_is_ours(const Group_Chats *g_c, int groupnumber, int peernumber) +int group_peernumber_is_ours(const Group_Chats *g_c, uint32_t groupnumber, int peernumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -945,7 +946,7 @@ int group_peernumber_is_ours(const Group_Chats *g_c, int groupnumber, int peernu * return -1 on failure. * return type on success. */ -int group_get_type(const Group_Chats *g_c, int groupnumber) +int group_get_type(const Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1010,7 +1011,7 @@ static unsigned int send_lossy_group_peer(Friend_Connections *fr_c, int friendco * return -1 if groupnumber is invalid. * return -2 if invite packet failed to send. */ -int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber) +int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1046,7 +1047,7 @@ static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t * return -5 if group instance failed to initialize. * return -6 if join packet fails to send. */ -int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length) +int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length) { if (length != sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) { return -1; @@ -1109,9 +1110,10 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type * * NOTE: Handler must return 0 if packet is to be relayed, -1 if the packet should not be relayed. * - * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object), const uint8_t *packet, uint16_t length) + * Function(void *group object (set with group_set_object), uint32_t groupnumber, uint32_t friendgroupnumber, void *group peer object (set with group_peer_set_object), const uint8_t *packet, uint16_t length) */ -void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(void *, int, int, void *, +void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(void *, uint32_t, uint32_t, + void *, const uint8_t *, uint16_t)) { g_c->lossy_packethandlers[byte].function = function; @@ -1119,7 +1121,7 @@ void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*fu /* Set the callback for group invites. * - * Function(Group_Chats *g_c, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata) + * Function(Group_Chats *g_c, int32_t friendnumber, uint8_t type, uint8_t *data, size_t length, void *userdata) * * data of length is what needs to be passed to join_groupchat(). */ @@ -1129,9 +1131,10 @@ void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, ui g_c->invite_callback = function; } +// TODO(sudden6): function signatures in comments are incorrect /* Set the callback for group messages. * - * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Group_Chats *g_c, uint32_t groupnumber, uint32_t friendgroupnumber, uint8_t * message, size_t length, void *userdata) */ void g_callback_group_message(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, uint32_t, int, const uint8_t *, size_t, void *)) @@ -1139,16 +1142,28 @@ void g_callback_group_message(Group_Chats *g_c, void (*function)(Messenger *m, u g_c->message_callback = function; } -/* Set callback function for peer name list changes. +/* Set callback function for peer nickname changes. + * + * It gets called every time a peer changes their nickname. + * Function(Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, const uint8_t *nick, size_t nick_len, void *userdata) + */ +void g_callback_peer_name(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, uint32_t, const uint8_t *, + size_t, void *)) +{ + g_c->peer_name_callback = function; +} + +/* Set callback function for peer list changes. * - * It gets called every time the name list changes(new peer/name, deleted peer) - * Function(Group_Chats *g_c, int groupnumber, int peernumber, TOX_CHAT_CHANGE change, void *userdata) + * It gets called every time the name list changes(new peer, deleted peer) + * Function(Group_Chats *g_c, uint32_t groupnumber, void *userdata) */ -void g_callback_group_namelistchange(Group_Chats *g_c, void (*function)(Messenger *m, int, int, uint8_t, void *)) +void g_callback_peer_list_changed(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, void *)) { - g_c->group_namelistchange = function; + g_c->peer_list_changed_callback = function; } +// TODO(sudden6): function signatures are incorrect /* Set callback function for title changes. * * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * title, uint8_t length, void *userdata) @@ -1162,12 +1177,13 @@ void g_callback_group_title(Group_Chats *g_c, void (*function)(Messenger *m, uin /* Set a function to be called when a new peer joins a group chat. * - * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber) + * Function(void *group object (set with group_set_object), uint32_t groupnumber, uint32_t friendgroupnumber) * * return 0 on success. * return -1 on failure. */ -int callback_groupchat_peer_new(const Group_Chats *g_c, int groupnumber, void (*function)(void *, int, int)) +int callback_groupchat_peer_new(const Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t, + uint32_t)) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1181,12 +1197,13 @@ int callback_groupchat_peer_new(const Group_Chats *g_c, int groupnumber, void (* /* Set a function to be called when a peer leaves a group chat. * - * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object)) + * Function(void *group object (set with group_set_object), uint32_t groupnumber, uint32_t friendgroupnumber, void *group peer object (set with group_peer_set_object)) * * return 0 on success. * return -1 on failure. */ -int callback_groupchat_peer_delete(Group_Chats *g_c, int groupnumber, void (*function)(void *, int, int, void *)) +int callback_groupchat_peer_delete(Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t, uint32_t, + void *)) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1205,7 +1222,7 @@ int callback_groupchat_peer_delete(Group_Chats *g_c, int groupnumber, void (*fun * return 0 on success. * return -1 on failure. */ -int callback_groupchat_delete(Group_Chats *g_c, int groupnumber, void (*function)(void *, int)) +int callback_groupchat_delete(Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t)) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1217,13 +1234,13 @@ int callback_groupchat_delete(Group_Chats *g_c, int groupnumber, void (*function return 0; } -static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t message_id, const uint8_t *data, +static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *data, uint16_t len); #define GROUP_MESSAGE_PING_ID 0 -static int group_ping_send(const Group_Chats *g_c, int groupnumber) +static int group_ping_send(const Group_Chats *g_c, uint32_t groupnumber) { - if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, 0, 0) > 0) { + if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0) { return 0; } @@ -1236,7 +1253,7 @@ static int group_ping_send(const Group_Chats *g_c, int groupnumber) * return 0 on success * return -1 on failure */ -static int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_num, const uint8_t *real_pk, +static int group_new_peer_send(const Group_Chats *g_c, uint32_t groupnumber, uint16_t peer_num, const uint8_t *real_pk, uint8_t *temp_pk) { uint8_t packet[GROUP_MESSAGE_NEW_PEER_LENGTH]; @@ -1260,7 +1277,7 @@ static int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t * return 0 on success * return -1 on failure */ -static int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t peer_num) +static int group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, uint16_t peer_num) { uint8_t packet[GROUP_MESSAGE_KILL_PEER_LENGTH]; @@ -1280,7 +1297,7 @@ static int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_ * return 0 on success * return -1 on failure */ -static int group_name_send(const Group_Chats *g_c, int groupnumber, const uint8_t *nick, uint16_t nick_len) +static int group_name_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *nick, uint16_t nick_len) { if (nick_len > MAX_NAME_LENGTH) { return -1; @@ -1301,7 +1318,7 @@ static int group_name_send(const Group_Chats *g_c, int groupnumber, const uint8_ * return -2 if title is too long or empty. * return -3 if packet fails to send. */ -int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *title, uint8_t title_len) +int group_title_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *title, uint8_t title_len) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1336,7 +1353,7 @@ int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *tit * return -1 of groupnumber is invalid. * return -2 if title is too long or empty. */ -int group_title_get_size(const Group_Chats *g_c, int groupnumber) +int group_title_get_size(const Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1358,7 +1375,7 @@ int group_title_get_size(const Group_Chats *g_c, int groupnumber) * return -1 if groupnumber is invalid. * return -2 if title is too long or empty. */ -int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title) +int group_title_get(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *title) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1524,6 +1541,11 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_ } int groupnumber = get_group_num(g_c, data + sizeof(uint16_t)); + + if (groupnumber == -1) { + return -1; + } + uint16_t other_groupnum; memcpy(&other_groupnum, data, sizeof(uint16_t)); other_groupnum = net_ntohs(other_groupnum); @@ -1600,12 +1622,12 @@ static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t /* return number of peers sent on success. * return 0 on failure. */ -static unsigned int send_peers(Group_Chats *g_c, int groupnumber, int friendcon_id, uint16_t group_num) +static unsigned int send_peers(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id, uint16_t group_num) { Group_c *g = get_group_c(g_c, groupnumber); if (!g) { - return -1; + return 0; } uint8_t packet[MAX_CRYPTO_DATA_SIZE - (1 + sizeof(uint16_t))]; @@ -1655,7 +1677,8 @@ static unsigned int send_peers(Group_Chats *g_c, int groupnumber, int friendcon_ return sent; } -static int handle_send_peers(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, void *userdata) +static int handle_send_peers(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length, + void *userdata) { if (length == 0) { return -1; @@ -1702,7 +1725,7 @@ static int handle_send_peers(Group_Chats *g_c, int groupnumber, const uint8_t *d return 0; } -static void handle_direct_packet(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, +static void handle_direct_packet(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length, int close_index, void *userdata) { if (length == 0) { @@ -1758,7 +1781,7 @@ static void handle_direct_packet(Group_Chats *g_c, int groupnumber, const uint8_ * * return number of messages sent. */ -static unsigned int send_message_all_close(const Group_Chats *g_c, int groupnumber, const uint8_t *data, +static unsigned int send_message_all_close(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length, int receiver) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1792,7 +1815,8 @@ static unsigned int send_message_all_close(const Group_Chats *g_c, int groupnumb * * return number of messages sent. */ -static unsigned int send_lossy_all_close(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, +static unsigned int send_lossy_all_close(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, + uint16_t length, int receiver) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1885,7 +1909,7 @@ static unsigned int send_lossy_all_close(const Group_Chats *g_c, int groupnumber * return -3 if we are not connected to the group. * reutrn -4 if message failed to send. */ -static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t message_id, const uint8_t *data, +static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint8_t message_id, const uint8_t *data, uint16_t len) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1930,7 +1954,7 @@ static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t m * return 0 on success * see: send_message_group() for error codes. */ -int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *message, uint16_t length) +int group_message_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *message, uint16_t length) { int ret = send_message_group(g_c, groupnumber, PACKET_ID_MESSAGE, message, length); @@ -1945,7 +1969,7 @@ int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *m * return 0 on success * see: send_message_group() for error codes. */ -int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length) +int group_action_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *action, uint16_t length) { int ret = send_message_group(g_c, groupnumber, PACKET_ID_ACTION, action, length); @@ -1956,27 +1980,12 @@ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *ac return ret; } -/* send a group correction message - * return 0 on success - * see: send_message_group() for error codes. - */ -int group_correction_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length) -{ - int ret = send_message_group(g_c, groupnumber, PACKET_ID_CORRECTION, action, length); - - if (ret > 0) { - return 0; - } - - return ret; -} - /* High level function to send custom lossy packets. * * return -1 on failure. * return 0 on success. */ -int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length) +int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length) { // TODO(irungentoo): length check here? Group_c *g = get_group_c(g_c, groupnumber); @@ -2000,7 +2009,7 @@ int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8 return 0; } -static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, +static void handle_message_packet_group(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length, int close_index, void *userdata) { if (length < sizeof(uint16_t) + sizeof(uint32_t) + 1) { @@ -2098,9 +2107,24 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const } break; - case PACKET_ID_MESSAGE: - case PACKET_ID_ACTION: - case PACKET_ID_CORRECTION: { + case PACKET_ID_MESSAGE: { + if (msg_data_len == 0) { + return; + } + + VLA(uint8_t, newmsg, msg_data_len + 1); + memcpy(newmsg, msg_data, msg_data_len); + newmsg[msg_data_len] = 0; + + // TODO(irungentoo): + if (g_c->message_callback) { + g_c->message_callback(g_c->m, groupnumber, index, 0, newmsg, msg_data_len, userdata); + } + + break; + } + + case PACKET_ID_ACTION: { if (msg_data_len == 0) { return; } @@ -2111,9 +2135,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const // TODO(irungentoo): if (g_c->message_callback) { - uint8_t chat_message_id = message_id - PACKET_ID_MESSAGE; - g_c->message_callback(g_c->m, groupnumber, index, chat_message_id, - newmsg, msg_data_len, userdata); + g_c->message_callback(g_c->m, groupnumber, index, 1, newmsg, msg_data_len, userdata); } break; @@ -2188,6 +2210,7 @@ static int g_handle_packet(void *object, int friendcon_id, const uint8_t *data, static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16_t message_number) { if (peer_index == -1) { + // TODO(sudden6): invalid return value return -1; } @@ -2208,6 +2231,7 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 } if ((uint16_t)(message_number - g->group[peer_index].bottom_lossy_number) > (1 << 15)) { + // TODO(sudden6): invalid return value return -1; } @@ -2218,10 +2242,9 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; - return 0; - } - if (top_distance < MAX_LOSSY_COUNT) { + return 0; + } else { // top_distance < MAX_LOSSY_COUNT unsigned int i; for (i = g->group[peer_index].bottom_lossy_number; i != (g->group[peer_index].bottom_lossy_number + top_distance); @@ -2232,10 +2255,9 @@ static unsigned int lossy_packet_not_received(Group_c *g, int peer_index, uint16 g->group[peer_index].top_lossy_number = message_number; g->group[peer_index].bottom_lossy_number = (message_number - MAX_LOSSY_COUNT) + 1; g->group[peer_index].recv_lossy[message_number % MAX_LOSSY_COUNT] = 1; + return 0; } - - return -1; } static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata) @@ -2308,7 +2330,7 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin * return 0 on success. * return -1 on failure */ -int group_set_object(const Group_Chats *g_c, int groupnumber, void *object) +int group_set_object(const Group_Chats *g_c, uint32_t groupnumber, void *object) { Group_c *g = get_group_c(g_c, groupnumber); @@ -2325,7 +2347,7 @@ int group_set_object(const Group_Chats *g_c, int groupnumber, void *object) * return 0 on success. * return -1 on failure */ -int group_peer_set_object(const Group_Chats *g_c, int groupnumber, int peernumber, void *object) +int group_peer_set_object(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, void *object) { Group_c *g = get_group_c(g_c, groupnumber); @@ -2346,12 +2368,12 @@ int group_peer_set_object(const Group_Chats *g_c, int groupnumber, int peernumbe * return NULL on failure. * return object on success. */ -void *group_get_object(const Group_Chats *g_c, int groupnumber) +void *group_get_object(const Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); if (!g) { - return NULL; + return nullptr; } return g->object; @@ -2362,16 +2384,16 @@ void *group_get_object(const Group_Chats *g_c, int groupnumber) * return NULL on failure. * return object on success. */ -void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernumber) +void *group_peer_get_object(const Group_Chats *g_c, uint32_t groupnumber, int peernumber) { Group_c *g = get_group_c(g_c, groupnumber); if (!g) { - return NULL; + return nullptr; } if ((uint32_t)peernumber >= g->numpeers) { - return NULL; + return nullptr; } return g->group[peernumber].object; @@ -2380,7 +2402,7 @@ void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernum /* Interval in seconds to send ping messages */ #define GROUP_PING_INTERVAL 20 -static int ping_groupchat(Group_Chats *g_c, int groupnumber) +static int ping_groupchat(Group_Chats *g_c, uint32_t groupnumber) { Group_c *g = get_group_c(g_c, groupnumber); @@ -2397,7 +2419,7 @@ static int ping_groupchat(Group_Chats *g_c, int groupnumber) return 0; } -static int groupchat_clear_timedout(Group_Chats *g_c, int groupnumber, void *userdata) +static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void *userdata) { Group_c *g = get_group_c(g_c, groupnumber); @@ -2412,7 +2434,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, int groupnumber, void *use delpeer(g_c, groupnumber, i, userdata); } - if (g->group == NULL || i >= g->numpeers) { + if (g->group == nullptr || i >= g->numpeers) { break; } } @@ -2443,13 +2465,13 @@ void send_name_all_groups(Group_Chats *g_c) Group_Chats *new_groupchats(Messenger *m) { if (!m) { - return NULL; + return nullptr; } Group_Chats *temp = (Group_Chats *)calloc(1, sizeof(Group_Chats)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } temp->m = m; @@ -2491,8 +2513,8 @@ void kill_groupchats(Group_Chats *g_c) del_groupchat(g_c, i); } - m_callback_conference_invite(g_c->m, NULL); - g_c->m->conferences_object = NULL; + m_callback_conference_invite(g_c->m, nullptr); + g_c->m->conferences_object = nullptr; free(g_c); } diff --git a/protocols/Tox/libtox/src/toxcore/group.h b/protocols/Tox/libtox/src/toxcore/group.h index 6c0baebffb..a81dac39db 100644 --- a/protocols/Tox/libtox/src/toxcore/group.h +++ b/protocols/Tox/libtox/src/toxcore/group.h @@ -103,9 +103,9 @@ typedef struct { void *object; - void (*peer_on_join)(void *, int, int); - void (*peer_on_leave)(void *, int, int, void *); - void (*group_on_delete)(void *, int); + void (*peer_on_join)(void *, uint32_t, uint32_t); + void (*peer_on_leave)(void *, uint32_t, uint32_t, void *); + void (*group_on_delete)(void *, uint32_t); } Group_c; typedef struct { @@ -117,17 +117,18 @@ typedef struct { void (*invite_callback)(Messenger *m, uint32_t, int, const uint8_t *, size_t, void *); void (*message_callback)(Messenger *m, uint32_t, uint32_t, int, const uint8_t *, size_t, void *); - void (*group_namelistchange)(Messenger *m, int, int, uint8_t, void *); + void (*peer_name_callback)(Messenger *m, uint32_t, uint32_t, const uint8_t *, size_t, void *); + void (*peer_list_changed_callback)(Messenger *m, uint32_t, void *); void (*title_callback)(Messenger *m, uint32_t, uint32_t, const uint8_t *, size_t, void *); struct { - int (*function)(void *, int, int, void *, const uint8_t *, uint16_t); + int (*function)(void *, uint32_t, uint32_t, void *, const uint8_t *, uint16_t); } lossy_packethandlers[256]; } Group_Chats; /* Set the callback for group invites. * - * Function(Group_Chats *g_c, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata) + * Function(Group_Chats *g_c, uint32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata) * * data of length is what needs to be passed to join_groupchat(). */ @@ -136,7 +137,7 @@ void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, ui /* Set the callback for group messages. * - * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) + * Function(Group_Chats *g_c, uint32_t groupnumber, uint32_t friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) */ void g_callback_group_message(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, uint32_t, int, const uint8_t *, size_t, void *)); @@ -144,23 +145,26 @@ void g_callback_group_message(Group_Chats *g_c, void (*function)(Messenger *m, u /* Set callback function for title changes. * - * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * title, uint8_t length, void *userdata) + * Function(Group_Chats *g_c, uint32_t groupnumber, uint32_t friendgroupnumber, uint8_t * title, uint8_t length, void *userdata) * if friendgroupnumber == -1, then author is unknown (e.g. initial joining the group) */ void g_callback_group_title(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, uint32_t, const uint8_t *, size_t, void *)); -/* Set callback function for peer name list changes. +/* Set callback function for peer nickname changes. * - * It gets called every time the name list changes(new peer/name, deleted peer) - * Function(Group_Chats *g_c, int groupnumber, int peernumber, TOX_CHAT_CHANGE change, void *userdata) + * It gets called every time a peer changes their nickname. + * Function(Group_Chats *g_c, uint32_t groupnumber, uint32_t peernumber, const uint8_t *nick, size_t nick_len, void *userdata) */ -enum { - CHAT_CHANGE_PEER_ADD, - CHAT_CHANGE_PEER_DEL, - CHAT_CHANGE_PEER_NAME, -}; -void g_callback_group_namelistchange(Group_Chats *g_c, void (*function)(Messenger *m, int, int, uint8_t, void *)); +void g_callback_peer_name(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, uint32_t, const uint8_t *, + size_t, void *)); + +/* Set callback function for peer list changes. + * + * It gets called every time the name list changes(new peer, deleted peer) + * Function(Group_Chats *g_c, uint32_t groupnumber, void *userdata) + */ +void g_callback_peer_list_changed(Group_Chats *g_c, void (*function)(Messenger *m, uint32_t, void *)); /* Creates a new groupchat and puts it in the chats array. * @@ -176,7 +180,7 @@ int add_groupchat(Group_Chats *g_c, uint8_t type); * return 0 on success. * return -1 if groupnumber is invalid. */ -int del_groupchat(Group_Chats *g_c, int groupnumber); +int del_groupchat(Group_Chats *g_c, uint32_t groupnumber); /* Copy the public key of peernumber who is in groupnumber to pk. * pk must be CRYPTO_PUBLIC_KEY_SIZE long. @@ -185,7 +189,7 @@ int del_groupchat(Group_Chats *g_c, int groupnumber); * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peer_pubkey(const Group_Chats *g_c, int groupnumber, int peernumber, uint8_t *pk); +int group_peer_pubkey(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *pk); /* * Return the size of peernumber's name. @@ -193,7 +197,7 @@ int group_peer_pubkey(const Group_Chats *g_c, int groupnumber, int peernumber, u * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peername_size(const Group_Chats *g_c, int groupnumber, int peernumber); +int group_peername_size(const Group_Chats *g_c, uint32_t groupnumber, int32_t peernumber); /* Copy the name of peernumber who is in groupnumber to name. * name must be at least MAX_NAME_LENGTH long. @@ -202,7 +206,7 @@ int group_peername_size(const Group_Chats *g_c, int groupnumber, int peernumber) * return -1 if groupnumber is invalid. * return -2 if peernumber is invalid. */ -int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint8_t *name); +int group_peername(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *name); /* invite friendnumber to groupnumber * @@ -210,7 +214,7 @@ int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint * return -1 if groupnumber is invalid. * return -2 if invite packet failed to send. */ -int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber); +int invite_friend(Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber); /* Join a group (you need to have been invited first.) * @@ -224,25 +228,20 @@ int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber); * return -5 if group instance failed to initialize. * return -6 if join packet fails to send. */ -int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length); +int join_groupchat(Group_Chats *g_c, uint32_t friendnumber, uint8_t expected_type, const uint8_t *data, + uint16_t length); /* send a group message * return 0 on success * see: send_message_group() for error codes. */ -int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *message, uint16_t length); +int group_message_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *message, uint16_t length); /* send a group action * return 0 on success * see: send_message_group() for error codes. */ -int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length); - -/* send a group correction message - * return 0 on success - * see: send_message_group() for error codes. - */ -int group_correction_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length); +int group_action_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *action, uint16_t length); /* set the group's title, limited to MAX_NAME_LENGTH * return 0 on success @@ -250,14 +249,14 @@ int group_correction_send(const Group_Chats *g_c, int groupnumber, const uint8_t * return -2 if title is too long or empty. * return -3 if packet fails to send. */ -int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *title, uint8_t title_len); +int group_title_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *title, uint8_t title_len); /* return the group's title size. * return -1 of groupnumber is invalid. * return -2 if title is too long or empty. */ -int group_title_get_size(const Group_Chats *g_c, int groupnumber); +int group_title_get_size(const Group_Chats *g_c, uint32_t groupnumber); /* Get group title from groupnumber and put it in title. * Title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. @@ -266,12 +265,12 @@ int group_title_get_size(const Group_Chats *g_c, int groupnumber); * return -1 if groupnumber is invalid. * return -2 if title is too long or empty. */ -int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title); +int group_title_get(const Group_Chats *g_c, uint32_t groupnumber, uint8_t *title); /* Return the number of peers in the group chat on success. * return -1 if groupnumber is invalid. */ -int group_number_peers(const Group_Chats *g_c, int groupnumber); +int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber); /* return 1 if the peernumber corresponds to ours. * return 0 if the peernumber is not ours. @@ -279,7 +278,7 @@ int group_number_peers(const Group_Chats *g_c, int groupnumber); * return -2 if peernumber is invalid. * return -3 if we are not connected to the group chat. */ -int group_peernumber_is_ours(const Group_Chats *g_c, int groupnumber, int peernumber); +int group_peernumber_is_ours(const Group_Chats *g_c, uint32_t groupnumber, int peernumber); /* List all the peers in the group chat. * @@ -291,16 +290,17 @@ int group_peernumber_is_ours(const Group_Chats *g_c, int groupnumber, int peernu * * return -1 on failure. */ -int group_names(const Group_Chats *g_c, int groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], +int group_names(const Group_Chats *g_c, uint32_t groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[], uint16_t length); /* Set handlers for custom lossy packets. * * NOTE: Handler must return 0 if packet is to be relayed, -1 if the packet should not be relayed. * - * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object), const uint8_t *packet, uint16_t length) + * Function(void *group object (set with group_set_object), uint32_t groupnumber, uint32_t friendgroupnumber, void *group peer object (set with group_peer_set_object), const uint8_t *packet, uint16_t length) */ -void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(void *, int, int, void *, +void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*function)(void *, uint32_t, uint32_t, + void *, const uint8_t *, uint16_t)); /* High level function to send custom lossy packets. @@ -308,7 +308,7 @@ void group_lossy_packet_registerhandler(Group_Chats *g_c, uint8_t byte, int (*fu * return -1 on failure. * return 0 on success. */ -int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length); +int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *data, uint16_t length); /* Return the number of chats in the instance m. * You should use this to determine how much memory to allocate @@ -328,7 +328,7 @@ uint32_t copy_chatlist(Group_Chats *g_c, uint32_t *out_list, uint32_t list_size) * return -1 on failure. * return type on success. */ -int group_get_type(const Group_Chats *g_c, int groupnumber); +int group_get_type(const Group_Chats *g_c, uint32_t groupnumber); /* Send current name (set in messenger) to all online groups. */ @@ -339,28 +339,28 @@ void send_name_all_groups(Group_Chats *g_c); * return 0 on success. * return -1 on failure */ -int group_set_object(const Group_Chats *g_c, int groupnumber, void *object); +int group_set_object(const Group_Chats *g_c, uint32_t groupnumber, void *object); /* Set the object that is tied to the group peer. * * return 0 on success. * return -1 on failure */ -int group_peer_set_object(const Group_Chats *g_c, int groupnumber, int peernumber, void *object); +int group_peer_set_object(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, void *object); /* Return the object tide to the group chat previously set by group_set_object. * * return NULL on failure. * return object on success. */ -void *group_get_object(const Group_Chats *g_c, int groupnumber); +void *group_get_object(const Group_Chats *g_c, uint32_t groupnumber); /* Return the object tide to the group chat peer previously set by group_peer_set_object. * * return NULL on failure. * return object on success. */ -void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernumber); +void *group_peer_get_object(const Group_Chats *g_c, uint32_t groupnumber, int peernumber); /* Set a function to be called when a new peer joins a group chat. * @@ -369,25 +369,27 @@ void *group_peer_get_object(const Group_Chats *g_c, int groupnumber, int peernum * return 0 on success. * return -1 on failure. */ -int callback_groupchat_peer_new(const Group_Chats *g_c, int groupnumber, void (*function)(void *, int, int)); +int callback_groupchat_peer_new(const Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t, + uint32_t)); /* Set a function to be called when a peer leaves a group chat. * - * Function(void *group object (set with group_set_object), int groupnumber, int friendgroupnumber, void *group peer object (set with group_peer_set_object)) + * Function(void *group object (set with group_set_object), uint32_t groupnumber, uint32_t friendgroupnumber, void *group peer object (set with group_peer_set_object)) * * return 0 on success. * return -1 on failure. */ -int callback_groupchat_peer_delete(Group_Chats *g_c, int groupnumber, void (*function)(void *, int, int, void *)); +int callback_groupchat_peer_delete(Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t, uint32_t, + void *)); /* Set a function to be called when the group chat is deleted. * - * Function(void *group object (set with group_set_object), int groupnumber) + * Function(void *group object (set with group_set_object), uint32_t groupnumber) * * return 0 on success. * return -1 on failure. */ -int callback_groupchat_delete(Group_Chats *g_c, int groupnumber, void (*function)(void *, int)); +int callback_groupchat_delete(Group_Chats *g_c, uint32_t groupnumber, void (*function)(void *, uint32_t)); /* Create new groupchat instance. */ Group_Chats *new_groupchats(Messenger *m); diff --git a/protocols/Tox/libtox/src/toxcore/list.c b/protocols/Tox/libtox/src/toxcore/list.c index 36d609fbd1..8fd1f5fb4c 100644 --- a/protocols/Tox/libtox/src/toxcore/list.c +++ b/protocols/Tox/libtox/src/toxcore/list.c @@ -29,6 +29,8 @@ #include "list.h" +#include "ccompat.h" + /* Basically, the elements in the list are placed in order so that they can be searched for easily * -each element is seen as a big-endian integer when ordering them * -the ids array is maintained so that each id always matches @@ -148,8 +150,8 @@ int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity list->n = 0; list->element_size = element_size; list->capacity = 0; - list->data = NULL; - list->ids = NULL; + list->data = nullptr; + list->ids = nullptr; if (initial_capacity != 0) { if (!resize(list, initial_capacity)) { @@ -166,10 +168,10 @@ void bs_list_free(BS_LIST *list) { //free both arrays free(list->data); - list->data = NULL; + list->data = nullptr; free(list->ids); - list->ids = NULL; + list->ids = nullptr; } int bs_list_find(const BS_LIST *list, const uint8_t *data) diff --git a/protocols/Tox/libtox/src/toxcore/logger.h b/protocols/Tox/libtox/src/toxcore/logger.h index 8c8a639bec..b3a8f7dc28 100644 --- a/protocols/Tox/libtox/src/toxcore/logger.h +++ b/protocols/Tox/libtox/src/toxcore/logger.h @@ -26,6 +26,8 @@ #include <stdint.h> +#include "ccompat.h" + #ifndef MIN_LOGGER_LEVEL #define MIN_LOGGER_LEVEL LOG_INFO #endif @@ -59,8 +61,8 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void * /** * Main write function. If logging disabled does nothing. */ -void logger_write(Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format, - ...); +void logger_write( + Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, const char *format, ...) GNU_PRINTF; #define LOGGER_WRITE(log, level, ...) \ diff --git a/protocols/Tox/libtox/src/toxcore/net_crypto.c b/protocols/Tox/libtox/src/toxcore/net_crypto.c index 521dad2f1c..65c81d92a0 100644 --- a/protocols/Tox/libtox/src/toxcore/net_crypto.c +++ b/protocols/Tox/libtox/src/toxcore/net_crypto.c @@ -180,7 +180,7 @@ static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_conn return 1; } - if (c->crypto_connections == NULL) { + if (c->crypto_connections == nullptr) { return 1; } @@ -223,7 +223,7 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t * uint8_t nonce[CRYPTO_NONCE_SIZE]; random_nonce(nonce); packet[0] = NET_PACKET_COOKIE_REQUEST; - memcpy(packet + 1, c->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(packet + 1, dht_get_self_public_key(c->dht), CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); int len = encrypt_data_symmetric(shared_key, nonce, plain, sizeof(plain), packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); @@ -362,7 +362,7 @@ static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t return 1; } - if ((uint32_t)sendpacket(c->dht->net, source, data, sizeof(data)) != sizeof(data)) { + if ((uint32_t)sendpacket(dht_get_net(c->dht), source, data, sizeof(data)) != sizeof(data)) { return 1; } @@ -551,7 +551,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id) { if (crypt_connection_id_not_valid(c, crypt_connection_id)) { - return 0; + return nullptr; } return &c->crypto_connections[crypt_connection_id]; @@ -567,7 +567,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -607,7 +607,7 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return empty; } @@ -647,7 +647,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t // TODO(irungentoo): TCP, etc... Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -659,10 +659,10 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t // TODO(irungentoo): on bad networks, direct connections might not last indefinitely. if (ip_port.ip.family != 0) { bool direct_connected = 0; - crypto_connection_status(c, crypt_connection_id, &direct_connected, NULL); + crypto_connection_status(c, crypt_connection_id, &direct_connected, nullptr); if (direct_connected) { - if ((uint32_t)sendpacket(c->dht->net, ip_port, data, length) == length) { + if ((uint32_t)sendpacket(dht_get_net(c->dht), ip_port, data, length) == length) { pthread_mutex_unlock(&conn->mutex); return 0; } @@ -676,7 +676,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) > current_time && length < 96) || data[0] == NET_PACKET_COOKIE_REQUEST || data[0] == NET_PACKET_CRYPTO_HS) { - if ((uint32_t)sendpacket(c->dht->net, ip_port, data, length) == length) { + if ((uint32_t)sendpacket(dht_get_net(c->dht), ip_port, data, length) == length) { direct_send_attempt = 1; conn->direct_send_attempt_time = unix_time(); } @@ -733,7 +733,7 @@ static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packe Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data)); - if (new_d == NULL) { + if (new_d == nullptr) { return -1; } @@ -784,7 +784,7 @@ static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *d Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data)); - if (new_d == NULL) { + if (new_d == nullptr) { return -1; } @@ -816,7 +816,7 @@ static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) uint32_t id = array->buffer_start; ++array->buffer_start; free(array->buffer[num]); - array->buffer[num] = NULL; + array->buffer[num] = nullptr; return id; } @@ -840,7 +840,7 @@ static int clear_buffer_until(Packets_Array *array, uint32_t number) if (array->buffer[num]) { free(array->buffer[num]); - array->buffer[num] = NULL; + array->buffer[num] = nullptr; } } @@ -857,7 +857,7 @@ static int clear_buffer(Packets_Array *array) if (array->buffer[num]) { free(array->buffer[num]); - array->buffer[num] = NULL; + array->buffer[num] = nullptr; } } @@ -996,7 +996,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, } free(send_array->buffer[num]); - send_array->buffer[num] = NULL; + send_array->buffer[num] = nullptr; } } @@ -1038,7 +1038,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_ Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1087,14 +1087,14 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } /* 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 (conn->maximum_speed_reached) { - Packet_Data *dt = NULL; + Packet_Data *dt = nullptr; uint32_t packet_num = conn->send_array.buffer_end - 1; int ret = get_data_pointer(&conn->send_array, &dt, packet_num); @@ -1133,7 +1133,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1162,7 +1162,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons } if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { - Packet_Data *dt1 = NULL; + Packet_Data *dt1 = nullptr; if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) { dt1->sent_time = current_time_monotonic(); @@ -1203,7 +1203,7 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1238,7 +1238,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1266,7 +1266,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1318,13 +1318,13 @@ static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const u Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } uint8_t *temp_packet = (uint8_t *)malloc(length); - if (temp_packet == 0) { + if (temp_packet == nullptr) { return -1; } @@ -1349,7 +1349,7 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1357,7 +1357,7 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id) free(conn->temp_packet); } - conn->temp_packet = 0; + conn->temp_packet = nullptr; conn->temp_packet_length = 0; conn->temp_packet_sent_time = 0; conn->temp_packet_num_sent = 0; @@ -1374,7 +1374,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1402,7 +1402,7 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const u { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1430,7 +1430,7 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1443,7 +1443,7 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id, void *userda { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return; } @@ -1469,7 +1469,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1542,8 +1542,6 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const return -1; } - // else { /* TODO(irungentoo): ? */ } - set_buffer_end(&conn->recv_array, num); } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { Packet_Data dt; @@ -1571,7 +1569,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const /* conn might get killed in callback. */ conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } } @@ -1616,7 +1614,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1702,14 +1700,14 @@ static int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) { if (num == 0) { free(c->crypto_connections); - c->crypto_connections = NULL; + c->crypto_connections = nullptr; return 0; } Crypto_Connection *newcrypto_connections = (Crypto_Connection *)realloc(c->crypto_connections, num * sizeof(Crypto_Connection)); - if (newcrypto_connections == NULL) { + if (newcrypto_connections == nullptr) { return -1; } @@ -1756,7 +1754,7 @@ static int create_crypto_connection(Net_Crypto *c) c->crypto_connections[id].packet_send_rate_requested = 0; c->crypto_connections[id].last_packets_left_requested_rem = 0; - if (pthread_mutex_init(&c->crypto_connections[id].mutex, NULL) != 0) { + if (pthread_mutex_init(&c->crypto_connections[id].mutex, nullptr) != 0) { pthread_mutex_unlock(&c->connections_mutex); return -1; } @@ -1831,7 +1829,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -1850,7 +1848,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, } if (source.ip.family == TCP_FAMILY) { - if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) { + if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip.v6.uint32[0]) == 0) { return 1; } } @@ -1884,7 +1882,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const New_Connection n_c; n_c.cookie = (uint8_t *)malloc(COOKIE_LENGTH); - if (n_c.cookie == NULL) { + if (n_c.cookie == nullptr) { return -1; } @@ -1892,7 +1890,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const n_c.cookie_length = COOKIE_LENGTH; if (handle_crypto_handshake(c, n_c.recv_nonce, n_c.peersessionpublic_key, n_c.public_key, n_c.dht_public_key, - n_c.cookie, data, length, 0) != 0) { + n_c.cookie, data, length, nullptr) != 0) { free(n_c.cookie); return -1; } @@ -2009,7 +2007,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2059,7 +2057,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2095,7 +2093,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t Crypto_Connection *conn = get_crypto_connection(c, id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2134,7 +2132,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in IP_Port source; source.port = 0; source.ip.family = TCP_FAMILY; - source.ip.ip6.uint32[0] = tcp_connections_number; + source.ip.ip.v6.uint32[0] = tcp_connections_number; if (handle_new_connection_handshake(c, source, data, length, userdata) != 0) { return -1; @@ -2155,7 +2153,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2239,13 +2237,13 @@ static void do_tcp(Net_Crypto *c, void *userdata) for (i = 0; i < c->crypto_connections_length; ++i) { Crypto_Connection *conn = get_crypto_connection(c, i); - if (conn == 0) { + if (conn == nullptr) { return; } if (conn->status == CRYPTO_CONN_ESTABLISHED) { bool direct_connected = 0; - crypto_connection_status(c, i, &direct_connected, NULL); + crypto_connection_status(c, i, &direct_connected, nullptr); if (direct_connected) { pthread_mutex_lock(&c->tcp_mutex); @@ -2275,7 +2273,7 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2298,7 +2296,7 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (* { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2322,7 +2320,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2348,7 +2346,7 @@ int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)( { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2405,7 +2403,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2449,7 +2447,7 @@ static void send_crypto_packets(Net_Crypto *c) for (i = 0; i < c->crypto_connections_length; ++i) { Crypto_Connection *conn = get_crypto_connection(c, i); - if (conn == 0) { + if (conn == nullptr) { return; } @@ -2517,7 +2515,6 @@ static void send_crypto_packets(Net_Crypto *c) conn->last_sendqueue_size[pos] = num_packets_array(&conn->send_array); ++conn->last_sendqueue_counter; - unsigned int j; long signed int sum = 0; sum = (long signed int)conn->last_sendqueue_size[(pos) % CONGESTION_QUEUE_ARRAY_SIZE] - (long signed int)conn->last_sendqueue_size[(pos - (CONGESTION_QUEUE_ARRAY_SIZE - 1)) % CONGESTION_QUEUE_ARRAY_SIZE]; @@ -2527,7 +2524,7 @@ static void send_crypto_packets(Net_Crypto *c) conn->last_num_packets_resent[n_p_pos] = packets_resent; bool direct_connected = 0; - crypto_connection_status(c, i, &direct_connected, NULL); + crypto_connection_status(c, i, &direct_connected, nullptr); if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) { /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */ @@ -2542,7 +2539,7 @@ static void send_crypto_packets(Net_Crypto *c) delay = packets_set_rem_array; } - for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) { + for (unsigned j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) { unsigned int ind = (j + (packets_set_rem_array - delay) + n_p_pos) % CONGESTION_LAST_SENT_ARRAY_SIZE; total_sent += conn->last_num_packets_sent[ind]; total_resent += conn->last_num_packets_resent[ind]; @@ -2688,7 +2685,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return 0; } @@ -2725,7 +2722,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2763,7 +2760,7 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return -1; } @@ -2872,7 +2869,7 @@ unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_ { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); - if (conn == 0) { + if (conn == nullptr) { return CRYPTO_CONN_NO_CONNECTION; } @@ -2929,33 +2926,33 @@ Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) { unix_time_update(); - if (dht == NULL) { - return NULL; + if (dht == nullptr) { + return nullptr; } Net_Crypto *temp = (Net_Crypto *)calloc(1, sizeof(Net_Crypto)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } temp->log = log; - temp->tcp_c = new_tcp_connections(dht->self_secret_key, proxy_info); + temp->tcp_c = new_tcp_connections(dht_get_self_secret_key(dht), proxy_info); - if (temp->tcp_c == NULL) { + if (temp->tcp_c == nullptr) { free(temp); - return NULL; + return nullptr; } set_packet_tcp_connection_callback(temp->tcp_c, &tcp_data_callback, temp); set_oob_packet_tcp_connection_callback(temp->tcp_c, &tcp_oob_callback, temp); if (create_recursive_mutex(&temp->tcp_mutex) != 0 || - pthread_mutex_init(&temp->connections_mutex, NULL) != 0) { + pthread_mutex_init(&temp->connections_mutex, nullptr) != 0) { kill_tcp_connections(temp->tcp_c); free(temp); - return NULL; + return nullptr; } temp->dht = dht; @@ -2965,10 +2962,10 @@ Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL; - networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); - networking_registerhandler(dht->net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); - networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); - networking_registerhandler(dht->net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); + networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); + networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); + networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); + networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8); @@ -2983,7 +2980,7 @@ static void kill_timedout(Net_Crypto *c, void *userdata) for (i = 0; i < c->crypto_connections_length; ++i) { Crypto_Connection *conn = get_crypto_connection(c, i); - if (conn == 0) { + if (conn == nullptr) { return; } @@ -3039,10 +3036,10 @@ void kill_net_crypto(Net_Crypto *c) kill_tcp_connections(c->tcp_c); bs_list_free(&c->ip_port_list); - networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL); - networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); - networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_HS, NULL, NULL); - networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_DATA, NULL, NULL); + networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); + networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); + networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_HS, nullptr, nullptr); + networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_DATA, nullptr, nullptr); crypto_memzero(c, sizeof(Net_Crypto)); free(c); } diff --git a/protocols/Tox/libtox/src/toxcore/network.c b/protocols/Tox/libtox/src/toxcore/network.c index fb4748d4ec..e9c82c3930 100644 --- a/protocols/Tox/libtox/src/toxcore/network.c +++ b/protocols/Tox/libtox/src/toxcore/network.c @@ -25,10 +25,16 @@ #include "config.h" #endif +#ifdef __APPLE__ #define _DARWIN_C_SOURCE +#endif + +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 600 +#endif #if defined(_WIN32) && _WIN32_WINNT >= _WIN32_WINNT_WINXP +#undef _WIN32_WINNT #define _WIN32_WINNT 0x501 #endif @@ -46,7 +52,6 @@ #ifndef IPV6_ADD_MEMBERSHIP #ifdef IPV6_JOIN_GROUP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP -#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP #endif #endif @@ -79,8 +84,8 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t DWORD len = bufsize; - if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), NULL, buf, &len)) { - return NULL; + if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { + return nullptr; } return buf; @@ -93,14 +98,14 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t DWORD len = bufsize; - if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), NULL, buf, &len)) { - return NULL; + if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { + return nullptr; } return buf; } - return NULL; + return nullptr; } static int inet_pton(Family family, const char *addrString, void *addrbuf) @@ -111,7 +116,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) INT len = sizeof(saddr); - if (WSAStringToAddress((LPTSTR)addrString, AF_INET, NULL, (LPSOCKADDR)&saddr, &len)) { + if (WSAStringToAddress((LPTSTR)addrString, AF_INET, nullptr, (LPSOCKADDR)&saddr, &len)) { return 0; } @@ -124,7 +129,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) INT len = sizeof(saddr); - if (WSAStringToAddress((LPTSTR)addrString, AF_INET6, NULL, (LPSOCKADDR)&saddr, &len)) { + if (WSAStringToAddress((LPTSTR)addrString, AF_INET6, nullptr, (LPSOCKADDR)&saddr, &len)) { return 0; } @@ -182,14 +187,14 @@ const IP6 IP6_BROADCAST = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; -IP4 get_ip4_loopback() +IP4 get_ip4_loopback(void) { IP4 loopback; loopback.uint32 = htonl(INADDR_LOOPBACK); return loopback; } -IP6 get_ip6_loopback() +IP6 get_ip6_loopback(void) { IP6 loopback; get_ip6(&loopback, &in6addr_loopback); @@ -303,7 +308,7 @@ static uint64_t current_time_actual(void) return time / 10; #else struct timeval a; - gettimeofday(&a, NULL); + gettimeofday(&a, nullptr); time = 1000000ULL * a.tv_sec + a.tv_usec; return time; #endif @@ -371,18 +376,18 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, char ip_str[IP_NTOA_LEN]; if (res < 0) { /* Windows doesn't necessarily know %zu */ - LOGGER_TRACE(log, "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", - buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', + LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", + buffer[0], message, (buflen < 999 ? buflen : 999), 'E', ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), errno, strerror(errno), data_0(buflen, buffer), data_1(buflen, buffer)); } else if ((res > 0) && ((size_t)res <= buflen)) { - LOGGER_TRACE(log, "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", - buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), + LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", + buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='), ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, buffer)); } else { /* empty or overwrite */ - LOGGER_TRACE(log, "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", - buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, + LOGGER_TRACE(log, "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x", + buffer[0], message, res, (!res ? '!' : '>'), buflen, ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, buffer)); } @@ -447,7 +452,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 ip6.uint32[0] = 0; ip6.uint32[1] = 0; ip6.uint32[2] = net_htonl(0xFFFF); - ip6.uint32[3] = ip_port.ip.ip4.uint32; + ip6.uint32[3] = ip_port.ip.ip.v4.uint32; fill_addr6(ip6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; @@ -457,7 +462,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); addr4->sin_port = ip_port.port; } } else if (ip_port.ip.family == TOX_AF_INET6) { @@ -466,7 +471,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; addr6->sin6_port = ip_port.port; - fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -514,17 +519,17 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; ip_port->ip.family = make_tox_family(addr_in->sin_family); - get_ip4(&ip_port->ip.ip4, &addr_in->sin_addr); + get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); ip_port->port = addr_in->sin_port; } else if (addr.ss_family == AF_INET6) { struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; ip_port->ip.family = make_tox_family(addr_in6->sin6_family); - get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr); + get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); ip_port->port = addr_in6->sin6_port; - if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip_port->ip.ip.v6)) { ip_port->ip.family = TOX_AF_INET; - ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; + ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; } } else { return -1; @@ -621,7 +626,7 @@ static void at_shutdown(void) */ Networking_Core *new_networking(Logger *log, IP ip, uint16_t port) { - return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), 0); + return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); } /* Initialize networking. @@ -660,17 +665,17 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) { LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family); - return NULL; + return nullptr; } if (networking_at_startup() != 0) { - return NULL; + return nullptr; } Networking_Core *temp = (Networking_Core *)calloc(1, sizeof(Networking_Core)); - if (temp == NULL) { - return NULL; + if (temp == nullptr) { + return nullptr; } temp->log = log; @@ -690,7 +695,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 *error = 1; } - return NULL; + return nullptr; } /* Functions to increase the size of the send and receive UDP buffers. @@ -711,7 +716,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 *error = 1; } - return NULL; + return nullptr; } /* Set socket nonblocking. */ @@ -722,11 +727,11 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 *error = 1; } - return NULL; + return nullptr; } /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ - uint16_t *portptr = NULL; + uint16_t *portptr = nullptr; struct sockaddr_storage addr; size_t addrsize; @@ -738,7 +743,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; addr4->sin_port = 0; - fill_addr4(ip.ip4, &addr4->sin_addr); + fill_addr4(ip.ip.v4, &addr4->sin_addr); portptr = &addr4->sin_port; } else if (temp->family == TOX_AF_INET6) { @@ -747,7 +752,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; addr6->sin6_port = 0; - fill_addr6(ip.ip6, &addr6->sin6_addr); + fill_addr6(ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -755,7 +760,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 portptr = &addr6->sin6_port; } else { free(temp); - return NULL; + return nullptr; } if (ip.family == TOX_AF_INET6) { @@ -838,7 +843,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 *error = 1; } - return NULL; + return nullptr; } Networking_Core *new_networking_no_udp(Logger *log) @@ -846,8 +851,8 @@ Networking_Core *new_networking_no_udp(Logger *log) /* this is the easiest way to completely disable UDP without changing too much code. */ Networking_Core *net = (Networking_Core *)calloc(1, sizeof(Networking_Core)); - if (net == NULL) { - return NULL; + if (net == nullptr) { + return nullptr; } net->log = log; @@ -887,14 +892,14 @@ int ip_equal(const IP *a, const IP *b) if (a->family == TOX_AF_INET || a->family == TCP_INET) { struct in_addr addr_a; struct in_addr addr_b; - fill_addr4(a->ip4, &addr_a); - fill_addr4(b->ip4, &addr_b); + fill_addr4(a->ip.v4, &addr_a); + fill_addr4(b->ip.v4, &addr_b); return addr_a.s_addr == addr_b.s_addr; } if (a->family == TOX_AF_INET6 || a->family == TCP_INET6) { - return a->ip6.uint64[0] == b->ip6.uint64[0] && - a->ip6.uint64[1] == b->ip6.uint64[1]; + return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && + a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; } return 0; @@ -902,16 +907,16 @@ int ip_equal(const IP *a, const IP *b) /* different family: check on the IPv6 one if it is the IPv4 one embedded */ if ((a->family == TOX_AF_INET) && (b->family == TOX_AF_INET6)) { - if (IPV6_IPV4_IN_V6(b->ip6)) { + if (IPV6_IPV4_IN_V6(b->ip.v6)) { struct in_addr addr_a; - fill_addr4(a->ip4, &addr_a); - return addr_a.s_addr == b->ip6.uint32[3]; + fill_addr4(a->ip.v4, &addr_a); + return addr_a.s_addr == b->ip.v6.uint32[3]; } } else if ((a->family == TOX_AF_INET6) && (b->family == TOX_AF_INET)) { - if (IPV6_IPV4_IN_V6(a->ip6)) { + if (IPV6_IPV4_IN_V6(a->ip.v6)) { struct in_addr addr_b; - fill_addr4(b->ip4, &addr_b); - return a->ip6.uint32[3] == addr_b.s_addr; + fill_addr4(b->ip.v4, &addr_b); + return a->ip.v6.uint32[3] == addr_b.s_addr; } } @@ -948,7 +953,7 @@ void ip_reset(IP *ip) } /* nulls out ip, sets family according to flag */ -void ip_init(IP *ip, uint8_t ipv6enabled) +void ip_init(IP *ip, bool ipv6enabled) { if (!ip) { return; @@ -1024,14 +1029,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) if (ip->family == TOX_AF_INET) { /* returns standard quad-dotted notation */ struct in_addr addr; - fill_addr4(ip->ip4, &addr); + fill_addr4(ip->ip.v4, &addr); ip_str[0] = 0; inet_ntop(family, &addr, ip_str, length); } else if (ip->family == TOX_AF_INET6) { /* returns hex-groups enclosed into square brackets */ struct in6_addr addr; - fill_addr6(ip->ip6, &addr); + fill_addr6(ip->ip.v6, &addr); ip_str[0] = '['; inet_ntop(family, &addr, &ip_str[1], length - 3); @@ -1072,13 +1077,13 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) } if (ip->family == TOX_AF_INET) { - const struct in_addr *addr = (const struct in_addr *)&ip->ip4; - return inet_ntop(ip->family, addr, address, length) != NULL; + const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; + return inet_ntop(ip->family, addr, address, length) != nullptr; } if (ip->family == TOX_AF_INET6) { - const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; - return inet_ntop(ip->family, addr, address, length) != NULL; + const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; + return inet_ntop(ip->family, addr, address, length) != nullptr; } return 0; @@ -1107,7 +1112,7 @@ int addr_parse_ip(const char *address, IP *to) if (inet_pton(AF_INET, address, &addr4) == 1) { to->family = TOX_AF_INET; - get_ip4(&to->ip4, &addr4); + get_ip4(&to->ip.v4, &addr4); return 1; } @@ -1115,7 +1120,7 @@ int addr_parse_ip(const char *address, IP *to) if (inet_pton(AF_INET6, address, &addr6) == 1) { to->family = TOX_AF_INET6; - get_ip6(&to->ip6, &addr6); + get_ip6(&to->ip.v6, &addr6); return 1; } @@ -1148,8 +1153,8 @@ int addr_resolve(const char *address, IP *to, IP *extra) Family tox_family = to->family; Family family = make_family(tox_family); - struct addrinfo *server = NULL; - struct addrinfo *walker = NULL; + struct addrinfo *server = nullptr; + struct addrinfo *walker = nullptr; struct addrinfo hints; int rc; int result = 0; @@ -1163,7 +1168,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) return 0; } - rc = getaddrinfo(address, NULL, &hints, &server); + rc = getaddrinfo(address, nullptr, &hints, &server); // Lookup failed. if (rc != 0) { @@ -1175,17 +1180,17 @@ int addr_resolve(const char *address, IP *to, IP *extra) IP ip6; ip_init(&ip6, 1); // ipv6enabled = 1 - for (walker = server; (walker != NULL) && !done; walker = walker->ai_next) { + for (walker = server; (walker != nullptr) && !done; walker = walker->ai_next) { switch (walker->ai_family) { case AF_INET: if (walker->ai_family == family) { /* AF_INET requested, done */ struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; - get_ip4(&to->ip4, &addr->sin_addr); + get_ip4(&to->ip.v4, &addr->sin_addr); result = TOX_ADDR_RESOLVE_INET; done = 1; } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */ struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; - get_ip4(&ip4.ip4, &addr->sin_addr); + get_ip4(&ip4.ip.v4, &addr->sin_addr); result |= TOX_ADDR_RESOLVE_INET; } @@ -1195,14 +1200,14 @@ int addr_resolve(const char *address, IP *to, IP *extra) if (walker->ai_family == family) { /* AF_INET6 requested, done */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - get_ip6(&to->ip6, &addr->sin6_addr); + get_ip6(&to->ip.v6, &addr->sin6_addr); result = TOX_ADDR_RESOLVE_INET6; done = 1; } } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - get_ip6(&ip6.ip6, &addr->sin6_addr); + get_ip6(&ip6.ip.v6, &addr->sin6_addr); result |= TOX_ADDR_RESOLVE_INET6; } } @@ -1215,7 +1220,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) if (result & TOX_ADDR_RESOLVE_INET6) { ip_copy(to, &ip6); - if ((result & TOX_ADDR_RESOLVE_INET) && (extra != NULL)) { + if ((result & TOX_ADDR_RESOLVE_INET) && (extra != nullptr)) { ip_copy(extra, &ip4); } } else if (result & TOX_ADDR_RESOLVE_INET) { @@ -1265,14 +1270,14 @@ int net_connect(Socket sock, IP_Port ip_port) addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); addr4->sin_port = ip_port.port; } else if (ip_port.ip.family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; - fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_port = ip_port.port; } else { return 0; @@ -1284,8 +1289,8 @@ int net_connect(Socket sock, IP_Port ip_port) int32_t net_getipport(const char *node, IP_Port **res, int tox_type) { struct addrinfo *infos; - int ret = getaddrinfo(node, NULL, NULL, &infos); - *res = NULL; + int ret = getaddrinfo(node, nullptr, nullptr, &infos); + *res = nullptr; if (ret != 0) { return -1; @@ -1295,9 +1300,9 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) const size_t MAX_COUNT = MIN(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); int type = make_socktype(tox_type); struct addrinfo *cur; - int32_t count = 0; + size_t count = 0; - for (cur = infos; count < MAX_COUNT && cur != NULL; cur = cur->ai_next) { + for (cur = infos; count < MAX_COUNT && cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { continue; } @@ -1318,24 +1323,24 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) *res = (IP_Port *)malloc(sizeof(IP_Port) * count); - if (*res == NULL) { + if (*res == nullptr) { freeaddrinfo(infos); return -1; } IP_Port *ip_port = *res; - for (cur = infos; cur != NULL; cur = cur->ai_next) { + for (cur = infos; cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { continue; } if (cur->ai_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)cur->ai_addr; - memcpy(&ip_port->ip.ip4, &addr->sin_addr, sizeof(IP4)); + memcpy(&ip_port->ip.ip.v4, &addr->sin_addr, sizeof(IP4)); } else if (cur->ai_family == AF_INET6) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)cur->ai_addr; - memcpy(&ip_port->ip.ip6, &addr->sin6_addr, sizeof(IP6)); + memcpy(&ip_port->ip.ip.v6, &addr->sin6_addr, sizeof(IP6)); } else { continue; } @@ -1471,3 +1476,54 @@ uint16_t net_ntohs(uint16_t hostshort) { return ntohs(hostshort); } + +size_t net_pack_u16(uint8_t *bytes, uint16_t v) +{ + bytes[0] = (v >> 8) & 0xff; + bytes[1] = v & 0xff; + return sizeof(v); +} + +size_t net_pack_u32(uint8_t *bytes, uint32_t v) +{ + uint8_t *p = bytes; + p += net_pack_u16(p, (v >> 16) & 0xffff); + p += net_pack_u16(p, v & 0xffff); + return p - bytes; +} + +size_t net_pack_u64(uint8_t *bytes, uint64_t v) +{ + uint8_t *p = bytes; + p += net_pack_u32(p, (v >> 32) & 0xffffffff); + p += net_pack_u32(p, v & 0xffffffff); + return p - bytes; +} + +size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v) +{ + uint8_t hi = bytes[0]; + uint8_t lo = bytes[1]; + *v = ((uint16_t)hi << 8) | lo; + return sizeof(*v); +} + +size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v) +{ + const uint8_t *p = bytes; + uint16_t lo, hi; + p += net_unpack_u16(p, &hi); + p += net_unpack_u16(p, &lo); + *v = ((uint32_t)hi << 16) | lo; + return p - bytes; +} + +size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v) +{ + const uint8_t *p = bytes; + uint32_t lo, hi; + p += net_unpack_u32(p, &hi); + p += net_unpack_u32(p, &lo); + *v = ((uint64_t)hi << 32) | lo; + return p - bytes; +} diff --git a/protocols/Tox/libtox/src/toxcore/network.h b/protocols/Tox/libtox/src/toxcore/network.h index d3b80218b0..405721b290 100644 --- a/protocols/Tox/libtox/src/toxcore/network.h +++ b/protocols/Tox/libtox/src/toxcore/network.h @@ -33,6 +33,7 @@ #include "ccompat.h" #include "logger.h" +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -63,6 +64,10 @@ #endif +#ifdef __cplusplus +extern "C" { +#endif + typedef short Family; typedef int Socket; @@ -125,17 +130,16 @@ typedef enum NET_PACKET_TYPE { #define TCP_INET6 (TOX_AF_INET6 + 3) #define TCP_FAMILY (TOX_AF_INET6 + 4) -typedef union { +typedef union IP4 { uint32_t uint32; uint16_t uint16[2]; uint8_t uint8[4]; -} -IP4; +} IP4; IP4 get_ip4_loopback(void); extern const IP4 IP4_BROADCAST; -typedef union { +typedef union IP6 { uint8_t uint8[16]; uint16_t uint16[8]; uint32_t uint32[4]; @@ -146,12 +150,12 @@ IP6 get_ip6_loopback(void); extern const IP6 IP6_BROADCAST; #define IP_DEFINED -typedef struct { +typedef struct IP { uint8_t family; - GNU_EXTENSION union { - IP4 ip4; - IP6 ip6; - }; + union { + IP4 v4; + IP6 v6; + } ip; } IP; #define IP_PORT_DEFINED @@ -167,6 +171,14 @@ uint16_t net_htons(uint16_t hostshort); uint32_t net_ntohl(uint32_t hostlong); uint16_t net_ntohs(uint16_t hostshort); +size_t net_pack_u16(uint8_t *bytes, uint16_t v); +size_t net_pack_u32(uint8_t *bytes, uint32_t v); +size_t net_pack_u64(uint8_t *bytes, uint64_t v); + +size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v); +size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v); +size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v); + /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ #define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == net_htonl (0xffff))) @@ -176,7 +188,7 @@ uint16_t net_ntohs(uint16_t hostshort); #define SIZE_PORT 2 #define SIZE_IPPORT (SIZE_IP + SIZE_PORT) -#define TOX_ENABLE_IPV6_DEFAULT 1 +#define TOX_ENABLE_IPV6_DEFAULT true /* addr_resolve return values */ #define TOX_ADDR_RESOLVE_INET 1 @@ -249,7 +261,7 @@ int ipport_equal(const IP_Port *a, const IP_Port *b); /* nulls out ip */ void ip_reset(IP *ip); /* nulls out ip, sets family according to flag */ -void ip_init(IP *ip, uint8_t ipv6enabled); +void ip_init(IP *ip, bool ipv6enabled); /* checks if ip is valid */ int ip_isset(const IP *ip); /* checks if ip is valid */ @@ -409,4 +421,8 @@ Networking_Core *new_networking_no_udp(Logger *log); /* Function to cleanup networking stuff (doesn't do much right now). */ void kill_networking(Networking_Core *net); +#ifdef __cplusplus +} // extern "C" +#endif + #endif diff --git a/protocols/Tox/libtox/src/toxcore/onion.c b/protocols/Tox/libtox/src/toxcore/onion.c index fbaf7205d9..48cc4d768c 100644 --- a/protocols/Tox/libtox/src/toxcore/onion.c +++ b/protocols/Tox/libtox/src/toxcore/onion.c @@ -55,9 +55,9 @@ static void ip_pack(uint8_t *data, IP source) if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) { memset(data + 1, 0, SIZE_IP6); - memcpy(data + 1, source.ip4.uint8, SIZE_IP4); + memcpy(data + 1, source.ip.v4.uint8, SIZE_IP4); } else { - memcpy(data + 1, source.ip6.uint8, SIZE_IP6); + memcpy(data + 1, source.ip.v6.uint8, SIZE_IP6); } } @@ -71,9 +71,9 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo target->family = data[0]; if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) { - memcpy(target->ip4.uint8, data + 1, SIZE_IP4); + memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4); } else { - memcpy(target->ip6.uint8, data + 1, SIZE_IP6); + memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6); } bool valid = disable_family_check || @@ -120,8 +120,8 @@ int create_onion_path(const DHT *dht, Onion_Path *new_path, const Node_format *n return -1; } - encrypt_precompute(nodes[0].public_key, dht->self_secret_key, new_path->shared_key1); - memcpy(new_path->public_key1, dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + encrypt_precompute(nodes[0].public_key, dht_get_self_secret_key(dht), new_path->shared_key1); + memcpy(new_path->public_key1, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE); uint8_t random_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t random_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -338,7 +338,7 @@ static int handle_send_initial(void *object, IP_Port source, const uint8_t *pack uint8_t plain[ONION_MAX_PACKET_SIZE]; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; - get_shared_key(&onion->shared_keys_1, shared_key, onion->dht->self_secret_key, packet + 1 + CRYPTO_NONCE_SIZE); + get_shared_key(&onion->shared_keys_1, shared_key, dht_get_self_secret_key(onion->dht), packet + 1 + CRYPTO_NONCE_SIZE); int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE), plain); @@ -407,7 +407,7 @@ static int handle_send_1(void *object, IP_Port source, const uint8_t *packet, ui uint8_t plain[ONION_MAX_PACKET_SIZE]; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; - get_shared_key(&onion->shared_keys_2, shared_key, onion->dht->self_secret_key, packet + 1 + CRYPTO_NONCE_SIZE); + get_shared_key(&onion->shared_keys_2, shared_key, dht_get_self_secret_key(onion->dht), packet + 1 + CRYPTO_NONCE_SIZE); int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_1), plain); @@ -463,7 +463,7 @@ static int handle_send_2(void *object, IP_Port source, const uint8_t *packet, ui uint8_t plain[ONION_MAX_PACKET_SIZE]; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; - get_shared_key(&onion->shared_keys_3, shared_key, onion->dht->self_secret_key, packet + 1 + CRYPTO_NONCE_SIZE); + get_shared_key(&onion->shared_keys_3, shared_key, dht_get_self_secret_key(onion->dht), packet + 1 + CRYPTO_NONCE_SIZE); int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + RETURN_2), plain); @@ -635,18 +635,18 @@ void set_callback_handle_recv_1(Onion *onion, int (*function)(void *, IP_Port, c Onion *new_onion(DHT *dht) { - if (dht == NULL) { - return NULL; + if (dht == nullptr) { + return nullptr; } Onion *onion = (Onion *)calloc(1, sizeof(Onion)); - if (onion == NULL) { - return NULL; + if (onion == nullptr) { + return nullptr; } onion->dht = dht; - onion->net = dht->net; + onion->net = dht_get_net(dht); new_symmetric_key(onion->secret_symmetric_key); onion->timestamp = unix_time(); @@ -663,17 +663,17 @@ Onion *new_onion(DHT *dht) void kill_onion(Onion *onion) { - if (onion == NULL) { + if (onion == nullptr) { return; } - networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, NULL, NULL); - networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, NULL, NULL); - networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, NULL, NULL); + networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, nullptr, nullptr); + networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, nullptr, nullptr); + networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, nullptr, nullptr); - networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, NULL, NULL); - networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, NULL, NULL); - networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_1, NULL, NULL); + networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_3, nullptr, nullptr); + networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_2, nullptr, nullptr); + networking_registerhandler(onion->net, NET_PACKET_ONION_RECV_1, nullptr, nullptr); free(onion); } diff --git a/protocols/Tox/libtox/src/toxcore/onion_announce.c b/protocols/Tox/libtox/src/toxcore/onion_announce.c index ab96a546fb..8e49f7bdc7 100644 --- a/protocols/Tox/libtox/src/toxcore/onion_announce.c +++ b/protocols/Tox/libtox/src/toxcore/onion_announce.c @@ -87,7 +87,7 @@ int create_announce_request(uint8_t *packet, uint16_t max_packet_length, const u } uint8_t plain[ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_PUBLIC_KEY_SIZE + - ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; memcpy(plain, ping_id, ONION_PING_ID_SIZE); memcpy(plain + ONION_PING_ID_SIZE, client_id, CRYPTO_PUBLIC_KEY_SIZE); memcpy(plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE, data_public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -332,10 +332,8 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const ui int pos = in_entries(onion_a, public_key); - unsigned int i; - if (pos == -1) { - for (i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) { + for (unsigned i = 0; i < ONION_ANNOUNCE_MAX_ENTRIES; ++i) { if (is_timeout(onion_a->entries[i].time, ONION_ANNOUNCE_TIMEOUT)) { pos = i; } @@ -343,7 +341,7 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const ui } if (pos == -1) { - if (id_closest(onion_a->dht->self_public_key, public_key, onion_a->entries[0].public_key) == 1) { + if (id_closest(dht_get_self_public_key(onion_a->dht), public_key, onion_a->entries[0].public_key) == 1) { pos = 0; } } @@ -358,7 +356,7 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const ui memcpy(onion_a->entries[pos].data_public_key, data_public_key, CRYPTO_PUBLIC_KEY_SIZE); onion_a->entries[pos].time = unix_time(); - sort_onion_announce_list(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, onion_a->dht->self_public_key); + sort_onion_announce_list(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, dht_get_self_public_key(onion_a->dht)); return in_entries(onion_a, public_key); } @@ -372,10 +370,10 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * const uint8_t *packet_public_key = packet + 1 + CRYPTO_NONCE_SIZE; uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; - get_shared_key(&onion_a->shared_keys_recv, shared_key, onion_a->dht->self_secret_key, packet_public_key); + get_shared_key(&onion_a->shared_keys_recv, shared_key, dht_get_self_secret_key(onion_a->dht), packet_public_key); uint8_t plain[ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_PUBLIC_KEY_SIZE + - ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH]; int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_PUBLIC_KEY_SIZE + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_MAC_SIZE, plain); @@ -390,7 +388,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * uint8_t ping_id2[ONION_PING_ID_SIZE]; generate_ping_id(onion_a, unix_time() + PING_ID_TIMEOUT, packet_public_key, source, ping_id2); - int index = -1; + int index; uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE; @@ -493,18 +491,18 @@ static int handle_data_request(void *object, IP_Port source, const uint8_t *pack Onion_Announce *new_onion_announce(DHT *dht) { - if (dht == NULL) { - return NULL; + if (dht == nullptr) { + return nullptr; } Onion_Announce *onion_a = (Onion_Announce *)calloc(1, sizeof(Onion_Announce)); - if (onion_a == NULL) { - return NULL; + if (onion_a == nullptr) { + return nullptr; } onion_a->dht = dht; - onion_a->net = dht->net; + onion_a->net = dht_get_net(dht); new_symmetric_key(onion_a->secret_bytes); networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_announce_request, onion_a); @@ -515,11 +513,11 @@ Onion_Announce *new_onion_announce(DHT *dht) void kill_onion_announce(Onion_Announce *onion_a) { - if (onion_a == NULL) { + if (onion_a == nullptr) { return; } - networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, NULL, NULL); - networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, NULL, NULL); + networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, nullptr, nullptr); + networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, nullptr, nullptr); free(onion_a); } diff --git a/protocols/Tox/libtox/src/toxcore/onion_client.c b/protocols/Tox/libtox/src/toxcore/onion_client.c index f7ff41bd7a..c4eb3fc175 100644 --- a/protocols/Tox/libtox/src/toxcore/onion_client.c +++ b/protocols/Tox/libtox/src/toxcore/onion_client.c @@ -280,7 +280,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format if (num_nodes >= 2) { nodes[0].ip_port.ip.family = TCP_FAMILY; - nodes[0].ip_port.ip.ip4.uint32 = random_tcp; + nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { nodes[i] = onion_c->path_nodes[rand() % num_nodes]; @@ -294,7 +294,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format } nodes[0].ip_port.ip.family = TCP_FAMILY; - nodes[0].ip_port.ip.ip4.uint32 = random_tcp; + nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs]; @@ -485,7 +485,7 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa return -1; } - return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len); + return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip.v4.uint32, packet, len); } return -1; @@ -579,7 +579,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0}; - if (ping_id == NULL) { + if (ping_id == nullptr) { ping_id = zero_ping_id; } @@ -670,8 +670,8 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t return -1; } - Onion_Node *list_nodes = NULL; - const uint8_t *reference_id = NULL; + Onion_Node *list_nodes = nullptr; + const uint8_t *reference_id = nullptr; unsigned int list_length; if (num == 0) { @@ -772,12 +772,12 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for return 0; } - Onion_Node *list_nodes = NULL; - const uint8_t *reference_id = NULL; + Onion_Node *list_nodes = nullptr; + const uint8_t *reference_id = nullptr; unsigned int list_length; - Last_Pinged *last_pinged = NULL; - uint8_t *last_pinged_index = NULL; + Last_Pinged *last_pinged = nullptr; + uint8_t *last_pinged_index = nullptr; if (num == 0) { list_nodes = onion_c->clients_announce_list; @@ -816,7 +816,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for } if (j == list_length && good_to_ping(last_pinged, last_pinged_index, nodes[i].public_key)) { - client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].public_key, NULL, ~0); + client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].public_key, nullptr, ~0); } } } @@ -845,7 +845,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t } VLA(uint8_t, plain, 1 + ONION_PING_ID_SIZE + len_nodes); - int len = -1; + int len; if (num == 0) { len = decrypt_data(public_key, nc_get_self_secret_key(onion_c->c), @@ -875,7 +875,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t if (len_nodes != 0) { Node_format nodes[MAX_SENT_NODES]; - int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, plain + 1 + ONION_PING_ID_SIZE, len_nodes, 0); + int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, plain + 1 + ONION_PING_ID_SIZE, len_nodes, 0); if (num_nodes <= 0) { return 1; @@ -974,7 +974,7 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con if (len_nodes != 0) { Node_format nodes[MAX_SENT_NODES]; - int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + CRYPTO_PUBLIC_KEY_SIZE, + int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, data + 1 + sizeof(uint64_t) + CRYPTO_PUBLIC_KEY_SIZE, len_nodes, 1); if (num_nodes <= 0) { @@ -1133,7 +1133,7 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin } uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; - len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet, + len = create_request(dht_get_self_public_key(onion_c->dht), dht_get_self_secret_key(onion_c->dht), packet, onion_c->friends_list[friend_num].dht_public_key, temp, SIZEOF_VLA(temp), CRYPTO_PACKET_DHTPK); if (len == -1) { @@ -1192,7 +1192,7 @@ static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8 uint64_t no_replay = unix_time(); host_to_net((uint8_t *)&no_replay, sizeof(no_replay)); memcpy(data + 1, &no_replay, sizeof(no_replay)); - memcpy(data + 1 + sizeof(uint64_t), onion_c->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(data + 1 + sizeof(uint64_t), dht_get_self_public_key(onion_c->dht), CRYPTO_PUBLIC_KEY_SIZE); Node_format nodes[MAX_SENT_NODES]; uint16_t num_relays = copy_connected_tcp_relays(onion_c->c, nodes, (MAX_SENT_NODES / 2)); uint16_t num_nodes = closelist_nodes(onion_c->dht, &nodes[num_relays], MAX_SENT_NODES - num_relays); @@ -1260,13 +1260,13 @@ static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num) { if (num == 0) { free(onion_c->friends_list); - onion_c->friends_list = NULL; + onion_c->friends_list = nullptr; return 0; } Onion_Friend *newonion_friends = (Onion_Friend *)realloc(onion_c->friends_list, num * sizeof(Onion_Friend)); - if (newonion_friends == NULL) { + if (newonion_friends == nullptr) { return -1; } @@ -1351,14 +1351,14 @@ int onion_delfriend(Onion_Client *onion_c, int friend_num) * return -1 on failure. * return 0 on success. */ -int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, +int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*callback)(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number) { if ((uint32_t)friend_num >= onion_c->num_friends) { return -1; } - onion_c->friends_list[friend_num].tcp_relay_node_callback = tcp_relay_node_callback; + onion_c->friends_list[friend_num].tcp_relay_node_callback = callback; onion_c->friends_list[friend_num].tcp_relay_node_callback_object = object; onion_c->friends_list[friend_num].tcp_relay_node_callback_number = number; return 0; @@ -1551,15 +1551,15 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) } } - unsigned int i, count = 0; - Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; - if (!onion_c->friends_list[friendnum].is_online) { + unsigned int count = 0; + Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; + // ensure we get a response from some node roughly once per // (interval / MAX_ONION_CLIENTS) bool ping_random = true; - for (i = 0; i < MAX_ONION_CLIENTS; ++i) { + for (unsigned i = 0; i < MAX_ONION_CLIENTS; ++i) { if (!(is_timeout(list_nodes[i].timestamp, interval / MAX_ONION_CLIENTS) && is_timeout(list_nodes[i].last_pinged, ONION_NODE_PING_INTERVAL))) { ping_random = false; @@ -1567,7 +1567,7 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) } } - for (i = 0; i < MAX_ONION_CLIENTS; ++i) { + for (unsigned i = 0; i < MAX_ONION_CLIENTS; ++i) { if (onion_node_timed_out(&list_nodes[i])) { continue; } @@ -1586,7 +1586,8 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) if (is_timeout(list_nodes[i].last_pinged, interval) || (ping_random && rand() % (MAX_ONION_CLIENTS - i) == 0)) { - if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, list_nodes[i].public_key, 0, ~0) == 0) { + if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, + list_nodes[i].public_key, nullptr, ~0) == 0) { list_nodes[i].last_pinged = unix_time(); ++list_nodes[i].unsuccessful_pings; ping_random = false; @@ -1610,7 +1611,7 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) for (j = 0; j < n; ++j) { unsigned int num = rand() % num_nodes; client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port, - onion_c->path_nodes[num].public_key, 0, ~0); + onion_c->path_nodes[num].public_key, nullptr, ~0); } ++onion_c->friends_list[friendnum].run_count; @@ -1729,7 +1730,7 @@ static void do_announce(Onion_Client *onion_c) if (num_nodes != 0) { for (i = 0; i < (MAX_ONION_CLIENTS_ANNOUNCE / 2); ++i) { unsigned int num = rand() % num_nodes; - client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].public_key, 0, ~0); + client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].public_key, nullptr, ~0); } } } @@ -1799,8 +1800,6 @@ unsigned int onion_connection_status(const Onion_Client *onion_c) void do_onion_client(Onion_Client *onion_c) { - unsigned int i; - if (onion_c->last_run == unix_time()) { return; } @@ -1832,7 +1831,7 @@ void do_onion_client(Onion_Client *onion_c) || 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 (i = 0; i < onion_c->num_friends; ++i) { + for (unsigned i = 0; i < onion_c->num_friends; ++i) { do_friend(onion_c, i); } } @@ -1846,25 +1845,25 @@ void do_onion_client(Onion_Client *onion_c) Onion_Client *new_onion_client(Net_Crypto *c) { - if (c == NULL) { - return NULL; + if (c == nullptr) { + return nullptr; } Onion_Client *onion_c = (Onion_Client *)calloc(1, sizeof(Onion_Client)); - if (onion_c == NULL) { - return NULL; + if (onion_c == nullptr) { + return nullptr; } onion_c->announce_ping_array = ping_array_new(ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT); - if (onion_c->announce_ping_array == NULL) { + if (onion_c->announce_ping_array == nullptr) { free(onion_c); - return NULL; + return nullptr; } onion_c->dht = nc_get_dht(c); - onion_c->net = onion_c->dht->net; + onion_c->net = dht_get_net(onion_c->dht); onion_c->c = c; new_symmetric_key(onion_c->secret_symmetric_key); crypto_new_keypair(onion_c->temp_public_key, onion_c->temp_secret_key); @@ -1879,17 +1878,17 @@ Onion_Client *new_onion_client(Net_Crypto *c) void kill_onion_client(Onion_Client *onion_c) { - if (onion_c == NULL) { + if (onion_c == nullptr) { return; } ping_array_kill(onion_c->announce_ping_array); realloc_onion_friends(onion_c, 0); - networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, NULL, NULL); - networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); - oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL); - cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL); - set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), NULL, NULL); + networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, nullptr, nullptr); + networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, nullptr, nullptr); + oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, nullptr, nullptr); + cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, nullptr, nullptr); + set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), nullptr, nullptr); crypto_memzero(onion_c, sizeof(Onion_Client)); free(onion_c); } diff --git a/protocols/Tox/libtox/src/toxcore/ping.c b/protocols/Tox/libtox/src/toxcore/ping.c index f2f560136f..bede07a117 100644 --- a/protocols/Tox/libtox/src/toxcore/ping.c +++ b/protocols/Tox/libtox/src/toxcore/ping.c @@ -64,7 +64,7 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) int rc; uint64_t ping_id; - if (id_equal(public_key, ping->dht->self_public_key)) { + if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { return 1; } @@ -87,7 +87,7 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); pk[0] = NET_PACKET_PING_REQUEST; - id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey + id_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce @@ -100,7 +100,7 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key) return 1; } - return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); + return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); } static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id, @@ -109,7 +109,7 @@ static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key uint8_t pk[DHT_PING_SIZE]; int rc; - if (id_equal(public_key, ping->dht->self_public_key)) { + if (id_equal(public_key, dht_get_self_public_key(ping->dht))) { return 1; } @@ -118,7 +118,7 @@ static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); pk[0] = NET_PACKET_PING_RESPONSE; - id_copy(pk + 1, ping->dht->self_public_key); // Our pubkey + id_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey random_nonce(pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce // Encrypt ping_id using recipient privkey @@ -131,7 +131,7 @@ static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key return 1; } - return sendpacket(ping->dht->net, ipp, pk, sizeof(pk)); + return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); } static int handle_ping_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) @@ -143,9 +143,9 @@ static int handle_ping_request(void *object, IP_Port source, const uint8_t *pack return 1; } - Ping *ping = dht->ping; + Ping *ping = dht_get_ping(dht); - if (id_equal(packet + 1, ping->dht->self_public_key)) { + if (id_equal(packet + 1, dht_get_self_public_key(ping->dht))) { return 1; } @@ -186,9 +186,9 @@ static int handle_ping_response(void *object, IP_Port source, const uint8_t *pac return 1; } - Ping *ping = dht->ping; + Ping *ping = dht_get_ping(dht); - if (id_equal(packet + 1, ping->dht->self_public_key)) { + if (id_equal(packet + 1, dht_get_self_public_key(ping->dht))) { return 1; } @@ -284,7 +284,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port) return -1; } - if (in_list(ping->dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) { + if (in_list(dht_get_close_clientlist(ping->dht), LCLIENT_LIST, public_key, ip_port)) { return -1; } @@ -309,7 +309,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, IP_Port ip_port) } } - if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, ping->dht->self_public_key)) { + if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, dht_get_self_public_key(ping->dht))) { return 0; } @@ -355,28 +355,28 @@ Ping *ping_new(DHT *dht) { Ping *ping = (Ping *)calloc(1, sizeof(Ping)); - if (ping == NULL) { - return NULL; + if (ping == nullptr) { + return nullptr; } ping->ping_array = ping_array_new(PING_NUM_MAX, PING_TIMEOUT); - if (ping->ping_array == NULL) { + if (ping->ping_array == nullptr) { free(ping); - return NULL; + return nullptr; } ping->dht = dht; - networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht); - networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); + networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, &handle_ping_request, dht); + networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); return ping; } void ping_kill(Ping *ping) { - networking_registerhandler(ping->dht->net, NET_PACKET_PING_REQUEST, NULL, NULL); - networking_registerhandler(ping->dht->net, NET_PACKET_PING_RESPONSE, NULL, NULL); + networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, nullptr, nullptr); + networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, nullptr, nullptr); ping_array_kill(ping->ping_array); free(ping); diff --git a/protocols/Tox/libtox/src/toxcore/ping_array.c b/protocols/Tox/libtox/src/toxcore/ping_array.c index 627f8d5a64..a54ebfe209 100644 --- a/protocols/Tox/libtox/src/toxcore/ping_array.c +++ b/protocols/Tox/libtox/src/toxcore/ping_array.c @@ -57,20 +57,20 @@ struct Ping_Array { Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) { if (size == 0 || timeout == 0) { - return NULL; + return nullptr; } Ping_Array *empty_array = (Ping_Array *)calloc(1, sizeof(Ping_Array)); - if (empty_array == NULL) { - return NULL; + if (empty_array == nullptr) { + return nullptr; } empty_array->entries = (Ping_Array_Entry *)calloc(size, sizeof(Ping_Array_Entry)); - if (empty_array->entries == NULL) { + if (empty_array->entries == nullptr) { free(empty_array); - return NULL; + return nullptr; } empty_array->last_deleted = empty_array->last_added = 0; @@ -82,7 +82,7 @@ Ping_Array *ping_array_new(uint32_t size, uint32_t timeout) static void clear_entry(Ping_Array *array, uint32_t index) { free(array->entries[index].data); - array->entries[index].data = NULL; + array->entries[index].data = nullptr; array->entries[index].length = array->entries[index].time = array->entries[index].ping_id = 0; @@ -128,14 +128,14 @@ uint64_t ping_array_add(Ping_Array *array, const uint8_t *data, uint32_t length) ping_array_clear_timedout(array); uint32_t index = array->last_added % array->total_size; - if (array->entries[index].data != NULL) { + if (array->entries[index].data != nullptr) { array->last_deleted = array->last_added - array->total_size; clear_entry(array, index); } array->entries[index].data = malloc(length); - if (array->entries[index].data == NULL) { + if (array->entries[index].data == nullptr) { return 0; } @@ -184,7 +184,7 @@ int32_t ping_array_check(Ping_Array *array, uint8_t *data, size_t length, uint64 return -1; } - if (array->entries[index].data == NULL) { + if (array->entries[index].data == nullptr) { return -1; } diff --git a/protocols/Tox/libtox/src/toxcore/tox.api.h b/protocols/Tox/libtox/src/toxcore/tox.api.h index 7f3d8d4cff..59af69b9c3 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.api.h +++ b/protocols/Tox/libtox/src/toxcore/tox.api.h @@ -188,19 +188,19 @@ const VERSION_PATCH = 0; * features, but can't break the API. */ #define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \ - (TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \ + ((TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \ /* 1.x.x, 2.x.x, etc. with matching major version. */ \ TOX_VERSION_MINOR > MINOR || \ - TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH \ - ) || (TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \ + (TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH) \ + )) || ((TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \ /* 0.x.x makes minor behave like major above. */ \ - (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \ + ((TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \ TOX_VERSION_PATCH >= PATCH \ - ) || (TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \ + )) || ((TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \ /* 0.0.x and 0.0.y are only compatible if x == y. */ \ TOX_VERSION_PATCH == PATCH \ - ) \ - ) + )) \ + )) static namespace version { @@ -257,26 +257,36 @@ const ADDRESS_SIZE = PUBLIC_KEY_SIZE + NOSPAM_SIZE + sizeof(uint1 /** * Maximum length of a nickname in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_NAME_LENGTH = 128; /** * Maximum length of a status message in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_STATUS_MESSAGE_LENGTH = 1007; /** * Maximum length of a friend request message in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_FRIEND_REQUEST_LENGTH = 1016; /** * Maximum length of a single message after which it should be split. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_MESSAGE_LENGTH = 1372; /** * Maximum size of custom packets. TODO(iphydf): should be LENGTH? + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_CUSTOM_PACKET_SIZE = 1373; @@ -292,6 +302,8 @@ const FILE_ID_LENGTH = 32; /** * Maximum file name length for file transfers. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ const MAX_FILENAME_LENGTH = 255; @@ -338,11 +350,6 @@ enum class MESSAGE_TYPE { * on IRC. */ ACTION, - /** - * Correction of the last message. With empty message body can be used to mark - * last message as deleted. - */ - CORRECTION, } @@ -448,7 +455,7 @@ static class options { * @deprecated The memory layout of this struct (size, alignment, and field * order) is not part of the ABI. To remain compatible, prefer to use $new to * allocate the object and accessor functions to set the members. The struct - * will become opaque (i.e. the definition will become private) in v0.2.0. + * will become opaque (i.e. the definition will become private) in v0.3.0. */ struct this [get, set] { /** @@ -806,6 +813,9 @@ inline namespace self { /** * Return whether we are connected to the DHT. The return value is equal to the * last value received through the `${event connection_status}` callback. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ get(); } @@ -1222,11 +1232,11 @@ namespace friend { uint64_t last_online { /** - * Return a unix-time timestamp of the last time the friend associated with a given - * friend number was seen online. This function will return UINT64_MAX on error. - * - * @param friend_number The friend number you want to query. - */ + * Return a unix-time timestamp of the last time the friend associated with a given + * friend number was seen online. This function will return UINT64_MAX on error. + * + * @param friend_number The friend number you want to query. + */ get(uint32_t friend_number) { /** * No friend with the given number exists on the friend list. @@ -1356,6 +1366,9 @@ namespace friend { * * The status returned is equal to the last status received through the * `${event status}` callback. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ get(uint32_t friend_number) with error for query; @@ -1387,6 +1400,9 @@ namespace friend { * * @return the friend's connection status as it was received through the * `${event connection_status}` event. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ get(uint32_t friend_number) with error for query; @@ -1420,6 +1436,9 @@ namespace friend { * @return true if the friend is typing. * @return false if the friend is not typing, or the friend number was * invalid. Inspect the error code to determine which case it is. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ get(uint32_t friend_number) with error for query; @@ -2110,34 +2129,33 @@ namespace conference { typedef void(uint32_t conference_number, uint32_t peer_number, const uint8_t[length] title); } - /** - * Peer list state change types. - */ - enum class STATE_CHANGE { - /** - * A peer has joined the conference. - */ - PEER_JOIN, - /** - * A peer has exited the conference. - */ - PEER_EXIT, + namespace peer { + /** - * A peer has changed their name. + * This event is triggered when a peer changes their name. */ - PEER_NAME_CHANGE, - } + event name const { + /** + * @param conference_number The conference number of the conference the + * peer is in. + * @param peer_number The ID of the peer who changed their nickname. + * @param name A byte array containing the new nickname. + * @param length The size of the name byte array. + */ + typedef void(uint32_t conference_number, uint32_t peer_number, const uint8_t[length] name); + } - /** - * This event is triggered when the peer list changes (name change, peer join, peer exit). - */ - event namelist_change const { /** - * @param conference_number The conference number of the conference the title change is intended for. - * @param peer_number The ID of the peer who changed the title. - * @param change The type of change (one of $STATE_CHANGE). + * This event is triggered when a peer joins or leaves the conference. */ - typedef void(uint32_t conference_number, uint32_t peer_number, STATE_CHANGE change); + event list_changed const { + /** + * @param conference_number The conference number of the conference the + * peer is in. + */ + typedef void(uint32_t conference_number); + } + } diff --git a/protocols/Tox/libtox/src/toxcore/tox.c b/protocols/Tox/libtox/src/toxcore/tox.c index 2fd478dc8c..3db2bd35f3 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.c +++ b/protocols/Tox/libtox/src/toxcore/tox.c @@ -82,37 +82,37 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) { Messenger_Options m_options = {0}; - bool load_savedata_sk = 0, load_savedata_tox = 0; + bool load_savedata_sk = false, load_savedata_tox = false; - if (options == NULL) { + if (options == nullptr) { m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; } else { if (tox_options_get_savedata_type(options) != TOX_SAVEDATA_TYPE_NONE) { - if (tox_options_get_savedata_data(options) == NULL || tox_options_get_savedata_length(options) == 0) { + if (tox_options_get_savedata_data(options) == nullptr || tox_options_get_savedata_length(options) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); - return NULL; + return nullptr; } } if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_SECRET_KEY) { if (tox_options_get_savedata_length(options) != TOX_SECRET_KEY_SIZE) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); - return NULL; + return nullptr; } - load_savedata_sk = 1; + load_savedata_sk = true; } else if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_TOX_SAVE) { if (tox_options_get_savedata_length(options) < TOX_ENC_SAVE_MAGIC_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); - return NULL; + return nullptr; } if (crypto_memcmp(tox_options_get_savedata_data(options), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); - return NULL; + return nullptr; } - load_savedata_tox = 1; + load_savedata_tox = true; } m_options.ipv6enabled = tox_options_get_ipv6_enabled(options); @@ -141,13 +141,13 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) default: SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); - return NULL; + return nullptr; } if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { if (tox_options_get_proxy_port(options) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); - return NULL; + return nullptr; } ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); @@ -156,10 +156,10 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) m_options.proxy_info.ip_port.ip.family = TOX_AF_UNSPEC; } - if (!addr_resolve_or_parse_ip(tox_options_get_proxy_host(options), &m_options.proxy_info.ip_port.ip, NULL)) { + if (addr_resolve_or_parse_ip(tox_options_get_proxy_host(options), &m_options.proxy_info.ip_port.ip, nullptr) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. - return NULL; + return nullptr; } m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(options)); @@ -180,7 +180,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); } - return NULL; + return nullptr; } if (load_savedata_tox @@ -198,7 +198,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) void tox_kill(Tox *tox) { - if (tox == NULL) { + if (tox == nullptr) { return; } @@ -457,7 +457,8 @@ void tox_self_set_status(Tox *tox, TOX_USER_STATUS status) TOX_USER_STATUS tox_self_get_status(const Tox *tox) { const Messenger *m = tox; - return (TOX_USER_STATUS)m_get_self_userstatus(m); + const uint8_t status = m_get_self_userstatus(m); + return (TOX_USER_STATUS)status; } static void set_friend_error(int32_t ret, TOX_ERR_FRIEND_ADD *error) @@ -1103,11 +1104,16 @@ void tox_callback_conference_title(Tox *tox, tox_conference_title_cb *callback) g_callback_group_title((Group_Chats *)m->conferences_object, callback); } -void tox_callback_conference_namelist_change(Tox *tox, tox_conference_namelist_change_cb *callback) +void tox_callback_conference_peer_name(Tox *tox, tox_conference_peer_name_cb *callback) { Messenger *m = tox; - g_callback_group_namelistchange((Group_Chats *)m->conferences_object, (void (*)(struct Messenger *, int, int, uint8_t, - void *))callback); + g_callback_peer_name((Group_Chats *)m->conferences_object, callback); +} + +void tox_callback_conference_peer_list_changed(Tox *tox, tox_conference_peer_list_changed_cb *callback) +{ + Messenger *m = tox; + g_callback_peer_list_changed((Group_Chats *)m->conferences_object, callback); } uint32_t tox_conference_new(Tox *tox, TOX_ERR_CONFERENCE_NEW *error) @@ -1519,7 +1525,7 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) { if (dht_id) { const Messenger *m = tox; - memcpy(dht_id, m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(dht_id, dht_get_self_public_key(m->dht), CRYPTO_PUBLIC_KEY_SIZE); } } diff --git a/protocols/Tox/libtox/src/toxcore/tox.h b/protocols/Tox/libtox/src/toxcore/tox.h index f4f3d06639..41a0994ed6 100644 --- a/protocols/Tox/libtox/src/toxcore/tox.h +++ b/protocols/Tox/libtox/src/toxcore/tox.h @@ -191,19 +191,19 @@ uint32_t tox_version_patch(void); * features, but can't break the API. */ #define TOX_VERSION_IS_API_COMPATIBLE(MAJOR, MINOR, PATCH) \ - (TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \ + ((TOX_VERSION_MAJOR > 0 && TOX_VERSION_MAJOR == MAJOR) && ( \ /* 1.x.x, 2.x.x, etc. with matching major version. */ \ TOX_VERSION_MINOR > MINOR || \ - TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH \ - ) || (TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \ + (TOX_VERSION_MINOR == MINOR && TOX_VERSION_PATCH >= PATCH) \ + )) || ((TOX_VERSION_MAJOR == 0 && MAJOR == 0) && ( \ /* 0.x.x makes minor behave like major above. */ \ - (TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \ + ((TOX_VERSION_MINOR > 0 && TOX_VERSION_MINOR == MINOR) && ( \ TOX_VERSION_PATCH >= PATCH \ - ) || (TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \ + )) || ((TOX_VERSION_MINOR == 0 && MINOR == 0) && ( \ /* 0.0.x and 0.0.y are only compatible if x == y. */ \ TOX_VERSION_PATCH == PATCH \ - ) \ - ) + )) \ + )) /** * Return whether the compiled library version is compatible with the passed @@ -266,6 +266,8 @@ uint32_t tox_address_size(void); /** * Maximum length of a nickname in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_NAME_LENGTH 128 @@ -273,6 +275,8 @@ uint32_t tox_max_name_length(void); /** * Maximum length of a status message in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_STATUS_MESSAGE_LENGTH 1007 @@ -280,6 +284,8 @@ uint32_t tox_max_status_message_length(void); /** * Maximum length of a friend request message in bytes. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_FRIEND_REQUEST_LENGTH 1016 @@ -287,6 +293,8 @@ uint32_t tox_max_friend_request_length(void); /** * Maximum length of a single message after which it should be split. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_MESSAGE_LENGTH 1372 @@ -294,6 +302,8 @@ uint32_t tox_max_message_length(void); /** * Maximum size of custom packets. TODO(iphydf): should be LENGTH? + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_CUSTOM_PACKET_SIZE 1373 @@ -315,6 +325,8 @@ uint32_t tox_file_id_length(void); /** * Maximum file name length for file transfers. + * + * @deprecated The macro will be removed in 0.3.0. Use the function instead. */ #define TOX_MAX_FILENAME_LENGTH 255 @@ -371,12 +383,6 @@ typedef enum TOX_MESSAGE_TYPE { */ TOX_MESSAGE_TYPE_ACTION, - /** - * Correction of the last message. With empty message body can be used to mark - * last message as deleted. - */ - TOX_MESSAGE_TYPE_CORRECTION, - } TOX_MESSAGE_TYPE; @@ -500,7 +506,7 @@ typedef void tox_log_cb(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_ * @deprecated The memory layout of this struct (size, alignment, and field * order) is not part of the ABI. To remain compatible, prefer to use tox_options_new to * allocate the object and accessor functions to set the members. The struct - * will become opaque (i.e. the definition will become private) in v0.2.0. + * will become opaque (i.e. the definition will become private) in v0.3.0. */ struct Tox_Options { @@ -962,6 +968,9 @@ typedef enum TOX_CONNECTION { /** * Return whether we are connected to the DHT. The return value is equal to the * last value received through the `self_connection_status` callback. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ TOX_CONNECTION tox_self_get_connection_status(const Tox *tox); @@ -1531,6 +1540,9 @@ void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb * * * The status returned is equal to the last status received through the * `friend_status` callback. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ TOX_USER_STATUS tox_friend_get_status(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error); @@ -1560,6 +1572,9 @@ void tox_callback_friend_status(Tox *tox, tox_friend_status_cb *callback); * * @return the friend's connection status as it was received through the * `friend_connection_status` event. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ TOX_CONNECTION tox_friend_get_connection_status(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error); @@ -1592,6 +1607,9 @@ void tox_callback_friend_connection_status(Tox *tox, tox_friend_connection_statu * @return true if the friend is typing. * @return false if the friend is not typing, or the friend number was * invalid. Inspect the error code to determine which case it is. + * + * @deprecated This getter is deprecated. Use the event and store the status + * in the client state. */ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_QUERY *error); @@ -2394,43 +2412,36 @@ typedef void tox_conference_title_cb(Tox *tox, uint32_t conference_number, uint3 void tox_callback_conference_title(Tox *tox, tox_conference_title_cb *callback); /** - * Peer list state change types. + * @param conference_number The conference number of the conference the + * peer is in. + * @param peer_number The ID of the peer who changed their nickname. + * @param name A byte array containing the new nickname. + * @param length The size of the name byte array. */ -typedef enum TOX_CONFERENCE_STATE_CHANGE { +typedef void tox_conference_peer_name_cb(Tox *tox, uint32_t conference_number, uint32_t peer_number, + const uint8_t *name, size_t length, void *user_data); - /** - * A peer has joined the conference. - */ - TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN, - - /** - * A peer has exited the conference. - */ - TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT, - - /** - * A peer has changed their name. - */ - TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE, - -} TOX_CONFERENCE_STATE_CHANGE; +/** + * Set the callback for the `conference_peer_name` event. Pass NULL to unset. + * + * This event is triggered when a peer changes their name. + */ +void tox_callback_conference_peer_name(Tox *tox, tox_conference_peer_name_cb *callback); /** - * @param conference_number The conference number of the conference the title change is intended for. - * @param peer_number The ID of the peer who changed the title. - * @param change The type of change (one of TOX_CONFERENCE_STATE_CHANGE). + * @param conference_number The conference number of the conference the + * peer is in. */ -typedef void tox_conference_namelist_change_cb(Tox *tox, uint32_t conference_number, uint32_t peer_number, - TOX_CONFERENCE_STATE_CHANGE change, void *user_data); +typedef void tox_conference_peer_list_changed_cb(Tox *tox, uint32_t conference_number, void *user_data); /** - * Set the callback for the `conference_namelist_change` event. Pass NULL to unset. + * Set the callback for the `conference_peer_list_changed` event. Pass NULL to unset. * - * This event is triggered when the peer list changes (name change, peer join, peer exit). + * This event is triggered when a peer joins or leaves the conference. */ -void tox_callback_conference_namelist_change(Tox *tox, tox_conference_namelist_change_cb *callback); +void tox_callback_conference_peer_list_changed(Tox *tox, tox_conference_peer_list_changed_cb *callback); typedef enum TOX_ERR_CONFERENCE_NEW { diff --git a/protocols/Tox/libtox/src/toxcore/tox_api.c b/protocols/Tox/libtox/src/toxcore/tox_api.c index b6c8c38618..6c0bd71fca 100644 --- a/protocols/Tox/libtox/src/toxcore/tox_api.c +++ b/protocols/Tox/libtox/src/toxcore/tox_api.c @@ -1,5 +1,7 @@ #include "tox.h" +#include "ccompat.h" + #include <stdlib.h> #include <string.h> @@ -38,20 +40,20 @@ void tox_options_set_##ns##name(struct Tox_Options *options, type name) \ options->ns##name = name; \ } -ACCESSORS(bool, , ipv6_enabled) -ACCESSORS(bool, , udp_enabled) -ACCESSORS(TOX_PROXY_TYPE, proxy_ , type) -ACCESSORS(const char *, proxy_ , host) -ACCESSORS(uint16_t, proxy_ , port) -ACCESSORS(uint16_t, , start_port) -ACCESSORS(uint16_t, , end_port) -ACCESSORS(uint16_t, , tcp_port) -ACCESSORS(bool, , hole_punching_enabled) +ACCESSORS(bool,, ipv6_enabled) +ACCESSORS(bool,, udp_enabled) +ACCESSORS(TOX_PROXY_TYPE, proxy_, type) +ACCESSORS(const char *, proxy_, host) +ACCESSORS(uint16_t, proxy_, port) +ACCESSORS(uint16_t,, start_port) +ACCESSORS(uint16_t,, end_port) +ACCESSORS(uint16_t,, tcp_port) +ACCESSORS(bool,, hole_punching_enabled) ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) ACCESSORS(size_t, savedata_, length) ACCESSORS(tox_log_cb *, log_, callback) ACCESSORS(void *, log_, user_data) -ACCESSORS(bool, , local_discovery_enabled) +ACCESSORS(bool,, local_discovery_enabled) const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) { @@ -88,7 +90,7 @@ struct Tox_Options *tox_options_new(TOX_ERR_OPTIONS_NEW *error) } SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_MALLOC); - return NULL; + return nullptr; } void tox_options_free(struct Tox_Options *options) diff --git a/protocols/Tox/libtox/src/toxcore/util.c b/protocols/Tox/libtox/src/toxcore/util.c index 92bbb68c1f..b7a8fda653 100644 --- a/protocols/Tox/libtox/src/toxcore/util.c +++ b/protocols/Tox/libtox/src/toxcore/util.c @@ -27,7 +27,9 @@ #include "config.h" #endif +#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 600 +#endif #include "util.h" @@ -46,7 +48,7 @@ static uint64_t unix_base_time_value; void unix_time_update(void) { if (unix_base_time_value == 0) { - unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL)); + unix_base_time_value = ((uint64_t)time(nullptr) - (current_time_monotonic() / 1000ULL)); } unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value; @@ -128,7 +130,6 @@ int load_state(load_state_callback_func load_state_callback, Logger *log, void * } - uint16_t type; uint32_t length_sub, cookie_type; uint32_t size_head = sizeof(uint32_t) * 2; @@ -150,9 +151,8 @@ int load_state(load_state_callback_func load_state_callback, Logger *log, void * return -1; } - type = lendian_to_host16(cookie_type & 0xFFFF); - - int ret = load_state_callback(outer, data, length_sub, type); + const uint16_t type = lendian_to_host16(cookie_type & 0xFFFF); + const int ret = load_state_callback(outer, data, length_sub, type); if (ret == -1) { return -1; @@ -193,3 +193,13 @@ int create_recursive_mutex(pthread_mutex_t *mutex) return 0; } + +int32_t max_s32(int32_t a, int32_t b) +{ + return a > b ? a : b; +} + +uint64_t min_u64(uint64_t a, uint64_t b) +{ + return a < b ? a : b; +} diff --git a/protocols/Tox/libtox/src/toxcore/util.h b/protocols/Tox/libtox/src/toxcore/util.h index 8777e191d8..a9faa86349 100644 --- a/protocols/Tox/libtox/src/toxcore/util.h +++ b/protocols/Tox/libtox/src/toxcore/util.h @@ -32,6 +32,10 @@ #include "logger.h" +#ifdef __cplusplus +extern "C" { +#endif + #define MIN(a,b) (((a)<(b))?(a):(b)) #define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } @@ -61,4 +65,11 @@ int load_state(load_state_callback_func load_state_callback, Logger *log, void * /* Returns -1 if failed or 0 if success */ int create_recursive_mutex(pthread_mutex_t *mutex); +int32_t max_s32(int32_t a, int32_t b); +uint64_t min_u64(uint64_t a, uint64_t b); + +#ifdef __cplusplus +} // extern "C" +#endif + #endif /* UTIL_H */ diff --git a/protocols/Tox/libtox/src/toxcore/util_test.cpp b/protocols/Tox/libtox/src/toxcore/util_test.cpp new file mode 100644 index 0000000000..8de6384848 --- /dev/null +++ b/protocols/Tox/libtox/src/toxcore/util_test.cpp @@ -0,0 +1,55 @@ +#include "util.h" + +#include "crypto_core.h" + +#include <gtest/gtest.h> + +TEST(Util, UnixTimeIncreasesOverTime) +{ + unix_time_update(); + uint64_t const start = unix_time(); + + while (start == unix_time()) { + unix_time_update(); + } + + uint64_t const end = unix_time(); + EXPECT_GT(end, start); +} + +TEST(Util, IsTimeout) +{ + uint64_t const start = unix_time(); + EXPECT_FALSE(is_timeout(start, 1)); + + while (start == unix_time()) { + unix_time_update(); + } + + EXPECT_TRUE(is_timeout(start, 1)); +} + +TEST(Util, TwoRandomIdsAreNotEqual) +{ + uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; + uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk2[CRYPTO_SECRET_KEY_SIZE]; + + crypto_new_keypair(pk1, sk1); + crypto_new_keypair(pk2, sk2); + + EXPECT_FALSE(id_equal(pk1, pk2)); +} + +TEST(Util, IdCopyMakesKeysEqual) +{ + uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; + uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; + uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0}; + + crypto_new_keypair(pk1, sk1); + id_copy(pk2, pk1); + + EXPECT_TRUE(id_equal(pk1, pk2)); +} diff --git a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c index b7360b5650..b83d6f1c6c 100644 --- a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c +++ b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c @@ -25,6 +25,7 @@ #include "config.h" #endif +#include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" #include "defines.h" #include "toxencryptsave.h" @@ -134,7 +135,7 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp { if (!salt || (!passphrase && pplength != 0)) { SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_NULL); - return NULL; + return nullptr; } uint8_t passkey[crypto_hash_sha256_BYTES]; @@ -152,7 +153,7 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) != 0) { /* out of memory most likely */ SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_FAILED); - return NULL; + return nullptr; } sodium_memzero(passkey, crypto_hash_sha256_BYTES); /* wipe plaintext pw */ @@ -161,7 +162,7 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp if (!out_key) { SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_FAILED); - return NULL; + return nullptr; } memcpy(out_key->salt, salt, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); @@ -320,7 +321,7 @@ bool tox_pass_decrypt(const uint8_t *data, size_t length, const uint8_t *passphr memcpy(salt, data + TOX_ENC_SAVE_MAGIC_LENGTH, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); /* derive the key */ - Tox_Pass_Key *key = tox_pass_key_derive_with_salt(passphrase, pplength, salt, NULL); + Tox_Pass_Key *key = tox_pass_key_derive_with_salt(passphrase, pplength, salt, nullptr); if (!key) { /* out of memory most likely */ diff --git a/protocols/Tox/res/edit.ico b/protocols/Tox/res/edit.ico Binary files differdeleted file mode 100644 index 6264e76a93..0000000000 --- a/protocols/Tox/res/edit.ico +++ /dev/null diff --git a/protocols/Tox/res/resource.rc b/protocols/Tox/res/resource.rc index 90ddb8da93..5ba9c28ba3 100644 --- a/protocols/Tox/res/resource.rc +++ b/protocols/Tox/res/resource.rc @@ -66,9 +66,6 @@ IDI_TOX ICON "tox.ico" IDI_ME ICON "me.ico"
-IDI_EDIT ICON "edit.ico"
-
-
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
diff --git a/protocols/Tox/src/stdafx.h b/protocols/Tox/src/stdafx.h index 6d0f70935f..0c53490d34 100644 --- a/protocols/Tox/src/stdafx.h +++ b/protocols/Tox/src/stdafx.h @@ -70,7 +70,7 @@ extern HINSTANCE g_hInstance; #define TOX_MAX_RECONNECT_RETRIES 10
#define TOX_INI_PATH "%miranda_path%\\Plugins\\tox.ini"
-#define TOX_JSON_PATH "%miranda_userdata%\\tox.json"
+#define TOX_JSON_PATH L"%miranda_userdata%\\tox.json"
#define TOX_SETTINGS_ID "ToxID"
#define TOX_SETTINGS_DNS "DnsID"
diff --git a/protocols/Tox/src/tox_bootstrap.cpp b/protocols/Tox/src/tox_bootstrap.cpp index f144c0af16..7bdf45bbdc 100644 --- a/protocols/Tox/src/tox_bootstrap.cpp +++ b/protocols/Tox/src/tox_bootstrap.cpp @@ -33,76 +33,57 @@ void CToxProto::BootstrapNodesFromDb(Tox *tox, bool isIPv6) char module[MAX_PATH]; mir_snprintf(module, "%s_Nodes", m_szModuleName); int nodeCount = db_get_w(NULL, module, TOX_SETTINGS_NODE_COUNT, 0); - if (nodeCount > 0) { - char setting[MAX_PATH]; - for (int i = 0; i < nodeCount; i++) { - mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i); - ptrA address(db_get_sa(NULL, module, setting)); - mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i); - int port = db_get_w(NULL, module, setting, 33445); - mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i); - ptrA pubKey(db_get_sa(NULL, module, setting)); + if (nodeCount == 0) + return; + + char setting[MAX_PATH]; + for (int i = 0; i < nodeCount; i++) { + mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i); + ptrA address(db_get_sa(NULL, module, setting)); + mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i); + int port = db_get_w(NULL, module, setting, 33445); + mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i); + ptrA pubKey(db_get_sa(NULL, module, setting)); + BootstrapUdpNode(tox, address, port, pubKey); + BootstrapTcpRelay(tox, address, port, pubKey); + if (isIPv6) { + mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i); + address = db_get_sa(NULL, module, setting); BootstrapUdpNode(tox, address, port, pubKey); BootstrapTcpRelay(tox, address, port, pubKey); - if (isIPv6) { - mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i); - address = db_get_sa(NULL, module, setting); - BootstrapUdpNode(tox, address, port, pubKey); - BootstrapTcpRelay(tox, address, port, pubKey); - } } } } void CToxProto::BootstrapNodesFromJson(Tox *tox, bool isIPv6) { - ptrA json; - - VARSW path(_A2W(TOX_JSON_PATH)); - - if (!IsFileExists(path)) + VARSW path(TOX_JSON_PATH); + long lastUpdate = getDword("NodesUpdate", 0); + if (!IsFileExists(path) || lastUpdate < (now() - 86400 /* 24h */)) UpdateNodes(); - if (IsFileExists(path)) { - FILE *hFile = _wfopen(path, L"r"); - if (hFile != nullptr) { - _fseeki64(hFile, 0, SEEK_END); - size_t size = _ftelli64(hFile); - json = (char*)mir_calloc(size); - rewind(hFile); - fread(json, sizeof(char), size, hFile); - fclose(hFile); - } - } + JSONNode nodes = ParseNodes(); + for (const auto &node : nodes) { + JSONNode address = node.at("ipv4"); + JSONNode pubKey = node.at("public_key"); - if (json) { - JSONNode root = JSONNode::parse(json); - if (!root.empty()) { - JSONNode nodes = root.at("nodes").as_array(); - for (size_t i = 0; i < nodes.size(); i++) { - JSONNode node = nodes[i]; - JSONNode address = node.at("ipv4"); - JSONNode pubKey = node.at("public_key"); - - if (node.at("status_udp").as_bool()) { - int port = node.at("port").as_int(); - BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - if (isIPv6) { - address = node.at("ipv6"); - BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - } - } + if (node.at("status_udp").as_bool()) { + int port = node.at("port").as_int(); + BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + if (isIPv6) { + address = node.at("ipv6"); + BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + } + } - if (node.at("status_tcp").as_bool()) { - JSONNode tcpPorts = node.at("tcp_ports").as_array(); - for (size_t k = 0; k < tcpPorts.size(); k++) { - int port = tcpPorts[k].as_int(); - BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - if (isIPv6) { - address = node.at("ipv6"); - BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - } - } + if (node.at("status_tcp").as_bool()) { + JSONNode tcpPorts = node.at("tcp_ports").as_array(); + for (size_t k = 0; k < tcpPorts.size(); k++) { + int port = tcpPorts[k].as_int(); + BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + if (isIPv6) { + address = node.at("ipv6"); + BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); } } } @@ -111,16 +92,16 @@ void CToxProto::BootstrapNodesFromJson(Tox *tox, bool isIPv6) void CToxProto::BootstrapNodes(Tox *tox) { - UpdateNodes(); debugLogA(__FUNCTION__": bootstraping DHT"); - // bool isUdp = getBool("EnableUDP", 1); bool isIPv6 = getBool("EnableIPv6", 0); - BootstrapNodesFromDb(tox, isIPv6); BootstrapNodesFromJson(tox, isIPv6); + BootstrapNodesFromDb(tox, isIPv6); } void CToxProto::UpdateNodes() { + VARSW path(TOX_JSON_PATH); + debugLogA(__FUNCTION__": updating nodes"); HttpRequest request(REQUEST_GET, "https://nodes.tox.chat/json"); NLHR_PTR response(request.Send(m_hNetlibUser)); @@ -139,18 +120,8 @@ void CToxProto::UpdateNodes() if (lastUpdate <= getDword("NodesUpdate", 0)) return; - ptrW path(mir_wstrdup((wchar_t*)VARSW(_A2W(TOX_JSON_PATH)))); - if (!IsFileExists(path)) { - HANDLE hProfile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); - if (hProfile == nullptr) { - debugLogA(__FUNCTION__": failed to create tox.json"); - return; - } - CloseHandle(hProfile); - } - FILE *hFile = _wfopen(path, L"w"); - if (!hFile) { + if (hFile == nullptr) { debugLogA(__FUNCTION__": failed to open tox.json"); return; } @@ -162,3 +133,34 @@ void CToxProto::UpdateNodes() setDword("NodesUpdate", lastUpdate); } + +JSONNode CToxProto::ParseNodes() +{ + VARSW path(TOX_JSON_PATH); + + if (!IsFileExists(path)) { + debugLogA(__FUNCTION__": could not find tox.json"); + return JSONNode(JSON_ARRAY); + } + + FILE *hFile = _wfopen(path, L"r"); + if (hFile == nullptr) { + debugLogA(__FUNCTION__": failed to open tox.json"); + return JSONNode(JSON_ARRAY); + } + + _fseeki64(hFile, 0, SEEK_END); + size_t size = _ftelli64(hFile); + ptrA json((char*)mir_calloc(size)); + rewind(hFile); + fread(json, sizeof(char), size, hFile); + fclose(hFile); + + JSONNode root = JSONNode::parse(json); + if (root.empty()) { + debugLogA(__FUNCTION__": failed to parse tox.json"); + return JSONNode(JSON_ARRAY); + } + + return root.at("nodes").as_array(); +} diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index 8d39d1d6d7..3ca2d81ca6 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -9,10 +9,6 @@ void CToxProto::InitCustomDbEvents() dbEventType.eventType = DB_EVENT_ACTION;
dbEventType.descr = Translate("Action");
DbEvent_RegisterType(&dbEventType);
-
- dbEventType.eventType = DB_EVENT_CORRECTION;
- dbEventType.descr = Translate("Correction");
- DbEvent_RegisterType(&dbEventType);
}
INT_PTR CToxProto::EventGetIcon(WPARAM wParam, LPARAM lParam)
@@ -25,10 +21,6 @@ INT_PTR CToxProto::EventGetIcon(WPARAM wParam, LPARAM lParam) icon = GetIcon(IDI_ME);
break;
- case DB_EVENT_CORRECTION:
- icon = GetIcon(IDI_EDIT);
- break;
-
default:
icon = Skin_LoadIcon(SKINICON_EVENT_MESSAGE);
break;
@@ -69,9 +61,6 @@ void CToxProto::OnFriendMessage(Tox *tox, uint32_t friendNumber, TOX_MESSAGE_TYP case TOX_MESSAGE_TYPE_ACTION:
recv.lParam = DB_EVENT_ACTION;
break;
- case TOX_MESSAGE_TYPE_CORRECTION:
- recv.lParam = DB_EVENT_CORRECTION;
- break;
}
ProtoChainRecvMsg(hContact, &recv);
diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 119bce5a0d..f52202c1eb 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -439,42 +439,20 @@ void CToxOptionsNodeList::ReloadNodeList() int iItem = -1;
- VARSW path(_A2W(TOX_JSON_PATH));
- if (CToxProto::IsFileExists(path)) {
- ptrA json;
-
- FILE *hFile = _wfopen(path, L"r");
- if (hFile != nullptr) {
- _fseeki64(hFile, 0, SEEK_END);
- size_t size = _ftelli64(hFile);
- json = (char*)mir_calloc(size);
- rewind(hFile);
- fread(json, sizeof(char), size, hFile);
- fclose(hFile);
- }
-
- if (json) {
- JSONNode root = JSONNode::parse(json);
- if (!root.empty()) {
- JSONNode nodes = root.at("nodes").as_array();
- for (size_t i = 0; i < nodes.size(); i++) {
- JSONNode node = nodes[i];
+ JSONNode nodes = m_proto->ParseNodes();
+ for (const auto &node : nodes) {
+ ptrW ipv4(mir_utf8decodeW(node.at("ipv4").as_string().c_str()));
+ iItem = m_nodes.AddItem(ipv4, -1, NULL, 0);
- ptrW ipv4(mir_utf8decodeW(node.at("ipv4").as_string().c_str()));
- iItem = m_nodes.AddItem(ipv4, -1, NULL, 0);
+ ptrW ipv6(mir_utf8decodeW(node.at("ipv6").as_string().c_str()));
+ if (mir_wstrcmp(ipv6, L"-"))
+ m_nodes.SetItem(iItem, 1, ipv6);
- ptrW ipv6(mir_utf8decodeW(node.at("ipv6").as_string().c_str()));
- if (mir_wstrcmp(ipv6, L"-"))
- m_nodes.SetItem(iItem, 1, ipv6);
+ ptrW port(mir_utf8decodeW(node.at("port").as_string().c_str()));
+ m_nodes.SetItem(iItem, 2, port);
- ptrW port(mir_utf8decodeW(node.at("port").as_string().c_str()));
- m_nodes.SetItem(iItem, 2, port);
-
- ptrW pubKey(mir_utf8decodeW(node.at("public_key").as_string().c_str()));
- m_nodes.SetItem(iItem, 3, pubKey);
- }
- }
- }
+ ptrW pubKey(mir_utf8decodeW(node.at("public_key").as_string().c_str()));
+ m_nodes.SetItem(iItem, 3, pubKey);
}
char module[MAX_PATH], setting[MAX_PATH];
diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 3971d76a72..b09ba4d4e8 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -104,6 +104,7 @@ private: void BootstrapNodes(Tox *tox);
void UpdateNodes();
+ JSONNode ParseNodes();
// tox connection
bool IsOnline();
|