Bitcoin ABC 0.32.12
P2P Digital Currency
compat.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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#ifndef NOMINMAX
18#define NOMINMAX
19#endif
20#ifdef FD_SETSIZE
21#undef FD_SETSIZE
22#endif
23#define FD_SETSIZE 1024
24#include <cstdint>
25#include <winsock2.h>
26#include <ws2tcpip.h>
27#else
28#include <arpa/inet.h>
29#include <climits>
30#include <ifaddrs.h>
31#include <net/if.h>
32#include <netdb.h>
33#include <netinet/in.h>
34#include <netinet/tcp.h>
35#include <sys/fcntl.h>
36#include <sys/mman.h>
37#include <sys/select.h>
38#include <sys/socket.h>
39#include <sys/types.h>
40#include <unistd.h>
41#endif
42
43// Windows does not have `sa_family_t` - it defines `sockaddr::sa_family` as
44// `u_short`. Thus define `sa_family_t` on Windows too so that the rest of the
45// code can use `sa_family_t`. See
46// https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-sockaddr#syntax
47#ifdef WIN32
48typedef u_short sa_family_t;
49#endif
50
51// We map Linux / BSD error functions and codes, to the equivalent
52// Windows definitions, and use the WSA* names throughout our code.
53// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).
54#ifndef WIN32
55typedef unsigned int SOCKET;
56#include <cerrno>
57#define WSAGetLastError() errno
58#define WSAEINVAL EINVAL
59#define WSAEWOULDBLOCK EWOULDBLOCK
60#define WSAEAGAIN EAGAIN
61#define WSAEMSGSIZE EMSGSIZE
62#define WSAEINTR EINTR
63#define WSAEINPROGRESS EINPROGRESS
64#define WSAEADDRINUSE EADDRINUSE
65#define INVALID_SOCKET (SOCKET)(~0)
66#define SOCKET_ERROR -1
67#else
68// WSAEAGAIN doesn't exist on Windows
69#ifdef EAGAIN
70#define WSAEAGAIN EAGAIN
71#else
72#define WSAEAGAIN WSAEWOULDBLOCK
73#endif
74#endif
75
76// Windows doesn't define S_IRUSR or S_IWUSR. We define both
77// here, with the same values as glibc (see stat.h).
78#ifdef WIN32
79#ifndef S_IRUSR
80#define S_IRUSR 0400
81#define S_IWUSR 0200
82#endif
83#endif
84
85// Windows defines MAX_PATH as it's maximum path length.
86// We define MAX_PATH for use on non-Windows systems.
87#ifndef WIN32
88#define MAX_PATH 1024
89#endif
90
91// ssize_t is POSIX, and not present when using MSVC.
92#ifdef _MSC_VER
93#include <BaseTsd.h>
94typedef SSIZE_T ssize_t;
95#endif
96
97#if HAVE_DECL_STRNLEN == 0
98size_t strnlen(const char *start, size_t max_len);
99#endif // HAVE_DECL_STRNLEN
100
101// The type of the option value passed to getsockopt & setsockopt
102// differs between Windows and non-Windows.
103#ifndef WIN32
104typedef void *sockopt_arg_type;
105#else
106typedef char *sockopt_arg_type;
107#endif
108
109// Note these both should work with the current usage of poll, but best to be
110// safe
111// WIN32 poll is broken
112// https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
113// __APPLE__ poll is broke
114// https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
115#if defined(__linux__)
116#define USE_POLL
117#endif
118
119// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define
120// it as 0
121#if !defined(MSG_NOSIGNAL)
122#define MSG_NOSIGNAL 0
123#endif
124
125// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define
126// it as 0
127#if !defined(MSG_DONTWAIT)
128#define MSG_DONTWAIT 0
129#endif
130
131#endif // BITCOIN_COMPAT_COMPAT_H
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
unsigned int SOCKET
Definition: compat.h:55
void * sockopt_arg_type
Definition: compat.h:104