Skip to content

Commit

Permalink
net: Minor accumulated cleanups
Browse files Browse the repository at this point in the history
Adaptation of btc@2c084a6609bed24979c2a144743007f8b10a5c70
  • Loading branch information
furszy committed Aug 10, 2021
1 parent 9f9c871 commit 31064a8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 37 deletions.
6 changes: 6 additions & 0 deletions src/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ typedef u_int SOCKET;
size_t strnlen( const char *start, size_t max_len);
#endif // HAVE_DECL_STRNLEN

#ifndef WIN32
typedef void* sockopt_arg_type;
#else
typedef char* sockopt_arg_type;
#endif

bool static inline IsSelectableSocket(SOCKET s)
{
#ifdef WIN32
Expand Down
22 changes: 10 additions & 12 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,25 +1640,27 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
}
}

for (std::string& strAddNode : lAddresses) {
for (const std::string& strAddNode : lAddresses) {
CService service(LookupNumeric(strAddNode, 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.emplace_back(strAddNode, service, true, it->second);
} else {
ret.emplace_back(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.emplace_back(strAddNode, it->second.second, true, it->second.first);
} else {
ret.emplace_back(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;
Expand Down Expand Up @@ -1824,11 +1826,7 @@ bool CConnman::BindListenPort(const CService& addrBind, std::string& strError, b
// 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
setsockopt(hListenSocket, IPPROTO_IPV6, IPV6_V6ONLY, (sockopt_arg_type)&nOne, sizeof(int));
#endif
#ifdef WIN32
int nProtLevel = PROTECTION_LEVEL_UNRESTRICTED;
Expand Down
15 changes: 2 additions & 13 deletions src/netaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
// 0xFD + sha256("bitcoin")[0:5]
static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };

void CNetAddr::Init()
CNetAddr::CNetAddr()
{
memset(ip, 0, sizeof(ip));
}
Expand Down Expand Up @@ -88,11 +88,6 @@ bool CNetAddr::SetSpecial(const std::string& strName)
return false;
}

CNetAddr::CNetAddr()
{
Init();
}

CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
{
SetRaw(NET_IPV4, (const uint8_t*)&ipv4Addr);
Expand Down Expand Up @@ -569,14 +564,8 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
}
}

void CService::Init()
{
port = 0;
}

CService::CService()
CService::CService() : port(0)
{
Init();
}

CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
Expand Down
4 changes: 1 addition & 3 deletions src/netaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ class CNetAddr

public:
CNetAddr();
CNetAddr(const struct in_addr& ipv4Addr);
void Init();
explicit CNetAddr(const struct in_addr& ipv4Addr);
void SetIP(const CNetAddr& ip);

/**
Expand Down Expand Up @@ -208,7 +207,6 @@ class CService : public CNetAddr
CService(const CNetAddr& ip, uint16_t port);
CService(const struct in_addr& ipv4Addr, uint16_t port);
CService(const struct sockaddr_in& addr);
void Init();
uint16_t GetPort() const;
bool GetSockAddr(struct sockaddr* paddr, socklen_t* addrlen) const;
bool SetSockAddr(const struct sockaddr* paddr);
Expand Down
6 changes: 1 addition & 5 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,7 @@ bool static ConnectSocketDirectly(const CService& addrConnect, SOCKET& hSocketRe
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()));
CloseSocket(hSocket);
Expand Down
5 changes: 1 addition & 4 deletions src/pivxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ static bool fDaemon;

void WaitForShutdown()
{
bool fShutdown = ShutdownRequested();
// Tell the main threads to shutdown.
while (!fShutdown) {
while (!ShutdownRequested()) {
MilliSleep(200);
fShutdown = ShutdownRequested();
}
Interrupt();
}
Expand Down

0 comments on commit 31064a8

Please sign in to comment.