1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <stdint.h>
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 #ifndef WIN32
41 typedef unsigned int SOCKET;
42 #include <errno.h>
43 #define WSAGetLastError()   errno
44 #define WSAEINVAL           EINVAL
45 #define WSAEALREADY         EALREADY
46 #define WSAEWOULDBLOCK      EWOULDBLOCK
47 #define WSAEAGAIN           EAGAIN
48 #define WSAEMSGSIZE         EMSGSIZE
49 #define WSAEINTR            EINTR
50 #define WSAEINPROGRESS      EINPROGRESS
51 #define WSAEADDRINUSE       EADDRINUSE
52 #define WSAENOTSOCK         EBADF
53 #define INVALID_SOCKET      (SOCKET)(~0)
54 #define SOCKET_ERROR        -1
55 #else
56 #ifndef WSAEAGAIN
57 #ifdef EAGAIN
58 #define WSAEAGAIN EAGAIN
59 #else
60 #define WSAEAGAIN WSAEWOULDBLOCK
61 #endif
62 #endif
63 #endif
64 
65 #ifdef WIN32
66 #ifndef S_IRUSR
67 #define S_IRUSR             0400
68 #define S_IWUSR             0200
69 #endif
70 #else
71 #define MAX_PATH            1024
72 #endif
73 #ifdef _MSC_VER
74 #if !defined(ssize_t)
75 #ifdef _WIN64
76 typedef int64_t ssize_t;
77 #else
78 typedef int32_t ssize_t;
79 #endif
80 #endif
81 #endif
82 
83 #if HAVE_DECL_STRNLEN == 0
84 size_t strnlen( const char *start, size_t max_len);
85 #endif // HAVE_DECL_STRNLEN
86 
87 #ifndef WIN32
88 typedef void* sockopt_arg_type;
89 #else
90 typedef char* sockopt_arg_type;
91 #endif
92 
93 // Note these both should work with the current usage of poll, but best to be safe
94 // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
95 // __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
96 #if defined(__linux__)
97 #define USE_POLL
98 #endif
99 
IsSelectableSocket(const SOCKET & s)100 bool static inline IsSelectableSocket(const SOCKET& s) {
101 #if defined(USE_POLL) || defined(WIN32)
102     return true;
103 #else
104     return (s < FD_SETSIZE);
105 #endif
106 }
107 
108 // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
109 #if !defined(MSG_NOSIGNAL)
110 #define MSG_NOSIGNAL 0
111 #endif
112 
113 // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
114 #if !defined(MSG_DONTWAIT)
115 #define MSG_DONTWAIT 0
116 #endif
117 
118 #endif // BITCOIN_COMPAT_H
119