From 096ce54faea0126b4ce0028ce04c3baebebbd9fc Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 11 Nov 2017 13:39:43 +0100 Subject: [PATCH] fix issue where new listen sockets would not be opened when leaving force_proxy mode --- ChangeLog | 1 + simulation/test_session.cpp | 50 +++++++++++++++++++++++++++++++++++++ src/session_impl.cpp | 21 +++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 1e3af50e9..aefc6bc1f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ + * fix listen socket issue when disabling "force_proxy" mode * fix full allocation failure on APFS 1.1.5 release diff --git a/simulation/test_session.cpp b/simulation/test_session.cpp index 7c889687c..df5f36f80 100644 --- a/simulation/test_session.cpp +++ b/simulation/test_session.cpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "utils.hpp" #include "libtorrent/session.hpp" #include "libtorrent/socket.hpp" +#include "libtorrent/alert_types.hpp" #include "simulator/simulator.hpp" #include "simulator/utils.hpp" // for timer #include "settings.hpp" @@ -64,3 +65,52 @@ TORRENT_TEST(seed_mode) }); } +TORRENT_TEST(force_proxy) +{ + // setup the simulation + sim::default_config network_cfg; + sim::simulation sim{network_cfg}; + std::unique_ptr ios{new sim::asio::io_service(sim + , address_v4::from_string("50.0.0.1"))}; + lt::session_proxy zombie; + + lt::settings_pack pack = settings(); + pack.set_bool(settings_pack::force_proxy, true); + // create session + std::shared_ptr ses = std::make_shared(pack, *ios); + + // disable force proxy in 3 seconds (this should make us open up listen + // sockets) + sim::timer t1(sim, lt::seconds(3), [&](boost::system::error_code const& ec) + { + lt::settings_pack p; + p.set_bool(settings_pack::force_proxy, false); + ses->apply_settings(p); + }); + + int num_listen_tcp = 0; + int num_listen_udp = 0; + print_alerts(*ses, [&](lt::session& ses, lt::alert const* a) { + if (auto la = alert_cast(a)) + { + if (la->sock_type == listen_succeeded_alert::tcp) + ++num_listen_tcp; + else if (la->sock_type == listen_succeeded_alert::udp) + ++num_listen_udp; + } + }); + + // run for 10 seconds. + sim::timer t2(sim, lt::seconds(10), [&](boost::system::error_code const& ec) + { + fprintf(stderr, "shutting down\n"); + // shut down + zombie = ses->abort(); + ses.reset(); + }); + sim.run(); + + TEST_EQUAL(num_listen_tcp, 1); + TEST_EQUAL(num_listen_udp, 1); +} + diff --git a/src/session_impl.cpp b/src/session_impl.cpp index cea3ca98a..9534fa7ab 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1673,6 +1673,9 @@ namespace aux { (pack.has_val(settings_pack::ssl_listen) && pack.get_int(settings_pack::ssl_listen) != m_settings.get_int(settings_pack::ssl_listen)) + || (pack.has_val(settings_pack::force_proxy) + && !pack.get_bool(settings_pack::force_proxy) + && m_settings.get_bool(settings_pack::force_proxy)) || (pack.has_val(settings_pack::listen_interfaces) && pack.get_str(settings_pack::listen_interfaces) != m_settings.get_str(settings_pack::listen_interfaces)); @@ -1882,6 +1885,12 @@ retry: m_stats_counters.set_value(counters::has_incoming_connections, 0); ec.clear(); + if (m_settings.get_bool(settings_pack::force_proxy)) + { + // in force-proxy mode, we don't open any listen sockets + return; + } + if (m_abort) return; m_ipv6_interface = tcp::endpoint(); @@ -6493,7 +6502,17 @@ retry: m_ssl_udp_socket.set_force_proxy(m_settings.get_bool(settings_pack::force_proxy)); #endif - if (!m_settings.get_bool(settings_pack::force_proxy)) return; + if (!m_settings.get_bool(settings_pack::force_proxy)) + { +#ifndef TORRENT_DISABLE_LOGGING + session_log("force-proxy disabled"); +#endif + return; + } + +#ifndef TORRENT_DISABLE_LOGGING + session_log("force-proxy enabled"); +#endif // enable force_proxy mode. We don't want to accept any incoming // connections, except through a proxy.