toxcore/other/DHT_bootstrap.c

138 lines
3.9 KiB
C
Raw Normal View History

2013-07-14 01:20:12 +08:00
/* DHT boostrap
*
2013-07-14 01:20:12 +08:00
* A simple DHT boostrap server for tox.
*
* Build commands (use one or the other):
* gcc -O2 -Wall -D VANILLA_NACL -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_bootstrap.c
*
* gcc -O2 -Wall -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c -lsodium DHT_bootstrap.c
2013-08-17 01:11:09 +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
*/
#include "../toxcore/DHT.h"
#include "../toxcore/friend_requests.h"
#include "../toxcore/misc_tools.h"
2013-07-14 01:20:12 +08:00
/* Sleep function (x = milliseconds) */
2013-07-14 01:20:12 +08:00
#ifdef WIN32
#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-14 01:20:12 +08:00
void manage_keys(DHT *dht)
{
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
if (keys_file != NULL) {
/* If file was opened successfully -- load keys,
otherwise save new keys */
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
2013-08-17 01:11:09 +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
2013-08-21 04:37:34 +08:00
load_keys(dht->c, keys);
printf("Keys loaded successfully.\n");
} else {
2013-08-21 04:37:34 +08:00
new_keys(dht->c);
save_keys(dht->c, keys);
keys_file = fopen("key", "w");
2013-08-17 01:11:09 +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
printf("Keys saved successfully.\n");
}
fclose(keys_file);
}
2013-07-14 01:20:12 +08:00
int main(int argc, char *argv[])
{
/* Initialize networking -
Bind to ip 0.0.0.0:PORT */
2013-08-21 04:37:34 +08:00
IP ip;
2013-08-30 15:28:50 +08:00
ip.uint32 = 0;
DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT)));
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;
FILE *file;
file = fopen("PUBLIC_ID.txt", "w");
2013-08-17 01:11:09 +08:00
for (i = 0; i < 32; i++) {
2013-08-21 04:37:34 +08:00
if (dht->c->self_public_key[i] < 16)
2013-07-14 01:20:12 +08:00
printf("0");
2013-08-17 01:11:09 +08:00
2013-08-21 04:37:34 +08:00
printf("%hhX", dht->c->self_public_key[i]);
fprintf(file, "%hhX", dht->c->self_public_key[i]);
2013-07-14 01:20:12 +08:00
}
fclose(file);
2013-07-14 01:20:12 +08:00
printf("\n");
2013-07-24 00:11:07 +08:00
printf("Port: %u\n", PORT);
perror("Initialization.");
2013-07-24 00:11:07 +08:00
if (argc > 3) {
printf("Trying to bootstrap into the network...\n");
IP_Port bootstrap_info;
2013-08-30 15:28:50 +08:00
bootstrap_info.ip.uint32 = inet_addr(argv[1]);
bootstrap_info.port = htons(atoi(argv[2]));
uint8_t *bootstrap_key = hex_string_to_bin(argv[3]);
2013-08-21 04:37:34 +08:00
DHT_bootstrap(dht, bootstrap_info, bootstrap_key);
free(bootstrap_key);
}
2013-07-14 01:20:12 +08:00
int is_waiting_for_dht_connection = 1;
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)) {
printf("Connected to other bootstrap server successfully.\n");
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-07-24 00:11:07 +08:00
2013-08-21 04:37:34 +08:00
networking_poll(dht->c->lossless_udp->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
return 0;
}