2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2023-04-04 01:35:10 +00:00
|
|
|
* Copyright (c) 2013-2023, The PurpleI2P Project
|
2020-05-22 13:18:41 +00: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-10 23:19:49 +00:00
|
|
|
#ifndef TRANSIT_TUNNEL_H__
|
|
|
|
#define TRANSIT_TUNNEL_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2015-01-22 02:50:46 +00:00
|
|
|
#include <vector>
|
2014-06-26 19:41:12 +00:00
|
|
|
#include <mutex>
|
2015-06-17 16:31:28 +00:00
|
|
|
#include <memory>
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Crypto.h"
|
2013-11-10 23:19:49 +00:00
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "TunnelEndpoint.h"
|
|
|
|
#include "TunnelGateway.h"
|
2013-11-29 12:52:09 +00:00
|
|
|
#include "TunnelBase.h"
|
2013-11-10 23:19:49 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
|
|
|
class TransitTunnel: public TunnelBase
|
2013-11-10 23:19:49 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnel (uint32_t receiveTunnelID,
|
2022-12-25 14:36:16 +00:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2015-01-22 02:50:46 +00:00
|
|
|
virtual size_t GetNumTransmittedBytes () const { return 0; };
|
2013-11-10 23:19:49 +00:00
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
// implements TunnelBase
|
2023-04-04 01:35:10 +00: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;
|
2013-11-10 23:19:49 +00:00
|
|
|
private:
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2022-12-25 14:36:16 +00:00
|
|
|
i2p::crypto::AESKey m_LayerKey, m_IVKey;
|
|
|
|
std::unique_ptr<i2p::crypto::TunnelEncryption> m_Encryption;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2013-11-29 12:52:09 +00:00
|
|
|
|
2015-01-22 02:50:46 +00:00
|
|
|
class TransitTunnelParticipant: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelParticipant (uint32_t receiveTunnelID,
|
2022-12-25 14:36:16 +00:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2018-01-06 03:48:51 +00:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
|
2015-01-22 02:50:46 +00:00
|
|
|
layerKey, ivKey), m_NumTransmittedBytes (0) {};
|
|
|
|
~TransitTunnelParticipant ();
|
|
|
|
|
2023-04-04 17:42:54 +00:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_NumTransmittedBytes; };
|
2023-04-04 01:35:10 +00:00
|
|
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
|
|
|
void FlushTunnelDataMsgs () override;
|
2015-01-22 02:50:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
size_t m_NumTransmittedBytes;
|
2015-06-17 16:31:28 +00:00
|
|
|
std::vector<std::shared_ptr<i2p::I2NPMessage> > m_TunnelDataMsgs;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
class TransitTunnelGateway: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelGateway (uint32_t receiveTunnelID,
|
2022-12-25 14:36:16 +00:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2018-01-06 03:48:51 +00:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
|
2013-11-29 12:52:09 +00:00
|
|
|
layerKey, ivKey), m_Gateway(this) {};
|
|
|
|
|
2023-04-04 01:35:10 +00:00
|
|
|
void SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg) override;
|
|
|
|
void FlushTunnelDataMsgs () override;
|
2023-04-04 17:42:54 +00:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_Gateway.GetNumSentBytes (); };
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
private:
|
|
|
|
|
2014-06-26 19:41:12 +00:00
|
|
|
std::mutex m_SendMutex;
|
2013-11-29 12:52:09 +00:00
|
|
|
TunnelGateway m_Gateway;
|
2018-01-06 03:48:51 +00:00
|
|
|
};
|
2013-11-29 12:52:09 +00:00
|
|
|
|
|
|
|
class TransitTunnelEndpoint: public TransitTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
TransitTunnelEndpoint (uint32_t receiveTunnelID,
|
2022-12-25 14:36:16 +00:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
|
2014-07-10 16:44:49 +00:00
|
|
|
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey),
|
|
|
|
m_Endpoint (false) {}; // transit endpoint is always outbound
|
2013-11-29 12:52:09 +00:00
|
|
|
|
2023-04-04 02:50:31 +00:00
|
|
|
void Cleanup () override { m_Endpoint.Cleanup (); }
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2023-04-04 01:35:10 +00:00
|
|
|
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
|
2023-04-04 17:57:46 +00:00
|
|
|
size_t GetNumTransmittedBytes () const override { return m_Endpoint.GetNumReceivedBytes (); }
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
TunnelEndpoint m_Endpoint;
|
|
|
|
};
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-03-01 20:22:36 +00:00
|
|
|
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
|
2022-12-25 14:36:16 +00:00
|
|
|
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
|
|
|
|
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey,
|
2013-11-29 12:52:09 +00:00
|
|
|
bool isGateway, bool isEndpoint);
|
2013-11-10 23:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|