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/findrouter.cpp

150 lines
3.9 KiB
C++

#include "findrouter.hpp"
6 years ago
#include <llarp/dht/context.hpp>
#include "gotrouter.hpp"
#include <llarp/nodedb.hpp>
#include <llarp/path/path_context.hpp>
#include <llarp/router/abstractrouter.hpp>
#include <llarp/routing/path_dht_message.hpp>
6 years ago
#include <llarp/tooling/dht_event.hpp>
1 year ago
namespace llarp::dht
6 years ago
{
1 year ago
bool
RelayedFindRouterMessage::handle_message(
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
6 years ago
{
1 year ago
/// lookup for us, send an immeidate reply
const Key_t us = dht.OurKey();
const Key_t k{targetKey};
if (k == us)
6 years ago
{
1 year ago
auto path = dht.GetRouter()->pathContext().GetByUpstream(targetKey, pathID);
if (path)
6 years ago
{
1 year ago
replies.emplace_back(new GotRouterMessage(k, txid, {dht.GetRouter()->rc()}, false));
6 years ago
return true;
}
1 year ago
return false;
}
Key_t peer;
// check if we know this in our nodedb first
if (not dht.GetRouter()->SessionToRouterAllowed(targetKey))
{
// explicitly disallowed by network
replies.emplace_back(new GotRouterMessage(k, txid, {}, false));
6 years ago
return true;
}
1 year ago
// check netdb
const auto rc = dht.GetRouter()->nodedb()->FindClosestTo(k);
if (rc.pubkey == targetKey)
{
replies.emplace_back(new GotRouterMessage(k, txid, {rc}, false));
return true;
}
peer = Key_t(rc.pubkey);
// lookup if we don't have it in our nodedb
dht.LookupRouterForPath(targetKey, txid, pathID, peer);
return true;
}
6 years ago
1 year ago
FindRouterMessage::~FindRouterMessage() = default;
6 years ago
1 year ago
void
FindRouterMessage::bt_encode(oxenc::bt_dict_producer& btdp) const
{
try
6 years ago
{
1 year ago
btdp.append("A", "R");
btdp.append("T", exploratory ? 1 : 0);
btdp.append("I", iterative ? 1 : 0);
btdp.append("K", targetKey.ToView());
btdp.append("T", txid);
btdp.append("V", version);
}
catch (...)
{
log::error(dht_cat, "Error: FindRouterMessage failed to bt encode contents!");
}
}
6 years ago
1 year ago
bool
FindRouterMessage::decode_key(const llarp_buffer_t& key, llarp_buffer_t* val)
{
llarp_buffer_t strbuf;
6 years ago
1 year ago
if (key.startswith("E"))
{
uint64_t result;
if (!bencode_read_integer(val, &result))
6 years ago
return false;
1 year ago
exploratory = result != 0;
return true;
}
6 years ago
1 year ago
if (key.startswith("I"))
{
uint64_t result;
if (!bencode_read_integer(val, &result))
6 years ago
return false;
1 year ago
iterative = result != 0;
return true;
}
if (key.startswith("K"))
{
if (!bencode_read_string(val, &strbuf))
6 years ago
return false;
1 year ago
if (strbuf.sz != targetKey.size())
6 years ago
return false;
1 year ago
std::copy(strbuf.base, strbuf.base + targetKey.SIZE, targetKey.begin());
return true;
6 years ago
}
1 year ago
if (key.startswith("T"))
6 years ago
{
1 year ago
return bencode_read_integer(val, &txid);
}
if (key.startswith("V"))
{
return bencode_read_integer(val, &version);
}
return false;
}
6 years ago
1 year ago
bool
FindRouterMessage::handle_message(
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
1 year ago
{
auto router = dht.GetRouter();
router->NotifyRouterEvent<tooling::FindRouterReceivedEvent>(router->pubkey(), *this);
6 years ago
1 year ago
if (!dht.AllowTransit())
{
llarp::LogWarn("Got DHT lookup from ", From, " when we are not allowing dht transit");
6 years ago
return false;
}
1 year ago
if (dht.pendingRouterLookups().HasPendingLookupFrom({From, txid}))
6 years ago
{
1 year ago
llarp::LogWarn("Duplicate FRM from ", From, " txid=", txid);
return false;
}
RouterContact found;
if (targetKey.IsZero())
{
llarp::LogError("invalid FRM from ", From, " key is zero");
return false;
6 years ago
}
1 year ago
const Key_t k(targetKey);
if (exploratory)
return dht.HandleExploritoryRouterLookup(From, txid, targetKey, replies);
dht.LookupRouterRelayed(From, txid, k, !iterative, replies);
return true;
}
} // namespace llarp::dht