1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/dht/serviceaddresslookup.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

2019-01-22 02:14:02 +01:00
#include <dht/serviceaddresslookup.hpp>
#include <dht/context.hpp>
#include <dht/messages/findintro.hpp>
#include <dht/messages/gotintro.hpp>
2019-07-31 01:42:13 +02:00
#include <utility>
2019-01-22 02:14:02 +01:00
namespace llarp
{
namespace dht
{
ServiceAddressLookup::ServiceAddressLookup(
2020-01-27 22:30:41 +01:00
const TXOwner &asker, const Key_t &addr, AbstractContext *ctx,
uint32_t order, service::EncryptedIntroSetLookupHandler handler)
2020-01-27 22:30:41 +01:00
: TX< Key_t, service::EncryptedIntroSet >(asker, addr, ctx)
2019-07-31 01:42:13 +02:00
, handleResult(std::move(handler))
, relayOrder(order)
2019-01-22 02:14:02 +01:00
{
peersAsked.insert(ctx->OurKey());
}
bool
2020-01-27 22:30:41 +01:00
ServiceAddressLookup::Validate(
const service::EncryptedIntroSet &value) const
2019-01-22 02:14:02 +01:00
{
if(!value.Verify(parent->Now()))
2019-01-22 02:14:02 +01:00
{
llarp::LogWarn("Got invalid introset from service lookup");
return false;
}
2020-01-27 22:30:41 +01:00
if(value.derivedSigningKey != target)
2019-01-22 02:14:02 +01: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 13:36:27 +01:00
const auto &nodes = parent->Nodes();
2019-01-28 20:39:17 +01:00
if(nodes)
{
2020-01-27 22:30:41 +01:00
return nodes->FindCloseExcluding(target, next, exclude);
2019-01-28 20:39:17 +01:00
}
2019-07-06 19:03:40 +02:00
return false;
2019-01-22 02:14:02 +01:00
}
void
ServiceAddressLookup::Start(const TXOwner &peer)
{
2020-02-20 21:14:20 +01:00
parent->DHTSendTo(peer.node.as_array(),
new FindIntroMessage(peer.txid, target, relayOrder));
2019-01-22 02:14:02 +01:00
}
void
ServiceAddressLookup::DoNextRequest(const Key_t &ask)
{
(void)ask;
// do nothing -- we handle propagating this explicitly
2019-01-22 02:14:02 +01:00
}
2019-02-08 20:43:25 +01:00
void
ServiceAddressLookup::SendReply()
{
// get newest introset
if(valuesFound.size())
{
2020-01-27 22:30:41 +01:00
llarp::service::EncryptedIntroSet found;
for(const auto &introset : valuesFound)
2019-02-08 20:43:25 +01:00
{
if(found.OtherIsNewer(introset))
found = introset;
2019-02-08 20:43:25 +01:00
}
valuesFound.clear();
valuesFound.emplace_back(found);
}
2019-02-08 20:43:25 +01:00
if(handleResult)
{
handleResult(valuesFound);
}
2019-01-22 02:14:02 +01:00
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage(valuesFound, whoasked.txid));
}
} // namespace dht
} // namespace llarp