From 4cb3880b64b70cdbc39bd950b70ec396894108b1 Mon Sep 17 00:00:00 2001 From: Maxime THIEBAUT <46688461+0xThiebaut@users.noreply.github.com> Date: Fri, 25 Oct 2024 23:06:18 +0200 Subject: [PATCH] Add announce_port support --- include/libtorrent/settings_pack.hpp | 10 ++++++++++ src/http_tracker_connection.cpp | 3 ++- src/settings_pack.cpp | 3 ++- src/tracker_manager.cpp | 11 +++++++++-- src/udp_tracker_connection.cpp | 4 +++- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index 0446f2b60d6..b3edea16b6b 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -2068,6 +2068,16 @@ namespace aux { i2p_inbound_length, i2p_outbound_length, + // ``announce_port`` is the port passed along to trackers as the + // ``&port=`` parameter. If left as the default, the listening port + // is used. + // + // .. note:: + // This setting is only meant for very special cases + // where a seed's listening port differs from the exposed + // port (e.g., through external NAT-PMP). + announce_port, + max_int_setting_internal }; diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index d7b66c57dc2..c2528e8a4b7 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -126,6 +126,7 @@ namespace libtorrent { if (!(tracker_req().kind & tracker_request::scrape_request)) { static aux::array const event_string{{{"completed", "started", "stopped", "paused"}}}; + const auto announce_port = std::uint16_t(settings.get_int(settings_pack::announce_port)); char str[1024]; std::snprintf(str, sizeof(str) @@ -143,7 +144,7 @@ namespace libtorrent { , escape_string({tracker_req().pid.data(), 20}).c_str() // the i2p tracker seems to verify that the port is not 0, // even though it ignores it otherwise - , tracker_req().listen_port + , announce_port ? announce_port : tracker_req().listen_port , tracker_req().uploaded , tracker_req().downloaded , tracker_req().left diff --git a/src/settings_pack.cpp b/src/settings_pack.cpp index 5a2c20f7405..72d0a886fd9 100644 --- a/src/settings_pack.cpp +++ b/src/settings_pack.cpp @@ -401,7 +401,8 @@ constexpr int DISK_WRITE_MODE = settings_pack::enable_os_cache; SET(i2p_inbound_quantity, 3, nullptr), SET(i2p_outbound_quantity, 3, nullptr), SET(i2p_inbound_length, 3, nullptr), - SET(i2p_outbound_length, 3, nullptr) + SET(i2p_outbound_length, 3, nullptr), + SET(announce_port, 0, nullptr) }}); #undef SET diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index 7c135da21b9..397cce4353b 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -278,8 +278,15 @@ constexpr tracker_request_flags_t tracker_request::i2p; #ifndef TORRENT_DISABLE_LOGGING std::shared_ptr cb = c.lock(); - if (cb) cb->debug_log("*** QUEUE_TRACKER_REQUEST [ listen_port: %d ]" - , req.listen_port); + if (cb) { + if (const auto announce_port = std::uint16_t(sett.get_int(settings_pack::announce_port))) { + cb->debug_log("*** QUEUE_TRACKER_REQUEST [ listen_port: %d, announce_port: %d ]" + , req.listen_port, announce_port); + } else { + cb->debug_log("*** QUEUE_TRACKER_REQUEST [ listen_port: %d ]" + , req.listen_port); + } + } #endif std::string const protocol = req.url.substr(0, req.url.find(':')); diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 498b38d2047..43c29f40ecd 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -729,7 +729,9 @@ namespace libtorrent { aux::write_uint32(announce_ip.to_uint(), out); aux::write_int32(req.key, out); // key aux::write_int32(req.num_want, out); // num_want - aux::write_uint16(req.listen_port, out); // port + + const auto announce_port = std::uint16_t(settings.get_int(settings_pack::announce_port)); + aux::write_uint16(announce_port ? announce_port : req.listen_port, out); // port std::string request_string; error_code ec;