Skip to content

Commit

Permalink
net: Move socket binding into CConnman
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and Fuzzbawls committed Aug 25, 2020
1 parent 7762b97 commit 573200f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ static void registerSignalHandler(int signal, void(*handler)(int))
}
#endif

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 UIError(strError);
return false;
Expand Down Expand Up @@ -1393,21 +1393,21 @@ bool AppInit2()
CService addrBind;
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
return UIError(ResolveErrMsg("bind", strBind));
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
fBound |= Bind(connman, addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
}
for (std::string strBind : mapMultiArgs["-whitebind"]) {
CService addrBind;
if (!Lookup(strBind.c_str(), addrBind, 0, false))
return UIError(ResolveErrMsg("whitebind", strBind));
if (addrBind.GetPort() == 0)
return UIError(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((in6_addr)IN6ADDR_ANY_INIT, GetListenPort()), BF_NONE);
fBound |= Bind(CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
fBound |= Bind(connman, CService((in6_addr)IN6ADDR_ANY_INIT, GetListenPort()), BF_NONE);
fBound |= Bind(connman, CService(inaddr_any, GetListenPort()), !fBound ? BF_REPORT_ERROR : BF_NONE);
}
if (!fBound)
return UIError(_("Failed to listen on any port. Use -listen=0 if you want this."));
Expand Down
3 changes: 1 addition & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL;
uint64_t nLocalHostNonce = 0;
static std::vector<ListenSocket> vhListenSocket;
CAddrMan addrman;
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
bool fAddressesInitialized = false;
Expand Down Expand Up @@ -1826,7 +1825,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;
Expand Down
18 changes: 11 additions & 7 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,32 @@ CNode* FindNode(const std::string& addrName);
CNode* FindNode(const CService& ip);
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:
CConnman();
~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();
void ThreadMessageHandler();
void AcceptConnection(const ListenSocket& hListenSocket);
void ThreadSocketHandler();
void ThreadDNSAddressSeed();

std::vector<ListenSocket> vhListenSocket;
};
extern std::unique_ptr<CConnman> g_connman;
void MapPort(bool fUseUPnP);
Expand Down

0 comments on commit 573200f

Please sign in to comment.