/Users/eugenesiegel/btc/bitcoin/src/compat/compat.h
| Line | Count | Source (jump to first uncovered line) | 
| 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 | 
| 38 |  | typedef 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 | 
| 45 |  | typedef unsigned int SOCKET; | 
| 46 |  | #include <cerrno> | 
| 47 | 11.0k | #define WSAGetLastError()   errno | 
| 48 | 0 | #define WSAEINVAL           EINVAL | 
| 49 | 22.0k | #define WSAEWOULDBLOCK      EWOULDBLOCK | 
| 50 | 0 | #define WSAEAGAIN           EAGAIN | 
| 51 | 21.2k | #define WSAEMSGSIZE         EMSGSIZE | 
| 52 | 21.1k | #define WSAEINTR            EINTR | 
| 53 | 10.0k | #define WSAEINPROGRESS      EINPROGRESS | 
| 54 | 0 | #define WSAEADDRINUSE       EADDRINUSE | 
| 55 | 769k | #define INVALID_SOCKET      (SOCKET)(~0) | 
| 56 | 0 | #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> | 
| 75 |  | typedef SSIZE_T ssize_t; | 
| 76 |  | #endif | 
| 77 |  |  | 
| 78 |  | #ifdef WIN32 | 
| 79 |  | // Export main() and ensure working ASLR when using mingw-w64. | 
| 80 |  | // Exporting a symbol will prevent the linker from stripping | 
| 81 |  | // the .reloc section from the binary, which is a requirement | 
| 82 |  | // for ASLR. While release builds are not affected, anyone | 
| 83 |  | // building with a binutils < 2.36 is subject to this ld bug. | 
| 84 |  | #define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[]) | 
| 85 |  | #else | 
| 86 |  | #define MAIN_FUNCTION int main(int argc, char* argv[]) | 
| 87 |  | #endif | 
| 88 |  |  | 
| 89 |  | // Note these both should work with the current usage of poll, but best to be safe | 
| 90 |  | // WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/ | 
| 91 |  | // __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408 | 
| 92 |  | #if defined(__linux__) | 
| 93 |  | #define USE_POLL | 
| 94 |  | #endif | 
| 95 |  |  | 
| 96 |  | // MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0 | 
| 97 |  | #if !defined(MSG_NOSIGNAL) | 
| 98 |  | #define MSG_NOSIGNAL 0 | 
| 99 |  | #endif | 
| 100 |  |  | 
| 101 |  | // MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0 | 
| 102 |  | #if !defined(MSG_DONTWAIT) | 
| 103 |  | #define MSG_DONTWAIT 0 | 
| 104 |  | #endif | 
| 105 |  |  | 
| 106 |  | #endif // BITCOIN_COMPAT_COMPAT_H |