From 0aa036fd7f702e5d420beb12ec5d0ac478c07121 Mon Sep 17 00:00:00 2001 From: Will Szumski Date: Tue, 22 Nov 2016 18:16:34 +0000 Subject: iterate through net.dns* and pick first valid IPv4 address --- jni/iodine-client.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'jni') diff --git a/jni/iodine-client.c b/jni/iodine-client.c index 2c08229..0af59a7 100644 --- a/jni/iodine-client.c +++ b/jni/iodine-client.c @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -17,6 +18,7 @@ #define IODINE_CLIENT_CLASS "org/xapek/andiodine/IodineClient" #define IODINE_CLIENT_CLASS_LOG_CALLBACK "log_callback" #define IODINE_CLIENT_CLASS_LOG_CALLBACK_SIG "(Ljava/lang/String;)V" +#define MAX_DNS_PROPERTIES 4 static int dns_fd; @@ -208,7 +210,15 @@ JNIEXPORT jint JNICALL Java_org_xapek_andiodine_IodineClient_getMtu(JNIEnv *env, // String IodineClient.getPropertyNetDns1 JNIEXPORT jstring JNICALL Java_org_xapek_andiodine_IodineClient_getPropertyNetDns1( JNIEnv *env, jclass klass) { - char value[PROP_VALUE_MAX]; - __system_property_get("net.dns1", value); - return (*env)->NewStringUTF(env, value); + struct sockaddr_in sa; + for (int i = 1; i <= MAX_DNS_PROPERTIES; i++) { + char prop_name[PROP_NAME_MAX]; + char dns[PROP_VALUE_MAX]; + snprintf(prop_name, sizeof(prop_name), "net.dns%d", i); + __system_property_get(prop_name, dns); + if (inet_pton(AF_INET, dns, &(sa.sin_addr)) == 1) { + return (*env)->NewStringUTF(env, dns); + } + } + return (*env)->NewStringUTF(env, ""); } -- cgit v1.2.1