From bf14b7da9a92e96622ffda33ce5973863fe907ce Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 3 Jul 2015 11:11:07 -0400 Subject: [PATCH] move FillI2NPMessageHeader into I2NPMessage --- Datagram.cpp | 2 +- Garlic.cpp | 2 +- I2NPProtocol.cpp | 49 ++++++++++++++++++++++------------------------- I2NPProtocol.h | 5 +++-- NetDb.cpp | 2 +- Streaming.cpp | 2 +- TransitTunnel.cpp | 2 +- Tunnel.cpp | 2 +- TunnelGateway.cpp | 2 +- 9 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index 32499232..63b487ff 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -143,7 +143,7 @@ namespace datagram htobe16buf (buf + 6, toPort); // destination port buf[9] = i2p::client::PROTOCOL_TYPE_DATAGRAM; // datagram protocol msg->len += size + 4; - FillI2NPMessageHeader (msg, eI2NPData); + msg->FillI2NPMessageHeader (eI2NPData); return msg; } } diff --git a/Garlic.cpp b/Garlic.cpp index 00a943e2..fb67b0ae 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -167,7 +167,7 @@ namespace garlic len += CreateAESBlock (buf, msg.get ()); // TODO htobe32buf (m->GetPayload (), len); m->len += len + 4; - FillI2NPMessageHeader (m.get (), eI2NPGarlic); // TODO + m->FillI2NPMessageHeader (eI2NPGarlic); return m; } diff --git a/I2NPProtocol.cpp b/I2NPProtocol.cpp index 0c9d82da..f98bdc1c 100644 --- a/I2NPProtocol.cpp +++ b/I2NPProtocol.cpp @@ -41,25 +41,22 @@ namespace i2p return std::shared_ptr(msg, DeleteI2NPMessage); } - void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID) + void I2NPMessage::FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID) { - msg->SetTypeID (msgType); + SetTypeID (msgType); if (replyMsgID) // for tunnel creation - msg->SetMsgID (replyMsgID); + SetMsgID (replyMsgID); else - msg->SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ()); - msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number - msg->UpdateSize (); - msg->UpdateChks (); + SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ()); + SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); // TODO: 5 secs is a magic number + UpdateSize (); + UpdateChks (); } - void RenewI2NPMessageHeader (I2NPMessage * msg) + void I2NPMessage::RenewI2NPMessageHeader () { - if (msg) - { - msg->SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ()); - msg->SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); - } + SetMsgID (i2p::context.GetRandomNumberGenerator ().GenerateWord32 ()); + SetExpiration (i2p::util::GetMillisecondsSinceEpoch () + 5000); } I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID) @@ -72,7 +69,7 @@ namespace i2p } else LogPrint (eLogError, "I2NP message length ", len, " exceeds max length"); - FillI2NPMessageHeader (msg, msgType, replyMsgID); + msg->FillI2NPMessageHeader (msgType, replyMsgID); return msg; } @@ -105,7 +102,7 @@ namespace i2p htobe64buf (buf + DELIVERY_STATUS_TIMESTAMP_OFFSET, 2); // netID = 2 } m->len += DELIVERY_STATUS_SIZE; - FillI2NPMessageHeader (m, eI2NPDeliveryStatus); + m->FillI2NPMessageHeader (eI2NPDeliveryStatus); return ToSharedI2NPMessage (m); } @@ -150,7 +147,7 @@ namespace i2p } m->len += (buf - m->GetPayload ()); - FillI2NPMessageHeader (m, eI2NPDatabaseLookup); + m->FillI2NPMessageHeader (eI2NPDatabaseLookup); return m; } @@ -187,7 +184,7 @@ namespace i2p buf += 65; m->len += (buf - m->GetPayload ()); - FillI2NPMessageHeader (m, eI2NPDatabaseLookup); + m->FillI2NPMessageHeader (eI2NPDatabaseLookup); return m; } @@ -211,7 +208,7 @@ namespace i2p memcpy (buf + len, i2p::context.GetRouterInfo ().GetIdentHash (), 32); len += 32; m->len += len; - FillI2NPMessageHeader (m, eI2NPDatabaseSearchReply); + m->FillI2NPMessageHeader (eI2NPDatabaseSearchReply); return m; } @@ -245,7 +242,7 @@ namespace i2p compressor.Get (buf, size); buf += size; m->len += (buf - payload); // payload size - FillI2NPMessageHeader (m, eI2NPDatabaseStore); + m->FillI2NPMessageHeader (eI2NPDatabaseStore); return m; } @@ -275,7 +272,7 @@ namespace i2p memcpy (payload + size, leaseSet->GetBuffer (), leaseSet->GetBufferLen ()); size += leaseSet->GetBufferLen (); m->len += size; - FillI2NPMessageHeader (m, eI2NPDatabaseStore); + m->FillI2NPMessageHeader (eI2NPDatabaseStore); return m; } @@ -419,7 +416,7 @@ namespace i2p I2NPMessage * msg = NewI2NPShortMessage (); memcpy (msg->GetPayload (), buf, i2p::tunnel::TUNNEL_DATA_MSG_SIZE); msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE; - FillI2NPMessageHeader (msg, eI2NPTunnelData); + msg->FillI2NPMessageHeader (eI2NPTunnelData); return msg; } @@ -429,7 +426,7 @@ namespace i2p memcpy (msg->GetPayload () + 4, payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4); htobe32buf (msg->GetPayload (), tunnelID); msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE; - FillI2NPMessageHeader (msg, eI2NPTunnelData); + msg->FillI2NPMessageHeader (eI2NPTunnelData); return msg; } @@ -448,7 +445,7 @@ namespace i2p htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len); memcpy (payload + TUNNEL_GATEWAY_HEADER_SIZE, buf, len); msg->len += TUNNEL_GATEWAY_HEADER_SIZE + len; - FillI2NPMessageHeader (msg, eI2NPTunnelGateway); + msg->FillI2NPMessageHeader (eI2NPTunnelGateway); return msg; } @@ -463,7 +460,7 @@ namespace i2p htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len); msg->offset -= (I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE); msg->len = msg->offset + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE +len; - FillI2NPMessageHeader (msg.get(), eI2NPTunnelGateway); // TODO + msg->FillI2NPMessageHeader (eI2NPTunnelGateway); return msg; } else @@ -482,13 +479,13 @@ namespace i2p msg->len += gatewayMsgOffset; memcpy (msg->GetPayload (), buf, len); msg->len += len; - FillI2NPMessageHeader (msg, msgType, replyMsgID); // create content message + msg->FillI2NPMessageHeader (msgType, replyMsgID); // create content message len = msg->GetLength (); msg->offset -= gatewayMsgOffset; uint8_t * payload = msg->GetPayload (); htobe32buf (payload + TUNNEL_GATEWAY_HEADER_TUNNELID_OFFSET, tunnelID); htobe16buf (payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET, len); - FillI2NPMessageHeader (msg, eI2NPTunnelGateway); // gateway message + msg->FillI2NPMessageHeader (eI2NPTunnelGateway); // gateway message return msg; } diff --git a/I2NPProtocol.h b/I2NPProtocol.h index 98e5f1c5..0e6f5621 100644 --- a/I2NPProtocol.h +++ b/I2NPProtocol.h @@ -184,6 +184,9 @@ namespace tunnel len = offset + I2NP_SHORT_HEADER_SIZE + bufbe16toh (header + I2NP_HEADER_SIZE_OFFSET); return bufbe32toh (header + I2NP_HEADER_MSGID_OFFSET); } + + void FillI2NPMessageHeader (I2NPMessageType msgType, uint32_t replyMsgID = 0); + void RenewI2NPMessageHeader (); }; template @@ -199,8 +202,6 @@ namespace tunnel void DeleteI2NPMessage (I2NPMessage * msg); std::shared_ptr ToSharedI2NPMessage (I2NPMessage * msg); - void FillI2NPMessageHeader (I2NPMessage * msg, I2NPMessageType msgType, uint32_t replyMsgID = 0); - void RenewI2NPMessageHeader (I2NPMessage * msg); I2NPMessage * CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, int len, uint32_t replyMsgID = 0); std::shared_ptr CreateI2NPMessage (const uint8_t * buf, int len, std::shared_ptr from = nullptr); diff --git a/NetDb.cpp b/NetDb.cpp index db163002..dad122a2 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -492,7 +492,7 @@ namespace data htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0); // zero reply token memcpy (payload + DATABASE_STORE_HEADER_SIZE, buf + offset, len - offset); floodMsg->len += DATABASE_STORE_HEADER_SIZE + len -offset; - FillI2NPMessageHeader (floodMsg.get (), eI2NPDatabaseStore); // TODO + floodMsg->FillI2NPMessageHeader (eI2NPDatabaseStore); std::set excluded; for (int i = 0; i < 3; i++) { diff --git a/Streaming.cpp b/Streaming.cpp index 41d39a63..785e2987 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -780,7 +780,7 @@ namespace stream htobe16buf (buf + 6, m_Port); // destination port buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol msg->len += size + 4; - FillI2NPMessageHeader (msg.get (), eI2NPData); // TODO: + msg->FillI2NPMessageHeader (eI2NPData); return msg; } diff --git a/TransitTunnel.cpp b/TransitTunnel.cpp index 7c58dabb..ad6aa0b3 100644 --- a/TransitTunnel.cpp +++ b/TransitTunnel.cpp @@ -36,7 +36,7 @@ namespace tunnel m_NumTransmittedBytes += tunnelMsg->GetLength (); htobe32buf (newMsg->GetPayload (), GetNextTunnelID ()); - FillI2NPMessageHeader (newMsg.get (), eI2NPTunnelData); // TODO + newMsg->FillI2NPMessageHeader (eI2NPTunnelData); m_TunnelDataMsgs.push_back (newMsg); } diff --git a/Tunnel.cpp b/Tunnel.cpp index b773cbfc..0c5600e7 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -77,7 +77,7 @@ namespace tunnel } hop = hop->prev; } - FillI2NPMessageHeader (msg, eI2NPVariableTunnelBuild); + msg->FillI2NPMessageHeader (eI2NPVariableTunnelBuild); // send message if (outboundTunnel) diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index ffbb8fe3..b05a342c 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -201,7 +201,7 @@ namespace tunnel for (auto tunnelMsg : tunnelMsgs) { m_Tunnel->EncryptTunnelMsg (tunnelMsg, tunnelMsg); - FillI2NPMessageHeader (tunnelMsg.get (), eI2NPTunnelData); // TODO: + tunnelMsg->FillI2NPMessageHeader (eI2NPTunnelData); m_NumSentBytes += TUNNEL_DATA_MSG_SIZE; } i2p::transport::transports.SendMessages (m_Tunnel->GetNextIdentHash (), tunnelMsgs);