First Commit
This commit is contained in:
236
java/jni/ZT_jnicache.cpp
Normal file
236
java/jni/ZT_jnicache.cpp
Normal file
@@ -0,0 +1,236 @@
|
||||
//
|
||||
// Created by Brenton Bostick on 1/18/23.
|
||||
//
|
||||
|
||||
#include "ZT_jnicache.h"
|
||||
|
||||
#include "ZT_jniutils.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#define LOG_TAG "Cache"
|
||||
|
||||
#define EXCEPTIONANDNULLCHECK(var) \
|
||||
do { \
|
||||
if (env->ExceptionCheck()) { \
|
||||
assert(false && "Exception"); \
|
||||
} \
|
||||
if ((var) == NULL) { \
|
||||
assert(false && #var " is NULL"); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
#define SETCLASS(classVar, classNameString) \
|
||||
do { \
|
||||
jclass classVar ## _local = env->FindClass(classNameString); \
|
||||
EXCEPTIONANDNULLCHECK(classVar ## _local); \
|
||||
classVar = reinterpret_cast<jclass>(env->NewGlobalRef(classVar ## _local)); \
|
||||
EXCEPTIONANDNULLCHECK(classVar); \
|
||||
env->DeleteLocalRef(classVar ## _local); \
|
||||
} while (false)
|
||||
|
||||
#define SETOBJECT(objectVar, code) \
|
||||
do { \
|
||||
jobject objectVar ## _local = code; \
|
||||
EXCEPTIONANDNULLCHECK(objectVar ## _local); \
|
||||
objectVar = env->NewGlobalRef(objectVar ## _local); \
|
||||
EXCEPTIONANDNULLCHECK(objectVar); \
|
||||
env->DeleteLocalRef(objectVar ## _local); \
|
||||
} while (false)
|
||||
|
||||
|
||||
//
|
||||
// Classes
|
||||
//
|
||||
|
||||
jclass ArrayList_class;
|
||||
jclass DataStoreGetListener_class;
|
||||
jclass DataStorePutListener_class;
|
||||
jclass EventListener_class;
|
||||
jclass Event_class;
|
||||
jclass Inet4Address_class;
|
||||
jclass Inet6Address_class;
|
||||
jclass InetAddress_class;
|
||||
jclass InetSocketAddress_class;
|
||||
jclass NodeStatus_class;
|
||||
jclass Node_class;
|
||||
jclass PacketSender_class;
|
||||
jclass PathChecker_class;
|
||||
jclass PeerPhysicalPath_class;
|
||||
jclass PeerRole_class;
|
||||
jclass Peer_class;
|
||||
jclass ResultCode_class;
|
||||
jclass Version_class;
|
||||
jclass VirtualNetworkConfigListener_class;
|
||||
jclass VirtualNetworkConfigOperation_class;
|
||||
jclass VirtualNetworkConfig_class;
|
||||
jclass VirtualNetworkDNS_class;
|
||||
jclass VirtualNetworkFrameListener_class;
|
||||
jclass VirtualNetworkRoute_class;
|
||||
jclass VirtualNetworkStatus_class;
|
||||
jclass VirtualNetworkType_class;
|
||||
|
||||
//
|
||||
// Instance methods
|
||||
//
|
||||
|
||||
jmethodID ArrayList_add_method;
|
||||
jmethodID ArrayList_ctor;
|
||||
jmethodID DataStoreGetListener_onDataStoreGet_method;
|
||||
jmethodID DataStorePutListener_onDataStorePut_method;
|
||||
jmethodID DataStorePutListener_onDelete_method;
|
||||
jmethodID EventListener_onEvent_method;
|
||||
jmethodID EventListener_onTrace_method;
|
||||
jmethodID InetAddress_getAddress_method;
|
||||
jmethodID InetSocketAddress_ctor;
|
||||
jmethodID InetSocketAddress_getAddress_method;
|
||||
jmethodID InetSocketAddress_getPort_method;
|
||||
jmethodID NodeStatus_ctor;
|
||||
jmethodID PacketSender_onSendPacketRequested_method;
|
||||
jmethodID PathChecker_onPathCheck_method;
|
||||
jmethodID PathChecker_onPathLookup_method;
|
||||
jmethodID PeerPhysicalPath_ctor;
|
||||
jmethodID Peer_ctor;
|
||||
jmethodID Version_ctor;
|
||||
jmethodID VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method;
|
||||
jmethodID VirtualNetworkConfig_ctor;
|
||||
jmethodID VirtualNetworkDNS_ctor;
|
||||
jmethodID VirtualNetworkFrameListener_onVirtualNetworkFrame_method;
|
||||
jmethodID VirtualNetworkRoute_ctor;
|
||||
|
||||
//
|
||||
// Static methods
|
||||
//
|
||||
|
||||
jmethodID Event_fromInt_method;
|
||||
jmethodID InetAddress_getByAddress_method;
|
||||
jmethodID PeerRole_fromInt_method;
|
||||
jmethodID ResultCode_fromInt_method;
|
||||
jmethodID VirtualNetworkConfigOperation_fromInt_method;
|
||||
jmethodID VirtualNetworkStatus_fromInt_method;
|
||||
jmethodID VirtualNetworkType_fromInt_method;
|
||||
|
||||
//
|
||||
// Enums
|
||||
//
|
||||
|
||||
jobject ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
|
||||
jobject ResultCode_RESULT_OK_enum;
|
||||
|
||||
void setupJNICache(JavaVM *vm) {
|
||||
|
||||
JNIEnv *env;
|
||||
GETENV(env, vm);
|
||||
|
||||
//
|
||||
// Classes
|
||||
//
|
||||
|
||||
SETCLASS(ArrayList_class, "java/util/ArrayList");
|
||||
SETCLASS(DataStoreGetListener_class, "com/zerotier/sdk/DataStoreGetListener");
|
||||
SETCLASS(DataStorePutListener_class, "com/zerotier/sdk/DataStorePutListener");
|
||||
SETCLASS(EventListener_class, "com/zerotier/sdk/EventListener");
|
||||
SETCLASS(Event_class, "com/zerotier/sdk/Event");
|
||||
SETCLASS(Inet4Address_class, "java/net/Inet4Address");
|
||||
SETCLASS(Inet6Address_class, "java/net/Inet6Address");
|
||||
SETCLASS(InetAddress_class, "java/net/InetAddress");
|
||||
SETCLASS(InetSocketAddress_class, "java/net/InetSocketAddress");
|
||||
SETCLASS(NodeStatus_class, "com/zerotier/sdk/NodeStatus");
|
||||
SETCLASS(Node_class, "com/zerotier/sdk/Node");
|
||||
SETCLASS(PacketSender_class, "com/zerotier/sdk/PacketSender");
|
||||
SETCLASS(PathChecker_class, "com/zerotier/sdk/PathChecker");
|
||||
SETCLASS(PeerPhysicalPath_class, "com/zerotier/sdk/PeerPhysicalPath");
|
||||
SETCLASS(PeerRole_class, "com/zerotier/sdk/PeerRole");
|
||||
SETCLASS(Peer_class, "com/zerotier/sdk/Peer");
|
||||
SETCLASS(ResultCode_class, "com/zerotier/sdk/ResultCode");
|
||||
SETCLASS(Version_class, "com/zerotier/sdk/Version");
|
||||
SETCLASS(VirtualNetworkConfigListener_class, "com/zerotier/sdk/VirtualNetworkConfigListener");
|
||||
SETCLASS(VirtualNetworkConfigOperation_class, "com/zerotier/sdk/VirtualNetworkConfigOperation");
|
||||
SETCLASS(VirtualNetworkConfig_class, "com/zerotier/sdk/VirtualNetworkConfig");
|
||||
SETCLASS(VirtualNetworkDNS_class, "com/zerotier/sdk/VirtualNetworkDNS");
|
||||
SETCLASS(VirtualNetworkFrameListener_class, "com/zerotier/sdk/VirtualNetworkFrameListener");
|
||||
SETCLASS(VirtualNetworkRoute_class, "com/zerotier/sdk/VirtualNetworkRoute");
|
||||
SETCLASS(VirtualNetworkStatus_class, "com/zerotier/sdk/VirtualNetworkStatus");
|
||||
SETCLASS(VirtualNetworkType_class, "com/zerotier/sdk/VirtualNetworkType");
|
||||
|
||||
//
|
||||
// Instance methods
|
||||
//
|
||||
|
||||
EXCEPTIONANDNULLCHECK(ArrayList_add_method = env->GetMethodID(ArrayList_class, "add", "(Ljava/lang/Object;)Z"));
|
||||
EXCEPTIONANDNULLCHECK(ArrayList_ctor = env->GetMethodID(ArrayList_class, "<init>", "(I)V"));
|
||||
EXCEPTIONANDNULLCHECK(DataStoreGetListener_onDataStoreGet_method = env->GetMethodID(DataStoreGetListener_class, "onDataStoreGet", "(Ljava/lang/String;[B)J"));
|
||||
EXCEPTIONANDNULLCHECK(DataStorePutListener_onDataStorePut_method = env->GetMethodID(DataStorePutListener_class, "onDataStorePut", "(Ljava/lang/String;[BZ)I"));
|
||||
EXCEPTIONANDNULLCHECK(DataStorePutListener_onDelete_method = env->GetMethodID(DataStorePutListener_class, "onDelete", "(Ljava/lang/String;)I"));
|
||||
EXCEPTIONANDNULLCHECK(EventListener_onEvent_method = env->GetMethodID(EventListener_class, "onEvent", "(Lcom/zerotier/sdk/Event;)V"));
|
||||
EXCEPTIONANDNULLCHECK(EventListener_onTrace_method = env->GetMethodID(EventListener_class, "onTrace", "(Ljava/lang/String;)V"));
|
||||
EXCEPTIONANDNULLCHECK(InetAddress_getAddress_method = env->GetMethodID(InetAddress_class, "getAddress", "()[B"));
|
||||
EXCEPTIONANDNULLCHECK(InetSocketAddress_ctor = env->GetMethodID(InetSocketAddress_class, "<init>", "(Ljava/net/InetAddress;I)V"));
|
||||
EXCEPTIONANDNULLCHECK(InetSocketAddress_getAddress_method = env->GetMethodID(InetSocketAddress_class, "getAddress", "()Ljava/net/InetAddress;"));
|
||||
EXCEPTIONANDNULLCHECK(InetSocketAddress_getPort_method = env->GetMethodID(InetSocketAddress_class, "getPort", "()I"));
|
||||
EXCEPTIONANDNULLCHECK(NodeStatus_ctor = env->GetMethodID(NodeStatus_class, "<init>", "(JLjava/lang/String;Ljava/lang/String;Z)V"));
|
||||
EXCEPTIONANDNULLCHECK(PacketSender_onSendPacketRequested_method = env->GetMethodID(PacketSender_class, "onSendPacketRequested", "(JLjava/net/InetSocketAddress;[BI)I"));
|
||||
EXCEPTIONANDNULLCHECK(PathChecker_onPathCheck_method = env->GetMethodID(PathChecker_class, "onPathCheck", "(JJLjava/net/InetSocketAddress;)Z"));
|
||||
EXCEPTIONANDNULLCHECK(PathChecker_onPathLookup_method = env->GetMethodID(PathChecker_class, "onPathLookup", "(JI)Ljava/net/InetSocketAddress;"));
|
||||
EXCEPTIONANDNULLCHECK(PeerPhysicalPath_ctor = env->GetMethodID(PeerPhysicalPath_class, "<init>", "(Ljava/net/InetSocketAddress;JJZ)V"));
|
||||
EXCEPTIONANDNULLCHECK(Peer_ctor = env->GetMethodID(Peer_class, "<init>", "(JIIIILcom/zerotier/sdk/PeerRole;[Lcom/zerotier/sdk/PeerPhysicalPath;)V"));
|
||||
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>", "(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>", "(Ljava/net/InetSocketAddress;Ljava/net/InetSocketAddress;II)V"));
|
||||
|
||||
//
|
||||
// Static methods
|
||||
//
|
||||
|
||||
EXCEPTIONANDNULLCHECK(Event_fromInt_method = env->GetStaticMethodID(Event_class, "fromInt", "(I)Lcom/zerotier/sdk/Event;"));
|
||||
EXCEPTIONANDNULLCHECK(InetAddress_getByAddress_method = env->GetStaticMethodID(InetAddress_class, "getByAddress", "([B)Ljava/net/InetAddress;"));
|
||||
EXCEPTIONANDNULLCHECK(PeerRole_fromInt_method = env->GetStaticMethodID(PeerRole_class, "fromInt", "(I)Lcom/zerotier/sdk/PeerRole;"));
|
||||
EXCEPTIONANDNULLCHECK(ResultCode_fromInt_method = env->GetStaticMethodID(ResultCode_class, "fromInt", "(I)Lcom/zerotier/sdk/ResultCode;"));
|
||||
EXCEPTIONANDNULLCHECK(VirtualNetworkConfigOperation_fromInt_method = env->GetStaticMethodID(VirtualNetworkConfigOperation_class, "fromInt", "(I)Lcom/zerotier/sdk/VirtualNetworkConfigOperation;"));
|
||||
EXCEPTIONANDNULLCHECK(VirtualNetworkStatus_fromInt_method = env->GetStaticMethodID(VirtualNetworkStatus_class, "fromInt", "(I)Lcom/zerotier/sdk/VirtualNetworkStatus;"));
|
||||
EXCEPTIONANDNULLCHECK(VirtualNetworkType_fromInt_method = env->GetStaticMethodID(VirtualNetworkType_class, "fromInt", "(I)Lcom/zerotier/sdk/VirtualNetworkType;"));
|
||||
|
||||
//
|
||||
// Enums
|
||||
//
|
||||
|
||||
SETOBJECT(ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum, createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL));
|
||||
SETOBJECT(ResultCode_RESULT_OK_enum, createResultObject(env, ZT_RESULT_OK));
|
||||
}
|
||||
|
||||
void teardownJNICache(JavaVM *vm) {
|
||||
|
||||
JNIEnv *env;
|
||||
GETENV(env, vm);
|
||||
|
||||
env->DeleteGlobalRef(ArrayList_class);
|
||||
env->DeleteGlobalRef(DataStoreGetListener_class);
|
||||
env->DeleteGlobalRef(DataStorePutListener_class);
|
||||
env->DeleteGlobalRef(EventListener_class);
|
||||
env->DeleteGlobalRef(Event_class);
|
||||
env->DeleteGlobalRef(InetAddress_class);
|
||||
env->DeleteGlobalRef(InetSocketAddress_class);
|
||||
env->DeleteGlobalRef(NodeStatus_class);
|
||||
env->DeleteGlobalRef(Node_class);
|
||||
env->DeleteGlobalRef(PacketSender_class);
|
||||
env->DeleteGlobalRef(PathChecker_class);
|
||||
env->DeleteGlobalRef(PeerPhysicalPath_class);
|
||||
env->DeleteGlobalRef(PeerRole_class);
|
||||
env->DeleteGlobalRef(Peer_class);
|
||||
env->DeleteGlobalRef(ResultCode_class);
|
||||
env->DeleteGlobalRef(Version_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkConfigListener_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkConfigOperation_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkConfig_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkDNS_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkFrameListener_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkRoute_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkStatus_class);
|
||||
env->DeleteGlobalRef(VirtualNetworkType_class);
|
||||
|
||||
env->DeleteGlobalRef(ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum);
|
||||
env->DeleteGlobalRef(ResultCode_RESULT_OK_enum);
|
||||
}
|
||||
92
java/jni/ZT_jnicache.h
Normal file
92
java/jni/ZT_jnicache.h
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by Brenton Bostick on 1/18/23.
|
||||
//
|
||||
|
||||
#ifndef ZEROTIERANDROID_JNICACHE_H
|
||||
#define ZEROTIERANDROID_JNICACHE_H
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
|
||||
//
|
||||
// Classes
|
||||
//
|
||||
|
||||
extern jclass ArrayList_class;
|
||||
extern jclass DataStoreGetListener_class;
|
||||
extern jclass DataStorePutListener_class;
|
||||
extern jclass EventListener_class;
|
||||
extern jclass Event_class;
|
||||
extern jclass Inet4Address_class;
|
||||
extern jclass Inet6Address_class;
|
||||
extern jclass InetAddress_class;
|
||||
extern jclass InetSocketAddress_class;
|
||||
extern jclass NodeStatus_class;
|
||||
extern jclass Node_class;
|
||||
extern jclass PacketSender_class;
|
||||
extern jclass PathChecker_class;
|
||||
extern jclass PeerPhysicalPath_class;
|
||||
extern jclass PeerRole_class;
|
||||
extern jclass Peer_class;
|
||||
extern jclass ResultCode_class;
|
||||
extern jclass Version_class;
|
||||
extern jclass VirtualNetworkConfigListener_class;
|
||||
extern jclass VirtualNetworkConfigOperation_class;
|
||||
extern jclass VirtualNetworkConfig_class;
|
||||
extern jclass VirtualNetworkDNS_class;
|
||||
extern jclass VirtualNetworkFrameListener_class;
|
||||
extern jclass VirtualNetworkRoute_class;
|
||||
extern jclass VirtualNetworkStatus_class;
|
||||
extern jclass VirtualNetworkType_class;
|
||||
|
||||
//
|
||||
// Instance methods
|
||||
//
|
||||
|
||||
extern jmethodID ArrayList_add_method;
|
||||
extern jmethodID ArrayList_ctor;
|
||||
extern jmethodID DataStoreGetListener_onDataStoreGet_method;
|
||||
extern jmethodID DataStorePutListener_onDataStorePut_method;
|
||||
extern jmethodID DataStorePutListener_onDelete_method;
|
||||
extern jmethodID EventListener_onEvent_method;
|
||||
extern jmethodID EventListener_onTrace_method;
|
||||
extern jmethodID InetAddress_getAddress_method;
|
||||
extern jmethodID InetSocketAddress_ctor;
|
||||
extern jmethodID InetSocketAddress_getAddress_method;
|
||||
extern jmethodID InetSocketAddress_getPort_method;
|
||||
extern jmethodID NodeStatus_ctor;
|
||||
extern jmethodID PacketSender_onSendPacketRequested_method;
|
||||
extern jmethodID PathChecker_onPathCheck_method;
|
||||
extern jmethodID PathChecker_onPathLookup_method;
|
||||
extern jmethodID PeerPhysicalPath_ctor;
|
||||
extern jmethodID Peer_ctor;
|
||||
extern jmethodID Version_ctor;
|
||||
extern jmethodID VirtualNetworkConfigListener_onNetworkConfigurationUpdated_method;
|
||||
extern jmethodID VirtualNetworkConfig_ctor;
|
||||
extern jmethodID VirtualNetworkDNS_ctor;
|
||||
extern jmethodID VirtualNetworkFrameListener_onVirtualNetworkFrame_method;
|
||||
extern jmethodID VirtualNetworkRoute_ctor;
|
||||
|
||||
//
|
||||
// Static methods
|
||||
//
|
||||
|
||||
extern jmethodID Event_fromInt_method;
|
||||
extern jmethodID InetAddress_getByAddress_method;
|
||||
extern jmethodID PeerRole_fromInt_method;
|
||||
extern jmethodID ResultCode_fromInt_method;
|
||||
extern jmethodID VirtualNetworkConfigOperation_fromInt_method;
|
||||
extern jmethodID VirtualNetworkStatus_fromInt_method;
|
||||
extern jmethodID VirtualNetworkType_fromInt_method;
|
||||
|
||||
//
|
||||
// Enums
|
||||
//
|
||||
|
||||
extern jobject ResultCode_RESULT_FATAL_ERROR_INTERNAL_enum;
|
||||
extern jobject ResultCode_RESULT_OK_enum;
|
||||
|
||||
void setupJNICache(JavaVM *vm);
|
||||
void teardownJNICache(JavaVM *vm);
|
||||
|
||||
#endif // ZEROTIERANDROID_JNICACHE_H
|
||||
625
java/jni/ZT_jniutils.cpp
Normal file
625
java/jni/ZT_jniutils.cpp
Normal file
@@ -0,0 +1,625 @@
|
||||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ZT_jniutils.h"
|
||||
|
||||
#include "ZT_jnicache.h"
|
||||
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define LOG_TAG "Utils"
|
||||
|
||||
jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
|
||||
{
|
||||
jobject resultObject = env->CallStaticObjectMethod(ResultCode_class, ResultCode_fromInt_method, (jint)code);
|
||||
if(env->ExceptionCheck() || resultObject == NULL) {
|
||||
LOGE("Error creating ResultCode object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return resultObject;
|
||||
}
|
||||
|
||||
|
||||
jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status)
|
||||
{
|
||||
jobject statusObject = env->CallStaticObjectMethod(VirtualNetworkStatus_class, VirtualNetworkStatus_fromInt_method, (jint)status);
|
||||
if (env->ExceptionCheck() || statusObject == NULL) {
|
||||
LOGE("Error creating VirtualNetworkStatus object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return statusObject;
|
||||
}
|
||||
|
||||
jobject createEvent(JNIEnv *env, ZT_Event event)
|
||||
{
|
||||
jobject eventObject = env->CallStaticObjectMethod(Event_class, Event_fromInt_method, (jint)event);
|
||||
if (env->ExceptionCheck() || eventObject == NULL) {
|
||||
LOGE("Error creating Event object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return eventObject;
|
||||
}
|
||||
|
||||
jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
|
||||
{
|
||||
jobject peerRoleObject = env->CallStaticObjectMethod(PeerRole_class, PeerRole_fromInt_method, (jint)role);
|
||||
if (env->ExceptionCheck() || peerRoleObject == NULL) {
|
||||
LOGE("Error creating PeerRole object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return peerRoleObject;
|
||||
}
|
||||
|
||||
jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type)
|
||||
{
|
||||
jobject vntypeObject = env->CallStaticObjectMethod(VirtualNetworkType_class, VirtualNetworkType_fromInt_method, (jint)type);
|
||||
if (env->ExceptionCheck() || vntypeObject == NULL) {
|
||||
LOGE("Error creating VirtualNetworkType object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return vntypeObject;
|
||||
}
|
||||
|
||||
jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op)
|
||||
{
|
||||
jobject vnetConfigOpObject = env->CallStaticObjectMethod(VirtualNetworkConfigOperation_class, VirtualNetworkConfigOperation_fromInt_method, (jint)op);
|
||||
if (env->ExceptionCheck() || vnetConfigOpObject == NULL) {
|
||||
LOGE("Error creating VirtualNetworkConfigOperation object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return vnetConfigOpObject;
|
||||
}
|
||||
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
jobject inetAddressObj = NULL;
|
||||
switch(addr.ss_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
{
|
||||
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
||||
const unsigned char *bytes = reinterpret_cast<const unsigned char *>(&ipv6->sin6_addr.s6_addr);
|
||||
|
||||
jbyteArray buff = newByteArray(env, bytes, 16);
|
||||
if(env->ExceptionCheck() || buff == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inetAddressObj = env->CallStaticObjectMethod(
|
||||
InetAddress_class, InetAddress_getByAddress_method, (jbyteArray)buff);
|
||||
}
|
||||
break;
|
||||
case AF_INET:
|
||||
{
|
||||
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
||||
const unsigned char *bytes = reinterpret_cast<const unsigned char *>(&ipv4->sin_addr.s_addr);
|
||||
jbyteArray buff = newByteArray(env, bytes, 4);
|
||||
if(env->ExceptionCheck() || buff == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inetAddressObj = env->CallStaticObjectMethod(
|
||||
InetAddress_class, InetAddress_getByAddress_method, (jbyteArray)buff);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
assert(false && "addr.ss_family is neither AF_INET6 nor AF_INET");
|
||||
}
|
||||
}
|
||||
if(env->ExceptionCheck() || inetAddressObj == NULL) {
|
||||
LOGE("Error creating InetAddress object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return inetAddressObj;
|
||||
}
|
||||
|
||||
int addressPort(const sockaddr_storage addr) {
|
||||
|
||||
int port = 0;
|
||||
switch(addr.ss_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
{
|
||||
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
||||
port = ntohs(ipv6->sin6_port);
|
||||
}
|
||||
break;
|
||||
case AF_INET:
|
||||
{
|
||||
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
||||
port = ntohs(ipv4->sin_port);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
assert(false && "addr.ss_family is neither AF_INET6 nor AF_INET");
|
||||
}
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
//
|
||||
// addr may be empty
|
||||
//
|
||||
// may return NULL
|
||||
//
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
if(isSocketAddressEmpty(addr))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject inetAddressObject = newInetAddress(env, addr);
|
||||
|
||||
if(env->ExceptionCheck() || inetAddressObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int port = addressPort(addr);
|
||||
|
||||
jobject inetSocketAddressObject = env->NewObject(InetSocketAddress_class, InetSocketAddress_ctor, (jobject)inetAddressObject, (jint)port);
|
||||
if(env->ExceptionCheck() || inetSocketAddressObject == NULL) {
|
||||
LOGE("Error creating InetSocketAddress object");
|
||||
return NULL;
|
||||
}
|
||||
return inetSocketAddressObject;
|
||||
}
|
||||
|
||||
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
|
||||
{
|
||||
//
|
||||
// may be NULL
|
||||
//
|
||||
jobject addressObject = newInetSocketAddress(env, ppp.address);
|
||||
if(env->ExceptionCheck()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject pppObject = env->NewObject(
|
||||
PeerPhysicalPath_class,
|
||||
PeerPhysicalPath_ctor,
|
||||
(jobject)addressObject,
|
||||
(jlong)ppp.lastSend,
|
||||
(jlong)ppp.lastReceive,
|
||||
(jboolean)ppp.preferred); // ANDROID-56: cast to correct size
|
||||
if(env->ExceptionCheck() || pppObject == NULL)
|
||||
{
|
||||
LOGE("Error creating PPP object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return pppObject;
|
||||
}
|
||||
|
||||
jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
|
||||
{
|
||||
jobject peerRoleObj = createPeerRole(env, peer.role);
|
||||
if(env->ExceptionCheck() || peerRoleObj == NULL)
|
||||
{
|
||||
return NULL; // out of memory
|
||||
}
|
||||
|
||||
jobjectArray arrayObject = newPeerPhysicalPathArray(env, peer.paths, peer.pathCount);
|
||||
if (env->ExceptionCheck() || arrayObject == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject peerObject = env->NewObject(
|
||||
Peer_class,
|
||||
Peer_ctor,
|
||||
(jlong)peer.address,
|
||||
(jint)peer.versionMajor,
|
||||
(jint)peer.versionMinor,
|
||||
(jint)peer.versionRev,
|
||||
(jint)peer.latency,
|
||||
(jobject)peerRoleObj,
|
||||
(jobjectArray)arrayObject);
|
||||
if(env->ExceptionCheck() || peerObject == NULL)
|
||||
{
|
||||
LOGE("Error creating Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return peerObject;
|
||||
}
|
||||
|
||||
jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
|
||||
{
|
||||
jstring nameStr = env->NewStringUTF(vnetConfig.name);
|
||||
if(env->ExceptionCheck() || nameStr == NULL)
|
||||
{
|
||||
LOGE("Exception creating new string");
|
||||
return NULL; // out of memory
|
||||
}
|
||||
|
||||
jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status);
|
||||
if(env->ExceptionCheck() || statusObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject typeObject = createVirtualNetworkType(env, vnetConfig.type);
|
||||
if(env->ExceptionCheck() || typeObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobjectArray assignedAddrArrayObj = newInetSocketAddressArray(env, vnetConfig.assignedAddresses, vnetConfig.assignedAddressCount);
|
||||
if (env->ExceptionCheck() || assignedAddrArrayObj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobjectArray routesArrayObj = newVirtualNetworkRouteArray(env, vnetConfig.routes, vnetConfig.routeCount);
|
||||
if (env->ExceptionCheck() || routesArrayObj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// may be NULL
|
||||
//
|
||||
jobject dnsObj = newVirtualNetworkDNS(env, vnetConfig.dns);
|
||||
if(env->ExceptionCheck()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject vnetConfigObj = env->NewObject(
|
||||
VirtualNetworkConfig_class,
|
||||
VirtualNetworkConfig_ctor,
|
||||
(jlong)vnetConfig.nwid,
|
||||
(jlong)vnetConfig.mac,
|
||||
(jstring)nameStr,
|
||||
(jobject)statusObject,
|
||||
(jobject)typeObject,
|
||||
(jint)vnetConfig.mtu,
|
||||
(jboolean)vnetConfig.dhcp, // ANDROID-56: cast to correct size
|
||||
(jboolean)vnetConfig.bridge, // ANDROID-56: cast to correct size
|
||||
(jboolean)vnetConfig.broadcastEnabled, // ANDROID-56: cast to correct size
|
||||
(jint)vnetConfig.portError,
|
||||
(jlong)vnetConfig.netconfRevision,
|
||||
(jobjectArray)assignedAddrArrayObj,
|
||||
(jobjectArray)routesArrayObj,
|
||||
(jobject)dnsObj);
|
||||
if(env->ExceptionCheck() || vnetConfigObj == NULL)
|
||||
{
|
||||
LOGE("Error creating new VirtualNetworkConfig object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return vnetConfigObj;
|
||||
}
|
||||
|
||||
jobject newVersion(JNIEnv *env, int major, int minor, int rev)
|
||||
{
|
||||
// create a com.zerotier.sdk.Version object
|
||||
jobject versionObj = env->NewObject(Version_class, Version_ctor, (jint)major, (jint)minor, (jint)rev);
|
||||
if(env->ExceptionCheck() || versionObj == NULL)
|
||||
{
|
||||
LOGE("Error creating new Version object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return versionObj;
|
||||
}
|
||||
|
||||
jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route)
|
||||
{
|
||||
//
|
||||
// may be NULL
|
||||
//
|
||||
jobject targetObj = newInetSocketAddress(env, route.target);
|
||||
if (env->ExceptionCheck()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// may be NULL
|
||||
//
|
||||
jobject viaObj = newInetSocketAddress(env, route.via);
|
||||
if (env->ExceptionCheck()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject routeObj = env->NewObject(
|
||||
VirtualNetworkRoute_class,
|
||||
VirtualNetworkRoute_ctor,
|
||||
(jobject)targetObj,
|
||||
(jobject)viaObj,
|
||||
(jint)route.flags, // ANDROID-56: cast to correct size
|
||||
(jint)route.metric); // ANDROID-56: cast to correct size
|
||||
if(env->ExceptionCheck() || routeObj == NULL)
|
||||
{
|
||||
LOGE("Exception creating VirtualNetworkRoute");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return routeObj;
|
||||
}
|
||||
|
||||
//
|
||||
// may return NULL
|
||||
//
|
||||
jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns)
|
||||
{
|
||||
if (strlen(dns.domain) == 0) {
|
||||
LOGD("dns.domain is empty; returning NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jstring domain = env->NewStringUTF(dns.domain);
|
||||
if (env->ExceptionCheck() || domain == NULL) {
|
||||
LOGE("Exception creating new string");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject addrList = env->NewObject(ArrayList_class, ArrayList_ctor, (jint)0);
|
||||
if (env->ExceptionCheck() || addrList == NULL) {
|
||||
LOGE("Exception creating new ArrayList");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ZT_MAX_DNS_SERVERS; ++i) { //NOLINT
|
||||
|
||||
struct sockaddr_storage tmp = dns.server_addr[i];
|
||||
|
||||
//
|
||||
// may be NULL
|
||||
//
|
||||
jobject addr = newInetSocketAddress(env, tmp);
|
||||
if (env->ExceptionCheck()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (addr == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
env->CallBooleanMethod(addrList, ArrayList_add_method, (jobject)addr);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
LOGE("Exception calling add");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->DeleteLocalRef(addr);
|
||||
}
|
||||
|
||||
jobject dnsObj = env->NewObject(
|
||||
VirtualNetworkDNS_class,
|
||||
VirtualNetworkDNS_ctor,
|
||||
(jstring)domain,
|
||||
(jobject)addrList);
|
||||
if (env->ExceptionCheck() || dnsObj == NULL) {
|
||||
LOGE("Exception creating new VirtualNetworkDNS");
|
||||
return NULL;
|
||||
}
|
||||
return dnsObj;
|
||||
}
|
||||
|
||||
jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status) {
|
||||
|
||||
jstring pubIdentStr = env->NewStringUTF(status.publicIdentity);
|
||||
if(env->ExceptionCheck() || pubIdentStr == NULL)
|
||||
{
|
||||
LOGE("Exception creating new string");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jstring secIdentStr = env->NewStringUTF(status.secretIdentity);
|
||||
if(env->ExceptionCheck() || secIdentStr == NULL)
|
||||
{
|
||||
LOGE("Exception creating new string");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject nodeStatusObj = env->NewObject(
|
||||
NodeStatus_class,
|
||||
NodeStatus_ctor,
|
||||
(jlong)status.address,
|
||||
(jstring)pubIdentStr,
|
||||
(jstring)secIdentStr,
|
||||
(jboolean)status.online);
|
||||
if(env->ExceptionCheck() || nodeStatusObj == NULL) {
|
||||
LOGE("Exception creating new NodeStatus");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return nodeStatusObj;
|
||||
}
|
||||
|
||||
jobjectArray newPeerArray(JNIEnv *env, const ZT_Peer *peers, size_t count) {
|
||||
return newArrayObject<ZT_Peer, newPeer>(env, peers, count, Peer_class);
|
||||
}
|
||||
|
||||
jobjectArray newVirtualNetworkConfigArray(JNIEnv *env, const ZT_VirtualNetworkConfig *networks, size_t count) {
|
||||
return newArrayObject<ZT_VirtualNetworkConfig, newNetworkConfig>(env, networks, count, VirtualNetworkConfig_class);
|
||||
}
|
||||
|
||||
jobjectArray newPeerPhysicalPathArray(JNIEnv *env, const ZT_PeerPhysicalPath *paths, size_t count) {
|
||||
return newArrayObject<ZT_PeerPhysicalPath, newPeerPhysicalPath>(env, paths, count, PeerPhysicalPath_class);
|
||||
}
|
||||
|
||||
jobjectArray newInetSocketAddressArray(JNIEnv *env, const sockaddr_storage *addresses, size_t count) {
|
||||
return newArrayObject<sockaddr_storage, newInetSocketAddress>(env, addresses, count, InetSocketAddress_class);
|
||||
}
|
||||
|
||||
jobjectArray newVirtualNetworkRouteArray(JNIEnv *env, const ZT_VirtualNetworkRoute *routes, size_t count) {
|
||||
return newArrayObject<ZT_VirtualNetworkRoute, newVirtualNetworkRoute>(env, routes, count, VirtualNetworkRoute_class);
|
||||
}
|
||||
|
||||
void newArrayObject_logCount(size_t count) {
|
||||
LOGE("count > JSIZE_MAX: %zu", count);
|
||||
}
|
||||
|
||||
void newArrayObject_log(const char *msg) {
|
||||
LOGE("%s", msg);
|
||||
}
|
||||
|
||||
jbyteArray newByteArray(JNIEnv *env, const unsigned char *bytes, size_t count) {
|
||||
|
||||
if (count > JSIZE_MAX) {
|
||||
LOGE("count > JSIZE_MAX: %zu", count);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jsize jCount = static_cast<jsize>(count);
|
||||
const jbyte *jBytes = reinterpret_cast<const jbyte *>(bytes);
|
||||
|
||||
jbyteArray byteArrayObj = env->NewByteArray(jCount);
|
||||
if(byteArrayObj == NULL)
|
||||
{
|
||||
LOGE("NewByteArray returned NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(byteArrayObj, 0, jCount, jBytes);
|
||||
if (env->ExceptionCheck()) {
|
||||
LOGE("Exception when calling SetByteArrayRegion");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return byteArrayObj;
|
||||
}
|
||||
|
||||
jbyteArray newByteArray(JNIEnv *env, size_t count) {
|
||||
|
||||
if (count > JSIZE_MAX) {
|
||||
LOGE("count > JSIZE_MAX: %zu", count);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jsize jCount = static_cast<jsize>(count);
|
||||
|
||||
jbyteArray byteArrayObj = env->NewByteArray(jCount);
|
||||
if(byteArrayObj == NULL)
|
||||
{
|
||||
LOGE("NewByteArray returned NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return byteArrayObj;
|
||||
}
|
||||
|
||||
bool isSocketAddressEmpty(const sockaddr_storage addr) {
|
||||
|
||||
//
|
||||
// was:
|
||||
// struct sockaddr_storage nullAddress = {0};
|
||||
//
|
||||
// but was getting this warning:
|
||||
// warning: suggest braces around initialization of subobject
|
||||
//
|
||||
// when building ZeroTierOne
|
||||
//
|
||||
sockaddr_storage emptyAddress; //NOLINT
|
||||
|
||||
//
|
||||
// It is possible to assume knowledge about internals of sockaddr_storage and construct
|
||||
// correct 0-initializer, but it is simpler to just treat sockaddr_storage as opaque and
|
||||
// use memset here to fill with 0
|
||||
//
|
||||
// This is also done in InetAddress.hpp for InetAddress
|
||||
//
|
||||
memset(&emptyAddress, 0, sizeof(sockaddr_storage));
|
||||
|
||||
return (memcmp(&addr, &emptyAddress, sizeof(sockaddr_storage)) == 0); //NOLINT
|
||||
}
|
||||
|
||||
//
|
||||
// returns empty sockaddr_storage on error
|
||||
//
|
||||
sockaddr_storage fromSocketAddressObject(JNIEnv *env, jobject sockAddressObject) {
|
||||
|
||||
sockaddr_storage emptyAddress; //NOLINT
|
||||
|
||||
memset(&emptyAddress, 0, sizeof(sockaddr_storage));
|
||||
|
||||
jint port = env->CallIntMethod(sockAddressObject, InetSocketAddress_getPort_method);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
LOGE("Exception calling getPort");
|
||||
return emptyAddress;
|
||||
}
|
||||
|
||||
jobject addressObject = env->CallObjectMethod(sockAddressObject, InetSocketAddress_getAddress_method);
|
||||
if(env->ExceptionCheck() || addressObject == NULL)
|
||||
{
|
||||
LOGE("Exception calling getAddress");
|
||||
return emptyAddress;
|
||||
}
|
||||
|
||||
jbyteArray addressArrayObj = reinterpret_cast<jbyteArray>(env->CallObjectMethod(addressObject, InetAddress_getAddress_method));
|
||||
if(env->ExceptionCheck() || addressArrayObj == NULL)
|
||||
{
|
||||
LOGE("Exception calling getAddress");
|
||||
return emptyAddress;
|
||||
}
|
||||
|
||||
sockaddr_storage addr = {};
|
||||
|
||||
if (env->IsInstanceOf(addressObject, Inet4Address_class)) {
|
||||
|
||||
// IPV4
|
||||
|
||||
assert(env->GetArrayLength(addressArrayObj) == 4);
|
||||
|
||||
sockaddr_in *addr_4 = reinterpret_cast<sockaddr_in *>(&addr);
|
||||
addr_4->sin_family = AF_INET;
|
||||
addr_4->sin_port = htons(port);
|
||||
|
||||
void *data = env->GetPrimitiveArrayCritical(addressArrayObj, NULL);
|
||||
memcpy(&addr_4->sin_addr.s_addr, data, 4);
|
||||
env->ReleasePrimitiveArrayCritical(addressArrayObj, data, 0);
|
||||
|
||||
} else if (env->IsInstanceOf(addressObject, Inet6Address_class)) {
|
||||
|
||||
// IPV6
|
||||
|
||||
assert(env->GetArrayLength(addressArrayObj) == 16);
|
||||
|
||||
sockaddr_in6 *addr_6 = reinterpret_cast<sockaddr_in6 *>(&addr);
|
||||
addr_6->sin6_family = AF_INET6;
|
||||
addr_6->sin6_port = htons(port);
|
||||
|
||||
void *data = env->GetPrimitiveArrayCritical(addressArrayObj, NULL);
|
||||
memcpy(&addr_6->sin6_addr.s6_addr, data, 16);
|
||||
env->ReleasePrimitiveArrayCritical(addressArrayObj, data, 0);
|
||||
|
||||
} else {
|
||||
assert(false && "addressObject is neither Inet4Address nor Inet6Address");
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
169
java/jni/ZT_jniutils.h
Normal file
169
java/jni/ZT_jniutils.h
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef ZT_jniutils_h_
|
||||
#define ZT_jniutils_h_
|
||||
|
||||
#include <jni.h>
|
||||
#include <ZeroTierOne.h>
|
||||
|
||||
#include <limits> // for numeric_limits
|
||||
#include <sys/socket.h> // for sockaddr_storage
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
#define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
|
||||
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
|
||||
#else
|
||||
#define LOGV(...)
|
||||
#define LOGD(...)
|
||||
#endif
|
||||
|
||||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
|
||||
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
|
||||
#else
|
||||
#if !defined(NDEBUG)
|
||||
#define LOGV(...) fprintf(stdout, __VA_ARGS__)
|
||||
#define LOGD(...) fprintf(stdout, __VA_ARGS__)
|
||||
#else
|
||||
#define LOGV(...)
|
||||
#define LOGD(...)
|
||||
#endif
|
||||
|
||||
#define LOGI(...) fprintf(stdout, __VA_ARGS__)
|
||||
#define LOGE(...) fprintf(stdout, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
//
|
||||
// Call GetEnv and assert if there is an error
|
||||
//
|
||||
#define GETENV(env, vm) \
|
||||
do { \
|
||||
jint getEnvRet; \
|
||||
assert(vm); \
|
||||
if ((getEnvRet = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) != JNI_OK) { \
|
||||
LOGE("Error calling GetEnv: %d", getEnvRet); \
|
||||
assert(false && "Error calling GetEnv"); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
//
|
||||
// Call GetJavaVM and assert if there is an error
|
||||
//
|
||||
#define GETJAVAVM(env, vm) \
|
||||
do { \
|
||||
jint getJavaVMRet; \
|
||||
if ((getJavaVMRet = env->GetJavaVM(&vm)) != 0) { \
|
||||
LOGE("Error calling GetJavaVM: %d", getJavaVMRet); \
|
||||
assert(false && "Error calling GetJavaVM"); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
|
||||
const jsize JSIZE_MAX = std::numeric_limits<jsize>::max();
|
||||
|
||||
jobject createResultObject(JNIEnv *env, ZT_ResultCode code);
|
||||
jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status);
|
||||
jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type);
|
||||
jobject createEvent(JNIEnv *env, ZT_Event event);
|
||||
jobject createPeerRole(JNIEnv *env, ZT_PeerRole role);
|
||||
jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op);
|
||||
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
int addressPort(const sockaddr_storage addr);
|
||||
|
||||
jobject newPeer(JNIEnv *env, const ZT_Peer &peer);
|
||||
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp);
|
||||
|
||||
jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &config);
|
||||
|
||||
jobject newVersion(JNIEnv *env, int major, int minor, int rev);
|
||||
|
||||
jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route);
|
||||
|
||||
jobject newVirtualNetworkDNS(JNIEnv *env, const ZT_VirtualNetworkDNS &dns);
|
||||
|
||||
jobject newNodeStatus(JNIEnv *env, const ZT_NodeStatus &status);
|
||||
|
||||
jobjectArray newPeerArray(JNIEnv *env, const ZT_Peer *peers, size_t count);
|
||||
|
||||
jobjectArray newVirtualNetworkConfigArray(JNIEnv *env, const ZT_VirtualNetworkConfig *networks, size_t count);
|
||||
|
||||
jobjectArray newPeerPhysicalPathArray(JNIEnv *env, const ZT_PeerPhysicalPath *paths, size_t count);
|
||||
|
||||
jobjectArray newInetSocketAddressArray(JNIEnv *env, const sockaddr_storage *addresses, size_t count);
|
||||
|
||||
jobjectArray newVirtualNetworkRouteArray(JNIEnv *env, const ZT_VirtualNetworkRoute *routes, size_t count);
|
||||
|
||||
//
|
||||
// log functions only for newArrayObject below
|
||||
//
|
||||
void newArrayObject_logCount(size_t count);
|
||||
void newArrayObject_log(const char *msg);
|
||||
|
||||
//
|
||||
// function template for creating array objects
|
||||
//
|
||||
template <typename T, jobject (*F)(JNIEnv *, const T &)>
|
||||
jobjectArray newArrayObject(JNIEnv *env, const T *buffer, size_t count, jclass clazz) {
|
||||
|
||||
if (count > JSIZE_MAX) {
|
||||
newArrayObject_logCount(count);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jsize jCount = static_cast<jsize>(count);
|
||||
|
||||
jobjectArray arrayObj = env->NewObjectArray(jCount, clazz, NULL);
|
||||
if (env->ExceptionCheck() || arrayObj == NULL) {
|
||||
newArrayObject_log("Error creating array object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (jsize i = 0; i < jCount; i++) {
|
||||
|
||||
jobject obj = F(env, buffer[i]);
|
||||
if(env->ExceptionCheck() || obj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetObjectArrayElement(arrayObj, i, obj);
|
||||
if(env->ExceptionCheck()) {
|
||||
newArrayObject_log("Error assigning object to array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->DeleteLocalRef(obj);
|
||||
}
|
||||
|
||||
return arrayObj;
|
||||
}
|
||||
|
||||
jbyteArray newByteArray(JNIEnv *env, const unsigned char *bytes, size_t count);
|
||||
|
||||
jbyteArray newByteArray(JNIEnv *env, size_t count);
|
||||
|
||||
bool isSocketAddressEmpty(const sockaddr_storage addr);
|
||||
|
||||
sockaddr_storage fromSocketAddressObject(JNIEnv *env, jobject sockAddressObject);
|
||||
|
||||
#endif // ZT_jniutils_h_
|
||||
1432
java/jni/com_zerotierone_sdk_Node.cpp
Normal file
1432
java/jni/com_zerotierone_sdk_Node.cpp
Normal file
File diff suppressed because it is too large
Load Diff
141
java/jni/com_zerotierone_sdk_Node.h
Normal file
141
java/jni/com_zerotierone_sdk_Node.h
Normal file
@@ -0,0 +1,141 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class com_zerotier_sdk_Node */
|
||||
|
||||
#ifndef _Included_com_zerotierone_sdk_Node
|
||||
#define _Included_com_zerotierone_sdk_Node
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: node_init
|
||||
* Signature: (JLcom/zerotier/sdk/DataStoreGetListener;Lcom/zerotier/sdk/DataStorePutListener;Lcom/zerotier/sdk/PacketSender;Lcom/zerotier/sdk/EventListener;Lcom/zerotier/sdk/VirtualNetworkFrameListener;Lcom/zerotier/sdk/VirtualNetworkConfigListener;Lcom/zerotier/sdk/PathChecker;)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init
|
||||
(JNIEnv *, jobject, jlong, jobject, jobject, jobject, jobject, jobject, jobject, jobject);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: node_isInited
|
||||
* Signature: (J)Z;
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_sdk_Node_node_1isInited
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: node_delete
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processVirtualNetworkFrame
|
||||
* Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong, jlong, jint, jint, jbyteArray, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processWirePacket
|
||||
* Signature: (JJJLjava/net/InetSockAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jobject, jbyteArray, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processBackgroundTasks
|
||||
* Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks
|
||||
(JNIEnv *, jobject, jlong, jlong, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: join
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: leave
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: multicastSubscribe
|
||||
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: multicastUnsubscribe
|
||||
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: address
|
||||
* Signature: (J)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: status
|
||||
* Signature: (J)Lcom/zerotier/sdk/NodeStatus;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: networkConfig
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: version
|
||||
* Signature: ()Lcom/zerotier/sdk/Version;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: peers
|
||||
* Signature: (J)[Lcom/zerotier/sdk/Peer;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: networkConfigs
|
||||
* Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networkConfigs
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Reference in New Issue
Block a user