handle NTCP2 I2NP messages

This commit is contained in:
orignal 2018-07-17 15:17:05 -04:00
parent 5575b981c8
commit 8c5111e11a
3 changed files with 64 additions and 1 deletions

View File

@ -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 ();

View File

@ -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)

View File

@ -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