2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
|
|
|
* Copyright © 2013 Tox project.
|
2017-01-14 23:46:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-03-12 09:10:33 +08:00
|
|
|
* DHT bootstrap
|
2013-08-17 01:11:09 +08:00
|
|
|
*
|
2020-03-12 09:10:33 +08:00
|
|
|
* A simple DHT boostrap node for tox.
|
2013-07-14 01:20:12 +08:00
|
|
|
*/
|
2022-01-13 03:07:34 +08:00
|
|
|
#include <stdint.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-07-14 01:20:12 +08:00
|
|
|
|
2013-08-04 20:10:37 +08:00
|
|
|
#include "../toxcore/DHT.h"
|
2016-11-11 19:10:24 +08:00
|
|
|
#include "../toxcore/LAN_discovery.h"
|
2018-05-20 21:13:47 +08:00
|
|
|
#include "../toxcore/friend_requests.h"
|
|
|
|
#include "../toxcore/logger.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2016-11-11 19:10:24 +08:00
|
|
|
#include "../toxcore/tox.h"
|
2013-10-25 04:32:28 +08:00
|
|
|
#include "../toxcore/util.h"
|
|
|
|
|
2014-04-16 19:19:40 +08:00
|
|
|
#define TCP_RELAY_ENABLED
|
|
|
|
|
2014-04-14 08:40:48 +08:00
|
|
|
#ifdef TCP_RELAY_ENABLED
|
|
|
|
#include "../toxcore/TCP_server.h"
|
|
|
|
#endif
|
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2013-07-14 01:20:12 +08:00
|
|
|
|
2014-02-23 06:06:07 +08:00
|
|
|
#ifdef DHT_NODE_EXTRA_PACKETS
|
2016-01-01 13:19:35 +08:00
|
|
|
#include "./bootstrap_node_packets.h"
|
2014-02-02 07:45:37 +08:00
|
|
|
|
|
|
|
#define DHT_VERSION_NUMBER 1
|
|
|
|
#define DHT_MOTD "This is a test motd"
|
|
|
|
#endif
|
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
#define PORT 33445
|
|
|
|
|
2013-07-28 00:10:41 +08:00
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void manage_keys(DHT *dht)
|
2013-07-23 03:16:01 +08:00
|
|
|
{
|
2016-12-19 10:47:42 +08:00
|
|
|
enum { KEYS_SIZE = CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE };
|
2013-07-23 03:16:01 +08:00
|
|
|
uint8_t keys[KEYS_SIZE];
|
|
|
|
|
2022-01-24 22:00:01 +08:00
|
|
|
FILE *keys_file = fopen("key", "rb");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (keys_file != nullptr) {
|
2013-08-30 05:17:51 +08:00
|
|
|
/* If file was opened successfully -- load keys,
|
|
|
|
otherwise save new keys */
|
2013-07-23 03:16:01 +08:00
|
|
|
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-23 03:16:01 +08:00
|
|
|
if (read_size != KEYS_SIZE) {
|
|
|
|
printf("Error while reading the key file\nExiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-01-15 09:23:08 +08:00
|
|
|
dht_set_self_public_key(dht, keys);
|
2018-04-20 10:02:11 +08:00
|
|
|
dht_set_self_secret_key(dht, keys + CRYPTO_PUBLIC_KEY_SIZE);
|
2013-08-30 05:17:51 +08:00
|
|
|
printf("Keys loaded successfully.\n");
|
2013-07-23 03:16:01 +08:00
|
|
|
} else {
|
2018-01-15 09:23:08 +08:00
|
|
|
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);
|
2022-01-24 22:00:01 +08:00
|
|
|
keys_file = fopen("key", "wb");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (keys_file == nullptr) {
|
2014-11-30 12:45:32 +08:00
|
|
|
printf("Error opening key file in write mode.\nKeys will not be saved.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-23 03:16:01 +08:00
|
|
|
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
|
|
|
|
printf("Error while writing the key file.\nExiting.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
printf("Keys saved successfully.\n");
|
2013-07-23 03:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(keys_file);
|
|
|
|
}
|
|
|
|
|
2018-05-22 01:39:36 +08:00
|
|
|
static void print_log(void *context, Logger_Level level, const char *file, int line,
|
|
|
|
const char *func, const char *message, void *userdata)
|
|
|
|
{
|
2018-06-29 09:01:13 +08:00
|
|
|
const char *strlevel;
|
2018-05-22 01:39:36 +08:00
|
|
|
|
|
|
|
switch (level) {
|
|
|
|
case LOGGER_LEVEL_TRACE:
|
|
|
|
strlevel = "TRACE";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGGER_LEVEL_DEBUG:
|
|
|
|
strlevel = "DEBUG";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGGER_LEVEL_INFO:
|
|
|
|
strlevel = "INFO";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGGER_LEVEL_WARNING:
|
|
|
|
strlevel = "WARNING";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOGGER_LEVEL_ERROR:
|
|
|
|
strlevel = "ERROR";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
strlevel = "<unknown>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "[%s] %s:%d(%s) %s\n", strlevel, file, line, func, message);
|
|
|
|
}
|
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-02-20 10:59:02 +08:00
|
|
|
if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
|
2013-09-11 04:59:33 +08:00
|
|
|
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
|
|
|
|
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* let user override default by cmdline */
|
2018-10-09 09:00:19 +08:00
|
|
|
bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
|
2013-09-11 06:14:20 +08:00
|
|
|
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (argvoffset < 0) {
|
2013-09-11 06:14:20 +08:00
|
|
|
exit(1);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2013-09-10 22:36:20 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Initialize networking -
|
2013-09-11 04:59:33 +08:00
|
|
|
Bind to ip 0.0.0.0 / [::] : PORT */
|
2013-09-10 22:36:20 +08:00
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, ipv6enabled);
|
|
|
|
|
2018-05-20 21:13:47 +08:00
|
|
|
Logger *logger = logger_new();
|
2018-05-22 01:39:36 +08:00
|
|
|
|
|
|
|
if (MIN_LOGGER_LEVEL == LOGGER_LEVEL_TRACE || MIN_LOGGER_LEVEL == LOGGER_LEVEL_DEBUG) {
|
|
|
|
logger_callback_log(logger, print_log, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
2022-02-04 00:17:29 +08:00
|
|
|
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
|
2021-12-08 02:01:15 +08:00
|
|
|
Onion *onion = new_onion(logger, mono_time, dht);
|
2022-01-17 18:01:26 +08:00
|
|
|
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);
|
2014-01-18 03:00:00 +08:00
|
|
|
|
2014-02-23 06:06:07 +08:00
|
|
|
#ifdef DHT_NODE_EXTRA_PACKETS
|
2018-01-15 09:23:08 +08:00
|
|
|
bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
|
2014-02-02 07:45:37 +08:00
|
|
|
#endif
|
|
|
|
|
2014-01-18 03:00:00 +08:00
|
|
|
if (!(onion && onion_a)) {
|
|
|
|
printf("Something failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-01-21 02:05:34 +08:00
|
|
|
|
2013-09-11 06:44:05 +08:00
|
|
|
perror("Initialization");
|
|
|
|
|
2013-08-21 04:37:34 +08:00
|
|
|
manage_keys(dht);
|
2013-07-14 01:20:12 +08:00
|
|
|
printf("Public key: ");
|
2013-07-30 06:00:56 +08:00
|
|
|
|
2014-04-14 08:40:48 +08:00
|
|
|
#ifdef TCP_RELAY_ENABLED
|
|
|
|
#define NUM_PORTS 3
|
|
|
|
uint16_t ports[NUM_PORTS] = {443, 3389, PORT};
|
2020-05-04 07:40:59 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(logger, ipv6enabled, NUM_PORTS, ports, dht_get_self_secret_key(dht), onion);
|
2014-04-14 08:40:48 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (tcp_s == nullptr) {
|
2014-04-14 08:40:48 +08:00
|
|
|
printf("TCP server failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-01-14 19:48:35 +08:00
|
|
|
const char *const public_id_filename = "PUBLIC_ID.txt";
|
|
|
|
FILE *file = fopen(public_id_filename, "w");
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (file == nullptr) {
|
2018-01-14 19:48:35 +08:00
|
|
|
printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-30 06:00:56 +08:00
|
|
|
|
2022-01-17 18:01:26 +08:00
|
|
|
for (uint32_t i = 0; i < 32; ++i) {
|
2018-01-15 09:23:08 +08:00
|
|
|
const uint8_t *const self_public_key = dht_get_self_public_key(dht);
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("%02X", self_public_key[i]);
|
|
|
|
fprintf(file, "%02X", self_public_key[i]);
|
2013-07-14 01:20:12 +08:00
|
|
|
}
|
2013-07-30 06:00:56 +08:00
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
printf("\n");
|
2018-01-15 09:23:08 +08:00
|
|
|
printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht))));
|
2013-07-24 00:11:07 +08:00
|
|
|
|
2013-09-11 04:59:33 +08:00
|
|
|
if (argc > argvoffset + 3) {
|
2013-07-19 14:25:01 +08:00
|
|
|
printf("Trying to bootstrap into the network...\n");
|
2022-01-13 03:07:34 +08:00
|
|
|
|
|
|
|
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
|
|
|
|
|
|
|
|
if (port_conv <= 0 || port_conv > UINT16_MAX) {
|
|
|
|
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint16_t port = net_htons((uint16_t)port_conv);
|
|
|
|
|
2013-09-11 04:59:33 +08:00
|
|
|
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
|
2018-07-05 18:31:29 +08:00
|
|
|
int res = dht_bootstrap_from_address(dht, argv[argvoffset + 1],
|
2013-09-15 00:42:17 +08:00
|
|
|
ipv6enabled, port, bootstrap_key);
|
2013-07-19 14:25:01 +08:00
|
|
|
free(bootstrap_key);
|
2013-09-11 04:59:33 +08:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-19 14:25:01 +08:00
|
|
|
}
|
2013-07-14 01:20:12 +08:00
|
|
|
|
2013-07-19 14:25:01 +08:00
|
|
|
int is_waiting_for_dht_connection = 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-13 01:09:25 +08:00
|
|
|
uint64_t last_LANdiscovery = 0;
|
2018-01-15 08:48:35 +08:00
|
|
|
lan_discovery_init(dht);
|
2013-09-13 01:09:25 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
while (1) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_time);
|
2018-08-10 07:53:39 +08:00
|
|
|
|
2018-07-05 18:31:29 +08:00
|
|
|
if (is_waiting_for_dht_connection && dht_isconnected(dht)) {
|
2014-02-23 06:06:07 +08:00
|
|
|
printf("Connected to other bootstrap node successfully.\n");
|
2013-07-19 14:25:01 +08:00
|
|
|
is_waiting_for_dht_connection = 0;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2018-07-05 18:31:29 +08:00
|
|
|
do_dht(dht);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
if (mono_time_is_timeout(mono_time, last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
|
2022-02-04 04:39:33 +08:00
|
|
|
lan_discovery_send(dht_get_net(dht), dht_get_self_public_key(dht), net_htons(PORT));
|
2018-08-02 07:02:13 +08:00
|
|
|
last_LANdiscovery = mono_time_get(mono_time);
|
2013-09-13 01:09:25 +08:00
|
|
|
}
|
2013-07-24 00:11:07 +08:00
|
|
|
|
2014-04-14 08:40:48 +08:00
|
|
|
#ifdef TCP_RELAY_ENABLED
|
2018-08-02 07:02:13 +08:00
|
|
|
do_TCP_server(tcp_s, mono_time);
|
2014-04-14 08:40:48 +08:00
|
|
|
#endif
|
2018-01-29 05:30:39 +08:00
|
|
|
networking_poll(dht_get_net(dht), nullptr);
|
2013-08-10 07:30:18 +08:00
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
c_sleep(1);
|
|
|
|
}
|
2013-07-27 04:16:58 +08:00
|
|
|
}
|