From 49430547422f82c85792bf4cb0bd3b2597fa7f43 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 10 Nov 2013 17:57:24 -0500 Subject: [PATCH] Fixed small issues. DHT_test now only prints non zero entries. --- testing/DHT_test.c | 7 ++++++- toxcore/DHT.c | 33 +++++++++++++++++++++------------ toxcore/DHT.h | 7 +++++++ toxcore/Messenger.c | 2 +- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 375a2cc1..eac56313 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -52,6 +52,8 @@ #define PORT 33445 +uint8_t zeroes_cid[CLIENT_ID_SIZE]; + void print_client_id(uint8_t * client_id) { uint32_t j; @@ -96,9 +98,10 @@ void print_clientlist(DHT *dht) { uint32_t i; printf("___________________CLOSE________________________________\n"); - for (i = 0; i < LCLIENT_LIST; i++) { Client_data *client = &dht->close_clientlist[i]; + if(memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0) + continue; printf("ClientID: "); print_client_id(client->client_id); @@ -126,6 +129,8 @@ void print_friendlist(DHT *dht) for (i = 0; i < MAX_FRIEND_CLIENTS; i++) { Client_data *client = &dht->friends_list[k].client_list[i]; + if(memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0) + continue; printf("ClientID: "); print_client_id(client->client_id); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 2f85e172..eaf417e2 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -812,7 +812,8 @@ static uint8_t sent_getnode_to_node(DHT *dht, uint8_t *client_id, IP_Port node_i } /* Function is needed in following functions. */ -static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes); +static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, Node_format *list, + uint16_t num_nodes); static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, uint32_t length, size_t node_format_size, uint8_t *plain, uint16_t plain_length, uint32_t *num_nodes_out, Node_format *sendback_node) @@ -891,7 +892,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 ipport_copy(&nodes_list[i].ip_port, &ipp); } - send_hardening_getnode_res(dht, &sendback_node, nodes_list, num_nodes); + send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); return 0; } @@ -910,7 +911,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, Node_format *nodes_list = (Node_format *)(plain); uint32_t i; - send_hardening_getnode_res(dht, &sendback_node, nodes_list, num_nodes); + send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); for (i = 0; i < num_nodes; i++) if (ipport_isset(&nodes_list[i].ip_port)) { @@ -1579,15 +1580,17 @@ static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format * } /* Send a get node hardening response */ -static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes) +static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, Node_format *list, + uint16_t num_nodes) { if (!ip_isset(&sendto->ip_port.ip)) return -1; uint8_t packet[MAX_DATA_SIZE]; - uint8_t data[1 + num_nodes * sizeof(Node_format)]; + uint8_t data[1 + CLIENT_ID_SIZE + num_nodes * sizeof(Node_format)]; data[0] = CHECK_TYPE_GETNODE_RES; - memcpy(data + 1, list, num_nodes * sizeof(Node_format)); + memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); + memcpy(data + 1 + CLIENT_ID_SIZE, list, num_nodes * sizeof(Node_format)); int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, sendto->client_id, data, sizeof(data), CRYPTO_PACKET_HARDENING); @@ -1676,10 +1679,13 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey } case CHECK_TYPE_GETNODE_RES: { - if ((length - 1) % sizeof(Node_format) != 0) + if (length <= CLIENT_ID_SIZE + 1) return 1; - uint16_t num = (length - 1) / sizeof(Node_format); + if ((length - 1 - CLIENT_ID_SIZE) % sizeof(Node_format) != 0) + return 1; + + uint16_t num = (length - 1 - CLIENT_ID_SIZE) / sizeof(Node_format); /* TODO: 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..) */ @@ -1687,14 +1693,14 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey return 1; Node_format nodes[num]; - memcpy(nodes, packet + 1, sizeof(Node_format)*num); + memcpy(nodes, packet + 1 + CLIENT_ID_SIZE, sizeof(Node_format)*num); /* NOTE: This should work for now but should be changed to something better. */ if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (num + 1) / 2) return 1; - IPPTsPng *temp = get_closelist_IPPTsPng(dht, source_pubkey, source.ip.family); + IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); if (temp == NULL) return 1; @@ -1765,12 +1771,15 @@ void do_hardening(DHT *dht) if (!ipport_isset(&rand_node.ip_port)) continue; + if (id_equal(client_id, rand_node.client_id)) + continue; + Node_format to_test; to_test.ip_port = cur_iptspng->ip_port; memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE); //TODO: The search id should maybe not be ours? - if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->c->self_public_key) != -1) { + if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->c->self_public_key) > 0) { memcpy(cur_iptspng->hardening.send_nodes_pingedid, rand_node.client_id, CLIENT_ID_SIZE); cur_iptspng->hardening.send_nodes_timestamp = unix_time(); } @@ -1973,7 +1982,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, * return -1 if failure. * return 0 if success. */ -int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) +int DHT_load(DHT *dht, uint8_t *data, uint32_t length) { uint32_t cookie_len = sizeof(uint32_t); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 9f70f423..f4651515 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -230,6 +230,13 @@ uint32_t DHT_size(DHT *dht); /* Save the DHT in data where data is an array of size DHT_size(). */ void DHT_save(DHT *dht, uint8_t *data); +/* Load the DHT from data of size size. + * + * return -1 if failure. + * return 0 if success. + */ +int DHT_load(DHT *dht, uint8_t *data, uint32_t length); + /* Initialize DHT. */ DHT *new_DHT(Net_Crypto *c); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 659c837b..08e3c70c 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2001,7 +2001,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le break; case MESSENGER_STATE_TYPE_DHT: - DHT_load_new(m->dht, data, length); + DHT_load(m->dht, data, length); break; case MESSENGER_STATE_TYPE_FRIENDS: