2012-01-03 23:33:31 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 22:02:28 +08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2022-06-28 13:27:57 +01:00
|
|
|
#ifndef BITCOIN_COMPAT_COMPAT_H
|
|
|
|
#define BITCOIN_COMPAT_COMPAT_H
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2014-11-26 08:19:07 +01:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <config/bitcoin-config.h>
|
2014-11-26 08:19:07 +01:00
|
|
|
#endif
|
|
|
|
|
2022-06-28 14:53:28 +01:00
|
|
|
// Windows defines FD_SETSIZE to 64 (see _fd_types.h in mingw-w64),
|
|
|
|
// which is too small for our usage, but allows us to redefine it safely.
|
|
|
|
// We redefine it to be 1024, to match glibc, see typesizes.h.
|
2012-04-15 22:10:54 +02:00
|
|
|
#ifdef WIN32
|
2013-10-11 15:37:36 +02:00
|
|
|
#ifdef FD_SETSIZE
|
2022-06-28 14:53:28 +01:00
|
|
|
#undef FD_SETSIZE
|
2013-10-11 15:37:36 +02:00
|
|
|
#endif
|
2022-06-28 14:53:28 +01:00
|
|
|
#define FD_SETSIZE 1024
|
2020-10-22 18:21:47 +03:00
|
|
|
#include <winsock2.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#include <ws2tcpip.h>
|
2022-06-28 14:53:28 +01:00
|
|
|
#include <cstdint>
|
2012-04-15 22:10:54 +02:00
|
|
|
#else
|
2018-01-12 11:21:47 +00:00
|
|
|
#include <fcntl.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <sys/mman.h>
|
2016-11-27 12:08:39 +01:00
|
|
|
#include <sys/select.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <sys/socket.h>
|
2013-11-22 23:22:53 +10:00
|
|
|
#include <sys/types.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2015-10-21 23:52:29 +00:00
|
|
|
#include <netinet/tcp.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#include <arpa/inet.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#endif
|
|
|
|
|
2022-06-28 17:37:32 +01:00
|
|
|
// We map Linux / BSD error functions and codes, to the equivalent
|
|
|
|
// Windows definitions, and use the WSA* names throughout our code.
|
|
|
|
// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).
|
2017-03-05 09:29:37 +00:00
|
|
|
#ifndef WIN32
|
2017-03-05 09:51:21 +00:00
|
|
|
typedef unsigned int SOCKET;
|
2022-06-28 17:37:32 +01:00
|
|
|
#include <cerrno>
|
2012-01-03 23:33:31 +01:00
|
|
|
#define WSAGetLastError() errno
|
|
|
|
#define WSAEINVAL EINVAL
|
|
|
|
#define WSAEWOULDBLOCK EWOULDBLOCK
|
2020-12-04 15:11:25 +01:00
|
|
|
#define WSAEAGAIN EAGAIN
|
2012-01-03 23:33:31 +01:00
|
|
|
#define WSAEMSGSIZE EMSGSIZE
|
|
|
|
#define WSAEINTR EINTR
|
|
|
|
#define WSAEINPROGRESS EINPROGRESS
|
|
|
|
#define WSAEADDRINUSE EADDRINUSE
|
|
|
|
#define INVALID_SOCKET (SOCKET)(~0)
|
|
|
|
#define SOCKET_ERROR -1
|
2020-12-04 15:11:25 +01:00
|
|
|
#else
|
2022-06-28 17:37:32 +01:00
|
|
|
// WSAEAGAIN doesn't exist on Windows
|
2020-12-04 15:11:25 +01:00
|
|
|
#ifdef EAGAIN
|
|
|
|
#define WSAEAGAIN EAGAIN
|
|
|
|
#else
|
|
|
|
#define WSAEAGAIN WSAEWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2022-06-28 16:54:01 +01:00
|
|
|
// Windows defines MAX_PATH as it's maximum path length.
|
|
|
|
// We define MAX_PATH for use on non-Windows systems.
|
|
|
|
#ifndef WIN32
|
2014-08-21 15:50:59 +02:00
|
|
|
#define MAX_PATH 1024
|
|
|
|
#endif
|
2022-06-28 16:54:01 +01:00
|
|
|
|
2022-06-29 12:18:44 +01:00
|
|
|
// ssize_t is POSIX, and not present when using MSVC.
|
2017-11-10 07:06:49 +11:00
|
|
|
#ifdef _MSC_VER
|
2022-06-29 12:18:44 +01:00
|
|
|
#include <BaseTsd.h>
|
|
|
|
typedef SSIZE_T ssize_t;
|
2017-11-10 07:06:49 +11:00
|
|
|
#endif
|
2014-08-21 15:50:59 +02:00
|
|
|
|
2022-06-28 17:09:45 +01:00
|
|
|
// The type of the option value passed to getsockopt & setsockopt
|
|
|
|
// differs between Windows and non-Windows.
|
2018-01-26 02:48:56 -08:00
|
|
|
#ifndef WIN32
|
|
|
|
typedef void* sockopt_arg_type;
|
|
|
|
#else
|
|
|
|
typedef char* sockopt_arg_type;
|
|
|
|
#endif
|
|
|
|
|
2022-05-30 15:59:43 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
// Export main() and ensure working ASLR when using mingw-w64.
|
|
|
|
// Exporting a symbol will prevent the linker from stripping
|
|
|
|
// the .reloc section from the binary, which is a requirement
|
|
|
|
// for ASLR. While release builds are not affected, anyone
|
|
|
|
// building with a binutils < 2.36 is subject to this ld bug.
|
|
|
|
#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
|
|
|
|
#else
|
|
|
|
#define MAIN_FUNCTION int main(int argc, char* argv[])
|
|
|
|
#endif
|
|
|
|
|
2018-09-26 21:54:52 -04:00
|
|
|
// Note these both should work with the current usage of poll, but best to be safe
|
|
|
|
// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
|
|
|
|
// __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408
|
|
|
|
#if defined(__linux__)
|
|
|
|
#define USE_POLL
|
|
|
|
#endif
|
|
|
|
|
2021-02-12 15:56:15 +01:00
|
|
|
// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0
|
|
|
|
#if !defined(MSG_NOSIGNAL)
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0
|
|
|
|
#if !defined(MSG_DONTWAIT)
|
|
|
|
#define MSG_DONTWAIT 0
|
|
|
|
#endif
|
|
|
|
|
2022-06-28 13:27:57 +01:00
|
|
|
#endif // BITCOIN_COMPAT_COMPAT_H
|