lokinet/llarp/dht/messages/gotrouter.cpp

140 lines
3.8 KiB
C++
Raw Permalink Normal View History

#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>
#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
GotRouterMessage::BEncode(llarp_buffer_t* buf) const
2019-01-16 00:24:16 +00:00
{
if (not bencode_start_dict(buf))
2019-01-16 00:24:16 +00:00
return false;
// message type
if (not BEncodeWriteDictMsgType(buf, "A", "S"))
2019-01-16 00:24:16 +00:00
return false;
if (closerTarget)
2019-01-16 00:24:16 +00:00
{
if (not BEncodeWriteDictEntry("K", *closerTarget, buf))
2019-01-16 00:24:16 +00:00
return false;
}
// near
if (not nearKeys.empty())
2019-01-16 00:24:16 +00:00
{
if (not BEncodeWriteDictList("N", nearKeys, buf))
2019-01-16 00:24:16 +00:00
return false;
}
if (not BEncodeWriteDictList("R", foundRCs, buf))
2019-01-16 00:24:16 +00:00
return false;
// txid
if (not BEncodeWriteDictInt("T", txid, buf))
2019-01-16 00:24:16 +00:00
return false;
// version
if (not BEncodeWriteDictInt("V", version, buf))
2019-01-16 00:24:16 +00:00
return false;
return bencode_end(buf);
}
bool
GotRouterMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
2019-01-16 00:24:16 +00:00
{
if (key.startswith("K"))
2019-01-16 00:24:16 +00:00
{
if (closerTarget) // duplicate key?
2019-01-16 00:24:16 +00:00
return false;
closerTarget = std::make_unique<dht::Key_t>();
return closerTarget->BDecode(val);
2019-01-16 00:24:16 +00:00
}
if (key.startswith("N"))
2019-01-16 00:24:16 +00:00
{
return BEncodeReadList(nearKeys, val);
2019-01-16 00:24:16 +00:00
}
if (key.startswith("R"))
2019-01-16 00:24:16 +00:00
{
return BEncodeReadList(foundRCs, val);
2019-01-16 00:24:16 +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(
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
{
auto& dht = *ctx->impl;
if (relayed)
2019-01-16 00:24:16 +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
if (dht.pendingExploreLookups().HasPendingLookupFrom(owner))
2019-01-16 00:24:16 +00:00
{
LogDebug("got ", nearKeys.size(), " results in GRM for explore");
if (nearKeys.empty())
dht.pendingExploreLookups().NotFound(owner, closerTarget);
2019-01-16 00:24:16 +00:00
else
{
dht.pendingExploreLookups().Found(owner, From.as_array(), nearKeys);
2019-01-16 00:24:16 +00:00
}
return true;
}
// not explore lookup
if (dht.pendingRouterLookups().HasPendingLookupFrom(owner))
2019-01-16 00:24:16 +00:00
{
LogDebug("got ", foundRCs.size(), " results in GRM for lookup");
if (foundRCs.empty())
dht.pendingRouterLookups().NotFound(owner, closerTarget);
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
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
for (const auto& rc : foundRCs)
2020-01-14 18:08:27 +00:00
{
if (not dht.GetRouter()->rcLookupHandler().CheckRC(rc))
2020-01-14 18:08:27 +00:00
return false;
if (txid == 0) // txid == 0 on gossip
2020-01-21 17:31:48 +00:00
{
auto* router = dht.GetRouter();
router->NotifyRouterEvent<tooling::RCGossipReceivedEvent>(router->pubkey(), rc);
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