diff --git a/src/netbase.cpp b/src/netbase.cpp index 973d888722d..bb65f412a21 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 +#endif + #include #include @@ -21,6 +25,10 @@ #include #include +#if HAVE_SOCKADDR_UN +#include +#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 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 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; } diff --git a/src/netbase.h b/src/netbase.h index 6f2d86f1536..e772a1c046b 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -229,7 +229,7 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLoo CSubNet LookupSubNet(const std::string& subnet_str); /** - * Create a socket in the given address family. + * Create a TCP or UNIX socket in the given address family. * @param[in] address_family to use for the socket. * @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure */