i2pd/libi2pd/TransitTunnel.cpp

124 lines
3.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* 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
#include <string.h>
#include "I2PEndian.h"
2013-11-10 23:19:49 +00:00
#include "Log.h"
#include "RouterContext.h"
#include "I2NPProtocol.h"
#include "Tunnel.h"
#include "Transports.h"
#include "TransitTunnel.h"
namespace i2p
{
namespace tunnel
2018-01-06 03:48:51 +00:00
{
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID,
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey):
TunnelBase (receiveTunnelID, nextTunnelID, nextIdent),
m_LayerKey (layerKey), m_IVKey (ivKey)
2018-01-06 03:48:51 +00:00
{
}
2013-11-10 23:19:49 +00:00
void TransitTunnel::EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out)
2018-01-06 03:48:51 +00:00
{
if (!m_Encryption)
{
m_Encryption.reset (new i2p::crypto::TunnelEncryption);
m_Encryption->SetKeys (m_LayerKey, m_IVKey);
}
m_Encryption->Encrypt (in->GetPayload () + 4, out->GetPayload () + 4);
2017-05-02 18:20:00 +00:00
i2p::transport::transports.UpdateTotalTransitTransmittedBytes (TUNNEL_DATA_MSG_SIZE);
2018-01-06 03:48:51 +00:00
}
2015-01-22 02:50:46 +00:00
TransitTunnelParticipant::~TransitTunnelParticipant ()
{
2018-01-06 03:48:51 +00:00
}
void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg)
2013-11-10 23:19:49 +00:00
{
EncryptTunnelMsg (tunnelMsg, tunnelMsg);
2018-01-06 03:48:51 +00:00
2014-08-02 14:06:38 +00:00
m_NumTransmittedBytes += tunnelMsg->GetLength ();
htobe32buf (tunnelMsg->GetPayload (), GetNextTunnelID ());
tunnelMsg->FillI2NPMessageHeader (eI2NPTunnelData);
m_TunnelDataMsgs.push_back (tunnelMsg);
2013-11-10 23:19:49 +00:00
}
2015-01-22 02:50:46 +00:00
void TransitTunnelParticipant::FlushTunnelDataMsgs ()
{
if (!m_TunnelDataMsgs.empty ())
2018-01-06 03:48:51 +00:00
{
2015-04-10 23:58:32 +00:00
auto num = m_TunnelDataMsgs.size ();
if (num > 1)
2015-12-21 02:20:36 +00:00
LogPrint (eLogDebug, "TransitTunnel: ", GetTunnelID (), "->", GetNextTunnelID (), " ", num);
2015-01-22 02:50:46 +00:00
i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
m_TunnelDataMsgs.clear ();
2018-01-06 03:48:51 +00:00
}
}
void TransitTunnel::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
2018-01-06 03:48:51 +00:00
{
2015-12-21 02:20:36 +00:00
LogPrint (eLogError, "TransitTunnel: We are not a gateway for ", GetTunnelID ());
2018-01-06 03:48:51 +00:00
}
2013-11-29 12:52:09 +00:00
void TransitTunnel::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg)
2015-01-22 02:50:46 +00:00
{
2015-12-21 02:20:36 +00:00
LogPrint (eLogError, "TransitTunnel: Incoming tunnel message is not supported ", GetTunnelID ());
2018-01-06 03:48:51 +00:00
}
void TransitTunnelGateway::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
2013-11-29 12:52:09 +00:00
{
TunnelMessageBlock block;
block.deliveryType = eDeliveryTypeLocal;
block.data = msg;
2014-06-26 19:41:12 +00:00
std::unique_lock<std::mutex> l(m_SendMutex);
2015-01-24 03:05:33 +00:00
m_Gateway.PutTunnelDataMsg (block);
2018-01-06 03:48:51 +00:00
}
2013-11-29 12:52:09 +00:00
2015-01-24 03:05:33 +00:00
void TransitTunnelGateway::FlushTunnelDataMsgs ()
{
2015-02-11 23:47:20 +00:00
std::unique_lock<std::mutex> l(m_SendMutex);
2015-01-24 03:05:33 +00:00
m_Gateway.SendBuffer ();
2018-01-06 03:48:51 +00:00
}
void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg)
2013-11-29 12:52:09 +00:00
{
auto newMsg = CreateEmptyTunnelDataMsg (true);
EncryptTunnelMsg (tunnelMsg, newMsg);
2018-01-06 03:48:51 +00:00
2015-12-21 02:20:36 +00:00
LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());
2018-01-06 03:48:51 +00:00
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
2013-11-29 12:52:09 +00:00
}
2018-01-06 03:48:51 +00:00
2016-03-01 20:22:36 +00:00
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
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
{
2013-11-29 12:52:09 +00:00
if (isEndpoint)
2018-01-06 03:48:51 +00:00
{
2016-06-27 13:00:00 +00:00
LogPrint (eLogDebug, "TransitTunnel: endpoint ", receiveTunnelID, " created");
2016-03-01 20:22:36 +00:00
return std::make_shared<TransitTunnelEndpoint> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
2018-01-06 03:48:51 +00:00
}
2013-11-29 12:52:09 +00:00
else if (isGateway)
2018-01-06 03:48:51 +00:00
{
2015-12-21 02:20:36 +00:00
LogPrint (eLogInfo, "TransitTunnel: gateway ", receiveTunnelID, " created");
2016-03-01 20:22:36 +00:00
return std::make_shared<TransitTunnelGateway> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
2018-01-06 03:48:51 +00:00
}
else
{
2016-06-27 13:00:00 +00:00
LogPrint (eLogDebug, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
2016-03-01 20:22:36 +00:00
return std::make_shared<TransitTunnelParticipant> (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
2018-01-06 03:48:51 +00:00
}
}
2013-11-10 23:19:49 +00:00
}
2014-06-26 19:41:12 +00:00
}