2012-01-03 23:33:31 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 18:50:11 +01:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_COMPAT_H
|
|
|
|
#define BITCOIN_COMPAT_H
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2014-11-26 08:19:07 +01:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2022-08-02 18:34:58 +02:00
|
|
|
#include <config/bitcoin-config.h>
|
2014-11-26 08:19:07 +01:00
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2012-04-15 22:10:54 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
2013-10-11 15:37:36 +02:00
|
|
|
#ifdef FD_SETSIZE
|
|
|
|
#undef FD_SETSIZE // prevent redefinition compiler warning
|
|
|
|
#endif
|
2013-04-26 01:05:35 +02:00
|
|
|
#define FD_SETSIZE 1024 // max number of fds in fd_set
|
2020-12-03 14:52:23 +01:00
|
|
|
#include <winsock2.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#include <ws2tcpip.h>
|
2017-12-13 13:32:00 +01:00
|
|
|
#include <stdint.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#else
|
2018-01-13 15:32:33 +01:00
|
|
|
#include <fcntl.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <sys/mman.h>
|
2016-11-29 12:37:33 +01:00
|
|
|
#include <sys/select.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <sys/socket.h>
|
2013-11-22 14:22:53 +01:00
|
|
|
#include <sys/types.h>
|
2014-05-02 20:45:03 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2015-10-22 01:52:29 +02:00
|
|
|
#include <netinet/tcp.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#include <arpa/inet.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
2012-04-15 22:10:54 +02:00
|
|
|
#endif
|
|
|
|
|
2017-03-16 12:02:54 +01:00
|
|
|
#ifndef WIN32
|
|
|
|
typedef unsigned int SOCKET;
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <errno.h>
|
2012-01-03 23:33:31 +01:00
|
|
|
#define WSAGetLastError() errno
|
|
|
|
#define WSAEINVAL EINVAL
|
|
|
|
#define WSAEALREADY EALREADY
|
|
|
|
#define WSAEWOULDBLOCK EWOULDBLOCK
|
2020-11-18 17:13:27 +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 WSAENOTSOCK EBADF
|
|
|
|
#define INVALID_SOCKET (SOCKET)(~0)
|
|
|
|
#define SOCKET_ERROR -1
|
2020-04-22 07:50:40 +02:00
|
|
|
#define SD_SEND SHUT_WR
|
2020-11-18 17:13:27 +01:00
|
|
|
#else
|
|
|
|
#ifndef WSAEAGAIN
|
|
|
|
#ifdef EAGAIN
|
|
|
|
#define WSAEAGAIN EAGAIN
|
|
|
|
#else
|
|
|
|
#define WSAEAGAIN WSAEWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2014-08-21 15:50:59 +02:00
|
|
|
#ifndef S_IRUSR
|
|
|
|
#define S_IRUSR 0400
|
|
|
|
#define S_IWUSR 0200
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
#else
|
2014-08-21 15:50:59 +02:00
|
|
|
#define MAX_PATH 1024
|
2012-01-03 23:33:31 +01:00
|
|
|
#endif
|
2017-12-13 13:32:00 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#if !defined(ssize_t)
|
|
|
|
#ifdef _WIN64
|
|
|
|
typedef int64_t ssize_t;
|
|
|
|
#else
|
|
|
|
typedef int32_t ssize_t;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2016-02-14 05:51:06 +01:00
|
|
|
#if HAVE_DECL_STRNLEN == 0
|
|
|
|
size_t strnlen( const char *start, size_t max_len);
|
|
|
|
#endif // HAVE_DECL_STRNLEN
|
2014-11-21 10:38:27 +01:00
|
|
|
|
2018-04-19 14:24:26 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
typedef void* sockopt_arg_type;
|
|
|
|
#else
|
|
|
|
typedef char* sockopt_arg_type;
|
|
|
|
#endif
|
|
|
|
|
2022-06-13 21:43:22 +02: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-27 03:54:52 +02: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
|
2020-12-21 22:33:06 +01:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__)
|
2018-09-27 03:54:52 +02:00
|
|
|
#define USE_POLL
|
|
|
|
#endif
|
|
|
|
|
2020-04-07 17:58:38 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
#define USE_EPOLL
|
|
|
|
#endif
|
|
|
|
|
2020-12-30 20:34:42 +01:00
|
|
|
#if defined(__FreeBSD__) || defined(__APPLE__)
|
|
|
|
#define USE_KQUEUE
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 14:58:25 +02:00
|
|
|
bool static inline IsSelectableSocket(const SOCKET& s) {
|
2018-09-27 03:54:52 +02:00
|
|
|
#if defined(USE_POLL) || defined(WIN32)
|
2015-07-10 00:23:27 +02:00
|
|
|
return true;
|
|
|
|
#else
|
2015-07-20 17:10:02 +02:00
|
|
|
return (s < FD_SETSIZE);
|
2015-07-10 00:23:27 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-18 17:13:27 +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
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_COMPAT_H
|