Skip to content

Commit 43f510d

Browse files
committed
Convert closesocket 'compat wrapper' to function in netbase
Simpler alternative to #4348. The current setup with closesocket() is strange. It poses as a compatibility wrapper but adds functionality. Rename it and make it a documented utility function in netbase. Code movement only, zero effect on the functionality.
1 parent c4f11ca commit 43f510d

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
lines changed

src/compat.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,4 @@ typedef u_int SOCKET;
5959
#define SOCKET_ERROR -1
6060
#endif
6161

62-
inline int myclosesocket(SOCKET& hSocket)
63-
{
64-
if (hSocket == INVALID_SOCKET)
65-
return WSAENOTSOCK;
66-
#ifdef WIN32
67-
int ret = closesocket(hSocket);
68-
#else
69-
int ret = close(hSocket);
70-
#endif
71-
hSocket = INVALID_SOCKET;
72-
return ret;
73-
}
74-
#define closesocket(s) myclosesocket(s)
75-
76-
7762
#endif

src/net.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
332332
{
333333
if (!RecvLine(hSocket, strLine))
334334
{
335-
closesocket(hSocket);
335+
CloseSocket(hSocket);
336336
return false;
337337
}
338338
if (pszKeyword == NULL)
@@ -343,7 +343,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
343343
break;
344344
}
345345
}
346-
closesocket(hSocket);
346+
CloseSocket(hSocket);
347347
if (strLine.find("<") != string::npos)
348348
strLine = strLine.substr(0, strLine.find("<"));
349349
strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r"));
@@ -357,7 +357,7 @@ bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const cha
357357
return true;
358358
}
359359
}
360-
closesocket(hSocket);
360+
CloseSocket(hSocket);
361361
return error("GetMyExternalIP() : connection closed");
362362
}
363363

@@ -533,8 +533,7 @@ void CNode::CloseSocketDisconnect()
533533
if (hSocket != INVALID_SOCKET)
534534
{
535535
LogPrint("net", "disconnecting peer=%d\n", id);
536-
closesocket(hSocket);
537-
hSocket = INVALID_SOCKET;
536+
CloseSocket(hSocket);
538537
}
539538

540539
// in case this fails, we'll empty the recv buffer when the CNode is deleted
@@ -975,12 +974,12 @@ void ThreadSocketHandler()
975974
}
976975
else if (nInbound >= nMaxConnections - MAX_OUTBOUND_CONNECTIONS)
977976
{
978-
closesocket(hSocket);
977+
CloseSocket(hSocket);
979978
}
980979
else if (CNode::IsBanned(addr) && !whitelisted)
981980
{
982981
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
983-
closesocket(hSocket);
982+
CloseSocket(hSocket);
984983
}
985984
else
986985
{
@@ -1817,11 +1816,11 @@ class CNetCleanup
18171816
// Close sockets
18181817
BOOST_FOREACH(CNode* pnode, vNodes)
18191818
if (pnode->hSocket != INVALID_SOCKET)
1820-
closesocket(pnode->hSocket);
1819+
CloseSocket(pnode->hSocket);
18211820
BOOST_FOREACH(ListenSocket& hListenSocket, vhListenSocket)
18221821
if (hListenSocket.socket != INVALID_SOCKET)
1823-
if (closesocket(hListenSocket.socket) == SOCKET_ERROR)
1824-
LogPrintf("closesocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
1822+
if (!CloseSocket(hListenSocket.socket))
1823+
LogPrintf("CloseSocket(hListenSocket) failed with error %s\n", NetworkErrorString(WSAGetLastError()));
18251824

18261825
// clean up some globals (to help leak detection)
18271826
BOOST_FOREACH(CNode *pnode, vNodes)

src/net.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ class CNode
357357
{
358358
if (hSocket != INVALID_SOCKET)
359359
{
360-
closesocket(hSocket);
361-
hSocket = INVALID_SOCKET;
360+
CloseSocket(hSocket);
362361
}
363362
if (pfilter)
364363
delete pfilter;

src/netbase.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
218218
LogPrintf("SOCKS5 connecting %s\n", strDest);
219219
if (strDest.size() > 255)
220220
{
221-
closesocket(hSocket);
221+
CloseSocket(hSocket);
222222
return error("Hostname too long");
223223
}
224224
char pszSocks5Init[] = "\5\1\0";
@@ -227,18 +227,18 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
227227
ssize_t ret = send(hSocket, pszSocks5Init, nSize, MSG_NOSIGNAL);
228228
if (ret != nSize)
229229
{
230-
closesocket(hSocket);
230+
CloseSocket(hSocket);
231231
return error("Error sending to proxy");
232232
}
233233
char pchRet1[2];
234234
if (recv(hSocket, pchRet1, 2, 0) != 2)
235235
{
236-
closesocket(hSocket);
236+
CloseSocket(hSocket);
237237
return error("Error reading proxy response");
238238
}
239239
if (pchRet1[0] != 0x05 || pchRet1[1] != 0x00)
240240
{
241-
closesocket(hSocket);
241+
CloseSocket(hSocket);
242242
return error("Proxy failed to initialize");
243243
}
244244
string strSocks5("\5\1");
@@ -250,23 +250,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
250250
ret = send(hSocket, strSocks5.c_str(), strSocks5.size(), MSG_NOSIGNAL);
251251
if (ret != (ssize_t)strSocks5.size())
252252
{
253-
closesocket(hSocket);
253+
CloseSocket(hSocket);
254254
return error("Error sending to proxy");
255255
}
256256
char pchRet2[4];
257257
if (recv(hSocket, pchRet2, 4, 0) != 4)
258258
{
259-
closesocket(hSocket);
259+
CloseSocket(hSocket);
260260
return error("Error reading proxy response");
261261
}
262262
if (pchRet2[0] != 0x05)
263263
{
264-
closesocket(hSocket);
264+
CloseSocket(hSocket);
265265
return error("Proxy failed to accept request");
266266
}
267267
if (pchRet2[1] != 0x00)
268268
{
269-
closesocket(hSocket);
269+
CloseSocket(hSocket);
270270
switch (pchRet2[1])
271271
{
272272
case 0x01: return error("Proxy error: general failure");
@@ -282,7 +282,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
282282
}
283283
if (pchRet2[2] != 0x00)
284284
{
285-
closesocket(hSocket);
285+
CloseSocket(hSocket);
286286
return error("Error: malformed proxy response");
287287
}
288288
char pchRet3[256];
@@ -294,23 +294,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
294294
{
295295
ret = recv(hSocket, pchRet3, 1, 0) != 1;
296296
if (ret) {
297-
closesocket(hSocket);
297+
CloseSocket(hSocket);
298298
return error("Error reading from proxy");
299299
}
300300
int nRecv = pchRet3[0];
301301
ret = recv(hSocket, pchRet3, nRecv, 0) != nRecv;
302302
break;
303303
}
304-
default: closesocket(hSocket); return error("Error: malformed proxy response");
304+
default: CloseSocket(hSocket); return error("Error: malformed proxy response");
305305
}
306306
if (ret)
307307
{
308-
closesocket(hSocket);
308+
CloseSocket(hSocket);
309309
return error("Error reading from proxy");
310310
}
311311
if (recv(hSocket, pchRet3, 2, 0) != 2)
312312
{
313-
closesocket(hSocket);
313+
CloseSocket(hSocket);
314314
return error("Error reading from proxy");
315315
}
316316
LogPrintf("SOCKS5 connected %s\n", strDest);
@@ -344,7 +344,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
344344
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
345345
#endif
346346
{
347-
closesocket(hSocket);
347+
CloseSocket(hSocket);
348348
return false;
349349
}
350350

@@ -365,13 +365,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
365365
if (nRet == 0)
366366
{
367367
LogPrint("net", "connection to %s timeout\n", addrConnect.ToString());
368-
closesocket(hSocket);
368+
CloseSocket(hSocket);
369369
return false;
370370
}
371371
if (nRet == SOCKET_ERROR)
372372
{
373373
LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
374-
closesocket(hSocket);
374+
CloseSocket(hSocket);
375375
return false;
376376
}
377377
socklen_t nRetSize = sizeof(nRet);
@@ -382,13 +382,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
382382
#endif
383383
{
384384
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
385-
closesocket(hSocket);
385+
CloseSocket(hSocket);
386386
return false;
387387
}
388388
if (nRet != 0)
389389
{
390390
LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
391-
closesocket(hSocket);
391+
CloseSocket(hSocket);
392392
return false;
393393
}
394394
}
@@ -399,7 +399,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
399399
#endif
400400
{
401401
LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
402-
closesocket(hSocket);
402+
CloseSocket(hSocket);
403403
return false;
404404
}
405405
}
@@ -415,7 +415,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
415415
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR)
416416
#endif
417417
{
418-
closesocket(hSocket);
418+
CloseSocket(hSocket);
419419
return false;
420420
}
421421

@@ -1258,3 +1258,16 @@ std::string NetworkErrorString(int err)
12581258
return strprintf("%s (%d)", s, err);
12591259
}
12601260
#endif
1261+
1262+
bool CloseSocket(SOCKET& hSocket)
1263+
{
1264+
if (hSocket == INVALID_SOCKET)
1265+
return false;
1266+
#ifdef WIN32
1267+
int ret = closesocket(hSocket);
1268+
#else
1269+
int ret = close(hSocket);
1270+
#endif
1271+
hSocket = INVALID_SOCKET;
1272+
return ret != SOCKET_ERROR;
1273+
}

src/netbase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,7 @@ bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nCon
178178
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
179179
/** Return readable error string for a network error code */
180180
std::string NetworkErrorString(int err);
181+
/** Close socket and set hSocket to INVALID_SOCKET */
182+
bool CloseSocket(SOCKET& hSocket);
181183

182184
#endif

0 commit comments

Comments
 (0)