2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include <llarp/dht/messages/gotrouter.hpp>
|
2018-12-10 16:26:46 +00:00
|
|
|
|
|
|
|
#include <router.hpp>
|
2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
GotRouterMessage::~GotRouterMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotRouterMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// message type
|
|
|
|
if(!BEncodeWriteDictMsgType(buf, "A", "S"))
|
|
|
|
return false;
|
|
|
|
|
2018-11-08 15:15:02 +00:00
|
|
|
if(K)
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("K", *K.get(), buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:44:16 +00:00
|
|
|
// near
|
|
|
|
if(N.size())
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictList("N", N, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:20:14 +00:00
|
|
|
if(!BEncodeWriteDictList("R", R, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// txid
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("T", txid, buf))
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// version
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("V", version, buf))
|
2018-07-11 13:20:14 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GotRouterMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t *val)
|
|
|
|
{
|
2018-11-08 15:15:02 +00:00
|
|
|
if(llarp_buffer_eq(key, "K"))
|
|
|
|
{
|
|
|
|
if(K) // duplicate key?
|
|
|
|
return false;
|
|
|
|
K.reset(new dht::Key_t());
|
|
|
|
return K->BDecode(val);
|
|
|
|
}
|
2018-08-27 13:44:16 +00:00
|
|
|
if(llarp_buffer_eq(key, "N"))
|
|
|
|
{
|
|
|
|
return BEncodeReadList(N, val);
|
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
if(llarp_buffer_eq(key, "R"))
|
|
|
|
{
|
|
|
|
return BEncodeReadList(R, val);
|
|
|
|
}
|
|
|
|
if(llarp_buffer_eq(key, "T"))
|
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &txid);
|
|
|
|
}
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadVersion("V", version, LLARP_PROTO_VERSION, read, key,
|
|
|
|
val))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-02 18:25:42 +00:00
|
|
|
GotRouterMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
2018-11-07 15:30:22 +00:00
|
|
|
__attribute__((unused))
|
2018-09-02 18:25:42 +00:00
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
auto &dht = ctx->impl;
|
|
|
|
if(relayed)
|
|
|
|
{
|
|
|
|
auto pathset = ctx->impl.router->paths.GetLocalPathSet(pathID);
|
2018-09-27 12:47:21 +00:00
|
|
|
return pathset && pathset->HandleGotRouterMessage(this);
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
// not relayed
|
2018-08-29 20:40:26 +00:00
|
|
|
TXOwner owner(From, txid);
|
|
|
|
|
|
|
|
if(dht.pendingExploreLookups.HasPendingLookupFrom(owner))
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
2018-08-29 20:40:26 +00:00
|
|
|
if(N.size() == 0)
|
2018-11-08 15:15:02 +00:00
|
|
|
dht.pendingExploreLookups.NotFound(owner, K);
|
2018-08-29 20:40:26 +00:00
|
|
|
else
|
2018-09-27 12:47:21 +00:00
|
|
|
{
|
2018-12-10 17:22:59 +00:00
|
|
|
dht.pendingExploreLookups.Found(owner, From.data(), N);
|
2018-09-27 12:47:21 +00:00
|
|
|
}
|
2018-07-11 13:20:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
// not explore lookup
|
2018-08-29 20:40:26 +00:00
|
|
|
|
|
|
|
if(!dht.pendingRouterLookups.HasPendingLookupFrom(owner))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("Unwarrented GRM from ", From, " txid=", txid);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-03 13:19:18 +00:00
|
|
|
// no pending lookup
|
|
|
|
|
|
|
|
llarp::LogInfo("DHT no pending lookup");
|
2018-08-29 20:40:26 +00:00
|
|
|
if(R.size() == 1)
|
2018-09-27 12:47:21 +00:00
|
|
|
dht.pendingRouterLookups.Found(owner, R[0].pubkey, {R[0]});
|
2018-08-29 20:40:26 +00:00
|
|
|
else
|
2018-11-08 15:15:02 +00:00
|
|
|
dht.pendingRouterLookups.NotFound(owner, K);
|
2018-08-29 20:40:26 +00:00
|
|
|
return true;
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
} // namespace dht
|
2018-07-13 09:28:16 +00:00
|
|
|
} // namespace llarp
|