diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 7f80cab49..743017942 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -37,6 +37,11 @@ printNode(struct llarp_nodedb_iter *iter) return false; } +void HandleDHTLocate(llarp_router_lookup_job *job) { + llarp::Info("DHT result: ", job->found ? "found" : "not found"); + // save to nodedb? +} + int main(int argc, char *argv[]) { @@ -58,7 +63,8 @@ main(int argc, char *argv[]) "--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" + "--export a hex formatted public key\n" + "--locate a hex formatted public key" "\n"); return 0; } @@ -67,6 +73,7 @@ main(int argc, char *argv[]) bool listMode = false; bool importMode = false; bool exportMode = false; + bool locateMode = false; int c; char *conffname; char defaultConfName[] = "daemon.ini"; @@ -84,9 +91,10 @@ main(int argc, char *argv[]) {"list", no_argument, 0, 'l'}, {"import", required_argument, 0, 'i'}, {"export", required_argument, 0, 'e'}, + {"locate", required_argument, 0, 'q'}, {0, 0, 0, 0}}; int option_index = 0; - c = getopt_long(argc, argv, "cgluie", long_options, &option_index); + c = getopt_long(argc, argv, "cgluieq", long_options, &option_index); if(c == -1) break; switch(c) @@ -112,6 +120,12 @@ main(int argc, char *argv[]) haveRequiredOptions = true; exportMode = true; break; + case 'q': + // printf ("option -g with value `%s'\n", optarg); + rcfname = optarg; + haveRequiredOptions = true; + locateMode = true; + break; case 'g': // printf ("option -g with value `%s'\n", optarg); rcfname = optarg; @@ -134,7 +148,7 @@ main(int argc, char *argv[]) return 0; } printf("parsed options\n"); - if(!genMode && !updMode && !listMode && !importMode && !exportMode) + if(!genMode && !updMode && !listMode && !importMode && !exportMode && !locateMode) { llarp::Error("I don't know what to do, no generate or update parameter\n"); return 0; @@ -249,6 +263,24 @@ main(int argc, char *argv[]) llarp::Info("Writing out: ", filename); llarp_rc_write(rc, filename.c_str()); } + if (locateMode) { + llarp::Info("Going online"); + llarp_main_setup(ctx); + + llarp::PubKey binaryPK; + llarp::HexDecode(rcfname, binaryPK.data()); + + llarp::Info("Queueing job"); + llarp_router_lookup_job *job = new llarp_router_lookup_job; + job->found = false; + job->hook = &HandleDHTLocate; + memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target + llarp_main_queryDHT(ctx, job); + + llarp::Info("Processing"); + // run system and wait + llarp_main_run(ctx); + } llarp_main_free(ctx); return 1; // success } diff --git a/include/llarp.h b/include/llarp.h index b6913ddf0..e0cd6d10d 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -27,6 +27,10 @@ llarp_main_signal(struct llarp_main *ptr, int sig); void llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler h); +/// setup main context +int +llarp_main_setup(struct llarp_main *ptr); + /// run main context int llarp_main_run(struct llarp_main *ptr); @@ -43,9 +47,14 @@ llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i); bool llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc); +/// get RC from nodeDB struct llarp_rc * llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk); +/// get RC from DHT +void +llarp_main_queryDHT(struct llarp_main *ptr, struct llarp_router_lookup_job *job); + void llarp_main_free(struct llarp_main *ptr); diff --git a/include/llarp.hpp b/include/llarp.hpp index 8448dbb35..51289549c 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -45,6 +45,9 @@ namespace llarp struct llarp_rc * GetDatabase(const byte_t *pk); + int + Setup(); + int Run(); diff --git a/include/llarp/logger.hpp b/include/llarp/logger.hpp index 0d38cd546..1ad9975ad 100644 --- a/include/llarp/logger.hpp +++ b/include/llarp/logger.hpp @@ -9,6 +9,7 @@ namespace llarp { + // probably will need to move out of llarp namespace for c api enum LogLevel { eLogDebug, diff --git a/llarp/api/message.cpp b/llarp/api/message.cpp index 977731216..fc3c7b3f0 100644 --- a/llarp/api/message.cpp +++ b/llarp/api/message.cpp @@ -86,7 +86,7 @@ namespace llarp passbuf.sz = password.size(); crypto->shorthash(secret, passbuf); - llarp::ShortHash digest; + //llarp::ShortHash digest; // zero hash hash.Zero(); @@ -106,4 +106,4 @@ namespace llarp } } } // namespace api -} // namespace llarp \ No newline at end of file +} // namespace llarp diff --git a/llarp/context.cpp b/llarp/context.cpp index 40b4323c1..25c4d2f7d 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -125,7 +125,7 @@ namespace llarp } int - Context::Run() + Context::Setup() { llarp::Info("starting up"); this->LoadDatabase(); @@ -149,26 +149,48 @@ namespace llarp router = llarp_init_router(worker, mainloop, logic); - if(llarp_configure_router(router, config)) + if(!llarp_configure_router(router, config)) { - if(custom_dht_func) - { - llarp::Info("using custom dht function"); - llarp_dht_set_msg_handler(router->dht, custom_dht_func); - } - llarp_run_router(router, nodedb); - // run net io thread - if(singleThreaded) + llarp::Error("Failed to configure router"); + return 1; + } + if(custom_dht_func) + { + llarp::Info("using custom dht function"); + llarp_dht_set_msg_handler(router->dht, custom_dht_func); + } + // set nodedb, load our RC, establish DHT + llarp_run_router(router, nodedb); + + return 0; // success + } + + int + Context::Run() + { + // just check to make sure it's not already set up (either this or we add a bool and/or add another function) + if (!this->router) + { + // set up all requirements + if (this->Setup()) { - llarp::Info("running mainloop"); - llarp_ev_loop_run_single_process(mainloop, worker, logic); + llarp::Error("Failed to setup router"); + return 1; } - else + } + + // run net io thread + if(singleThreaded) + { + llarp::Info("running mainloop"); + llarp_ev_loop_run_single_process(mainloop, worker, logic); + } + else + { + auto netio = mainloop; + while(num_nethreads--) { - auto netio = mainloop; - while(num_nethreads--) - { - netio_threads.emplace_back([netio]() { llarp_ev_loop_run(netio); }); + netio_threads.emplace_back([netio]() { llarp_ev_loop_run(netio); }); #if(__APPLE__ && __MACH__) #elif(__FreeBSD__) @@ -178,15 +200,11 @@ namespace llarp pthread_setname_np(netio_threads.back().native_handle(), "llarp-netio"); #endif - } - llarp::Info("running mainloop"); - llarp_logic_mainloop(logic); } - return 0; + llarp::Info("running mainloop"); + llarp_logic_mainloop(logic); } - else - llarp::Error("Failed to configure router"); - return 1; + return 0; } void @@ -309,6 +327,12 @@ llarp_main_signal(struct llarp_main *ptr, int sig) ptr->ctx->HandleSignal(sig); } +int +llarp_main_setup(struct llarp_main *ptr) +{ + return ptr->ctx->Setup(); +} + int llarp_main_run(struct llarp_main *ptr) { @@ -339,6 +363,11 @@ llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk) return ptr->ctx->GetDatabase(pk); } +void llarp_main_queryDHT(struct llarp_main *ptr, llarp_router_lookup_job *job) +{ + llarp_dht_lookup_router(ptr->ctx->router->dht, job); +} + void llarp_main_free(struct llarp_main *ptr) { diff --git a/llarp/logger.c b/llarp/logger.c new file mode 100644 index 000000000..3f37f58f7 --- /dev/null +++ b/llarp/logger.c @@ -0,0 +1,2 @@ +#include "logger.h" + diff --git a/llarp/logger.cpp b/llarp/logger.cpp index 15636856b..c9db6eb5d 100644 --- a/llarp/logger.cpp +++ b/llarp/logger.cpp @@ -1,4 +1,5 @@ #include "logger.hpp" +#include namespace llarp { @@ -10,3 +11,12 @@ namespace llarp _glog.minlevel = lvl; } } + +extern "C" { +void +cSetLogLevel(LogLevel lvl) +{ + llarp::SetLogLevel((llarp::LogLevel)lvl); +} + +} diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 50fcece92..579171c44 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -374,6 +374,8 @@ llarp_nodedb_load_dir(struct llarp_nodedb *n, const char *dir) return n->Load(dir); } +/// c api for nodedb::setRC +/// maybe better to use llarp_nodedb_async_verify bool llarp_nodedb_put_rc(struct llarp_nodedb *n, struct llarp_rc *rc) { @@ -387,6 +389,7 @@ llarp_nodedb_iterate_all(struct llarp_nodedb *n, struct llarp_nodedb_iter i) return n->entries.size(); } +/// maybe rename to verify_and_set void llarp_nodedb_async_verify(struct llarp_async_verify_rc *job) { @@ -396,12 +399,15 @@ llarp_nodedb_async_verify(struct llarp_async_verify_rc *job) {job, &crypto_threadworker_verifyrc}); } +// disabled for now +/* void llarp_nodedb_async_load_rc(struct llarp_async_load_rc *job) { // call in the disk io thread so we don't bog down the others llarp_threadpool_queue_job(job->diskworker, {job, &nodedb_async_load_rc}); } +*/ struct llarp_rc * llarp_nodedb_get_rc(struct llarp_nodedb *n, const byte_t *pk) diff --git a/llarp/router.cpp b/llarp/router.cpp index 2b577f14d..b8737f8ce 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -93,6 +93,12 @@ llarp_router::SendToOrQueue(const llarp::RouterID &remote, llarp_router_try_connect(this, rc, 10); return true; } + + // this would never be true, as everything is in memory + // but we'll keep around if we ever need to swap them out of memory + // but it's best to keep the paradigm that everythign is in memory at this point in development + // as it will reduce complexity + /* // try requesting the rc from the disk llarp_async_load_rc *job = new llarp_async_load_rc; job->diskworker = disk; @@ -102,10 +108,19 @@ llarp_router::SendToOrQueue(const llarp::RouterID &remote, job->hook = &HandleAsyncLoadRCForSendTo; memcpy(job->pubkey, remote, PUBKEYSIZE); llarp_nodedb_async_load_rc(job); + */ + + // we don't have the RC locally so do a dht lookup + llarp_router_lookup_job *lookup = new llarp_router_lookup_job; + lookup->user = this; + memcpy(lookup->target, this->rc.pubkey, PUBKEYSIZE); + lookup->hook = &HandleDHTLookupForSendTo; + llarp_dht_lookup_router(this->dht, lookup); return true; } +/* void llarp_router::HandleAsyncLoadRCForSendTo(llarp_async_load_rc *job) { @@ -125,6 +140,7 @@ llarp_router::HandleAsyncLoadRCForSendTo(llarp_async_load_rc *job) } delete job; } +*/ void llarp_router::HandleDHTLookupForSendTo(llarp_router_lookup_job *job) @@ -144,6 +160,7 @@ llarp_router::HandleDHTLookupForSendTo(llarp_router_lookup_job *job) void llarp_router::try_connect(fs::path rcfile) { + // FIXME: update API byte_t tmp[MAX_RC_SIZE]; llarp_rc remote = {0}; llarp_buffer_t buf; @@ -295,7 +312,6 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) llarp::async_verify_context *ctx = static_cast< llarp::async_verify_context * >(job->user); auto router = ctx->router; - llarp::Debug("rc verified? ", job->valid ? "valid" : "invalid"); llarp::PubKey pk(job->rc.pubkey); if(!job->valid) { @@ -312,8 +328,9 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) router->DiscardOutboundFor(pk); return; } + // we're valid, which means it's already been committed to the nodedb - llarp::Debug("rc verified"); + llarp::Debug("rc verified and saved to nodedb"); // refresh valid routers RC value if it's there auto v = router->validRouters.find(pk); @@ -324,8 +341,6 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) } router->validRouters[pk] = job->rc; - // TODO: update nodedb here (?) - // track valid router in dht llarp_dht_put_peer(router->dht, &router->validRouters[pk]); @@ -640,16 +655,12 @@ llarp_router::Run() // immediate connect all for service node uint64_t delay = rand() % 100; llarp_logic_call_later(logic, {delay, this, &ConnectAll}); - // llarp_logic_call_later(logic, {static_cast(delay), this, - // &ConnectAll}); } else { // delayed connect all for clients uint64_t delay = ((rand() % 10) * 500) + 1000; llarp_logic_call_later(logic, {delay, this, &ConnectAll}); - // llarp_logic_call_later(logic, {static_cast(delay), this, - // &ConnectAll}); } llarp::PubKey ourPubkey = pubkey(); @@ -1039,6 +1050,7 @@ namespace llarp { if(!StrEq(key, "*")) { + llarp::Info("interface specific binding activated"); link = new llarp_link; llarp::Zero(link, sizeof(llarp_link)); @@ -1071,8 +1083,12 @@ namespace llarp } } } + else + { + llarp::Error("link ", key, " failed to initialize. Link state", link); + } } - llarp::Error("link ", key, " failed to configure"); + llarp::Error("link ", key, " failed to configure. (Note: We don't support * yet)"); } else if(StrEq(section, "connect")) {