2018-05-21 12:51:10 +00:00
|
|
|
#include <llarp.h>
|
2018-05-22 18:41:38 +00:00
|
|
|
#include <pthread.h>
|
2018-05-21 12:51:10 +00:00
|
|
|
#include <signal.h>
|
2018-05-22 18:41:38 +00:00
|
|
|
#include <stdio.h>
|
2018-05-21 12:51:10 +00:00
|
|
|
#include <string.h>
|
2018-05-22 18:41:38 +00:00
|
|
|
#include <experimental/filesystem>
|
2018-06-10 14:05:48 +00:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
|
|
|
|
static void
|
|
|
|
progress()
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct llarp_main
|
|
|
|
{
|
|
|
|
struct llarp_crypto crypto;
|
2018-05-22 18:41:38 +00:00
|
|
|
struct llarp_router *router = nullptr;
|
2018-05-21 12:51:10 +00:00
|
|
|
struct llarp_threadpool *worker = nullptr;
|
|
|
|
struct llarp_threadpool *thread = nullptr;
|
2018-05-22 18:41:38 +00:00
|
|
|
struct llarp_logic *logic = nullptr;
|
|
|
|
struct llarp_config *config = nullptr;
|
|
|
|
struct llarp_nodedb *nodedb = nullptr;
|
|
|
|
struct llarp_ev_loop *mainloop = nullptr;
|
2018-05-21 12:51:10 +00:00
|
|
|
char nodedb_dir[256];
|
|
|
|
int exitcode;
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
int
|
|
|
|
shutdown()
|
2018-05-21 12:51:10 +00:00
|
|
|
{
|
|
|
|
printf("Shutting down ");
|
|
|
|
|
|
|
|
progress();
|
|
|
|
if(mainloop)
|
|
|
|
llarp_ev_loop_stop(mainloop);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_stop(worker);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
|
|
|
|
if(worker)
|
|
|
|
llarp_threadpool_join(worker);
|
|
|
|
|
|
|
|
progress();
|
2018-05-22 18:41:38 +00:00
|
|
|
if(logic)
|
2018-05-21 12:51:10 +00:00
|
|
|
llarp_logic_stop(logic);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
|
|
|
|
if(router)
|
|
|
|
llarp_stop_router(router);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
llarp_free_router(&router);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
llarp_free_config(&config);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
llarp_ev_loop_free(&mainloop);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
llarp_free_threadpool(&worker);
|
|
|
|
|
|
|
|
progress();
|
|
|
|
|
|
|
|
llarp_free_logic(&logic);
|
|
|
|
progress();
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
return exitcode;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
void
|
|
|
|
iter_main_config(struct llarp_config_iterator *itr, const char *section,
|
|
|
|
const char *key, const char *val)
|
|
|
|
{
|
|
|
|
llarp_main *m = static_cast< llarp_main * >(itr->user);
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
if(!strcmp(section, "router"))
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "threads"))
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
int workers = atoi(val);
|
2018-05-22 18:41:38 +00:00
|
|
|
if(workers > 0 && m->worker == nullptr)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
m->worker = llarp_init_threadpool(workers, "llarp-worker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 18:41:38 +00:00
|
|
|
if(!strcmp(section, "netdb"))
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "dir"))
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
llarp_main *sllarp = nullptr;
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
void
|
|
|
|
run_net(void *user)
|
2018-05-21 12:51:10 +00:00
|
|
|
{
|
2018-05-22 18:41:38 +00:00
|
|
|
llarp_ev_loop_run(static_cast< llarp_ev_loop * >(user));
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
void
|
|
|
|
handle_signal(int sig)
|
2018-05-21 12:51:10 +00:00
|
|
|
{
|
|
|
|
printf("\ninterrupted\n");
|
|
|
|
llarp_ev_loop_stop(sllarp->mainloop);
|
|
|
|
llarp_logic_stop(sllarp->logic);
|
|
|
|
}
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
#include <getopt.h>
|
2018-05-21 12:51:10 +00:00
|
|
|
#include <llarp/router_contact.h>
|
|
|
|
#include <llarp/time.h>
|
|
|
|
#include <fstream>
|
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
// --generate-blank /path/to/file.signed
|
|
|
|
// --update-ifs /path/to/file.signed
|
|
|
|
// --key /path/to/long_term_identity.key
|
|
|
|
|
|
|
|
// --generate /path/to/file.signed
|
|
|
|
// --update /path/to/file.signed
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf("has [%d]options\n", argc);
|
|
|
|
if(argc < 3)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"please specify --generate or --update with a path to a router contact "
|
|
|
|
"file\n");
|
|
|
|
return 0;
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
2018-05-22 23:55:00 +00:00
|
|
|
bool genMode = false;
|
|
|
|
bool updMode = false;
|
2018-05-21 12:51:10 +00:00
|
|
|
int c;
|
|
|
|
char *rcfname;
|
|
|
|
while(1)
|
|
|
|
{
|
2018-05-22 18:41:38 +00:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"generate", required_argument, 0, 'g'},
|
|
|
|
{"update", required_argument, 0, 'u'},
|
|
|
|
{0, 0, 0, 0}};
|
2018-05-21 12:51:10 +00:00
|
|
|
int option_index = 0;
|
2018-05-22 18:41:38 +00:00
|
|
|
c = getopt_long(argc, argv, "gu", long_options, &option_index);
|
|
|
|
if(c == -1)
|
2018-05-21 12:51:10 +00:00
|
|
|
break;
|
2018-05-22 18:41:38 +00:00
|
|
|
switch(c)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 'g':
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf ("option -g with value `%s'\n", optarg);
|
2018-05-21 12:51:10 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
genMode = true;
|
|
|
|
break;
|
|
|
|
case 'u':
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf ("option -u with value `%s'\n", optarg);
|
2018-05-21 12:51:10 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
updMode = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("parsed options\n");
|
2018-05-22 18:41:38 +00:00
|
|
|
if(!genMode && !updMode)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("I don't know what to do, no generate or update parameter\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sllarp = new llarp_main;
|
2018-05-22 18:41:38 +00:00
|
|
|
// llarp_new_config(&sllarp->config);
|
|
|
|
// llarp_ev_loop_alloc(&sllarp->mainloop);
|
2018-05-21 12:51:10 +00:00
|
|
|
llarp_crypto_libsodium_init(&sllarp->crypto);
|
|
|
|
|
|
|
|
llarp_rc tmp;
|
2018-05-22 18:41:38 +00:00
|
|
|
if(genMode)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("Creating [%s]\n", rcfname);
|
|
|
|
// Jeff wanted tmp to be stack created
|
|
|
|
// do we still need to zero it out?
|
|
|
|
llarp_rc_clear(&tmp);
|
|
|
|
// if we zero it out then
|
|
|
|
// allocate fresh pointers that the bencoder can expect to be ready
|
2018-05-25 17:52:10 +00:00
|
|
|
tmp.addrs = llarp_ai_list_new();
|
|
|
|
tmp.exits = llarp_xi_list_new();
|
2018-05-21 12:51:10 +00:00
|
|
|
// set updated timestamp
|
|
|
|
tmp.last_updated = llarp_time_now_ms();
|
|
|
|
// load longterm identity
|
|
|
|
llarp_crypto crypt;
|
2018-05-22 23:55:00 +00:00
|
|
|
llarp_crypto_libsodium_init(&crypt);
|
2018-05-21 12:51:10 +00:00
|
|
|
fs::path ident_keyfile = "identity.key";
|
2018-06-12 11:57:14 +00:00
|
|
|
byte_t identity[SECKEYSIZE];
|
2018-05-22 19:19:06 +00:00
|
|
|
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
|
2018-05-21 12:51:10 +00:00
|
|
|
// get identity public key
|
2018-06-10 14:05:48 +00:00
|
|
|
uint8_t *pubkey = llarp::seckey_topublic(identity);
|
2018-05-21 12:51:10 +00:00
|
|
|
llarp_rc_set_pubkey(&tmp, pubkey);
|
|
|
|
// this causes a segfault
|
2018-05-22 23:55:00 +00:00
|
|
|
llarp_rc_sign(&crypt, identity, &tmp);
|
2018-05-21 12:51:10 +00:00
|
|
|
// set filename
|
|
|
|
fs::path our_rc_file = rcfname;
|
|
|
|
// write file
|
2018-05-22 18:41:38 +00:00
|
|
|
llarp_rc_write(&tmp, our_rc_file.c_str());
|
2018-05-21 12:51:10 +00:00
|
|
|
// release memory for tmp lists
|
|
|
|
llarp_rc_free(&tmp);
|
|
|
|
}
|
2018-05-22 18:41:38 +00:00
|
|
|
if(updMode)
|
|
|
|
{
|
2018-05-22 23:55:00 +00:00
|
|
|
printf("rcutil.cpp - Loading [%s]\n", rcfname);
|
2018-05-21 12:51:10 +00:00
|
|
|
fs::path our_rc_file = rcfname;
|
|
|
|
std::error_code ec;
|
2018-05-22 18:41:38 +00:00
|
|
|
if(!fs::exists(our_rc_file, ec))
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("File not found\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
std::ifstream f(our_rc_file, std::ios::binary);
|
2018-05-22 18:41:38 +00:00
|
|
|
if(!f.is_open())
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("Can't open file\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-22 18:41:38 +00:00
|
|
|
byte_t tmpc[MAX_RC_SIZE];
|
2018-05-21 12:51:10 +00:00
|
|
|
llarp_buffer_t buf;
|
2018-05-22 18:41:38 +00:00
|
|
|
buf.base = tmpc;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
buf.sz = sizeof(tmpc);
|
|
|
|
f.read((char *)tmpc, sizeof(MAX_RC_SIZE));
|
2018-05-25 12:27:54 +00:00
|
|
|
// printf("contents[%s]\n", tmpc);
|
2018-05-25 17:52:10 +00:00
|
|
|
if(!llarp_rc_bdecode(&tmp, &buf))
|
2018-05-22 18:41:38 +00:00
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("Can't decode\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// set updated timestamp
|
|
|
|
tmp.last_updated = llarp_time_now_ms();
|
|
|
|
// load longterm identity
|
|
|
|
llarp_crypto crypt;
|
2018-05-22 23:55:00 +00:00
|
|
|
llarp_crypto_libsodium_init(&crypt);
|
2018-05-21 12:51:10 +00:00
|
|
|
fs::path ident_keyfile = "identity.key";
|
2018-06-12 11:57:14 +00:00
|
|
|
byte_t identity[SECKEYSIZE];
|
2018-05-22 19:19:06 +00:00
|
|
|
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
|
2018-05-21 12:51:10 +00:00
|
|
|
// get identity public key
|
2018-06-10 14:05:48 +00:00
|
|
|
uint8_t *pubkey = llarp::seckey_topublic(identity);
|
2018-05-21 12:51:10 +00:00
|
|
|
llarp_rc_set_pubkey(&tmp, pubkey);
|
2018-05-22 23:17:11 +00:00
|
|
|
llarp_rc_sign(&crypt, identity, &tmp);
|
2018-05-21 12:51:10 +00:00
|
|
|
// set filename
|
|
|
|
fs::path our_rc_file_out = "update_debug.rc";
|
|
|
|
// write file
|
2018-05-22 18:41:38 +00:00
|
|
|
llarp_rc_write(&tmp, our_rc_file_out.c_str());
|
2018-05-21 12:51:10 +00:00
|
|
|
// release memory for tmp lists
|
|
|
|
llarp_rc_free(&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete sllarp;
|
|
|
|
return 1;
|
|
|
|
}
|