From cc106ed37bfb51b5bf8ce7ed42407c868b3cac5f Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 21 Oct 2018 10:19:49 -0400 Subject: [PATCH] fix dht bug, probably, re-enable loopback testnet functionality --- CMakeLists.txt | 4 ++ Makefile | 4 +- include/llarp/dht/messages/findintro.hpp | 5 +- include/llarp/service/tag.hpp | 14 ++---- llarp/dht/context.cpp | 60 ++++++++++++++++++++---- llarp/dht/find_intro.cpp | 11 ++++- llarp/dht/got_intro.cpp | 4 +- llarp/net.cpp | 4 ++ llarp/service/endpoint.cpp | 20 +++++--- 9 files changed, 92 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 432a7bfa6..ff51850cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,10 @@ else() set(WITH_STATIC ON) endif() +if(TESTNET) + add_definitions(-DTESTNET=1) +endif() + add_cflags("-Wall") add_cxxflags("-Wall") diff --git a/Makefile b/Makefile index 3f22def8f..c47431766 100644 --- a/Makefile +++ b/Makefile @@ -91,7 +91,7 @@ testnet-clean: clean rm -rf $(TESTNET_ROOT) testnet-configure: testnet-clean - cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) + cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=$(CC) -DCMAKE_CXX_COMPILER=$(CXX) -DTESTNET=1 testnet-build: testnet-configure ninja @@ -105,7 +105,7 @@ shared: shared-configure testnet: cp $(EXE) $(TESTNET_EXE) mkdir -p $(TESTNET_ROOT) - python3 contrib/testnet/genconf.py --bin=$(TESTNET_EXE) --svc=$(TESTNET_SERVERS) --clients=$(TESTNET_CLIENTS) --dir=$(TESTNET_ROOT) --out $(TESTNET_CONF) --connect=3 + python3 contrib/testnet/genconf.py --bin=$(TESTNET_EXE) --svc=$(TESTNET_SERVERS) --clients=$(TESTNET_CLIENTS) --dir=$(TESTNET_ROOT) --out $(TESTNET_CONF) --connect=4 LLARP_DEBUG=$(TESTNET_DEBUG) supervisord -n -d $(TESTNET_ROOT) -l $(TESTNET_LOG) -c $(TESTNET_CONF) test: debug diff --git a/include/llarp/dht/messages/findintro.hpp b/include/llarp/dht/messages/findintro.hpp index a696ceb56..a0b7fb697 100644 --- a/include/llarp/dht/messages/findintro.hpp +++ b/include/llarp/dht/messages/findintro.hpp @@ -21,8 +21,9 @@ namespace llarp relayed = relay; } - FindIntroMessage(const llarp::service::Tag& tag, uint64_t txid) - : IMessage({}), N(tag), T(txid) + FindIntroMessage(const llarp::service::Tag& tag, uint64_t txid, + uint64_t r = 3) + : IMessage({}), R(r), N(tag), T(txid) { S.Zero(); } diff --git a/include/llarp/service/tag.hpp b/include/llarp/service/tag.hpp index 559f543a6..9c5a6d13c 100644 --- a/include/llarp/service/tag.hpp +++ b/include/llarp/service/tag.hpp @@ -21,11 +21,7 @@ namespace llarp Tag(const std::string& str) : Tag() { -#ifndef MIN -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - memcpy(data(), str.c_str(), MIN(16UL, str.size())); -#undef MIN -#endif + memcpy(data(), str.c_str(), std::min(16UL, str.size())); } Tag& @@ -38,11 +34,7 @@ namespace llarp Tag& operator=(const std::string& str) { -#ifndef MIN -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) - memcpy(data(), str.data(), MIN(16UL, str.size())); -#undef MIN -#endif + memcpy(data(), str.data(), std::min(16UL, str.size())); return *this; } @@ -67,4 +59,4 @@ namespace llarp } // namespace service } // namespace llarp -#endif \ No newline at end of file +#endif diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index 12aab82b6..60ea97868 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -132,12 +132,6 @@ namespace llarp ctx->ScheduleCleanupTimer(); } - void - Context::LookupTagForPath(const service::Tag &tag, uint64_t txid, - const llarp::PathID_t &path, const Key_t &askpeer) - { - } - std::set< service::IntroSet > Context::FindRandomIntroSetsWithTagExcluding( const service::Tag &tag, size_t max, @@ -567,7 +561,8 @@ namespace llarp void Start(const TXOwner &peer) { - parent->DHTSendTo(peer.node, new FindIntroMessage(target, peer.txid)); + parent->DHTSendTo(peer.node, + new FindIntroMessage(target, peer.txid, R)); } bool @@ -590,10 +585,10 @@ namespace llarp found.insert(remoteTag); } // collect our local values if we haven't hit a limit - if(found.size() < 8) + if(found.size() < 3) { for(const auto &localTag : - parent->FindRandomIntroSetsWithTagExcluding(target, 2, found)) + parent->FindRandomIntroSetsWithTagExcluding(target, 1, found)) { found.insert(localTag); } @@ -616,6 +611,53 @@ namespace llarp TXOwner asker(whoasked, whoaskedTX); TXOwner peer(askpeer, ++ids); pendingTagLookups.NewTX(peer, tag, new TagLookup(asker, tag, this, R)); + llarp::LogInfo("ask ", askpeer, " for ", tag, " on behalf of ", whoasked, + " R=", R); + } + + struct LocalTagLookup : public TagLookup + { + PathID_t localPath; + + LocalTagLookup(const PathID_t &path, uint64_t txid, + const service::Tag &target, Context *ctx) + : TagLookup(TXOwner{ctx->OurKey(), txid}, target, ctx, 3) + , localPath(path) + { + } + + void + SendReply() + { + auto path = + parent->router->paths.GetByUpstream(parent->OurKey(), localPath); + if(!path) + { + llarp::LogWarn( + "did not send reply for relayed dht request, no such local path " + "for pathid=", + localPath); + return; + } + routing::DHTMessage msg; + msg.M.emplace_back(new GotIntroMessage(valuesFound, whoasked.txid)); + if(!path->SendRoutingMessage(&msg, parent->router)) + { + llarp::LogWarn( + "failed to send routing message when informing result of dht " + "request, pathid=", + localPath); + } + } + }; + + void + Context::LookupTagForPath(const service::Tag &tag, uint64_t txid, + const llarp::PathID_t &path, const Key_t &askpeer) + { + TXOwner peer(askpeer, ++ids); + pendingTagLookups.NewTX(peer, tag, + new LocalTagLookup(path, txid, tag, this)); } bool diff --git a/llarp/dht/find_intro.cpp b/llarp/dht/find_intro.cpp index eb4b017c8..1df2e9061 100644 --- a/llarp/dht/find_intro.cpp +++ b/llarp/dht/find_intro.cpp @@ -182,13 +182,22 @@ namespace llarp replies.emplace_back(new GotIntroMessage(reply, T)); return true; } - else + else if(R < 5) { // tag lookup if(dht.nodes->GetRandomNodeExcluding(peer, exclude)) { dht.LookupTagRecursive(N, From, T, peer, R - 1); } + else + { + replies.emplace_back(new GotIntroMessage({}, T)); + } + } + else + { + // too big R value + replies.emplace_back(new GotIntroMessage({}, T)); } } } diff --git a/llarp/dht/got_intro.cpp b/llarp/dht/got_intro.cpp index 8e28881e1..57c15378d 100644 --- a/llarp/dht/got_intro.cpp +++ b/llarp/dht/got_intro.cpp @@ -41,14 +41,14 @@ namespace llarp auto tagLookup = dht.pendingTagLookups.GetPendingLookupFrom(owner); if(tagLookup) { - dht.pendingTagLookups.Inform(owner, tagLookup->target, I); + dht.pendingTagLookups.Found(owner, tagLookup->target, I); return true; } auto serviceLookup = dht.pendingIntrosetLookups.GetPendingLookupFrom(owner); if(serviceLookup) { - dht.pendingIntrosetLookups.Inform(owner, serviceLookup->target, I); + dht.pendingIntrosetLookups.Found(owner, serviceLookup->target, I); return true; } llarp::LogError("no pending TX for GIM from ", From, " txid=", T); diff --git a/llarp/net.cpp b/llarp/net.cpp index fb5e1b476..3698e1d00 100644 --- a/llarp/net.cpp +++ b/llarp/net.cpp @@ -985,10 +985,14 @@ namespace llarp bool IsBogon(const in6_addr& addr) { +#ifdef TESTNET + return false; +#else if(!ipv6_is_siit(addr)) return false; return IsIPv4Bogon(ipaddr_ipv4_bits(addr.s6_addr[12], addr.s6_addr[13], addr.s6_addr[14], addr.s6_addr[15])); +#endif } bool diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 9558e459d..d5068d27d 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -186,7 +186,7 @@ namespace llarp } } } - +#ifdef TESTNET // prefetch tags for(const auto& tag : m_PrefetchTags) { @@ -201,12 +201,12 @@ namespace llarp { if(HasPendingPathToService(introset.A.Addr())) continue; - if(!EnsurePathToService(introset.A.Addr(), - [](Address addr, OutboundContext* ctx) {}, - 10000)) + byte_t tmp[1024] = {0}; + auto buf = StackBuffer< decltype(tmp) >(tmp); + if(!SendToOrQueue(introset.A.Addr(), buf, eProtocolText)) { - llarp::LogWarn("failed to ensure path to ", introset.A.Addr(), - " for tag ", tag.ToString()); + llarp::LogWarn(Name(), " failed to send/queue data to ", + introset.A.Addr(), " for tag ", tag.ToString()); } } itr->second.Expire(now); @@ -216,10 +216,16 @@ namespace llarp if(path) { auto job = new TagLookupJob(this, &itr->second); - job->SendRequestViaPath(path, Router()); + if(!job->SendRequestViaPath(path, Router())) + llarp::LogError(Name(), " failed to send tag lookup"); + } + else + { + llarp::LogError(Name(), " has no paths for tag lookup"); } } } +#endif // tick remote sessions {