From 7b5e18d94b5bcf5b1483aa84159d4592262a9b48 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 21 Aug 2016 21:17:09 -0400 Subject: [PATCH] changes --- ClientContext.cpp | 2 +- Destination.cpp | 17 +++++------------ Destination.h | 6 +++--- I2PTunnel.cpp | 32 ++++++++++++++------------------ I2PTunnel.h | 4 ++-- 5 files changed, 25 insertions(+), 36 deletions(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index 3bc6a2d3..c031aae6 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -466,7 +466,7 @@ namespace client std::make_pair( localDestination->GetIdentHash(), port), std::unique_ptr(serverTunnel))).second) { - LogPrint(eLogInfo, "Cleints: I2P Server Forward created for UDP Endpoint ", host, ":", port, " via ",localDestination->GetIdentHash().ToBase32()); + LogPrint(eLogInfo, "Clients: I2P Server Forward created for UDP Endpoint ", host, ":", port, " via ",localDestination->GetIdentHash().ToBase32()); } else { LogPrint(eLogError, "Clients: I2P Server Forward for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash()), "/", port, "already exists"); } diff --git a/Destination.cpp b/Destination.cpp index 6f26a84c..d48bee92 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -677,8 +677,6 @@ namespace client ClientDestination::~ClientDestination () { - if (m_DatagramDestination) - delete m_DatagramDestination; } bool ClientDestination::Start () @@ -703,13 +701,8 @@ namespace client m_StreamingDestination = nullptr; for (auto& it: m_StreamingDestinationsByPorts) it.second->Stop (); - if (m_DatagramDestination) - { - auto d = m_DatagramDestination; - m_DatagramDestination = nullptr; - delete d; - } - return true; + m_DatagramDestination = nullptr; + return true; } else return false; @@ -819,10 +812,10 @@ namespace client return dest; } - i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination () + std::shared_ptr ClientDestination::CreateDatagramDestination () { - if (!m_DatagramDestination) - m_DatagramDestination = new i2p::datagram::DatagramDestination (GetSharedFromThis ()); + if (m_DatagramDestination == nullptr) + m_DatagramDestination = std::make_shared (GetSharedFromThis ()); return m_DatagramDestination; } diff --git a/Destination.h b/Destination.h index 75c94efc..6cd4c19c 100644 --- a/Destination.h +++ b/Destination.h @@ -164,8 +164,8 @@ namespace client bool IsAcceptingStreams () const; // datagram - i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; }; - i2p::datagram::DatagramDestination * CreateDatagramDestination (); + std::shared_ptr GetDatagramDestination () const { return m_DatagramDestination; }; + std::shared_ptr CreateDatagramDestination (); // implements LocalDestination const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; @@ -190,7 +190,7 @@ namespace client std::shared_ptr m_StreamingDestination; // default std::map > m_StreamingDestinationsByPorts; - i2p::datagram::DatagramDestination * m_DatagramDestination; + std::shared_ptr m_DatagramDestination; public: diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index d5b1a25b..068cbe37 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -539,14 +539,14 @@ namespace client } /** create new */ boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0); - m_Sessions.push_back(UDPSession(m_Service, ep, m_Destination, m_Endpoint, ih, localPort, remotePort)); + m_Sessions.push_back(UDPSession(m_Service, ep, m_LocalDest, m_Endpoint, ih, localPort, remotePort)); auto & s = m_Sessions.back(); s.SendEndpoint = s.IPSocket.local_endpoint(); return s; } UDPSession::UDPSession(boost::asio::io_service & ios, boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr localDestination, boost::asio::ip::udp::endpoint endpoint, const i2p::data::IdentHash to, uint16_t ourPort, uint16_t theirPort) : - Destination(localDestination), + m_Destination(localDestination), IPSocket(ios, localEndpoint), Identity(to), SendEndpoint(endpoint), @@ -569,17 +569,12 @@ namespace client LogPrint(eLogDebug, "UDPSesssion: HandleRecveived"); if(!ecode) { LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint); - if (Destination) { - auto dgram = Destination->CreateDatagramDestination(); - if(dgram) { - LastActivity = i2p::util::GetMillisecondsSinceEpoch(); - dgram->SendDatagramTo(m_Buffer, len, Identity, 0, 0); - LogPrint(eLogDebug, "UDPSession: forward ", len, "B to ", Identity.ToBase32(), " from ", Destination->GetIdentHash().ToBase32()); - } else { - LogPrint(eLogWarning, "UDPSession: no datagram destination"); - } + auto dgram = m_Destination->GetDatagramDestination(); + if(dgram) { + LastActivity = i2p::util::GetMillisecondsSinceEpoch(); + dgram->SendDatagramTo(m_Buffer, len, Identity, 0, 0); } else { - LogPrint(eLogWarning, "UDPSession: no Local Destination"); + LogPrint(eLogWarning, "UDPSession: no datagram destination"); } Receive(); } else { @@ -590,20 +585,21 @@ namespace client I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr localDestination, boost::asio::ip::udp::endpoint forwardTo, uint16_t port, boost::asio::io_service & service) : LocalPort(port), m_Endpoint(forwardTo), - m_Service(service), - m_Destination(localDestination) + m_Service(service) { - i2p::datagram::DatagramDestination * dgram = m_Destination->CreateDatagramDestination(); - if(dgram) - dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), 0); + m_LocalDest = localDestination; + m_LocalDest->Start(); + auto dgram = m_LocalDest->CreateDatagramDestination(); + dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), 0); } I2PUDPServerTunnel::~I2PUDPServerTunnel() { - i2p::datagram::DatagramDestination * dgram = m_Destination->GetDatagramDestination(); + auto dgram = m_LocalDest->GetDatagramDestination(); if (dgram) { dgram->ResetReceiver(0); } + LogPrint(eLogInfo, "UDPServer: done"); } I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr localDestination, uint16_t remotePort, boost::asio::io_service & service) : diff --git a/I2PTunnel.h b/I2PTunnel.h index e50c47f7..54ea09e9 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -140,7 +140,7 @@ namespace client struct UDPSession { - std::shared_ptr Destination; + std::shared_ptr m_Destination; boost::asio::ip::udp::socket IPSocket; i2p::data::IdentHash Identity; boost::asio::ip::udp::endpoint FromEndpoint; @@ -177,7 +177,7 @@ namespace client std::mutex m_SessionsMutex; std::vector m_Sessions; boost::asio::io_service & m_Service; - std::shared_ptr m_Destination; + std::shared_ptr m_LocalDest; uint8_t m_Buffer[I2P_UDP_MAX_MTU]; };