From f21c472a597c1a8d2f06e5b20499b15717cee3b0 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sat, 29 Jan 2022 12:16:48 -0500 Subject: [PATCH] Fix some minor issues with dht api - Use network.c wrapper for ntohs and remove unnecessary system headers - Less nesting in tox_dht_get_nodes_response_handler() - Make dht_getnodes return a bool instead of int --- toxcore/DHT.c | 4 ++-- toxcore/DHT.h | 5 ++--- toxcore/tox.c | 38 +++++++++++++++++--------------------- 3 files changed, 21 insertions(+), 26 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 764b1ec7f3..2ee59817cb 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1773,10 +1773,10 @@ static void do_Close(DHT *dht) } } -int dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id) +bool dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id) { const int ret = getnodes(dht, *from_ipp, from_id, which_id); - return ret < 0 ? -1 : 0; + return ret < 0 ? false : true; } void dht_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key) diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 1a9761c516..a32d3c3b07 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -232,10 +232,9 @@ void dht_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi /** Sends a getnodes request to `from_ipp` with `from_id` for the public key `which_id`. * - * Return 0 on success. - * Return -1 on failure. + * Return true on success. */ -int dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); +bool dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); typedef void dht_get_nodes_response_cb(const DHT *dht, const Node_format *node, void *userdata); diff --git a/toxcore/tox.c b/toxcore/tox.c index 12f25fc3b7..1e8352e8b6 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -13,12 +13,6 @@ #include "tox.h" #include "tox_private.h" -#if defined(OS_WIN32) || (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) -#include -#else -#include -#endif - #include #include #include @@ -308,23 +302,25 @@ static void tox_dht_get_nodes_response_handler(const DHT *dht, const Node_format { struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; - if (tox_data->tox->dht_get_nodes_response_callback != nullptr) { - Tox_Dht_Node *tox_node = (Tox_Dht_Node *)calloc(1, sizeof(Tox_Dht_Node)); + if (tox_data->tox->dht_get_nodes_response_callback == nullptr) { + return; + } - if (tox_node == nullptr) { - return; - } + Tox_Dht_Node *tox_node = (Tox_Dht_Node *)calloc(1, sizeof(Tox_Dht_Node)); - tox_node->data = (Node_format *)calloc(1, sizeof(Node_format)); + if (tox_node == nullptr) { + return; + } - if (tox_node->data == nullptr) { - free(tox_node); - return; - } + tox_node->data = (Node_format *)calloc(1, sizeof(Node_format)); - memcpy(tox_node->data, node, sizeof(Node_format)); - tox_data->tox->dht_get_nodes_response_callback(tox_data->tox, (Tox_Dht_Node *)tox_node, tox_data->user_data); + if (tox_node->data == nullptr) { + free(tox_node); + return; } + + memcpy(tox_node->data, node, sizeof(Node_format)); + tox_data->tox->dht_get_nodes_response_callback(tox_data->tox, (Tox_Dht_Node *)tox_node, tox_data->user_data); } static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id, @@ -2581,10 +2577,10 @@ bool tox_dht_get_nodes(const Tox *tox, const Tox_Dht_Node *dest_node, const uint } lock(tox); - const int ret = dht_getnodes(tox->m->dht, &node->ip_port, node->public_key, public_key); + const bool ret = dht_getnodes(tox->m->dht, &node->ip_port, node->public_key, public_key); unlock(tox); - if (ret < 0) { + if (!ret) { SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_FAIL); return false; } @@ -2601,7 +2597,7 @@ uint16_t tox_dht_node_get_port(const Tox_Dht_Node *dht_node) } const Node_format *node = (const Node_format *)dht_node->data; - return ntohs(node->ip_port.port); + return net_ntohs(node->ip_port.port); } void tox_dht_node_get_public_key(const Tox_Dht_Node *dht_node, uint8_t *public_key)