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

Reviewed-by: alanb
  • Loading branch information
MBaesken committed Apr 19, 2023
1 parent 42b7260 commit ebba42a
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 @@ -614,7 +659,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 @@ -691,7 +736,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 @@ -912,38 +957,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;
}

3 comments on commit ebba42a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on ebba42a May 9, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-ebba42ac in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit ebba42ac from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 19 Apr 2023 and was reviewed by Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-ebba42ac:MBaesken-backport-ebba42ac
$ git checkout MBaesken-backport-ebba42ac
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-ebba42ac

Please sign in to comment.