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.2 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(!bencode_start_dict(buf))
return false;
// message type
if(!BEncodeWriteDictMsgType(buf, "A", "S"))
return false;
if(K)
{
if(!BEncodeWriteDictEntry("K", *K.get(), buf))
return false;
}
// near
if(N.size())
{
if(!BEncodeWriteDictList("N", N, buf))
return false;
}
if(!BEncodeWriteDictList("R", R, buf))
return false;
// txid
if(!BEncodeWriteDictInt("T", txid, buf))
return false;
// version
if(!BEncodeWriteDictInt("V", version, buf))
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(K) // duplicate key?
return false;
K = std::make_unique< dht::Key_t >();
5 years ago
return K->BDecode(val);
}
if(key == "N")
5 years ago
{
return BEncodeReadList(N, val);
}
if(key == "R")
5 years ago
{
return BEncodeReadList(R, val);
}
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
{
5 years ago
LogDebug("got ", N.size(), " results in GRM for explore");
5 years ago
if(N.size() == 0)
dht.pendingExploreLookups().NotFound(owner, K);
5 years ago
else
{
dht.pendingExploreLookups().Found(owner, From.as_array(), N);
5 years ago
}
return true;
}
// not explore lookup
if(dht.pendingRouterLookups().HasPendingLookupFrom(owner))
5 years ago
{
5 years ago
LogDebug("got ", R.size(), " results in GRM for lookup");
if(R.size() == 0)
dht.pendingRouterLookups().NotFound(owner, K);
else if(R[0].pubkey.IsZero())
return false;
else
dht.pendingRouterLookups().Found(owner, R[0].pubkey, R);
return true;
5 years ago
}
// store if valid
for(const auto &rc : R)
{
4 years ago
if(not dht.GetRouter()->rcLookupHandler().CheckRC(rc))
return false;
}
return true;
5 years ago
}
} // namespace dht
} // namespace llarp