lokinet/llarp/dht/recursiverouterlookup.cpp

87 lines
2.3 KiB
C++
Raw Normal View History

2019-01-22 01:14:02 +00:00
#include <dht/recursiverouterlookup.hpp>
#include <dht/context.hpp>
#include <dht/messages/findrouter.hpp>
#include <dht/messages/gotrouter.hpp>
2019-07-30 23:42:13 +00:00
#include <utility>
2019-01-22 01:14:02 +00:00
namespace llarp
{
namespace dht
{
2019-08-02 09:27:27 +00:00
RecursiveRouterLookup::RecursiveRouterLookup(const TXOwner &_whoasked,
const RouterID &_target,
AbstractContext *ctx,
2019-01-22 01:14:02 +00:00
RouterLookupHandler result)
2019-08-02 09:27:27 +00:00
: TX< RouterID, RouterContact >(_whoasked, _target, ctx)
2019-07-30 23:42:13 +00:00
, resultHandler(std::move(result))
2019-01-22 01:14:02 +00:00
{
peersAsked.insert(ctx->OurKey());
}
bool
RecursiveRouterLookup::Validate(const RouterContact &rc) const
{
if(!rc.Verify(parent->Now()))
2019-01-22 01:14:02 +00:00
{
llarp::LogWarn("rc from lookup result is invalid");
return false;
}
return true;
}
bool
RecursiveRouterLookup::GetNextPeer(Key_t &nextPeer,
const std::set< Key_t > &exclude)
{
const Key_t K(target.as_array());
return parent->Nodes()->FindCloseExcluding(K, nextPeer, exclude);
}
void
RecursiveRouterLookup::DoNextRequest(const Key_t &peer)
{
parent->LookupRouterRecursive(target, whoasked.node, whoasked.txid, peer,
resultHandler);
}
2019-01-22 01:14:02 +00:00
void
RecursiveRouterLookup::Start(const TXOwner &peer)
{
parent->DHTSendTo(peer.node.as_array(),
new FindRouterMessage(peer.txid, target));
}
void
RecursiveRouterLookup::SendReply()
{
if(valuesFound.size())
{
RouterContact found;
for(const auto &rc : valuesFound)
{
if(found.OtherIsNewer(rc))
found = rc;
}
valuesFound.clear();
valuesFound.emplace_back(found);
}
2019-01-22 01:14:02 +00:00
if(resultHandler)
{
resultHandler(valuesFound);
}
else if(whoasked.node != parent->OurKey())
{
2019-01-22 01:14:02 +00:00
parent->DHTSendTo(
whoasked.node.as_array(),
2019-02-23 17:22:34 +00:00
new GotRouterMessage({}, whoasked.txid, valuesFound, false), false);
// store this in our nodedb for caching
if(valuesFound.size() > 0)
parent->StoreRC(valuesFound[0]);
}
2019-01-22 01:14:02 +00:00
}
} // namespace dht
} // namespace llarp