Bitcoin Core 28.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// Windows does not have `sa_family_t` - it defines `sockaddr::sa_family` as `u_short`.
36// Thus define `sa_family_t` on Windows too so that the rest of the code can use `sa_family_t`.
37// See https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-sockaddr#syntax
38#ifdef WIN32
39typedef u_short sa_family_t;
40#endif
41
42// We map Linux / BSD error functions and codes, to the equivalent
43// Windows definitions, and use the WSA* names throughout our code.
44// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).
45#ifndef WIN32
46typedef unsigned int SOCKET;
47#include <cerrno>
48#define WSAGetLastError() errno
49#define WSAEINVAL EINVAL
50#define WSAEWOULDBLOCK EWOULDBLOCK
51#define WSAEAGAIN EAGAIN
52#define WSAEMSGSIZE EMSGSIZE
53#define WSAEINTR EINTR
54#define WSAEINPROGRESS EINPROGRESS
55#define WSAEADDRINUSE EADDRINUSE
56#define INVALID_SOCKET (SOCKET)(~0)
57#define SOCKET_ERROR -1
58#else
59// WSAEAGAIN doesn't exist on Windows
60#ifdef EAGAIN
61#define WSAEAGAIN EAGAIN
62#else
63#define WSAEAGAIN WSAEWOULDBLOCK
64#endif
65#endif
66
67// Windows defines MAX_PATH as it's maximum path length.
68// We define MAX_PATH for use on non-Windows systems.
69#ifndef WIN32
70#define MAX_PATH 1024
71#endif
72
73// ssize_t is POSIX, and not present when using MSVC.
74#ifdef _MSC_VER
75#include <BaseTsd.h>
76typedef SSIZE_T ssize_t;
77#endif
78
79// The type of the option value passed to getsockopt & setsockopt
80// differs between Windows and non-Windows.
81#ifndef WIN32
82typedef void* sockopt_arg_type;
83#else
84typedef char* sockopt_arg_type;
85#endif
86
87#ifdef WIN32
88// Export main() and ensure working ASLR when using mingw-w64.
89// Exporting a symbol will prevent the linker from stripping
90// the .reloc section from the binary, which is a requirement
91// for ASLR. While release builds are not affected, anyone
92// building with a binutils < 2.36 is subject to this ld bug.
93#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
94#else
95#define MAIN_FUNCTION int main(int argc, char* argv[])
96#endif
97
98// Note these both should work with the current usage of poll, but best to be safe
99// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
100// __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
101#if defined(__linux__)
102#define USE_POLL
103#endif
104
105// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
106#if !defined(MSG_NOSIGNAL)
107#define MSG_NOSIGNAL 0
108#endif
109
110// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
111#if !defined(MSG_DONTWAIT)
112#define MSG_DONTWAIT 0
113#endif
114
115#endif // BITCOIN_COMPAT_COMPAT_H
unsigned int SOCKET
Definition: compat.h:46
void * sockopt_arg_type
Definition: compat.h:82