2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
#include <llarp/router_id.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/thread/threading.hpp>
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
#include <chrono>
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <set>
|
2021-02-02 14:35:40 +00:00
|
|
|
#include <unordered_set>
|
2019-06-26 21:39:29 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
struct llarp_dht_context;
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2021-02-02 14:35:40 +00:00
|
|
|
class NodeDB;
|
2021-03-02 07:02:59 +00:00
|
|
|
class EventLoop;
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2023-09-13 15:57:29 +00:00
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
struct AbstractDHTMessageHandler;
|
|
|
|
} // namespace dht
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
struct Context;
|
|
|
|
} // namespace service
|
|
|
|
|
2023-09-29 21:00:13 +00:00
|
|
|
struct Contacts;
|
2023-09-13 19:57:18 +00:00
|
|
|
struct LinkManager;
|
|
|
|
struct RouterContact;
|
|
|
|
|
|
|
|
enum class RCRequestResult
|
|
|
|
{
|
|
|
|
Success,
|
|
|
|
InvalidRouter,
|
|
|
|
RouterNotFound,
|
|
|
|
BadRC
|
|
|
|
};
|
|
|
|
|
|
|
|
using RCRequestCallback =
|
|
|
|
std::function<void(const RouterID&, const RouterContact* const, const RCRequestResult)>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
struct RCLookupHandler
|
2019-06-26 21:39:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-09-15 14:55:32 +00:00
|
|
|
using worker_func = std::function<void(std::function<void()>)>;
|
|
|
|
using callback_que = std::list<RCRequestCallback>;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
~RCLookupHandler() = default;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
add_valid_router(const RouterID& router) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
remove_valid_router(const RouterID& router) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
set_router_whitelist(
|
2022-10-14 23:55:21 +00:00
|
|
|
const std::vector<RouterID>& whitelist,
|
|
|
|
const std::vector<RouterID>& greylist,
|
|
|
|
const std::vector<RouterID>& greenlist
|
|
|
|
|
2023-09-13 19:57:18 +00:00
|
|
|
) EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-01-16 04:12:38 +00:00
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
has_received_whitelist() const;
|
2020-01-16 04:12:38 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
get_rc(const RouterID& router, RCRequestCallback callback, bool forceLookup = false)
|
2020-04-07 18:38:56 +00:00
|
|
|
EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_path_allowed(const RouterID& remote) const EXCLUDES(_mutex);
|
2021-06-07 14:57:33 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_session_allowed(const RouterID& remote) const EXCLUDES(_mutex);
|
2021-06-07 14:57:33 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_grey_listed(const RouterID& remote) const EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2022-10-14 23:55:21 +00:00
|
|
|
// "greenlist" = new routers (i.e. "green") that aren't fully funded yet
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_green_listed(const RouterID& remote) const EXCLUDES(_mutex);
|
2022-10-14 23:55:21 +00:00
|
|
|
|
|
|
|
// registered just means that there is at least an operator stake, but doesn't require the node
|
|
|
|
// be fully funded, active, or not decommed. (In other words: it is any of the white, grey, or
|
|
|
|
// green list).
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_registered(const RouterID& remote) const EXCLUDES(_mutex);
|
2022-10-14 23:55:21 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
check_rc(const RouterContact& rc) const;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
get_random_whitelist_router(RouterID& router) const EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
check_renegotiate_valid(RouterContact newrc, RouterContact oldrc);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
periodic_update(llarp_time_t now);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
explore_network();
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2019-09-03 15:56:56 +00:00
|
|
|
size_t
|
2023-09-15 14:55:32 +00:00
|
|
|
num_strict_connect_routers() const;
|
2019-09-03 15:56:56 +00:00
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
init(
|
2023-09-29 21:00:13 +00:00
|
|
|
std::shared_ptr<Contacts> contacts,
|
2021-02-02 14:35:40 +00:00
|
|
|
std::shared_ptr<NodeDB> nodedb,
|
2021-03-02 07:02:59 +00:00
|
|
|
std::shared_ptr<EventLoop> loop,
|
2023-09-15 14:55:32 +00:00
|
|
|
worker_func dowork,
|
2023-09-13 19:57:18 +00:00
|
|
|
LinkManager* linkManager,
|
2020-04-07 18:38:56 +00:00
|
|
|
service::Context* hiddenServiceContext,
|
2021-02-02 14:35:40 +00:00
|
|
|
const std::unordered_set<RouterID>& strictConnectPubkeys,
|
2020-04-07 18:38:56 +00:00
|
|
|
const std::set<RouterContact>& bootstrapRCList,
|
|
|
|
bool useWhitelist_arg,
|
|
|
|
bool isServiceNode_arg);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2021-06-06 12:32:23 +00:00
|
|
|
std::unordered_set<RouterID>
|
2023-09-15 14:55:32 +00:00
|
|
|
whitelist() const
|
2021-06-06 12:32:23 +00:00
|
|
|
{
|
|
|
|
util::Lock lock{_mutex};
|
2023-09-15 14:55:32 +00:00
|
|
|
return router_whitelist;
|
2021-06-06 12:32:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 21:39:29 +00:00
|
|
|
private:
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
handle_dht_lookup_result(RouterID remote, const std::vector<RouterContact>& results);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
has_pending_lookup(RouterID remote) const EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-09-15 14:55:32 +00:00
|
|
|
is_remote_in_bootstrap(const RouterID& remote) const;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
void
|
2023-09-15 14:55:32 +00:00
|
|
|
finalize_request(const RouterID& router, const RouterContact* const rc, RCRequestResult result)
|
2020-04-07 18:38:56 +00:00
|
|
|
EXCLUDES(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
mutable util::Mutex _mutex; // protects pendingCallbacks, whitelistRouters
|
|
|
|
|
2023-09-29 21:00:13 +00:00
|
|
|
std::shared_ptr<Contacts> contacts = nullptr;
|
2023-09-15 14:55:32 +00:00
|
|
|
std::shared_ptr<NodeDB> node_db;
|
|
|
|
std::shared_ptr<EventLoop> loop;
|
|
|
|
worker_func work_func = nullptr;
|
|
|
|
service::Context* hidden_service_context = nullptr;
|
|
|
|
LinkManager* link_manager = nullptr;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
|
|
|
/// explicit whitelist of routers we will connect to directly (not for
|
|
|
|
/// service nodes)
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_set<RouterID> strict_connect_pubkeys;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::set<RouterContact> bootstrap_rc_list;
|
|
|
|
std::unordered_set<RouterID> boostrap_rid_list;
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<RouterID, callback_que> pending_callbacks GUARDED_BY(_mutex);
|
2019-06-26 21:39:29 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
bool useWhitelist = false;
|
2019-06-26 21:39:29 +00:00
|
|
|
bool isServiceNode = false;
|
|
|
|
|
2022-10-14 23:55:21 +00:00
|
|
|
// whitelist = active routers
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_set<RouterID> router_whitelist GUARDED_BY(_mutex);
|
2022-10-14 23:55:21 +00:00
|
|
|
// greylist = fully funded, but decommissioned routers
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_set<RouterID> router_greylist GUARDED_BY(_mutex);
|
2022-10-14 23:55:21 +00:00
|
|
|
// greenlist = registered but not fully-staked routers
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_set<RouterID> router_greenlist GUARDED_BY(_mutex);
|
2020-01-17 21:54:34 +00:00
|
|
|
|
|
|
|
using TimePoint = std::chrono::steady_clock::time_point;
|
2023-09-15 14:55:32 +00:00
|
|
|
std::unordered_map<RouterID, TimePoint> router_lookup_times;
|
2019-06-26 21:39:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|