Browse Source

methods for I2NP header access

pull/129/head
orignal 10 years ago
parent
commit
bfc6274cd8
  1. 24
      I2NPProtocol.cpp
  2. 31
      I2NPProtocol.h

24
I2NPProtocol.cpp

@ -1,7 +1,6 @@
#include <string.h> #include <string.h>
#include <atomic> #include <atomic>
#include "I2PEndian.h" #include "I2PEndian.h"
#include <cryptopp/sha.h>
#include <cryptopp/gzip.h> #include <cryptopp/gzip.h>
#include "ElGamal.h" #include "ElGamal.h"
#include "Timestamp.h" #include "Timestamp.h"
@ -40,31 +39,26 @@ namespace i2p
static std::atomic<uint32_t> I2NPmsgID(0); // TODO: create class static std::atomic<uint32_t> I2NPmsgID(0); // TODO: create class
void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID) void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID)
{ {
I2NPHeader * header = msg->GetHeader (); msg->SetTypeID (msgType);
header->typeID = msgType;
if (replyMsgID) // for tunnel creation if (replyMsgID) // for tunnel creation
header->msgID = htobe32 (replyMsgID); msg->SetMsgID (replyMsgID);
else else
{ {
header->msgID = htobe32 (I2NPmsgID); msg->SetMsgID (I2NPmsgID);
I2NPmsgID++; I2NPmsgID++;
} }
header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number
int len = msg->GetLength () - sizeof (I2NPHeader); msg->UpdateSize ();
header->size = htobe16 (len); msg->UpdateChks ();
uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, msg->GetPayload (), len);
header->chks = hash[0];
} }
void RenewI2NPMessageHeader (I2NPMessage * msg) void RenewI2NPMessageHeader (I2NPMessage * msg)
{ {
if (msg) if (msg)
{ {
I2NPHeader * header = msg->GetHeader (); msg->SetMsgID (I2NPmsgID);
header->msgID = htobe32 (I2NPmsgID);
I2NPmsgID++; I2NPmsgID++;
header->expiration = htobe64 (i2p::util::GetMillisecondsSinceEpoch () + 5000); msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000);
} }
} }
@ -589,7 +583,7 @@ namespace i2p
{ {
if (msg) if (msg)
{ {
switch (msg->GetHeader ()->typeID) switch (msg->GetTypeID ())
{ {
case eI2NPTunnelData: case eI2NPTunnelData:
LogPrint ("TunnelData"); LogPrint ("TunnelData");

31
I2NPProtocol.h

@ -3,6 +3,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <set> #include <set>
#include <cryptopp/sha.h>
#include <string.h> #include <string.h>
#include "I2PEndian.h" #include "I2PEndian.h"
#include "Identity.h" #include "Identity.h"
@ -11,6 +12,13 @@
namespace i2p 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) #pragma pack (1)
struct I2NPHeader struct I2NPHeader
@ -114,11 +122,32 @@ namespace tunnel
I2NPMessage (): buf (nullptr),len (sizeof (I2NPHeader) + 2), I2NPMessage (): buf (nullptr),len (sizeof (I2NPHeader) + 2),
offset(2), maxLen (0), from (nullptr) {}; offset(2), maxLen (0), from (nullptr) {};
// reserve 2 bytes for NTCP header // 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 * GetPayload () { return GetBuffer () + sizeof(I2NPHeader); };
uint8_t * GetBuffer () { return buf + offset; }; uint8_t * GetBuffer () { return buf + offset; };
const uint8_t * GetBuffer () const { 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) void Align (size_t alignment)
{ {
size_t rem = ((size_t)GetBuffer ()) % alignment; size_t rem = ((size_t)GetBuffer ()) % alignment;

Loading…
Cancel
Save