aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Zipkin <pinheadmz@gmail.com>2023-05-26 14:05:28 -0400
committerMatthew Zipkin <pinheadmz@gmail.com>2024-03-01 14:47:24 -0500
commit74f568cb6fd5c74b7b9bf0ce69876430746a53b1 (patch)
tree80d7146d466dd8ad65925a4515765e5f1a180735
parentbae86c8d318d06818aa75a9ebe3db864197f0bc6 (diff)
downloadbitcoin-74f568cb6fd5c74b7b9bf0ce69876430746a53b1.tar.xz
netbase: allow CreateSock() to create UNIX sockets if supported
-rw-r--r--src/netbase.cpp35
-rw-r--r--src/netbase.h2
2 files changed, 27 insertions, 10 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;
}
diff --git a/src/netbase.h b/src/netbase.h
index 6f2d86f153..e772a1c046 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
*/