lokinet/llarp/dht/messages/gotrouter.cpp

140 lines
3.7 KiB
C++
Raw Normal View History

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