aboutsummaryrefslogtreecommitdiff
path: root/src/compat
diff options
context:
space:
mode:
authorMacroFake <falke.marco@gmail.com>2022-07-20 15:50:51 +0200
committerMacroFake <falke.marco@gmail.com>2022-07-20 15:50:58 +0200
commit5c82ca3365f6451d342c94cedc3f9b015598e9c2 (patch)
tree78ffaf25b923c42df40e382ab0688cd5f9901275 /src/compat
parent1eedde157f2e475243ad1e0527309d85f423d6bb (diff)
parentf7dc99244c8e78dbd0196f612690efcc449c37dc (diff)
downloadbitcoin-5c82ca3365f6451d342c94cedc3f9b015598e9c2.tar.xz
Merge bitcoin/bitcoin#25493: compat: document code in compat.h
f7dc99244c8e78dbd0196f612690efcc449c37dc compat: document redefining ssize_t when using MSVC (fanquake) 3be7ee750fd0d31d6e995140025e0d18e6aa788e compat: document error-code mapping (fanquake) 3f1d2fb035bf6413c33847326ac5938802cd5860 compat: document sockopt_arg_type definition (fanquake) fb6db6fb0eb96f96dc331f565acaa8193f285ab2 compat: document S_I* defines when building for Windows (fanquake) 203e682d22a89af23dab21418e841e3b54b136d4 compat: extract and document MAX_PATH (fanquake) b63ddb7e6d5c0463b4b8888ae015df87a381c0f6 compat: remove unused WSA* definitions (fanquake) 7c3df5e548ee3404d1ad5b47410dd7b6f77258d3 compat: document FD_SETSIZE redefinition for WIN32 (fanquake) cc7b2fdd70da439c3cf8daef3bb79cf593f1cef3 refactor: move compat.h into compat/ (fanquake) Pull request description: Move `compat.h` into `compat/`, and document what is in there. ACKs for top commit: vasild: ACK f7dc99244c8e78dbd0196f612690efcc449c37dc hebasto: re-ACK f7dc99244c8e78dbd0196f612690efcc449c37dc Tree-SHA512: 9e7e90261a97eae7998ef8d140d8ffab504cccf19abb44ca253d8919a067bb01e3fa9876a44194a1a9fb08a4b0489376844f827d8a27aa66c0f99c60ad5d7041
Diffstat (limited to 'src/compat')
-rw-r--r--src/compat/compat.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/compat/compat.h b/src/compat/compat.h
new file mode 100644
index 0000000000..a8e5552c0a
--- /dev/null
+++ b/src/compat/compat.h
@@ -0,0 +1,130 @@
+// Copyright (c) 2009-2010 Satoshi Nakamoto
+// Copyright (c) 2009-2021 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_COMPAT_COMPAT_H
+#define BITCOIN_COMPAT_COMPAT_H
+
+#if defined(HAVE_CONFIG_H)
+#include <config/bitcoin-config.h>
+#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
+#endif
+#define FD_SETSIZE 1024
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#include <cstdint>
+#else
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <ifaddrs.h>
+#include <limits.h>
+#include <netdb.h>
+#include <unistd.h>
+#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 <cerrno>
+#define WSAGetLastError() errno
+#define WSAEINVAL EINVAL
+#define WSAEWOULDBLOCK EWOULDBLOCK
+#define WSAEAGAIN EAGAIN
+#define WSAEMSGSIZE EMSGSIZE
+#define WSAEINTR EINTR
+#define WSAEINPROGRESS EINPROGRESS
+#define WSAEADDRINUSE EADDRINUSE
+#define INVALID_SOCKET (SOCKET)(~0)
+#define SOCKET_ERROR -1
+#else
+// WSAEAGAIN doesn't exist on Windows
+#ifdef EAGAIN
+#define WSAEAGAIN EAGAIN
+#else
+#define WSAEAGAIN WSAEWOULDBLOCK
+#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
+#define S_IWUSR 0200
+#endif
+#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
+
+// ssize_t is POSIX, and not present when using MSVC.
+#ifdef _MSC_VER
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#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
+typedef char* sockopt_arg_type;
+#endif
+
+#ifdef WIN32
+// Export main() and ensure working ASLR when using mingw-w64.
+// Exporting a symbol will prevent the linker from stripping
+// the .reloc section from the binary, which is a requirement
+// for ASLR. While release builds are not affected, anyone
+// building with a binutils < 2.36 is subject to this ld bug.
+#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
+#else
+#define MAIN_FUNCTION int main(int argc, char* argv[])
+#endif
+
+// Note these both should work with the current usage of poll, but best to be safe
+// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
+// __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
+#if defined(__linux__)
+#define USE_POLL
+#endif
+
+bool static inline IsSelectableSocket(const SOCKET& s) {
+#if defined(USE_POLL) || defined(WIN32)
+ return true;
+#else
+ return (s < FD_SETSIZE);
+#endif
+}
+
+// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
+#if !defined(MSG_NOSIGNAL)
+#define MSG_NOSIGNAL 0
+#endif
+
+// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
+#if !defined(MSG_DONTWAIT)
+#define MSG_DONTWAIT 0
+#endif
+
+#endif // BITCOIN_COMPAT_COMPAT_H