2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
|
|
|
#include <llarp/config/key_manager.hpp>
|
2019-12-06 18:21:14 +00:00
|
|
|
#include <memory>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/types.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
|
|
|
#include "i_outbound_message_handler.hpp"
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <vector>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/ev/ev.hpp>
|
2019-05-03 13:15:03 +00:00
|
|
|
#include <functional>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/router_contact.hpp>
|
|
|
|
#include <llarp/tooling/router_event.hpp>
|
|
|
|
#include <llarp/peerstats/peer_db.hpp>
|
2021-06-06 12:32:23 +00:00
|
|
|
#include <llarp/consensus/reachability_testing.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2021-04-06 12:25:46 +00:00
|
|
|
#include <optional>
|
|
|
|
|
2020-03-12 17:50:46 +00:00
|
|
|
#ifdef LOKINET_HIVE
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/tooling/router_event.hpp>
|
2020-03-12 17:50:46 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
struct llarp_buffer_t;
|
|
|
|
struct llarp_dht_context;
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
namespace oxenmq
|
2020-05-19 18:53:03 +00:00
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
class OxenMQ;
|
2020-05-19 18:53:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
class NodeDB;
|
2019-02-22 16:21:05 +00:00
|
|
|
struct Config;
|
2019-02-11 19:45:42 +00:00
|
|
|
struct RouterID;
|
|
|
|
struct ILinkMessage;
|
|
|
|
struct ILinkSession;
|
|
|
|
struct PathID_t;
|
|
|
|
struct Profiling;
|
|
|
|
struct SecretKey;
|
|
|
|
struct Signature;
|
2019-06-26 21:39:29 +00:00
|
|
|
struct IOutboundMessageHandler;
|
|
|
|
struct IOutboundSessionMaker;
|
|
|
|
struct ILinkManager;
|
|
|
|
struct I_RCLookupHandler;
|
2020-09-24 00:28:38 +00:00
|
|
|
struct RoutePoker;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
namespace exit
|
|
|
|
{
|
|
|
|
struct Context;
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:41:42 +00:00
|
|
|
namespace rpc
|
|
|
|
{
|
|
|
|
struct LokidRpcClient;
|
|
|
|
}
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
namespace path
|
|
|
|
{
|
|
|
|
struct PathContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace routing
|
|
|
|
{
|
|
|
|
struct IMessageHandler;
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:18:54 +00:00
|
|
|
namespace service
|
|
|
|
{
|
2019-02-22 16:21:05 +00:00
|
|
|
struct Context;
|
|
|
|
}
|
|
|
|
|
2019-05-18 18:46:49 +00:00
|
|
|
namespace thread
|
|
|
|
{
|
|
|
|
class ThreadPool;
|
|
|
|
}
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
namespace vpn
|
|
|
|
{
|
|
|
|
class Platform;
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:35:40 +00:00
|
|
|
using LMQ_ptr = std::shared_ptr<oxenmq::OxenMQ>;
|
2020-05-19 18:53:03 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
struct AbstractRouter : public std::enable_shared_from_this<AbstractRouter>
|
2019-02-11 19:45:42 +00:00
|
|
|
{
|
2020-02-27 20:17:37 +00:00
|
|
|
#ifdef LOKINET_HIVE
|
2020-06-11 11:44:02 +00:00
|
|
|
tooling::RouterHive* hive = nullptr;
|
2020-02-27 20:17:37 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
virtual ~AbstractRouter() = default;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HandleRecvLinkMessageBuffer(ILinkSession* from, const llarp_buffer_t& msg) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual const LMQ_ptr&
|
2020-05-19 18:53:03 +00:00
|
|
|
lmq() const = 0;
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
virtual vpn::Platform*
|
|
|
|
GetVPNPlatform() const = 0;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual const std::shared_ptr<rpc::LokidRpcClient>&
|
2020-05-20 11:41:42 +00:00
|
|
|
RpcClient() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual llarp_dht_context*
|
2019-02-11 19:45:42 +00:00
|
|
|
dht() const = 0;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual const std::shared_ptr<NodeDB>&
|
2019-02-11 19:45:42 +00:00
|
|
|
nodedb() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const path::PathContext&
|
2019-02-11 19:45:42 +00:00
|
|
|
pathContext() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual path::PathContext&
|
2019-02-11 19:45:42 +00:00
|
|
|
pathContext() = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const RouterContact&
|
2019-02-11 19:45:42 +00:00
|
|
|
rc() const = 0;
|
|
|
|
|
2021-04-06 12:25:46 +00:00
|
|
|
/// modify our rc
|
|
|
|
/// modify returns nullopt if unmodified otherwise it returns the new rc to be sigend and
|
|
|
|
/// published out
|
|
|
|
virtual void
|
|
|
|
ModifyOurRC(std::function<std::optional<RouterContact>(RouterContact)> modify) = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual exit::Context&
|
2019-02-11 19:45:42 +00:00
|
|
|
exitContext() = 0;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual const std::shared_ptr<KeyManager>&
|
2019-12-06 18:21:14 +00:00
|
|
|
keyManager() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const SecretKey&
|
2019-02-11 19:45:42 +00:00
|
|
|
identity() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const SecretKey&
|
2019-02-11 19:45:42 +00:00
|
|
|
encryption() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual Profiling&
|
2019-02-11 19:45:42 +00:00
|
|
|
routerProfiling() = 0;
|
|
|
|
|
2021-03-02 02:06:20 +00:00
|
|
|
virtual const EventLoop_ptr&
|
2021-03-02 07:02:59 +00:00
|
|
|
loop() const = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2020-06-11 11:44:02 +00:00
|
|
|
/// call function in crypto worker
|
2020-06-11 11:48:40 +00:00
|
|
|
virtual void QueueWork(std::function<void(void)>) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2020-06-11 11:44:02 +00:00
|
|
|
/// call function in disk io thread
|
2020-06-11 11:48:40 +00:00
|
|
|
virtual void QueueDiskIO(std::function<void(void)>) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2020-08-27 12:43:53 +00:00
|
|
|
virtual std::shared_ptr<Config>
|
|
|
|
GetConfig() const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual service::Context&
|
2019-02-22 16:21:05 +00:00
|
|
|
hiddenServiceContext() = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const service::Context&
|
2019-02-22 16:21:05 +00:00
|
|
|
hiddenServiceContext() const = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual IOutboundMessageHandler&
|
2019-06-26 21:39:29 +00:00
|
|
|
outboundMessageHandler() = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual IOutboundSessionMaker&
|
2019-06-26 21:39:29 +00:00
|
|
|
outboundSessionMaker() = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual ILinkManager&
|
2019-06-26 21:39:29 +00:00
|
|
|
linkManager() = 0;
|
|
|
|
|
2020-09-24 00:28:38 +00:00
|
|
|
virtual RoutePoker&
|
|
|
|
routePoker() = 0;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual I_RCLookupHandler&
|
2019-06-26 21:39:29 +00:00
|
|
|
rcLookupHandler() = 0;
|
|
|
|
|
2020-05-27 01:57:27 +00:00
|
|
|
virtual std::shared_ptr<PeerDb>
|
|
|
|
peerDb() = 0;
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
Sign(Signature& sig, const llarp_buffer_t& buf) const = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2019-02-22 16:21:05 +00:00
|
|
|
virtual bool
|
2021-03-23 19:00:46 +00:00
|
|
|
Configure(std::shared_ptr<Config> conf, bool isSNode, std::shared_ptr<NodeDB> nodedb) = 0;
|
2019-02-22 16:21:05 +00:00
|
|
|
|
2020-01-06 23:13:23 +00:00
|
|
|
virtual bool
|
|
|
|
IsServiceNode() const = 0;
|
|
|
|
|
2019-02-22 16:21:05 +00:00
|
|
|
virtual bool
|
2020-05-20 11:41:42 +00:00
|
|
|
StartRpcServer() = 0;
|
2019-10-04 09:10:55 +00:00
|
|
|
|
|
|
|
virtual bool
|
|
|
|
Run() = 0;
|
2019-02-22 16:21:05 +00:00
|
|
|
|
2019-10-09 13:08:38 +00:00
|
|
|
virtual bool
|
|
|
|
IsRunning() const = 0;
|
|
|
|
|
2019-12-07 19:58:19 +00:00
|
|
|
virtual bool
|
|
|
|
LooksAlive() const = 0;
|
|
|
|
|
2019-02-22 16:21:05 +00:00
|
|
|
/// stop running the router logic gracefully
|
|
|
|
virtual void
|
|
|
|
Stop() = 0;
|
|
|
|
|
2021-02-05 21:48:57 +00:00
|
|
|
/// thaw from long sleep or network changed event
|
|
|
|
virtual void
|
|
|
|
Thaw() = 0;
|
|
|
|
|
2020-06-16 11:28:20 +00:00
|
|
|
/// non gracefully stop the router
|
|
|
|
virtual void
|
|
|
|
Die() = 0;
|
|
|
|
|
2021-11-12 13:51:39 +00:00
|
|
|
/// Trigger a pump of low level links. Idempotent.
|
2019-04-30 16:07:17 +00:00
|
|
|
virtual void
|
2021-11-12 13:51:39 +00:00
|
|
|
TriggerPump() = 0;
|
2019-04-30 16:07:17 +00:00
|
|
|
|
2019-04-03 19:05:44 +00:00
|
|
|
virtual bool
|
|
|
|
IsBootstrapNode(RouterID r) const = 0;
|
2019-04-08 12:01:52 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
virtual const byte_t*
|
2019-02-11 19:45:42 +00:00
|
|
|
pubkey() const = 0;
|
|
|
|
|
2019-02-20 12:09:18 +00:00
|
|
|
/// connect to N random routers
|
|
|
|
virtual void
|
|
|
|
ConnectToRandomRouters(int N) = 0;
|
2019-02-22 16:21:05 +00:00
|
|
|
|
2019-05-03 13:15:03 +00:00
|
|
|
virtual bool
|
|
|
|
TryConnectAsync(RouterContact rc, uint16_t tries) = 0;
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
/// called by link when a remote session has no more sessions open
|
|
|
|
virtual void
|
|
|
|
SessionClosed(RouterID remote) = 0;
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
/// returns system clock milliseconds since epoch
|
2019-02-11 19:45:42 +00:00
|
|
|
virtual llarp_time_t
|
|
|
|
Now() const = 0;
|
|
|
|
|
2019-04-22 12:25:25 +00:00
|
|
|
/// returns milliseconds since started
|
|
|
|
virtual llarp_time_t
|
|
|
|
Uptime() const = 0;
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
GetRandomGoodRouter(RouterID& r) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
SendToOrQueue(
|
2021-04-12 11:39:07 +00:00
|
|
|
const RouterID& remote, const ILinkMessage& msg, SendStatusHandler handler = nullptr) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
virtual void
|
2020-04-07 18:38:56 +00:00
|
|
|
PersistSessionUntil(const RouterID& remote, llarp_time_t until) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ParseRoutingMessageBuffer(
|
|
|
|
const llarp_buffer_t& buf, routing::IMessageHandler* h, const PathID_t& rxid) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2019-05-09 15:36:39 +00:00
|
|
|
/// count the number of service nodes we are connected to
|
2019-02-11 19:45:42 +00:00
|
|
|
virtual size_t
|
|
|
|
NumberOfConnectedRouters() const = 0;
|
|
|
|
|
2019-05-09 15:36:39 +00:00
|
|
|
/// count the number of clients that are connected to us
|
|
|
|
virtual size_t
|
|
|
|
NumberOfConnectedClients() const = 0;
|
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
GetRandomConnectedRouter(RouterContact& result) const = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
|
|
|
virtual void
|
2020-04-07 18:38:56 +00:00
|
|
|
HandleDHTLookupForExplore(RouterID remote, const std::vector<RouterContact>& results) = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
|
2020-08-21 19:09:13 +00:00
|
|
|
virtual void SetDownHook(std::function<void(void)>){};
|
|
|
|
|
2019-03-31 15:09:59 +00:00
|
|
|
/// lookup router by pubkey
|
|
|
|
/// if we are a service node this is done direct otherwise it's done via
|
|
|
|
/// path
|
|
|
|
virtual void
|
2019-05-03 13:15:03 +00:00
|
|
|
LookupRouter(RouterID remote, RouterLookupHandler resultHandler) = 0;
|
2019-03-31 15:09:59 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
/// check if newRc matches oldRC and update local rc for this remote contact
|
|
|
|
/// if valid
|
|
|
|
/// returns true on valid and updated
|
|
|
|
/// returns false otherwise
|
|
|
|
virtual bool
|
|
|
|
CheckRenegotiateValid(RouterContact newRc, RouterContact oldRC) = 0;
|
2019-02-15 22:19:19 +00:00
|
|
|
|
|
|
|
/// set router's service node whitelist
|
2019-02-18 19:44:41 +00:00
|
|
|
virtual void
|
2021-06-07 14:57:33 +00:00
|
|
|
SetRouterWhitelist(
|
2021-06-07 18:08:41 +00:00
|
|
|
const std::vector<RouterID>& whitelist, const std::vector<RouterID>& greylist) = 0;
|
2019-02-15 22:19:19 +00:00
|
|
|
|
2021-06-06 12:32:23 +00:00
|
|
|
virtual std::unordered_set<RouterID>
|
|
|
|
GetRouterWhitelist() const = 0;
|
|
|
|
|
2019-02-18 19:44:41 +00:00
|
|
|
/// visit each connected link session
|
2019-02-15 22:19:19 +00:00
|
|
|
virtual void
|
2020-04-07 18:38:56 +00:00
|
|
|
ForEachPeer(std::function<void(const ILinkSession*, bool)> visit, bool randomize) const = 0;
|
2019-04-17 19:05:54 +00:00
|
|
|
|
|
|
|
virtual bool
|
2021-06-07 14:57:33 +00:00
|
|
|
SessionToRouterAllowed(const RouterID& router) const = 0;
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
PathToRouterAllowed(const RouterID& router) const = 0;
|
2019-04-19 15:10:26 +00:00
|
|
|
|
2021-05-13 11:30:53 +00:00
|
|
|
/// return true if we have an exit as a client
|
|
|
|
virtual bool
|
|
|
|
HasClientExit() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-05-05 12:21:39 +00:00
|
|
|
virtual path::BuildLimiter&
|
|
|
|
pathBuildLimiter() = 0;
|
|
|
|
|
2019-05-28 11:35:26 +00:00
|
|
|
/// return true if we have at least 1 session to this router in either
|
|
|
|
/// direction
|
2019-05-27 19:01:09 +00:00
|
|
|
virtual bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HasSessionTo(const RouterID& router) const = 0;
|
2019-05-27 19:01:09 +00:00
|
|
|
|
2020-02-20 21:37:39 +00:00
|
|
|
virtual uint32_t
|
2020-02-20 21:57:48 +00:00
|
|
|
NextPathBuildNumber() = 0;
|
2020-02-20 21:37:39 +00:00
|
|
|
|
|
|
|
virtual std::string
|
|
|
|
ShortName() const = 0;
|
|
|
|
|
2019-04-19 15:10:26 +00:00
|
|
|
virtual util::StatusObject
|
|
|
|
ExtractStatus() const = 0;
|
2020-01-30 17:23:16 +00:00
|
|
|
|
2021-10-13 11:20:36 +00:00
|
|
|
virtual util::StatusObject
|
|
|
|
ExtractSummaryStatus() const = 0;
|
|
|
|
|
2020-01-30 17:23:16 +00:00
|
|
|
/// gossip an rc if required
|
|
|
|
virtual void
|
|
|
|
GossipRCIfNeeded(const RouterContact rc) = 0;
|
2020-02-27 01:18:38 +00:00
|
|
|
|
2020-07-01 19:46:52 +00:00
|
|
|
/// Templated convenience function to generate a RouterHive event and
|
|
|
|
/// delegate to non-templated (and overridable) function for handling.
|
2020-04-07 18:38:56 +00:00
|
|
|
template <class EventType, class... Params>
|
2020-03-12 17:50:46 +00:00
|
|
|
void
|
2020-06-11 11:44:02 +00:00
|
|
|
NotifyRouterEvent([[maybe_unused]] Params&&... args) const
|
2020-03-12 17:50:46 +00:00
|
|
|
{
|
2020-07-01 19:46:52 +00:00
|
|
|
// TODO: no-op when appropriate
|
|
|
|
auto event = std::make_unique<EventType>(args...);
|
|
|
|
HandleRouterEvent(std::move(event));
|
2020-03-12 17:50:46 +00:00
|
|
|
}
|
2020-07-01 19:46:52 +00:00
|
|
|
|
2021-03-02 18:18:22 +00:00
|
|
|
#if defined(ANDROID)
|
|
|
|
virtual int
|
|
|
|
GetOutboundUDPSocket() const = 0;
|
|
|
|
#endif
|
|
|
|
|
2020-07-01 19:46:52 +00:00
|
|
|
protected:
|
|
|
|
/// Virtual function to handle RouterEvent. HiveRouter overrides this in
|
|
|
|
/// order to inject the event. The default implementation in Router simply
|
|
|
|
/// logs it.
|
|
|
|
virtual void
|
|
|
|
HandleRouterEvent(tooling::RouterEventPtr event) const = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
};
|
|
|
|
} // namespace llarp
|