#include "netbase.h"
#include "util.h"
#include "sync.h"
+#include "hash.h"
#ifndef WIN32
+#ifdef ANDROID
+#include <fcntl.h>
+#else
#include <sys/fcntl.h>
#endif
+#endif
+
+#ifdef _MSC_VER
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#endif
-#include "strlcpy.h"
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
+#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
using namespace std;
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;
struct addrinfo *aiTrav = aiRes;
while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
{
- if (aiTrav->ai_family == AF_INET)
+ switch (aiTrav->ai_family)
{
- assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
- vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
- }
+ case (AF_INET):
+ assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
+ vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
+ break;
#ifdef USE_IPV6
- if (aiTrav->ai_family == AF_INET6)
- {
- assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
- vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
- }
+ case (AF_INET6):
+ assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
+ vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr));
+ break;
#endif
+ }
aiTrav = aiTrav->ai_next;
}
bool LookupHost(const char *pszName, std::vector<CNetAddr>& 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<CNetAddr>& vIP, unsigned int nMaxSolutions)
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);
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;
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);
return false;
break;
default:
+ closesocket(hSocket);
return false;
}
switch(nameproxy.second) {
default:
- case 4: return false;
+ case 4:
+ closesocket(hSocket);
+ return false;
case 5:
if (!Socks5(strDest, port, hSocket))
return false;
void CNetAddr::Init()
{
- memset(ip, 0, 16);
+ memset(ip, 0, sizeof(ip));
}
void CNetAddr::SetIP(const CNetAddr& ipIn)
nBits = 4;
}
// for he.net, use /36 groups
- else if (GetByte(15) == 0x20 && GetByte(14) == 0x11 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
+ else if (GetByte(15) == 0x20 && GetByte(14) == 0x01 && GetByte(13) == 0x04 && GetByte(12) == 0x70)
nBits = 36;
// for the rest of the IPv6 network, use /32 groups
else
return vchRet;
}
-uint64 CNetAddr::GetHash() const
+uint64_t CNetAddr::GetHash() const
{
uint256 hash = Hash(&ip[0], &ip[16]);
- uint64 nRet;
+ uint64_t nRet;
memcpy(&nRet, &hash, sizeof(nRet));
return nRet;
}