summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c121
-rw-r--r--toxcore/ping.c12
2 files changed, 64 insertions, 69 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 105d10d4..83bf4888 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -661,17 +661,28 @@ int addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id)
661 used++; 661 used++;
662 } 662 }
663 663
664 if (dht->assoc) {
665 IPPTs ippts;
666
667 ippts.ip_port = ip_port;
668 ippts.timestamp = unix_time();
669
670 Assoc_add_entry(dht->assoc, client_id, &ippts, NULL, used ? 1 : 0);
671 }
672
664 return used; 673 return used;
665} 674}
666 675
667/* If client_id is a friend or us, update ret_ip_port 676/* If client_id is a friend or us, update ret_ip_port
668 * nodeclient_id is the id of the node that sent us this info. 677 * nodeclient_id is the id of the node that sent us this info.
669 */ 678 */
670static int returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) 679static int returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id, IP_Port source)
671{ 680{
672 uint32_t i, j; 681 uint32_t i, j;
673 uint64_t temp_time = unix_time(); 682 uint64_t temp_time = unix_time();
674 683
684 uint32_t used = 0;
685
675 /* convert IPv4-in-IPv6 to IPv4 */ 686 /* convert IPv4-in-IPv6 to IPv4 */
676 if ((ip_port.ip.family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&ip_port.ip.ip6.in6_addr)) { 687 if ((ip_port.ip.family == AF_INET6) && IN6_IS_ADDR_V4MAPPED(&ip_port.ip.ip6.in6_addr)) {
677 ip_port.ip.family = AF_INET; 688 ip_port.ip.family = AF_INET;
@@ -689,7 +700,8 @@ static int returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8
689 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; 700 dht->close_clientlist[i].assoc6.ret_timestamp = temp_time;
690 } 701 }
691 702
692 return 1; 703 ++used;
704 break;
693 } 705 }
694 } 706 }
695 } else { 707 } else {
@@ -705,13 +717,23 @@ static int returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint8
705 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; 717 dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time;
706 } 718 }
707 719
708 return 1; 720 ++used;
721 goto end;
709 } 722 }
710 } 723 }
711 } 724 }
712 } 725 }
713 } 726 }
714 727
728end:
729
730 if (dht->assoc) {
731 IPPTs ippts;
732 ippts.ip_port = source;
733 ippts.timestamp = temp_time;
734 Assoc_add_entry(dht->assoc, nodeclient_id, &ippts, &ip_port, used ? 1 : 0);
735 }
736
715 return 0; 737 return 0;
716} 738}
717 739
@@ -853,6 +875,7 @@ void to_net_family(IP *ip)
853 ip->padding[0] = 0; 875 ip->padding[0] = 0;
854 ip->padding[1] = 0; 876 ip->padding[1] = 0;
855 ip->padding[2] = 0; 877 ip->padding[2] = 0;
878
856 if (ip->family == AF_INET) 879 if (ip->family == AF_INET)
857 ip->family = TOX_AF_INET; 880 ip->family = TOX_AF_INET;
858 else if (ip->family == AF_INET6) 881 else if (ip->family == AF_INET6)
@@ -889,6 +912,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
889 new_nonce(nonce); 912 new_nonce(nonce);
890 913
891 uint32_t i; 914 uint32_t i;
915
892 for (i = 0; i < num_nodes; ++i) 916 for (i = 0; i < num_nodes; ++i)
893 to_net_family(&nodes_list[i].ip_port.ip); 917 to_net_family(&nodes_list[i].ip_port.ip);
894 918
@@ -1016,16 +1040,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet,
1016 return 1; 1040 return 1;
1017 1041
1018 /* store the address the *request* was sent to */ 1042 /* store the address the *request* was sent to */
1019 int used = addto_lists(dht, source, packet + 1); 1043 addto_lists(dht, source, packet + 1);
1020
1021 if (dht->assoc) {
1022 IPPTs ippts;
1023
1024 ippts.ip_port = source;
1025 ippts.timestamp = unix_time();
1026
1027 Assoc_add_entry(dht->assoc, packet + 1, &ippts, &source, used ? 1 : 0);
1028 }
1029 1044
1030 *num_nodes_out = num_nodes; 1045 *num_nodes_out = num_nodes;
1031 1046
@@ -1065,13 +1080,11 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
1065 ippts.ip_port.port = nodes4_list[i].ip_port.port; 1080 ippts.ip_port.port = nodes4_list[i].ip_port.port;
1066 1081
1067 send_ping_request(dht->ping, ippts.ip_port, nodes4_list[i].client_id); 1082 send_ping_request(dht->ping, ippts.ip_port, nodes4_list[i].client_id);
1068 int used = returnedip_ports(dht, ippts.ip_port, nodes4_list[i].client_id, packet + 1); 1083 returnedip_ports(dht, ippts.ip_port, nodes4_list[i].client_id, packet + 1, source);
1069 1084
1070 memcpy(nodes_list[i].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE); 1085 memcpy(nodes_list[i].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE);
1071 ipport_copy(&nodes_list[i].ip_port, &ippts.ip_port); 1086 ipport_copy(&nodes_list[i].ip_port, &ippts.ip_port);
1072 1087
1073 if (dht->assoc)
1074 Assoc_add_entry(dht->assoc, nodes4_list[i].client_id, &ippts, NULL, used ? 1 : 0);
1075 } 1088 }
1076 1089
1077 send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); 1090 send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes);
@@ -1095,23 +1108,18 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet,
1095 return 0; 1108 return 0;
1096 1109
1097 Node_format *nodes_list = (Node_format *)(plain); 1110 Node_format *nodes_list = (Node_format *)(plain);
1098 uint64_t time_now = unix_time();
1099 uint32_t i; 1111 uint32_t i;
1100 send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes); 1112 send_hardening_getnode_res(dht, &sendback_node, packet + 1, nodes_list, num_nodes);
1113
1101 for (i = 0; i < num_nodes; i++) { 1114 for (i = 0; i < num_nodes; i++) {
1102 to_host_family(&nodes_list[i].ip_port.ip); 1115 to_host_family(&nodes_list[i].ip_port.ip);
1116
1103 if (ipport_isset(&nodes_list[i].ip_port)) { 1117 if (ipport_isset(&nodes_list[i].ip_port)) {
1104 send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id); 1118 send_ping_request(dht->ping, nodes_list[i].ip_port, nodes_list[i].client_id);
1105 int used = returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); 1119 returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1, source);
1106 if (dht->assoc) {
1107 IPPTs ippts;
1108 ippts.ip_port = nodes_list[i].ip_port;
1109 ippts.timestamp = time_now;
1110
1111 Assoc_add_entry(dht->assoc, nodes_list[i].client_id, &ippts, NULL, used ? 1 : 0);
1112 }
1113 } 1120 }
1114 } 1121 }
1122
1115 return 0; 1123 return 0;
1116} 1124}
1117 1125
@@ -1852,39 +1860,7 @@ static int send_hardening_getnode_res(DHT *dht, Node_format *sendto, uint8_t *qu
1852 1860
1853 return sendpacket(dht->c->lossless_udp->net, sendto->ip_port, packet, len); 1861 return sendpacket(dht->c->lossless_udp->net, sendto->ip_port, packet, len);
1854} 1862}
1855/*
1856 * check how many nodes in nodes are also present in the closelist.
1857 * TODO: make this function better.
1858 */
1859static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num, sa_family_t sa_family)
1860{
1861 Node_format nodes_list[MAX_SENT_NODES];
1862 uint32_t num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1, 1);
1863
1864 if (num_nodes < num) {
1865 num_nodes = get_close_nodes(dht, dht->c->self_public_key, nodes_list, sa_family, 1, 0);
1866 }
1867 1863
1868 uint32_t counter = 0;
1869 uint32_t i, j;
1870
1871 for (i = 0; i < num; ++i) {
1872 if (id_equal(nodes[i].client_id, dht->c->self_public_key)) {
1873 ++counter;
1874 }
1875
1876 for (j = 0; j < num_nodes; ++j) {
1877 if (id_equal(nodes[i].client_id, nodes_list[j].client_id)) {
1878 if (ipport_equal(&nodes[i].ip_port, &nodes_list[j].ip_port)) {
1879 ++counter;
1880 break;
1881 }
1882 }
1883 }
1884 }
1885
1886 return counter;
1887}
1888/* TODO: improve */ 1864/* TODO: improve */
1889static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, uint8_t *client_id, sa_family_t sa_family) 1865static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, uint8_t *client_id, sa_family_t sa_family)
1890{ 1866{
@@ -1903,6 +1879,33 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, uint8_t *client_id, sa_family_
1903 return NULL; 1879 return NULL;
1904} 1880}
1905 1881
1882/*
1883 * check how many nodes in nodes are also present in the closelist.
1884 * TODO: make this function better.
1885 */
1886static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num)
1887{
1888 uint32_t counter = 0;
1889 uint32_t i;
1890
1891 for (i = 0; i < num; ++i) {
1892 if (id_equal(nodes[i].client_id, dht->c->self_public_key)) {
1893 ++counter;
1894 continue;
1895 }
1896
1897 IPPTsPng *temp = get_closelist_IPPTsPng(dht, nodes[i].client_id, nodes[i].ip_port.ip.family);
1898
1899 if (temp) {
1900 if (!is_timeout(temp->timestamp, BAD_NODE_TIMEOUT)) {
1901 ++counter;
1902 }
1903 }
1904 }
1905
1906 return counter;
1907}
1908
1906/* Interval in seconds between hardening checks */ 1909/* Interval in seconds between hardening checks */
1907#define HARDENING_INTERVAL 5 1910#define HARDENING_INTERVAL 5
1908#define HARDEN_TIMEOUT 500 1911#define HARDEN_TIMEOUT 500
@@ -1949,10 +1952,12 @@ static int handle_hardening(void *object, IP_Port source, uint8_t *source_pubkey
1949 Node_format nodes[num]; 1952 Node_format nodes[num];
1950 memcpy(nodes, packet + 1 + CLIENT_ID_SIZE, sizeof(Node_format)*num); 1953 memcpy(nodes, packet + 1 + CLIENT_ID_SIZE, sizeof(Node_format)*num);
1951 uint32_t i; 1954 uint32_t i;
1955
1952 for (i = 0; i < num; ++i) 1956 for (i = 0; i < num; ++i)
1953 to_host_family(&nodes[i].ip_port.ip); 1957 to_host_family(&nodes[i].ip_port.ip);
1958
1954 /* NOTE: This should work for now but should be changed to something better. */ 1959 /* NOTE: This should work for now but should be changed to something better. */
1955 if (have_nodes_closelist(dht, nodes, num, nodes[0].ip_port.ip.family) < (uint32_t)((num + 1) / 2)) 1960 if (have_nodes_closelist(dht, nodes, num) < (uint32_t)((num + 2) / 2))
1956 return 1; 1961 return 1;
1957 1962
1958 1963
diff --git a/toxcore/ping.c b/toxcore/ping.c
index e3db6ed1..6c368d0c 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -30,7 +30,6 @@
30#include <stdint.h> 30#include <stdint.h>
31 31
32#include "DHT.h" 32#include "DHT.h"
33#include "assoc.h"
34#include "ping.h" 33#include "ping.h"
35 34
36#include "network.h" 35#include "network.h"
@@ -260,16 +259,7 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin
260 if (!ping_index) 259 if (!ping_index)
261 return 1; 260 return 1;
262 261
263 /* Associate client_id with the ip the request was sent to */ 262 addto_lists(dht, source, packet + 1);
264 int used = addto_lists(dht, ping->pings[ping_index - 1].ip_port, packet + 1);
265
266 if (dht->assoc) {
267 IPPTs ippts;
268 ippts.ip_port = ping->pings[ping_index - 1].ip_port;
269 ippts.timestamp = ping->pings[ping_index - 1].timestamp;
270
271 Assoc_add_entry(dht->assoc, packet + 1, &ippts, &source, used > 0 ? 1 : 0);
272 }
273 263
274 return 0; 264 return 0;
275} 265}