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