From fa82d6258c0d8b92b47f67110b3f51a13624686e Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 19 Jun 2018 02:43:18 -0700 Subject: [PATCH 1/5] llarp_rc_read() --- include/llarp/router_contact.h | 3 +++ llarp/router.cpp | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index c1712a0b5..bced33a70 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -69,6 +69,9 @@ llarp_rc_clear(struct llarp_rc *rc); bool llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai); +struct llarp_rc * +llarp_rc_read(const char *fpath); + bool llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file); diff --git a/llarp/router.cpp b/llarp/router.cpp index 0c9339c52..28768ce71 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -805,6 +805,43 @@ llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath, return false; } +struct llarp_rc * +llarp_rc_read(const char *fpath) +{ + fs::path our_rc_file(fpath); + std::error_code ec; + if(!fs::exists(our_rc_file, ec)) + { + printf("File[%s] not found\n", fpath); + return 0; + } + std::ifstream f(our_rc_file, std::ios::binary); + if(!f.is_open()) + { + printf("Can't open file [%s]\n", fpath); + return 0; + } + byte_t tmp[MAX_RC_SIZE]; + llarp_buffer_t buf = llarp::StackBuffer< decltype(tmp) >(tmp); + f.seekg(0, std::ios::end); + size_t sz = f.tellg(); + f.seekg(0, std::ios::beg); + + if(sz > buf.sz) + return 0; + + f.read((char *)buf.base, sz); + //printf("contents[%s]\n", tmpc); + llarp_rc *rc = new llarp_rc; + llarp::Zero(rc, sizeof(llarp_rc)); + if(!llarp_rc_bdecode(rc, &buf)) + { + printf("Can't decode [%s]\n", fpath); + return 0; + } + return rc; +} + bool llarp_rc_write(struct llarp_rc *rc, const char *fpath) { From 51c97d44b394da8b8ab7e1a31df7730847e83c47 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 19 Jun 2018 02:44:53 -0700 Subject: [PATCH 2/5] ReloadConfig refactor, direct nodedb functions --- include/llarp.h | 24 ++++++++++++---- llarp/context.cpp | 71 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/include/llarp.h b/include/llarp.h index db00ef7c8..3cef50ab5 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -12,25 +12,39 @@ extern "C" { #endif -/** llarp application context for C api */ +/// llarp application context for C api struct llarp_main; -/** initialize application context and load config */ +/// initialize application context and load config struct llarp_main * llarp_main_init(const char *fname, bool multiProcess); -/** handle signal for main context */ +/// handle signal for main context void llarp_main_signal(struct llarp_main *ptr, int sig); -/** set custom dht message handler function */ +/// set custom dht message handler function void llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler h); -/** run main context */ +/// run main context int llarp_main_run(struct llarp_main *ptr); +/// load nodeDB into memory +int +llarp_main_loadDatabase(struct llarp_main *ptr); + +/// iterator on nodedb entries +int +llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i); + + +/// put RC into nodeDB +bool +llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc); + + void llarp_main_free(struct llarp_main *ptr); diff --git a/llarp/context.cpp b/llarp/context.cpp index cf66ce087..da6c731a2 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -29,19 +29,19 @@ namespace llarp bool Context::ReloadConfig() { - llarp::Info("loading config at ", configfile); - if(!llarp_load_config(config, configfile.c_str())) + //llarp::Info("loading config at ", configfile); + if(llarp_load_config(config, configfile.c_str())) { - llarp_config_iterator iter; - iter.user = this; - iter.visit = &iter_config; - llarp_config_iter(config, &iter); - llarp::Info("config loaded"); - return true; + llarp_free_config(&config); + llarp::Error("failed to load config file ", configfile); + return false; } - llarp_free_config(&config); - llarp::Error("failed to load config file ", configfile); - return false; + llarp_config_iterator iter; + iter.user = this; + iter.visit = &iter_config; + llarp_config_iter(config, &iter); + llarp::Info("config [", configfile, "] loaded"); + return true; } void @@ -78,10 +78,8 @@ namespace llarp } int - Context::Run() + Context::LoadDatabase() { - llarp::Info("starting up"); - llarp_ev_loop_alloc(&mainloop); llarp_crypto_libsodium_init(&crypto); nodedb = llarp_nodedb_new(&crypto); if(!nodedb_dir[0]) @@ -96,15 +94,37 @@ namespace llarp llarp::Error("nodedb_dir is incorrect"); return 0; } - llarp::Info("nodedb_dir configured!"); + //llarp::Info("nodedb_dir [", nodedb_dir, "] configured!"); ssize_t loaded = llarp_nodedb_load_dir(nodedb, nodedb_dir); - llarp::Info("nodedb_dir loaded ", loaded, " RCs"); + llarp::Info("nodedb_dir loaded ", loaded, " RCs from [", nodedb_dir, "]"); if(loaded < 0) { // shouldn't be possible llarp::Error("nodedb_dir directory doesn't exist"); return 0; } + return 1; + } + + int + Context::IterateDatabase(struct llarp_nodedb_iter i) + { + return llarp_nodedb_iterate_all(nodedb, i); + } + + bool + Context::PutDatabase(struct llarp_rc *rc) + { + return llarp_nodedb_put_rc(nodedb, rc); + } + + + int + Context::Run() + { + llarp::Info("starting up"); + this->LoadDatabase(); + llarp_ev_loop_alloc(&mainloop); // ensure worker thread pool if(!worker && !singleThreaded) @@ -290,6 +310,25 @@ llarp_main_run(struct llarp_main *ptr) return ptr->ctx->Run(); } +int +llarp_main_loadDatabase(struct llarp_main *ptr) +{ + return ptr->ctx->LoadDatabase(); +} + +int +llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i) +{ + return ptr->ctx->IterateDatabase(i); +} + +bool +llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc) +{ + return ptr->ctx->PutDatabase(rc); +} + + void llarp_main_free(struct llarp_main *ptr) { From bf6829abf1cfb394cb1a9473beb65a91537effc4 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 19 Jun 2018 02:45:29 -0700 Subject: [PATCH 3/5] more declrations for last commit --- include/llarp.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/llarp.hpp b/include/llarp.hpp index 4848b5bea..0c4a68148 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -33,6 +33,15 @@ namespace llarp void Close(); + int + LoadDatabase(); + + int + IterateDatabase(struct llarp_nodedb_iter i); + + bool + PutDatabase(struct llarp_rc *rc); + int Run(); From 9e013167f3988110a9d54431463bd4e75d12dea3 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 19 Jun 2018 02:46:21 -0700 Subject: [PATCH 4/5] change llarp_nodedb_iterate_all prototype, add index to iterator, use llarp_rc_read, llarp_nodedb_put_rc implementation, llarp_nodedb_iterate_all implementation --- include/llarp/nodedb.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/llarp/nodedb.h b/include/llarp/nodedb.h index 9d3e30ded..0399ac5e2 100644 --- a/include/llarp/nodedb.h +++ b/include/llarp/nodedb.h @@ -41,11 +41,12 @@ struct llarp_nodedb_iter { void *user; struct llarp_rc *rc; + size_t index; bool (*visit)(struct llarp_nodedb_iter *); }; /// iterate over all loaded rc with an iterator -void +int llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i); /// get a random rc that is loaded From 58bf5b1e41223d23d3b927c1332f1ba5151417ad Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 19 Jun 2018 02:48:29 -0700 Subject: [PATCH 5/5] modernize, implement --list and --import, start on --export --- daemon/rcutil.cpp | 264 +++++++++++++++++++--------------------------- 1 file changed, 109 insertions(+), 155 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 413b0f4b1..47a697642 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -1,190 +1,148 @@ #include -#include #include -#include -#include -#include -#include "fs.hpp" +#include "logger.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) - 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)); - } - } -} +struct llarp_main *ctx = 0; llarp_main *sllarp = nullptr; -void -run_net(void *user) -{ - llarp_ev_loop_run(static_cast< llarp_ev_loop * >(user)); -} - void handle_signal(int sig) { - printf("\ninterrupted\n"); - llarp_ev_loop_stop(sllarp->mainloop); - llarp_logic_stop(sllarp->logic); + if(ctx) + llarp_main_signal(ctx, sig); } +#ifndef TESTNET +#define TESTNET 0 +#endif + #include #include #include #include +#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 main(int argc, char *argv[]) { + // take -c to set location of daemon.ini // --generate-blank /path/to/file.signed // --update-ifs /path/to/file.signed // --key /path/to/long_term_identity.key + // --import + // --export // --generate /path/to/file.signed // --update /path/to/file.signed // printf("has [%d]options\n", argc); - if(argc < 3) + if(argc < 2) { printf( - "please specify --generate or --update with a path to a router contact " - "file\n"); + "please specify: \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; } bool genMode = false; bool updMode = false; + bool listMode = false; + bool importMode = false; + bool exportMode = false; int c; + char *conffname; + char defaultConfName[] = "daemon.ini"; + conffname = defaultConfName; char *rcfname; - char defaultName[] = "other.signed"; - rcfname = defaultName; + char defaultRcName[] = "other.signed"; + rcfname = defaultRcName; + bool haveRequiredOptions = false; while(1) { static struct option long_options[] = { + {"config", required_argument, 0, 'c'}, {"generate", required_argument, 0, 'g'}, {"update", required_argument, 0, 'u'}, + {"list", no_argument, 0, 'l'}, + {"import", required_argument, 0, 'i'}, + {"export", required_argument, 0, 'e'}, {0, 0, 0, 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) break; switch(c) { case 0: 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': // printf ("option -g with value `%s'\n", optarg); rcfname = optarg; + haveRequiredOptions = true; genMode = true; break; case 'u': // printf ("option -u with value `%s'\n", optarg); rcfname = optarg; + haveRequiredOptions = true; updMode = true; break; default: abort(); } } + if (!haveRequiredOptions) { + llarp::Error("Parameters dont all have their required parameters.\n"); + return 0; + } 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"); - return 1; + llarp::Error("I don't know what to do, no generate or update parameter\n"); + return 0; } - sllarp = new llarp_main; - // llarp_new_config(&sllarp->config); - // llarp_ev_loop_alloc(&sllarp->mainloop); - llarp_crypto_libsodium_init(&sllarp->crypto); + ctx = llarp_main_init(conffname, !TESTNET); + if (!ctx) { + llarp::Error("Cant set up context"); + return 0; + } + signal(SIGINT, handle_signal); llarp_rc tmp; if(genMode) @@ -220,33 +178,10 @@ main(int argc, char *argv[]) if(updMode) { printf("rcutil.cpp - Loading [%s]\n", rcfname); - fs::path our_rc_file = 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; - } + llarp_rc *rc = llarp_rc_read(rcfname); + // set updated timestamp - tmp.last_updated = llarp_time_now_ms(); + rc->last_updated = llarp_time_now_ms(); // load longterm identity llarp_crypto crypt; llarp_crypto_libsodium_init(&crypt); @@ -255,16 +190,35 @@ main(int argc, char *argv[]) llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity); // get identity public key uint8_t *pubkey = llarp::seckey_topublic(identity); - llarp_rc_set_pubkey(&tmp, pubkey); - llarp_rc_sign(&crypt, identity, &tmp); + llarp_rc_set_pubkey(rc, pubkey); + llarp_rc_sign(&crypt, identity, rc); + // set filename fs::path our_rc_file_out = "update_debug.rc"; // write file llarp_rc_write(&tmp, our_rc_file_out.c_str()); - // release memory for tmp lists - llarp_rc_free(&tmp); } - - delete sllarp; - return 1; + if (listMode) { + llarp_main_loadDatabase(ctx); + 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 }