From d169281a00acec1ca88d247401fee482d45446e7 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Wed, 6 Apr 2016 17:15:39 +0100 Subject: [PATCH 1/3] Problem: zmq_bind IPv4 fallback still tries IPv6 Solution: if opening an IPv6 TCP socket fails because IPv6 is not available, try to open an IPv4 socket instead when creating and binding a TCP endpoint. --- src/tcp_listener.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 1058954f..0dd8caa6 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -184,13 +184,13 @@ int zmq::tcp_listener_t::set_address (const char *addr_) #endif // IPv6 address family not supported, try automatic downgrade to IPv4. - if (address.family () == AF_INET6 + if (s == -1 && address.family () == AF_INET6 && errno == EAFNOSUPPORT && options.ipv6) { - rc = address.resolve (addr_, true, true); + rc = address.resolve (addr_, true, false); if (rc != 0) return rc; - s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP); + s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); } #ifdef ZMQ_HAVE_WINDOWS From 99a8795dfe101782d6ac828208b435b863ae4aae Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Thu, 7 Apr 2016 13:15:41 +0100 Subject: [PATCH 2/3] Problem: zmq_connect (TCP) has no IPv4 fallback Solution: if opening an IPv6 TCP socket fails because IPv6 is not available, try to open an IPv4 socket instead when creating and connecting a TCP endpoint. --- src/tcp_connecter.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 78093917..b6f5b6ed 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -271,6 +271,20 @@ int zmq::tcp_connecter_t::open () // Create the socket. s = open_socket (tcp_addr->family (), SOCK_STREAM, IPPROTO_TCP); + + // IPv6 address family not supported, try automatic downgrade to IPv4. + if (s == -1 && tcp_addr->family () == AF_INET6 + && errno == EAFNOSUPPORT + && options.ipv6) { + rc = addr->resolved.tcp_addr->resolve ( + addr->address.c_str (), false, false); + if (rc != 0) { + LIBZMQ_DELETE(addr->resolved.tcp_addr); + return -1; + } + s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + } + #ifdef ZMQ_HAVE_WINDOWS if (s == INVALID_SOCKET) { errno = wsa_error_to_errno (WSAGetLastError ()); From 9cf6f85a252b83807f3da6cfeeca75aed60b415b Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Thu, 7 Apr 2016 13:17:08 +0100 Subject: [PATCH 3/3] Problem: redundant Windows errno conversion Solution: in the Windows-specific ifdef in tcp_listener set_address, check for error and set errno only after the IPv4 fallback has failed too, to avoid setting errno when the socket creation succeeds through the fallback. --- src/tcp_listener.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 0dd8caa6..b0c18dd6 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -178,10 +178,6 @@ int zmq::tcp_listener_t::set_address (const char *addr_) // Create a listening socket. s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP); -#ifdef ZMQ_HAVE_WINDOWS - if (s == INVALID_SOCKET) - errno = wsa_error_to_errno (WSAGetLastError ()); -#endif // IPv6 address family not supported, try automatic downgrade to IPv4. if (s == -1 && address.family () == AF_INET6