Skip to content

Commit 9491676

Browse files
committed
Merge bitcoin#31157: Cleanups to port mapping module post UPnP drop
70398ae mapport: make ProcessPCP void (Antoine Poinsot) 9e6cba2 mapport: remove unnecessary 'g_mapport_enabled' (Antoine Poinsot) 8fb45fc mapport: remove unnecessary 'g_mapport_current' variable (Antoine Poinsot) 1b223cb mapport: merge DispatchMapPort into StartMapPort (Antoine Poinsot) 9bd936f mapport: drop unnecessary function (Antoine Poinsot) 2a6536c mapport: rename 'use_pcp' to 'enable' (Antoine Poinsot) c4e82b8 mapport: make 'enabled' and 'current' bool (Antoine Poinsot) Pull request description: Followup to bitcoin#31130, this does a couple cleanups to `src/mapport.*` to clarify the logic now that there is a single protocol option for port mapping. ACKs for top commit: laanwj: Code review ACK 70398ae TheCharlatan: ACK 70398ae Tree-SHA512: d9a3ab4fcd59a7cf4872415c40cc7ac3a98dfc5aa25e195d4df880bb588bac286c30c3471e9d9499de379a75f45dcd0a82019eba3cb9f342004ae1482d0ba075
2 parents 109bfe9 + 70398ae commit 9491676

File tree

4 files changed

+10
-61
lines changed

4 files changed

+10
-61
lines changed

src/interfaces/node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Node
121121
virtual void resetSettings() = 0;
122122

123123
//! Map port.
124-
virtual void mapPort(bool use_pcp) = 0;
124+
virtual void mapPort(bool enable) = 0;
125125

126126
//! Get proxy.
127127
virtual bool getProxy(Network net, Proxy& proxy_info) = 0;

src/mapport.cpp

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525

2626
static CThreadInterrupt g_mapport_interrupt;
2727
static std::thread g_mapport_thread;
28-
static std::atomic_uint g_mapport_enabled_protos{MapPortProtoFlag::NONE};
29-
static std::atomic<MapPortProtoFlag> g_mapport_current_proto{MapPortProtoFlag::NONE};
3028

3129
using namespace std::chrono_literals;
3230
static constexpr auto PORT_MAPPING_REANNOUNCE_PERIOD{20min};
3331
static constexpr auto PORT_MAPPING_RETRY_PERIOD{5min};
3432

35-
static bool ProcessPCP()
33+
static void ProcessPCP()
3634
{
3735
// The same nonce is used for all mappings, this is allowed by the spec, and simplifies keeping track of them.
3836
PCPMappingNonce pcp_nonce;
@@ -108,7 +106,7 @@ static bool ProcessPCP()
108106
// Sanity-check returned lifetime.
109107
if (actual_lifetime < 30) {
110108
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "portmap: Got impossibly short mapping lifetime of %d seconds\n", actual_lifetime);
111-
return false;
109+
return;
112110
}
113111
// RFC6887 11.2.1 recommends that clients send their first renewal packet at a time chosen with uniform random
114112
// distribution in the range 1/2 to 5/8 of expiration time.
@@ -119,28 +117,13 @@ static bool ProcessPCP()
119117

120118
// We don't delete the mappings when the thread is interrupted because this would add additional complexity, so
121119
// we rather just choose a fairly short expiry time.
122-
123-
return ret;
124120
}
125121

126122
static void ThreadMapPort()
127123
{
128-
bool ok;
129124
do {
130-
ok = false;
131-
132-
if (g_mapport_enabled_protos & MapPortProtoFlag::PCP) {
133-
g_mapport_current_proto = MapPortProtoFlag::PCP;
134-
ok = ProcessPCP();
135-
if (ok) continue;
136-
}
137-
138-
g_mapport_current_proto = MapPortProtoFlag::NONE;
139-
if (g_mapport_enabled_protos == MapPortProtoFlag::NONE) {
140-
return;
141-
}
142-
143-
} while (ok || g_mapport_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
125+
ProcessPCP();
126+
} while (g_mapport_interrupt.sleep_for(PORT_MAPPING_RETRY_PERIOD));
144127
}
145128

146129
void StartThreadMapPort()
@@ -151,46 +134,18 @@ void StartThreadMapPort()
151134
}
152135
}
153136

154-
static void DispatchMapPort()
137+
void StartMapPort(bool enable)
155138
{
156-
if (g_mapport_current_proto == MapPortProtoFlag::NONE && g_mapport_enabled_protos == MapPortProtoFlag::NONE) {
157-
return;
158-
}
159-
160-
if (g_mapport_current_proto == MapPortProtoFlag::NONE && g_mapport_enabled_protos != MapPortProtoFlag::NONE) {
139+
if (enable) {
161140
StartThreadMapPort();
162-
return;
163-
}
164-
165-
if (g_mapport_current_proto != MapPortProtoFlag::NONE && g_mapport_enabled_protos == MapPortProtoFlag::NONE) {
141+
} else {
166142
InterruptMapPort();
167143
StopMapPort();
168-
return;
169-
}
170-
171-
if (g_mapport_enabled_protos & g_mapport_current_proto) {
172-
return;
173144
}
174145
}
175146

176-
static void MapPortProtoSetEnabled(MapPortProtoFlag proto, bool enabled)
177-
{
178-
if (enabled) {
179-
g_mapport_enabled_protos |= proto;
180-
} else {
181-
g_mapport_enabled_protos &= ~proto;
182-
}
183-
}
184-
185-
void StartMapPort(bool use_pcp)
186-
{
187-
MapPortProtoSetEnabled(MapPortProtoFlag::PCP, use_pcp);
188-
DispatchMapPort();
189-
}
190-
191147
void InterruptMapPort()
192148
{
193-
g_mapport_enabled_protos = MapPortProtoFlag::NONE;
194149
if (g_mapport_thread.joinable()) {
195150
g_mapport_interrupt();
196151
}

src/mapport.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77

88
static constexpr bool DEFAULT_NATPMP = false;
99

10-
enum MapPortProtoFlag : unsigned int {
11-
NONE = 0x00,
12-
// 0x01 was for UPnP, for which we dropped support.
13-
PCP = 0x02, // PCP with NAT-PMP fallback.
14-
};
15-
16-
void StartMapPort(bool use_pcp);
10+
void StartMapPort(bool enable);
1711
void InterruptMapPort();
1812
void StopMapPort();
1913

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class NodeImpl : public Node
188188
});
189189
args().WriteSettingsFile();
190190
}
191-
void mapPort(bool use_pcp) override { StartMapPort(use_pcp); }
191+
void mapPort(bool enable) override { StartMapPort(enable); }
192192
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
193193
size_t getNodeCount(ConnectionDirection flags) override
194194
{

0 commit comments

Comments
 (0)