diff options
Diffstat (limited to 'zerotierone/java')
31 files changed, 4697 insertions, 0 deletions
diff --git a/zerotierone/java/README.md b/zerotierone/java/README.md new file mode 100644 index 0000000..2650ec3 --- /dev/null +++ b/zerotierone/java/README.md @@ -0,0 +1,17 @@ +ZeroTier One SDK - Android JNI Wrapper +===== + + +Building +----- + +Reqires: + +* JDK +* ANT +* Android NDK + +Required Environment Variables: + +* NDK\_BUILD\_LOC - Path do the ndk-build script in the Android NDK +* ANDROID\_PLATFORM - path to the directory android.jar lives (on Windows: C:\Users\<username>\AppData\Local\Android\sdk\platforms\android-21) diff --git a/zerotierone/java/build.xml b/zerotierone/java/build.xml new file mode 100644 index 0000000..4604ad6 --- /dev/null +++ b/zerotierone/java/build.xml @@ -0,0 +1,118 @@ +<project default="build_jar" name="ZeroTierOneSDK" basedir="."> + <property environment="env"/> + + <condition property="isWindows"> + <os family="windows"/> + </condition> + + <condition property="isMac"> + <os family="mac"/> + </condition> + + <target name="clean_ant"> + <delete dir="bin" failonerror="false"/> + <delete dir="classes" failonerror="false"/> + <delete dir="build_win32" failonerror="false"/> + <delete dir="build_win64" failonerror="false"/> + <delete dir="mac32_64" failonerror="false"/> + <delete dir="libs" failonerror="false"/> + <delete dir="obj" failonerror="false"/> + </target> + + <target name="build_java"> + <echo message="os.name = ${os.name}"/> + <echo message="os.arch = ${os.arch}"/> + <echo message="ant.java.version = ${ant.java.version}"/> + <echo message="java.version = ${java.version}"/> + <echo message="ndk.loc = ${env.NDK_BUILD_LOC}"/> + <echo message="sdk.loc = ${env.ANDROID_PLATFORM}"/> + <echo message="user.dir = ${user.dir}"/> + <echo message="zt1.dir = ${env.ZT}"/> + <mkdir dir="bin"/> + <mkdir dir="classes"/> + <javac srcdir="src" + destdir="classes" + source="1.7" + target="1.7" + classpath="${env.ANDROID_PLATFORM}/android.jar" + includeantruntime="false"/> + </target> + + <target name="build_android"> + <exec dir="jni" executable="${env.NDK_BUILD_LOC}" failonerror="true"> + <arg value="ZT1=${env.ZT}"/> + <arg value="V=1"/> + <!-- <arg value="NDK_DEBUG=1"/> --> + </exec> + <copy file="libs/arm64-v8a/libZeroTierOneJNI.so" + tofile="classes/lib/arm64-v8a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/armeabi/libZeroTierOneJNI.so" + tofile="classes/lib/armeabi/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/armeabi-v7a/libZeroTierOneJNI.so" + tofile="classes/lib/armeabi-v7a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/mips/libZeroTierOneJNI.so" + tofile="classes/lib/mips/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/mips64/libZeroTierOneJNI.so" + tofile="classes/lib/mips64/libZeroTierOne.so" + overwrite="true"/> + <copy file="libs/x86/libZeroTierOneJNI.so" + tofile="classes/lib/x86/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/x86_64/libZeroTierOneJNI.so" + tofile="classes/lib/x86_64/libZeroTierOneJNI.so" + overwrite="true"/> + </target> + + + <target name="windows" if="isWindows"> + <mkdir dir="build_win32"/> + <exec dir="build_win32/" executable="cmake" failonerror="true"> + <arg line=".. -G"Visual Studio 11 2012" -DCMAKE_BUILD_TYPE=Release"/> + </exec> + <exec dir="build_win32/" executable="cmake" failonerror="true"> + <arg line="--build . --config Release"/> + </exec> + <copy file="build_win32/Release/ZeroTierOneJNI.dll" + tofile="classes/lib/ZeroTierOneJNI_win32.dll" + overwrite="true"/> + + <mkdir dir="build_win64"/> + <exec dir="build_win64/" executable="cmake" failonerror="true"> + <arg line=".. -G"Visual Studio 11 2012 Win64" -DCMAKE_BUILD_TYPE=Release"/> + </exec> + <exec dir="build_win64/" executable="cmake" failonerror="true"> + <arg line="--build . --config Release"/> + </exec> + <copy file="build_win64/Release/ZeroTierOneJNI.dll" + tofile="classes/lib/ZeroTierOneJNI_win64.dll" + overwrite="true"/> + </target> + + <target name="mac" if="isMac"> + <mkdir dir="mac32_64"/> + <exec dir="mac32_64/" executable="cmake" failonerror="true"> + <arg line=".. -DCMAKE_BUILD_TYPE=Release"/> + </exec> + <exec dir="mac32_64/" executable="cmake" failonerror="true"> + <arg line="--build . --config Release"/> + </exec> + <copy file="mac32_64/libZeroTierOneJNI.jnilib" + tofile="classes/lib/libZeroTierOneJNI.jnilib" + overwrite="true"/> + </target> + + <target name="build_jar" depends="build_java,build_android,windows,mac"> + <jar destfile="bin/ZeroTierOneSDK.jar" basedir="classes"/> + </target> + + <target name="docs"> + <echo message="Generating Javadocs"/> + <mkdir dir="doc/"/> + <javadoc sourcepath="src/" + destdir="doc/"/> + </target> +</project>
\ No newline at end of file diff --git a/zerotierone/java/jni/Android.mk b/zerotierone/java/jni/Android.mk new file mode 100644 index 0000000..5c2f1c7 --- /dev/null +++ b/zerotierone/java/jni/Android.mk @@ -0,0 +1,47 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := ZeroTierOneJNI +LOCAL_C_INCLUDES := $(ZT1)/include +LOCAL_C_INCLUDES += $(ZT1)/node +LOCAL_LDLIBS := -llog +# LOCAL_CFLAGS := -g + +# ZeroTierOne SDK source files +LOCAL_SRC_FILES := \ + $(ZT1)/ext/lz4/lz4.c \ + $(ZT1)/ext/json-parser/json.c \ + $(ZT1)/ext/http-parser/http_parser.c \ + $(ZT1)/node/C25519.cpp \ + $(ZT1)/node/CertificateOfMembership.cpp \ + $(ZT1)/node/DeferredPackets.cpp \ + $(ZT1)/node/Dictionary.cpp \ + $(ZT1)/node/Identity.cpp \ + $(ZT1)/node/IncomingPacket.cpp \ + $(ZT1)/node/InetAddress.cpp \ + $(ZT1)/node/Multicaster.cpp \ + $(ZT1)/node/Network.cpp \ + $(ZT1)/node/NetworkConfig.cpp \ + $(ZT1)/node/Node.cpp \ + $(ZT1)/node/OutboundMulticast.cpp \ + $(ZT1)/node/Packet.cpp \ + $(ZT1)/node/Path.cpp \ + $(ZT1)/node/Peer.cpp \ + $(ZT1)/node/Poly1305.cpp \ + $(ZT1)/node/Salsa20.cpp \ + $(ZT1)/node/SelfAwareness.cpp \ + $(ZT1)/node/SHA512.cpp \ + $(ZT1)/node/Switch.cpp \ + $(ZT1)/node/Topology.cpp \ + $(ZT1)/node/Utils.cpp \ + $(ZT1)/osdep/Http.cpp \ + $(ZT1)/osdep/OSUtils.cpp + +# JNI Files +LOCAL_SRC_FILES += \ + com_zerotierone_sdk_Node.cpp \ + ZT_jniutils.cpp \ + ZT_jnilookup.cpp + +include $(BUILD_SHARED_LIBRARY)
\ No newline at end of file diff --git a/zerotierone/java/jni/Application.mk b/zerotierone/java/jni/Application.mk new file mode 100644 index 0000000..608e94c --- /dev/null +++ b/zerotierone/java/jni/Application.mk @@ -0,0 +1,5 @@ +NDK_TOOLCHAIN_VERSION := clang +APP_STL := c++_static +APP_CPPFLAGS := -O3 -fPIC -fPIE -fvectorize -Wall -fstack-protector -fexceptions -fno-strict-aliasing -Wno-deprecated-register -DZT_NO_TYPE_PUNNING=1 +APP_PLATFORM := android-14 +APP_ABI := all diff --git a/zerotierone/java/jni/ZT_jnilookup.cpp b/zerotierone/java/jni/ZT_jnilookup.cpp new file mode 100644 index 0000000..be52a36 --- /dev/null +++ b/zerotierone/java/jni/ZT_jnilookup.cpp @@ -0,0 +1,158 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#include "ZT_jnilookup.h" +#include "ZT_jniutils.h" + +JniLookup::JniLookup() + : m_jvm(NULL) +{ + LOGV("JNI Cache Created"); +} + +JniLookup::JniLookup(JavaVM *jvm) + : m_jvm(jvm) +{ + LOGV("JNI Cache Created"); +} + +JniLookup::~JniLookup() +{ + LOGV("JNI Cache Destroyed"); +} + + +void JniLookup::setJavaVM(JavaVM *jvm) +{ + LOGV("Assigned JVM to object"); + m_jvm = jvm; +} + + +jclass JniLookup::findClass(const std::string &name) +{ + if(!m_jvm) + return NULL; + + // get the class from the JVM + JNIEnv *env = NULL; + if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) + { + LOGE("Error retreiving JNI Environment"); + return NULL; + } + + jclass cls = env->FindClass(name.c_str()); + if(env->ExceptionCheck()) + { + LOGE("Error finding class: %s", name.c_str()); + return NULL; + } + + return cls; +} + + +jmethodID JniLookup::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig) +{ + if(!m_jvm) + return NULL; + + JNIEnv *env = NULL; + if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) + { + return NULL; + } + + jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str()); + if(env->ExceptionCheck()) + { + return NULL; + } + + return mid; +} + +jmethodID JniLookup::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig) +{ + if(!m_jvm) + return NULL; + + JNIEnv *env = NULL; + if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) + { + return NULL; + } + + jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str()); + if(env->ExceptionCheck()) + { + return NULL; + } + + return mid; +} + +jfieldID JniLookup::findField(jclass cls, const std::string &fieldName, const std::string &typeStr) +{ + if(!m_jvm) + return NULL; + + JNIEnv *env = NULL; + if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) + { + return NULL; + } + + jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str()); + if(env->ExceptionCheck()) + { + return NULL; + } + + return fid; +} + +jfieldID JniLookup::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr) +{ + if(!m_jvm) + return NULL; + + JNIEnv *env = NULL; + if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK) + { + return NULL; + } + + jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str()); + if(env->ExceptionCheck()) + { + return NULL; + } + + return fid; +}
\ No newline at end of file diff --git a/zerotierone/java/jni/ZT_jnilookup.h b/zerotierone/java/jni/ZT_jnilookup.h new file mode 100644 index 0000000..f5bd97d --- /dev/null +++ b/zerotierone/java/jni/ZT_jnilookup.h @@ -0,0 +1,54 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#ifndef ZT_JNILOOKUP_H_ +#define ZT_JNILOOKUP_H_ + +#include <jni.h> +#include <map> +#include <string> + + + +class JniLookup { +public: + JniLookup(); + JniLookup(JavaVM *jvm); + ~JniLookup(); + + void setJavaVM(JavaVM *jvm); + + jclass findClass(const std::string &name); + jmethodID findMethod(jclass cls, const std::string &methodName, const std::string &methodSig); + jmethodID findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig); + jfieldID findField(jclass cls, const std::string &fieldName, const std::string &typeStr); + jfieldID findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr); +private: + JavaVM *m_jvm; +}; + +#endif
\ No newline at end of file diff --git a/zerotierone/java/jni/ZT_jniutils.cpp b/zerotierone/java/jni/ZT_jniutils.cpp new file mode 100644 index 0000000..ae1aa92 --- /dev/null +++ b/zerotierone/java/jni/ZT_jniutils.cpp @@ -0,0 +1,931 @@ +#include "ZT_jniutils.h" +#include "ZT_jnilookup.h" +#include <string> +#include <assert.h> + +extern JniLookup lookup; + +#ifdef __cplusplus +extern "C" { +#endif + +jobject createResultObject(JNIEnv *env, ZT_ResultCode code) +{ + jclass resultClass = NULL; + + jobject resultObject = NULL; + + resultClass = lookup.findClass("com/zerotier/sdk/ResultCode"); + if(resultClass == NULL) + { + LOGE("Couldnt find ResultCode class"); + return NULL; // exception thrown + } + + std::string fieldName; + switch(code) + { + case ZT_RESULT_OK: + LOGV("ZT_RESULT_OK"); + fieldName = "RESULT_OK"; + break; + case ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY: + LOGV("ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY"); + fieldName = "RESULT_FATAL_ERROR_OUT_OF_MEMORY"; + break; + case ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED: + LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED"); + fieldName = "RESULT_FATAL_ERROR_DATA_STORE_FAILED"; + break; + case ZT_RESULT_ERROR_NETWORK_NOT_FOUND: + LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED"); + fieldName = "RESULT_ERROR_NETWORK_NOT_FOUND"; + break; + case ZT_RESULT_FATAL_ERROR_INTERNAL: + default: + LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED"); + fieldName = "RESULT_FATAL_ERROR_INTERNAL"; + break; + } + + jfieldID enumField = lookup.findStaticField(resultClass, fieldName.c_str(), "Lcom/zerotier/sdk/ResultCode;"); + if(env->ExceptionCheck() || enumField == NULL) + { + LOGE("Error on FindStaticField"); + return NULL; + } + + resultObject = env->GetStaticObjectField(resultClass, enumField); + if(env->ExceptionCheck() || resultObject == NULL) + { + LOGE("Error on GetStaticObjectField"); + } + return resultObject; +} + + +jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status) +{ + jobject statusObject = NULL; + + jclass statusClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkStatus"); + if(statusClass == NULL) + { + return NULL; // exception thrown + } + + std::string fieldName; + switch(status) + { + case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: + fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION"; + break; + case ZT_NETWORK_STATUS_OK: + fieldName = "NETWORK_STATUS_OK"; + break; + case ZT_NETWORK_STATUS_ACCESS_DENIED: + fieldName = "NETWORK_STATUS_ACCESS_DENIED"; + break; + case ZT_NETWORK_STATUS_NOT_FOUND: + fieldName = "NETWORK_STATUS_NOT_FOUND"; + break; + case ZT_NETWORK_STATUS_PORT_ERROR: + fieldName = "NETWORK_STATUS_PORT_ERROR"; + break; + case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: + fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD"; + break; + } + + jfieldID enumField = lookup.findStaticField(statusClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkStatus;"); + + statusObject = env->GetStaticObjectField(statusClass, enumField); + + return statusObject; +} + +jobject createEvent(JNIEnv *env, ZT_Event event) +{ + jclass eventClass = NULL; + jobject eventObject = NULL; + + eventClass = lookup.findClass("com/zerotier/sdk/Event"); + if(eventClass == NULL) + { + return NULL; + } + + std::string fieldName; + switch(event) + { + case ZT_EVENT_UP: + fieldName = "EVENT_UP"; + break; + case ZT_EVENT_OFFLINE: + fieldName = "EVENT_OFFLINE"; + break; + case ZT_EVENT_ONLINE: + fieldName = "EVENT_ONLINE"; + break; + case ZT_EVENT_DOWN: + fieldName = "EVENT_DOWN"; + break; + case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION"; + break; + case ZT_EVENT_TRACE: + fieldName = "EVENT_TRACE"; + break; + } + + jfieldID enumField = lookup.findStaticField(eventClass, fieldName.c_str(), "Lcom/zerotier/sdk/Event;"); + + eventObject = env->GetStaticObjectField(eventClass, enumField); + + return eventObject; +} + +jobject createPeerRole(JNIEnv *env, ZT_PeerRole role) +{ + jclass peerRoleClass = NULL; + jobject peerRoleObject = NULL; + + peerRoleClass = lookup.findClass("com/zerotier/sdk/PeerRole"); + if(peerRoleClass == NULL) + { + return NULL; + } + + std::string fieldName; + switch(role) + { + case ZT_PEER_ROLE_LEAF: + fieldName = "PEER_ROLE_LEAF"; + break; + case ZT_PEER_ROLE_RELAY: + fieldName = "PEER_ROLE_RELAY"; + break; + case ZT_PEER_ROLE_ROOT: + fieldName = "PEER_ROLE_ROOTS"; + break; + } + + jfieldID enumField = lookup.findStaticField(peerRoleClass, fieldName.c_str(), "Lcom/zerotier/sdk/PeerRole;"); + + peerRoleObject = env->GetStaticObjectField(peerRoleClass, enumField); + + return peerRoleObject; +} + +jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type) +{ + jclass vntypeClass = NULL; + jobject vntypeObject = NULL; + + vntypeClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkType"); + if(env->ExceptionCheck() || vntypeClass == NULL) + { + return NULL; + } + + std::string fieldName; + switch(type) + { + case ZT_NETWORK_TYPE_PRIVATE: + fieldName = "NETWORK_TYPE_PRIVATE"; + break; + case ZT_NETWORK_TYPE_PUBLIC: + fieldName = "NETWORK_TYPE_PUBLIC"; + break; + } + + jfieldID enumField = lookup.findStaticField(vntypeClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkType;"); + vntypeObject = env->GetStaticObjectField(vntypeClass, enumField); + return vntypeObject; +} + +jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op) +{ + jclass vnetConfigOpClass = NULL; + jobject vnetConfigOpObject = NULL; + + vnetConfigOpClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfigOperation"); + if(env->ExceptionCheck() || vnetConfigOpClass == NULL) + { + return NULL; + } + + std::string fieldName; + switch(op) + { + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_UP"; + break; + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE"; + break; + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN"; + break; + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY"; + break; + } + + jfieldID enumField = lookup.findStaticField(vnetConfigOpClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkConfigOperation;"); + vnetConfigOpObject = env->GetStaticObjectField(vnetConfigOpClass, enumField); + return vnetConfigOpObject; +} + +jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr) +{ + LOGV("newInetAddress"); + jclass inetAddressClass = NULL; + jmethodID inetAddress_getByAddress = NULL; + + inetAddressClass = lookup.findClass("java/net/InetAddress"); + if(env->ExceptionCheck() || inetAddressClass == NULL) + { + LOGE("Error finding InetAddress class"); + return NULL; + } + + inetAddress_getByAddress = lookup.findStaticMethod( + inetAddressClass, "getByAddress", "([B)Ljava/net/InetAddress;"); + if(env->ExceptionCheck() || inetAddress_getByAddress == NULL) + { + LOGE("Erorr finding getByAddress() static method"); + return NULL; + } + + jobject inetAddressObj = NULL; + switch(addr.ss_family) + { + case AF_INET6: + { + sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr; + jbyteArray buff = env->NewByteArray(16); + if(buff == NULL) + { + LOGE("Error creating IPV6 byte array"); + return NULL; + } + + env->SetByteArrayRegion(buff, 0, 16, (jbyte*)ipv6->sin6_addr.s6_addr); + inetAddressObj = env->CallStaticObjectMethod( + inetAddressClass, inetAddress_getByAddress, buff); + } + break; + case AF_INET: + { + sockaddr_in *ipv4 = (sockaddr_in*)&addr; + jbyteArray buff = env->NewByteArray(4); + if(buff == NULL) + { + LOGE("Error creating IPV4 byte array"); + return NULL; + } + + env->SetByteArrayRegion(buff, 0, 4, (jbyte*)&ipv4->sin_addr); + inetAddressObj = env->CallStaticObjectMethod( + inetAddressClass, inetAddress_getByAddress, buff); + } + break; + } + if(env->ExceptionCheck() || inetAddressObj == NULL) { + LOGE("Error creating InetAddress object"); + return NULL; + } + + return inetAddressObj; +} + +jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr) +{ + LOGV("newInetSocketAddress Called"); + jclass inetSocketAddressClass = NULL; + jmethodID inetSocketAddress_constructor = NULL; + + inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress"); + if(env->ExceptionCheck() || inetSocketAddressClass == NULL) + { + LOGE("Error finding InetSocketAddress Class"); + return NULL; + } + + jobject inetAddressObject = newInetAddress(env, addr); + + if(env->ExceptionCheck() || inetAddressObject == NULL) + { + LOGE("Error creating new inet address"); + return NULL; + } + + inetSocketAddress_constructor = lookup.findMethod( + inetSocketAddressClass, "<init>", "(Ljava/net/InetAddress;I)V"); + if(env->ExceptionCheck() || inetSocketAddress_constructor == NULL) + { + LOGE("Error finding InetSocketAddress constructor"); + return NULL; + } + + int port = 0; + switch(addr.ss_family) + { + case AF_INET6: + { + LOGV("IPV6 Address"); + sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr; + port = ntohs(ipv6->sin6_port); + LOGV("Port %d", port); + } + break; + case AF_INET: + { + LOGV("IPV4 Address"); + sockaddr_in *ipv4 = (sockaddr_in*)&addr; + port = ntohs(ipv4->sin_port); + LOGV("Port: %d", port); + } + break; + default: + { + LOGE("ERROR: addr.ss_family is not set or unknown"); + break; + } + }; + + + jobject inetSocketAddressObject = env->NewObject(inetSocketAddressClass, inetSocketAddress_constructor, inetAddressObject, port); + if(env->ExceptionCheck() || inetSocketAddressObject == NULL) { + LOGE("Error creating InetSocketAddress object"); + } + return inetSocketAddressObject; +} + +jobject newMulticastGroup(JNIEnv *env, const ZT_MulticastGroup &mc) +{ + jclass multicastGroupClass = NULL; + jmethodID multicastGroup_constructor = NULL; + + jfieldID macField = NULL; + jfieldID adiField = NULL; + + multicastGroupClass = lookup.findClass("com/zerotier/sdk/MulticastGroup"); + if(env->ExceptionCheck() || multicastGroupClass == NULL) + { + return NULL; + } + + multicastGroup_constructor = lookup.findMethod( + multicastGroupClass, "<init>", "()V"); + if(env->ExceptionCheck() || multicastGroup_constructor == NULL) + { + return NULL; + } + + jobject multicastGroupObj = env->NewObject(multicastGroupClass, multicastGroup_constructor); + if(env->ExceptionCheck() || multicastGroupObj == NULL) + { + return NULL; + } + + macField = lookup.findField(multicastGroupClass, "mac", "J"); + if(env->ExceptionCheck() || macField == NULL) + { + return NULL; + } + + adiField = lookup.findField(multicastGroupClass, "adi", "J"); + if(env->ExceptionCheck() || adiField == NULL) + { + return NULL; + } + + env->SetLongField(multicastGroupObj, macField, mc.mac); + env->SetLongField(multicastGroupObj, adiField, mc.adi); + + return multicastGroupObj; +} + +jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp) +{ + LOGV("newPeerPhysicalPath Called"); + jclass pppClass = NULL; + + jfieldID addressField = NULL; + jfieldID lastSendField = NULL; + jfieldID lastReceiveField = NULL; + jfieldID activeField = NULL; + jfieldID preferredField = NULL; + + jmethodID ppp_constructor = NULL; + + pppClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath"); + if(env->ExceptionCheck() || pppClass == NULL) + { + LOGE("Error finding PeerPhysicalPath class"); + return NULL; + } + + addressField = lookup.findField(pppClass, "address", "Ljava/net/InetSocketAddress;"); + if(env->ExceptionCheck() || addressField == NULL) + { + LOGE("Error finding address field"); + return NULL; + } + + lastSendField = lookup.findField(pppClass, "lastSend", "J"); + if(env->ExceptionCheck() || lastSendField == NULL) + { + LOGE("Error finding lastSend field"); + return NULL; + } + + lastReceiveField = lookup.findField(pppClass, "lastReceive", "J"); + if(env->ExceptionCheck() || lastReceiveField == NULL) + { + LOGE("Error finding lastReceive field"); + return NULL; + } + + activeField = lookup.findField(pppClass, "active", "Z"); + if(env->ExceptionCheck() || activeField == NULL) + { + LOGE("Error finding active field"); + return NULL; + } + + preferredField = lookup.findField(pppClass, "preferred", "Z"); + if(env->ExceptionCheck() || preferredField == NULL) + { + LOGE("Error finding preferred field"); + return NULL; + } + + ppp_constructor = lookup.findMethod(pppClass, "<init>", "()V"); + if(env->ExceptionCheck() || ppp_constructor == NULL) + { + LOGE("Error finding PeerPhysicalPath constructor"); + return NULL; + } + + jobject pppObject = env->NewObject(pppClass, ppp_constructor); + if(env->ExceptionCheck() || pppObject == NULL) + { + LOGE("Error creating PPP object"); + return NULL; // out of memory + } + + jobject addressObject = newInetSocketAddress(env, ppp.address); + if(env->ExceptionCheck() || addressObject == NULL) { + LOGE("Error creating InetSocketAddress object"); + return NULL; + } + + env->SetObjectField(pppObject, addressField, addressObject); + env->SetLongField(pppObject, lastSendField, ppp.lastSend); + env->SetLongField(pppObject, lastReceiveField, ppp.lastReceive); + env->SetBooleanField(pppObject, activeField, ppp.active); + env->SetBooleanField(pppObject, preferredField, ppp.preferred); + + if(env->ExceptionCheck()) { + LOGE("Exception assigning fields to PeerPhysicalPath object"); + } + + return pppObject; +} + +jobject newPeer(JNIEnv *env, const ZT_Peer &peer) +{ + LOGV("newPeer called"); + + jclass peerClass = NULL; + + jfieldID addressField = NULL; + jfieldID lastUnicastFrameField = NULL; + jfieldID lastMulticastFrameField = NULL; + jfieldID versionMajorField = NULL; + jfieldID versionMinorField = NULL; + jfieldID versionRevField = NULL; + jfieldID latencyField = NULL; + jfieldID roleField = NULL; + jfieldID pathsField = NULL; + + jmethodID peer_constructor = NULL; + + peerClass = lookup.findClass("com/zerotier/sdk/Peer"); + if(env->ExceptionCheck() || peerClass == NULL) + { + LOGE("Error finding Peer class"); + return NULL; + } + + addressField = lookup.findField(peerClass, "address", "J"); + if(env->ExceptionCheck() || addressField == NULL) + { + LOGE("Error finding address field of Peer object"); + return NULL; + } + + lastUnicastFrameField = lookup.findField(peerClass, "lastUnicastFrame", "J"); + if(env->ExceptionCheck() || lastUnicastFrameField == NULL) + { + LOGE("Error finding lastUnicastFrame field of Peer object"); + return NULL; + } + + lastMulticastFrameField = lookup.findField(peerClass, "lastMulticastFrame", "J"); + if(env->ExceptionCheck() || lastMulticastFrameField == NULL) + { + LOGE("Error finding lastMulticastFrame field of Peer object"); + return NULL; + } + + versionMajorField = lookup.findField(peerClass, "versionMajor", "I"); + if(env->ExceptionCheck() || versionMajorField == NULL) + { + LOGE("Error finding versionMajor field of Peer object"); + return NULL; + } + + versionMinorField = lookup.findField(peerClass, "versionMinor", "I"); + if(env->ExceptionCheck() || versionMinorField == NULL) + { + LOGE("Error finding versionMinor field of Peer object"); + return NULL; + } + + versionRevField = lookup.findField(peerClass, "versionRev", "I"); + if(env->ExceptionCheck() || versionRevField == NULL) + { + LOGE("Error finding versionRev field of Peer object"); + return NULL; + } + + latencyField = lookup.findField(peerClass, "latency", "I"); + if(env->ExceptionCheck() || latencyField == NULL) + { + LOGE("Error finding latency field of Peer object"); + return NULL; + } + + roleField = lookup.findField(peerClass, "role", "Lcom/zerotier/sdk/PeerRole;"); + if(env->ExceptionCheck() || roleField == NULL) + { + LOGE("Error finding role field of Peer object"); + return NULL; + } + + pathsField = lookup.findField(peerClass, "paths", "[Lcom/zerotier/sdk/PeerPhysicalPath;"); + if(env->ExceptionCheck() || pathsField == NULL) + { + LOGE("Error finding paths field of Peer object"); + return NULL; + } + + peer_constructor = lookup.findMethod(peerClass, "<init>", "()V"); + if(env->ExceptionCheck() || peer_constructor == NULL) + { + LOGE("Error finding Peer constructor"); + return NULL; + } + + jobject peerObject = env->NewObject(peerClass, peer_constructor); + if(env->ExceptionCheck() || peerObject == NULL) + { + LOGE("Error creating Peer object"); + return NULL; // out of memory + } + + env->SetLongField(peerObject, addressField, (jlong)peer.address); + env->SetLongField(peerObject, lastUnicastFrameField, (jlong)peer.lastUnicastFrame); + env->SetLongField(peerObject, lastMulticastFrameField, (jlong)peer.lastMulticastFrame); + env->SetIntField(peerObject, versionMajorField, peer.versionMajor); + env->SetIntField(peerObject, versionMinorField, peer.versionMinor); + env->SetIntField(peerObject, versionRevField, peer.versionRev); + env->SetIntField(peerObject, latencyField, peer.latency); + env->SetObjectField(peerObject, roleField, createPeerRole(env, peer.role)); + + jclass peerPhysicalPathClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath"); + if(env->ExceptionCheck() || peerPhysicalPathClass == NULL) + { + LOGE("Error finding PeerPhysicalPath class"); + return NULL; + } + + jobjectArray arrayObject = env->NewObjectArray( + peer.pathCount, peerPhysicalPathClass, NULL); + if(env->ExceptionCheck() || arrayObject == NULL) + { + LOGE("Error creating PeerPhysicalPath[] array"); + return NULL; + } + + for(unsigned int i = 0; i < peer.pathCount; ++i) + { + jobject path = newPeerPhysicalPath(env, peer.paths[i]); + + env->SetObjectArrayElement(arrayObject, i, path); + if(env->ExceptionCheck()) { + LOGE("exception assigning PeerPhysicalPath to array"); + break; + } + } + + env->SetObjectField(peerObject, pathsField, arrayObject); + + return peerObject; +} + +jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig) +{ + jclass vnetConfigClass = NULL; + jmethodID vnetConfig_constructor = NULL; + jfieldID nwidField = NULL; + jfieldID macField = NULL; + jfieldID nameField = NULL; + jfieldID statusField = NULL; + jfieldID typeField = NULL; + jfieldID mtuField = NULL; + jfieldID dhcpField = NULL; + jfieldID bridgeField = NULL; + jfieldID broadcastEnabledField = NULL; + jfieldID portErrorField = NULL; + jfieldID enabledField = NULL; + jfieldID netconfRevisionField = NULL; + jfieldID multicastSubscriptionsField = NULL; + jfieldID assignedAddressesField = NULL; + + vnetConfigClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfig"); + if(vnetConfigClass == NULL) + { + LOGE("Couldn't find com.zerotier.sdk.VirtualNetworkConfig"); + return NULL; + } + + vnetConfig_constructor = lookup.findMethod( + vnetConfigClass, "<init>", "()V"); + if(env->ExceptionCheck() || vnetConfig_constructor == NULL) + { + LOGE("Couldn't find VirtualNetworkConfig Constructor"); + return NULL; + } + + jobject vnetConfigObj = env->NewObject(vnetConfigClass, vnetConfig_constructor); + if(env->ExceptionCheck() || vnetConfigObj == NULL) + { + LOGE("Error creating new VirtualNetworkConfig object"); + return NULL; + } + + nwidField = lookup.findField(vnetConfigClass, "nwid", "J"); + if(env->ExceptionCheck() || nwidField == NULL) + { + LOGE("Error getting nwid field"); + return NULL; + } + + macField = lookup.findField(vnetConfigClass, "mac", "J"); + if(env->ExceptionCheck() || macField == NULL) + { + LOGE("Error getting mac field"); + return NULL; + } + + nameField = lookup.findField(vnetConfigClass, "name", "Ljava/lang/String;"); + if(env->ExceptionCheck() || nameField == NULL) + { + LOGE("Error getting name field"); + return NULL; + } + + statusField = lookup.findField(vnetConfigClass, "status", "Lcom/zerotier/sdk/VirtualNetworkStatus;"); + if(env->ExceptionCheck() || statusField == NULL) + { + LOGE("Error getting status field"); + return NULL; + } + + typeField = lookup.findField(vnetConfigClass, "type", "Lcom/zerotier/sdk/VirtualNetworkType;"); + if(env->ExceptionCheck() || typeField == NULL) + { + LOGE("Error getting type field"); + return NULL; + } + + mtuField = lookup.findField(vnetConfigClass, "mtu", "I"); + if(env->ExceptionCheck() || mtuField == NULL) + { + LOGE("Error getting mtu field"); + return NULL; + } + + dhcpField = lookup.findField(vnetConfigClass, "dhcp", "Z"); + if(env->ExceptionCheck() || dhcpField == NULL) + { + LOGE("Error getting dhcp field"); + return NULL; + } + + bridgeField = lookup.findField(vnetConfigClass, "bridge", "Z"); + if(env->ExceptionCheck() || bridgeField == NULL) + { + LOGE("Error getting bridge field"); + return NULL; + } + + broadcastEnabledField = lookup.findField(vnetConfigClass, "broadcastEnabled", "Z"); + if(env->ExceptionCheck() || broadcastEnabledField == NULL) + { + LOGE("Error getting broadcastEnabled field"); + return NULL; + } + + portErrorField = lookup.findField(vnetConfigClass, "portError", "I"); + if(env->ExceptionCheck() || portErrorField == NULL) + { + LOGE("Error getting portError field"); + return NULL; + } + + enabledField = lookup.findField(vnetConfigClass, "enabled", "Z"); + if(env->ExceptionCheck() || enabledField == NULL) + { + LOGE("Error getting enabled field"); + return NULL; + } + + netconfRevisionField = lookup.findField(vnetConfigClass, "netconfRevision", "J"); + if(env->ExceptionCheck() || netconfRevisionField == NULL) + { + LOGE("Error getting netconfRevision field"); + return NULL; + } + + multicastSubscriptionsField = lookup.findField(vnetConfigClass, "multicastSubscriptions", "[Lcom/zerotier/sdk/MulticastGroup;"); + if(env->ExceptionCheck() || multicastSubscriptionsField == NULL) + { + LOGE("Error getting multicastSubscriptions field"); + return NULL; + } + + assignedAddressesField = lookup.findField(vnetConfigClass, "assignedAddresses", "[Ljava/net/InetSocketAddress;"); + if(env->ExceptionCheck() || assignedAddressesField == NULL) + { + LOGE("Error getting assignedAddresses field"); + return NULL; + } + + env->SetLongField(vnetConfigObj, nwidField, vnetConfig.nwid); + env->SetLongField(vnetConfigObj, macField, vnetConfig.mac); + jstring nameStr = env->NewStringUTF(vnetConfig.name); + if(env->ExceptionCheck() || nameStr == NULL) + { + return NULL; // out of memory + } + env->SetObjectField(vnetConfigObj, nameField, nameStr); + + jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status); + if(env->ExceptionCheck() || statusObject == NULL) + { + return NULL; + } + env->SetObjectField(vnetConfigObj, statusField, statusObject); + + jobject typeObject = createVirtualNetworkType(env, vnetConfig.type); + if(env->ExceptionCheck() || typeObject == NULL) + { + return NULL; + } + env->SetObjectField(vnetConfigObj, typeField, typeObject); + + env->SetIntField(vnetConfigObj, mtuField, (int)vnetConfig.mtu); + env->SetBooleanField(vnetConfigObj, dhcpField, vnetConfig.dhcp); + env->SetBooleanField(vnetConfigObj, bridgeField, vnetConfig.bridge); + env->SetBooleanField(vnetConfigObj, broadcastEnabledField, vnetConfig.broadcastEnabled); + env->SetBooleanField(vnetConfigObj, enabledField, vnetConfig.enabled); + env->SetIntField(vnetConfigObj, portErrorField, vnetConfig.portError); + + jclass multicastGroupClass = lookup.findClass("com/zerotier/sdk/MulticastGroup"); + if(env->ExceptionCheck() || multicastGroupClass == NULL) + { + LOGE("Error finding MulticastGroup class"); + return NULL; + } + + jobjectArray mcastSubsArrayObj = env->NewObjectArray( + vnetConfig.multicastSubscriptionCount, multicastGroupClass, NULL); + if(env->ExceptionCheck() || mcastSubsArrayObj == NULL) { + LOGE("Error creating MulticastGroup[] array"); + return NULL; + } + + for(unsigned int i = 0; i < vnetConfig.multicastSubscriptionCount; ++i) + { + jobject mcastObj = newMulticastGroup(env, vnetConfig.multicastSubscriptions[i]); + env->SetObjectArrayElement(mcastSubsArrayObj, i, mcastObj); + if(env->ExceptionCheck()) + { + LOGE("Error assigning MulticastGroup to array"); + } + } + env->SetObjectField(vnetConfigObj, multicastSubscriptionsField, mcastSubsArrayObj); + + jclass inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress"); + if(env->ExceptionCheck() || inetSocketAddressClass == NULL) + { + LOGE("Error finding InetSocketAddress class"); + return NULL; + } + + jobjectArray assignedAddrArrayObj = env->NewObjectArray( + vnetConfig.assignedAddressCount, inetSocketAddressClass, NULL); + if(env->ExceptionCheck() || assignedAddrArrayObj == NULL) + { + LOGE("Error creating InetSocketAddress[] array"); + return NULL; + } + + for(unsigned int i = 0; i < vnetConfig.assignedAddressCount; ++i) + { + jobject inetAddrObj = newInetSocketAddress(env, vnetConfig.assignedAddresses[i]); + env->SetObjectArrayElement(assignedAddrArrayObj, i, inetAddrObj); + if(env->ExceptionCheck()) + { + LOGE("Error assigning InetSocketAddress to array"); + return NULL; + } + } + + env->SetObjectField(vnetConfigObj, assignedAddressesField, assignedAddrArrayObj); + + return vnetConfigObj; +} + +jobject newVersion(JNIEnv *env, int major, int minor, int rev, long featureFlags) +{ + // create a com.zerotier.sdk.Version object + jclass versionClass = NULL; + jmethodID versionConstructor = NULL; + + versionClass = lookup.findClass("com/zerotier/sdk/Version"); + if(env->ExceptionCheck() || versionClass == NULL) + { + return NULL; + } + + versionConstructor = lookup.findMethod( + versionClass, "<init>", "()V"); + if(env->ExceptionCheck() || versionConstructor == NULL) + { + return NULL; + } + + jobject versionObj = env->NewObject(versionClass, versionConstructor); + if(env->ExceptionCheck() || versionObj == NULL) + { + return NULL; + } + + // copy data to Version object + jfieldID majorField = NULL; + jfieldID minorField = NULL; + jfieldID revisionField = NULL; + jfieldID featureFlagsField = NULL; + + majorField = lookup.findField(versionClass, "major", "I"); + if(env->ExceptionCheck() || majorField == NULL) + { + return NULL; + } + + minorField = lookup.findField(versionClass, "minor", "I"); + if(env->ExceptionCheck() || minorField == NULL) + { + return NULL; + } + + revisionField = lookup.findField(versionClass, "revision", "I"); + if(env->ExceptionCheck() || revisionField == NULL) + { + return NULL; + } + + featureFlagsField = lookup.findField(versionClass, "featureFlags", "J"); + if(env->ExceptionCheck() || featureFlagsField == NULL) + { + return NULL; + } + + env->SetIntField(versionObj, majorField, (jint)major); + env->SetIntField(versionObj, minorField, (jint)minor); + env->SetIntField(versionObj, revisionField, (jint)rev); + env->SetLongField(versionObj, featureFlagsField, (jlong)featureFlags); + + return versionObj; +} + +#ifdef __cplusplus +} +#endif
\ No newline at end of file diff --git a/zerotierone/java/jni/ZT_jniutils.h b/zerotierone/java/jni/ZT_jniutils.h new file mode 100644 index 0000000..b76a28c --- /dev/null +++ b/zerotierone/java/jni/ZT_jniutils.h @@ -0,0 +1,49 @@ +#ifndef ZT_jniutils_h_ +#define ZT_jniutils_h_ +#include <stdio.h> +#include <jni.h> +#include <ZeroTierOne.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define LOG_TAG "ZeroTierOneJNI" + +#if __ANDROID__ +#include <android/log.h> +#define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) +#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) +#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) +#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) +#else +#define LOGV(...) fprintf(stdout, __VA_ARGS__) +#define LOGI(...) fprintf(stdout, __VA_ARGS__) +#define LOGD(...) fprintf(stdout, __VA_ARGS__) +#define LOGE(...) fprintf(stdout, __VA_ARGS__) +#endif + +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); + +jobject newMulticastGroup(JNIEnv *env, const ZT_MulticastGroup &mc); + +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, long featureFlags); + +#ifdef __cplusplus +} +#endif + +#endif
\ No newline at end of file diff --git a/zerotierone/java/jni/com_zerotierone_sdk_Node.cpp b/zerotierone/java/jni/com_zerotierone_sdk_Node.cpp new file mode 100644 index 0000000..dbabf80 --- /dev/null +++ b/zerotierone/java/jni/com_zerotierone_sdk_Node.cpp @@ -0,0 +1,1370 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#include "com_zerotierone_sdk_Node.h" +#include "ZT_jniutils.h" +#include "ZT_jnilookup.h" + +#include <ZeroTierOne.h> +#include "Mutex.hpp" + +#include <map> +#include <string> +#include <assert.h> +#include <string.h> + +// global static JNI Lookup Object +JniLookup lookup; + +#ifdef __cplusplus +extern "C" { +#endif + +namespace { + struct JniRef + { + JniRef() + : jvm(NULL) + , node(NULL) + , dataStoreGetListener(NULL) + , dataStorePutListener(NULL) + , packetSender(NULL) + , eventListener(NULL) + , frameListener(NULL) + , configListener(NULL) + {} + + ~JniRef() + { + JNIEnv *env = NULL; + jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + env->DeleteGlobalRef(dataStoreGetListener); + env->DeleteGlobalRef(dataStorePutListener); + env->DeleteGlobalRef(packetSender); + env->DeleteGlobalRef(eventListener); + env->DeleteGlobalRef(frameListener); + env->DeleteGlobalRef(configListener); + } + + uint64_t id; + + JavaVM *jvm; + + ZT_Node *node; + + jobject dataStoreGetListener; + jobject dataStorePutListener; + jobject packetSender; + jobject eventListener; + jobject frameListener; + jobject configListener; + }; + + + int VirtualNetworkConfigFunctionCallback( + ZT_Node *node, + void *userData, + uint64_t nwid, + void **, + enum ZT_VirtualNetworkConfigOperation operation, + const ZT_VirtualNetworkConfig *config) + { + LOGV("VritualNetworkConfigFunctionCallback"); + JniRef *ref = (JniRef*)userData; + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + jclass configListenerClass = env->GetObjectClass(ref->configListener); + if(configListenerClass == NULL) + { + LOGE("Couldn't find class for VirtualNetworkConfigListener instance"); + return -1; + } + + jmethodID configListenerCallbackMethod = lookup.findMethod(configListenerClass, + "onNetworkConfigurationUpdated", + "(JLcom/zerotier/sdk/VirtualNetworkConfigOperation;Lcom/zerotier/sdk/VirtualNetworkConfig;)I"); + if(configListenerCallbackMethod == NULL) + { + LOGE("Couldn't find onVirtualNetworkFrame() method"); + return -2; + } + + jobject operationObject = createVirtualNetworkConfigOperation(env, operation); + if(operationObject == NULL) + { + LOGE("Error creating VirtualNetworkConfigOperation object"); + return -3; + } + + jobject networkConfigObject = newNetworkConfig(env, *config); + if(networkConfigObject == NULL) + { + LOGE("Error creating VirtualNetworkConfig object"); + return -4; + } + + return env->CallIntMethod( + ref->configListener, + configListenerCallbackMethod, + (jlong)nwid, operationObject, networkConfigObject); + } + + void VirtualNetworkFrameFunctionCallback(ZT_Node *node, + void *userData, + uint64_t nwid, + void**, + uint64_t sourceMac, + uint64_t destMac, + unsigned int etherType, + unsigned int vlanid, + const void *frameData, + unsigned int frameLength) + { + LOGV("VirtualNetworkFrameFunctionCallback"); + unsigned char* local = (unsigned char*)frameData; + LOGV("Type Bytes: 0x%02x%02x", local[12], local[13]); + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + + jclass frameListenerClass = env->GetObjectClass(ref->frameListener); + if(env->ExceptionCheck() || frameListenerClass == NULL) + { + LOGE("Couldn't find class for VirtualNetworkFrameListener instance"); + return; + } + + jmethodID frameListenerCallbackMethod = lookup.findMethod( + frameListenerClass, + "onVirtualNetworkFrame", "(JJJJJ[B)V"); + if(env->ExceptionCheck() || frameListenerCallbackMethod == NULL) + { + LOGE("Couldn't find onVirtualNetworkFrame() method"); + return; + } + + jbyteArray dataArray = env->NewByteArray(frameLength); + if(env->ExceptionCheck() || dataArray == NULL) + { + LOGE("Couldn't create frame data array"); + return; + } + + void *data = env->GetPrimitiveArrayCritical(dataArray, NULL); + memcpy(data, frameData, frameLength); + env->ReleasePrimitiveArrayCritical(dataArray, data, 0); + + if(env->ExceptionCheck()) + { + LOGE("Error setting frame data to array"); + return; + } + + env->CallVoidMethod(ref->frameListener, frameListenerCallbackMethod, (jlong)nwid, (jlong)sourceMac, (jlong)destMac, (jlong)etherType, (jlong)vlanid, dataArray); + } + + + void EventCallback(ZT_Node *node, + void *userData, + enum ZT_Event event, + const void *data) + { + LOGV("EventCallback"); + JniRef *ref = (JniRef*)userData; + if(ref->node != node && event != ZT_EVENT_UP) + { + LOGE("Nodes not equal. ref->node %p, node %p. Event: %d", ref->node, node, event); + return; + } + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + + jclass eventListenerClass = env->GetObjectClass(ref->eventListener); + if(eventListenerClass == NULL) + { + LOGE("Couldn't class for EventListener instance"); + return; + } + + jmethodID onEventMethod = lookup.findMethod(eventListenerClass, + "onEvent", "(Lcom/zerotier/sdk/Event;)V"); + if(onEventMethod == NULL) + { + LOGE("Couldn't find onEvent method"); + return; + } + + jmethodID onTraceMethod = lookup.findMethod(eventListenerClass, + "onTrace", "(Ljava/lang/String;)V"); + if(onTraceMethod == NULL) + { + LOGE("Couldn't find onTrace method"); + return; + } + + jobject eventObject = createEvent(env, event); + if(eventObject == NULL) + { + return; + } + + switch(event) + { + case ZT_EVENT_UP: + { + LOGD("Event Up"); + env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject); + break; + } + case ZT_EVENT_OFFLINE: + { + LOGD("Event Offline"); + env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject); + break; + } + case ZT_EVENT_ONLINE: + { + LOGD("Event Online"); + env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject); + break; + } + case ZT_EVENT_DOWN: + { + LOGD("Event Down"); + env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject); + break; + } + case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + { + LOGV("Identity Collision"); + // call onEvent() + env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject); + } + break; + case ZT_EVENT_TRACE: + { + LOGV("Trace Event"); + // call onTrace() + if(data != NULL) + { + const char* message = (const char*)data; + jstring messageStr = env->NewStringUTF(message); + env->CallVoidMethod(ref->eventListener, onTraceMethod, messageStr); + } + } + break; + } + } + + long DataStoreGetFunction(ZT_Node *node, + void *userData, + const char *objectName, + void *buffer, + unsigned long bufferSize, + unsigned long bufferIndex, + unsigned long *out_objectSize) + { + JniRef *ref = (JniRef*)userData; + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + jclass dataStoreGetClass = env->GetObjectClass(ref->dataStoreGetListener); + if(dataStoreGetClass == NULL) + { + LOGE("Couldn't find class for DataStoreGetListener instance"); + return -2; + } + + jmethodID dataStoreGetCallbackMethod = lookup.findMethod( + dataStoreGetClass, + "onDataStoreGet", + "(Ljava/lang/String;[BJ[J)J"); + if(dataStoreGetCallbackMethod == NULL) + { + LOGE("Couldn't find onDataStoreGet method"); + return -2; + } + + jstring nameStr = env->NewStringUTF(objectName); + if(nameStr == NULL) + { + LOGE("Error creating name string object"); + return -2; // out of memory + } + + jbyteArray bufferObj = env->NewByteArray(bufferSize); + if(bufferObj == NULL) + { + LOGE("Error creating byte[] buffer of size: %lu", bufferSize); + return -2; + } + + jlongArray objectSizeObj = env->NewLongArray(1); + if(objectSizeObj == NULL) + { + LOGE("Error creating long[1] array for actual object size"); + return -2; // couldn't create long[1] array + } + + LOGV("Calling onDataStoreGet(%s, %p, %lu, %p)", + objectName, buffer, bufferIndex, objectSizeObj); + + long retval = (long)env->CallLongMethod( + ref->dataStoreGetListener, dataStoreGetCallbackMethod, + nameStr, bufferObj, (jlong)bufferIndex, objectSizeObj); + + if(retval > 0) + { + void *data = env->GetPrimitiveArrayCritical(bufferObj, NULL); + memcpy(buffer, data, retval); + env->ReleasePrimitiveArrayCritical(bufferObj, data, 0); + + jlong *objSize = (jlong*)env->GetPrimitiveArrayCritical(objectSizeObj, NULL); + *out_objectSize = (unsigned long)objSize[0]; + env->ReleasePrimitiveArrayCritical(objectSizeObj, objSize, 0); + } + + LOGV("Out Object Size: %lu", *out_objectSize); + + return retval; + } + + int DataStorePutFunction(ZT_Node *node, + void *userData, + const char *objectName, + const void *buffer, + unsigned long bufferSize, + int secure) + { + JniRef *ref = (JniRef*)userData; + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + + jclass dataStorePutClass = env->GetObjectClass(ref->dataStorePutListener); + if(dataStorePutClass == NULL) + { + LOGE("Couldn't find class for DataStorePutListener instance"); + return -1; + } + + jmethodID dataStorePutCallbackMethod = lookup.findMethod( + dataStorePutClass, + "onDataStorePut", + "(Ljava/lang/String;[BZ)I"); + if(dataStorePutCallbackMethod == NULL) + { + LOGE("Couldn't find onDataStorePut method"); + return -2; + } + + jmethodID deleteMethod = lookup.findMethod(dataStorePutClass, + "onDelete", "(Ljava/lang/String;)I"); + if(deleteMethod == NULL) + { + LOGE("Couldn't find onDelete method"); + return -3; + } + + jstring nameStr = env->NewStringUTF(objectName); + + if(buffer == NULL) + { + LOGD("JNI: Delete file: %s", objectName); + // delete operation + return env->CallIntMethod( + ref->dataStorePutListener, deleteMethod, nameStr); + } + else + { + LOGD("JNI: Write file: %s", objectName); + // set operation + jbyteArray bufferObj = env->NewByteArray(bufferSize); + if(env->ExceptionCheck() || bufferObj == NULL) + { + LOGE("Error creating byte array buffer!"); + return -4; + } + + env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer); + bool bsecure = secure != 0; + + return env->CallIntMethod(ref->dataStorePutListener, + dataStorePutCallbackMethod, + nameStr, bufferObj, bsecure); + } + } + + int WirePacketSendFunction(ZT_Node *node, + void *userData, + const struct sockaddr_storage *localAddress, + const struct sockaddr_storage *remoteAddress, + const void *buffer, + unsigned int bufferSize, + unsigned int ttl) + { + LOGV("WirePacketSendFunction(%p, %p, %p, %d)", localAddress, remoteAddress, buffer, bufferSize); + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = NULL; + ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6); + + + jclass packetSenderClass = env->GetObjectClass(ref->packetSender); + if(packetSenderClass == NULL) + { + LOGE("Couldn't find class for PacketSender instance"); + return -1; + } + + jmethodID packetSenderCallbackMethod = lookup.findMethod(packetSenderClass, + "onSendPacketRequested", "(Ljava/net/InetSocketAddress;Ljava/net/InetSocketAddress;[BI)I"); + if(packetSenderCallbackMethod == NULL) + { + LOGE("Couldn't find onSendPacketRequested method"); + return -2; + } + + jobject localAddressObj = NULL; + if(memcmp(localAddress, &ZT_SOCKADDR_NULL, sizeof(sockaddr_storage)) != 0) + { + localAddressObj = newInetSocketAddress(env, *localAddress); + } + + jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress); + jbyteArray bufferObj = env->NewByteArray(bufferSize); + env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer); + int retval = env->CallIntMethod(ref->packetSender, packetSenderCallbackMethod, localAddressObj, remoteAddressObj, bufferObj); + + LOGV("JNI Packet Sender returned: %d", retval); + return retval; + } + + typedef std::map<uint64_t, JniRef*> NodeMap; + static NodeMap nodeMap; + ZeroTier::Mutex nodeMapMutex; + + ZT_Node* findNode(uint64_t nodeId) + { + ZeroTier::Mutex::Lock lock(nodeMapMutex); + NodeMap::iterator found = nodeMap.find(nodeId); + if(found != nodeMap.end()) + { + JniRef *ref = found->second; + return ref->node; + } + return NULL; + } +} + +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) +{ + lookup.setJavaVM(vm); + return JNI_VERSION_1_6; +} + +JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) +{ + +} + + +/* + * Class: com_zerotier_sdk_Node + * Method: node_init + * Signature: (J)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init( + JNIEnv *env, jobject obj, jlong now) +{ + LOGV("Creating ZT_Node struct"); + jobject resultObject = createResultObject(env, ZT_RESULT_OK); + + ZT_Node *node; + JniRef *ref = new JniRef; + ref->id = (uint64_t)now; + env->GetJavaVM(&ref->jvm); + + jclass cls = env->GetObjectClass(obj); + jfieldID fid = lookup.findField( + cls, "getListener", "Lcom/zerotier/sdk/DataStoreGetListener;"); + + if(fid == NULL) + { + return NULL; // exception already thrown + } + + jobject tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->dataStoreGetListener = env->NewGlobalRef(tmp); + + fid = lookup.findField( + cls, "putListener", "Lcom/zerotier/sdk/DataStorePutListener;"); + + if(fid == NULL) + { + return NULL; // exception already thrown + } + + tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->dataStorePutListener = env->NewGlobalRef(tmp); + + fid = lookup.findField( + cls, "sender", "Lcom/zerotier/sdk/PacketSender;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->packetSender = env->NewGlobalRef(tmp); + + fid = lookup.findField( + cls, "frameListener", "Lcom/zerotier/sdk/VirtualNetworkFrameListener;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->frameListener = env->NewGlobalRef(tmp); + + fid = lookup.findField( + cls, "configListener", "Lcom/zerotier/sdk/VirtualNetworkConfigListener;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->configListener = env->NewGlobalRef(tmp); + + fid = lookup.findField( + cls, "eventListener", "Lcom/zerotier/sdk/EventListener;"); + if(fid == NULL) + { + return NULL; + } + + tmp = env->GetObjectField(obj, fid); + if(tmp == NULL) + { + return NULL; + } + ref->eventListener = env->NewGlobalRef(tmp); + + ZT_ResultCode rc = ZT_Node_new( + &node, + ref, + (uint64_t)now, + &DataStoreGetFunction, + &DataStorePutFunction, + &WirePacketSendFunction, + &VirtualNetworkFrameFunctionCallback, + &VirtualNetworkConfigFunctionCallback, + NULL, + &EventCallback); + + if(rc != ZT_RESULT_OK) + { + LOGE("Error creating Node: %d", rc); + resultObject = createResultObject(env, rc); + if(node) + { + ZT_Node_delete(node); + node = NULL; + } + delete ref; + ref = NULL; + return resultObject; + } + + ZeroTier::Mutex::Lock lock(nodeMapMutex); + ref->node = node; + nodeMap.insert(std::make_pair(ref->id, ref)); + + + return resultObject; +} + +/* + * Class: com_zerotier_sdk_Node + * Method: node_delete + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete( + JNIEnv *env, jobject obj, jlong id) +{ + LOGV("Destroying ZT_Node struct"); + uint64_t nodeId = (uint64_t)id; + + NodeMap::iterator found; + { + ZeroTier::Mutex::Lock lock(nodeMapMutex); + found = nodeMap.find(nodeId); + } + + if(found != nodeMap.end()) + { + JniRef *ref = found->second; + nodeMap.erase(found); + + ZT_Node_delete(ref->node); + + delete ref; + ref = NULL; + } + else + { + LOGE("Attempted to delete a node that doesn't exist!"); + } +} + +/* + * 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 *env, jobject obj, + jlong id, + jlong in_now, + jlong in_nwid, + jlong in_sourceMac, + jlong in_destMac, + jint in_etherType, + jint in_vlanId, + jbyteArray in_frameData, + jlongArray out_nextBackgroundTaskDeadline) +{ + uint64_t nodeId = (uint64_t) id; + + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline); + if(nbtd_len < 1) + { + // array for next background task length has 0 elements! + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t now = (uint64_t)in_now; + uint64_t nwid = (uint64_t)in_nwid; + uint64_t sourceMac = (uint64_t)in_sourceMac; + uint64_t destMac = (uint64_t)in_destMac; + unsigned int etherType = (unsigned int)in_etherType; + unsigned int vlanId = (unsigned int)in_vlanId; + + unsigned int frameLength = env->GetArrayLength(in_frameData); + void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL); + void *localData = malloc(frameLength); + memcpy(localData, frameData, frameLength); + env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0); + + uint64_t nextBackgroundTaskDeadline = 0; + + ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame( + node, + now, + nwid, + sourceMac, + destMac, + etherType, + vlanId, + (const void*)localData, + frameLength, + &nextBackgroundTaskDeadline); + + jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: processWirePacket + * Signature: (JJLjava/net/InetSocketAddress;I[B[J)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket( + JNIEnv *env, jobject obj, + jlong id, + jlong in_now, + jobject in_localAddress, + jobject in_remoteAddress, + jbyteArray in_packetData, + jlongArray out_nextBackgroundTaskDeadline) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + LOGE("Couldn't find a valid node!"); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline); + if(nbtd_len < 1) + { + LOGE("nbtd_len < 1"); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t now = (uint64_t)in_now; + + // get the java.net.InetSocketAddress class and getAddress() method + jclass inetAddressClass = lookup.findClass("java/net/InetAddress"); + if(inetAddressClass == NULL) + { + LOGE("Can't find InetAddress class"); + // can't find java.net.InetAddress + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + jmethodID getAddressMethod = lookup.findMethod( + inetAddressClass, "getAddress", "()[B"); + if(getAddressMethod == NULL) + { + // cant find InetAddress.getAddres() + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + jclass InetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress"); + if(InetSocketAddressClass == NULL) + { + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + jmethodID inetSockGetAddressMethod = lookup.findMethod( + InetSocketAddressClass, "getAddress", "()Ljava/net/InetAddress;"); + + jobject localAddrObj = NULL; + if(in_localAddress != NULL) + { + localAddrObj = env->CallObjectMethod(in_localAddress, inetSockGetAddressMethod); + } + + jobject remoteAddrObject = env->CallObjectMethod(in_remoteAddress, inetSockGetAddressMethod); + + if(remoteAddrObject == NULL) + { + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + jmethodID inetSock_getPort = lookup.findMethod( + InetSocketAddressClass, "getPort", "()I"); + + if(env->ExceptionCheck() || inetSock_getPort == NULL) + { + LOGE("Couldn't find getPort method on InetSocketAddress"); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + // call InetSocketAddress.getPort() + int remotePort = env->CallIntMethod(in_remoteAddress, inetSock_getPort); + if(env->ExceptionCheck()) + { + LOGE("Exception calling InetSocketAddress.getPort()"); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + // Call InetAddress.getAddress() + jbyteArray remoteAddressArray = (jbyteArray)env->CallObjectMethod(remoteAddrObject, getAddressMethod); + if(remoteAddressArray == NULL) + { + LOGE("Unable to call getAddress()"); + // unable to call getAddress() + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int addrSize = env->GetArrayLength(remoteAddressArray); + + + sockaddr_storage localAddress = {}; + + if(localAddrObj == NULL) + { + localAddress = ZT_SOCKADDR_NULL; + } + else + { + int localPort = env->CallIntMethod(in_localAddress, inetSock_getPort); + jbyteArray localAddressArray = (jbyteArray)env->CallObjectMethod(localAddrObj, getAddressMethod); + if(localAddressArray != NULL) + { + + unsigned int localAddrSize = env->GetArrayLength(localAddressArray); + jbyte *addr = (jbyte*)env->GetPrimitiveArrayCritical(localAddressArray, NULL); + + if(localAddrSize == 16) + { + sockaddr_in6 ipv6 = {}; + ipv6.sin6_family = AF_INET6; + ipv6.sin6_port = htons(localPort); + memcpy(ipv6.sin6_addr.s6_addr, addr, 16); + memcpy(&localAddress, &ipv6, sizeof(sockaddr_in6)); + } + else if(localAddrSize) + { + // IPV4 address + sockaddr_in ipv4 = {}; + ipv4.sin_family = AF_INET; + ipv4.sin_port = htons(localPort); + memcpy(&ipv4.sin_addr, addr, 4); + memcpy(&localAddress, &ipv4, sizeof(sockaddr_in)); + } + else + { + localAddress = ZT_SOCKADDR_NULL; + } + env->ReleasePrimitiveArrayCritical(localAddressArray, addr, 0); + } + } + + // get the address bytes + jbyte *addr = (jbyte*)env->GetPrimitiveArrayCritical(remoteAddressArray, NULL); + sockaddr_storage remoteAddress = {}; + + if(addrSize == 16) + { + // IPV6 address + sockaddr_in6 ipv6 = {}; + ipv6.sin6_family = AF_INET6; + ipv6.sin6_port = htons(remotePort); + memcpy(ipv6.sin6_addr.s6_addr, addr, 16); + memcpy(&remoteAddress, &ipv6, sizeof(sockaddr_in6)); + } + else if(addrSize == 4) + { + // IPV4 address + sockaddr_in ipv4 = {}; + ipv4.sin_family = AF_INET; + ipv4.sin_port = htons(remotePort); + memcpy(&ipv4.sin_addr, addr, 4); + memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in)); + } + else + { + LOGE("Unknown IP version"); + // unknown address type + env->ReleasePrimitiveArrayCritical(remoteAddressArray, addr, 0); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + env->ReleasePrimitiveArrayCritical(remoteAddressArray, addr, 0); + + unsigned int packetLength = env->GetArrayLength(in_packetData); + if(packetLength == 0) + { + LOGE("Empty packet?!?"); + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL); + void *localData = malloc(packetLength); + memcpy(localData, packetData, packetLength); + env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0); + + uint64_t nextBackgroundTaskDeadline = 0; + + ZT_ResultCode rc = ZT_Node_processWirePacket( + node, + now, + &localAddress, + &remoteAddress, + localData, + packetLength, + &nextBackgroundTaskDeadline); + if(rc != ZT_RESULT_OK) + { + LOGE("ZT_Node_processWirePacket returned: %d", rc); + } + + free(localData); + + jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: processBackgroundTasks + * Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks( + JNIEnv *env, jobject obj, + jlong id, + jlong in_now, + jlongArray out_nextBackgroundTaskDeadline) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline); + if(nbtd_len < 1) + { + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t now = (uint64_t)in_now; + uint64_t nextBackgroundTaskDeadline = 0; + + ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline); + + jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: join + * Signature: (JJ)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join( + JNIEnv *env, jobject obj, jlong id, jlong in_nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + + ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: leave + * Signature: (JJ)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave( + JNIEnv *env, jobject obj, jlong id, jlong in_nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + + ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: multicastSubscribe + * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe( + JNIEnv *env, jobject obj, + jlong id, + jlong in_nwid, + jlong in_multicastGroup, + jlong in_multicastAdi) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + uint64_t multicastGroup = (uint64_t)in_multicastGroup; + unsigned long multicastAdi = (unsigned long)in_multicastAdi; + + ZT_ResultCode rc = ZT_Node_multicastSubscribe( + node, nwid, multicastGroup, multicastAdi); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: multicastUnsubscribe + * Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe( + JNIEnv *env, jobject obj, + jlong id, + jlong in_nwid, + jlong in_multicastGroup, + jlong in_multicastAdi) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + uint64_t multicastGroup = (uint64_t)in_multicastGroup; + unsigned long multicastAdi = (unsigned long)in_multicastAdi; + + ZT_ResultCode rc = ZT_Node_multicastUnsubscribe( + node, nwid, multicastGroup, multicastAdi); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: address + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address( + JNIEnv *env , jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + uint64_t address = ZT_Node_address(node); + return (jlong)address; +} + +/* + * Class: com_zerotier_sdk_Node + * Method: status + * Signature: (J)Lcom/zerotier/sdk/NodeStatus; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status + (JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + jclass nodeStatusClass = NULL; + jmethodID nodeStatusConstructor = NULL; + + // create a com.zerotier.sdk.NodeStatus object + nodeStatusClass = lookup.findClass("com/zerotier/sdk/NodeStatus"); + if(nodeStatusClass == NULL) + { + return NULL; + } + + nodeStatusConstructor = lookup.findMethod( + nodeStatusClass, "<init>", "()V"); + if(nodeStatusConstructor == NULL) + { + return NULL; + } + + jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor); + if(nodeStatusObj == NULL) + { + return NULL; + } + + ZT_NodeStatus nodeStatus; + ZT_Node_status(node, &nodeStatus); + + jfieldID addressField = NULL; + jfieldID publicIdentityField = NULL; + jfieldID secretIdentityField = NULL; + jfieldID onlineField = NULL; + + addressField = lookup.findField(nodeStatusClass, "address", "J"); + if(addressField == NULL) + { + return NULL; + } + + publicIdentityField = lookup.findField(nodeStatusClass, "publicIdentity", "Ljava/lang/String;"); + if(publicIdentityField == NULL) + { + return NULL; + } + + secretIdentityField = lookup.findField(nodeStatusClass, "secretIdentity", "Ljava/lang/String;"); + if(secretIdentityField == NULL) + { + return NULL; + } + + onlineField = lookup.findField(nodeStatusClass, "online", "Z"); + if(onlineField == NULL) + { + return NULL; + } + + env->SetLongField(nodeStatusObj, addressField, nodeStatus.address); + + jstring pubIdentStr = env->NewStringUTF(nodeStatus.publicIdentity); + if(pubIdentStr == NULL) + { + return NULL; // out of memory + } + env->SetObjectField(nodeStatusObj, publicIdentityField, pubIdentStr); + + jstring secIdentStr = env->NewStringUTF(nodeStatus.secretIdentity); + if(secIdentStr == NULL) + { + return NULL; // out of memory + } + env->SetObjectField(nodeStatusObj, secretIdentityField, secIdentStr); + + env->SetBooleanField(nodeStatusObj, onlineField, nodeStatus.online); + + return nodeStatusObj; +} + +/* + * Class: com_zerotier_sdk_Node + * Method: networkConfig + * Signature: (J)Lcom/zerotier/sdk/VirtualNetworkConfig; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig( + JNIEnv *env, jobject obj, jlong id, jlong nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid); + + jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig); + + ZT_Node_freeQueryResult(node, vnetConfig); + + return vnetConfigObject; +} + +/* + * Class: com_zerotier_sdk_Node + * Method: version + * Signature: (J)Lcom/zerotier/sdk/Version; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version( + JNIEnv *env, jobject obj) +{ + int major = 0; + int minor = 0; + int revision = 0; + unsigned long featureFlags = 0; + + ZT_version(&major, &minor, &revision, &featureFlags); + + return newVersion(env, major, minor, revision, featureFlags); +} + +/* + * Class: com_zerotier_sdk_Node + * Method: peers + * Signature: (J)[Lcom/zerotier/sdk/Peer; + */ +JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers( + JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT_PeerList *peerList = ZT_Node_peers(node); + + if(peerList == NULL) + { + LOGE("ZT_Node_peers returned NULL"); + return NULL; + } + + int peerCount = peerList->peerCount * 100; + LOGV("Ensure Local Capacity: %d", peerCount); + if(env->EnsureLocalCapacity(peerCount)) + { + LOGE("EnsureLocalCapacity failed!!"); + ZT_Node_freeQueryResult(node, peerList); + return NULL; + } + + jclass peerClass = lookup.findClass("com/zerotier/sdk/Peer"); + if(env->ExceptionCheck() || peerClass == NULL) + { + LOGE("Error finding Peer class"); + ZT_Node_freeQueryResult(node, peerList); + return NULL; + } + + jobjectArray peerArrayObj = env->NewObjectArray( + peerList->peerCount, peerClass, NULL); + + if(env->ExceptionCheck() || peerArrayObj == NULL) + { + LOGE("Error creating Peer[] array"); + ZT_Node_freeQueryResult(node, peerList); + return NULL; + } + + + for(unsigned int i = 0; i < peerList->peerCount; ++i) + { + jobject peerObj = newPeer(env, peerList->peers[i]); + env->SetObjectArrayElement(peerArrayObj, i, peerObj); + if(env->ExceptionCheck()) + { + LOGE("Error assigning Peer object to array"); + break; + } + } + + ZT_Node_freeQueryResult(node, peerList); + peerList = NULL; + + return peerArrayObj; +} + +/* + * Class: com_zerotier_sdk_Node + * Method: networks + * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig; + */ +JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks( + JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT_VirtualNetworkList *networkList = ZT_Node_networks(node); + if(networkList == NULL) + { + return NULL; + } + + jclass vnetConfigClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfig"); + if(env->ExceptionCheck() || vnetConfigClass == NULL) + { + LOGE("Error finding VirtualNetworkConfig class"); + ZT_Node_freeQueryResult(node, networkList); + return NULL; + } + + jobjectArray networkListObject = env->NewObjectArray( + networkList->networkCount, vnetConfigClass, NULL); + if(env->ExceptionCheck() || networkListObject == NULL) + { + LOGE("Error creating VirtualNetworkConfig[] array"); + ZT_Node_freeQueryResult(node, networkList); + return NULL; + } + + for(unsigned int i = 0; i < networkList->networkCount; ++i) + { + jobject networkObject = newNetworkConfig(env, networkList->networks[i]); + env->SetObjectArrayElement(networkListObject, i, networkObject); + if(env->ExceptionCheck()) + { + LOGE("Error assigning VirtualNetworkConfig object to array"); + break; + } + } + + ZT_Node_freeQueryResult(node, networkList); + + return networkListObject; +} + +#ifdef __cplusplus +} // extern "C" +#endif
\ No newline at end of file diff --git a/zerotierone/java/jni/com_zerotierone_sdk_Node.h b/zerotierone/java/jni/com_zerotierone_sdk_Node.h new file mode 100644 index 0000000..7c1011a --- /dev/null +++ b/zerotierone/java/jni/com_zerotierone_sdk_Node.h @@ -0,0 +1,133 @@ +/* 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: (J)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init + (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: (JJLjava/net/InetSockAddress;Ljava/net/InetSockAddress;[B[J)Lcom/zerotier/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket + (JNIEnv *, jobject, jlong, jlong, jobject, 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: networks + * Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig; + */ +JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks + (JNIEnv *, jobject, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/zerotierone/java/src/com/zerotier/sdk/DataStoreGetListener.java b/zerotierone/java/src/com/zerotier/sdk/DataStoreGetListener.java new file mode 100644 index 0000000..b525be6 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/DataStoreGetListener.java @@ -0,0 +1,58 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +public interface DataStoreGetListener { + + /** + * Function to get an object from the data store + * + * <p>Object names can contain forward slash (/) path separators. They will + * never contain .. or backslash (\), so this is safe to map as a Unix-style + * path if the underlying storage permits. For security reasons we recommend + * returning errors if .. or \ are used.</p> + * + * <p>The function must return the actual number of bytes read. If the object + * doesn't exist, it should return -1. -2 should be returned on other errors + * such as errors accessing underlying storage.</p> + * + * <p>If the read doesn't fit in the buffer, the max number of bytes should be + * read. The caller may call the function multiple times to read the whole + * object.</p> + * + * @param name Name of the object in the data store + * @param out_buffer buffer to put the object in + * @param bufferIndex index in the object to start reading + * @param out_objectSize long[1] to be set to the actual size of the object if it exists. + * @return the actual number of bytes read. + */ + public long onDataStoreGet( + String name, + byte[] out_buffer, + long bufferIndex, + long[] out_objectSize); +} diff --git a/zerotierone/java/src/com/zerotier/sdk/DataStorePutListener.java b/zerotierone/java/src/com/zerotier/sdk/DataStorePutListener.java new file mode 100644 index 0000000..77e5502 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/DataStorePutListener.java @@ -0,0 +1,59 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +public interface DataStorePutListener { + + /** + * Function to store an object in the data store + * + * <p>If secure is true, the file should be set readable and writable only + * to the user running ZeroTier One. What this means is platform-specific.</p> + * + * <p>Name semantics are the same as {@link DataStoreGetListener}. This must return + * zero on success. You can return any OS-specific error code on failure, as these + * may be visible in logs or error messages and might aid in debugging.</p> + * + * @param name Object name + * @param buffer data to store + * @param secure set to user read/write only. + * @return 0 on success. + */ + public int onDataStorePut( + String name, + byte[] buffer, + boolean secure); + + /** + * Function to delete an object from the data store + * + * @param name Object name + * @return 0 on success. + */ + public int onDelete( + String name); +} diff --git a/zerotierone/java/src/com/zerotier/sdk/Event.java b/zerotierone/java/src/com/zerotier/sdk/Event.java new file mode 100644 index 0000000..22d350e --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/Event.java @@ -0,0 +1,98 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +public enum Event { + /** + * Node has been initialized + * + * This is the first event generated, and is always sent. It may occur + * before Node's constructor returns. + */ + EVENT_UP, + + /** + * Node is offline -- network does not seem to be reachable by any available strategy + */ + EVENT_OFFLINE, + + /** + * Node is online -- at least one upstream node appears reachable + * + * Meta-data: none + */ + EVENT_ONLINE, + + /** + * Node is shutting down + * + * <p>This is generated within Node's destructor when it is being shut down. + * It's done for convenience, since cleaning up other state in the event + * handler may appear more idiomatic.</p> + */ + EVENT_DOWN, + + /** + * Your identity has collided with another node's ZeroTier address + * + * <p>This happens if two different public keys both hash (via the algorithm + * in Identity::generate()) to the same 40-bit ZeroTier address.</p> + * + * <p>This is something you should "never" see, where "never" is defined as + * once per 2^39 new node initializations / identity creations. If you do + * see it, you're going to see it very soon after a node is first + * initialized.</p> + * + * <p>This is reported as an event rather than a return code since it's + * detected asynchronously via error messages from authoritative nodes.</p> + * + * <p>If this occurs, you must shut down and delete the node, delete the + * identity.secret record/file from the data store, and restart to generate + * a new identity. If you don't do this, you will not be able to communicate + * with other nodes.</p> + * + * <p>We'd automate this process, but we don't think silently deleting + * private keys or changing our address without telling the calling code + * is good form. It violates the principle of least surprise.</p> + * + * <p>You can technically get away with not handling this, but we recommend + * doing so in a mature reliable application. Besides, handling this + * condition is a good way to make sure it never arises. It's like how + * umbrellas prevent rain and smoke detectors prevent fires. They do, right?</p> + */ + EVENT_FATAL_ERROR_IDENTITY_COLLISION, + + /** + * Trace (debugging) message + * + * <p>These events are only generated if this is a TRACE-enabled build.</p> + * + * <p>Meta-data: {@link String}, TRACE message</p> + */ + EVENT_TRACE +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/EventListener.java b/zerotierone/java/src/com/zerotier/sdk/EventListener.java new file mode 100644 index 0000000..91050aa --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/EventListener.java @@ -0,0 +1,52 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.net.InetSocketAddress; +import java.lang.String; + +/** + * Interface to handle callbacks for ZeroTier One events. + */ +public interface EventListener { + /** + * Callback for events with no other associated metadata + * + * @param event {@link Event} enum + */ + public void onEvent(Event event); + + /** + * Trace messages + * + * <p>These events are only generated if the underlying ZeroTierOne SDK is a TRACE-enabled build.</p> + * + * @param message the trace message + */ + public void onTrace(String message); +} diff --git a/zerotierone/java/src/com/zerotier/sdk/MulticastGroup.java b/zerotierone/java/src/com/zerotier/sdk/MulticastGroup.java new file mode 100644 index 0000000..6811442 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/MulticastGroup.java @@ -0,0 +1,53 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + + +public final class MulticastGroup { + private MulticastGroup() {} + + private long mac; + private long adi; + + public boolean equals(MulticastGroup other) { + return mac == other.mac && adi == other.adi; + } + + /** + * MAC address (least significant 48 bits) + */ + public final long getMacAddress() { + return mac; + } + + /** + * Additional distinguishing information (usually zero) + */ + public final long getAdi() { + return adi; + } +} diff --git a/zerotierone/java/src/com/zerotier/sdk/NativeUtils.java b/zerotierone/java/src/com/zerotier/sdk/NativeUtils.java new file mode 100644 index 0000000..07e1ef5 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/NativeUtils.java @@ -0,0 +1,93 @@ +package com.zerotier.sdk; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Simple library class for working with JNI (Java Native Interface) + * + * @see http://adamheinrich.com/2012/how-to-load-native-jni-library-from-jar + * + * @author Adam Heirnich <[email protected]>, http://www.adamh.cz + */ +public class NativeUtils { + + /** + * Private constructor - this class will never be instanced + */ + private NativeUtils() { + } + + /** + * Loads library from current JAR archive + * + * The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after exiting. + * Method uses String as filename because the pathname is "abstract", not system-dependent. + * + * @param filename The filename inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext + * @throws IOException If temporary file creation or read/write operation fails + * @throws IllegalArgumentException If source file (param path) does not exist + * @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of {@see File#createTempFile(java.lang.String, java.lang.String)}). + */ + public static void loadLibraryFromJar(String path) throws IOException { + + if (!path.startsWith("/")) { + throw new IllegalArgumentException("The path has to be absolute (start with '/')."); + } + + // Obtain filename from path + String[] parts = path.split("/"); + String filename = (parts.length > 1) ? parts[parts.length - 1] : null; + + // Split filename to prexif and suffix (extension) + String prefix = ""; + String suffix = null; + if (filename != null) { + parts = filename.split("\\.", 2); + prefix = parts[0]; + suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-) + } + + // Check if the filename is okay + if (filename == null || prefix.length() < 3) { + throw new IllegalArgumentException("The filename has to be at least 3 characters long."); + } + + // Prepare temporary file + File temp = File.createTempFile(prefix, suffix); + temp.deleteOnExit(); + + if (!temp.exists()) { + throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist."); + } + + // Prepare buffer for data copying + byte[] buffer = new byte[1024]; + int readBytes; + + // Open and check input stream + InputStream is = NativeUtils.class.getResourceAsStream(path); + if (is == null) { + throw new FileNotFoundException("File " + path + " was not found inside JAR."); + } + + // Open output stream and copy data between source file in JAR and the temporary file + OutputStream os = new FileOutputStream(temp); + try { + while ((readBytes = is.read(buffer)) != -1) { + os.write(buffer, 0, readBytes); + } + } finally { + // If read/write fails, close streams safely before throwing an exception + os.close(); + is.close(); + } + + // Finally, load the library + System.load(temp.getAbsolutePath()); + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/Node.java b/zerotierone/java/src/com/zerotier/sdk/Node.java new file mode 100644 index 0000000..4bc6e18 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/Node.java @@ -0,0 +1,434 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.io.IOException; + +/** + * A ZeroTier One node + */ +public class Node { + static { + try { + System.loadLibrary("ZeroTierOneJNI"); + } catch (UnsatisfiedLinkError e) { + try { + if(System.getProperty("os.name").startsWith("Windows")) { + System.out.println("Arch: " + System.getProperty("sun.arch.data.model")); + if(System.getProperty("sun.arch.data.model").equals("64")) { + NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win64.dll"); + } else { + NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win32.dll"); + } + } else if(System.getProperty("os.name").startsWith("Mac")) { + NativeUtils.loadLibraryFromJar("/lib/libZeroTierOneJNI.jnilib"); + } else { + // TODO: Linux + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + } + + private static final String TAG = "NODE"; + + /** + * Node ID for JNI purposes. + * Currently set to the now value passed in at the constructor + * + * -1 if the node has already been closed + */ + private long nodeId; + + private final DataStoreGetListener getListener; + private final DataStorePutListener putListener; + private final PacketSender sender; + private final EventListener eventListener; + private final VirtualNetworkFrameListener frameListener; + private final VirtualNetworkConfigListener configListener; + + /** + * Create a new ZeroTier One node + * + * <p>Note that this can take a few seconds the first time it's called, as it + * will generate an identity.</p> + * + * @param now Current clock in milliseconds + * @param getListener User written instance of the {@link DataStoreGetListener} interface called to get objects from persistent storage. This instance must be unique per Node object. + * @param putListener User written intstance of the {@link DataStorePutListener} interface called to put objects in persistent storage. This instance must be unique per Node object. + * @param sender + * @param eventListener User written instance of the {@link EventListener} interface to receive status updates and non-fatal error notices. This instance must be unique per Node object. + * @param frameListener + * @param configListener User written instance of the {@link VirtualNetworkConfigListener} interface to be called when virtual LANs are created, deleted, or their config parameters change. This instance must be unique per Node object. + */ + public Node(long now, + DataStoreGetListener getListener, + DataStorePutListener putListener, + PacketSender sender, + EventListener eventListener, + VirtualNetworkFrameListener frameListener, + VirtualNetworkConfigListener configListener) throws NodeException + { + this.nodeId = now; + + this.getListener = getListener; + this.putListener = putListener; + this.sender = sender; + this.eventListener = eventListener; + this.frameListener = frameListener; + this.configListener = configListener; + + ResultCode rc = node_init(now); + if(rc != ResultCode.RESULT_OK) + { + // TODO: Throw Exception + throw new NodeException(rc.toString()); + } + } + + /** + * Close this Node. + * + * <p>The Node object can no longer be used once this method is called.</p> + */ + public void close() { + if(nodeId != -1) { + node_delete(nodeId); + nodeId = -1; + } + } + + @Override + protected void finalize() { + close(); + } + + /** + * Process a frame from a virtual network port + * + * @param now Current clock in milliseconds + * @param nwid ZeroTier 64-bit virtual network ID + * @param sourceMac Source MAC address (least significant 48 bits) + * @param destMac Destination MAC address (least significant 48 bits) + * @param etherType 16-bit Ethernet frame type + * @param vlanId 10-bit VLAN ID or 0 if none + * @param frameData Frame payload data + * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks() + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode processVirtualNetworkFrame( + long now, + long nwid, + long sourceMac, + long destMac, + int etherType, + int vlanId, + byte[] frameData, + long[] nextBackgroundTaskDeadline) { + return processVirtualNetworkFrame( + nodeId, now, nwid, sourceMac, destMac, etherType, vlanId, + frameData, nextBackgroundTaskDeadline); + } + + /** + * Process a packet received from the physical wire + * + * @param now Current clock in milliseconds + * @param remoteAddress Origin of packet + * @param packetData Packet data + * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks() + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode processWirePacket( + long now, + InetSocketAddress localAddress, + InetSocketAddress remoteAddress, + byte[] packetData, + long[] nextBackgroundTaskDeadline) { + return processWirePacket( + nodeId, now, localAddress, remoteAddress, packetData, + nextBackgroundTaskDeadline); + } + + /** + * Perform periodic background operations + * + * @param now Current clock in milliseconds + * @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks() + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode processBackgroundTasks(long now, long[] nextBackgroundTaskDeadline) { + return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline); + } + + /** + * Join a network + * + * <p>This may generate calls to the port config callback before it returns, + * or these may be deffered if a netconf is not available yet.</p> + * + * <p>If we are already a member of the network, nothing is done and OK is + * returned.</p> + * + * @param nwid 64-bit ZeroTier network ID + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode join(long nwid) { + return join(nodeId, nwid); + } + + /** + * Leave a network + * + * <p>If a port has been configured for this network this will generate a call + * to the port config callback with a NULL second parameter to indicate that + * the port is now deleted.</p> + * + * @param nwid 64-bit network ID + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode leave(long nwid) { + return leave(nodeId, nwid); + } + + /** + * Subscribe to an Ethernet multicast group + * + * <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the + * broadcast address) but with an ADI equal to each IPv4 address in host + * byte order. This converts ARP from a non-scalable broadcast protocol to + * a scalable multicast protocol with perfect address specificity.</p> + * + * <p>If this is not done, ARP will not work reliably.</p> + * + * <p>Multiple calls to subscribe to the same multicast address will have no + * effect. It is perfectly safe to do this.</p> + * + * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p> + * + * @param nwid 64-bit network ID + * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits) + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode multicastSubscribe( + long nwid, + long multicastGroup) { + return multicastSubscribe(nodeId, nwid, multicastGroup, 0); + } + + /** + * Subscribe to an Ethernet multicast group + * + * <p>ADI stands for additional distinguishing information. This defaults to zero + * and is rarely used. Right now its only use is to enable IPv4 ARP to scale, + * and this must be done.</p> + * + * <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the + * broadcast address) but with an ADI equal to each IPv4 address in host + * byte order. This converts ARP from a non-scalable broadcast protocol to + * a scalable multicast protocol with perfect address specificity.</p> + * + * <p>If this is not done, ARP will not work reliably.</p> + * + * <p>Multiple calls to subscribe to the same multicast address will have no + * effect. It is perfectly safe to do this.</p> + * + * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p> + * + * @param nwid 64-bit network ID + * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits) + * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0) + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode multicastSubscribe( + long nwid, + long multicastGroup, + long multicastAdi) { + return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi); + } + + + /** + * Unsubscribe from an Ethernet multicast group (or all groups) + * + * <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If + * you are not subscribed to a group this has no effect.</p> + * + * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p> + * + * @param nwid 64-bit network ID + * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits) + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode multicastUnsubscribe( + long nwid, + long multicastGroup) { + return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0); + } + + /** + * Unsubscribe from an Ethernet multicast group (or all groups) + * + * <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If + * you are not subscribed to a group this has no effect.</p> + * + * <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p> + * + * <p>ADI stands for additional distinguishing information. This defaults to zero + * and is rarely used. Right now its only use is to enable IPv4 ARP to scale, + * and this must be done.</p> + * + * @param nwid 64-bit network ID + * @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits) + * @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0) + * @return OK (0) or error code if a fatal error condition has occurred + */ + public ResultCode multicastUnsubscribe( + long nwid, + long multicastGroup, + long multicastAdi) { + return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi); + } + + /** + * Get this node's 40-bit ZeroTier address + * + * @return ZeroTier address (least significant 40 bits of 64-bit int) + */ + public long address() { + return address(nodeId); + } + + /** + * Get the status of this node + * + * @return @{link NodeStatus} struct with the current node status. + */ + public NodeStatus status() { + return status(nodeId); + } + + /** + * Get a list of known peer nodes + * + * @return List of known peers or NULL on failure + */ + public Peer[] peers() { + return peers(nodeId); + } + + /** + * Get the status of a virtual network + * + * @param nwid 64-bit network ID + * @return {@link VirtualNetworkConfig} or NULL if we are not a member of this network + */ + public VirtualNetworkConfig networkConfig(long nwid) { + return networkConfig(nodeId, nwid); + } + + /** + * Enumerate and get status of all networks + * + * @return List of networks or NULL on failure + */ + public VirtualNetworkConfig[] networks() { + return networks(nodeId); + } + + /** + * Get ZeroTier One version + * + * @return {@link Version} object with ZeroTierOne version information. + */ + public Version getVersion() { + return version(); + } + + // + // function declarations for JNI + // + private native ResultCode node_init(long now); + + private native void node_delete(long nodeId); + + private native ResultCode processVirtualNetworkFrame( + long nodeId, + long now, + long nwid, + long sourceMac, + long destMac, + int etherType, + int vlanId, + byte[] frameData, + long[] nextBackgroundTaskDeadline); + + private native ResultCode processWirePacket( + long nodeId, + long now, + InetSocketAddress localAddress, + InetSocketAddress remoteAddress, + byte[] packetData, + long[] nextBackgroundTaskDeadline); + + private native ResultCode processBackgroundTasks( + long nodeId, + long now, + long[] nextBackgroundTaskDeadline); + + private native ResultCode join(long nodeId, long nwid); + + private native ResultCode leave(long nodeId, long nwid); + + private native ResultCode multicastSubscribe( + long nodeId, + long nwid, + long multicastGroup, + long multicastAdi); + + private native ResultCode multicastUnsubscribe( + long nodeId, + long nwid, + long multicastGroup, + long multicastAdi); + + private native long address(long nodeId); + + private native NodeStatus status(long nodeId); + + private native VirtualNetworkConfig networkConfig(long nodeId, long nwid); + + private native Version version(); + + private native Peer[] peers(long nodeId); + + private native VirtualNetworkConfig[] networks(long nodeId); +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/NodeException.java b/zerotierone/java/src/com/zerotier/sdk/NodeException.java new file mode 100644 index 0000000..1fdef72 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/NodeException.java @@ -0,0 +1,36 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.lang.RuntimeException; + +public class NodeException extends RuntimeException { + public NodeException(String message) { + super(message); + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/NodeStatus.java b/zerotierone/java/src/com/zerotier/sdk/NodeStatus.java new file mode 100644 index 0000000..94376d8 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/NodeStatus.java @@ -0,0 +1,69 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +public final class NodeStatus { + private long address; + private String publicIdentity; + private String secretIdentity; + private boolean online; + + private NodeStatus() {} + + /** + * 40-bit ZeroTier address of this node + */ + public final long getAddres() { + return address; + } + + /** + * Public identity in string-serialized form (safe to send to others) + * + * <p>This identity will remain valid as long as the node exists.</p> + */ + public final String getPublicIdentity() { + return publicIdentity; + } + + /** + * Full identity including secret key in string-serialized form + * + * <p>This identity will remain valid as long as the node exists.</p> + */ + public final String getSecretIdentity() { + return secretIdentity; + } + + /** + * True if some kind of connectivity appears available + */ + public final boolean isOnline() { + return online; + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/PacketSender.java b/zerotierone/java/src/com/zerotier/sdk/PacketSender.java new file mode 100644 index 0000000..22893ec --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/PacketSender.java @@ -0,0 +1,50 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +import java.net.InetSocketAddress; + + +public interface PacketSender { + /** + * Function to send a ZeroTier packet out over the wire + * + * <p>The function must return zero on success and may return any error code + * on failure. Note that success does not (of course) guarantee packet + * delivery. It only means that the packet appears to have been sent.</p> + * + * @param localAddr {@link InetSocketAddress} to send from. Set to null if not specified. + * @param remoteAddr {@link InetSocketAddress} to send to + * @param packetData data to send + * @return 0 on success, any error code on failure. + */ + public int onSendPacketRequested( + InetSocketAddress localAddr, + InetSocketAddress remoteAddr, + byte[] packetData, + int ttl); +} diff --git a/zerotierone/java/src/com/zerotier/sdk/Peer.java b/zerotierone/java/src/com/zerotier/sdk/Peer.java new file mode 100644 index 0000000..fb2d106 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/Peer.java @@ -0,0 +1,110 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.util.ArrayList; + +/** + * Peer status result + */ +public final class Peer { + private long address; + private long lastUnicastFrame; + private long lastMulticastFrame; + private int versionMajor; + private int versionMinor; + private int versionRev; + private int latency; + private PeerRole role; + private PeerPhysicalPath[] paths; + + private Peer() {} + + /** + * ZeroTier address (40 bits) + */ + public final long address() { + return address; + } + + /** + * Time we last received a unicast frame from this peer + */ + public final long lastUnicastFrame() { + return lastUnicastFrame; + } + + /** + * Time we last received a multicast rame from this peer + */ + public final long lastMulticastFrame() { + return lastMulticastFrame; + } + + /** + * Remote major version or -1 if not known + */ + public final int versionMajor() { + return versionMajor; + } + + /** + * Remote minor version or -1 if not known + */ + public final int versionMinor() { + return versionMinor; + } + + /** + * Remote revision or -1 if not known + */ + public final int versionRev() { + return versionRev; + } + + /** + * Last measured latency in milliseconds or zero if unknown + */ + public final int latency() { + return latency; + } + + /** + * What trust hierarchy role does this device have? + */ + public final PeerRole role() { + return role; + } + + /** + * Known network paths to peer + */ + public final PeerPhysicalPath[] paths() { + return paths; + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/PeerPhysicalPath.java b/zerotierone/java/src/com/zerotier/sdk/PeerPhysicalPath.java new file mode 100644 index 0000000..d64ea56 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/PeerPhysicalPath.java @@ -0,0 +1,86 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.net.InetSocketAddress; + +/** + * Physical network path to a peer + */ +public final class PeerPhysicalPath { + private InetSocketAddress address; + private long lastSend; + private long lastReceive; + private boolean fixed; + private boolean active; + private boolean preferred; + + private PeerPhysicalPath() {} + + /** + * Address of endpoint + */ + public final InetSocketAddress address() { + return address; + } + + /** + * Time of last send in milliseconds or 0 for never + */ + public final long lastSend() { + return lastSend; + } + + /** + * Time of last receive in milliseconds or 0 for never + */ + public final long lastReceive() { + return lastReceive; + } + + /** + * Is path fixed? (i.e. not learned, static) + */ + public final boolean isFixed() { + return fixed; + } + + /** + * Is path active? + */ + public final boolean isActive() { + return active; + } + + /** + * Is path preferred? + */ + public final boolean isPreferred() { + return preferred; + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/PeerRole.java b/zerotierone/java/src/com/zerotier/sdk/PeerRole.java new file mode 100644 index 0000000..d7d55f0 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/PeerRole.java @@ -0,0 +1,45 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +public enum PeerRole { + /** + * An ordinary node + */ + PEER_ROLE_LEAF, + + /** + * relay node + */ + PEER_ROLE_RELAY, + + /** + * root server + */ + PEER_ROLE_ROOT +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/ResultCode.java b/zerotierone/java/src/com/zerotier/sdk/ResultCode.java new file mode 100644 index 0000000..5da82b3 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/ResultCode.java @@ -0,0 +1,74 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +/** + * Function return code: OK (0) or error results + * + * <p>Use {@link ResultCode#isFatal) to check for a fatal error. If a fatal error + * occurs, the node should be considered to not be working correctly. These + * indicate serious problems like an inaccessible data store or a compile + * problem.</p> + */ +public enum ResultCode { + /** + * Operation completed normally + */ + RESULT_OK(0), + + // Fatal errors (> 0, < 1000) + /** + * Ran out of memory + */ + RESULT_FATAL_ERROR_OUT_OF_MEMORY(1), + + /** + * Data store is not writable or has failed + */ + RESULT_FATAL_ERROR_DATA_STORE_FAILED(2), + + /** + * Internal error (e.g. unexpected exception indicating bug or build problem) + */ + RESULT_FATAL_ERROR_INTERNAL(3), + + // non-fatal errors + + /** + * Network ID not valid + */ + RESULT_ERROR_NETWORK_NOT_FOUND(1000); + + private final int id; + ResultCode(int id) { this.id = id; } + public int getValue() { return id; } + + public boolean isFatal(int id) { + return (id > 0 && id < 1000); + } +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/Version.java b/zerotierone/java/src/com/zerotier/sdk/Version.java new file mode 100644 index 0000000..d7fa0ce --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/Version.java @@ -0,0 +1,37 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +public final class Version { + private Version() {} + + public int major = 0; + public int minor = 0; + public int revision = 0; + public long featureFlags = 0; +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfig.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfig.java new file mode 100644 index 0000000..9816180 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfig.java @@ -0,0 +1,206 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +import java.lang.Comparable; +import java.lang.Override; +import java.lang.String; +import java.util.ArrayList; +import java.net.InetSocketAddress; + +public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConfig> { + public static final int MAX_MULTICAST_SUBSCRIPTIONS = 4096; + public static final int ZT_MAX_ZT_ASSIGNED_ADDRESSES = 16; + + private long nwid; + private long mac; + private String name; + private VirtualNetworkStatus status; + private VirtualNetworkType type; + private int mtu; + private boolean dhcp; + private boolean bridge; + private boolean broadcastEnabled; + private int portError; + private boolean enabled; + private long netconfRevision; + private MulticastGroup[] multicastSubscriptions; + private InetSocketAddress[] assignedAddresses; + + private VirtualNetworkConfig() { + + } + + public boolean equals(VirtualNetworkConfig cfg) { + boolean aaEqual = true; + if(assignedAddresses.length == cfg.assignedAddresses.length) { + for(int i = 0; i < assignedAddresses.length; ++i) { + if(!assignedAddresses[i].equals(cfg.assignedAddresses[i])) { + return false; + } + } + } else { + aaEqual = false; + } + + return nwid == cfg.nwid && + mac == cfg.mac && + name.equals(cfg.name) && + status.equals(cfg.status) && + type.equals(cfg.type) && + mtu == cfg.mtu && + dhcp == cfg.dhcp && + bridge == cfg.bridge && + broadcastEnabled == cfg.broadcastEnabled && + portError == cfg.portError && + enabled == cfg.enabled && + aaEqual; + } + + public int compareTo(VirtualNetworkConfig cfg) { + if(cfg.nwid == this.nwid) { + return 0; + } else { + return this.nwid > cfg.nwid ? 1 : -1; + } + } + + /** + * 64-bit ZeroTier network ID + */ + public final long networkId() { + return nwid; + } + + /** + * Ethernet MAC (40 bits) that should be assigned to port + */ + public final long macAddress() { + return mac; + } + + /** + * Network name (from network configuration master) + */ + public final String name() { + return name; + } + + /** + * Network configuration request status + */ + public final VirtualNetworkStatus networkStatus() { + return status; + } + + /** + * Network type + */ + public final VirtualNetworkType networkType() { + return type; + } + + /** + * Maximum interface MTU + */ + public final int mtu() { + return mtu; + } + + /** + * If the network this port belongs to indicates DHCP availability + * + * <p>This is a suggestion. The underlying implementation is free to ignore it + * for security or other reasons. This is simply a netconf parameter that + * means 'DHCP is available on this network.'</p> + */ + public final boolean isDhcpAvailable() { + return dhcp; + } + + /** + * If this port is allowed to bridge to other networks + * + * <p>This is informational. If this is false, bridged packets will simply + * be dropped and bridging won't work.</p> + */ + public final boolean isBridgeEnabled() { + return bridge; + } + + /** + * If true, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic + */ + public final boolean broadcastEnabled() { + return broadcastEnabled; + } + + /** + * If the network is in PORT_ERROR state, this is the error most recently returned by the port config callback + */ + public final int portError() { + return portError; + } + + /** + * Is this network enabled? If not, all frames to/from are dropped. + */ + public final boolean isEnabled() { + return enabled; + } + + /** + * Network config revision as reported by netconf master + * + * <p>If this is zero, it means we're still waiting for our netconf.</p> + */ + public final long netconfRevision() { + return netconfRevision; + } + + /** + * Multicast group subscriptions + */ + public final MulticastGroup[] multicastSubscriptions() { + return multicastSubscriptions; + } + + /** + * ZeroTier-assigned addresses (in {@link java.net.InetSocketAddress} objects) + * + * For IP, the port number of the sockaddr_XX structure contains the number + * of bits in the address netmask. Only the IP address and port are used. + * Other fields like interface number can be ignored. + * + * This is only used for ZeroTier-managed address assignments sent by the + * virtual network's configuration master. + */ + public final InetSocketAddress[] assignedAddresses() { + return assignedAddresses; + } +} diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigListener.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigListener.java new file mode 100644 index 0000000..15ae301 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigListener.java @@ -0,0 +1,60 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + + +package com.zerotier.sdk; + + +public interface VirtualNetworkConfigListener { + /** + * Callback called to update virtual network port configuration + * + * <p>This can be called at any time to update the configuration of a virtual + * network port. The parameter after the network ID specifies whether this + * port is being brought up, updated, brought down, or permanently deleted. + * + * This in turn should be used by the underlying implementation to create + * and configure tap devices at the OS (or virtual network stack) layer.</P> + * + * This should not call {@link Node#multicastSubscribe} or other network-modifying + * methods, as this could cause a deadlock in multithreaded or interrupt + * driven environments. + * + * This must return 0 on success. It can return any OS-dependent error code + * on failure, and this results in the network being placed into the + * PORT_ERROR state. + * + * @param nwid network id + * @param op {@link VirtualNetworkConfigOperation} enum describing the configuration operation + * @param config {@link VirtualNetworkConfig} object with the new configuration + * @return 0 on success + */ + public int onNetworkConfigurationUpdated( + long nwid, + VirtualNetworkConfigOperation op, + VirtualNetworkConfig config); +}
\ No newline at end of file diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigOperation.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigOperation.java new file mode 100644 index 0000000..b70eb47 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkConfigOperation.java @@ -0,0 +1,49 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +public enum VirtualNetworkConfigOperation { + /** + * Network is coming up (either for the first time or after service restart) + */ + VIRTUAL_NETWORK_CONFIG_OPERATION_UP, + + /** + * Network configuration has been updated + */ + VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE, + + /** + * Network is going down (not permanently) + */ + VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN, + + /** + * Network is going down permanently (leave/delete) + */ + VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY +} diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkFrameListener.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkFrameListener.java new file mode 100644 index 0000000..9ad3228 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkFrameListener.java @@ -0,0 +1,48 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +package com.zerotier.sdk; + +public interface VirtualNetworkFrameListener { + /** + * Function to send a frame out to a virtual network port + * + * @param nwid ZeroTier One network ID + * @param srcMac source MAC address + * @param destMac destination MAC address + * @param ethertype + * @param vlanId + * @param frameData data to send + */ + public void onVirtualNetworkFrame( + long nwid, + long srcMac, + long destMac, + long etherType, + long vlanId, + byte[] frameData); +} diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkStatus.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkStatus.java new file mode 100644 index 0000000..2d00561 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkStatus.java @@ -0,0 +1,59 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +public enum VirtualNetworkStatus { + /** + * Waiting for network configuration (also means revision == 0) + */ + NETWORK_STATUS_REQUESTING_CONFIGURATION, + + /** + * Configuration received and we are authorized + */ + NETWORK_STATUS_OK, + + /** + * Netconf master told us 'nope' + */ + NETWORK_STATUS_ACCESS_DENIED, + + /** + * Netconf master exists, but this virtual network does not + */ + NETWORK_STATUS_NOT_FOUND, + + /** + * Initialization of network failed or other internal error + */ + NETWORK_STATUS_PORT_ERROR, + + /** + * ZeroTier One version too old + */ + NETWORK_STATUS_CLIENT_TOO_OLD +} diff --git a/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkType.java b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkType.java new file mode 100644 index 0000000..ab1f4e0 --- /dev/null +++ b/zerotierone/java/src/com/zerotier/sdk/VirtualNetworkType.java @@ -0,0 +1,39 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * 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/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ +package com.zerotier.sdk; + +public enum VirtualNetworkType { + /** + * Private networks are authorized via certificates of membership + */ + NETWORK_TYPE_PRIVATE, + + /** + * Public networks have no access control -- they'll always be AUTHORIZED + */ + NETWORK_TYPE_PUBLIC +} |
