2021-03-09 22:24:35 +00:00
|
|
|
#include "recursiverouterlookup.hpp"
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "context.hpp"
|
|
|
|
#include <llarp/dht/messages/findrouter.hpp>
|
|
|
|
#include <llarp/dht/messages/gotrouter.hpp>
|
2020-01-14 20:48:39 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/router/i_rc_lookup_handler.hpp>
|
2020-01-14 20:48:39 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2019-01-22 01:14:02 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
RecursiveRouterLookup::Validate(const RouterContact& rc) const
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +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
|
2020-04-07 18:38:56 +00:00
|
|
|
RecursiveRouterLookup::Start(const TXOwner& peer)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
parent->DHTSendTo(peer.node.as_array(), new FindRouterMessage(peer.txid, target));
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RecursiveRouterLookup::SendReply()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (valuesFound.size())
|
2019-02-08 13:42:12 +00:00
|
|
|
{
|
|
|
|
RouterContact found;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& rc : valuesFound)
|
2019-02-08 13:42:12 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (found.OtherIsNewer(rc) && parent->GetRouter()->rcLookupHandler().CheckRC(rc))
|
2019-02-08 13:42:12 +00:00
|
|
|
found = rc;
|
|
|
|
}
|
|
|
|
valuesFound.clear();
|
|
|
|
valuesFound.emplace_back(found);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (resultHandler)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
|
|
|
resultHandler(valuesFound);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (whoasked.node != parent->OurKey())
|
2019-07-19 17:21:20 +00:00
|
|
|
{
|
2019-01-22 01:14:02 +00:00
|
|
|
parent->DHTSendTo(
|
|
|
|
whoasked.node.as_array(),
|
2020-04-07 18:38:56 +00:00
|
|
|
new GotRouterMessage({}, whoasked.txid, valuesFound, false),
|
|
|
|
false);
|
2019-07-19 17:21:20 +00:00
|
|
|
}
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|