]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Translate WinSock errors to Posix counterparts
authorKevin Wasserman <kevin.wasserman@painless-security.com>
Fri, 20 Apr 2012 15:36:13 +0000 (11:36 -0400)
committerSam Hartman <hartmans@mit.edu>
Thu, 12 Jul 2012 02:36:07 +0000 (22:36 -0400)
MSVC 2010 defines both Posix and WinSock error values so we can no longer
simply #define the Posix error values to be their WinSock counterpart.
This patch explicitly #includes <errno.h> in port-sockets.h and still
conditionally defines the Posix error values for compatibility with older
MSVC but also translates WinSock errors to Posix for MSVC 2010
compatibility.

The downside to this approach is that there are some Posix errors we
do not currently detect (e.g. EADDRINUSE) that are neither #defined nor
translated.  If we use one of those in the future but fail to update
TranslateWSAGetLastError() we'll once again be in the situation that the
windows build will compile but fail to work, possibly only when some rare
error condition occurs.

Signed-off-by: Kevin Wasserman <kevin.wasserman@painless-security.com>
ticket: 7197 (new)
tags: pullup

src/include/port-sockets.h

index bb2b5173f9013e3c90cccf215dfbae0e4e08c472..0ccc670e715a76834a11a2fd2707f017056e985a 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <winsock2.h>
 #include <ws2tcpip.h>
+#include <errno.h>
 
 /* Some of our own infrastructure where the WinSock stuff was too hairy
    to dump into a clean Unix program...  */
@@ -22,7 +23,7 @@ typedef WSABUF sg_buf;
 
 #define SOCKET_INITIALIZE()     0
 #define SOCKET_CLEANUP()
-#define SOCKET_ERRNO            (WSAGetLastError())
+#define SOCKET_ERRNO            (TranslatedWSAGetLastError())
 #define SOCKET_SET_ERRNO(x)     (WSASetLastError (x))
 #define SOCKET_NFDS(f)          (0)     /* select()'s first arg is ignored */
 #define SOCKET_READ(fd, b, l)   (recv(fd, b, l, 0))
@@ -44,6 +45,10 @@ typedef WSABUF sg_buf;
 #define SHUTDOWN_WRITE  SD_SEND
 #define SHUTDOWN_BOTH   SD_BOTH
 
+/*
+ * Define any missing Posix socket errors
+ * This is for compatibiliy with older versions of msvc (pre-2010)
+ */
 #ifndef EINPROGRESS
 #define EINPROGRESS WSAEINPROGRESS
 #endif
@@ -66,6 +71,35 @@ typedef WSABUF sg_buf;
 #define ETIMEDOUT WSAETIMEDOUT
 #endif
 
+/*
+ * Translate WinSock errors to their Posix counterparts.
+ * This is necessary for msvc 2010+, where both WinSock and Posix errors
+ * are defined.
+ */
+static __inline int TranslatedWSAGetLastError()
+{
+    int err = WSAGetLastError();
+    switch (err) {
+    case WSAEINPROGRESS:
+        err = EINPROGRESS; break;
+    case WSAEWOULDBLOCK:
+        err = EWOULDBLOCK; break;
+    case WSAECONNRESET:
+        err = ECONNRESET; break;
+    case WSAECONNABORTED:
+        err = ECONNABORTED; break;
+    case WSAECONNREFUSED:
+        err = ECONNREFUSED; break;
+    case WSAEHOSTUNREACH:
+        err = EHOSTUNREACH; break;
+    case WSAETIMEDOUT:
+        err = ETIMEDOUT; break;
+    default:
+        break;
+    }
+    return err;
+}
+
 #elif defined(__palmos__)
 
 /* If this source file requires it, define struct sockaddr_in