lokinet/llarp/dht/messages/gotintro.cpp

126 lines
3.3 KiB
C++
Raw Normal View History

2018-12-15 16:21:52 +00:00
#include <dht/messages/gotintro.hpp>
2019-01-16 00:24:16 +00:00
#include <dht/context.hpp>
2019-07-30 23:42:13 +00:00
#include <memory>
2019-06-17 23:19:39 +00:00
#include <path/path_context.hpp>
#include <router/abstractrouter.hpp>
#include <routing/dht_message.hpp>
2019-07-30 23:42:13 +00:00
#include <utility>
2019-01-16 00:24:16 +00:00
namespace llarp
{
namespace dht
{
2019-07-30 23:42:13 +00:00
GotIntroMessage::GotIntroMessage(std::vector< service::IntroSet > results,
uint64_t tx)
: IMessage({}), I(std::move(results)), T(tx)
2019-01-16 00:24:16 +00:00
{
}
bool
GotIntroMessage::HandleMessage(
llarp_dht_context *ctx,
2019-07-30 23:42:13 +00:00
ABSL_ATTRIBUTE_UNUSED std::vector< std::unique_ptr< IMessage > >
&replies) const
2019-01-16 00:24:16 +00:00
{
auto &dht = *ctx->impl;
2019-01-16 00:24:16 +00:00
for(const auto &introset : I)
{
if(!introset.Verify(dht.Now()))
2019-01-16 00:24:16 +00:00
{
2019-07-30 23:42:13 +00:00
LogWarn(
2019-01-16 00:24:16 +00:00
"Invalid introset while handling direct GotIntro "
"from ",
From);
return false;
}
}
TXOwner owner(From, T);
auto tagLookup = dht.pendingTagLookups().GetPendingLookupFrom(owner);
2019-01-16 00:24:16 +00:00
if(tagLookup)
{
dht.pendingTagLookups().Found(owner, tagLookup->target, I);
2019-01-16 00:24:16 +00:00
return true;
}
auto serviceLookup =
dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
2019-01-16 00:24:16 +00:00
if(serviceLookup)
{
if(I.size())
{
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target, I);
2019-01-16 00:24:16 +00:00
}
else
{
dht.pendingIntrosetLookups().NotFound(owner, K);
2019-01-16 00:24:16 +00:00
}
return true;
}
2019-07-30 23:42:13 +00:00
LogError("no pending TX for GIM from ", From, " txid=", T);
2019-01-16 00:24:16 +00:00
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);
2019-01-16 00:24:16 +00:00
if(pathset)
{
2019-05-03 13:15:03 +00:00
auto copy = std::make_shared< const RelayedGotIntroMessage >(*this);
return pathset->HandleGotIntroMessage(copy);
2019-01-16 00:24:16 +00:00
}
2019-07-30 23:42:13 +00:00
LogWarn("No path for got intro message pathid=", pathID);
2019-01-16 00:24:16 +00:00
return false;
}
bool
GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
2019-01-16 00:24:16 +00:00
{
if(key == "I")
2019-01-16 00:24:16 +00:00
{
return BEncodeReadList(I, buf);
}
if(key == "K")
2019-01-16 00:24:16 +00:00
{
if(K) // duplicate key?
return false;
2019-07-30 23:42:13 +00:00
K = std::make_unique< dht::Key_t >();
2019-01-16 00:24:16 +00:00
return K->BDecode(buf);
}
bool read = false;
if(!BEncodeMaybeReadDictInt("T", T, read, key, buf))
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", I, buf))
return false;
if(K)
{
if(!BEncodeWriteDictEntry("K", *K.get(), buf))
return false;
}
if(!BEncodeWriteDictInt("T", T, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp