Skip to content

Commit 74f568c

Browse files
committed
netbase: allow CreateSock() to create UNIX sockets if supported
1 parent bae86c8 commit 74f568c

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/netbase.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#if defined(HAVE_CONFIG_H)
7+
#include <config/bitcoin-config.h>
8+
#endif
9+
610
#include <netbase.h>
711

812
#include <compat/compat.h>
@@ -21,6 +25,10 @@
2125
#include <limits>
2226
#include <memory>
2327

28+
#if HAVE_SOCKADDR_UN
29+
#include <sys/un.h>
30+
#endif
31+
2432
// Settings
2533
static GlobalMutex g_proxyinfo_mutex;
2634
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
446454

447455
std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family)
448456
{
449-
// Not IPv4 or IPv6
457+
// Not IPv4, IPv6 or UNIX
450458
if (address_family == AF_UNSPEC) return nullptr;
451459

452-
// Create a TCP socket in the address family of the specified service.
453-
SOCKET hSocket = socket(address_family, SOCK_STREAM, IPPROTO_TCP);
460+
int protocol{IPPROTO_TCP};
461+
#if HAVE_SOCKADDR_UN
462+
if (address_family == AF_UNIX) protocol = 0;
463+
#endif
464+
465+
// Create a socket in the specified address family.
466+
SOCKET hSocket = socket(address_family, SOCK_STREAM, protocol);
454467
if (hSocket == INVALID_SOCKET) {
455468
return nullptr;
456469
}
@@ -474,17 +487,21 @@ std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family)
474487
}
475488
#endif
476489

477-
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
478-
const int on{1};
479-
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
480-
LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
481-
}
482-
483490
// Set the non-blocking option on the socket.
484491
if (!sock->SetNonBlocking()) {
485492
LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
486493
return nullptr;
487494
}
495+
496+
#if HAVE_SOCKADDR_UN
497+
if (address_family == AF_UNIX) return sock;
498+
#endif
499+
500+
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
501+
const int on{1};
502+
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
503+
LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
504+
}
488505
return sock;
489506
}
490507

src/netbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLoo
229229
CSubNet LookupSubNet(const std::string& subnet_str);
230230

231231
/**
232-
* Create a socket in the given address family.
232+
* Create a TCP or UNIX socket in the given address family.
233233
* @param[in] address_family to use for the socket.
234234
* @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
235235
*/

0 commit comments

Comments
 (0)