From 6732ba21f92e973f2c7a1a1f2a7c90a20fbd580e Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 12 Mar 2014 20:13:49 -0400 Subject: [PATCH] inbound tunnel where an I2NP messages has been received from --- Garlic.cpp | 9 +++++---- Garlic.h | 3 ++- I2NPProtocol.cpp | 18 +++++++++--------- I2NPProtocol.h | 11 +++++++++-- NTCPSession.cpp | 2 +- NetDb.cpp | 2 +- SSU.cpp | 2 +- Transports.cpp | 2 +- Tunnel.cpp | 1 + TunnelEndpoint.cpp | 2 +- 10 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index e73a6f02..a269daa5 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -262,8 +262,9 @@ namespace garlic return ret; } - void GarlicRouting::HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel) + void GarlicRouting::HandleGarlicMessage (I2NPMessage * msg) { + uint8_t * buf = msg->GetPayload (); uint32_t length = be32toh (*(uint32_t *)buf); buf += 4; std::string sessionTag((const char *)buf, 32); @@ -284,7 +285,7 @@ namespace garlic // new session ElGamalBlock elGamal; if (i2p::crypto::ElGamalDecrypt ( - isFromTunnel ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (), + msg->from ? i2p::context.GetLeaseSetPrivateKey () : i2p::context.GetPrivateKey (), buf, (uint8_t *)&elGamal, true)) { uint8_t iv[32]; // IV is first 16 bytes @@ -296,7 +297,7 @@ namespace garlic else LogPrint ("Failed to decrypt garlic"); } - + DeleteI2NPMessage (msg); } void GarlicRouting::HandleAESBlock (uint8_t * buf, size_t len, uint8_t * sessionKey) @@ -351,7 +352,7 @@ namespace garlic { case eGarlicDeliveryTypeLocal: LogPrint ("Garlic type local"); - i2p::HandleI2NPMessage (buf, len, false); + i2p::HandleI2NPMessage (buf, len); break; case eGarlicDeliveryTypeDestination: { diff --git a/Garlic.h b/Garlic.h index 0383b030..a31b14cf 100644 --- a/Garlic.h +++ b/Garlic.h @@ -9,6 +9,7 @@ #include #include "I2NPProtocol.h" #include "LeaseSet.h" +#include "Tunnel.h" namespace i2p { @@ -75,7 +76,7 @@ namespace garlic GarlicRouting (); ~GarlicRouting (); - void HandleGarlicMessage (uint8_t * buf, size_t len, bool isFromTunnel); + void HandleGarlicMessage (I2NPMessage * msg); void HandleDeliveryStatusMessage (uint8_t * buf, size_t len); I2NPMessage * WrapSingleMessage (const i2p::data::RoutingDestination& destination, I2NPMessage * msg); diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 0e189218..8990da3b 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -22,6 +22,7 @@ namespace i2p I2NPMessage * msg = new I2NPMessage; msg->offset = 2; // reserve 2 bytes for NTCP header, should reserve more for SSU in future msg->len = sizeof (I2NPHeader) + 2; + msg->from = nullptr; return msg; } @@ -414,7 +415,7 @@ namespace i2p return be16toh (header->size) + sizeof (I2NPHeader); } - void HandleI2NPMessage (uint8_t * msg, size_t len, bool isFromTunnel) + void HandleI2NPMessage (uint8_t * msg, size_t len) { I2NPHeader * header = (I2NPHeader *)msg; uint32_t msgID = be32toh (header->msgID); @@ -423,12 +424,7 @@ namespace i2p uint8_t * buf = msg + sizeof (I2NPHeader); int size = be16toh (header->size); switch (header->typeID) - { - case eI2NPGarlic: - LogPrint ("Garlic"); - i2p::garlic::routing.HandleGarlicMessage (buf, size, isFromTunnel); - break; - break; + { case eI2NPDeliveryStatus: LogPrint ("DeliveryStatus"); // we assume DeliveryStatusMessage is sent with garlic only @@ -451,7 +447,7 @@ namespace i2p } } - void HandleI2NPMessage (I2NPMessage * msg, bool isFromTunnel) + void HandleI2NPMessage (I2NPMessage * msg) { if (msg) { @@ -473,8 +469,12 @@ namespace i2p LogPrint ("DatabaseSearchReply"); i2p::data::netdb.PostI2NPMsg (msg); break; + case eI2NPGarlic: + LogPrint ("Garlic"); + i2p::garlic::routing.HandleGarlicMessage (msg); + break; default: - HandleI2NPMessage (msg->GetBuffer (), msg->GetLength (), isFromTunnel); + HandleI2NPMessage (msg->GetBuffer (), msg->GetLength ()); DeleteI2NPMessage (msg); } } diff --git a/I2NPProtocol.h b/I2NPProtocol.h index e7c04d16..2030bdad 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -91,11 +91,17 @@ namespace i2p eI2NPVariableTunnelBuildReply = 24 }; +namespace tunnel +{ + class InboundTunnel; +} + const int NTCP_MAX_MESSAGE_SIZE = 16384; struct I2NPMessage { uint8_t buf[NTCP_MAX_MESSAGE_SIZE]; size_t len, offset; + i2p::tunnel::InboundTunnel * from; I2NPHeader * GetHeader () { return (I2NPHeader *)(buf + offset); }; uint8_t * GetPayload () { return buf + offset + sizeof(I2NPHeader); }; @@ -106,6 +112,7 @@ namespace i2p { memcpy (buf + offset, other.buf + other.offset, other.GetLength ()); len = offset + other.GetLength (); + from = other.from; return *this; } @@ -169,8 +176,8 @@ namespace i2p I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessage * msg); size_t GetI2NPMessageLength (uint8_t * msg); - void HandleI2NPMessage (uint8_t * msg, size_t len, bool isFromTunnel); - void HandleI2NPMessage (I2NPMessage * msg, bool isFromTunnel); + void HandleI2NPMessage (uint8_t * msg, size_t len); + void HandleI2NPMessage (I2NPMessage * msg); } #endif diff --git a/NTCPSession.cpp b/NTCPSession.cpp index e2686248..1a72fdbc 100644 --- a/NTCPSession.cpp +++ b/NTCPSession.cpp @@ -424,7 +424,7 @@ namespace ntcp if (m_NextMessageOffset >= m_NextMessage->len + 4) // +checksum { // we have a complete I2NP message - i2p::HandleI2NPMessage (m_NextMessage, false); + i2p::HandleI2NPMessage (m_NextMessage); m_NextMessage = nullptr; } } diff --git a/NetDb.cpp b/NetDb.cpp index 14a33353..1b115c63 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -111,7 +111,7 @@ namespace data else // WTF? { LogPrint ("NetDb: unexpected message type ", msg->GetHeader ()->typeID); - i2p::HandleI2NPMessage (msg, false); + i2p::HandleI2NPMessage (msg); } msg = m_Queue.Get (); } diff --git a/SSU.cpp b/SSU.cpp index 9eb5cb7c..7efd14e4 100644 --- a/SSU.cpp +++ b/SSU.cpp @@ -614,7 +614,7 @@ namespace ssu m_IncomleteMessages.erase (msgID); msg->FromSSU (msgID); if (m_State == eSessionStateEstablished) - i2p::HandleI2NPMessage (msg, false); + i2p::HandleI2NPMessage (msg); else { // we expect DeliveryStatus diff --git a/Transports.cpp b/Transports.cpp index c0c0ea8f..9dc733c4 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -148,7 +148,7 @@ namespace i2p { if (ident == i2p::context.GetRouterInfo ().GetIdentHash ()) // we send it to ourself - i2p::HandleI2NPMessage (msg, false); + i2p::HandleI2NPMessage (msg); else m_Service.post (boost::bind (&Transports::PostMessage, this, ident, msg)); } diff --git a/Tunnel.cpp b/Tunnel.cpp index 601586ae..ed9708cb 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -130,6 +130,7 @@ namespace tunnel void InboundTunnel::HandleTunnelDataMsg (I2NPMessage * msg) { + msg->from = this; EncryptTunnelMsg (msg); m_Endpoint.HandleDecryptedTunnelDataMsg (msg); } diff --git a/TunnelEndpoint.cpp b/TunnelEndpoint.cpp index 0e23a837..fc92ec29 100644 --- a/TunnelEndpoint.cpp +++ b/TunnelEndpoint.cpp @@ -147,7 +147,7 @@ namespace tunnel switch (msg.deliveryType) { case eDeliveryTypeLocal: - i2p::HandleI2NPMessage (msg.data, true); + i2p::HandleI2NPMessage (msg.data); break; case eDeliveryTypeTunnel: i2p::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data));