lokinet/llarp/dht/recursiverouterlookup.cpp

72 lines
1.7 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>
2020-01-14 20:48:39 +00:00
#include <router/abstractrouter.hpp>
#include <router/i_rc_lookup_handler.hpp>
2019-07-30 23:42:13 +00:00
#include <utility>
2019-01-22 01:14:02 +00:00
namespace llarp
{
namespace dht
{
RecursiveRouterLookup::RecursiveRouterLookup(
const TXOwner& _whoasked,
const RouterID& _target,
AbstractContext* ctx,
RouterLookupHandler result)
: TX<RouterID, RouterContact>(_whoasked, _target, ctx), resultHandler(std::move(result))
2019-01-22 01:14:02 +00:00
{
peersAsked.insert(ctx->OurKey());
}
bool
RecursiveRouterLookup::Validate(const RouterContact& rc) const
2019-01-22 01:14:02 +00:00
{
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;
}
void
RecursiveRouterLookup::Start(const TXOwner& peer)
2019-01-22 01:14:02 +00:00
{
parent->DHTSendTo(peer.node.as_array(), new FindRouterMessage(peer.txid, target));
2019-01-22 01:14:02 +00:00
}
void
RecursiveRouterLookup::SendReply()
{
if (valuesFound.size())
{
RouterContact found;
for (const auto& rc : valuesFound)
{
if (found.OtherIsNewer(rc) && parent->GetRouter()->rcLookupHandler().CheckRC(rc))
found = rc;
}
valuesFound.clear();
valuesFound.emplace_back(found);
}
if (resultHandler)
2019-01-22 01:14:02 +00:00
{
resultHandler(valuesFound);
}
if (whoasked.node != parent->OurKey())
{
2019-01-22 01:14:02 +00:00
parent->DHTSendTo(
whoasked.node.as_array(),
new GotRouterMessage({}, whoasked.txid, valuesFound, false),
false);
}
2019-01-22 01:14:02 +00:00
}
} // namespace dht
} // namespace llarp