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>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
#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)
|
2020-01-23 12:11:11 +00:00
|
|
|
: 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
|
|
|
{
|
2019-05-28 19:45:08 +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
|
|
|
|
2020-01-23 12:11:11 +00:00
|
|
|
for(const auto &introset : found)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +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;
|
|
|
|
}
|
|
|
|
}
|
2020-01-23 12:11:11 +00:00
|
|
|
TXOwner owner(From, txid);
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
auto serviceLookup =
|
2019-02-22 15:08:00 +00:00
|
|
|
dht.pendingIntrosetLookups().GetPendingLookupFrom(owner);
|
2019-01-16 00:24:16 +00:00
|
|
|
if(serviceLookup)
|
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
if(not found.empty())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingIntrosetLookups().Found(owner, serviceLookup->target,
|
|
|
|
found);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingIntrosetLookups().NotFound(owner, nullptr);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-23 12:11:11 +00:00
|
|
|
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?
|
2019-02-22 15:08:00 +00:00
|
|
|
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
|
2019-02-01 01:58:06 +00:00
|
|
|
GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "I")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
return BEncodeReadList(found, buf);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "K")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
if(closer.has_value()) // duplicate key?
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-01-23 12:11:11 +00:00
|
|
|
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;
|
2020-01-23 12:11:11 +00:00
|
|
|
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;
|
2020-01-23 12:11:11 +00:00
|
|
|
if(!BEncodeWriteDictList("I", found, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-01-23 12:11:11 +00:00
|
|
|
if(closer.has_value())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
if(!BEncodeWriteDictEntry("K", closer.value(), buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-23 12:11:11 +00:00
|
|
|
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
|