2020-05-22 15:18:41 +02:00
|
|
|
/*
|
2024-10-29 02:15:16 +01:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 15:18:41 +02:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2013-11-11 00:19:49 +01:00
|
|
|
#ifndef TRANSIT_TUNNEL_H__
|
|
|
|
#define TRANSIT_TUNNEL_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2024-10-29 02:15:16 +01:00
|
|
|
#include <list>
|
2014-06-26 21:41:12 +02:00
|
|
|
#include <mutex>
|
2015-06-17 18:31:28 +02:00
|
|
|
#include <memory>
|
2015-11-03 15:15:49 +01:00
|
|
|
#include "Crypto.h"
|
2024-11-20 01:11:09 +01:00
|
|
|
#include "Queue.h"
|
2013-11-11 00:19:49 +01:00
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "TunnelEndpoint.h"
|
|
|
|
#include "TunnelGateway.h"
|
2013-11-29 13:52:09 +01:00
|
|
|
#include "TunnelBase.h"
|
2013-11-11 00:19:49 +01:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
2018-01-06 04:48:51 +01:00
|
|
|
{
|
|
|
|
class TransitTunnel: public TunnelBase
|
2013-11-11 00:19:49 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnel (uint32_t receiveTunnelID,
|
2022-12-25 15:36:16 +01:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey);
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2015-01-22 03:50:46 +01:00
|
|
|
virtual size_t GetNumTransmittedBytes () const { return 0; };
|
2024-12-17 01:49:14 +01:00
|
|
|
virtual std::string GetNextPeerName () const;
|
2013-11-11 00:19:49 +01:00
|
|
|
|
2013-11-29 13:52:09 +01:00
|
|
|
// implements TunnelBase
|
2023-04-04 03:35:10 +02:00
|
|
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg) override;
|
|
|
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
|
|
|
void EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out) override;
|
2024-12-17 01:49:14 +01:00
|
|
|
|
2013-11-11 00:19:49 +01:00
|
|
|
private:
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2022-12-25 15:36:16 +01:00
|
|
|
i2p::crypto::AESKey m_LayerKey, m_IVKey;
|
|
|
|
std::unique_ptr<i2p::crypto::TunnelEncryption> m_Encryption;
|
2018-01-06 04:48:51 +01:00
|
|
|
};
|
2013-11-29 13:52:09 +01:00
|
|
|
|
2015-01-22 03:50:46 +01:00
|
|
|
class TransitTunnelParticipant: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelParticipant (uint32_t receiveTunnelID,
|
2022-12-25 15:36:16 +01:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2018-01-06 04:48:51 +01:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
|
2015-01-22 03:50:46 +01:00
|
|
|
layerKey, ivKey), m_NumTransmittedBytes (0) {};
|
|
|
|
~TransitTunnelParticipant ();
|
|
|
|
|
2023-04-04 19:42:54 +02:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_NumTransmittedBytes; };
|
2024-12-17 01:49:14 +01:00
|
|
|
std::string GetNextPeerName () const override;
|
2023-04-04 03:35:10 +02:00
|
|
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
|
|
|
void FlushTunnelDataMsgs () override;
|
2015-01-22 03:50:46 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
size_t m_NumTransmittedBytes;
|
2024-10-29 02:15:16 +01:00
|
|
|
std::list<std::shared_ptr<i2p::I2NPMessage> > m_TunnelDataMsgs;
|
2024-12-16 00:03:31 +01:00
|
|
|
std::unique_ptr<TunnelTransportSender> m_Sender;
|
2018-01-06 04:48:51 +01:00
|
|
|
};
|
|
|
|
|
2013-11-29 13:52:09 +01:00
|
|
|
class TransitTunnelGateway: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelGateway (uint32_t receiveTunnelID,
|
2022-12-25 15:36:16 +01:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2018-01-06 04:48:51 +01:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
|
2024-11-29 17:31:13 +01:00
|
|
|
layerKey, ivKey), m_Gateway(*this) {};
|
2013-11-29 13:52:09 +01:00
|
|
|
|
2023-04-04 03:35:10 +02:00
|
|
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg) override;
|
|
|
|
void FlushTunnelDataMsgs () override;
|
2023-04-04 19:42:54 +02:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_Gateway.GetNumSentBytes (); };
|
2024-12-17 01:49:14 +01:00
|
|
|
std::string GetNextPeerName () const override;
|
|
|
|
|
2013-11-29 13:52:09 +01:00
|
|
|
private:
|
|
|
|
|
2014-06-26 21:41:12 +02:00
|
|
|
std::mutex m_SendMutex;
|
2013-11-29 13:52:09 +01:00
|
|
|
TunnelGateway m_Gateway;
|
2018-01-06 04:48:51 +01:00
|
|
|
};
|
2013-11-29 13:52:09 +01:00
|
|
|
|
|
|
|
class TransitTunnelEndpoint: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelEndpoint (uint32_t receiveTunnelID,
|
2022-12-25 15:36:16 +01:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2014-07-10 18:44:49 +02:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey),
|
|
|
|
m_Endpoint (false) {}; // transit endpoint is always outbound
|
2013-11-29 13:52:09 +01:00
|
|
|
|
2023-04-04 04:50:31 +02:00
|
|
|
void Cleanup () override { m_Endpoint.Cleanup (); }
|
2024-12-17 01:49:14 +01:00
|
|
|
|
2023-04-04 03:35:10 +02:00
|
|
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
2023-04-04 19:57:46 +02:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_Endpoint.GetNumReceivedBytes (); }
|
2024-12-17 01:49:14 +01:00
|
|
|
std::string GetNextPeerName () const override { return ""; }
|
|
|
|
|
2013-11-29 13:52:09 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelEndpoint m_Endpoint;
|
|
|
|
};
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2016-03-01 21:22:36 +01:00
|
|
|
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
|
2022-12-25 15:36:16 +01:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey,
|
2013-11-29 13:52:09 +01:00
|
|
|
bool isGateway, bool isEndpoint);
|
2024-11-08 01:00:11 +01:00
|
|
|
|
2024-11-20 01:11:09 +01:00
|
|
|
|
|
|
|
const int TRANSIT_TUNNELS_QUEUE_WAIT_INTERVAL = 10; // in seconds
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
class TransitTunnels
|
2024-11-16 22:05:46 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2024-11-20 01:11:09 +01:00
|
|
|
TransitTunnels ();
|
|
|
|
~TransitTunnels ();
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2024-11-20 01:11:09 +01:00
|
|
|
void PostTransitTunnelBuildMsg (std::shared_ptr<I2NPMessage>&& msg);
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
size_t GetNumTransitTunnels () const { return m_TransitTunnels.size (); }
|
|
|
|
int GetTransitTunnelsExpirationTimeout ();
|
2024-11-16 22:05:46 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
bool AddTransitTunnel (std::shared_ptr<TransitTunnel> tunnel);
|
2024-11-20 01:11:09 +01:00
|
|
|
void ManageTransitTunnels (uint64_t ts);
|
|
|
|
|
|
|
|
void HandleShortTransitTunnelBuildMsg (std::shared_ptr<I2NPMessage>&& msg);
|
|
|
|
void HandleVariableTransitTunnelBuildMsg (std::shared_ptr<I2NPMessage>&& msg);
|
2024-11-16 22:05:46 +01:00
|
|
|
bool HandleBuildRequestRecords (int num, uint8_t * records, uint8_t * clearText);
|
2024-11-17 02:56:35 +01:00
|
|
|
|
2024-11-20 01:11:09 +01:00
|
|
|
void Run ();
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
private:
|
|
|
|
|
2024-11-20 01:11:09 +01:00
|
|
|
volatile bool m_IsRunning;
|
|
|
|
std::unique_ptr<std::thread> m_Thread;
|
2024-11-17 02:56:35 +01:00
|
|
|
std::list<std::shared_ptr<TransitTunnel> > m_TransitTunnels;
|
2024-11-20 01:11:09 +01:00
|
|
|
i2p::util::Queue<std::shared_ptr<I2NPMessage> > m_TunnelBuildMsgQueue;
|
|
|
|
|
2024-11-17 02:56:35 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP only
|
2024-11-20 01:11:09 +01:00
|
|
|
const auto& GetTransitTunnels () const { return m_TransitTunnels; };
|
2024-11-20 19:28:01 +01:00
|
|
|
size_t GetTunnelBuildMsgQueueSize () const { return m_TunnelBuildMsgQueue.GetSize (); };
|
2024-11-16 22:05:46 +01:00
|
|
|
};
|
2013-11-11 00:19:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|