2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include "gotrouter.hpp"
|
2019-01-16 00:24:16 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <memory>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/path/path_context.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/router/i_rc_lookup_handler.hpp>
|
|
|
|
#include <llarp/tooling/rc_event.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
2019-07-30 23:42:13 +00:00
|
|
|
GotRouterMessage::~GotRouterMessage() = default;
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
GotRouterMessage::BEncode(llarp_buffer_t* buf) const
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not bencode_start_dict(buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// message type
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictMsgType(buf, "A", "S"))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closerTarget)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictEntry("K", *closerTarget, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// near
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not nearKeys.empty())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictList("N", nearKeys, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictList("R", foundRCs, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// txid
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictInt("T", txid, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// version
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not BEncodeWriteDictInt("V", version, buf))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
GotRouterMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("K"))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (closerTarget) // duplicate key?
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
closerTarget = std::make_unique<dht::Key_t>();
|
2020-01-23 12:11:11 +00:00
|
|
|
return closerTarget->BDecode(val);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("N"))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
return BEncodeReadList(nearKeys, val);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("R"))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
return BEncodeReadList(foundRCs, val);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("T"))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &txid);
|
|
|
|
}
|
|
|
|
bool read = false;
|
2022-05-26 15:59:44 +00:00
|
|
|
if (!BEncodeMaybeVerifyVersion("V", version, llarp::constants::proto_version, read, key, val))
|
2019-01-16 00:24:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotRouterMessage::HandleMessage(
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_dht_context* ctx,
|
2021-03-03 20:44:32 +00:00
|
|
|
[[maybe_unused]] std::vector<std::unique_ptr<IMessage>>& replies) const
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto& dht = *ctx->impl;
|
|
|
|
if (relayed)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
|
|
|
|
auto copy = std::make_shared<const GotRouterMessage>(*this);
|
2019-05-03 13:15:03 +00:00
|
|
|
return pathset && pathset->HandleGotRouterMessage(copy);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
// not relayed
|
2019-04-16 18:06:12 +00:00
|
|
|
const TXOwner owner(From, txid);
|
2019-01-16 00:24:16 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dht.pendingExploreLookups().HasPendingLookupFrom(owner))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
LogDebug("got ", nearKeys.size(), " results in GRM for explore");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (nearKeys.empty())
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingExploreLookups().NotFound(owner, closerTarget);
|
2019-01-16 00:24:16 +00:00
|
|
|
else
|
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingExploreLookups().Found(owner, From.as_array(), nearKeys);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// not explore lookup
|
2020-04-07 18:38:56 +00:00
|
|
|
if (dht.pendingRouterLookups().HasPendingLookupFrom(owner))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2020-01-23 12:11:11 +00:00
|
|
|
LogDebug("got ", foundRCs.size(), " results in GRM for lookup");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (foundRCs.empty())
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingRouterLookups().NotFound(owner, closerTarget);
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (foundRCs[0].pubkey.IsZero())
|
2020-01-15 15:43:21 +00:00
|
|
|
return false;
|
2019-04-16 18:06:12 +00:00
|
|
|
else
|
2020-01-23 12:11:11 +00:00
|
|
|
dht.pendingRouterLookups().Found(owner, foundRCs[0].pubkey, foundRCs);
|
2019-04-16 18:06:12 +00:00
|
|
|
return true;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2020-01-14 18:08:27 +00:00
|
|
|
// store if valid
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& rc : foundRCs)
|
2020-01-14 18:08:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not dht.GetRouter()->rcLookupHandler().CheckRC(rc))
|
2020-01-14 18:08:27 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (txid == 0) // txid == 0 on gossip
|
2020-01-21 17:31:48 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto* router = dht.GetRouter();
|
|
|
|
router->NotifyRouterEvent<tooling::RCGossipReceivedEvent>(router->pubkey(), rc);
|
2020-03-03 19:56:33 +00:00
|
|
|
router->GossipRCIfNeeded(rc);
|
2020-05-27 01:57:27 +00:00
|
|
|
|
|
|
|
auto peerDb = router->peerDb();
|
|
|
|
if (peerDb)
|
|
|
|
peerDb->handleGossipedRC(rc);
|
2020-01-21 17:31:48 +00:00
|
|
|
}
|
2020-01-14 18:08:27 +00:00
|
|
|
}
|
|
|
|
return true;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|