diff --git a/AddressBook.cpp b/AddressBook.cpp index a77b7194..609b9aa7 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -435,7 +435,7 @@ namespace client { std::condition_variable newDataReceived; std::mutex newDataReceivedMutex; - const i2p::data::LeaseSet * leaseSet = i2p::data::netdb.FindLeaseSet (ident); + auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (ident); if (!leaseSet) { bool found = false; @@ -462,7 +462,7 @@ namespace client if (m_LastModified.length () > 0) // if-modfief-since request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n"; request << "\r\n"; // end of header - auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (*leaseSet, u.port_); + auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, u.port_); stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ()); uint8_t buf[4095]; diff --git a/BOB.cpp b/BOB.cpp index f925979f..3b31569d 100644 --- a/BOB.cpp +++ b/BOB.cpp @@ -132,7 +132,7 @@ namespace client delete receiver; } - void BOBI2PInboundTunnel::CreateConnection (AddressReceiver * receiver, const i2p::data::LeaseSet * leaseSet) + void BOBI2PInboundTunnel::CreateConnection (AddressReceiver * receiver, std::shared_ptr leaseSet) { LogPrint ("New BOB inbound connection"); auto connection = std::make_shared(this, receiver->socket, leaseSet); diff --git a/BOB.h b/BOB.h index bb75e40d..5858cdb8 100644 --- a/BOB.h +++ b/BOB.h @@ -84,7 +84,7 @@ namespace client void HandleDestinationRequestTimer (const boost::system::error_code& ecode, AddressReceiver * receiver, i2p::data::IdentHash ident); - void CreateConnection (AddressReceiver * receiver, const i2p::data::LeaseSet * leaseSet); + void CreateConnection (AddressReceiver * receiver, std::shared_ptr leaseSet); private: diff --git a/Destination.cpp b/Destination.cpp index 23567817..8d5d59bb 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -57,8 +57,6 @@ namespace client Stop (); for (auto it: m_LeaseSetRequests) delete it.second; - for (auto it: m_RemoteLeaseSets) - delete it.second; if (m_Pool) i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool); if (m_StreamingDestination) @@ -126,7 +124,7 @@ namespace client } } - const i2p::data::LeaseSet * ClientDestination::FindLeaseSet (const i2p::data::IdentHash& ident) + std::shared_ptr ClientDestination::FindLeaseSet (const i2p::data::IdentHash& ident) { auto it = m_RemoteLeaseSets.find (ident); if (it != m_RemoteLeaseSets.end ()) @@ -141,7 +139,7 @@ namespace client auto ls = i2p::data::netdb.FindLeaseSet (ident); if (ls) { - ls = new i2p::data::LeaseSet (*ls); + ls = std::make_shared (*ls); m_RemoteLeaseSets[ident] = ls; return ls; } @@ -232,7 +230,7 @@ namespace client else { LogPrint (eLogDebug, "New remote LeaseSet added"); - m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = new i2p::data::LeaseSet (buf + offset, len - offset); + m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = std::make_shared (buf + offset, len - offset); } } else @@ -391,9 +389,9 @@ namespace client void ClientDestination::CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port) { assert(streamRequestComplete); - const i2p::data::LeaseSet * leaseSet = FindLeaseSet (dest); + auto leaseSet = FindLeaseSet (dest); if (leaseSet) - streamRequestComplete(CreateStream (*leaseSet, port)); + streamRequestComplete(CreateStream (leaseSet, port)); else { RequestDestination (dest, @@ -403,9 +401,9 @@ namespace client streamRequestComplete (nullptr); else { - const i2p::data::LeaseSet * leaseSet = FindLeaseSet (dest); + auto leaseSet = FindLeaseSet (dest); if (leaseSet) - streamRequestComplete(CreateStream (*leaseSet, port)); + streamRequestComplete(CreateStream (leaseSet, port)); else streamRequestComplete (nullptr); } @@ -413,7 +411,7 @@ namespace client } } - std::shared_ptr ClientDestination::CreateStream (const i2p::data::LeaseSet& remote, int port) + std::shared_ptr ClientDestination::CreateStream (std::shared_ptr remote, int port) { if (m_StreamingDestination) return m_StreamingDestination->CreateNewOutgoingStream (remote, port); diff --git a/Destination.h b/Destination.h index 4104c426..1e49e932 100644 --- a/Destination.h +++ b/Destination.h @@ -64,13 +64,13 @@ namespace client boost::asio::io_service& GetService () { return m_Service; }; std::shared_ptr GetTunnelPool () { return m_Pool; }; bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); }; - const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident); + std::shared_ptr FindLeaseSet (const i2p::data::IdentHash& ident); bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr); // streaming i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; }; void CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port = 0); - std::shared_ptr CreateStream (const i2p::data::LeaseSet& remote, int port = 0); + std::shared_ptr CreateStream (std::shared_ptr remote, int port = 0); void AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor); void StopAcceptingStreams (); bool IsAcceptingStreams () const; @@ -120,7 +120,7 @@ namespace client boost::asio::io_service::work m_Work; i2p::data::PrivateKeys m_Keys; uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; - std::map m_RemoteLeaseSets; + std::map > m_RemoteLeaseSets; std::map m_LeaseSetRequests; std::shared_ptr m_Pool; diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 042a03c4..f55396a6 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -901,10 +901,10 @@ namespace util } } - void HTTPConnection::SendToDestination (const i2p::data::LeaseSet * remote, int port, const char * buf, size_t len) + void HTTPConnection::SendToDestination (std::shared_ptr remote, int port, const char * buf, size_t len) { if (!m_Stream) - m_Stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (*remote, port); + m_Stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (remote, port); if (m_Stream) { m_Stream->Send ((uint8_t *)buf, len); diff --git a/HTTPServer.h b/HTTPServer.h index 51bbc98b..658a9f7f 100644 --- a/HTTPServer.h +++ b/HTTPServer.h @@ -93,7 +93,7 @@ namespace util void SendToAddress (const std::string& address, int port, const char * buf, size_t len); void HandleDestinationRequestTimeout (const boost::system::error_code& ecode, i2p::data::IdentHash destination, int port, const char * buf, size_t len); - void SendToDestination (const i2p::data::LeaseSet * remote, int port, const char * buf, size_t len); + void SendToDestination (std::shared_ptr remote, int port, const char * buf, size_t len); public: diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index bfd7cfce..8177b18e 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -10,11 +10,11 @@ namespace i2p namespace client { I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, - boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet): + boost::asio::ip::tcp::socket * socket, std::shared_ptr leaseSet): I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true) { - m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (*leaseSet); + m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (leaseSet); } I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, diff --git a/I2PTunnel.h b/I2PTunnel.h index 010f4843..51de3b73 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -25,7 +25,7 @@ namespace client public: I2PTunnelConnection (I2PService * owner, boost::asio::ip::tcp::socket * socket, - const i2p::data::LeaseSet * leaseSet); // to I2P + std::shared_ptr leaseSet); // to I2P I2PTunnelConnection (I2PService * owner, boost::asio::ip::tcp::socket * socket, std::shared_ptr stream); // to I2P using simplified API :) I2PTunnelConnection (I2PService * owner, std::shared_ptr stream, boost::asio::ip::tcp::socket * socket, diff --git a/NetDb.cpp b/NetDb.cpp index 2c2ae884..3783bcbd 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -116,8 +116,6 @@ namespace data delete m_Thread; m_Thread = 0; } - for (auto l: m_LeaseSets) - delete l.second; m_LeaseSets.clear(); for (auto r: m_RequestedDestinations) delete r.second; @@ -254,7 +252,7 @@ namespace data else { LogPrint ("New LeaseSet added"); - m_LeaseSets[ident] = new LeaseSet (buf, len); + m_LeaseSets[ident] = std::make_shared (buf, len); } } } @@ -269,7 +267,7 @@ namespace data return nullptr; } - LeaseSet * NetDb::FindLeaseSet (const IdentHash& destination) const + std::shared_ptr NetDb::FindLeaseSet (const IdentHash& destination) const { auto it = m_LeaseSets.find (destination); if (it != m_LeaseSets.end ()) @@ -641,7 +639,7 @@ namespace data if (leaseSet) // we don't send back our LeaseSets { LogPrint ("Requested LeaseSet ", key, " found"); - replyMsg = CreateDatabaseStoreMsg (leaseSet); + replyMsg = CreateDatabaseStoreMsg (leaseSet.get ()); } } if (!replyMsg) @@ -862,7 +860,6 @@ namespace data if (it->second->HasNonExpiredLeases ()) // all leases expired { LogPrint ("LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired"); - delete it->second; it = m_LeaseSets.erase (it); } else diff --git a/NetDb.h b/NetDb.h index afcae70d..f2e20661 100644 --- a/NetDb.h +++ b/NetDb.h @@ -68,7 +68,7 @@ namespace data void AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len); void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from); std::shared_ptr FindRouter (const IdentHash& ident) const; - LeaseSet * FindLeaseSet (const IdentHash& destination) const; + std::shared_ptr FindLeaseSet (const IdentHash& destination) const; void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr); @@ -110,7 +110,7 @@ namespace data private: - std::map m_LeaseSets; + std::map > m_LeaseSets; mutable std::mutex m_RouterInfosMutex; std::map > m_RouterInfos; mutable std::mutex m_FloodfillsMutex; diff --git a/SAM.cpp b/SAM.cpp index d8386fcc..b85fc4ee 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -322,7 +322,7 @@ namespace client context.GetAddressBook ().InsertAddress (dest); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); if (leaseSet) - Connect (*leaseSet); + Connect (leaseSet); else { m_Session->localDestination->RequestDestination (dest.GetIdentHash (), @@ -334,7 +334,7 @@ namespace client SendMessageReply (SAM_STREAM_STATUS_INVALID_ID, strlen(SAM_STREAM_STATUS_INVALID_ID), true); } - void SAMSocket::Connect (const i2p::data::LeaseSet& remote) + void SAMSocket::Connect (std::shared_ptr remote) { m_SocketType = eSAMSocketTypeStream; m_Session->sockets.push_back (shared_from_this ()); @@ -346,11 +346,11 @@ namespace client void SAMSocket::HandleConnectLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident) { - const i2p::data::LeaseSet * leaseSet = nullptr; + std::shared_ptr leaseSet; if (success) // timeout expired leaseSet = m_Session->localDestination->FindLeaseSet (ident); if (leaseSet) - Connect (*leaseSet); + Connect (leaseSet); else { LogPrint ("SAM destination to connect not found"); @@ -418,7 +418,7 @@ namespace client else if (m_Session && m_Session->localDestination && context.GetAddressBook ().GetIdentHash (name, ident)) { - auto leaseSet = i2p::data::netdb.FindLeaseSet (ident); + auto leaseSet = m_Session->localDestination->FindLeaseSet (ident); if (leaseSet) SendNamingLookupReply (leaseSet->GetIdentity ()); else @@ -440,7 +440,7 @@ namespace client void SAMSocket::HandleNamingLookupLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident) { - const i2p::data::LeaseSet * leaseSet = nullptr; + std::shared_ptr leaseSet; if (success) leaseSet = m_Session->localDestination->FindLeaseSet (ident); if (leaseSet) diff --git a/SAM.h b/SAM.h index e6de9102..0660f35c 100644 --- a/SAM.h +++ b/SAM.h @@ -104,7 +104,7 @@ namespace client void ProcessNamingLookup (char * buf, size_t len); void ExtractParams (char * buf, size_t len, std::map& params); - void Connect (const i2p::data::LeaseSet& remote); + void Connect (std::shared_ptr remote); void HandleConnectLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident); void SendNamingLookupReply (const i2p::data::IdentityEx& identity); void HandleNamingLookupLeaseSetRequestComplete (bool success, i2p::data::IdentHash ident); diff --git a/Streaming.cpp b/Streaming.cpp index 680444b6..74807cae 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -12,10 +12,10 @@ namespace i2p namespace stream { Stream::Stream (boost::asio::io_service& service, StreamingDestination& local, - const i2p::data::LeaseSet& remote, int port): m_Service (service), m_SendStreamID (0), + std::shared_ptr remote, int port): m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), - m_RemoteLeaseSet (&remote), m_CurrentOutboundTunnel (nullptr), + m_RemoteLeaseSet (remote), m_CurrentOutboundTunnel (nullptr), m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (port) { @@ -26,8 +26,8 @@ namespace stream Stream::Stream (boost::asio::io_service& service, StreamingDestination& local): m_Service (service), m_SendStreamID (0), m_SequenceNumber (0), m_LastReceivedSequenceNumber (-1), m_IsOpen (false), m_IsReset (false), m_IsAckSendScheduled (false), m_LocalDestination (local), - m_RemoteLeaseSet (nullptr), m_CurrentOutboundTunnel (nullptr), - m_ReceiveTimer (m_Service), m_ResendTimer (m_Service), m_AckSendTimer (m_Service), + m_CurrentOutboundTunnel (nullptr), m_ReceiveTimer (m_Service), + m_ResendTimer (m_Service), m_AckSendTimer (m_Service), m_NumSentBytes (0), m_NumReceivedBytes (0), m_Port (0) { m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); @@ -692,7 +692,7 @@ namespace stream } } - std::shared_ptr StreamingDestination::CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port) + std::shared_ptr StreamingDestination::CreateNewOutgoingStream (std::shared_ptr remote, int port) { auto s = std::make_shared (m_Owner.GetService (), *this, remote, port); std::unique_lock l(m_StreamsMutex); diff --git a/Streaming.h b/Streaming.h index 83514966..76c0f73d 100644 --- a/Streaming.h +++ b/Streaming.h @@ -86,13 +86,13 @@ namespace stream public: Stream (boost::asio::io_service& service, StreamingDestination& local, - const i2p::data::LeaseSet& remote, int port = 0); // outgoing + std::shared_ptr remote, int port = 0); // outgoing Stream (boost::asio::io_service& service, StreamingDestination& local); // incoming ~Stream (); uint32_t GetSendStreamID () const { return m_SendStreamID; }; uint32_t GetRecvStreamID () const { return m_RecvStreamID; }; - const i2p::data::LeaseSet * GetRemoteLeaseSet () const { return m_RemoteLeaseSet; }; + std::shared_ptr GetRemoteLeaseSet () const { return m_RemoteLeaseSet; }; const i2p::data::IdentityEx& GetRemoteIdentity () const { return m_RemoteIdentity; }; bool IsOpen () const { return m_IsOpen; }; bool IsEstablished () const { return m_SendStreamID; }; @@ -144,7 +144,7 @@ namespace stream bool m_IsOpen, m_IsReset, m_IsAckSendScheduled; StreamingDestination& m_LocalDestination; i2p::data::IdentityEx m_RemoteIdentity; - const i2p::data::LeaseSet * m_RemoteLeaseSet; + std::shared_ptr m_RemoteLeaseSet; std::shared_ptr m_RoutingSession; i2p::data::Lease m_CurrentRemoteLease; i2p::tunnel::OutboundTunnel * m_CurrentOutboundTunnel; @@ -171,7 +171,7 @@ namespace stream void Start (); void Stop (); - std::shared_ptr CreateNewOutgoingStream (const i2p::data::LeaseSet& remote, int port = 0); + std::shared_ptr CreateNewOutgoingStream (std::shared_ptr remote, int port = 0); void DeleteStream (std::shared_ptr stream); void SetAcceptor (const Acceptor& acceptor) { m_Acceptor = acceptor; }; void ResetAcceptor () { m_Acceptor = nullptr; }; diff --git a/api.cpp b/api.cpp index 3cddf4f5..0fb4256f 100644 --- a/api.cpp +++ b/api.cpp @@ -85,7 +85,7 @@ namespace api auto leaseSet = dest->FindLeaseSet (remote); if (leaseSet) { - auto stream = dest->CreateStream (*leaseSet); + auto stream = dest->CreateStream (leaseSet); stream->Send (nullptr, 0); // connect return stream; }