Bitcoin Core  26.99.0
P2P Digital Currency
compat.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <ifaddrs.h> // IWYU pragma: export
24 #include <net/if.h> // IWYU pragma: export
25 #include <netdb.h> // IWYU pragma: export
26 #include <netinet/in.h> // IWYU pragma: export
27 #include <netinet/tcp.h> // IWYU pragma: export
28 #include <sys/mman.h> // IWYU pragma: export
29 #include <sys/select.h> // IWYU pragma: export
30 #include <sys/socket.h> // IWYU pragma: export
31 #include <sys/types.h> // IWYU pragma: export
32 #include <unistd.h> // IWYU pragma: export
33 #endif
34 
35 // We map Linux / BSD error functions and codes, to the equivalent
36 // Windows definitions, and use the WSA* names throughout our code.
37 // Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).
38 #ifndef WIN32
39 typedef unsigned int SOCKET;
40 #include <cerrno>
41 #define WSAGetLastError() errno
42 #define WSAEINVAL EINVAL
43 #define WSAEWOULDBLOCK EWOULDBLOCK
44 #define WSAEAGAIN EAGAIN
45 #define WSAEMSGSIZE EMSGSIZE
46 #define WSAEINTR EINTR
47 #define WSAEINPROGRESS EINPROGRESS
48 #define WSAEADDRINUSE EADDRINUSE
49 #define INVALID_SOCKET (SOCKET)(~0)
50 #define SOCKET_ERROR -1
51 #else
52 // WSAEAGAIN doesn't exist on Windows
53 #ifdef EAGAIN
54 #define WSAEAGAIN EAGAIN
55 #else
56 #define WSAEAGAIN WSAEWOULDBLOCK
57 #endif
58 #endif
59 
60 // Windows defines MAX_PATH as it's maximum path length.
61 // We define MAX_PATH for use on non-Windows systems.
62 #ifndef WIN32
63 #define MAX_PATH 1024
64 #endif
65 
66 // ssize_t is POSIX, and not present when using MSVC.
67 #ifdef _MSC_VER
68 #include <BaseTsd.h>
69 typedef SSIZE_T ssize_t;
70 #endif
71 
72 // The type of the option value passed to getsockopt & setsockopt
73 // differs between Windows and non-Windows.
74 #ifndef WIN32
75 typedef void* sockopt_arg_type;
76 #else
77 typedef char* sockopt_arg_type;
78 #endif
79 
80 #ifdef WIN32
81 // Export main() and ensure working ASLR when using mingw-w64.
82 // Exporting a symbol will prevent the linker from stripping
83 // the .reloc section from the binary, which is a requirement
84 // for ASLR. While release builds are not affected, anyone
85 // building with a binutils < 2.36 is subject to this ld bug.
86 #define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
87 #else
88 #define MAIN_FUNCTION int main(int argc, char* argv[])
89 #endif
90 
91 // Note these both should work with the current usage of poll, but best to be safe
92 // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
93 // __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
94 #if defined(__linux__)
95 #define USE_POLL
96 #endif
97 
98 // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
99 #if !defined(MSG_NOSIGNAL)
100 #define MSG_NOSIGNAL 0
101 #endif
102 
103 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
104 #if !defined(MSG_DONTWAIT)
105 #define MSG_DONTWAIT 0
106 #endif
107 
108 #endif // BITCOIN_COMPAT_COMPAT_H
unsigned int SOCKET
Definition: compat.h:39
void * sockopt_arg_type
Definition: compat.h:75