Skip to content

Commit 07b2afe

Browse files
author
Marko Bencun
committed
add Binds, WhiteBinds to CConnman::Options
Part of a series of changes to clean up the instantiation of connman by decoupling the command line arguments. We also now abort with an error when explicit binds are set with -listen=0.
1 parent ce79f32 commit 07b2afe

File tree

3 files changed

+90
-59
lines changed

3 files changed

+90
-59
lines changed

src/init.cpp

Lines changed: 34 additions & 55 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);
@@ -900,10 +881,16 @@ bool AppInitParameterInteraction()
900881
return InitError(_("Prune mode is incompatible with -txindex."));
901882
}
902883

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

@@ -1339,36 +1326,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
13391326
fDiscover = GetBoolArg("-discover", true);
13401327
fRelayTxes = !GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY);
13411328

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

1638-
std::string strNodeError;
16391595
CConnman::Options connOptions;
16401596
connOptions.nLocalServices = nLocalServices;
16411597
connOptions.nRelevantServices = nRelevantServices;
@@ -1651,6 +1607,28 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16511607
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
16521608
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
16531609

1610+
if (gArgs.IsArgSet("-bind")) {
1611+
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1612+
CService addrBind;
1613+
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) {
1614+
return InitError(ResolveErrMsg("bind", strBind));
1615+
}
1616+
connOptions.vBinds.push_back(addrBind);
1617+
}
1618+
}
1619+
if (gArgs.IsArgSet("-whitebind")) {
1620+
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1621+
CService addrBind;
1622+
if (!Lookup(strBind.c_str(), addrBind, 0, false)) {
1623+
return InitError(ResolveErrMsg("whitebind", strBind));
1624+
}
1625+
if (addrBind.GetPort() == 0) {
1626+
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
1627+
}
1628+
connOptions.vWhiteBinds.push_back(addrBind);
1629+
}
1630+
}
1631+
16541632
if (gArgs.IsArgSet("-whitelist")) {
16551633
for (const auto& net : gArgs.GetArgs("-whitelist")) {
16561634
CSubNet subnet;
@@ -1665,8 +1643,9 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16651643
connOptions.vSeedNodes = gArgs.GetArgs("-seednode");
16661644
}
16671645

1668-
if (!connman.Start(scheduler, strNodeError, connOptions))
1669-
return InitError(strNodeError);
1646+
if (!connman.Start(scheduler, connOptions)) {
1647+
return false;
1648+
}
16701649

16711650
// ********************************************************* Step 12: finished
16721651

src/net.cpp

Lines changed: 51 additions & 2 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]
@@ -2219,7 +2227,38 @@ NodeId CConnman::GetNewNodeId()
22192227
return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
22202228
}
22212229

2222-
bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2230+
2231+
bool CConnman::Bind(const CService &addr, unsigned int flags) {
2232+
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2233+
return false;
2234+
std::string strError;
2235+
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
2236+
if ((flags & BF_REPORT_ERROR) && clientInterface) {
2237+
clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
2238+
}
2239+
return false;
2240+
}
2241+
return true;
2242+
}
2243+
2244+
bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
2245+
bool fBound = false;
2246+
for (const auto& addrBind : binds) {
2247+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
2248+
}
2249+
for (const auto& addrBind : whiteBinds) {
2250+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
2251+
}
2252+
if (binds.empty() && whiteBinds.empty()) {
2253+
struct in_addr inaddr_any;
2254+
inaddr_any.s_addr = INADDR_ANY;
2255+
fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
2256+
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
2257+
}
2258+
return fBound;
2259+
}
2260+
2261+
bool CConnman::Start(CScheduler& scheduler, Options connOptions)
22232262
{
22242263
nTotalBytesRecv = 0;
22252264
nTotalBytesSent = 0;
@@ -2241,13 +2280,23 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
22412280

22422281
SetBestHeight(connOptions.nBestHeight);
22432282

2283+
clientInterface = connOptions.uiInterface;
2284+
2285+
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
2286+
if (clientInterface) {
2287+
clientInterface->ThreadSafeMessageBox(
2288+
_("Failed to listen on any port. Use -listen=0 if you want this."),
2289+
"", CClientUIInterface::MSG_ERROR);
2290+
}
2291+
return false;
2292+
}
2293+
22442294
vWhitelistedRange = connOptions.vWhitelistedRange;
22452295

22462296
for (const auto& strDest : connOptions.vSeedNodes) {
22472297
AddOneShot(strDest);
22482298
}
22492299

2250-
clientInterface = connOptions.uiInterface;
22512300
if (clientInterface) {
22522301
clientInterface->InitMessage(_("Loading P2P addresses..."));
22532302
}

src/net.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ class CConnman
145145
uint64_t nMaxOutboundLimit = 0;
146146
std::vector<std::string> vSeedNodes;
147147
std::vector<CSubNet> vWhitelistedRange;
148+
std::vector<CService> vBinds, vWhiteBinds;
148149
};
149150
CConnman(uint64_t seed0, uint64_t seed1);
150151
~CConnman();
151-
bool Start(CScheduler& scheduler, std::string& strNodeError, Options options);
152+
bool Start(CScheduler& scheduler, Options options);
152153
void Stop();
153154
void Interrupt();
154-
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
155155
bool GetNetworkActive() const { return fNetworkActive; };
156156
void SetNetworkActive(bool active);
157157
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false, bool fAddnode = false);
@@ -288,6 +288,9 @@ class CConnman
288288
ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {}
289289
};
290290

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);
291294
void ThreadOpenAddedConnections();
292295
void AddOneShot(const std::string& strDest);
293296
void ProcessOneShot();

0 commit comments

Comments
 (0)