mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.5 KiB
111 lines
3.5 KiB
11 years ago
|
#include <string.h>
|
||
11 years ago
|
#include "I2PEndian.h"
|
||
11 years ago
|
#include "Log.h"
|
||
|
#include "RouterContext.h"
|
||
|
#include "I2NPProtocol.h"
|
||
|
#include "Tunnel.h"
|
||
|
#include "Transports.h"
|
||
|
#include "TransitTunnel.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace tunnel
|
||
|
{
|
||
|
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID,
|
||
|
const uint8_t * nextIdent, uint32_t nextTunnelID,
|
||
11 years ago
|
const uint8_t * layerKey,const uint8_t * ivKey):
|
||
9 years ago
|
TunnelBase (receiveTunnelID, nextTunnelID, nextIdent)
|
||
11 years ago
|
{
|
||
11 years ago
|
m_Encryption.SetKeys (layerKey, ivKey);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnel::EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out)
|
||
11 years ago
|
{
|
||
10 years ago
|
m_Encryption.Encrypt (in->GetPayload () + 4, out->GetPayload () + 4);
|
||
11 years ago
|
}
|
||
10 years ago
|
|
||
|
TransitTunnelParticipant::~TransitTunnelParticipant ()
|
||
|
{
|
||
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
|
||
11 years ago
|
{
|
||
10 years ago
|
auto newMsg = CreateEmptyTunnelDataMsg ();
|
||
|
EncryptTunnelMsg (tunnelMsg, newMsg);
|
||
11 years ago
|
|
||
11 years ago
|
m_NumTransmittedBytes += tunnelMsg->GetLength ();
|
||
10 years ago
|
htobe32buf (newMsg->GetPayload (), GetNextTunnelID ());
|
||
10 years ago
|
newMsg->FillI2NPMessageHeader (eI2NPTunnelData);
|
||
10 years ago
|
m_TunnelDataMsgs.push_back (newMsg);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnelParticipant::FlushTunnelDataMsgs ()
|
||
|
{
|
||
|
if (!m_TunnelDataMsgs.empty ())
|
||
|
{
|
||
10 years ago
|
auto num = m_TunnelDataMsgs.size ();
|
||
|
if (num > 1)
|
||
9 years ago
|
LogPrint (eLogDebug, "TransitTunnel: ", GetTunnelID (), "->", GetNextTunnelID (), " ", num);
|
||
10 years ago
|
i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
|
||
|
m_TunnelDataMsgs.clear ();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnel::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
|
||
11 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "TransitTunnel: We are not a gateway for ", GetTunnelID ());
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnel::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "TransitTunnel: Incoming tunnel message is not supported ", GetTunnelID ());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnelGateway::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
|
||
11 years ago
|
{
|
||
11 years ago
|
TunnelMessageBlock block;
|
||
|
block.deliveryType = eDeliveryTypeLocal;
|
||
|
block.data = msg;
|
||
11 years ago
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
||
10 years ago
|
m_Gateway.PutTunnelDataMsg (block);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnelGateway::FlushTunnelDataMsgs ()
|
||
|
{
|
||
10 years ago
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
||
10 years ago
|
m_Gateway.SendBuffer ();
|
||
|
}
|
||
|
|
||
10 years ago
|
void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
|
||
11 years ago
|
{
|
||
10 years ago
|
auto newMsg = CreateEmptyTunnelDataMsg ();
|
||
|
EncryptTunnelMsg (tunnelMsg, newMsg);
|
||
11 years ago
|
|
||
9 years ago
|
LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
|
||
10 years ago
|
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
|
||
11 years ago
|
const uint8_t * nextIdent, uint32_t nextTunnelID,
|
||
|
const uint8_t * layerKey,const uint8_t * ivKey,
|
||
|
bool isGateway, bool isEndpoint)
|
||
11 years ago
|
{
|
||
11 years ago
|
if (isEndpoint)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "TransitTunnel: endpoint ", receiveTunnelID, " created");
|
||
9 years ago
|
return std::make_shared<TransitTunnelEndpoint> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
||
11 years ago
|
}
|
||
|
else if (isGateway)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "TransitTunnel: gateway ", receiveTunnelID, " created");
|
||
9 years ago
|
return std::make_shared<TransitTunnelGateway> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
||
11 years ago
|
}
|
||
|
else
|
||
11 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
|
||
9 years ago
|
return std::make_shared<TransitTunnelParticipant> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|