Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for overriding the NAT-PMP gateway with a nat_pmp_gateway config option #7890

Open
wants to merge 1 commit into
base: RC_2_0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/libtorrent/natpmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct TORRENT_EXTRA_EXPORT natpmp final
{
natpmp(io_context& ios, aux::portmap_callback& cb, aux::listen_socket_handle ls);

void start(ip_interface const& ip);
void start(ip_interface const& ip, boost::optional<address> const& gateway);

// maps the ports, if a port is set to 0
// it will not be mapped
Expand Down
5 changes: 5 additions & 0 deletions include/libtorrent/settings_pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ namespace aux {
// ``router.bt.ouinet.work:6881``,
dht_bootstrap_nodes,

// Overrides the NAT-PMP service gateway. When set, libtorrent won't try
// to resolve the default gateway and instead will send the requests to
// the address specified.
nat_pmp_gateway,

max_string_setting_internal
};

Expand Down
30 changes: 17 additions & 13 deletions src/natpmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,35 @@ natpmp::natpmp(io_context& ios
m_mappings.reserve(10);
}

void natpmp::start(ip_interface const& ip)
void natpmp::start(ip_interface const& ip, boost::optional<address> const& gateway)
{
TORRENT_ASSERT(is_single_thread());

// assume servers support PCP and fall back to NAT-PMP
// if necessary
m_version = version_pcp;

error_code ec;
address const& local_address = ip.interface_address;

error_code ec;
auto const routes = enum_routes(m_ioc, ec);
if (ec)
boost::optional<address> route = gateway;
if (!route)
{
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())

auto const routes = enum_routes(m_ioc, ec);
if (ec)
{
log("failed to enumerate routes: %s"
, convert_from_native(ec.message()).c_str());
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
{
log("failed to enumerate routes: %s"
, convert_from_native(ec.message()).c_str());
}
#endif
disable(ec);
}
#endif
disable(ec);
}

auto const route = get_gateway(ip, routes);
route = get_gateway(ip, routes);
}

if (!route)
{
Expand Down
6 changes: 5 additions & 1 deletion src/session_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5676,7 +5676,11 @@ namespace {
ip.netmask = s->netmask;
std::strncpy(ip.name, s->device.c_str(), sizeof(ip.name) - 1);
ip.name[sizeof(ip.name) - 1] = '\0';
s->natpmp_mapper->start(ip);

error_code ignored;
std::string gateway = m_settings.get_str(settings_pack::nat_pmp_gateway);
boost::optional<address> gateway_addr = make_address(gateway, ignored);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make sense to move the call to make_address() into the condition, to only make it in case the setting is set

s->natpmp_mapper->start(ip, gateway == "" ? boost::none : gateway_addr);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/settings_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ constexpr int DISK_WRITE_MODE = settings_pack::enable_os_cache;
SET(proxy_password, "", &session_impl::update_proxy),
SET(i2p_hostname, "", &session_impl::update_i2p_bridge),
SET(peer_fingerprint, "-LT20B0-", nullptr),
SET(dht_bootstrap_nodes, "dht.libtorrent.org:25401", &session_impl::update_dht_bootstrap_nodes)
SET(dht_bootstrap_nodes, "dht.libtorrent.org:25401", &session_impl::update_dht_bootstrap_nodes),
SET(nat_pmp_gateway, "", &session_impl::update_upnp)
}});

CONSTEXPR_SETTINGS
Expand Down
Loading