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