Skip to content

Commit

Permalink
Further fixes for WSA/Posix socket error translation
Browse files Browse the repository at this point in the history
Don't translate '0' (no error).
Handle WSAEAFNOSUPPORT and WSAEINVAL.
Add Posix->WSA translation.
Add default translation for unrecognized errors.

Signed-off-by: Kevin Wasserman <kevin.wasserman@painless-security.com>
  • Loading branch information
Kevin Wasserman committed Jul 29, 2012
1 parent be43a3a commit c294a7c
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/include/port-sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -71,6 +71,42 @@ typedef WSABUF sg_buf;
#define ETIMEDOUT WSAETIMEDOUT
#endif

/*
* Translate Posix errors to their WinSock counterparts.
* This is necessary for msvc 2010+, where both WinSock and Posix errors
* are defined.
*/
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
Expand All @@ -80,6 +116,8 @@ static __inline int TranslatedWSAGetLastError()
{
int err = WSAGetLastError();
switch (err) {
case 0:
break;
case WSAEINPROGRESS:
err = EINPROGRESS; break;
case WSAEWOULDBLOCK:
Expand All @@ -94,8 +132,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;
}
Expand Down

0 comments on commit c294a7c

Please sign in to comment.