summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-11-10 17:57:24 -0500
committerirungentoo <irungentoo@gmail.com>2013-11-10 17:57:24 -0500
commit49430547422f82c85792bf4cb0bd3b2597fa7f43 (patch)
tree7ad4fa0a1ef2b788ca3b7fd8d3228461a105a49d /toxcore/DHT.c
parentce29937b8dda03f7b45d3ac2dbc9ee90bdaaa921 (diff)
Fixed small issues.
DHT_test now only prints non zero entries.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c33
1 files changed, 21 insertions, 12 deletions
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. */
815static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes); 815static 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
817static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, uint32_t length, 818static 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 */
1582static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format *list, uint16_t num_nodes) 1583static 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 */
1976int DHT_load_new(DHT *dht, uint8_t *data, uint32_t length) 1985int 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