From 876324fdbc0b57183c85fb6a818b207303952620 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 3 Jul 2018 04:25:36 -0700 Subject: [PATCH 01/21] llarp_main_abort => llarp_logic_stop_timer --- include/llarp.h | 3 +++ include/llarp/logic.h | 3 +++ llarp/context.cpp | 6 ++++++ llarp/logic.cpp | 7 +++++++ 4 files changed, 19 insertions(+) diff --git a/include/llarp.h b/include/llarp.h index 87338f9fd..89dba8594 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -35,6 +35,9 @@ llarp_main_setup(struct llarp_main *ptr); int llarp_main_run(struct llarp_main *ptr); +void +llarp_main_abort(struct llarp_main *ptr); + /// load nodeDB into memory int llarp_main_loadDatabase(struct llarp_main *ptr); diff --git a/include/llarp/logic.h b/include/llarp/logic.h index 9465ecf87..36559a343 100644 --- a/include/llarp/logic.h +++ b/include/llarp/logic.h @@ -34,6 +34,9 @@ llarp_logic_cancel_call(struct llarp_logic* logic, uint32_t id); void llarp_logic_remove_call(struct llarp_logic* logic, uint32_t id); +void +llarp_logic_stop_timer(struct llarp_logic* logic); + void llarp_logic_stop(struct llarp_logic* logic); diff --git a/llarp/context.cpp b/llarp/context.cpp index c7d93e8e5..29d1da14f 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -344,6 +344,12 @@ llarp_main_run(struct llarp_main *ptr) return ptr->ctx->Run(); } +void +llarp_main_abort(struct llarp_main *ptr) +{ + llarp_logic_stop_timer(ptr->ctx->router->logic); +} + int llarp_main_loadDatabase(struct llarp_main *ptr) { diff --git a/llarp/logic.cpp b/llarp/logic.cpp index c90898b6d..450058362 100644 --- a/llarp/logic.cpp +++ b/llarp/logic.cpp @@ -51,6 +51,13 @@ llarp_free_logic(struct llarp_logic** logic) *logic = nullptr; } +void +llarp_logic_stop_timer(struct llarp_logic* logic) +{ + if(logic->timer) + llarp_timer_stop(logic->timer); +} + void llarp_logic_stop(struct llarp_logic* logic) { From 6fb363473403d06cc8439ef6626b077507d75b9e Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Tue, 3 Jul 2018 04:26:54 -0700 Subject: [PATCH 02/21] proper quit on DHT locate result --- daemon/rcutil.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index b437faeac..2ef103a63 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -9,6 +9,7 @@ llarp_main *sllarp = nullptr; void handle_signal(int sig) { + printf("rcutil::handle_signal [%x] got [%d]\n", ctx, sig); if(ctx) llarp_main_signal(ctx, sig); } @@ -45,7 +46,21 @@ void HandleDHTLocate(llarp_router_lookup_job *job) { llarp::Info("DHT result: ", job->found ? "found" : "not found"); - // save to nodedb? + if (job->found) + { + // save to nodedb? + } + // shutdown router + + // well because we're in the gotroutermessage, we can't sigint because we'll deadlock because we're session locked + //llarp_main_signal(ctx, SIGINT); + + // llarp_timer_run(logic->timer, logic->thread); + // we'll we don't want logic thread + // but we want to switch back to the main thread + //llarp_logic_stop(); + // still need to exit this logic thread... + llarp_main_abort(ctx); } bool @@ -294,13 +309,14 @@ main(int argc, char *argv[]) llarp::PubKey binaryPK; llarp::HexDecode(rcfname, binaryPK.data()); - // llarp::SetLogLevel(llarp::eLogDebug); + //llarp::SetLogLevel(llarp::eLogDebug); 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 + // create query DHT request check_online_request *request = new check_online_request; request->ptr = ctx; @@ -330,6 +346,7 @@ main(int argc, char *argv[]) iter.visit = &aiLister; llarp_ai_list_iterate(rc->addrs, &iter); } - llarp_main_free(ctx); + // it's a unique_ptr, should clean up itself + //llarp_main_free(ctx); return 1; // success } From 9805cc153f1d956b7d065a5bbfb17d95db77695c Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 4 Jul 2018 05:09:18 -0700 Subject: [PATCH 03/21] resolve_job_type() and on_verify_client_rc() clean u --- llarp/router.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++---- llarp/router.hpp | 4 ++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/llarp/router.cpp b/llarp/router.cpp index e0bd3d539..fc5422733 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -304,8 +304,25 @@ llarp_router::on_verify_client_rc(llarp_async_verify_rc *job) llarp::async_verify_context *ctx = static_cast< llarp::async_verify_context * >(job->user); llarp::PubKey pk = job->rc.pubkey; + if(!job->valid) + { + llarp::Warn("invalid inbound RC"); + llarp_rc_free(&job->rc); + ctx->router->pendingEstablishJobs.erase(pk); + delete ctx; + return; + } + // we're valid, which means it's already been committed to the nodedb + llarp::Info("inbound connection: rc verified and saved to nodedb"); + + auto router = ctx->router; + router->validRouters[pk] = job->rc; + llarp::Info("putting valid router into dht ", pk); + // track valid router in dht + llarp_dht_put_peer(router->dht, &router->validRouters[pk]); + llarp_rc_free(&job->rc); - ctx->router->pendingEstablishJobs.erase(pk); + router->pendingEstablishJobs.erase(pk); delete ctx; } @@ -318,7 +335,7 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) llarp::PubKey pk(job->rc.pubkey); if(!job->valid) { - llarp::Warn("invalid server RC"); + llarp::Warn("invalid outbound RC"); if(ctx->establish_job) { // was an outbound attempt @@ -333,7 +350,7 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) } // we're valid, which means it's already been committed to the nodedb - llarp::Debug("rc verified and saved to nodedb"); + llarp::Info("rc verified and saved to nodedb"); // refresh valid routers RC value if it's there auto v = router->validRouters.find(pk); @@ -343,6 +360,7 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) llarp_rc_free(&v->second); } router->validRouters[pk] = job->rc; + llarp::Info("putting valid router into dht ", pk); // track valid router in dht llarp_dht_put_peer(router->dht, &router->validRouters[pk]); @@ -381,12 +399,13 @@ llarp_router::Tick() { llarp::Debug("tick router"); paths.ExpirePaths(); - // TODO: don't do this if we have enough paths already + // why is it important we have no inboundLinks if(inboundLinks.size() == 0) { auto N = llarp_nodedb_num_loaded(nodedb); if(N > 2) { + // TODO: don't do this if we have enough paths already paths.BuildPaths(); } else @@ -541,6 +560,7 @@ llarp_router::FlushOutboundFor(const llarp::RouterID &remote, void llarp_router::on_try_connect_result(llarp_link_establish_job *job) { + llarp::Info("llarp_router::on_try_connect_result"); llarp_router *router = static_cast< llarp_router * >(job->user); if(job->session) { @@ -582,6 +602,29 @@ llarp_router::DiscardOutboundFor(const llarp::RouterID &remote) outboundMesssageQueue.erase(remote); } +void +llarp_router::resolve_job_type(llarp_rc *rc, llarp_link_establish_job *establish_job) +{ + char ftmp[68] = {0}; + const char *hexPubSigKey = + llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp); + + llarp::Info("is this a outbound or inbound job? ", hexPubSigKey); + llarp::Info("RC status: ", !llarp_rc_is_public_router(rc)?"private":"public"); + + auto it = pendingEstablishJobs.find(rc->pubkey); + if (it != pendingEstablishJobs.end()) + { + llarp::Info("in outbound pending queue"); + async_verify_RC(rc, false, establish_job); + } + else + { + llarp::Info("likely inbound connection"); + async_verify_RC(rc, true, establish_job); + } +} + void llarp_router::async_verify_RC(llarp_rc *rc, bool isExpectingClient, llarp_link_establish_job *establish_job) diff --git a/llarp/router.hpp b/llarp/router.hpp index 2b59a7ab7..c9a8d8efd 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -193,6 +193,10 @@ struct llarp_router llarp_link * GetLinkWithSessionByPubkey(const llarp::RouterID &remote); + void + resolve_job_type(llarp_rc *rc, + llarp_link_establish_job *establish_job = nullptr); + void async_verify_RC(llarp_rc *rc, bool isExpectingClient, llarp_link_establish_job *job = nullptr); From 9aa5163c8187c2f55ad236b57b2e30f3be259712 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 4 Jul 2018 05:09:44 -0700 Subject: [PATCH 04/21] use resolve_job_type() instead of public --- llarp/link_intro.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llarp/link_intro.cpp b/llarp/link_intro.cpp index 38d394d1a..911b87800 100644 --- a/llarp/link_intro.cpp +++ b/llarp/link_intro.cpp @@ -72,7 +72,8 @@ namespace llarp bool LinkIntroMessage::HandleMessage(llarp_router* router) const { - router->async_verify_RC(RC, !llarp_rc_is_public_router(RC)); + //router->async_verify_RC(RC, true); + router->resolve_job_type(RC); return true; } } // namespace llarp From d7fc0f83a73c0d0b2572e78130d6efed1115378e Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 4 Jul 2018 14:01:20 -0700 Subject: [PATCH 05/21] Revert "use resolve_job_type() instead of public" This reverts commit 9aa5163c8187c2f55ad236b57b2e30f3be259712. --- llarp/link_intro.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llarp/link_intro.cpp b/llarp/link_intro.cpp index 911b87800..38d394d1a 100644 --- a/llarp/link_intro.cpp +++ b/llarp/link_intro.cpp @@ -72,8 +72,7 @@ namespace llarp bool LinkIntroMessage::HandleMessage(llarp_router* router) const { - //router->async_verify_RC(RC, true); - router->resolve_job_type(RC); + router->async_verify_RC(RC, !llarp_rc_is_public_router(RC)); return true; } } // namespace llarp From 938cfec657aea8da9ce11e0aa1d3892b34bab5b3 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 4 Jul 2018 14:01:32 -0700 Subject: [PATCH 06/21] Revert "resolve_job_type() and on_verify_client_rc() clean u" This reverts commit 9805cc153f1d956b7d065a5bbfb17d95db77695c. --- llarp/router.cpp | 51 ++++-------------------------------------------- llarp/router.hpp | 4 ---- 2 files changed, 4 insertions(+), 51 deletions(-) diff --git a/llarp/router.cpp b/llarp/router.cpp index fc5422733..e0bd3d539 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -304,25 +304,8 @@ llarp_router::on_verify_client_rc(llarp_async_verify_rc *job) llarp::async_verify_context *ctx = static_cast< llarp::async_verify_context * >(job->user); llarp::PubKey pk = job->rc.pubkey; - if(!job->valid) - { - llarp::Warn("invalid inbound RC"); - llarp_rc_free(&job->rc); - ctx->router->pendingEstablishJobs.erase(pk); - delete ctx; - return; - } - // we're valid, which means it's already been committed to the nodedb - llarp::Info("inbound connection: rc verified and saved to nodedb"); - - auto router = ctx->router; - router->validRouters[pk] = job->rc; - llarp::Info("putting valid router into dht ", pk); - // track valid router in dht - llarp_dht_put_peer(router->dht, &router->validRouters[pk]); - llarp_rc_free(&job->rc); - router->pendingEstablishJobs.erase(pk); + ctx->router->pendingEstablishJobs.erase(pk); delete ctx; } @@ -335,7 +318,7 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) llarp::PubKey pk(job->rc.pubkey); if(!job->valid) { - llarp::Warn("invalid outbound RC"); + llarp::Warn("invalid server RC"); if(ctx->establish_job) { // was an outbound attempt @@ -350,7 +333,7 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) } // we're valid, which means it's already been committed to the nodedb - llarp::Info("rc verified and saved to nodedb"); + llarp::Debug("rc verified and saved to nodedb"); // refresh valid routers RC value if it's there auto v = router->validRouters.find(pk); @@ -360,7 +343,6 @@ llarp_router::on_verify_server_rc(llarp_async_verify_rc *job) llarp_rc_free(&v->second); } router->validRouters[pk] = job->rc; - llarp::Info("putting valid router into dht ", pk); // track valid router in dht llarp_dht_put_peer(router->dht, &router->validRouters[pk]); @@ -399,13 +381,12 @@ llarp_router::Tick() { llarp::Debug("tick router"); paths.ExpirePaths(); - // why is it important we have no inboundLinks + // TODO: don't do this if we have enough paths already if(inboundLinks.size() == 0) { auto N = llarp_nodedb_num_loaded(nodedb); if(N > 2) { - // TODO: don't do this if we have enough paths already paths.BuildPaths(); } else @@ -560,7 +541,6 @@ llarp_router::FlushOutboundFor(const llarp::RouterID &remote, void llarp_router::on_try_connect_result(llarp_link_establish_job *job) { - llarp::Info("llarp_router::on_try_connect_result"); llarp_router *router = static_cast< llarp_router * >(job->user); if(job->session) { @@ -602,29 +582,6 @@ llarp_router::DiscardOutboundFor(const llarp::RouterID &remote) outboundMesssageQueue.erase(remote); } -void -llarp_router::resolve_job_type(llarp_rc *rc, llarp_link_establish_job *establish_job) -{ - char ftmp[68] = {0}; - const char *hexPubSigKey = - llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp); - - llarp::Info("is this a outbound or inbound job? ", hexPubSigKey); - llarp::Info("RC status: ", !llarp_rc_is_public_router(rc)?"private":"public"); - - auto it = pendingEstablishJobs.find(rc->pubkey); - if (it != pendingEstablishJobs.end()) - { - llarp::Info("in outbound pending queue"); - async_verify_RC(rc, false, establish_job); - } - else - { - llarp::Info("likely inbound connection"); - async_verify_RC(rc, true, establish_job); - } -} - void llarp_router::async_verify_RC(llarp_rc *rc, bool isExpectingClient, llarp_link_establish_job *establish_job) diff --git a/llarp/router.hpp b/llarp/router.hpp index c9a8d8efd..2b59a7ab7 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -193,10 +193,6 @@ struct llarp_router llarp_link * GetLinkWithSessionByPubkey(const llarp::RouterID &remote); - void - resolve_job_type(llarp_rc *rc, - llarp_link_establish_job *establish_job = nullptr); - void async_verify_RC(llarp_rc *rc, bool isExpectingClient, llarp_link_establish_job *job = nullptr); From 425a486d848f70da945dda694eed6dfeb82d802c Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:22:02 -0700 Subject: [PATCH 07/21] irelay the pending->job as the current job when checking the next node, add iterate flag to job, potential fix for possible problem, debug --- daemon.ini | 11 +++++++---- include/llarp/dht.h | 2 ++ llarp/dht.cpp | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/daemon.ini b/daemon.ini index 79b371ef6..b639dd960 100644 --- a/daemon.ini +++ b/daemon.ini @@ -3,16 +3,19 @@ worker-threads=8 net-threads=2 contact-file=router.signed ident-privkey=server-ident.key -#public-address=0.0.0.0 +#public-address=192.168.10.253 +public-address=206.81.102.81 #public-port=1090 +public-ifname=en0 [netdb] dir=./tmp-nodes [connect] -#i2p.rocks=i2p.rocks.signed.txt +i2p.rocks=i2p.rocks.signed.txt #named-node1=/path/to/routercontact1.signed +napanet100_183_ipv5=napanet100_183_ipv4_v5.signed [bind] -#en0=1090 -*=1090 +en0=1090 +#*=1090 diff --git a/include/llarp/dht.h b/include/llarp/dht.h index 6f4db8635..f7b8d7820 100644 --- a/include/llarp/dht.h +++ b/include/llarp/dht.h @@ -53,7 +53,9 @@ struct llarp_router_lookup_job struct llarp_dht_context* dht; byte_t target[PUBKEYSIZE]; bool found; + // make sure you initialize addr and exits struct llarp_rc result; + bool iterative; }; /// start allowing dht participation on a context diff --git a/llarp/dht.cpp b/llarp/dht.cpp index 1cedb6ff9..492531874 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -136,7 +136,7 @@ namespace llarp std::vector< IMessage * > &replies) const { auto &dht = router->dht->impl; - auto pending = dht.FindPendingTX(From, txid); + SearchJob *pending = dht.FindPendingTX(From, txid); if(pending) { if(R.size()) @@ -160,8 +160,9 @@ namespace llarp llarp::Info(pending->target, " was not found via ", From, " iterating to next peer ", nextPeer, " already asked ", pending->exclude.size(), " other peers"); + // REVIEW: is this ok to relay the pending->job as the current job (seems to make things work) dht.LookupRouter(pending->target, pending->requester, - pending->requesterTX, nextPeer, nullptr, true, + pending->requesterTX, nextPeer, pending->job, true, pending->exclude); } else @@ -465,6 +466,7 @@ namespace llarp std::set< Key_t > excluding = {requester, ourKey}; if(nodes->FindCloseExcluding(target, next, excluding)) { + llarp::Info("LookupRouterRelayed tick"); if(next == target) { // we know it @@ -484,6 +486,10 @@ namespace llarp else { // yeah, ask neighboor recursively + llarp::Info("asking neighbor recursively"); + // FIXME: we may need to pass a job here... + //auto sj = FindPendingTX(requester, txid); + //LookupRouter(target, requester, txid, next, sj->job); LookupRouter(target, requester, txid, next); } } @@ -605,8 +611,17 @@ namespace llarp Context::LookupRouterViaJob(llarp_router_lookup_job *job) { Key_t peer; + /* + llarp::Info("LookupRouterViaJob dumping nodes"); + for(const auto &item : nodes->nodes) + { + llarp::Info("LookupRouterViaJob dumping node: ", item.first); + } + */ + llarp::Info("LookupRouterViaJob node count: ", nodes->nodes.size()); + llarp::Info("LookupRouterViaJob recursive: ", job->iterative?"yes":"no"); if(nodes->FindClosest(job->target, peer)) - LookupRouter(job->target, ourKey, 0, peer, job); + LookupRouter(job->target, ourKey, 0, peer, job, job->iterative); else if(job->hook) { job->found = false; @@ -648,6 +663,7 @@ llarp_dht_put_peer(struct llarp_dht_context *ctx, struct llarp_rc *rc) { llarp::dht::RCNode n(rc); + llarp::Debug("Adding ", n.ID, " to DHT"); ctx->impl.nodes->PutNode(n); } @@ -655,6 +671,7 @@ void llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id) { llarp::dht::Key_t k = id; + llarp::Debug("Removing ", k, " to DHT"); ctx->impl.nodes->DelNode(k); } From 016da4b7aac1e15d0f0dc78c8d10195db5de519a Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:24:53 -0700 Subject: [PATCH 08/21] remove unneeded debug --- llarp/dht.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llarp/dht.cpp b/llarp/dht.cpp index 492531874..d58bb7e92 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -466,7 +466,7 @@ namespace llarp std::set< Key_t > excluding = {requester, ourKey}; if(nodes->FindCloseExcluding(target, next, excluding)) { - llarp::Info("LookupRouterRelayed tick"); + //llarp::Info("LookupRouterRelayed tick"); if(next == target) { // we know it From 3a21ca604fddbf0ab6ccdfab6e6fc972d4eae493 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:26:24 -0700 Subject: [PATCH 09/21] llarp_rc_new to initialize properties correctly --- include/llarp/router_contact.h | 4 ++++ llarp/router_contact.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index f9b3e12ca..ddd7b712e 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -47,6 +47,10 @@ struct llarp_rc void llarp_rc_free(struct llarp_rc *rc); +bool +llarp_rc_new(struct llarp_rc *rc); + + bool llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc); diff --git a/llarp/router_contact.cpp b/llarp/router_contact.cpp index d144164e0..2e3b3b4e4 100644 --- a/llarp/router_contact.cpp +++ b/llarp/router_contact.cpp @@ -6,6 +6,16 @@ #include "logger.hpp" extern "C" { + +bool +llarp_rc_new(struct llarp_rc *rc) +{ + rc->addrs = llarp_ai_list_new(); + rc->exits = llarp_xi_list_new(); + rc->last_updated = 0; + return true; +} + void llarp_rc_free(struct llarp_rc *rc) { From 840a50abbcd1eb327681a4a6387b7da8216a9d91 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:28:10 -0700 Subject: [PATCH 10/21] don't skip private interfaces quite yet/we need them for NAT to work correctly, try_connect refactor clean up --- llarp/router.cpp | 54 ++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/llarp/router.cpp b/llarp/router.cpp index e0bd3d539..98135cbdc 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -163,49 +163,25 @@ 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; - llarp::StackBuffer< decltype(tmp) >(buf, tmp); - // open file + llarp_rc *remote = new llarp_rc; + llarp_rc_new(remote); + remote = llarp_rc_read(rcfile.c_str()); + if (!remote) { - std::ifstream f(rcfile, std::ios::binary); - if(f.is_open()) - { - f.seekg(0, std::ios::end); - size_t sz = f.tellg(); - f.seekg(0, std::ios::beg); - if(sz <= buf.sz) - { - f.read((char *)buf.base, sz); - } - else - llarp::Error(rcfile, " too large"); - } - else - { - llarp::Error("failed to open ", rcfile); - return; - } + llarp::Error("aborting verification of remote RC"); + return; } - if(llarp_rc_bdecode(&remote, &buf)) + if(llarp_rc_verify_sig(&crypto, remote)) { - if(llarp_rc_verify_sig(&crypto, &remote)) + llarp::Debug("verified signature"); + if(!llarp_router_try_connect(this, remote, 10)) { - llarp::Debug("verified signature"); - if(!llarp_router_try_connect(this, &remote, 10)) - { - llarp::Warn("session already made"); - } + llarp::Warn("session already made"); } - else - llarp::Error("failed to verify signature of RC", rcfile); } else - llarp::Error("failed to decode RC"); - - llarp_rc_free(&remote); + llarp::Error("failed to verify signature of RC", rcfile); + llarp_rc_free(remote); } bool @@ -265,6 +241,7 @@ llarp_router::SaveRC() void llarp_router::Close() { + llarp::Info("Closing ", inboundLinks.size(), " server bindings"); for(auto link : inboundLinks) { link->stop_link(link); @@ -273,6 +250,7 @@ llarp_router::Close() } inboundLinks.clear(); + llarp::Info("Closing LokiNetwork client"); outboundLink->stop_link(outboundLink); outboundLink->free_impl(outboundLink); delete outboundLink; @@ -642,8 +620,8 @@ llarp_router::Run() } if(a.isPrivate()) { - llarp::Warn("Skipping private network link: ", a); - continue; + //llarp::Warn("Skipping private network link: ", a); + //continue; } llarp::Info("Loading Addr: ", a, " into our RC"); From 961f1a858b15e3e365bbbdbbf142e33fa99e34c3 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:29:29 -0700 Subject: [PATCH 11/21] displayRC refactor, --locate show RC if found, --LogLevel support, make --locate lookups iterative, fix short option arguments --- daemon/rcutil.cpp | 102 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 2ef103a63..02eadf091 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -2,14 +2,25 @@ #include #include "logger.hpp" +#include +#include +#include +#include +#include "buffer.hpp" +#include "crypto.hpp" +#include "fs.hpp" +#include "net.hpp" +#include "router.hpp" +#include + struct llarp_main *ctx = 0; llarp_main *sllarp = nullptr; +//extern char *optarg; void handle_signal(int sig) { - printf("rcutil::handle_signal [%x] got [%d]\n", ctx, sig); if(ctx) llarp_main_signal(ctx, sig); } @@ -18,16 +29,6 @@ handle_signal(int sig) #define TESTNET 0 #endif -#include -#include -#include -#include -#include "buffer.hpp" -#include "crypto.hpp" -#include "fs.hpp" -#include "net.hpp" -#include "router.hpp" - bool printNode(struct llarp_nodedb_iter *iter) { @@ -39,6 +40,29 @@ printNode(struct llarp_nodedb_iter *iter) return false; } +bool +aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr) +{ + static size_t count = 0; + count++; + llarp::Addr a(*addr); + std::cout << "AddressInfo " << count << ": " << a << std::endl; + return true; +} + +void displayRC(llarp_rc *rc) +{ + char ftmp[68] = {0}; + const char *hexPubSigKey = + llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp); + printf("PubSigKey [%s]\n", hexPubSigKey); + + struct llarp_ai_list_iter iter; + // iter.user + iter.visit = &aiLister; + llarp_ai_list_iterate(rc->addrs, &iter); +} + // fwd declr struct check_online_request; @@ -49,6 +73,7 @@ HandleDHTLocate(llarp_router_lookup_job *job) if (job->found) { // save to nodedb? + displayRC(&job->result); } // shutdown router @@ -63,20 +88,11 @@ HandleDHTLocate(llarp_router_lookup_job *job) llarp_main_abort(ctx); } -bool -aiLister(struct llarp_ai_list_iter *request, struct llarp_ai *addr) -{ - static size_t count = 0; - count++; - llarp::Addr a(*addr); - std::cout << "AddressInfo " << count << ": " << a << std::endl; - return true; -} - int main(int argc, char *argv[]) { // take -c to set location of daemon.ini + // take -o to set log level // --generate-blank /path/to/file.signed // --update-ifs /path/to/file.signed // --key /path/to/long_term_identity.key @@ -125,11 +141,13 @@ main(int argc, char *argv[]) {"export", required_argument, 0, 'e'}, {"locate", required_argument, 0, 'q'}, {"localInfo", no_argument, 0, 'n'}, + {"logLevel", required_argument, 0, 'o'}, {0, 0, 0, 0}}; int option_index = 0; - c = getopt_long(argc, argv, "cgluieqn", long_options, &option_index); + c = getopt_long(argc, argv, "c:g:lu:i:e:q:no:", long_options, &option_index); if(c == -1) break; + std::string mode; switch(c) { case 0: @@ -137,6 +155,34 @@ main(int argc, char *argv[]) case 'c': conffname = optarg; break; + case 'o': + //mode = optarg; + //llarp::Info("opt[", mode, "]"); + if (!optarg) + { + llarp::Info("No parameter given to logLevel"); + continue; + } + if (strncmp(optarg, "debug", std::min(strlen(optarg), static_cast(5)))==0) + { + llarp::SetLogLevel(llarp::eLogDebug); + } + else + if (strncmp(optarg, "info", std::min(strlen(optarg), static_cast(4)))==0) + { + llarp::SetLogLevel(llarp::eLogInfo); + } + else + if (strncmp(optarg, "warn", std::min(strlen(optarg), static_cast(4)))==0) + { + llarp::SetLogLevel(llarp::eLogWarn); + } + else + if (strncmp(optarg, "error", std::min(strlen(optarg), static_cast(5)))==0) + { + llarp::SetLogLevel(llarp::eLogError); + } + break; case 'l': haveRequiredOptions = true; listMode = true; @@ -313,8 +359,10 @@ main(int argc, char *argv[]) llarp::Info("Queueing job"); llarp_router_lookup_job *job = new llarp_router_lookup_job; + job->iterative = true; job->found = false; job->hook = &HandleDHTLocate; + llarp_rc_new(&job->result); memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target // create query DHT request @@ -336,15 +384,7 @@ main(int argc, char *argv[]) // llarp_rc *rc = llarp_rc_read("router.signed"); llarp_rc *rc = llarp_main_getLocalRC(ctx); - char ftmp[68] = {0}; - const char *hexPubSigKey = - llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(rc->pubkey, ftmp); - printf("PubSigKey [%s]\n", hexPubSigKey); - - struct llarp_ai_list_iter iter; - // iter.user - iter.visit = &aiLister; - llarp_ai_list_iterate(rc->addrs, &iter); + displayRC(rc); } // it's a unique_ptr, should clean up itself //llarp_main_free(ctx); From 37d9e774a3b3cc2a25f069397bca4b97854521ba Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Sun, 8 Jul 2018 06:33:51 -0700 Subject: [PATCH 12/21] clean up --- daemon/rcutil.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 02eadf091..9b886acc9 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -11,12 +11,10 @@ #include "fs.hpp" #include "net.hpp" #include "router.hpp" -#include struct llarp_main *ctx = 0; llarp_main *sllarp = nullptr; -//extern char *optarg; void handle_signal(int sig) @@ -147,7 +145,6 @@ main(int argc, char *argv[]) c = getopt_long(argc, argv, "c:g:lu:i:e:q:no:", long_options, &option_index); if(c == -1) break; - std::string mode; switch(c) { case 0: @@ -156,13 +153,6 @@ main(int argc, char *argv[]) conffname = optarg; break; case 'o': - //mode = optarg; - //llarp::Info("opt[", mode, "]"); - if (!optarg) - { - llarp::Info("No parameter given to logLevel"); - continue; - } if (strncmp(optarg, "debug", std::min(strlen(optarg), static_cast(5)))==0) { llarp::SetLogLevel(llarp::eLogDebug); @@ -355,8 +345,6 @@ main(int argc, char *argv[]) llarp::PubKey binaryPK; llarp::HexDecode(rcfname, binaryPK.data()); - //llarp::SetLogLevel(llarp::eLogDebug); - llarp::Info("Queueing job"); llarp_router_lookup_job *job = new llarp_router_lookup_job; job->iterative = true; @@ -380,9 +368,6 @@ main(int argc, char *argv[]) } if(localMode) { - // llarp::Info("find our local rc file"); - - // llarp_rc *rc = llarp_rc_read("router.signed"); llarp_rc *rc = llarp_main_getLocalRC(ctx); displayRC(rc); } From 86db6256b892251438ef8a18a5352d6cd6670ae0 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 11 Jul 2018 04:04:15 -0700 Subject: [PATCH 13/21] support -c and -o --- daemon/main.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/daemon/main.c b/daemon/main.c index b1c0c9eb6..462f8f3a7 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1,5 +1,8 @@ #include #include +#include +#include +#include // for MIN struct llarp_main *ctx = 0; @@ -18,15 +21,57 @@ int main(int argc, char *argv[]) { const char *conffname = "daemon.ini"; - if(argc > 1) - conffname = argv[1]; + int c; + while(1) + { + static struct option long_options[] = { + {"config", required_argument, 0, 'c'}, + {"logLevel", required_argument, 0, 'o'}, + {0, 0, 0, 0}}; + int option_index = 0; + c = getopt_long(argc, argv, "c:o:", long_options, &option_index); + if(c == -1) + break; + switch(c) + { + case 0: + break; + case 'c': + conffname = optarg; + break; + case 'o': + if (strncmp(optarg, "debug", MIN(strlen(optarg), (unsigned long)5))==0) + { + cSetLogLevel(eLogDebug); + } + else + if (strncmp(optarg, "info", MIN(strlen(optarg), (unsigned long)4))==0) + { + cSetLogLevel(eLogInfo); + } + else + if (strncmp(optarg, "warn", MIN(strlen(optarg), (unsigned long)4))==0) + { + cSetLogLevel(eLogWarn); + } + else + if (strncmp(optarg, "error", MIN(strlen(optarg), (unsigned long)5))==0) + { + cSetLogLevel(eLogError); + } + break; + default: + abort(); + } + } + ctx = llarp_main_init(conffname, !TESTNET); int code = 1; if(ctx) { signal(SIGINT, handle_signal); code = llarp_main_run(ctx); - llarp_main_free(ctx); + //llarp_main_free(ctx); } return code; } From ccc6300849a5375132dad112ff894eccba7be231 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Wed, 11 Jul 2018 04:04:45 -0700 Subject: [PATCH 14/21] logLevel and read support --- daemon/rcutil.cpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 9b886acc9..a889d7cec 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -104,12 +104,14 @@ main(int argc, char *argv[]) { printf( "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 a hex formatted public key\n" - "--locate a hex formatted public key" + "--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 a hex formatted public key\n" + "--locate a hex formatted public key" + "--localInfo \n" + "--read with a path to a router contact file\n" "\n"); return 0; } @@ -120,6 +122,7 @@ main(int argc, char *argv[]) bool exportMode = false; bool locateMode = false; bool localMode = false; + bool readMode = false; int c; char *conffname; char defaultConfName[] = "daemon.ini"; @@ -132,6 +135,7 @@ main(int argc, char *argv[]) { static struct option long_options[] = { {"config", required_argument, 0, 'c'}, + {"logLevel", required_argument, 0, 'o'}, {"generate", required_argument, 0, 'g'}, {"update", required_argument, 0, 'u'}, {"list", no_argument, 0, 'l'}, @@ -139,10 +143,10 @@ main(int argc, char *argv[]) {"export", required_argument, 0, 'e'}, {"locate", required_argument, 0, 'q'}, {"localInfo", no_argument, 0, 'n'}, - {"logLevel", required_argument, 0, 'o'}, + {"read", required_argument, 0, 'r'}, {0, 0, 0, 0}}; int option_index = 0; - c = getopt_long(argc, argv, "c:g:lu:i:e:q:no:", long_options, &option_index); + c = getopt_long(argc, argv, "c:o:g:lu:i:e:q:nr:", long_options, &option_index); if(c == -1) break; switch(c) @@ -211,6 +215,11 @@ main(int argc, char *argv[]) haveRequiredOptions = true; localMode = true; break; + case 'r': + rcfname = optarg; + haveRequiredOptions = true; + readMode = true; + break; default: abort(); } @@ -222,7 +231,7 @@ main(int argc, char *argv[]) } printf("parsed options\n"); if(!genMode && !updMode && !listMode && !importMode && !exportMode - && !locateMode && !localMode) + && !locateMode && !localMode && !readMode) { llarp::Error("I don't know what to do, no generate or update parameter\n"); return 0; @@ -371,6 +380,11 @@ main(int argc, char *argv[]) llarp_rc *rc = llarp_main_getLocalRC(ctx); displayRC(rc); } + if(readMode) + { + llarp_rc *rc = llarp_rc_read(rcfname); + displayRC(rc); + } // it's a unique_ptr, should clean up itself //llarp_main_free(ctx); return 1; // success From acec52a2c73c5795afda35b3835e0cacae88f3a5 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 12 Jul 2018 11:46:30 -0400 Subject: [PATCH 15/21] update spec --- doc/dht_v0.txt | 25 +++---------------------- doc/proto_v0.txt | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/doc/dht_v0.txt b/doc/dht_v0.txt index beddfb2c5..215c3fb70 100644 --- a/doc/dht_v0.txt +++ b/doc/dht_v0.txt @@ -11,6 +11,7 @@ recursively find an IS by SA { A: "F", + I: 0 or 1 if iterative request, R: r_counter, S: "<32 bytes SA>", T: transaction_id_uint64, @@ -123,33 +124,13 @@ find a router by long term RC.k public key { A: "R", + I: 0 or 1 if iterative lookup K: "<32 byte public key of router>", T: transaction_id_uint64, V: 0 } -find RC who's RC.k is closest to K: - -if A.k is equal to K: - -* reply with a GRCM with an R value of just A - -if A.k is not equal to K and we are closesr to A.k than anyone we know: - -* reply with a GRCM with an empty R value - -find a pending transaction id for K, P - -if P exists: - -* link transaction T to P - -if P does not exist: - -* generate a new transaction id, U -* start transaction U for A.k -* link transaction U to transaction T -* send FRCM to A.k requesting K +TODO: document me got router contact message (GRCM) diff --git a/doc/proto_v0.txt b/doc/proto_v0.txt index a9e552e79..4c875f91f 100644 --- a/doc/proto_v0.txt +++ b/doc/proto_v0.txt @@ -516,20 +516,43 @@ B is set to a backoff value. R contains additional metadata text describing why the exit was rejected. +hidden service frame message (HSFM) -hidden service data message (HSDM) +TODO: document this better -signed data sent anonymously over the network to a recipiant from a sender. -sent inside a TDFM encrypted to the hidden service's public encryption key. +intro message (variant 1) { A: "H", - H: "", - I: Introducer for reply, - R: SA of recipiant, - S: SI of sender, + D: "", + H: "<32 bytes ephemeral public encryption key>", + N: "<32 bytes nonce for key exchange>", + S: 0, + V: 0, + Z: "<64 bytes signature of entire message using sender's signing key>" +} + +ordered data message (variant 2) + +{ + A: "H", + D: "", + N: "<32 bytes nonce for symettric cipher>", + S: sequence_number_uint64, V: 0, - Z: "<64 bytes signature from sender of the entire message>" + Z: "<64 bytes signature using sender's signing key>" +} + +hidden service data (HSD) + +data sent anonymously over the network to a recipiant from a sender. +sent inside a HSFM encrypted with a shared secret. + +{ + D: "", + I: Introduction for reply, + S: SI of sender, + V: 0 } transfer data fragment message (TDFM) @@ -540,11 +563,10 @@ transfer data between paths. A: "T", P: "<16 bytes path id>", T: "", - V: 0, - Y: "<32 bytes nounce>" + V: 0 } -transfer data to another path with id P on the local router place Y and T values +transfer data to another path with id P on the local router place a random 32 byte and T values into y and z values into a LRDM message (respectively) and send it in the downstream direction. From 5bbaae4e2e66328a57135cd397beacb02b0ed24b Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 12 Jul 2018 14:21:44 -0400 Subject: [PATCH 16/21] more --- CMakeLists.txt | 1 + doc/proto_v0.txt | 4 +- include/llarp.h | 4 -- include/llarp.hpp | 17 ++++--- include/llarp/dht.h | 13 ----- include/llarp/dht/context.hpp | 9 +++- include/llarp/dht/messages/findintro.hpp | 7 +-- include/llarp/path.hpp | 6 --- include/llarp/pathbuilder.h | 2 +- include/llarp/pathbuilder.hpp | 2 +- include/llarp/pathset.hpp | 16 +++++++ include/llarp/routing/handler.hpp | 6 ++- include/llarp/service/address.hpp | 10 ++++ include/llarp/service/endpoint.hpp | 60 ++++++++++++++++++++++++ llarp/context.cpp | 11 ----- llarp/dht.cpp | 7 --- llarp/dht/find_intro.cpp | 11 +++++ llarp/path.cpp | 7 --- llarp/pathbuilder.cpp | 9 ++-- llarp/router.cpp | 1 - llarp/service/endpoint.cpp | 24 ++++++++-- llarp/transit_hop.cpp | 7 --- 22 files changed, 151 insertions(+), 83 deletions(-) create mode 100644 llarp/dht/find_intro.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 89f9bd4c1..d869b34cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,7 @@ set(LIB_SRC llarp/dht/context.cpp llarp/dht/decode.cpp llarp/dht/dht_immediate.cpp + llarp/dht/find_intro.cpp llarp/dht/find_router.cpp llarp/dht/got_intro.cpp llarp/dht/got_router.cpp diff --git a/doc/proto_v0.txt b/doc/proto_v0.txt index 4c875f91f..35a34249f 100644 --- a/doc/proto_v0.txt +++ b/doc/proto_v0.txt @@ -516,7 +516,7 @@ B is set to a backoff value. R contains additional metadata text describing why the exit was rejected. -hidden service frame message (HSFM) +hidden service frame (HSF) TODO: document this better @@ -562,7 +562,7 @@ transfer data between paths. { A: "T", P: "<16 bytes path id>", - T: "", + T: message_transfered_between_paths, V: 0 } diff --git a/include/llarp.h b/include/llarp.h index 87338f9fd..f8ec9a1f9 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -23,10 +23,6 @@ llarp_main_init(const char *fname, bool multiProcess); void llarp_main_signal(struct llarp_main *ptr, int sig); -/// set custom dht message handler function -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); diff --git a/include/llarp.hpp b/include/llarp.hpp index 2f208cb51..e62434de4 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -18,15 +18,14 @@ namespace llarp bool singleThreaded = false; std::vector< std::thread > netio_threads; llarp_crypto crypto; - llarp_router *router = nullptr; - llarp_threadpool *worker = nullptr; - llarp_logic *logic = nullptr; - llarp_config *config = nullptr; - llarp_nodedb *nodedb = nullptr; - llarp_ev_loop *mainloop = nullptr; - llarp_dht_msg_handler custom_dht_func = nullptr; - char nodedb_dir[256] = {0}; - char conatctFile[256] = "router.signed"; + llarp_router *router = nullptr; + llarp_threadpool *worker = nullptr; + llarp_logic *logic = nullptr; + llarp_config *config = nullptr; + llarp_nodedb *nodedb = nullptr; + llarp_ev_loop *mainloop = nullptr; + char nodedb_dir[256] = {0}; + char conatctFile[256] = "router.signed"; bool LoadConfig(const std::string &fname); diff --git a/include/llarp/dht.h b/include/llarp/dht.h index 5b06c0611..71f143193 100644 --- a/include/llarp/dht.h +++ b/include/llarp/dht.h @@ -20,23 +20,10 @@ llarp_dht_context_new(struct llarp_router* parent); void llarp_dht_context_free(struct llarp_dht_context* dht); -struct llarp_dht_msg; - -/// handler function -/// f(outmsg, inmsg) -/// returns true if outmsg has been filled otherwise returns false -typedef bool (*llarp_dht_msg_handler)(struct llarp_dht_msg*, - struct llarp_dht_msg*); - /// start dht context with our location in keyspace void llarp_dht_context_start(struct llarp_dht_context* ctx, const byte_t* key); -// override dht message handler with custom handler -void -llarp_dht_set_msg_handler(struct llarp_dht_context* ctx, - llarp_dht_msg_handler func); - struct llarp_router_lookup_job; typedef void (*llarp_router_lookup_handler)(struct llarp_router_lookup_job*); diff --git a/include/llarp/dht/context.hpp b/include/llarp/dht/context.hpp index 475337ee7..c98ea79b1 100644 --- a/include/llarp/dht/context.hpp +++ b/include/llarp/dht/context.hpp @@ -20,14 +20,19 @@ namespace llarp Context(); ~Context(); - llarp_dht_msg_handler custom_handler = nullptr; - SearchJob* FindPendingTX(const Key_t& owner, uint64_t txid); void RemovePendingLookup(const Key_t& owner, uint64_t txid); + void + LookupServiceDirect(const Key_t& target, const Key_t& whoasked, + uint64_t whoaskedTX, const Key_t& askpeer, + SearchJob::IntroSetHookFunc handler, + bool iterateive = false, + std::set< Key_t > excludes = {}); + void LookupRouter(const Key_t& target, const Key_t& whoasked, uint64_t whoaskedTX, const Key_t& askpeer, diff --git a/include/llarp/dht/messages/findintro.hpp b/include/llarp/dht/messages/findintro.hpp index 7c1e8f58e..35719643f 100644 --- a/include/llarp/dht/messages/findintro.hpp +++ b/include/llarp/dht/messages/findintro.hpp @@ -9,7 +9,8 @@ namespace llarp { struct FindIntroMessage : public IMessage { - uint64_t R = 0; + uint64_t R = 0; + bool iterative = false; llarp::service::Address S; uint64_t T = 0; @@ -25,6 +26,6 @@ namespace llarp HandleMessage(llarp_dht_context* ctx, std::vector< IMessage* >& replies) const; }; - } -} + } // namespace dht +} // namespace llarp #endif \ No newline at end of file diff --git a/include/llarp/path.hpp b/include/llarp/path.hpp index 6ea399759..fcfada9c8 100644 --- a/include/llarp/path.hpp +++ b/include/llarp/path.hpp @@ -165,9 +165,6 @@ namespace llarp bool HandleDHTMessage(const llarp::dht::IMessage* msg, llarp_router* r); - bool - HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r); - // handle data in upstream direction bool HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r); @@ -246,9 +243,6 @@ namespace llarp bool HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r); - bool - HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r); - // handle data in upstream direction bool HandleUpstream(llarp_buffer_t X, const TunnelNonce& Y, llarp_router* r); diff --git a/include/llarp/pathbuilder.h b/include/llarp/pathbuilder.h index 4b4204a5c..89af36666 100644 --- a/include/llarp/pathbuilder.h +++ b/include/llarp/pathbuilder.h @@ -20,7 +20,7 @@ struct llarp_pathbuilder_context; /// alloc struct llarp_pathbuilder_context* llarp_pathbuilder_context_new(struct llarp_router* router, - struct llarp_dht_context* dht); + struct llarp_dht_context* dht, size_t numpaths); /// dealloc void llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx); diff --git a/include/llarp/pathbuilder.hpp b/include/llarp/pathbuilder.hpp index 80704249a..618454288 100644 --- a/include/llarp/pathbuilder.hpp +++ b/include/llarp/pathbuilder.hpp @@ -10,7 +10,7 @@ struct llarp_pathbuilder_context : public llarp::path::PathSet struct llarp_dht_context* dht; /// construct llarp_pathbuilder_context(llarp_router* p_router, - struct llarp_dht_context* p_dht); + struct llarp_dht_context* p_dht, size_t numPaths); virtual ~llarp_pathbuilder_context(){}; diff --git a/include/llarp/pathset.hpp b/include/llarp/pathset.hpp index d0155abbf..b7580c457 100644 --- a/include/llarp/pathset.hpp +++ b/include/llarp/pathset.hpp @@ -1,6 +1,7 @@ #ifndef LLARP_PATHSET_HPP #define LLARP_PATHSET_HPP #include +#include #include #include #include @@ -81,6 +82,18 @@ namespace llarp PublishIntroSet(const llarp::service::IntroSet& introset, llarp_router* r); + typedef std::function< void(const llarp::service::IntroSet*) > + ServiceLookupHandler; + + /// return false if we are already pending a lookup for this address + bool + LookupService(const llarp::service::Address& addr, + ServiceLookupHandler handler); + + protected: + void + IssueServiceLookup(const llarp::service::Address& addr); + private: typedef std::pair< RouterID, PathID_t > PathInfo_t; typedef std::map< PathInfo_t, Path* > PathMap_t; @@ -88,6 +101,9 @@ namespace llarp size_t m_NumPaths; PathMap_t m_Paths; uint64_t m_CurrentPublishTX = 0; + std::unordered_map< llarp::service::Address, ServiceLookupHandler, + llarp::service::Address::Hash > + m_ServiceLookups; }; } // namespace path diff --git a/include/llarp/routing/handler.hpp b/include/llarp/routing/handler.hpp index e11ef4fda..02069953f 100644 --- a/include/llarp/routing/handler.hpp +++ b/include/llarp/routing/handler.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +21,10 @@ namespace llarp llarp_router *r) = 0; virtual bool - HandleHiddenServiceData(llarp_buffer_t buf, llarp_router *r) = 0; + HandleHiddenServiceFrame(const HiddenServiceFrame *msg) + { + return false; + } virtual bool HandlePathConfirmMessage(const PathConfirmMessage *msg, diff --git a/include/llarp/service/address.hpp b/include/llarp/service/address.hpp index 4113f3667..c64603741 100644 --- a/include/llarp/service/address.hpp +++ b/include/llarp/service/address.hpp @@ -20,6 +20,16 @@ namespace llarp Address(const byte_t* data) : llarp::AlignedBuffer< 32 >(data) { } + struct Hash + { + size_t + operator()(const Address& addr) const + { + size_t idx = 0; + memcpy(&idx, addr, sizeof(idx)); + return idx; + } + }; }; } // namespace service diff --git a/include/llarp/service/endpoint.hpp b/include/llarp/service/endpoint.hpp index ca2ff36db..c04408a92 100644 --- a/include/llarp/service/endpoint.hpp +++ b/include/llarp/service/endpoint.hpp @@ -1,5 +1,6 @@ #ifndef LLARP_SERVICE_ENDPOINT_HPP #define LLARP_SERVICE_ENDPOINT_HPP +#include #include #include @@ -24,11 +25,70 @@ namespace llarp bool HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg); + bool + HandleHiddenServiceFrame(const llarp::routing::HiddenServiceFrame* msg); + + /// return true if we have an established path to a hidden service + bool + HasPathToService(const Address& remote) const; + + /// return false if we don't have a path to the service + /// return true if we did and we removed it + bool + ForgetPathToService(const Address& remote); + + /// context needed to initiate an outbound hidden service session + struct OutboundContext : public llarp_pathbuilder_context + { + OutboundContext(Endpoint* parent); + ~OutboundContext(); + + /// the remote hidden service's curren intro set + IntroSet currentIntroSet; + + uint64_t sequenceNo = 0; + + /// encrypt asynchronously and send to remote endpoint from us + /// returns false if we cannot send yet otherwise returns true + bool + AsyncEncryptAndSendTo(llarp_buffer_t D); + + /// issues a lookup to find the current intro set of the remote service + void + UpdateIntroSet(); + + bool + HandleGotIntroMessage(const llarp::dht::GotIntroMessage* msg); + + private: + llarp::SharedSecret sharedKey; + Endpoint* m_Parent; + }; + + // passed a sendto context when we have a path established otherwise + // nullptr if the path was not made before the timeout + typedef std::function< void(OutboundContext*) > PathEnsureHook; + + /// return false if we have already called this function before for this + /// address + bool + EnsurePathToService(const Address& remote, PathEnsureHook h, + uint64_t timeoutMS); + + virtual bool + HandleAuthenticatedDataFrom(const Address& remote, llarp_buffer_t data) + { + /// TODO: imlement me + return true; + } + private: llarp_router* m_Router; std::string m_Keyfile; std::string m_Name; Identity m_Identity; + std::unordered_map< Address, OutboundContext*, Address::Hash > + m_RemoteSessions; }; } // namespace service } // namespace llarp diff --git a/llarp/context.cpp b/llarp/context.cpp index 3edefc2fe..434e1eb05 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -161,11 +161,6 @@ namespace llarp llarp::LogError("Failed to configure router"); return 1; } - if(custom_dht_func) - { - llarp::LogInfo("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); @@ -325,12 +320,6 @@ llarp_main_init(const char *fname, bool multiProcess) return m; } -void -llarp_main_set_dht_handler(struct llarp_main *ptr, llarp_dht_msg_handler func) -{ - ptr->ctx->custom_dht_func = func; -} - void llarp_main_signal(struct llarp_main *ptr, int sig) { diff --git a/llarp/dht.cpp b/llarp/dht.cpp index cd6817ec5..f09c85d4e 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -34,13 +34,6 @@ llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id) ctx->impl.nodes->DelNode(k); } -void -llarp_dht_set_msg_handler(struct llarp_dht_context *ctx, - llarp_dht_msg_handler handler) -{ - ctx->impl.custom_handler = handler; -} - void llarp_dht_allow_transit(llarp_dht_context *ctx) { diff --git a/llarp/dht/find_intro.cpp b/llarp/dht/find_intro.cpp new file mode 100644 index 000000000..c113317c9 --- /dev/null +++ b/llarp/dht/find_intro.cpp @@ -0,0 +1,11 @@ +#include + +namespace llarp +{ + namespace dht + { + FindIntroMessage::~FindIntroMessage() + { + } + } // namespace dht +} // namespace llarp \ No newline at end of file diff --git a/llarp/path.cpp b/llarp/path.cpp index f48761e8e..aad811228 100644 --- a/llarp/path.cpp +++ b/llarp/path.cpp @@ -381,13 +381,6 @@ namespace llarp return HandleRoutingMessage(buf, r); } - bool - Path::HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r) - { - // TODO: implement me - return false; - } - bool Path::HandleRoutingMessage(llarp_buffer_t buf, llarp_router* r) { diff --git a/llarp/pathbuilder.cpp b/llarp/pathbuilder.cpp index 669e76c93..7c5708ece 100644 --- a/llarp/pathbuilder.cpp +++ b/llarp/pathbuilder.cpp @@ -175,9 +175,8 @@ namespace llarp } // namespace llarp llarp_pathbuilder_context::llarp_pathbuilder_context( - llarp_router* p_router, struct llarp_dht_context* p_dht) - // TODO: hardcoded value - : llarp::path::PathSet(4), router(p_router), dht(p_dht) + llarp_router* p_router, struct llarp_dht_context* p_dht, size_t pathNum) + : llarp::path::PathSet(pathNum), router(p_router), dht(p_dht) { p_router->paths.AddPathBuilder(this); } @@ -196,9 +195,9 @@ llarp_pathbuilder_context::BuildOne() struct llarp_pathbuilder_context* llarp_pathbuilder_context_new(struct llarp_router* router, - struct llarp_dht_context* dht) + struct llarp_dht_context* dht, size_t sz) { - return new llarp_pathbuilder_context(router, dht); + return new llarp_pathbuilder_context(router, dht, sz); } void diff --git a/llarp/router.cpp b/llarp/router.cpp index 1f65f06c5..15e7929bc 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -34,7 +34,6 @@ llarp_router::llarp_router() , paths(this) , dht(llarp_dht_context_new(this)) , inbound_link_msg_parser(this) - , explorePool(llarp_pathbuilder_context_new(this, dht)) , hiddenServiceContext(this) { diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index b0453f161..6ed0fcf34 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -6,7 +6,7 @@ namespace llarp namespace service { Endpoint::Endpoint(const std::string& name, llarp_router* r) - : llarp_pathbuilder_context(r, r->dht), m_Router(r), m_Name(name) + : llarp_pathbuilder_context(r, r->dht, 2), m_Router(r), m_Name(name) { } @@ -96,5 +96,23 @@ namespace llarp Endpoint::~Endpoint() { } - } -} \ No newline at end of file + + Endpoint::OutboundContext::OutboundContext(Endpoint* parent) + : llarp_pathbuilder_context(parent->m_Router, parent->m_Router->dht, 2) + , m_Parent(parent) + { + } + + Endpoint::OutboundContext::~OutboundContext() + { + } + + bool + Endpoint::OutboundContext::HandleGotIntroMessage( + const llarp::dht::GotIntroMessage* msg) + { + // TODO: implement me + return false; + } + } // namespace service +} // namespace llarp \ No newline at end of file diff --git a/llarp/transit_hop.cpp b/llarp/transit_hop.cpp index 9afc470e5..8f20c7251 100644 --- a/llarp/transit_hop.cpp +++ b/llarp/transit_hop.cpp @@ -136,12 +136,5 @@ namespace llarp return false; } - bool - TransitHop::HandleHiddenServiceData(llarp_buffer_t buf, llarp_router* r) - { - llarp::LogWarn("unwarrented hidden service data on ", info); - return false; - } - } // namespace path } // namespace llarp From 5b68d437301777be7060d2a7b02c32aa0a41cf93 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 12 Jul 2018 14:21:58 -0400 Subject: [PATCH 17/21] forgot file --- include/llarp/messages/hidden_service.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 include/llarp/messages/hidden_service.hpp diff --git a/include/llarp/messages/hidden_service.hpp b/include/llarp/messages/hidden_service.hpp new file mode 100644 index 000000000..b417f81d2 --- /dev/null +++ b/include/llarp/messages/hidden_service.hpp @@ -0,0 +1,16 @@ +#ifndef LLARP_MESSAGES_HIDDEN_SERIVCE_HPP +#define LLARP_MESSAGES_HIDDEN_SERIVCE_HPP + +#include + +namespace llarp +{ + namespace routing + { + struct HiddenServiceFrame : public IMessage + { + }; + } // namespace routing +} // namespace llarp + +#endif \ No newline at end of file From 9073a5a9f37afd6bbfcc935f18ef0f76edf535e1 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Fri, 13 Jul 2018 09:27:13 +0000 Subject: [PATCH 18/21] actually resolve conflict --- llarp/dht.cpp | 643 +------------------------------------------------- 1 file changed, 2 insertions(+), 641 deletions(-) diff --git a/llarp/dht.cpp b/llarp/dht.cpp index eeac32c2c..246749e9d 100644 --- a/llarp/dht.cpp +++ b/llarp/dht.cpp @@ -2,645 +2,6 @@ #include "router.hpp" #include "router_contact.hpp" -<<<<<<< HEAD -======= -#include - -#include // std::find -#include - -namespace llarp -{ - DHTImmeidateMessage::~DHTImmeidateMessage() - { - for(auto &msg : msgs) - delete msg; - msgs.clear(); - } - - bool - DHTImmeidateMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf) - { - if(llarp_buffer_eq(key, "m")) - return llarp::dht::DecodeMesssageList(remote.data(), buf, msgs); - if(llarp_buffer_eq(key, "v")) - { - if(!bencode_read_integer(buf, &version)) - return false; - return version == LLARP_PROTO_VERSION; - } - // bad key - return false; - } - - bool - DHTImmeidateMessage::BEncode(llarp_buffer_t *buf) const - { - if(!bencode_start_dict(buf)) - return false; - - // message type - if(!bencode_write_bytestring(buf, "a", 1)) - return false; - if(!bencode_write_bytestring(buf, "m", 1)) - return false; - - // dht messages - if(!bencode_write_bytestring(buf, "m", 1)) - return false; - // begin list - if(!bencode_start_list(buf)) - return false; - for(const auto &msg : msgs) - { - if(!msg->BEncode(buf)) - return false; - } - // end list - if(!bencode_end(buf)) - return false; - - // protocol version - if(!bencode_write_version_entry(buf)) - return false; - - return bencode_end(buf); - } - - bool - DHTImmeidateMessage::HandleMessage(llarp_router *router) const - { - DHTImmeidateMessage *reply = new DHTImmeidateMessage(remote); - bool result = true; - for(auto &msg : msgs) - { - result &= msg->HandleMessage(router, reply->msgs); - } - return result && router->SendToOrQueue(remote.data(), reply); - } - - namespace dht - { - GotRouterMessage::~GotRouterMessage() - { - for(auto &rc : R) - llarp_rc_free(&rc); - R.clear(); - } - - bool - GotRouterMessage::BEncode(llarp_buffer_t *buf) const - { - if(!bencode_start_dict(buf)) - return false; - - // message type - if(!BEncodeWriteDictMsgType(buf, "A", "S")) - return false; - - if(!BEncodeWriteDictList("R", R, buf)) - return false; - - // txid - if(!BEncodeWriteDictInt(buf, "T", txid)) - return false; - - // version - if(!BEncodeWriteDictInt(buf, "V", version)) - return false; - - return bencode_end(buf); - } - - bool - GotRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val) - { - if(llarp_buffer_eq(key, "R")) - { - return BEncodeReadList(R, val); - } - if(llarp_buffer_eq(key, "T")) - { - return bencode_read_integer(val, &txid); - } - bool read = false; - if(!BEncodeMaybeReadVersion("V", version, LLARP_PROTO_VERSION, read, key, - val)) - return false; - - return read; - } - - bool - GotRouterMessage::HandleMessage(llarp_router *router, - std::vector< IMessage * > &replies) const - { - auto &dht = router->dht->impl; - SearchJob *pending = dht.FindPendingTX(From, txid); - if(pending) - { - if(R.size()) - { - pending->Completed(&R[0]); - if(pending->requester != dht.OurKey()) - { - replies.push_back(new GotRouterMessage( - pending->target, pending->requesterTX, &R[0])); - } - } - else - { - // iterate to next closest peer - Key_t nextPeer; - pending->exclude.insert(From); - if(pending->exclude.size() < 3 - && dht.nodes->FindCloseExcluding(pending->target, nextPeer, - pending->exclude)) - { - llarp::Info(pending->target, " was not found via ", From, - " iterating to next peer ", nextPeer, " already asked ", - pending->exclude.size(), " other peers"); - // REVIEW: is this ok to relay the pending->job as the current job (seems to make things work) - dht.LookupRouter(pending->target, pending->requester, - pending->requesterTX, nextPeer, pending->job, true, - pending->exclude); - } - else - { - llarp::Info(pending->target, " was not found via ", From, - " and we won't look it up"); - pending->Completed(nullptr); - if(pending->requester != dht.OurKey()) - { - replies.push_back(new GotRouterMessage( - pending->target, pending->requesterTX, nullptr)); - } - } - } - dht.RemovePendingLookup(From, txid); - return true; - } - llarp::Warn("Got response for DHT transaction we are not tracking, txid=", - txid); - return false; - } - - FindRouterMessage::~FindRouterMessage() - { - } - - bool - FindRouterMessage::BEncode(llarp_buffer_t *buf) const - { - if(!bencode_start_dict(buf)) - return false; - - // message type - if(!bencode_write_bytestring(buf, "A", 1)) - return false; - if(!bencode_write_bytestring(buf, "R", 1)) - return false; - - // iterative or not? - if(!bencode_write_bytestring(buf, "I", 1)) - return false; - if(!bencode_write_int(buf, iterative ? 1 : 0)) - return false; - - // key - if(!bencode_write_bytestring(buf, "K", 1)) - return false; - if(!bencode_write_bytestring(buf, K.data(), K.size())) - return false; - - // txid - if(!bencode_write_bytestring(buf, "T", 1)) - return false; - if(!bencode_write_uint64(buf, txid)) - return false; - - // version - if(!bencode_write_bytestring(buf, "V", 1)) - return false; - if(!bencode_write_uint64(buf, version)) - return false; - - return bencode_end(buf); - } - - bool - FindRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val) - { - llarp_buffer_t strbuf; - - if(llarp_buffer_eq(key, "I")) - { - uint64_t result; - if(!bencode_read_integer(val, &result)) - return false; - - iterative = result != 0; - return true; - } - if(llarp_buffer_eq(key, "K")) - { - if(!bencode_read_string(val, &strbuf)) - return false; - if(strbuf.sz != K.size()) - return false; - - memcpy(K.data(), strbuf.base, K.size()); - return true; - } - if(llarp_buffer_eq(key, "T")) - { - return bencode_read_integer(val, &txid); - } - if(llarp_buffer_eq(key, "V")) - { - return bencode_read_integer(val, &version); - } - return false; - } - - bool - FindRouterMessage::HandleMessage(llarp_router *router, - std::vector< IMessage * > &replies) const - { - auto &dht = router->dht->impl; - if(!dht.allowTransit) - { - llarp::Warn("Got DHT lookup from ", From, - " when we are not allowing dht transit"); - return false; - } - auto pending = dht.FindPendingTX(From, txid); - if(pending) - { - llarp::Warn("Got duplicate DHT lookup from ", From, " txid=", txid); - return false; - } - dht.LookupRouterRelayed(From, txid, K, !iterative, replies); - return true; - } - - struct MessageDecoder - { - const Key_t &From; - bool firstKey = true; - IMessage *msg = nullptr; - - MessageDecoder(const Key_t &from) : From(from) - { - } - - static bool - on_key(dict_reader *r, llarp_buffer_t *key) - { - llarp_buffer_t strbuf; - MessageDecoder *dec = static_cast< MessageDecoder * >(r->user); - // check for empty dict - if(!key) - return !dec->firstKey; - - // first key - if(dec->firstKey) - { - if(!llarp_buffer_eq(*key, "A")) - return false; - if(!bencode_read_string(r->buffer, &strbuf)) - return false; - // bad msg size? - if(strbuf.sz != 1) - return false; - switch(*strbuf.base) - { - case 'R': - dec->msg = new FindRouterMessage(dec->From); - break; - case 'S': - dec->msg = new GotRouterMessage(dec->From); - break; - default: - llarp::Warn("unknown dht message type: ", (char)*strbuf.base); - // bad msg type - return false; - } - dec->firstKey = false; - return true; - } - else - return dec->msg->DecodeKey(*key, r->buffer); - } - }; - - IMessage * - DecodeMesssage(const Key_t &from, llarp_buffer_t *buf) - { - MessageDecoder dec(from); - dict_reader r; - r.user = &dec; - r.on_key = &MessageDecoder::on_key; - if(bencode_read_dict(buf, &r)) - return dec.msg; - else - { - if(dec.msg) - delete dec.msg; - return nullptr; - } - } - - struct ListDecoder - { - ListDecoder(const Key_t &from, std::vector< IMessage * > &list) - : From(from), l(list){}; - - const Key_t &From; - std::vector< IMessage * > &l; - - static bool - on_item(list_reader *r, bool has) - { - ListDecoder *dec = static_cast< ListDecoder * >(r->user); - if(!has) - return true; - auto msg = DecodeMesssage(dec->From, r->buffer); - if(msg) - { - dec->l.push_back(msg); - return true; - } - else - return false; - } - }; - - bool - DecodeMesssageList(const Key_t &from, llarp_buffer_t *buf, - std::vector< IMessage * > &list) - { - ListDecoder dec(from, list); - - list_reader r; - r.user = &dec; - r.on_item = &ListDecoder::on_item; - return bencode_read_list(buf, &r); - } - - SearchJob::SearchJob() - { - started = 0; - requester.Zero(); - target.Zero(); - } - - SearchJob::SearchJob(const Key_t &asker, uint64_t tx, const Key_t &key, - llarp_router_lookup_job *j, - const std::set< Key_t > &excludes) - : job(j) - , started(llarp_time_now_ms()) - , requester(asker) - , requesterTX(tx) - , target(key) - , exclude(excludes) - { - } - - void - SearchJob::Completed(const llarp_rc *router, bool timeout) const - { - if(job && job->hook) - { - if(router) - { - job->found = true; - llarp_rc_copy(&job->result, router); - } - job->hook(job); - } - } - - bool - SearchJob::IsExpired(llarp_time_t now) const - { - return now - started >= JobTimeout; - } - - Context::Context() - { - randombytes((byte_t *)&ids, sizeof(uint64_t)); - } - - Context::~Context() - { - if(nodes) - delete nodes; - if(services) - delete services; - } - - void - Context::handle_cleaner_timer(void *u, uint64_t orig, uint64_t left) - { - if(left) - return; - Context *ctx = static_cast< Context * >(u); - - ctx->CleanupTX(); - ctx->ScheduleCleanupTimer(); - } - - void - Context::LookupRouterRelayed(const Key_t &requester, uint64_t txid, - const Key_t &target, bool recursive, - std::vector< IMessage * > &replies) - { - if(target == ourKey) - { - // we are the target, give them our RC - replies.push_back(new GotRouterMessage(requester, txid, &router->rc)); - return; - } - Key_t next; - std::set< Key_t > excluding = {requester, ourKey}; - if(nodes->FindCloseExcluding(target, next, excluding)) - { - //llarp::Info("LookupRouterRelayed tick"); - if(next == target) - { - // we know it - replies.push_back( - new GotRouterMessage(requester, txid, nodes->nodes[target].rc)); - } - else if(recursive) // are we doing a recursive lookup? - { - if((requester ^ target) < (ourKey ^ target)) - { - // we aren't closer to the target than next hop - // so we won't ask neighboor recursively, tell them we don't have it - llarp::Info("we aren't closer to ", target, " than ", next, - " so we end it here"); - replies.push_back(new GotRouterMessage(requester, txid, nullptr)); - } - else - { - // yeah, ask neighboor recursively - llarp::Info("asking neighbor recursively"); - // FIXME: we may need to pass a job here... - //auto sj = FindPendingTX(requester, txid); - //LookupRouter(target, requester, txid, next, sj->job); - LookupRouter(target, requester, txid, next); - } - } - else // otherwise tell them we don't have it - { - llarp::Info("we don't have ", target, - " and this was an iterative request so telling ", - requester, " that we don't have it"); - replies.push_back(new GotRouterMessage(requester, txid, nullptr)); - } - } - else - { - // we don't know it and have no closer peers - llarp::Info("we don't have ", target, - " and have no closer peers so telling ", requester, - " that we don't have it"); - replies.push_back(new GotRouterMessage(requester, txid, nullptr)); - } - } - - void - Context::RemovePendingLookup(const Key_t &owner, uint64_t id) - { - TXOwner search; - search.node = owner; - search.txid = id; - auto itr = pendingTX.find(search); - if(itr == pendingTX.end()) - return; - pendingTX.erase(itr); - } - - SearchJob * - Context::FindPendingTX(const Key_t &owner, uint64_t id) - { - TXOwner search; - search.node = owner; - search.txid = id; - auto itr = pendingTX.find(search); - if(itr == pendingTX.end()) - return nullptr; - else - return &itr->second; - } - - void - Context::CleanupTX() - { - auto now = llarp_time_now_ms(); - llarp::Debug("DHT tick"); - - auto itr = pendingTX.begin(); - while(itr != pendingTX.end()) - { - if(itr->second.IsExpired(now)) - { - itr->second.Completed(nullptr, true); - itr = pendingTX.erase(itr); - } - else - ++itr; - } - } - - void - Context::Init(const Key_t &us, llarp_router *r) - { - router = r; - ourKey = us; - nodes = new Bucket< RCNode >(ourKey); - services = new Bucket< ISNode >(ourKey); - llarp::Debug("intialize dht with key ", ourKey); - } - - void - Context::ScheduleCleanupTimer() - { - llarp_logic_call_later(router->logic, - {1000, this, &handle_cleaner_timer}); - } - - bool - Context::RelayRequestForPath(const llarp::PathID_t &id, const IMessage *msg) - { - // TODO: implement me - return false; - } - - void - Context::LookupRouter(const Key_t &target, const Key_t &whoasked, - uint64_t txid, const Key_t &askpeer, - llarp_router_lookup_job *job, bool iterative, - std::set< Key_t > excludes) - { - if(target.IsZero() || whoasked.IsZero() || askpeer.IsZero()) - { - return; - } - auto id = ++ids; - TXOwner ownerKey; - ownerKey.node = askpeer; - ownerKey.txid = id; - if(txid == 0) - txid = id; - - pendingTX[ownerKey] = SearchJob(whoasked, txid, target, job, excludes); - - llarp::Info("Asking ", askpeer, " for router ", target, " for ", - whoasked); - auto msg = new llarp::DHTImmeidateMessage(askpeer); - auto dhtmsg = new FindRouterMessage(askpeer, target, id); - dhtmsg->iterative = iterative; - msg->msgs.push_back(dhtmsg); - router->SendToOrQueue(askpeer, msg); - } - - void - Context::LookupRouterViaJob(llarp_router_lookup_job *job) - { - Key_t peer; - /* - llarp::Info("LookupRouterViaJob dumping nodes"); - for(const auto &item : nodes->nodes) - { - llarp::Info("LookupRouterViaJob dumping node: ", item.first); - } - */ - llarp::Info("LookupRouterViaJob node count: ", nodes->nodes.size()); - llarp::Info("LookupRouterViaJob recursive: ", job->iterative?"yes":"no"); - if(nodes->FindClosest(job->target, peer)) - LookupRouter(job->target, ourKey, 0, peer, job, job->iterative); - else if(job->hook) - { - job->found = false; - job->hook(job); - } - } - - void - Context::queue_router_lookup(void *user) - { - llarp_router_lookup_job *job = - static_cast< llarp_router_lookup_job * >(user); - job->dht->impl.LookupRouterViaJob(job); - } - - } // namespace dht -} // namespace llarp - ->>>>>>> ccc6300849a5375132dad112ff894eccba7be231 llarp_dht_context::llarp_dht_context(llarp_router *router) { parent = router; @@ -663,7 +24,7 @@ llarp_dht_put_peer(struct llarp_dht_context *ctx, struct llarp_rc *rc) { llarp::dht::RCNode n(rc); - llarp::Debug("Adding ", n.ID, " to DHT"); + llarp::LogDebug("Adding ", n.ID, " to DHT"); ctx->impl.nodes->PutNode(n); } @@ -671,7 +32,7 @@ void llarp_dht_remove_peer(struct llarp_dht_context *ctx, const byte_t *id) { llarp::dht::Key_t k = id; - llarp::Debug("Removing ", k, " to DHT"); + llarp::LogDebug("Removing ", k, " to DHT"); ctx->impl.nodes->DelNode(k); } From 8a7db419f9482c9631af33b5f1247ecf8615934a Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Fri, 13 Jul 2018 09:27:57 +0000 Subject: [PATCH 19/21] reapply DHT changes --- llarp/dht/context.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 8b83cb27d..8beb46ee9 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -66,6 +66,9 @@ namespace llarp else { // yeah, ask neighboor recursively + // FIXME: we may need to pass a job here... + //auto sj = FindPendingTX(requester, txid); + //LookupRouter(target, requester, txid, next, sj->job); LookupRouter(target, requester, txid, next); } } @@ -189,8 +192,18 @@ namespace llarp Context::LookupRouterViaJob(llarp_router_lookup_job *job) { Key_t peer; + /* + llarp::LogInfo("LookupRouterViaJob dumping nodes"); + for(const auto &item : nodes->nodes) + { + llarp::LogInfo("LookupRouterViaJob dumping node: ", item.first); + } + */ + llarp::LogInfo("LookupRouterViaJob node count: ", nodes->nodes.size()); + llarp::LogInfo("LookupRouterViaJob recursive: ", job->iterative?"yes":"no"); + if(nodes->FindClosest(job->target, peer)) - LookupRouter(job->target, ourKey, 0, peer, job); + LookupRouter(job->target, ourKey, 0, peer, job, job->iterative); else if(job->hook) { job->found = false; @@ -207,4 +220,4 @@ namespace llarp } } // namespace dht -} // namespace llarp \ No newline at end of file +} // namespace llarp From 14fac288fcec00166bc45df42043681a5b053e2e Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Fri, 13 Jul 2018 09:28:16 +0000 Subject: [PATCH 20/21] relay job if you need to ask again (reapply, merge fix) --- llarp/dht/got_router.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llarp/dht/got_router.cpp b/llarp/dht/got_router.cpp index d50e880a6..08537e482 100644 --- a/llarp/dht/got_router.cpp +++ b/llarp/dht/got_router.cpp @@ -61,8 +61,8 @@ namespace llarp GotRouterMessage::HandleMessage(llarp_dht_context *ctx, std::vector< IMessage * > &replies) const { - auto &dht = ctx->impl; - auto pending = dht.FindPendingTX(From, txid); + auto &dht = ctx->impl; + SearchJob *pending = dht.FindPendingTX(From, txid); if(pending) { if(R.size()) @@ -87,9 +87,10 @@ namespace llarp " iterating to next peer ", nextPeer, " already asked ", pending->exclude.size(), " other peers"); + // REVIEW: is this ok to relay the pending->job as the current job (seems to make things work) dht.LookupRouter(pending->target, pending->requester, - pending->requesterTX, nextPeer, nullptr, true, - pending->exclude); + pending->requesterTX, nextPeer, pending->job, + true, pending->exclude); } else { @@ -111,4 +112,4 @@ namespace llarp return false; } } // namespace dht -} // namespace llarp \ No newline at end of file +} // namespace llarp From a3d791e5161c316e421e6a88cc03cd4db431c8fe Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Fri, 13 Jul 2018 09:28:50 +0000 Subject: [PATCH 21/21] update logging to new format --- llarp/router.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llarp/router.cpp b/llarp/router.cpp index b1767e42c..5a771ff06 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -174,7 +174,7 @@ llarp_router::try_connect(fs::path rcfile) } if(llarp_rc_verify_sig(&crypto, remote)) { - llarp::Debug("verified signature"); + llarp::LogDebug("verified signature"); if(!llarp_router_try_connect(this, remote, 10)) { // or error? @@ -243,7 +243,7 @@ llarp_router::SaveRC() void llarp_router::Close() { - llarp::Info("Closing ", inboundLinks.size(), " server bindings"); + llarp::LogInfo("Closing ", inboundLinks.size(), " server bindings"); for(auto link : inboundLinks) { link->stop_link(); @@ -251,7 +251,7 @@ llarp_router::Close() } inboundLinks.clear(); - llarp::Info("Closing LokiNetwork client"); + llarp::LogInfo("Closing LokiNetwork client"); outboundLink->stop_link(); delete outboundLink; outboundLink = nullptr;