2018-07-11 13:20:14 +00:00
|
|
|
|
|
|
|
#include <llarp/dht/context.hpp>
|
|
|
|
#include <llarp/dht/messages/gotrouter.hpp>
|
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
GotRouterMessage::~GotRouterMessage()
|
|
|
|
{
|
|
|
|
for(auto &rc : R)
|
|
|
|
llarp_rc_free(&rc);
|
|
|
|
R.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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
|
|
|
|
GotRouterMessage::HandleMessage(llarp_dht_context *ctx,
|
|
|
|
std::vector< IMessage * > &replies) const
|
|
|
|
{
|
2018-08-10 21:34:11 +00:00
|
|
|
auto &dht = ctx->impl;
|
|
|
|
if(relayed)
|
|
|
|
{
|
|
|
|
auto pathset = ctx->impl.router->paths.GetLocalPathSet(pathID);
|
|
|
|
if(pathset)
|
|
|
|
{
|
|
|
|
return pathset->HandleGotRouterMessage(this);
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 09:28:16 +00:00
|
|
|
SearchJob *pending = dht.FindPendingTX(From, txid);
|
2018-07-11 13:20:14 +00:00
|
|
|
if(pending)
|
|
|
|
{
|
|
|
|
if(R.size())
|
|
|
|
{
|
2018-07-12 13:43:37 +00:00
|
|
|
pending->FoundRouter(&R[0]);
|
2018-07-11 13:20:14 +00:00
|
|
|
if(pending->requester != dht.OurKey())
|
|
|
|
{
|
|
|
|
replies.push_back(new GotRouterMessage(
|
2018-08-10 21:34:11 +00:00
|
|
|
pending->target, pending->requesterTX, &R[0], false));
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 13:44:16 +00:00
|
|
|
else if(N.empty())
|
2018-07-11 13:20:14 +00:00
|
|
|
{
|
|
|
|
// iterate to next closest peer
|
|
|
|
Key_t nextPeer;
|
|
|
|
pending->exclude.insert(From);
|
|
|
|
if(pending->exclude.size() < 3
|
|
|
|
&& dht.nodes->FindCloseExcluding(pending->target, nextPeer,
|
|
|
|
pending->exclude))
|
|
|
|
{
|
|
|
|
llarp::LogInfo(pending->target, " was not found via ", From,
|
|
|
|
" iterating to next peer ", nextPeer,
|
|
|
|
" already asked ", pending->exclude.size(),
|
|
|
|
" other peers");
|
2018-07-13 13:36:51 +00:00
|
|
|
// REVIEW: is this ok to relay the pending->job as the current job
|
|
|
|
// (seems to make things work)
|
2018-07-11 13:20:14 +00:00
|
|
|
dht.LookupRouter(pending->target, pending->requester,
|
2018-07-13 13:36:51 +00:00
|
|
|
pending->requesterTX, nextPeer, pending->job, true,
|
|
|
|
pending->exclude);
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogInfo(pending->target, " was not found via ", From,
|
|
|
|
" and we won't look it up");
|
2018-07-12 13:43:37 +00:00
|
|
|
pending->FoundRouter(nullptr);
|
2018-07-11 13:20:14 +00:00
|
|
|
if(pending->requester != dht.OurKey())
|
|
|
|
{
|
|
|
|
replies.push_back(new GotRouterMessage(
|
2018-08-10 21:34:11 +00:00
|
|
|
pending->target, pending->requesterTX, nullptr, false));
|
2018-07-11 13:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 13:44:16 +00:00
|
|
|
else if(pending->foundNear)
|
|
|
|
{
|
|
|
|
// near peers provided
|
|
|
|
pending->foundNear(N);
|
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
dht.RemovePendingTX(From, txid);
|
2018-07-11 13:20:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llarp::LogWarn(
|
|
|
|
"Got response for DHT transaction we are not tracking, txid=", txid);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-12 13:43:37 +00:00
|
|
|
} // namespace dht
|
2018-07-13 09:28:16 +00:00
|
|
|
} // namespace llarp
|