From 154105a05986cd0b29cfe97389f3b0d37988f4d9 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 19 Feb 2014 21:24:55 -0500 Subject: [PATCH] pass delivery intructions to tunnel gateway --- I2NPProtocol.cpp | 2 +- NTCPSession.h | 2 +- TransitTunnel.cpp | 6 +++--- TransitTunnel.h | 4 ++-- Tunnel.cpp | 17 +--------------- Tunnel.h | 2 +- TunnelGateway.cpp | 52 +++++++++++++++++++++++++++++++++-------------- TunnelGateway.h | 6 ++++-- 8 files changed, 50 insertions(+), 41 deletions(-) diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 6503d70f..f42d9f9d 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -392,7 +392,7 @@ namespace i2p LogPrint ("TunnelGateway of ", (int)len, " bytes for tunnel ", (unsigned int)tunnelID, ". Msg type ", (int)msg->GetHeader()->typeID); i2p::tunnel::TransitTunnel * tunnel = i2p::tunnel::tunnels.GetTransitTunnel (tunnelID); if (tunnel) - tunnel->SendTunnelDataMsg (nullptr, 0, msg); + tunnel->SendTunnelDataMsg (msg); else { LogPrint ("Tunnel ", (unsigned int)tunnelID, " not found"); diff --git a/NTCPSession.h b/NTCPSession.h index 71cf5af9..0976581d 100644 --- a/NTCPSession.h +++ b/NTCPSession.h @@ -60,7 +60,7 @@ namespace ntcp #pragma pack() - const int TERMINATION_TIMEOUT = 60; // 1 minute + const int TERMINATION_TIMEOUT = 150; // 2.5 minutes class NTCPSession { public: diff --git a/TransitTunnel.cpp b/TransitTunnel.cpp index 6c9a4f6f..2435cbe5 100644 --- a/TransitTunnel.cpp +++ b/TransitTunnel.cpp @@ -47,15 +47,15 @@ namespace tunnel m_NumTransmittedBytes += tunnelMsg->GetLength (); } - void TransitTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) + void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg) { LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID); i2p::DeleteI2NPMessage (msg); } - void TransitTunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) + void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg) { - m_Gateway.SendTunnelDataMsg (gwHash, gwTunnel, msg); + m_Gateway.SendTunnelDataMsg (msg); } void TransitTunnelEndpoint::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) diff --git a/TransitTunnel.h b/TransitTunnel.h index 570429f5..feeeaa58 100644 --- a/TransitTunnel.h +++ b/TransitTunnel.h @@ -22,7 +22,7 @@ namespace tunnel const uint8_t * layerKey,const uint8_t * ivKey); virtual void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg); - virtual void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); + virtual void SendTunnelDataMsg (i2p::I2NPMessage * msg); virtual size_t GetNumTransmittedBytes () const { return m_NumTransmittedBytes; }; uint32_t GetTunnelID () const { return m_TunnelID; }; @@ -54,7 +54,7 @@ namespace tunnel TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey), m_Gateway(this) {}; - void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); + void SendTunnelDataMsg (i2p::I2NPMessage * msg); size_t GetNumTransmittedBytes () const { return m_Gateway.GetNumSentBytes (); }; private: diff --git a/Tunnel.cpp b/Tunnel.cpp index 1163f47e..88ab9534 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -142,22 +142,7 @@ namespace tunnel void OutboundTunnel::SendTunnelDataMsg (std::vector msgs) { for (auto& it : msgs) - { - switch (it.deliveryType) - { - case eDeliveryTypeLocal: - m_Gateway.SendTunnelDataMsg (nullptr, 0, it.data); - break; - case eDeliveryTypeTunnel: - m_Gateway.SendTunnelDataMsg (it.hash, it.tunnelID, it.data); - break; - case eDeliveryTypeRouter: - m_Gateway.SendTunnelDataMsg (it.hash, 0, it.data); - break; - default: - LogPrint ("Unexpected delivery type ", (int)it.deliveryType); - } - } + m_Gateway.PutTunnelDataMsg (it); m_Gateway.SendBuffer (); } diff --git a/Tunnel.h b/Tunnel.h index 3498eae3..7e7f94d5 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -21,7 +21,7 @@ namespace i2p { namespace tunnel { - const int TUNNEL_EXPIRATION_TIMEOUT = 600; // 10 minutes + const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes class OutboundTunnel; class InboundTunnel; diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index 55d78326..50eb1c7f 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -10,7 +10,7 @@ namespace i2p { namespace tunnel { - void TunnelGatewayBuffer::PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg) + void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block) { if (!m_CurrentTunnelDataMsg) CreateCurrentTunnelDataMessage (); @@ -18,24 +18,21 @@ namespace tunnel // create delivery instructions uint8_t di[43]; // max delivery instruction length is 43 for tunnel size_t diLen = 1;// flag - TunnelDeliveryType dt = eDeliveryTypeLocal; - if (gwHash) + if (block.deliveryType != eDeliveryTypeLocal) // tunnel or router { - if (gwTunnel) + if (block.deliveryType == eDeliveryTypeTunnel) { - *(uint32_t *)(di + diLen) = htobe32 (gwTunnel); + *(uint32_t *)(di + diLen) = htobe32 (block.tunnelID); diLen += 4; // tunnelID - dt = eDeliveryTypeTunnel; } - else - dt = eDeliveryTypeRouter; - memcpy (di + diLen, gwHash, 32); + memcpy (di + diLen, block.hash, 32); diLen += 32; //len } - di[0] = dt << 5; // set delivery type + di[0] = block.deliveryType << 5; // set delivery type // create fragments + I2NPMessage * msg = block.data; if (diLen + msg->GetLength () + 2<= m_RemainingSize) { // message fits. First and last fragment @@ -104,7 +101,7 @@ namespace tunnel { // delivery instructions don't fit. Create new message CompleteCurrentTunnelDataMessage (); - PutI2NPMsg (gwHash, gwTunnel, msg); + PutI2NPMsg (block); // don't delete msg because it's taken care inside } } @@ -151,16 +148,41 @@ namespace tunnel m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg); m_CurrentTunnelDataMsg = nullptr; } - + + void TunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg) + { + SendTunnelDataMsg (nullptr, 0, msg); + } + void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) { - PutTunnelDataMsg (gwHash, gwTunnel, msg); + TunnelMessageBlock block; + if (gwHash) + { + block.hash = gwHash; + if (gwTunnel) + { + block.deliveryType = eDeliveryTypeTunnel; + block.tunnelID = gwTunnel; + } + else + block.deliveryType = eDeliveryTypeRouter; + } + else + block.deliveryType = eDeliveryTypeLocal; + block.data = msg; + SendTunnelDataMsg (block); + } + + void TunnelGateway::SendTunnelDataMsg (const TunnelMessageBlock& block) + { + PutTunnelDataMsg (block); SendBuffer (); } - void TunnelGateway::PutTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) + void TunnelGateway::PutTunnelDataMsg (const TunnelMessageBlock& block) { - m_Buffer.PutI2NPMsg (gwHash, gwTunnel, msg); + m_Buffer.PutI2NPMsg (block); } void TunnelGateway::SendBuffer () diff --git a/TunnelGateway.h b/TunnelGateway.h index d68c8580..6226c8ec 100644 --- a/TunnelGateway.h +++ b/TunnelGateway.h @@ -15,7 +15,7 @@ namespace tunnel public: TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID), m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0) {}; - void PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg); + void PutI2NPMsg (const TunnelMessageBlock& block); std::vector GetTunnelDataMsgs (); private: @@ -37,8 +37,10 @@ namespace tunnel TunnelGateway (TunnelBase * tunnel): m_Tunnel (tunnel), m_Buffer (tunnel->GetNextTunnelID ()), m_NumSentBytes (0) {}; + void SendTunnelDataMsg (i2p::I2NPMessage * msg); void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); - void PutTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); + void SendTunnelDataMsg (const TunnelMessageBlock& block); + void PutTunnelDataMsg (const TunnelMessageBlock& block); void SendBuffer (); size_t GetNumSentBytes () const { return m_NumSentBytes; };