krb5 commit [krb5-1.10]: Translate WinSock errors toPosix counterparts

Kevin Wasserman krwasserman at hotmail.com
Tue Jul 24 22:25:05 EDT 2012


Sorry, I've been fighting fires recently; I should have time to get
to this by Friday at the latest.

-----Original Message----- 
From: Danilo Almeida
Sent: Tuesday, July 24, 2012 10:12 PM
To: 'Tom Yu'
Cc: krbdev at MIT.EDU
Subject: RE: krb5 commit [krb5-1.10]: Translate WinSock errors toPosix 
counterparts

Tom,

We need to pick up a follow-up fix from Kevin as discussed on krbdev a
couple weeks ago (see below).

Kevin, do we have an ETA on the additional fix?

>From Danilo Almeida on Wednesday, July 11, 2012:
> A couple comments/quesitons based on just reading the patch
> (w/o any other context):
>
> 1) TranslatedWSAGetLastError - it should have a case for 0 (no error).
> Also, the default case should map to some generic Posix error (and log
> the failed translation if the library has the capability).
>
> 2) Does SOCKET_SET_ERRNO() need some similar treatment?

>From Kevin Wasserman on Thursday, July 12, 2012:
> In theory, TranslatedWSAGetLastError() shouldn't be used unless
> there actually is an error, but the changes you suggest make sense
> to me.  And yes, it seems to me too that SOCKET_SET_ERRNO()
> needs similar treatment.  I will try to get to these shortly.

Thanks,
- Danilo

-----Original Message-----
From: cvs-krb5-bounces at MIT.EDU [mailto:cvs-krb5-bounces at MIT.EDU] On Behalf
Of Tom Yu
Sent: Monday, July 23, 2012 1:26 PM
To: cvs-krb5 at mit.edu
Subject: krb5 commit [krb5-1.10]: Translate WinSock errors to Posix
counterparts

https://github.com/krb5/krb5/commit/87a863eb65796196dbe09de8da0de634ce6d8817
commit 87a863eb65796196dbe09de8da0de634ce6d8817
Author: Kevin Wasserman <kevin.wasserman at painless-security.com>
Date:   Fri Apr 20 11:36:13 2012 -0400

    Translate WinSock errors to Posix counterparts

    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 at painless-security.com>

    (cherry picked from commit fc08c21ab33fcc0c8851a2a0fb0e55721ff975ea)

    ticket: 7197
    version_fixed: 1.10.3
    status: resolved

src/include/port-sockets.h |   36 +++++++++++++++++++++++++++++++++++-
1 files changed, 35 insertions(+), 1 deletions(-)

diff --git a/src/include/port-sockets.h b/src/include/port-sockets.h
index bb2b517..0ccc670 100644
--- a/src/include/port-sockets.h
+++ b/src/include/port-sockets.h
@@ -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
_______________________________________________
cvs-krb5 mailing list
cvs-krb5 at mit.edu
https://mailman.mit.edu/mailman/listinfo/cvs-krb5

_______________________________________________
krbdev mailing list             krbdev at mit.edu
https://mailman.mit.edu/mailman/listinfo/krbdev 



More information about the krbdev mailing list