lokinet/llarp/dht/serviceaddresslookup.cpp

98 lines
2.5 KiB
C++
Raw Normal View History

2019-01-22 01:14:02 +00:00
#include <dht/serviceaddresslookup.hpp>
#include <dht/context.hpp>
#include <dht/messages/findintro.hpp>
#include <dht/messages/gotintro.hpp>
namespace llarp
{
namespace dht
{
ServiceAddressLookup::ServiceAddressLookup(
2019-02-05 00:41:33 +00:00
const TXOwner &asker, const service::Address &addr,
AbstractContext *ctx, uint64_t r,
service::IntroSetLookupHandler handler)
2019-01-22 01:14:02 +00:00
: TX< service::Address, service::IntroSet >(asker, addr, ctx)
, handleResult(handler)
, R(r)
{
peersAsked.insert(ctx->OurKey());
}
bool
ServiceAddressLookup::Validate(const service::IntroSet &value) const
{
if(!value.Verify(parent->Now()))
2019-01-22 01:14:02 +00:00
{
llarp::LogWarn("Got invalid introset from service lookup");
return false;
}
if(value.A.Addr() != target)
{
llarp::LogWarn("got introset with wrong target from service lookup");
return false;
}
return true;
}
bool
ServiceAddressLookup::GetNextPeer(Key_t &next,
const std::set< Key_t > &exclude)
{
2019-03-27 12:36:27 +00:00
Key_t k = target.ToKey();
const auto &nodes = parent->Nodes();
2019-01-28 19:39:17 +00:00
if(nodes)
{
return nodes->FindCloseExcluding(k, next, exclude);
}
2019-07-06 17:03:40 +00:00
return false;
2019-01-22 01:14:02 +00:00
}
void
ServiceAddressLookup::Start(const TXOwner &peer)
{
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(peer.txid, target, R));
}
void
ServiceAddressLookup::DoNextRequest(const Key_t &ask)
{
if(R)
{
parent->LookupIntroSetRecursive(target, whoasked.node, whoasked.txid,
ask, R - 1);
}
else
{
parent->LookupIntroSetIterative(target, whoasked.node, whoasked.txid,
ask);
}
}
2019-02-08 19:43:25 +00:00
void
ServiceAddressLookup::SendReply()
{
// get newest introset
if(valuesFound.size())
{
2019-02-07 23:04:28 +00:00
llarp::service::IntroSet found;
for(const auto &introset : valuesFound)
2019-02-08 19:43:25 +00:00
{
if(found.OtherIsNewer(introset))
found = introset;
2019-02-08 19:43:25 +00:00
}
valuesFound.clear();
valuesFound.emplace_back(found);
}
2019-02-08 19:43:25 +00:00
if(handleResult)
{
handleResult(valuesFound);
}
2019-01-22 01:14:02 +00:00
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage(valuesFound, whoasked.txid));
}
} // namespace dht
} // namespace llarp