Merge pull request #268 from svost/CloseSocket
author0xDEADFACE <masmfan@gmail.com>
Wed, 3 Feb 2016 07:45:26 +0000 (07:45 +0000)
committer0xDEADFACE <masmfan@gmail.com>
Wed, 3 Feb 2016 07:45:26 +0000 (07:45 +0000)
Add CloseSocket (bitcoin@43f510d37d680ca4347878d2fb6f8b97b54e7611)

1  2 
src/net.h
src/netbase.cpp
src/netbase.h
src/ntp.cpp

diff --combined src/net.h
+++ b/src/net.h
@@@ -19,6 -19,7 +19,6 @@@
  
  #include "mruset.h"
  #include "netbase.h"
 -#include "protocol.h"
  #include "addrman.h"
  #include "hash.h"
  
@@@ -76,7 -77,7 +76,7 @@@ CAddress GetLocalAddress(const CNetAdd
  enum
  {
      MSG_TX = 1,
 -    MSG_BLOCK,
 +    MSG_BLOCK
  };
  
  class CRequestTracker
@@@ -265,8 -266,7 +265,7 @@@ public
      {
          if (hSocket != INVALID_SOCKET)
          {
-             closesocket(hSocket);
-             hSocket = INVALID_SOCKET;
+             CloseSocket(hSocket);
          }
      }
  
diff --combined src/netbase.cpp
@@@ -179,7 -179,7 +179,7 @@@ bool static Socks4(const CService &addr
      printf("SOCKS4 connecting %s\n", addrDest.ToString().c_str());
      if (!addrDest.IsIPv4())
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Proxy destination is not IPv4");
      }
      char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
      socklen_t len = sizeof(addr);
      if (!addrDest.GetSockAddr((struct sockaddr*)&addr, &len) || addr.sin_family != AF_INET)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Cannot get proxy destination address");
      }
      memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
      int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
      if (ret != nSize)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error sending to proxy");
      }
      char pchRet[8];
      if (recv(hSocket, pchRet, 8, 0) != 8)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error reading proxy response");
      }
      if (pchRet[1] != 0x5a)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          if (pchRet[1] != 0x5b)
              printf("ERROR: Proxy returned error %d\n", pchRet[1]);
          return false;
@@@ -223,7 -223,7 +223,7 @@@ bool static Socks5(string strDest, uint
      printf("SOCKS5 connecting %s\n", strDest.c_str());
      if (strDest.size() > 255)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Hostname too long");
      }
      char pszSocks5Init[] = "\5\1\0";
      ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
      if (ret != nSize)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error sending to proxy");
      }
      char pchRet1[2];
      if (recv(hSocket, pchRet1, 2, 0) != 2)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error reading proxy response");
      }
      if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Proxy failed to initialize");
      }
      string strSocks5("\5\1");
      ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
      if (ret != (ssize_t)strSocks5.size())
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error sending to proxy");
      }
      char pchRet2[4];
      if (recv(hSocket, pchRet2, 4, 0) != 4)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error reading proxy response");
      }
      if (pchRet2[0] != 0x05)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Proxy failed to accept request");
      }
      if (pchRet2[1] != 0x00)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          switch (pchRet2[1])
          {
              case 0x01: return error("Proxy error: general failure");
      }
      if (pchRet2[2] != 0x00)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error: malformed proxy response");
      }
      char pchRet3[256];
          {
              ret = recv(hSocket, pchRet3, 1, 0) != 1;
              if (ret) {
-                 closesocket(hSocket);
+                 CloseSocket(hSocket);
                  return error("Error reading from proxy");
              }
              int nRecv = pchRet3[0];
              ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv;
              break;
          }
-         default: closesocket(hSocket); return error("Error: malformed proxy response");
+         default: CloseSocket(hSocket); return error("Error: malformed proxy response");
      }
      if (ret)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error reading from proxy");
      }
      if (recv(hSocket, pchRet3, 2, 0) != 2)
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return error("Error reading from proxy");
      }
      printf("SOCKS5 connected %s\n", strDest.c_str());
@@@ -353,15 -353,14 +353,15 @@@ bool static ConnectSocketDirectly(cons
      if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
  #endif
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return false;
      }
  
      if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
      {
 +        int nErr = WSAGetLastError();
          // WSAEINVAL is here because some legacy version of winsock uses it
 -        if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
 +        if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
          {
              struct timeval timeout;
              timeout.tv_sec  = nTimeout / 1000;
              if (nRet == 0)
              {
                  printf("connection timeout\n");
-                 closesocket(hSocket);
+                 CloseSocket(hSocket);
                  return false;
              }
              if (nRet == SOCKET_ERROR)
              {
                  printf("select() for connection failed: %i\n",WSAGetLastError());
-                 closesocket(hSocket);
+                 CloseSocket(hSocket);
                  return false;
              }
              socklen_t nRetSize = sizeof(nRet);
  #endif
              {
                  printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
-                 closesocket(hSocket);
+                 CloseSocket(hSocket);
                  return false;
              }
              if (nRet != 0)
              {
                  printf("connect() failed after select(): %s\n",strerror(nRet));
-                 closesocket(hSocket);
+                 CloseSocket(hSocket);
                  return false;
              }
          }
  #endif
          {
              printf("connect() failed: %i\n",WSAGetLastError());
-             closesocket(hSocket);
+             CloseSocket(hSocket);
              return false;
          }
      }
      if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR)
  #endif
      {
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return false;
      }
  
@@@ -509,7 -508,7 +509,7 @@@ bool ConnectSocket(const CService &addr
              return false;
          break;
      default:
-         closesocket(hSocket);
+         CloseSocket(hSocket);
          return false;
      }
  
@@@ -542,7 -541,7 +542,7 @@@ bool ConnectSocketByName(CService &addr
      switch(nameproxy.second) {
          default:
          case 4:
-             closesocket(hSocket);
+             CloseSocket(hSocket);
              return false;
          case 5:
              if (!Socks5(strDest, port, hSocket))
@@@ -1186,3 -1185,16 +1186,16 @@@ void CService::SetPort(unsigned short p
  {
      port = portIn;
  }
+ bool CloseSocket(SOCKET& hSocket)
+ {
+     if (hSocket == INVALID_SOCKET)
+         return false;
+ #ifdef WIN32
+     int ret = closesocket(hSocket);
+ #else
+     int ret = close(hSocket);
+ #endif
+     hSocket = INVALID_SOCKET;
+     return ret != SOCKET_ERROR;
+ }
diff --combined src/netbase.h
@@@ -25,7 -25,7 +25,7 @@@ enum Networ
      NET_TOR,
      NET_I2P,
  
 -    NET_MAX,
 +    NET_MAX
  };
  
  extern int nConnectTimeout;
@@@ -149,5 -149,7 +149,7 @@@ bool Lookup(const char *pszName, std::v
  bool LookupNumeric(const char *pszName, CService& addr, uint16_t portDefault = 0);
  bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
  bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, uint16_t portDefault = 0, int nTimeout = nConnectTimeout);
+ /** Close socket and set hSocket to INVALID_SOCKET */
+ bool CloseSocket(SOCKET& hSocket);
  
- #endif
+ #endif
diff --combined src/ntp.cpp
@@@ -411,7 -411,7 +411,7 @@@ int64_t NtpGetTime(CNetAddr& ip) 
      ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr);
      int64_t nTime = DoReq(sockfd, servlen, cliaddr);
  
-     closesocket(sockfd);
+     CloseSocket(sockfd);
  
      return nTime;
  }
@@@ -428,7 -428,7 +428,7 @@@ int64_t NtpGetTime(const std::string &s
  
      int64_t nTime = DoReq(sockfd, servlen, cliaddr);
  
-     closesocket(sockfd);
+     CloseSocket(sockfd);
  
      return nTime;
  }
@@@ -445,7 -445,7 +445,7 @@@ int64_t GetNtpOffset() 
  }
  
  void ThreadNtpSamples(void* parg) {
 -    const int64_t nMaxOffset = 86400; // Not a real limit, just sanity threshold.
 +    const int64_t nMaxOffset = nOneDay; // Not a real limit, just sanity threshold.
  
      printf("Trying to find NTP server at localhost...\n");