@@ -487,24 +487,23 @@ bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* a
487487 }
488488}
489489
490- std::unique_ptr<Sock> CreateSockOS (sa_family_t address_family )
490+ std::unique_ptr<Sock> CreateSockOS (int domain, int type, int protocol )
491491{
492492 // 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 ;
499494
500495 // Create a socket in the specified address family.
501- SOCKET hSocket = socket (address_family, SOCK_STREAM , protocol);
496+ SOCKET hSocket = socket (domain, type , protocol);
502497 if (hSocket == INVALID_SOCKET) {
503498 return nullptr ;
504499 }
505500
506501 auto sock = std::make_unique<Sock>(hSocket);
507502
503+ if (domain != AF_INET && domain != AF_INET6 && domain != AF_UNIX) {
504+ return sock;
505+ }
506+
508507 // Ensure that waiting for I/O on this socket won't result in undefined
509508 // behavior.
510509 if (!sock->IsSelectable ()) {
@@ -529,18 +528,21 @@ std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family)
529528 }
530529
531530#if HAVE_SOCKADDR_UN
532- if (address_family == AF_UNIX) return sock;
531+ if (domain == AF_UNIX) return sock;
533532#endif
534533
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+ }
539540 }
541+
540542 return sock;
541543}
542544
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;
544546
545547template <typename ... Args>
546548static 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
609611
610612std::unique_ptr<Sock> ConnectDirectly (const CService& dest, bool manual_connection)
611613{
612- auto sock = CreateSock (dest.GetSAFamily ());
614+ auto sock = CreateSock (dest.GetSAFamily (), SOCK_STREAM, IPPROTO_TCP );
613615 if (!sock) {
614616 LogPrintLevel (BCLog::NET, BCLog::Level::Error, " Cannot create a socket for connecting to %s\n " , dest.ToStringAddrPort ());
615617 return {};
@@ -637,7 +639,7 @@ std::unique_ptr<Sock> Proxy::Connect() const
637639 if (!m_is_unix_socket) return ConnectDirectly (proxy, /* manual_connection=*/ true );
638640
639641#if HAVE_SOCKADDR_UN
640- auto sock = CreateSock (AF_UNIX);
642+ auto sock = CreateSock (AF_UNIX, SOCK_STREAM, 0 );
641643 if (!sock) {
642644 LogPrintLevel (BCLog::NET, BCLog::Level::Error, " Cannot create a socket for connecting to %s\n " , m_unix_socket_path);
643645 return {};
0 commit comments