2018-12-15 16:21:52 +00:00
|
|
|
#include <dht/messages/findrouter.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
#include <dht/context.hpp>
|
|
|
|
#include <dht/messages/gotrouter.hpp>
|
|
|
|
#include <messages/dht.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <path/path.hpp>
|
|
|
|
#include <nodedb.hpp>
|
|
|
|
#include <router/abstractrouter.hpp>
|
2019-01-16 00:24:16 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
RelayedFindRouterMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
|
|
|
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
|
|
|
/// lookup for us, send an immeidate reply
|
|
|
|
Key_t us = dht.OurKey();
|
|
|
|
Key_t k{K};
|
|
|
|
if(K == us)
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto path = dht.GetRouter()->pathContext().GetByUpstream(K, pathID);
|
2019-01-16 00:24:16 +00:00
|
|
|
if(path)
|
|
|
|
{
|
|
|
|
replies.emplace_back(
|
2019-02-22 15:08:00 +00:00
|
|
|
new GotRouterMessage(k, txid, {dht.GetRouter()->rc()}, false));
|
2019-01-16 00:24:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key_t peer;
|
|
|
|
// check if we know this in our nodedb first
|
|
|
|
RouterContact found;
|
2019-02-22 15:08:00 +00:00
|
|
|
if(dht.GetRouter()->nodedb()->Get(K, found))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
replies.emplace_back(new GotRouterMessage(k, txid, {found}, false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// lookup if we don't have it in our nodedb
|
2019-02-22 15:08:00 +00:00
|
|
|
if(dht.Nodes()->FindClosest(k, peer))
|
2019-01-16 00:24:16 +00:00
|
|
|
dht.LookupRouterForPath(K, txid, pathID, peer);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FindRouterMessage::~FindRouterMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindRouterMessage::BEncode(llarp_buffer_t *buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// message type
|
|
|
|
if(!bencode_write_bytestring(buf, "A", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buf, "R", 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// exploritory or not?
|
|
|
|
if(!bencode_write_bytestring(buf, "E", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_uint64(buf, exploritory ? 1 : 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// iterative or not?
|
|
|
|
if(!bencode_write_bytestring(buf, "I", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_uint64(buf, iterative ? 1 : 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// key
|
|
|
|
if(!bencode_write_bytestring(buf, "K", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buf, K.data(), K.size()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// txid
|
|
|
|
if(!bencode_write_bytestring(buf, "T", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_uint64(buf, txid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// version
|
|
|
|
if(!bencode_write_bytestring(buf, "V", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_uint64(buf, version))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
FindRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "E")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
uint64_t result;
|
|
|
|
if(!bencode_read_integer(val, &result))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
exploritory = result != 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "I")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
uint64_t result;
|
|
|
|
if(!bencode_read_integer(val, &result))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
iterative = result != 0;
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "K")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
if(!bencode_read_string(val, &strbuf))
|
|
|
|
return false;
|
|
|
|
if(strbuf.sz != K.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::copy(strbuf.base, strbuf.base + K.SIZE, K.begin());
|
|
|
|
return true;
|
|
|
|
}
|
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);
|
|
|
|
}
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "V")
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(val, &version);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FindRouterMessage::HandleMessage(
|
|
|
|
llarp_dht_context *ctx,
|
|
|
|
std::vector< std::unique_ptr< IMessage > > &replies) const
|
|
|
|
{
|
2019-02-22 15:08:00 +00:00
|
|
|
auto &dht = *ctx->impl;
|
|
|
|
if(!dht.AllowTransit())
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Got DHT lookup from ", From,
|
|
|
|
" when we are not allowing dht transit");
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-22 15:08:00 +00:00
|
|
|
if(dht.pendingRouterLookups().HasPendingLookupFrom({From, txid}))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Duplicate FRM from ", From, " txid=", txid);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RouterContact found;
|
|
|
|
Key_t k{K};
|
|
|
|
if(exploritory)
|
|
|
|
return dht.HandleExploritoryRouterLookup(From, txid, K, replies);
|
2019-02-23 16:46:29 +00:00
|
|
|
else if(dht.Nodes()->HasNode(k))
|
2019-01-16 00:24:16 +00:00
|
|
|
{
|
2019-02-23 16:46:29 +00:00
|
|
|
found = dht.Nodes()->nodes[k].rc;
|
2019-01-16 00:24:16 +00:00
|
|
|
replies.emplace_back(new GotRouterMessage(k, txid, {found}, false));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dht.LookupRouterRelayed(From, txid, k, !iterative, replies);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace dht
|
|
|
|
} // namespace llarp
|