Skip to content

Commit c481f62

Browse files
committed
refactor: Move port mapping code to its own module
This commit does not change behavior.
1 parent e8d2cf0 commit c481f62

File tree

8 files changed

+163
-124
lines changed

8 files changed

+163
-124
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ set(SERVER_SOURCES
195195
./src/interfaces/wallet.cpp
196196
./src/dbwrapper.cpp
197197
./src/legacy/validation_zerocoin_legacy.cpp
198+
./src/mapport.cpp
198199
./src/merkleblock.cpp
199200
./src/miner.cpp
200201
./src/blockassembler.cpp

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ BITCOIN_CORE_H = \
216216
budget/budgetvote.h \
217217
budget/finalizedbudget.h \
218218
budget/finalizedbudgetvote.h \
219+
mapport.h \
219220
memusage.h \
220221
masternode.h \
221222
masternode-payments.h \
@@ -343,6 +344,7 @@ libbitcoin_server_a_SOURCES = \
343344
sapling/sapling_validation.cpp \
344345
merkleblock.cpp \
345346
blockassembler.cpp \
347+
mapport.cpp \
346348
miner.cpp \
347349
net.cpp \
348350
net_processing.cpp \

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "httprpc.h"
3030
#include "invalid.h"
3131
#include "key.h"
32+
#include "mapport.h"
3233
#include "masternode-payments.h"
3334
#include "masternodeconfig.h"
3435
#include "masternodeman.h"

src/mapport.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright (c) 2011-2020 The Bitcoin Core developers
2+
// Copyright (c) 2021 The PIVX developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#if defined(HAVE_CONFIG_H)
7+
#include "config/pivx-config.h"
8+
#endif
9+
10+
#include "mapport.h"
11+
12+
#include "clientversion.h"
13+
#include "logging.h"
14+
#include "net.h"
15+
#include "netaddress.h"
16+
#include "netbase.h"
17+
#include "threadinterrupt.h"
18+
#include "util/system.h"
19+
20+
#ifdef USE_UPNP
21+
#include <miniupnpc/miniupnpc.h>
22+
#include <miniupnpc/upnpcommands.h>
23+
#include <miniupnpc/upnperrors.h>
24+
// The minimum supported miniUPnPc API version is set to 10. This keeps compatibility
25+
// with Ubuntu 16.04 LTS and Debian 8 libminiupnpc-dev packages.
26+
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
27+
#endif
28+
29+
#include <cassert>
30+
#include <chrono>
31+
#include <functional>
32+
#include <string>
33+
#include <thread>
34+
35+
#ifdef USE_UPNP
36+
static CThreadInterrupt g_upnp_interrupt;
37+
static std::thread g_upnp_thread;
38+
void ThreadMapPort()
39+
{
40+
std::string port = strprintf("%u", GetListenPort());
41+
const char* multicastif = 0;
42+
const char* minissdpdpath = 0;
43+
struct UPNPDev* devlist = 0;
44+
char lanaddr[64];
45+
46+
int error = 0;
47+
#if MINIUPNPC_API_VERSION < 14
48+
devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
49+
#else
50+
devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
51+
#endif
52+
53+
struct UPNPUrls urls;
54+
struct IGDdatas data;
55+
int r;
56+
57+
r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
58+
if (r == 1) {
59+
if (fDiscover) {
60+
char externalIPAddress[40];
61+
r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
62+
if (r != UPNPCOMMAND_SUCCESS) {
63+
LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
64+
} else {
65+
if (externalIPAddress[0]) {
66+
CNetAddr resolved;
67+
if (LookupHost(externalIPAddress, resolved, false)) {
68+
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
69+
AddLocal(resolved, LOCAL_UPNP);
70+
}
71+
} else {
72+
LogPrintf("UPnP: GetExternalIPAddress failed.\n");
73+
}
74+
}
75+
}
76+
77+
std::string strDesc = PACKAGE_NAME " " + FormatFullVersion();
78+
79+
do {
80+
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
81+
82+
if (r != UPNPCOMMAND_SUCCESS) {
83+
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n", port, port, lanaddr, r, strupnperror(r));
84+
} else {
85+
LogPrintf("UPnP Port Mapping successful.\n");
86+
}
87+
} while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
88+
89+
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
90+
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
91+
freeUPNPDevlist(devlist); devlist = nullptr;
92+
FreeUPNPUrls(&urls);
93+
} else {
94+
LogPrintf("No valid UPnP IGDs found\n");
95+
freeUPNPDevlist(devlist);
96+
devlist = 0;
97+
if (r != 0)
98+
FreeUPNPUrls(&urls);
99+
}
100+
}
101+
102+
void StartMapPort()
103+
{
104+
if (!g_upnp_thread.joinable()) {
105+
assert(!g_upnp_interrupt);
106+
g_upnp_thread = std::thread(std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
107+
}
108+
}
109+
110+
void InterruptMapPort()
111+
{
112+
if (g_upnp_thread.joinable()) {
113+
g_upnp_interrupt();
114+
}
115+
}
116+
117+
void StopMapPort()
118+
{
119+
if (g_upnp_thread.joinable()) {
120+
g_upnp_thread.join();
121+
g_upnp_interrupt.reset();
122+
}
123+
}
124+
125+
#else
126+
void StartMapPort()
127+
{
128+
// Intentionally left blank.
129+
}
130+
void InterruptMapPort()
131+
{
132+
// Intentionally left blank.
133+
}
134+
void StopMapPort()
135+
{
136+
// Intentionally left blank.
137+
}
138+
#endif

src/mapport.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2011-2020 The Bitcoin Core developers
2+
// Copyright (c) 2021 The PIVX developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef PIVX_MAPPORT_H
7+
#define PIVX_MAPPORT_H
8+
9+
/** -upnp default */
10+
#ifdef USE_UPNP
11+
static const bool DEFAULT_UPNP = USE_UPNP;
12+
#else
13+
static const bool DEFAULT_UPNP = false;
14+
#endif
15+
16+
void StartMapPort();
17+
void InterruptMapPort();
18+
void StopMapPort();
19+
20+
#endif //PIVX_MAPPORT_H

src/net.cpp

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@
3535
#include <ifaddrs.h>
3636
#endif
3737

38-
#ifdef USE_UPNP
39-
#include <miniupnpc/miniupnpc.h>
40-
#include <miniupnpc/upnpcommands.h>
41-
#include <miniupnpc/upnperrors.h>
42-
// The minimum supported miniUPnPc API version is set to 10. This keeps compatibility
43-
// with Ubuntu 16.04 LTS and Debian 8 libminiupnpc-dev packages.
44-
static_assert(MINIUPNPC_API_VERSION >= 10, "miniUPnPc API version >= 10 assumed");
45-
#endif
46-
47-
4838
#include <math.h>
4939

5040
// Dump addresses to peers.dat and banlist.dat every 15 minutes (900s)
@@ -1357,111 +1347,6 @@ void CConnman::WakeMessageHandler()
13571347
}
13581348

13591349

1360-
#ifdef USE_UPNP
1361-
static CThreadInterrupt g_upnp_interrupt;
1362-
static std::thread g_upnp_thread;
1363-
void ThreadMapPort()
1364-
{
1365-
std::string port = strprintf("%u", GetListenPort());
1366-
const char* multicastif = 0;
1367-
const char* minissdpdpath = 0;
1368-
struct UPNPDev* devlist = 0;
1369-
char lanaddr[64];
1370-
1371-
int error = 0;
1372-
#if MINIUPNPC_API_VERSION < 14
1373-
devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
1374-
#else
1375-
devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, 2, &error);
1376-
#endif
1377-
1378-
struct UPNPUrls urls;
1379-
struct IGDdatas data;
1380-
int r;
1381-
1382-
r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
1383-
if (r == 1) {
1384-
if (fDiscover) {
1385-
char externalIPAddress[40];
1386-
r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
1387-
if (r != UPNPCOMMAND_SUCCESS) {
1388-
LogPrintf("UPnP: GetExternalIPAddress() returned %d\n", r);
1389-
} else {
1390-
if (externalIPAddress[0]) {
1391-
CNetAddr resolved;
1392-
if (LookupHost(externalIPAddress, resolved, false)) {
1393-
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
1394-
AddLocal(resolved, LOCAL_UPNP);
1395-
}
1396-
} else {
1397-
LogPrintf("UPnP: GetExternalIPAddress failed.\n");
1398-
}
1399-
}
1400-
}
1401-
1402-
std::string strDesc = PACKAGE_NAME " " + FormatFullVersion();
1403-
1404-
do {
1405-
r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, port.c_str(), port.c_str(), lanaddr, strDesc.c_str(), "TCP", 0, "0");
1406-
1407-
if (r != UPNPCOMMAND_SUCCESS) {
1408-
LogPrintf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n", port, port, lanaddr, r, strupnperror(r));
1409-
} else {
1410-
LogPrintf("UPnP Port Mapping successful.\n");
1411-
}
1412-
} while(g_upnp_interrupt.sleep_for(std::chrono::minutes(20)));
1413-
1414-
r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port.c_str(), "TCP", 0);
1415-
LogPrintf("UPNP_DeletePortMapping() returned: %d\n", r);
1416-
freeUPNPDevlist(devlist); devlist = nullptr;
1417-
FreeUPNPUrls(&urls);
1418-
} else {
1419-
LogPrintf("No valid UPnP IGDs found\n");
1420-
freeUPNPDevlist(devlist);
1421-
devlist = 0;
1422-
if (r != 0)
1423-
FreeUPNPUrls(&urls);
1424-
}
1425-
}
1426-
1427-
void StartMapPort()
1428-
{
1429-
if (!g_upnp_thread.joinable()) {
1430-
assert(!g_upnp_interrupt);
1431-
g_upnp_thread = std::thread(std::bind(&TraceThread<void (*)()>, "upnp", &ThreadMapPort));
1432-
}
1433-
}
1434-
1435-
void InterruptMapPort()
1436-
{
1437-
if (g_upnp_thread.joinable()) {
1438-
g_upnp_interrupt();
1439-
}
1440-
}
1441-
1442-
void StopMapPort()
1443-
{
1444-
if (g_upnp_thread.joinable()) {
1445-
g_upnp_thread.join();
1446-
g_upnp_interrupt.reset();
1447-
}
1448-
}
1449-
1450-
#else
1451-
void StartMapPort()
1452-
{
1453-
// Intentionally left blank.
1454-
}
1455-
void InterruptMapPort()
1456-
{
1457-
// Intentionally left blank.
1458-
}
1459-
void StopMapPort()
1460-
{
1461-
// Intentionally left blank.
1462-
}
1463-
#endif
1464-
14651350
static std::string GetDNSHost(const CDNSSeedData& data, ServiceFlags* requiredServiceBits)
14661351
{
14671352
//use default host for non-filter-capable seeds or if we use the default service bits (NODE_NETWORK)

src/net.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ static const unsigned int MAX_SUBVERSION_LENGTH = 256;
6161
static const int MAX_OUTBOUND_CONNECTIONS = 16;
6262
/** -listen default */
6363
static const bool DEFAULT_LISTEN = true;
64-
/** -upnp default */
65-
#ifdef USE_UPNP
66-
static const bool DEFAULT_UPNP = USE_UPNP;
67-
#else
68-
static const bool DEFAULT_UPNP = false;
69-
#endif
7064
/** The maximum number of entries in mapAskFor */
7165
static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
7266
/** The maximum number of entries in setAskFor (larger due to getdata latency)*/
@@ -389,9 +383,6 @@ class CConnman
389383
};
390384
extern std::unique_ptr<CConnman> g_connman;
391385
void Discover();
392-
void StartMapPort();
393-
void InterruptMapPort();
394-
void StopMapPort();
395386
unsigned short GetListenPort();
396387
bool BindListenPort(const CService& bindAddr, std::string& strError, bool fWhitelisted = false);
397388
void CheckOffsetDisconnectedPeers(const CNetAddr& ip);

src/qt/optionsmodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "amount.h"
1717
#include "init.h"
18+
#include "mapport.h"
1819
#include "net.h"
1920
#include "netbase.h"
2021
#include "txdb.h" // for -dbcache defaults

0 commit comments

Comments
 (0)