diff options
-rw-r--r-- | testing/DHT_test.c | 7 | ||||
-rw-r--r-- | toxcore/DHT.c | 33 | ||||
-rw-r--r-- | toxcore/DHT.h | 7 | ||||
-rw-r--r-- | 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 @@ | |||
52 | 52 | ||
53 | #define PORT 33445 | 53 | #define PORT 33445 |
54 | 54 | ||
55 | uint8_t zeroes_cid[CLIENT_ID_SIZE]; | ||
56 | |||
55 | void print_client_id(uint8_t * client_id) | 57 | void print_client_id(uint8_t * client_id) |
56 | { | 58 | { |
57 | uint32_t j; | 59 | uint32_t j; |
@@ -96,9 +98,10 @@ void print_clientlist(DHT *dht) | |||
96 | { | 98 | { |
97 | uint32_t i; | 99 | uint32_t i; |
98 | printf("___________________CLOSE________________________________\n"); | 100 | printf("___________________CLOSE________________________________\n"); |
99 | |||
100 | for (i = 0; i < LCLIENT_LIST; i++) { | 101 | for (i = 0; i < LCLIENT_LIST; i++) { |
101 | Client_data *client = &dht->close_clientlist[i]; | 102 | Client_data *client = &dht->close_clientlist[i]; |
103 | if(memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0) | ||
104 | continue; | ||
102 | printf("ClientID: "); | 105 | printf("ClientID: "); |
103 | print_client_id(client->client_id); | 106 | print_client_id(client->client_id); |
104 | 107 | ||
@@ -126,6 +129,8 @@ void print_friendlist(DHT *dht) | |||
126 | 129 | ||
127 | for (i = 0; i < MAX_FRIEND_CLIENTS; i++) { | 130 | for (i = 0; i < MAX_FRIEND_CLIENTS; i++) { |
128 | Client_data *client = &dht->friends_list[k].client_list[i]; | 131 | Client_data *client = &dht->friends_list[k].client_list[i]; |
132 | if(memcmp(client->client_id, zeroes_cid, CLIENT_ID_SIZE) == 0) | ||
133 | continue; | ||
129 | printf("ClientID: "); | 134 | printf("ClientID: "); |
130 | print_client_id(client->client_id); | 135 | print_client_id(client->client_id); |
131 | 136 | ||
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 | |||
812 | } | 812 | } |
813 | 813 | ||
814 | /* Function is needed in following functions. */ | 814 | /* Function is needed in following functions. */ |
815 | static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes); | 815 | static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, Node_format *list, |
816 | uint16_t num_nodes); | ||
816 | 817 | ||
817 | static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, uint32_t length, | 818 | static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, uint32_t length, |
818 | size_t node_format_size, uint8_t *plain, uint16_t plain_length, uint32_t *num_nodes_out, Node_format *sendback_node) | 819 | 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 | |||
891 | ipport_copy(&nodes_list[i].ip_port, &ipp); | 892 | ipport_copy(&nodes_list[i].ip_port, &ipp); |
892 | } | 893 | } |
893 | 894 | ||
894 | send_hardening_getnode_res(dht, &sendback_node, nodes_list, num_nodes); | 895 | send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); |
895 | return 0; | 896 | return 0; |
896 | } | 897 | } |
897 | 898 | ||
@@ -910,7 +911,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
910 | 911 | ||
911 | Node_format *nodes_list = (Node_format *)(plain); | 912 | Node_format *nodes_list = (Node_format *)(plain); |
912 | uint32_t i; | 913 | uint32_t i; |
913 | send_hardening_getnode_res(dht, &sendback_node, nodes_list, num_nodes); | 914 | send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); |
914 | 915 | ||
915 | for (i = 0; i < num_nodes; i++) | 916 | for (i = 0; i < num_nodes; i++) |
916 | if (ipport_isset(&nodes_list[i].ip_port)) { | 917 | 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 * | |||
1579 | } | 1580 | } |
1580 | 1581 | ||
1581 | /* Send a get node hardening response */ | 1582 | /* Send a get node hardening response */ |
1582 | static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes) | 1583 | static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *queried_client_id, Node_format *list, |
1584 | uint16_t num_nodes) | ||
1583 | { | 1585 | { |
1584 | if (!ip_isset(&sendto->ip_port.ip)) | 1586 | if (!ip_isset(&sendto->ip_port.ip)) |
1585 | return -1; | 1587 | return -1; |
1586 | 1588 | ||
1587 | uint8_t packet[MAX_DATA_SIZE]; | 1589 | uint8_t packet[MAX_DATA_SIZE]; |
1588 | uint8_t data[1 + num_nodes * sizeof(Node_format)]; | 1590 | uint8_t data[1 + CLIENT_ID_SIZE + num_nodes * sizeof(Node_format)]; |
1589 | data[0] = CHECK_TYPE_GETNODE_RES; | 1591 | data[0] = CHECK_TYPE_GETNODE_RES; |
1590 | memcpy(data + 1, list, num_nodes * sizeof(Node_format)); | 1592 | memcpy(data + 1, queried_client_id, CLIENT_ID_SIZE); |
1593 | memcpy(data + 1 + CLIENT_ID_SIZE, list, num_nodes * sizeof(Node_format)); | ||
1591 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, sendto->client_id, data, | 1594 | int len = create_request(dht->c->self_public_key, dht->c->self_secret_key, packet, sendto->client_id, data, |
1592 | sizeof(data), CRYPTO_PACKET_HARDENING); | 1595 | sizeof(data), CRYPTO_PACKET_HARDENING); |
1593 | 1596 | ||
@@ -1676,10 +1679,13 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey | |||
1676 | } | 1679 | } |
1677 | 1680 | ||
1678 | case CHECK_TYPE_GETNODE_RES: { | 1681 | case CHECK_TYPE_GETNODE_RES: { |
1679 | if ((length - 1) % sizeof(Node_format) != 0) | 1682 | if (length <= CLIENT_ID_SIZE + 1) |
1680 | return 1; | 1683 | return 1; |
1681 | 1684 | ||
1682 | uint16_t num = (length - 1) / sizeof(Node_format); | 1685 | if ((length - 1 - CLIENT_ID_SIZE) % sizeof(Node_format) != 0) |
1686 | return 1; | ||
1687 | |||
1688 | uint16_t num = (length - 1 - CLIENT_ID_SIZE) / sizeof(Node_format); | ||
1683 | 1689 | ||
1684 | /* TODO: MAX_SENT_NODES nodes should be returned at all times | 1690 | /* TODO: MAX_SENT_NODES nodes should be returned at all times |
1685 | (right now we have a small network size so it could cause problems for testing and etc..) */ | 1691 | (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 | |||
1687 | return 1; | 1693 | return 1; |
1688 | 1694 | ||
1689 | Node_format nodes[num]; | 1695 | Node_format nodes[num]; |
1690 | memcpy(nodes, packet + 1, sizeof(Node_format)*num); | 1696 | memcpy(nodes, packet + 1 + CLIENT_ID_SIZE, sizeof(Node_format)*num); |
1691 | 1697 | ||
1692 | /* NOTE: This should work for now but should be changed to something better. */ | 1698 | /* NOTE: This should work for now but should be changed to something better. */ |
1693 | if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (num + 1) / 2) | 1699 | if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (num + 1) / 2) |
1694 | return 1; | 1700 | return 1; |
1695 | 1701 | ||
1696 | 1702 | ||
1697 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, source_pubkey, source.ip.family); | 1703 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); |
1698 | 1704 | ||
1699 | if (temp == NULL) | 1705 | if (temp == NULL) |
1700 | return 1; | 1706 | return 1; |
@@ -1765,12 +1771,15 @@ void do_hardening(DHT *dht) | |||
1765 | if (!ipport_isset(&rand_node.ip_port)) | 1771 | if (!ipport_isset(&rand_node.ip_port)) |
1766 | continue; | 1772 | continue; |
1767 | 1773 | ||
1774 | if (id_equal(client_id, rand_node.client_id)) | ||
1775 | continue; | ||
1776 | |||
1768 | Node_format to_test; | 1777 | Node_format to_test; |
1769 | to_test.ip_port = cur_iptspng->ip_port; | 1778 | to_test.ip_port = cur_iptspng->ip_port; |
1770 | memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE); | 1779 | memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE); |
1771 | 1780 | ||
1772 | //TODO: The search id should maybe not be ours? | 1781 | //TODO: The search id should maybe not be ours? |
1773 | if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->c->self_public_key) != -1) { | 1782 | if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->c->self_public_key) > 0) { |
1774 | memcpy(cur_iptspng->hardening.send_nodes_pingedid, rand_node.client_id, CLIENT_ID_SIZE); | 1783 | memcpy(cur_iptspng->hardening.send_nodes_pingedid, rand_node.client_id, CLIENT_ID_SIZE); |
1775 | cur_iptspng->hardening.send_nodes_timestamp = unix_time(); | 1784 | cur_iptspng->hardening.send_nodes_timestamp = unix_time(); |
1776 | } | 1785 | } |
@@ -1973,7 +1982,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, | |||
1973 | * return -1 if failure. | 1982 | * return -1 if failure. |
1974 | * return 0 if success. | 1983 | * return 0 if success. |
1975 | */ | 1984 | */ |
1976 | int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) | 1985 | int DHT_load(DHT *dht, uint8_t *data, uint32_t length) |
1977 | { | 1986 | { |
1978 | uint32_t cookie_len = sizeof(uint32_t); | 1987 | uint32_t cookie_len = sizeof(uint32_t); |
1979 | 1988 | ||
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); | |||
230 | /* Save the DHT in data where data is an array of size DHT_size(). */ | 230 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
231 | void DHT_save(DHT *dht, uint8_t *data); | 231 | void DHT_save(DHT *dht, uint8_t *data); |
232 | 232 | ||
233 | /* Load the DHT from data of size size. | ||
234 | * | ||
235 | * return -1 if failure. | ||
236 | * return 0 if success. | ||
237 | */ | ||
238 | int DHT_load(DHT *dht, uint8_t *data, uint32_t length); | ||
239 | |||
233 | /* Initialize DHT. */ | 240 | /* Initialize DHT. */ |
234 | DHT *new_DHT(Net_Crypto *c); | 241 | DHT *new_DHT(Net_Crypto *c); |
235 | 242 | ||
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 | |||
2001 | break; | 2001 | break; |
2002 | 2002 | ||
2003 | case MESSENGER_STATE_TYPE_DHT: | 2003 | case MESSENGER_STATE_TYPE_DHT: |
2004 | DHT_load_new(m->dht, data, length); | 2004 | DHT_load(m->dht, data, length); |
2005 | break; | 2005 | break; |
2006 | 2006 | ||
2007 | case MESSENGER_STATE_TYPE_FRIENDS: | 2007 | case MESSENGER_STATE_TYPE_FRIENDS: |