net: Minor accumulated cleanups
This commit is contained in:
parent
07825088f9
commit
2c084a6609
6 changed files with 36 additions and 68 deletions
|
@ -42,12 +42,9 @@
|
||||||
|
|
||||||
void WaitForShutdown()
|
void WaitForShutdown()
|
||||||
{
|
{
|
||||||
bool fShutdown = ShutdownRequested();
|
while (!ShutdownRequested())
|
||||||
// Tell the main threads to shutdown.
|
|
||||||
while (!fShutdown)
|
|
||||||
{
|
{
|
||||||
MilliSleep(200);
|
MilliSleep(200);
|
||||||
fShutdown = ShutdownRequested();
|
|
||||||
}
|
}
|
||||||
Interrupt();
|
Interrupt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,6 +96,12 @@ typedef int32_t ssize_t;
|
||||||
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;
|
||||||
|
|
27
src/net.cpp
27
src/net.cpp
|
@ -1923,23 +1923,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;
|
||||||
|
@ -2088,23 +2090,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;
|
||||||
|
|
|
@ -14,7 +14,7 @@ static const unsigned char pchOnionCat[] = {0xFD,0x87,0xD8,0x7E,0xEB,0x43};
|
||||||
// 0xFD + sha256("bitcoin")[0:5]
|
// 0xFD + sha256("bitcoin")[0:5]
|
||||||
static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
|
static const unsigned char g_internal_prefix[] = { 0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 };
|
||||||
|
|
||||||
void CNetAddr::Init()
|
CNetAddr::CNetAddr()
|
||||||
{
|
{
|
||||||
memset(ip, 0, sizeof(ip));
|
memset(ip, 0, sizeof(ip));
|
||||||
scopeId = 0;
|
scopeId = 0;
|
||||||
|
@ -67,11 +67,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);
|
||||||
|
@ -290,11 +285,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);
|
||||||
|
@ -469,14 +459,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)
|
||||||
|
@ -525,11 +509,6 @@ bool operator==(const CService& a, const CService& b)
|
||||||
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
|
return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const CService& a, const CService& b)
|
|
||||||
{
|
|
||||||
return static_cast<CNetAddr>(a) != static_cast<CNetAddr>(b) || a.port != b.port;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<(const CService& a, const CService& b)
|
bool operator<(const CService& a, const CService& b)
|
||||||
{
|
{
|
||||||
return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
|
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
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -724,11 +703,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));
|
||||||
|
|
|
@ -38,15 +38,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.
|
||||||
|
@ -87,7 +88,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;
|
||||||
|
@ -124,7 +125,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;
|
||||||
|
@ -148,12 +149,11 @@ 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();
|
|
||||||
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() const;
|
std::string ToString() const;
|
||||||
|
|
|
@ -513,11 +513,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;
|
||||||
|
|
Loading…
Reference in a new issue