From bfc6274cd8e1f0cbbe9a8a6b569edd7bfe9f652d Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 1 Jan 2015 16:51:15 -0500 Subject: [PATCH] methods for I2NP header access --- I2NPProtocol.cpp | 28 +++++++++++----------------- I2NPProtocol.h | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index c2ae6d3b..a9c1f1f3 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -1,7 +1,6 @@ #include #include #include "I2PEndian.h" -#include #include #include "ElGamal.h" #include "Timestamp.h" @@ -40,31 +39,26 @@ namespace i2p static std::atomic I2NPmsgID(0); // TODO: create class void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID) { - I2NPHeader * header = msg->GetHeader (); - header->typeID = msgType; + msg->SetTypeID (msgType); if (replyMsgID) // for tunnel creation - header->msgID = htobe32 (replyMsgID); + msg->SetMsgID (replyMsgID); else { - header->msgID = htobe32 (I2NPmsgID); + msg->SetMsgID (I2NPmsgID); I2NPmsgID++; } - header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number - int len = msg->GetLength () - sizeof (I2NPHeader); - header->size = htobe16 (len); - uint8_t hash[32]; - CryptoPP::SHA256().CalculateDigest(hash, msg->GetPayload (), len); - header->chks = hash[0]; - } - + msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number + msg->UpdateSize (); + msg->UpdateChks (); + } + void RenewI2NPMessageHeader (I2NPMessage * msg) { if (msg) { - I2NPHeader * header = msg->GetHeader (); - header->msgID = htobe32 (I2NPmsgID); + msg->SetMsgID (I2NPmsgID); I2NPmsgID++; - header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); + msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); } } @@ -589,7 +583,7 @@ namespace i2p { if (msg) { - switch (msg->GetHeader ()->typeID) + switch (msg->GetTypeID ()) { case eI2NPTunnelData: LogPrint ("TunnelData"); diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 9d3fa79b..c0d98459 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "I2PEndian.h" #include "Identity.h" @@ -10,7 +11,14 @@ #include "LeaseSet.h" namespace i2p -{ +{ + // I2NP header + const size_t I2NP_HEADER_TYPEID_OFFSET = 0; + const size_t I2NP_HEADER_MSGID_OFFSET = I2NP_HEADER_TYPEID_OFFSET + 1; + const size_t I2NP_HEADER_EXPIRATION_OFFSET = I2NP_HEADER_MSGID_OFFSET + 4; + const size_t I2NP_HEADER_SIZE_OFFSET = I2NP_HEADER_EXPIRATION_OFFSET + 8; + const size_t I2NP_HEADER_CHKS_OFFSET = I2NP_HEADER_SIZE_OFFSET + 2; + #pragma pack (1) struct I2NPHeader @@ -114,11 +122,32 @@ namespace tunnel I2NPMessage (): buf (nullptr),len (sizeof (I2NPHeader) + 2), offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header - I2NPHeader * GetHeader () { return (I2NPHeader *)GetBuffer (); }; + I2NPHeader * GetHeader () { return (I2NPHeader *)GetBuffer (); }; // depricated + // header accessors + uint8_t * GetHeaderBuffer () { return GetBuffer (); }; + const uint8_t * GetHeaderBuffer () const { return GetBuffer (); }; + void SetTypeID (uint8_t typeID) { GetHeaderBuffer ()[I2NP_HEADER_TYPEID_OFFSET] = typeID; }; + uint8_t GetTypeID () const { return GetHeaderBuffer ()[I2NP_HEADER_TYPEID_OFFSET]; }; + void SetMsgID (uint32_t msgID) { htobe32buf (GetHeaderBuffer () + I2NP_HEADER_MSGID_OFFSET, msgID); }; + uint32_t GetMsgID () const { return bufbe32toh (GetHeaderBuffer () + I2NP_HEADER_MSGID_OFFSET); }; + void SetExpiration (uint64_t expiration) { htobe64buf (GetHeaderBuffer () + I2NP_HEADER_EXPIRATION_OFFSET, expiration); }; + uint64_t GetExpiration () const { return bufbe64toh (GetHeaderBuffer () + I2NP_HEADER_EXPIRATION_OFFSET); }; + uint16_t GetSize () const { return bufbe16toh (GetHeaderBuffer () + I2NP_HEADER_SIZE_OFFSET); }; + void UpdateSize () { htobe16buf (GetHeaderBuffer () + I2NP_HEADER_SIZE_OFFSET, GetPayloadLength ()); }; + void UpdateChks () + { + uint8_t hash[32]; + CryptoPP::SHA256().CalculateDigest(hash, GetPayload (), GetPayloadLength ()); + GetHeaderBuffer ()[I2NP_HEADER_CHKS_OFFSET] = hash[0]; + } + + // payload uint8_t * GetPayload () { return GetBuffer () + sizeof(I2NPHeader); }; uint8_t * GetBuffer () { return buf + offset; }; const uint8_t * GetBuffer () const { return buf + offset; }; - size_t GetLength () const { return len - offset; }; + size_t GetLength () const { return len - offset; }; + size_t GetPayloadLength () const { return GetLength () - sizeof(I2NPHeader); }; + void Align (size_t alignment) { size_t rem = ((size_t)GetBuffer ()) % alignment;