2019-01-16 00:24:16 +00:00
|
|
|
#include <dht/context.hpp>
|
2018-12-15 16:21:52 +00:00
|
|
|
#include <dht/messages/gotrouter.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <memory>
|
2019-06-17 23:19:39 +00:00
|
|
|
#include <path/path_context.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.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
|
|
|
|
{
|
|
|
|
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
|
2019-02-01 01:58:06 +00:00
|
|
|
GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "K")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
if(K) // duplicate key?
|
|
|
|
return false;
|
2019-07-30 23:42:13 +00:00
|
|
|
K = std::make_unique< dht::Key_t >();
|
2019-01-16 00:24:16 +00:00
|
|
|
return K->BDecode(val);
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "N")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
return BEncodeReadList(N, val);
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "R")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
return BEncodeReadList(R, val);
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "T")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &txid);
|
|
|
|
}
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadVersion("V", version, LLARP_PROTO_VERSION, read, key,
|
|
|
|
val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotRouterMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
|
|
|
__attribute__((unused))
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto &dht = *ctx->impl;
|
2019-01-16 00:24:16 +00:00
|
|
|
if(relayed)
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto pathset =
|
|
|
|
ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
|
2019-05-03 13:15:03 +00:00
|
|
|
auto copy = std::make_shared< const GotRouterMessage >(*this);
|
|
|
|
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
|
|
|
|
2019-02-22 15:08:00 +00:00
|
|
|
if(dht.pendingExploreLookups().HasPendingLookupFrom(owner))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-08-28 12:44:50 +00:00
|
|
|
LogDebug("got ", N.size(), " results in GRM for explore");
|
2019-01-16 00:24:16 +00:00
|
|
|
if(N.size() == 0)
|
2019-02-22 15:08:00 +00:00
|
|
|
dht.pendingExploreLookups().NotFound(owner, K);
|
2019-01-16 00:24:16 +00:00
|
|
|
else
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
dht.pendingExploreLookups().Found(owner, From.as_array(), N);
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// not explore lookup
|
2019-04-16 18:06:12 +00:00
|
|
|
if(dht.pendingRouterLookups().HasPendingLookupFrom(owner))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-08-28 12:44:50 +00:00
|
|
|
LogDebug("got ", R.size(), " results in GRM for lookup");
|
2019-04-16 18:06:12 +00:00
|
|
|
if(R.size() == 0)
|
|
|
|
dht.pendingRouterLookups().NotFound(owner, K);
|
|
|
|
else
|
|
|
|
dht.pendingRouterLookups().Found(owner, R[0].pubkey, R);
|
|
|
|
return true;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
2019-04-16 18:06:12 +00:00
|
|
|
llarp::LogWarn("Unwarranted GRM from ", From, " txid=", txid);
|
|
|
|
return false;
|
2019-01-16 00:24:16 +00:00
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|