Skip to content

Commit d9e91ff

Browse files
Marko Bencunfurszy
authored andcommitted
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. Coming from btc@ce79f3251851f6177f38009341802e6065cb70af
1 parent 41c89af commit d9e91ff

File tree

3 files changed

+84
-55
lines changed

3 files changed

+84
-55
lines changed

src/init.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,6 @@ static EvoNotificationInterface* pEvoNotificationInterface = nullptr;
115115

116116
static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
117117

118-
/** Used to pass flags to the Bind() function */
119-
enum BindFlags {
120-
BF_NONE = 0,
121-
BF_EXPLICIT = (1U << 0),
122-
BF_REPORT_ERROR = (1U << 1),
123-
BF_WHITELIST = (1U << 2),
124-
};
125-
126118
static const char* FEE_ESTIMATES_FILENAME = "fee_estimates.dat";
127119
CClientUIInterface uiInterface; // Declared but not defined in guiinterface.h
128120

@@ -387,19 +379,6 @@ static void registerSignalHandler(int signal, void(*handler)(int))
387379
}
388380
#endif
389381

390-
bool static Bind(CConnman& connman, const CService& addr, unsigned int flags)
391-
{
392-
if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
393-
return false;
394-
std::string strError;
395-
if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
396-
if (flags & BF_REPORT_ERROR)
397-
return UIError(strError);
398-
return false;
399-
}
400-
return true;
401-
}
402-
403382
void OnRPCStarted()
404383
{
405384
uiInterface.NotifyBlockTip.connect(RPCNotifyBlockChange);
@@ -1044,11 +1023,14 @@ bool AppInitParameterInteraction()
10441023
// Make sure enough file descriptors are available
10451024

10461025
// -bind and -whitebind can't be set when not listening
1047-
size_t nUserBind = gArgs.GetArgs("-bind").size() + gArgs.GetArgs("-whitebind").size();
1026+
size_t nUserBind =
1027+
(gArgs.IsArgSet("-bind") ? gArgs.GetArgs("-bind").size() : 0) +
1028+
(gArgs.IsArgSet("-whitebind") ? gArgs.GetArgs("-whitebind").size() : 0);
10481029
if (nUserBind != 0 && !gArgs.GetBoolArg("-listen", DEFAULT_LISTEN)) {
10491030
return UIError(strprintf(_("Cannot set %s or %s together with %s"), "-bind", "-whitebind", "-listen=0"));
10501031
}
10511032

1033+
// Make sure enough file descriptors are available
10521034
int nBind = std::max(nUserBind, size_t(1));
10531035
nUserMaxConnections = gArgs.GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
10541036
nMaxConnections = std::max(nUserMaxConnections, 0);
@@ -1440,32 +1422,6 @@ bool AppInitMain()
14401422
fListen = gArgs.GetBoolArg("-listen", DEFAULT_LISTEN);
14411423
fDiscover = gArgs.GetBoolArg("-discover", true);
14421424

1443-
bool fBound = false;
1444-
if (fListen) {
1445-
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1446-
CService addrBind;
1447-
if (!Lookup(strBind, addrBind, GetListenPort(), false))
1448-
return UIError(ResolveErrMsg("bind", strBind));
1449-
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
1450-
}
1451-
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1452-
CService addrBind;
1453-
if (!Lookup(strBind, addrBind, 0, false))
1454-
return UIError(ResolveErrMsg("whitebind", strBind));
1455-
if (addrBind.GetPort() == 0)
1456-
return UIError(strprintf(_("Need to specify a port with %s: '%s'"), "-whitebind", strBind));
1457-
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
1458-
}
1459-
if (!gArgs.IsArgSet("-bind") && !gArgs.IsArgSet("-whitebind")) {
1460-
struct in_addr inaddr_any;
1461-
inaddr_any.s_addr = INADDR_ANY;
1462-
fBound |= Bind(connman, CService((in6_addr)IN6ADDR_ANY_INIT, GetListenPort()), BF_NONE);
1463-
fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
1464-
}
1465-
if (!fBound)
1466-
return UIError(strprintf(_("Failed to listen on any port. Use %s if you want this."), "-listen=0"));
1467-
}
1468-
14691425
for (const std::string& strAddr : gArgs.GetArgs("-externalip")) {
14701426
CService addrLocal;
14711427
if (Lookup(strAddr, addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
@@ -1963,7 +1919,6 @@ bool AppInitMain()
19631919
// Map ports with UPnP or NAT-PMP
19641920
StartMapPort(gArgs.GetBoolArg("-upnp", DEFAULT_UPNP), gArgs.GetBoolArg("-natpmp", DEFAULT_NATPMP));
19651921

1966-
std::string strNodeError;
19671922
CConnman::Options connOptions;
19681923
connOptions.nLocalServices = nLocalServices;
19691924
connOptions.nRelevantServices = nRelevantServices;
@@ -1977,6 +1932,28 @@ bool AppInitMain()
19771932
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
19781933
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
19791934

1935+
if (gArgs.IsArgSet("-bind")) {
1936+
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
1937+
CService addrBind;
1938+
if (!Lookup(strBind, addrBind, GetListenPort(), false)) {
1939+
return UIError(ResolveErrMsg("bind", strBind));
1940+
}
1941+
connOptions.vBinds.emplace_back(addrBind);
1942+
}
1943+
}
1944+
if (gArgs.IsArgSet("-whitebind")) {
1945+
for (const std::string& strBind : gArgs.GetArgs("-whitebind")) {
1946+
CService addrBind;
1947+
if (!Lookup(strBind, addrBind, 0, false)) {
1948+
return UIError(ResolveErrMsg("whitebind", strBind));
1949+
}
1950+
if (addrBind.GetPort() == 0) {
1951+
return UIError(strprintf(_("Need to specify a port with %s: '%s'"), "-whitebind", strBind));
1952+
}
1953+
connOptions.vWhiteBinds.emplace_back(addrBind);
1954+
}
1955+
}
1956+
19801957
for (const auto& net : gArgs.GetArgs("-whitelist")) {
19811958
CSubNet subnet;
19821959
LookupSubNet(net, subnet);
@@ -1985,8 +1962,9 @@ bool AppInitMain()
19851962
connOptions.vWhitelistedRange.emplace_back(subnet);
19861963
}
19871964

1988-
if (!connman.Start(scheduler, strNodeError, connOptions))
1989-
return UIError(strNodeError);
1965+
if (!connman.Start(scheduler, connOptions)) {
1966+
return false;
1967+
}
19901968

19911969
#ifdef ENABLE_WALLET
19921970
// Generate coins in the background (disabled on mainnet. use only wallet 0)

src/net.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
#endif
6060
#endif
6161

62+
/** Used to pass flags to the Bind() function */
63+
enum BindFlags {
64+
BF_NONE = 0,
65+
BF_EXPLICIT = (1U << 0),
66+
BF_REPORT_ERROR = (1U << 1),
67+
BF_WHITELIST = (1U << 2),
68+
};
69+
6270
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
6371

6472
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
@@ -1931,7 +1939,37 @@ NodeId CConnman::GetNewNodeId()
19311939
return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
19321940
}
19331941

1934-
bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options connOptions)
1942+
bool CConnman::Bind(const CService& addr, unsigned int flags) {
1943+
if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
1944+
return false;
1945+
std::string strError;
1946+
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
1947+
if ((flags & BF_REPORT_ERROR) && clientInterface) {
1948+
clientInterface->ThreadSafeMessageBox(strError, "", CClientUIInterface::MSG_ERROR);
1949+
}
1950+
return false;
1951+
}
1952+
return true;
1953+
}
1954+
1955+
bool CConnman::InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds) {
1956+
bool fBound = false;
1957+
for (const auto& addrBind : binds) {
1958+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
1959+
}
1960+
for (const auto& addrBind : whiteBinds) {
1961+
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
1962+
}
1963+
if (binds.empty() && whiteBinds.empty()) {
1964+
struct in_addr inaddr_any;
1965+
inaddr_any.s_addr = INADDR_ANY;
1966+
fBound |= Bind(CService((in6_addr)IN6ADDR_ANY_INIT, GetListenPort()), BF_NONE);
1967+
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
1968+
}
1969+
return fBound;
1970+
}
1971+
1972+
bool CConnman::Start(CScheduler& scheduler, Options connOptions)
19351973
{
19361974
nTotalBytesRecv = 0;
19371975
nTotalBytesSent = 0;
@@ -1948,9 +1986,19 @@ bool CConnman::Start(CScheduler& scheduler, std::string& strNodeError, Options c
19481986

19491987
SetBestHeight(connOptions.nBestHeight);
19501988

1989+
clientInterface = connOptions.uiInterface;
1990+
1991+
if (fListen && !InitBinds(connOptions.vBinds, connOptions.vWhiteBinds)) {
1992+
if (clientInterface) {
1993+
clientInterface->ThreadSafeMessageBox(
1994+
_("Failed to listen on any port. Use -listen=0 if you want this."),
1995+
"", CClientUIInterface::MSG_ERROR);
1996+
}
1997+
return false;
1998+
}
1999+
19512000
vWhitelistedRange = connOptions.vWhitelistedRange;
19522001

1953-
clientInterface = connOptions.uiInterface;
19542002
if (clientInterface)
19552003
clientInterface->InitMessage(_("Loading addresses..."));
19562004
m_msgproc = connOptions.m_msgproc;

src/net.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ class CConnman
143143
unsigned int nReceiveFloodSize = 0;
144144
std::vector<bool> m_asmap;
145145
std::vector<CSubNet> vWhitelistedRange;
146+
std::vector<CService> vBinds, vWhiteBinds;
146147
};
147148
CConnman(uint64_t seed0, uint64_t seed1);
148149
~CConnman();
149-
bool Start(CScheduler& scheduler, std::string& strNodeError, Options options);
150+
bool Start(CScheduler& scheduler, Options options);
150151
void Stop();
151152
void Interrupt();
152-
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
153153
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant* grantOutbound = nullptr, const char* strDest = nullptr, bool fOneShot = false, bool fFeeler = false, bool fAddnode = false);
154154
bool CheckIncomingNonce(uint64_t nonce);
155155

@@ -307,6 +307,9 @@ class CConnman
307307
ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {}
308308
};
309309

310+
bool BindListenPort(const CService& bindAddr, std::string& strError, bool fWhitelisted = false);
311+
bool Bind(const CService& addr, unsigned int flags);
312+
bool InitBinds(const std::vector<CService>& binds, const std::vector<CService>& whiteBinds);
310313
void ThreadOpenAddedConnections();
311314
void ProcessOneShot();
312315
void ThreadOpenConnections();

0 commit comments

Comments
 (0)