X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fnetbase.cpp;h=320db3eb45a970cf9a558ad3511adb3e97738382;hb=afc2bcf340f842111978138a9e04e0b14e5de9fe;hp=67a8e89226f199da7ac96550744f51f9bf2dbdf2;hpb=60e774cc878cdeed2b9dccf2dff3aebe2ccb55cd;p=novacoin.git diff --git a/src/netbase.cpp b/src/netbase.cpp index 67a8e89..320db3e 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -21,8 +21,8 @@ typedef SSIZE_T ssize_t; #endif -#include "strlcpy.h" #include // for to_lower() +#include // for startswith() and endswith() using namespace std; @@ -82,19 +82,14 @@ bool static LookupIntern(const char *pszName, std::vector& vIP, unsign aiHint.ai_socktype = SOCK_STREAM; aiHint.ai_protocol = IPPROTO_TCP; -#ifdef WIN32 -# ifdef USE_IPV6 +#ifdef USE_IPV6 aiHint.ai_family = AF_UNSPEC; -# else +#else aiHint.ai_family = AF_INET; -# endif +#endif +#ifdef WIN32 aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST; #else -# ifdef USE_IPV6 - aiHint.ai_family = AF_UNSPEC; -# else - aiHint.ai_family = AF_INET; -# endif aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; #endif struct addrinfo *aiRes = NULL; @@ -130,18 +125,15 @@ bool static LookupIntern(const char *pszName, std::vector& vIP, unsign bool LookupHost(const char *pszName, std::vector& vIP, unsigned int nMaxSolutions, bool fAllowLookup) { - if (pszName[0] == 0) + std::string strHost(pszName); + if (strHost.empty()) return false; - char psz[256]; - char *pszHost = psz; - strlcpy(psz, pszName, sizeof(psz)); - if (psz[0] == '[' && psz[strlen(psz)-1] == ']') + if (boost::algorithm::starts_with(strHost, "[") && boost::algorithm::ends_with(strHost, "]")) { - pszHost = psz+1; - psz[strlen(psz)-1] = 0; + strHost = strHost.substr(1, strHost.size() - 2); } - return LookupIntern(pszHost, vIP, nMaxSolutions, fAllowLookup); + return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup); } bool LookupHostNumeric(const char *pszName, std::vector& vIP, unsigned int nMaxSolutions) @@ -235,10 +227,9 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket) return error("Hostname too long"); } char pszSocks5Init[] = "\5\1\0"; - char *pszSocks5 = pszSocks5Init; ssize_t nSize = sizeof(pszSocks5Init) - 1; - ssize_t ret = send(hSocket, pszSocks5, nSize, MSG_NOSIGNAL); + ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL); if (ret != nSize) { closesocket(hSocket); @@ -307,8 +298,10 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket) case 0x03: { ret = recv(hSocket, pchRet3, 1, 0) != 1; - if (ret) + if (ret) { + closesocket(hSocket); return error("Error reading from proxy"); + } int nRecv = pchRet3[0]; ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv; break; @@ -427,7 +420,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR) #else fFlags = fcntl(hSocket, F_GETFL, 0); - if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR) + if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) #endif { closesocket(hSocket); @@ -515,6 +508,7 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout) return false; break; default: + closesocket(hSocket); return false; } @@ -546,7 +540,9 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest switch(nameproxy.second) { default: - case 4: return false; + case 4: + closesocket(hSocket); + return false; case 5: if (!Socks5(strDest, port, hSocket)) return false; @@ -559,7 +555,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest void CNetAddr::Init() { - memset(ip, 0, 16); + memset(ip, 0, sizeof(ip)); } void CNetAddr::SetIP(const CNetAddr& ipIn) @@ -627,7 +623,7 @@ CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup) *this = vIP[0]; } -unsigned int CNetAddr::GetByte(int n) const +uint8_t CNetAddr::GetByte(int n) const { return ip[15-n]; } @@ -855,7 +851,7 @@ bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const std::vector CNetAddr::GetGroup() const { std::vector vchRet; - int nClass = NET_IPV6; + uint8_t nClass = NET_IPV6; int nStartByte = 0; int nBits = 16;