From 987ad214ffc99a90fc995d7f168525f3ba3a1681 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 2 Aug 2017 21:00:04 -0400 Subject: [PATCH] avoid bind exeptions during reload --- libi2pd_client/ClientContext.cpp | 20 +++++++++++++------- libi2pd_client/I2PService.cpp | 11 ++++++++--- libi2pd_client/I2PService.h | 9 +++++---- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index 5c9048e3..4eea297e 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -48,7 +48,8 @@ namespace client std::shared_ptr localDestination; bool httproxy; i2p::config::GetOption("httpproxy.enabled", httproxy); - if (httproxy) { + if (httproxy) + { std::string httpProxyKeys; i2p::config::GetOption("httpproxy.keys", httpProxyKeys); std::string httpProxyAddr; i2p::config::GetOption("httpproxy.address", httpProxyAddr); uint16_t httpProxyPort; i2p::config::GetOption("httpproxy.port", httpProxyPort); @@ -62,14 +63,18 @@ namespace client std::map params; ReadI2CPOptionsFromConfig ("httpproxy.", params); localDestination = CreateNewLocalDestination (keys, false, ¶ms); + localDestination->Acquire (); } else LogPrint(eLogError, "Clients: failed to load HTTP Proxy key"); } - try { + try + { m_HttpProxy = new i2p::proxy::HTTPProxy(httpProxyAddr, httpProxyPort, localDestination); m_HttpProxy->Start(); - } catch (std::exception& e) { + } + catch (std::exception& e) + { LogPrint(eLogError, "Clients: Exception in HTTP Proxy: ", e.what()); } } @@ -94,6 +99,7 @@ namespace client std::map params; ReadI2CPOptionsFromConfig ("socksproxy.", params); localDestination = CreateNewLocalDestination (keys, false, ¶ms); + localDestination->Acquire (); } else LogPrint(eLogError, "Clients: failed to load SOCKS Proxy key"); @@ -263,7 +269,7 @@ namespace client std::unique_lock l(m_DestinationsMutex); for (auto it = m_Destinations.begin (); it != m_Destinations.end ();) { - auto dest = it->second; + auto dest = it->second; if (dest->GetRefCounter () > 0) ++it; // skip else { @@ -525,13 +531,13 @@ namespace client { // socks proxy clientTunnel = new i2p::proxy::SOCKSProxy(address, port, false, "", destinationPort, localDestination); - clientEndpoint = ((i2p::proxy::SOCKSProxy*)clientTunnel)->GetAcceptor().local_endpoint(); + clientEndpoint = ((i2p::proxy::SOCKSProxy*)clientTunnel)->GetLocalEndpoint (); } else if (type == I2P_TUNNELS_SECTION_TYPE_HTTPPROXY) { // http proxy clientTunnel = new i2p::proxy::HTTPProxy(address, port, localDestination); - clientEndpoint = ((i2p::proxy::HTTPProxy*)clientTunnel)->GetAcceptor().local_endpoint(); + clientEndpoint = ((i2p::proxy::HTTPProxy*)clientTunnel)->GetLocalEndpoint (); } else if (type == I2P_TUNNELS_SECTION_TYPE_WEBSOCKS) { @@ -543,7 +549,7 @@ namespace client { // tcp client clientTunnel = new I2PClientTunnel (name, dest, address, port, localDestination, destinationPort); - clientEndpoint = ((I2PClientTunnel*)clientTunnel)->GetAcceptor().local_endpoint(); + clientEndpoint = ((I2PClientTunnel*)clientTunnel)->GetLocalEndpoint (); } auto ins = m_ClientTunnels.insert (std::make_pair (clientEndpoint, std::unique_ptr(clientTunnel))); if (ins.second) diff --git a/libi2pd_client/I2PService.cpp b/libi2pd_client/I2PService.cpp index 446500a0..e3b85cb8 100644 --- a/libi2pd_client/I2PService.cpp +++ b/libi2pd_client/I2PService.cpp @@ -182,13 +182,18 @@ namespace client void TCPIPAcceptor::Start () { - m_Acceptor.listen (); + m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint)); + m_Acceptor->listen (); Accept (); } void TCPIPAcceptor::Stop () { - m_Acceptor.close(); + if (m_Acceptor) + { + m_Acceptor->close(); + m_Acceptor.reset (nullptr); + } m_Timer.cancel (); ClearHandlers(); } @@ -196,7 +201,7 @@ namespace client void TCPIPAcceptor::Accept () { auto newSocket = std::make_shared (GetService ()); - m_Acceptor.async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this, + m_Acceptor->async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this, std::placeholders::_1, newSocket)); } diff --git a/libi2pd_client/I2PService.h b/libi2pd_client/I2PService.h index dd0a3c4a..e0465d56 100644 --- a/libi2pd_client/I2PService.h +++ b/libi2pd_client/I2PService.h @@ -111,11 +111,11 @@ namespace client public: TCPIPAcceptor (const std::string& address, int port, std::shared_ptr localDestination = nullptr) : I2PService(localDestination), - m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)), + m_LocalEndpoint (boost::asio::ip::address::from_string(address), port), m_Timer (GetService ()) {} TCPIPAcceptor (const std::string& address, int port, i2p::data::SigningKeyType kt) : I2PService(kt), - m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)), + m_LocalEndpoint (boost::asio::ip::address::from_string(address), port), m_Timer (GetService ()) {} virtual ~TCPIPAcceptor () { TCPIPAcceptor::Stop(); } //If you override this make sure you call it from the children @@ -123,7 +123,7 @@ namespace client //If you override this make sure you call it from the children void Stop (); - const boost::asio::ip::tcp::acceptor& GetAcceptor () const { return m_Acceptor; }; + const boost::asio::ip::tcp::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; }; virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; } @@ -132,7 +132,8 @@ namespace client private: void Accept(); void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr socket); - boost::asio::ip::tcp::acceptor m_Acceptor; + boost::asio::ip::tcp::endpoint m_LocalEndpoint; + std::unique_ptr m_Acceptor; boost::asio::deadline_timer m_Timer; }; }