2013-12-07 04:02:49 +04:00
|
|
|
#ifndef TUNNEL_H__
|
|
|
|
#define TUNNEL_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2014-01-21 03:37:51 +04:00
|
|
|
#include <vector>
|
2013-12-07 04:02:49 +04:00
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
2014-04-03 20:19:12 +04:00
|
|
|
#include <mutex>
|
2015-01-20 06:28:13 +03:00
|
|
|
#include <memory>
|
2013-12-07 04:02:49 +04:00
|
|
|
#include "Queue.h"
|
|
|
|
#include "TunnelConfig.h"
|
2014-03-14 20:35:02 +04:00
|
|
|
#include "TunnelPool.h"
|
2013-12-07 04:02:49 +04:00
|
|
|
#include "TransitTunnel.h"
|
|
|
|
#include "TunnelEndpoint.h"
|
|
|
|
#include "TunnelGateway.h"
|
|
|
|
#include "TunnelBase.h"
|
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
2014-02-20 06:24:55 +04:00
|
|
|
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes
|
2014-08-26 18:31:32 +04:00
|
|
|
const int TUNNEL_EXPIRATION_THRESHOLD = 60; // 1 minute
|
2015-04-17 18:36:42 +03:00
|
|
|
const int TUNNEL_RECREATION_THRESHOLD = 90; // 1.5 minutes
|
2014-09-26 18:15:34 +04:00
|
|
|
const int TUNNEL_CREATION_TIMEOUT = 30; // 30 seconds
|
2014-08-16 22:35:35 +04:00
|
|
|
const int STANDARD_NUM_RECORDS = 5; // in VariableTunnelBuild message
|
2014-07-27 04:56:42 +04:00
|
|
|
|
|
|
|
enum TunnelState
|
|
|
|
{
|
|
|
|
eTunnelStatePending,
|
2014-09-26 18:15:34 +04:00
|
|
|
eTunnelStateBuildReplyReceived,
|
|
|
|
eTunnelStateBuildFailed,
|
2014-07-27 04:56:42 +04:00
|
|
|
eTunnelStateEstablished,
|
|
|
|
eTunnelStateTestFailed,
|
2014-08-26 18:31:32 +04:00
|
|
|
eTunnelStateFailed,
|
|
|
|
eTunnelStateExpiring
|
2014-07-27 04:56:42 +04:00
|
|
|
};
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
class OutboundTunnel;
|
|
|
|
class InboundTunnel;
|
|
|
|
class Tunnel: public TunnelBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Tunnel (TunnelConfig * config);
|
|
|
|
~Tunnel ();
|
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
void Build (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
TunnelConfig * GetTunnelConfig () const { return m_Config; }
|
2014-07-27 04:56:42 +04:00
|
|
|
TunnelState GetState () const { return m_State; };
|
|
|
|
void SetState (TunnelState state) { m_State = state; };
|
|
|
|
bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
|
|
|
|
bool IsFailed () const { return m_State == eTunnelStateFailed; };
|
2015-04-17 18:36:42 +03:00
|
|
|
bool IsRecreated () const { return m_IsRecreated; };
|
|
|
|
void SetIsRecreated () { m_IsRecreated = true; };
|
2014-03-21 23:54:55 +04:00
|
|
|
|
2015-01-20 06:28:13 +03:00
|
|
|
std::shared_ptr<TunnelPool> GetTunnelPool () const { return m_Pool; };
|
|
|
|
void SetTunnelPool (std::shared_ptr<TunnelPool> pool) { m_Pool = pool; };
|
2014-03-14 20:35:02 +04:00
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);
|
|
|
|
|
|
|
|
// implements TunnelBase
|
2015-01-24 00:26:39 +03:00
|
|
|
void SendTunnelDataMsg (i2p::I2NPMessage * msg);
|
2013-12-07 04:02:49 +04:00
|
|
|
void EncryptTunnelMsg (I2NPMessage * tunnelMsg);
|
|
|
|
uint32_t GetNextTunnelID () const { return m_Config->GetFirstHop ()->tunnelID; };
|
|
|
|
const i2p::data::IdentHash& GetNextIdentHash () const { return m_Config->GetFirstHop ()->router->GetIdentHash (); };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelConfig * m_Config;
|
2015-01-20 06:28:13 +03:00
|
|
|
std::shared_ptr<TunnelPool> m_Pool; // pool, tunnel belongs to, or null
|
2014-07-27 04:56:42 +04:00
|
|
|
TunnelState m_State;
|
2015-04-17 18:36:42 +03:00
|
|
|
bool m_IsRecreated;
|
2013-12-07 04:02:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class OutboundTunnel: public Tunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
OutboundTunnel (TunnelConfig * config): Tunnel (config), m_Gateway (this) {};
|
|
|
|
|
|
|
|
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
|
2014-10-07 18:33:17 +04:00
|
|
|
void SendTunnelDataMsg (const std::vector<TunnelMessageBlock>& msgs); // multiple messages
|
2014-11-21 18:46:11 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> GetEndpointRouter () const
|
2014-01-26 06:47:01 +04:00
|
|
|
{ return GetTunnelConfig ()->GetLastHop ()->router; };
|
2013-12-07 04:02:49 +04:00
|
|
|
size_t GetNumSentBytes () const { return m_Gateway.GetNumSentBytes (); };
|
2014-01-04 07:56:28 +04:00
|
|
|
|
|
|
|
// implements TunnelBase
|
2015-01-21 22:40:48 +03:00
|
|
|
void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg);
|
2014-01-04 07:56:28 +04:00
|
|
|
uint32_t GetTunnelID () const { return GetNextTunnelID (); };
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-04-03 20:19:12 +04:00
|
|
|
std::mutex m_SendMutex;
|
2013-12-07 04:02:49 +04:00
|
|
|
TunnelGateway m_Gateway;
|
|
|
|
};
|
|
|
|
|
2015-02-06 02:53:43 +03:00
|
|
|
class InboundTunnel: public Tunnel, public std::enable_shared_from_this<InboundTunnel>
|
2013-12-07 04:02:49 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-07-10 20:44:49 +04:00
|
|
|
InboundTunnel (TunnelConfig * config): Tunnel (config), m_Endpoint (true) {};
|
2013-12-07 04:02:49 +04:00
|
|
|
void HandleTunnelDataMsg (I2NPMessage * msg);
|
2014-01-04 07:56:28 +04:00
|
|
|
size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); };
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2014-01-04 07:56:28 +04:00
|
|
|
// implements TunnelBase
|
2013-12-07 04:02:49 +04:00
|
|
|
uint32_t GetTunnelID () const { return GetTunnelConfig ()->GetLastHop ()->nextTunnelID; };
|
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelEndpoint m_Endpoint;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Tunnels
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Tunnels ();
|
|
|
|
~Tunnels ();
|
|
|
|
void Start ();
|
2014-03-15 04:24:12 +04:00
|
|
|
void Stop ();
|
2013-12-07 04:02:49 +04:00
|
|
|
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<InboundTunnel> GetInboundTunnel (uint32_t tunnelID);
|
|
|
|
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 ();
|
2015-01-20 06:28:13 +03:00
|
|
|
std::shared_ptr<TunnelPool> GetExploratoryPool () const { return m_ExploratoryPool; };
|
2013-12-07 04:02:49 +04:00
|
|
|
TransitTunnel * GetTransitTunnel (uint32_t tunnelID);
|
2015-01-10 06:27:52 +03:00
|
|
|
int GetTransitTunnelsExpirationTimeout ();
|
2013-12-07 04:02:49 +04:00
|
|
|
void AddTransitTunnel (TransitTunnel * tunnel);
|
2015-01-27 22:55:46 +03:00
|
|
|
void AddOutboundTunnel (std::shared_ptr<OutboundTunnel> newTunnel);
|
|
|
|
void AddInboundTunnel (std::shared_ptr<InboundTunnel> newTunnel);
|
2013-12-07 04:02:49 +04:00
|
|
|
void PostTunnelData (I2NPMessage * msg);
|
2015-01-23 06:00:41 +03:00
|
|
|
void PostTunnelData (const std::vector<I2NPMessage *>& msgs);
|
2013-12-07 04:02:49 +04:00
|
|
|
template<class TTunnel>
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<TTunnel> CreateTunnel (TunnelConfig * config, std::shared_ptr<OutboundTunnel> outboundTunnel = nullptr);
|
|
|
|
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<InboundTunnel> tunnel);
|
|
|
|
void AddPendingTunnel (uint32_t replyMsgID, std::shared_ptr<OutboundTunnel> tunnel);
|
2015-05-05 19:32:13 +03:00
|
|
|
std::shared_ptr<TunnelPool> CreateTunnelPool (i2p::garlic::GarlicDestination * localDestination, int numInboundHops, int numOuboundHops, int numInboundTunnels, int numOutboundTunnels);
|
2015-01-20 06:28:13 +03:00
|
|
|
void DeleteTunnelPool (std::shared_ptr<TunnelPool> pool);
|
|
|
|
void StopTunnelPool (std::shared_ptr<TunnelPool> pool);
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
private:
|
2015-01-26 19:56:10 +03:00
|
|
|
|
|
|
|
template<class TTunnel>
|
2015-01-27 22:55:46 +03:00
|
|
|
std::shared_ptr<TTunnel> GetPendingTunnel (uint32_t replyMsgID, const std::map<uint32_t, std::shared_ptr<TTunnel> >& pendingTunnels);
|
2015-01-26 19:56:10 +03:00
|
|
|
|
2015-01-24 00:26:39 +03:00
|
|
|
void HandleTunnelGatewayMsg (TunnelBase * tunnel, I2NPMessage * msg);
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
void Run ();
|
|
|
|
void ManageTunnels ();
|
|
|
|
void ManageOutboundTunnels ();
|
|
|
|
void ManageInboundTunnels ();
|
2014-01-04 07:56:28 +04:00
|
|
|
void ManageTransitTunnels ();
|
2014-10-06 20:50:36 +04:00
|
|
|
void ManagePendingTunnels ();
|
2015-01-26 19:56:10 +03:00
|
|
|
template<class PendingTunnels>
|
|
|
|
void ManagePendingTunnels (PendingTunnels& pendingTunnels);
|
2014-03-15 04:24:12 +04:00
|
|
|
void ManageTunnelPools ();
|
2013-12-07 04:02:49 +04:00
|
|
|
|
|
|
|
void CreateZeroHopsInboundTunnel ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
2015-01-27 22:55:46 +03:00
|
|
|
std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_PendingInboundTunnels; // by replyMsgID
|
|
|
|
std::map<uint32_t, std::shared_ptr<OutboundTunnel> > m_PendingOutboundTunnels; // by replyMsgID
|
|
|
|
std::map<uint32_t, std::shared_ptr<InboundTunnel> > m_InboundTunnels;
|
|
|
|
std::list<std::shared_ptr<OutboundTunnel> > m_OutboundTunnels;
|
2014-09-14 15:50:01 +04:00
|
|
|
std::mutex m_TransitTunnelsMutex;
|
2013-12-07 04:02:49 +04:00
|
|
|
std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
|
2014-10-05 19:01:12 +04:00
|
|
|
std::mutex m_PoolsMutex;
|
2015-01-20 06:28:13 +03:00
|
|
|
std::list<std::shared_ptr<TunnelPool>> m_Pools;
|
|
|
|
std::shared_ptr<TunnelPool> m_ExploratoryPool;
|
2013-12-07 04:02:49 +04:00
|
|
|
i2p::util::Queue<I2NPMessage> m_Queue;
|
|
|
|
|
2015-02-28 15:59:34 +03:00
|
|
|
// some stats
|
|
|
|
int m_NumSuccesiveTunnelCreations, m_NumFailedTunnelCreations;
|
|
|
|
|
2013-12-07 04:02:49 +04:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP only
|
|
|
|
const decltype(m_OutboundTunnels)& GetOutboundTunnels () const { return m_OutboundTunnels; };
|
|
|
|
const decltype(m_InboundTunnels)& GetInboundTunnels () const { return m_InboundTunnels; };
|
2013-12-10 17:10:49 +04:00
|
|
|
const decltype(m_TransitTunnels)& GetTransitTunnels () const { return m_TransitTunnels; };
|
2015-02-04 00:45:19 +03:00
|
|
|
int GetQueueSize () { return m_Queue.GetSize (); };
|
2015-02-28 15:59:34 +03:00
|
|
|
int GetTunnelCreationSuccessRate () const // in percents
|
|
|
|
{
|
|
|
|
int totalNum = m_NumSuccesiveTunnelCreations + m_NumFailedTunnelCreations;
|
|
|
|
return totalNum ? m_NumSuccesiveTunnelCreations*100/totalNum : 0;
|
|
|
|
}
|
2013-12-07 04:02:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
extern Tunnels tunnels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|