lokinet/llarp/dht/messages/gotintro.cpp

134 lines
3.6 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
2020-03-01 08:14:27 +00:00
#include <service/intro.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>
2020-03-01 08:14:27 +00:00
#include <tooling/dht_event.hpp>
2019-07-30 23:42:13 +00:00
#include <utility>
2019-01-16 00:24:16 +00:00
namespace llarp
{
namespace dht
{
2020-01-27 21:30:41 +00:00
GotIntroMessage::GotIntroMessage(
std::vector< service::EncryptedIntroSet > results, uint64_t tx)
: IMessage({}), found(std::move(results)), txid(tx)
2019-01-16 00:24:16 +00:00
{
}
bool
GotIntroMessage::HandleMessage(
llarp_dht_context *ctx,
2020-02-23 02:21:38 +00:00
std::vector< std::unique_ptr< IMessage > > & /*replies*/) const
2019-01-16 00:24:16 +00:00
{
auto &dht = *ctx->impl;
2020-03-01 08:14:27 +00:00
auto *router = dht.GetRouter();
auto ev = std::make_unique<tooling::GotIntroReceivedEvent>(
router->pubkey(),
Key_t(From.data()),
(found.size() > 0 ? found[0] : llarp::service::EncryptedIntroSet{}),
txid);
router->NotifyRouterEvent(std::move(ev));
2019-01-16 00:24:16 +00:00
for(const auto &introset : found)
2019-01-16 00:24:16 +00:00
{
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, txid);
2020-01-27 21:30:41 +00:00
2019-01-16 00:24:16 +00:00
auto serviceLookup =
dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
2019-01-16 00:24:16 +00:00
if(serviceLookup)
{
if(not found.empty())
2019-01-16 00:24:16 +00:00
{
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target,
found);
2019-01-16 00:24:16 +00:00
}
else
{
dht.pendingIntrosetLookups().NotFound(owner, nullptr);
2019-01-16 00:24:16 +00:00
}
return true;
}
LogError("no pending TX for GIM from ", From, " txid=", txid);
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(found, buf);
2019-01-16 00:24:16 +00:00
}
if(key == "K")
2019-01-16 00:24:16 +00:00
{
if(closer.has_value()) // duplicate key?
2019-01-16 00:24:16 +00:00
return false;
dht::Key_t K;
if(not K.BDecode(buf))
return false;
closer = K;
return true;
2019-01-16 00:24:16 +00:00
}
bool read = false;
if(!BEncodeMaybeReadDictInt("T", txid, read, key, buf))
2019-01-16 00:24:16 +00:00
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))
2019-01-16 00:24:16 +00:00
return false;
if(closer.has_value())
2019-01-16 00:24:16 +00:00
{
if(!BEncodeWriteDictEntry("K", closer.value(), buf))
2019-01-16 00:24:16 +00:00
return false;
}
if(!BEncodeWriteDictInt("T", txid, buf))
2019-01-16 00:24:16 +00:00
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
return bencode_end(buf);
}
} // namespace dht
} // namespace llarp