Bitcoin ABC 0.30.5
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_H
7#define BITCOIN_COMPAT_H
8
9#if defined(HAVE_CONFIG_H)
10#include <config/bitcoin-config.h>
11#endif
12
13#ifdef WIN32
14#ifndef NOMINMAX
15#define NOMINMAX
16#endif
17#ifdef FD_SETSIZE
18#undef FD_SETSIZE // prevent redefinition compiler warning
19#endif
20#define FD_SETSIZE 1024 // max number of fds in fd_set
21#include <winsock2.h>
22#include <ws2tcpip.h>
23#else
24#include <arpa/inet.h>
25#include <climits>
26#include <ifaddrs.h>
27#include <net/if.h>
28#include <netdb.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#include <sys/fcntl.h>
32#include <sys/mman.h>
33#include <sys/select.h>
34#include <sys/socket.h>
35#include <sys/types.h>
36#include <unistd.h>
37#endif
38
39#ifndef WIN32
40typedef unsigned int SOCKET;
41#include <cerrno>
42#define WSAGetLastError() errno
43#define WSAEINVAL EINVAL
44#define WSAEALREADY EALREADY
45#define WSAEWOULDBLOCK EWOULDBLOCK
46#define WSAEAGAIN EAGAIN
47#define WSAEMSGSIZE EMSGSIZE
48#define WSAEINTR EINTR
49#define WSAEINPROGRESS EINPROGRESS
50#define WSAEADDRINUSE EADDRINUSE
51#define WSAENOTSOCK EBADF
52#define INVALID_SOCKET (SOCKET)(~0)
53#define SOCKET_ERROR -1
54#else
55#ifndef WSAEAGAIN
56#ifdef EAGAIN
57#define WSAEAGAIN EAGAIN
58#else
59#define WSAEAGAIN WSAEWOULDBLOCK
60#endif
61#endif
62#endif
63
64#ifdef WIN32
65#ifndef S_IRUSR
66#define S_IRUSR 0400
67#define S_IWUSR 0200
68#endif
69#else
70#define MAX_PATH 1024
71#endif
72#ifdef _MSC_VER
73#if !defined(ssize_t)
74#ifdef _WIN64
75typedef int64_t ssize_t;
76#else
77typedef int32_t ssize_t;
78#endif
79#endif
80#endif
81
82#if HAVE_DECL_STRNLEN == 0
83size_t strnlen(const char *start, size_t max_len);
84#endif // HAVE_DECL_STRNLEN
85
86#ifndef WIN32
87typedef void *sockopt_arg_type;
88#else
89typedef char *sockopt_arg_type;
90#endif
91
92// Note these both should work with the current usage of poll, but best to be
93// safe
94// WIN32 poll is broken
95// https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
96// __APPLE__ poll is broke
97// https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
98#if defined(__linux__)
99#define USE_POLL
100#endif
101
102static bool inline IsSelectableSocket(const SOCKET &s) {
103#if defined(USE_POLL) || defined(WIN32)
104 return true;
105#else
106 return (s < FD_SETSIZE);
107#endif
108}
109
110// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define
111// it as 0
112#if !defined(MSG_NOSIGNAL)
113#define MSG_NOSIGNAL 0
114#endif
115
116// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define
117// it as 0
118#if !defined(MSG_DONTWAIT)
119#define MSG_DONTWAIT 0
120#endif
121
122#endif // BITCOIN_COMPAT_H
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
static bool IsSelectableSocket(const SOCKET &s)
Definition: compat.h:102
unsigned int SOCKET
Definition: compat.h:40
void * sockopt_arg_type
Definition: compat.h:87