diff options
Diffstat (limited to '3rdParty/Unbound/src/android')
-rw-r--r-- | 3rdParty/Unbound/src/android/dns/dns_android.cpp | 79 | ||||
-rw-r--r-- | 3rdParty/Unbound/src/android/dns/dns_android.h | 41 |
2 files changed, 120 insertions, 0 deletions
diff --git a/3rdParty/Unbound/src/android/dns/dns_android.cpp b/3rdParty/Unbound/src/android/dns/dns_android.cpp new file mode 100644 index 0000000..4b1c368 --- /dev/null +++ b/3rdParty/Unbound/src/android/dns/dns_android.cpp @@ -0,0 +1,79 @@ +/* + * 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; + + 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; +} diff --git a/3rdParty/Unbound/src/android/dns/dns_android.h b/3rdParty/Unbound/src/android/dns/dns_android.h new file mode 100644 index 0000000..6d38a6f --- /dev/null +++ b/3rdParty/Unbound/src/android/dns/dns_android.h @@ -0,0 +1,41 @@ +/* + * 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. + */ + +#ifndef __DNS_ANDROID_H__ +#define __DNS_ANDROID_H__ + +#include <string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/*! Result codes for dns_android_server_get */ +typedef enum { + DNS_ANDROID_SUCCESS = 0, + DNS_ANDROID_FAILURE +} dns_android_result; + +/*! + \brief Retrieve the number of dns servers available. + \return Zero in case of error. +*/ +size_t dns_android_server_count(); + +/*! + \brief Retrieve the address of a dns server + \param index The server index between zero and the number returned by the count function + \param buffer Output buffer into which the null terminated dns server string is written + \param length Size in bytes available in the output buffer + \return Error code. The buffer contents are invalid if an error occurs. +*/ +dns_android_result dns_android_server_get(size_t index, char* buffer, size_t length); + +#ifdef __cplusplus +} +#endif + +#endif // __DNS_ANDROID_H__ |