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/gotintro.cpp

127 lines
3.2 KiB
C++

#include "gotintro.hpp"
6 years ago
#include <llarp/service/intro.hpp>
#include <llarp/dht/context.hpp>
#include <memory>
#include <llarp/path/path_context.hpp>
#include <llarp/router/router.hpp>
#include <llarp/routing/path_dht_message.hpp>
#include <llarp/tooling/dht_event.hpp>
#include <utility>
6 years ago
1 year ago
namespace llarp::dht
6 years ago
{
1 year ago
GotIntroMessage::GotIntroMessage(std::vector<service::EncryptedIntroSet> results, uint64_t tx)
: AbstractDHTMessage({}), found(std::move(results)), txid(tx)
{}
6 years ago
1 year ago
bool
GotIntroMessage::handle_message(
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& /*replies*/) const
1 year ago
{
auto* router = dht.GetRouter();
router->notify_router_event<tooling::GotIntroReceivedEvent>(
1 year ago
router->pubkey(),
Key_t(From.data()),
(found.size() > 0 ? found[0] : llarp::service::EncryptedIntroSet{}),
txid);
6 years ago
1 year ago
for (const auto& introset : found)
{
if (!introset.Verify(dht.Now()))
6 years ago
{
1 year ago
LogWarn(
"Invalid introset while handling direct GotIntro "
"from ",
From);
return false;
6 years ago
}
1 year ago
}
TXOwner owner(From, txid);
1 year ago
auto serviceLookup = dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
if (serviceLookup)
{
if (not found.empty())
6 years ago
{
1 year ago
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target, found);
6 years ago
}
1 year ago
else
{
dht.pendingIntrosetLookups().NotFound(owner, nullptr);
}
return true;
6 years ago
}
1 year ago
LogError("no pending TX for GIM from ", From, " txid=", txid);
return false;
}
6 years ago
1 year ago
bool
RelayedGotIntroMessage::handle_message(
AbstractDHTMessageHandler& dht,
1 year ago
[[maybe_unused]] std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
// TODO: implement me better?
auto pathset = dht.GetRouter()->path_context().GetLocalPathSet(pathID);
1 year ago
if (pathset)
6 years ago
{
1 year ago
auto copy = std::make_shared<const RelayedGotIntroMessage>(*this);
return pathset->HandleGotIntroMessage(copy);
6 years ago
}
1 year ago
LogWarn("No path for got intro message pathid=", pathID);
return false;
}
6 years ago
1 year ago
bool
GotIntroMessage::decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if (key.startswith("I"))
6 years ago
{
1 year ago
return BEncodeReadList(found, buf);
}
if (key.startswith("K"))
{
if (closer) // duplicate key?
6 years ago
return false;
1 year ago
dht::Key_t K;
if (not K.BDecode(buf))
6 years ago
return false;
1 year ago
closer = K;
return true;
6 years ago
}
1 year ago
bool read = false;
if (!BEncodeMaybeReadDictInt("T", txid, read, key, buf))
return false;
if (!BEncodeMaybeReadDictInt("V", version, read, key, buf))
return false;
return read;
}
6 years ago
1 year ago
void
GotIntroMessage::bt_encode(oxenc::bt_dict_producer& btdp) const
{
try
6 years ago
{
1 year ago
btdp.append("A", "G");
6 years ago
{
1 year ago
auto sublist = btdp.append_list("I");
for (auto f : found)
sublist.append(f.ToString());
6 years ago
}
1 year ago
if (closer)
btdp.append("K", closer->ToView());
btdp.append("T", txid);
btdp.append("V", version);
}
catch (...)
{
log::error(dht_cat, "Error: GotIntroMessage failed to bt encode contents!");
6 years ago
}
1 year ago
}
} // namespace llarp::dht