@@ -487,24 +487,23 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
487
487
}
488
488
}
489
489
490
- std::unique_ptr<Sock> CreateSockOS (sa_family_t address_family )
490
+ std::unique_ptr<Sock> CreateSockOS (int domain, int type, int protocol )
491
491
{
492
492
// Not IPv4, IPv6 or UNIX
493
- if (address_family == AF_UNSPEC) return nullptr ;
494
-
495
- int protocol{IPPROTO_TCP};
496
- #if HAVE_SOCKADDR_UN
497
- if (address_family == AF_UNIX) protocol = 0 ;
498
- #endif
493
+ if (domain == AF_UNSPEC) return nullptr ;
499
494
500
495
// Create a socket in the specified address family.
501
- SOCKET hSocket = socket (address_family, SOCK_STREAM , protocol);
496
+ SOCKET hSocket = socket (domain, type , protocol);
502
497
if (hSocket == INVALID_SOCKET) {
503
498
return nullptr ;
504
499
}
505
500
506
501
auto sock = std::make_unique<Sock>(hSocket);
507
502
503
+ if (domain != AF_INET && domain != AF_INET6 && domain != AF_UNIX) {
504
+ return sock;
505
+ }
506
+
508
507
// Ensure that waiting for I/O on this socket won't result in undefined
509
508
// behavior.
510
509
if (!sock->IsSelectable ()) {
@@ -529,18 +528,21 @@ std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family)
529
528
}
530
529
531
530
#if HAVE_SOCKADDR_UN
532
- if (address_family == AF_UNIX) return sock;
531
+ if (domain == AF_UNIX) return sock;
533
532
#endif
534
533
535
- // Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
536
- const int on{1 };
537
- if (sock->SetSockOpt (IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) == SOCKET_ERROR) {
538
- LogPrint (BCLog::NET, " Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n " );
534
+ if (protocol == IPPROTO_TCP) {
535
+ // Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
536
+ const int on{1 };
537
+ if (sock->SetSockOpt (IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) == SOCKET_ERROR) {
538
+ LogPrint (BCLog::NET, " Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n " );
539
+ }
539
540
}
541
+
540
542
return sock;
541
543
}
542
544
543
- std::function<std::unique_ptr<Sock>(const sa_family_t & )> CreateSock = CreateSockOS;
545
+ std::function<std::unique_ptr<Sock>(int , int , int )> CreateSock = CreateSockOS;
544
546
545
547
template <typename ... Args>
546
548
static void LogConnectFailure (bool manual_connection, const char * fmt, const Args&... args) {
@@ -609,7 +611,7 @@ static bool ConnectToSocket(const Sock& sock, struct sockaddr* sockaddr, socklen
609
611
610
612
std::unique_ptr<Sock> ConnectDirectly (const CService& dest, bool manual_connection)
611
613
{
612
- auto sock = CreateSock (dest.GetSAFamily ());
614
+ auto sock = CreateSock (dest.GetSAFamily (), SOCK_STREAM, IPPROTO_TCP );
613
615
if (!sock) {
614
616
LogPrintLevel (BCLog::NET, BCLog::Level::Error, " Cannot create a socket for connecting to %s\n " , dest.ToStringAddrPort ());
615
617
return {};
@@ -637,7 +639,7 @@ std::unique_ptr<Sock> Proxy::Connect() const
637
639
if (!m_is_unix_socket) return ConnectDirectly (proxy, /* manual_connection=*/ true );
638
640
639
641
#if HAVE_SOCKADDR_UN
640
- auto sock = CreateSock (AF_UNIX);
642
+ auto sock = CreateSock (AF_UNIX, SOCK_STREAM, 0 );
641
643
if (!sock) {
642
644
LogPrintLevel (BCLog::NET, BCLog::Level::Error, " Cannot create a socket for connecting to %s\n " , m_unix_socket_path);
643
645
return {};
0 commit comments