2013-07-14 01:20:12 +08:00
|
|
|
/* DHT boostrap
|
2013-07-19 14:25:01 +08:00
|
|
|
*
|
2013-07-14 01:20:12 +08:00
|
|
|
* A simple DHT boostrap server for tox.
|
2013-07-19 14:25:01 +08:00
|
|
|
*
|
|
|
|
* Build commands (use one or the other):
|
2013-07-23 03:36:13 +08:00
|
|
|
* 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
|
2013-07-19 14:25:01 +08:00
|
|
|
*
|
2013-07-23 03:36:13 +08:00
|
|
|
* 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-07-14 01:20:12 +08:00
|
|
|
*/
|
|
|
|
|
2013-07-20 23:47:59 +08:00
|
|
|
#include "../core/DHT.h"
|
2013-07-23 03:36:13 +08:00
|
|
|
#include "../core/friend_requests.h"
|
2013-07-14 01:20:12 +08:00
|
|
|
|
|
|
|
//Sleep function (x = milliseconds)
|
|
|
|
#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-19 14:25:01 +08:00
|
|
|
unsigned char * hex_string_to_bin(char hex_string[])
|
|
|
|
{
|
|
|
|
unsigned char * val = malloc(strlen(hex_string));
|
|
|
|
char * pos = hex_string;
|
|
|
|
int i=0;
|
|
|
|
while(i < strlen(hex_string))
|
|
|
|
{
|
|
|
|
sscanf(pos,"%2hhx",&val[i]);
|
|
|
|
pos+=2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
2013-07-14 01:20:12 +08:00
|
|
|
|
2013-07-23 03:16:01 +08:00
|
|
|
void manage_keys()
|
|
|
|
{
|
|
|
|
const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
|
|
|
uint8_t keys[KEYS_SIZE];
|
|
|
|
|
|
|
|
FILE *keys_file = fopen("key", "r");
|
|
|
|
if (keys_file != NULL) {
|
|
|
|
//if file was opened successfully -- load keys
|
|
|
|
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
|
|
|
|
if (read_size != KEYS_SIZE) {
|
|
|
|
printf("Error while reading the key file\nExiting.\n");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("Keys loaded successfully\n");
|
|
|
|
}
|
|
|
|
load_keys(keys);
|
|
|
|
} else {
|
|
|
|
//otherwise save new keys
|
|
|
|
new_keys();
|
|
|
|
save_keys(keys);
|
|
|
|
keys_file = fopen("key", "w");
|
|
|
|
if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
|
|
|
|
printf("Error while writing the key file.\nExiting.\n");
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
printf("Keys saved successfully\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(keys_file);
|
|
|
|
}
|
|
|
|
|
2013-07-14 01:20:12 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-07-23 03:16:01 +08:00
|
|
|
manage_keys();
|
2013-07-14 01:20:12 +08:00
|
|
|
printf("Public key: ");
|
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
if(self_public_key[i] < 16)
|
|
|
|
printf("0");
|
|
|
|
printf("%hhX",self_public_key[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
//initialize networking
|
|
|
|
//bind to ip 0.0.0.0:PORT
|
|
|
|
IP ip;
|
|
|
|
ip.i = 0;
|
|
|
|
init_networking(ip, PORT);
|
|
|
|
|
|
|
|
perror("Initialization");
|
2013-07-19 14:25:01 +08:00
|
|
|
|
|
|
|
if (argc > 3) {
|
|
|
|
printf("Trying to bootstrap into the network...\n");
|
|
|
|
IP_Port bootstrap_info;
|
|
|
|
bootstrap_info.ip.i = inet_addr(argv[1]);
|
|
|
|
bootstrap_info.port = htons(atoi(argv[2]));
|
|
|
|
uint8_t *bootstrap_key = hex_string_to_bin(argv[3]);
|
|
|
|
DHT_bootstrap(bootstrap_info, bootstrap_key);
|
|
|
|
free(bootstrap_key);
|
|
|
|
}
|
2013-07-14 01:20:12 +08:00
|
|
|
|
|
|
|
IP_Port ip_port;
|
|
|
|
uint8_t data[MAX_UDP_PACKET_SIZE];
|
|
|
|
uint32_t length;
|
|
|
|
|
2013-07-19 14:25:01 +08:00
|
|
|
int is_waiting_for_dht_connection = 1;
|
2013-07-14 01:20:12 +08:00
|
|
|
while(1)
|
2013-07-19 14:25:01 +08:00
|
|
|
{
|
|
|
|
if (is_waiting_for_dht_connection && DHT_isconnected())
|
|
|
|
{
|
|
|
|
printf("Connected to other bootstrap server successfully.\n");
|
|
|
|
is_waiting_for_dht_connection = 0;
|
|
|
|
}
|
2013-07-14 01:20:12 +08:00
|
|
|
doDHT();
|
|
|
|
|
|
|
|
while(receivepacket(&ip_port, data, &length) != -1)
|
|
|
|
{
|
|
|
|
DHT_handlepacket(data, length, ip_port);
|
2013-07-23 03:36:13 +08:00
|
|
|
friendreq_handlepacket(data, length, ip_port);
|
2013-07-14 01:20:12 +08:00
|
|
|
}
|
|
|
|
c_sleep(1);
|
|
|
|
}
|
|
|
|
shutdown_networking();
|
2013-07-19 14:25:01 +08:00
|
|
|
return 0;
|
2013-07-14 01:20:12 +08:00
|
|
|
}
|