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>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2019-01-22 01:14:02 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
ServiceAddressLookup::ServiceAddressLookup(
|
2020-01-27 21:30:41 +00:00
|
|
|
const TXOwner &asker, const Key_t &addr, AbstractContext *ctx,
|
2020-02-13 22:19:12 +00:00
|
|
|
uint64_t recursion, uint32_t order,
|
|
|
|
service::EncryptedIntroSetLookupHandler handler)
|
2020-01-27 21:30:41 +00:00
|
|
|
: TX< Key_t, service::EncryptedIntroSet >(asker, addr, ctx)
|
2019-07-30 23:42:13 +00:00
|
|
|
, handleResult(std::move(handler))
|
2020-02-13 15:51:25 +00:00
|
|
|
, recursionDepth(recursion)
|
2020-02-13 22:19:12 +00:00
|
|
|
, relayOrder(order)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
|
|
|
peersAsked.insert(ctx->OurKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-01-27 21:30:41 +00:00
|
|
|
ServiceAddressLookup::Validate(
|
|
|
|
const service::EncryptedIntroSet &value) const
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
if(value.derivedSigningKey != target)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
const auto &nodes = parent->Nodes();
|
2019-01-28 19:39:17 +00:00
|
|
|
if(nodes)
|
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
return nodes->FindCloseExcluding(target, next, exclude);
|
2019-01-28 19:39:17 +00:00
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return false;
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceAddressLookup::Start(const TXOwner &peer)
|
|
|
|
{
|
2020-02-13 15:51:25 +00:00
|
|
|
parent->DHTSendTo(
|
|
|
|
peer.node.as_array(),
|
2020-02-13 22:19:12 +00:00
|
|
|
new FindIntroMessage(peer.txid, target, recursionDepth, relayOrder));
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceAddressLookup::DoNextRequest(const Key_t &ask)
|
|
|
|
{
|
2020-02-13 15:51:25 +00:00
|
|
|
if(recursionDepth)
|
2019-01-22 01:14:02 +00:00
|
|
|
{
|
|
|
|
parent->LookupIntroSetRecursive(target, whoasked.node, whoasked.txid,
|
2020-02-13 22:19:12 +00:00
|
|
|
ask, recursionDepth - 1, relayOrder);
|
2019-01-22 01:14:02 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
llarp::service::EncryptedIntroSet 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
|