diff options
author | Matthew Zipkin <pinheadmz@gmail.com> | 2023-05-26 14:05:28 -0400 |
---|---|---|
committer | Matthew Zipkin <pinheadmz@gmail.com> | 2024-03-01 14:47:24 -0500 |
commit | 74f568cb6fd5c74b7b9bf0ce69876430746a53b1 (patch) | |
tree | 80d7146d466dd8ad65925a4515765e5f1a180735 /src/netbase.cpp | |
parent | bae86c8d318d06818aa75a9ebe3db864197f0bc6 (diff) |
netbase: allow CreateSock() to create UNIX sockets if supported
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r-- | src/netbase.cpp | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index 973d888722..bb65f412a2 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -3,6 +3,10 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#if defined(HAVE_CONFIG_H) +#include <config/bitcoin-config.h> +#endif + #include <netbase.h> #include <compat/compat.h> @@ -21,6 +25,10 @@ #include <limits> #include <memory> +#if HAVE_SOCKADDR_UN +#include <sys/un.h> +#endif + // Settings static GlobalMutex g_proxyinfo_mutex; static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex); @@ -446,11 +454,16 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family) { - // Not IPv4 or IPv6 + // Not IPv4, IPv6 or UNIX if (address_family == AF_UNSPEC) return nullptr; - // Create a TCP socket in the address family of the specified service. - SOCKET hSocket = socket(address_family, SOCK_STREAM, IPPROTO_TCP); + int protocol{IPPROTO_TCP}; +#if HAVE_SOCKADDR_UN + if (address_family == AF_UNIX) protocol = 0; +#endif + + // Create a socket in the specified address family. + SOCKET hSocket = socket(address_family, SOCK_STREAM, protocol); if (hSocket == INVALID_SOCKET) { return nullptr; } @@ -474,17 +487,21 @@ std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family) } #endif - // Set the no-delay option (disable Nagle's algorithm) on the TCP socket. - const int on{1}; - if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) { - LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n"); - } - // Set the non-blocking option on the socket. if (!sock->SetNonBlocking()) { LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError())); return nullptr; } + +#if HAVE_SOCKADDR_UN + if (address_family == AF_UNIX) return sock; +#endif + + // Set the no-delay option (disable Nagle's algorithm) on the TCP socket. + const int on{1}; + if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) { + LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n"); + } return sock; } |