diff --git a/Destination.cpp b/Destination.cpp index 5e1dd2c8..148d9429 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -190,18 +190,17 @@ namespace client void ClientDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from) { - //TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved - I2NPHeader * header = (I2NPHeader *)buf; - switch (header->typeID) + uint8_t typeID = buf[I2NP_HEADER_TYPEID_OFFSET]; + switch (typeID) { case eI2NPData: - HandleDataMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDataMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; case eI2NPDatabaseStore: - HandleDatabaseStoreMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDatabaseStoreMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; case eI2NPDatabaseSearchReply: - HandleDatabaseSearchReplyMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDatabaseSearchReplyMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; default: i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from)); diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 3ad54ff5..423ee047 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -545,19 +545,18 @@ namespace i2p size_t GetI2NPMessageLength (const uint8_t * msg) { - I2NPHeader * header = (I2NPHeader *)msg; - return be16toh (header->size) + sizeof (I2NPHeader); + return bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET) + I2NP_HEADER_SIZE; } void HandleI2NPMessage (uint8_t * msg, size_t len) { - I2NPHeader * header = (I2NPHeader *)msg; - uint32_t msgID = be32toh (header->msgID); - LogPrint ("I2NP msg received len=", len,", type=", (int)header->typeID, ", msgID=", (unsigned int)msgID); + uint8_t typeID = msg[I2NP_HEADER_TYPEID_OFFSET]; + uint32_t msgID = bufbe32toh (msg + I2NP_HEADER_MSGID_OFFSET); + LogPrint ("I2NP msg received len=", len,", type=", (int)typeID, ", msgID=", (unsigned int)msgID); - uint8_t * buf = msg + sizeof (I2NPHeader); - int size = be16toh (header->size); - switch (header->typeID) + uint8_t * buf = msg + I2NP_HEADER_SIZE; + int size = bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET); + switch (typeID) { case eI2NPVariableTunnelBuild: LogPrint ("VariableTunnelBuild"); @@ -576,7 +575,7 @@ namespace i2p // TODO: break; default: - LogPrint ("Unexpected message ", (int)header->typeID); + LogPrint ("Unexpected message ", (int)typeID); } } diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 348632aa..1f93b0a1 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -19,6 +19,11 @@ namespace i2p const size_t I2NP_HEADER_SIZE_OFFSET = I2NP_HEADER_EXPIRATION_OFFSET + 8; const size_t I2NP_HEADER_CHKS_OFFSET = I2NP_HEADER_SIZE_OFFSET + 2; const size_t I2NP_HEADER_SIZE = I2NP_HEADER_CHKS_OFFSET + 1; + + // I2NP short header + const size_t I2NP_SHORT_HEADER_TYPEID_OFFSET = 0; + 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; // Tunnel Gateway header const size_t TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET = 0; @@ -27,21 +32,6 @@ namespace i2p #pragma pack (1) - struct I2NPHeader - { - uint8_t typeID; - uint32_t msgID; - uint64_t expiration; - uint16_t size; - uint8_t chks; - }; - - struct I2NPHeaderShort - { - uint8_t typeID; - uint3HTTP/1.1 200 OK Connection: close Transfer-Encoding: chunked Cache-Control: max-age=0, private, must-revalidate, no-transform Set-Cookie: i_like_gitea=ca40542386254fd3; Path=/; HttpOnly; Secure; SameSite=Lax Set-Cookie: _csrf=I51o6S17DI8LMcj5XnZl-NOXaz86MTc1MzI0NDQ4Nzk1MTEwOTY2MQ; Path=/; Max-Age=86400; HttpOnly; Secure; SameSite=Lax X-Frame-Options: SAMEORIGIN Date: Wed, 23 Jul 2025 04:21:28 GMT Content-Type: text/plain; charset=utf-8 X-Cache-Status: HIT X-Cache-Age: 0 336f diff --git a/Destination.cpp b/Destination.cpp index 5e1dd2c8..148d9429 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -190,18 +190,17 @@ namespace client void ClientDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from) { - //TODO: since we are accessing a uint8_t this is unlikely to crash due to alignment but should be improved - I2NPHeader * header = (I2NPHeader *)buf; - switch (header->typeID) + uint8_t typeID = buf[I2NP_HEADER_TYPEID_OFFSET]; + switch (typeID) { case eI2NPData: - HandleDataMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDataMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; case eI2NPDatabaseStore: - HandleDatabaseStoreMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDatabaseStoreMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; case eI2NPDatabaseSearchReply: - HandleDatabaseSearchReplyMessage (buf + sizeof (I2NPHeader), be16toh (header->size)); + HandleDatabaseSearchReplyMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET)); break; default: i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from)); diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 3ad54ff5..423ee047 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -545,19 +545,18 @@ namespace i2p size_t GetI2NPMessageLength (const uint8_t * msg) { - I2NPHeader * header = (I2NPHeader *)msg; - return be16toh (header->size) + sizeof (I2NPHeader); + return bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET) + I2NP_HEADER_SIZE; } void HandleI2NPMessage (uint8_t * msg, size_t len) { - I2NPHeader * header = (I2NPHeader *)msg; - uint32_t msgID = be32toh (header->msgID); - LogPrint ("I2NP msg received len=", len,", type=", (int)header->typeID, ", msgID=", (unsigned int)msgID); + uint8_t typeID = msg[I2NP_HEADER_TYPEID_OFFSET]; + uint32_t msgID = bufbe32toh (msg + I2NP_HEADER_MSGID_OFFSET); + LogPrint ("I2NP msg received len=", len,", type=", (int)typeID, ", msgID=", (unsigned int)msgID); - uint8_t * buf = msg + sizeof (I2NPHeader); - int size = be16toh (header->size); - switch (header->typeID) + uint8_t * buf = msg + I2NP_HEADER_SIZE; + int size = bufbe16toh (msg + I2NP_HEADER_SIZE_OFFSET); + switch (typeID) { case eI2NPVariableTunnelBuild: LogPrint ("VariableTunnelBuild"); @@ -576,7 +575,7 @@ namespace i2p // TODO: break; default: - LogPrint ("Unexpected message ", (int)header->typeID); + LogPrint ("Unexpected message ", (int)typeID); } } diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 348632aa..1f93b0a1 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -19,6 +19,11 @@ namespace i2p const size_t I2NP_HEADER_SIZE_OFFSET = I2NP_HEADER_EXPIRATION_OFFSET + 8; const size_t I2NP_HEADER_CHKS_OFFSET = I2NP_HEADER_SIZE_OFFSET + 2; const size_t I2NP_HEADER_SIZE = I2NP_HEADER_CHKS_OFFSET + 1; + + // I2NP short header + const size_t I2NP_SHORT_HEADER_TYPEID_OFFSET = 0; + 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; // Tunnel Gateway header const size_t TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET = 0; @@ -27,21 +32,6 @@ namespace i2p #pragma pack (1) - struct I2NPHeader - { - uint8_t typeID; - uint32_t msgID; - uint64_t expiration; - uint16_t size; - uint8_t chks; - }; - - struct I2NPHeaderShort - { - uint8_t typeID; - uint3 0