1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/include/llarp/pathset.hpp

198 lines
4.9 KiB
C++
Raw Normal View History

#ifndef LLARP_PATHSET_HPP
#define LLARP_PATHSET_HPP
2018-11-19 23:45:37 +01:00
#include <llarp/time.hpp>
2018-07-12 20:21:44 +02:00
#include <functional>
#include <list>
#include <llarp/path_types.hpp>
#include <llarp/router_id.hpp>
2018-07-18 05:10:21 +02:00
#include <llarp/routing/message.hpp>
#include <llarp/service/IntroSet.hpp>
2018-07-18 05:10:21 +02:00
#include <llarp/service/lookup.hpp>
2018-08-30 20:48:43 +02:00
#include <llarp/dht/messages/all.hpp>
#include <map>
#include <tuple>
namespace llarp
{
2018-07-09 19:32:11 +02:00
namespace dht
{
struct GotIntroMessage;
}
namespace path
{
2018-11-14 19:02:27 +01:00
/// status of a path
enum PathStatus
{
ePathBuilding,
ePathEstablished,
ePathTimeout,
ePathExpired
};
2018-11-14 19:02:27 +01:00
/// the role of this path can fuffill
using PathRole = int;
/// capable of any role
constexpr PathRole ePathRoleAny = 0;
/// outbound hs traffic capabale
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 13:31:36 +01:00
/// service node capable
constexpr PathRole ePathRoleSVC = (1 << 3);
2018-11-14 19:02:27 +01:00
/// dht message capable
2018-11-21 13:31:36 +01:00
constexpr PathRole ePathRoleDHT = (1 << 4);
2018-11-14 19:02:27 +01: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);
/// tick owned paths
void
Tick(llarp_time_t now, llarp_router* r);
void
RemovePath(Path* path);
virtual void
HandlePathBuilt(__attribute__((unused)) Path* path);
virtual void
HandlePathBuildTimeout(__attribute__((unused)) Path* path);
bool
GetNewestIntro(service::Introduction& intro) const;
void
AddPath(Path* path);
Path*
2018-08-02 00:10:38 +02:00
GetByUpstream(const RouterID& remote, const PathID_t& rxid) const;
void
ExpirePaths(llarp_time_t now);
2018-11-14 19:02:27 +01:00
/// get the number of paths in this status
size_t
NumInStatus(PathStatus st) const;
2018-11-14 19:02:27 +01:00
/// get the number of paths that match the role that are available
size_t
AvailablePaths(PathRole role) const;
2018-10-29 17:48:36 +01:00
/// get time from event loop
virtual llarp_time_t
Now() const = 0;
/// return true if we should build another path
virtual bool
2018-10-29 17:48:36 +01:00
ShouldBuildMore(llarp_time_t now) const;
2018-11-14 19:02:27 +01: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 19:32:11 +02:00
/// return true if we should publish a new hidden service descriptor
2018-07-18 05:10:21 +02:00
virtual bool
ShouldPublishDescriptors(__attribute__((unused)) llarp_time_t now) const
2018-07-18 05:10:21 +02:00
{
return false;
}
2018-07-09 19:32:11 +02:00
2018-07-17 08:17:13 +02:00
/// override me in subtype
virtual bool
HandleGotIntroMessage(__attribute__((unused))
const llarp::dht::GotIntroMessage* msg)
{
return false;
}
2018-08-10 23:34:11 +02:00
/// override me in subtype
virtual bool
HandleGotRouterMessage(__attribute__((unused))
const llarp::dht::GotRouterMessage* msg)
2018-08-10 23:34:11 +02:00
{
return false;
}
virtual routing::IMessageHandler*
GetDHTHandler()
{
return nullptr;
}
Path*
2018-11-14 19:02:27 +01:00
GetEstablishedPathClosestTo(const RouterID& router,
PathRole roles = ePathRoleAny) const;
2018-07-23 01:14:29 +02:00
Path*
2018-11-14 19:02:27 +01:00
PickRandomEstablishedPath(PathRole roles = ePathRoleAny) const;
2018-08-02 00:10:38 +02:00
Path*
2018-11-14 19:02:27 +01:00
GetPathByRouter(const RouterID& router,
PathRole roles = ePathRoleAny) const;
2018-07-23 01:14:29 +02:00
Path*
2018-11-14 19:02:27 +01:00
GetNewestPathByRouter(const RouterID& router,
PathRole roles = ePathRoleAny) const;
2018-08-10 23:34:11 +02:00
Path*
GetPathByID(const PathID_t& id) const;
bool
GetCurrentIntroductionsWithFilter(
std::set< llarp::service::Introduction >& intros,
std::function< bool(const llarp::service::Introduction&) > filter)
const;
bool
GetCurrentIntroductions(
2018-07-19 06:58:39 +02:00
std::set< llarp::service::Introduction >& intros) const;
2018-07-18 05:10:21 +02:00
virtual bool
PublishIntroSet(__attribute__((unused)) llarp_router* r)
2018-07-18 05:10:21 +02:00
{
return false;
}
2018-07-17 08:17:13 +02:00
virtual bool
2018-08-30 20:48:43 +02:00
SelectHop(llarp_nodedb* db, const RouterContact& prev, RouterContact& cur,
2018-11-14 19:02:27 +01:00
size_t hop, PathRole roles) = 0;
protected:
size_t m_NumPaths;
private:
using PathInfo_t = std::pair< RouterID, PathID_t >;
2018-10-03 20:17:34 +02:00
struct PathInfoHash
{
2018-10-05 09:57:48 +02:00
size_t
operator()(const PathInfo_t& i) const
2018-10-03 20:17:34 +02:00
{
return *i.first.data_l() ^ *i.second.data_l();
}
};
using PathMap_t = std::unordered_map< PathInfo_t, Path*, PathInfoHash >;
PathMap_t m_Paths;
};
} // namespace path
} // namespace llarp
2018-08-18 16:01:21 +02:00
#endif