mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
309 lines
10 KiB
309 lines
10 KiB
11 years ago
|
#ifndef TUNNEL_H__
|
||
|
#define TUNNEL_H__
|
||
|
|
||
|
#include <inttypes.h>
|
||
|
#include <map>
|
||
9 years ago
|
#include <unordered_map>
|
||
11 years ago
|
#include <list>
|
||
11 years ago
|
#include <vector>
|
||
11 years ago
|
#include <string>
|
||
|
#include <thread>
|
||
11 years ago
|
#include <mutex>
|
||
10 years ago
|
#include <memory>
|
||
11 years ago
|
#include "Queue.h"
|
||
9 years ago
|
#include "Crypto.h"
|
||
11 years ago
|
#include "TunnelConfig.h"
|
||
11 years ago
|
#include "TunnelPool.h"
|
||
11 years ago
|
#include "TransitTunnel.h"
|
||
|
#include "TunnelEndpoint.h"
|
||
|
#include "TunnelGateway.h"
|
||
|
#include "TunnelBase.h"
|
||
|
#include "I2NPProtocol.h"
|
||
8 years ago
|
#include "Event.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace tunnel
|
||
8 years ago
|
{
|
||
|
|
||
|
template<typename TunnelT>
|
||
|
static void EmitTunnelEvent(const std::string & ev, const TunnelT & t)
|
||
|
{
|
||
|
#ifdef WITH_EVENTS
|
||
|
EmitEvent({{"type", ev}, {"tid", std::to_string(t->GetTunnelID())}});
|
||
8 years ago
|
#else
|
||
|
(void) ev;
|
||
|
(void) t;
|
||
|
#endif
|
||
8 years ago
|
}
|
||
|
|
||
|
template<typename TunnelT, typename T>
|
||
|
static void EmitTunnelEvent(const std::string & ev, TunnelT * t, const T & val)
|
||
|
{
|
||
|
#ifdef WITH_EVENTS
|
||
|
EmitEvent({{"type", ev}, {"tid", std::to_string(t->GetTunnelID())}, {"value", std::to_string(val)}, {"inbound", std::to_string(t->IsInbound())}});
|
||
8 years ago
|
#else
|
||
|
(void) ev;
|
||
|
(void) t;
|
||
|
(void) val;
|
||
8 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
template<typename TunnelT>
|
||
|
static void EmitTunnelEvent(const std::string & ev, TunnelT * t, const std::string & val)
|
||
|
{
|
||
|
#ifdef WITH_EVENTS
|
||
|
EmitEvent({{"type", ev}, {"tid", std::to_string(t->GetTunnelID())}, {"value", val}, {"inbound", std::to_string(t->IsInbound())}});
|
||
8 years ago
|
#else
|
||
|
(void) ev;
|
||
|
(void) t;
|
||
|
(void) val;
|
||
8 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
11 years ago
|
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes
|
||
10 years ago
|
const int TUNNEL_EXPIRATION_THRESHOLD = 60; // 1 minute
|
||
10 years ago
|
const int TUNNEL_RECREATION_THRESHOLD = 90; // 1.5 minutes
|
||
10 years ago
|
const int TUNNEL_CREATION_TIMEOUT = 30; // 30 seconds
|
||
11 years ago
|
const int STANDARD_NUM_RECORDS = 5; // in VariableTunnelBuild message
|
||
11 years ago
|
|
||
|
enum TunnelState
|
||
|
{
|
||
|
eTunnelStatePending,
|
||
10 years ago
|
eTunnelStateBuildReplyReceived,
|
||
|
eTunnelStateBuildFailed,
|
||
11 years ago
|
eTunnelStateEstablished,
|
||
|
eTunnelStateTestFailed,
|
||
10 years ago
|
eTunnelStateFailed,
|
||
|
eTunnelStateExpiring
|
||
11 years ago
|
};
|
||
8 years ago
|
|
||
11 years ago
|
class OutboundTunnel;
|
||
|
class InboundTunnel;
|
||
|
class Tunnel: public TunnelBase
|
||
|
{
|
||
9 years ago
|
struct TunnelHop
|
||
|
{
|
||
|
std::shared_ptr<const i2p::data::IdentityEx> ident;
|
||
|
i2p::crypto::TunnelDecryption decryption;
|
||
|
};
|
||
|
|
||
11 years ago
|
public:
|
||
|
|
||
10 years ago
|
Tunnel (std::shared_ptr<const TunnelConfig> config);
|
||
11 years ago
|
~Tunnel ();
|
||
|
|
||
10 years ago
|
void Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
|
||
11 years ago
|
|
||
10 years ago
|
std::shared_ptr<const TunnelConfig> GetTunnelConfig () const { return m_Config; }
|
||
9 years ago
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetPeers () const;
|
||
|
std::vector<std::shared_ptr<const i2p::data::IdentityEx> > GetInvertedPeers () const;
|
||
11 years ago
|
TunnelState GetState () const { return m_State; };
|
||
8 years ago
|
void SetState (TunnelState state);
|
||
11 years ago
|
bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
|
||
|
bool IsFailed () const { return m_State == eTunnelStateFailed; };
|
||
10 years ago
|
bool IsRecreated () const { return m_IsRecreated; };
|
||
|
void SetIsRecreated () { m_IsRecreated = true; };
|
||
8 years ago
|
virtual bool IsInbound() const = 0;
|
||
|
|
||
10 years ago
|
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
||
|
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
||
11 years ago
|
|
||
11 years ago
|
bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);
|
||
9 years ago
|
|
||
9 years ago
|
virtual void Print (std::stringstream&) const {};
|
||
11 years ago
|
|
||
|
// implements TunnelBase
|
||
10 years ago
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg);
|
||
10 years ago
|
void EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out);
|
||
9 years ago
|
|
||
8 years ago
|
/** @brief add latency sample */
|
||
|
void AddLatencySample(const uint64_t ms) { m_Latency = (m_Latency + ms) >> 1; }
|
||
|
/** @brief get this tunnel's estimated latency */
|
||
|
uint64_t GetMeanLatency() const { return m_Latency; }
|
||
|
/** @breif return true if this tunnel's latency fits in range [lowerbound, upperbound] */
|
||
|
bool LatencyFitsRange(uint64_t lowerbound, uint64_t upperbound) const;
|
||
8 years ago
|
|
||
8 years ago
|
bool LatencyIsKnown() const { return m_Latency > 0; }
|
||
9 years ago
|
protected:
|
||
|
|
||
|
void PrintHops (std::stringstream& s) const;
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
std::shared_ptr<const TunnelConfig> m_Config;
|
||
9 years ago
|
std::vector<std::unique_ptr<TunnelHop> > m_Hops;
|
||
10 years ago
|
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
|
||
11 years ago
|
TunnelState m_State;
|
||
10 years ago
|
bool m_IsRecreated;
|
||
8 years ago
|
uint64_t m_Latency; // in milliseconds
|
||
11 years ago
|
};
|
||
|
|
||
|
class OutboundTunnel: public Tunnel
|
||
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
OutboundTunnel (std::shared_ptr<const TunnelConfig> config):
|
||
|
Tunnel (config), m_Gateway (this), m_EndpointIdentHash (config->GetLastIdentHash ()) {};
|
||
11 years ago
|
|
||
10 years ago
|
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr<i2p::I2NPMessage> msg);
|
||
9 years ago
|
virtual void SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs); // multiple messages
|
||
9 years ago
|
const i2p::data::IdentHash& GetEndpointIdentHash () const { return m_EndpointIdentHash; };
|
||
9 years ago
|
virtual size_t GetNumSentBytes () const { return m_Gateway.GetNumSentBytes (); };
|
||
9 years ago
|
void Print (std::stringstream& s) const;
|
||
9 years ago
|
|
||
11 years ago
|
// implements TunnelBase
|
||
10 years ago
|
void HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg);
|
||
8 years ago
|
|
||
|
bool IsInbound() const { return false; }
|
||
11 years ago
|
|
||
|
private:
|
||
|
|
||
11 years ago
|
std::mutex m_SendMutex;
|
||
11 years ago
|
TunnelGateway m_Gateway;
|
||
9 years ago
|
i2p::data::IdentHash m_EndpointIdentHash;
|
||
11 years ago
|
};
|
||
9 years ago
|
|
||
10 years ago
|
class InboundTunnel: public Tunnel, public std::enable_shared_from_this<InboundTunnel>
|
||
11 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
InboundTunnel (std::shared_ptr<const TunnelConfig> config): Tunnel (config), m_Endpoint (true) {};
|
||
10 years ago
|
void HandleTunnelDataMsg (std::shared_ptr<const I2NPMessage> msg);
|
||
9 years ago
|
virtual size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
||
9 years ago
|
void Print (std::stringstream& s) const;
|
||
8 years ago
|
bool IsInbound() const { return true; }
|
||
8 years ago
|
|
||
|
// override TunnelBase
|
||
|
void Cleanup () { m_Endpoint.Cleanup (); };
|
||
|
|
||
9 years ago
|
private:
|
||
11 years ago
|
|
||
|
TunnelEndpoint m_Endpoint;
|
||
|
};
|
||
9 years ago
|
|
||
|
class ZeroHopsInboundTunnel: public InboundTunnel
|
||
|
{
|
||
|
public:
|
||
11 years ago
|
|
||
9 years ago
|
ZeroHopsInboundTunnel ();
|
||
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg);
|
||
|
void Print (std::stringstream& s) const;
|
||
9 years ago
|
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
size_t m_NumReceivedBytes;
|
||
9 years ago
|
};
|
||
11 years ago
|
|
||
9 years ago
|
class ZeroHopsOutboundTunnel: public OutboundTunnel
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
ZeroHopsOutboundTunnel ();
|
||
|
void SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs);
|
||
|
void Print (std::stringstream& s) const;
|
||
|
size_t GetNumSentBytes () const { return m_NumSentBytes; };
|
||
|
|
||
|
private:
|
||
|
|
||
|
size_t m_NumSentBytes;
|
||
|
};
|
||
|
|
||
11 years ago
|
class Tunnels
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
Tunnels ();
|
||
|
~Tunnels ();
|
||
|
void Start ();
|
||
11 years ago
|
void Stop ();
|
||
11 years ago
|
|
||
10 years ago
|
std::shared_ptr<InboundTunnel> GetPendingInboundTunnel (uint32_t replyMsgID);
|
||
|
std::shared_ptr<OutboundTunnel> GetPendingOutboundTunnel (uint32_t replyMsgID);
|
||
|
std::shared_ptr<InboundTunnel> GetNextInboundTunnel ();
|
||
|
std::shared_ptr<OutboundTunnel> GetNextOutboundTunnel ();
|
||
10 years ago
|
std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
|
||
9 years ago
|
std::shared_ptr<TunnelBase> GetTunnel (uint32_t tunnelID);
|
||
10 years ago
|
int GetTransitTunnelsExpirationTimeout ();
|
||
9 years ago
|
void AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel);
|
||
10 years ago
|
void AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel);
|
||
|
void AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel);
|
||
9 years ago
|
std::shared_ptr<InboundTunnel> CreateInboundTunnel (std::shared_ptr<TunnelConfig> config, std::shared_ptr<OutboundTunnel> outboundTunnel);
|
||
|
std::shared_ptr<OutboundTunnel> CreateOutboundTunnel (std::shared_ptr<TunnelConfig> config);
|
||
10 years ago
|
void PostTunnelData (std::shared_ptr<I2NPMessage> msg);
|
||
|
void PostTunnelData (const std::vector<std::shared_ptr<I2NPMessage> >& msgs);
|
||
10 years ago
|
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel);
|
||
|
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel);
|
||
9 years ago
|
std::shared_ptr<TunnelPool> CreateTunnelPool (int numInboundHops,
|
||
|
int numOuboundHops, int numInboundTunnels, int numOutboundTunnels);
|
||
10 years ago
|
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
|
||
|
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
|
||
11 years ago
|
|
||
|
private:
|
||
10 years ago
|
|
||
9 years ago
|
template<class TTunnel>
|
||
|
std::shared_ptr<TTunnel> CreateTunnel (std::shared_ptr<TunnelConfig> config, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
|
||
|
|
||
10 years ago
|
template<class TTunnel>
|
||
10 years ago
|
std::shared_ptr<TTunnel> GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels);
|
||
10 years ago
|
|
||
9 years ago
|
void HandleTunnelGatewayMsg (std::shared_ptr<TunnelBase> tunnel, std::shared_ptr<I2NPMessage> msg);
|
||
10 years ago
|
|
||
11 years ago
|
void Run ();
|
||
|
void ManageTunnels ();
|
||
|
void ManageOutboundTunnels ();
|
||
|
void ManageInboundTunnels ();
|
||
11 years ago
|
void ManageTransitTunnels ();
|
||
10 years ago
|
void ManagePendingTunnels ();
|
||
10 years ago
|
template<class PendingTunnels>
|
||
|
void ManagePendingTunnels (PendingTunnels& pendingTunnels);
|
||
11 years ago
|
void ManageTunnelPools ();
|
||
11 years ago
|
|
||
9 years ago
|
std::shared_ptr<ZeroHopsInboundTunnel> CreateZeroHopsInboundTunnel ();
|
||
|
std::shared_ptr<ZeroHopsOutboundTunnel> CreateZeroHopsOutboundTunnel ();
|
||
9 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
|
bool m_IsRunning;
|
||
|
std::thread * m_Thread;
|
||
10 years ago
|
std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_PendingInboundTunnels; // by replyMsgID
|
||
|
std::map<uint32_t, std::shared_ptr<OutboundTunnel> > m_PendingOutboundTunnels; // by replyMsgID
|
||
9 years ago
|
std::list<std::shared_ptr<InboundTunnel> > m_InboundTunnels;
|
||
10 years ago
|
std::list<std::shared_ptr<OutboundTunnel> > m_OutboundTunnels;
|
||
9 years ago
|
std::list<std::shared_ptr<TransitTunnel> > m_TransitTunnels;
|
||
|
std::unordered_map<uint32_t, std::shared_ptr<TunnelBase> > m_Tunnels; // tunnelID->tunnel known by this id
|
||
10 years ago
|
std::mutex m_PoolsMutex;
|
||
10 years ago
|
std::list<std::shared_ptr<TunnelPool>> m_Pools;
|
||
|
std::shared_ptr<TunnelPool> m_ExploratoryPool;
|
||
10 years ago
|
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_Queue;
|
||
9 years ago
|
|
||
10 years ago
|
// some stats
|
||
|
int m_NumSuccesiveTunnelCreations, m_NumFailedTunnelCreations;
|
||
|
|
||
11 years ago
|
public:
|
||
|
|
||
|
// for HTTP only
|
||
|
const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; };
|
||
|
const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; };
|
||
11 years ago
|
const decltype(m_TransitTunnels)& GetTransitTunnels () const { return m_TransitTunnels; };
|
||
9 years ago
|
|
||
9 years ago
|
size_t CountTransitTunnels() const;
|
||
|
size_t CountInboundTunnels() const;
|
||
|
size_t CountOutboundTunnels() const;
|
||
9 years ago
|
|
||
10 years ago
|
int GetQueueSize () { return m_Queue.GetSize (); };
|
||
10 years ago
|
int GetTunnelCreationSuccessRate () const // in percents
|
||
|
{
|
||
|
int totalNum = m_NumSuccesiveTunnelCreations + m_NumFailedTunnelCreations;
|
||
|
return totalNum ? m_NumSuccesiveTunnelCreations*100/totalNum : 0;
|
||
|
}
|
||
11 years ago
|
};
|
||
|
|
||
|
extern Tunnels tunnels;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|