You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/dht/messages/gotrouter.cpp

132 lines
3.4 KiB
C++

5 years ago
#include <dht/context.hpp>
#include <dht/messages/gotrouter.hpp>
5 years ago
#include <memory>
#include <path/path_context.hpp>
#include <router/abstractrouter.hpp>
4 years ago
#include <router/i_rc_lookup_handler.hpp>
5 years ago
namespace llarp
{
namespace dht
{
GotRouterMessage::~GotRouterMessage() = default;
5 years ago
bool
GotRouterMessage::BEncode(llarp_buffer_t *buf) const
{
if(not bencode_start_dict(buf))
5 years ago
return false;
// message type
if(not BEncodeWriteDictMsgType(buf, "A", "S"))
5 years ago
return false;
if(closerTarget)
5 years ago
{
if(not BEncodeWriteDictEntry("K", *closerTarget, buf))
5 years ago
return false;
}
// near
if(not nearKeys.empty())
5 years ago
{
if(not BEncodeWriteDictList("N", nearKeys, buf))
5 years ago
return false;
}
if(not BEncodeWriteDictList("R", foundRCs, buf))
5 years ago
return false;
// txid
if(not BEncodeWriteDictInt("T", txid, buf))
5 years ago
return false;
// version
if(not BEncodeWriteDictInt("V", version, buf))
5 years ago
return false;
return bencode_end(buf);
}
bool
GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
5 years ago
{
if(key == "K")
5 years ago
{
if(closerTarget) // duplicate key?
5 years ago
return false;
closerTarget = std::make_unique< dht::Key_t >();
return closerTarget->BDecode(val);
5 years ago
}
if(key == "N")
5 years ago
{
return BEncodeReadList(nearKeys, val);
5 years ago
}
if(key == "R")
5 years ago
{
return BEncodeReadList(foundRCs, val);
5 years ago
}
if(key == "T")
5 years ago
{
return bencode_read_integer(val, &txid);
}
bool read = false;
5 years ago
if(!BEncodeMaybeVerifyVersion("V", version, LLARP_PROTO_VERSION, read,
key, val))
5 years ago
return false;
return read;
}
bool
GotRouterMessage::HandleMessage(
llarp_dht_context *ctx,
__attribute__((unused))
std::vector< std::unique_ptr< IMessage > > &replies) const
{
auto &dht = *ctx->impl;
5 years ago
if(relayed)
{
auto pathset =
ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
5 years ago
auto copy = std::make_shared< const GotRouterMessage >(*this);
return pathset && pathset->HandleGotRouterMessage(copy);
5 years ago
}
// not relayed
const TXOwner owner(From, txid);
5 years ago
if(dht.pendingExploreLookups().HasPendingLookupFrom(owner))
5 years ago
{
LogDebug("got ", nearKeys.size(), " results in GRM for explore");
if(nearKeys.empty())
dht.pendingExploreLookups().NotFound(owner, closerTarget);
5 years ago
else
{
dht.pendingExploreLookups().Found(owner, From.as_array(), nearKeys);
5 years ago
}
return true;
}
// not explore lookup
if(dht.pendingRouterLookups().HasPendingLookupFrom(owner))
5 years ago
{
LogDebug("got ", foundRCs.size(), " results in GRM for lookup");
if(foundRCs.empty())
dht.pendingRouterLookups().NotFound(owner, closerTarget);
else if(foundRCs[0].pubkey.IsZero())
return false;
else
dht.pendingRouterLookups().Found(owner, foundRCs[0].pubkey, foundRCs);
return true;
5 years ago
}
// store if valid
for(const auto &rc : foundRCs)
{
4 years ago
if(not dht.GetRouter()->rcLookupHandler().CheckRC(rc))
return false;
}
return true;
5 years ago
}
} // namespace dht
} // namespace llarp