]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Further fixes for WSA/Posix error translation
authorKevin Wasserman <kevin.wasserman@painless-security.com>
Sun, 29 Jul 2012 13:27:02 +0000 (09:27 -0400)
committerGreg Hudson <ghudson@mit.edu>
Fri, 3 Aug 2012 14:32:35 +0000 (10:32 -0400)
Don't translate '0' (no error).
Handle WSAEAFNOSUPPORT and WSAEINVAL.
Add Posix->WSA translation.
Add default translation for unrecognized errors.

[ghudson@mit.edu: Merged with master and adjusted comments.]

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

src/include/port-sockets.h

index d9429088f1934f88e81a3378df83e3d324823764..b3ab9c906bb7bd6cb2d75bfca6f92c19aa68f807 100644 (file)
@@ -24,7 +24,7 @@ typedef WSABUF sg_buf;
 #define SOCKET_INITIALIZE()     0
 #define SOCKET_CLEANUP()
 #define SOCKET_ERRNO            (TranslatedWSAGetLastError())
-#define SOCKET_SET_ERRNO(x)     (WSASetLastError (x))
+#define SOCKET_SET_ERRNO(x)     (TranslatedWSASetLastError(x))
 #define SOCKET_NFDS(f)          (0)     /* select()'s first arg is ignored */
 #define SOCKET_READ(fd, b, l)   (recv(fd, b, l, 0))
 #define SOCKET_WRITE(fd, b, l)  (send(fd, b, l, 0))
@@ -73,6 +73,39 @@ typedef WSABUF sg_buf;
 #define ETIMEDOUT WSAETIMEDOUT
 #endif
 
+/* Translate posix_error to its Winsock counterpart and set the last Winsock
+ * error to the result. */
+static __inline void TranslatedWSASetLastError(int posix_error)
+{
+    int wsa_error;
+    switch (posix_error) {
+    case 0:
+        wsa_error = 0; break;
+    case EINPROGRESS:
+        wsa_error = WSAEINPROGRESS; break;
+    case EWOULDBLOCK:
+        wsa_error = WSAEWOULDBLOCK; break;
+    case ECONNRESET:
+        wsa_error = WSAECONNRESET; break;
+    case ECONNABORTED:
+        wsa_error = WSAECONNABORTED; break;
+    case ECONNREFUSED:
+        wsa_error = WSAECONNREFUSED; break;
+    case EHOSTUNREACH:
+        wsa_error = WSAEHOSTUNREACH; break;
+    case ETIMEDOUT:
+        wsa_error = WSAETIMEDOUT; break;
+    case EAFNOSUPPORT:
+        wsa_error = WSAEAFNOSUPPORT; break;
+    case EINVAL:
+        wsa_error = WSAEINVAL; break;
+    default:
+        /* Ideally, we would log via k5-trace here, but we have no context. */
+        wsa_error = WSAEINVAL; break;
+    }
+    WSASetLastError(wsa_error);
+}
+
 /*
  * Translate Winsock errors to their POSIX counterparts.  This is necessary for
  * MSVC 2010+, where both Winsock and POSIX errors are defined.
@@ -81,6 +114,8 @@ static __inline int TranslatedWSAGetLastError()
 {
     int err = WSAGetLastError();
     switch (err) {
+    case 0:
+        break;
     case WSAEINPROGRESS:
         err = EINPROGRESS; break;
     case WSAEWOULDBLOCK:
@@ -95,8 +130,13 @@ static __inline int TranslatedWSAGetLastError()
         err = EHOSTUNREACH; break;
     case WSAETIMEDOUT:
         err = ETIMEDOUT; break;
+    case WSAEAFNOSUPPORT:
+        err = EAFNOSUPPORT; break;
+    case WSAEINVAL:
+        err = EINVAL; break;
     default:
-        break;
+        /* Ideally, we would log via k5-trace here, but we have no context. */
+        err = EINVAL; break;
     }
     return err;
 }