2013-11-10 23:19:49 +00:00
|
|
|
#include <string.h>
|
2015-07-28 20:48:38 +00:00
|
|
|
#include "util/I2PEndian.h"
|
2015-07-30 13:34:56 +00:00
|
|
|
#include "util/Log.h"
|
2013-11-10 23:19:49 +00:00
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "Tunnel.h"
|
2015-07-27 20:17:31 +00:00
|
|
|
#include "transport/Transports.h"
|
2013-11-10 23:19:49 +00:00
|
|
|
#include "TransitTunnel.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
2015-07-16 21:29:52 +00:00
|
|
|
{
|
|
|
|
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID,
|
|
|
|
const uint8_t * nextIdent, uint32_t nextTunnelID,
|
|
|
|
const uint8_t * layerKey,const uint8_t * ivKey):
|
|
|
|
m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID),
|
|
|
|
m_NextIdent (nextIdent)
|
|
|
|
{
|
|
|
|
m_Encryption.SetKeys (layerKey, ivKey);
|
|
|
|
}
|
2013-11-10 23:19:49 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
void TransitTunnel::EncryptTunnelMsg (std::shared_ptr<const I2NPMessage> in, std::shared_ptr<I2NPMessage> out)
|
|
|
|
{
|
|
|
|
m_Encryption.Encrypt (in->GetPayload () + 4, out->GetPayload () + 4);
|
|
|
|
}
|
2015-01-22 02:50:46 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
TransitTunnelParticipant::~TransitTunnelParticipant ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
|
|
|
|
{
|
|
|
|
auto newMsg = CreateEmptyTunnelDataMsg ();
|
|
|
|
EncryptTunnelMsg (tunnelMsg, newMsg);
|
|
|
|
|
|
|
|
m_NumTransmittedBytes += tunnelMsg->GetLength ();
|
|
|
|
htobe32buf (newMsg->GetPayload (), GetNextTunnelID ());
|
|
|
|
newMsg->FillI2NPMessageHeader (eI2NPTunnelData);
|
|
|
|
m_TunnelDataMsgs.push_back (newMsg);
|
|
|
|
}
|
2013-11-10 23:19:49 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
void TransitTunnelParticipant::FlushTunnelDataMsgs ()
|
|
|
|
{
|
|
|
|
if (!m_TunnelDataMsgs.empty ())
|
|
|
|
{
|
|
|
|
auto num = m_TunnelDataMsgs.size ();
|
|
|
|
if (num > 1)
|
|
|
|
LogPrint (eLogDebug, "TransitTunnel: ",GetTunnelID (),"->", GetNextTunnelID (), " ", num);
|
|
|
|
i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
|
|
|
|
m_TunnelDataMsgs.clear ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 10:02:09 +00:00
|
|
|
void TransitTunnel::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>)
|
2015-07-16 21:29:52 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogError, "We are not a gateway for transit tunnel ", m_TunnelID);
|
|
|
|
}
|
2013-11-29 12:52:09 +00:00
|
|
|
|
2015-08-30 10:02:09 +00:00
|
|
|
void TransitTunnel::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage>)
|
2015-07-16 21:29:52 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Incoming tunnel message is not supported ", m_TunnelID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransitTunnelGateway::SendTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage> msg)
|
|
|
|
{
|
|
|
|
TunnelMessageBlock block;
|
|
|
|
block.deliveryType = eDeliveryTypeLocal;
|
|
|
|
block.data = msg;
|
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
|
|
|
m_Gateway.PutTunnelDataMsg (block);
|
|
|
|
}
|
2013-11-29 12:52:09 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
void TransitTunnelGateway::FlushTunnelDataMsgs ()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_SendMutex);
|
|
|
|
m_Gateway.SendBuffer ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
|
|
|
|
{
|
|
|
|
auto newMsg = CreateEmptyTunnelDataMsg ();
|
|
|
|
EncryptTunnelMsg (tunnelMsg, newMsg);
|
|
|
|
|
|
|
|
LogPrint (eLogDebug, "TransitTunnel endpoint for ", GetTunnelID ());
|
|
|
|
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
TransitTunnel * CreateTransitTunnel (uint32_t receiveTunnelID,
|
|
|
|
const uint8_t * nextIdent, uint32_t nextTunnelID,
|
|
|
|
const uint8_t * layerKey,const uint8_t * ivKey,
|
|
|
|
bool isGateway, bool isEndpoint)
|
|
|
|
{
|
|
|
|
if (isEndpoint)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "TransitTunnel endpoint: ", receiveTunnelID, " created");
|
|
|
|
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
|
|
|
}
|
|
|
|
else if (isGateway)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "TransitTunnel gateway: ", receiveTunnelID, " created");
|
|
|
|
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
|
|
|
|
return new TransitTunnelParticipant (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
|
|
|
}
|
|
|
|
}
|
2013-11-10 23:19:49 +00:00
|
|
|
}
|
2014-06-26 19:41:12 +00:00
|
|
|
}
|