Bitcoin ABC  0.22.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_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 
22 #include <winsock2.h> // Must be included before mswsock.h and windows.h
23 
24 #include <mswsock.h>
25 #include <windows.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 #ifndef WIN32
44 typedef unsigned int SOCKET;
45 #include <cerrno>
46 #define WSAGetLastError() errno
47 #define WSAEINVAL EINVAL
48 #define WSAEALREADY EALREADY
49 #define WSAEWOULDBLOCK EWOULDBLOCK
50 #define WSAEMSGSIZE EMSGSIZE
51 #define WSAEINTR EINTR
52 #define WSAEINPROGRESS EINPROGRESS
53 #define WSAEADDRINUSE EADDRINUSE
54 #define WSAENOTSOCK EBADF
55 #define INVALID_SOCKET (SOCKET)(~0)
56 #define SOCKET_ERROR -1
57 #endif
58 
59 #ifdef WIN32
60 #ifndef S_IRUSR
61 #define S_IRUSR 0400
62 #define S_IWUSR 0200
63 #endif
64 #else
65 #define MAX_PATH 1024
66 #endif
67 #ifdef _MSC_VER
68 #if !defined(ssize_t)
69 #ifdef _WIN64
70 typedef int64_t ssize_t;
71 #else
72 typedef int32_t ssize_t;
73 #endif
74 #endif
75 #endif
76 
77 #if HAVE_DECL_STRNLEN == 0
78 size_t strnlen(const char *start, size_t max_len);
79 #endif // HAVE_DECL_STRNLEN
80 
81 #ifndef WIN32
82 typedef void *sockopt_arg_type;
83 #else
84 typedef char *sockopt_arg_type;
85 #endif
86 
87 // Note these both should work with the current usage of poll, but best to be
88 // safe
89 // WIN32 poll is broken
90 // https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
91 // __APPLE__ poll is broke
92 // https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
93 #if defined(__linux__)
94 #define USE_POLL
95 #endif
96 
97 static bool inline IsSelectableSocket(const SOCKET &s) {
98 #if defined(USE_POLL) || defined(WIN32)
99  return true;
100 #else
101  return (s < FD_SETSIZE);
102 #endif
103 }
104 
105 #endif // BITCOIN_COMPAT_H
static bool IsSelectableSocket(const SOCKET &s)
Definition: compat.h:97
void * sockopt_arg_type
Definition: compat.h:82
unsigned int SOCKET
Definition: compat.h:44
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12