From 2cbd6e85c664bc36532a921b009b5534acf7fc81 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 21 Jun 2015 22:29:50 -0400 Subject: [PATCH] use shared_ptr for garlic messages --- Datagram.cpp | 4 ++-- Destination.cpp | 10 +++++----- Garlic.cpp | 19 ++++++++----------- Garlic.h | 6 +++--- NetDb.cpp | 14 +++++++------- NetDbRequests.cpp | 2 +- Streaming.cpp | 8 ++++---- Streaming.h | 2 +- Tunnel.cpp | 6 +++--- Tunnel.h | 2 +- TunnelPool.cpp | 2 +- 11 files changed, 36 insertions(+), 39 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index 9179fb80..32499232 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -60,12 +60,12 @@ namespace datagram { std::vector msgs; uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1); - auto garlic = m_Owner.WrapMessage (remote, msg, true); + auto garlic = m_Owner.WrapMessage (remote, ToSharedI2NPMessage (msg), true); msgs.push_back (i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeTunnel, leases[i].tunnelGateway, leases[i].tunnelID, - ToSharedI2NPMessage (garlic) + garlic }); outboundTunnel->SendTunnelDataMsg (msgs); } diff --git a/Destination.cpp b/Destination.cpp index 107bcf0a..d98de3f7 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -393,7 +393,7 @@ namespace client m_ExcludedFloodfills.insert (floodfill->GetIdentHash ()); LogPrint (eLogDebug, "Publish LeaseSet of ", GetIdentHash ().ToBase32 ()); m_PublishReplyToken = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); - auto msg = WrapMessage (floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken)); + auto msg = WrapMessage (floodfill, ToSharedI2NPMessage (i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken))); m_PublishConfirmationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT)); m_PublishConfirmationTimer.async_wait (std::bind (&ClientDestination::HandlePublishConfirmationTimer, this, std::placeholders::_1)); @@ -581,15 +581,15 @@ namespace client rnd.GenerateBlock (replyTag, 32); // random session tag AddSessionKey (replyKey, replyTag); - I2NPMessage * msg = WrapMessage (nextFloodfill, - CreateLeaseSetDatabaseLookupMsg (dest, request->excluded, - replyTunnel.get (), replyKey, replyTag)); + auto msg = WrapMessage (nextFloodfill, + ToSharedI2NPMessage (CreateLeaseSetDatabaseLookupMsg (dest, request->excluded, + replyTunnel.get (), replyKey, replyTag))); outboundTunnel->SendTunnelDataMsg ( { i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeRouter, - nextFloodfill->GetIdentHash (), 0, ToSharedI2NPMessage (msg) + nextFloodfill->GetIdentHash (), 0, msg } }); request->requestTimeoutTimer.expires_from_now (boost::posix_time::seconds(LEASESET_REQUEST_TIMEOUT)); diff --git a/Garlic.cpp b/Garlic.cpp index 8bdfb10c..4b89b147 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -107,9 +107,9 @@ namespace garlic return !m_SessionTags.empty () || m_UnconfirmedTagsMsgs.empty (); } - I2NPMessage * GarlicRoutingSession::WrapSingleMessage (I2NPMessage * msg) + std::shared_ptr GarlicRoutingSession::WrapSingleMessage (std::shared_ptr msg) { - I2NPMessage * m = NewI2NPMessage (); + auto m = ToSharedI2NPMessage(NewI2NPMessage ()); m->Align (12); // in order to get buf aligned to 16 (12 + 4) size_t len = 0; uint8_t * buf = m->GetPayload () + 4; // 4 bytes for length @@ -164,12 +164,10 @@ namespace garlic len += 32; } // AES block - len += CreateAESBlock (buf, msg); + len += CreateAESBlock (buf, msg.get ()); // TODO htobe32buf (m->GetPayload (), len); m->len += len + 4; - FillI2NPMessageHeader (m, eI2NPGarlic); - if (msg) - DeleteI2NPMessage (msg); + FillI2NPMessageHeader (m.get (), eI2NPGarlic); // TODO return m; } @@ -309,7 +307,7 @@ namespace garlic htobe32buf (buf + size, inboundTunnel->GetNextTunnelID ()); // tunnelID size += 4; // create msg - I2NPMessage * msg = CreateDeliveryStatusMsg (msgID); + auto msg = ToSharedI2NPMessage (CreateDeliveryStatusMsg (msgID)); if (m_Owner) { //encrypt @@ -322,7 +320,6 @@ namespace garlic } memcpy (buf + size, msg->GetBuffer (), msg->GetLength ()); size += msg->GetLength (); - DeleteI2NPMessage (msg); // fill clove uint64_t ts = i2p::util::GetMillisecondsSinceEpoch () + 5000; // 5 sec htobe32buf (buf + size, m_Rnd.GenerateWord32 ()); // CloveID @@ -512,7 +509,7 @@ namespace garlic if (tunnel) // we have send it through an outbound tunnel { I2NPMessage * msg = CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from); - tunnel->SendTunnelDataMsg (gwHash, gwTunnel, msg); + tunnel->SendTunnelDataMsg (gwHash, gwTunnel, ToSharedI2NPMessage (msg)); } else LogPrint ("No outbound tunnels available for garlic clove"); @@ -537,8 +534,8 @@ namespace garlic } } - I2NPMessage * GarlicDestination::WrapMessage (std::shared_ptr destination, - I2NPMessage * msg, bool attachLeaseSet) + std::shared_ptr GarlicDestination::WrapMessage (std::shared_ptr destination, + std::shared_ptr msg, bool attachLeaseSet) { auto session = GetRoutingSession (destination, attachLeaseSet); // 32 tags by default return session->WrapSingleMessage (msg); diff --git a/Garlic.h b/Garlic.h index d5330735..580cabec 100644 --- a/Garlic.h +++ b/Garlic.h @@ -80,7 +80,7 @@ namespace garlic int numTags, bool attachLeaseSet); GarlicRoutingSession (const uint8_t * sessionKey, const SessionTag& sessionTag); // one time encryption ~GarlicRoutingSession (); - I2NPMessage * WrapSingleMessage (I2NPMessage * msg); + std::shared_ptr WrapSingleMessage (std::shared_ptr msg); void MessageConfirmed (uint32_t msgID); bool CleanupExpiredTags (); // returns true if something left @@ -126,8 +126,8 @@ namespace garlic std::shared_ptr GetRoutingSession (std::shared_ptr destination, bool attachLeaseSet); void CleanupRoutingSessions (); void RemoveCreatedSession (uint32_t msgID); - I2NPMessage * WrapMessage (std::shared_ptr destination, - I2NPMessage * msg, bool attachLeaseSet = false); + std::shared_ptr WrapMessage (std::shared_ptr destination, + std::shared_ptr msg, bool attachLeaseSet = false); void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread diff --git a/NetDb.cpp b/NetDb.cpp index b802f3dd..90dcfc96 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -477,7 +477,7 @@ namespace data auto pool = i2p::tunnel::tunnels.GetExploratoryPool (); auto outbound = pool ? pool->GetNextOutboundTunnel () : nullptr; if (outbound) - outbound->SendTunnelDataMsg (buf + offset, tunnelID, deliveryStatus); + outbound->SendTunnelDataMsg (buf + offset, tunnelID, ToSharedI2NPMessage (deliveryStatus)); else { LogPrint (eLogError, "No outbound tunnels for DatabaseStore reply found"); @@ -665,7 +665,7 @@ namespace data numExcluded = 0; // TODO: } - I2NPMessage * replyMsg = nullptr; + std::shared_ptr replyMsg; if (lookupType == DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP) { LogPrint ("Exploratory close to ", key, " ", numExcluded, " excluded"); @@ -685,7 +685,7 @@ namespace data excludedRouters.insert (r->GetIdentHash ()); } } - replyMsg = CreateDatabaseSearchReply (ident, routers); + replyMsg = ToSharedI2NPMessage (CreateDatabaseSearchReply (ident, routers)); } else { @@ -698,7 +698,7 @@ namespace data LogPrint ("Requested RouterInfo ", key, " found"); router->LoadBuffer (); if (router->GetBuffer ()) - replyMsg = CreateDatabaseStoreMsg (router); + replyMsg = ToSharedI2NPMessage (CreateDatabaseStoreMsg (router)); } } @@ -709,7 +709,7 @@ namespace data if (leaseSet) // we don't send back our LeaseSets { LogPrint ("Requested LeaseSet ", key, " found"); - replyMsg = CreateDatabaseStoreMsg (leaseSet); + replyMsg = ToSharedI2NPMessage (CreateDatabaseStoreMsg (leaseSet)); } } @@ -722,7 +722,7 @@ namespace data excludedRouters.insert (excluded); excluded += 32; } - replyMsg = CreateDatabaseSearchReply (ident, GetClosestFloodfills (ident, 3, excludedRouters)); + replyMsg = ToSharedI2NPMessage (CreateDatabaseSearchReply (ident, GetClosestFloodfills (ident, 3, excludedRouters))); } } @@ -747,7 +747,7 @@ namespace data if (outbound) outbound->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg); else - transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, ToSharedI2NPMessage(replyMsg))); + transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg)); } else transports.SendMessage (buf+32, replyMsg); diff --git a/NetDbRequests.cpp b/NetDbRequests.cpp index 3509bcc7..670070fd 100644 --- a/NetDbRequests.cpp +++ b/NetDbRequests.cpp @@ -118,7 +118,7 @@ namespace data auto nextFloodfill = netdb.GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ()); if (nextFloodfill && outbound && inbound) outbound->SendTunnelDataMsg (nextFloodfill->GetIdentHash (), 0, - dest->CreateRequestMessage (nextFloodfill, inbound)); + ToSharedI2NPMessage (dest->CreateRequestMessage (nextFloodfill, inbound))); else { done = true; diff --git a/Streaming.cpp b/Streaming.cpp index e20efffb..bd6cb4bb 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -617,7 +617,7 @@ namespace stream { i2p::tunnel::eDeliveryTypeTunnel, m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, - ToSharedI2NPMessage (msg) + msg }); m_NumSentBytes += it->GetLength (); } @@ -761,9 +761,9 @@ namespace stream m_CurrentRemoteLease.endDate = 0; } - I2NPMessage * Stream::CreateDataMessage (const uint8_t * payload, size_t len) + std::shared_ptr Stream::CreateDataMessage (const uint8_t * payload, size_t len) { - I2NPMessage * msg = NewI2NPShortMessage (); + auto msg = ToSharedI2NPMessage (NewI2NPShortMessage ()); CryptoPP::Gzip compressor; if (len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE) compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL); @@ -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, eI2NPData); + FillI2NPMessageHeader (msg.get (), eI2NPData); // TODO: return msg; } diff --git a/Streaming.h b/Streaming.h index b3cd4b30..7e2aaa71 100644 --- a/Streaming.h +++ b/Streaming.h @@ -156,7 +156,7 @@ namespace stream void HandleResendTimer (const boost::system::error_code& ecode); void HandleAckSendTimer (const boost::system::error_code& ecode); - I2NPMessage * CreateDataMessage (const uint8_t * payload, size_t len); + std::shared_ptr CreateDataMessage (const uint8_t * payload, size_t len); private: diff --git a/Tunnel.cpp b/Tunnel.cpp index de95ad68..8b0d6cfd 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -81,7 +81,7 @@ namespace tunnel // send message if (outboundTunnel) - outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg); + outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, ToSharedI2NPMessage (msg)); else i2p::transport::transports.SendMessage (GetNextIdentHash (), ToSharedI2NPMessage (msg)); } @@ -164,7 +164,7 @@ namespace tunnel m_Endpoint.HandleDecryptedTunnelDataMsg (msg); } - void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) + void OutboundTunnel::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr msg) { TunnelMessageBlock block; if (gwHash) @@ -180,7 +180,7 @@ namespace tunnel } else block.deliveryType = eDeliveryTypeLocal; - block.data = ToSharedI2NPMessage (msg); + block.data = msg; std::unique_lock l(m_SendMutex); m_Gateway.SendTunnelDataMsg (block); diff --git a/Tunnel.h b/Tunnel.h index f8c5e9cc..4ce44240 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -83,7 +83,7 @@ namespace tunnel OutboundTunnel (std::shared_ptr config): Tunnel (config), m_Gateway (this) {}; - void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg); + void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, std::shared_ptr msg); void SendTunnelDataMsg (const std::vector& msgs); // multiple messages std::shared_ptr GetEndpointRouter () const { return GetTunnelConfig ()->GetLastHop ()->router; }; diff --git a/TunnelPool.cpp b/TunnelPool.cpp index 0f0eb709..689d31e8 100644 --- a/TunnelPool.cpp +++ b/TunnelPool.cpp @@ -254,7 +254,7 @@ namespace tunnel uint32_t msgID = rnd.GenerateWord32 (); m_Tests[msgID] = std::make_pair (*it1, *it2); (*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (), - CreateDeliveryStatusMsg (msgID)); + ToSharedI2NPMessage (CreateDeliveryStatusMsg (msgID))); it1++; it2++; } }