i2pd/TransitTunnel.cpp

110 lines
3.3 KiB
C++
Raw Normal View History

2013-11-11 03:19:49 +04:00
#include <string.h>
#include "I2PEndian.h"
2013-11-11 03:19:49 +04:00
#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,
2013-11-29 16:52:09 +04:00
const uint8_t * layerKey,const uint8_t * ivKey):
2014-01-06 07:52:17 +04:00
m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID),
2015-01-22 05:50:46 +03:00
m_NextIdent (nextIdent)
2013-11-11 03:19:49 +04:00
{
2014-05-16 01:55:09 +04:00
m_Encryption.SetKeys (layerKey, ivKey);
2013-11-11 03:19:49 +04:00
}
void TransitTunnel::EncryptTunnelMsg (std::shared_ptr<I2NPMessage> tunnelMsg)
2014-05-16 01:55:09 +04:00
{
m_Encryption.Encrypt (tunnelMsg->GetPayload () + 4, tunnelMsg->GetPayload () + 4);
2013-11-11 03:19:49 +04:00
}
2015-01-22 05:50:46 +03:00
TransitTunnelParticipant::~TransitTunnelParticipant ()
{
}
void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> tunnelMsg)
2013-11-11 03:19:49 +04:00
{
2013-11-29 16:52:09 +04:00
EncryptTunnelMsg (tunnelMsg);
2013-11-11 03:19:49 +04:00
2014-08-02 18:06:38 +04:00
m_NumTransmittedBytes += tunnelMsg->GetLength ();
2015-01-22 05:50:46 +03:00
htobe32buf (tunnelMsg->GetPayload (), GetNextTunnelID ());
FillI2NPMessageHeader (tunnelMsg.get (), eI2NPTunnelData); // TODO
m_TunnelDataMsgs.push_back (tunnelMsg);
2013-11-11 03:19:49 +04:00
}
2015-01-22 05:50:46 +03:00
void TransitTunnelParticipant::FlushTunnelDataMsgs ()
{
if (!m_TunnelDataMsgs.empty ())
{
2015-04-11 02:58:32 +03:00
auto num = m_TunnelDataMsgs.size ();
if (num > 1)
LogPrint (eLogDebug, "TransitTunnel: ",GetTunnelID (),"->", GetNextTunnelID (), " ", num);
2015-01-22 05:50:46 +03:00
i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
m_TunnelDataMsgs.clear ();
}
}
void TransitTunnel::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
2013-11-29 16:52:09 +04:00
{
2015-01-22 05:50:46 +03:00
LogPrint (eLogError, "We are not a gateway for transit tunnel ", m_TunnelID);
2013-11-29 16:52:09 +04:00
}
void TransitTunnel::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> tunnelMsg)
2015-01-22 05:50:46 +03:00
{
LogPrint (eLogError, "Incoming tunnel message is not supported ", m_TunnelID);
}
void TransitTunnelGateway::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
2013-11-29 16:52:09 +04:00
{
TunnelMessageBlock block;
block.deliveryType = eDeliveryTypeLocal;
block.data = msg;
2014-06-26 23:41:12 +04:00
std::unique_lock<std::mutex> l(m_SendMutex);
2015-01-24 06:05:33 +03:00
m_Gateway.PutTunnelDataMsg (block);
2013-11-29 16:52:09 +04:00
}
2015-01-24 06:05:33 +03:00
void TransitTunnelGateway::FlushTunnelDataMsgs ()
{
2015-02-12 02:47:20 +03:00
std::unique_lock<std::mutex> l(m_SendMutex);
2015-01-24 06:05:33 +03:00
m_Gateway.SendBuffer ();
}
void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> tunnelMsg)
2013-11-29 16:52:09 +04:00
{
EncryptTunnelMsg (tunnelMsg);
2015-01-22 05:50:46 +03:00
LogPrint (eLogDebug, "TransitTunnel endpoint for ", GetTunnelID ());
2013-11-29 16:52:09 +04:00
m_Endpoint.HandleDecryptedTunnelDataMsg (tunnelMsg);
}
TransitTunnel * CreateTransitTunnel (uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey,
bool isGateway, bool isEndpoint)
2013-11-11 03:19:49 +04:00
{
2013-11-29 16:52:09 +04:00
if (isEndpoint)
{
2015-01-22 05:50:46 +03:00
LogPrint (eLogInfo, "TransitTunnel endpoint: ", receiveTunnelID, " created");
2013-11-29 16:52:09 +04:00
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
}
else if (isGateway)
{
2015-01-22 05:50:46 +03:00
LogPrint (eLogInfo, "TransitTunnel gateway: ", receiveTunnelID, " created");
2013-11-29 16:52:09 +04:00
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
}
else
2013-11-11 03:19:49 +04:00
{
2015-01-22 05:50:46 +03:00
LogPrint (eLogInfo, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
return new TransitTunnelParticipant (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
2013-11-11 03:19:49 +04:00
}
}
}
2014-06-26 23:41:12 +04:00
}