From 0a5d4741af2dfba293c8226122bcc529702cdf89 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 30 Jul 2014 18:20:42 -0400 Subject: [PATCH] use short I2NP messages --- I2NPProtocol.cpp | 17 +++++++++++------ I2NPProtocol.h | 8 +++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 81d078c3..7e1e78d7 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -26,6 +26,11 @@ namespace i2p return new I2NPMessageBuffer(); } + I2NPMessage * NewI2NPMessage (size_t len) + { + return (len < I2NP_MAX_SHORT_MESSAGE_SIZE/2) ? NewI2NPShortMessage () : NewI2NPMessage (); + } + void DeleteI2NPMessage (I2NPMessage * msg) { delete msg; @@ -64,7 +69,7 @@ namespace i2p I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID) { - I2NPMessage * msg = NewI2NPMessage (); + I2NPMessage * msg = NewI2NPMessage (len); memcpy (msg->GetPayload (), buf, len); msg->len += len; FillI2NPMessageHeader (msg, msgType, replyMsgID); @@ -164,7 +169,7 @@ namespace i2p I2NPMessage * CreateDatabaseSearchReply (const i2p::data::IdentHash& ident, const i2p::data::RouterInfo * floodfill) { - I2NPMessage * m = NewI2NPMessage (); + I2NPMessage * m = NewI2NPShortMessage (); uint8_t * buf = m->GetPayload (); size_t len = 0; memcpy (buf, ident, 32); @@ -188,7 +193,7 @@ namespace i2p if (!router) // we send own RouterInfo router = &context.GetRouterInfo (); - I2NPMessage * m = NewI2NPMessage (); + I2NPMessage * m = NewI2NPShortMessage (); I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload (); memcpy (msg->key, router->GetIdentHash (), 32); @@ -213,7 +218,7 @@ namespace i2p I2NPMessage * CreateDatabaseStoreMsg (const i2p::data::LeaseSet * leaseSet) { if (!leaseSet) return nullptr; - I2NPMessage * m = NewI2NPMessage (); + I2NPMessage * m = NewI2NPShortMessage (); I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)m->GetPayload (); memcpy (msg->key, leaseSet->GetIdentHash (), 32); msg->type = 1; // LeaseSet @@ -398,7 +403,7 @@ namespace i2p I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len) { - I2NPMessage * msg = NewI2NPMessage (); + I2NPMessage * msg = NewI2NPMessage (len); TunnelGatewayHeader * header = (TunnelGatewayHeader *)msg->GetPayload (); header->tunnelID = htobe32 (tunnelID); header->length = htobe16 (len); @@ -433,7 +438,7 @@ namespace i2p I2NPMessage * CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID) { - I2NPMessage * msg = NewI2NPMessage (); + I2NPMessage * msg = NewI2NPMessage (len); size_t gatewayMsgOffset = sizeof (I2NPHeader) + sizeof (TunnelGatewayHeader); msg->offset += gatewayMsgOffset; msg->len += gatewayMsgOffset; diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 19301444..815cd7cf 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -106,10 +106,11 @@ namespace tunnel struct I2NPMessage { uint8_t * buf; - size_t len, offset; + size_t len, offset, maxLen; i2p::tunnel::InboundTunnel * from; - I2NPMessage (): buf (nullptr),len (sizeof (I2NPHeader) + 2), offset(2), from (nullptr) {}; + I2NPMessage (): buf (nullptr),len (sizeof (I2NPHeader) + 2), + offset(2), maxLen (0), from (nullptr) {}; // reserve 2 bytes for NTCP header I2NPHeader * GetHeader () { return (I2NPHeader *)GetBuffer (); }; uint8_t * GetPayload () { return GetBuffer () + sizeof(I2NPHeader); }; @@ -151,12 +152,13 @@ namespace tunnel template struct I2NPMessageBuffer: public I2NPMessage { - I2NPMessageBuffer () { buf = m_Buffer; }; + I2NPMessageBuffer () { buf = m_Buffer; maxLen = sz; }; uint8_t m_Buffer[sz]; }; I2NPMessage * NewI2NPMessage (); I2NPMessage * NewI2NPShortMessage (); + I2NPMessage * NewI2NPMessage (size_t len); void DeleteI2NPMessage (I2NPMessage * msg); void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0); void RenewI2NPMessageHeader (I2NPMessage * msg);