Bitcoin Core  31.0.0
P2P Digital Currency
compat.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_COMPAT_COMPAT_H
7 #define BITCOIN_COMPAT_COMPAT_H
8 
9 // Windows defines FD_SETSIZE to 64 (see _fd_types.h in mingw-w64),
10 // which is too small for our usage, but allows us to redefine it safely.
11 // We redefine it to be 1024, to match glibc, see typesizes.h.
12 #ifdef WIN32
13 #ifdef FD_SETSIZE
14 #undef FD_SETSIZE
15 #endif
16 #define FD_SETSIZE 1024
17 #include <winsock2.h>
18 #include <ws2tcpip.h>
19 #include <cstdint>
20 #else
21 #include <arpa/inet.h> // IWYU pragma: export
22 #include <fcntl.h> // IWYU pragma: export
23 #include <net/if.h> // IWYU pragma: export
24 #include <netdb.h> // IWYU pragma: export
25 #include <netinet/in.h> // IWYU pragma: export
26 #include <netinet/tcp.h> // IWYU pragma: export
27 #include <sys/mman.h> // IWYU pragma: export
28 #include <sys/select.h> // IWYU pragma: export
29 #include <sys/socket.h> // IWYU pragma: export
30 #include <sys/types.h> // IWYU pragma: export
31 #include <unistd.h> // IWYU pragma: export
32 #endif
33 
34 // Windows does not have `sa_family_t` - it defines `sockaddr::sa_family` as `u_short`.
35 // Thus define `sa_family_t` on Windows too so that the rest of the code can use `sa_family_t`.
36 // See https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-sockaddr#syntax
37 #ifdef WIN32
38 typedef u_short sa_family_t;
39 #endif
40 
41 // Brace style in the IN6ADDR_*_INIT macros differs across platforms.
42 #if defined(__illumos__)
43 #define COMPAT_IN6ADDR_ANY_INIT {{IN6ADDR_ANY_INIT}}
44 #else
45 #define COMPAT_IN6ADDR_ANY_INIT IN6ADDR_ANY_INIT
46 #endif
47 #if defined(__illumos__) || defined(_MSC_VER)
48 #define COMPAT_IN6ADDR_LOOPBACK_INIT {{IN6ADDR_LOOPBACK_INIT}}
49 #else
50 #define COMPAT_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
51 #endif
52 
53 // We map Linux / BSD error functions and codes, to the equivalent
54 // Windows definitions, and use the WSA* names throughout our code.
55 // Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).
56 #ifndef WIN32
57 typedef unsigned int SOCKET;
58 #include <cerrno>
59 #define WSAGetLastError() errno
60 #define WSAEINVAL EINVAL
61 #define WSAEWOULDBLOCK EWOULDBLOCK
62 #define WSAEAGAIN EAGAIN
63 #define WSAEMSGSIZE EMSGSIZE
64 #define WSAEINTR EINTR
65 #define WSAEINPROGRESS EINPROGRESS
66 #define WSAEADDRINUSE EADDRINUSE
67 #define INVALID_SOCKET (SOCKET)(~0)
68 #define SOCKET_ERROR -1
69 #else
70 // WSAEAGAIN doesn't exist on Windows
71 #ifdef EAGAIN
72 #define WSAEAGAIN EAGAIN
73 #else
74 #define WSAEAGAIN WSAEWOULDBLOCK
75 #endif
76 #endif
77 
78 // Windows defines MAX_PATH as it's maximum path length.
79 // We define MAX_PATH for use on non-Windows systems.
80 #ifndef WIN32
81 #define MAX_PATH 1024
82 #endif
83 
84 // ssize_t is POSIX, and not present when using MSVC.
85 #ifdef _MSC_VER
86 #include <BaseTsd.h>
87 typedef SSIZE_T ssize_t;
88 #endif
89 
90 #ifdef WIN32
91 // Export main() and ensure working ASLR when using mingw-w64.
92 // Exporting a symbol will prevent the linker from stripping
93 // the .reloc section from the binary, which is a requirement
94 // for ASLR. While release builds are not affected, anyone
95 // building with a binutils < 2.36 is subject to this ld bug.
96 #define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
97 #else
98 #define MAIN_FUNCTION int main(int argc, char* argv[])
99 #endif
100 
101 // Note these both should work with the current usage of poll, but best to be safe
102 // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
103 // __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
104 #if defined(__linux__)
105 #define USE_POLL
106 #endif
107 
108 // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
109 #if !defined(MSG_NOSIGNAL)
110 #define MSG_NOSIGNAL 0
111 #endif
112 
113 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
114 #if !defined(MSG_DONTWAIT)
115 #define MSG_DONTWAIT 0
116 #endif
117 
118 #endif // BITCOIN_COMPAT_COMPAT_H
unsigned int SOCKET
Definition: compat.h:57