Skip to content

Commit

Permalink
Handle EHOSTUNREACH errors in io.netty.channel.unix.Errors (#13317) (#…
Browse files Browse the repository at this point in the history
…13318)

Motivation:

    We should throw a NoRouteToHostException when a socket connection fails with EHOSTUNREACH in the same way as
    ENETUNREACH.

    Modifications:

    Add handling for EHOSTUNREACH

    Result:

    Correct exception is thrown.
  • Loading branch information
tristantarrant committed Apr 3, 2023
1 parent e1fad99 commit 76d83fe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions transport-native-unix-common/src/main/c/netty_unix_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ static jint netty_unix_errors_errorENETUNREACH(JNIEnv* env, jclass clazz) {
return ENETUNREACH;
}

static jint netty_unix_errors_errorEHOSTUNREACH(JNIEnv* env, jclass clazz) {
return EHOSTUNREACH;
}

static jstring netty_unix_errors_strError(JNIEnv* env, jclass clazz, jint error) {
return (*env)->NewStringUTF(env, strerror(error));
}
Expand All @@ -207,6 +211,7 @@ static const JNINativeMethod statically_referenced_fixed_method_table[] = {
{ "errorEISCONN", "()I", (void *) netty_unix_errors_errorEISCONN },
{ "errorEALREADY", "()I", (void *) netty_unix_errors_errorEALREADY },
{ "errorENETUNREACH", "()I", (void *) netty_unix_errors_errorENETUNREACH },
{ "errorEHOSTUNREACH", "()I", (void *) netty_unix_errors_errorEHOSTUNREACH },
{ "strError", "(I)Ljava/lang/String;", (void *) netty_unix_errors_strError }
};
static const jint statically_referenced_fixed_method_table_size = sizeof(statically_referenced_fixed_method_table) / sizeof(statically_referenced_fixed_method_table[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errnoEWOULDBLOCK;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEALREADY;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errorECONNREFUSED;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEHOSTUNREACH;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errorEISCONN;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.errorENETUNREACH;
import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.strError;
Expand All @@ -58,6 +59,7 @@ public final class Errors {
public static final int ERROR_EISCONN_NEGATIVE = -errorEISCONN();
public static final int ERROR_EALREADY_NEGATIVE = -errorEALREADY();
public static final int ERROR_ENETUNREACH_NEGATIVE = -errorENETUNREACH();
public static final int ERROR_EHOSTUNREACH_NEGATIVE = -errorEHOSTUNREACH();

/**
* Holds the mappings for errno codes to String messages.
Expand Down Expand Up @@ -152,7 +154,7 @@ private static String errnoString(int err) {
}

private static IOException newConnectException0(String method, int err) {
if (err == ERROR_ENETUNREACH_NEGATIVE) {
if (err == ERROR_ENETUNREACH_NEGATIVE || err == ERROR_EHOSTUNREACH_NEGATIVE) {
return new NoRouteToHostException();
}
if (err == ERROR_EISCONN_NEGATIVE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ private ErrorsStaticallyReferencedJniMethods() { }
static native int errorEISCONN();
static native int errorEALREADY();
static native int errorENETUNREACH();
static native int errorEHOSTUNREACH();
static native String strError(int err);
}

0 comments on commit 76d83fe

Please sign in to comment.