2013-10-25 04:32:28 +08:00
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
/* DHT boostrap
|
2013-07-19 14:25:01 +08:00
|
|
|
*
|
2014-02-23 06:06:07 +08:00
|
|
|
* A simple DHT boostrap node for tox.
|
2013-07-19 14:25:01 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-17 01:11:09 +08:00
|
|
|
*
|
2013-07-14 01:20:12 +08:00
|
|
|
*/
|
2013-10-25 04:32:28 +08:00
|
|
|
|
2013-09-11 05:15:26 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2013-07-14 01:20:12 +08:00
|
|
|
|
2013-08-04 20:10:37 +08:00
|
|
|
#include "../toxcore/DHT.h"
|
2013-09-13 01:09:25 +08:00
|
|
|
#include "../toxcore/LAN_discovery.h"
|
2013-08-04 20:10:37 +08:00
|
|
|
#include "../toxcore/friend_requests.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
|
|
|
|
|
2013-08-30 20:16:34 +08:00
|
|
|
#include "../testing/misc_tools.c"
|
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-08-30 05:17:51 +08:00
|
|
|
/* Sleep function (x = milliseconds) */
|
2014-01-20 07:18:25 +08:00
|
|
|
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
2013-07-14 01:20:12 +08:00
|
|
|
#define c_sleep(x) Sleep(1*x)
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2013-07-20 23:47:59 +08:00
|
|
|
#include <arpa/inet.h>
|
2013-07-14 01:20:12 +08:00
|
|
|
#define c_sleep(x) usleep(1000*x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PORT 33445
|
|
|
|
|
2013-07-28 00:10:41 +08:00
|
|
|
|
2013-08-21 07:37:05 +08:00
|
|
|
void manage_keys(DHT *dht)
|
2013-07-23 03:16:01 +08:00
|
|
|
{
|
|
|
|
const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
|
|
|
uint8_t keys[KEYS_SIZE];
|
|
|
|
|
|
|
|
FILE *keys_file = fopen("key", "r");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-23 03:16:01 +08:00
|
|
|
if (keys_file != NULL) {
|
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
|
|
|
|
2014-04-24 00:29:24 +08:00
|
|
|
memcpy(dht->self_public_key, keys, crypto_box_PUBLICKEYBYTES);
|
|
|
|
memcpy(dht->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
|
2013-08-30 05:17:51 +08:00
|
|
|
printf("Keys loaded successfully.\n");
|
2013-07-23 03:16:01 +08:00
|
|
|
} else {
|
2014-04-24 00:29:24 +08:00
|
|
|
memcpy(keys, dht->self_public_key, crypto_box_PUBLICKEYBYTES);
|
|
|
|
memcpy(keys + crypto_box_PUBLICKEYBYTES, dht->self_secret_key, crypto_box_SECRETKEYBYTES);
|
2013-07-23 03:16:01 +08:00
|
|
|
keys_file = fopen("key", "w");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-11-30 12:45:32 +08:00
|
|
|
if (keys_file == NULL) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-09-11 04:59:33 +08:00
|
|
|
if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) {
|
|
|
|
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 */
|
|
|
|
uint8_t 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
|
|
|
|
2013-09-11 06:14:20 +08:00
|
|
|
if (argvoffset < 0)
|
|
|
|
exit(1);
|
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);
|
|
|
|
|
2014-04-24 00:29:24 +08:00
|
|
|
DHT *dht = new_DHT(new_networking(ip, PORT));
|
2014-01-18 03:00:00 +08:00
|
|
|
Onion *onion = new_onion(dht);
|
|
|
|
Onion_Announce *onion_a = new_onion_announce(dht);
|
|
|
|
|
2014-02-23 06:06:07 +08:00
|
|
|
#ifdef DHT_NODE_EXTRA_PACKETS
|
2014-02-02 07:45:37 +08:00
|
|
|
bootstrap_set_callbacks(dht->net, DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD));
|
|
|
|
#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: ");
|
|
|
|
uint32_t i;
|
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};
|
2015-06-29 10:14:54 +08:00
|
|
|
TCP_Server *tcp_s = new_TCP_server(ipv6enabled, NUM_PORTS, ports, dht->self_secret_key, onion);
|
2014-04-14 08:40:48 +08:00
|
|
|
|
|
|
|
if (tcp_s == NULL) {
|
|
|
|
printf("TCP server failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 06:00:56 +08:00
|
|
|
FILE *file;
|
|
|
|
file = fopen("PUBLIC_ID.txt", "w");
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
for (i = 0; i < 32; i++) {
|
2014-04-24 00:29:24 +08:00
|
|
|
printf("%02hhX", dht->self_public_key[i]);
|
|
|
|
fprintf(file, "%02hhX", dht->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");
|
2014-04-24 00:29:24 +08:00
|
|
|
printf("Port: %u\n", ntohs(dht->net->port));
|
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");
|
2013-09-11 04:59:33 +08:00
|
|
|
uint16_t port = htons(atoi(argv[argvoffset + 2]));
|
|
|
|
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
|
2013-09-12 02:50:15 +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;
|
|
|
|
LANdiscovery_init(dht);
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
while (1) {
|
2013-08-21 04:37:34 +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
|
|
|
|
2013-08-21 04:37:34 +08:00
|
|
|
do_DHT(dht);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-25 04:32:28 +08:00
|
|
|
if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
|
2014-01-18 03:00:00 +08:00
|
|
|
send_LANdiscovery(htons(PORT), dht);
|
2013-09-13 01:09:25 +08:00
|
|
|
last_LANdiscovery = unix_time();
|
|
|
|
}
|
2013-07-24 00:11:07 +08:00
|
|
|
|
2014-04-14 08:40:48 +08:00
|
|
|
#ifdef TCP_RELAY_ENABLED
|
|
|
|
do_TCP_server(tcp_s);
|
|
|
|
#endif
|
2014-04-24 00:29:24 +08:00
|
|
|
networking_poll(dht->net);
|
2013-08-10 07:30:18 +08:00
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
c_sleep(1);
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-19 14:25:01 +08:00
|
|
|
return 0;
|
2013-07-27 04:16:58 +08:00
|
|
|
}
|