From cc7b2fdd70da439c3cf8daef3bb79cf593f1cef3 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 13:27:57 +0100 Subject: [PATCH 1/8] refactor: move compat.h into compat/ --- src/Makefile.am | 2 +- src/bitcoin-cli.cpp | 2 +- src/bitcoin-tx.cpp | 2 +- src/bitcoin-util.cpp | 2 +- src/bitcoin-wallet.cpp | 2 +- src/bitcoind.cpp | 2 +- src/{ => compat}/compat.h | 6 +++--- src/httpserver.cpp | 2 +- src/i2p.cpp | 2 +- src/i2p.h | 2 +- src/mapport.cpp | 2 +- src/net.cpp | 2 +- src/net.h | 2 +- src/netaddress.h | 2 +- src/netbase.cpp | 2 +- src/netbase.h | 2 +- src/qt/main.cpp | 2 +- src/random.cpp | 2 +- src/randomenv.cpp | 2 +- src/test/fuzz/util.h | 2 +- src/test/net_tests.cpp | 2 +- src/test/sock_tests.cpp | 2 +- src/test/util/net.h | 2 +- src/torcontrol.cpp | 2 +- src/util/sock.cpp | 2 +- src/util/sock.h | 2 +- src/util/system.h | 2 +- src/util/time.cpp | 2 +- src/util/time.h | 2 +- 29 files changed, 31 insertions(+), 31 deletions(-) rename src/{ => compat}/compat.h (96%) diff --git a/src/Makefile.am b/src/Makefile.am index 7745f4d7c8..23bc180095 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -133,9 +133,9 @@ BITCOIN_CORE_H = \ clientversion.h \ coins.h \ common/bloom.h \ - compat.h \ compat/assumptions.h \ compat/byteswap.h \ + compat/compat.h \ compat/cpuid.h \ compat/endian.h \ compressor.h \ diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 0db2b75384..7cc956ebda 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index e0d5c6e5dc..b006353cb0 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/bitcoin-util.cpp b/src/bitcoin-util.cpp index 1739804edb..fb184c0486 100644 --- a/src/bitcoin-util.cpp +++ b/src/bitcoin-util.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bitcoin-wallet.cpp b/src/bitcoin-wallet.cpp index 7bec3292a1..a7d49452b0 100644 --- a/src/bitcoin-wallet.cpp +++ b/src/bitcoin-wallet.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index be894e192e..85ba88f6ab 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/compat.h b/src/compat/compat.h similarity index 96% rename from src/compat.h rename to src/compat/compat.h index 0a44b98b4e..51ed03eef4 100644 --- a/src/compat.h +++ b/src/compat/compat.h @@ -3,8 +3,8 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#ifndef BITCOIN_COMPAT_H -#define BITCOIN_COMPAT_H +#ifndef BITCOIN_COMPAT_COMPAT_H +#define BITCOIN_COMPAT_COMPAT_H #if defined(HAVE_CONFIG_H) #include @@ -119,4 +119,4 @@ bool static inline IsSelectableSocket(const SOCKET& s) { #define MSG_DONTWAIT 0 #endif -#endif // BITCOIN_COMPAT_H +#endif // BITCOIN_COMPAT_COMPAT_H diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b8f69b038c..8e00a6278f 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include // For HTTP status codes diff --git a/src/i2p.cpp b/src/i2p.cpp index 8611984555..c45bcc15d2 100644 --- a/src/i2p.cpp +++ b/src/i2p.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include #include #include diff --git a/src/i2p.h b/src/i2p.h index 433fcc3a08..eb0a10103d 100644 --- a/src/i2p.h +++ b/src/i2p.h @@ -5,7 +5,7 @@ #ifndef BITCOIN_I2P_H #define BITCOIN_I2P_H -#include +#include #include #include #include diff --git a/src/mapport.cpp b/src/mapport.cpp index 235e6f904c..6262e51879 100644 --- a/src/mapport.cpp +++ b/src/mapport.cpp @@ -19,7 +19,7 @@ #include #ifdef USE_NATPMP -#include +#include #include #endif // USE_NATPMP diff --git a/src/net.cpp b/src/net.cpp index 6151ab9e08..e9aa7ee43b 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/net.h b/src/net.h index f3be7e8dff..2036e9078c 100644 --- a/src/net.h +++ b/src/net.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/netaddress.h b/src/netaddress.h index 47ba045334..e52beb783d 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -9,7 +9,7 @@ #include #endif -#include +#include #include #include #include diff --git a/src/netbase.cpp b/src/netbase.cpp index 030f462ed9..4b8d2f8d0c 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -5,7 +5,7 @@ #include -#include +#include #include #include #include diff --git a/src/netbase.h b/src/netbase.h index bf7522210d..fadc8b418e 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -9,7 +9,7 @@ #include #endif -#include +#include #include #include #include diff --git a/src/qt/main.cpp b/src/qt/main.cpp index 38b0ac71a3..e8f39584ad 100644 --- a/src/qt/main.cpp +++ b/src/qt/main.cpp @@ -4,7 +4,7 @@ #include -#include +#include #include #include diff --git a/src/random.cpp b/src/random.cpp index fca4b5041a..f92e679a00 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -10,7 +10,7 @@ #include #include #ifdef WIN32 -#include // for Windows API +#include #include #endif #include diff --git a/src/randomenv.cpp b/src/randomenv.cpp index c5dca346d6..9e58180b7a 100644 --- a/src/randomenv.cpp +++ b/src/randomenv.cpp @@ -15,7 +15,7 @@ #include #include // for GetTime() #ifdef WIN32 -#include // for Windows API +#include #endif #include diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index b0893e7926..60e2875953 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index f2eaa0179f..f6642d3218 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/test/sock_tests.cpp b/src/test/sock_tests.cpp index 01a402833d..8376ec1a68 100644 --- a/src/test/sock_tests.cpp +++ b/src/test/sock_tests.cpp @@ -2,7 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include +#include #include #include #include diff --git a/src/test/util/net.h b/src/test/util/net.h index 7f61a03d27..ec6b4e6e88 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -5,7 +5,7 @@ #ifndef BITCOIN_TEST_UTIL_NET_H #define BITCOIN_TEST_UTIL_NET_H -#include +#include #include #include #include diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index d6e792a55f..3a21a79a34 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 2588575d81..125dbc7f18 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -2,7 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include +#include #include #include #include diff --git a/src/util/sock.h b/src/util/sock.h index b854609c22..38a7dc80d6 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -5,7 +5,7 @@ #ifndef BITCOIN_UTIL_SOCK_H #define BITCOIN_UTIL_SOCK_H -#include +#include #include #include diff --git a/src/util/system.h b/src/util/system.h index 04c66341d3..fa03e88920 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -14,7 +14,7 @@ #include #endif -#include +#include #include #include #include diff --git a/src/util/time.cpp b/src/util/time.cpp index 2cafc55c69..f6d37347f8 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -7,7 +7,7 @@ #include #endif -#include +#include #include #include #include diff --git a/src/util/time.h b/src/util/time.h index fc49f23ce3..c75b1e94ed 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -6,7 +6,7 @@ #ifndef BITCOIN_UTIL_TIME_H #define BITCOIN_UTIL_TIME_H -#include +#include #include #include From 7c3df5e548ee3404d1ad5b47410dd7b6f77258d3 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 14:53:28 +0100 Subject: [PATCH 2/8] compat: document FD_SETSIZE redefinition for WIN32 --- src/compat/compat.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/compat/compat.h b/src/compat/compat.h index 51ed03eef4..75f47bc700 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -10,14 +10,17 @@ #include #endif +// Windows defines FD_SETSIZE to 64 (see _fd_types.h in mingw-w64), +// which is too small for our usage, but allows us to redefine it safely. +// We redefine it to be 1024, to match glibc, see typesizes.h. #ifdef WIN32 #ifdef FD_SETSIZE -#undef FD_SETSIZE // prevent redefinition compiler warning +#undef FD_SETSIZE #endif -#define FD_SETSIZE 1024 // max number of fds in fd_set +#define FD_SETSIZE 1024 #include #include -#include +#include #else #include #include From b63ddb7e6d5c0463b4b8888ae015df87a381c0f6 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 15:00:52 +0100 Subject: [PATCH 3/8] compat: remove unused WSA* definitions --- src/compat/compat.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/compat/compat.h b/src/compat/compat.h index 75f47bc700..5c9639be26 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -42,14 +42,12 @@ typedef unsigned int SOCKET; #include #define WSAGetLastError() errno #define WSAEINVAL EINVAL -#define WSAEALREADY EALREADY #define WSAEWOULDBLOCK EWOULDBLOCK #define WSAEAGAIN EAGAIN #define WSAEMSGSIZE EMSGSIZE #define WSAEINTR EINTR #define WSAEINPROGRESS EINPROGRESS #define WSAEADDRINUSE EADDRINUSE -#define WSAENOTSOCK EBADF #define INVALID_SOCKET (SOCKET)(~0) #define SOCKET_ERROR -1 #else From 203e682d22a89af23dab21418e841e3b54b136d4 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 16:54:01 +0100 Subject: [PATCH 4/8] compat: extract and document MAX_PATH --- src/compat/compat.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/compat/compat.h b/src/compat/compat.h index 5c9639be26..21ec3b59b9 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -65,9 +65,14 @@ typedef unsigned int SOCKET; #define S_IRUSR 0400 #define S_IWUSR 0200 #endif -#else +#endif + +// Windows defines MAX_PATH as it's maximum path length. +// We define MAX_PATH for use on non-Windows systems. +#ifndef WIN32 #define MAX_PATH 1024 #endif + #ifdef _MSC_VER #if !defined(ssize_t) #ifdef _WIN64 From fb6db6fb0eb96f96dc331f565acaa8193f285ab2 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 16:54:38 +0100 Subject: [PATCH 5/8] compat: document S_I* defines when building for Windows --- src/compat/compat.h | 2 ++ src/wallet/bdb.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/src/compat/compat.h b/src/compat/compat.h index 21ec3b59b9..be97c6fe0b 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -60,6 +60,8 @@ typedef unsigned int SOCKET; #endif #endif +// Windows doesn't define S_IRUSR or S_IWUSR. We define both +// here, with the same values as glibc (see stat.h). #ifdef WIN32 #ifndef S_IRUSR #define S_IRUSR 0400 diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index dbd768a758..60715ff3c8 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -3,6 +3,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include From 3f1d2fb035bf6413c33847326ac5938802cd5860 Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 17:09:45 +0100 Subject: [PATCH 6/8] compat: document sockopt_arg_type definition --- src/compat/compat.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compat/compat.h b/src/compat/compat.h index be97c6fe0b..633aa6f3b5 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -85,6 +85,8 @@ typedef int32_t ssize_t; #endif #endif +// The type of the option value passed to getsockopt & setsockopt +// differs between Windows and non-Windows. #ifndef WIN32 typedef void* sockopt_arg_type; #else From 3be7ee750fd0d31d6e995140025e0d18e6aa788e Mon Sep 17 00:00:00 2001 From: fanquake Date: Tue, 28 Jun 2022 17:37:32 +0100 Subject: [PATCH 7/8] compat: document error-code mapping See: https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2 --- src/compat/compat.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/compat/compat.h b/src/compat/compat.h index 633aa6f3b5..670da9a631 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -37,9 +37,12 @@ #include #endif +// We map Linux / BSD error functions and codes, to the equivalent +// Windows definitions, and use the WSA* names throughout our code. +// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h). #ifndef WIN32 typedef unsigned int SOCKET; -#include +#include #define WSAGetLastError() errno #define WSAEINVAL EINVAL #define WSAEWOULDBLOCK EWOULDBLOCK @@ -51,14 +54,13 @@ typedef unsigned int SOCKET; #define INVALID_SOCKET (SOCKET)(~0) #define SOCKET_ERROR -1 #else -#ifndef WSAEAGAIN +// WSAEAGAIN doesn't exist on Windows #ifdef EAGAIN #define WSAEAGAIN EAGAIN #else #define WSAEAGAIN WSAEWOULDBLOCK #endif #endif -#endif // Windows doesn't define S_IRUSR or S_IWUSR. We define both // here, with the same values as glibc (see stat.h). From f7dc99244c8e78dbd0196f612690efcc449c37dc Mon Sep 17 00:00:00 2001 From: fanquake Date: Wed, 29 Jun 2022 12:18:44 +0100 Subject: [PATCH 8/8] compat: document redefining ssize_t when using MSVC See: https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#ssize_t https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html --- src/compat/compat.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/compat/compat.h b/src/compat/compat.h index 670da9a631..a8e5552c0a 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -77,14 +77,10 @@ typedef unsigned int SOCKET; #define MAX_PATH 1024 #endif +// ssize_t is POSIX, and not present when using MSVC. #ifdef _MSC_VER -#if !defined(ssize_t) -#ifdef _WIN64 -typedef int64_t ssize_t; -#else -typedef int32_t ssize_t; -#endif -#endif +#include +typedef SSIZE_T ssize_t; #endif // The type of the option value passed to getsockopt & setsockopt