>From b48faf4fa12897df3bfd07efcf3a000aa5a05bbd Mon Sep 17 00:00:00 2001
From: Martin Sustrik <sust...@250bpm.com>
Date: Sat, 7 May 2011 09:20:13 +0200
Subject: [PATCH] Async connect doesn't fail on EWSANETDOWN

Signed-off-by: Martin Sustrik <sust...@250bpm.com>
---
 src/err.cpp           |  112 ++++++++++++++++++++++++++-----------------------
 src/err.hpp           |   12 +++++
 src/tcp_connecter.cpp |   10 ++--
 3 files changed, 76 insertions(+), 58 deletions(-)

diff --git a/src/err.cpp b/src/err.cpp
index 87a0006..8ac7dd9 100644
--- a/src/err.cpp
+++ b/src/err.cpp
@@ -66,119 +66,125 @@ const char *zmq::errno_to_string (int errno_)
 
 const char *zmq::wsa_error()
 {
-    int errcode = WSAGetLastError ();
+    int no = WSAGetLastError ();
     //  TODO: This is not a generic way to handle this...
-    if (errcode == WSAEWOULDBLOCK)
+    if (no == WSAEWOULDBLOCK)
         return NULL;
 
+    return wsa_error_no (no);
+}
+
+const char *zmq::wsa_error_no (int no_)
+{
     //  TODO:  It seems that list of Windows socket errors is longer than this.
     //         Investigate whether there's a way to convert it into the string
     //         automatically (wsaError->HRESULT->string?).
     return
-        (errcode == WSABASEERR) ?
+        (no_ == WSABASEERR) ?
             "No Error" : 
-        (errcode == WSAEINTR) ?
+        (no_ == WSAEINTR) ?
             "Interrupted system call" : 
-        (errcode == WSAEBADF) ?
+        (no_ == WSAEBADF) ?
             "Bad file number" : 
-        (errcode == WSAEACCES) ?
+        (no_ == WSAEACCES) ?
             "Permission denied" : 
-        (errcode == WSAEFAULT) ?
+        (no_ == WSAEFAULT) ?
             "Bad address" : 
-        (errcode == WSAEINVAL) ?
+        (no_ == WSAEINVAL) ?
             "Invalid argument" : 
-        (errcode == WSAEMFILE) ?
+        (no_ == WSAEMFILE) ?
             "Too many open files" : 
-        (errcode == WSAEWOULDBLOCK) ?
+        (no_ == WSAEWOULDBLOCK) ?
             "Operation would block" : 
-        (errcode == WSAEINPROGRESS) ?
+        (no_ == WSAEINPROGRESS) ?
             "Operation now in progress" : 
-        (errcode == WSAEALREADY) ?
+        (no_ == WSAEALREADY) ?
             "Operation already in progress" : 
-        (errcode == WSAENOTSOCK) ?
+        (no_ == WSAENOTSOCK) ?
             "Socket operation on non-socket" : 
-        (errcode == WSAEDESTADDRREQ) ?
+        (no_ == WSAEDESTADDRREQ) ?
             "Destination address required" : 
-        (errcode == WSAEMSGSIZE) ?
+        (no_ == WSAEMSGSIZE) ?
             "Message too long" : 
-        (errcode == WSAEPROTOTYPE) ?
+        (no_ == WSAEPROTOTYPE) ?
             "Protocol wrong type for socket" : 
-        (errcode == WSAENOPROTOOPT) ?
+        (no_ == WSAENOPROTOOPT) ?
             "Bad protocol option" : 
-        (errcode == WSAEPROTONOSUPPORT) ?
+        (no_ == WSAEPROTONOSUPPORT) ?
             "Protocol not supported" : 
-        (errcode == WSAESOCKTNOSUPPORT) ?
+        (no_ == WSAESOCKTNOSUPPORT) ?
             "Socket type not supported" : 
-        (errcode == WSAEOPNOTSUPP) ?
+        (no_ == WSAEOPNOTSUPP) ?
             "Operation not supported on socket" : 
-        (errcode == WSAEPFNOSUPPORT) ?
+        (no_ == WSAEPFNOSUPPORT) ?
             "Protocol family not supported" : 
-        (errcode == WSAEAFNOSUPPORT) ?
+        (no_ == WSAEAFNOSUPPORT) ?
             "Address family not supported by protocol family" : 
-        (errcode == WSAEADDRINUSE) ?
+        (no_ == WSAEADDRINUSE) ?
             "Address already in use" : 
-        (errcode == WSAEADDRNOTAVAIL) ?
+        (no_ == WSAEADDRNOTAVAIL) ?
             "Can't assign requested address" : 
-        (errcode == WSAENETDOWN) ?
+        (no_ == WSAENETDOWN) ?
             "Network is down" : 
-        (errcode == WSAENETUNREACH) ?
+        (no_ == WSAENETUNREACH) ?
             "Network is unreachable" : 
-        (errcode == WSAENETRESET) ?
+        (no_ == WSAENETRESET) ?
             "Net dropped connection or reset" : 
-        (errcode == WSAECONNABORTED) ?
+        (no_ == WSAECONNABORTED) ?
             "Software caused connection abort" : 
-        (errcode == WSAECONNRESET) ?
+        (no_ == WSAECONNRESET) ?
             "Connection reset by peer" : 
-        (errcode == WSAENOBUFS) ?
+        (no_ == WSAENOBUFS) ?
             "No buffer space available" : 
-        (errcode == WSAEISCONN) ?
+        (no_ == WSAEISCONN) ?
             "Socket is already connected" : 
-        (errcode == WSAENOTCONN) ?
+        (no_ == WSAENOTCONN) ?
             "Socket is not connected" : 
-        (errcode == WSAESHUTDOWN) ?
+        (no_ == WSAESHUTDOWN) ?
             "Can't send after socket shutdown" : 
-        (errcode == WSAETOOMANYREFS) ?
+        (no_ == WSAETOOMANYREFS) ?
             "Too many references can't splice" : 
-        (errcode == WSAETIMEDOUT) ?
+        (no_ == WSAETIMEDOUT) ?
             "Connection timed out" : 
-        (errcode == WSAECONNREFUSED) ?
+        (no_ == WSAECONNREFUSED) ?
             "Connection refused" : 
-        (errcode == WSAELOOP) ?
+        (no_ == WSAELOOP) ?
             "Too many levels of symbolic links" : 
-        (errcode == WSAENAMETOOLONG) ?
+        (no_ == WSAENAMETOOLONG) ?
             "File name too long" : 
-        (errcode == WSAEHOSTDOWN) ?
+        (no_ == WSAEHOSTDOWN) ?
             "Host is down" : 
-        (errcode == WSAEHOSTUNREACH) ?
+        (no_ == WSAEHOSTUNREACH) ?
             "No Route to Host" : 
-        (errcode == WSAENOTEMPTY) ?
+        (no_ == WSAENOTEMPTY) ?
             "Directory not empty" : 
-        (errcode == WSAEPROCLIM) ?
+        (no_ == WSAEPROCLIM) ?
             "Too many processes" : 
-        (errcode == WSAEUSERS) ?
+        (no_ == WSAEUSERS) ?
             "Too many users" : 
-        (errcode == WSAEDQUOT) ?
+        (no_ == WSAEDQUOT) ?
             "Disc Quota Exceeded" : 
-        (errcode == WSAESTALE) ?
+        (no_ == WSAESTALE) ?
             "Stale NFS file handle" : 
-        (errcode == WSAEREMOTE) ?
+        (no_ == WSAEREMOTE) ?
             "Too many levels of remote in path" : 
-        (errcode == WSASYSNOTREADY) ?
+        (no_ == WSASYSNOTREADY) ?
             "Network SubSystem is unavailable" : 
-        (errcode == WSAVERNOTSUPPORTED) ?
+        (no_ == WSAVERNOTSUPPORTED) ?
             "WINSOCK DLL Version out of range" : 
-        (errcode == WSANOTINITIALISED) ?
+        (no_ == WSANOTINITIALISED) ?
             "Successful WSASTARTUP not yet performed" : 
-        (errcode == WSAHOST_NOT_FOUND) ?
+        (no_ == WSAHOST_NOT_FOUND) ?
             "Host not found" : 
-        (errcode == WSATRY_AGAIN) ?
+        (no_ == WSATRY_AGAIN) ?
             "Non-Authoritative Host not found" : 
-        (errcode == WSANO_RECOVERY) ?
+        (no_ == WSANO_RECOVERY) ?
             "Non-Recoverable errors: FORMERR REFUSED NOTIMP" : 
-        (errcode == WSANO_DATA) ?
+        (no_ == WSANO_DATA) ?
             "Valid name no data record of requested" :
         "error not defined"; 
 }
+
 void zmq::win_error (char *buffer_, size_t buffer_size_)
 {
     DWORD errcode = GetLastError ();
diff --git a/src/err.hpp b/src/err.hpp
index 6289a08..9558a10 100644
--- a/src/err.hpp
+++ b/src/err.hpp
@@ -49,6 +49,7 @@ namespace zmq
 namespace zmq
 {
     const char *wsa_error ();
+    const char *wsa_error_no (int no_);
     void win_error (char *buffer_, size_t buffer_size_);
     void wsa_error_to_errno (); 
 }
@@ -66,6 +67,17 @@ namespace zmq
         }\
     } while (false)
 
+//  Provides convenient way to assert on WSA-style errors on Windows.
+#define wsa_assert_no(no) \
+    do {\
+        const char *errstr = zmq::wsa_error_no (no);\
+        if (errstr != NULL) {\
+            fprintf (stderr, "Assertion failed: %s (%s:%d)\n", errstr, \
+                __FILE__, __LINE__);\
+            abort ();\
+        }\
+    } while (false)
+
 // Provides convenient way to check GetLastError-style errors on Windows.
 #define win_assert(x) \
     do {\
diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp
index 01463c0..94891b5 100644
--- a/src/tcp_connecter.cpp
+++ b/src/tcp_connecter.cpp
@@ -116,12 +116,12 @@ zmq::fd_t zmq::tcp_connecter_t::connect ()
 
         //  Assert that the error was caused by the networking problems
         //  rather than 0MQ bug.
-        errno = err;
-        errno_assert (errno == WSAECONNREFUSED || errno == WSAETIMEDOUT ||
-            errno == WSAECONNABORTED || errno == WSAEHOSTUNREACH ||
-            errno == WSAENETUNREACH);
+        if (err == WSAECONNREFUSED || err == WSAETIMEDOUT ||
+              err == WSAECONNABORTED || err == WSAEHOSTUNREACH ||
+              err == WSAENETUNREACH || err == WSAENETDOWN)
+            return retired_fd;
 
-        return retired_fd;
+        wsa_assert_no (err);
     }
 
     //  Return the newly connected socket.
-- 
1.6.5.1.1367.gcd48

_______________________________________________
zeromq-dev mailing list
zeromq-dev@lists.zeromq.org
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to