modernize, implement --list and --import, start on --export

pull/1/head
Ryan Tharp 6 years ago
parent 9e013167f3
commit 58bf5b1e41

@ -1,190 +1,148 @@
#include <llarp.h> #include <llarp.h>
#include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include "logger.hpp"
#include <string.h>
#include <llarp/crypto.hpp>
#include "fs.hpp"
static void
progress()
{
printf(".");
fflush(stdout);
}
struct llarp_main
{
struct llarp_crypto crypto;
struct llarp_router *router = nullptr;
struct llarp_threadpool *worker = nullptr;
struct llarp_threadpool *thread = nullptr;
struct llarp_logic *logic = nullptr;
struct llarp_config *config = nullptr;
struct llarp_nodedb *nodedb = nullptr;
struct llarp_ev_loop *mainloop = nullptr;
char nodedb_dir[256];
int exitcode;
int
shutdown()
{
printf("Shutting down ");
progress();
if(mainloop)
llarp_ev_loop_stop(mainloop);
progress();
if(worker)
llarp_threadpool_stop(worker);
progress();
if(worker) struct llarp_main *ctx = 0;
llarp_threadpool_join(worker);
progress();
if(logic)
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;
}
};
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);
if(!strcmp(section, "router"))
{
if(!strcmp(key, "threads"))
{
int workers = atoi(val);
if(workers > 0 && m->worker == nullptr)
{
m->worker = llarp_init_threadpool(workers, "llarp-worker");
}
}
}
if(!strcmp(section, "netdb"))
{
if(!strcmp(key, "dir"))
{
strncpy(m->nodedb_dir, val, sizeof(m->nodedb_dir));
}
}
}
llarp_main *sllarp = nullptr; llarp_main *sllarp = nullptr;
void
run_net(void *user)
{
llarp_ev_loop_run(static_cast< llarp_ev_loop * >(user));
}
void void
handle_signal(int sig) handle_signal(int sig)
{ {
printf("\ninterrupted\n"); if(ctx)
llarp_ev_loop_stop(sllarp->mainloop); llarp_main_signal(ctx, sig);
llarp_logic_stop(sllarp->logic);
} }
#ifndef TESTNET
#define TESTNET 0
#endif
#include <getopt.h> #include <getopt.h>
#include <llarp/router_contact.h> #include <llarp/router_contact.h>
#include <llarp/time.h> #include <llarp/time.h>
#include <fstream> #include <fstream>
#include "fs.hpp"
#include "buffer.hpp"
#include "crypto.hpp"
bool printNode(struct llarp_nodedb_iter *iter) {
char ftmp[68] = {0};
const char *hexname =
llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(iter->rc->pubkey, ftmp);
printf("[%d]=>[%s]\n", iter->index, hexname);
return false;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
// take -c to set location of daemon.ini
// --generate-blank /path/to/file.signed // --generate-blank /path/to/file.signed
// --update-ifs /path/to/file.signed // --update-ifs /path/to/file.signed
// --key /path/to/long_term_identity.key // --key /path/to/long_term_identity.key
// --import
// --export
// --generate /path/to/file.signed // --generate /path/to/file.signed
// --update /path/to/file.signed // --update /path/to/file.signed
// printf("has [%d]options\n", argc); // printf("has [%d]options\n", argc);
if(argc < 3) if(argc < 2)
{ {
printf( printf(
"please specify --generate or --update with a path to a router contact " "please specify: \n"
"file\n"); "--generate with a path to a router contact file\n"
"--update with a path to a router contact file\n"
"--list \n"
"--import with a path to a router contact file\n"
"--export with a path to a router contact file\n"
"\n");
return 0; return 0;
} }
bool genMode = false; bool genMode = false;
bool updMode = false; bool updMode = false;
bool listMode = false;
bool importMode = false;
bool exportMode = false;
int c; int c;
char *conffname;
char defaultConfName[] = "daemon.ini";
conffname = defaultConfName;
char *rcfname; char *rcfname;
char defaultName[] = "other.signed"; char defaultRcName[] = "other.signed";
rcfname = defaultName; rcfname = defaultRcName;
bool haveRequiredOptions = false;
while(1) while(1)
{ {
static struct option long_options[] = { static struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"generate", required_argument, 0, 'g'}, {"generate", required_argument, 0, 'g'},
{"update", required_argument, 0, 'u'}, {"update", required_argument, 0, 'u'},
{"list", no_argument, 0, 'l'},
{"import", required_argument, 0, 'i'},
{"export", required_argument, 0, 'e'},
{0, 0, 0, 0}}; {0, 0, 0, 0}};
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "gu", long_options, &option_index); c = getopt_long(argc, argv, "cgluie", long_options, &option_index);
if(c == -1) if(c == -1)
break; break;
switch(c) switch(c)
{ {
case 0: case 0:
break; break;
case 'c':
conffname = optarg;
break;
case 'l':
haveRequiredOptions = true;
listMode = true;
break;
case 'i':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
importMode = true;
break;
case 'e':
// printf ("option -g with value `%s'\n", optarg);
rcfname = optarg;
haveRequiredOptions = true;
exportMode = true;
break;
case 'g': case 'g':
// printf ("option -g with value `%s'\n", optarg); // printf ("option -g with value `%s'\n", optarg);
rcfname = optarg; rcfname = optarg;
haveRequiredOptions = true;
genMode = true; genMode = true;
break; break;
case 'u': case 'u':
// printf ("option -u with value `%s'\n", optarg); // printf ("option -u with value `%s'\n", optarg);
rcfname = optarg; rcfname = optarg;
haveRequiredOptions = true;
updMode = true; updMode = true;
break; break;
default: default:
abort(); abort();
} }
} }
if (!haveRequiredOptions) {
llarp::Error("Parameters dont all have their required parameters.\n");
return 0;
}
printf("parsed options\n"); printf("parsed options\n");
if(!genMode && !updMode) if(!genMode && !updMode && !listMode &&!importMode && !exportMode)
{ {
printf("I don't know what to do, no generate or update parameter\n"); llarp::Error("I don't know what to do, no generate or update parameter\n");
return 1; return 0;
} }
sllarp = new llarp_main; ctx = llarp_main_init(conffname, !TESTNET);
// llarp_new_config(&sllarp->config); if (!ctx) {
// llarp_ev_loop_alloc(&sllarp->mainloop); llarp::Error("Cant set up context");
llarp_crypto_libsodium_init(&sllarp->crypto); return 0;
}
signal(SIGINT, handle_signal);
llarp_rc tmp; llarp_rc tmp;
if(genMode) if(genMode)
@ -220,33 +178,10 @@ main(int argc, char *argv[])
if(updMode) if(updMode)
{ {
printf("rcutil.cpp - Loading [%s]\n", rcfname); printf("rcutil.cpp - Loading [%s]\n", rcfname);
fs::path our_rc_file = rcfname; llarp_rc *rc = llarp_rc_read(rcfname);
std::error_code ec;
if(!fs::exists(our_rc_file, ec))
{
printf("File not found\n");
return 0;
}
std::ifstream f(our_rc_file, std::ios::binary);
if(!f.is_open())
{
printf("Can't open file\n");
return 0;
}
byte_t tmpc[MAX_RC_SIZE];
llarp_buffer_t buf;
buf.base = tmpc;
buf.cur = buf.base;
buf.sz = sizeof(tmpc);
f.read((char *)tmpc, sizeof(MAX_RC_SIZE));
// printf("contents[%s]\n", tmpc);
if(!llarp_rc_bdecode(&tmp, &buf))
{
printf("Can't decode\n");
return 0;
}
// set updated timestamp // set updated timestamp
tmp.last_updated = llarp_time_now_ms(); rc->last_updated = llarp_time_now_ms();
// load longterm identity // load longterm identity
llarp_crypto crypt; llarp_crypto crypt;
llarp_crypto_libsodium_init(&crypt); llarp_crypto_libsodium_init(&crypt);
@ -255,16 +190,35 @@ main(int argc, char *argv[])
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity); llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
// get identity public key // get identity public key
uint8_t *pubkey = llarp::seckey_topublic(identity); uint8_t *pubkey = llarp::seckey_topublic(identity);
llarp_rc_set_pubkey(&tmp, pubkey); llarp_rc_set_pubkey(rc, pubkey);
llarp_rc_sign(&crypt, identity, &tmp); llarp_rc_sign(&crypt, identity, rc);
// set filename // set filename
fs::path our_rc_file_out = "update_debug.rc"; fs::path our_rc_file_out = "update_debug.rc";
// write file // write file
llarp_rc_write(&tmp, our_rc_file_out.c_str()); llarp_rc_write(&tmp, our_rc_file_out.c_str());
// release memory for tmp lists
llarp_rc_free(&tmp);
} }
if (listMode) {
delete sllarp; llarp_main_loadDatabase(ctx);
return 1; llarp_nodedb_iter iter;
iter.visit = printNode;
llarp_main_iterateDatabase(ctx, iter);
}
if (importMode) {
llarp_main_loadDatabase(ctx);
llarp::Info("Loading ", rcfname);
llarp_rc *rc = llarp_rc_read(rcfname);
if (!rc)
{
llarp::Error("Can't load RC");
return 0;
}
llarp_main_putDatabase(ctx, rc);
}
if (exportMode) {
llarp_main_loadDatabase(ctx);
// TODO: write me
}
llarp_main_free(ctx);
return 1; // success
} }

Loading…
Cancel
Save