Skip to content

Commit a39f733

Browse files
committed
net: Add -natpmp command line option
1 parent 28acffd commit a39f733

File tree

9 files changed

+49
-14
lines changed

9 files changed

+49
-14
lines changed

src/init.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ void SetupServerArgs(NodeContext& node)
469469
#else
470470
hidden_args.emplace_back("-upnp");
471471
#endif
472+
#ifdef USE_NATPMP
473+
argsman.AddArg("-natpmp", strprintf("Use NAT-PMP to map the listening port (default: %s)", DEFAULT_NATPMP ? "1 when listening and no -proxy" : "0"), ArgsManager::ALLOW_BOOL, OptionsCategory::CONNECTION);
474+
#else
475+
hidden_args.emplace_back("-natpmp");
476+
#endif // USE_NATPMP
472477
argsman.AddArg("-whitebind=<[permissions@]addr>", "Bind to the given address and add permission flags to the peers connecting to it. "
473478
"Use [host]:port notation for IPv6. Allowed permissions: " + Join(NET_PERMISSIONS_DOC, ", ") + ". "
474479
"Specify multiple permissions separated by commas (default: download,noban,mempool,relay). Can be specified multiple times.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -813,10 +818,13 @@ void InitParameterInteraction(ArgsManager& args)
813818
// to protect privacy, do not listen by default if a default proxy server is specified
814819
if (args.SoftSetBoolArg("-listen", false))
815820
LogPrintf("%s: parameter interaction: -proxy set -> setting -listen=0\n", __func__);
816-
// to protect privacy, do not use UPNP when a proxy is set. The user may still specify -listen=1
821+
// to protect privacy, do not map ports when a proxy is set. The user may still specify -listen=1
817822
// to listen locally, so don't rely on this happening through -listen below.
818823
if (args.SoftSetBoolArg("-upnp", false))
819824
LogPrintf("%s: parameter interaction: -proxy set -> setting -upnp=0\n", __func__);
825+
if (args.SoftSetBoolArg("-natpmp", false)) {
826+
LogPrintf("%s: parameter interaction: -proxy set -> setting -natpmp=0\n", __func__);
827+
}
820828
// to protect privacy, do not discover addresses by default
821829
if (args.SoftSetBoolArg("-discover", false))
822830
LogPrintf("%s: parameter interaction: -proxy set -> setting -discover=0\n", __func__);
@@ -826,6 +834,9 @@ void InitParameterInteraction(ArgsManager& args)
826834
// do not map ports or try to retrieve public IP when not listening (pointless)
827835
if (args.SoftSetBoolArg("-upnp", false))
828836
LogPrintf("%s: parameter interaction: -listen=0 -> setting -upnp=0\n", __func__);
837+
if (args.SoftSetBoolArg("-natpmp", false)) {
838+
LogPrintf("%s: parameter interaction: -listen=0 -> setting -natpmp=0\n", __func__);
839+
}
829840
if (args.SoftSetBoolArg("-discover", false))
830841
LogPrintf("%s: parameter interaction: -listen=0 -> setting -discover=0\n", __func__);
831842
if (args.SoftSetBoolArg("-listenonion", false))
@@ -1899,8 +1910,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
18991910

19001911
Discover();
19011912

1902-
// Map ports with UPnP
1903-
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP));
1913+
// Map ports with UPnP or NAT-PMP.
1914+
StartMapPort(args.GetBoolArg("-upnp", DEFAULT_UPNP), gArgs.GetBoolArg("-natpmp", DEFAULT_NATPMP));
19041915

19051916
CConnman::Options connOptions;
19061917
connOptions.nLocalServices = nLocalServices;

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Node
8282
virtual bool shutdownRequested() = 0;
8383

8484
//! Map port.
85-
virtual void mapPort(bool use_upnp) = 0;
85+
virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
8686

8787
//! Get proxy.
8888
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;

src/mapport.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
297297
}
298298
}
299299

300-
void StartMapPort(bool use_upnp)
300+
void StartMapPort(bool use_upnp, bool use_natpmp)
301301
{
302302
MapPortProtoSetEnabled(MapPortProtoFlag::UPNP, use_upnp);
303+
MapPortProtoSetEnabled(MapPortProtoFlag::NAT_PMP, use_natpmp);
303304
DispatchMapPort();
304305
}
305306

@@ -320,7 +321,7 @@ void StopMapPort()
320321
}
321322

322323
#else // #if defined(USE_NATPMP) || defined(USE_UPNP)
323-
void StartMapPort(bool use_upnp)
324+
void StartMapPort(bool use_upnp, bool use_natpmp)
324325
{
325326
// Intentionally left blank.
326327
}

src/mapport.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55
#ifndef BITCOIN_MAPPORT_H
66
#define BITCOIN_MAPPORT_H
77

8-
/** -upnp default */
98
#ifdef USE_UPNP
10-
static const bool DEFAULT_UPNP = USE_UPNP;
9+
static constexpr bool DEFAULT_UPNP = USE_UPNP;
1110
#else
12-
static const bool DEFAULT_UPNP = false;
13-
#endif
11+
static constexpr bool DEFAULT_UPNP = false;
12+
#endif // USE_UPNP
13+
14+
#ifdef USE_NATPMP
15+
static constexpr bool DEFAULT_NATPMP = USE_NATPMP;
16+
#else
17+
static constexpr bool DEFAULT_NATPMP = false;
18+
#endif // USE_NATPMP
1419

1520
enum MapPortProtoFlag : unsigned int {
1621
NONE = 0x00,
1722
UPNP = 0x01,
1823
NAT_PMP = 0x02,
1924
};
2025

21-
void StartMapPort(bool use_upnp);
26+
void StartMapPort(bool use_upnp, bool use_natpmp);
2227
void InterruptMapPort();
2328
void StopMapPort();
2429

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class NodeImpl : public Node
9494
}
9595
}
9696
bool shutdownRequested() override { return ShutdownRequested(); }
97-
void mapPort(bool use_upnp) override { StartMapPort(use_upnp); }
97+
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
9898
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
9999
size_t getNodeCount(CConnman::NumConnections flags) override
100100
{

src/qt/optionsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
5353
#endif
5454
connect(this, &QDialog::accepted, [this](){
5555
QSettings settings;
56-
model->node().mapPort(settings.value("fUseUPnP").toBool());
56+
model->node().mapPort(settings.value("fUseUPnP").toBool(), settings.value("fUseNatpmp").toBool());
5757
});
5858

5959
ui->proxyIp->setEnabled(false);

src/qt/optionsmodel.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ void OptionsModel::Init(bool resetSettings)
124124
if (!gArgs.SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool()))
125125
addOverriddenOption("-upnp");
126126

127+
if (!settings.contains("fUseNatpmp")) {
128+
settings.setValue("fUseNatpmp", DEFAULT_NATPMP);
129+
}
130+
if (!gArgs.SoftSetBoolArg("-natpmp", settings.value("fUseNatpmp").toBool())) {
131+
addOverriddenOption("-natpmp");
132+
}
133+
127134
if (!settings.contains("fListen"))
128135
settings.setValue("fListen", DEFAULT_LISTEN);
129136
if (!gArgs.SoftSetBoolArg("-listen", settings.value("fListen").toBool()))
@@ -283,7 +290,13 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
283290
return settings.value("fUseUPnP");
284291
#else
285292
return false;
286-
#endif
293+
#endif // USE_UPNP
294+
case MapPortNatpmp:
295+
#ifdef USE_NATPMP
296+
return settings.value("fUseNatpmp");
297+
#else
298+
return false;
299+
#endif // USE_NATPMP
287300
case MinimizeOnClose:
288301
return fMinimizeOnClose;
289302

@@ -356,6 +369,9 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
356369
case MapPortUPnP: // core option - can be changed on-the-fly
357370
settings.setValue("fUseUPnP", value.toBool());
358371
break;
372+
case MapPortNatpmp: // core option - can be changed on-the-fly
373+
settings.setValue("fUseNatpmp", value.toBool());
374+
break;
359375
case MinimizeOnClose:
360376
fMinimizeOnClose = value.toBool();
361377
settings.setValue("fMinimizeOnClose", fMinimizeOnClose);

src/qt/optionsmodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class OptionsModel : public QAbstractListModel
4848
ShowTrayIcon, // bool
4949
MinimizeToTray, // bool
5050
MapPortUPnP, // bool
51+
MapPortNatpmp, // bool
5152
MinimizeOnClose, // bool
5253
ProxyUse, // bool
5354
ProxyIP, // QString

test/functional/test_framework/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def initialize_datadir(dirname, n, chain):
362362
f.write("listenonion=0\n")
363363
f.write("printtoconsole=0\n")
364364
f.write("upnp=0\n")
365+
f.write("natpmp=0\n")
365366
f.write("shrinkdebugfile=0\n")
366367
os.makedirs(os.path.join(datadir, 'stderr'), exist_ok=True)
367368
os.makedirs(os.path.join(datadir, 'stdout'), exist_ok=True)

0 commit comments

Comments
 (0)