diff --git a/core/src/main/java/com/github/shadowsocks/JniHelper.java b/core/src/main/java/com/github/shadowsocks/JniHelper.java index 5aa353d7..ff111a65 100755 --- a/core/src/main/java/com/github/shadowsocks/JniHelper.java +++ b/core/src/main/java/com/github/shadowsocks/JniHelper.java @@ -75,5 +75,5 @@ public class JniHelper { public static native int sendFd(int fd, @NonNull String path); public static native void close(int fd); @Nullable - public static native InetAddress parseNumericAddress(@NonNull String str); + public static native byte[] parseNumericAddress(@NonNull String str); } diff --git a/core/src/main/jni/jni-helper.cpp b/core/src/main/jni/jni-helper.cpp index 8e0f6ce4..2e40ce90 100644 --- a/core/src/main/jni/jni-helper.cpp +++ b/core/src/main/jni/jni-helper.cpp @@ -27,10 +27,10 @@ using namespace std; #define THROW(env, clazz, msg) do { env->ThrowNew(env->FindClass(clazz), msg); } while (0) static int sdk_version; -static jclass ProcessImpl, Inet4Address, Inet6Address; +static jclass ProcessImpl; static jfieldID ProcessImpl_pid, ProcessImpl_exitValue, ProcessImpl_exitValueMutex; -static jmethodID Inet4Address__init_, Inet6Address__init_; +#pragma clang diagnostic ignored "-Wunused-parameter" extern "C" { JNIEXPORT jint JNICALL Java_com_github_shadowsocks_JniHelper_sigkill(JNIEnv *env, jobject thiz, jint pid) { // Suppress "No such process" errors. We just want the process killed. It's fine if it's already killed. @@ -104,24 +104,20 @@ JNIEXPORT jint JNICALL return 0; } -JNIEXPORT jobject JNICALL +JNIEXPORT jbyteArray JNICALL Java_com_github_shadowsocks_JniHelper_parseNumericAddress(JNIEnv *env, jobject thiz, jstring str) { const char *src = env->GetStringUTFChars(str, 0); jbyte dst[max(sizeof(in_addr), sizeof(in6_addr))]; - jobject result = nullptr; + jbyteArray arr = nullptr; if (inet_pton(AF_INET, src, dst) == 1) { - jbyteArray arr = env->NewByteArray(sizeof(in_addr)); + arr = env->NewByteArray(sizeof(in_addr)); env->SetByteArrayRegion(arr, 0, sizeof(in_addr), dst); - result = sdk_version < 24 ? env->NewObject(Inet4Address, Inet4Address__init_, arr, str) - : env->NewObject(Inet4Address, Inet4Address__init_, str, arr); } else if (inet_pton(AF_INET6, src, dst) == 1) { - jbyteArray arr = env->NewByteArray(sizeof(in6_addr)); + arr = env->NewByteArray(sizeof(in6_addr)); env->SetByteArrayRegion(arr, 0, sizeof(in6_addr), dst); - result = sdk_version < 24 ? env->NewObject(Inet6Address, Inet6Address__init_, arr, str, 0) - : env->NewObject(Inet6Address, Inet6Address__init_, str, arr, 0); } env->ReleaseStringUTFChars(str, src); - return result; + return arr; } } @@ -134,6 +130,7 @@ typedef union { void* venv; } UnionJNIEnvToVoid; +#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" jint JNI_OnLoad(JavaVM* vm, void* reserved) { UnionJNIEnvToVoid uenv; uenv.venv = NULL; @@ -161,24 +158,12 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { THROW(env, "java/lang/RuntimeException", "Field " #clazz "." name " with type " sig " not found"); \ goto bail; \ } -#define GET_METHOD(out, clazz, name, sig) \ - if (!(out = env->GetMethodID(clazz, name, sig))) { \ - THROW(env, "java/lang/RuntimeException", "Method " #clazz "." name sig " not found"); \ - goto bail; \ - } - FIND_CLASS(Inet4Address, "java/net/Inet4Address"); - FIND_CLASS(Inet6Address, "java/net/Inet6Address"); if (sdk_version < 24) { FIND_CLASS(ProcessImpl, "java/lang/ProcessManager$ProcessImpl"); GET_FIELD(ProcessImpl_pid, ProcessImpl, "pid", "I") GET_FIELD(ProcessImpl_exitValue, ProcessImpl, "exitValue", "Ljava/lang/Integer;") GET_FIELD(ProcessImpl_exitValueMutex, ProcessImpl, "exitValueMutex", "Ljava/lang/Object;") - GET_METHOD(Inet4Address__init_, Inet4Address, "", "([BLjava/lang/String;)V") - GET_METHOD(Inet6Address__init_, Inet6Address, "", "([BLjava/lang/String;I)V") - } else { - GET_METHOD(Inet4Address__init_, Inet4Address, "", "(Ljava/lang/String;[B)V") - GET_METHOD(Inet6Address__init_, Inet6Address, "", "(Ljava/lang/String;[BI)V") } result = JNI_VERSION_1_6; diff --git a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt index 5aa41fcd..e328fcff 100644 --- a/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt +++ b/mobile/src/main/java/com/github/shadowsocks/utils/Utils.kt @@ -12,8 +12,6 @@ import android.support.v7.util.SortedList import android.util.TypedValue import com.github.shadowsocks.App.Companion.app import com.github.shadowsocks.JniHelper -import java.lang.reflect.InvocationTargetException -import java.net.Inet4Address import java.net.InetAddress import java.net.URLConnection @@ -23,8 +21,11 @@ private val fieldChildFragmentManager by lazy { field } -fun String.isNumericAddress() = parseNumericAddress() != null -fun String.parseNumericAddress() = JniHelper.parseNumericAddress(this) +fun String.isNumericAddress() = JniHelper.parseNumericAddress(this) != null +fun String.parseNumericAddress(): InetAddress? { + val addr = JniHelper.parseNumericAddress(this) + return if (addr == null) null else InetAddress.getByAddress(this, addr) +} fun parsePort(str: String?, default: Int, min: Int = 1025): Int { val x = str?.toIntOrNull() ?: default