Skip to content

Commit

Permalink
8305993: Add handleSocketErrorWithMessage to extend nio Net.c excepti…
Browse files Browse the repository at this point in the history
…on message

Backport-of: ebba42ac52109ca036f2e721402c06afa8f455bb
  • Loading branch information
MBaesken committed May 10, 2023
1 parent 6a84974 commit b9eca2d
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions src/java.base/unix/native/libnio/ch/Net.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,51 @@ static jboolean isSourceFilterSupported(){
static jclass isa_class; /* java.net.InetSocketAddress */
static jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */

static jint handleSocketErrorWithMessage(JNIEnv *env, jint errorValue,
const char* message)
{
char *xn;
switch (errorValue) {
case EINPROGRESS: /* Non-blocking connect */
return 0;
#ifdef EPROTO
case EPROTO:
xn = JNU_JAVANETPKG "ProtocolException";
break;
#endif
case ECONNREFUSED:
case ETIMEDOUT:
case ENOTCONN:
xn = JNU_JAVANETPKG "ConnectException";
break;

case EHOSTUNREACH:
xn = JNU_JAVANETPKG "NoRouteToHostException";
break;
case EADDRINUSE: /* Fall through */
case EADDRNOTAVAIL:
case EACCES:
xn = JNU_JAVANETPKG "BindException";
break;
default:
xn = JNU_JAVANETPKG "SocketException";
break;
}
errno = errorValue;
if (message == NULL) {
JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
} else {
JNU_ThrowByNameWithMessageAndLastError(env, xn, message);
}
return IOS_THROWN;
}

/* Declared in nio_util.h */
jint handleSocketError(JNIEnv *env, jint errorValue)
{
return handleSocketErrorWithMessage(env, errorValue, NULL);
}

JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_initIDs(JNIEnv *env, jclass clazz)
{
Expand Down Expand Up @@ -615,7 +660,7 @@ Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobjec
if (n < 0) {
if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
return IOS_UNAVAILABLE;
handleSocketError(env, errno);
handleSocketErrorWithMessage(env, errno, "setsockopt failed");
}
return 0;
}
Expand Down Expand Up @@ -692,7 +737,7 @@ Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobjec
if (n < 0) {
if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
return IOS_UNAVAILABLE;
handleSocketError(env, errno);
handleSocketErrorWithMessage(env, errno, "setsockopt failed");
}
return 0;
}
Expand Down Expand Up @@ -913,38 +958,3 @@ Java_sun_nio_ch_Net_sendOOB(JNIEnv* env, jclass this, jobject fdo, jbyte b)
return convertReturnVal(env, n, JNI_FALSE);
}

/* Declared in nio_util.h */

jint handleSocketError(JNIEnv *env, jint errorValue)
{
char *xn;
switch (errorValue) {
case EINPROGRESS: /* Non-blocking connect */
return 0;
#ifdef EPROTO
case EPROTO:
xn = JNU_JAVANETPKG "ProtocolException";
break;
#endif
case ECONNREFUSED:
case ETIMEDOUT:
case ENOTCONN:
xn = JNU_JAVANETPKG "ConnectException";
break;

case EHOSTUNREACH:
xn = JNU_JAVANETPKG "NoRouteToHostException";
break;
case EADDRINUSE: /* Fall through */
case EADDRNOTAVAIL:
case EACCES:
xn = JNU_JAVANETPKG "BindException";
break;
default:
xn = JNU_JAVANETPKG "SocketException";
break;
}
errno = errorValue;
JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
return IOS_THROWN;
}

1 comment on commit b9eca2d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.