lokinet/llarp/exit/session.hpp

230 lines
5.3 KiB
C++
Raw Normal View History

#ifndef LLARP_EXIT_SESSION_HPP
#define LLARP_EXIT_SESSION_HPP
2019-06-15 14:55:14 +00:00
#include <exit/exit_messages.hpp>
#include <net/ip_packet.hpp>
2019-01-11 01:19:36 +00:00
#include <path/pathbuilder.hpp>
2019-06-19 22:30:07 +00:00
#include <routing/transfer_traffic_message.hpp>
#include <constants/path.hpp>
2018-11-28 16:38:20 +00:00
#include <deque>
2018-12-20 12:41:17 +00:00
#include <queue>
namespace llarp
{
namespace exit
{
struct BaseSession;
using BaseSession_ptr = std::shared_ptr<BaseSession>;
2019-04-23 16:13:22 +00:00
using SessionReadyFunc = std::function<void(BaseSession_ptr)>;
2020-02-24 19:40:45 +00:00
static constexpr auto LifeSpan = path::default_lifetime;
/// a persisting exit session with an exit router
2019-04-23 16:13:22 +00:00
struct BaseSession : public llarp::path::Builder,
public std::enable_shared_from_this<BaseSession>
{
2018-11-29 13:12:35 +00:00
static constexpr size_t MaxUpstreamQueueLength = 256;
BaseSession(
const llarp::RouterID& exitRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool bundleRC);
2019-07-30 23:42:13 +00:00
~BaseSession() override;
std::shared_ptr<path::PathSet>
2019-04-23 16:13:22 +00:00
GetSelf() override
{
return shared_from_this();
}
2019-05-10 16:19:33 +00:00
void
BlacklistSnode(const RouterID snode);
2019-02-11 17:14:43 +00:00
util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const;
2019-02-08 19:43:25 +00:00
bool
ShouldBundleRC() const override
{
return m_BundleRC;
}
2019-07-30 23:42:13 +00:00
void
2019-05-07 18:15:22 +00:00
ResetInternalState() override;
2019-05-07 17:46:38 +00:00
bool UrgentBuild(llarp_time_t) const override;
2019-05-07 15:08:57 +00:00
2019-03-30 13:02:10 +00:00
void
HandlePathDied(llarp::path::Path_ptr p) override;
2019-03-30 13:02:10 +00:00
2019-04-16 19:39:58 +00:00
bool
CheckPathDead(path::Path_ptr p, llarp_time_t dlt);
2019-04-16 19:39:58 +00:00
bool
SelectHop(
llarp_nodedb* db,
const std::set<RouterID>& prev,
RouterContact& cur,
size_t hop,
llarp::path::PathRole roles) override;
bool
ShouldBuildMore(llarp_time_t now) const override;
2018-11-14 19:34:17 +00:00
void
HandlePathBuilt(llarp::path::Path_ptr p) override;
2018-11-14 19:34:17 +00:00
bool
2019-06-11 16:44:05 +00:00
QueueUpstreamTraffic(llarp::net::IPPacket pkt, const size_t packSize);
2018-11-28 16:38:20 +00:00
2019-04-30 13:56:39 +00:00
/// flush upstream to exit via paths
2018-11-28 16:38:20 +00:00
bool
2019-04-30 13:56:39 +00:00
FlushUpstream();
/// flush downstream to user via tun
void
FlushDownstream();
2018-11-28 16:38:20 +00:00
2019-03-07 15:17:29 +00:00
path::PathRole
GetRoles() const override
{
return path::ePathRoleExit;
}
/// send close and stop session
bool
Stop() override;
bool
2018-11-29 13:12:35 +00:00
IsReady() const;
2018-12-10 15:44:18 +00:00
const llarp::RouterID
Endpoint() const
{
return m_ExitRouter;
}
2018-12-13 12:27:14 +00:00
bool
IsExpired(llarp_time_t now) const;
bool
LoadIdentityFromFile(const char* fname);
void
AddReadyHook(SessionReadyFunc func);
protected:
llarp::RouterID m_ExitRouter;
llarp::SecretKey m_ExitIdentity;
std::function<bool(const llarp_buffer_t&)> m_WritePacket;
virtual void
PopulateRequest(llarp::routing::ObtainExitMessage& msg) const = 0;
2018-11-14 19:34:17 +00:00
bool
HandleTrafficDrop(llarp::path::Path_ptr p, const llarp::PathID_t& path, uint64_t s);
2018-11-14 19:34:17 +00:00
bool
HandleGotExit(llarp::path::Path_ptr p, llarp_time_t b);
2018-11-14 19:34:17 +00:00
bool
HandleTraffic(llarp::path::Path_ptr p, const llarp_buffer_t& buf, uint64_t seqno);
2018-11-14 19:34:17 +00:00
private:
std::set<RouterID> m_SnodeBlacklist;
2019-05-10 16:19:33 +00:00
using UpstreamTrafficQueue_t = std::deque<llarp::routing::TransferTrafficMessage>;
using TieredQueue_t = std::map<uint8_t, UpstreamTrafficQueue_t>;
TieredQueue_t m_Upstream;
2018-12-20 12:41:17 +00:00
using DownstreamPkt = std::pair<uint64_t, llarp::net::IPPacket>;
2018-12-20 12:41:17 +00:00
struct DownstreamPktSorter
{
bool
operator()(const DownstreamPkt& left, const DownstreamPkt& right) const
{
2018-12-20 13:06:36 +00:00
return left.first < right.first;
2018-12-20 12:41:17 +00:00
}
};
using DownstreamTrafficQueue_t =
std::priority_queue<DownstreamPkt, std::vector<DownstreamPkt>, DownstreamPktSorter>;
2018-12-20 12:41:17 +00:00
DownstreamTrafficQueue_t m_Downstream;
2018-11-29 21:19:20 +00:00
uint64_t m_Counter;
2018-12-13 12:27:14 +00:00
llarp_time_t m_LastUse;
std::vector<SessionReadyFunc> m_PendingCallbacks;
const bool m_BundleRC;
2019-03-07 15:17:29 +00:00
void
CallPendingCallbacks(bool success);
};
2018-11-29 13:12:35 +00:00
struct ExitSession final : public BaseSession
{
ExitSession(
const llarp::RouterID& snodeRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool bundleRC)
: BaseSession(snodeRouter, writepkt, r, numpaths, hoplen, bundleRC)
{
}
2019-07-30 23:42:13 +00:00
~ExitSession() override = default;
2019-03-22 14:10:30 +00:00
std::string
Name() const override;
protected:
2019-07-30 23:42:13 +00:00
void
PopulateRequest(llarp::routing::ObtainExitMessage& msg) const override
2018-11-29 13:12:35 +00:00
{
// TODO: set expiration time
msg.X = 0;
msg.E = 1;
}
};
struct SNodeSession final : public BaseSession
{
SNodeSession(
const llarp::RouterID& snodeRouter,
std::function<bool(const llarp_buffer_t&)> writepkt,
AbstractRouter* r,
size_t numpaths,
size_t hoplen,
bool useRouterSNodeKey,
bool bundleRC);
2018-11-29 13:12:35 +00:00
2019-07-30 23:42:13 +00:00
~SNodeSession() override = default;
2018-11-29 13:12:35 +00:00
2019-03-22 14:10:30 +00:00
std::string
Name() const override;
protected:
void
PopulateRequest(llarp::routing::ObtainExitMessage& msg) const override
2018-11-29 13:12:35 +00:00
{
// TODO: set expiration time
msg.X = 0;
msg.E = 0;
}
};
} // namespace exit
} // namespace llarp
2018-12-10 15:44:18 +00:00
#endif