From 8c5111e11a25568befa87e2f107d0f7422de79a7 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 17 Jul 2018 15:17:05 -0400 Subject: [PATCH] handle NTCP2 I2NP messages --- libi2pd/I2NPProtocol.h | 20 ++++++++++++++++++++ libi2pd/NTCP2.cpp | 33 ++++++++++++++++++++++++++++++++- libi2pd/NTCP2.h | 12 ++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/libi2pd/I2NPProtocol.h b/libi2pd/I2NPProtocol.h index f394d284..01b474be 100644 --- a/libi2pd/I2NPProtocol.h +++ b/libi2pd/I2NPProtocol.h @@ -26,6 +26,9 @@ namespace i2p const size_t I2NP_SHORT_HEADER_EXPIRATION_OFFSET = I2NP_SHORT_HEADER_TYPEID_OFFSET + 1; const size_t I2NP_SHORT_HEADER_SIZE = I2NP_SHORT_HEADER_EXPIRATION_OFFSET + 4; + // I2NP NTCP2 header + const size_t I2NP_NTCP2_HEADER_SIZE = I2NP_HEADER_EXPIRATION_OFFSET + 4; + // Tunnel Gateway header const size_t TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET = 0; const size_t TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET = TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET + 4; @@ -194,6 +197,23 @@ namespace tunnel len = offset + I2NP_SHORT_HEADER_SIZE + bufbe16toh (header + I2NP_HEADER_SIZE_OFFSET); return bufbe32toh (header + I2NP_HEADER_MSGID_OFFSET); } + // for NTCP2 only + uint8_t * GetNTCP2Header () { return GetPayload () - I2NP_NTCP2_HEADER_SIZE; }; + void FromNTCP2 () + { + const uint8_t * ntcp2 = GetNTCP2Header (); + memcpy (GetHeader () + I2NP_HEADER_TYPEID_OFFSET, ntcp2 + I2NP_HEADER_TYPEID_OFFSET, 5); // typeid + msgid + SetExpiration (bufbe32toh (ntcp2 + I2NP_HEADER_EXPIRATION_OFFSET)*1000LL); + SetSize (len - offset - I2NP_HEADER_SIZE); + SetChks (0); + } + + void ToNTCP2 () + { + uint8_t * ntcp2 = GetNTCP2Header (); + htobe32buf (ntcp2 + I2NP_HEADER_EXPIRATION_OFFSET, bufbe64toh (GetHeader () + I2NP_HEADER_EXPIRATION_OFFSET)/1000LL); + memcpy (ntcp2 + I2NP_HEADER_TYPEID_OFFSET, GetHeader () + I2NP_HEADER_TYPEID_OFFSET, 5); // typeid + msgid + } void FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID = 0); void RenewI2NPMessageHeader (); diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index a5e70c24..3a6aa1c6 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -167,7 +167,7 @@ namespace transport { m_IsEstablished = true; m_Establisher.reset (nullptr); - transports.PeerConnected (shared_from_this ()); + // transports.PeerConnected (shared_from_this ()); } void NTCP2Session::CreateNonce (uint64_t seqn, uint8_t * nonce) @@ -619,8 +619,39 @@ namespace transport LogPrint (eLogError, "NTCP2: Unexpected block length ", size); break; } + switch (blk) + { + case eNTCP2BlkDateTime: + LogPrint (eLogDebug, "NTCP2: datetime"); + break; + case eNTCP2BlkOptions: + LogPrint (eLogDebug, "NTCP2: options"); + break; + case eNTCP2BlkRouterInfo: + LogPrint (eLogDebug, "NTCP2: RouterInfo"); + break; + case eNTCP2BlkI2NPMessage: + { + LogPrint (eLogDebug, "NTCP2: I2NP"); + auto nextMsg = NewI2NPMessage (size); + nextMsg->len = nextMsg->offset + size + 7; // 7 more bytes for full I2NP header + memcpy (nextMsg->GetNTCP2Header (), frame + offset, size); + nextMsg->FromNTCP2 (); + m_Handler.PutNextMessage (nextMsg); + break; + } + case eNTCP2BlkTermination: + LogPrint (eLogDebug, "NTCP2: termination"); + break; + case eNTCP2BlkPadding: + LogPrint (eLogDebug, "NTCP2: padding"); + break; + default: + LogPrint (eLogWarning, "NTCP2: Unknown block type ", (int)blk); + } offset += size; } + m_Handler.Flush (); } void NTCP2Session::SendNextFrame (const uint8_t * payload, size_t len) diff --git a/libi2pd/NTCP2.h b/libi2pd/NTCP2.h index 167b0528..8ce0ba6d 100644 --- a/libi2pd/NTCP2.h +++ b/libi2pd/NTCP2.h @@ -13,6 +13,16 @@ namespace i2p { namespace transport { + enum NTCP2BlockType + { + eNTCP2BlkDateTime = 0, + eNTCP2BlkOptions, // 1 + eNTCP2BlkRouterInfo, // 2 + eNTCP2BlkI2NPMessage, // 3 + eNTCP2BlkTermination, // 4 + eNTCP2BlkPadding = 254 + }; + struct NTCP2Establisher { NTCP2Establisher (); @@ -107,6 +117,8 @@ namespace transport uint8_t * m_NextReceivedBuffer, * m_NextSendBuffer; uint8_t m_ReceiveIV[8], m_SendIV[8]; uint64_t m_ReceiveSequenceNumber, m_SendSequenceNumber; + + i2p::I2NPMessagesHandler m_Handler; }; class NTCP2Server