Skip to content

Commit

Permalink
native: Fix error handling in Windows
Browse files Browse the repository at this point in the history
Some winsock error codes were not checked individually.

Add a wrapper that returns the error codes expected by POSIX/UNIX
compliant code.
  • Loading branch information
kohlschuetter committed Apr 18, 2024
1 parent c5962ba commit 1a5c525
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions junixsocket-native/src/main/c/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ int jux_mangleErrno(int err) {
}
}
#endif

#if defined(_WIN32)
int jux_mangleErrno(int err) {
switch(err) {
case WSAEWOULDBLOCK:
return EWOULDBLOCK;
case WSAEINPROGRESS:
return EINPROGRESS;
case WSAEALREADY:
return EALREADY;
case WSAECONNRESET:
return ECONNRESET;
default:
return err;
}
}
#endif
3 changes: 2 additions & 1 deletion junixsocket-native/src/main/c/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ typedef unsigned long socklen_t; /* 64-bits */

// Windows requires us fetching errno for socket-related errors
#if defined(_WIN32)
# define socket_errno (errno = WSAGetLastError())
int jux_mangleErrno(int);
# define socket_errno (errno = jux_mangleErrno(WSAGetLastError()))
# define ssize_t int
#elif defined(_OS400)
CK_VISIBILITY_INTERNAL
Expand Down
2 changes: 1 addition & 1 deletion junixsocket-native/src/main/c/receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ JNIEXPORT jint JNICALL Java_org_newsclub_net_unix_NativeUnixSocket_receive
// no data on non-blocking socket, or terminated connection?
if(count == 0 && theError != 0) {
_throwException(env, kExceptionClosedChannelException, NULL);
} else if(theError == 0 || theError == EAGAIN || theError == ETIMEDOUT
} else if(theError == 0 || theError == EAGAIN || theError == EWOULDBLOCK || theError == ETIMEDOUT
#if defined(_WIN32)
|| theError == WSAETIMEDOUT
#endif
Expand Down

0 comments on commit 1a5c525

Please sign in to comment.