Skip to content

Commit 22a0aca

Browse files
committed
Merge #10496: Add Binds, WhiteBinds, Whitelistedrange to CConnman::Options
07b2afe add Binds, WhiteBinds to CConnman::Options (Marko Bencun) ce79f32 add WhitelistedRange to CConnman::Options (Marko Bencun) Tree-SHA512: c23a6f317c955338af531fa3e53e3c42e995f88c6e1939bbc2ad119fa5b786c54b3dad3d2e9b3f830b7292c0c63a02fcff66a89907d0fa8d7c83aefade01af45
2 parents f3f1e2e + 07b2afe commit 22a0aca

File tree

3 files changed

+103
-79
lines changed

3 files changed

+103
-79
lines changed

src/init.cpp

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,6 @@ static CZMQNotificationInterface* pzmqNotificationInterface = NULL;
8888
#define MIN_CORE_FILEDESCRIPTORS 150
8989
#endif
9090

91-
/** Used to pass flags to the Bind() function */
92-
enum BindFlags {
93-
BF_NONE = 0,
94-
BF_EXPLICIT = (1U << 0),
95-
BF_REPORT_ERROR = (1U << 1),
96-
BF_WHITELIST = (1U << 2),
97-
};
98-
9991
static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
10092

10193
//////////////////////////////////////////////////////////////////////////////
@@ -296,17 +288,6 @@ static void registerSignalHandler(int signal, void(*handler)(int))
296288
}
297289
#endif
298290

299-
bool static Bind(CConnman& connman, const CService &addr, unsigned int flags) {
300-
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
301-
return false;
302-
std::string strError;
303-
if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
304-
if (flags & BF_REPORT_ERROR)
305-
return InitError(strError);
306-
return false;
307-
}
308-
return true;
309-
}
310291
void OnRPCStarted()
311292
{
312293
uiInterface.NotifyBlockTip.connect(&RPCNotifyBlockChange);
@@ -898,10 +879,16 @@ bool AppInitParameterInteraction()
898879
return InitError(_("Prune mode is incompatible with -txindex."));
899880
}
900881

882+
// -bind and -whitebind can't be set when not listening
883+
size_t nUserBind =
884+
(gArgs.IsArgSet("-bind") ? gArgs.GetArgs("-bind").size() : 0) +
885+
(gArgs.IsArgSet("-whitebind") ? gArgs.GetArgs("-whitebind").size() : 0);
886+
if (nUserBind != 0 && !gArgs.GetBoolArg("-listen", DEFAULT_LISTEN)) {
887+
return InitError("Cannot set -bind or -whitebind together with -listen=0");
888+
}
889+
901890
// Make sure enough file descriptors are available
902-
int nBind = std::max(
903-
(gArgs.IsArgSet("-bind") ? gArgs.GetArgs("-bind").size() : 0) +
904-
(gArgs.IsArgSet("-whitebind") ? gArgs.GetArgs("-whitebind").size() : 0), size_t(1));
891+
int nBind = std::max(nUserBind, size_t(1));
905892
nUserMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
906893
nMaxConnections = std::max(nUserMaxConnections, 0);
907894

@@ -1280,16 +1267,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12801267
}
12811268
}
12821269

1283-
if (gArgs.IsArgSet("-whitelist")) {
1284-
for (const std::string& net : gArgs.GetArgs("-whitelist")) {
1285-
CSubNet subnet;
1286-
LookupSubNet(net.c_str(), subnet);
1287-
if (!subnet.IsValid())
1288-
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
1289-
connman.AddWhitelistedRange(subnet);
1290-
}
1291-
}
1292-
12931270
// Check for host lookup allowed before parsing any network related parameters
12941271
fNameLookup = GetBoolArg("-dns", DEFAULT_NAME_LOOKUP);
12951272

@@ -1340,36 +1317,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13401317
fDiscover = GetBoolArg("-discover", true);
13411318
fRelayTxes = !GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY);
13421319

1343-
if (fListen) {
1344-
bool fBound = false;
1345-
if (gArgs.IsArgSet("-bind")) {
1346-
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1347-
CService addrBind;
1348-
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
1349-
return InitError(ResolveErrMsg("bind", strBind));
1350-
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
1351-
}
1352-
}
1353-
if (gArgs.IsArgSet("-whitebind")) {
1354-
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1355-
CService addrBind;
1356-
if (!Lookup(strBind.c_str(), addrBind, 0, false))
1357-
return InitError(ResolveErrMsg("whitebind", strBind));
1358-
if (addrBind.GetPort() == 0)
1359-
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
1360-
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
1361-
}
1362-
}
1363-
if (!gArgs.IsArgSet("-bind") && !gArgs.IsArgSet("-whitebind")) {
1364-
struct in_addr inaddr_any;
1365-
inaddr_any.s_addr = INADDR_ANY;
1366-
fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE);
1367-
fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
1368-
}
1369-
if (!fBound)
1370-
return InitError(_("Failed to listen on any port. Use -listen=0 if you want this."));
1371-
}
1372-
13731320
if (gArgs.IsArgSet("-externalip")) {
13741321
for (const std::string& strAddr : gArgs.GetArgs("-externalip")) {
13751322
CService addrLocal;
@@ -1636,7 +1583,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16361583
// Map ports with UPnP
16371584
MapPort(GetBoolArg("-upnp", DEFAULT_UPNP));
16381585

1639-
std::string strNodeError;
16401586
CConnman::Options connOptions;
16411587
connOptions.nLocalServices = nLocalServices;
16421588
connOptions.nRelevantServices = nRelevantServices;
@@ -1652,12 +1598,45 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16521598
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
16531599
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
16541600

1601+
if (gArgs.IsArgSet("-bind")) {
1602+
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1603+
CService addrBind;
1604+
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) {
1605+
return InitError(ResolveErrMsg("bind", strBind));
1606+
}
1607+
connOptions.vBinds.push_back(addrBind);
1608+
}
1609+
}
1610+
if (gArgs.IsArgSet("-whitebind")) {
1611+
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1612+
CService addrBind;
1613+
if (!Lookup(strBind.c_str(), addrBind, 0, false)) {
1614+
return InitError(ResolveErrMsg("whitebind", strBind));
1615+
}
1616+
if (addrBind.GetPort() == 0) {
1617+
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
1618+
}
1619+
connOptions.vWhiteBinds.push_back(addrBind);
1620+
}
1621+
}
1622+
1623+
if (gArgs.IsArgSet("-whitelist")) {
1624+
for (const auto& net : gArgs.GetArgs("-whitelist")) {
1625+
CSubNet subnet;
1626+
LookupSubNet(net.c_str(), subnet);
1627+
if (!subnet.IsValid())
1628+
return InitError(strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net));
1629+
connOptions.vWhitelistedRange.push_back(subnet);
1630+
}
1631+
}
1632+
16551633
if (gArgs.IsArgSet("-seednode")) {
16561634
connOptions.vSeedNodes = gArgs.GetArgs("-seednode");
16571635
}
16581636

1659-
if (!connman.Start(scheduler, strNodeError, connOptions))
1660-
return InitError(strNodeError);
1637+
if (!connman.Start(scheduler, connOptions)) {
1638+
return false;
1639+
}
16611640

16621641
// ********************************************************* Step 12: finished
16631642

src/net.cpp

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
#endif
6565
#endif
6666

67+
/** Used to pass flags to the Bind() function */
68+
enum BindFlags {
69+
BF_NONE = 0,
70+
BF_EXPLICIT = (1U << 0),
71+
BF_REPORT_ERROR = (1U << 1),
72+
BF_WHITELIST = (1U << 2),
73+
};
74+
6775
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
6876

6977
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
@@ -601,20 +609,13 @@ void CConnman::SetBannedSetDirty(bool dirty)
601609

602610

603611
bool CConnman::IsWhitelistedRange(const CNetAddr &addr) {
604-
LOCK(cs_vWhitelistedRange);
605612
for (const CSubNet& subnet : vWhitelistedRange) {
606613
if (subnet.Match(addr))
607614
return true;
608615
}
609616
return false;
610617
}
611618

612-
void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
613-
LOCK(cs_vWhitelistedRange);
614-
vWhitelistedRange.push_back(subnet);
615-
}
616-
617-
618619
std::string CNode::GetAddrName() const {
619620
LOCK(cs_addrName);
620621
return addrName;
@@ -2220,7 +2221,38 @@ NodeId CConnman::GetNewNodeId()
22202221
return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
22212222
}
22222223

2223-
bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2224+
2225+
bool CConnman::Bind(const CService &addr, unsigned int flags) {
2226+
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2227+
return false;
2228+
std::string strError;
2229+
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2230+
if ((flags & BF_REPORT_ERROR) && clientInterface) {
2231+
clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2232+
}
2233+
return false;
2234+
}
2235+
return true;
2236+
}
2237+
2238+
bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2239+
bool fBound = false;
2240+
for (const auto& addrBind : binds) {
2241+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2242+
}
2243+
for (const auto& addrBind : whiteBinds) {
2244+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2245+
}
2246+
if (binds.empty() && whiteBinds.empty()) {
2247+
struct in_addr inaddr_any;
2248+
inaddr_any.s_addr = INADDR_ANY;
2249+
fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2250+
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2251+
}
2252+
return fBound;
2253+
}
2254+
2255+
bool CConnman::Start(CScheduler& scheduler, Options connOptions)
22242256
{
22252257
nTotalBytesRecv = 0;
22262258
nTotalBytesSent = 0;
@@ -2242,11 +2274,23 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
22422274

22432275
SetBestHeight(connOptions.nBestHeight);
22442276

2277+
clientInterface = connOptions.uiInterface;
2278+
2279+
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2280+
if (clientInterface) {
2281+
clientInterface->ThreadSafeMessageBox(
2282+
_("Failed to listen on any port. Use -listen=0 if you want this."),
2283+
"", CClientUIInterface::MSG_ERROR);
2284+
}
2285+
return false;
2286+
}
2287+
2288+
vWhitelistedRange = connOptions.vWhitelistedRange;
2289+
22452290
for (const auto& strDest : connOptions.vSeedNodes) {
22462291
AddOneShot(strDest);
22472292
}
22482293

2249-
clientInterface = connOptions.uiInterface;
22502294
if (clientInterface) {
22512295
clientInterface->InitMessage(_("Loading P2P addresses..."));
22522296
}

src/net.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ class CConnman
144144
uint64_t nMaxOutboundTimeframe = 0;
145145
uint64_t nMaxOutboundLimit = 0;
146146
std::vector<std::string> vSeedNodes;
147+
std::vector<CSubNet> vWhitelistedRange;
148+
std::vector<CService> vBinds, vWhiteBinds;
147149
};
148150
CConnman(uint64_t seed0, uint64_t seed1);
149151
~CConnman();
150-
bool Start(CScheduler& scheduler, std::string& strNodeError, Options options);
152+
bool Start(CScheduler& scheduler, Options options);
151153
void Stop();
152154
void Interrupt();
153-
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
154155
bool GetNetworkActive() const { return fNetworkActive; };
155156
void SetNetworkActive(bool active);
156157
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false, bool fAddnode = false);
@@ -244,8 +245,6 @@ class CConnman
244245

245246
unsigned int GetSendBufferSize() const;
246247

247-
void AddWhitelistedRange(const CSubNet &subnet);
248-
249248
ServiceFlags GetLocalServices() const;
250249

251250
//!set the max outbound target in bytes
@@ -289,6 +288,9 @@ class CConnman
289288
ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {}
290289
};
291290

291+
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
292+
bool Bind(const CService &addr, unsigned int flags);
293+
bool InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds);
292294
void ThreadOpenAddedConnections();
293295
void AddOneShot(const std::string& strDest);
294296
void ProcessOneShot();
@@ -346,7 +348,6 @@ class CConnman
346348
// Whitelisted ranges. Any node connecting from these is automatically
347349
// whitelisted (as well as those connecting to whitelisted binds).
348350
std::vector<CSubNet> vWhitelistedRange;
349-
CCriticalSection cs_vWhitelistedRange;
350351

351352
unsigned int nSendBufferMaxSize;
352353
unsigned int nReceiveFloodSize;

0 commit comments

Comments
 (0)