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

130 lines
3.4 KiB
C++

#include <dht/messages/gotintro.hpp>
5 years ago
#include <dht/context.hpp>
#include <memory>
#include <path/path_context.hpp>
#include <router/abstractrouter.hpp>
#include <routing/dht_message.hpp>
#include <utility>
5 years ago
namespace llarp
{
namespace dht
{
GotIntroMessage::GotIntroMessage(std::vector< service::IntroSet > results,
uint64_t tx)
: IMessage({}), found(std::move(results)), txid(tx)
5 years ago
{
}
bool
GotIntroMessage::HandleMessage(
llarp_dht_context *ctx,
ABSL_ATTRIBUTE_UNUSED std::vector< std::unique_ptr< IMessage > >
&replies) const
5 years ago
{
auto &dht = *ctx->impl;
5 years ago
for(const auto &introset : found)
5 years ago
{
if(!introset.Verify(dht.Now()))
5 years ago
{
LogWarn(
5 years ago
"Invalid introset while handling direct GotIntro "
"from ",
From);
return false;
}
}
TXOwner owner(From, txid);
auto tagLookup = dht.pendingTagLookups().GetPendingLookupFrom(owner);
5 years ago
if(tagLookup)
{
dht.pendingTagLookups().Found(owner, tagLookup->target, found);
5 years ago
return true;
}
auto serviceLookup =
dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
5 years ago
if(serviceLookup)
{
if(not found.empty())
5 years ago
{
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target,
found);
5 years ago
}
else
{
dht.pendingIntrosetLookups().NotFound(owner, nullptr);
5 years ago
}
return true;
}
LogError("no pending TX for GIM from ", From, " txid=", txid);
5 years ago
return false;
}
bool
RelayedGotIntroMessage::HandleMessage(
llarp_dht_context *ctx,
__attribute__((unused))
std::vector< std::unique_ptr< IMessage > > &replies) const
{
// TODO: implement me better?
auto pathset =
ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
5 years ago
if(pathset)
{
5 years ago
auto copy = std::make_shared< const RelayedGotIntroMessage >(*this);
return pathset->HandleGotIntroMessage(copy);
5 years ago
}
LogWarn("No path for got intro message pathid=", pathID);
5 years ago
return false;
}
bool
GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
5 years ago
{
if(key == "I")
5 years ago
{
return BEncodeReadList(found, buf);
5 years ago
}
if(key == "K")
5 years ago
{
if(closer.has_value()) // duplicate key?
5 years ago
return false;
dht::Key_t K;
if(not K.BDecode(buf))
return false;
closer = K;
return true;
5 years ago
}
bool read = false;
if(!BEncodeMaybeReadDictInt("T", txid, read, key, buf))
5 years ago
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, key, buf))
return false;
return read;
}
bool
GotIntroMessage::BEncode(llarp_buffer_t *buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictMsgType(buf, "A", "G"))
return false;
if(!BEncodeWriteDictList("I", found, buf))
5 years ago
return false;
if(closer.has_value())
5 years ago
{
if(!BEncodeWriteDictEntry("K", closer.value(), buf))
5 years ago
return false;
}
if(!BEncodeWriteDictInt("T", txid, buf))
5 years ago
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp