diff options
-rw-r--r-- | toxcore/DHT.c | 78 | ||||
-rw-r--r-- | toxcore/DHT.h | 2 |
2 files changed, 75 insertions, 5 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 3a1b9cf0..c43382ed 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1597,6 +1597,59 @@ static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, Node_format | |||
1597 | 1597 | ||
1598 | return sendpacket(dht->c->lossless_udp->net, sendto->ip_port, packet, len); | 1598 | return sendpacket(dht->c->lossless_udp->net, sendto->ip_port, packet, len); |
1599 | } | 1599 | } |
1600 | /* | ||
1601 | * check how many nodes in nodes are also present in the closelist. | ||
1602 | * TODO: make this function better. | ||
1603 | */ | ||
1604 | static int have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num, sa_family_t sa_family) | ||
1605 | { | ||
1606 | Node_format nodes_list[MAX_SENT_NODES]; | ||
1607 | int num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1); | ||
1608 | |||
1609 | if (num_nodes < 1) | ||
1610 | return -1; | ||
1611 | |||
1612 | int counter = 0; | ||
1613 | uint32_t i, j; | ||
1614 | |||
1615 | for (i = 0; i < num; ++i) { | ||
1616 | if (id_equal(nodes[i].client_id, dht->c->self_public_key)) { | ||
1617 | ++counter; | ||
1618 | } | ||
1619 | |||
1620 | for (j = 0; j < (uint32_t)num_nodes; ++j) { | ||
1621 | if (id_equal(nodes[i].client_id, nodes_list[j].client_id)) { | ||
1622 | if (ipport_equal(&nodes[i].ip_port, &nodes_list[j].ip_port)) { | ||
1623 | ++counter; | ||
1624 | break; | ||
1625 | } | ||
1626 | } | ||
1627 | } | ||
1628 | } | ||
1629 | |||
1630 | return counter; | ||
1631 | } | ||
1632 | /* TODO: improve */ | ||
1633 | static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, uint8_t *client_id, sa_family_t sa_family) | ||
1634 | { | ||
1635 | uint32_t i; | ||
1636 | |||
1637 | for (i = 0; i < LCLIENT_LIST; ++i) { | ||
1638 | if (memcmp(dht->close_clientlist[i].client_id, client_id, CLIENT_ID_SIZE) != 0) | ||
1639 | continue; | ||
1640 | |||
1641 | if (sa_family == AF_INET) | ||
1642 | return &dht->close_clientlist[i].assoc4; | ||
1643 | else if (sa_family == AF_INET6) | ||
1644 | return &dht->close_clientlist[i].assoc6; | ||
1645 | } | ||
1646 | |||
1647 | return NULL; | ||
1648 | } | ||
1649 | |||
1650 | /* Interval in seconds between hardening checks */ | ||
1651 | #define HARDENING_INTERVAL 5 | ||
1652 | #define HARDEN_TIMEOUT 500 | ||
1600 | 1653 | ||
1601 | /* Handle a received hardening packet */ | 1654 | /* Handle a received hardening packet */ |
1602 | static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) | 1655 | static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey, uint8_t *packet, uint32_t length) |
@@ -1629,13 +1682,32 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey | |||
1629 | 1682 | ||
1630 | uint16_t num = (length - 1) / sizeof(Node_format); | 1683 | uint16_t num = (length - 1) / sizeof(Node_format); |
1631 | 1684 | ||
1685 | /* TODO: MAX_SENT_NODES nodes should be returned at all times | ||
1686 | (right now we have a small network size so it could cause problems for testing and etc..) */ | ||
1632 | if (num > MAX_SENT_NODES || num == 0) | 1687 | if (num > MAX_SENT_NODES || num == 0) |
1633 | return 1; | 1688 | return 1; |
1634 | 1689 | ||
1635 | Node_format nodes[num]; | 1690 | Node_format nodes[num]; |
1636 | memcpy(nodes, packet + 1, sizeof(Node_format)*num); | 1691 | memcpy(nodes, packet + 1, sizeof(Node_format)*num); |
1692 | |||
1693 | /* NOTE: This should work for now but should be changed to something better. */ | ||
1694 | if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (num + 1) / 2) | ||
1695 | return 1; | ||
1696 | |||
1697 | |||
1698 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, source_pubkey, source.ip.family); | ||
1699 | |||
1700 | if (temp == NULL) | ||
1701 | return 1; | ||
1702 | |||
1703 | if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) | ||
1704 | return 1; | ||
1705 | |||
1706 | if (memcmp(temp->hardening.send_nodes_pingedid, source_pubkey, CLIENT_ID_SIZE) != 0) | ||
1707 | return 1; | ||
1708 | |||
1637 | /* If Nodes look good and the request checks out */ | 1709 | /* If Nodes look good and the request checks out */ |
1638 | //TODO | 1710 | temp->hardening.routes_requests_ok = 1; |
1639 | return 0;/* success*/ | 1711 | return 0;/* success*/ |
1640 | } | 1712 | } |
1641 | } | 1713 | } |
@@ -1666,9 +1738,6 @@ Node_format random_node(DHT *dht, sa_family_t sa_family) | |||
1666 | return nodes_list[rand() % num_nodes]; | 1738 | return nodes_list[rand() % num_nodes]; |
1667 | } | 1739 | } |
1668 | 1740 | ||
1669 | /* Interval in seconds between checks */ | ||
1670 | #define HARDENING_INTERVAL 5 | ||
1671 | #define HARDEN_TIMEOUT 500 | ||
1672 | 1741 | ||
1673 | void do_hardening(DHT *dht) | 1742 | void do_hardening(DHT *dht) |
1674 | { | 1743 | { |
@@ -1759,6 +1828,7 @@ void do_DHT(DHT *dht) | |||
1759 | do_DHT_friends(dht); | 1828 | do_DHT_friends(dht); |
1760 | do_NAT(dht); | 1829 | do_NAT(dht); |
1761 | do_toping(dht->ping); | 1830 | do_toping(dht->ping); |
1831 | do_hardening(dht); | ||
1762 | } | 1832 | } |
1763 | void kill_DHT(DHT *dht) | 1833 | void kill_DHT(DHT *dht) |
1764 | { | 1834 | { |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index ec62442e..9f70f423 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -60,7 +60,7 @@ typedef struct { | |||
60 | IP_Port ip_port; | 60 | IP_Port ip_port; |
61 | uint64_t timestamp; | 61 | uint64_t timestamp; |
62 | uint64_t last_pinged; | 62 | uint64_t last_pinged; |
63 | 63 | ||
64 | Hardening hardening; | 64 | Hardening hardening; |
65 | /* Returned by this node. Either our friend or us. */ | 65 | /* Returned by this node. Either our friend or us. */ |
66 | IP_Port ret_ip_port; | 66 | IP_Port ret_ip_port; |