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

210 lines
5.2 KiB
C++
Raw Normal View History

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