i2pd/Tunnel.h

158 lines
4.6 KiB
C
Raw Normal View History

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>
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
{
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes
2013-12-07 04:02:49 +04:00
class OutboundTunnel;
class InboundTunnel;
class Tunnel: public TunnelBase
{
public:
Tunnel (TunnelConfig * config);
~Tunnel ();
void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0);
TunnelConfig * GetTunnelConfig () const { return m_Config; }
bool IsEstablished () const { return m_IsEstablished; };
2014-03-22 02:26:11 +04:00
bool IsFailed () const { return m_IsFailed; };
2014-03-21 23:54:55 +04:00
void SetFailed (bool failed) { m_IsFailed = failed; }
2014-03-14 20:35:02 +04:00
TunnelPool * GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; };
2013-12-07 04:02:49 +04:00
bool HandleTunnelBuildResponse (uint8_t * msg, size_t len);
// implements TunnelBase
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;
2014-03-14 20:35:02 +04:00
TunnelPool * m_Pool; // pool, tunnel belongs to, or null
2014-05-10 03:34:12 +04:00
bool m_IsEstablished, m_IsFailed;
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-01-21 03:37:51 +04:00
void SendTunnelDataMsg (std::vector<TunnelMessageBlock> msgs); // multiple messages
const i2p::data::RouterInfo * GetEndpointRouter () const
{ 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
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;
};
class InboundTunnel: public Tunnel
{
public:
InboundTunnel (TunnelConfig * config): Tunnel (config) {};
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
InboundTunnel * GetInboundTunnel (uint32_t tunnelID);
Tunnel * GetPendingTunnel (uint32_t replyMsgID);
InboundTunnel * GetNextInboundTunnel ();
OutboundTunnel * GetNextOutboundTunnel ();
TunnelPool * GetExploratoryPool () const { return m_ExploratoryPool; };
2013-12-07 04:02:49 +04:00
TransitTunnel * GetTransitTunnel (uint32_t tunnelID);
void AddTransitTunnel (TransitTunnel * tunnel);
void AddOutboundTunnel (OutboundTunnel * newTunnel);
void AddInboundTunnel (InboundTunnel * newTunnel);
void PostTunnelData (I2NPMessage * msg);
template<class TTunnel>
TTunnel * CreateTunnel (TunnelConfig * config, OutboundTunnel * outboundTunnel = 0);
2014-04-01 21:55:09 +04:00
TunnelPool * CreateTunnelPool (i2p::data::LocalDestination& localDestination);
2014-03-15 04:51:51 +04:00
void DeleteTunnelPool (TunnelPool * pool);
2013-12-07 04:02:49 +04:00
private:
void Run ();
void ManageTunnels ();
void ManageOutboundTunnels ();
void ManageInboundTunnels ();
2014-01-04 07:56:28 +04:00
void ManageTransitTunnels ();
2014-03-15 04:24:12 +04:00
void ManageTunnelPools ();
2013-12-07 04:02:49 +04:00
void CreateZeroHopsInboundTunnel ();
private:
bool m_IsRunning;
bool m_IsTunnelCreated; // TODO: temporary
uint32_t m_NextReplyMsgID; // TODO: make it random later
std::thread * m_Thread;
std::map<uint32_t, Tunnel *> m_PendingTunnels; // by replyMsgID
std::map<uint32_t, InboundTunnel *> m_InboundTunnels;
std::list<OutboundTunnel *> m_OutboundTunnels;
std::map<uint32_t, TransitTunnel *> m_TransitTunnels;
2014-04-01 23:08:53 +04:00
std::map<i2p::data::IdentHash, TunnelPool *> m_Pools;
2014-04-02 21:14:21 +04:00
TunnelPool * m_ExploratoryPool;
2013-12-07 04:02:49 +04:00
i2p::util::Queue<I2NPMessage> m_Queue;
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; };
2013-12-07 04:02:49 +04:00
};
extern Tunnels tunnels;
}
}
#endif