8317603: Improve exception messages thrown by sun.nio.ch.Net native methods (win)

Reviewed-by: vtewari, alanb, djelinski
This commit is contained in:
Matthias Baesken 2023-10-11 06:43:45 +00:00
parent 0fd807118c
commit a9b41da9df
5 changed files with 31 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -89,7 +89,7 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jclass clazz,
rv = connect((SOCKET)fd, &sa.sa, sa_len);
if (rv == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "connect");
} else {
/* Disable WSAECONNRESET errors as socket is no longer connected */
BOOL enable = FALSE;
@ -136,7 +136,10 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jclass clazz,
}
} else if (theErr == WSAEWOULDBLOCK) {
return IOS_UNAVAILABLE;
} else return handleSocketError(env, theErr);
} else {
NET_ThrowNew(env, theErr, "recvfrom");
return IOS_THROWN;
}
}
} while (retry);
@ -160,7 +163,8 @@ Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jclass clazz,
if (theErr == WSAEWOULDBLOCK) {
return IOS_UNAVAILABLE;
}
return handleSocketError(env, (jint)WSAGetLastError());
NET_ThrowNew(env, (jint)WSAGetLastError(), "sendto");
return IOS_THROWN;
}
return rv;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -149,8 +149,7 @@ Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
}
result = ioctlsocket(fd, FIONBIO, &argp);
if (result == SOCKET_ERROR) {
int error = WSAGetLastError();
handleSocketError(env, (jint)error);
NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket");
}
}

View File

@ -77,12 +77,6 @@ static void setConnectionReset(SOCKET s, BOOL enable) {
NULL, 0, &bytesReturned, NULL, NULL);
}
jint handleSocketError(JNIEnv *env, int errorValue)
{
NET_ThrowNew(env, errorValue, NULL);
return IOS_THROWN;
}
static jclass isa_class; /* java.net.InetSocketAddress */
static jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */
@ -392,7 +386,7 @@ Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
}
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return IOS_THROWN;
}
@ -436,7 +430,7 @@ Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
}
if (n == SOCKET_ERROR)
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt");
}
JNIEXPORT jint JNICALL
@ -467,7 +461,7 @@ Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobjec
if (n == SOCKET_ERROR) {
if (join && (WSAGetLastError() == WSAENOPROTOOPT))
return IOS_UNAVAILABLE;
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt");
}
return 0;
}
@ -489,7 +483,7 @@ Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, j
if (n == SOCKET_ERROR) {
if (block && (WSAGetLastError() == WSAENOPROTOOPT))
return IOS_UNAVAILABLE;
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
return 0;
}
@ -542,7 +536,7 @@ Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobjec
}
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
return 0;
}
@ -554,7 +548,7 @@ Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, j
int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
int n = setGroupSourceReqOption(env, fdo, opt, group, index, source);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt to block or unblock source");
}
return 0;
}
@ -571,7 +565,7 @@ Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint i
n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
(void*)&(in.s_addr), arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
}
@ -584,7 +578,7 @@ Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return IOS_THROWN;
}
return ntohl(in.s_addr);
@ -600,7 +594,7 @@ Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint i
n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
(void*)&(index), arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
}
@ -613,7 +607,7 @@ Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return -1;
}
return (jint)index;
@ -631,12 +625,12 @@ Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow) {
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
{
int count = 0;
if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
handleSocketError(env, WSAGetLastError());
u_long arg;
if (ioctlsocket((SOCKET) fdval(env, fdo), FIONREAD, &arg) == SOCKET_ERROR) {
NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket");
return IOS_THROWN;
}
return (jint) count;
return (jint) arg;
}
JNIEXPORT jint JNICALL
@ -667,7 +661,7 @@ Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlo
/* save last winsock error */
if (rv == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "select");
return IOS_THROWN;
} else if (rv >= 0) {
rv = 0;
@ -707,7 +701,7 @@ Java_sun_nio_ch_Net_pollConnect(JNIEnv* env, jclass this, jobject fdo, jlong tim
result = select(fd+1, 0, &wr, &ex, (timeout >= 0) ? &t : NULL);
if (result == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "select");
return JNI_FALSE;
} else if (result == 0) {
return JNI_FALSE;
@ -727,7 +721,7 @@ Java_sun_nio_ch_Net_pollConnect(JNIEnv* env, jclass this, jobject fdo, jlong tim
NET_ThrowNew(env, lastError, "getsockopt");
}
} else if (optError != NO_ERROR) {
handleSocketError(env, optError);
NET_ThrowNew(env, optError, "getsockopt");
}
return JNI_FALSE;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -158,7 +158,8 @@ Java_sun_nio_ch_UnixDomainSockets_socket0(JNIEnv *env, jclass cl)
{
SOCKET s = WSASocketW(PF_UNIX, SOCK_STREAM, 0, &provider, 0, WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET) {
return handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "WSASocketW");
return IOS_THROWN;
}
SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
return (int)s;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -46,7 +46,6 @@ jlong handleval(JNIEnv *env, jobject fdo);
jint convertReturnVal(JNIEnv *env, jint n, jboolean r);
jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean r);
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd);
jint handleSocketError(JNIEnv *env, int errorValue);
#ifdef _WIN64