2013-11-11 03:19:49 +04:00
|
|
|
#include <string.h>
|
2014-01-09 23:56:07 +04:00
|
|
|
#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),
|
|
|
|
m_NextIdent (nextIdent), m_NumTransmittedBytes (0)
|
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
|
|
|
}
|
|
|
|
|
2013-11-29 16:52:09 +04:00
|
|
|
void TransitTunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
|
2014-05-16 01:55:09 +04:00
|
|
|
{
|
|
|
|
m_Encryption.Encrypt (tunnelMsg->GetPayload () + 4);
|
2013-11-11 03:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
|
|
|
|
{
|
2013-11-29 16:52:09 +04:00
|
|
|
EncryptTunnelMsg (tunnelMsg);
|
2013-11-11 03:19:49 +04:00
|
|
|
|
2013-11-29 16:52:09 +04:00
|
|
|
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID);
|
2014-08-02 18:06:38 +04:00
|
|
|
m_NumTransmittedBytes += tunnelMsg->GetLength ();
|
2014-12-30 17:37:24 +03:00
|
|
|
htobe32buf (tunnelMsg->GetPayload (), m_NextTunnelID);
|
2013-11-29 16:52:09 +04:00
|
|
|
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
|
2014-08-02 18:06:38 +04:00
|
|
|
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.SendMessage (m_NextIdent, tunnelMsg);
|
2013-11-11 03:19:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-20 06:24:55 +04:00
|
|
|
void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg)
|
2013-11-29 16:52:09 +04:00
|
|
|
{
|
|
|
|
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID);
|
|
|
|
i2p::DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:24:55 +04:00
|
|
|
void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg)
|
2013-11-29 16:52:09 +04:00
|
|
|
{
|
2014-02-20 07:08:57 +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);
|
2014-02-20 07:08:57 +04:00
|
|
|
m_Gateway.SendTunnelDataMsg (block);
|
2013-11-29 16:52:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransitTunnelEndpoint::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
|
|
|
|
{
|
|
|
|
EncryptTunnelMsg (tunnelMsg);
|
|
|
|
|
|
|
|
LogPrint ("TransitTunnel endpoint for ", GetTunnelID ());
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LogPrint ("TransitTunnel endpoint: ", receiveTunnelID, " created");
|
|
|
|
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
|
|
|
}
|
|
|
|
else if (isGateway)
|
|
|
|
{
|
|
|
|
LogPrint ("TransitTunnel gateway: ", receiveTunnelID, " created");
|
|
|
|
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
|
|
|
}
|
|
|
|
else
|
2013-11-11 03:19:49 +04:00
|
|
|
{
|
2013-11-29 16:52:09 +04:00
|
|
|
LogPrint ("TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
|
|
|
|
return new TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
|
2013-11-11 03:19:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 23:41:12 +04:00
|
|
|
}
|