Skip to content

Commit

Permalink
Fix some minor issues with dht api
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
JFreegman committed Jan 29, 2022
1 parent b6f8847 commit f21c472
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
38 changes: 17 additions & 21 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
#include "tox.h"
#include "tox_private.h"

#if defined(OS_WIN32) || (defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down

0 comments on commit f21c472

Please sign in to comment.