2018-06-25 15:12:08 +00:00
|
|
|
#ifndef LLARP_PATHSET_HPP
|
|
|
|
#define LLARP_PATHSET_HPP
|
2018-12-10 16:26:46 +00:00
|
|
|
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <path/path_types.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <router_id.hpp>
|
2018-12-12 02:04:32 +00:00
|
|
|
#include <routing/message.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/IntroSet.hpp>
|
|
|
|
#include <service/lookup.hpp>
|
2019-02-08 19:43:25 +00:00
|
|
|
#include <util/status.hpp>
|
2019-03-03 20:51:47 +00:00
|
|
|
#include <util/threading.hpp>
|
|
|
|
#include <util/time.hpp>
|
2018-12-12 00:48:54 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2018-06-25 15:12:08 +00:00
|
|
|
#include <map>
|
|
|
|
#include <tuple>
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
struct llarp_nodedb;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-01-16 00:24:16 +00:00
|
|
|
struct RouterContact;
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
namespace dht
|
|
|
|
{
|
|
|
|
struct GotIntroMessage;
|
2019-01-16 00:24:16 +00:00
|
|
|
struct GotRouterMessage;
|
|
|
|
} // namespace dht
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
namespace path
|
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
/// status of a path
|
2018-06-25 15:12:08 +00:00
|
|
|
enum PathStatus
|
|
|
|
{
|
|
|
|
ePathBuilding,
|
|
|
|
ePathEstablished,
|
|
|
|
ePathTimeout,
|
|
|
|
ePathExpired
|
|
|
|
};
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2019-01-16 00:24:16 +00:00
|
|
|
/// the role of this path can fulfill
|
2018-11-14 18:02:27 +00:00
|
|
|
using PathRole = int;
|
|
|
|
|
|
|
|
/// capable of any role
|
|
|
|
constexpr PathRole ePathRoleAny = 0;
|
2019-01-16 00:24:16 +00:00
|
|
|
/// outbound hs traffic capable
|
2018-11-14 18:02:27 +00:00
|
|
|
constexpr PathRole ePathRoleOutboundHS = (1 << 0);
|
|
|
|
/// inbound hs traffic capable
|
|
|
|
constexpr PathRole ePathRoleInboundHS = (1 << 1);
|
|
|
|
/// exit traffic capable
|
|
|
|
constexpr PathRole ePathRoleExit = (1 << 2);
|
2018-11-21 12:31:36 +00:00
|
|
|
/// service node capable
|
|
|
|
constexpr PathRole ePathRoleSVC = (1 << 3);
|
2018-11-14 18:02:27 +00:00
|
|
|
/// dht message capable
|
2018-11-21 12:31:36 +00:00
|
|
|
constexpr PathRole ePathRoleDHT = (1 << 4);
|
2018-11-14 18:02:27 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
// forward declare
|
|
|
|
struct Path;
|
|
|
|
|
|
|
|
/// a set of paths owned by an entity
|
|
|
|
struct PathSet
|
|
|
|
{
|
|
|
|
/// construct
|
|
|
|
/// @params numPaths the number of paths to maintain
|
|
|
|
PathSet(size_t numPaths);
|
|
|
|
|
2018-06-29 16:02:39 +00:00
|
|
|
/// tick owned paths
|
|
|
|
void
|
2019-02-11 19:45:42 +00:00
|
|
|
Tick(llarp_time_t now, AbstractRouter* r);
|
2018-06-29 16:02:39 +00:00
|
|
|
|
2018-11-25 16:58:27 +00:00
|
|
|
/// count the number of paths that will exist at this timestamp in future
|
|
|
|
size_t
|
|
|
|
NumPathsExistingAt(llarp_time_t futureTime) const;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
RemovePath(Path* path);
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
virtual void
|
2018-11-07 15:30:22 +00:00
|
|
|
HandlePathBuilt(__attribute__((unused)) Path* path);
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-09-26 13:04:25 +00:00
|
|
|
virtual void
|
2018-11-07 15:30:22 +00:00
|
|
|
HandlePathBuildTimeout(__attribute__((unused)) Path* path);
|
2018-09-26 13:04:25 +00:00
|
|
|
|
2018-09-27 10:51:30 +00:00
|
|
|
bool
|
|
|
|
GetNewestIntro(service::Introduction& intro) const;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
AddPath(Path* path);
|
|
|
|
|
|
|
|
Path*
|
2018-12-18 18:36:19 +00:00
|
|
|
GetByUpstream(RouterID remote, PathID_t rxid) const;
|
2018-06-25 15:12:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ExpirePaths(llarp_time_t now);
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// get the number of paths in this status
|
2018-06-25 15:12:08 +00:00
|
|
|
size_t
|
|
|
|
NumInStatus(PathStatus st) const;
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// get the number of paths that match the role that are available
|
|
|
|
size_t
|
|
|
|
AvailablePaths(PathRole role) const;
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
/// get time from event loop
|
|
|
|
virtual llarp_time_t
|
|
|
|
Now() const = 0;
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
/// stop this pathset and mark it as to be removed
|
|
|
|
virtual bool
|
|
|
|
Stop() = 0;
|
|
|
|
|
2019-02-05 14:50:33 +00:00
|
|
|
/// return true if we are stopped
|
|
|
|
virtual bool
|
|
|
|
IsStopped() const = 0;
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
/// return true if we can and should remove this pathset and underlying
|
|
|
|
/// resources from its parent context
|
|
|
|
virtual bool
|
|
|
|
ShouldRemove() const = 0;
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
/// return true if we should build another path
|
2018-08-23 15:19:16 +00:00
|
|
|
virtual bool
|
2018-10-29 16:48:36 +00:00
|
|
|
ShouldBuildMore(llarp_time_t now) const;
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
/// return true if we need another path with the given path roles
|
|
|
|
virtual bool
|
|
|
|
ShouldBuildMoreForRoles(llarp_time_t now, PathRole roles) const;
|
|
|
|
|
|
|
|
/// return the minimum number of paths we want for given roles
|
|
|
|
virtual size_t
|
|
|
|
MinRequiredForRoles(PathRole roles) const;
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
/// return true if we should publish a new hidden service descriptor
|
2018-07-18 03:10:21 +00:00
|
|
|
virtual bool
|
2018-11-07 15:30:22 +00:00
|
|
|
ShouldPublishDescriptors(__attribute__((unused)) llarp_time_t now) const
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-17 06:17:13 +00:00
|
|
|
/// override me in subtype
|
2018-07-11 16:11:19 +00:00
|
|
|
virtual bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleGotIntroMessage(__attribute__((unused))
|
2019-02-18 10:35:16 +00:00
|
|
|
const dht::GotIntroMessage* msg)
|
2018-07-11 16:11:19 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
/// override me in subtype
|
|
|
|
virtual bool
|
2018-11-07 15:30:22 +00:00
|
|
|
HandleGotRouterMessage(__attribute__((unused))
|
2019-02-18 10:35:16 +00:00
|
|
|
const dht::GotRouterMessage* msg)
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual routing::IMessageHandler*
|
|
|
|
GetDHTHandler()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
Path*
|
2018-12-18 18:36:19 +00:00
|
|
|
GetEstablishedPathClosestTo(RouterID router,
|
2018-11-14 18:02:27 +00:00
|
|
|
PathRole roles = ePathRoleAny) const;
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
Path*
|
2018-11-14 18:02:27 +00:00
|
|
|
PickRandomEstablishedPath(PathRole roles = ePathRoleAny) const;
|
2018-08-01 22:10:38 +00:00
|
|
|
|
|
|
|
Path*
|
2018-12-18 18:36:19 +00:00
|
|
|
GetPathByRouter(RouterID router, PathRole roles = ePathRoleAny) const;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2018-09-18 23:56:26 +00:00
|
|
|
Path*
|
2018-12-18 18:36:19 +00:00
|
|
|
GetNewestPathByRouter(RouterID router,
|
2018-11-14 18:02:27 +00:00
|
|
|
PathRole roles = ePathRoleAny) const;
|
2018-09-18 23:56:26 +00:00
|
|
|
|
2018-08-10 21:34:11 +00:00
|
|
|
Path*
|
2018-12-18 18:36:19 +00:00
|
|
|
GetPathByID(PathID_t id) const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2019-03-08 17:26:29 +00:00
|
|
|
Path*
|
|
|
|
GetByEndpointWithID(RouterID router, PathID_t id) const;
|
|
|
|
|
2018-10-01 14:18:17 +00:00
|
|
|
bool
|
|
|
|
GetCurrentIntroductionsWithFilter(
|
2019-02-18 10:35:16 +00:00
|
|
|
std::set< service::Introduction >& intros,
|
|
|
|
std::function< bool(const service::Introduction&) > filter) const;
|
2018-10-01 14:18:17 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
bool
|
2019-02-18 10:35:16 +00:00
|
|
|
GetCurrentIntroductions(std::set< service::Introduction >& intros) const;
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2018-07-18 03:10:21 +00:00
|
|
|
virtual bool
|
2019-02-11 19:45:42 +00:00
|
|
|
PublishIntroSet(__attribute__((unused)) AbstractRouter* r)
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-17 06:17:13 +00:00
|
|
|
|
2018-07-19 21:08:11 +00:00
|
|
|
virtual bool
|
2018-08-30 18:48:43 +00:00
|
|
|
SelectHop(llarp_nodedb* db, const RouterContact& prev, RouterContact& cur,
|
2018-11-14 18:02:27 +00:00
|
|
|
size_t hop, PathRole roles) = 0;
|
2018-07-19 21:08:11 +00:00
|
|
|
|
2018-09-27 10:51:30 +00:00
|
|
|
protected:
|
|
|
|
size_t m_NumPaths;
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
void
|
|
|
|
ForEachPath(std::function< void(Path*) > visit)
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock_t lock(&m_PathsMutex);
|
2018-12-24 16:09:05 +00:00
|
|
|
auto itr = m_Paths.begin();
|
|
|
|
while(itr != m_Paths.end())
|
|
|
|
{
|
|
|
|
visit(itr->second);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:43:25 +00:00
|
|
|
void
|
|
|
|
ForEachPath(std::function< void(const Path*) > visit) const
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock_t lock(&m_PathsMutex);
|
2019-02-08 19:43:25 +00:00
|
|
|
auto itr = m_Paths.begin();
|
|
|
|
while(itr != m_Paths.end())
|
|
|
|
{
|
|
|
|
visit(itr->second);
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-22 23:59:03 +00:00
|
|
|
using PathInfo_t = std::pair< RouterID, PathID_t >;
|
2018-10-03 18:17:34 +00:00
|
|
|
|
|
|
|
struct PathInfoHash
|
|
|
|
{
|
2018-10-05 07:57:48 +00:00
|
|
|
size_t
|
|
|
|
operator()(const PathInfo_t& i) const
|
2018-10-03 18:17:34 +00:00
|
|
|
{
|
2018-12-20 14:18:03 +00:00
|
|
|
return RouterID::Hash()(i.first) ^ PathID_t::Hash()(i.second);
|
2018-10-03 18:17:34 +00:00
|
|
|
}
|
|
|
|
};
|
2019-02-18 10:35:16 +00:00
|
|
|
using Mtx_t = util::NullMutex;
|
|
|
|
using Lock_t = util::NullLock;
|
2018-11-22 23:59:03 +00:00
|
|
|
using PathMap_t = std::unordered_map< PathInfo_t, Path*, PathInfoHash >;
|
2018-11-27 00:02:32 +00:00
|
|
|
mutable Mtx_t m_PathsMutex;
|
2018-06-26 14:52:19 +00:00
|
|
|
PathMap_t m_Paths;
|
2018-06-25 15:12:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace path
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-08-18 14:01:21 +00:00
|
|
|
#endif
|