Skip to content

Commit

Permalink
Add announce_port support
Browse files Browse the repository at this point in the history
  • Loading branch information
0xThiebaut committed Oct 25, 2024
1 parent 24e658a commit 4cb3880
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
10 changes: 10 additions & 0 deletions include/libtorrent/settings_pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
3 changes: 2 additions & 1 deletion src/http_tracker_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ namespace libtorrent {
if (!(tracker_req().kind & tracker_request::scrape_request))
{
static aux::array<const char*, 4> 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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/settings_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/tracker_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,15 @@ constexpr tracker_request_flags_t tracker_request::i2p;

#ifndef TORRENT_DISABLE_LOGGING
std::shared_ptr<request_callback> 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(':'));
Expand Down
4 changes: 3 additions & 1 deletion src/udp_tracker_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4cb3880

Please sign in to comment.