Skip to content

Commit

Permalink
Make DHT a module-private type.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 16, 2018
1 parent 7110f13 commit 7d6650a
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 149 deletions.
3 changes: 1 addition & 2 deletions auto_tests/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ static void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *path, uint3
return;
}

if (strncmp(message, "Bound successfully to ", strlen("Bound successfully to ")) ||
strncmp(message, "Found node in LAN: ", strlen("Found node in LAN: "))) {
if (strncmp(message, "Bound successfully to ", strlen("Bound successfully to "))) {
return;
}

Expand Down
56 changes: 33 additions & 23 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ static int handle_test_3(void *object, IP_Port source, const uint8_t *packet, ui
#if 0
print_client_id(packet, length);
#endif
int len = decrypt_data(test_3_pub_key, onion->dht->self_secret_key, packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
int len = decrypt_data(test_3_pub_key, dht_get_self_secret_key(onion->dht),
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE,
1 + CRYPTO_SHA256_SIZE + CRYPTO_MAC_SIZE, plain);

Expand Down Expand Up @@ -140,7 +141,7 @@ static int handle_test_4(void *object, IP_Port source, const uint8_t *packet, ui
return 1;
}

int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, onion->dht->self_secret_key, packet + 1,
int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, dht_get_self_secret_key(onion->dht), packet + 1,
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, sizeof("Install gentoo") + CRYPTO_MAC_SIZE, plain);

if (len == -1) {
Expand All @@ -165,12 +166,12 @@ START_TEST(test_basic)

IP_Port on1 = {ip, net_port(onion1->net)};
Node_format n1;
memcpy(n1.public_key, onion1->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(n1.public_key, dht_get_self_public_key(onion1->dht), CRYPTO_PUBLIC_KEY_SIZE);
n1.ip_port = on1;

IP_Port on2 = {ip, net_port(onion2->net)};
Node_format n2;
memcpy(n2.public_key, onion2->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(n2.public_key, dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
n2.ip_port = on2;

Node_format nodes[4];
Expand Down Expand Up @@ -208,9 +209,12 @@ START_TEST(test_basic)
uint64_t s;
memcpy(&s, sb_data, sizeof(uint64_t));
memcpy(test_3_pub_key, nodes[3].public_key, CRYPTO_PUBLIC_KEY_SIZE);
ret = send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key,
onion1->dht->self_secret_key,
zeroes, onion1->dht->self_public_key, onion1->dht->self_public_key, s);
ret = send_announce_request(onion1->net, &path, nodes[3],
dht_get_self_public_key(onion1->dht),
dht_get_self_secret_key(onion1->dht),
zeroes,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht), s);
ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet.");
handled_test_3 = 0;

Expand All @@ -222,13 +226,18 @@ START_TEST(test_basic)

random_bytes(sb_data, sizeof(sb_data));
memcpy(&s, sb_data, sizeof(uint64_t));
memcpy(onion_announce_entry_public_key(onion2_a, 1), onion2->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
onion_announce_entry_set_time(onion2_a, 1, unix_time());
networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
send_announce_request(onion1->net, &path, nodes[3], onion1->dht->self_public_key, onion1->dht->self_secret_key,
test_3_ping_id, onion1->dht->self_public_key, onion1->dht->self_public_key, s);

while (memcmp(onion_announce_entry_public_key(onion2_a, ONION_ANNOUNCE_MAX_ENTRIES - 2), onion1->dht->self_public_key,
send_announce_request(onion1->net, &path, nodes[3],
dht_get_self_public_key(onion1->dht),
dht_get_self_secret_key(onion1->dht),
test_3_ping_id,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht), s);

while (memcmp(onion_announce_entry_public_key(onion2_a, ONION_ANNOUNCE_MAX_ENTRIES - 2),
dht_get_self_public_key(onion1->dht),
CRYPTO_PUBLIC_KEY_SIZE) != 0) {
do_onion(onion1);
do_onion(onion2);
Expand All @@ -240,8 +249,9 @@ START_TEST(test_basic)
ck_assert_msg((onion3 != NULL), "Onion failed initializing.");

random_nonce(nonce);
ret = send_data_request(onion3->net, &path, nodes[3].ip_port, onion1->dht->self_public_key,
onion1->dht->self_public_key,
ret = send_data_request(onion3->net, &path, nodes[3].ip_port,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht),
nonce, (const uint8_t *)"Install gentoo", sizeof("Install gentoo"));
ck_assert_msg(ret == 0, "Failed to create/send onion data_request packet.");
handled_test_4 = 0;
Expand All @@ -258,7 +268,7 @@ START_TEST(test_basic)
{
Onion *onion = onion1;

Networking_Core *net = onion->dht->net;
Networking_Core *net = dht_get_net(onion->dht);
DHT *dht = onion->dht;
kill_onion(onion);
kill_DHT(dht);
Expand All @@ -268,7 +278,7 @@ START_TEST(test_basic)
{
Onion *onion = onion2;

Networking_Core *net = onion->dht->net;
Networking_Core *net = dht_get_net(onion->dht);
DHT *dht = onion->dht;
kill_onion(onion);
kill_DHT(dht);
Expand All @@ -278,7 +288,7 @@ START_TEST(test_basic)
{
Onion *onion = onion3;

Networking_Core *net = onion->dht->net;
Networking_Core *net = dht_get_net(onion->dht);
DHT *dht = onion->dht;
kill_onion(onion);
kill_DHT(dht);
Expand Down Expand Up @@ -361,7 +371,7 @@ static void do_onions(Onions *on)

static void kill_onions(Onions *on)
{
Networking_Core *net = on->onion->dht->net;
Networking_Core *net = dht_get_net(on->onion->dht);
DHT *dht = on->onion->dht;
Net_Crypto *c = onion_get_net_crypto(on->onion_c);
kill_onion_client(on->onion_c);
Expand Down Expand Up @@ -444,11 +454,11 @@ START_TEST(test_announce)

for (i = 3; i < NUM_ONIONS; ++i) {
IP_Port ip_port = {ip, net_port(onions[i - 1]->onion->net)};
DHT_bootstrap(onions[i]->onion->dht, ip_port, onions[i - 1]->onion->dht->self_public_key);
DHT_bootstrap(onions[i]->onion->dht, ip_port, dht_get_self_public_key(onions[i - 1]->onion->dht));
IP_Port ip_port1 = {ip, net_port(onions[i - 2]->onion->net)};
DHT_bootstrap(onions[i]->onion->dht, ip_port1, onions[i - 2]->onion->dht->self_public_key);
DHT_bootstrap(onions[i]->onion->dht, ip_port1, dht_get_self_public_key(onions[i - 2]->onion->dht));
IP_Port ip_port2 = {ip, net_port(onions[i - 3]->onion->net)};
DHT_bootstrap(onions[i]->onion->dht, ip_port2, onions[i - 3]->onion->dht->self_public_key);
DHT_bootstrap(onions[i]->onion->dht, ip_port2, dht_get_self_public_key(onions[i - 3]->onion->dht));
}

uint32_t connected = 0;
Expand All @@ -474,8 +484,8 @@ START_TEST(test_announce)
c_sleep(50);
}

memcpy(first_dht_pk, onions[NUM_FIRST]->onion->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(last_dht_pk, onions[NUM_LAST]->onion->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(first_dht_pk, dht_get_self_public_key(onions[NUM_FIRST]->onion->dht), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(last_dht_pk, dht_get_self_public_key(onions[NUM_LAST]->onion->dht), CRYPTO_PUBLIC_KEY_SIZE);

printf("adding friend\n");
int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c,
Expand Down
21 changes: 11 additions & 10 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ static void manage_keys(DHT *dht)
exit(1);
}

memcpy(dht->self_public_key, keys, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(dht->self_secret_key, keys + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_SECRET_KEY_SIZE);
dht_set_self_public_key(dht, keys);
dht_set_self_public_key(dht, keys + CRYPTO_PUBLIC_KEY_SIZE);
printf("Keys loaded successfully.\n");
} else {
memcpy(keys, dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht->self_secret_key, CRYPTO_SECRET_KEY_SIZE);
memcpy(keys, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht_get_self_secret_key(dht), CRYPTO_SECRET_KEY_SIZE);
keys_file = fopen("key", "w");

if (keys_file == NULL) {
Expand Down Expand Up @@ -120,7 +120,7 @@ int main(int argc, char *argv[])
Onion_Announce *onion_a = new_onion_announce(dht);

#ifdef DHT_NODE_EXTRA_PACKETS
bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
#endif

if (!(onion && onion_a)) {
Expand All @@ -137,7 +137,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
#define NUM_PORTS 3
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_secret_key, onion);
TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion);

if (tcp_s == NULL) {
printf("TCP server failed to initialize.\n");
Expand All @@ -155,14 +155,15 @@ int main(int argc, char *argv[])
}

for (i = 0; i < 32; i++) {
printf("%02hhX", dht->self_public_key[i]);
fprintf(file, "%02hhX", dht->self_public_key[i]);
const uint8_t *const self_public_key = dht_get_self_public_key(dht);
printf("%02hhX", self_public_key[i]);
fprintf(file, "%02hhX", self_public_key[i]);
}

fclose(file);

printf("\n");
printf("Port: %u\n", net_ntohs(net_port(dht->net)));
printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht))));

if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n");
Expand Down Expand Up @@ -199,7 +200,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
do_TCP_server(tcp_s);
#endif
networking_poll(dht->net, NULL);
networking_poll(dht_get_net(dht), NULL);

c_sleep(1);
}
Expand Down
16 changes: 8 additions & 8 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ static int manage_keys(DHT *dht, char *keys_file_path)
return 0;
}

memcpy(dht->self_public_key, keys, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(dht->self_secret_key, keys + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_SECRET_KEY_SIZE);
dht_set_self_public_key(dht, keys);
dht_set_self_secret_key(dht, keys + CRYPTO_PUBLIC_KEY_SIZE);
} else {
// Otherwise save new keys
memcpy(keys, dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht->self_secret_key, CRYPTO_SECRET_KEY_SIZE);
memcpy(keys, dht_get_self_public_key(dht), CRYPTO_PUBLIC_KEY_SIZE);
memcpy(keys + CRYPTO_PUBLIC_KEY_SIZE, dht_get_self_secret_key(dht), CRYPTO_SECRET_KEY_SIZE);

keys_file = fopen(keys_file_path, "w");

Expand Down Expand Up @@ -261,7 +261,7 @@ int main(int argc, char *argv[])
}

if (enable_motd) {
if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd);
Expand All @@ -288,7 +288,7 @@ int main(int argc, char *argv[])
return 1;
}

tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_secret_key, onion);
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht_get_self_secret_key(dht), onion);

// tcp_relay_port_count != 0 at this point
free(tcp_relay_ports);
Expand All @@ -308,7 +308,7 @@ int main(int argc, char *argv[])
return 1;
}

print_public_key(dht->self_public_key);
print_public_key(dht_get_self_public_key(dht));

uint64_t last_LANdiscovery = 0;
const uint16_t net_htons_port = net_htons(port);
Expand All @@ -332,7 +332,7 @@ int main(int argc, char *argv[])
do_TCP_server(tcp_server);
}

networking_poll(dht->net, NULL);
networking_poll(dht_get_net(dht), NULL);

if (waiting_for_dht_connection && DHT_isconnected(dht)) {
log_write(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n");
Expand Down
26 changes: 14 additions & 12 deletions testing/DHT_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

static uint8_t zeroes_cid[CRYPTO_PUBLIC_KEY_SIZE];

static void print_client_id(uint8_t *public_key)
static void print_client_id(const uint8_t *public_key)
{
uint32_t j;

Expand All @@ -58,7 +58,7 @@ static void print_client_id(uint8_t *public_key)
}
}

static void print_hardening(Hardening *h)
static void print_hardening(const Hardening *h)
{
printf("Hardening:\n");
printf("routes_requests_ok: %hhu\n", h->routes_requests_ok);
Expand All @@ -76,9 +76,9 @@ static void print_hardening(Hardening *h)
printf("\n\n");
}

static void print_assoc(IPPTsPng *assoc, uint8_t ours)
static void print_assoc(const IPPTsPng *assoc, uint8_t ours)
{
IP_Port *ipp = &assoc->ip_port;
const IP_Port *ipp = &assoc->ip_port;
char ip_str[IP_NTOA_LEN];
printf("\nIP: %s Port: %u", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), net_ntohs(ipp->port));
printf("\nTimestamp: %llu", (long long unsigned int) assoc->timestamp);
Expand All @@ -102,7 +102,7 @@ static void print_clientlist(DHT *dht)
printf("___________________CLOSE________________________________\n");

for (i = 0; i < LCLIENT_LIST; i++) {
Client_data *client = &dht->close_clientlist[i];
const Client_data *client = dht_get_close_client(dht, i);

if (public_key_cmp(client->public_key, zeroes_cid) == 0) {
continue;
Expand All @@ -122,20 +122,20 @@ static void print_friendlist(DHT *dht)
IP_Port p_ip;
printf("_________________FRIENDS__________________________________\n");

for (k = 0; k < dht->num_friends; k++) {
for (k = 0; k < dht_get_num_friends(dht); k++) {
printf("FRIEND %u\n", k);
printf("ID: ");

print_client_id(dht->friends_list[k].public_key);
print_client_id(dht_get_friend_public_key(dht, k));

int friendok = DHT_getfriendip(dht, dht->friends_list[k].public_key, &p_ip);
int friendok = DHT_getfriendip(dht, dht_get_friend_public_key(dht, k), &p_ip);
char ip_str[IP_NTOA_LEN];
printf("\nIP: %s:%u (%d)", ip_ntoa(&p_ip.ip, ip_str, sizeof(ip_str)), net_ntohs(p_ip.port), friendok);

printf("\nCLIENTS IN LIST:\n\n");

for (i = 0; i < MAX_FRIEND_CLIENTS; i++) {
Client_data *client = &dht->friends_list[k].client_list[i];
const Client_data *client = &dht_get_friend(dht, k)->client_list[i];

if (public_key_cmp(client->public_key, zeroes_cid) == 0) {
continue;
Expand Down Expand Up @@ -195,11 +195,13 @@ int main(int argc, char *argv[])
uint32_t i;

for (i = 0; i < 32; i++) {
if (dht->self_public_key[i] < 16) {
const uint8_t *const self_public_key = dht_get_self_public_key(dht);

if (self_public_key[i] < 16) {
printf("0");
}

printf("%hhX", dht->self_public_key[i]);
printf("%hhX", self_public_key[i]);
}

char temp_id[128];
Expand Down Expand Up @@ -250,7 +252,7 @@ int main(int argc, char *argv[])
}

#endif
networking_poll(dht->net, NULL);
networking_poll(dht_get_net(dht), NULL);

print_clientlist(dht);
print_friendlist(dht);
Expand Down
Loading

0 comments on commit 7d6650a

Please sign in to comment.