Switch to builtin tolower
[novacoin.git] / src / netbase.cpp
index a9caddf..ebc5f9f 100644 (file)
@@ -21,9 +21,6 @@
 typedef SSIZE_T ssize_t;
 #endif
 
-#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
-#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
-
 using namespace std;
 
 // Settings
@@ -36,7 +33,7 @@ bool fNameLookup = false;
 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
 
 enum Network ParseNetwork(std::string net) {
-    boost::to_lower(net);
+    transform(net.begin(), net.end(), net.begin(), ::tolower);
     if (net == "ipv4") return NET_IPV4;
     if (net == "ipv6") return NET_IPV6;
     if (net == "tor" || net == "onion")  return NET_TOR;
@@ -45,7 +42,7 @@ enum Network ParseNetwork(std::string net) {
 }
 
 void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut) {
-    size_t colon = in.find_last_of(':');
+    auto colon = in.find_last_of(':');
     // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
     bool fHaveColon = colon != in.npos;
     bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
@@ -123,12 +120,12 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
     return (vIP.size() > 0);
 }
 
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
+bool LookupHost(const string& strName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
 {
-    std::string strHost(pszName);
+    std::string strHost(strName);
     if (strHost.empty())
         return false;
-    if (boost::algorithm::starts_with(strHost, "[") && boost::algorithm::ends_with(strHost, "]"))
+    if ((strHost.front() == '[') && (strHost.back() == ']'))
     {
         strHost = strHost.substr(1, strHost.size() - 2);
     }
@@ -136,16 +133,24 @@ bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nM
     return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
 }
 
-bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions)
+bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
 {
-    return LookupHost(pszName, vIP, nMaxSolutions, false);
+    std::string strHost(pszName);
+    if (strHost.empty())
+        return false;
+    if ((strHost.front() == '[') && (strHost.back() == ']'))
+    {
+        strHost = strHost.substr(1, strHost.size() - 2);
+    }
+
+    return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
 }
 
 bool Lookup(const char *pszName, std::vector<CService>& vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
 {
     if (pszName[0] == 0)
         return false;
-    uint16_t port = portDefault;
+    auto port = portDefault;
     std::string hostname = "";
     SplitHostPort(std::string(pszName), port, hostname);
 
@@ -226,11 +231,9 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
         CloseSocket(hSocket);
         return error("Hostname too long");
     }
-    char pszSocks5Init[] = "\5\1\0";
-    ssize_t nSize = sizeof(pszSocks5Init) - 1;
-
-    ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
-    if (ret != nSize)
+    const char pszSocks5Init[] = "\5\1\0";
+        ssize_t ret = send(hSocket, pszSocks5Init, 3, MSG_NOSIGNAL);
+    if (ret != 3)
     {
         CloseSocket(hSocket);
         return error("Error sending to proxy");
@@ -252,7 +255,7 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
     strSocks5 += strDest;
     strSocks5 += static_cast<char>((port >> 8) & 0xFF);
     strSocks5 += static_cast<char>((port >> 0) & 0xFF);
-    ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
+    ret = send(hSocket, strSocks5.data(), strSocks5.size(), MSG_NOSIGNAL);
     if (ret != (ssize_t)strSocks5.size())
     {
         CloseSocket(hSocket);
@@ -520,7 +523,7 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, uint16_t portDefault, int nTimeout)
 {
     string strDest;
-    uint16_t port = portDefault;
+    auto port = portDefault;
     SplitHostPort(string(pszDest), port, strDest);
 
     SOCKET hSocket = INVALID_SOCKET;
@@ -620,7 +623,7 @@ CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
 {
     Init();
     std::vector<CNetAddr> vIP;
-    if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
+    if (LookupHost(strIp, vIP, 1, fAllowLookup))
         *this = vIP[0];
 }
 
@@ -924,17 +927,12 @@ std::vector<unsigned char> CNetAddr::GetGroup() const
 
 uint64_t CNetAddr::GetHash() const
 {
-    uint256 hash = Hash(&ip[0], &ip[16]);
+    auto hash = Hash(&ip[0], &ip[16]);
     uint64_t nRet;
     memcpy(&nRet, &hash, sizeof(nRet));
     return nRet;
 }
 
-void CNetAddr::print() const
-{
-    printf("CNetAddr(%s)\n", ToString().c_str());
-}
-
 // private extensions to enum Network, only returned by GetExtNetwork,
 // and only used in GetReachabilityFrom
 static const int NET_UNKNOWN = NET_MAX + 0;
@@ -1097,7 +1095,7 @@ CService::CService(const std::string &strIpPort, uint16_t portDefault, bool fAll
         *this = ip;
 }
 
-unsigned short CService::GetPort() const
+uint16_t CService::GetPort() const
 {
     return port;
 }
@@ -1177,12 +1175,7 @@ std::string CService::ToString() const
     return ToStringIPPort();
 }
 
-void CService::print() const
-{
-    printf("CService(%s)\n", ToString().c_str());
-}
-
-void CService::SetPort(unsigned short portIn)
+void CService::SetupPort(uint16_t portIn)
 {
     port = portIn;
 }
@@ -1198,4 +1191,4 @@ bool CloseSocket(SOCKET& hSocket)
 #endif
     hSocket = INVALID_SOCKET;
     return ret != SOCKET_ERROR;
-}
\ No newline at end of file
+}