diff options
author | George Hazan <george.hazan@gmail.com> | 2024-02-15 12:18:35 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2024-02-15 12:18:35 +0300 |
commit | 31e72718ee54867accf0b739a24adc86f8b7ab54 (patch) | |
tree | f964c10c5d97d9fe4fd2bd8187c250faedcb0fd7 /protocols/Tox/libtox/src/toxcore/DHT.c | |
parent | 282e9c18d9d3b726cce3d2ef0babc88029661cb8 (diff) |
libtox update
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/DHT.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxcore/DHT.c | 482 |
1 files changed, 215 insertions, 267 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/DHT.c b/protocols/Tox/libtox/src/toxcore/DHT.c index 91f0e0ae47..2567d1b5a8 100644 --- a/protocols/Tox/libtox/src/toxcore/DHT.c +++ b/protocols/Tox/libtox/src/toxcore/DHT.c @@ -13,14 +13,18 @@ #include <string.h> #include "LAN_discovery.h" +#include "attributes.h" +#include "bin_pack.h" #include "ccompat.h" +#include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "ping.h" +#include "ping_array.h" #include "shared_key_cache.h" #include "state.h" -#include "util.h" /** The timeout after which a node is discarded completely. */ #define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) @@ -80,7 +84,7 @@ struct DHT_Friend { static const DHT_Friend empty_dht_friend = {{0}}; const Node_format empty_node_format = {{0}}; -static_assert(sizeof (empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match"); +static_assert(sizeof(empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match"); typedef struct Cryptopacket_Handler { cryptopacket_handler_cb *function; @@ -91,6 +95,7 @@ struct DHT { const Logger *log; const Network *ns; Mono_Time *mono_time; + const Memory *mem; const Random *rng; Networking_Core *net; @@ -358,69 +363,14 @@ int packed_node_size(Family ip_family) return -1; } - -int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port) -{ - if (data == nullptr) { - return -1; - } - - bool is_ipv4; - uint8_t family; - - if (net_family_is_ipv4(ip_port->ip.family)) { - // TODO(irungentoo): use functions to convert endianness - is_ipv4 = true; - family = TOX_AF_INET; - } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) { - is_ipv4 = true; - family = TOX_TCP_INET; - } else if (net_family_is_ipv6(ip_port->ip.family)) { - is_ipv4 = false; - family = TOX_AF_INET6; - } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) { - is_ipv4 = false; - family = TOX_TCP_INET6; - } else { - Ip_Ntoa ip_str; - // TODO(iphydf): Find out why we're trying to pack invalid IPs, stop - // doing that, and turn this into an error. - LOGGER_TRACE(logger, "cannot pack invalid IP: %s", net_ip_ntoa(&ip_port->ip, &ip_str)); - return -1; - } - - if (is_ipv4) { - const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); - - if (size > length) { - return -1; - } - - data[0] = family; - memcpy(data + 1, &ip_port->ip.ip.v4, SIZE_IP4); - memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); - return size; - } else { - const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); - - if (size > length) { - return -1; - } - - data[0] = family; - memcpy(data + 1, &ip_port->ip.ip.v6, SIZE_IP6); - memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t)); - return size; - } -} - -int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], +int dht_create_packet(const Memory *mem, const Random *rng, + const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t *shared_key, const uint8_t type, const uint8_t *plain, size_t plain_length, uint8_t *packet, size_t length) { - uint8_t *encrypted = (uint8_t *)malloc(plain_length + CRYPTO_MAC_SIZE); uint8_t nonce[CRYPTO_NONCE_SIZE]; + uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); if (encrypted == nullptr) { return -1; @@ -431,12 +381,12 @@ int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_ const int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted); if (encrypted_length == -1) { - free(encrypted); + mem_delete(mem, encrypted); return -1; } if (length < 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length) { - free(encrypted); + mem_delete(mem, encrypted); return -1; } @@ -445,97 +395,29 @@ int dht_create_packet(const Random *rng, const uint8_t public_key[CRYPTO_PUBLIC_ memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypted, encrypted_length); - free(encrypted); + mem_delete(mem, encrypted); return 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length; } -int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled) +/** @brief Pack a single node from a node array. + * + * @retval true on success. + */ +non_null() +static bool bin_pack_node_handler(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp) { - if (data == nullptr) { - return -1; - } - - bool is_ipv4; - Family host_family; - - if (data[0] == TOX_AF_INET) { - is_ipv4 = true; - host_family = net_family_ipv4(); - } else if (data[0] == TOX_TCP_INET) { - if (!tcp_enabled) { - return -1; - } - - is_ipv4 = true; - host_family = net_family_tcp_ipv4(); - } else if (data[0] == TOX_AF_INET6) { - is_ipv4 = false; - host_family = net_family_ipv6(); - } else if (data[0] == TOX_TCP_INET6) { - if (!tcp_enabled) { - return -1; - } - - is_ipv4 = false; - host_family = net_family_tcp_ipv6(); - } else { - return -1; - } - - *ip_port = empty_ip_port; - - if (is_ipv4) { - 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.ip.v4, data + 1, SIZE_IP4); - memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); - return size; - } else { - 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.ip.v6, data + 1, SIZE_IP6); - memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); - return size; - } + const Node_format *nodes = (const Node_format *)arr; + return bin_pack_ip_port(bp, logger, &nodes[index].ip_port) + && bin_pack_bin_b(bp, nodes[index].public_key, CRYPTO_PUBLIC_KEY_SIZE); } int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number) { - uint32_t packed_length = 0; - - for (uint32_t i = 0; i < number && packed_length < length; ++i) { - const int ipp_size = pack_ip_port(logger, data + packed_length, length - packed_length, &nodes[i].ip_port); - - if (ipp_size == -1) { - return -1; - } - - packed_length += ipp_size; - - if (packed_length + CRYPTO_PUBLIC_KEY_SIZE > length) { - return -1; - } - - memcpy(data + packed_length, nodes[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); - packed_length += CRYPTO_PUBLIC_KEY_SIZE; - -#ifndef NDEBUG - const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; -#endif - assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); + const uint32_t size = bin_pack_obj_array_b_size(bin_pack_node_handler, nodes, number, logger); + if (!bin_pack_obj_array_b(bin_pack_node_handler, nodes, number, logger, data, length)) { + return -1; } - - return packed_length; + return size; } int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, @@ -563,8 +445,8 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed #ifndef NDEBUG const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; -#endif assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); +#endif /* NDEBUG */ } if (processed_data_len != nullptr) { @@ -729,16 +611,21 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t return true; } -bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, - const uint8_t *cmp_pk) +bool add_to_list( + Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE], + const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]) { for (uint32_t i = 0; i < length; ++i) { - if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) { + Node_format *node = &nodes_list[i]; + + if (id_closest(cmp_pk, node->public_key, pk) == 2) { uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE]; - memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); - const IP_Port ip_port_bak = nodes_list[i].ip_port; - memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); - nodes_list[i].ip_port = *ip_port; + memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE); + + const IP_Port ip_port_bak = node->ip_port; + memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); + + node->ip_port = *ip_port; if (i != length - 1) { add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk); @@ -755,10 +642,11 @@ bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, co * helper for `get_close_nodes()`. argument list is a monster :D */ non_null() -static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list, - Family sa_family, const Client_data *client_list, uint32_t client_list_length, - uint32_t *num_nodes_ptr, bool is_LAN, - bool want_announce) +static void get_close_nodes_inner( + uint64_t cur_time, const uint8_t *public_key, + Node_format *nodes_list, uint32_t *num_nodes_ptr, + Family sa_family, const Client_data *client_list, uint32_t client_list_length, + bool is_lan, bool want_announce) { if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) { return; @@ -792,7 +680,7 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, } /* don't send LAN ips to non LAN peers */ - if (ip_is_lan(&ipptp->ip_port.ip) && !is_LAN) { + if (ip_is_lan(&ipptp->ip_port.ip) && !is_lan) { continue; } @@ -802,7 +690,7 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, continue; } -#endif +#endif /* CHECK_ANNOUNCE_NODE */ if (num_nodes < MAX_SENT_NODES) { memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -825,28 +713,46 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, * want_announce: return only nodes which implement the dht announcements protocol. */ non_null() -static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, - Family sa_family, bool is_LAN, bool want_announce) +static int get_somewhat_close_nodes( + uint64_t cur_time, const uint8_t *public_key, Node_format nodes_list[MAX_SENT_NODES], + Family sa_family, const Client_data *close_clientlist, + const DHT_Friend *friends_list, uint16_t friends_list_size, + bool is_lan, bool want_announce) { + for (uint16_t i = 0; i < MAX_SENT_NODES; ++i) { + nodes_list[i] = empty_node_format; + } + uint32_t num_nodes = 0; - get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family, - dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_LAN, want_announce); + get_close_nodes_inner( + cur_time, public_key, + nodes_list, &num_nodes, + sa_family, close_clientlist, LCLIENT_LIST, + is_lan, want_announce); - for (uint32_t i = 0; i < dht->num_friends; ++i) { - get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family, - dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, - &num_nodes, is_LAN, want_announce); + for (uint16_t i = 0; i < friends_list_size; ++i) { + const DHT_Friend *dht_friend = &friends_list[i]; + + get_close_nodes_inner( + cur_time, public_key, + nodes_list, &num_nodes, + sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS, + is_lan, want_announce); } return num_nodes; } -int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, - bool is_LAN, bool want_announce) +int get_close_nodes( + const DHT *dht, const uint8_t *public_key, + Node_format nodes_list[MAX_SENT_NODES], Family sa_family, + bool is_lan, bool want_announce) { - memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); - return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, - is_LAN, want_announce); + return get_somewhat_close_nodes( + dht->cur_time, public_key, nodes_list, + sa_family, dht->close_clientlist, + dht->friends_list, dht->num_friends, + is_lan, want_announce); } typedef struct DHT_Cmp_Data { @@ -937,7 +843,8 @@ static bool send_announce_ping(DHT *dht, const uint8_t *public_key, const IP_Por uint8_t request[1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE]; - if (dht_create_packet(dht->rng, dht->self_public_key, shared_key, NET_PACKET_DATA_SEARCH_REQUEST, + if (dht_create_packet(dht->mem, dht->rng, + dht->self_public_key, shared_key, NET_PACKET_DATA_SEARCH_REQUEST, plain, sizeof(plain), request, sizeof(request)) != sizeof(request)) { return false; } @@ -991,7 +898,7 @@ static int handle_data_search_response(void *object, const IP_Port *source, return 0; } -#endif +#endif /* CHECK_ANNOUNCE_NODE */ /** @brief Is it ok to store node with public_key in client. * @@ -1008,12 +915,12 @@ static bool store_node_ok(const Client_data *client, uint64_t cur_time, const ui } non_null() -static void sort_client_list(Client_data *list, uint64_t cur_time, unsigned int length, +static void sort_client_list(const Memory *mem, Client_data *list, uint64_t cur_time, unsigned int length, const uint8_t *comp_public_key) { // Pass comp_public_key to qsort with each Client_data entry, so the // comparison function can use it as the base of comparison. - DHT_Cmp_Data *cmp_list = (DHT_Cmp_Data *)calloc(length, sizeof(DHT_Cmp_Data)); + DHT_Cmp_Data *cmp_list = (DHT_Cmp_Data *)mem_valloc(mem, length, sizeof(DHT_Cmp_Data)); if (cmp_list == nullptr) { return; @@ -1031,7 +938,7 @@ static void sort_client_list(Client_data *list, uint64_t cur_time, unsigned int list[i] = cmp_list[i].entry; } - free(cmp_list); + mem_delete(mem, cmp_list); } non_null() @@ -1057,7 +964,8 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl ipptp_write->ret_ip_self = false; /* zero out other address */ - memset(ipptp_clear, 0, sizeof(*ipptp_clear)); + const IPPTsPng empty_ipptp = {{{{0}}}}; + *ipptp_clear = empty_ipptp; } /** @@ -1092,7 +1000,7 @@ static bool replace_all(const DHT *dht, return false; } - sort_client_list(list, dht->cur_time, length, comp_public_key); + sort_client_list(dht->mem, list, dht->cur_time, length, comp_public_key); Client_data *const client = &list[0]; pk_copy(client->public_key, public_key); @@ -1136,7 +1044,7 @@ static bool add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_ #ifdef CHECK_ANNOUNCE_NODE client->announce_node = false; send_announce_ping(dht, public_key, ip_port); -#endif +#endif /* CHECK_ANNOUNCE_NODE */ return true; } @@ -1254,7 +1162,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons */ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key) { - IP_Port ipp_copy = ip_port_normalize(ip_port); + const IP_Port ipp_copy = ip_port_normalize(ip_port); uint32_t used = 0; @@ -1340,7 +1248,7 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s non_null() static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) { - IP_Port ipp_copy = ip_port_normalize(ip_port); + const IP_Port ipp_copy = ip_port_normalize(ip_port); if (pk_equal(public_key, dht->self_public_key)) { update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true); @@ -1392,7 +1300,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c const uint8_t *shared_key = dht_get_shared_key_sent(dht, public_key); - const int len = dht_create_packet(dht->rng, + const int len = dht_create_packet(dht->mem, dht->rng, dht->self_public_key, shared_key, NET_PACKET_GET_NODES, plain, sizeof(plain), data, sizeof(data)); @@ -1440,13 +1348,14 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t memcpy(plain + 1 + nodes_length, sendback_data, length); 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); + const uint32_t data_size = 1 + nodes_length + length + crypto_size; + VLA(uint8_t, data, data_size); - const int len = dht_create_packet(dht->rng, + const int len = dht_create_packet(dht->mem, dht->rng, dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, - plain, 1 + nodes_length + length, data, SIZEOF_VLA(data)); + plain, 1 + nodes_length + length, data, data_size); - if (len != SIZEOF_VLA(data)) { + if (len != data_size) { return -1; } @@ -1458,12 +1367,12 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t non_null() static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata) { + DHT *const dht = (DHT *)object; + if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) { return 1; } - DHT *const dht = (DHT *)object; - /* Check if packet is from ourself. */ if (pk_equal(packet + 1, dht->self_public_key)) { return 1; @@ -1529,7 +1438,8 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin return false; } - VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t)); + const uint32_t plain_size = 1 + data_size + sizeof(uint64_t); + VLA(uint8_t, plain, plain_size); const uint8_t *shared_key = dht_get_shared_key_sent(dht, packet + 1); const int len = decrypt_data_symmetric( shared_key, @@ -1538,7 +1448,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, plain); - if ((unsigned int)len != SIZEOF_VLA(plain)) { + if ((uint32_t)len != plain_size) { return false; } @@ -1611,7 +1521,7 @@ static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint non_null(1) nullable(2, 3) static uint32_t dht_friend_lock(DHT_Friend *const dht_friend, dht_ip_cb *ip_callback, - void *data, int32_t number) + void *data, int32_t number) { // find first free slot uint8_t lock_num; @@ -1681,7 +1591,7 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback, return 0; } - DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); + DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend)); if (temp == nullptr) { return -1; @@ -1726,12 +1636,12 @@ int dht_delfriend(DHT *dht, const uint8_t *public_key, uint32_t lock_token) } if (dht->num_friends == 0) { - free(dht->friends_list); + mem_delete(dht->mem, dht->friends_list); dht->friends_list = nullptr; return 0; } - DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * dht->num_friends); + DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend)); if (temp == nullptr) { return -1; @@ -1784,14 +1694,14 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co const uint64_t temp_time = mono_time_get(dht->mono_time); uint32_t num_nodes = 0; - Client_data **client_list = (Client_data **)calloc(list_count * 2, sizeof(Client_data *)); - IPPTsPng **assoc_list = (IPPTsPng **)calloc(list_count * 2, sizeof(IPPTsPng *)); + Client_data **client_list = (Client_data **)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *)); + IPPTsPng **assoc_list = (IPPTsPng **)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *)); unsigned int sort = 0; bool sort_ok = false; if (client_list == nullptr || assoc_list == nullptr) { - free(assoc_list); - free(client_list); + mem_delete(dht->mem, assoc_list); + mem_delete(dht->mem, client_list); return 0; } @@ -1809,7 +1719,9 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co ++not_kill; if (mono_time_is_timeout(dht->mono_time, assoc->last_pinged, PING_INTERVAL)) { - dht_getnodes(dht, &assoc->ip_port, client->public_key, public_key); + const IP_Port *target = &assoc->ip_port; + const uint8_t *target_key = client->public_key; + dht_getnodes(dht, target, target_key, public_key); assoc->last_pinged = temp_time; } @@ -1831,7 +1743,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co } if (sortable && sort_ok) { - sort_client_list(list, dht->cur_time, list_count, public_key); + sort_client_list(dht->mem, list, dht->cur_time, list_count, public_key); } if (num_nodes > 0 && (mono_time_is_timeout(dht->mono_time, *lastgetnode, GET_NODE_INTERVAL) @@ -1842,14 +1754,16 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co rand_node += random_range_u32(dht->rng, num_nodes - (rand_node + 1)); } - dht_getnodes(dht, &assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key); + const IP_Port *target = &assoc_list[rand_node]->ip_port; + const uint8_t *target_key = client_list[rand_node]->public_key; + dht_getnodes(dht, target, target_key, public_key); *lastgetnode = temp_time; ++*bootstrap_times; } - free(assoc_list); - free(client_list); + mem_delete(dht->mem, assoc_list); + mem_delete(dht->mem, client_list); return not_kill; } @@ -1871,8 +1785,7 @@ static void do_dht_friends(DHT *dht) dht_friend->num_to_bootstrap = 0; do_ping_and_sendnode_requests(dht, &dht_friend->lastgetnode, dht_friend->public_key, dht_friend->client_list, - MAX_FRIEND_CLIENTS, - &dht_friend->bootstrap_times, true); + MAX_FRIEND_CLIENTS, &dht_friend->bootstrap_times, true); } } @@ -1881,7 +1794,7 @@ static void do_dht_friends(DHT *dht) * Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list. */ non_null() -static void do_Close(DHT *dht) +static void do_close(DHT *dht) { for (size_t i = 0; i < dht->num_to_bootstrap; ++i) { dht_getnodes(dht, &dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key); @@ -1908,14 +1821,11 @@ static void do_Close(DHT *dht) for (size_t i = 0; i < LCLIENT_LIST; ++i) { Client_data *const client = &dht->close_clientlist[i]; - IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; - - for (IPPTsPng * const *it = assocs; *it != nullptr; ++it) { - IPPTsPng *const assoc = *it; - - if (assoc->timestamp != 0) { - assoc->timestamp = badonly; - } + if (client->assoc4.timestamp != 0) { + client->assoc4.timestamp = badonly; + } + if (client->assoc6.timestamp != 0) { + client->assoc6.timestamp = badonly; } } } @@ -1930,8 +1840,8 @@ bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key) return dht_getnodes(dht, ip_port, public_key, dht->self_public_key); } -int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, - uint16_t port, const uint8_t *public_key) +bool dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, + uint16_t port, const uint8_t *public_key) { IP_Port ip_port_v64; IP *ip_extra = nullptr; @@ -1954,10 +1864,10 @@ int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, dht_bootstrap(dht, &ip_port_v4, public_key); } - return 1; + return true; } - return 0; + return false; } int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length) @@ -2059,7 +1969,6 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n #endif /* !FRIEND_IPLIST_PAD */ } - /** * Callback invoked for each IP/port of each client of a friend. * @@ -2146,7 +2055,6 @@ uint32_t route_to_friend(const DHT *dht, const uint8_t *friend_id, const Packet return 0; } - IP_Port ip_list[MAX_FRIEND_CLIENTS]; const int ip_num = friend_iplist(dht, ip_list, num); @@ -2206,7 +2114,7 @@ static uint32_t routeone_to_friend(const DHT *dht, const uint8_t *friend_id, con /*---------------------BEGINNING OF NAT PUNCHING FUNCTIONS--------------------------*/ non_null() -static int send_NATping(const DHT *dht, const uint8_t *public_key, uint64_t ping_id, uint8_t type) +static int send_nat_ping(const DHT *dht, const uint8_t *public_key, uint64_t ping_id, uint8_t type) { uint8_t data[sizeof(uint64_t) + 1]; uint8_t packet_data[MAX_CRYPTO_REQUEST_SIZE]; @@ -2241,14 +2149,15 @@ static int send_NATping(const DHT *dht, const uint8_t *public_key, uint64_t ping /** Handle a received ping request for. */ non_null() -static int handle_NATping(void *object, const IP_Port *source, const uint8_t *source_pubkey, const uint8_t *packet, - uint16_t length, void *userdata) +static int handle_nat_ping(void *object, const IP_Port *source, const uint8_t *source_pubkey, const uint8_t *packet, + uint16_t length, void *userdata) { + DHT *const dht = (DHT *)object; + if (length != sizeof(uint64_t) + 1) { return 1; } - DHT *const dht = (DHT *)object; uint64_t ping_id; memcpy(&ping_id, packet + 1, sizeof(uint64_t)); @@ -2262,7 +2171,7 @@ static int handle_NATping(void *object, const IP_Port *source, const uint8_t *so if (packet[0] == NAT_PING_REQUEST) { /* 1 is reply */ - send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); + send_nat_ping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); dht_friend->nat.recv_nat_ping_timestamp = mono_time_get(dht->mono_time); return 0; } @@ -2381,7 +2290,7 @@ static void punch_holes(DHT *dht, const IP *ip, const uint16_t *port_list, uint1 uint16_t i; for (i = 0; i < MAX_PUNCHING_PORTS; ++i) { - uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2; + const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2; const uint16_t port = 1024; pinging.port = net_htons(port + it); ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key); @@ -2394,7 +2303,7 @@ static void punch_holes(DHT *dht, const IP *ip, const uint16_t *port_list, uint1 } non_null() -static void do_NAT(DHT *dht) +static void do_nat(DHT *dht) { const uint64_t temp_time = mono_time_get(dht->mono_time); @@ -2408,7 +2317,7 @@ static void do_NAT(DHT *dht) } if (dht->friends_list[i].nat.nat_ping_timestamp + PUNCH_INTERVAL < temp_time) { - send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.nat_ping_id, NAT_PING_REQUEST); + send_nat_ping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.nat_ping_id, NAT_PING_REQUEST); dht->friends_list[i].nat.nat_ping_timestamp = temp_time; } @@ -2463,9 +2372,7 @@ static uint16_t list_nodes(const Random *rng, const Client_data *list, size_t le } if (!assoc_timeout(cur_time, &list[i - 1].assoc6)) { - if (assoc == nullptr) { - assoc = &list[i - 1].assoc6; - } else if ((random_u08(rng) % 2) != 0) { + if (assoc == nullptr || (random_u08(rng) % 2) != 0) { assoc = &list[i - 1].assoc6; } } @@ -2583,8 +2490,8 @@ void dht_callback_get_nodes_response(DHT *dht, dht_get_nodes_response_cb *functi } non_null(1, 2, 3) nullable(5) -static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, - void *userdata) +static int handle_lan_discovery(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, + void *userdata) { DHT *dht = (DHT *)object; @@ -2606,16 +2513,18 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_ /*----------------------------------------------------------------------------------*/ -DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time *mono_time, Networking_Core *net, +DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns, + Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled) { if (net == nullptr) { return nullptr; } - DHT *const dht = (DHT *)calloc(1, sizeof(DHT)); + DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT)); if (dht == nullptr) { + LOGGER_ERROR(log, "failed to allocate DHT struct (%ld bytes)", (unsigned long)sizeof(DHT)); return nullptr; } @@ -2625,13 +2534,15 @@ DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time dht->log = log; dht->net = net; dht->rng = rng; + dht->mem = mem; dht->hole_punching_enabled = hole_punching_enabled; dht->lan_discovery_enabled = lan_discovery_enabled; - dht->ping = ping_new(mono_time, rng, dht); + dht->ping = ping_new(mem, mono_time, rng, dht); if (dht->ping == nullptr) { + LOGGER_ERROR(log, "failed to initialise ping"); kill_dht(dht); return nullptr; } @@ -2639,27 +2550,28 @@ DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht); networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht); - networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); - cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); + networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_lan_discovery, dht); + cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_nat_ping, dht); #ifdef CHECK_ANNOUNCE_NODE networking_registerhandler(dht->net, NET_PACKET_DATA_SEARCH_RESPONSE, &handle_data_search_response, dht); -#endif +#endif /* CHECK_ANNOUNCE_NODE */ crypto_new_keypair(rng, dht->self_public_key, dht->self_secret_key); - dht->shared_keys_recv = shared_key_cache_new(mono_time, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - dht->shared_keys_sent = shared_key_cache_new(mono_time, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + dht->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + dht->shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); if (dht->shared_keys_recv == nullptr || dht->shared_keys_sent == nullptr) { + LOGGER_ERROR(log, "failed to initialise shared key cache"); kill_dht(dht); return nullptr; } - - dht->dht_ping_array = ping_array_new(DHT_PING_ARRAY_SIZE, PING_TIMEOUT); + dht->dht_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); if (dht->dht_ping_array == nullptr) { + LOGGER_ERROR(log, "failed to initialise ping array"); kill_dht(dht); return nullptr; } @@ -2672,6 +2584,7 @@ DHT *new_dht(const Logger *log, const Random *rng, const Network *ns, Mono_Time uint32_t token; // We don't intend to delete these ever, but need to pass the token if (dht_addfriend(dht, random_public_key_bytes, nullptr, nullptr, 0, &token) != 0) { + LOGGER_ERROR(log, "failed to add initial random seed DHT friends"); kill_dht(dht); return nullptr; } @@ -2701,9 +2614,9 @@ void do_dht(DHT *dht) dht_connect_after_load(dht); } - do_Close(dht); + do_close(dht); do_dht_friends(dht); - do_NAT(dht); + do_nat(dht); ping_iterate(dht->ping); } @@ -2722,11 +2635,11 @@ void kill_dht(DHT *dht) shared_key_cache_free(dht->shared_keys_recv); shared_key_cache_free(dht->shared_keys_sent); ping_array_kill(dht->dht_ping_array); - ping_kill(dht->ping); - free(dht->friends_list); - free(dht->loaded_nodes_list); + ping_kill(dht->mem, dht->ping); + mem_delete(dht->mem, dht->friends_list); + mem_delete(dht->mem, dht->loaded_nodes_list); crypto_memzero(dht->self_secret_key, sizeof(dht->self_secret_key)); - free(dht); + mem_delete(dht->mem, dht); } /* new DHT format for load/save, more robust and forward compatible */ @@ -2745,21 +2658,21 @@ uint32_t dht_size(const DHT *dht) uint32_t numv6 = 0; for (uint32_t i = 0; i < dht->loaded_num_nodes; ++i) { - numv4 += net_family_is_ipv4(dht->loaded_nodes_list[i].ip_port.ip.family); - numv6 += net_family_is_ipv6(dht->loaded_nodes_list[i].ip_port.ip.family); + numv4 += net_family_is_ipv4(dht->loaded_nodes_list[i].ip_port.ip.family) ? 1 : 0; + numv6 += net_family_is_ipv6(dht->loaded_nodes_list[i].ip_port.ip.family) ? 1 : 0; } for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { - numv4 += dht->close_clientlist[i].assoc4.timestamp != 0; - numv6 += dht->close_clientlist[i].assoc6.timestamp != 0; + numv4 += dht->close_clientlist[i].assoc4.timestamp != 0 ? 1 : 0; + numv6 += dht->close_clientlist[i].assoc6.timestamp != 0 ? 1 : 0; } for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++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; - numv6 += fr->client_list[j].assoc6.timestamp != 0; + numv4 += fr->client_list[j].assoc4.timestamp != 0 ? 1 : 0; + numv6 += fr->client_list[j].assoc6.timestamp != 0 ? 1 : 0; } } @@ -2780,7 +2693,7 @@ void dht_save(const DHT *dht, uint8_t *data) /* get right offset. we write the actual header later. */ data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0); - Node_format *clients = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); + Node_format *clients = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); if (clients == nullptr) { LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES); @@ -2826,10 +2739,11 @@ void dht_save(const DHT *dht, uint8_t *data) } } - state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, - clients, num), DHT_STATE_TYPE_NODES); + state_write_section_header( + old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, clients, num), + DHT_STATE_TYPE_NODES); - free(clients); + mem_delete(dht->mem, clients); } /** Bootstrap from this number of nodes every time `dht_connect_after_load()` is called */ @@ -2847,7 +2761,7 @@ int dht_connect_after_load(DHT *dht) /* DHT is connected, stop. */ if (dht_non_lan_connected(dht)) { - free(dht->loaded_nodes_list); + mem_delete(dht->mem, dht->loaded_nodes_list); dht->loaded_nodes_list = nullptr; dht->loaded_num_nodes = 0; return 0; @@ -2873,24 +2787,28 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat break; } - free(dht->loaded_nodes_list); + mem_delete(dht->mem, dht->loaded_nodes_list); + // Copy to loaded_clients_list - dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); + Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); - if (dht->loaded_nodes_list == nullptr) { + if (nodes == nullptr) { LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES); dht->loaded_num_nodes = 0; break; } - const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, false); + const int num = unpack_nodes(nodes, MAX_SAVED_DHT_NODES, nullptr, data, length, false); - if (num > 0) { - dht->loaded_num_nodes = num; - } else { + if (num < 0) { + // Unpack error happened, we ignore it. dht->loaded_num_nodes = 0; + } else { + dht->loaded_num_nodes = num; } + dht->loaded_nodes_list = nodes; + break; } @@ -2962,6 +2880,36 @@ bool dht_non_lan_connected(const DHT *dht) return false; } +uint16_t dht_get_num_closelist(const DHT *dht) +{ + uint16_t num_valid_close_clients = 0; + for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { + const Client_data *const client = dht_get_close_client(dht, i); + + // check if client is valid + if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6))) { + ++num_valid_close_clients; + } + } + + return num_valid_close_clients; +} + +uint16_t dht_get_num_closelist_announce_capable(const DHT *dht) +{ + uint16_t num_valid_close_clients_with_cap = 0; + for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { + const Client_data *const client = dht_get_close_client(dht, i); + + // check if client is valid + if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6)) && client->announce_node) { + ++num_valid_close_clients_with_cap; + } + } + + return num_valid_close_clients_with_cap; +} + unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest) { ipport_reset(dest); |