@@ -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-
9991static 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- }
310291void 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
0 commit comments