Skip to content

Commit 2c084a6

Browse files
committed
net: Minor accumulated cleanups
1 parent 0782508 commit 2c084a6

File tree

6 files changed

+36
-68
lines changed

6 files changed

+36
-68
lines changed

src/bitcoind.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,9 @@
4242

4343
void WaitForShutdown()
4444
{
45-
bool fShutdown = ShutdownRequested();
46-
// Tell the main threads to shutdown.
47-
while (!fShutdown)
45+
while (!ShutdownRequested())
4846
{
4947
MilliSleep(200);
50-
fShutdown = ShutdownRequested();
5148
}
5249
Interrupt();
5350
}

src/compat.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ typedef int32_t ssize_t;
9696
size_t strnlen( const char *start, size_t max_len);
9797
#endif // HAVE_DECL_STRNLEN
9898

99+
#ifndef WIN32
100+
typedef void* sockopt_arg_type;
101+
#else
102+
typedef char* sockopt_arg_type;
103+
#endif
104+
99105
bool static inline IsSelectableSocket(const SOCKET& s) {
100106
#ifdef WIN32
101107
return true;

src/net.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,23 +1923,25 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
19231923

19241924
for (const std::string& strAddNode : lAddresses) {
19251925
CService service(LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
1926+
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
19261927
if (service.IsValid()) {
19271928
// strAddNode is an IP:port
19281929
auto it = mapConnected.find(service);
19291930
if (it != mapConnected.end()) {
1930-
ret.push_back(AddedNodeInfo{strAddNode, service, true, it->second});
1931-
} else {
1932-
ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1931+
addedNode.resolvedAddress = service;
1932+
addedNode.fConnected = true;
1933+
addedNode.fInbound = it->second;
19331934
}
19341935
} else {
19351936
// strAddNode is a name
19361937
auto it = mapConnectedByName.find(strAddNode);
19371938
if (it != mapConnectedByName.end()) {
1938-
ret.push_back(AddedNodeInfo{strAddNode, it->second.second, true, it->second.first});
1939-
} else {
1940-
ret.push_back(AddedNodeInfo{strAddNode, CService(), false, false});
1939+
addedNode.resolvedAddress = it->second.second;
1940+
addedNode.fConnected = true;
1941+
addedNode.fInbound = it->second.first;
19411942
}
19421943
}
1944+
ret.emplace_back(std::move(addedNode));
19431945
}
19441946

19451947
return ret;
@@ -2088,23 +2090,16 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
20882090
LogPrintf("%s\n", strError);
20892091
return false;
20902092
}
2091-
#ifndef WIN32
2093+
20922094
// Allow binding if the port is still in TIME_WAIT state after
20932095
// the program was closed and restarted.
2094-
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
2095-
#else
2096-
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
2097-
#endif
2096+
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (sockopt_arg_type)&nOne, sizeof(int));
20982097

20992098
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
21002099
// and enable it by default or not. Try to enable it, if possible.
21012100
if (addrBind.IsIPv6()) {
21022101
#ifdef IPV6_V6ONLY
2103-
#ifdef WIN32
2104-
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&nOne, sizeof(int));
2105-
#else
2106-
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&nOne, sizeof(int));
2107-
#endif
2102+
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int));
21082103
#endif
21092104
#ifdef WIN32
21102105
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;

src/netaddress.cpp

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
1414
// 0xFD + sha256("bitcoin")[0:5]
1515
static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
1616

17-
void CNetAddr::Init()
17+
CNetAddr::CNetAddr()
1818
{
1919
memset(ip, 0, sizeof(ip));
2020
scopeId = 0;
@@ -67,11 +67,6 @@ bool CNetAddr::SetSpecial(const std::string &strName)
6767
return false;
6868
}
6969

70-
CNetAddr::CNetAddr()
71-
{
72-
Init();
73-
}
74-
7570
CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
7671
{
7772
SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
@@ -290,11 +285,6 @@ bool operator==(const CNetAddr& a, const CNetAddr& b)
290285
return (memcmp(a.ip, b.ip, 16) == 0);
291286
}
292287

293-
bool operator!=(const CNetAddr& a, const CNetAddr& b)
294-
{
295-
return (memcmp(a.ip, b.ip, 16) != 0);
296-
}
297-
298288
bool operator<(const CNetAddr& a, const CNetAddr& b)
299289
{
300290
return (memcmp(a.ip, b.ip, 16) < 0);
@@ -469,14 +459,8 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
469459
}
470460
}
471461

472-
void CService::Init()
462+
CService::CService() : port(0)
473463
{
474-
port = 0;
475-
}
476-
477-
CService::CService()
478-
{
479-
Init();
480464
}
481465

482466
CService::CService(const CNetAddr& cip, unsigned short portIn) : CNetAddr(cip), port(portIn)
@@ -525,11 +509,6 @@ bool operator==(const CService& a, const CService& b)
525509
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
526510
}
527511

528-
bool operator!=(const CService& a, const CService& b)
529-
{
530-
return static_cast<CNetAddr>(a) != static_cast<CNetAddr>(b) || a.port != b.port;
531-
}
532-
533512
bool operator<(const CService& a, const CService& b)
534513
{
535514
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
@@ -663,16 +642,16 @@ bool CSubNet::Match(const CNetAddr &addr) const
663642
static inline int NetmaskBits(uint8_t x)
664643
{
665644
switch(x) {
666-
case 0x00: return 0; break;
667-
case 0x80: return 1; break;
668-
case 0xc0: return 2; break;
669-
case 0xe0: return 3; break;
670-
case 0xf0: return 4; break;
671-
case 0xf8: return 5; break;
672-
case 0xfc: return 6; break;
673-
case 0xfe: return 7; break;
674-
case 0xff: return 8; break;
675-
default: return -1; break;
645+
case 0x00: return 0;
646+
case 0x80: return 1;
647+
case 0xc0: return 2;
648+
case 0xe0: return 3;
649+
case 0xf0: return 4;
650+
case 0xf8: return 5;
651+
case 0xfc: return 6;
652+
case 0xfe: return 7;
653+
case 0xff: return 8;
654+
default: return -1;
676655
}
677656
}
678657

@@ -724,11 +703,6 @@ bool operator==(const CSubNet& a, const CSubNet& b)
724703
return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
725704
}
726705

727-
bool operator!=(const CSubNet& a, const CSubNet& b)
728-
{
729-
return !(a==b);
730-
}
731-
732706
bool operator<(const CSubNet& a, const CSubNet& b)
733707
{
734708
return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));

src/netaddress.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ class CNetAddr
3838
public:
3939
CNetAddr();
4040
explicit CNetAddr(const struct in_addr& ipv4Addr);
41-
void Init();
4241
void SetIP(const CNetAddr& ip);
4342

43+
private:
4444
/**
4545
* Set raw IPv4 or IPv6 address (in network byte order)
4646
* @note Only NET_IPV4 and NET_IPV6 are allowed for network.
4747
*/
4848
void SetRaw(Network network, const uint8_t *data);
4949

50+
public:
5051
/**
5152
* Transform an arbitrary string into a non-routable ipv6 address.
5253
* Useful for mapping resolved addresses back to their source.
@@ -87,7 +88,7 @@ class CNetAddr
8788
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
8889

8990
friend bool operator==(const CNetAddr& a, const CNetAddr& b);
90-
friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
91+
friend bool operator!=(const CNetAddr& a, const CNetAddr& b) { return !(a == b); }
9192
friend bool operator<(const CNetAddr& a, const CNetAddr& b);
9293

9394
ADD_SERIALIZE_METHODS;
@@ -124,7 +125,7 @@ class CSubNet
124125
bool IsValid() const;
125126

126127
friend bool operator==(const CSubNet& a, const CSubNet& b);
127-
friend bool operator!=(const CSubNet& a, const CSubNet& b);
128+
friend bool operator!=(const CSubNet& a, const CSubNet& b) { return !(a == b); }
128129
friend bool operator<(const CSubNet& a, const CSubNet& b);
129130

130131
ADD_SERIALIZE_METHODS;
@@ -148,12 +149,11 @@ class CService : public CNetAddr
148149
CService(const CNetAddr& ip, unsigned short port);
149150
CService(const struct in_addr& ipv4Addr, unsigned short port);
150151
explicit CService(const struct sockaddr_in& addr);
151-
void Init();
152152
unsigned short GetPort() const;
153153
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
154154
bool SetSockAddr(const struct sockaddr* paddr);
155155
friend bool operator==(const CService& a, const CService& b);
156-
friend bool operator!=(const CService& a, const CService& b);
156+
friend bool operator!=(const CService& a, const CService& b) { return !(a == b); }
157157
friend bool operator<(const CService& a, const CService& b);
158158
std::vector<unsigned char> GetKey() const;
159159
std::string ToString() const;

src/netbase.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
513513
return false;
514514
}
515515
socklen_t nRetSize = sizeof(nRet);
516-
#ifdef WIN32
517-
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
518-
#else
519-
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
520-
#endif
516+
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
521517
{
522518
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
523519
return false;

0 commit comments

Comments
 (0)