diff options
author | irungentoo <irungentoo@gmail.com> | 2013-10-30 19:50:06 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-10-30 19:50:06 -0400 |
commit | beaa31bebcee71a33652f3a0fd232f47acb3be56 (patch) | |
tree | 2de14bf1010724f7812f8bb3a73135c2df56f035 /toxcore | |
parent | 415835ce3d2daa9ecd4100e76d58d93b3afa2db0 (diff) |
Testing requests are now sent.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 82 | ||||
-rw-r--r-- | toxcore/DHT.h | 37 | ||||
-rw-r--r-- | toxcore/group_chats.c | 2 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 2 | ||||
-rw-r--r-- | toxcore/network.c | 2 |
5 files changed, 103 insertions, 22 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 5232deed..3a1b9cf0 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1545,8 +1545,12 @@ static void do_NAT(DHT *dht) | |||
1545 | 1545 | ||
1546 | #define HARDREQ_DATA_SIZE 768 /* Attempt to prevent amplification/other attacks*/ | 1546 | #define HARDREQ_DATA_SIZE 768 /* Attempt to prevent amplification/other attacks*/ |
1547 | 1547 | ||
1548 | #define CHECK_TYPE_GETNODE_REQ 0 | 1548 | #define CHECK_TYPE_ROUTE_REQ 0 |
1549 | #define CHECK_TYPE_GETNODE_RES 1 | 1549 | #define CHECK_TYPE_ROUTE_RES 1 |
1550 | #define CHECK_TYPE_GETNODE_REQ 2 | ||
1551 | #define CHECK_TYPE_GETNODE_RES 3 | ||
1552 | #define CHECK_TYPE_TEST_REQ 4 | ||
1553 | #define CHECK_TYPE_TEST_RES 5 | ||
1550 | 1554 | ||
1551 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) | 1555 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) |
1552 | { | 1556 | { |
@@ -1639,6 +1643,80 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey | |||
1639 | return 1; | 1643 | return 1; |
1640 | } | 1644 | } |
1641 | 1645 | ||
1646 | /* Return a random node from all the nodes we are connected to. | ||
1647 | * TODO: improve this function. | ||
1648 | */ | ||
1649 | Node_format random_node(DHT *dht, sa_family_t sa_family) | ||
1650 | { | ||
1651 | uint8_t id[CLIENT_ID_SIZE]; | ||
1652 | uint32_t i; | ||
1653 | |||
1654 | for (i = 0; i < CLIENT_ID_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ | ||
1655 | uint32_t t = rand(); | ||
1656 | memcpy(id + i * sizeof(t), &t, sizeof(t)); | ||
1657 | } | ||
1658 | |||
1659 | Node_format nodes_list[MAX_SENT_NODES]; | ||
1660 | memset(nodes_list, 0, sizeof(nodes_list)); | ||
1661 | int num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1); | ||
1662 | |||
1663 | if (num_nodes < 1) | ||
1664 | return nodes_list[0]; | ||
1665 | else | ||
1666 | return nodes_list[rand() % num_nodes]; | ||
1667 | } | ||
1668 | |||
1669 | /* Interval in seconds between checks */ | ||
1670 | #define HARDENING_INTERVAL 5 | ||
1671 | #define HARDEN_TIMEOUT 500 | ||
1672 | |||
1673 | void do_hardening(DHT *dht) | ||
1674 | { | ||
1675 | uint32_t i; | ||
1676 | |||
1677 | for (i = 0; i < LCLIENT_LIST * 2; ++i) { | ||
1678 | IPPTsPng *cur_iptspng; | ||
1679 | sa_family_t sa_family; | ||
1680 | uint8_t *client_id = dht->close_clientlist[i / 2].client_id; | ||
1681 | |||
1682 | if (i % 2 == 0) { | ||
1683 | cur_iptspng = &dht->close_clientlist[i / 2].assoc4; | ||
1684 | sa_family = AF_INET; | ||
1685 | } else { | ||
1686 | cur_iptspng = &dht->close_clientlist[i / 2].assoc6; | ||
1687 | sa_family = AF_INET6; | ||
1688 | } | ||
1689 | |||
1690 | if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) | ||
1691 | continue; | ||
1692 | |||
1693 | if (cur_iptspng->hardening.send_nodes_ok == 0) { | ||
1694 | if (is_timeout(cur_iptspng->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) { | ||
1695 | Node_format rand_node = random_node(dht, sa_family); | ||
1696 | |||
1697 | if (!ipport_isset(&rand_node.ip_port)) | ||
1698 | continue; | ||
1699 | |||
1700 | Node_format to_test; | ||
1701 | to_test.ip_port = cur_iptspng->ip_port; | ||
1702 | memcpy(to_test.client_id, client_id, CLIENT_ID_SIZE); | ||
1703 | |||
1704 | //TODO: The search id should maybe not be ours? | ||
1705 | if (send_hardening_getnode_req(dht, &rand_node, &to_test, dht->c->self_public_key) != -1) { | ||
1706 | memcpy(cur_iptspng->hardening.send_nodes_pingedid, rand_node.client_id, CLIENT_ID_SIZE); | ||
1707 | cur_iptspng->hardening.send_nodes_timestamp = unix_time(); | ||
1708 | } | ||
1709 | } | ||
1710 | } else { | ||
1711 | if (is_timeout(cur_iptspng->hardening.send_nodes_timestamp, HARDEN_TIMEOUT)) { | ||
1712 | cur_iptspng->hardening.send_nodes_ok = 0; | ||
1713 | } | ||
1714 | } | ||
1715 | |||
1716 | //TODO: add the 2 other testers. | ||
1717 | } | ||
1718 | } | ||
1719 | |||
1642 | /*----------------------------------------------------------------------------------*/ | 1720 | /*----------------------------------------------------------------------------------*/ |
1643 | 1721 | ||
1644 | DHT *new_DHT(Net_Crypto *c) | 1722 | DHT *new_DHT(Net_Crypto *c) |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index ac02710f..ec62442e 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -39,10 +39,29 @@ | |||
39 | #define MAX_TOPING 16 | 39 | #define MAX_TOPING 16 |
40 | 40 | ||
41 | typedef struct { | 41 | typedef struct { |
42 | /* Node routes request correctly (true (1) or false/didn't check (0)) */ | ||
43 | uint8_t routes_requests_ok; | ||
44 | /* Time which we last checked this.*/ | ||
45 | uint64_t routes_requests_timestamp; | ||
46 | uint8_t routes_requests_pingedid[CLIENT_ID_SIZE]; | ||
47 | /* Node sends correct send_node (true (1) or false/didn't check (0)) */ | ||
48 | uint8_t send_nodes_ok; | ||
49 | /* Time which we last checked this.*/ | ||
50 | uint64_t send_nodes_timestamp; | ||
51 | uint8_t send_nodes_pingedid[CLIENT_ID_SIZE]; | ||
52 | /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */ | ||
53 | uint8_t testing_requests; | ||
54 | /* Time which we last checked this.*/ | ||
55 | uint64_t testing_timestamp; | ||
56 | uint8_t testing_pingedid[CLIENT_ID_SIZE]; | ||
57 | } Hardening; | ||
58 | |||
59 | typedef struct { | ||
42 | IP_Port ip_port; | 60 | IP_Port ip_port; |
43 | uint64_t timestamp; | 61 | uint64_t timestamp; |
44 | uint64_t last_pinged; | 62 | uint64_t last_pinged; |
45 | 63 | ||
64 | Hardening hardening; | ||
46 | /* Returned by this node. Either our friend or us. */ | 65 | /* Returned by this node. Either our friend or us. */ |
47 | IP_Port ret_ip_port; | 66 | IP_Port ret_ip_port; |
48 | uint64_t ret_timestamp; | 67 | uint64_t ret_timestamp; |
@@ -67,28 +86,12 @@ typedef struct { | |||
67 | } NAT; | 86 | } NAT; |
68 | 87 | ||
69 | typedef struct { | 88 | typedef struct { |
70 | /* Node routes request correctly (true (1) or false/didn't check (0)) */ | ||
71 | uint8_t routes_requests_ok; | ||
72 | /* Time which we last checked this.*/ | ||
73 | uint64_t routes_requests_timestamp; | ||
74 | /* Node sends correct send_node (true (1) or false/didn't check (0)) */ | ||
75 | uint8_t send_nodes_ok; | ||
76 | /* Time which we last checked this.*/ | ||
77 | uint64_t send_nodes_timestamp; | ||
78 | /* Node can be used to test other nodes (true (1) or false/didn't check (0)) */ | ||
79 | uint8_t testing_requests; | ||
80 | /* Time which we last checked this.*/ | ||
81 | uint64_t testing_timestamp; | ||
82 | } Hardening; | ||
83 | |||
84 | typedef struct { | ||
85 | uint8_t client_id[CLIENT_ID_SIZE]; | 89 | uint8_t client_id[CLIENT_ID_SIZE]; |
86 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 90 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
87 | 91 | ||
88 | /* Time at which the last get_nodes request was sent. */ | 92 | /* Time at which the last get_nodes request was sent. */ |
89 | uint64_t lastgetnode; | 93 | uint64_t lastgetnode; |
90 | 94 | ||
91 | Hardening hardening; | ||
92 | /* Symetric NAT hole punching stuff. */ | 95 | /* Symetric NAT hole punching stuff. */ |
93 | NAT nat; | 96 | NAT nat; |
94 | } DHT_Friend; | 97 | } DHT_Friend; |
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index fdf6e834..5831e7d4 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -531,7 +531,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, | |||
531 | Group_Chat *new_groupchat(Networking_Core *net) | 531 | Group_Chat *new_groupchat(Networking_Core *net) |
532 | { | 532 | { |
533 | unix_time_update(); | 533 | unix_time_update(); |
534 | 534 | ||
535 | if (net == 0) | 535 | if (net == 0) |
536 | return 0; | 536 | return 0; |
537 | 537 | ||
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index d58f4c27..8be1059e 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -800,7 +800,7 @@ static void receive_crypto(Net_Crypto *c) | |||
800 | Net_Crypto *new_net_crypto(Networking_Core *net) | 800 | Net_Crypto *new_net_crypto(Networking_Core *net) |
801 | { | 801 | { |
802 | unix_time_update(); | 802 | unix_time_update(); |
803 | 803 | ||
804 | if (net == NULL) | 804 | if (net == NULL) |
805 | return NULL; | 805 | return NULL; |
806 | 806 | ||
diff --git a/toxcore/network.c b/toxcore/network.c index d163c0eb..5cbc4695 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -278,7 +278,7 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl | |||
278 | void networking_poll(Networking_Core *net) | 278 | void networking_poll(Networking_Core *net) |
279 | { | 279 | { |
280 | unix_time_update(); | 280 | unix_time_update(); |
281 | 281 | ||
282 | IP_Port ip_port; | 282 | IP_Port ip_port; |
283 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 283 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
284 | uint32_t length; | 284 | uint32_t length; |