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
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2019-02-07 22:48:41 +00:00
|
|
|
// get newest introset
|
2019-02-08 13:42:12 +00:00
|
|
|
if(valuesFound.size())
|
2019-02-07 22:48:41 +00:00
|
|
|
{
|
2019-02-07 23:04:28 +00:00
|
|
|
llarp::service::IntroSet found;
|
2019-02-08 13:42:12 +00:00
|
|
|
for(const auto &introset : valuesFound)
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-02-07 22:48:41 +00:00
|
|
|
if(found.OtherIsNewer(introset))
|
|
|
|
found = introset;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
2019-02-07 22:48:41 +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
|