Add CloseSocket (bitcoin@43f510d37d680ca4347878d2fb6f8b97b54e7611) 268/head
authorsvost <ya.nowa@yandex.ru>
Mon, 4 Jan 2016 12:32:31 +0000 (22:32 +1030)
committersvost <ya.nowa@yandex.ru>
Mon, 4 Jan 2016 12:32:31 +0000 (22:32 +1030)
src/compat.h
src/irc.cpp
src/net.cpp
src/net.h
src/netbase.cpp
src/netbase.h
src/ntp.cpp
src/stun.cpp

index c38e2fa..a4c6489 100644 (file)
@@ -56,18 +56,4 @@ typedef int socklen_t;
 #define SOCKET_ERROR        -1
 #endif
 
-inline int myclosesocket(SOCKET& hSocket)
-{
-    if (hSocket == INVALID_SOCKET)
-        return WSAENOTSOCK;
-#ifdef WIN32
-    int ret = closesocket(hSocket);
-#else
-    int ret = close(hSocket);
-#endif
-    hSocket = INVALID_SOCKET;
-    return ret;
-}
-#define closesocket(s)      myclosesocket(s)
-
-#endif
+#endif
\ No newline at end of file
index b3201b1..57e7d88 100644 (file)
@@ -248,8 +248,7 @@ void ThreadIRCSeed2(void* parg)
 
         if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
         {
-            closesocket(hSocket);
-            hSocket = INVALID_SOCKET;
+            CloseSocket(hSocket);
             nErrorWait = nErrorWait * 11 / 10;
             if (Wait(nErrorWait += 60))
                 continue;
@@ -273,8 +272,7 @@ void ThreadIRCSeed2(void* parg)
         int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
         if (nRet != 1)
         {
-            closesocket(hSocket);
-            hSocket = INVALID_SOCKET;
+            CloseSocket(hSocket);
             if (nRet == 2)
             {
                 printf("IRC name already in use\n");
@@ -365,8 +363,7 @@ void ThreadIRCSeed2(void* parg)
                 }
             }
         }
-        closesocket(hSocket);
-        hSocket = INVALID_SOCKET;
+        CloseSocket(hSocket);
 
         if (GetTime() - nStart > 20 * 60)
         {
index 26b8174..ca898b3 100644 (file)
@@ -447,8 +447,7 @@ void CNode::CloseSocketDisconnect()
     if (hSocket != INVALID_SOCKET)
     {
         printf("disconnecting node %s\n", addrName.c_str());
-        closesocket(hSocket);
-        hSocket = INVALID_SOCKET;
+        CloseSocket(hSocket);
         vRecv.clear();
     }
 
@@ -777,13 +776,13 @@ void ThreadSocketHandler2(void* parg)
                 {
                     LOCK(cs_setservAddNodeAddresses);
                     if (!setservAddNodeAddresses.count(addr))
-                        closesocket(hSocket);
+                        CloseSocket(hSocket);
                 }
             }
             else if (CNode::IsBanned(addr))
             {
                 printf("connection from %s dropped (banned)\n", addr.ToString().c_str());
-                closesocket(hSocket);
+                CloseSocket(hSocket);
             }
             else
             {
@@ -1607,7 +1606,7 @@ bool BindListenPort(const CService &addrBind, string& strError)
         else
             strError = strprintf(_("Unable to bind to %s on this computer (bind returned error %d, %s)"), addrBind.ToString().c_str(), nErr, strerror(nErr));
         printf("%s\n", strError.c_str());
-        closesocket(hListenSocket);
+        CloseSocket(hListenSocket);
         return false;
     }
     printf("Bound to %s\n", addrBind.ToString().c_str());
@@ -1617,7 +1616,7 @@ bool BindListenPort(const CService &addrBind, string& strError)
     {
         strError = strprintf("Error: Listening for incoming connections failed (listen returned error %d)", WSAGetLastError());
         printf("%s\n", strError.c_str());
-        closesocket(hListenSocket);
+        CloseSocket(hListenSocket);
         return false;
     }
 
@@ -1803,11 +1802,11 @@ public:
         // Close sockets
         BOOST_FOREACH(CNode* pnode, vNodes)
             if (pnode->hSocket != INVALID_SOCKET)
-                closesocket(pnode->hSocket);
+                CloseSocket(pnode->hSocket);
         BOOST_FOREACH(SOCKET hListenSocket, vhListenSocket)
             if (hListenSocket != INVALID_SOCKET)
-                if (closesocket(hListenSocket) == SOCKET_ERROR)
-                    printf("closesocket(hListenSocket) failed with error %d\n", WSAGetLastError());
+                if (!CloseSocket(hListenSocket))
+                    printf("CloseSocket(hListenSocket) failed with error %d\n", WSAGetLastError());
 
         // clean up some globals (to help leak detection)
         BOOST_FOREACH(CNode *pnode, vNodes)
index 56c7b2f..b12750c 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -266,8 +266,7 @@ public:
     {
         if (hSocket != INVALID_SOCKET)
         {
-            closesocket(hSocket);
-            hSocket = INVALID_SOCKET;
+            CloseSocket(hSocket);
         }
     }
 
index 1e7172e..7cc3fb0 100644 (file)
@@ -179,7 +179,7 @@ bool static Socks4(const CService &addrDest, SOCKET& hSocket)
     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";
@@ -187,7 +187,7 @@ bool static Socks4(const CService &addrDest, SOCKET& hSocket)
     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);
@@ -198,18 +198,18 @@ bool static Socks4(const CService &addrDest, SOCKET& hSocket)
     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 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
     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";
@@ -232,18 +232,18 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
     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");
@@ -255,23 +255,23 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
     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");
@@ -287,7 +287,7 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
     }
     if (pchRet2[2] != 0x00)
     {
-        closesocket(hSocket);
+        CloseSocket(hSocket);
         return error("Error: malformed proxy response");
     }
     char pchRet3[256];
@@ -299,23 +299,23 @@ bool static Socks5(string strDest, uint16_t port, SOCKET& hSocket)
         {
             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,7 +353,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
     if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
 #endif
     {
-        closesocket(hSocket);
+        CloseSocket(hSocket);
         return false;
     }
 
@@ -373,13 +373,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
             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);
@@ -390,13 +390,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
 #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;
             }
         }
@@ -407,7 +407,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
 #endif
         {
             printf("connect() failed: %i\n",WSAGetLastError());
-            closesocket(hSocket);
+            CloseSocket(hSocket);
             return false;
         }
     }
@@ -423,7 +423,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
     if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR)
 #endif
     {
-        closesocket(hSocket);
+        CloseSocket(hSocket);
         return false;
     }
 
@@ -508,7 +508,7 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
             return false;
         break;
     default:
-        closesocket(hSocket);
+        CloseSocket(hSocket);
         return false;
     }
 
@@ -541,7 +541,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
     switch(nameproxy.second) {
         default:
         case 4:
-            closesocket(hSocket);
+            CloseSocket(hSocket);
             return false;
         case 5:
             if (!Socks5(strDest, port, hSocket))
@@ -1185,3 +1185,16 @@ void CService::SetPort(unsigned short portIn)
 {
     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;
+}
\ No newline at end of file
index 9300003..5e63d2f 100644 (file)
@@ -149,5 +149,7 @@ bool Lookup(const char *pszName, std::vector<CService>& vAddr, uint16_t portDefa
 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
\ No newline at end of file
index 527a02c..c4f9f55 100644 (file)
@@ -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 @@ int64_t NtpGetTime(const std::string &strHostName)
 
     int64_t nTime = DoReq(sockfd, servlen, cliaddr);
 
-    closesocket(sockfd);
+    CloseSocket(sockfd);
 
     return nTime;
 }
index 62e302f..42d02c5 100644 (file)
@@ -49,6 +49,7 @@
 #include <errno.h>
 
 #include "ministun.h"
+#include "netbase.h"
 
 extern int GetRandInt(int nMax);
 extern uint64_t GetRand(uint64_t nMax);
@@ -499,6 +500,7 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp
     if(hostinfo == NULL)
         return -1;
 
+    SOCKET sock = INVALID_SOCKET;
     struct sockaddr_in server, client;
     memset(&server, 0, sizeof(server));
     memset(&client, 0, sizeof(client));
@@ -507,8 +509,8 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp
     server.sin_addr = *(struct in_addr*) hostinfo->h_addr;
     server.sin_port = htons(port);
 
-    int sock = socket(AF_INET, SOCK_DGRAM, 0);
-    if(sock < 0)
+    sock = socket(AF_INET, SOCK_DGRAM, 0);
+    if(sock == INVALID_SOCKET)
         return -2;
 
     client.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -516,11 +518,7 @@ static int StunRequest(const char *host, uint16_t port, struct sockaddr_in *mapp
     int rc = -3;
     if(bind(sock, (struct sockaddr*)&client, sizeof(client)) >= 0)
         rc = StunRequest2(sock, &server, mapped);
-#ifndef WIN32
-    close(sock);
-#else
-    closesocket(sock);
-#endif
+    CloseSocket(sock);
     return rc;
 } // StunRequest