Message handling methods

- `::handle_message` is transposed; Rather than the message calling the method and taking a reference to the router, the router should have a handle_message method and take a reference to the message
- `::EndcodeBuffer` takes a string reference, to which the result of `::bt_encode()` is assigned
pull/2204/head
dr7ana 9 months ago
parent 11e54f6552
commit 511c20cdb6

@ -25,13 +25,13 @@
namespace llarp::dht
{
AbstractContext::~AbstractContext() = default;
AbstractDHTMessageHandler::~AbstractDHTMessageHandler() = default;
struct Context final : public AbstractContext
struct DHTMessageHandler final : public AbstractDHTMessageHandler
{
Context();
DHTMessageHandler();
~Context() override = default;
~DHTMessageHandler() override = default;
util::StatusObject
ExtractStatus() const override;
@ -280,6 +280,10 @@ namespace llarp::dht
void
ExploreNetworkVia(const Key_t& peer) override;
bool
handle_message(
const AbstractDHTMessage&, std::vector<std::unique_ptr<dht::AbstractDHTMessage>>&) override;
private:
std::shared_ptr<int> _timer_keepalive;
@ -291,13 +295,13 @@ namespace llarp::dht
Key_t ourKey;
};
Context::Context()
DHTMessageHandler::DHTMessageHandler()
{
randombytes((byte_t*)&ids, sizeof(uint64_t));
}
void
Context::Explore(size_t N)
DHTMessageHandler::Explore(size_t N)
{
// ask N random peers for new routers
llarp::LogDebug("Exploring network via ", N, " peers");
@ -313,7 +317,7 @@ namespace llarp::dht
}
void
Context::ExploreNetworkVia(const Key_t& askpeer)
DHTMessageHandler::ExploreNetworkVia(const Key_t& askpeer)
{
uint64_t txid = ++ids;
const TXOwner peer(askpeer, txid);
@ -324,7 +328,7 @@ namespace llarp::dht
}
void
Context::handle_cleaner_timer()
DHTMessageHandler::handle_cleaner_timer()
{
// clean up transactions
CleanupTX();
@ -364,7 +368,7 @@ namespace llarp::dht
}
void
Context::LookupRouterRelayed(
DHTMessageHandler::LookupRouterRelayed(
const Key_t& requester,
uint64_t txid,
const Key_t& target,
@ -424,7 +428,7 @@ namespace llarp::dht
}
std::optional<llarp::service::EncryptedIntroSet>
Context::GetIntroSetByLocation(const Key_t& key) const
DHTMessageHandler::GetIntroSetByLocation(const Key_t& key) const
{
auto itr = _services->nodes.find(key);
if (itr == _services->nodes.end())
@ -433,7 +437,7 @@ namespace llarp::dht
}
void
Context::CleanupTX()
DHTMessageHandler::CleanupTX()
{
auto now = Now();
llarp::LogTrace("DHT tick");
@ -444,7 +448,7 @@ namespace llarp::dht
}
util::StatusObject
Context::ExtractStatus() const
DHTMessageHandler::ExtractStatus() const
{
util::StatusObject obj{
{"pendingRouterLookups", pendingRouterLookups().ExtractStatus()},
@ -456,8 +460,15 @@ namespace llarp::dht
return obj;
}
bool
DHTMessageHandler::handle_message(
const AbstractDHTMessage& msg, std::vector<std::unique_ptr<dht::AbstractDHTMessage>>& replies)
{
return msg.handle_message(*this, replies);
}
void
Context::Init(const Key_t& us, AbstractRouter* r)
DHTMessageHandler::Init(const Key_t& us, AbstractRouter* r)
{
router = r;
ourKey = us;
@ -470,7 +481,7 @@ namespace llarp::dht
}
void
Context::DHTSendTo(const RouterID& peer, AbstractDHTMessage* msg, bool)
DHTMessageHandler::DHTSendTo(const RouterID& peer, AbstractDHTMessage* msg, bool)
{
DHTImmediateMessage m;
m.msgs.emplace_back(msg);
@ -484,10 +495,10 @@ namespace llarp::dht
// namespace. by the time this is called, we are inside
// llarp::routing::DHTMessage::HandleMessage()
bool
Context::RelayRequestForPath(const llarp::PathID_t& id, const AbstractDHTMessage& msg)
DHTMessageHandler::RelayRequestForPath(const llarp::PathID_t& id, const AbstractDHTMessage& msg)
{
routing::PathDHTMessage reply;
if (!msg.handle_message(router->dht(), reply.dht_msgs))
if (not handle_message(msg, reply.dht_msgs))
return false;
if (not reply.dht_msgs.empty())
{
@ -498,7 +509,7 @@ namespace llarp::dht
}
void
Context::LookupIntroSetForPath(
DHTMessageHandler::LookupIntroSetForPath(
const Key_t& addr,
uint64_t txid,
const llarp::PathID_t& path,
@ -515,7 +526,7 @@ namespace llarp::dht
}
void
Context::PropagateIntroSetTo(
DHTMessageHandler::PropagateIntroSetTo(
const Key_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
@ -529,7 +540,7 @@ namespace llarp::dht
}
void
Context::PropagateLocalIntroSet(
DHTMessageHandler::PropagateLocalIntroSet(
const PathID_t& from,
uint64_t txid,
const service::EncryptedIntroSet& introset,
@ -546,7 +557,7 @@ namespace llarp::dht
}
void
Context::LookupIntroSetRelayed(
DHTMessageHandler::LookupIntroSetRelayed(
const Key_t& addr,
const Key_t& whoasked,
uint64_t txid,
@ -561,7 +572,7 @@ namespace llarp::dht
}
void
Context::LookupIntroSetDirect(
DHTMessageHandler::LookupIntroSetDirect(
const Key_t& addr,
const Key_t& whoasked,
uint64_t txid,
@ -575,7 +586,7 @@ namespace llarp::dht
}
bool
Context::HandleExploritoryRouterLookup(
DHTMessageHandler::HandleExploritoryRouterLookup(
const Key_t& requester,
uint64_t txid,
const RouterID& target,
@ -621,7 +632,7 @@ namespace llarp::dht
}
void
Context::LookupRouterForPath(
DHTMessageHandler::LookupRouterForPath(
const RouterID& target, uint64_t txid, const llarp::PathID_t& path, const Key_t& askpeer)
{
@ -632,7 +643,7 @@ namespace llarp::dht
}
void
Context::LookupRouterRecursive(
DHTMessageHandler::LookupRouterRecursive(
const RouterID& target,
const Key_t& whoasked,
uint64_t txid,
@ -646,15 +657,15 @@ namespace llarp::dht
}
llarp_time_t
Context::Now() const
DHTMessageHandler::Now() const
{
return router->Now();
}
std::unique_ptr<AbstractContext>
makeContext()
std::unique_ptr<AbstractDHTMessageHandler>
make_handler()
{
return std::make_unique<Context>();
return std::make_unique<DHTMessageHandler>();
}
} // namespace llarp::dht

@ -32,13 +32,13 @@ namespace llarp
static constexpr size_t IntroSetStorageRedundancy =
(IntroSetRelayRedundancy * IntroSetRequestsPerRelay);
struct AbstractContext
struct AbstractDHTMessageHandler /* : public AbstractMessageHandler */
{
using PendingIntrosetLookups = TXHolder<TXOwner, service::EncryptedIntroSet>;
using PendingRouterLookups = TXHolder<RouterID, RouterContact>;
using PendingExploreLookups = TXHolder<RouterID, RouterID>;
virtual ~AbstractContext() = 0;
virtual ~AbstractDHTMessageHandler() = 0;
virtual bool
LookupRouter(const RouterID& target, RouterLookupHandler result) = 0;
@ -190,16 +190,20 @@ namespace llarp
virtual void
StoreRC(const RouterContact rc) const = 0;
virtual bool
handle_message(
const AbstractDHTMessage&, std::vector<std::unique_ptr<dht::AbstractDHTMessage>>&) = 0;
};
std::unique_ptr<AbstractContext>
makeContext();
std::unique_ptr<AbstractDHTMessageHandler>
make_handler();
} // namespace dht
} // namespace llarp
struct llarp_dht_context
{
std::unique_ptr<llarp::dht::AbstractContext> impl;
std::unique_ptr<llarp::dht::AbstractDHTMessageHandler> impl;
llarp::AbstractRouter* parent;
llarp_dht_context(llarp::AbstractRouter* router);
};

@ -5,7 +5,7 @@
llarp_dht_context::llarp_dht_context(llarp::AbstractRouter* router)
{
parent = router;
impl = llarp::dht::makeContext();
impl = llarp::dht::make_handler();
}
struct llarp_dht_context*

@ -10,7 +10,7 @@ namespace llarp
{
struct ExploreNetworkJob : public TX<RouterID, RouterID>
{
ExploreNetworkJob(const RouterID& peer, AbstractContext* ctx)
ExploreNetworkJob(const RouterID& peer, AbstractDHTMessageHandler* ctx)
: TX<RouterID, RouterID>(TXOwner{}, peer, ctx)
{}

@ -11,7 +11,7 @@
namespace llarp::dht
{
LocalRouterLookup::LocalRouterLookup(
const PathID_t& path, uint64_t txid, const RouterID& _target, AbstractContext* ctx)
const PathID_t& path, uint64_t txid, const RouterID& _target, AbstractDHTMessageHandler* ctx)
: RecursiveRouterLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx, nullptr), localPath(path)
{}

@ -14,7 +14,10 @@ namespace llarp::dht
PathID_t localPath;
LocalRouterLookup(
const PathID_t& path, uint64_t txid, const RouterID& target, AbstractContext* ctx);
const PathID_t& path,
uint64_t txid,
const RouterID& target,
AbstractDHTMessageHandler* ctx);
void
SendReply() override;

@ -14,7 +14,7 @@ namespace llarp::dht
uint64_t txid,
uint64_t relayOrder,
const Key_t& addr,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
[[maybe_unused]] const Key_t& askpeer)
: ServiceAddressLookup(TXOwner{ctx->OurKey(), txid}, addr, ctx, relayOrder, nullptr)
, localPath(pathid)

@ -16,7 +16,7 @@ namespace llarp::dht
uint64_t txid,
uint64_t relayOrder,
const Key_t& addr,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
[[maybe_unused]] const Key_t& askpeer);
void

@ -9,7 +9,10 @@
namespace llarp::dht
{
LocalTagLookup::LocalTagLookup(
const PathID_t& path, uint64_t txid, const service::Tag& _target, AbstractContext* ctx)
const PathID_t& path,
uint64_t txid,
const service::Tag& _target,
AbstractDHTMessageHandler* ctx)
: TagLookup(TXOwner{ctx->OurKey(), txid}, _target, ctx, 0), localPath(path)
{}

@ -10,7 +10,10 @@ namespace llarp::dht
PathID_t localPath;
LocalTagLookup(
const PathID_t& path, uint64_t txid, const service::Tag& target, AbstractContext* ctx);
const PathID_t& path,
uint64_t txid,
const service::Tag& target,
AbstractDHTMessageHandler* ctx);
void
SendReply() override;

@ -17,6 +17,8 @@ namespace llarp::dht
{
constexpr size_t MAX_MSG_SIZE = 2048;
struct AbstractDHTMessageHandler;
struct AbstractDHTMessage : private AbstractSerializable
{
virtual ~AbstractDHTMessage() = default;
@ -27,7 +29,7 @@ namespace llarp::dht
virtual bool
handle_message(
struct llarp_dht_context* dht,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const = 0;
void

@ -60,9 +60,9 @@ namespace llarp::dht
bool
FindIntroMessage::handle_message(
llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
auto& dht = *ctx->impl;
if (dht.pendingIntrosetLookups().HasPendingLookupFrom(TXOwner{From, txID}))
{
llarp::LogWarn("duplicate FIM from ", From, " txid=", txID);

@ -41,7 +41,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
};
} // namespace llarp::dht

@ -44,28 +44,30 @@ namespace llarp::dht
bool
FindNameMessage::handle_message(
struct llarp_dht_context* dht,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
(void)replies;
auto r = dht->impl->GetRouter();
if (pathID.IsZero() or not r->IsServiceNode())
auto router = dht.GetRouter();
if (pathID.IsZero() or not router->IsServiceNode())
return false;
r->RpcClient()->LookupLNSNameHash(NameHash, [r, pathID = pathID, TxID = TxID](auto maybe) {
auto path = r->pathContext().GetPathForTransfer(pathID);
if (path == nullptr)
return;
routing::PathDHTMessage msg;
if (maybe.has_value())
{
msg.dht_msgs.emplace_back(new GotNameMessage(dht::Key_t{}, TxID, *maybe));
}
else
{
msg.dht_msgs.emplace_back(new GotNameMessage(dht::Key_t{}, TxID, service::EncryptedName{}));
}
path->SendRoutingMessage(msg, r);
});
router->RpcClient()->LookupLNSNameHash(
NameHash, [router, pathID = pathID, TxID = TxID](auto maybe) {
auto path = router->pathContext().GetPathForTransfer(pathID);
if (path == nullptr)
return;
routing::PathDHTMessage msg;
if (maybe.has_value())
{
msg.dht_msgs.emplace_back(new GotNameMessage(dht::Key_t{}, TxID, *maybe));
}
else
{
msg.dht_msgs.emplace_back(
new GotNameMessage(dht::Key_t{}, TxID, service::EncryptedName{}));
}
path->SendRoutingMessage(msg, router);
});
return true;
}

@ -16,7 +16,7 @@ namespace llarp::dht
bool
handle_message(
struct llarp_dht_context* dht,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
Key_t NameHash;

@ -13,9 +13,9 @@ namespace llarp::dht
{
bool
RelayedFindRouterMessage::handle_message(
llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
auto& dht = *ctx->impl;
/// lookup for us, send an immeidate reply
const Key_t us = dht.OurKey();
const Key_t k{targetKey};
@ -118,10 +118,9 @@ namespace llarp::dht
bool
FindRouterMessage::handle_message(
llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
auto& dht = *ctx->impl;
auto router = dht.GetRouter();
router->NotifyRouterEvent<tooling::FindRouterReceivedEvent>(router->pubkey(), *this);

@ -30,7 +30,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
RouterID targetKey;
@ -51,7 +51,7 @@ namespace llarp::dht
/// TODO: smart path expiration logic needs to be implemented
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
};
} // namespace llarp::dht

@ -17,9 +17,9 @@ namespace llarp::dht
bool
GotIntroMessage::handle_message(
llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>& /*replies*/) const
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& /*replies*/) const
{
auto& dht = *ctx->impl;
auto* router = dht.GetRouter();
router->NotifyRouterEvent<tooling::GotIntroReceivedEvent>(
@ -60,11 +60,11 @@ namespace llarp::dht
bool
RelayedGotIntroMessage::handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
[[maybe_unused]] std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
// TODO: implement me better?
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto pathset = dht.GetRouter()->pathContext().GetLocalPathSet(pathID);
if (pathset)
{
auto copy = std::make_shared<const RelayedGotIntroMessage>(*this);

@ -46,7 +46,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
};
@ -57,7 +57,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
};

@ -58,9 +58,9 @@ namespace llarp::dht
bool
GotNameMessage::handle_message(
struct llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>&) const
AbstractDHTMessageHandler& dht, std::vector<std::unique_ptr<AbstractDHTMessage>>&) const
{
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto pathset = dht.GetRouter()->pathContext().GetLocalPathSet(pathID);
if (pathset == nullptr)
return false;
auto copy = std::make_shared<const GotNameMessage>(*this);

@ -17,7 +17,7 @@ namespace llarp::dht
bool
handle_message(
struct llarp_dht_context* dht,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
service::EncryptedName result;

@ -75,13 +75,12 @@ namespace llarp::dht
bool
GotRouterMessage::handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
[[maybe_unused]] std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
auto& dht = *ctx->impl;
if (relayed)
{
auto pathset = ctx->impl->GetRouter()->pathContext().GetLocalPathSet(pathID);
auto pathset = dht.GetRouter()->pathContext().GetLocalPathSet(pathID);
auto copy = std::make_shared<const GotRouterMessage>(*this);
return pathset && pathset->HandleGotRouterMessage(copy);
}

@ -52,7 +52,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
std::vector<RouterContact> foundRCs;

@ -52,16 +52,16 @@ namespace llarp::dht
bool
PublishIntroMessage::handle_message(
llarp_dht_context* ctx, std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const
{
const auto now = ctx->impl->Now();
const auto now = dht.Now();
const llarp::dht::Key_t addr{introset.derivedSigningKey.data()};
auto router = ctx->impl->GetRouter();
auto router = dht.GetRouter();
router->NotifyRouterEvent<tooling::PubIntroReceivedEvent>(
router->pubkey(), Key_t(relayed ? router->pubkey() : From.data()), addr, txID, relayOrder);
auto& dht = *ctx->impl;
if (!introset.Verify(now))
{
llarp::LogWarn("Received PublishIntroMessage with invalid introset: ", introset);

@ -40,7 +40,7 @@ namespace llarp::dht
bool
handle_message(
llarp_dht_context* ctx,
AbstractDHTMessageHandler& dht,
std::vector<std::unique_ptr<AbstractDHTMessage>>& replies) const override;
};
} // namespace llarp::dht

@ -13,7 +13,7 @@ namespace llarp::dht
PublishServiceJob::PublishServiceJob(
const TXOwner& asker,
const service::EncryptedIntroSet& introset_,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint64_t relayOrder_)
: TX<TXOwner, service::EncryptedIntroSet>(asker, asker, ctx)
, relayOrder(relayOrder_)
@ -50,7 +50,7 @@ namespace llarp::dht
const PathID_t& fromID,
uint64_t _txid,
const service::EncryptedIntroSet& introset,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint64_t relayOrder)
: PublishServiceJob(peer, introset, ctx, relayOrder), localPath(fromID), txid(_txid)
{}

@ -18,7 +18,7 @@ namespace llarp::dht
PublishServiceJob(
const TXOwner& asker,
const service::EncryptedIntroSet& introset,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint64_t relayOrder);
bool
@ -40,7 +40,7 @@ namespace llarp::dht
const PathID_t& fromID,
uint64_t txid,
const service::EncryptedIntroSet& introset,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint64_t relayOrder);
void

@ -16,7 +16,7 @@ namespace llarp
RecursiveRouterLookup::RecursiveRouterLookup(
const TXOwner& _whoasked,
const RouterID& _target,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
RouterLookupHandler result)
: TX<RouterID, RouterContact>(_whoasked, _target, ctx), resultHandler(std::move(result))

@ -16,7 +16,7 @@ namespace llarp
RecursiveRouterLookup(
const TXOwner& whoasked,
const RouterID& target,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
RouterLookupHandler result);
bool

@ -12,7 +12,7 @@ namespace llarp
ServiceAddressLookup::ServiceAddressLookup(
const TXOwner& asker,
const Key_t& addr,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint32_t order,
service::EncryptedIntroSetLookupHandler handler)
: TX<TXOwner, service::EncryptedIntroSet>(asker, asker, ctx)

@ -21,7 +21,7 @@ namespace llarp
ServiceAddressLookup(
const TXOwner& asker,
const Key_t& addr,
AbstractContext* ctx,
AbstractDHTMessageHandler* ctx,
uint32_t relayOrder,
service::EncryptedIntroSetLookupHandler handler);

@ -13,7 +13,10 @@ namespace llarp
{
uint64_t recursionDepth;
TagLookup(
const TXOwner& asker, const service::Tag& tag, AbstractContext* ctx, uint64_t recursion)
const TXOwner& asker,
const service::Tag& tag,
AbstractDHTMessageHandler* ctx,
uint64_t recursion)
: TX<service::Tag, service::EncryptedIntroSet>(asker, tag, ctx), recursionDepth(recursion)
{}

@ -13,18 +13,18 @@ namespace llarp
{
namespace dht
{
struct AbstractContext;
struct AbstractDHTMessageHandler;
template <typename K, typename V>
struct TX
{
K target;
AbstractContext* parent;
AbstractDHTMessageHandler* parent;
std::set<Key_t> peersAsked;
std::vector<V> valuesFound;
TXOwner whoasked;
TX(const TXOwner& asker, const K& k, AbstractContext* p)
TX(const TXOwner& asker, const K& k, AbstractDHTMessageHandler* p)
: target(k), parent(p), whoasked(asker)
{}

@ -18,4 +18,10 @@ namespace llarp
virtual void
bt_encode(oxenc::bt_list_producer& btlp) const = 0;
};
struct AbstractMessageHandler
{
virtual bool
handle_message(AbstractSerializable&) = 0;
};
} // namespace llarp

@ -1,6 +1,7 @@
#include "dht_immediate.hpp"
#include <llarp/router/abstractrouter.hpp>
#include <llarp/dht/context.hpp>
namespace llarp
{
@ -66,9 +67,10 @@ namespace llarp
DHTImmediateMessage reply;
reply.session = session;
bool result = true;
for (auto& msg : msgs)
auto dht = router->dht();
for (const auto& msg : msgs)
{
result &= msg->handle_message(router->dht(), reply.msgs);
result &= dht->handle_message(*msg, reply.msgs);
}
if (reply.msgs.size())
{

@ -1,5 +1,6 @@
#include "path.hpp"
#include <llarp/dht/context.hpp>
#include <llarp/exit/exit_messages.hpp>
#include <llarp/link/i_link_manager.hpp>
#include <llarp/messages/discard.hpp>
@ -788,7 +789,7 @@ namespace llarp::path
{
MarkActive(r->Now());
routing::PathDHTMessage reply;
if (!msg.handle_message(r->dht(), reply.dht_msgs))
if (not r->dht()->handle_message(msg, reply.dht_msgs))
return false;
if (reply.dht_msgs.size())
return SendRoutingMessage(reply, r);

@ -256,7 +256,7 @@ namespace llarp::path
bool
TransitHop::HandleDHTMessage(const llarp::dht::AbstractDHTMessage& msg, AbstractRouter* r)
{
return r->dht()->impl->RelayRequestForPath(info.rxID, msg);
return r->dht()->RelayRequestForPath(info.rxID, msg);
}
bool

@ -44,6 +44,11 @@ namespace llarp
struct I_RCLookupHandler;
struct RoutePoker;
namespace dht
{
struct AbstractDHTMessageHandler;
}
namespace dns
{
class I_SystemSettings;
@ -114,7 +119,7 @@ namespace llarp
virtual const std::shared_ptr<rpc::LokidRpcClient>&
RpcClient() const = 0;
virtual llarp_dht_context*
virtual std::shared_ptr<dht::AbstractDHTMessageHandler>
dht() const = 0;
virtual const std::shared_ptr<NodeDB>&

@ -36,15 +36,11 @@ namespace llarp
ent.pathid = msg.pathid;
ent.priority = msg.priority();
// std::array<byte_t, MAX_LINK_MSG_SIZE> linkmsg_buffer;
// llarp_buffer_t buf{linkmsg_buffer};
llarp_buffer _buf{MAX_LINK_MSG_SIZE};
std::string _buf;
_buf.reserve(MAX_LINK_MSG_SIZE);
if (!EncodeBuffer(msg, _buf))
{
return false;
}
ent.message.resize(_buf.size());
@ -185,13 +181,10 @@ namespace llarp
will be used
*/
bool
OutboundMessageHandler::EncodeBuffer(const AbstractLinkMessage& msg, llarp_buffer& buf)
OutboundMessageHandler::EncodeBuffer(const AbstractLinkMessage& msg, std::string& buf)
{
if (auto str = msg.bt_encode(); not str.empty())
{
buf = llarp_buffer{std::move(str)};
if (buf = msg.bt_encode(); not buf.empty())
return true;
}
log::error(link_cat, "Error: OutboundMessageHandler failed to encode outbound message!");
return false;

@ -123,7 +123,7 @@ namespace llarp
QueueSessionCreation(const RouterID& remote);
bool
EncodeBuffer(const AbstractLinkMessage& msg, llarp_buffer& buf);
EncodeBuffer(const AbstractLinkMessage& msg, std::string& buf);
/* sends the message along to the link layer, and hopefully out to the network
*

@ -158,7 +158,7 @@ namespace llarp
void
OutboundSessionMaker::Init(
AbstractRouter* router,
ILinkManager* linkManager,
LinkManager* linkManager,
I_RCLookupHandler* rcLookup,
Profiling* profiler,
EventLoop_ptr loop,

@ -55,7 +55,7 @@ namespace llarp
void
Init(
AbstractRouter* router,
ILinkManager* linkManager,
LinkManager* linkManager,
I_RCLookupHandler* rcLookup,
Profiling* profiler,
EventLoop_ptr loop,

@ -114,7 +114,7 @@ namespace llarp
LogWarn("cannot lookup ", router, " anonymously");
}
if (!_dht->impl->LookupRouter(router, fn))
if (!_dht->LookupRouter(router, fn))
{
FinalizeRequest(router, nullptr, RCRequestResult::RouterNotFound);
}
@ -196,11 +196,11 @@ namespace llarp
{
if (not SessionIsAllowed(rc.pubkey))
{
_dht->impl->DelRCNodeAsync(dht::Key_t{rc.pubkey});
_dht->DelRCNodeAsync(dht::Key_t{rc.pubkey});
return false;
}
if (not rc.Verify(_dht->impl->Now()))
if (not rc.Verify(_dht->Now()))
{
LogWarn("RC for ", RouterID(rc.pubkey), " is invalid");
return false;
@ -211,7 +211,7 @@ namespace llarp
{
LogDebug("Adding or updating RC for ", RouterID(rc.pubkey), " to nodedb and dht.");
_loop->call([rc, n = _nodedb] { n->PutIfNewer(rc); });
_dht->impl->PutRCNodeAsync(rc);
_dht->PutRCNodeAsync(rc);
}
return true;
@ -252,9 +252,9 @@ namespace llarp
_work(func);
// update dht if required
if (_dht->impl->Nodes()->HasNode(dht::Key_t{newrc.pubkey}))
if (_dht->Nodes()->HasNode(dht::Key_t{newrc.pubkey}))
{
_dht->impl->Nodes()->PutNode(newrc);
_dht->Nodes()->PutNode(newrc);
}
// TODO: check for other places that need updating the RC
@ -296,7 +296,7 @@ namespace llarp
for (const auto& rc : _bootstrapRCList)
{
LogInfo("Doing explore via bootstrap node: ", RouterID(rc.pubkey));
_dht->impl->ExploreNetworkVia(dht::Key_t{rc.pubkey});
_dht->ExploreNetworkVia(dht::Key_t{rc.pubkey});
}
}
@ -333,7 +333,7 @@ namespace llarp
return;
}
// service nodes gossip, not explore
if (_dht->impl->GetRouter()->IsServiceNode())
if (_dht->GetRouter()->IsServiceNode())
return;
// explore via every connected peer
@ -357,7 +357,7 @@ namespace llarp
void
RCLookupHandler::Init(
llarp_dht_context* dht,
std::shared_ptr<dht::AbstractDHTMessageHandler> dht,
std::shared_ptr<NodeDB> nodedb,
EventLoop_ptr loop,
WorkerFunc_t dowork,

@ -17,6 +17,11 @@ namespace llarp
class NodeDB;
class EventLoop;
namespace dht
{
struct AbstractDHTMessageHandler;
} // namespace dht
namespace service
{
struct Context;
@ -94,7 +99,7 @@ namespace llarp
void
Init(
llarp_dht_context* dht,
std::shared_ptr<dht::AbstractDHTMessageHandler> dht,
std::shared_ptr<NodeDB> nodedb,
std::shared_ptr<EventLoop> loop,
WorkerFunc_t dowork,
@ -128,7 +133,7 @@ namespace llarp
mutable util::Mutex _mutex; // protects pendingCallbacks, whitelistRouters
llarp_dht_context* _dht = nullptr;
std::shared_ptr<dht::AbstractDHTMessageHandler> _dht = nullptr;
std::shared_ptr<NodeDB> _nodedb;
std::shared_ptr<EventLoop> _loop;
WorkerFunc_t _work = nullptr;

@ -54,7 +54,7 @@ namespace llarp
, _vpnPlatform{std::move(vpnPlatform)}
, paths{this}
, _exitContext{this}
, _dht{llarp_dht_context_new(this)}
, _dht{dht::make_handler()}
, m_DiskThread{m_lmq->add_tagged_thread("disk")}
, inbound_link_msg_parser{this}
, _hiddenServiceContext{this}
@ -76,7 +76,7 @@ namespace llarp
Router::~Router()
{
llarp_dht_context_free(_dht);
_dht.reset();
}
// TODO: investigate changes needed for libquic integration
@ -103,7 +103,7 @@ namespace llarp
return util::StatusObject{
{"running", true},
{"numNodesKnown", _nodedb->NumLoaded()},
{"dht", _dht->impl->ExtractStatus()},
{"dht", _dht->ExtractStatus()},
{"services", _hiddenServiceContext.ExtractStatus()},
{"exit", _exitContext.ExtractStatus()},
{"links", _linkManager.ExtractStatus()},
@ -1183,7 +1183,7 @@ namespace llarp
Router::SessionClosed(RouterID remote)
{
dht::Key_t k(remote);
dht()->impl->Nodes()->DelNode(k);
dht()->Nodes()->DelNode(k);
LogInfo("Session to ", remote, " fully closed");
if (IsServiceNode())
@ -1366,12 +1366,12 @@ namespace llarp
_nodedb->LoadFromDisk();
}
llarp_dht_context_start(dht(), pubkey());
_dht->Init(llarp::dht::Key_t(pubkey()), this);
for (const auto& rc : bootstrapRCList)
{
nodedb()->Put(rc);
_dht->impl->Nodes()->PutNode(rc);
_dht->Nodes()->PutNode(rc);
LogInfo("added bootstrap node ", RouterID{rc.pubkey});
}
@ -1589,7 +1589,7 @@ namespace llarp
{
LogInfo("accepting transit traffic");
paths.AllowTransit();
llarp_dht_allow_transit(dht());
_dht->AllowTransit() = true;
_exitContext.AddExitEndpoint("default", m_Config->network, m_Config->dns);
return true;
}

@ -7,6 +7,7 @@
#include <llarp/config/key_manager.hpp>
#include <llarp/constants/link_layer.hpp>
#include <llarp/crypto/types.hpp>
#include <llarp/dht/context.hpp>
#include <llarp/ev/ev.hpp>
#include <llarp/exit/context.hpp>
#include <llarp/handlers/tun.hpp>
@ -101,7 +102,7 @@ namespace llarp
return m_lokidRpcClient;
}
llarp_dht_context*
std::shared_ptr<dht::AbstractDHTMessageHandler>
dht() const override
{
return _dht;
@ -230,7 +231,7 @@ namespace llarp
exit::Context _exitContext;
SecretKey _identity;
SecretKey _encryption;
llarp_dht_context* _dht = nullptr;
std::shared_ptr<dht::AbstractDHTMessageHandler> _dht;
std::shared_ptr<NodeDB> _nodedb;
llarp_time_t _startedAt;
const oxenmq::TaggedThreadID m_DiskThread;
@ -593,7 +594,7 @@ namespace llarp
AddAddressToRC(AddressInfo& ai);
protected:
virtual void
void
HandleRouterEvent(tooling::RouterEventPtr event) const override;
virtual bool

Loading…
Cancel
Save