Imported Upstream version 1.2.4

This commit is contained in:
didyouexpectthat 2018-01-12 18:20:00 -08:00
commit 4722a0b75a
398 changed files with 38633 additions and 24919 deletions

View file

@ -5,20 +5,19 @@ include $(CLEAR_VARS)
LOCAL_MODULE := ZeroTierOneJNI
LOCAL_C_INCLUDES := $(ZT1)/include
LOCAL_C_INCLUDES += $(ZT1)/node
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -llog -latomic
# 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/Capability.cpp \
$(ZT1)/node/CertificateOfMembership.cpp \
$(ZT1)/node/DeferredPackets.cpp \
$(ZT1)/node/CertificateOfOwnership.cpp \
$(ZT1)/node/Identity.cpp \
$(ZT1)/node/IncomingPacket.cpp \
$(ZT1)/node/InetAddress.cpp \
$(ZT1)/node/Membership.cpp \
$(ZT1)/node/Multicaster.cpp \
$(ZT1)/node/Network.cpp \
$(ZT1)/node/NetworkConfig.cpp \
@ -28,14 +27,15 @@ LOCAL_SRC_FILES := \
$(ZT1)/node/Path.cpp \
$(ZT1)/node/Peer.cpp \
$(ZT1)/node/Poly1305.cpp \
$(ZT1)/node/Revocation.cpp \
$(ZT1)/node/Salsa20.cpp \
$(ZT1)/node/SelfAwareness.cpp \
$(ZT1)/node/SHA512.cpp \
$(ZT1)/node/Switch.cpp \
$(ZT1)/node/Tag.cpp \
$(ZT1)/node/Topology.cpp \
$(ZT1)/node/Utils.cpp \
$(ZT1)/osdep/Http.cpp \
$(ZT1)/osdep/OSUtils.cpp
$(ZT1)/node/Utils.cpp
# JNI Files
LOCAL_SRC_FILES += \

View file

@ -1,5 +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_STL := c++_static
APP_CPPFLAGS := -O3 -Wall -fstack-protector -fexceptions -fno-strict-aliasing -Wno-deprecated-register -DZT_NO_TYPE_PUNNING=1
APP_PLATFORM := android-14
APP_ABI := all

View file

@ -1,3 +1,21 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ZT_jniutils.h"
#include "ZT_jnilookup.h"
#include <string>
@ -12,7 +30,7 @@ extern "C" {
jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
{
jclass resultClass = NULL;
jobject resultObject = NULL;
resultClass = lookup.findClass("com/zerotier/sdk/ResultCode");
@ -49,14 +67,14 @@ jobject createResultObject(JNIEnv *env, ZT_ResultCode code)
}
jfieldID enumField = lookup.findStaticField(resultClass, fieldName.c_str(), "Lcom/zerotier/sdk/ResultCode;");
if(env->ExceptionCheck() || enumField == NULL)
if(env->ExceptionCheck() || enumField == NULL)
{
LOGE("Error on FindStaticField");
return NULL;
}
resultObject = env->GetStaticObjectField(resultClass, enumField);
if(env->ExceptionCheck() || resultObject == NULL)
if(env->ExceptionCheck() || resultObject == NULL)
{
LOGE("Error on GetStaticObjectField");
}
@ -136,6 +154,8 @@ jobject createEvent(JNIEnv *env, ZT_Event event)
case ZT_EVENT_TRACE:
fieldName = "EVENT_TRACE";
break;
case ZT_EVENT_USER_MESSAGE:
break;
}
jfieldID enumField = lookup.findStaticField(eventClass, fieldName.c_str(), "Lcom/zerotier/sdk/Event;");
@ -162,11 +182,11 @@ jobject createPeerRole(JNIEnv *env, ZT_PeerRole role)
case ZT_PEER_ROLE_LEAF:
fieldName = "PEER_ROLE_LEAF";
break;
case ZT_PEER_ROLE_RELAY:
fieldName = "PEER_ROLE_RELAY";
case ZT_PEER_ROLE_MOON:
fieldName = "PEER_ROLE_MOON";
break;
case ZT_PEER_ROLE_ROOT:
fieldName = "PEER_ROLE_ROOTS";
case ZT_PEER_ROLE_PLANET:
fieldName = "PEER_ROLE_PLANET";
break;
}
@ -313,11 +333,20 @@ jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
return NULL;
}
jobject inetAddressObject = newInetAddress(env, addr);
jobject inetAddressObject = NULL;
if(env->ExceptionCheck() || inetAddressObject == NULL)
if(addr.ss_family != 0)
{
inetAddressObject = newInetAddress(env, addr);
if(env->ExceptionCheck() || inetAddressObject == NULL)
{
LOGE("Error creating new inet address");
return NULL;
}
}
else
{
LOGE("Error creating new inet address");
return NULL;
}
@ -350,10 +379,9 @@ jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
break;
default:
{
LOGE("ERROR: addr.ss_family is not set or unknown");
break;
}
};
}
jobject inetSocketAddressObject = env->NewObject(inetSocketAddressClass, inetSocketAddress_constructor, inetAddressObject, port);
@ -371,7 +399,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
jfieldID addressField = NULL;
jfieldID lastSendField = NULL;
jfieldID lastReceiveField = NULL;
jfieldID activeField = NULL;
jfieldID preferredField = NULL;
jmethodID ppp_constructor = NULL;
@ -404,13 +431,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
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)
{
@ -441,7 +461,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
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()) {
@ -451,15 +470,13 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
return pppObject;
}
jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
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;
@ -483,20 +500,6 @@ jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
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)
{
@ -554,8 +557,6 @@ jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
}
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);
@ -571,7 +572,7 @@ jobject newPeer(JNIEnv *env, const ZT_Peer &peer)
jobjectArray arrayObject = env->NewObjectArray(
peer.pathCount, peerPhysicalPathClass, NULL);
if(env->ExceptionCheck() || arrayObject == NULL)
if(env->ExceptionCheck() || arrayObject == NULL)
{
LOGE("Error creating PeerPhysicalPath[] array");
return NULL;
@ -609,6 +610,7 @@ jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
jfieldID portErrorField = NULL;
jfieldID netconfRevisionField = NULL;
jfieldID assignedAddressesField = NULL;
jfieldID routesField = NULL;
vnetConfigClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkConfig");
if(vnetConfigClass == NULL)
@ -709,13 +711,22 @@ jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
return NULL;
}
assignedAddressesField = lookup.findField(vnetConfigClass, "assignedAddresses", "[Ljava/net/InetSocketAddress;");
assignedAddressesField = lookup.findField(vnetConfigClass, "assignedAddresses",
"[Ljava/net/InetSocketAddress;");
if(env->ExceptionCheck() || assignedAddressesField == NULL)
{
LOGE("Error getting assignedAddresses field");
return NULL;
}
routesField = lookup.findField(vnetConfigClass, "routes",
"[Lcom/zerotier/sdk/VirtualNetworkRoute;");
if(env->ExceptionCheck() || routesField == NULL)
{
LOGE("Error getting routes field");
return NULL;
}
env->SetLongField(vnetConfigObj, nwidField, vnetConfig.nwid);
env->SetLongField(vnetConfigObj, macField, vnetConfig.mac);
jstring nameStr = env->NewStringUTF(vnetConfig.name);
@ -773,6 +784,34 @@ jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &vnetConfig)
env->SetObjectField(vnetConfigObj, assignedAddressesField, assignedAddrArrayObj);
jclass virtualNetworkRouteClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkRoute");
if(env->ExceptionCheck() || virtualNetworkRouteClass == NULL)
{
LOGE("Error finding VirtualNetworkRoute class");
return NULL;
}
jobjectArray routesArrayObj = env->NewObjectArray(
vnetConfig.routeCount, virtualNetworkRouteClass, NULL);
if(env->ExceptionCheck() || routesArrayObj == NULL)
{
LOGE("Error creating VirtualNetworkRoute[] array");
return NULL;
}
for(unsigned int i = 0; i < vnetConfig.routeCount; ++i)
{
jobject routeObj = newVirtualNetworkRoute(env, vnetConfig.routes[i]);
env->SetObjectArrayElement(routesArrayObj, i, routeObj);
if(env->ExceptionCheck())
{
LOGE("Error assigning VirtualNetworkRoute to array");
return NULL;
}
}
env->SetObjectField(vnetConfigObj, routesField, routesArrayObj);
return vnetConfigObj;
}
@ -831,6 +870,72 @@ jobject newVersion(JNIEnv *env, int major, int minor, int rev)
return versionObj;
}
jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route)
{
jclass virtualNetworkRouteClass = NULL;
jmethodID routeConstructor = NULL;
virtualNetworkRouteClass = lookup.findClass("com/zerotier/sdk/VirtualNetworkRoute");
if(env->ExceptionCheck() || virtualNetworkRouteClass == NULL)
{
return NULL;
}
routeConstructor = lookup.findMethod(virtualNetworkRouteClass, "<init>", "()V");
if(env->ExceptionCheck() || routeConstructor == NULL)
{
return NULL;
}
jobject routeObj = env->NewObject(virtualNetworkRouteClass, routeConstructor);
if(env->ExceptionCheck() || routeObj == NULL)
{
return NULL;
}
jfieldID targetField = NULL;
jfieldID viaField = NULL;
jfieldID flagsField = NULL;
jfieldID metricField = NULL;
targetField = lookup.findField(virtualNetworkRouteClass, "target",
"Ljava/net/InetSocketAddress;");
if(env->ExceptionCheck() || targetField == NULL)
{
return NULL;
}
viaField = lookup.findField(virtualNetworkRouteClass, "via",
"Ljava/net/InetSocketAddress;");
if(env->ExceptionCheck() || targetField == NULL)
{
return NULL;
}
flagsField = lookup.findField(virtualNetworkRouteClass, "flags", "I");
if(env->ExceptionCheck() || flagsField == NULL)
{
return NULL;
}
metricField = lookup.findField(virtualNetworkRouteClass, "metric", "I");
if(env->ExceptionCheck() || metricField == NULL)
{
return NULL;
}
jobject targetObj = newInetSocketAddress(env, route.target);
jobject viaObj = newInetSocketAddress(env, route.via);
env->SetObjectField(routeObj, targetField, targetObj);
env->SetObjectField(routeObj, viaField, viaObj);
env->SetIntField(routeObj, flagsField, (jint)route.flags);
env->SetIntField(routeObj, metricField, (jint)route.metric);
return routeObj;
}
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,3 +1,21 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZT_jniutils_h_
#define ZT_jniutils_h_
#include <stdio.h>
@ -42,6 +60,8 @@ jobject newNetworkConfig(JNIEnv *env, const ZT_VirtualNetworkConfig &config);
jobject newVersion(JNIEnv *env, int major, int minor, int rev);
jobject newVirtualNetworkRoute(JNIEnv *env, const ZT_VirtualNetworkRoute &route);
#ifdef __cplusplus
}
#endif

View file

@ -56,7 +56,12 @@ namespace {
, eventListener(NULL)
, frameListener(NULL)
, configListener(NULL)
{}
, pathChecker(NULL)
, callbacks(NULL)
{
callbacks = (ZT_Node_Callbacks*)malloc(sizeof(ZT_Node_Callbacks));
memset(callbacks, 0, sizeof(ZT_Node_Callbacks));
}
~JniRef()
{
@ -69,6 +74,10 @@ namespace {
env->DeleteGlobalRef(eventListener);
env->DeleteGlobalRef(frameListener);
env->DeleteGlobalRef(configListener);
env->DeleteGlobalRef(pathChecker);
free(callbacks);
callbacks = NULL;
}
uint64_t id;
@ -83,12 +92,16 @@ namespace {
jobject eventListener;
jobject frameListener;
jobject configListener;
jobject pathChecker;
ZT_Node_Callbacks *callbacks;
};
int VirtualNetworkConfigFunctionCallback(
ZT_Node *node,
void *userData,
void *threadData,
uint64_t nwid,
void **,
enum ZT_VirtualNetworkConfigOperation operation,
@ -130,13 +143,14 @@ namespace {
}
return env->CallIntMethod(
ref->configListener,
configListenerCallbackMethod,
ref->configListener,
configListenerCallbackMethod,
(jlong)nwid, operationObject, networkConfigObject);
}
void VirtualNetworkFrameFunctionCallback(ZT_Node *node,
void *userData,
void *threadData,
uint64_t nwid,
void**,
uint64_t sourceMac,
@ -194,7 +208,8 @@ namespace {
void EventCallback(ZT_Node *node,
void *userData,
enum ZT_Event event,
void *threadData,
enum ZT_Event event,
const void *data)
{
LOGV("EventCallback");
@ -282,11 +297,14 @@ namespace {
}
}
break;
case ZT_EVENT_USER_MESSAGE:
break;
}
}
long DataStoreGetFunction(ZT_Node *node,
void *userData,
void *threadData,
const char *objectName,
void *buffer,
unsigned long bufferSize,
@ -339,7 +357,7 @@ namespace {
objectName, buffer, bufferIndex, objectSizeObj);
long retval = (long)env->CallLongMethod(
ref->dataStoreGetListener, dataStoreGetCallbackMethod,
ref->dataStoreGetListener, dataStoreGetCallbackMethod,
nameStr, bufferObj, (jlong)bufferIndex, objectSizeObj);
if(retval > 0)
@ -360,6 +378,7 @@ namespace {
int DataStorePutFunction(ZT_Node *node,
void *userData,
void *threadData,
const char *objectName,
const void *buffer,
unsigned long bufferSize,
@ -426,6 +445,7 @@ namespace {
int WirePacketSendFunction(ZT_Node *node,
void *userData,
void *threadData,
const struct sockaddr_storage *localAddress,
const struct sockaddr_storage *remoteAddress,
const void *buffer,
@ -454,7 +474,7 @@ namespace {
LOGE("Couldn't find onSendPacketRequested method");
return -2;
}
jobject localAddressObj = NULL;
if(memcmp(localAddress, &ZT_SOCKADDR_NULL, sizeof(sockaddr_storage)) != 0)
{
@ -470,6 +490,165 @@ namespace {
return retval;
}
int PathCheckFunction(ZT_Node *node,
void *userPtr,
void *threadPtr,
uint64_t address,
const struct sockaddr_storage *localAddress,
const struct sockaddr_storage *remoteAddress)
{
JniRef *ref = (JniRef*)userPtr;
assert(ref->node == node);
if(ref->pathChecker == NULL) {
return true;
}
JNIEnv *env = NULL;
ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
jclass pathCheckerClass = env->GetObjectClass(ref->pathChecker);
if(pathCheckerClass == NULL)
{
LOGE("Couldn't find class for PathChecker instance");
return true;
}
jmethodID pathCheckCallbackMethod = lookup.findMethod(pathCheckerClass,
"onPathCheck", "(JLjava/net/InetSocketAddress;Ljava/net/InetSocketAddress;)Z");
if(pathCheckCallbackMethod == NULL)
{
LOGE("Couldn't find onPathCheck method implementation");
return true;
}
jobject localAddressObj = NULL;
jobject remoteAddressObj = NULL;
if(memcmp(localAddress, &ZT_SOCKADDR_NULL, sizeof(sockaddr_storage)) != 0)
{
localAddressObj = newInetSocketAddress(env, *localAddress);
}
if(memcmp(remoteAddress, &ZT_SOCKADDR_NULL, sizeof(sockaddr_storage)) != 0)
{
remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
}
return env->CallBooleanMethod(ref->pathChecker, pathCheckCallbackMethod, address, localAddressObj, remoteAddressObj);
}
int PathLookupFunction(ZT_Node *node,
void *userPtr,
void *threadPtr,
uint64_t address,
int ss_family,
struct sockaddr_storage *result)
{
JniRef *ref = (JniRef*)userPtr;
assert(ref->node == node);
if(ref->pathChecker == NULL) {
return false;
}
JNIEnv *env = NULL;
ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
jclass pathCheckerClass = env->GetObjectClass(ref->pathChecker);
if(pathCheckerClass == NULL)
{
LOGE("Couldn't find class for PathChecker instance");
return false;
}
jmethodID pathLookupMethod = lookup.findMethod(pathCheckerClass,
"onPathLookup", "(JI)Ljava/net/InetSocketAddress;");
if(pathLookupMethod == NULL) {
return false;
}
jobject sockAddressObject = env->CallObjectMethod(ref->pathChecker, pathLookupMethod, address, ss_family);
if(sockAddressObject == NULL)
{
LOGE("Unable to call onPathLookup implementation");
return false;
}
jclass inetSockAddressClass = env->GetObjectClass(sockAddressObject);
if(inetSockAddressClass == NULL)
{
LOGE("Unable to find InetSocketAddress class");
return false;
}
jmethodID getAddressMethod = lookup.findMethod(inetSockAddressClass, "getAddress", "()Ljava/net/InetAddress;");
if(getAddressMethod == NULL)
{
LOGE("Unable to find InetSocketAddress.getAddress() method");
return false;
}
jmethodID getPortMethod = lookup.findMethod(inetSockAddressClass, "getPort", "()I");
if(getPortMethod == NULL)
{
LOGE("Unable to find InetSocketAddress.getPort() method");
return false;
}
jint port = env->CallIntMethod(sockAddressObject, getPortMethod);
jobject addressObject = env->CallObjectMethod(sockAddressObject, getAddressMethod);
jclass inetAddressClass = lookup.findClass("java/net/InetAddress");
if(inetAddressClass == NULL)
{
LOGE("Unable to find InetAddress class");
return false;
}
getAddressMethod = lookup.findMethod(inetAddressClass, "getAddress", "()[B");
if(getAddressMethod == NULL)
{
LOGE("Unable to find InetAddress.getAddress() method");
return false;
}
jbyteArray addressBytes = (jbyteArray)env->CallObjectMethod(addressObject, getAddressMethod);
if(addressBytes == NULL)
{
LOGE("Unable to call InetAddress.getBytes()");
return false;
}
int addressSize = env->GetArrayLength(addressBytes);
if(addressSize == 4)
{
// IPV4
sockaddr_in *addr = (sockaddr_in*)result;
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
void *data = env->GetPrimitiveArrayCritical(addressBytes, NULL);
memcpy(&addr->sin_addr, data, 4);
env->ReleasePrimitiveArrayCritical(addressBytes, data, 0);
}
else if (addressSize == 16)
{
// IPV6
sockaddr_in6 *addr = (sockaddr_in6*)result;
addr->sin6_family = AF_INET6;
addr->sin6_port = htons(port);
void *data = env->GetPrimitiveArrayCritical(addressBytes, NULL);
memcpy(&addr->sin6_addr, data, 16);
env->ReleasePrimitiveArrayCritical(addressBytes, data, 0);
}
else
{
return false;
}
return true;
}
typedef std::map<uint64_t, JniRef*> NodeMap;
static NodeMap nodeMap;
ZeroTier::Mutex nodeMapMutex;
@ -487,7 +666,7 @@ namespace {
}
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
lookup.setJavaVM(vm);
return JNI_VERSION_1_6;
@ -602,17 +781,35 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
}
ref->eventListener = env->NewGlobalRef(tmp);
fid = lookup.findField(
cls, "pathChecker", "Lcom/zerotier/sdk/PathChecker;");
if(fid == NULL)
{
LOGE("no path checker?");
return NULL;
}
tmp = env->GetObjectField(obj, fid);
if(tmp != NULL)
{
ref->pathChecker = env->NewGlobalRef(tmp);
}
ref->callbacks->dataStoreGetFunction = &DataStoreGetFunction;
ref->callbacks->dataStorePutFunction = &DataStorePutFunction;
ref->callbacks->wirePacketSendFunction = &WirePacketSendFunction;
ref->callbacks->virtualNetworkFrameFunction = &VirtualNetworkFrameFunctionCallback;
ref->callbacks->virtualNetworkConfigFunction = &VirtualNetworkConfigFunctionCallback;
ref->callbacks->eventCallback = &EventCallback;
ref->callbacks->pathCheckFunction = &PathCheckFunction;
ref->callbacks->pathLookupFunction = &PathLookupFunction;
ZT_ResultCode rc = ZT_Node_new(
&node,
ref,
(uint64_t)now,
&DataStoreGetFunction,
&DataStorePutFunction,
&WirePacketSendFunction,
&VirtualNetworkFrameFunctionCallback,
&VirtualNetworkConfigFunctionCallback,
NULL,
&EventCallback);
ref->callbacks,
(uint64_t)now);
if(rc != ZT_RESULT_OK)
{
@ -631,7 +828,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
ZeroTier::Mutex::Lock lock(nodeMapMutex);
ref->node = node;
nodeMap.insert(std::make_pair(ref->id, ref));
return resultObject;
}
@ -649,7 +846,7 @@ JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
NodeMap::iterator found;
{
ZeroTier::Mutex::Lock lock(nodeMapMutex);
ZeroTier::Mutex::Lock lock(nodeMapMutex);
found = nodeMap.find(nodeId);
}
@ -675,9 +872,9 @@ JNIEXPORT void JNICALL Java_com_zerotier_sdk_Node_node_1delete(
* Signature: (JJJJJII[B[J)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
JNIEnv *env, jobject obj,
jlong id,
jlong in_now,
JNIEnv *env, jobject obj,
jlong id,
jlong in_now,
jlong in_nwid,
jlong in_sourceMac,
jlong in_destMac,
@ -687,7 +884,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
jlongArray out_nextBackgroundTaskDeadline)
{
uint64_t nodeId = (uint64_t) id;
ZT_Node *node = findNode(nodeId);
if(node == NULL)
{
@ -719,6 +916,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
ZT_ResultCode rc = ZT_Node_processVirtualNetworkFrame(
node,
NULL,
now,
nwid,
sourceMac,
@ -742,9 +940,9 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
* Signature: (JJLjava/net/InetSocketAddress;I[B[J)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
JNIEnv *env, jobject obj,
JNIEnv *env, jobject obj,
jlong id,
jlong in_now,
jlong in_now,
jobject in_localAddress,
jobject in_remoteAddress,
jbyteArray in_packetData,
@ -810,7 +1008,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
jmethodID inetSock_getPort = lookup.findMethod(
InetSocketAddressClass, "getPort", "()I");
if(env->ExceptionCheck() || inetSock_getPort == NULL)
if(env->ExceptionCheck() || inetSock_getPort == NULL)
{
LOGE("Couldn't find getPort method on InetSocketAddress");
return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL);
@ -834,10 +1032,10 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
}
unsigned int addrSize = env->GetArrayLength(remoteAddressArray);
sockaddr_storage localAddress = {};
if(localAddrObj == NULL)
{
localAddress = ZT_SOCKADDR_NULL;
@ -923,13 +1121,14 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
ZT_ResultCode rc = ZT_Node_processWirePacket(
node,
NULL,
now,
&localAddress,
&remoteAddress,
localData,
packetLength,
&nextBackgroundTaskDeadline);
if(rc != ZT_RESULT_OK)
if(rc != ZT_RESULT_OK)
{
LOGE("ZT_Node_processWirePacket returned: %d", rc);
}
@ -949,7 +1148,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
* Signature: (JJ[J)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
JNIEnv *env, jobject obj,
JNIEnv *env, jobject obj,
jlong id,
jlong in_now,
jlongArray out_nextBackgroundTaskDeadline)
@ -971,7 +1170,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
uint64_t now = (uint64_t)in_now;
uint64_t nextBackgroundTaskDeadline = 0;
ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
ZT_ResultCode rc = ZT_Node_processBackgroundTasks(node, NULL, now, &nextBackgroundTaskDeadline);
jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
@ -998,7 +1197,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_join(
uint64_t nwid = (uint64_t)in_nwid;
ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL);
ZT_ResultCode rc = ZT_Node_join(node, nwid, NULL, NULL);
return createResultObject(env, rc);
}
@ -1021,8 +1220,8 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
uint64_t nwid = (uint64_t)in_nwid;
ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL);
ZT_ResultCode rc = ZT_Node_leave(node, nwid, NULL, NULL);
return createResultObject(env, rc);
}
@ -1032,8 +1231,8 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_leave(
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
JNIEnv *env, jobject obj,
jlong id,
JNIEnv *env, jobject obj,
jlong id,
jlong in_nwid,
jlong in_multicastGroup,
jlong in_multicastAdi)
@ -1051,7 +1250,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
unsigned long multicastAdi = (unsigned long)in_multicastAdi;
ZT_ResultCode rc = ZT_Node_multicastSubscribe(
node, nwid, multicastGroup, multicastAdi);
node, NULL, nwid, multicastGroup, multicastAdi);
return createResultObject(env, rc);
}
@ -1062,8 +1261,8 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastSubscribe(
* Signature: (JJJJ)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
JNIEnv *env, jobject obj,
jlong id,
JNIEnv *env, jobject obj,
jlong id,
jlong in_nwid,
jlong in_multicastGroup,
jlong in_multicastAdi)
@ -1086,6 +1285,54 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_multicastUnsubscribe(
return createResultObject(env, rc);
}
/*
* Class: com_zerotier_sdk_Node
* Method: orbit
* Signature: (JJJ)Lcom/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_orbit(
JNIEnv *env, jobject obj,
jlong id,
jlong in_moonWorldId,
jlong in_moonSeed)
{
uint64_t nodeId = (uint64_t)id;
ZT_Node *node = findNode(nodeId);
if(node == NULL)
{
return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL);
}
uint64_t moonWorldId = (uint64_t)in_moonWorldId;
uint64_t moonSeed = (uint64_t)in_moonSeed;
ZT_ResultCode rc = ZT_Node_orbit(node, NULL, moonWorldId, moonSeed);
return createResultObject(env, rc);
}
/*
* Class: com_zerotier_sdk_Node
* Method: deorbit
* Signature: (JJ)L/com/zerotier/sdk/ResultCode;
*/
JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_deorbit(
JNIEnv *env, jobject obj,
jlong id,
jlong in_moonWorldId)
{
uint64_t nodeId = (uint64_t)id;
ZT_Node *node = findNode(nodeId);
if(node == NULL)
{
return createResultObject(env, ZT_RESULT_FATAL_ERROR_INTERNAL);
}
uint64_t moonWorldId = (uint64_t)in_moonWorldId;
ZT_ResultCode rc = ZT_Node_deorbit(node, NULL, moonWorldId);
return createResultObject(env, rc);
}
/*
* Class: com_zerotier_sdk_Node
* Method: address
@ -1131,7 +1378,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_status
{
return NULL;
}
nodeStatusConstructor = lookup.findMethod(
nodeStatusClass, "<init>", "()V");
if(nodeStatusConstructor == NULL)
@ -1215,7 +1462,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_networkConfig(
}
ZT_VirtualNetworkConfig *vnetConfig = ZT_Node_networkConfig(node, nwid);
jobject vnetConfigObject = newNetworkConfig(env, *vnetConfig);
ZT_Node_freeQueryResult(node, vnetConfig);
@ -1257,7 +1504,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
}
ZT_PeerList *peerList = ZT_Node_peers(node);
if(peerList == NULL)
{
LOGE("ZT_Node_peers returned NULL");
@ -1296,7 +1543,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_zerotier_sdk_Node_peers(
{
jobject peerObj = newPeer(env, peerList->peers[i]);
env->SetObjectArrayElement(peerArrayObj, i, peerObj);
if(env->ExceptionCheck())
if(env->ExceptionCheck())
{
LOGE("Error assigning Peer object to array");
break;

View file

@ -74,6 +74,7 @@ public class Node {
private final EventListener eventListener;
private final VirtualNetworkFrameListener frameListener;
private final VirtualNetworkConfigListener configListener;
private final PathChecker pathChecker;
/**
* Create a new ZeroTier One node
@ -88,6 +89,7 @@ public class Node {
* @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.
* @param pathChecker User written instance of the {@link PathChecker} interface. Not required and can be null.
*/
public Node(long now,
DataStoreGetListener getListener,
@ -95,7 +97,8 @@ public class Node {
PacketSender sender,
EventListener eventListener,
VirtualNetworkFrameListener frameListener,
VirtualNetworkConfigListener configListener) throws NodeException
VirtualNetworkConfigListener configListener,
PathChecker pathChecker) throws NodeException
{
this.nodeId = now;
@ -105,6 +108,7 @@ public class Node {
this.eventListener = eventListener;
this.frameListener = frameListener;
this.configListener = configListener;
this.pathChecker = pathChecker;
ResultCode rc = node_init(now);
if(rc != ResultCode.RESULT_OK)
@ -318,6 +322,34 @@ public class Node {
return multicastUnsubscribe(nodeId, nwid, multicastGroup, multicastAdi);
}
/**
* Add or update a moon
*
* Moons are persisted in the data store in moons.d/, so this can persist
* across invocations if the contents of moon.d are scanned and orbit is
* called for each on startup.
*
* @param moonWorldId Moon's world ID
* @param moonSeed If non-zero, the ZeroTier address of any member of the moon to query for moon definition
* @return Error if moon was invalid or failed to be added
*/
public ResultCode orbit(
long moonWorldId,
long moonSeed) {
return orbit(nodeId, moonWorldId, moonSeed);
}
/**
* Remove a moon (does nothing if not present)
*
* @param moonWorldId World ID of moon to remove
* @return Error if anything bad happened
*/
public ResultCode deorbit(
long moonWorldId) {
return deorbit(nodeId, moonWorldId);
}
/**
* Get this node's 40-bit ZeroTier address
*
@ -420,6 +452,15 @@ public class Node {
long multicastGroup,
long multicastAdi);
private native ResultCode orbit(
long nodeId,
long moonWorldId,
long moonSeed);
private native ResultCode deorbit(
long nodeId,
long moonWorldId);
private native long address(long nodeId);
private native NodeStatus status(long nodeId);

View file

@ -0,0 +1,45 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*/
package com.zerotier.sdk;
import java.net.InetSocketAddress;
public interface PathChecker {
/**
* Callback to check whether a path should be used for ZeroTier traffic
*
* This function must return true if the path should be used.
*
* If no path check function is specified, ZeroTier will still exclude paths
* that overlap with ZeroTier-assigned and managed IP address blocks. But the
* use of a path check function is recommended to ensure that recursion does
* not occur in cases where addresses are assigned by the OS or managed by
* an out of band mechanism like DHCP. The path check function should examine
* all configured ZeroTier interfaces and check to ensure that the supplied
* addresses will not result in ZeroTier traffic being sent over a ZeroTier
* interface (recursion).
*
* Obviously this is not required in configurations where this can't happen,
* such as network containers or embedded.
*
* @param ztAddress ZeroTier address or 0 for none/any
* @param localAddress Local interface address
* @param remoteAddress remote address
*/
boolean onPathCheck(long ztAddress, InetSocketAddress localAddress, InetSocketAddress remoteAddress);
/**
* Function to get physical addresses for ZeroTier peers
*
* If provided this function will be occasionally called to get physical
* addresses that might be tried to reach a ZeroTier address.
*
* @param ztAddress ZeroTier address (least significant 40 bits)
* @param ss_family desired address family or -1 for any
* @return address and port of ztAddress or null
*/
InetSocketAddress onPathLookup(long ztAddress, int ss_family);
}

View file

@ -34,8 +34,6 @@ import java.util.ArrayList;
*/
public final class Peer {
private long address;
private long lastUnicastFrame;
private long lastMulticastFrame;
private int versionMajor;
private int versionMinor;
private int versionRev;
@ -52,20 +50,6 @@ public final class Peer {
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
*/

View file

@ -37,7 +37,6 @@ public final class PeerPhysicalPath {
private long lastSend;
private long lastReceive;
private boolean fixed;
private boolean active;
private boolean preferred;
private PeerPhysicalPath() {}
@ -70,13 +69,6 @@ public final class PeerPhysicalPath {
return fixed;
}
/**
* Is path active?
*/
public final boolean isActive() {
return active;
}
/**
* Is path preferred?
*/

View file

@ -34,12 +34,12 @@ public enum PeerRole {
PEER_ROLE_LEAF,
/**
* relay node
* moon root
*/
PEER_ROLE_RELAY,
PEER_ROLE_MOON,
/**
* root server
* planetary root
*/
PEER_ROLE_ROOT
PEER_ROLE_PLANET
}

View file

@ -50,6 +50,7 @@ public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConf
private boolean enabled;
private long netconfRevision;
private InetSocketAddress[] assignedAddresses;
private VirtualNetworkRoute[] routes;
private VirtualNetworkConfig() {
@ -60,13 +61,24 @@ public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConf
if(assignedAddresses.length == cfg.assignedAddresses.length) {
for(int i = 0; i < assignedAddresses.length; ++i) {
if(!assignedAddresses[i].equals(cfg.assignedAddresses[i])) {
return false;
aaEqual = false;
}
}
} else {
aaEqual = false;
}
boolean routesEqual = true;
if(routes.length == cfg.routes.length) {
for (int i = 0; i < routes.length; ++i) {
if (!routes[i].equals(cfg.routes[i])) {
routesEqual = false;
}
}
} else {
routesEqual = false;
}
return nwid == cfg.nwid &&
mac == cfg.mac &&
name.equals(cfg.name) &&
@ -78,7 +90,7 @@ public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConf
broadcastEnabled == cfg.broadcastEnabled &&
portError == cfg.portError &&
enabled == cfg.enabled &&
aaEqual;
aaEqual && routesEqual;
}
public int compareTo(VirtualNetworkConfig cfg) {
@ -188,4 +200,11 @@ public final class VirtualNetworkConfig implements Comparable<VirtualNetworkConf
public final InetSocketAddress[] assignedAddresses() {
return assignedAddresses;
}
/**
* ZeroTier-assigned routes (in {@link com.zerotier.sdk.VirtualNetworkRoute} objects)
*
* @return
*/
public final VirtualNetworkRoute[] routes() { return routes; }
}

View file

@ -0,0 +1,102 @@
/*
* 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 final class VirtualNetworkRoute implements Comparable<VirtualNetworkRoute>
{
private VirtualNetworkRoute() {
target = null;
via = null;
flags = 0;
metric = 0;
}
/**
* Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default
*/
public InetSocketAddress target;
/**
* Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway)
*/
public InetSocketAddress via;
/**
* Route flags
*/
public int flags;
/**
* Route metric (not currently used)
*/
public int metric;
@Override
public int compareTo(VirtualNetworkRoute other) {
return target.toString().compareTo(other.target.toString());
}
public boolean equals(VirtualNetworkRoute other) {
boolean targetEquals;
if (target == null && other.target == null) {
targetEquals = true;
}
else if (target == null && other.target != null) {
targetEquals = false;
}
else if (target != null && other.target == null) {
targetEquals = false;
}
else {
targetEquals = target.equals(other.target);
}
boolean viaEquals;
if (via == null && other.via == null) {
viaEquals = true;
}
else if (via == null && other.via != null) {
viaEquals = false;
}
else if (via != null && other.via == null) {
viaEquals = false;
}
else {
viaEquals = via.equals(other.via);
}
return viaEquals &&
viaEquals &&
flags == other.flags &&
metric == other.metric;
}
}