make VirtualNetworkDNS a plain data class

This commit is contained in:
Brenton Bostick 2023-02-01 06:40:42 -05:00
parent 8b0be5c1fc
commit f9a27d6778
4 changed files with 69 additions and 30 deletions

View File

@ -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, "<init>", "(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, "<init>", "(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, "<init>", "()V"));
EXCEPTIONANDNULLCHECK(VirtualNetworkDNS_ctor = env->GetMethodID(VirtualNetworkDNS_class, "<init>", "(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, "<init>", "()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;"));

View File

@ -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;

View File

@ -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) {

View File

@ -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<VirtualNetworkDNS> {
private String domain;
private ArrayList<InetSocketAddress> servers;
public VirtualNetworkDNS() {}
private final String domain;
private final ArrayList<InetSocketAddress> servers;
public VirtualNetworkDNS(String domain, ArrayList<InetSocketAddress> 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<VirtualNetworkDNS> {
return result;
}
public String getSearchDomain() { return domain; }
public String getDomain() {
return domain;
}
public ArrayList<InetSocketAddress> getServers() { return servers; }
public ArrayList<InetSocketAddress> getServers() {
return servers;
}
}