You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/dht/messages/findintro.cpp

125 lines
3.2 KiB
C++

#include <llarp/dht/context.hpp>
#include "findintro.hpp"
#include "gotintro.hpp"
#include <llarp/routing/message.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <llarp/nodedb.hpp>
6 years ago
1 year ago
namespace llarp::dht
6 years ago
{
1 year ago
FindIntroMessage::~FindIntroMessage() = default;
6 years ago
1 year ago
bool
FindIntroMessage::decode_key(const llarp_buffer_t& k, llarp_buffer_t* val)
{
bool read = false;
6 years ago
1 year ago
if (!BEncodeMaybeReadDictEntry("N", tagName, read, k, val))
return false;
1 year ago
if (!BEncodeMaybeReadDictInt("O", relayOrder, read, k, val))
return false;
6 years ago
1 year ago
if (!BEncodeMaybeReadDictEntry("S", location, read, k, val))
return false;
6 years ago
1 year ago
if (!BEncodeMaybeReadDictInt("T", txID, read, k, val))
return false;
6 years ago
1 year ago
if (!BEncodeMaybeVerifyVersion("V", version, llarp::constants::proto_version, read, k, val))
return false;
6 years ago
1 year ago
return read;
}
6 years ago
1 year ago
void
FindIntroMessage::bt_encode(oxenc::bt_dict_producer& btdp) const
{
try
6 years ago
{
1 year ago
btdp.append("A", "F");
if (tagName.Empty())
6 years ago
{
1 year ago
btdp.append("O", relayOrder);
btdp.append("S", location.ToView());
6 years ago
}
else
{
1 year ago
btdp.append("N", tagName.ToView());
btdp.append("O", relayOrder);
6 years ago
}
1 year ago
btdp.append("T", txID);
btdp.append("V", llarp::constants::proto_version);
}
catch (...)
{
log::critical(dht_cat, "FindIntroMessage failed to bt encode contents!");
6 years ago
}
1 year ago
}
6 years ago
1 year ago
bool
FindIntroMessage::handle_message(
llarp_dht_context* ctx, std::vector<AbstractDHTMessage::Ptr_t>& replies) const
{
auto& dht = *ctx->impl;
if (dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, txID}))
6 years ago
{
1 year ago
llarp::LogWarn("duplicate FIM from ", From, " txid=", txID);
return false;
}
1 year ago
if (not tagName.Empty())
{
return false;
}
// bad request (request for zero-key)
if (location.IsZero())
{
// we dont got it
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
// we are relaying this message for e.g. a client
if (relayed)
{
if (relayOrder >= IntroSetStorageRedundancy)
{
1 year ago
llarp::LogWarn("Invalid relayOrder received: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
1 year ago
auto closestRCs =
dht.GetRouter()->nodedb()->FindManyClosestTo(location, IntroSetStorageRedundancy);
if (closestRCs.size() <= relayOrder)
6 years ago
{
1 year ago
llarp::LogWarn("Can't fulfill FindIntro for relayOrder: ", relayOrder);
replies.emplace_back(new GotIntroMessage({}, txID));
return true;
}
1 year ago
const auto& entry = closestRCs[relayOrder];
Key_t peer = Key_t(entry.pubkey);
dht.LookupIntroSetForPath(location, txID, pathID, peer, 0);
}
else
{
// we should have this value if introset was propagated properly
const auto maybe = dht.GetIntroSetByLocation(location);
if (maybe)
{
1 year ago
replies.emplace_back(new GotIntroMessage({*maybe}, txID));
6 years ago
}
else
{
1 year ago
LogWarn("Got FIM with relayed == false and we don't have entry");
replies.emplace_back(new GotIntroMessage({}, txID));
6 years ago
}
}
1 year ago
return true;
}
} // namespace llarp::dht