diff --git a/llarp/dht/messages/findintro.cpp b/llarp/dht/messages/findintro.cpp index c64b2ebef..4b35e26d4 100644 --- a/llarp/dht/messages/findintro.cpp +++ b/llarp/dht/messages/findintro.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include namespace llarp { @@ -112,8 +114,14 @@ namespace llarp Key_t us = dht.OurKey(); Key_t target = S.ToKey(); // we are recursive - if(dht.Nodes()->FindCloseExcluding(target, peer, exclude)) + const auto rc = dht.GetRouter()->nodedb()->FindClosestTo(target); { + peer = Key_t(rc.pubkey); + if(peer == us) + { + replies.emplace_back(new GotIntroMessage({}, T)); + return true; + } if(relayed) dht.LookupIntroSetForPath(S, T, pathID, peer, R - 1); else diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 3c40e578c..35d016e6a 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -92,6 +93,25 @@ llarp_nodedb::Has(const llarp::RouterID &pk) return entries.find(pk) != entries.end(); } +llarp::RouterContact +llarp_nodedb::FindClosestTo(const llarp::dht::Key_t &location) +{ + llarp::RouterContact rc; + const llarp::dht::XorMetric compare(location); + visit([&rc, compare](const auto &otherRC) -> bool { + if(rc.pubkey.IsZero()) + { + rc = otherRC; + return true; + } + if(compare(llarp::dht::Key_t{otherRC.pubkey.as_array()}, + llarp::dht::Key_t{rc.pubkey.as_array()})) + rc = otherRC; + return true; + }); + return rc; +} + /// skiplist directory is hex encoded first nibble /// skiplist filename is .snode.signed std::string diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index 0886d7779..820615c87 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -73,6 +74,9 @@ struct llarp_nodedb NetDBMap_t entries GUARDED_BY(access); fs::path nodePath; + llarp::RouterContact + FindClosestTo(const llarp::dht::Key_t &location); + /// return true if we should save our nodedb to disk bool ShouldSaveToDisk(llarp_time_t now = 0) const;