Skip to content

Commit 038bbe7

Browse files
committed
daemon: remove UPnP support
Keep the "-upnp" option as a hidden arg for one major version in order to show a more user friendly error to people who had this option set in their config file.
1 parent 844770b commit 038bbe7

File tree

5 files changed

+13
-110
lines changed

5 files changed

+13
-110
lines changed

src/init.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,8 @@ void SetupServerArgs(ArgsManager& argsman, bool can_listen_ipc)
561561
argsman.AddArg("-peertimeout=<n>", strprintf("Specify a p2p connection timeout delay in seconds. After connecting to a peer, wait this amount of time before considering disconnection based on inactivity (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CONNECTION);
562562
argsman.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control host and port to use if onion listening enabled (default: %s). If no port is specified, the default port of %i will be used.", DEFAULT_TOR_CONTROL, DEFAULT_TOR_CONTROL_PORT), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
563563
argsman.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", ArgsManager::ALLOW_ANY | ArgsManager::SENSITIVE, OptionsCategory::CONNECTION);
564-
#ifdef USE_UPNP
565-
argsman.AddArg("-upnp", strprintf("Use UPnP to map the listening port (default: %u)", DEFAULT_UPNP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
566-
#else
567-
hidden_args.emplace_back("-upnp");
568-
#endif
564+
// UPnP support was dropped. We keep `-upnp` as a hidden arg to display a more user friendly error when set. TODO: remove (here and below) for 30.0.
565+
argsman.AddArg("-upnp", "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
569566
argsman.AddArg("-natpmp", strprintf("Use PCP or NAT-PMP to map the listening port (default: %u)", DEFAULT_NATPMP), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
570567
argsman.AddArg("-whitebind=<[permissions@]addr>", "Bind to the given address and add permission flags to the peers connecting to it. "
571568
"Use [host]:port notation for IPv6. Allowed permissions: " + Join(NET_PERMISSIONS_DOC, ", ") + ". "
@@ -740,8 +737,6 @@ void InitParameterInteraction(ArgsManager& args)
740737
LogInfo("parameter interaction: -proxy set -> setting -listen=0\n");
741738
// to protect privacy, do not map ports when a proxy is set. The user may still specify -listen=1
742739
// to listen locally, so don't rely on this happening through -listen below.
743-
if (args.SoftSetBoolArg("-upnp", false))
744-
LogInfo("parameter interaction: -proxy set -> setting -upnp=0\n");
745740
if (args.SoftSetBoolArg("-natpmp", false)) {
746741
LogInfo("parameter interaction: -proxy set -> setting -natpmp=0\n");
747742
}
@@ -752,8 +747,6 @@ void InitParameterInteraction(ArgsManager& args)
752747

753748
if (!args.GetBoolArg("-listen", DEFAULT_LISTEN)) {
754749
// do not map ports or try to retrieve public IP when not listening (pointless)
755-
if (args.SoftSetBoolArg("-upnp", false))
756-
LogInfo("parameter interaction: -listen=0 -> setting -upnp=0\n");
757750
if (args.SoftSetBoolArg("-natpmp", false)) {
758751
LogInfo("parameter interaction: -listen=0 -> setting -natpmp=0\n");
759752
}
@@ -877,6 +870,12 @@ bool AppInitParameterInteraction(const ArgsManager& args)
877870

878871
// also see: InitParameterInteraction()
879872

873+
// We drop UPnP support but kept the arg as hidden for now to display a friendlier error to user who have the
874+
// option in their config. TODO: remove (here and above) for version 30.0.
875+
if (args.IsArgSet("-upnp")) {
876+
return InitError(_("UPnP support was dropped in version 29.0. Consider using '-natpmp' instead."));
877+
}
878+
880879
// Error if network-specific options (-addnode, -connect, etc) are
881880
// specified in default section of config file, but not overridden
882881
// on the command line or in this chain's section of the config file.
@@ -1827,8 +1826,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18271826
LogPrintf("nBestHeight = %d\n", chain_active_height);
18281827
if (node.peerman) node.peerman->SetBestBlock(chain_active_height, std::chrono::seconds{best_block_time});
18291828

1830-
// Map ports with UPnP or NAT-PMP
1831-
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
1829+
// Map ports with NAT-PMP
1830+
StartMapPort(false, args.GetBoolArg("-natpmp", DEFAULT_NATPMP));
18321831

18331832
CConnman::Options connOptions;
18341833
connOptions.m_local_services = g_local_services;

src/mapport.cpp

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <bitcoin-build-config.h> // IWYU pragma: keep
6-
75
#include <mapport.h>
86

97
#include <clientversion.h>
@@ -18,15 +16,6 @@
1816
#include <util/thread.h>
1917
#include <util/threadinterrupt.h>
2018

21-
#ifdef USE_UPNP
22-
#include <miniupnpc/miniupnpc.h>
23-
#include <miniupnpc/upnpcommands.h>
24-
#include <miniupnpc/upnperrors.h>
25-
// The minimum supported miniUPnPc API version is set to 17. This excludes
26-
// versions with known vulnerabilities.
27-
static_assert(MINIUPNPC_API_VERSION >= 17, "miniUPnPc API version >= 17 assumed");
28-
#endif // USE_UPNP
29-
3019
#include <atomic>
3120
#include <cassert>
3221
#include <chrono>
@@ -134,78 +123,6 @@ static bool ProcessPCP()
134123
return ret;
135124
}
136125

137-
#ifdef USE_UPNP
138-
static bool ProcessUpnp()
139-
{
140-
bool ret = false;
141-
std::string port = strprintf("%u", GetListenPort());
142-
const char * multicastif = nullptr;
143-
const char * minissdpdpath = nullptr;
144-
struct UPNPDev * devlist = nullptr;
145-
char lanaddr[64];
146-
147-
int error = 0;
148-
devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
149-
150-
struct UPNPUrls urls;
151-
struct IGDdatas data;
152-
int r;
153-
#if MINIUPNPC_API_VERSION <= 17
154-
r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
155-
#else
156-
r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr), nullptr, 0);
157-
#endif
158-
if (r == 1)
159-
{
160-
if (fDiscover) {
161-
char externalIPAddress[40];
162-
r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
163-
if (r != UPNPCOMMAND_SUCCESS) {
164-
LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
165-
} else {
166-
if (externalIPAddress[0]) {
167-
std::optional<CNetAddr> resolved{LookupHost(externalIPAddress, false)};
168-
if (resolved.has_value()) {
169-
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved->ToStringAddr());
170-
AddLocal(resolved.value(), LOCAL_MAPPED);
171-
}
172-
} else {
173-
LogPrintf("UPnP: GetExternalIPAddress failed.\n");
174-
}
175-
}
176-
}
177-
178-
std::string strDesc = PACKAGE_NAME " " + FormatFullVersion();
179-
180-
do {
181-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", nullptr, "0");
182-
183-
if (r != UPNPCOMMAND_SUCCESS) {
184-
ret = false;
185-
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n", port, port, lanaddr, r, strupnperror(r));
186-
break;
187-
} else {
188-
ret = true;
189-
LogPrintf("UPnP Port Mapping successful.\n");
190-
}
191-
} while (g_mapport_interrupt.sleep_for(PORT_MAPPING_REANNOUNCE_PERIOD));
192-
g_mapport_interrupt.reset();
193-
194-
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", nullptr);
195-
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
196-
freeUPNPDevlist(devlist); devlist = nullptr;
197-
FreeUPNPUrls(&urls);
198-
} else {
199-
LogPrintf("No valid UPnP IGDs found\n");
200-
freeUPNPDevlist(devlist); devlist = nullptr;
201-
if (r != 0)
202-
FreeUPNPUrls(&urls);
203-
}
204-
205-
return ret;
206-
}
207-
#endif // USE_UPNP
208-
209126
static void ThreadMapPort()
210127
{
211128
bool ok;
@@ -219,15 +136,6 @@ static void ThreadMapPort()
219136
if (ok) continue;
220137
}
221138

222-
#ifdef USE_UPNP
223-
// Low priority protocol.
224-
if (g_mapport_enabled_protos & MapPortProtoFlag::UPNP) {
225-
g_mapport_current_proto = MapPortProtoFlag::UPNP;
226-
ok = ProcessUpnp();
227-
if (ok) continue;
228-
}
229-
#endif // USE_UPNP
230-
231139
g_mapport_current_proto = MapPortProtoFlag::NONE;
232140
if (g_mapport_enabled_protos == MapPortProtoFlag::NONE) {
233141
return;
@@ -268,7 +176,7 @@ static void DispatchMapPort()
268176

269177
assert(g_mapport_thread.joinable());
270178
assert(!g_mapport_interrupt);
271-
// Interrupt a protocol-specific loop in the ThreadUpnp() or in the ThreadPCP()
179+
// Interrupt a protocol-specific loop in the ThreadPCP()
272180
// to force trying the next protocol in the ThreadMapPort() loop.
273181
g_mapport_interrupt();
274182
}
@@ -284,7 +192,6 @@ static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
284192

285193
void StartMapPort(bool use_upnp, bool use_pcp)
286194
{
287-
MapPortProtoSetEnabled(MapPortProtoFlag::UPNP, use_upnp);
288195
MapPortProtoSetEnabled(MapPortProtoFlag::PCP, use_pcp);
289196
DispatchMapPort();
290197
}

src/mapport.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
#ifndef BITCOIN_MAPPORT_H
66
#define BITCOIN_MAPPORT_H
77

8-
static constexpr bool DEFAULT_UPNP = false;
9-
108
static constexpr bool DEFAULT_NATPMP = false;
119

1210
enum MapPortProtoFlag : unsigned int {
1311
NONE = 0x00,
14-
UPNP = 0x01,
12+
// 0x01 was for UPnP, for which we dropped support.
1513
PCP = 0x02, // PCP with NAT-PMP fallback.
1614
};
1715

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ enum
148148
LOCAL_NONE, // unknown
149149
LOCAL_IF, // address a local interface listens on
150150
LOCAL_BIND, // address explicit bound to
151-
LOCAL_MAPPED, // address reported by UPnP or PCP
151+
LOCAL_MAPPED, // address reported by PCP
152152
LOCAL_MANUAL, // address explicitly specified (-externalip=)
153153

154154
LOCAL_MAX

test/functional/test_framework/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect=
433433
# in tests.
434434
f.write("peertimeout=999999999\n")
435435
f.write("printtoconsole=0\n")
436-
f.write("upnp=0\n")
437436
f.write("natpmp=0\n")
438437
f.write("shrinkdebugfile=0\n")
439438
f.write("deprecatedrpc=create_bdb\n") # Required to run the tests

0 commit comments

Comments
 (0)