i2pd/TransitTunnel.cpp

94 lines
2.9 KiB
C++
Raw Normal View History

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
{
TransitTunnel::TransitTunnel (uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID,
2013-11-29 12:52:09 +00:00
const uint8_t * layerKey,const uint8_t * ivKey):
2014-01-06 03:52:17 +00:00
m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID),
m_NextIdent (nextIdent), m_NumTransmittedBytes (0)
2013-11-10 23:19:49 +00:00
{
memcpy (m_LayerKey, layerKey, 32);
memcpy (m_IVKey, ivKey, 32);
}
2013-11-29 12:52:09 +00:00
void TransitTunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
2013-11-10 23:19:49 +00:00
{
2013-11-29 12:52:09 +00:00
uint8_t * payload = tunnelMsg->GetPayload () + 4;
2013-11-10 23:19:49 +00:00
m_ECBEncryption.SetKey (m_IVKey, 32);
m_ECBEncryption.ProcessData(payload, payload, 16); // iv
m_CBCEncryption.SetKeyWithIV (m_LayerKey, 32, payload);
2013-11-29 12:52:09 +00:00
m_CBCEncryption.ProcessData(payload + 16, payload + 16, TUNNEL_DATA_ENCRYPTED_SIZE); // payload
2013-11-10 23:19:49 +00:00
m_ECBEncryption.SetKey (m_IVKey, 32);
m_ECBEncryption.ProcessData(payload, payload, 16); // double iv encryption
}
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
{
2013-11-29 12:52:09 +00:00
EncryptTunnelMsg (tunnelMsg);
2013-11-10 23:19:49 +00:00
2013-11-29 12:52:09 +00:00
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID);
*(uint32_t *)(tunnelMsg->GetPayload ()) = htobe32 (m_NextTunnelID);
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
i2p::transports.SendMessage (m_NextIdent, tunnelMsg);
2014-01-06 03:52:17 +00:00
m_NumTransmittedBytes += tunnelMsg->GetLength ();
2013-11-10 23:19:49 +00:00
}
void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg)
2013-11-29 12:52:09 +00:00
{
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID);
i2p::DeleteI2NPMessage (msg);
}
void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg)
2013-11-29 12:52:09 +00:00
{
TunnelMessageBlock block;
block.deliveryType = eDeliveryTypeLocal;
block.data = msg;
m_Gateway.SendTunnelDataMsg (block);
2013-11-29 12:52:09 +00: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-10 23:19:49 +00:00
{
2013-11-29 12:52:09 +00: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-10 23:19:49 +00:00
{
2013-11-29 12:52:09 +00:00
LogPrint ("TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
return new TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
2013-11-10 23:19:49 +00:00
}
}
}
}