2014-01-01 12:18:37 +08:00
|
|
|
#include <stdlib.h>
|
2016-09-01 07:33:20 +08:00
|
|
|
#include <string.h>
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2014-01-01 12:18:37 +08:00
|
|
|
#include "../toxcore/onion.h"
|
2014-01-05 08:40:43 +08:00
|
|
|
#include "../toxcore/onion_announce.h"
|
2014-01-30 09:59:50 +08:00
|
|
|
#include "../toxcore/onion_client.h"
|
2014-01-07 06:59:44 +08:00
|
|
|
#include "../toxcore/util.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "check_compat.h"
|
2014-08-08 04:59:47 +08:00
|
|
|
|
2017-11-15 16:13:31 +08:00
|
|
|
#ifndef USE_IPV6
|
|
|
|
#define USE_IPV6 1
|
|
|
|
#endif
|
|
|
|
|
2018-09-08 15:29:03 +08:00
|
|
|
static inline IP get_loopback(void)
|
2017-11-15 16:13:31 +08:00
|
|
|
{
|
|
|
|
IP ip;
|
|
|
|
#if USE_IPV6
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv6;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v6 = get_ip6_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#else
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv4;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v4 = get_ip4_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#endif
|
|
|
|
return ip;
|
|
|
|
}
|
2016-09-05 23:10:48 +08:00
|
|
|
static void do_onion(Onion *onion)
|
2014-01-01 12:18:37 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(onion->mono_time);
|
2018-08-10 07:53:39 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
networking_poll(onion->net, nullptr);
|
2018-07-05 18:31:29 +08:00
|
|
|
do_dht(onion->dht);
|
2014-01-01 12:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handled_test_1;
|
2022-02-03 04:02:23 +08:00
|
|
|
static int handle_test_1(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
|
2014-01-01 12:18:37 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
Onion *onion = (Onion *)object;
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
const char req_message[] = "Install Gentoo";
|
|
|
|
uint8_t req_packet[1 + sizeof(req_message)];
|
|
|
|
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
|
|
|
|
memcpy(req_packet + 1, req_message, sizeof(req_message));
|
|
|
|
|
|
|
|
if (memcmp(packet, req_packet, sizeof(req_packet)) != 0) {
|
2014-01-01 12:18:37 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
const char res_message[] = "install gentoo";
|
|
|
|
uint8_t res_packet[1 + sizeof(res_message)];
|
|
|
|
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
|
|
|
|
memcpy(res_packet + 1, res_message, sizeof(res_message));
|
|
|
|
|
|
|
|
if (send_onion_response(onion->net, source, res_packet, sizeof(res_packet),
|
|
|
|
packet + sizeof(res_packet)) == -1) {
|
2014-01-01 12:18:37 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
|
|
|
|
handled_test_1 = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handled_test_2;
|
2022-02-03 04:02:23 +08:00
|
|
|
static int handle_test_2(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
|
2014-01-01 12:18:37 +08:00
|
|
|
{
|
2020-05-04 09:12:57 +08:00
|
|
|
const char res_message[] = "install gentoo";
|
|
|
|
uint8_t res_packet[1 + sizeof(res_message)];
|
|
|
|
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
|
|
|
|
memcpy(res_packet + 1, res_message, sizeof(res_message));
|
|
|
|
|
|
|
|
if (length != sizeof(res_packet)) {
|
2014-01-01 12:18:37 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
if (memcmp(packet, res_packet, sizeof(res_packet)) != 0) {
|
2014-01-01 12:18:37 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
|
|
|
|
handled_test_2 = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-05 23:10:48 +08:00
|
|
|
#if 0
|
2014-01-06 08:22:38 +08:00
|
|
|
void print_client_id(uint8_t *client_id, uint32_t length)
|
|
|
|
{
|
|
|
|
uint32_t j;
|
|
|
|
|
|
|
|
for (j = 0; j < length; j++) {
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("%02X", client_id[j]);
|
2014-01-06 08:22:38 +08:00
|
|
|
}
|
2016-09-05 23:10:48 +08:00
|
|
|
|
2014-01-06 08:22:38 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
2016-09-05 23:10:48 +08:00
|
|
|
#endif
|
|
|
|
static uint8_t sb_data[ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
|
2014-01-06 08:22:38 +08:00
|
|
|
static int handled_test_3;
|
2016-12-19 10:47:42 +08:00
|
|
|
static uint8_t test_3_pub_key[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
static uint8_t test_3_ping_id[CRYPTO_SHA256_SIZE];
|
2022-02-03 04:02:23 +08:00
|
|
|
static int handle_test_3(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
|
2014-01-06 08:22:38 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
Onion *onion = (Onion *)object;
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (length != (1 + CRYPTO_NONCE_SIZE + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + 1 + CRYPTO_SHA256_SIZE +
|
|
|
|
CRYPTO_MAC_SIZE)) {
|
2014-01-06 08:22:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t plain[1 + CRYPTO_SHA256_SIZE];
|
|
|
|
#if 0
|
|
|
|
print_client_id(packet, length);
|
|
|
|
#endif
|
2018-01-15 09:23:08 +08:00
|
|
|
int len = decrypt_data(test_3_pub_key, dht_get_self_secret_key(onion->dht),
|
|
|
|
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH,
|
2016-12-19 10:47:42 +08:00
|
|
|
packet + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE,
|
|
|
|
1 + CRYPTO_SHA256_SIZE + CRYPTO_MAC_SIZE, plain);
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (len == -1) {
|
2014-01-06 08:22:38 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2014-01-10 08:54:42 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (memcmp(packet + 1, sb_data, ONION_ANNOUNCE_SENDBACK_DATA_LENGTH) != 0) {
|
2014-01-09 09:15:35 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-09 09:15:35 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(test_3_ping_id, plain + 1, CRYPTO_SHA256_SIZE);
|
|
|
|
#if 0
|
|
|
|
print_client_id(test_3_ping_id, sizeof(test_3_ping_id));
|
|
|
|
#endif
|
2014-01-06 08:22:38 +08:00
|
|
|
handled_test_3 = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-05 08:40:43 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
static uint8_t nonce[CRYPTO_NONCE_SIZE];
|
2014-01-07 06:59:44 +08:00
|
|
|
static int handled_test_4;
|
2022-02-03 04:02:23 +08:00
|
|
|
static int handle_test_4(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
|
2014-01-07 06:59:44 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
Onion *onion = (Onion *)object;
|
2014-01-07 06:59:44 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (length != (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + sizeof("Install gentoo") +
|
|
|
|
CRYPTO_MAC_SIZE)) {
|
2014-01-07 06:59:44 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-07 06:59:44 +08:00
|
|
|
|
|
|
|
uint8_t plain[sizeof("Install gentoo")] = {0};
|
2014-01-15 03:20:42 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(nonce, packet + 1, CRYPTO_NONCE_SIZE) != 0) {
|
2014-01-15 03:20:42 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-15 03:20:42 +08:00
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, dht_get_self_secret_key(onion->dht), packet + 1,
|
2016-12-19 10:47:42 +08:00
|
|
|
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, sizeof("Install gentoo") + CRYPTO_MAC_SIZE, plain);
|
2014-01-07 06:59:44 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (len == -1) {
|
2014-01-07 06:59:44 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-07 06:59:44 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (memcmp(plain, "Install gentoo", sizeof("Install gentoo")) != 0) {
|
2014-01-07 06:59:44 +08:00
|
|
|
return 1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-07 06:59:44 +08:00
|
|
|
|
|
|
|
handled_test_4 = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void test_basic(void)
|
2014-01-01 12:18:37 +08:00
|
|
|
{
|
2018-02-21 02:56:26 +08:00
|
|
|
uint32_t index[] = { 1, 2, 3 };
|
|
|
|
Logger *log1 = logger_new();
|
|
|
|
logger_callback_log(log1, (logger_cb *)print_debug_log, nullptr, &index[0]);
|
|
|
|
Logger *log2 = logger_new();
|
|
|
|
logger_callback_log(log2, (logger_cb *)print_debug_log, nullptr, &index[1]);
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time1 = mono_time_new();
|
|
|
|
Mono_Time *mono_time2 = mono_time_new();
|
|
|
|
|
2017-11-15 16:13:31 +08:00
|
|
|
IP ip = get_loopback();
|
2022-02-04 00:17:29 +08:00
|
|
|
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true));
|
|
|
|
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true));
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
|
2018-04-15 14:27:17 +08:00
|
|
|
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-01-11 00:44:49 +08:00
|
|
|
IP_Port on1 = {ip, net_port(onion1->net)};
|
2014-01-01 12:18:37 +08:00
|
|
|
Node_format n1;
|
2018-01-15 09:23:08 +08:00
|
|
|
memcpy(n1.public_key, dht_get_self_public_key(onion1->dht), CRYPTO_PUBLIC_KEY_SIZE);
|
2014-01-01 12:18:37 +08:00
|
|
|
n1.ip_port = on1;
|
|
|
|
|
2018-01-11 00:44:49 +08:00
|
|
|
IP_Port on2 = {ip, net_port(onion2->net)};
|
2014-01-01 12:18:37 +08:00
|
|
|
Node_format n2;
|
2018-01-15 09:23:08 +08:00
|
|
|
memcpy(n2.public_key, dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
|
2014-01-01 12:18:37 +08:00
|
|
|
n2.ip_port = on2;
|
|
|
|
|
2020-05-04 09:12:57 +08:00
|
|
|
const char req_message[] = "Install Gentoo";
|
|
|
|
uint8_t req_packet[1 + sizeof(req_message)];
|
|
|
|
req_packet[0] = NET_PACKET_ANNOUNCE_REQUEST;
|
|
|
|
memcpy(req_packet + 1, req_message, sizeof(req_message));
|
|
|
|
|
2014-01-01 12:18:37 +08:00
|
|
|
Node_format nodes[4];
|
|
|
|
nodes[0] = n1;
|
|
|
|
nodes[1] = n2;
|
|
|
|
nodes[2] = n1;
|
|
|
|
nodes[3] = n2;
|
2014-02-27 01:46:11 +08:00
|
|
|
Onion_Path path;
|
|
|
|
create_onion_path(onion1->dht, &path, nodes);
|
2022-02-03 04:02:23 +08:00
|
|
|
int ret = send_onion_packet(onion1->net, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));
|
2014-01-01 12:18:37 +08:00
|
|
|
ck_assert_msg(ret == 0, "Failed to create/send onion packet.");
|
|
|
|
|
|
|
|
handled_test_1 = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-01 12:18:37 +08:00
|
|
|
do_onion(onion1);
|
|
|
|
do_onion(onion2);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (handled_test_1 == 0);
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-04-15 14:27:17 +08:00
|
|
|
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_2, onion1);
|
2014-01-01 12:18:37 +08:00
|
|
|
handled_test_2 = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-01 12:18:37 +08:00
|
|
|
do_onion(onion1);
|
|
|
|
do_onion(onion2);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (handled_test_2 == 0);
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2021-12-08 02:01:15 +08:00
|
|
|
Onion_Announce *onion1_a = new_onion_announce(log1, mono_time1, onion1->dht);
|
|
|
|
Onion_Announce *onion2_a = new_onion_announce(log2, mono_time2, onion2->dht);
|
2014-01-06 08:22:38 +08:00
|
|
|
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing.");
|
2014-01-06 08:22:38 +08:00
|
|
|
uint8_t zeroes[64] = {0};
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(sb_data, sizeof(sb_data));
|
2014-05-13 07:43:44 +08:00
|
|
|
uint64_t s;
|
|
|
|
memcpy(&s, sb_data, sizeof(uint64_t));
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(test_3_pub_key, nodes[3].public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
2022-02-18 09:41:17 +08:00
|
|
|
ret = send_announce_request(onion1->net, &path, &nodes[3],
|
2018-01-15 09:23:08 +08:00
|
|
|
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);
|
2014-01-06 08:22:38 +08:00
|
|
|
ck_assert_msg(ret == 0, "Failed to create/send onion announce_request packet.");
|
|
|
|
handled_test_3 = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-06 08:22:38 +08:00
|
|
|
do_onion(onion1);
|
|
|
|
do_onion(onion2);
|
2014-04-16 07:16:35 +08:00
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (handled_test_3 == 0);
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(sb_data, sizeof(sb_data));
|
2014-05-13 07:43:44 +08:00
|
|
|
memcpy(&s, sb_data, sizeof(uint64_t));
|
2018-01-15 09:23:08 +08:00
|
|
|
memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
|
2018-08-02 07:02:13 +08:00
|
|
|
onion_announce_entry_set_time(onion2_a, 1, mono_time_get(mono_time2));
|
2014-01-07 06:59:44 +08:00
|
|
|
networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
|
2022-02-18 09:41:17 +08:00
|
|
|
send_announce_request(onion1->net, &path, &nodes[3],
|
2018-01-15 09:23:08 +08:00
|
|
|
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);
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-06 08:22:38 +08:00
|
|
|
do_onion(onion1);
|
|
|
|
do_onion(onion2);
|
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} 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);
|
2014-01-07 06:59:44 +08:00
|
|
|
|
|
|
|
c_sleep(1000);
|
2018-02-21 02:56:26 +08:00
|
|
|
Logger *log3 = logger_new();
|
|
|
|
logger_callback_log(log3, (logger_cb *)print_debug_log, nullptr, &index[2]);
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time3 = mono_time_new();
|
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, &ip, 36569), true));
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
|
2014-01-15 03:20:42 +08:00
|
|
|
|
2016-11-06 23:14:02 +08:00
|
|
|
random_nonce(nonce);
|
2022-02-03 04:02:23 +08:00
|
|
|
ret = send_data_request(onion3->net, &path, &nodes[3].ip_port,
|
2018-01-15 09:23:08 +08:00
|
|
|
dht_get_self_public_key(onion1->dht),
|
|
|
|
dht_get_self_public_key(onion1->dht),
|
2016-09-05 23:10:48 +08:00
|
|
|
nonce, (const uint8_t *)"Install gentoo", sizeof("Install gentoo"));
|
2014-01-07 06:59:44 +08:00
|
|
|
ck_assert_msg(ret == 0, "Failed to create/send onion data_request packet.");
|
|
|
|
handled_test_4 = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-07 06:59:44 +08:00
|
|
|
do_onion(onion1);
|
|
|
|
do_onion(onion2);
|
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (handled_test_4 == 0);
|
2015-02-08 11:57:04 +08:00
|
|
|
|
|
|
|
kill_onion_announce(onion2_a);
|
2018-02-21 02:56:26 +08:00
|
|
|
kill_onion_announce(onion1_a);
|
2015-02-08 11:57:04 +08:00
|
|
|
|
|
|
|
{
|
2018-02-21 02:56:26 +08:00
|
|
|
Onion *onion = onion3;
|
2015-02-08 11:57:04 +08:00
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
Networking_Core *net = dht_get_net(onion->dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
DHT *dht = onion->dht;
|
|
|
|
kill_onion(onion);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time3);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(log3);
|
2015-02-08 11:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Onion *onion = onion2;
|
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
Networking_Core *net = dht_get_net(onion->dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
DHT *dht = onion->dht;
|
|
|
|
kill_onion(onion);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time2);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(log2);
|
2015-02-08 11:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-02-21 02:56:26 +08:00
|
|
|
Onion *onion = onion1;
|
2015-02-08 11:57:04 +08:00
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
Networking_Core *net = dht_get_net(onion->dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
DHT *dht = onion->dht;
|
|
|
|
kill_onion(onion);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_time1);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(log1);
|
2015-02-08 11:57:04 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:22:38 +08:00
|
|
|
typedef struct {
|
2018-02-21 02:56:26 +08:00
|
|
|
Logger *log;
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time;
|
2014-01-06 08:22:38 +08:00
|
|
|
Onion *onion;
|
|
|
|
Onion_Announce *onion_a;
|
2014-01-30 09:59:50 +08:00
|
|
|
Onion_Client *onion_c;
|
2014-01-06 08:22:38 +08:00
|
|
|
} Onions;
|
|
|
|
|
2018-02-21 02:56:26 +08:00
|
|
|
static Onions *new_onions(uint16_t port, uint32_t *index)
|
2014-01-06 08:22:38 +08:00
|
|
|
{
|
2017-11-15 16:13:31 +08:00
|
|
|
IP ip = get_loopback();
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v6.uint8[15] = 1;
|
2016-09-18 08:31:55 +08:00
|
|
|
Onions *on = (Onions *)malloc(sizeof(Onions));
|
2018-01-14 19:48:35 +08:00
|
|
|
|
|
|
|
if (!on) {
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2018-01-14 19:48:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:56:26 +08:00
|
|
|
on->log = logger_new();
|
|
|
|
|
|
|
|
if (!on->log) {
|
|
|
|
free(on);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger_callback_log(on->log, (logger_cb *)print_debug_log, nullptr, index);
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
on->mono_time = mono_time_new();
|
|
|
|
|
|
|
|
if (!on->mono_time) {
|
|
|
|
logger_kill(on->log);
|
|
|
|
free(on);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
Networking_Core *net = new_networking(on->log, &ip, port);
|
2018-01-14 19:48:35 +08:00
|
|
|
|
|
|
|
if (!net) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2018-01-14 19:48:35 +08:00
|
|
|
free(on);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2018-01-14 19:48:35 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
DHT *dht = new_dht(on->log, on->mono_time, net, true);
|
2018-01-14 19:48:35 +08:00
|
|
|
|
|
|
|
if (!dht) {
|
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2018-01-14 19:48:35 +08:00
|
|
|
free(on);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2018-01-14 19:48:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 02:01:15 +08:00
|
|
|
on->onion = new_onion(on->log, on->mono_time, dht);
|
2018-01-14 19:48:35 +08:00
|
|
|
|
|
|
|
if (!on->onion) {
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2018-01-14 19:48:35 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2018-01-14 19:48:35 +08:00
|
|
|
free(on);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2018-01-14 19:48:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 02:01:15 +08:00
|
|
|
on->onion_a = new_onion_announce(on->log, on->mono_time, dht);
|
2018-01-14 19:48:35 +08:00
|
|
|
|
|
|
|
if (!on->onion_a) {
|
|
|
|
kill_onion(on->onion);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2018-01-14 19:48:35 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2018-01-14 19:48:35 +08:00
|
|
|
free(on);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2018-01-14 19:48:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-27 09:07:46 +08:00
|
|
|
TCP_Proxy_Info inf = {{{{0}}}};
|
2020-05-04 07:40:59 +08:00
|
|
|
on->onion_c = new_onion_client(on->log, on->mono_time, new_net_crypto(on->log, on->mono_time, dht, &inf));
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2018-01-14 19:48:35 +08:00
|
|
|
if (!on->onion_c) {
|
|
|
|
kill_onion_announce(on->onion_a);
|
|
|
|
kill_onion(on->onion);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2018-01-14 19:48:35 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2018-01-14 19:48:35 +08:00
|
|
|
free(on);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2018-01-14 19:48:35 +08:00
|
|
|
return on;
|
2014-01-06 08:22:38 +08:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void do_onions(Onions *on)
|
2014-01-30 09:59:50 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(on->mono_time);
|
2018-08-10 07:53:39 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
networking_poll(on->onion->net, nullptr);
|
2018-07-05 18:31:29 +08:00
|
|
|
do_dht(on->onion->dht);
|
2014-01-30 09:59:50 +08:00
|
|
|
do_onion_client(on->onion_c);
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void kill_onions(Onions *on)
|
2015-02-08 11:57:04 +08:00
|
|
|
{
|
2018-01-15 09:23:08 +08:00
|
|
|
Networking_Core *net = dht_get_net(on->onion->dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
DHT *dht = on->onion->dht;
|
2018-01-14 01:06:18 +08:00
|
|
|
Net_Crypto *c = onion_get_net_crypto(on->onion_c);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_onion_client(on->onion_c);
|
|
|
|
kill_onion_announce(on->onion_a);
|
|
|
|
kill_onion(on->onion);
|
2015-04-07 03:57:23 +08:00
|
|
|
kill_net_crypto(c);
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2015-02-08 11:57:04 +08:00
|
|
|
kill_networking(net);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(on->mono_time);
|
2018-02-21 02:56:26 +08:00
|
|
|
logger_kill(on->log);
|
2015-02-08 11:57:04 +08:00
|
|
|
free(on);
|
|
|
|
}
|
|
|
|
|
2014-01-31 03:59:31 +08:00
|
|
|
#define NUM_ONIONS 50
|
2015-04-07 03:57:23 +08:00
|
|
|
#define NUM_FIRST 7
|
|
|
|
#define NUM_LAST 37
|
|
|
|
|
2016-09-14 05:01:45 +08:00
|
|
|
static bool first_ip, last_ip;
|
2022-02-03 04:02:23 +08:00
|
|
|
static void dht_ip_callback(void *object, int32_t number, const IP_Port *ip_port)
|
2015-04-07 03:57:23 +08:00
|
|
|
{
|
|
|
|
if (NUM_FIRST == number) {
|
|
|
|
first_ip = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NUM_LAST == number) {
|
|
|
|
last_ip = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ck_abort_msg("Error.");
|
|
|
|
}
|
|
|
|
|
2016-09-14 05:01:45 +08:00
|
|
|
static bool first, last;
|
2016-12-19 10:47:42 +08:00
|
|
|
static uint8_t first_dht_pk[CRYPTO_PUBLIC_KEY_SIZE];
|
|
|
|
static uint8_t last_dht_pk[CRYPTO_PUBLIC_KEY_SIZE];
|
2015-04-07 03:57:23 +08:00
|
|
|
|
2016-08-23 05:44:58 +08:00
|
|
|
static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata)
|
2015-04-07 03:57:23 +08:00
|
|
|
{
|
2015-12-16 03:58:36 +08:00
|
|
|
if ((NUM_FIRST == number && !first) || (NUM_LAST == number && !last)) {
|
2016-09-18 08:31:55 +08:00
|
|
|
Onions *on = (Onions *)object;
|
2015-12-16 03:58:36 +08:00
|
|
|
uint16_t count = 0;
|
2018-07-05 18:31:29 +08:00
|
|
|
int ret = dht_addfriend(on->onion->dht, dht_public_key, &dht_ip_callback, object, number, &count);
|
|
|
|
ck_assert_msg(ret == 0, "dht_addfriend() did not return 0");
|
2015-12-16 03:58:36 +08:00
|
|
|
ck_assert_msg(count == 1, "Count not 1, count is %u", count);
|
2015-04-07 03:57:23 +08:00
|
|
|
|
2015-12-16 03:58:36 +08:00
|
|
|
if (NUM_FIRST == number && !first) {
|
|
|
|
first = 1;
|
2015-04-07 03:57:23 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(dht_public_key, last_dht_pk, CRYPTO_PUBLIC_KEY_SIZE) != 0) {
|
2015-12-16 03:58:36 +08:00
|
|
|
ck_abort_msg("Error wrong dht key.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2015-04-07 03:57:23 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 03:58:36 +08:00
|
|
|
if (NUM_LAST == number && !last) {
|
|
|
|
last = 1;
|
2015-04-07 03:57:23 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(dht_public_key, first_dht_pk, CRYPTO_PUBLIC_KEY_SIZE) != 0) {
|
2015-12-16 03:58:36 +08:00
|
|
|
ck_abort_msg("Error wrong dht key.");
|
|
|
|
}
|
2015-04-07 03:57:23 +08:00
|
|
|
|
2015-12-16 03:58:36 +08:00
|
|
|
return;
|
2015-04-07 03:57:23 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 03:58:36 +08:00
|
|
|
ck_abort_msg("Error.");
|
2015-04-07 03:57:23 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-06 08:22:38 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void test_announce(void)
|
2014-01-05 08:40:43 +08:00
|
|
|
{
|
2014-02-28 05:59:17 +08:00
|
|
|
uint32_t i, j;
|
2018-02-21 02:56:26 +08:00
|
|
|
uint32_t index[NUM_ONIONS];
|
2014-01-06 08:22:38 +08:00
|
|
|
Onions *onions[NUM_ONIONS];
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ONIONS; ++i) {
|
2018-02-21 02:56:26 +08:00
|
|
|
index[i] = i + 1;
|
2018-06-24 20:41:26 +08:00
|
|
|
onions[i] = new_onions(i + 36655, &index[i]);
|
2018-02-06 05:38:58 +08:00
|
|
|
ck_assert_msg(onions[i] != nullptr, "Failed to create onions. %u", i);
|
2014-01-06 08:22:38 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:13:31 +08:00
|
|
|
IP ip = get_loopback();
|
2014-01-30 09:59:50 +08:00
|
|
|
|
2014-02-06 08:51:22 +08:00
|
|
|
for (i = 3; i < NUM_ONIONS; ++i) {
|
2018-01-11 00:44:49 +08:00
|
|
|
IP_Port ip_port = {ip, net_port(onions[i - 1]->onion->net)};
|
2022-02-03 04:02:23 +08:00
|
|
|
dht_bootstrap(onions[i]->onion->dht, &ip_port, dht_get_self_public_key(onions[i - 1]->onion->dht));
|
2018-01-11 00:44:49 +08:00
|
|
|
IP_Port ip_port1 = {ip, net_port(onions[i - 2]->onion->net)};
|
2022-02-03 04:02:23 +08:00
|
|
|
dht_bootstrap(onions[i]->onion->dht, &ip_port1, dht_get_self_public_key(onions[i - 2]->onion->dht));
|
2018-01-11 00:44:49 +08:00
|
|
|
IP_Port ip_port2 = {ip, net_port(onions[i - 3]->onion->net)};
|
2022-02-03 04:02:23 +08:00
|
|
|
dht_bootstrap(onions[i]->onion->dht, &ip_port2, dht_get_self_public_key(onions[i - 3]->onion->dht));
|
2014-01-30 09:59:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t connected = 0;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-30 09:59:50 +08:00
|
|
|
connected = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ONIONS; ++i) {
|
|
|
|
do_onions(onions[i]);
|
2018-07-05 18:31:29 +08:00
|
|
|
connected += dht_isconnected(onions[i]->onion->dht);
|
2014-01-30 09:59:50 +08:00
|
|
|
}
|
2014-02-06 08:51:22 +08:00
|
|
|
|
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (connected != NUM_ONIONS);
|
2014-01-30 09:59:50 +08:00
|
|
|
|
2015-04-07 03:57:23 +08:00
|
|
|
printf("connected\n");
|
|
|
|
|
2014-02-28 05:59:17 +08:00
|
|
|
for (i = 0; i < 25 * 2; ++i) {
|
|
|
|
for (j = 0; j < NUM_ONIONS; ++j) {
|
|
|
|
do_onions(onions[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
c_sleep(50);
|
|
|
|
}
|
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
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);
|
2015-04-07 03:57:23 +08:00
|
|
|
|
|
|
|
printf("adding friend\n");
|
2018-01-14 01:06:18 +08:00
|
|
|
int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c,
|
2018-01-15 08:29:51 +08:00
|
|
|
nc_get_self_public_key(onion_get_net_crypto(onions[NUM_LAST]->onion_c)));
|
2018-01-14 01:06:18 +08:00
|
|
|
int frnum = onion_addfriend(onions[NUM_LAST]->onion_c,
|
2018-01-15 08:29:51 +08:00
|
|
|
nc_get_self_public_key(onion_get_net_crypto(onions[NUM_FIRST]->onion_c)));
|
2015-04-07 03:57:23 +08:00
|
|
|
|
|
|
|
onion_dht_pk_callback(onions[NUM_FIRST]->onion_c, frnum_f, &dht_pk_callback, onions[NUM_FIRST], NUM_FIRST);
|
|
|
|
onion_dht_pk_callback(onions[NUM_LAST]->onion_c, frnum, &dht_pk_callback, onions[NUM_LAST], NUM_LAST);
|
2014-02-06 08:51:22 +08:00
|
|
|
|
|
|
|
IP_Port ip_port;
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-02-06 08:51:22 +08:00
|
|
|
for (i = 0; i < NUM_ONIONS; ++i) {
|
2015-04-07 03:57:23 +08:00
|
|
|
do_onions(onions[i]);
|
2014-02-06 08:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (!first || !last);
|
2014-01-30 09:59:50 +08:00
|
|
|
|
2015-04-07 03:57:23 +08:00
|
|
|
printf("Waiting for ips\n");
|
2014-01-30 09:59:50 +08:00
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2014-01-31 03:59:31 +08:00
|
|
|
for (i = 0; i < NUM_ONIONS; ++i) {
|
|
|
|
do_onions(onions[i]);
|
|
|
|
}
|
2014-01-30 09:59:50 +08:00
|
|
|
|
2014-01-31 03:59:31 +08:00
|
|
|
c_sleep(50);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (!first_ip || !last_ip);
|
2014-02-06 08:51:22 +08:00
|
|
|
|
2015-04-07 03:57:23 +08:00
|
|
|
onion_getfriendip(onions[NUM_LAST]->onion_c, frnum, &ip_port);
|
2018-01-11 00:44:49 +08:00
|
|
|
ck_assert_msg(ip_port.port == net_port(onions[NUM_FIRST]->onion->net), "Port in returned ip not correct.");
|
2015-02-08 11:57:04 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_ONIONS; ++i) {
|
|
|
|
kill_onions(onions[i]);
|
|
|
|
}
|
2014-01-05 08:40:43 +08:00
|
|
|
}
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2014-01-01 12:18:37 +08:00
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
test_basic();
|
|
|
|
test_announce();
|
2014-01-01 12:18:37 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
return 0;
|
2014-01-01 12:18:37 +08:00
|
|
|
}
|