summaryrefslogtreecommitdiffstats
blob: d7cfa15638d7dbbd8718032632f762c5170b1f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (c) 2014 Kevin Smith and Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include "dns_android.h"
#include <vector>
#include <string>
#include <jni.h>

JavaVM* javaVM = 0;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* /*aReserved*/) {
  javaVM = vm;
  return JNI_VERSION_1_6;
}

std::vector<std::string> get_servers() {
  std::vector<std::string> servers;
  if (javaVM == 0) {
    return servers;
  }

  JNIEnv* env = 0;
  int result = javaVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
  if (result == JNI_EDETACHED) {
    if (javaVM->AttachCurrentThread(&env, 0) != 0) {
      return servers;
    }
  } else if (result != JNI_OK) {
    return servers;
  }

  jclass systemProperties = env->FindClass("android.os.SystemProperties");
  if (!systemProperties) {
    return servers;
  }

  jmethodID method = env->GetStaticMethodID(systemProperties, "get", "(Ljava/lang/String;)Ljava/lang/String;");
  if (!method) {
    return servers;
  }

  const char* parameters[] = {
    "net.dns1",
    "net.dns2",
    "net.dns3",
    "net.dns4"
  };

  for (size_t i = 0; i < sizeof(parameters)/sizeof(*parameters); ++i) {
    jstring parameter = env->NewStringUTF(parameters[i]);
    jstring value = jstring(env->CallStaticObjectMethod(systemProperties, method, parameter));
    const char* server = env->GetStringUTFChars(value, JNI_FALSE);
    if (strcmp(server, "")) {
      servers.push_back(server);
    }
    env->ReleaseStringUTFChars(value, server);
  }

  return servers;
}

size_t dns_android_server_count() {
  std::vector<std::string> servers = get_servers();
  return servers.size();
}

dns_android_result dns_android_server_get(size_t index, char* buffer, size_t length) {
  std::vector<std::string> servers = get_servers();
  if (index >= servers.size()) {
    return DNS_ANDROID_FAILURE; /* invalid server index */
  }

  if (length < servers[index].length() + 1) {
    return DNS_ANDROID_FAILURE; /* insufficient buffer space */
  }

  strncpy(buffer, servers[index].c_str(), length);
  return DNS_ANDROID_SUCCESS;
}