diff options
Diffstat (limited to 'java')
27 files changed, 4129 insertions, 0 deletions
diff --git a/java/README.md b/java/README.md new file mode 100644 index 00000000..2650ec3d --- /dev/null +++ b/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/java/build.xml b/java/build.xml new file mode 100644 index 00000000..1d85a8e5 --- /dev/null +++ b/java/build.xml @@ -0,0 +1,68 @@ +<project default="build" name="ZeroTierOneSDK"> + <property environment="env"/> + + <target name="clean"> + <delete dir="bin" failonerror="false"/> + <delete dir="classes" failonerror="false"/> + <!--<delete dir="libs" failonerror="false"/>--> + <delete dir="obj" failonerror="false"/> + </target> + + <target name="build"> + <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}"/> + <mkdir dir="bin"/> + <mkdir dir="classes"/> + <javac srcdir="src" + destdir="classes" + classpath="${env.ANDROID_PLATFORM}/android.jar" + includeantruntime="false"/> + <exec dir="jni" executable="${env.NDK_BUILD_LOC}" failonerror="true"> + <arg value="ZT1=${user.dir}/../"/> + </exec> + <copy file="libs/armeabi/libZeroTierOneJNI.so" + tofile="${user.dir}/classes/lib/armeabi/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/arm64-v8a/libZeroTierOneJNI.so" + tofile="${user.dir}/classes/lib/arm64-v8a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/armeabi-v7a/libZeroTierOneJNI.so" + tofile="${user.dir}/classes/lib/armeabi-v7a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/x86/libZeroTierOneJNI.so" + tofile="${user.dir}/classes/lib/x86/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="bin/ZeroTierOneSDK.jar" + tofile="${user.dir}/classes/lib/ZeroTierOneSDK.jar" + overwrite="true"/> + <jar destfile="bin/ZeroTierOneSDK.jar" basedir="classes"/> + </target> + + <target name="docs"> + <echo message="Generating Javadocs"/> + <mkdir dir="doc/"/> + <javadoc sourcepath="src/" + destdir="doc/"/> + </target> + +<!-- <target name="android" depends="build"> + <echo message="OS is Android, installing..."/> + <copy file="libs/armeabi/libZeroTierOneJNI.so" + tofile="${aproj_loc}/libs/armeabi/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/arm64-v8a/libZeroTierOneJNI.so" + tofile="${aproj_loc}/libs/arm64-v8a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/armeabi-v7a/libZeroTierOneJNI.so" + tofile="${aproj_loc}/libs/armeabi-v7a/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="libs/x86/libZeroTierOneJNI.so" + tofile="${aproj_loc}/libs/x86/libZeroTierOneJNI.so" + overwrite="true"/> + <copy file="bin/ZeroTierOneSDK.jar" + tofile="${aproj_loc}/libs/ZeroTierOneSDK.jar" + overwrite="true"/> + </target> --> +</project>
\ No newline at end of file diff --git a/java/jni/Android.mk b/java/jni/Android.mk new file mode 100644 index 00000000..136b6c81 --- /dev/null +++ b/java/jni/Android.mk @@ -0,0 +1,42 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := ZeroTierOneJNI +LOCAL_C_INCLUDES := $(ZT1)/include + +# 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/Defaults.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/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 \ + ZT1_jniutils.cpp + +include $(BUILD_SHARED_LIBRARY)
\ No newline at end of file diff --git a/java/jni/Application.mk b/java/jni/Application.mk new file mode 100644 index 00000000..11c5c2cf --- /dev/null +++ b/java/jni/Application.mk @@ -0,0 +1,3 @@ +APP_ABI := armeabi armeabi-v7a arm64-v8a x86 +APP_STL := gnustl_static +APP_CPPFLAGS += -fexceptions
\ No newline at end of file diff --git a/java/jni/ZT1_jniutils.cpp b/java/jni/ZT1_jniutils.cpp new file mode 100644 index 00000000..af7a58bc --- /dev/null +++ b/java/jni/ZT1_jniutils.cpp @@ -0,0 +1,963 @@ +#include "ZT1_jniutils.h" +#include <string> +#include <assert.h> + +#ifdef __cplusplus +extern "C" { +#endif + +namespace +{ + static jclass arrayListClass = NULL; + static jmethodID arrayList_constructor = NULL; + static jmethodID arrayList_add = NULL; + + static jclass inetAddressClass = NULL; + static jmethodID inetAddress_getByAddress = NULL; +} + +jobject createResultObject(JNIEnv *env, ZT1_ResultCode code) +{ + // cache the class so we don't have to + // look it up every time we need to create a java + // ResultCode object + static jclass resultClass = NULL; + + jobject resultObject = NULL; + + if(resultClass == NULL) + { + resultClass = env->FindClass("com/zerotierone/sdk/ResultCode"); + if(resultClass == NULL) + { + return NULL; // exception thrown + } + } + + std::string fieldName; + switch(code) + { + case ZT1_RESULT_OK: + fieldName = "ZT1_RESULT_OK"; + break; + case ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY: + fieldName = "ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY"; + break; + case ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED: + fieldName = "ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED"; + break; + case ZT1_RESULT_ERROR_NETWORK_NOT_FOUND: + fieldName = "ZT1_RESULT_ERROR_NETWORK_NOT_FOUND"; + break; + case ZT1_RESULT_FATAL_ERROR_INTERNAL: + default: + fieldName = "ZT1_RESULT_FATAL_ERROR_INTERNAL"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(resultClass, fieldName.c_str(), "Lcom/zerotierone/sdk/ResultCode;"); + + resultObject = env->GetStaticObjectField(resultClass, enumField); + + return resultObject; +} + + +jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status) +{ + static jclass statusClass = NULL; + + jobject statusObject = NULL; + + if(statusClass == NULL) + { + statusClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkStatus"); + if(statusClass == NULL) + { + return NULL; // exception thrown + } + } + + std::string fieldName; + switch(status) + { + case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: + fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION"; + break; + case ZT1_NETWORK_STATUS_OK: + fieldName = "NETWORK_STATUS_OK"; + break; + case ZT1_NETWORK_STATUS_ACCESS_DENIED: + fieldName = "NETWORK_STATUS_ACCESS_DENIED"; + break; + case ZT1_NETWORK_STATUS_NOT_FOUND: + fieldName = "NETWORK_STATUS_NOT_FOUND"; + break; + case ZT1_NETWORK_STATUS_PORT_ERROR: + fieldName = "NETWORK_STATUS_PORT_ERROR"; + break; + case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD: + fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(statusClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkStatus;"); + + statusObject = env->GetStaticObjectField(statusClass, enumField); + + return statusObject; +} + +jobject createEvent(JNIEnv *env, ZT1_Event event) +{ + static jclass eventClass = NULL; + jobject eventObject = NULL; + + if(eventClass == NULL) + { + eventClass = env->FindClass("com/zerotierone/sdk/Event"); + if(eventClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(event) + { + case ZT1_EVENT_UP: + fieldName = "EVENT_UP"; + break; + case ZT1_EVENT_OFFLINE: + fieldName = "EVENT_OFFLINE"; + break; + case ZT1_EVENT_DOWN: + fieldName = "EVENT_DOWN"; + break; + case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION"; + break; + case ZT1_EVENT_AUTHENTICATION_FAILURE: + fieldName = "EVENT_AUTHENTICATION_FAILURE"; + break; + case ZT1_EVENT_INVALID_PACKET: + fieldName = "EVENT_INVALID_PACKET"; + break; + case ZT1_EVENT_TRACE: + fieldName = "EVENT_TRACE"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(eventClass, fieldName.c_str(), "Lcom/zerotierone/sdk/Event;"); + + eventObject = env->GetStaticObjectField(eventClass, enumField); + + return eventObject; +} + +jobject createPeerRole(JNIEnv *env, ZT1_PeerRole role) +{ + static jclass peerRoleClass = NULL; + jobject peerRoleObject = NULL; + + if(peerRoleClass == NULL) + { + peerRoleClass = env->FindClass("com/zerotierone/sdk/PeerRole"); + if(peerRoleClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(role) + { + case ZT1_PEER_ROLE_LEAF: + fieldName = "PEER_ROLE_LEAF"; + break; + case ZT1_PEER_ROLE_HUB: + fieldName = "PEER_ROLE_HUB"; + break; + case ZT1_PEER_ROLE_SUPERNODE: + fieldName = "PEER_ROLE_SUPERNODE"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(peerRoleClass, fieldName.c_str(), "Lcom/zerotierone/sdk/PeerRole;"); + + peerRoleObject = env->GetStaticObjectField(peerRoleClass, enumField); + + return peerRoleObject; +} + +jobject createVirtualNetworkType(JNIEnv *env, ZT1_VirtualNetworkType type) +{ + static jclass vntypeClass = NULL; + jobject vntypeObject = NULL; + + if(vntypeClass == NULL) + { + vntypeClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkType"); + if(vntypeClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(type) + { + case ZT1_NETWORK_TYPE_PRIVATE: + fieldName = "NETWORK_TYPE_PRIVATE"; + break; + case ZT1_NETWORK_TYPE_PUBLIC: + fieldName = "NETWORK_TYPE_PUBLIC"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(vntypeClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkType;"); + vntypeObject = env->GetStaticObjectField(vntypeClass, enumField); + return vntypeObject; +} + +jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT1_VirtualNetworkConfigOperation op) +{ + static jclass vnetConfigOpClass = NULL; + jobject vnetConfigOpObject = NULL; + + if(vnetConfigOpClass == NULL) + { + vnetConfigOpClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkConfigOperation"); + if(vnetConfigOpClass == NULL) + { + return NULL; + } + } + + std::string fieldName; + switch(op) + { + case ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_UP: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_UP"; + break; + case ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE"; + break; + case ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN"; + break; + case ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY: + fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY"; + break; + } + + jfieldID enumField = env->GetStaticFieldID(vnetConfigOpClass, fieldName.c_str(), "Lcom/zerotierone/sdk/VirtualNetworkConfigOperation;"); + vnetConfigOpObject = env->GetStaticObjectField(vnetConfigOpClass, enumField); + return vnetConfigOpObject; +} + +jobject newArrayList(JNIEnv *env) +{ + if(arrayListClass == NULL) + { + arrayListClass = env->FindClass("java/util/ArrayList"); + if(arrayListClass == NULL) + { + return NULL; + } + } + + if(arrayList_constructor == NULL) + { + arrayList_constructor = env->GetMethodID( + arrayListClass, "<init>", "()V"); + if(arrayList_constructor == NULL) + { + return NULL; + } + } + + jobject arrayListObj = env->NewObject(arrayListClass, arrayList_constructor); + + return arrayListObj; +} + +bool appendItemToArrayList(JNIEnv *env, jobject array, jobject object) +{ + assert(array != NULL); + assert(object != NULL); + + if(arrayList_add == NULL) + { + arrayList_add = env->GetMethodID(arrayListClass, "add", "(Ljava.lang.Object;)Z"); + if(arrayList_add == NULL) + { + return false; + } + } + + return env->CallBooleanMethod(array, arrayList_add, object); +} + +jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr) +{ + if(inetAddressClass == NULL) + { + inetAddressClass = env->FindClass("java/net/InetAddress"); + if(inetAddressClass == NULL) + { + return NULL; + } + } + + if(inetAddress_getByAddress == NULL) + { + inetAddress_getByAddress = env->GetStaticMethodID( + inetAddressClass, "getByAddress", "([B)Ljava/net/InetAddress;"); + if(inetAddress_getByAddress == NULL) + { + 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) + { + 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) + { + return NULL; + } + + env->SetByteArrayRegion(buff, 0, 4, (jbyte*)&ipv4->sin_addr); + inetAddressObj = env->CallStaticObjectMethod( + inetAddressClass, inetAddress_getByAddress, buff); + } + break; + } + + return inetAddressObj; +} + +jobject newMulticastGroup(JNIEnv *env, const ZT1_MulticastGroup &mc) +{ + static jclass multicastGroupClass = NULL; + static jmethodID multicastGroup_constructor = NULL; + + static jfieldID macField = NULL; + static jfieldID adiField = NULL; + + if(multicastGroupClass == NULL) + { + multicastGroupClass = env->FindClass("com/zerotierone/sdk/MulticastGroup"); + if(multicastGroupClass == NULL) + { + return NULL; + } + } + + if(multicastGroup_constructor == NULL) + { + multicastGroup_constructor = env->GetMethodID( + multicastGroupClass, "<init>", "()V"); + if(multicastGroup_constructor == NULL) + { + return NULL; + } + } + + jobject multicastGroupObj = env->NewObject(multicastGroupClass, multicastGroup_constructor); + if(multicastGroupObj == NULL) + { + return NULL; + } + + if(macField == NULL) + { + macField = env->GetFieldID(multicastGroupClass, "mac", "J"); + if(macField == NULL) + { + return NULL; + } + } + + if(adiField == NULL) + { + adiField = env->GetFieldID(multicastGroupClass, "adi", "J"); + if(adiField == NULL) + { + return NULL; + } + } + + env->SetLongField(multicastGroupObj, macField, mc.mac); + env->SetLongField(multicastGroupObj, adiField, mc.adi); + + return multicastGroupObj; +} + +jobject newPeerPhysicalPath(JNIEnv *env, const ZT1_PeerPhysicalPath &ppp) +{ + static jclass pppClass = NULL; + + static jfieldID addressField = NULL; + static jfieldID lastSendField = NULL; + static jfieldID lastReceiveField = NULL; + static jfieldID fixedField = NULL; + static jfieldID activeField = NULL; + static jfieldID preferredField = NULL; + + static jmethodID ppp_constructor = NULL; + + if(pppClass == NULL) + { + pppClass = env->FindClass("com/zerotierone/sdk/PeerPhysicalPath"); + if(pppClass == NULL) + { + return NULL; + } + } + + if(addressField == NULL) + { + addressField = env->GetFieldID(pppClass, "address", "Ljava/net/InetAddress;"); + if(addressField == NULL) + { + return NULL; + } + } + + if(lastSendField == NULL) + { + lastSendField = env->GetFieldID(pppClass, "lastSend", "J"); + if(lastSendField == NULL) + { + return NULL; + } + } + + if(lastReceiveField == NULL) + { + lastReceiveField = env->GetFieldID(pppClass, "lastReceive", "J"); + if(lastReceiveField == NULL) + { + return NULL; + } + } + + if(fixedField == NULL) + { + fixedField = env->GetFieldID(pppClass, "fixed", "Z"); + if(fixedField == NULL) + { + return NULL; + } + } + + if(activeField == NULL) + { + activeField = env->GetFieldID(pppClass, "active", "Z"); + if(activeField == NULL) + { + return NULL; + } + } + + if(preferredField == NULL) + { + preferredField = env->GetFieldID(pppClass, "preferred", "Z"); + if(preferredField == NULL) + { + return NULL; + } + } + + if(ppp_constructor == NULL) + { + ppp_constructor = env->GetMethodID(pppClass, "<init>", "()V"); + if(ppp_constructor == NULL) + { + return NULL; + } + } + + jobject pppObject = env->NewObject(pppClass, ppp_constructor); + if(pppObject == NULL) + { + return NULL; // out of memory + } + + jobject addressObject = newInetAddress(env, ppp.address); + + env->SetObjectField(pppClass, addressField, addressObject); + env->SetLongField(pppClass, lastSendField, ppp.lastSend); + env->SetLongField(pppClass, lastReceiveField, ppp.lastReceive); + env->SetBooleanField(pppClass, fixedField, ppp.fixed); + env->SetBooleanField(pppClass, activeField, ppp.active); + env->SetBooleanField(pppClass, preferredField, ppp.preferred); + + return pppObject; +} + +jobject newPeer(JNIEnv *env, const ZT1_Peer &peer) +{ + static jclass peerClass = NULL; + + static jfieldID addressField = NULL; + static jfieldID lastUnicastFrameField = NULL; + static jfieldID lastMulticastFrameField = NULL; + static jfieldID versionMajorField = NULL; + static jfieldID versionMinorField = NULL; + static jfieldID versionRevField = NULL; + static jfieldID latencyField = NULL; + static jfieldID roleField = NULL; + static jfieldID pathsField = NULL; + + static jmethodID peer_constructor = NULL; + + if(peerClass == NULL) + { + peerClass = env->FindClass("com/zerotierone/sdk/Peer"); + if(peerClass == NULL) + { + return NULL; + } + } + + if(addressField == NULL) + { + addressField = env->GetFieldID(peerClass, "address", "J"); + if(addressField == NULL) + { + return NULL; + } + } + + if(lastUnicastFrameField == NULL) + { + lastUnicastFrameField = env->GetFieldID(peerClass, "lastUnicastFrame", "J"); + if(lastUnicastFrameField == NULL) + { + return NULL; + } + } + + if(lastMulticastFrameField == NULL) + { + lastMulticastFrameField = env->GetFieldID(peerClass, "lastMulticastFrame", "J"); + if(lastMulticastFrameField == NULL) + { + return NULL; + } + } + + if(versionMajorField == NULL) + { + versionMajorField = env->GetFieldID(peerClass, "versionMajor", "I"); + if(versionMajorField == NULL) + { + return NULL; + } + } + + if(versionMinorField == NULL) + { + versionMinorField = env->GetFieldID(peerClass, "versionMinor", "I"); + if(versionMinorField == NULL) + { + return NULL; + } + } + + if(versionRevField == NULL) + { + versionRevField = env->GetFieldID(peerClass, "versionRev", "I"); + if(versionRevField == NULL) + { + return NULL; + } + } + + if(latencyField == NULL) + { + latencyField = env->GetFieldID(peerClass, "latency", "I"); + if(latencyField == NULL) + { + return NULL; + } + } + + if(roleField == NULL) + { + roleField = env->GetFieldID(peerClass, "role", "Lcom/zerotierone/sdk/PeerRole;"); + if(roleField == NULL) + { + return NULL; + } + } + + if(pathsField == NULL) + { + pathsField = env->GetFieldID(peerClass, "paths", "Ljava.util.ArrayList;"); + if(pathsField == NULL) + { + return NULL; + } + } + + if(peer_constructor == NULL) + { + peer_constructor = env->GetMethodID(peerClass, "<init>", "()V"); + if(peer_constructor == NULL) + { + return NULL; + } + } + + jobject peerObject = env->NewObject(peerClass, peer_constructor); + if(peerObject == NULL) + { + return NULL; // out of memory + } + + env->SetLongField(peerClass, addressField, (jlong)peer.address); + env->SetLongField(peerClass, lastUnicastFrameField, (jlong)peer.lastUnicastFrame); + env->SetLongField(peerClass, lastMulticastFrameField, (jlong)peer.lastMulticastFrame); + env->SetIntField(peerClass, versionMajorField, peer.versionMajor); + env->SetIntField(peerClass, versionMinorField, peer.versionMinor); + env->SetIntField(peerClass, versionRevField, peer.versionRev); + env->SetIntField(peerClass, latencyField, peer.latency); + env->SetObjectField(peerClass, roleField, createPeerRole(env, peer.role)); + + jobject arrayObject = newArrayList(env); + for(unsigned int i = 0; i < peer.pathCount; ++i) + { + jobject path = newPeerPhysicalPath(env, peer.paths[i]); + appendItemToArrayList(env, arrayObject, path); + } + + env->SetObjectField(peerClass, pathsField, arrayObject); + + return peerObject; +} + +jobject newNetworkConfig(JNIEnv *env, const ZT1_VirtualNetworkConfig &vnetConfig) +{ + static jclass vnetConfigClass = NULL; + static jmethodID vnetConfig_constructor = NULL; + static jfieldID nwidField = NULL; + static jfieldID macField = NULL; + static jfieldID nameField = NULL; + static jfieldID statusField = NULL; + static jfieldID typeField = NULL; + static jfieldID mtuField = NULL; + static jfieldID dhcpField = NULL; + static jfieldID bridgeField = NULL; + static jfieldID broadcastEnabledField = NULL; + static jfieldID portErrorField = NULL; + static jfieldID enabledField = NULL; + static jfieldID netconfRevisionField = NULL; + static jfieldID multicastSubscriptionsField = NULL; + static jfieldID assignedAddressesField = NULL; + + if(vnetConfigClass == NULL) + { + vnetConfigClass = env->FindClass("com/zerotierone/sdk/VirtualNetworkConfig"); + if(vnetConfigClass == NULL) + { + return NULL; + } + } + + if(vnetConfig_constructor == NULL) + { + jmethodID vnetConfig_constructor = env->GetMethodID( + vnetConfigClass, "<init>", "()V"); + if(vnetConfig_constructor == NULL) + { + return NULL; + } + } + + jobject vnetConfigObj = env->NewObject(vnetConfigClass, vnetConfig_constructor); + if(vnetConfigObj == NULL) + { + return NULL; + } + + if(nwidField == NULL) + { + nwidField = env->GetFieldID(vnetConfigClass, "nwid", "J"); + if(nwidField == NULL) + { + return NULL; + } + } + + if(macField == NULL) + { + macField = env->GetFieldID(vnetConfigClass, "mac", "J"); + if(macField == NULL) + { + return NULL; + } + } + + if(nameField == NULL) + { + nameField = env->GetFieldID(vnetConfigClass, "name", "Ljava/lang/String;"); + if(nameField == NULL) + { + return NULL; + } + } + + if(statusField == NULL) + { + statusField = env->GetFieldID(vnetConfigClass, "status", "Lcom/zerotierone/sdk/VirtualNetworStatus;"); + if(statusField == NULL) + { + return NULL; + } + } + + if(typeField == NULL) + { + typeField = env->GetFieldID(vnetConfigClass, "type", "Lcom/zerotierone/sdk/VirtualNetworkType;"); + if(typeField == NULL) + { + return NULL; + } + } + + if(mtuField == NULL) + { + mtuField = env->GetFieldID(vnetConfigClass, "mtu", "I"); + if(mtuField == NULL) + { + return NULL; + } + } + + if(dhcpField == NULL) + { + dhcpField = env->GetFieldID(vnetConfigClass, "dhcp", "Z"); + if(dhcpField == NULL) + { + return NULL; + } + } + + if(bridgeField == NULL) + { + bridgeField = env->GetFieldID(vnetConfigClass, "bridge", "Z"); + if(bridgeField == NULL) + { + return NULL; + } + } + + if(broadcastEnabledField == NULL) + { + broadcastEnabledField = env->GetFieldID(vnetConfigClass, "broadcastEnabled", "Z"); + if(broadcastEnabledField == NULL) + { + return NULL; + } + } + + if(portErrorField == NULL) + { + portErrorField == env->GetFieldID(vnetConfigClass, "portError", "I"); + if(portErrorField == NULL) + { + return NULL; + } + } + + if(enabledField == NULL) + { + enabledField = env->GetFieldID(vnetConfigClass, "enabled", "Z"); + if(enabledField == NULL) + { + return NULL; + } + } + + if(netconfRevisionField == NULL) + { + netconfRevisionField = env->GetFieldID(vnetConfigClass, "netconfRevision", "J"); + if(netconfRevisionField == NULL) + { + return NULL; + } + } + + if(multicastSubscriptionsField == NULL) + { + multicastSubscriptionsField = env->GetFieldID(vnetConfigClass, "multicastSubscriptions", "Ljava/util/ArrayList;"); + if(multicastSubscriptionsField == NULL) + { + return NULL; + } + } + + if(assignedAddressesField == NULL) + { + assignedAddressesField = env->GetFieldID(vnetConfigClass, "assignedAddresses", "Ljava/util/ArrayList;"); + if(assignedAddressesField == NULL) + { + return NULL; + } + } + + env->SetLongField(vnetConfigObj, nwidField, vnetConfig.nwid); + env->SetLongField(vnetConfigObj, macField, vnetConfig.mac); + jstring nameStr = env->NewStringUTF(vnetConfig.name); + if(nameStr == NULL) + { + return NULL; // out of memory + } + env->SetObjectField(vnetConfigObj, nameField, nameStr); + + jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status); + if(statusObject == NULL) + { + return NULL; + } + env->SetObjectField(vnetConfigObj, statusField, statusObject); + + jobject typeObject = createVirtualNetworkType(env, vnetConfig.type); + if(typeObject == NULL) + { + return NULL; + } + env->SetObjectField(vnetConfigObj, typeField, typeObject); + + env->SetIntField(vnetConfigObj, mtuField, vnetConfig.mtu); + env->SetBooleanField(vnetConfigObj, dhcpField, vnetConfig.dhcp); + env->SetBooleanField(vnetConfigObj, bridgeField, vnetConfig.bridge); + env->SetBooleanField(vnetConfigObj, broadcastEnabledField, vnetConfig.broadcastEnabled); + env->SetIntField(vnetConfigObj, portErrorField, vnetConfig.portError); + + + jobject mcastSubsArrayObj = newArrayList(env); + for(unsigned int i = 0; i < vnetConfig.multicastSubscriptionCount; ++i) + { + jobject mcastObj = newMulticastGroup(env, vnetConfig.multicastSubscriptions[i]); + appendItemToArrayList(env, mcastSubsArrayObj, mcastObj); + } + env->SetObjectField(vnetConfigObj, multicastSubscriptionsField, mcastSubsArrayObj); + + + jobject assignedAddrArrayObj = newArrayList(env); + for(unsigned int i = 0; i < vnetConfig.assignedAddressCount; ++i) + { + jobject inetAddrObj = newInetAddress(env, vnetConfig.assignedAddresses[i]); + appendItemToArrayList(env, assignedAddrArrayObj, inetAddrObj); + } + + env->SetObjectField(vnetConfigObj, assignedAddressesField, assignedAddrArrayObj); + + return vnetConfigObj; +} + +jobject newVersion(JNIEnv *env, int major, int minor, int rev, long featureFlags) +{ + // create a com.zerotierone.sdk.Version object + static jclass versionClass = NULL; + static jmethodID versionConstructor = NULL; + + if(versionClass == NULL) + { + versionClass = env->FindClass("com/zerotierone/sdk/Version"); + if(versionClass == NULL) + { + return NULL; + } + } + + if(versionConstructor == NULL) + { + versionConstructor = env->GetMethodID( + versionClass, "<init>", "()V"); + if(versionConstructor == NULL) + { + return NULL; + } + } + + jobject versionObj = env->NewObject(versionClass, versionConstructor); + if(versionObj == NULL) + { + return NULL; + } + + // copy data to Version object + static jfieldID majorField = NULL; + static jfieldID minorField = NULL; + static jfieldID revisionField = NULL; + static jfieldID featureFlagsField = NULL; + + if(majorField == NULL) + { + majorField = env->GetFieldID(versionClass, "major", "I"); + if(majorField = NULL) + { + return NULL; + } + } + + if(minorField == NULL) + { + minorField = env->GetFieldID(versionClass, "minor", "I"); + if(minorField == NULL) + { + return NULL; + } + } + + if(revisionField == NULL) + { + revisionField = env->GetFieldID(versionClass, "revision", "I"); + if(revisionField == NULL) + { + return NULL; + } + } + + if(featureFlagsField == NULL) + { + featureFlagsField = env->GetFieldID(versionClass, "featureFlags", "J"); + if(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); +} + +#ifdef __cplusplus +} +#endif
\ No newline at end of file diff --git a/java/jni/ZT1_jniutils.h b/java/jni/ZT1_jniutils.h new file mode 100644 index 00000000..a379ed05 --- /dev/null +++ b/java/jni/ZT1_jniutils.h @@ -0,0 +1,35 @@ +#ifndef ZT1_jniutils_h_ +#define ZT1_jniutils_h_ +#include <jni.h> +#include <ZeroTierOne.h> + +#ifdef __cplusplus +extern "C" { +#endif + +jobject createResultObject(JNIEnv *env, ZT1_ResultCode code); +jobject createVirtualNetworkStatus(JNIEnv *env, ZT1_VirtualNetworkStatus status); +jobject createVirtualNetworkType(JNIEnv *env, ZT1_VirtualNetworkType type); +jobject createEvent(JNIEnv *env, ZT1_Event event); +jobject createPeerRole(JNIEnv *env, ZT1_PeerRole role); +jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT1_VirtualNetworkConfigOperation op); + +jobject newArrayList(JNIEnv *env); +bool appendItemToArrayList(JNIEnv *env, jobject array, jobject object); + +jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr); + +jobject newMulticastGroup(JNIEnv *env, const ZT1_MulticastGroup &mc); + +jobject newPeer(JNIEnv *env, const ZT1_Peer &peer); +jobject newPeerPhysicalPath(JNIEnv *env, const ZT1_PeerPhysicalPath &ppp); + +jobject newNetworkConfig(JNIEnv *env, const ZT1_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/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp new file mode 100644 index 00000000..fef9b4d6 --- /dev/null +++ b/java/jni/com_zerotierone_sdk_Node.cpp @@ -0,0 +1,1202 @@ +/* + * 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 "ZT1_jniutils.h" + +#include <ZeroTierOne.h> + +#include <map> +#include <string> +#include <assert.h> +#include <string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +namespace { + struct JniRef + { + JniRef() + : env(NULL) + , node(NULL) + , dataStoreGetListener(NULL) + , dataStoreGetClass(NULL) + , dataStoreGetCallbackMethod(NULL) + , dataStorePutListener(NULL) + , dataStorePutClass(NULL) + , dataStorePutCallbackMethod(NULL) + , deleteMethod(NULL) + , packetSender(NULL) + , packetSenderClass(NULL) + , packetSenderCallbackMethod(NULL) + , eventListener(NULL) + , eventListenerClass(NULL) + , onEventMethod(NULL) + , onOutOfDateMethod(NULL) + , onNetworkErrorMethod(NULL) + , onTraceMethod(NULL) + , frameListener(NULL) + , frameListenerClass(NULL) + , frameListenerCallbackMethod(NULL) + , configListener(NULL) + , configListenerClass(NULL) + , configListenerCallbackMethod(NULL) + {} + uint64_t id; + + JNIEnv *env; + + ZT1_Node *node; + + jobject dataStoreGetListener; + jclass dataStoreGetClass; + jmethodID dataStoreGetCallbackMethod; + + jobject dataStorePutListener; + jclass dataStorePutClass; + jmethodID dataStorePutCallbackMethod; + jmethodID deleteMethod; + + jobject packetSender; + jclass packetSenderClass; + jmethodID packetSenderCallbackMethod; + + jobject eventListener; + jclass eventListenerClass; + jmethodID onEventMethod; + jmethodID onOutOfDateMethod; + jmethodID onNetworkErrorMethod; + jmethodID onTraceMethod; + + jobject frameListener; + jclass frameListenerClass; + jmethodID frameListenerCallbackMethod; + + jobject configListener; + jclass configListenerClass; + jmethodID configListenerCallbackMethod; + }; + + + int VirtualNetworkConfigFunctionCallback( + ZT1_Node *node, + void *userData, + uint64_t nwid, + enum ZT1_VirtualNetworkConfigOperation operation, + const ZT1_VirtualNetworkConfig *config) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + if(ref->configListenerClass == NULL) + { + ref->configListenerClass = env->GetObjectClass(ref->configListener); + if(ref->configListenerClass == NULL) + { + return -1; + } + } + + if(ref->configListenerCallbackMethod == NULL) + { + ref->configListenerCallbackMethod = env->GetMethodID(ref->configListenerClass, + "onNetworkConfigurationUpdated", + "(JLcom/zerotierone/sdk/VirtualNetworkConfigOperation;Lcom/zerotierone/sdk/VirtualNetworkConfig;)I"); + if(ref->configListenerCallbackMethod == NULL) + { + return -2; + } + } + + jobject operationObject = createVirtualNetworkConfigOperation(env, operation); + if(operationObject == NULL) + { + return -3; + } + + jobject networkConfigObject = newNetworkConfig(env, *config); + if(networkConfigObject == NULL) + { + return -4; + } + + return env->CallIntMethod( + ref->configListener, + ref->configListenerCallbackMethod, + (jlong)nwid, operationObject, networkConfigObject); + } + + void VirtualNetworkFrameFunctionCallback(ZT1_Node *node,void *userData, + uint64_t nwid, + uint64_t sourceMac, + uint64_t destMac, + unsigned int etherType, + unsigned int vlanid, + const void *frameData, + unsigned int frameLength) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + if(ref->frameListenerClass == NULL) + { + ref->frameListenerClass = env->GetObjectClass(ref->frameListener); + if(ref->frameListenerClass == NULL) + { + return; + } + } + + if(ref->frameListenerCallbackMethod == NULL) + { + ref->frameListenerCallbackMethod = env->GetMethodID( + ref->frameListenerClass, + "onVirtualNetworkFrame", "(JJJJJ[B)V"); + if(ref->frameListenerCallbackMethod == NULL) + { + return; + } + } + + jbyteArray dataArray = env->NewByteArray(frameLength); + env->SetByteArrayRegion(dataArray, 0, frameLength, (jbyte*)frameData); + + env->CallVoidMethod(ref->frameListener, ref->frameListenerCallbackMethod, nwid, sourceMac, destMac, etherType, vlanid, dataArray); + } + + + void EventCallback(ZT1_Node *node,void *userData,enum ZT1_Event event, const void *data) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + if(ref->eventListenerClass) + { + ref->eventListenerClass = env->GetObjectClass(ref->eventListener); + if(ref->eventListenerClass == NULL) + { + return; + } + } + + if(ref->onEventMethod == NULL) + { + ref->onEventMethod = env->GetMethodID(ref->eventListenerClass, + "onEvent", "(Lcom/zerotierone/sdk/Event;)V"); + if(ref->onEventMethod == NULL) + { + return; + } + } + + if(ref->onOutOfDateMethod == NULL) + { + ref->onOutOfDateMethod = env->GetMethodID(ref->eventListenerClass, + "onOutOfDate", "(Lcom/zerotierone/sdk/Version;)V"); + if(ref->onOutOfDateMethod == NULL) + { + return; + } + } + + if(ref->onOutOfDateMethod == NULL) + { + ref->onNetworkErrorMethod = env->GetMethodID(ref->eventListenerClass, + "onNetworkError", "(Lcom/zerotierone/sdk/Version;Ljava/net/InetAddress;)V"); + if(ref->onNetworkErrorMethod == NULL) + { + return; + } + } + + if(ref->onTraceMethod == NULL) + { + ref->onTraceMethod = env->GetMethodID(ref->eventListenerClass, + "onTrace", "(Ljava/lang/String;)V"); + if(ref->onTraceMethod == NULL) + { + return; + } + } + + jobject eventObject = createEvent(env, event); + if(eventObject == NULL) + { + return; + } + + switch(event) + { + case ZT1_EVENT_UP: + case ZT1_EVENT_OFFLINE: + case ZT1_EVENT_ONLINE: + case ZT1_EVENT_DOWN: + case ZT1_EVENT_FATAL_ERROR_IDENTITY_COLLISION: + { + // call onEvent() + env->CallVoidMethod(ref->eventListener, ref->onEventMethod, eventObject); + } + break; + case ZT1_EVENT_SAW_MORE_RECENT_VERSION: + { + // call onOutOfDate() + if(data != NULL) + { + int *version = (int*)data; + jobject verisonObj = newVersion(env, version[0], version[1], version[2], 0); + env->CallVoidMethod(ref->eventListener, ref->onOutOfDateMethod, verisonObj); + } + } + break; + case ZT1_EVENT_AUTHENTICATION_FAILURE: + case ZT1_EVENT_INVALID_PACKET: + { + // call onNetworkError() + if(data != NULL) + { + sockaddr_storage *addr = (sockaddr_storage*)data; + jobject addressObj = newInetAddress(env, *addr); + env->CallVoidMethod(ref->eventListener, ref->onNetworkErrorMethod, addressObj); + } + } + case ZT1_EVENT_TRACE: + { + // call onTrace() + if(data != NULL) + { + const char* message = (const char*)data; + jstring messageStr = env->NewStringUTF(message); + env->CallVoidMethod(ref->eventListener, ref->onTraceMethod); + } + } + break; + } + } + + long DataStoreGetFunction(ZT1_Node *node,void *userData, + const char *objectName, + void *buffer, + unsigned long bufferSize, + unsigned long bufferIndex, + unsigned long *out_objectSize) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + + + if(ref->dataStoreGetClass == NULL) + { + ref->dataStoreGetClass = env->GetObjectClass(ref->dataStoreGetListener); + if(ref->dataStoreGetClass == NULL) + { + return -2; + } + } + + if(ref->dataStoreGetCallbackMethod == NULL) + { + ref->dataStoreGetCallbackMethod = env->GetMethodID( + ref->dataStoreGetClass, + "onDataStoreGet", + "(Ljava/lang/String;[BJ[J)J"); + if(ref->dataStoreGetCallbackMethod == NULL) + { + return -2; + } + } + + jstring nameStr = env->NewStringUTF(objectName); + jbyteArray bufferObj = env->NewByteArray(bufferSize); + jlongArray objectSizeObj = env->NewLongArray(1); + + long retval = env->CallLongMethod( + ref->dataStoreGetClass, ref->dataStoreGetCallbackMethod, + nameStr, bufferObj, bufferIndex, objectSizeObj); + + env->GetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer); + env->GetLongArrayRegion(objectSizeObj, 0, 1, (jlong*)&out_objectSize); + env->ReleaseByteArrayElements(bufferObj, (jbyte*)buffer, 0); + env->ReleaseLongArrayElements(objectSizeObj, (jlong*)&out_objectSize, 0); + + return retval; + } + + int DataStorePutFunction(ZT1_Node *node,void *userData, + const char *objectName, + const void *buffer, + unsigned long bufferSize, + int secure) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + if(ref->dataStorePutClass == NULL) + { + ref->dataStorePutClass = env->GetObjectClass(ref->dataStorePutListener); + if(ref->dataStorePutClass == NULL) + { + return -1; + } + } + + if(ref->dataStorePutCallbackMethod == NULL) + { + ref->dataStorePutCallbackMethod = env->GetMethodID( + ref->dataStorePutClass, + "onDataStorePut", + "(Ljava/lang/String;[BZ)I"); + if(ref->dataStorePutCallbackMethod == NULL) + { + return -2; + } + } + + if(ref->deleteMethod == NULL) + { + ref->deleteMethod = env->GetMethodID(ref->dataStorePutClass, + "onDelete", "(Ljava/lang/String;)I"); + if(ref->deleteMethod == NULL) + { + return -3; + } + } + + jstring nameStr = env->NewStringUTF(objectName); + + if(buffer == NULL) + { + // delete operation + return env->CallIntMethod( + ref->dataStorePutListener, ref->deleteMethod, nameStr); + } + else + { + // set operation + jbyteArray bufferObj = env->NewByteArray(bufferSize); + env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer); + bool secure = secure != 0; + + + return env->CallIntMethod(ref->dataStorePutListener, + ref->dataStorePutCallbackMethod, + nameStr, bufferObj, secure); + } + } + + int WirePacketSendFunction(ZT1_Node *node,void *userData,\ + const struct sockaddr_storage *address, + unsigned int linkDesparation, + const void *buffer, + unsigned int bufferSize) + { + JniRef *ref = (JniRef*)userData; + assert(ref->node == node); + + JNIEnv *env = ref->env; + + if(ref->packetSenderClass == NULL) + { + ref->packetSenderClass = env->GetObjectClass(ref->packetSender); + if(ref->packetSenderClass == NULL) + { + return -1; + } + } + + if(ref->packetSenderCallbackMethod == NULL) + { + ref->packetSenderCallbackMethod = env->GetMethodID(ref->packetSenderClass, + "onSendPacketRequested", "(Ljava/net/InetAddress;I[B)I"); + if(ref->packetSenderCallbackMethod == NULL) + { + return -2; + } + } + + jobject addressObj = newInetAddress(env, *address); + jbyteArray bufferObj = env->NewByteArray(bufferSize); + env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer); + return env->CallIntMethod(ref->packetSender, ref->packetSenderCallbackMethod, addressObj, linkDesparation, bufferObj); + } + + typedef std::map<uint64_t, JniRef*> NodeMap; + static NodeMap nodeMap; + + ZT1_Node* findNode(uint64_t nodeId) + { + NodeMap::iterator found = nodeMap.find(nodeId); + if(found != nodeMap.end()) + { + JniRef *ref = found->second; + return ref->node; + } + return NULL; + } +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: node_init + * Signature: (J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init( + JNIEnv *env, jobject obj, jlong now) +{ + jobject resultObject = createResultObject(env, ZT1_RESULT_OK); + + ZT1_Node *node; + JniRef *ref = new JniRef; + + ZT1_ResultCode rc = ZT1_Node_new( + &node, + ref, + (uint64_t)now, + &DataStoreGetFunction, + &DataStorePutFunction, + &WirePacketSendFunction, + &VirtualNetworkFrameFunctionCallback, + &VirtualNetworkConfigFunctionCallback, + &EventCallback); + + if(rc != ZT1_RESULT_OK) + { + resultObject = createResultObject(env, rc); + if(node) + { + ZT1_Node_delete(node); + node = NULL; + } + delete ref; + ref = NULL; + return resultObject; + } + + + ref->id = (uint64_t)now; + ref->env = env; + ref->node = node; + + jclass cls = env->GetObjectClass(obj); + jfieldID fid = env->GetFieldID( + cls, "getListener", "Lcom/zerotierone/sdk/DataStoreGetListener;"); + + if(fid == NULL) + { + return NULL; // exception already thrown + } + + ref->dataStoreGetListener = env->GetObjectField(obj, fid); + if(ref->dataStoreGetListener == NULL) + { + return NULL; + } + + fid = env->GetFieldID( + cls, "putListener", "Lcom/zerotierone/sdk/DataStorePutLisetner;"); + + if(fid == NULL) + { + return NULL; // exception already thrown + } + + ref->dataStorePutListener = env->GetObjectField(obj, fid); + if(ref->dataStorePutListener == NULL) + { + return NULL; + } + + fid = env->GetFieldID( + cls, "sender", "Lcom/zerotierone/sdk/PacketSender;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + ref->packetSender = env->GetObjectField(obj, fid); + if(ref->packetSender == NULL) + { + return NULL; + } + + fid = env->GetFieldID( + cls, "frameListener", "Lcom/zerotierone/sdk/VirtualNetworkFrameListener;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + ref->frameListener = env->GetObjectField(obj, fid); + if(ref->frameListener = NULL) + { + return NULL; + } + + fid = env->GetFieldID( + cls, "configListener", "Lcom/zerotierone/sdk/VirtualNetworkConfigListener;"); + if(fid == NULL) + { + return NULL; // exception already thrown + } + + ref->configListener = env->GetObjectField(obj, fid); + if(ref->configListener == NULL) + { + return NULL; + } + + fid = env->GetFieldID( + cls, "eventListener", "Lcom/zerotierone/sdk/EventListener;"); + if(fid == NULL) + { + return NULL; + } + + ref->eventListener = env->GetObjectField(obj, fid); + if(ref->eventListener == NULL) + { + return NULL; + } + + nodeMap.insert(std::make_pair(ref->id, ref)); + + return resultObject; +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: node_delete + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_zerotierone_sdk_Node_node_1delete( + JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t)id; + + NodeMap::iterator found = nodeMap.find(nodeId); + if(found != nodeMap.end()) + { + JniRef *ref = found->second; + nodeMap.erase(found); + + ZT1_Node_delete(ref->node); + + delete ref; + ref = NULL; + } +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: processVirtualNetworkFrame + * Signature: (JJJJJII[B[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_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; + + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_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, ZT1_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); + jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL); + + uint64_t nextBackgroundTaskDeadline = 0; + + ZT1_ResultCode rc = ZT1_Node_processVirtualNetworkFrame( + node, + now, + nwid, + sourceMac, + destMac, + etherType, + vlanId, + (const void*)frameData, + frameLength, + &nextBackgroundTaskDeadline); + + jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0); + + env->ReleaseByteArrayElements(in_frameData, frameData, 0); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: processWirePacket + * Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket( + JNIEnv *env, jobject obj, + jlong id, + jlong in_now, + jobject in_remoteAddress, + jint in_linkDesparation, + jbyteArray in_packetData, + jlongArray out_nextBackgroundTaskDeadline) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline); + if(nbtd_len < 1) + { + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t now = (uint64_t)in_now; + unsigned int linkDesparation = (unsigned int)in_linkDesparation; + + // get the java.net.InetAddress class and getAddress() method + jclass inetAddressClass = env->FindClass("java/net/InetAddress"); + if(inetAddressClass == NULL) + { + // can't find java.net.InetAddress + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + jmethodID getAddressMethod = env->GetMethodID( + inetAddressClass, "getAddress", "()[B"); + if(getAddressMethod == NULL) + { + // cant find InetAddress.getAddres() + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + // Call InetAddress.getAddress() + jbyteArray addressArray = (jbyteArray)env->CallObjectMethod(in_remoteAddress, getAddressMethod); + if(addressArray == NULL) + { + // unable to call getAddress() + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int addrSize = env->GetArrayLength(addressArray); + // get the address bytes + jbyte *addr = env->GetByteArrayElements(addressArray, NULL); + + + sockaddr_storage remoteAddress = {}; + + if(addrSize == 16) + { + // IPV6 address + sockaddr_in6 ipv6 = {}; + ipv6.sin6_family = AF_INET6; + 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; + memcpy(&ipv4.sin_addr, addr, 4); + memcpy(&remoteAddress, &ipv4, sizeof(sockaddr_in)); + } + else + { + // unknown address type + env->ReleaseByteArrayElements(addressArray, addr, 0); + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + + unsigned int packetLength = env->GetArrayLength(in_packetData); + jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL); + + uint64_t nextBackgroundTaskDeadline = 0; + + ZT1_ResultCode rc = ZT1_Node_processWirePacket( + node, + now, + &remoteAddress, + linkDesparation, + packetData, + packetLength, + &nextBackgroundTaskDeadline); + + jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0); + + env->ReleaseByteArrayElements(addressArray, addr, 0); + env->ReleaseByteArrayElements(in_packetData, packetData, 0); + + return createResultObject(env, ZT1_RESULT_OK); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: processBackgroundTasks + * Signature: (JJ[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processBackgroundTasks( + JNIEnv *env, jobject obj, + jlong id, + jlong in_now, + jlongArray out_nextBackgroundTaskDeadline) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline); + if(nbtd_len < 1) + { + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t now = (uint64_t)in_now; + uint64_t nextBackgroundTaskDeadline = 0; + + ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline); + + jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL); + outDeadline[0] = (jlong)nextBackgroundTaskDeadline; + env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: join + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_join( + JNIEnv *env, jobject obj, jlong id, jlong in_nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + + ZT1_ResultCode rc = ZT1_Node_join(node, nwid); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: leave + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_leave( + JNIEnv *env, jobject obj, jlong id, jlong in_nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + + ZT1_ResultCode rc = ZT1_Node_leave(node, nwid); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: multicastSubscribe + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastSubscribe( + JNIEnv *env, jobject obj, + jlong id, + jlong in_nwid, + jlong in_multicastGroup, + jlong in_multicastAdi) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + uint64_t multicastGroup = (uint64_t)in_multicastGroup; + uint64_t multicastAdi = (uint64_t)in_multicastAdi; + + ZT1_ResultCode rc = ZT1_Node_multicastSubscribe( + node, nwid, multicastGroup, multicastAdi); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: multicastUnsubscribe + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastUnsubscribe( + JNIEnv *env, jobject obj, + jlong id, + jlong in_nwid, + jlong in_multicastGroup, + jlong in_multicastAdi) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL); + } + + uint64_t nwid = (uint64_t)in_nwid; + uint64_t multicastGroup = (uint64_t)in_multicastGroup; + uint64_t multicastAdi = (uint64_t)in_multicastAdi; + + ZT1_ResultCode rc = ZT1_Node_multicastUnsubscribe( + node, nwid, multicastGroup, multicastAdi); + + return createResultObject(env, rc); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: address + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_zerotierone_sdk_Node_address( + JNIEnv *env , jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + uint64_t address = ZT1_Node_address(node); + return (jlong)address; +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: status + * Signature: (J)Lcom/zerotierone/sdk/NodeStatus; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_status + (JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + // static so we only have to look these up once + static jclass nodeStatusClass = NULL; + static jmethodID nodeStatusConstructor = NULL; + + // create a com.zerotierone.sdk.NodeStatus object + if(nodeStatusClass == NULL) + { + nodeStatusClass = env->FindClass("com/zerotierone/sdk/NodeStatus"); + if(nodeStatusClass == NULL) + { + return NULL; + } + } + + if(nodeStatusConstructor == NULL) + { + nodeStatusConstructor = env->GetMethodID( + nodeStatusClass, "<init>", "()V"); + if(nodeStatusConstructor == NULL) + { + return NULL; + } + } + + jobject nodeStatusObj = env->NewObject(nodeStatusClass, nodeStatusConstructor); + if(nodeStatusObj == NULL) + { + return NULL; + } + + ZT1_NodeStatus nodeStatus; + ZT1_Node_status(node, &nodeStatus); + + static jfieldID addressField = NULL; + static jfieldID publicIdentityField = NULL; + static jfieldID secretIdentityField = NULL; + static jfieldID onlineField = NULL; + + if(addressField == NULL) + { + addressField = env->GetFieldID(nodeStatusClass, "address", "J"); + if(addressField == NULL) + { + return NULL; + } + } + + if(publicIdentityField == NULL) + { + publicIdentityField = env->GetFieldID(nodeStatusClass, "publicIdentity", "Ljava/lang/String;"); + if(publicIdentityField == NULL) + { + return NULL; + } + } + + if(secretIdentityField == NULL) + { + secretIdentityField = env->GetFieldID(nodeStatusClass, "secretIdentity", "Ljava/lang/String;"); + if(secretIdentityField == NULL) + { + return NULL; + } + } + + if(onlineField == NULL) + { + onlineField = env->GetFieldID(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_zerotierone_sdk_Node + * Method: networkConfig + * Signature: (J)Lcom/zerotierone/sdk/VirtualNetworkConfig; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networkConfig( + JNIEnv *env, jobject obj, jlong id, jlong nwid) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT1_VirtualNetworkConfig *vnetConfig = ZT1_Node_networkConfig(node, nwid); + + jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig); + + ZT1_Node_freeQueryResult(node, vnetConfig); + + return vnetConfigObject; +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: version + * Signature: (J)Lcom/zerotierone/sdk/Version; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version( + JNIEnv *env, jobject obj) +{ + int major = 0; + int minor = 0; + int revision = 0; + unsigned long featureFlags = 0; + + ZT1_version(&major, &minor, &revision, &featureFlags); + + return newVersion(env, major, minor, revision, featureFlags); +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: peers + * Signature: (J)Ljava/util/ArrayList; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_peers( + JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT1_PeerList *peerList = ZT1_Node_peers(node); + + if(peerList == NULL) + { + return NULL; + } + + jobject peerListObject = newArrayList(env); + if(peerListObject == NULL) + { + ZT1_Node_freeQueryResult(node, peerList); + return NULL; + } + + for(unsigned int i = 0; i < peerList->peerCount; ++i) + { + jobject peerObj = newPeer(env, peerList->peers[i]); + appendItemToArrayList(env, peerListObject, peerObj); + } + + ZT1_Node_freeQueryResult(node, peerList); + peerList = NULL; + + return peerListObject; +} + +/* + * Class: com_zerotierone_sdk_Node + * Method: networks + * Signature: (J)Ljava/util/ArrayList; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networks( + JNIEnv *env, jobject obj, jlong id) +{ + uint64_t nodeId = (uint64_t) id; + ZT1_Node *node = findNode(nodeId); + if(node == NULL) + { + // cannot find valid node. We should never get here. + return 0; + } + + ZT1_VirtualNetworkList *networkList = ZT1_Node_networks(node); + if(networkList == NULL) + { + return NULL; + } + + jobject networkListObject = newArrayList(env); + if(networkListObject == NULL) + { + ZT1_Node_freeQueryResult(node, networkList); + return NULL; + } + + for(unsigned int i = 0; i < networkList->networkCount; ++i) + { + jobject networkObject = newNetworkConfig(env, networkList->networks[i]); + appendItemToArrayList(env, networkListObject, networkObject); + } + + ZT1_Node_freeQueryResult(node, networkList); + + return networkListObject; +} + +#ifdef __cplusplus +} // extern "C" +#endif
\ No newline at end of file diff --git a/java/jni/com_zerotierone_sdk_Node.h b/java/jni/com_zerotierone_sdk_Node.h new file mode 100644 index 00000000..fefc9fc5 --- /dev/null +++ b/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_zerotierone_sdk_Node */ + +#ifndef _Included_com_zerotierone_sdk_Node +#define _Included_com_zerotierone_sdk_Node +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_zerotierone_sdk_Node + * Method: node_init + * Signature: (J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_node_1init + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: node_delete + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_zerotierone_sdk_Node_node_1delete + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: processVirtualNetworkFrame + * Signature: (JJJJJII[B[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processVirtualNetworkFrame + (JNIEnv *, jobject, jlong, jlong, jlong, jlong, jlong, jint, jint, jbyteArray, jlongArray); + +/* + * Class: com_zerotierone_sdk_Node + * Method: processWirePacket + * Signature: (JJLjava/net/InetAddress;I[B[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processWirePacket + (JNIEnv *, jobject, jlong, jlong, jobject, jint, jbyteArray, jlongArray); + +/* + * Class: com_zerotierone_sdk_Node + * Method: processBackgroundTasks + * Signature: (JJ[J)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_processBackgroundTasks + (JNIEnv *, jobject, jlong, jlong, jlongArray); + +/* + * Class: com_zerotierone_sdk_Node + * Method: join + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_join + (JNIEnv *, jobject, jlong, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: leave + * Signature: (JJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_leave + (JNIEnv *, jobject, jlong, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: multicastSubscribe + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastSubscribe + (JNIEnv *, jobject, jlong, jlong, jlong, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: multicastUnsubscribe + * Signature: (JJJJ)Lcom/zerotierone/sdk/ResultCode; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_multicastUnsubscribe + (JNIEnv *, jobject, jlong, jlong, jlong, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: address + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_zerotierone_sdk_Node_address + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: status + * Signature: (J)Lcom/zerotierone/sdk/NodeStatus; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_status + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: networkConfig + * Signature: (JJ)Lcom/zerotierone/sdk/VirtualNetworkConfig; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networkConfig + (JNIEnv *, jobject, jlong, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: version + * Signature: ()Lcom/zerotierone/sdk/Version; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_version + (JNIEnv *, jobject); + +/* + * Class: com_zerotierone_sdk_Node + * Method: peers + * Signature: (J)Ljava/util/ArrayList; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_peers + (JNIEnv *, jobject, jlong); + +/* + * Class: com_zerotierone_sdk_Node + * Method: networks + * Signature: (J)Ljava/util/ArrayList; + */ +JNIEXPORT jobject JNICALL Java_com_zerotierone_sdk_Node_networks + (JNIEnv *, jobject, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/src/com/zerotierone/sdk/DataStoreGetListener.java b/java/src/com/zerotierone/sdk/DataStoreGetListener.java new file mode 100644 index 00000000..eaf35f18 --- /dev/null +++ b/java/src/com/zerotierone/sdk/DataStoreGetListener.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.zerotierone.sdk; + +import java.nio.ByteBuffer; + +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/java/src/com/zerotierone/sdk/DataStorePutListener.java b/java/src/com/zerotierone/sdk/DataStorePutListener.java new file mode 100644 index 00000000..26ab1928 --- /dev/null +++ b/java/src/com/zerotierone/sdk/DataStorePutListener.java @@ -0,0 +1,61 @@ +/* + * 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.zerotierone.sdk; + +import java.nio.ByteBuffer; + +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/java/src/com/zerotierone/sdk/Event.java b/java/src/com/zerotierone/sdk/Event.java new file mode 100644 index 00000000..382fbf48 --- /dev/null +++ b/java/src/com/zerotierone/sdk/Event.java @@ -0,0 +1,116 @@ +/* + * 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.zerotierone.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 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, + + /** + * A more recent version was observed on the network + * + * <p>Right now this is only triggered if a hub or supernode reports a + * more recent version, and only once. It can be used to trigger a + * software update check.</p> + * + * <p>Meta-data: {@link Version}, more recent version number</p> + */ + EVENT_SAW_MORE_RECENT_VERSION, + + /** + * A packet failed authentication + * + * <p>Meta-data: {@link InetAddress} containing origin address of packet</p> + */ + EVENT_AUTHENTICATION_FAILURE, + + /** + * A received packet was not valid + * + * <p>Meta-data: {@link InetAddress} containing origin address of packet</p> + */ + EVENT_INVALID_PACKET, + + /** + * 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/java/src/com/zerotierone/sdk/EventListener.java b/java/src/com/zerotierone/sdk/EventListener.java new file mode 100644 index 00000000..dedd271a --- /dev/null +++ b/java/src/com/zerotierone/sdk/EventListener.java @@ -0,0 +1,67 @@ +/* + * 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.zerotierone.sdk; + +import java.net.InetAddress; +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); + + /** + * Callback for network error events: {@link Event.EVENT_AUTHENTICATION_FAILUER}, {link Event.EVENT_INVALID_PACKET} + * + * @param event {@link Event} enum + * @param source {@link InetAddress} containing the origin address of the packet + */ + public void onNetworkError(Event event, InetAddress source); + + /** + * Callback when the node detects that it's out of date. + * + * @param newVersion {@link Version} object with the latest version of ZeroTier One + */ + public void onOutOfDate(Version newVersion); + + /** + * 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/java/src/com/zerotierone/sdk/MulticastGroup.java b/java/src/com/zerotierone/sdk/MulticastGroup.java new file mode 100644 index 00000000..aa75ab85 --- /dev/null +++ b/java/src/com/zerotierone/sdk/MulticastGroup.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.zerotierone.sdk; + + +public final class MulticastGroup { + private MulticastGroup() {} + + private long mac; + private long 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/java/src/com/zerotierone/sdk/Node.java b/java/src/com/zerotierone/sdk/Node.java new file mode 100644 index 00000000..318f0701 --- /dev/null +++ b/java/src/com/zerotierone/sdk/Node.java @@ -0,0 +1,416 @@ +/* + * 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.zerotierone.sdk; + +import java.nio.ByteBuffer; +import java.lang.Long; +import java.net.InetAddress; +import java.util.ArrayList; + +/** + * A ZeroTier One node + */ +public class Node { + static { + System.loadLibrary("ZeroTierOneJNI"); + } + + 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) + { + 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 + } + } + + /** + * 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 linkDesperation Link desperation metric for link or protocol over which packet arrived + * @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, + InetAddress remoteAddress, + int linkDesperation, + byte[] packetData, + long[] nextBackgroundTaskDeadline) { + return processWirePacket( + nodeId, now, remoteAddress, linkDesperation, 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 ArrayList<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 ArrayList<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, + InetAddress remoteAddress, + int linkDesperation, + 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 ArrayList<Peer> peers(long nodeId); + + private native ArrayList<VirtualNetworkConfig> networks(long nodeId); +}
\ No newline at end of file diff --git a/java/src/com/zerotierone/sdk/NodeStatus.java b/java/src/com/zerotierone/sdk/NodeStatus.java new file mode 100644 index 00000000..163e3056 --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/PacketSender.java b/java/src/com/zerotierone/sdk/PacketSender.java new file mode 100644 index 00000000..0c1cd83e --- /dev/null +++ b/java/src/com/zerotierone/sdk/PacketSender.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.zerotierone.sdk; + +import java.net.InetAddress; + + +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 addr {@link InetAddress} to send to + * @param linkDesperation + * @param packetData data to send + * @return 0 on success, any error code on failure. + */ + public int onSendPacketRequested( + InetAddress addr, + int linkDesperation, + byte[] packetData); +} diff --git a/java/src/com/zerotierone/sdk/Peer.java b/java/src/com/zerotierone/sdk/Peer.java new file mode 100644 index 00000000..f01755ba --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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 ArrayList<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 ArrayList<PeerPhysicalPath> paths() { + return paths; + } +}
\ No newline at end of file diff --git a/java/src/com/zerotierone/sdk/PeerPhysicalPath.java b/java/src/com/zerotierone/sdk/PeerPhysicalPath.java new file mode 100644 index 00000000..17b0ff8b --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.sdk; + +import java.net.InetAddress; + +/** + * Physical network path to a peer + */ +public final class PeerPhysicalPath { + private InetAddress address; + private long lastSend; + private long lastReceive; + private boolean fixed; + private boolean active; + private boolean preferred; + + private PeerPhysicalPath() {} + + /** + * Address of endpoint + */ + public final InetAddress 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/java/src/com/zerotierone/sdk/PeerRole.java b/java/src/com/zerotierone/sdk/PeerRole.java new file mode 100644 index 00000000..7f507f6d --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.sdk; + +public enum PeerRole { + /** + * An ordinary node + */ + PEER_ROLE_LEAF, + + /** + * Locally federated hub + */ + PEER_ROLE_HUB, + + /** + * planetary supernode + */ + PEER_ROLE_SUPERNODE +}
\ No newline at end of file diff --git a/java/src/com/zerotierone/sdk/ResultCode.java b/java/src/com/zerotierone/sdk/ResultCode.java new file mode 100644 index 00000000..bb73f68c --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/Version.java b/java/src/com/zerotierone/sdk/Version.java new file mode 100644 index 00000000..14e9f152 --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/VirtualNetworkConfig.java b/java/src/com/zerotierone/sdk/VirtualNetworkConfig.java new file mode 100644 index 00000000..037fd802 --- /dev/null +++ b/java/src/com/zerotierone/sdk/VirtualNetworkConfig.java @@ -0,0 +1,170 @@ +/* + * 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.zerotierone.sdk; + +import java.lang.String; +import java.util.ArrayList; +import java.net.InetAddress; + +public final class VirtualNetworkConfig { + public static final int MAX_MULTICAST_SUBSCRIPTIONS = 4096; + public static final int ZT1_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 ArrayList<MulticastGroup> multicastSubscriptions; + private ArrayList<InetAddress> assignedAddresses; + + private VirtualNetworkConfig() { + + } + + /** + * 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 ArrayList<MulticastGroup> multicastSubscriptions() { + return multicastSubscriptions; + } + + /** + * ZeroTier-assigned addresses (in {@link java.net.InetAddress} 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 ArrayList<InetAddress> assignedAddresses() { + return assignedAddresses; + } +} diff --git a/java/src/com/zerotierone/sdk/VirtualNetworkConfigListener.java b/java/src/com/zerotierone/sdk/VirtualNetworkConfigListener.java new file mode 100644 index 00000000..2a26d79d --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/VirtualNetworkConfigOperation.java b/java/src/com/zerotierone/sdk/VirtualNetworkConfigOperation.java new file mode 100644 index 00000000..989961b3 --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/VirtualNetworkFrameListener.java b/java/src/com/zerotierone/sdk/VirtualNetworkFrameListener.java new file mode 100644 index 00000000..0b85cab1 --- /dev/null +++ b/java/src/com/zerotierone/sdk/VirtualNetworkFrameListener.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.zerotierone.sdk; + +import java.nio.ByteBuffer; + +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 + */ + void onVirtualNetworkFrame( + long nwid, + long srcMac, + long destMac, + long etherType, + long vlanId, + byte[] frameData); +} diff --git a/java/src/com/zerotierone/sdk/VirtualNetworkStatus.java b/java/src/com/zerotierone/sdk/VirtualNetworkStatus.java new file mode 100644 index 00000000..78df06b9 --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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/java/src/com/zerotierone/sdk/VirtualNetworkType.java b/java/src/com/zerotierone/sdk/VirtualNetworkType.java new file mode 100644 index 00000000..adc2eaff --- /dev/null +++ b/java/src/com/zerotierone/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.zerotierone.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 +} |