diff --git a/java/jni/ZT_jnicache.cpp b/java/jni/ZT_jnicache.cpp index e7f27ce52..f89a7d499 100644 --- a/java/jni/ZT_jnicache.cpp +++ b/java/jni/ZT_jnicache.cpp @@ -114,8 +114,6 @@ jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -jfieldID VirtualNetworkDNS_domain_field; -jfieldID VirtualNetworkDNS_servers_field; jfieldID VirtualNetworkRoute_flags_field; jfieldID VirtualNetworkRoute_metric_field; jfieldID VirtualNetworkRoute_target_field; @@ -188,7 +186,7 @@ void setupJNICache(JavaVM *vm) { EXCEPTIONANDNULLCHECK(Version_ctor = env->GetMethodID(Version_class, "", "(III)V")); EXCEPTIONANDNULLCHECK(VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method = env->GetMethodID(VirtualNetworkConfigListener_class, "onNetworkConfigurationUpdated", "(JLcom/zerotier/sdk/VirtualNetworkConfigOperation;Lcom/zerotier/sdk/VirtualNetworkConfig;)I")); EXCEPTIONANDNULLCHECK(VirtualNetworkConfig_ctor = env->GetMethodID(VirtualNetworkConfig_class, "", "(JJLjava/lang/String;Lcom/zerotier/sdk/VirtualNetworkStatus;Lcom/zerotier/sdk/VirtualNetworkType;IZZZIJ[Ljava/net/InetSocketAddress;[Lcom/zerotier/sdk/VirtualNetworkRoute;Lcom/zerotier/sdk/VirtualNetworkDNS;)V")); - EXCEPTIONANDNULLCHECK(VirtualNetworkDNS_ctor = env->GetMethodID(VirtualNetworkDNS_class, "", "()V")); + EXCEPTIONANDNULLCHECK(VirtualNetworkDNS_ctor = env->GetMethodID(VirtualNetworkDNS_class, "", "(Ljava/lang/String;Ljava/util/ArrayList;)V")); EXCEPTIONANDNULLCHECK(VirtualNetworkFrameListener_onVirtualNetworkFrame_method = env->GetMethodID(VirtualNetworkFrameListener_class, "onVirtualNetworkFrame", "(JJJJJ[B)V")); EXCEPTIONANDNULLCHECK(VirtualNetworkRoute_ctor = env->GetMethodID(VirtualNetworkRoute_class, "", "()V")); @@ -208,8 +206,6 @@ void setupJNICache(JavaVM *vm) { // Instance fields // - EXCEPTIONANDNULLCHECK(VirtualNetworkDNS_domain_field = env->GetFieldID(VirtualNetworkDNS_class, "domain", "Ljava/lang/String;")); - EXCEPTIONANDNULLCHECK(VirtualNetworkDNS_servers_field = env->GetFieldID(VirtualNetworkDNS_class, "servers", "Ljava/util/ArrayList;")); EXCEPTIONANDNULLCHECK(VirtualNetworkRoute_flags_field = env->GetFieldID(VirtualNetworkRoute_class, "flags", "I")); EXCEPTIONANDNULLCHECK(VirtualNetworkRoute_metric_field = env->GetFieldID(VirtualNetworkRoute_class, "metric", "I")); EXCEPTIONANDNULLCHECK(VirtualNetworkRoute_target_field = env->GetFieldID(VirtualNetworkRoute_class, "target", "Ljava/net/InetSocketAddress;")); diff --git a/java/jni/ZT_jnicache.h b/java/jni/ZT_jnicache.h index d230ad6cd..4c6703d02 100644 --- a/java/jni/ZT_jnicache.h +++ b/java/jni/ZT_jnicache.h @@ -83,8 +83,6 @@ extern jmethodID VirtualNetworkType_fromInt_method; // Instance fields // -extern jfieldID VirtualNetworkDNS_domain_field; -extern jfieldID VirtualNetworkDNS_servers_field; extern jfieldID VirtualNetworkRoute_flags_field; extern jfieldID VirtualNetworkRoute_metric_field; extern jfieldID VirtualNetworkRoute_target_field; diff --git a/java/jni/ZT_jniutils.cpp b/java/jni/ZT_jniutils.cpp index a7ca7e10b..bd1968442 100644 --- a/java/jni/ZT_jniutils.cpp +++ b/java/jni/ZT_jniutils.cpp @@ -353,37 +353,64 @@ jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route) return routeObj; } +// +// may return NULL +// jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns) { - jobject dnsObj = env->NewObject(VirtualNetworkDNS_class, VirtualNetworkDNS_ctor); - if(env->ExceptionCheck() || dnsObj == NULL) { + if (strlen(dns.domain) == 0) { + LOGD("dns.domain is empty; returning NULL"); return NULL; } - if (strlen(dns.domain) > 0) { + jstring domain = env->NewStringUTF(dns.domain); + if (env->ExceptionCheck() || domain == NULL) { + LOGE("Exception creating new string"); + return NULL; + } - jstring domain = env->NewStringUTF(dns.domain); + jobject addrList = env->NewObject(ArrayList_class, ArrayList_ctor, 0); + if (env->ExceptionCheck() || addrList == NULL) { + LOGE("Exception creating new ArrayList"); + return NULL; + } - jobject addrArray = env->NewObject(ArrayList_class, ArrayList_ctor, 0); + for (int i = 0; i < ZT_MAX_DNS_SERVERS; ++i) { //NOLINT - struct sockaddr_storage nullAddr; - memset(&nullAddr, 0, sizeof(struct sockaddr_storage)); - for(int i = 0; i < ZT_MAX_DNS_SERVERS; ++i) { - struct sockaddr_storage tmp = dns.server_addr[i]; + struct sockaddr_storage tmp = dns.server_addr[i]; - if (memcmp(&tmp, &nullAddr, sizeof(struct sockaddr_storage)) != 0) { - jobject addr = newInetSocketAddress(env, tmp); - env->CallBooleanMethod(addrArray, ArrayList_add_method, addr); - env->DeleteLocalRef(addr); - } + // + // may be NULL + // + jobject addr = newInetSocketAddress(env, tmp); + if (env->ExceptionCheck()) { + return NULL; } - env->SetObjectField(dnsObj, VirtualNetworkDNS_domain_field, domain); - env->SetObjectField(dnsObj, VirtualNetworkDNS_servers_field, addrArray); + if (addr == NULL) { + continue; + } - return dnsObj; + env->CallBooleanMethod(addrList, ArrayList_add_method, addr); + if(env->ExceptionCheck()) + { + LOGE("Exception calling add"); + return NULL; + } + + env->DeleteLocalRef(addr); } - return NULL; + + jobject dnsObj = env->NewObject( + VirtualNetworkDNS_class, + VirtualNetworkDNS_ctor, + domain, + addrList); + if (env->ExceptionCheck() || dnsObj == NULL) { + LOGE("Exception creating new VirtualNetworkDNS"); + return NULL; + } + return dnsObj; } jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status) { diff --git a/java/src/com/zerotier/sdk/VirtualNetworkDNS.java b/java/src/com/zerotier/sdk/VirtualNetworkDNS.java index 48b9ffcd0..6e4bb3d22 100644 --- a/java/src/com/zerotier/sdk/VirtualNetworkDNS.java +++ b/java/src/com/zerotier/sdk/VirtualNetworkDNS.java @@ -8,11 +8,25 @@ package com.zerotier.sdk; import java.net.InetSocketAddress; import java.util.ArrayList; +/** + * DNS configuration to be pushed on a virtual network + * + * Defined in ZeroTierOne.h as ZT_VirtualNetworkDNS + */ public class VirtualNetworkDNS implements Comparable { - private String domain; - private ArrayList servers; - public VirtualNetworkDNS() {} + private final String domain; + private final ArrayList servers; + + public VirtualNetworkDNS(String domain, ArrayList servers) { + this.domain = domain; + this.servers = servers; + } + + @Override + public String toString() { + return "VirtualNetworkDNS(" + domain + ", " + servers + ")"; + } @Override public boolean equals(Object o) { @@ -53,7 +67,11 @@ public class VirtualNetworkDNS implements Comparable { return result; } - public String getSearchDomain() { return domain; } + public String getDomain() { + return domain; + } - public ArrayList getServers() { return servers; } + public ArrayList getServers() { + return servers; + } }