Fixed small issues.

DHT_test now only prints non zero entries.
This commit is contained in:
irungentoo 2013-11-10 17:57:24 -05:00
parent ce29937b8d
commit 4943054742
4 changed files with 35 additions and 14 deletions

View File

@ -52,6 +52,8 @@
#define PORT 33445 #define PORT 33445
uint8_t zeroes_cid[CLIENT_ID_SIZE];
void print_client_id(uint8_t * client_id) void print_client_id(uint8_t * client_id)
{ {
uint32_t j; uint32_t j;
@ -96,9 +98,10 @@ void print_clientlist(DHT *dht)
{ {
uint32_t i; uint32_t i;
printf("___________________CLOSE________________________________\n"); printf("___________________CLOSE________________________________\n");
for (i = 0; i < LCLIENT_LIST; i++) { for (i = 0; i < LCLIENT_LIST; i++) {
Client_data *client = &dht->close_clientlist[i]; Client_data *client = &dht->close_clientlist[i];
if(memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0)
continue;
printf("ClientID: "); printf("ClientID: ");
print_client_id(client->client_id); print_client_id(client->client_id);
@ -126,6 +129,8 @@ void print_friendlist(DHT *dht)
for (i = 0; i < MAX_FRIEND_CLIENTS; i++) { for (i = 0; i < MAX_FRIEND_CLIENTS; i++) {
Client_data *client = &dht->friends_list[k].client_list[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: "); printf("ClientID: ");
print_client_id(client->client_id); print_client_id(client->client_id);

View File

@ -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. */ /* 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, 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) 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); 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; 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); Node_format *nodes_list = (Node_format *)(plain);
uint32_t i; 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++) for (i = 0; i < num_nodes; i++)
if (ipport_isset(&nodes_list[i].ip_port)) { 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 */ /* 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)) if (!ip_isset(&sendto->ip_port.ip))
return -1; return -1;
uint8_t packet[MAX_DATA_SIZE]; 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; 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, int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, sendto->client_id, data,
sizeof(data), CRYPTO_PACKET_HARDENING); 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: { case CHECK_TYPE_GETNODE_RES: {
if ((length - 1) % sizeof(Node_format) != 0) if (length <= CLIENT_ID_SIZE + 1)
return 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 /* 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..) */ (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; return 1;
Node_format nodes[num]; 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. */ /* 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) if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (num + 1) / 2)
return 1; 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) if (temp == NULL)
return 1; return 1;
@ -1765,12 +1771,15 @@ void do_hardening(DHT *dht)
if (!ipport_isset(&rand_node.ip_port)) if (!ipport_isset(&rand_node.ip_port))
continue; continue;
if (id_equal(client_id, rand_node.client_id))
continue;
Node_format to_test; Node_format to_test;
to_test.ip_port = cur_iptspng->ip_port; to_test.ip_port = cur_iptspng->ip_port;
memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE); memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE);
//TODO: The search id should maybe not be ours? //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); memcpy(cur_iptspng->hardening.send_nodes_pingedid, rand_node.client_id, CLIENT_ID_SIZE);
cur_iptspng->hardening.send_nodes_timestamp = unix_time(); 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 -1 if failure.
* return 0 if success. * 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); uint32_t cookie_len = sizeof(uint32_t);

View File

@ -230,6 +230,13 @@ uint32_t DHT_size(DHT *dht);
/* Save the DHT in data where data is an array of size DHT_size(). */ /* 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(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. */ /* Initialize DHT. */
DHT *new_DHT(Net_Crypto *c); DHT *new_DHT(Net_Crypto *c);

View File

@ -2001,7 +2001,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le
break; break;
case MESSENGER_STATE_TYPE_DHT: case MESSENGER_STATE_TYPE_DHT:
DHT_load_new(m->dht, data, length); DHT_load(m->dht, data, length);
break; break;
case MESSENGER_STATE_TYPE_FRIENDS: case MESSENGER_STATE_TYPE_FRIENDS: