Skip to content

Commit 02137f1

Browse files
committed
net: Move socket binding into CConnman
1 parent 5b446dd commit 02137f1

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

src/init.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ void HandleSIGHUP(int)
273273
fReopenDebugLog = true;
274274
}
275275

276-
bool static Bind(const CService &addr, unsigned int flags) {
276+
bool static Bind(CConnman& connman, const CService &addr, unsigned int flags) {
277277
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
278278
return false;
279279
std::string strError;
280-
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
280+
if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
281281
if (flags & BF_REPORT_ERROR)
282282
return InitError(strError);
283283
return false;
@@ -1198,22 +1198,22 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11981198
CService addrBind;
11991199
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
12001200
return InitError(ResolveErrMsg("bind", strBind));
1201-
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
1201+
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
12021202
}
12031203
BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-whitebind"]) {
12041204
CService addrBind;
12051205
if (!Lookup(strBind.c_str(), addrBind, 0, false))
12061206
return InitError(ResolveErrMsg("whitebind", strBind));
12071207
if (addrBind.GetPort() == 0)
12081208
return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind));
1209-
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
1209+
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST));
12101210
}
12111211
}
12121212
else {
12131213
struct in_addr inaddr_any;
12141214
inaddr_any.s_addr = INADDR_ANY;
1215-
fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE);
1216-
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
1215+
fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE);
1216+
fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
12171217
}
12181218
if (!fBound)
12191219
return InitError(_("Failed to listen on any port. Use -listen=0 if you want this."));

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
8484
static bool vfLimited[NET_MAX] = {};
8585
static CNode* pnodeLocalHost = NULL;
8686
uint64_t nLocalHostNonce = 0;
87-
static std::vector<ListenSocket> vhListenSocket;
8887
CAddrMan addrman;
8988
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
9089
bool fAddressesInitialized = false;
@@ -1908,7 +1907,7 @@ void CConnman::ThreadMessageHandler()
19081907

19091908

19101909

1911-
bool BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
1910+
bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted)
19121911
{
19131912
strError = "";
19141913
int nOne = 1;

src/net.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,32 @@ CNode* FindNode(const CService& ip);
9595
CNode* FindNode(const NodeId id); //TODO: Remove this
9696
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
9797

98-
struct ListenSocket {
99-
SOCKET socket;
100-
bool whitelisted;
101-
102-
ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {}
103-
};
104-
10598
class CConnman
10699
{
107100
public:
108101
CConnman();
109102
~CConnman();
110103
bool Start(boost::thread_group& threadGroup, std::string& strNodeError);
111104
void Stop();
105+
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
106+
112107
private:
108+
struct ListenSocket {
109+
SOCKET socket;
110+
bool whitelisted;
111+
112+
ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {}
113+
};
114+
113115
void ThreadOpenAddedConnections();
114116
void ProcessOneShot();
115117
void ThreadOpenConnections();
116118
void ThreadMessageHandler();
117119
void AcceptConnection(const ListenSocket& hListenSocket);
118120
void ThreadSocketHandler();
119121
void ThreadDNSAddressSeed();
122+
123+
std::vector<ListenSocket> vhListenSocket;
120124
};
121125
extern std::unique_ptr<CConnman> g_connman;
122126
void MapPort(bool fUseUPnP);

0 commit comments

Comments
 (0)