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