Browse Source

Merge pull request #4504

43f510d Convert closesocket 'compat wrapper' to function in netbase (Wladimir J. van der Laan)
0.10
Wladimir J. van der Laan 11 years ago
parent
commit
e8d4cb8071
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 15
      src/compat.h
  2. 19
      src/net.cpp
  3. 3
      src/net.h
  4. 53
      src/netbase.cpp
  5. 2
      src/netbase.h

15
src/compat.h

@ -59,19 +59,4 @@ typedef u_int SOCKET;
#define SOCKET_ERROR -1 #define SOCKET_ERROR -1
#endif #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

19
src/net.cpp

@ -332,7 +332,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
{ {
if (!RecvLine(hSocket, strLine)) if (!RecvLine(hSocket, strLine))
{ {
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
if (pszKeyword == NULL) if (pszKeyword == NULL)
@ -343,7 +343,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
break; break;
} }
} }
closesocket(hSocket); CloseSocket(hSocket);
if (strLine.find("<") != string::npos) if (strLine.find("<") != string::npos)
strLine = strLine.substr(0, strLine.find("<")); strLine = strLine.substr(0, strLine.find("<"));
strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r")); strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
@ -357,7 +357,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
return true; return true;
} }
} }
closesocket(hSocket); CloseSocket(hSocket);
return error("GetMyExternalIP() : connection closed"); return error("GetMyExternalIP() : connection closed");
} }
@ -533,8 +533,7 @@ void CNode::CloseSocketDisconnect()
if (hSocket != INVALID_SOCKET) if (hSocket != INVALID_SOCKET)
{ {
LogPrint("net", "disconnecting peer=%d\n", id); LogPrint("net", "disconnecting peer=%d\n", id);
closesocket(hSocket); CloseSocket(hSocket);
hSocket = INVALID_SOCKET;
} }
// in case this fails, we'll empty the recv buffer when the CNode is deleted // in case this fails, we'll empty the recv buffer when the CNode is deleted
@ -975,12 +974,12 @@ void ThreadSocketHandler()
} }
else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS) else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
{ {
closesocket(hSocket); CloseSocket(hSocket);
} }
else if (CNode::IsBanned(addr) && !whitelisted) else if (CNode::IsBanned(addr) && !whitelisted)
{ {
LogPrintf("connection from %s dropped (banned)\n", addr.ToString()); LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
closesocket(hSocket); CloseSocket(hSocket);
} }
else else
{ {
@ -1817,11 +1816,11 @@ public:
// Close sockets // Close sockets
BOOST_FOREACH(CNode* pnode, vNodes) BOOST_FOREACH(CNode* pnode, vNodes)
if (pnode->hSocket != INVALID_SOCKET) if (pnode->hSocket != INVALID_SOCKET)
closesocket(pnode->hSocket); CloseSocket(pnode->hSocket);
BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket) BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
if (hListenSocket.socket != INVALID_SOCKET) if (hListenSocket.socket != INVALID_SOCKET)
if (closesocket(hListenSocket.socket) == SOCKET_ERROR) if (!CloseSocket(hListenSocket.socket))
LogPrintf("closesocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError())); LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
// clean up some globals (to help leak detection) // clean up some globals (to help leak detection)
BOOST_FOREACH(CNode *pnode, vNodes) BOOST_FOREACH(CNode *pnode, vNodes)

3
src/net.h

@ -357,8 +357,7 @@ public:
{ {
if (hSocket != INVALID_SOCKET) if (hSocket != INVALID_SOCKET)
{ {
closesocket(hSocket); CloseSocket(hSocket);
hSocket = INVALID_SOCKET;
} }
if (pfilter) if (pfilter)
delete pfilter; delete pfilter;

53
src/netbase.cpp

@ -218,7 +218,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
LogPrintf("SOCKS5 connecting %s\n", strDest); LogPrintf("SOCKS5 connecting %s\n", strDest);
if (strDest.size() > 255) if (strDest.size() > 255)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Hostname too long"); return error("Hostname too long");
} }
char pszSocks5Init[] = "\5\1\0"; char pszSocks5Init[] = "\5\1\0";
@ -227,18 +227,18 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL); ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
if (ret != nSize) if (ret != nSize)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error sending to proxy"); return error("Error sending to proxy");
} }
char pchRet1[2]; char pchRet1[2];
if (recv(hSocket, pchRet1, 2, 0) != 2) if (recv(hSocket, pchRet1, 2, 0) != 2)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error reading proxy response"); return error("Error reading proxy response");
} }
if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00) if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Proxy failed to initialize"); return error("Proxy failed to initialize");
} }
string strSocks5("\5\1"); string strSocks5("\5\1");
@ -250,23 +250,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL); ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
if (ret != (ssize_t)strSocks5.size()) if (ret != (ssize_t)strSocks5.size())
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error sending to proxy"); return error("Error sending to proxy");
} }
char pchRet2[4]; char pchRet2[4];
if (recv(hSocket, pchRet2, 4, 0) != 4) if (recv(hSocket, pchRet2, 4, 0) != 4)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error reading proxy response"); return error("Error reading proxy response");
} }
if (pchRet2[0] != 0x05) if (pchRet2[0] != 0x05)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Proxy failed to accept request"); return error("Proxy failed to accept request");
} }
if (pchRet2[1] != 0x00) if (pchRet2[1] != 0x00)
{ {
closesocket(hSocket); CloseSocket(hSocket);
switch (pchRet2[1]) switch (pchRet2[1])
{ {
case 0x01: return error("Proxy error: general failure"); case 0x01: return error("Proxy error: general failure");
@ -282,7 +282,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
} }
if (pchRet2[2] != 0x00) if (pchRet2[2] != 0x00)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error: malformed proxy response"); return error("Error: malformed proxy response");
} }
char pchRet3[256]; char pchRet3[256];
@ -294,23 +294,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
{ {
ret = recv(hSocket, pchRet3, 1, 0) != 1; ret = recv(hSocket, pchRet3, 1, 0) != 1;
if (ret) { if (ret) {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error reading from proxy"); return error("Error reading from proxy");
} }
int nRecv = pchRet3[0]; int nRecv = pchRet3[0];
ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv; ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv;
break; break;
} }
default: closesocket(hSocket); return error("Error: malformed proxy response"); default: CloseSocket(hSocket); return error("Error: malformed proxy response");
} }
if (ret) if (ret)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error reading from proxy"); return error("Error reading from proxy");
} }
if (recv(hSocket, pchRet3, 2, 0) != 2) if (recv(hSocket, pchRet3, 2, 0) != 2)
{ {
closesocket(hSocket); CloseSocket(hSocket);
return error("Error reading from proxy"); return error("Error reading from proxy");
} }
LogPrintf("SOCKS5 connected %s\n", strDest); LogPrintf("SOCKS5 connected %s\n", strDest);
@ -344,7 +344,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1) if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
#endif #endif
{ {
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
@ -365,13 +365,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if (nRet == 0) if (nRet == 0)
{ {
LogPrint("net", "connection to %s timeout\n", addrConnect.ToString()); LogPrint("net", "connection to %s timeout\n", addrConnect.ToString());
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
if (nRet == SOCKET_ERROR) if (nRet == SOCKET_ERROR)
{ {
LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError())); LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
socklen_t nRetSize = sizeof(nRet); socklen_t nRetSize = sizeof(nRet);
@ -382,13 +382,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
#endif #endif
{ {
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError())); LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
if (nRet != 0) if (nRet != 0)
{ {
LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet)); LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
} }
@ -399,7 +399,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
#endif #endif
{ {
LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError())); LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
} }
@ -415,7 +415,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR)
#endif #endif
{ {
closesocket(hSocket); CloseSocket(hSocket);
return false; return false;
} }
@ -1258,3 +1258,16 @@ std::string NetworkErrorString(int err)
return strprintf("%s (%d)", s, err); return strprintf("%s (%d)", s, err);
} }
#endif #endif
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;
}

2
src/netbase.h

@ -178,5 +178,7 @@ bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nCon
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
/** Return readable error string for a network error code */ /** Return readable error string for a network error code */
std::string NetworkErrorString(int err); std::string NetworkErrorString(int err);
/** Close socket and set hSocket to INVALID_SOCKET */
bool CloseSocket(SOCKET& hSocket);
#endif #endif

Loading…
Cancel
Save