Skip to content

Commit dec9b5e

Browse files
committed
net: move CloseSocket() from netbase to util/sock
Move `CloseSocket()` (and `NetworkErrorString()` which it uses) from `netbase.{h,cpp}` to newly added `src/util/sock.{h,cpp}`. This is necessary in order to use `CloseSocket()` from a newly introduced Sock class (which will live in `src/util/sock.{h,cpp}`). `sock.{h,cpp}` cannot depend on netbase because netbase will depend on it.
1 parent aa17a44 commit dec9b5e

File tree

6 files changed

+86
-55
lines changed

6 files changed

+86
-55
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ BITCOIN_CORE_H = \
238238
util/rbf.h \
239239
util/ref.h \
240240
util/settings.h \
241+
util/sock.h \
241242
util/spanparsing.h \
242243
util/string.h \
243244
util/system.h \
@@ -552,6 +553,7 @@ libbitcoin_util_a_SOURCES = \
552553
util/error.cpp \
553554
util/fees.cpp \
554555
util/hasher.cpp \
556+
util/sock.cpp \
555557
util/system.cpp \
556558
util/message.cpp \
557559
util/moneystr.cpp \

src/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <protocol.h>
2121
#include <random.h>
2222
#include <scheduler.h>
23+
#include <util/sock.h>
2324
#include <util/strencodings.h>
2425
#include <util/translation.h>
2526

src/netbase.cpp

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <sync.h>
99
#include <tinyformat.h>
10+
#include <util/sock.h>
1011
#include <util/strencodings.h>
1112
#include <util/string.h>
1213
#include <util/system.h>
@@ -862,57 +863,6 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
862863
return false;
863864
}
864865

865-
#ifdef WIN32
866-
std::string NetworkErrorString(int err)
867-
{
868-
wchar_t buf[256];
869-
buf[0] = 0;
870-
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
871-
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
872-
buf, ARRAYSIZE(buf), nullptr))
873-
{
874-
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
875-
}
876-
else
877-
{
878-
return strprintf("Unknown error (%d)", err);
879-
}
880-
}
881-
#else
882-
std::string NetworkErrorString(int err)
883-
{
884-
char buf[256];
885-
buf[0] = 0;
886-
/* Too bad there are two incompatible implementations of the
887-
* thread-safe strerror. */
888-
const char *s;
889-
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
890-
s = strerror_r(err, buf, sizeof(buf));
891-
#else /* POSIX variant always returns message in buffer */
892-
s = buf;
893-
if (strerror_r(err, buf, sizeof(buf)))
894-
buf[0] = 0;
895-
#endif
896-
return strprintf("%s (%d)", s, err);
897-
}
898-
#endif
899-
900-
bool CloseSocket(SOCKET& hSocket)
901-
{
902-
if (hSocket == INVALID_SOCKET)
903-
return false;
904-
#ifdef WIN32
905-
int ret = closesocket(hSocket);
906-
#else
907-
int ret = close(hSocket);
908-
#endif
909-
if (ret) {
910-
LogPrintf("Socket close failed: %d. Error: %s\n", hSocket, NetworkErrorString(WSAGetLastError()));
911-
}
912-
hSocket = INVALID_SOCKET;
913-
return ret != SOCKET_ERROR;
914-
}
915-
916866
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking)
917867
{
918868
if (fNonBlocking) {

src/netbase.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
5454
SOCKET CreateSocket(const CService &addrConnect);
5555
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocketRet, int nTimeout, bool manual_connection);
5656
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
57-
/** Return readable error string for a network error code */
58-
std::string NetworkErrorString(int err);
59-
/** Close socket and set hSocket to INVALID_SOCKET */
60-
bool CloseSocket(SOCKET& hSocket);
6157
/** Disable or enable blocking-mode for a socket */
6258
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
6359
/** Set the TCP_NODELAY flag on a socket */

src/util/sock.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2020-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <compat.h>
6+
#include <logging.h>
7+
#include <tinyformat.h>
8+
#include <util/sock.h>
9+
10+
#include <codecvt>
11+
#include <cwchar>
12+
#include <locale>
13+
#include <string>
14+
15+
#ifdef WIN32
16+
std::string NetworkErrorString(int err)
17+
{
18+
wchar_t buf[256];
19+
buf[0] = 0;
20+
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
21+
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
22+
buf, ARRAYSIZE(buf), nullptr))
23+
{
24+
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
25+
}
26+
else
27+
{
28+
return strprintf("Unknown error (%d)", err);
29+
}
30+
}
31+
#else
32+
std::string NetworkErrorString(int err)
33+
{
34+
char buf[256];
35+
buf[0] = 0;
36+
/* Too bad there are two incompatible implementations of the
37+
* thread-safe strerror. */
38+
const char *s;
39+
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
40+
s = strerror_r(err, buf, sizeof(buf));
41+
#else /* POSIX variant always returns message in buffer */
42+
s = buf;
43+
if (strerror_r(err, buf, sizeof(buf)))
44+
buf[0] = 0;
45+
#endif
46+
return strprintf("%s (%d)", s, err);
47+
}
48+
#endif
49+
50+
bool CloseSocket(SOCKET& hSocket)
51+
{
52+
if (hSocket == INVALID_SOCKET)
53+
return false;
54+
#ifdef WIN32
55+
int ret = closesocket(hSocket);
56+
#else
57+
int ret = close(hSocket);
58+
#endif
59+
if (ret) {
60+
LogPrintf("Socket close failed: %d. Error: %s\n", hSocket, NetworkErrorString(WSAGetLastError()));
61+
}
62+
hSocket = INVALID_SOCKET;
63+
return ret != SOCKET_ERROR;
64+
}

src/util/sock.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2020-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_SOCK_H
6+
#define BITCOIN_UTIL_SOCK_H
7+
8+
#include <compat.h>
9+
10+
#include <string>
11+
12+
/** Return readable error string for a network error code */
13+
std::string NetworkErrorString(int err);
14+
15+
/** Close socket and set hSocket to INVALID_SOCKET */
16+
bool CloseSocket(SOCKET& hSocket);
17+
18+
#endif // BITCOIN_UTIL_SOCK_H

0 commit comments

Comments
 (0)