Skip to content

Commit

Permalink
upnpctrl: don't open mappings by default
Browse files Browse the repository at this point in the history
The UPnPContext class currently has hardcoded limits on the number of
mappings that need to be "available" at any given time. This patch adds
the ability to modify these limits and sets them to zero in the upnpctrl
tool. This changes the behavior of the tool in two ways:
1) setting the minimum limit to zero means that no mappings are opened
   when upnpctrl starts;
2) setting the maximum limit to zero means that the "close" command will
   always cause the relevant mapping to be closed immediately instead of
   (potentially) being put back in the list of available mappings.

Change-Id: Idcf4262ce80a8b39f19ce7d3a201b40b3ea56911
  • Loading branch information
François-Simon Fauteux-Chapleau committed Dec 4, 2024
1 parent 9b4b585 commit 6642a62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 13 additions & 2 deletions include/upnp/upnp_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ class UPnPContext : public UpnpMappingObserver
// then the state of all pending mappings is set to FAILED.
void setIgdDiscoveryTimeout(std::chrono::milliseconds timeout);

// Set limits on the number of "available" mappings that the UPnPContext
// can keep at any given time. An available mapping is one that has been
// opened, but isn't being used by any Controller. The context will attempt
// to close or open mappings as needed to keep the number of available
// mappings between `minCount` and `maxCount`.
void setAvailableMappingsLimits(PortType type, unsigned minCount, unsigned maxCount) {
unsigned index = (type == PortType::TCP) ? 0 : 1;
maxAvailableMappings_[index] = maxCount;
minAvailableMappings_[index] = (minCount <= maxCount) ? minCount : 0;
}

private:
// Initialization
void init();
Expand Down Expand Up @@ -286,8 +297,8 @@ class UPnPContext : public UpnpMappingObserver

// Minimum and maximum limits on the number of available
// mappings to keep in the list at any given time
static constexpr unsigned minAvailableMappings_[2] {4, 8};
static constexpr unsigned maxAvailableMappings_[2] {8, 12};
unsigned minAvailableMappings_[2] {4, 8};
unsigned maxAvailableMappings_[2] {8, 12};
unsigned getMinAvailableMappings(PortType type) {
unsigned index = (type == PortType::TCP) ? 0 : 1;
return minAvailableMappings_[index];
Expand Down
2 changes: 2 additions & 0 deletions tools/upnp/upnpctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ main(int argc, char** argv)
auto ioContext = std::make_shared<asio::io_context>();
std::shared_ptr<dht::log::Logger> logger = dht::log::getStdLogger();
auto upnpContext = std::make_shared<dhtnet::upnp::UPnPContext>(ioContext, logger);
upnpContext->setAvailableMappingsLimits(dhtnet::upnp::PortType::TCP, 0, 0);
upnpContext->setAvailableMappingsLimits(dhtnet::upnp::PortType::UDP, 0, 0);

auto ioContextRunner = std::make_shared<std::thread>([context = ioContext]() {
try {
Expand Down

0 comments on commit 6642a62

Please sign in to comment.