DHT code cleanup.

Overall behaviour should not have changed much except that there are now
3 different lists for lan, ipv6 and ipv6 nodes.

The DHT now uses a tree structure to store known nodes for close and friend
nodes.

It compiles but I barely tested it yet.
pull/1572/head
irungentoo 2016-03-29 11:38:07 -04:00
parent 532629d486
commit 3db527e3cd
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
13 changed files with 944 additions and 3000 deletions

View File

@ -1,7 +1,7 @@
if BUILD_TESTS
TESTS = encryptsave_test messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test dht_autotest
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test dht_autotest
TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest
AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \
@ -47,12 +47,6 @@ network_test_CFLAGS = $(AUTOTEST_CFLAGS)
network_test_LDADD = $(AUTOTEST_LDADD)
assoc_test_SOURCES = ../auto_tests/assoc_test.c
assoc_test_CFLAGS = $(AUTOTEST_CFLAGS)
assoc_test_LDADD = $(AUTOTEST_LDADD)
onion_test_SOURCES = ../auto_tests/onion_test.c

View File

@ -1,162 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define AUTO_TEST
#include "../toxcore/DHT.h"
#include "../toxcore/assoc.h"
#include "../toxcore/util.h"
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <check.h>
#include "helpers.h"
START_TEST(test_basics)
{
/* TODO: real test */
uint8_t id[crypto_box_PUBLICKEYBYTES];
Assoc *assoc = new_Assoc_default(id);
ck_assert_msg(assoc != NULL, "failed to create default assoc");
kill_Assoc(assoc);
assoc = new_Assoc(17, 4, id); /* results in an assoc of 16/3 */
ck_assert_msg(assoc != NULL, "failed to create customized assoc");
IP_Port ipp;
ipp.ip.family = AF_INET;
ipp.ip.ip4.uint8[0] = 1;
ipp.port = htons(12345);
IPPTs ippts_send;
ippts_send.ip_port = ipp;
ippts_send.timestamp = unix_time();
IP_Port ipp_recv = ipp;
uint8_t res = Assoc_add_entry(assoc, id, &ippts_send, &ipp_recv, 0);
ck_assert_msg(res == 0, "stored self as entry: expected %u, got %u", 0, res);
id[0]++;
res = Assoc_add_entry(assoc, id, &ippts_send, &ipp_recv, 0);
ck_assert_msg(res == 1, "failed to store entry: expected %u, got %u", 1, res);
Assoc_close_entries close_entries;
memset(&close_entries, 0, sizeof(close_entries));
close_entries.count = 4;
close_entries.count_good = 2;
close_entries.wanted_id = id;
Client_data *entries[close_entries.count];
close_entries.result = entries;
uint8_t found = Assoc_get_close_entries(assoc, &close_entries);
ck_assert_msg(found == 1, "get_close_entries(): expected %u, got %u", 1, found);
kill_Assoc(assoc);
}
END_TEST
START_TEST(test_fillup)
{
/* TODO: real test */
int i, j;
uint8_t id[crypto_box_PUBLICKEYBYTES];
//uint32_t a = current_time();
uint32_t a = 2710106197;
srand(a);
for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
id[i] = rand();
}
Assoc *assoc = new_Assoc(6, 15, id);
ck_assert_msg(assoc != NULL, "failed to create default assoc");
struct entry {
uint8_t id[crypto_box_PUBLICKEYBYTES];
IPPTs ippts_send;
IP_Port ipp_recv;
};
unsigned int fail = 0;
struct entry entries[128];
struct entry closest[8];
for (j = 0; j < 128; ++j) {
for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
entries[j].id[i] = rand();
}
IP_Port ipp;
ipp.ip.family = AF_INET;
ipp.ip.ip4.uint32 = rand();
ipp.port = rand();
entries[j].ippts_send.ip_port = ipp;
entries[j].ippts_send.timestamp = unix_time();
ipp.ip.ip4.uint32 = rand();
ipp.port = rand();
entries[j].ipp_recv = ipp;
if (j % 16 == 0) {
memcpy(entries[j].id, id, crypto_box_PUBLICKEYBYTES - 30);
memcpy(&closest[j / 16], &entries[j], sizeof(struct entry));
}
uint8_t res = Assoc_add_entry(assoc, entries[j].id, &entries[j].ippts_send, &entries[j].ipp_recv, 1);
ck_assert_msg(res == 1, "failed to store entry: expected %u, got %u, j = %u", 1, res, j);
}
int good = 0;
Assoc_close_entries close_entries;
memset(&close_entries, 0, sizeof(close_entries));
close_entries.count = 8;
close_entries.count_good = 8;
close_entries.wanted_id = id;
Client_data *entri[close_entries.count];
close_entries.result = entri;
uint8_t found = Assoc_get_close_entries(assoc, &close_entries);
ck_assert_msg(found == 8, "get_close_entries(): expected %u, got %u", 1, found);
for (i = 0; i < 8; ++i) {
for (j = 0; j < 8; ++j) {
if (id_equal(entri[j]->public_key, closest[i].id))
++good;
}
}
ck_assert_msg(good == 8, "Entries found were not the closest ones. Only %u/8 were.", good);
//printf("good: %u %u %u\n", good, a, ((uint32_t)current_time() - a));
kill_Assoc(assoc);
}
END_TEST
Suite *Assoc_suite(void)
{
Suite *s = suite_create("Assoc");
DEFTESTCASE(basics);
DEFTESTCASE(fillup);
return s;
}
int main(int argc, char *argv[])
{
unix_time_update();
Suite *Assoc = Assoc_suite();
SRunner *test_runner = srunner_create(Assoc);
srunner_set_fork_status(test_runner, CK_NOFORK);
srunner_run_all(test_runner, CK_NORMAL);
int number_failed = srunner_ntests_failed(test_runner);
srunner_free(test_runner);
return number_failed;
}

View File

@ -18,41 +18,6 @@
} while(0)
void mark_bad(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time() - 2 * BAD_NODE_TIMEOUT;
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}
void mark_possible_bad(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time();
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}
void mark_good(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time();
ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1;
ipptp->hardening.send_nodes_ok = (HARDENING_ALL_OK >> 1) & 1;
ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1;
}
void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)
{
uint32_t i;
for (i = 0; i < length; ++i) {
if (ipv6)
mark_good(&list[i].assoc6);
else
mark_good(&list[i].assoc4);
}
}
/* Returns 1 if public_key has a furthest distance to comp_client_id
than all public_key's in the list */
@ -67,304 +32,7 @@ uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t l
return 1;
}
int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key)
{
int i;
for (i = 0; i < (int)length; ++i)
if (id_equal(public_key, list[i].public_key))
return i;
return -1;
}
void test_addto_lists_update(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
{
int used, test, test1, test2, found;
IP_Port test_ipp;
uint8_t test_id[crypto_box_PUBLICKEYBYTES];
uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
// check id update for existing ip_port
test = rand() % length;
ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port);
randombytes(test_id, sizeof(test_id));
used = addto_lists(dht, test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test
found = client_in_list(list, length, test_id);
ck_assert_msg(found >= 0, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[found].assoc6.ip_port : &list[found].assoc4.ip_port),
"Client IP_Port is incorrect");
// check ip_port update for existing id
test = rand() % length;
test_ipp.port = rand() % TOX_PORT_DEFAULT;
id_copy(test_id, list[test].public_key);
used = addto_lists(dht, test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is not possible to have id duplicates in the list, so id @ found must be equal id @ test
ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port),
"Client IP_Port is incorrect");
// check ip_port update for existing id and ip_port (... port ... id ...)
test1 = rand() % (length / 2);
test2 = rand() % (length / 2) + length / 2;
ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port);
id_copy(test_id, list[test2].public_key);
if (ipv6) list[test2].assoc6.ip_port.port = -1;
else list[test2].assoc4.ip_port.port = -1;
used = addto_lists(dht, test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test2, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port),
"Client IP_Port is incorrect");
// check ip_port update for existing id and ip_port (... id ... port ...)
test1 = rand() % (length / 2);
test2 = rand() % (length / 2) + length / 2;
ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port);
id_copy(test_id, list[test1].public_key);
if (ipv6) list[test1].assoc6.ip_port.port = -1;
else list[test1].assoc4.ip_port.port = -1;
used = addto_lists(dht, test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test1, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port),
"Client IP_Port is incorrect");
}
void test_addto_lists_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
{
// check "bad" clients replacement
int used, test1, test2, test3;
uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES],
test_id3[crypto_box_PUBLICKEYBYTES];
uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
randombytes(public_key, sizeof(public_key));
mark_all_good(list, length, ipv6);
test1 = rand() % (length / 3);
test2 = rand() % (length / 3) + length / 3;
test3 = rand() % (length / 3) + 2 * length / 3;
ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen");
id_copy((uint8_t *)&test_id1, list[test1].public_key);
id_copy((uint8_t *)&test_id2, list[test2].public_key);
id_copy((uint8_t *)&test_id3, list[test3].public_key);
// mark nodes as "bad"
if (ipv6) {
mark_bad(&list[test1].assoc6);
mark_bad(&list[test2].assoc6);
mark_bad(&list[test3].assoc6);
} else {
mark_bad(&list[test1].assoc4);
mark_bad(&list[test2].assoc4);
mark_bad(&list[test3].assoc4);
}
ip_port->port += 1;
used = addto_lists(dht, *ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");
ck_assert_msg(client_in_list(list, length, test_id2) >= 0, "Wrong bad client removed");
ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed");
}
void test_addto_lists_possible_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
{
// check "possibly bad" clients replacement
int used, test1, test2, test3;
uint8_t public_key[crypto_box_PUBLICKEYBYTES], test_id1[crypto_box_PUBLICKEYBYTES], test_id2[crypto_box_PUBLICKEYBYTES],
test_id3[crypto_box_PUBLICKEYBYTES];
uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
randombytes(public_key, sizeof(public_key));
mark_all_good(list, length, ipv6);
test1 = rand() % (length / 3);
test2 = rand() % (length / 3) + length / 3;
test3 = rand() % (length / 3) + 2 * length / 3;
ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen");
id_copy((uint8_t *)&test_id1, list[test1].public_key);
id_copy((uint8_t *)&test_id2, list[test2].public_key);
id_copy((uint8_t *)&test_id3, list[test3].public_key);
// mark nodes as "possibly bad"
if (ipv6) {
mark_possible_bad(&list[test1].assoc6);
mark_possible_bad(&list[test2].assoc6);
mark_possible_bad(&list[test3].assoc6);
} else {
mark_possible_bad(&list[test1].assoc4);
mark_possible_bad(&list[test2].assoc4);
mark_possible_bad(&list[test3].assoc4);
}
ip_port->port += 1;
used = addto_lists(dht, *ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");
int inlist_id1 = client_in_list(list, length, test_id1) >= 0;
int inlist_id2 = client_in_list(list, length, test_id2) >= 0;
int inlist_id3 = client_in_list(list, length, test_id3) >= 0;
ck_assert_msg(inlist_id1 + inlist_id2 + inlist_id3 == 2, "Wrong client removed");
if (!inlist_id1) {
ck_assert_msg(id_closest(comp_client_id, test_id2, test_id1) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id3, test_id1) == 1,
"Id has been removed but is closer to than another one");
} else if (!inlist_id2) {
ck_assert_msg(id_closest(comp_client_id, test_id1, test_id2) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id3, test_id2) == 1,
"Id has been removed but is closer to than another one");
} else if (!inlist_id3) {
ck_assert_msg(id_closest(comp_client_id, test_id1, test_id3) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id2, test_id3) == 1,
"Id has been removed but is closer to than another one");
}
}
void test_addto_lists_good(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
{
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
mark_all_good(list, length, ipv6);
// check "good" client id replacement
do {
randombytes(public_key, sizeof(public_key));
} while (is_furthest(comp_client_id, list, length, public_key));
ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Good client id is not in the list");
// check "good" client id skip
do {
randombytes(public_key, sizeof(public_key));
} while (!is_furthest(comp_client_id, list, length, public_key));
ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
}
void test_addto_lists(IP ip)
{
Networking_Core *net = new_networking(ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != 0, "Failed to create Networking_Core");
DHT *dht = new_DHT(net);
ck_assert_msg(dht != 0, "Failed to create DHT");
IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT };
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
int i, used;
// check lists filling
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
randombytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port");
}
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
used = addto_lists(dht, ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing public_key");
}
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
randombytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");
}
/*check: Current behavior if there are two clients with the same id is
* to replace the first ip by the second. */
test_addto_lists_update(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port);
for (i = 0; i < dht->num_friends; ++i)
test_addto_lists_update(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port);
// check "bad" entries
test_addto_lists_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port);
for (i = 0; i < dht->num_friends; ++i)
test_addto_lists_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port);
// check "possibly bad" entries
/*
test_addto_lists_possible_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key);
for (i = 0; i < dht->num_friends; ++i)
test_addto_lists_possible_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port,
dht->friends_list[i].public_key);
*/
// check "good" entries
test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key);
for (i = 0; i < dht->num_friends; ++i)
test_addto_lists_good(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port,
dht->friends_list[i].public_key);
kill_DHT(dht);
kill_networking(net);
}
START_TEST(test_addto_lists_ipv4)
{
IP ip;
ip_init(&ip, 0);
test_addto_lists(ip);
}
END_TEST
START_TEST(test_addto_lists_ipv6)
{
IP ip;
ip_init(&ip, 1);
test_addto_lists(ip);
}
END_TEST
#define DHT_DEFAULT_PORT (TOX_PORT_DEFAULT + 20)
@ -413,45 +81,46 @@ void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigne
void test_list_main()
{
DHT *dhts[NUM_DHT];
/*
DHT *dhts[NUM_DHT];
uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][crypto_box_PUBLICKEYBYTES + 1];
memset(cmp_list1, 0, sizeof(cmp_list1));
uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][crypto_box_PUBLICKEYBYTES + 1];
memset(cmp_list1, 0, sizeof(cmp_list1));
IP ip;
ip_init(&ip, 1);
IP ip;
ip_init(&ip, 1);
unsigned int i, j, k, l;
unsigned int i, j, k, l;
for (i = 0; i < NUM_DHT; ++i) {
IP ip;
ip_init(&ip, 1);
for (i = 0; i < NUM_DHT; ++i) {
IP ip;
ip_init(&ip, 1);
dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i));
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i));
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
for (j = 0; j < NUM_DHT; ++j) {
for (i = 1; i < NUM_DHT; ++i) {
test_add_to_list(cmp_list1[j], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[j]->self_public_key);
}
}
for (j = 0; j < NUM_DHT; ++j) {
for (i = 1; i < NUM_DHT; ++i) {
test_add_to_list(cmp_list1[j], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[j]->self_public_key);
}
}
for (j = 0; j < NUM_DHT; ++j) {
for (i = 0; i < NUM_DHT; ++i) {
if (i == j)
continue;
IP_Port ip_port;
ip_init(&ip_port.ip, 0);
ip_port.ip.ip4.uint32 = rand();
ip_port.port = rand() % (UINT16_MAX - 1);
++ip_port.port;
addto_lists(dhts[j], ip_port, dhts[i]->self_public_key);
}
}
for (j = 0; j < NUM_DHT; ++j) {
for (i = 0; i < NUM_DHT; ++i) {
if (i == j)
continue;
IP_Port ip_port;
ip_init(&ip_port.ip, 0);
ip_port.ip.ip4.uint32 = rand();
ip_port.port = rand() % (UINT16_MAX - 1);
++ip_port.port;
addto_lists(dhts[j], ip_port, dhts[i]->self_public_key);
}
}
*/
/*
print_pk(dhts[0]->self_public_key);
@ -460,72 +129,73 @@ void test_list_main()
print_pk(cmp_list1[i]);
}
*/
unsigned int m_count = 0;
*//*
unsigned int m_count = 0;
for (l = 0; l < NUM_DHT; ++l) {
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
for (j = 1; j < NUM_DHT; ++j) {
if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, crypto_box_PUBLICKEYBYTES) != 0)
continue;
for (l = 0; l < NUM_DHT; ++l) {
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
for (j = 1; j < NUM_DHT; ++j) {
if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, crypto_box_PUBLICKEYBYTES) != 0)
continue;
unsigned int count = 0;
unsigned int count = 0;
for (k = 0; k < LCLIENT_LIST; ++k) {
if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key,
crypto_box_PUBLICKEYBYTES) == 0)
++count;
}
if (count != 1) {
print_pk(dhts[l]->self_public_key);
for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) {
printf("----Entry %u----\n", k);
print_pk(cmp_list1[l][k]);
}
for (k = 0; k < LCLIENT_LIST; ++k) {
printf("----Closel %u----\n", k);
print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key);
}
print_pk(dhts[(l + j) % NUM_DHT]->self_public_key);
}
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
Node_format ln[MAX_SENT_NODES];
int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0);
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);
count = 0;
for (k = 0; k < MAX_SENT_NODES; ++k) {
if (memcmp(dhts[l]->self_public_key, ln[k].public_key, crypto_box_PUBLICKEYBYTES) == 0)
++count;
}
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
/*
for (k = 0; k < MAX_SENT_NODES; ++k) {
printf("----gn %u----\n", k);
print_pk(ln[k].public_key);
}*/
++m_count;
}
for (k = 0; k < LCLIENT_LIST; ++k) {
if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key,
crypto_box_PUBLICKEYBYTES) == 0)
++count;
}
}
ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count,
(NUM_DHT) * (MAX_FRIEND_CLIENTS));
if (count != 1) {
print_pk(dhts[l]->self_public_key);
for (i = 0; i < NUM_DHT; ++i) {
void *n = dhts[i]->net;
kill_DHT(dhts[i]);
kill_networking(n);
}
for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) {
printf("----Entry %u----\n", k);
print_pk(cmp_list1[l][k]);
}
for (k = 0; k < LCLIENT_LIST; ++k) {
printf("----Closel %u----\n", k);
print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key);
}
print_pk(dhts[(l + j) % NUM_DHT]->self_public_key);
}
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
Node_format ln[MAX_SENT_NODES];
int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0);
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);
count = 0;
for (k = 0; k < MAX_SENT_NODES; ++k) {
if (memcmp(dhts[l]->self_public_key, ln[k].public_key, crypto_box_PUBLICKEYBYTES) == 0)
++count;
}
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);*/
/*
for (k = 0; k < MAX_SENT_NODES; ++k) {
printf("----gn %u----\n", k);
print_pk(ln[k].public_key);
}*//*
++m_count;
}
}
}
ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count,
(NUM_DHT) * (MAX_FRIEND_CLIENTS));
for (i = 0; i < NUM_DHT; ++i) {
void *n = dhts[i]->net;
kill_DHT(dhts[i]);
kill_networking(n);
}
*/
}
@ -652,7 +322,7 @@ int main(int argc, char *argv[])
SRunner *test_runner = srunner_create(dht);
int number_failed = 0;
//srunner_set_fork_status(test_runner, CK_NOFORK);
srunner_set_fork_status(test_runner, CK_NOFORK);
srunner_run_all(test_runner, CK_NORMAL);
number_failed = srunner_ntests_failed(test_runner);

View File

@ -62,7 +62,7 @@ void print_client_id(uint8_t *public_key)
printf("%02hhX", public_key[j]);
}
}
/*
void print_hardening(Hardening *h)
{
printf("Hardening:\n");
@ -165,7 +165,7 @@ void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port)
}
printf("\n--------------------END-----------------------------\n\n\n");
}
}*/
int main(int argc, char *argv[])
{
@ -245,8 +245,8 @@ int main(int argc, char *argv[])
*/
networking_poll(dht->net);
print_clientlist(dht);
print_friendlist(dht);
//print_clientlist(dht);
//print_friendlist(dht);
c_sleep(300);
}

File diff suppressed because it is too large Load Diff

View File

@ -31,11 +31,7 @@
/* Maximum number of clients stored per friend. */
#define MAX_FRIEND_CLIENTS 8
#define LCLIENT_NODES (MAX_FRIEND_CLIENTS)
#define LCLIENT_LENGTH 128
/* A list of the clients mathematically closest to ours. */
#define LCLIENT_LIST (LCLIENT_LENGTH * LCLIENT_NODES)
#define DHT_BUCKET_NODES (MAX_FRIEND_CLIENTS)
#define MAX_CLOSE_TO_BOOTSTRAP_NODES 8
@ -72,58 +68,31 @@ void to_net_family(IP *ip);
int to_host_family(IP *ip);
typedef struct {
IP_Port ip_port;
uint64_t timestamp;
} IPPTs;
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
typedef struct {
/* Node routes request correctly (true (1) or false/didn't check (0)) */
uint8_t routes_requests_ok;
/* Time which we last checked this.*/
uint64_t routes_requests_timestamp;
uint8_t routes_requests_pingedid[crypto_box_PUBLICKEYBYTES];
/* Node sends correct send_node (true (1) or false/didn't check (0)) */
uint8_t send_nodes_ok;
/* Time which we last checked this.*/
uint64_t send_nodes_timestamp;
uint8_t send_nodes_pingedid[crypto_box_PUBLICKEYBYTES];
/* Node can be used to test other nodes (true (1) or false/didn't check (0)) */
uint8_t testing_requests;
/* Time which we last checked this.*/
uint64_t testing_timestamp;
uint8_t testing_pingedid[crypto_box_PUBLICKEYBYTES];
} Hardening;
typedef struct {
IP_Port ip_port;
uint64_t timestamp;
uint64_t last_pinged;
Hardening hardening;
/* Returned by this node. Either our friend or us. */
IP_Port ret_ip_port;
uint64_t ret_timestamp;
} IPPTsPng;
struct {
uint8_t pk[crypto_box_PUBLICKEYBYTES];
IP_Port ip_port;
uint64_t timestamp;
} ret[DHT_BUCKET_NODES];
typedef struct {
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
IPPTsPng assoc4;
IPPTsPng assoc6;
} Client_data;
/*----------------------------------------------------------------------------------*/
typedef struct {
/* 1 if currently hole punching, otherwise 0 */
uint8_t hole_punching;
_Bool hole_punching;
uint32_t punching_index;
uint32_t tries;
uint32_t punching_index2;
uint64_t punching_timestamp;
uint64_t recvNATping_timestamp;
uint64_t NATping_id;
uint64_t NATping_timestamp;
} NAT;
#define DHT_FRIEND_MAX_LOCKS 32
@ -134,9 +103,19 @@ typedef struct {
}
Node_format;
typedef struct DHT_Bucket {
unsigned int deepness;
_Bool empty;
_Bool public_key;
uint8_t searched_public_key[crypto_box_PUBLICKEYBYTES];
Client_data client_list[DHT_BUCKET_NODES];
struct DHT_Bucket *buckets[2];
} DHT_Bucket;
typedef struct {
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
Client_data client_list[MAX_FRIEND_CLIENTS];
/* Time at which the last get_nodes request was sent. */
uint64_t lastgetnode;
@ -144,7 +123,12 @@ typedef struct {
uint32_t bootstrap_times;
/* Symetric NAT hole punching stuff. */
NAT nat;
NAT nat_v4;
NAT nat_v6;
uint64_t recvNATping_timestamp;
uint64_t NATping_id;
uint64_t NATping_timestamp;
uint16_t lock_count;
struct {
@ -206,8 +190,10 @@ typedef struct {
typedef struct {
Networking_Core *net;
DHT_Bucket bucket_v4;
DHT_Bucket bucket_v6;
DHT_Bucket bucket_lan;
Client_data close_clientlist[LCLIENT_LIST];
uint64_t close_lastgetnodes;
uint32_t close_bootstrap_times;
@ -307,6 +293,10 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
*/
int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2);
/* Return index of first unequal bit number.
*/
int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2);
/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list.
*/
_Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port,
@ -334,7 +324,7 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node
*
* return the number of nodes.
*/
uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num);
uint16_t randfriends_nodes(const DHT *dht, Node_format *nodes, uint16_t max_num);
/* Put up to max_num nodes in nodes from the closelist.
*
@ -381,11 +371,12 @@ int DHT_connect_after_load(DHT *dht);
*/
int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length);
/* Send the following packet to everyone who tells us they are connected to friend_id.
/* Send the following packet to X nodes who tells us they are connected to friend_pk.
*
* return number of nodes it sent the packet to.
* return number of nodes the packet was sent to.
*/
int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length);
int route_tofriend(const DHT *dht, const uint8_t *friend_pk, const uint8_t *packet, uint16_t length,
unsigned int num_to_send);
/* Function to handle crypto packets.
*/

View File

@ -32,8 +32,6 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/util.c \
../toxcore/group.h \
../toxcore/group.c \
../toxcore/assoc.h \
../toxcore/assoc.c \
../toxcore/onion.h \
../toxcore/onion.c \
../toxcore/logger.h \

View File

@ -31,7 +31,6 @@
#include "logger.h"
#include "Messenger.h"
#include "assoc.h"
#include "network.h"
#include "util.h"
@ -2325,10 +2324,6 @@ void do_messenger(Messenger *m)
if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
#ifdef ENABLE_ASSOC_DHT
Assoc_status(m->dht->assoc);
#endif
lastdump = unix_time();
uint32_t client, last_pinged;

File diff suppressed because it is too large Load Diff

View File

@ -1,104 +0,0 @@
#ifndef __ASSOC_H__
#define __ASSOC_H__
/* used by rendezvous */
#define ASSOC_AVAILABLE
/* For the legalese parts, see tox.h. */
/* enumerated lists are superior to magic numbers */
enum NODE_STATUS { BAD, SEENB_HEARDG, SEENG, USED };
/*
* Module to store currently unused ID <=> IP associations
* for a potential future use
*/
typedef struct Assoc Assoc;
/*****************************************************************************/
/* custom distance handler, if it's not ID-distance based
* return values exactly like id_closest() */
typedef int (*Assoc_distance_relative_callback)(const Assoc *assoc, void *callback_data, const uint8_t *client_id,
const uint8_t *client_id1, const uint8_t *client_id2);
#define DISTANCE_INDEX_DISTANCE_BITS 44
/* absolute distance: can be same for different client_id_check values
* return value should have DISTANCE_INDEX_DISTANCE_BITS valid bits */
typedef uint64_t (*Assoc_distance_absolute_callback)(const Assoc *assoc, void *callback_data,
const uint8_t *client_id_ref, const uint8_t *client_id_check);
/*****************************************************************************/
/* Central entry point for new associations: add a new candidate to the cache
* returns 1 if entry is stored, 2 if existing entry was updated, 0 else */
uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send, const IP_Port *ipp_recv,
uint8_t used);
/*****************************************************************************/
typedef enum AssocCloseEntriesFlags {
ProtoIPv4 = 1,
ProtoIPv6 = 2,
LANOk = 4,
} AssocCloseEntriesFlags;
typedef struct Assoc_close_entries {
void *custom_data; /* given to distance functions */
uint8_t *wanted_id; /* the target client_id */
uint8_t flags; /* additional flags */
Assoc_distance_relative_callback distance_relative_func;
Assoc_distance_absolute_callback distance_absolute_func;
uint8_t count_good; /* that many should be "good" w.r.t. timeout */
uint8_t count; /* allocated number of close_indices */
Client_data **result;
} Assoc_close_entries;
/* find up to close_count nodes to put into close_nodes_used of ID_Nodes
* the distance functions can be NULL, then standard distance functions will be used
* the caller is responsible for allocating close_indices of sufficient size
*
* returns 0 on error
* returns the number of found nodes and the list of indices usable by Assoc_client()
* the caller is assumed to be registered from Assoc_register_callback()
* if they aren't, they should copy the Client_data and call Assoc_client_drop()
*/
uint8_t Assoc_get_close_entries(Assoc *assoc, Assoc_close_entries *close_entries);
/*****************************************************************************/
/* create: default sizes (6, 5 => 320 entries) */
Assoc *new_Assoc_default(const uint8_t *public_id);
/* create: customized sizes
* total is (2^bits) * entries
* bits should be between 2 and 15 (else it's trimmed)
* entries will be reduced to the closest prime smaller or equal
*
* preferably bits should be large and entries small to ensure spread
* in the search space (e. g. 5, 5 is preferable to 2, 41) */
Assoc *new_Assoc(size_t bits, size_t entries, const uint8_t *public_id);
/* public_id changed (loaded), update which entry isn't stored */
void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *public_id);
/* every 45s send out a getnodes() for a "random" bucket */
#define ASSOC_BUCKET_REFRESH 45
/* refresh bucket's data from time to time
* this must be called only from DHT */
void do_Assoc(Assoc *assoc, DHT *dht);
/* destroy */
void kill_Assoc(Assoc *assoc);
#ifdef TOX_LOGGER
void Assoc_status(const Assoc *assoc);
#endif /* TOX_LOGGER */
#endif /* !__ASSOC_H__ */

View File

@ -125,7 +125,6 @@ void new_nonce(uint8_t *nonce);
#define MAX_CRYPTO_REQUEST_SIZE 1024
#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */
#define CRYPTO_PACKET_DHTPK 156
#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */

View File

@ -891,7 +891,8 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin
if (len == -1)
return -1;
return route_tofriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, packet, len);
return route_tofriend(onion_c->dht, onion_c->friends_list[friend_num].dht_public_key, packet, len,
DHT_BUCKET_NODES / 2);
}
static int handle_dht_dhtpk(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet,

View File

@ -220,33 +220,6 @@ static int handle_ping_response(void *_dht, IP_Port source, const uint8_t *packe
return 0;
}
/* Check if public_key with ip_port is in the list.
*
* return 1 if it is.
* return 0 if it isn't.
*/
static int in_list(const Client_data *list, uint16_t length, const uint8_t *public_key, IP_Port ip_port)
{
unsigned int i;
for (i = 0; i < length; ++i) {
if (id_equal(list[i].public_key, public_key)) {
const IPPTsPng *ipptp;
if (ip_port.ip.family == AF_INET) {
ipptp = &list[i].assoc4;
} else {
ipptp = &list[i].assoc6;
}
if (!is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT) && ipport_equal(&ipptp->ip_port, &ip_port))
return 1;
}
}
return 0;
}
/* Add nodes to the to_ping list.
* All nodes in this list are pinged every TIME_TO_PING seconds
* and are then removed from the list.
@ -265,9 +238,6 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
if (!node_addable_to_close_list(ping->dht, public_key, ip_port))
return -1;
if (in_list(ping->dht->close_clientlist, LCLIENT_LIST, public_key, ip_port))
return -1;
IP_Port temp;
if (DHT_getfriendip(ping->dht, public_key, &temp) == 0) {