diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -47,11 +47,8 @@ */ void WaitForShutdown(boost::thread_group *threadGroup) { - bool fShutdown = ShutdownRequested(); - // Tell the main threads to shutdown. - while (!fShutdown) { + while (!ShutdownRequested()) { MilliSleep(200); - fShutdown = ShutdownRequested(); } if (threadGroup) { Interrupt(*threadGroup); diff --git a/src/compat.h b/src/compat.h --- a/src/compat.h +++ b/src/compat.h @@ -86,7 +86,13 @@ size_t strnlen(const char *start, size_t max_len); #endif // HAVE_DECL_STRNLEN -static bool inline IsSelectableSocket(SOCKET s) { +#ifndef WIN32 +typedef void *sockopt_arg_type; +#else +typedef char *sockopt_arg_type; +#endif + +static bool inline IsSelectableSocket(const SOCKET s) { #ifdef WIN32 return true; #else diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -2050,29 +2050,27 @@ } for (const std::string &strAddNode : lAddresses) { - CService service(LookupNumeric( - strAddNode.c_str(), config->GetChainParams().GetDefaultPort())); + CService service( + LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort())); + AddedNodeInfo addedNode{strAddNode, CService(), false, false}; if (service.IsValid()) { // strAddNode is an IP:port auto it = mapConnected.find(service); if (it != mapConnected.end()) { - ret.push_back( - AddedNodeInfo{strAddNode, service, true, it->second}); - } else { - ret.push_back( - AddedNodeInfo{strAddNode, CService(), false, false}); + addedNode.resolvedAddress = service; + addedNode.fConnected = true; + addedNode.fInbound = it->second; } } else { // strAddNode is a name auto it = mapConnectedByName.find(strAddNode); if (it != mapConnectedByName.end()) { - ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, - it->second.first}); - } else { - ret.push_back( - AddedNodeInfo{strAddNode, CService(), false, false}); + addedNode.resolvedAddress = it->second.second; + addedNode.fConnected = true; + addedNode.fInbound = it->second.first; } } + ret.emplace_back(std::move(addedNode)); } return ret; @@ -2238,33 +2236,23 @@ LogPrintf("%s\n", strError); return false; } -#ifndef WIN32 // Allow binding if the port is still in TIME_WAIT state after // the program was closed and restarted. - setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&nOne, - sizeof(int)); -#else - setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char *)&nOne, + setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int)); -#endif // Some systems don't have IPV6_V6ONLY but are always v6only; others do have // the option and enable it by default or not. Try to enable it, if // possible. if (addrBind.IsIPv6()) { #ifdef IPV6_V6ONLY -#ifdef WIN32 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, - (const char *)&nOne, sizeof(int)); -#else - setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&nOne, - sizeof(int)); -#endif + (sockopt_arg_type)&nOne, sizeof(int)); #endif #ifdef WIN32 int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, - (const char *)&nProtLevel, sizeof(int)); + (sockopt_arg_type)&nProtLevel, sizeof(int)); #endif } diff --git a/src/netaddress.h b/src/netaddress.h --- a/src/netaddress.h +++ b/src/netaddress.h @@ -37,15 +37,16 @@ public: CNetAddr(); CNetAddr(const struct in_addr &ipv4Addr); - void Init(); void SetIP(const CNetAddr &ip); +private: /** * Set raw IPv4 or IPv6 address (in network byte order) * @note Only NET_IPV4 and NET_IPV6 are allowed for network. */ void SetRaw(Network network, const uint8_t *data); +public: /** * Transform an arbitrary string into a non-routable ipv6 address. * Useful for mapping resolved addresses back to their source. @@ -103,7 +104,9 @@ bool GetIn6Addr(struct in6_addr *pipv6Addr) const; friend bool operator==(const CNetAddr &a, const CNetAddr &b); - friend bool operator!=(const CNetAddr &a, const CNetAddr &b); + friend bool operator!=(const CNetAddr &a, const CNetAddr &b) { + return !(a == b); + } friend bool operator<(const CNetAddr &a, const CNetAddr &b); ADD_SERIALIZE_METHODS; @@ -139,7 +142,9 @@ bool IsValid() const; friend bool operator==(const CSubNet &a, const CSubNet &b); - friend bool operator!=(const CSubNet &a, const CSubNet &b); + friend bool operator!=(const CSubNet &a, const CSubNet &b) { + return !(a == b); + } friend bool operator<(const CSubNet &a, const CSubNet &b); ADD_SERIALIZE_METHODS; @@ -163,13 +168,14 @@ CService(const CNetAddr &ip, unsigned short port); CService(const struct in_addr &ipv4Addr, unsigned short port); CService(const struct sockaddr_in &addr); - void Init(); void SetPort(unsigned short portIn); unsigned short GetPort() const; bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const; bool SetSockAddr(const struct sockaddr *paddr); friend bool operator==(const CService &a, const CService &b); - friend bool operator!=(const CService &a, const CService &b); + friend bool operator!=(const CService &a, const CService &b) { + return !(a == b); + } friend bool operator<(const CService &a, const CService &b); std::vector GetKey() const; std::string ToString() const; diff --git a/src/netaddress.cpp b/src/netaddress.cpp --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -18,7 +18,7 @@ // 0xFD + sha256("bitcoin")[0:5] static const uint8_t g_internal_prefix[] = {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24}; -void CNetAddr::Init() { +CNetAddr::CNetAddr() { memset(ip, 0, sizeof(ip)); scopeId = 0; } @@ -70,10 +70,6 @@ return false; } -CNetAddr::CNetAddr() { - Init(); -} - CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) { SetRaw(NET_IPV4, (const uint8_t *)&ipv4Addr); } @@ -282,10 +278,6 @@ return (memcmp(a.ip, b.ip, 16) == 0); } -bool operator!=(const CNetAddr &a, const CNetAddr &b) { - return (memcmp(a.ip, b.ip, 16) != 0); -} - bool operator<(const CNetAddr &a, const CNetAddr &b) { return (memcmp(a.ip, b.ip, 16) < 0); } @@ -463,13 +455,7 @@ } } -void CService::Init() { - port = 0; -} - -CService::CService() { - Init(); -} +CService::CService() : port(0) {} CService::CService(const CNetAddr &cip, unsigned short portIn) : CNetAddr(cip), port(portIn) {} @@ -512,13 +498,10 @@ return (CNetAddr)a == (CNetAddr)b && a.port == b.port; } -bool operator!=(const CService &a, const CService &b) { - return (CNetAddr)a != (CNetAddr)b || a.port != b.port; -} - bool operator<(const CService &a, const CService &b) { - return (CNetAddr)a < (CNetAddr)b || - ((CNetAddr)a == (CNetAddr)b && a.port < b.port); + return static_cast(a) < static_cast(b) || + (static_cast(a) == static_cast(b) && + a.port < b.port); } bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const { @@ -640,34 +623,24 @@ switch (x) { case 0x00: return 0; - break; case 0x80: return 1; - break; case 0xc0: return 2; - break; case 0xe0: return 3; - break; case 0xf0: return 4; - break; case 0xf8: return 5; - break; case 0xfc: return 6; - break; case 0xfe: return 7; - break; case 0xff: return 8; - break; default: return -1; - break; } } @@ -718,10 +691,6 @@ !memcmp(a.netmask, b.netmask, 16); } -bool operator!=(const CSubNet &a, const CSubNet &b) { - return !(a == b); -} - bool operator<(const CSubNet &a, const CSubNet &b) { return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); diff --git a/src/netbase.cpp b/src/netbase.cpp --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -484,7 +484,8 @@ #ifdef SO_NOSIGPIPE int set = 1; // Different way of disabling SIGPIPE on BSD - setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int)); + setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (sockopt_arg_type)&set, + sizeof(int)); #endif // Disable Nagle's algorithm @@ -536,14 +537,9 @@ return false; } socklen_t nRetSize = sizeof(nRet); -#ifdef WIN32 - if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char *)(&nRet), - &nRetSize) == SOCKET_ERROR) -#else - if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == - SOCKET_ERROR) -#endif - { + if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, + (sockopt_arg_type)&nRet, + &nRetSize) == SOCKET_ERROR) { LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError())); @@ -740,8 +736,8 @@ bool SetSocketNoDelay(SOCKET &hSocket) { int set = 1; - int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char *)&set, - sizeof(int)); + int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, + (sockopt_arg_type)&set, sizeof(int)); return rc == 0; }