mirror of
https://github.com/NAStools/zerotierone.git
synced 2025-08-14 02:28:07 -07:00
Imported Upstream version 1.1.14
This commit is contained in:
commit
bb232b9d52
557 changed files with 115164 additions and 0 deletions
91
java/CMakeLists.txt
Normal file
91
java/CMakeLists.txt
Normal file
|
@ -0,0 +1,91 @@
|
|||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
project(ZeroTierOneJNI)
|
||||
|
||||
find_package(Java COMPONENTS Development)
|
||||
message("JAVA_HOME: $ENV{JAVA_HOME}")
|
||||
|
||||
if(WIN32)
|
||||
set(Java_INCLUDE_DIRS $ENV{JAVA_HOME}/include)
|
||||
endif()
|
||||
|
||||
if(APPLE)
|
||||
set(Java_INCLUDE_DIRS "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/JavaVM.framework/Headers")
|
||||
endif()
|
||||
|
||||
message("Java Include Dirs: ${Java_INCLUDE_DIRS}")
|
||||
|
||||
if(WIN32)
|
||||
add_definitions(-DNOMINMAX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W3 /MP")
|
||||
endif()
|
||||
|
||||
if(APPLE)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -arch i386 -arch x86_64 -Wall -O3 -flto -fPIE -fvectorize -fstack-protector -mmacosx-version-min=10.7 -Wno-unused-private-field")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS} -fno-rtti")
|
||||
endif()
|
||||
|
||||
set(src_files
|
||||
../ext/lz4/lz4.c
|
||||
../ext/json-parser/json.c
|
||||
../ext/http-parser/http_parser.c
|
||||
../node/C25519.cpp
|
||||
../node/CertificateOfMembership.cpp
|
||||
../node/Defaults.cpp
|
||||
../node/Dictionary.cpp
|
||||
../node/Identity.cpp
|
||||
../node/IncomingPacket.cpp
|
||||
../node/InetAddress.cpp
|
||||
../node/Multicaster.cpp
|
||||
../node/Network.cpp
|
||||
../node/NetworkConfig.cpp
|
||||
../node/Node.cpp
|
||||
../node/OutboundMulticast.cpp
|
||||
../node/Packet.cpp
|
||||
../node/Peer.cpp
|
||||
../node/Poly1305.cpp
|
||||
../node/Salsa20.cpp
|
||||
../node/SelfAwareness.cpp
|
||||
../node/SHA512.cpp
|
||||
../node/Switch.cpp
|
||||
../node/Topology.cpp
|
||||
../node/Utils.cpp
|
||||
../osdep/Http.cpp
|
||||
../osdep/OSUtils.cpp
|
||||
jni/com_zerotierone_sdk_Node.cpp
|
||||
jni/ZT_jniutils.cpp
|
||||
jni/ZT_jnicache.cpp
|
||||
)
|
||||
|
||||
set(include_dirs
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../include/
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../node/
|
||||
${Java_INCLUDE_DIRS})
|
||||
|
||||
if(WIN32)
|
||||
set(include_dirs
|
||||
${include_dirs}
|
||||
${Java_INCLUDE_DIRS}/win32)
|
||||
endif()
|
||||
|
||||
include_directories(
|
||||
${include_dirs}
|
||||
)
|
||||
|
||||
add_library(${PROJECT_NAME} SHARED ${src_files})
|
||||
|
||||
if(APPLE)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".jnilib")
|
||||
endif()
|
||||
|
||||
set(link_libs )
|
||||
|
||||
if(WIN32)
|
||||
set(link_libs
|
||||
wsock32
|
||||
ws2_32
|
||||
|
||||
)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ${link_libs})
|
17
java/README.md
Normal file
17
java/README.md
Normal file
|
@ -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)
|
118
java/build.xml
Normal file
118
java/build.xml
Normal file
|
@ -0,0 +1,118 @@
|
|||
<project default="build_jar" name="ZeroTierOneSDK" basedir=".">
|
||||
<property environment="env"/>
|
||||
|
||||
<condition property="isWindows">
|
||||
<os family="windows"/>
|
||||
</condition>
|
||||
|
||||
<condition property="isMac">
|
||||
<os family="mac"/>
|
||||
</condition>
|
||||
|
||||
<target name="clean_ant">
|
||||
<delete dir="bin" failonerror="false"/>
|
||||
<delete dir="classes" failonerror="false"/>
|
||||
<delete dir="build_win32" failonerror="false"/>
|
||||
<delete dir="build_win64" failonerror="false"/>
|
||||
<delete dir="mac32_64" failonerror="false"/>
|
||||
<delete dir="libs" failonerror="false"/>
|
||||
<delete dir="obj" failonerror="false"/>
|
||||
</target>
|
||||
|
||||
<target name="build_java">
|
||||
<echo message="os.name = ${os.name}"/>
|
||||
<echo message="os.arch = ${os.arch}"/>
|
||||
<echo message="ant.java.version = ${ant.java.version}"/>
|
||||
<echo message="java.version = ${java.version}"/>
|
||||
<echo message="ndk.loc = ${env.NDK_BUILD_LOC}"/>
|
||||
<echo message="sdk.loc = ${env.ANDROID_PLATFORM}"/>
|
||||
<echo message="user.dir = ${user.dir}"/>
|
||||
<echo message="zt1.dir = ${env.ZT}"/>
|
||||
<mkdir dir="bin"/>
|
||||
<mkdir dir="classes"/>
|
||||
<javac srcdir="src"
|
||||
destdir="classes"
|
||||
source="1.7"
|
||||
target="1.7"
|
||||
classpath="${env.ANDROID_PLATFORM}/android.jar"
|
||||
includeantruntime="false"/>
|
||||
</target>
|
||||
|
||||
<target name="build_android">
|
||||
<exec dir="jni" executable="${env.NDK_BUILD_LOC}" failonerror="true">
|
||||
<arg value="ZT1=${env.ZT}"/>
|
||||
<arg value="V=1"/>
|
||||
<!-- <arg value="NDK_DEBUG=1"/> -->
|
||||
</exec>
|
||||
<copy file="libs/arm64-v8a/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/arm64-v8a/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/armeabi/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/armeabi/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/armeabi-v7a/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/armeabi-v7a/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/mips/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/mips/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/mips64/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/mips64/libZeroTierOne.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/x86/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/x86/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
<copy file="libs/x86_64/libZeroTierOneJNI.so"
|
||||
tofile="classes/lib/x86_64/libZeroTierOneJNI.so"
|
||||
overwrite="true"/>
|
||||
</target>
|
||||
|
||||
|
||||
<target name="windows" if="isWindows">
|
||||
<mkdir dir="build_win32"/>
|
||||
<exec dir="build_win32/" executable="cmake" failonerror="true">
|
||||
<arg line=".. -G"Visual Studio 11 2012" -DCMAKE_BUILD_TYPE=Release"/>
|
||||
</exec>
|
||||
<exec dir="build_win32/" executable="cmake" failonerror="true">
|
||||
<arg line="--build . --config Release"/>
|
||||
</exec>
|
||||
<copy file="build_win32/Release/ZeroTierOneJNI.dll"
|
||||
tofile="classes/lib/ZeroTierOneJNI_win32.dll"
|
||||
overwrite="true"/>
|
||||
|
||||
<mkdir dir="build_win64"/>
|
||||
<exec dir="build_win64/" executable="cmake" failonerror="true">
|
||||
<arg line=".. -G"Visual Studio 11 2012 Win64" -DCMAKE_BUILD_TYPE=Release"/>
|
||||
</exec>
|
||||
<exec dir="build_win64/" executable="cmake" failonerror="true">
|
||||
<arg line="--build . --config Release"/>
|
||||
</exec>
|
||||
<copy file="build_win64/Release/ZeroTierOneJNI.dll"
|
||||
tofile="classes/lib/ZeroTierOneJNI_win64.dll"
|
||||
overwrite="true"/>
|
||||
</target>
|
||||
|
||||
<target name="mac" if="isMac">
|
||||
<mkdir dir="mac32_64"/>
|
||||
<exec dir="mac32_64/" executable="cmake" failonerror="true">
|
||||
<arg line=".. -DCMAKE_BUILD_TYPE=Release"/>
|
||||
</exec>
|
||||
<exec dir="mac32_64/" executable="cmake" failonerror="true">
|
||||
<arg line="--build . --config Release"/>
|
||||
</exec>
|
||||
<copy file="mac32_64/libZeroTierOneJNI.jnilib"
|
||||
tofile="classes/lib/libZeroTierOneJNI.jnilib"
|
||||
overwrite="true"/>
|
||||
</target>
|
||||
|
||||
<target name="build_jar" depends="build_java,build_android,windows,mac">
|
||||
<jar destfile="bin/ZeroTierOneSDK.jar" basedir="classes"/>
|
||||
</target>
|
||||
|
||||
<target name="docs">
|
||||
<echo message="Generating Javadocs"/>
|
||||
<mkdir dir="doc/"/>
|
||||
<javadoc sourcepath="src/"
|
||||
destdir="doc/"/>
|
||||
</target>
|
||||
</project>
|
46
java/jni/Android.mk
Normal file
46
java/jni/Android.mk
Normal file
|
@ -0,0 +1,46 @@
|
|||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE := ZeroTierOneJNI
|
||||
LOCAL_C_INCLUDES := $(ZT1)/include
|
||||
LOCAL_C_INCLUDES += $(ZT1)/node
|
||||
LOCAL_LDLIBS := -llog
|
||||
# LOCAL_CFLAGS := -g
|
||||
|
||||
# ZeroTierOne SDK source files
|
||||
LOCAL_SRC_FILES := \
|
||||
$(ZT1)/ext/lz4/lz4.c \
|
||||
$(ZT1)/ext/json-parser/json.c \
|
||||
$(ZT1)/ext/http-parser/http_parser.c \
|
||||
$(ZT1)/node/C25519.cpp \
|
||||
$(ZT1)/node/CertificateOfMembership.cpp \
|
||||
$(ZT1)/node/DeferredPackets.cpp \
|
||||
$(ZT1)/node/Identity.cpp \
|
||||
$(ZT1)/node/IncomingPacket.cpp \
|
||||
$(ZT1)/node/InetAddress.cpp \
|
||||
$(ZT1)/node/Multicaster.cpp \
|
||||
$(ZT1)/node/Network.cpp \
|
||||
$(ZT1)/node/NetworkConfig.cpp \
|
||||
$(ZT1)/node/Node.cpp \
|
||||
$(ZT1)/node/OutboundMulticast.cpp \
|
||||
$(ZT1)/node/Packet.cpp \
|
||||
$(ZT1)/node/Path.cpp \
|
||||
$(ZT1)/node/Peer.cpp \
|
||||
$(ZT1)/node/Poly1305.cpp \
|
||||
$(ZT1)/node/Salsa20.cpp \
|
||||
$(ZT1)/node/SelfAwareness.cpp \
|
||||
$(ZT1)/node/SHA512.cpp \
|
||||
$(ZT1)/node/Switch.cpp \
|
||||
$(ZT1)/node/Topology.cpp \
|
||||
$(ZT1)/node/Utils.cpp \
|
||||
$(ZT1)/osdep/Http.cpp \
|
||||
$(ZT1)/osdep/OSUtils.cpp
|
||||
|
||||
# JNI Files
|
||||
LOCAL_SRC_FILES += \
|
||||
com_zerotierone_sdk_Node.cpp \
|
||||
ZT_jniutils.cpp \
|
||||
ZT_jnilookup.cpp
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
5
java/jni/Application.mk
Normal file
5
java/jni/Application.mk
Normal file
|
@ -0,0 +1,5 @@
|
|||
# NDK_TOOLCHAIN_VERSION := clang3.5
|
||||
APP_STL := gnustl_static
|
||||
APP_CPPFLAGS := -O3 -fPIC -fPIE -Wall -fstack-protector -fexceptions -fno-strict-aliasing -Wno-deprecated-register -DZT_NO_TYPE_PUNNING=1
|
||||
APP_PLATFORM := android-14
|
||||
APP_ABI := all
|
158
java/jni/ZT_jnilookup.cpp
Normal file
158
java/jni/ZT_jnilookup.cpp
Normal file
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include "ZT_jnilookup.h"
|
||||
#include "ZT_jniutils.h"
|
||||
|
||||
JniLookup::JniLookup()
|
||||
: m_jvm(NULL)
|
||||
{
|
||||
LOGV("JNI Cache Created");
|
||||
}
|
||||
|
||||
JniLookup::JniLookup(JavaVM *jvm)
|
||||
: m_jvm(jvm)
|
||||
{
|
||||
LOGV("JNI Cache Created");
|
||||
}
|
||||
|
||||
JniLookup::~JniLookup()
|
||||
{
|
||||
LOGV("JNI Cache Destroyed");
|
||||
}
|
||||
|
||||
|
||||
void JniLookup::setJavaVM(JavaVM *jvm)
|
||||
{
|
||||
LOGV("Assigned JVM to object");
|
||||
m_jvm = jvm;
|
||||
}
|
||||
|
||||
|
||||
jclass JniLookup::findClass(const std::string &name)
|
||||
{
|
||||
if(!m_jvm)
|
||||
return NULL;
|
||||
|
||||
// get the class from the JVM
|
||||
JNIEnv *env = NULL;
|
||||
if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
LOGE("Error retreiving JNI Environment");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jclass cls = env->FindClass(name.c_str());
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
LOGE("Error finding class: %s", name.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cls;
|
||||
}
|
||||
|
||||
|
||||
jmethodID JniLookup::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
|
||||
{
|
||||
if(!m_jvm)
|
||||
return NULL;
|
||||
|
||||
JNIEnv *env = NULL;
|
||||
if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str());
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mid;
|
||||
}
|
||||
|
||||
jmethodID JniLookup::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
|
||||
{
|
||||
if(!m_jvm)
|
||||
return NULL;
|
||||
|
||||
JNIEnv *env = NULL;
|
||||
if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str());
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mid;
|
||||
}
|
||||
|
||||
jfieldID JniLookup::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
|
||||
{
|
||||
if(!m_jvm)
|
||||
return NULL;
|
||||
|
||||
JNIEnv *env = NULL;
|
||||
if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str());
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fid;
|
||||
}
|
||||
|
||||
jfieldID JniLookup::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
|
||||
{
|
||||
if(!m_jvm)
|
||||
return NULL;
|
||||
|
||||
JNIEnv *env = NULL;
|
||||
if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str());
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fid;
|
||||
}
|
54
java/jni/ZT_jnilookup.h
Normal file
54
java/jni/ZT_jnilookup.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_JNILOOKUP_H_
|
||||
#define ZT_JNILOOKUP_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
class JniLookup {
|
||||
public:
|
||||
JniLookup();
|
||||
JniLookup(JavaVM *jvm);
|
||||
~JniLookup();
|
||||
|
||||
void setJavaVM(JavaVM *jvm);
|
||||
|
||||
jclass findClass(const std::string &name);
|
||||
jmethodID findMethod(jclass cls, const std::string &methodName, const std::string &methodSig);
|
||||
jmethodID findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig);
|
||||
jfieldID findField(jclass cls, const std::string &fieldName, const std::string &typeStr);
|
||||
jfieldID findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr);
|
||||
private:
|
||||
JavaVM *m_jvm;
|
||||
};
|
||||
|
||||
#endif
|
836
java/jni/ZT_jniutils.cpp
Normal file
836
java/jni/ZT_jniutils.cpp
Normal file
|
@ -0,0 +1,836 @@
|
|||
#include "ZT_jniutils.h"
|
||||
#include "ZT_jnilookup.h"
|
||||
#include <string>
|
||||
#include <assert.h>
|
||||
|
||||
extern JniLookup lookup;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
|
||||
{
|
||||
jclass resultClass = NULL;
|
||||
|
||||
jobject resultObject = NULL;
|
||||
|
||||
resultClass = lookup.findClass("com/zerotier/sdk/ResultCode");
|
||||
if(resultClass == NULL)
|
||||
{
|
||||
LOGE("Couldnt find ResultCode class");
|
||||
return NULL; // exception thrown
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(code)
|
||||
{
|
||||
case ZT_RESULT_OK:
|
||||
LOGV("ZT_RESULT_OK");
|
||||
fieldName = "RESULT_OK";
|
||||
break;
|
||||
case ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY:
|
||||
LOGV("ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY");
|
||||
fieldName = "RESULT_FATAL_ERROR_OUT_OF_MEMORY";
|
||||
break;
|
||||
case ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED:
|
||||
LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
|
||||
fieldName = "RESULT_FATAL_ERROR_DATA_STORE_FAILED";
|
||||
break;
|
||||
case ZT_RESULT_ERROR_NETWORK_NOT_FOUND:
|
||||
LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
|
||||
fieldName = "RESULT_ERROR_NETWORK_NOT_FOUND";
|
||||
break;
|
||||
case ZT_RESULT_FATAL_ERROR_INTERNAL:
|
||||
default:
|
||||
LOGV("RESULT_FATAL_ERROR_DATA_STORE_FAILED");
|
||||
fieldName = "RESULT_FATAL_ERROR_INTERNAL";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(resultClass, fieldName.c_str(), "Lcom/zerotier/sdk/ResultCode;");
|
||||
if(env->ExceptionCheck() || enumField == NULL)
|
||||
{
|
||||
LOGE("Error on FindStaticField");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
resultObject = env->GetStaticObjectField(resultClass, enumField);
|
||||
if(env->ExceptionCheck() || resultObject == NULL)
|
||||
{
|
||||
LOGE("Error on GetStaticObjectField");
|
||||
}
|
||||
return resultObject;
|
||||
}
|
||||
|
||||
|
||||
jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status)
|
||||
{
|
||||
jobject statusObject = NULL;
|
||||
|
||||
jclass statusClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkStatus");
|
||||
if(statusClass == NULL)
|
||||
{
|
||||
return NULL; // exception thrown
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(status)
|
||||
{
|
||||
case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION:
|
||||
fieldName = "NETWORK_STATUS_REQUESTING_CONFIGURATION";
|
||||
break;
|
||||
case ZT_NETWORK_STATUS_OK:
|
||||
fieldName = "NETWORK_STATUS_OK";
|
||||
break;
|
||||
case ZT_NETWORK_STATUS_ACCESS_DENIED:
|
||||
fieldName = "NETWORK_STATUS_ACCESS_DENIED";
|
||||
break;
|
||||
case ZT_NETWORK_STATUS_NOT_FOUND:
|
||||
fieldName = "NETWORK_STATUS_NOT_FOUND";
|
||||
break;
|
||||
case ZT_NETWORK_STATUS_PORT_ERROR:
|
||||
fieldName = "NETWORK_STATUS_PORT_ERROR";
|
||||
break;
|
||||
case ZT_NETWORK_STATUS_CLIENT_TOO_OLD:
|
||||
fieldName = "NETWORK_STATUS_CLIENT_TOO_OLD";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(statusClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkStatus;");
|
||||
|
||||
statusObject = env->GetStaticObjectField(statusClass, enumField);
|
||||
|
||||
return statusObject;
|
||||
}
|
||||
|
||||
jobject createEvent(JNIEnv *env, ZT_Event event)
|
||||
{
|
||||
jclass eventClass = NULL;
|
||||
jobject eventObject = NULL;
|
||||
|
||||
eventClass = lookup.findClass("com/zerotier/sdk/Event");
|
||||
if(eventClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(event)
|
||||
{
|
||||
case ZT_EVENT_UP:
|
||||
fieldName = "EVENT_UP";
|
||||
break;
|
||||
case ZT_EVENT_OFFLINE:
|
||||
fieldName = "EVENT_OFFLINE";
|
||||
break;
|
||||
case ZT_EVENT_ONLINE:
|
||||
fieldName = "EVENT_ONLINE";
|
||||
break;
|
||||
case ZT_EVENT_DOWN:
|
||||
fieldName = "EVENT_DOWN";
|
||||
break;
|
||||
case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
|
||||
fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION";
|
||||
break;
|
||||
case ZT_EVENT_TRACE:
|
||||
fieldName = "EVENT_TRACE";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(eventClass, fieldName.c_str(), "Lcom/zerotier/sdk/Event;");
|
||||
|
||||
eventObject = env->GetStaticObjectField(eventClass, enumField);
|
||||
|
||||
return eventObject;
|
||||
}
|
||||
|
||||
jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
|
||||
{
|
||||
jclass peerRoleClass = NULL;
|
||||
jobject peerRoleObject = NULL;
|
||||
|
||||
peerRoleClass = lookup.findClass("com/zerotier/sdk/PeerRole");
|
||||
if(peerRoleClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(role)
|
||||
{
|
||||
case ZT_PEER_ROLE_LEAF:
|
||||
fieldName = "PEER_ROLE_LEAF";
|
||||
break;
|
||||
case ZT_PEER_ROLE_RELAY:
|
||||
fieldName = "PEER_ROLE_RELAY";
|
||||
break;
|
||||
case ZT_PEER_ROLE_ROOT:
|
||||
fieldName = "PEER_ROLE_ROOTS";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(peerRoleClass, fieldName.c_str(), "Lcom/zerotier/sdk/PeerRole;");
|
||||
|
||||
peerRoleObject = env->GetStaticObjectField(peerRoleClass, enumField);
|
||||
|
||||
return peerRoleObject;
|
||||
}
|
||||
|
||||
jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type)
|
||||
{
|
||||
jclass vntypeClass = NULL;
|
||||
jobject vntypeObject = NULL;
|
||||
|
||||
vntypeClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkType");
|
||||
if(env->ExceptionCheck() || vntypeClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(type)
|
||||
{
|
||||
case ZT_NETWORK_TYPE_PRIVATE:
|
||||
fieldName = "NETWORK_TYPE_PRIVATE";
|
||||
break;
|
||||
case ZT_NETWORK_TYPE_PUBLIC:
|
||||
fieldName = "NETWORK_TYPE_PUBLIC";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(vntypeClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkType;");
|
||||
vntypeObject = env->GetStaticObjectField(vntypeClass, enumField);
|
||||
return vntypeObject;
|
||||
}
|
||||
|
||||
jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op)
|
||||
{
|
||||
jclass vnetConfigOpClass = NULL;
|
||||
jobject vnetConfigOpObject = NULL;
|
||||
|
||||
vnetConfigOpClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfigOperation");
|
||||
if(env->ExceptionCheck() || vnetConfigOpClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::string fieldName;
|
||||
switch(op)
|
||||
{
|
||||
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP:
|
||||
fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_UP";
|
||||
break;
|
||||
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE:
|
||||
fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE";
|
||||
break;
|
||||
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN:
|
||||
fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN";
|
||||
break;
|
||||
case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY:
|
||||
fieldName = "VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY";
|
||||
break;
|
||||
}
|
||||
|
||||
jfieldID enumField = lookup.findStaticField(vnetConfigOpClass, fieldName.c_str(), "Lcom/zerotier/sdk/VirtualNetworkConfigOperation;");
|
||||
vnetConfigOpObject = env->GetStaticObjectField(vnetConfigOpClass, enumField);
|
||||
return vnetConfigOpObject;
|
||||
}
|
||||
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
LOGV("newInetAddress");
|
||||
jclass inetAddressClass = NULL;
|
||||
jmethodID inetAddress_getByAddress = NULL;
|
||||
|
||||
inetAddressClass = lookup.findClass("java/net/InetAddress");
|
||||
if(env->ExceptionCheck() || inetAddressClass == NULL)
|
||||
{
|
||||
LOGE("Error finding InetAddress class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inetAddress_getByAddress = lookup.findStaticMethod(
|
||||
inetAddressClass, "getByAddress", "([B)Ljava/net/InetAddress;");
|
||||
if(env->ExceptionCheck() || inetAddress_getByAddress == NULL)
|
||||
{
|
||||
LOGE("Erorr finding getByAddress() static method");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject inetAddressObj = NULL;
|
||||
switch(addr.ss_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
{
|
||||
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
||||
jbyteArray buff = env->NewByteArray(16);
|
||||
if(buff == NULL)
|
||||
{
|
||||
LOGE("Error creating IPV6 byte array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(buff, 0, 16, (jbyte*)ipv6->sin6_addr.s6_addr);
|
||||
inetAddressObj = env->CallStaticObjectMethod(
|
||||
inetAddressClass, inetAddress_getByAddress, buff);
|
||||
}
|
||||
break;
|
||||
case AF_INET:
|
||||
{
|
||||
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
||||
jbyteArray buff = env->NewByteArray(4);
|
||||
if(buff == NULL)
|
||||
{
|
||||
LOGE("Error creating IPV4 byte array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(buff, 0, 4, (jbyte*)&ipv4->sin_addr);
|
||||
inetAddressObj = env->CallStaticObjectMethod(
|
||||
inetAddressClass, inetAddress_getByAddress, buff);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(env->ExceptionCheck() || inetAddressObj == NULL) {
|
||||
LOGE("Error creating InetAddress object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return inetAddressObj;
|
||||
}
|
||||
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
|
||||
{
|
||||
LOGV("newInetSocketAddress Called");
|
||||
jclass inetSocketAddressClass = NULL;
|
||||
jmethodID inetSocketAddress_constructor = NULL;
|
||||
|
||||
inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress");
|
||||
if(env->ExceptionCheck() || inetSocketAddressClass == NULL)
|
||||
{
|
||||
LOGE("Error finding InetSocketAddress Class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject inetAddressObject = newInetAddress(env, addr);
|
||||
|
||||
if(env->ExceptionCheck() || inetAddressObject == NULL)
|
||||
{
|
||||
LOGE("Error creating new inet address");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inetSocketAddress_constructor = lookup.findMethod(
|
||||
inetSocketAddressClass, "<init>", "(Ljava/net/InetAddress;I)V");
|
||||
if(env->ExceptionCheck() || inetSocketAddress_constructor == NULL)
|
||||
{
|
||||
LOGE("Error finding InetSocketAddress constructor");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int port = 0;
|
||||
switch(addr.ss_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
{
|
||||
LOGV("IPV6 Address");
|
||||
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
|
||||
port = ntohs(ipv6->sin6_port);
|
||||
LOGV("Port %d", port);
|
||||
}
|
||||
break;
|
||||
case AF_INET:
|
||||
{
|
||||
LOGV("IPV4 Address");
|
||||
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
|
||||
port = ntohs(ipv4->sin_port);
|
||||
LOGV("Port: %d", port);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
LOGE("ERROR: addr.ss_family is not set or unknown");
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
jobject inetSocketAddressObject = env->NewObject(inetSocketAddressClass, inetSocketAddress_constructor, inetAddressObject, port);
|
||||
if(env->ExceptionCheck() || inetSocketAddressObject == NULL) {
|
||||
LOGE("Error creating InetSocketAddress object");
|
||||
}
|
||||
return inetSocketAddressObject;
|
||||
}
|
||||
|
||||
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
|
||||
{
|
||||
LOGV("newPeerPhysicalPath Called");
|
||||
jclass pppClass = NULL;
|
||||
|
||||
jfieldID addressField = NULL;
|
||||
jfieldID lastSendField = NULL;
|
||||
jfieldID lastReceiveField = NULL;
|
||||
jfieldID activeField = NULL;
|
||||
jfieldID preferredField = NULL;
|
||||
|
||||
jmethodID ppp_constructor = NULL;
|
||||
|
||||
pppClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath");
|
||||
if(env->ExceptionCheck() || pppClass == NULL)
|
||||
{
|
||||
LOGE("Error finding PeerPhysicalPath class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
addressField = lookup.findField(pppClass, "address", "Ljava/net/InetSocketAddress;");
|
||||
if(env->ExceptionCheck() || addressField == NULL)
|
||||
{
|
||||
LOGE("Error finding address field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lastSendField = lookup.findField(pppClass, "lastSend", "J");
|
||||
if(env->ExceptionCheck() || lastSendField == NULL)
|
||||
{
|
||||
LOGE("Error finding lastSend field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lastReceiveField = lookup.findField(pppClass, "lastReceive", "J");
|
||||
if(env->ExceptionCheck() || lastReceiveField == NULL)
|
||||
{
|
||||
LOGE("Error finding lastReceive field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
activeField = lookup.findField(pppClass, "active", "Z");
|
||||
if(env->ExceptionCheck() || activeField == NULL)
|
||||
{
|
||||
LOGE("Error finding active field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
preferredField = lookup.findField(pppClass, "preferred", "Z");
|
||||
if(env->ExceptionCheck() || preferredField == NULL)
|
||||
{
|
||||
LOGE("Error finding preferred field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ppp_constructor = lookup.findMethod(pppClass, "<init>", "()V");
|
||||
if(env->ExceptionCheck() || ppp_constructor == NULL)
|
||||
{
|
||||
LOGE("Error finding PeerPhysicalPath constructor");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject pppObject = env->NewObject(pppClass, ppp_constructor);
|
||||
if(env->ExceptionCheck() || pppObject == NULL)
|
||||
{
|
||||
LOGE("Error creating PPP object");
|
||||
return NULL; // out of memory
|
||||
}
|
||||
|
||||
jobject addressObject = newInetSocketAddress(env, ppp.address);
|
||||
if(env->ExceptionCheck() || addressObject == NULL) {
|
||||
LOGE("Error creating InetSocketAddress object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetObjectField(pppObject, addressField, addressObject);
|
||||
env->SetLongField(pppObject, lastSendField, ppp.lastSend);
|
||||
env->SetLongField(pppObject, lastReceiveField, ppp.lastReceive);
|
||||
env->SetBooleanField(pppObject, activeField, ppp.active);
|
||||
env->SetBooleanField(pppObject, preferredField, ppp.preferred);
|
||||
|
||||
if(env->ExceptionCheck()) {
|
||||
LOGE("Exception assigning fields to PeerPhysicalPath object");
|
||||
}
|
||||
|
||||
return pppObject;
|
||||
}
|
||||
|
||||
jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
|
||||
{
|
||||
LOGV("newPeer called");
|
||||
|
||||
jclass peerClass = NULL;
|
||||
|
||||
jfieldID addressField = NULL;
|
||||
jfieldID lastUnicastFrameField = NULL;
|
||||
jfieldID lastMulticastFrameField = NULL;
|
||||
jfieldID versionMajorField = NULL;
|
||||
jfieldID versionMinorField = NULL;
|
||||
jfieldID versionRevField = NULL;
|
||||
jfieldID latencyField = NULL;
|
||||
jfieldID roleField = NULL;
|
||||
jfieldID pathsField = NULL;
|
||||
|
||||
jmethodID peer_constructor = NULL;
|
||||
|
||||
peerClass = lookup.findClass("com/zerotier/sdk/Peer");
|
||||
if(env->ExceptionCheck() || peerClass == NULL)
|
||||
{
|
||||
LOGE("Error finding Peer class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
addressField = lookup.findField(peerClass, "address", "J");
|
||||
if(env->ExceptionCheck() || addressField == NULL)
|
||||
{
|
||||
LOGE("Error finding address field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lastUnicastFrameField = lookup.findField(peerClass, "lastUnicastFrame", "J");
|
||||
if(env->ExceptionCheck() || lastUnicastFrameField == NULL)
|
||||
{
|
||||
LOGE("Error finding lastUnicastFrame field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lastMulticastFrameField = lookup.findField(peerClass, "lastMulticastFrame", "J");
|
||||
if(env->ExceptionCheck() || lastMulticastFrameField == NULL)
|
||||
{
|
||||
LOGE("Error finding lastMulticastFrame field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
versionMajorField = lookup.findField(peerClass, "versionMajor", "I");
|
||||
if(env->ExceptionCheck() || versionMajorField == NULL)
|
||||
{
|
||||
LOGE("Error finding versionMajor field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
versionMinorField = lookup.findField(peerClass, "versionMinor", "I");
|
||||
if(env->ExceptionCheck() || versionMinorField == NULL)
|
||||
{
|
||||
LOGE("Error finding versionMinor field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
versionRevField = lookup.findField(peerClass, "versionRev", "I");
|
||||
if(env->ExceptionCheck() || versionRevField == NULL)
|
||||
{
|
||||
LOGE("Error finding versionRev field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
latencyField = lookup.findField(peerClass, "latency", "I");
|
||||
if(env->ExceptionCheck() || latencyField == NULL)
|
||||
{
|
||||
LOGE("Error finding latency field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
roleField = lookup.findField(peerClass, "role", "Lcom/zerotier/sdk/PeerRole;");
|
||||
if(env->ExceptionCheck() || roleField == NULL)
|
||||
{
|
||||
LOGE("Error finding role field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pathsField = lookup.findField(peerClass, "paths", "[Lcom/zerotier/sdk/PeerPhysicalPath;");
|
||||
if(env->ExceptionCheck() || pathsField == NULL)
|
||||
{
|
||||
LOGE("Error finding paths field of Peer object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
peer_constructor = lookup.findMethod(peerClass, "<init>", "()V");
|
||||
if(env->ExceptionCheck() || peer_constructor == NULL)
|
||||
{
|
||||
LOGE("Error finding Peer constructor");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject peerObject = env->NewObject(peerClass, peer_constructor);
|
||||
if(env->ExceptionCheck() || peerObject == NULL)
|
||||
{
|
||||
LOGE("Error creating Peer object");
|
||||
return NULL; // out of memory
|
||||
}
|
||||
|
||||
env->SetLongField(peerObject, addressField, (jlong)peer.address);
|
||||
env->SetLongField(peerObject, lastUnicastFrameField, (jlong)peer.lastUnicastFrame);
|
||||
env->SetLongField(peerObject, lastMulticastFrameField, (jlong)peer.lastMulticastFrame);
|
||||
env->SetIntField(peerObject, versionMajorField, peer.versionMajor);
|
||||
env->SetIntField(peerObject, versionMinorField, peer.versionMinor);
|
||||
env->SetIntField(peerObject, versionRevField, peer.versionRev);
|
||||
env->SetIntField(peerObject, latencyField, peer.latency);
|
||||
env->SetObjectField(peerObject, roleField, createPeerRole(env, peer.role));
|
||||
|
||||
jclass peerPhysicalPathClass = lookup.findClass("com/zerotier/sdk/PeerPhysicalPath");
|
||||
if(env->ExceptionCheck() || peerPhysicalPathClass == NULL)
|
||||
{
|
||||
LOGE("Error finding PeerPhysicalPath class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobjectArray arrayObject = env->NewObjectArray(
|
||||
peer.pathCount, peerPhysicalPathClass, NULL);
|
||||
if(env->ExceptionCheck() || arrayObject == NULL)
|
||||
{
|
||||
LOGE("Error creating PeerPhysicalPath[] array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < peer.pathCount; ++i)
|
||||
{
|
||||
jobject path = newPeerPhysicalPath(env, peer.paths[i]);
|
||||
|
||||
env->SetObjectArrayElement(arrayObject, i, path);
|
||||
if(env->ExceptionCheck()) {
|
||||
LOGE("exception assigning PeerPhysicalPath to array");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
env->SetObjectField(peerObject, pathsField, arrayObject);
|
||||
|
||||
return peerObject;
|
||||
}
|
||||
|
||||
jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
|
||||
{
|
||||
jclass vnetConfigClass = NULL;
|
||||
jmethodID vnetConfig_constructor = NULL;
|
||||
jfieldID nwidField = NULL;
|
||||
jfieldID macField = NULL;
|
||||
jfieldID nameField = NULL;
|
||||
jfieldID statusField = NULL;
|
||||
jfieldID typeField = NULL;
|
||||
jfieldID mtuField = NULL;
|
||||
jfieldID dhcpField = NULL;
|
||||
jfieldID bridgeField = NULL;
|
||||
jfieldID broadcastEnabledField = NULL;
|
||||
jfieldID portErrorField = NULL;
|
||||
jfieldID netconfRevisionField = NULL;
|
||||
jfieldID assignedAddressesField = NULL;
|
||||
|
||||
vnetConfigClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfig");
|
||||
if(vnetConfigClass == NULL)
|
||||
{
|
||||
LOGE("Couldn't find com.zerotier.sdk.VirtualNetworkConfig");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vnetConfig_constructor = lookup.findMethod(
|
||||
vnetConfigClass, "<init>", "()V");
|
||||
if(env->ExceptionCheck() || vnetConfig_constructor == NULL)
|
||||
{
|
||||
LOGE("Couldn't find VirtualNetworkConfig Constructor");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject vnetConfigObj = env->NewObject(vnetConfigClass, vnetConfig_constructor);
|
||||
if(env->ExceptionCheck() || vnetConfigObj == NULL)
|
||||
{
|
||||
LOGE("Error creating new VirtualNetworkConfig object");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nwidField = lookup.findField(vnetConfigClass, "nwid", "J");
|
||||
if(env->ExceptionCheck() || nwidField == NULL)
|
||||
{
|
||||
LOGE("Error getting nwid field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
macField = lookup.findField(vnetConfigClass, "mac", "J");
|
||||
if(env->ExceptionCheck() || macField == NULL)
|
||||
{
|
||||
LOGE("Error getting mac field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nameField = lookup.findField(vnetConfigClass, "name", "Ljava/lang/String;");
|
||||
if(env->ExceptionCheck() || nameField == NULL)
|
||||
{
|
||||
LOGE("Error getting name field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
statusField = lookup.findField(vnetConfigClass, "status", "Lcom/zerotier/sdk/VirtualNetworkStatus;");
|
||||
if(env->ExceptionCheck() || statusField == NULL)
|
||||
{
|
||||
LOGE("Error getting status field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
typeField = lookup.findField(vnetConfigClass, "type", "Lcom/zerotier/sdk/VirtualNetworkType;");
|
||||
if(env->ExceptionCheck() || typeField == NULL)
|
||||
{
|
||||
LOGE("Error getting type field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mtuField = lookup.findField(vnetConfigClass, "mtu", "I");
|
||||
if(env->ExceptionCheck() || mtuField == NULL)
|
||||
{
|
||||
LOGE("Error getting mtu field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dhcpField = lookup.findField(vnetConfigClass, "dhcp", "Z");
|
||||
if(env->ExceptionCheck() || dhcpField == NULL)
|
||||
{
|
||||
LOGE("Error getting dhcp field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bridgeField = lookup.findField(vnetConfigClass, "bridge", "Z");
|
||||
if(env->ExceptionCheck() || bridgeField == NULL)
|
||||
{
|
||||
LOGE("Error getting bridge field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
broadcastEnabledField = lookup.findField(vnetConfigClass, "broadcastEnabled", "Z");
|
||||
if(env->ExceptionCheck() || broadcastEnabledField == NULL)
|
||||
{
|
||||
LOGE("Error getting broadcastEnabled field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
portErrorField = lookup.findField(vnetConfigClass, "portError", "I");
|
||||
if(env->ExceptionCheck() || portErrorField == NULL)
|
||||
{
|
||||
LOGE("Error getting portError field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
netconfRevisionField = lookup.findField(vnetConfigClass, "netconfRevision", "J");
|
||||
if(env->ExceptionCheck() || netconfRevisionField == NULL)
|
||||
{
|
||||
LOGE("Error getting netconfRevision field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
assignedAddressesField = lookup.findField(vnetConfigClass, "assignedAddresses", "[Ljava/net/InetSocketAddress;");
|
||||
if(env->ExceptionCheck() || assignedAddressesField == NULL)
|
||||
{
|
||||
LOGE("Error getting assignedAddresses field");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetLongField(vnetConfigObj, nwidField, vnetConfig.nwid);
|
||||
env->SetLongField(vnetConfigObj, macField, vnetConfig.mac);
|
||||
jstring nameStr = env->NewStringUTF(vnetConfig.name);
|
||||
if(env->ExceptionCheck() || nameStr == NULL)
|
||||
{
|
||||
return NULL; // out of memory
|
||||
}
|
||||
env->SetObjectField(vnetConfigObj, nameField, nameStr);
|
||||
|
||||
jobject statusObject = createVirtualNetworkStatus(env, vnetConfig.status);
|
||||
if(env->ExceptionCheck() || statusObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
env->SetObjectField(vnetConfigObj, statusField, statusObject);
|
||||
|
||||
jobject typeObject = createVirtualNetworkType(env, vnetConfig.type);
|
||||
if(env->ExceptionCheck() || typeObject == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
env->SetObjectField(vnetConfigObj, typeField, typeObject);
|
||||
|
||||
env->SetIntField(vnetConfigObj, mtuField, (int)vnetConfig.mtu);
|
||||
env->SetBooleanField(vnetConfigObj, dhcpField, vnetConfig.dhcp);
|
||||
env->SetBooleanField(vnetConfigObj, bridgeField, vnetConfig.bridge);
|
||||
env->SetBooleanField(vnetConfigObj, broadcastEnabledField, vnetConfig.broadcastEnabled);
|
||||
env->SetIntField(vnetConfigObj, portErrorField, vnetConfig.portError);
|
||||
|
||||
jclass inetSocketAddressClass = lookup.findClass("java/net/InetSocketAddress");
|
||||
if(env->ExceptionCheck() || inetSocketAddressClass == NULL)
|
||||
{
|
||||
LOGE("Error finding InetSocketAddress class");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobjectArray assignedAddrArrayObj = env->NewObjectArray(
|
||||
vnetConfig.assignedAddressCount, inetSocketAddressClass, NULL);
|
||||
if(env->ExceptionCheck() || assignedAddrArrayObj == NULL)
|
||||
{
|
||||
LOGE("Error creating InetSocketAddress[] array");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < vnetConfig.assignedAddressCount; ++i)
|
||||
{
|
||||
jobject inetAddrObj = newInetSocketAddress(env, vnetConfig.assignedAddresses[i]);
|
||||
env->SetObjectArrayElement(assignedAddrArrayObj, i, inetAddrObj);
|
||||
if(env->ExceptionCheck())
|
||||
{
|
||||
LOGE("Error assigning InetSocketAddress to array");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
env->SetObjectField(vnetConfigObj, assignedAddressesField, assignedAddrArrayObj);
|
||||
|
||||
return vnetConfigObj;
|
||||
}
|
||||
|
||||
jobject newVersion(JNIEnv *env, int major, int minor, int rev)
|
||||
{
|
||||
// create a com.zerotier.sdk.Version object
|
||||
jclass versionClass = NULL;
|
||||
jmethodID versionConstructor = NULL;
|
||||
|
||||
versionClass = lookup.findClass("com/zerotier/sdk/Version");
|
||||
if(env->ExceptionCheck() || versionClass == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
versionConstructor = lookup.findMethod(
|
||||
versionClass, "<init>", "()V");
|
||||
if(env->ExceptionCheck() || versionConstructor == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
jobject versionObj = env->NewObject(versionClass, versionConstructor);
|
||||
if(env->ExceptionCheck() || versionObj == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// copy data to Version object
|
||||
jfieldID majorField = NULL;
|
||||
jfieldID minorField = NULL;
|
||||
jfieldID revisionField = NULL;
|
||||
|
||||
majorField = lookup.findField(versionClass, "major", "I");
|
||||
if(env->ExceptionCheck() || majorField == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
minorField = lookup.findField(versionClass, "minor", "I");
|
||||
if(env->ExceptionCheck() || minorField == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
revisionField = lookup.findField(versionClass, "revision", "I");
|
||||
if(env->ExceptionCheck() || revisionField == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
env->SetIntField(versionObj, majorField, (jint)major);
|
||||
env->SetIntField(versionObj, minorField, (jint)minor);
|
||||
env->SetIntField(versionObj, revisionField, (jint)rev);
|
||||
|
||||
return versionObj;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
49
java/jni/ZT_jniutils.h
Normal file
49
java/jni/ZT_jniutils.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef ZT_jniutils_h_
|
||||
#define ZT_jniutils_h_
|
||||
#include <stdio.h>
|
||||
#include <jni.h>
|
||||
#include <ZeroTierOne.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define LOG_TAG "ZeroTierOneJNI"
|
||||
|
||||
#if __ANDROID__
|
||||
#include <android/log.h>
|
||||
#define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
|
||||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
|
||||
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
|
||||
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
|
||||
#else
|
||||
#define LOGV(...) fprintf(stdout, __VA_ARGS__)
|
||||
#define LOGI(...) fprintf(stdout, __VA_ARGS__)
|
||||
#define LOGD(...) fprintf(stdout, __VA_ARGS__)
|
||||
#define LOGE(...) fprintf(stdout, __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
jobject createResultObject(JNIEnv *env, ZT_ResultCode code);
|
||||
jobject createVirtualNetworkStatus(JNIEnv *env, ZT_VirtualNetworkStatus status);
|
||||
jobject createVirtualNetworkType(JNIEnv *env, ZT_VirtualNetworkType type);
|
||||
jobject createEvent(JNIEnv *env, ZT_Event event);
|
||||
jobject createPeerRole(JNIEnv *env, ZT_PeerRole role);
|
||||
jobject createVirtualNetworkConfigOperation(JNIEnv *env, ZT_VirtualNetworkConfigOperation op);
|
||||
|
||||
jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
jobject newInetAddress(JNIEnv *env, const sockaddr_storage &addr);
|
||||
|
||||
jobject newMulticastGroup(JNIEnv *env, const ZT_MulticastGroup &mc);
|
||||
|
||||
jobject newPeer(JNIEnv *env, const ZT_Peer &peer);
|
||||
jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp);
|
||||
|
||||
jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &config);
|
||||
|
||||
jobject newVersion(JNIEnv *env, int major, int minor, int rev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
1369
java/jni/com_zerotierone_sdk_Node.cpp
Normal file
1369
java/jni/com_zerotierone_sdk_Node.cpp
Normal file
File diff suppressed because it is too large
Load diff
133
java/jni/com_zerotierone_sdk_Node.h
Normal file
133
java/jni/com_zerotierone_sdk_Node.h
Normal file
|
@ -0,0 +1,133 @@
|
|||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class com_zerotier_sdk_Node */
|
||||
|
||||
#ifndef _Included_com_zerotierone_sdk_Node
|
||||
#define _Included_com_zerotierone_sdk_Node
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: node_init
|
||||
* Signature: (J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: node_delete
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processVirtualNetworkFrame
|
||||
* Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong, jlong, jint, jint, jbyteArray, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processWirePacket
|
||||
* Signature: (JJLjava/net/InetSockAddress;Ljava/net/InetSockAddress;[B[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket
|
||||
(JNIEnv *, jobject, jlong, jlong, jobject, jobject, jbyteArray, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: processBackgroundTasks
|
||||
* Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks
|
||||
(JNIEnv *, jobject, jlong, jlong, jlongArray);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: join
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: leave
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: multicastSubscribe
|
||||
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: multicastUnsubscribe
|
||||
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe
|
||||
(JNIEnv *, jobject, jlong, jlong, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: address
|
||||
* Signature: (J)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_sdk_Node_address
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: status
|
||||
* Signature: (J)Lcom/zerotier/sdk/NodeStatus;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: networkConfig
|
||||
* Signature: (JJ)Lcom/zerotier/sdk/VirtualNetworkConfig;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig
|
||||
(JNIEnv *, jobject, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: version
|
||||
* Signature: ()Lcom/zerotier/sdk/Version;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_version
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: peers
|
||||
* Signature: (J)[Lcom/zerotier/sdk/Peer;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: com_zerotier_sdk_Node
|
||||
* Method: networks
|
||||
* Signature: (J)[Lcom/zerotier/sdk/VirtualNetworkConfig;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_networks
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
58
java/src/com/zerotier/sdk/DataStoreGetListener.java
Normal file
58
java/src/com/zerotier/sdk/DataStoreGetListener.java
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public interface DataStoreGetListener {
|
||||
|
||||
/**
|
||||
* Function to get an object from the data store
|
||||
*
|
||||
* <p>Object names can contain forward slash (/) path separators. They will
|
||||
* never contain .. or backslash (\), so this is safe to map as a Unix-style
|
||||
* path if the underlying storage permits. For security reasons we recommend
|
||||
* returning errors if .. or \ are used.</p>
|
||||
*
|
||||
* <p>The function must return the actual number of bytes read. If the object
|
||||
* doesn't exist, it should return -1. -2 should be returned on other errors
|
||||
* such as errors accessing underlying storage.</p>
|
||||
*
|
||||
* <p>If the read doesn't fit in the buffer, the max number of bytes should be
|
||||
* read. The caller may call the function multiple times to read the whole
|
||||
* object.</p>
|
||||
*
|
||||
* @param name Name of the object in the data store
|
||||
* @param out_buffer buffer to put the object in
|
||||
* @param bufferIndex index in the object to start reading
|
||||
* @param out_objectSize long[1] to be set to the actual size of the object if it exists.
|
||||
* @return the actual number of bytes read.
|
||||
*/
|
||||
public long onDataStoreGet(
|
||||
String name,
|
||||
byte[] out_buffer,
|
||||
long bufferIndex,
|
||||
long[] out_objectSize);
|
||||
}
|
59
java/src/com/zerotier/sdk/DataStorePutListener.java
Normal file
59
java/src/com/zerotier/sdk/DataStorePutListener.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public interface DataStorePutListener {
|
||||
|
||||
/**
|
||||
* Function to store an object in the data store
|
||||
*
|
||||
* <p>If secure is true, the file should be set readable and writable only
|
||||
* to the user running ZeroTier One. What this means is platform-specific.</p>
|
||||
*
|
||||
* <p>Name semantics are the same as {@link DataStoreGetListener}. This must return
|
||||
* zero on success. You can return any OS-specific error code on failure, as these
|
||||
* may be visible in logs or error messages and might aid in debugging.</p>
|
||||
*
|
||||
* @param name Object name
|
||||
* @param buffer data to store
|
||||
* @param secure set to user read/write only.
|
||||
* @return 0 on success.
|
||||
*/
|
||||
public int onDataStorePut(
|
||||
String name,
|
||||
byte[] buffer,
|
||||
boolean secure);
|
||||
|
||||
/**
|
||||
* Function to delete an object from the data store
|
||||
*
|
||||
* @param name Object name
|
||||
* @return 0 on success.
|
||||
*/
|
||||
public int onDelete(
|
||||
String name);
|
||||
}
|
98
java/src/com/zerotier/sdk/Event.java
Normal file
98
java/src/com/zerotier/sdk/Event.java
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public enum Event {
|
||||
/**
|
||||
* Node has been initialized
|
||||
*
|
||||
* This is the first event generated, and is always sent. It may occur
|
||||
* before Node's constructor returns.
|
||||
*/
|
||||
EVENT_UP,
|
||||
|
||||
/**
|
||||
* Node is offline -- network does not seem to be reachable by any available strategy
|
||||
*/
|
||||
EVENT_OFFLINE,
|
||||
|
||||
/**
|
||||
* Node is online -- at least one upstream node appears reachable
|
||||
*
|
||||
* Meta-data: none
|
||||
*/
|
||||
EVENT_ONLINE,
|
||||
|
||||
/**
|
||||
* Node is shutting down
|
||||
*
|
||||
* <p>This is generated within Node's destructor when it is being shut down.
|
||||
* It's done for convenience, since cleaning up other state in the event
|
||||
* handler may appear more idiomatic.</p>
|
||||
*/
|
||||
EVENT_DOWN,
|
||||
|
||||
/**
|
||||
* Your identity has collided with another node's ZeroTier address
|
||||
*
|
||||
* <p>This happens if two different public keys both hash (via the algorithm
|
||||
* in Identity::generate()) to the same 40-bit ZeroTier address.</p>
|
||||
*
|
||||
* <p>This is something you should "never" see, where "never" is defined as
|
||||
* once per 2^39 new node initializations / identity creations. If you do
|
||||
* see it, you're going to see it very soon after a node is first
|
||||
* initialized.</p>
|
||||
*
|
||||
* <p>This is reported as an event rather than a return code since it's
|
||||
* detected asynchronously via error messages from authoritative nodes.</p>
|
||||
*
|
||||
* <p>If this occurs, you must shut down and delete the node, delete the
|
||||
* identity.secret record/file from the data store, and restart to generate
|
||||
* a new identity. If you don't do this, you will not be able to communicate
|
||||
* with other nodes.</p>
|
||||
*
|
||||
* <p>We'd automate this process, but we don't think silently deleting
|
||||
* private keys or changing our address without telling the calling code
|
||||
* is good form. It violates the principle of least surprise.</p>
|
||||
*
|
||||
* <p>You can technically get away with not handling this, but we recommend
|
||||
* doing so in a mature reliable application. Besides, handling this
|
||||
* condition is a good way to make sure it never arises. It's like how
|
||||
* umbrellas prevent rain and smoke detectors prevent fires. They do, right?</p>
|
||||
*/
|
||||
EVENT_FATAL_ERROR_IDENTITY_COLLISION,
|
||||
|
||||
/**
|
||||
* Trace (debugging) message
|
||||
*
|
||||
* <p>These events are only generated if this is a TRACE-enabled build.</p>
|
||||
*
|
||||
* <p>Meta-data: {@link String}, TRACE message</p>
|
||||
*/
|
||||
EVENT_TRACE
|
||||
}
|
52
java/src/com/zerotier/sdk/EventListener.java
Normal file
52
java/src/com/zerotier/sdk/EventListener.java
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.lang.String;
|
||||
|
||||
/**
|
||||
* Interface to handle callbacks for ZeroTier One events.
|
||||
*/
|
||||
public interface EventListener {
|
||||
/**
|
||||
* Callback for events with no other associated metadata
|
||||
*
|
||||
* @param event {@link Event} enum
|
||||
*/
|
||||
public void onEvent(Event event);
|
||||
|
||||
/**
|
||||
* Trace messages
|
||||
*
|
||||
* <p>These events are only generated if the underlying ZeroTierOne SDK is a TRACE-enabled build.</p>
|
||||
*
|
||||
* @param message the trace message
|
||||
*/
|
||||
public void onTrace(String message);
|
||||
}
|
93
java/src/com/zerotier/sdk/NativeUtils.java
Normal file
93
java/src/com/zerotier/sdk/NativeUtils.java
Normal file
|
@ -0,0 +1,93 @@
|
|||
package com.zerotier.sdk;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Simple library class for working with JNI (Java Native Interface)
|
||||
*
|
||||
* @see http://adamheinrich.com/2012/how-to-load-native-jni-library-from-jar
|
||||
*
|
||||
* @author Adam Heirnich <adam@adamh.cz>, http://www.adamh.cz
|
||||
*/
|
||||
public class NativeUtils {
|
||||
|
||||
/**
|
||||
* Private constructor - this class will never be instanced
|
||||
*/
|
||||
private NativeUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads library from current JAR archive
|
||||
*
|
||||
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after exiting.
|
||||
* Method uses String as filename because the pathname is "abstract", not system-dependent.
|
||||
*
|
||||
* @param filename The filename inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext
|
||||
* @throws IOException If temporary file creation or read/write operation fails
|
||||
* @throws IllegalArgumentException If source file (param path) does not exist
|
||||
* @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of {@see File#createTempFile(java.lang.String, java.lang.String)}).
|
||||
*/
|
||||
public static void loadLibraryFromJar(String path) throws IOException {
|
||||
|
||||
if (!path.startsWith("/")) {
|
||||
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
|
||||
}
|
||||
|
||||
// Obtain filename from path
|
||||
String[] parts = path.split("/");
|
||||
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
|
||||
|
||||
// Split filename to prexif and suffix (extension)
|
||||
String prefix = "";
|
||||
String suffix = null;
|
||||
if (filename != null) {
|
||||
parts = filename.split("\\.", 2);
|
||||
prefix = parts[0];
|
||||
suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-)
|
||||
}
|
||||
|
||||
// Check if the filename is okay
|
||||
if (filename == null || prefix.length() < 3) {
|
||||
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
|
||||
}
|
||||
|
||||
// Prepare temporary file
|
||||
File temp = File.createTempFile(prefix, suffix);
|
||||
temp.deleteOnExit();
|
||||
|
||||
if (!temp.exists()) {
|
||||
throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist.");
|
||||
}
|
||||
|
||||
// Prepare buffer for data copying
|
||||
byte[] buffer = new byte[1024];
|
||||
int readBytes;
|
||||
|
||||
// Open and check input stream
|
||||
InputStream is = NativeUtils.class.getResourceAsStream(path);
|
||||
if (is == null) {
|
||||
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
|
||||
}
|
||||
|
||||
// Open output stream and copy data between source file in JAR and the temporary file
|
||||
OutputStream os = new FileOutputStream(temp);
|
||||
try {
|
||||
while ((readBytes = is.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
// If read/write fails, close streams safely before throwing an exception
|
||||
os.close();
|
||||
is.close();
|
||||
}
|
||||
|
||||
// Finally, load the library
|
||||
System.load(temp.getAbsolutePath());
|
||||
}
|
||||
}
|
434
java/src/com/zerotier/sdk/Node.java
Normal file
434
java/src/com/zerotier/sdk/Node.java
Normal file
|
@ -0,0 +1,434 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A ZeroTier One node
|
||||
*/
|
||||
public class Node {
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("ZeroTierOneJNI");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
try {
|
||||
if(System.getProperty("os.name").startsWith("Windows")) {
|
||||
System.out.println("Arch: " + System.getProperty("sun.arch.data.model"));
|
||||
if(System.getProperty("sun.arch.data.model").equals("64")) {
|
||||
NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win64.dll");
|
||||
} else {
|
||||
NativeUtils.loadLibraryFromJar("/lib/ZeroTierOneJNI_win32.dll");
|
||||
}
|
||||
} else if(System.getProperty("os.name").startsWith("Mac")) {
|
||||
NativeUtils.loadLibraryFromJar("/lib/libZeroTierOneJNI.jnilib");
|
||||
} else {
|
||||
// TODO: Linux
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final String TAG = "NODE";
|
||||
|
||||
/**
|
||||
* Node ID for JNI purposes.
|
||||
* Currently set to the now value passed in at the constructor
|
||||
*
|
||||
* -1 if the node has already been closed
|
||||
*/
|
||||
private long nodeId;
|
||||
|
||||
private final DataStoreGetListener getListener;
|
||||
private final DataStorePutListener putListener;
|
||||
private final PacketSender sender;
|
||||
private final EventListener eventListener;
|
||||
private final VirtualNetworkFrameListener frameListener;
|
||||
private final VirtualNetworkConfigListener configListener;
|
||||
|
||||
/**
|
||||
* Create a new ZeroTier One node
|
||||
*
|
||||
* <p>Note that this can take a few seconds the first time it's called, as it
|
||||
* will generate an identity.</p>
|
||||
*
|
||||
* @param now Current clock in milliseconds
|
||||
* @param getListener User written instance of the {@link DataStoreGetListener} interface called to get objects from persistent storage. This instance must be unique per Node object.
|
||||
* @param putListener User written intstance of the {@link DataStorePutListener} interface called to put objects in persistent storage. This instance must be unique per Node object.
|
||||
* @param sender
|
||||
* @param eventListener User written instance of the {@link EventListener} interface to receive status updates and non-fatal error notices. This instance must be unique per Node object.
|
||||
* @param frameListener
|
||||
* @param configListener User written instance of the {@link VirtualNetworkConfigListener} interface to be called when virtual LANs are created, deleted, or their config parameters change. This instance must be unique per Node object.
|
||||
*/
|
||||
public Node(long now,
|
||||
DataStoreGetListener getListener,
|
||||
DataStorePutListener putListener,
|
||||
PacketSender sender,
|
||||
EventListener eventListener,
|
||||
VirtualNetworkFrameListener frameListener,
|
||||
VirtualNetworkConfigListener configListener) throws NodeException
|
||||
{
|
||||
this.nodeId = now;
|
||||
|
||||
this.getListener = getListener;
|
||||
this.putListener = putListener;
|
||||
this.sender = sender;
|
||||
this.eventListener = eventListener;
|
||||
this.frameListener = frameListener;
|
||||
this.configListener = configListener;
|
||||
|
||||
ResultCode rc = node_init(now);
|
||||
if(rc != ResultCode.RESULT_OK)
|
||||
{
|
||||
// TODO: Throw Exception
|
||||
throw new NodeException(rc.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close this Node.
|
||||
*
|
||||
* <p>The Node object can no longer be used once this method is called.</p>
|
||||
*/
|
||||
public void close() {
|
||||
if(nodeId != -1) {
|
||||
node_delete(nodeId);
|
||||
nodeId = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a frame from a virtual network port
|
||||
*
|
||||
* @param now Current clock in milliseconds
|
||||
* @param nwid ZeroTier 64-bit virtual network ID
|
||||
* @param sourceMac Source MAC address (least significant 48 bits)
|
||||
* @param destMac Destination MAC address (least significant 48 bits)
|
||||
* @param etherType 16-bit Ethernet frame type
|
||||
* @param vlanId 10-bit VLAN ID or 0 if none
|
||||
* @param frameData Frame payload data
|
||||
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode processVirtualNetworkFrame(
|
||||
long now,
|
||||
long nwid,
|
||||
long sourceMac,
|
||||
long destMac,
|
||||
int etherType,
|
||||
int vlanId,
|
||||
byte[] frameData,
|
||||
long[] nextBackgroundTaskDeadline) {
|
||||
return processVirtualNetworkFrame(
|
||||
nodeId, now, nwid, sourceMac, destMac, etherType, vlanId,
|
||||
frameData, nextBackgroundTaskDeadline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a packet received from the physical wire
|
||||
*
|
||||
* @param now Current clock in milliseconds
|
||||
* @param remoteAddress Origin of packet
|
||||
* @param packetData Packet data
|
||||
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode processWirePacket(
|
||||
long now,
|
||||
InetSocketAddress localAddress,
|
||||
InetSocketAddress remoteAddress,
|
||||
byte[] packetData,
|
||||
long[] nextBackgroundTaskDeadline) {
|
||||
return processWirePacket(
|
||||
nodeId, now, localAddress, remoteAddress, packetData,
|
||||
nextBackgroundTaskDeadline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform periodic background operations
|
||||
*
|
||||
* @param now Current clock in milliseconds
|
||||
* @param nextBackgroundTaskDeadline Value/result: set to deadline for next call to processBackgroundTasks()
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode processBackgroundTasks(long now, long[] nextBackgroundTaskDeadline) {
|
||||
return processBackgroundTasks(nodeId, now, nextBackgroundTaskDeadline);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join a network
|
||||
*
|
||||
* <p>This may generate calls to the port config callback before it returns,
|
||||
* or these may be deffered if a netconf is not available yet.</p>
|
||||
*
|
||||
* <p>If we are already a member of the network, nothing is done and OK is
|
||||
* returned.</p>
|
||||
*
|
||||
* @param nwid 64-bit ZeroTier network ID
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode join(long nwid) {
|
||||
return join(nodeId, nwid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave a network
|
||||
*
|
||||
* <p>If a port has been configured for this network this will generate a call
|
||||
* to the port config callback with a NULL second parameter to indicate that
|
||||
* the port is now deleted.</p>
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode leave(long nwid) {
|
||||
return leave(nodeId, nwid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to an Ethernet multicast group
|
||||
*
|
||||
* <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
|
||||
* broadcast address) but with an ADI equal to each IPv4 address in host
|
||||
* byte order. This converts ARP from a non-scalable broadcast protocol to
|
||||
* a scalable multicast protocol with perfect address specificity.</p>
|
||||
*
|
||||
* <p>If this is not done, ARP will not work reliably.</p>
|
||||
*
|
||||
* <p>Multiple calls to subscribe to the same multicast address will have no
|
||||
* effect. It is perfectly safe to do this.</p>
|
||||
*
|
||||
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode multicastSubscribe(
|
||||
long nwid,
|
||||
long multicastGroup) {
|
||||
return multicastSubscribe(nodeId, nwid, multicastGroup, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to an Ethernet multicast group
|
||||
*
|
||||
* <p>ADI stands for additional distinguishing information. This defaults to zero
|
||||
* and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
|
||||
* and this must be done.</p>
|
||||
*
|
||||
* <p>For IPv4 ARP, the implementation must subscribe to 0xffffffffffff (the
|
||||
* broadcast address) but with an ADI equal to each IPv4 address in host
|
||||
* byte order. This converts ARP from a non-scalable broadcast protocol to
|
||||
* a scalable multicast protocol with perfect address specificity.</p>
|
||||
*
|
||||
* <p>If this is not done, ARP will not work reliably.</p>
|
||||
*
|
||||
* <p>Multiple calls to subscribe to the same multicast address will have no
|
||||
* effect. It is perfectly safe to do this.</p>
|
||||
*
|
||||
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
|
||||
* @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode multicastSubscribe(
|
||||
long nwid,
|
||||
long multicastGroup,
|
||||
long multicastAdi) {
|
||||
return multicastSubscribe(nodeId, nwid, multicastGroup, multicastAdi);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Unsubscribe from an Ethernet multicast group (or all groups)
|
||||
*
|
||||
* <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
|
||||
* you are not subscribed to a group this has no effect.</p>
|
||||
*
|
||||
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode multicastUnsubscribe(
|
||||
long nwid,
|
||||
long multicastGroup) {
|
||||
return multicastUnsubscribe(nodeId, nwid, multicastGroup, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from an Ethernet multicast group (or all groups)
|
||||
*
|
||||
* <p>If multicastGroup is zero (0), this will unsubscribe from all groups. If
|
||||
* you are not subscribed to a group this has no effect.</p>
|
||||
*
|
||||
* <p>This does not generate an update call to the {@link VirtualNetworkConfigListener#onNetworkConfigurationUpdated} method.</p>
|
||||
*
|
||||
* <p>ADI stands for additional distinguishing information. This defaults to zero
|
||||
* and is rarely used. Right now its only use is to enable IPv4 ARP to scale,
|
||||
* and this must be done.</p>
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @param multicastGroup Ethernet multicast or broadcast MAC (least significant 48 bits)
|
||||
* @param multicastAdi Multicast ADI (least significant 32 bits only, default: 0)
|
||||
* @return OK (0) or error code if a fatal error condition has occurred
|
||||
*/
|
||||
public ResultCode multicastUnsubscribe(
|
||||
long nwid,
|
||||
long multicastGroup,
|
||||
long multicastAdi) {
|
||||
return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this node's 40-bit ZeroTier address
|
||||
*
|
||||
* @return ZeroTier address (least significant 40 bits of 64-bit int)
|
||||
*/
|
||||
public long address() {
|
||||
return address(nodeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status of this node
|
||||
*
|
||||
* @return @{link NodeStatus} struct with the current node status.
|
||||
*/
|
||||
public NodeStatus status() {
|
||||
return status(nodeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of known peer nodes
|
||||
*
|
||||
* @return List of known peers or NULL on failure
|
||||
*/
|
||||
public Peer[] peers() {
|
||||
return peers(nodeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status of a virtual network
|
||||
*
|
||||
* @param nwid 64-bit network ID
|
||||
* @return {@link VirtualNetworkConfig} or NULL if we are not a member of this network
|
||||
*/
|
||||
public VirtualNetworkConfig networkConfig(long nwid) {
|
||||
return networkConfig(nodeId, nwid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerate and get status of all networks
|
||||
*
|
||||
* @return List of networks or NULL on failure
|
||||
*/
|
||||
public VirtualNetworkConfig[] networks() {
|
||||
return networks(nodeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get ZeroTier One version
|
||||
*
|
||||
* @return {@link Version} object with ZeroTierOne version information.
|
||||
*/
|
||||
public Version getVersion() {
|
||||
return version();
|
||||
}
|
||||
|
||||
//
|
||||
// function declarations for JNI
|
||||
//
|
||||
private native ResultCode node_init(long now);
|
||||
|
||||
private native void node_delete(long nodeId);
|
||||
|
||||
private native ResultCode processVirtualNetworkFrame(
|
||||
long nodeId,
|
||||
long now,
|
||||
long nwid,
|
||||
long sourceMac,
|
||||
long destMac,
|
||||
int etherType,
|
||||
int vlanId,
|
||||
byte[] frameData,
|
||||
long[] nextBackgroundTaskDeadline);
|
||||
|
||||
private native ResultCode processWirePacket(
|
||||
long nodeId,
|
||||
long now,
|
||||
InetSocketAddress localAddress,
|
||||
InetSocketAddress remoteAddress,
|
||||
byte[] packetData,
|
||||
long[] nextBackgroundTaskDeadline);
|
||||
|
||||
private native ResultCode processBackgroundTasks(
|
||||
long nodeId,
|
||||
long now,
|
||||
long[] nextBackgroundTaskDeadline);
|
||||
|
||||
private native ResultCode join(long nodeId, long nwid);
|
||||
|
||||
private native ResultCode leave(long nodeId, long nwid);
|
||||
|
||||
private native ResultCode multicastSubscribe(
|
||||
long nodeId,
|
||||
long nwid,
|
||||
long multicastGroup,
|
||||
long multicastAdi);
|
||||
|
||||
private native ResultCode multicastUnsubscribe(
|
||||
long nodeId,
|
||||
long nwid,
|
||||
long multicastGroup,
|
||||
long multicastAdi);
|
||||
|
||||
private native long address(long nodeId);
|
||||
|
||||
private native NodeStatus status(long nodeId);
|
||||
|
||||
private native VirtualNetworkConfig networkConfig(long nodeId, long nwid);
|
||||
|
||||
private native Version version();
|
||||
|
||||
private native Peer[] peers(long nodeId);
|
||||
|
||||
private native VirtualNetworkConfig[] networks(long nodeId);
|
||||
}
|
36
java/src/com/zerotier/sdk/NodeException.java
Normal file
36
java/src/com/zerotier/sdk/NodeException.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.lang.RuntimeException;
|
||||
|
||||
public class NodeException extends RuntimeException {
|
||||
public NodeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
69
java/src/com/zerotier/sdk/NodeStatus.java
Normal file
69
java/src/com/zerotier/sdk/NodeStatus.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public final class NodeStatus {
|
||||
private long address;
|
||||
private String publicIdentity;
|
||||
private String secretIdentity;
|
||||
private boolean online;
|
||||
|
||||
private NodeStatus() {}
|
||||
|
||||
/**
|
||||
* 40-bit ZeroTier address of this node
|
||||
*/
|
||||
public final long getAddres() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public identity in string-serialized form (safe to send to others)
|
||||
*
|
||||
* <p>This identity will remain valid as long as the node exists.</p>
|
||||
*/
|
||||
public final String getPublicIdentity() {
|
||||
return publicIdentity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Full identity including secret key in string-serialized form
|
||||
*
|
||||
* <p>This identity will remain valid as long as the node exists.</p>
|
||||
*/
|
||||
public final String getSecretIdentity() {
|
||||
return secretIdentity;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if some kind of connectivity appears available
|
||||
*/
|
||||
public final boolean isOnline() {
|
||||
return online;
|
||||
}
|
||||
}
|
50
java/src/com/zerotier/sdk/PacketSender.java
Normal file
50
java/src/com/zerotier/sdk/PacketSender.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
|
||||
public interface PacketSender {
|
||||
/**
|
||||
* Function to send a ZeroTier packet out over the wire
|
||||
*
|
||||
* <p>The function must return zero on success and may return any error code
|
||||
* on failure. Note that success does not (of course) guarantee packet
|
||||
* delivery. It only means that the packet appears to have been sent.</p>
|
||||
*
|
||||
* @param localAddr {@link InetSocketAddress} to send from. Set to null if not specified.
|
||||
* @param remoteAddr {@link InetSocketAddress} to send to
|
||||
* @param packetData data to send
|
||||
* @return 0 on success, any error code on failure.
|
||||
*/
|
||||
public int onSendPacketRequested(
|
||||
InetSocketAddress localAddr,
|
||||
InetSocketAddress remoteAddr,
|
||||
byte[] packetData,
|
||||
int ttl);
|
||||
}
|
110
java/src/com/zerotier/sdk/Peer.java
Normal file
110
java/src/com/zerotier/sdk/Peer.java
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Peer status result
|
||||
*/
|
||||
public final class Peer {
|
||||
private long address;
|
||||
private long lastUnicastFrame;
|
||||
private long lastMulticastFrame;
|
||||
private int versionMajor;
|
||||
private int versionMinor;
|
||||
private int versionRev;
|
||||
private int latency;
|
||||
private PeerRole role;
|
||||
private PeerPhysicalPath[] paths;
|
||||
|
||||
private Peer() {}
|
||||
|
||||
/**
|
||||
* ZeroTier address (40 bits)
|
||||
*/
|
||||
public final long address() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time we last received a unicast frame from this peer
|
||||
*/
|
||||
public final long lastUnicastFrame() {
|
||||
return lastUnicastFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time we last received a multicast rame from this peer
|
||||
*/
|
||||
public final long lastMulticastFrame() {
|
||||
return lastMulticastFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote major version or -1 if not known
|
||||
*/
|
||||
public final int versionMajor() {
|
||||
return versionMajor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote minor version or -1 if not known
|
||||
*/
|
||||
public final int versionMinor() {
|
||||
return versionMinor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote revision or -1 if not known
|
||||
*/
|
||||
public final int versionRev() {
|
||||
return versionRev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Last measured latency in milliseconds or zero if unknown
|
||||
*/
|
||||
public final int latency() {
|
||||
return latency;
|
||||
}
|
||||
|
||||
/**
|
||||
* What trust hierarchy role does this device have?
|
||||
*/
|
||||
public final PeerRole role() {
|
||||
return role;
|
||||
}
|
||||
|
||||
/**
|
||||
* Known network paths to peer
|
||||
*/
|
||||
public final PeerPhysicalPath[] paths() {
|
||||
return paths;
|
||||
}
|
||||
}
|
86
java/src/com/zerotier/sdk/PeerPhysicalPath.java
Normal file
86
java/src/com/zerotier/sdk/PeerPhysicalPath.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* Physical network path to a peer
|
||||
*/
|
||||
public final class PeerPhysicalPath {
|
||||
private InetSocketAddress address;
|
||||
private long lastSend;
|
||||
private long lastReceive;
|
||||
private boolean fixed;
|
||||
private boolean active;
|
||||
private boolean preferred;
|
||||
|
||||
private PeerPhysicalPath() {}
|
||||
|
||||
/**
|
||||
* Address of endpoint
|
||||
*/
|
||||
public final InetSocketAddress address() {
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time of last send in milliseconds or 0 for never
|
||||
*/
|
||||
public final long lastSend() {
|
||||
return lastSend;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time of last receive in milliseconds or 0 for never
|
||||
*/
|
||||
public final long lastReceive() {
|
||||
return lastReceive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is path fixed? (i.e. not learned, static)
|
||||
*/
|
||||
public final boolean isFixed() {
|
||||
return fixed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is path active?
|
||||
*/
|
||||
public final boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is path preferred?
|
||||
*/
|
||||
public final boolean isPreferred() {
|
||||
return preferred;
|
||||
}
|
||||
}
|
45
java/src/com/zerotier/sdk/PeerRole.java
Normal file
45
java/src/com/zerotier/sdk/PeerRole.java
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public enum PeerRole {
|
||||
/**
|
||||
* An ordinary node
|
||||
*/
|
||||
PEER_ROLE_LEAF,
|
||||
|
||||
/**
|
||||
* relay node
|
||||
*/
|
||||
PEER_ROLE_RELAY,
|
||||
|
||||
/**
|
||||
* root server
|
||||
*/
|
||||
PEER_ROLE_ROOT
|
||||
}
|
74
java/src/com/zerotier/sdk/ResultCode.java
Normal file
74
java/src/com/zerotier/sdk/ResultCode.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
/**
|
||||
* Function return code: OK (0) or error results
|
||||
*
|
||||
* <p>Use {@link ResultCode#isFatal) to check for a fatal error. If a fatal error
|
||||
* occurs, the node should be considered to not be working correctly. These
|
||||
* indicate serious problems like an inaccessible data store or a compile
|
||||
* problem.</p>
|
||||
*/
|
||||
public enum ResultCode {
|
||||
/**
|
||||
* Operation completed normally
|
||||
*/
|
||||
RESULT_OK(0),
|
||||
|
||||
// Fatal errors (> 0, < 1000)
|
||||
/**
|
||||
* Ran out of memory
|
||||
*/
|
||||
RESULT_FATAL_ERROR_OUT_OF_MEMORY(1),
|
||||
|
||||
/**
|
||||
* Data store is not writable or has failed
|
||||
*/
|
||||
RESULT_FATAL_ERROR_DATA_STORE_FAILED(2),
|
||||
|
||||
/**
|
||||
* Internal error (e.g. unexpected exception indicating bug or build problem)
|
||||
*/
|
||||
RESULT_FATAL_ERROR_INTERNAL(3),
|
||||
|
||||
// non-fatal errors
|
||||
|
||||
/**
|
||||
* Network ID not valid
|
||||
*/
|
||||
RESULT_ERROR_NETWORK_NOT_FOUND(1000);
|
||||
|
||||
private final int id;
|
||||
ResultCode(int id) { this.id = id; }
|
||||
public int getValue() { return id; }
|
||||
|
||||
public boolean isFatal(int id) {
|
||||
return (id > 0 && id < 1000);
|
||||
}
|
||||
}
|
36
java/src/com/zerotier/sdk/Version.java
Normal file
36
java/src/com/zerotier/sdk/Version.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public final class Version {
|
||||
private Version() {}
|
||||
|
||||
public int major = 0;
|
||||
public int minor = 0;
|
||||
public int revision = 0;
|
||||
}
|
191
java/src/com/zerotier/sdk/VirtualNetworkConfig.java
Normal file
191
java/src/com/zerotier/sdk/VirtualNetworkConfig.java
Normal file
|
@ -0,0 +1,191 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
import java.lang.Comparable;
|
||||
import java.lang.Override;
|
||||
import java.lang.String;
|
||||
import java.util.ArrayList;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConfig> {
|
||||
public static final int MAX_MULTICAST_SUBSCRIPTIONS = 4096;
|
||||
public static final int ZT_MAX_ZT_ASSIGNED_ADDRESSES = 16;
|
||||
|
||||
private long nwid;
|
||||
private long mac;
|
||||
private String name;
|
||||
private VirtualNetworkStatus status;
|
||||
private VirtualNetworkType type;
|
||||
private int mtu;
|
||||
private boolean dhcp;
|
||||
private boolean bridge;
|
||||
private boolean broadcastEnabled;
|
||||
private int portError;
|
||||
private boolean enabled;
|
||||
private long netconfRevision;
|
||||
private InetSocketAddress[] assignedAddresses;
|
||||
|
||||
private VirtualNetworkConfig() {
|
||||
|
||||
}
|
||||
|
||||
public boolean equals(VirtualNetworkConfig cfg) {
|
||||
boolean aaEqual = true;
|
||||
if(assignedAddresses.length == cfg.assignedAddresses.length) {
|
||||
for(int i = 0; i < assignedAddresses.length; ++i) {
|
||||
if(!assignedAddresses[i].equals(cfg.assignedAddresses[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
aaEqual = false;
|
||||
}
|
||||
|
||||
return nwid == cfg.nwid &&
|
||||
mac == cfg.mac &&
|
||||
name.equals(cfg.name) &&
|
||||
status.equals(cfg.status) &&
|
||||
type.equals(cfg.type) &&
|
||||
mtu == cfg.mtu &&
|
||||
dhcp == cfg.dhcp &&
|
||||
bridge == cfg.bridge &&
|
||||
broadcastEnabled == cfg.broadcastEnabled &&
|
||||
portError == cfg.portError &&
|
||||
enabled == cfg.enabled &&
|
||||
aaEqual;
|
||||
}
|
||||
|
||||
public int compareTo(VirtualNetworkConfig cfg) {
|
||||
if(cfg.nwid == this.nwid) {
|
||||
return 0;
|
||||
} else {
|
||||
return this.nwid > cfg.nwid ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 64-bit ZeroTier network ID
|
||||
*/
|
||||
public final long networkId() {
|
||||
return nwid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ethernet MAC (40 bits) that should be assigned to port
|
||||
*/
|
||||
public final long macAddress() {
|
||||
return mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Network name (from network configuration master)
|
||||
*/
|
||||
public final String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Network configuration request status
|
||||
*/
|
||||
public final VirtualNetworkStatus networkStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Network type
|
||||
*/
|
||||
public final VirtualNetworkType networkType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum interface MTU
|
||||
*/
|
||||
public final int mtu() {
|
||||
return mtu;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the network this port belongs to indicates DHCP availability
|
||||
*
|
||||
* <p>This is a suggestion. The underlying implementation is free to ignore it
|
||||
* for security or other reasons. This is simply a netconf parameter that
|
||||
* means 'DHCP is available on this network.'</p>
|
||||
*/
|
||||
public final boolean isDhcpAvailable() {
|
||||
return dhcp;
|
||||
}
|
||||
|
||||
/**
|
||||
* If this port is allowed to bridge to other networks
|
||||
*
|
||||
* <p>This is informational. If this is false, bridged packets will simply
|
||||
* be dropped and bridging won't work.</p>
|
||||
*/
|
||||
public final boolean isBridgeEnabled() {
|
||||
return bridge;
|
||||
}
|
||||
|
||||
/**
|
||||
* If true, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic
|
||||
*/
|
||||
public final boolean broadcastEnabled() {
|
||||
return broadcastEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the network is in PORT_ERROR state, this is the error most recently returned by the port config callback
|
||||
*/
|
||||
public final int portError() {
|
||||
return portError;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* ZeroTier-assigned addresses (in {@link java.net.InetSocketAddress} objects)
|
||||
*
|
||||
* For IP, the port number of the sockaddr_XX structure contains the number
|
||||
* of bits in the address netmask. Only the IP address and port are used.
|
||||
* Other fields like interface number can be ignored.
|
||||
*
|
||||
* This is only used for ZeroTier-managed address assignments sent by the
|
||||
* virtual network's configuration master.
|
||||
*/
|
||||
public final InetSocketAddress[] assignedAddresses() {
|
||||
return assignedAddresses;
|
||||
}
|
||||
}
|
60
java/src/com/zerotier/sdk/VirtualNetworkConfigListener.java
Normal file
60
java/src/com/zerotier/sdk/VirtualNetworkConfigListener.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
|
||||
public interface VirtualNetworkConfigListener {
|
||||
/**
|
||||
* Callback called to update virtual network port configuration
|
||||
*
|
||||
* <p>This can be called at any time to update the configuration of a virtual
|
||||
* network port. The parameter after the network ID specifies whether this
|
||||
* port is being brought up, updated, brought down, or permanently deleted.
|
||||
*
|
||||
* This in turn should be used by the underlying implementation to create
|
||||
* and configure tap devices at the OS (or virtual network stack) layer.</P>
|
||||
*
|
||||
* This should not call {@link Node#multicastSubscribe} or other network-modifying
|
||||
* methods, as this could cause a deadlock in multithreaded or interrupt
|
||||
* driven environments.
|
||||
*
|
||||
* This must return 0 on success. It can return any OS-dependent error code
|
||||
* on failure, and this results in the network being placed into the
|
||||
* PORT_ERROR state.
|
||||
*
|
||||
* @param nwid network id
|
||||
* @param op {@link VirtualNetworkConfigOperation} enum describing the configuration operation
|
||||
* @param config {@link VirtualNetworkConfig} object with the new configuration
|
||||
* @return 0 on success
|
||||
*/
|
||||
public int onNetworkConfigurationUpdated(
|
||||
long nwid,
|
||||
VirtualNetworkConfigOperation op,
|
||||
VirtualNetworkConfig config);
|
||||
}
|
49
java/src/com/zerotier/sdk/VirtualNetworkConfigOperation.java
Normal file
49
java/src/com/zerotier/sdk/VirtualNetworkConfigOperation.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public enum VirtualNetworkConfigOperation {
|
||||
/**
|
||||
* Network is coming up (either for the first time or after service restart)
|
||||
*/
|
||||
VIRTUAL_NETWORK_CONFIG_OPERATION_UP,
|
||||
|
||||
/**
|
||||
* Network configuration has been updated
|
||||
*/
|
||||
VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE,
|
||||
|
||||
/**
|
||||
* Network is going down (not permanently)
|
||||
*/
|
||||
VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN,
|
||||
|
||||
/**
|
||||
* Network is going down permanently (leave/delete)
|
||||
*/
|
||||
VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY
|
||||
}
|
48
java/src/com/zerotier/sdk/VirtualNetworkFrameListener.java
Normal file
48
java/src/com/zerotier/sdk/VirtualNetworkFrameListener.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public interface VirtualNetworkFrameListener {
|
||||
/**
|
||||
* Function to send a frame out to a virtual network port
|
||||
*
|
||||
* @param nwid ZeroTier One network ID
|
||||
* @param srcMac source MAC address
|
||||
* @param destMac destination MAC address
|
||||
* @param ethertype
|
||||
* @param vlanId
|
||||
* @param frameData data to send
|
||||
*/
|
||||
public void onVirtualNetworkFrame(
|
||||
long nwid,
|
||||
long srcMac,
|
||||
long destMac,
|
||||
long etherType,
|
||||
long vlanId,
|
||||
byte[] frameData);
|
||||
}
|
59
java/src/com/zerotier/sdk/VirtualNetworkStatus.java
Normal file
59
java/src/com/zerotier/sdk/VirtualNetworkStatus.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public enum VirtualNetworkStatus {
|
||||
/**
|
||||
* Waiting for network configuration (also means revision == 0)
|
||||
*/
|
||||
NETWORK_STATUS_REQUESTING_CONFIGURATION,
|
||||
|
||||
/**
|
||||
* Configuration received and we are authorized
|
||||
*/
|
||||
NETWORK_STATUS_OK,
|
||||
|
||||
/**
|
||||
* Netconf master told us 'nope'
|
||||
*/
|
||||
NETWORK_STATUS_ACCESS_DENIED,
|
||||
|
||||
/**
|
||||
* Netconf master exists, but this virtual network does not
|
||||
*/
|
||||
NETWORK_STATUS_NOT_FOUND,
|
||||
|
||||
/**
|
||||
* Initialization of network failed or other internal error
|
||||
*/
|
||||
NETWORK_STATUS_PORT_ERROR,
|
||||
|
||||
/**
|
||||
* ZeroTier One version too old
|
||||
*/
|
||||
NETWORK_STATUS_CLIENT_TOO_OLD
|
||||
}
|
39
java/src/com/zerotier/sdk/VirtualNetworkType.java
Normal file
39
java/src/com/zerotier/sdk/VirtualNetworkType.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* ZeroTier One - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
package com.zerotier.sdk;
|
||||
|
||||
public enum VirtualNetworkType {
|
||||
/**
|
||||
* Private networks are authorized via certificates of membership
|
||||
*/
|
||||
NETWORK_TYPE_PRIVATE,
|
||||
|
||||
/**
|
||||
* Public networks have no access control -- they'll always be AUTHORIZED
|
||||
*/
|
||||
NETWORK_TYPE_PUBLIC
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue