Merge #12855: net: Minor accumulated cleanups

2c084a6 net: Minor accumulated cleanups (Thomas Snider)

Pull request description:

  From now-derelict larger changes I had been working on, here are a series of DRY refactors/cleanups.  Net loss of 35 lines of code - a small step in the good fight.

  In particular I think operator!= should only ever be implemented as a negation of operator==.  Lower chance for errors, and removes the possibility of divergent behavior.

Tree-SHA512: 58bf4b542a4e8e5bc465b508aaa16e9ab51448c3f9bee52cd9db0a64a5c6c5a13e4b4286d0a5aa864934fc58064799f6a88a40a87154fd3a4bd731a72e254393
This commit is contained in:
Wladimir J. van der Laan 2018-04-19 14:24:26 +02:00 committed by Pasta
parent a3ea0e93ef
commit e7beff5bcd
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
6 changed files with 36 additions and 68 deletions

View File

@ -86,6 +86,12 @@ typedef unsigned int SOCKET;
size_t strnlen( const char *start, size_t max_len); size_t strnlen( const char *start, size_t max_len);
#endif // HAVE_DECL_STRNLEN #endif // HAVE_DECL_STRNLEN
#ifndef WIN32
typedef void* sockopt_arg_type;
#else
typedef char* sockopt_arg_type;
#endif
bool static inline IsSelectableSocket(const SOCKET& s) { bool static inline IsSelectableSocket(const SOCKET& s) {
#ifdef WIN32 #ifdef WIN32
return true; return true;

View File

@ -44,12 +44,9 @@
void WaitForShutdown(boost::thread_group* threadGroup) void WaitForShutdown(boost::thread_group* threadGroup)
{ {
bool fShutdown = ShutdownRequested(); while (!ShutdownRequested())
// Tell the main threads to shutdown.
while (!fShutdown)
{ {
MilliSleep(200); MilliSleep(200);
fShutdown = ShutdownRequested();
} }
if (threadGroup) if (threadGroup)
{ {

View File

@ -2067,23 +2067,25 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
for (const std::string& strAddNode : lAddresses) { for (const std::string& strAddNode : lAddresses) {
CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort())); CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
if (service.IsValid()) { if (service.IsValid()) {
// strAddNode is an IP:port // strAddNode is an IP:port
auto it = mapConnected.find(service); auto it = mapConnected.find(service);
if (it != mapConnected.end()) { if (it != mapConnected.end()) {
ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second}); addedNode.resolvedAddress = service;
} else { addedNode.fConnected = true;
ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false}); addedNode.fInbound = it->second;
} }
} else { } else {
// strAddNode is a name // strAddNode is a name
auto it = mapConnectedByName.find(strAddNode); auto it = mapConnectedByName.find(strAddNode);
if (it != mapConnectedByName.end()) { if (it != mapConnectedByName.end()) {
ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first}); addedNode.resolvedAddress = it->second.second;
} else { addedNode.fConnected = true;
ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false}); addedNode.fInbound = it->second.first;
} }
} }
ret.emplace_back(std::move(addedNode));
} }
return ret; return ret;
@ -2316,23 +2318,16 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
LogPrintf("%s\n", strError); LogPrintf("%s\n", strError);
return false; return false;
} }
#ifndef WIN32
// Allow binding if the port is still in TIME_WAIT state after // Allow binding if the port is still in TIME_WAIT state after
// the program was closed and restarted. // the program was closed and restarted.
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int)); setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int));
#else
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
#endif
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option // 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. // and enable it by default or not. Try to enable it, if possible.
if (addrBind.IsIPv6()) { if (addrBind.IsIPv6()) {
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
#ifdef WIN32 setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int));
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
#else
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
#endif
#endif #endif
#ifdef WIN32 #ifdef WIN32
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED; int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;

View File

@ -21,7 +21,7 @@ static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87,
bool fAllowPrivateNet = DEFAULT_ALLOWPRIVATENET; bool fAllowPrivateNet = DEFAULT_ALLOWPRIVATENET;
void CNetAddr::Init() CNetAddr::CNetAddr()
{ {
memset(ip, 0, sizeof(ip)); memset(ip, 0, sizeof(ip));
scopeId = 0; scopeId = 0;
@ -74,11 +74,6 @@ bool CNetAddr::SetSpecial(const std::string &strName)
return false; return false;
} }
CNetAddr::CNetAddr()
{
Init();
}
CNetAddr::CNetAddr(const struct in_addr& ipv4Addr) CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
{ {
SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr); SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
@ -304,11 +299,6 @@ bool operator==(const CNetAddr& a, const CNetAddr& b)
return (memcmp(a.ip, b.ip, 16) == 0); 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) bool operator<(const CNetAddr& a, const CNetAddr& b)
{ {
return (memcmp(a.ip, b.ip, 16) < 0); return (memcmp(a.ip, b.ip, 16) < 0);
@ -483,14 +473,8 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
} }
} }
void CService::Init() CService::CService() : port(0)
{ {
port = 0;
}
CService::CService()
{
Init();
} }
CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn) CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
@ -539,11 +523,6 @@ bool operator==(const CService& a, const CService& b)
return (CNetAddr)a == (CNetAddr)b && a.port == b.port; 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) bool operator<(const CService& a, const CService& b)
{ {
return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port); return (CNetAddr)a < (CNetAddr)b || ((CNetAddr)a == (CNetAddr)b && a.port < b.port);
@ -682,16 +661,16 @@ bool CSubNet::Match(const CNetAddr &addr) const
static inline int NetmaskBits(uint8_t x) static inline int NetmaskBits(uint8_t x)
{ {
switch(x) { switch(x) {
case 0x00: return 0; break; case 0x00: return 0;
case 0x80: return 1; break; case 0x80: return 1;
case 0xc0: return 2; break; case 0xc0: return 2;
case 0xe0: return 3; break; case 0xe0: return 3;
case 0xf0: return 4; break; case 0xf0: return 4;
case 0xf8: return 5; break; case 0xf8: return 5;
case 0xfc: return 6; break; case 0xfc: return 6;
case 0xfe: return 7; break; case 0xfe: return 7;
case 0xff: return 8; break; case 0xff: return 8;
default: return -1; break; default: return -1;
} }
} }
@ -743,11 +722,6 @@ bool operator==(const CSubNet& a, const CSubNet& b)
return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16); return a.valid == b.valid && a.network == b.network && !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) bool operator<(const CSubNet& a, const CSubNet& b)
{ {
return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));

View File

@ -39,15 +39,16 @@ class CNetAddr
public: public:
CNetAddr(); CNetAddr();
explicit CNetAddr(const struct in_addr& ipv4Addr); explicit CNetAddr(const struct in_addr& ipv4Addr);
void Init();
void SetIP(const CNetAddr& ip); void SetIP(const CNetAddr& ip);
private:
/** /**
* Set raw IPv4 or IPv6 address (in network byte order) * Set raw IPv4 or IPv6 address (in network byte order)
* @note Only NET_IPV4 and NET_IPV6 are allowed for network. * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
*/ */
void SetRaw(Network network, const uint8_t *data); void SetRaw(Network network, const uint8_t *data);
public:
/** /**
* Transform an arbitrary string into a non-routable ipv6 address. * Transform an arbitrary string into a non-routable ipv6 address.
* Useful for mapping resolved addresses back to their source. * Useful for mapping resolved addresses back to their source.
@ -88,7 +89,7 @@ class CNetAddr
bool GetIn6Addr(struct in6_addr* pipv6Addr) const; 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); friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
friend bool operator<(const CNetAddr& a, const CNetAddr& b); friend bool operator<(const CNetAddr& a, const CNetAddr& b);
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;
@ -125,7 +126,7 @@ class CSubNet
bool IsValid() const; 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); friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
friend bool operator<(const CSubNet& a, const CSubNet& b); friend bool operator<(const CSubNet& a, const CSubNet& b);
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;
@ -149,13 +150,12 @@ class CService : public CNetAddr
CService(const CNetAddr& ip, unsigned short port); CService(const CNetAddr& ip, unsigned short port);
CService(const struct in_addr& ipv4Addr, unsigned short port); CService(const struct in_addr& ipv4Addr, unsigned short port);
explicit CService(const struct sockaddr_in& addr); explicit CService(const struct sockaddr_in& addr);
void Init();
void SetPort(unsigned short portIn); void SetPort(unsigned short portIn);
unsigned short GetPort() const; unsigned short GetPort() const;
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
bool SetSockAddr(const struct sockaddr* paddr); 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); friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
friend bool operator<(const CService& a, const CService& b); friend bool operator<(const CService& a, const CService& b);
std::vector<unsigned char> GetKey() const; std::vector<unsigned char> GetKey() const;
std::string ToString(bool fUseGetnameinfo = true) const; std::string ToString(bool fUseGetnameinfo = true) const;

View File

@ -506,11 +506,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
return false; return false;
} }
socklen_t nRetSize = sizeof(nRet); socklen_t nRetSize = sizeof(nRet);
#ifdef WIN32 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
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
{ {
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError())); LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
return false; return false;