From 02137f11e2ea5d153f433493639730587836a1e3 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Sat, 16 Apr 2016 15:46:00 -0400 Subject: [PATCH] net: Move socket binding into CConnman --- src/init.cpp | 12 ++++++------ src/net.cpp | 3 +-- src/net.h | 18 +++++++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 6aaa7bfc5..b0ffb7a40 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -273,11 +273,11 @@ void HandleSIGHUP(int) fReopenDebugLog = true; } -bool static Bind(const CService &addr, unsigned int flags) { +bool static Bind(CConnman& connman, const CService &addr, unsigned int flags) { if (!(flags & BF_EXPLICIT) && IsLimited(addr)) return false; std::string strError; - if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) { + if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) { if (flags & BF_REPORT_ERROR) return InitError(strError); return false; @@ -1198,7 +1198,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) CService addrBind; if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) return InitError(ResolveErrMsg("bind", strBind)); - fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR)); + fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR)); } BOOST_FOREACH(const std::string& strBind, mapMultiArgs["-whitebind"]) { CService addrBind; @@ -1206,14 +1206,14 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) return InitError(ResolveErrMsg("whitebind", strBind)); if (addrBind.GetPort() == 0) return InitError(strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind)); - fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST)); + fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR | BF_WHITELIST)); } } else { struct in_addr inaddr_any; inaddr_any.s_addr = INADDR_ANY; - fBound |= Bind(CService(in6addr_any, GetListenPort()), BF_NONE); - fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE); + fBound |= Bind(connman, CService(in6addr_any, GetListenPort()), BF_NONE); + fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE); } if (!fBound) return InitError(_("Failed to listen on any port. Use -listen=0 if you want this.")); diff --git a/src/net.cpp b/src/net.cpp index b99989b28..006982000 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -84,7 +84,6 @@ std::map mapLocalHost; static bool vfLimited[NET_MAX] = {}; static CNode* pnodeLocalHost = NULL; uint64_t nLocalHostNonce = 0; -static std::vector vhListenSocket; CAddrMan addrman; int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS; bool fAddressesInitialized = false; @@ -1908,7 +1907,7 @@ void CConnman::ThreadMessageHandler() -bool BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted) +bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, bool fWhitelisted) { strError = ""; int nOne = 1; diff --git a/src/net.h b/src/net.h index d2a38e7dc..4d87408e3 100644 --- a/src/net.h +++ b/src/net.h @@ -95,13 +95,6 @@ CNode* FindNode(const CService& ip); CNode* FindNode(const NodeId id); //TODO: Remove this bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false); -struct ListenSocket { - SOCKET socket; - bool whitelisted; - - ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {} -}; - class CConnman { public: @@ -109,7 +102,16 @@ public: ~CConnman(); bool Start(boost::thread_group& threadGroup, std::string& strNodeError); void Stop(); + bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); + private: + struct ListenSocket { + SOCKET socket; + bool whitelisted; + + ListenSocket(SOCKET socket_, bool whitelisted_) : socket(socket_), whitelisted(whitelisted_) {} + }; + void ThreadOpenAddedConnections(); void ProcessOneShot(); void ThreadOpenConnections(); @@ -117,6 +119,8 @@ private: void AcceptConnection(const ListenSocket& hListenSocket); void ThreadSocketHandler(); void ThreadDNSAddressSeed(); + + std::vector vhListenSocket; }; extern std::unique_ptr g_connman; void MapPort(bool fUseUPnP);