From 3092b7ca44603da608d924e20f8982253c8bafb1 Mon Sep 17 00:00:00 2001 From: arvidn Date: Wed, 5 Feb 2020 18:01:38 +0100 Subject: [PATCH] fix binding TCP and UDP sockets to the same port, when specifying port 0 --- ChangeLog | 1 + simulation/libsimulator | 2 +- simulation/test_session.cpp | 46 +++++++++++++++++++++++++++++++++++++ src/session_impl.cpp | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 8f7bcc7f4..d5712bb21 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 1.2.4 release + * fix binding TCP and UDP sockets to the same port, when specifying port 0 * fix announce_to_all_trackers and announce_to_all_tiers behavior * fix suggest_read_cache setting * back-off tracker hostname looksups resulting in NXDOMAIN diff --git a/simulation/libsimulator b/simulation/libsimulator index ba65f2fc2..3e4a9fc7a 160000 --- a/simulation/libsimulator +++ b/simulation/libsimulator @@ -1 +1 @@ -Subproject commit ba65f2fc25cb0cf6ab141102952b9c5c54f02c41 +Subproject commit 3e4a9fc7a5971104154d11a361139043029435c5 diff --git a/simulation/test_session.cpp b/simulation/test_session.cpp index fa7dfda21..d53c47b74 100644 --- a/simulation/test_session.cpp +++ b/simulation/test_session.cpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "simulator/utils.hpp" // for timer #include "settings.hpp" #include "create_torrent.hpp" +#include "setup_transfer.hpp" // for addr() using namespace lt; @@ -175,3 +176,48 @@ TORRENT_TEST(add_extension_while_transfer) TEST_CHECK(p->m_files_checked); } #endif // TORRENT_DISABLE_EXTENSIONS + +// make sure TCP and UDP listen sockets use the same port +TORRENT_TEST(tie_listen_ports) +{ + using namespace libtorrent; + + sim::default_config network_cfg; + sim::simulation sim{network_cfg}; + sim::asio::io_service ios { sim, addr("50.0.0.1")}; + + lt::session_proxy zombie; + + // create session + auto pack = settings(); + pack.set_str(settings_pack::listen_interfaces, "0.0.0.0:0"); + pack.set_int(settings_pack::alert_mask, alert::error_notification + | alert::status_notification + | alert::torrent_log_notification); + + auto ses = std::make_shared(pack, ios); + + std::vector listen_ports; + + // only monitor alerts for session 0 (the downloader) + print_alerts(*ses, [&](lt::session&, lt::alert const* a){ + if (auto const* la = alert_cast(a)) + { + listen_ports.push_back(la->port); + } + }); + + sim::timer t(sim, lt::seconds(30), [&](boost::system::error_code const&) + { + // TEST + zombie = ses->abort(); + ses.reset(); + + TEST_CHECK(listen_ports.size() > 0); + int const port = listen_ports.front(); + for (int const p : listen_ports) + TEST_EQUAL(p, port); + }); + + sim.run(); +} diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 35ed6dffa..0891fdb76 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1549,6 +1549,8 @@ namespace aux { TORRENT_ASSERT(ret->local_endpoint.port() == bind_ep.port() || bind_ep.port() == 0); + if (bind_ep.port() == 0) bind_ep = ret->local_endpoint; + ret->sock->listen(m_settings.get_int(settings_pack::listen_queue_size), ec); last_op = operation_t::sock_listen;