Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_COMPAT_COMPAT_H7#define BITCOIN_COMPAT_COMPAT_H9// 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 WIN3213#ifdef FD_SETSIZE14#undef FD_SETSIZE15#endif16#define FD_SETSIZE 102417#include <winsock2.h>18#include <ws2tcpip.h>19#include <cstdint>20#else21#include <arpa/inet.h> // IWYU pragma: export22#include <fcntl.h> // IWYU pragma: export23#include <net/if.h> // IWYU pragma: export24#include <netdb.h> // IWYU pragma: export25#include <netinet/in.h> // IWYU pragma: export26#include <netinet/tcp.h> // IWYU pragma: export27#include <sys/mman.h> // IWYU pragma: export28#include <sys/select.h> // IWYU pragma: export29#include <sys/socket.h> // IWYU pragma: export30#include <sys/types.h> // IWYU pragma: export31#include <unistd.h> // IWYU pragma: export32#endif34// 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#syntax37#ifdef WIN3238typedef u_short sa_family_t;39#endif41// Brace style in the IN6ADDR_*_INIT macros differs across platforms.42#if defined(__illumos__)43#define COMPAT_IN6ADDR_ANY_INIT {{IN6ADDR_ANY_INIT}}44#else45#define COMPAT_IN6ADDR_ANY_INIT IN6ADDR_ANY_INIT46#endif47#if defined(__illumos__) || defined(_MSC_VER)48#define COMPAT_IN6ADDR_LOOPBACK_INIT {{IN6ADDR_LOOPBACK_INIT}}49#else50#define COMPAT_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT51#endif53// We map Linux / BSD error functions and codes, to the equivalent54// Windows definitions, and use the WSA* names throughout our code.55// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h).56#ifndef WIN3257typedef unsigned int SOCKET;58#include <cerrno>59#define WSAGetLastError() errno60#define WSAEINVAL EINVAL61#define WSAEWOULDBLOCK EWOULDBLOCK62#define WSAEAGAIN EAGAIN63#define WSAEMSGSIZE EMSGSIZE64#define WSAEINTR EINTR65#define WSAEINPROGRESS EINPROGRESS66#define WSAEADDRINUSE EADDRINUSE67#define INVALID_SOCKET (SOCKET)(~0)68#define SOCKET_ERROR -169#else70// WSAEAGAIN doesn't exist on Windows71#ifdef EAGAIN72#define WSAEAGAIN EAGAIN73#else74#define WSAEAGAIN WSAEWOULDBLOCK75#endif76#endif78// Windows defines MAX_PATH as it's maximum path length.79// We define MAX_PATH for use on non-Windows systems.80#ifndef WIN3281#define MAX_PATH 102482#endif84// ssize_t is POSIX, and not present when using MSVC.85#ifdef _MSC_VER86#include <BaseTsd.h>87typedef SSIZE_T ssize_t;88#endif90#ifdef WIN3291// Export main() and ensure working ASLR when using mingw-w64.92// Exporting a symbol will prevent the linker from stripping93// the .reloc section from the binary, which is a requirement94// for ASLR. While release builds are not affected, anyone95// building with a binutils < 2.36 is subject to this ld bug.96#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])97#else98#define MAIN_FUNCTION int main(int argc, char* argv[])99#endif101// Note these both should work with the current usage of poll, but best to be safe102// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/103// __APPLE__ poll is broke https://github.com/bitcoin/bitcoin/pull/14336#issuecomment-437384408104#if defined(__linux__)105#define USE_POLL106#endif108// MSG_NOSIGNAL is not available on some platforms, if it doesn't exist define it as 0109#if !defined(MSG_NOSIGNAL)110#define MSG_NOSIGNAL 0111#endif113// MSG_DONTWAIT is not available on some platforms, if it doesn't exist define it as 0114#if !defined(MSG_DONTWAIT)115#define MSG_DONTWAIT 0116#endif118#endif // BITCOIN_COMPAT_COMPAT_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.