From 8fb9df97511532a28ac8225282318dd1f31be0e4 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Wed, 3 Jun 2015 18:35:38 -0700
Subject: [PATCH 01/27] delete dead test code
---
.../com/zerotier/one/AndroidFileProvider.java | 43 ----
java/src/com/zerotier/one/DataStore.java | 73 -------
.../zerotier/one/DataStoreFileProvider.java | 12 --
.../com/zerotier/one/JavaFileProvider.java | 46 ----
java/src/com/zerotier/one/OneService.java | 204 ------------------
5 files changed, 378 deletions(-)
delete mode 100644 java/src/com/zerotier/one/AndroidFileProvider.java
delete mode 100644 java/src/com/zerotier/one/DataStore.java
delete mode 100644 java/src/com/zerotier/one/DataStoreFileProvider.java
delete mode 100644 java/src/com/zerotier/one/JavaFileProvider.java
delete mode 100644 java/src/com/zerotier/one/OneService.java
diff --git a/java/src/com/zerotier/one/AndroidFileProvider.java b/java/src/com/zerotier/one/AndroidFileProvider.java
deleted file mode 100644
index 0988f9dfa..000000000
--- a/java/src/com/zerotier/one/AndroidFileProvider.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package com.zerotier.one;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import android.content.Context;
-import android.util.Log;
-
-public class AndroidFileProvider implements DataStoreFileProvider {
- private static final String TAG = "AndroidFileProvider";
-
- Context _ctx;
-
- public AndroidFileProvider(Context ctx) {
- this._ctx = ctx;
- }
-
- @Override
- public FileInputStream getInputFileStream(String name)
- throws FileNotFoundException {
- Log.d(TAG, "Returning FileInputStream for: " + name);
- return _ctx.openFileInput(name);
- }
-
- @Override
- public FileOutputStream getOutputFileStream(String name)
- throws FileNotFoundException {
- Log.d(TAG, "Returning FileOutputStream for: " + name);
- return _ctx.openFileOutput(name, Context.MODE_PRIVATE);
- }
-
- @Override
- public void deleteFile(String name) throws IOException {
- boolean success = _ctx.deleteFile(name);
- if(!success)
- {
- throw new IOException("Unable to delete file.");
- }
- }
-
-}
diff --git a/java/src/com/zerotier/one/DataStore.java b/java/src/com/zerotier/one/DataStore.java
deleted file mode 100644
index d15b2d3d2..000000000
--- a/java/src/com/zerotier/one/DataStore.java
+++ /dev/null
@@ -1,73 +0,0 @@
-package com.zerotier.one;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import com.zerotier.sdk.DataStoreGetListener;
-import com.zerotier.sdk.DataStorePutListener;
-
-public class DataStore implements DataStoreGetListener, DataStorePutListener {
-
- private DataStoreFileProvider _provider;
-
- public DataStore(DataStoreFileProvider provider) {
- this._provider = provider;
- }
-
- @Override
- public int onDataStorePut(String name, byte[] buffer, boolean secure) {
- System.out.println("Writing File: " + name);
- try {
- FileOutputStream fos = _provider.getOutputFileStream(name);
- fos.write(buffer);
- fos.close();
- return 0;
- } catch (FileNotFoundException fnf) {
- fnf.printStackTrace();
- return -1;
- } catch (IOException io) {
- io.printStackTrace();
- return -2;
- }
- }
-
- @Override
- public int onDelete(String name) {
- System.out.println("Deleting File: " + name);
- try {
- _provider.deleteFile(name);
- return 0;
- } catch (IOException ex) {
- ex.printStackTrace();
- return -1;
- }
- }
-
- @Override
- public long onDataStoreGet(String name, byte[] out_buffer,
- long bufferIndex, long[] out_objectSize) {
- System.out.println("Reading File: " + name);
- try {
- FileInputStream fin = _provider.getInputFileStream(name);
- out_objectSize[0] = fin.getChannel().size();
- if(bufferIndex > 0)
- {
- fin.skip(bufferIndex);
- }
- int read = fin.read(out_buffer);
- fin.close();
- return read;
- } catch (FileNotFoundException fnf) {
- // Can't read a file that doesn't exist!
- out_objectSize[0] = 0;
- return 0;
- } catch (IOException io) {
- io.printStackTrace();
- return -2;
- }
- }
-
-
-}
diff --git a/java/src/com/zerotier/one/DataStoreFileProvider.java b/java/src/com/zerotier/one/DataStoreFileProvider.java
deleted file mode 100644
index ffe078eb4..000000000
--- a/java/src/com/zerotier/one/DataStoreFileProvider.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package com.zerotier.one;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-public interface DataStoreFileProvider {
- FileInputStream getInputFileStream(String name) throws FileNotFoundException;
- FileOutputStream getOutputFileStream(String name) throws FileNotFoundException;
- void deleteFile(String name) throws IOException;
-}
diff --git a/java/src/com/zerotier/one/JavaFileProvider.java b/java/src/com/zerotier/one/JavaFileProvider.java
deleted file mode 100644
index 41889e2f1..000000000
--- a/java/src/com/zerotier/one/JavaFileProvider.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.zerotier.one;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-public class JavaFileProvider implements DataStoreFileProvider {
- private String _path;
-
- public JavaFileProvider(String path) {
- this._path = path;
- }
-
- @Override
- public FileInputStream getInputFileStream(String name)
- throws FileNotFoundException {
- File f = new File(_path + File.separator + name);
- return new FileInputStream(f);
- }
-
- @Override
- public FileOutputStream getOutputFileStream(String name)
- throws FileNotFoundException {
- File f = new File(_path + File.separator + name);
- if(!f.exists())
- {
- try {
- f.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return new FileOutputStream(f);
- }
-
- @Override
- public void deleteFile(String name) throws IOException {
- File f = new File(_path + File.separator + name);
- boolean success = f.delete();
- if(!success) {
- throw new IOException("Unable to delete file: " + _path + File.pathSeparator + name);
- }
- }
-}
diff --git a/java/src/com/zerotier/one/OneService.java b/java/src/com/zerotier/one/OneService.java
deleted file mode 100644
index 1d3e34c85..000000000
--- a/java/src/com/zerotier/one/OneService.java
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * 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 .
- *
- * --
- *
- * 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.one;
-
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.DatagramSocket;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.SocketException;
-import java.net.SocketTimeoutException;
-
-import com.zerotier.sdk.Event;
-import com.zerotier.sdk.EventListener;
-import com.zerotier.sdk.Node;
-import com.zerotier.sdk.PacketSender;
-import com.zerotier.sdk.ResultCode;
-import com.zerotier.sdk.Version;
-import com.zerotier.sdk.VirtualNetworkConfig;
-import com.zerotier.sdk.VirtualNetworkConfigListener;
-import com.zerotier.sdk.VirtualNetworkConfigOperation;
-import com.zerotier.sdk.VirtualNetworkFrameListener;
-
-public class OneService extends Thread implements Runnable, PacketSender,
- EventListener, VirtualNetworkConfigListener,
- VirtualNetworkFrameListener {
- private Node _node;
- private int _port;
-
- private DatagramSocket _udpSocket;
- private ServerSocket _tcpSocket;
- private DataStore _ds;
- private long _nextBackgroundTaskDeadline = 0;
-
- private final Thread _udpReceiveThread = new Thread() {
- @Override
- public void run() {
- try {
- long[] bgtask = new long[1];
- byte[] buffer = new byte[16384];
- while(true) {
-
- bgtask[0] = 0;
- DatagramPacket p = new DatagramPacket(buffer, buffer.length);
-
- try {
- _udpSocket.receive(p);
- if(p.getLength() > 0)
- {
- System.out.println("Got Data From: " + p.getAddress().toString() +":" + p.getPort());
-
- _node.processWirePacket(System.currentTimeMillis(), new InetSocketAddress(p.getAddress(), p.getPort()), p.getData(), bgtask);
- _nextBackgroundTaskDeadline = bgtask[0];
- }
- } catch (SocketTimeoutException e) {}
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
-
-
- public OneService(DataStoreFileProvider prov, int port) {
- this._port = port;
- this._ds = new DataStore(prov);
-
- try {
- _udpSocket = new DatagramSocket(_port);
- _udpSocket.setSoTimeout(100);
- _tcpSocket = new ServerSocket();
- _tcpSocket.bind(new InetSocketAddress("127.0.0.1", _port));
- } catch (SocketException e) {
- e.printStackTrace();
- return;
- } catch (IOException e) {
- e.printStackTrace();
- return;
- }
-
- _udpReceiveThread.start();
-
- _node = new Node(
- System.currentTimeMillis(),
- _ds,
- _ds,
- this,
- this,
- this,
- this);
- }
-
- @Override
- public void run() {
- if(_node == null)
- return;
-
- while(true) {
- try {
-
- long dl = _nextBackgroundTaskDeadline;
- long now = System.currentTimeMillis();
-
- if (dl <= now) {
- long[] returnDeadline = {0};
- ResultCode rc = _node.processBackgroundTasks(now, returnDeadline);
- _nextBackgroundTaskDeadline = returnDeadline[0];
-
- if(rc != ResultCode.RESULT_OK) {
- System.out.println(rc.toString());
- }
- }
-
- long delay = (dl > now) ? (dl - now) : 100;
- Thread.sleep(delay);
-
- } catch (Exception ex) {
- System.out.println("Exception in run loop: " + ex.getMessage());
- ex.printStackTrace();
- }
- }
- }
-
- @Override
- public int onSendPacketRequested(InetSocketAddress addr, byte[] packetData) {
- System.out.println("onSendPacketRequested to: " + addr.getHostString() +":"+ addr.getPort() + " ");
-
- if(_udpSocket == null)
- return -1;
- try {
- DatagramPacket p = new DatagramPacket(packetData, packetData.length, addr);
- _udpSocket.send(p);
- System.out.println("Sent");
- } catch (Exception e) {
- System.out.println("Error sending datagram: " + e.getMessage());
- return -1;
- }
- return 0;
- }
-
- @Override
- public void onVirtualNetworkFrame(long nwid, long srcMac, long destMac,
- long etherType, long vlanId, byte[] frameData) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public int onNetworkConfigurationUpdated(long nwid,
- VirtualNetworkConfigOperation op, VirtualNetworkConfig config) {
- // TODO Auto-generated method stub
- return 0;
- }
-
- @Override
- public void onEvent(Event event) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onNetworkError(Event event, InetSocketAddress source) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onOutOfDate(Version newVersion) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onTrace(String message) {
- // TODO Auto-generated method stub
-
- }
-}
From b84dba3ecb2f750f5b5fda39544e20f741f961a4 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Wed, 3 Jun 2015 21:29:07 -0700
Subject: [PATCH 02/27] more logging
---
java/jni/com_zerotierone_sdk_Node.cpp | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index f04058130..c84524fa8 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -416,10 +416,15 @@ namespace {
{
// set operation
jbyteArray bufferObj = env->NewByteArray(bufferSize);
+ if(env->ExceptionCheck() || bufferObj == NULL)
+ {
+ LOGE("Error creating byte array buffer!");
+ return -4;
+ }
+
env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
bool bsecure = secure != 0;
-
return env->CallIntMethod(ref->dataStorePutListener,
dataStorePutCallbackMethod,
nameStr, bufferObj, bsecure);
@@ -736,12 +741,14 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
if(node == NULL)
{
// cannot find valid node. We should never get here.
+ LOGE("Couldn't find a valid node!");
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
}
unsigned int nbtd_len = env->GetArrayLength(out_nextBackgroundTaskDeadline);
if(nbtd_len < 1)
{
+ LOGE("nbtd_len < 1");
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
}
@@ -751,6 +758,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
jclass inetAddressClass = cache.findClass("java/net/InetAddress");
if(inetAddressClass == NULL)
{
+ LOGE("Can't find InetAddress class");
// can't find java.net.InetAddress
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
}
@@ -849,6 +857,10 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
packetData,
packetLength,
&nextBackgroundTaskDeadline);
+ if(rc != ZT1_RESULT_OK)
+ {
+ LOGE("ZT1_Node_processWirePacket returned: %d", rc);
+ }
jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
From 7cc64c5cb69f2962d32ca41f6dc12499964d22a7 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Wed, 3 Jun 2015 21:29:19 -0700
Subject: [PATCH 03/27] Might help to set the enabled field on a
VirtualNetworkConfig object :)
---
java/jni/ZT1_jniutils.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/java/jni/ZT1_jniutils.cpp b/java/jni/ZT1_jniutils.cpp
index e6404e87e..36f68f9e2 100644
--- a/java/jni/ZT1_jniutils.cpp
+++ b/java/jni/ZT1_jniutils.cpp
@@ -822,6 +822,7 @@ jobject newNetworkConfig(JNIEnv *env, const ZT1_VirtualNetworkConfig &vnetConfig
env->SetBooleanField(vnetConfigObj, dhcpField, vnetConfig.dhcp);
env->SetBooleanField(vnetConfigObj, bridgeField, vnetConfig.bridge);
env->SetBooleanField(vnetConfigObj, broadcastEnabledField, vnetConfig.broadcastEnabled);
+ env->SetBooleanField(vnetConfigObj, enabledField, vnetConfig.enabled);
env->SetIntField(vnetConfigObj, portErrorField, vnetConfig.portError);
jclass multicastGroupClass = cache.findClass("com/zerotier/sdk/MulticastGroup");
From ced040c5033bd61a963e65e8e8525459c4b8b59d Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 9 Jun 2015 19:38:05 -0700
Subject: [PATCH 04/27] Logging and adding .equals() methods to MulticastGroup
and VirtualNetworkCofnig
---
java/jni/ZT1_jniutils.cpp | 10 ++---
java/jni/com_zerotierone_sdk_Node.cpp | 12 +++---
java/src/com/zerotier/sdk/MulticastGroup.java | 4 ++
.../zerotier/sdk/VirtualNetworkConfig.java | 38 +++++++++++++++++++
4 files changed, 54 insertions(+), 10 deletions(-)
diff --git a/java/jni/ZT1_jniutils.cpp b/java/jni/ZT1_jniutils.cpp
index 36f68f9e2..1e8a48bff 100644
--- a/java/jni/ZT1_jniutils.cpp
+++ b/java/jni/ZT1_jniutils.cpp
@@ -343,18 +343,18 @@ jobject newInetSocketAddress(JNIEnv *env, const sockaddr_storage &addr)
{
case AF_INET6:
{
- LOGD("IPV6 Address");
+ LOGV("IPV6 Address");
sockaddr_in6 *ipv6 = (sockaddr_in6*)&addr;
port = ntohs(ipv6->sin6_port);
- LOGD("Port %d", port);
+ LOGV("Port %d", port);
}
break;
case AF_INET:
{
- LOGD("IPV4 Address");
+ LOGV("IPV4 Address");
sockaddr_in *ipv4 = (sockaddr_in*)&addr;
port = ntohs(ipv4->sin_port);
- LOGD("Port: %d", port);
+ LOGV("Port: %d", port);
}
break;
default:
@@ -818,7 +818,7 @@ jobject newNetworkConfig(JNIEnv *env, const ZT1_VirtualNetworkConfig &vnetConfig
}
env->SetObjectField(vnetConfigObj, typeField, typeObject);
- env->SetIntField(vnetConfigObj, mtuField, vnetConfig.mtu);
+ 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);
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index c84524fa8..2a90bb85b 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -92,7 +92,7 @@ namespace {
enum ZT1_VirtualNetworkConfigOperation operation,
const ZT1_VirtualNetworkConfig *config)
{
- LOGD("VritualNetworkConfigFunctionCallback");
+ LOGV("VritualNetworkConfigFunctionCallback");
JniRef *ref = (JniRef*)userData;
JNIEnv *env = NULL;
ref->jvm->GetEnv((void**)&env, JNI_VERSION_1_6);
@@ -142,7 +142,9 @@ namespace {
const void *frameData,
unsigned int frameLength)
{
- LOGD("VirtualNetworkFrameFunctionCallback");
+ LOGV("VirtualNetworkFrameFunctionCallback");
+ unsigned char* local = (unsigned char*)frameData;
+ LOGV("Type Bytes: 0x%02x%02x", local[12], local[13]);
JniRef *ref = (JniRef*)userData;
assert(ref->node == node);
JNIEnv *env = NULL;
@@ -188,7 +190,7 @@ namespace {
void EventCallback(ZT1_Node *node,void *userData,enum ZT1_Event event, const void *data)
{
- LOGD("EventCallback");
+ LOGV("EventCallback");
JniRef *ref = (JniRef*)userData;
assert(ref->node == node);
JNIEnv *env = NULL;
@@ -436,7 +438,7 @@ namespace {
const void *buffer,
unsigned int bufferSize)
{
- LOGD("WirePacketSendFunction(%p, %p, %d)", address, buffer, bufferSize);
+ LOGV("WirePacketSendFunction(%p, %p, %d)", address, buffer, bufferSize);
JniRef *ref = (JniRef*)userData;
assert(ref->node == node);
@@ -464,7 +466,7 @@ namespace {
env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
int retval = env->CallIntMethod(ref->packetSender, packetSenderCallbackMethod, addressObj, bufferObj);
- LOGD("JNI Packet Sender returned: %d", retval);
+ LOGV("JNI Packet Sender returned: %d", retval);
return retval;
}
diff --git a/java/src/com/zerotier/sdk/MulticastGroup.java b/java/src/com/zerotier/sdk/MulticastGroup.java
index 5c4df87a4..68114424f 100644
--- a/java/src/com/zerotier/sdk/MulticastGroup.java
+++ b/java/src/com/zerotier/sdk/MulticastGroup.java
@@ -33,6 +33,10 @@ public final class MulticastGroup {
private long mac;
private long adi;
+ public boolean equals(MulticastGroup other) {
+ return mac == other.mac && adi == other.adi;
+ }
+
/**
* MAC address (least significant 48 bits)
*/
diff --git a/java/src/com/zerotier/sdk/VirtualNetworkConfig.java b/java/src/com/zerotier/sdk/VirtualNetworkConfig.java
index 2be03acb3..35453ddc3 100644
--- a/java/src/com/zerotier/sdk/VirtualNetworkConfig.java
+++ b/java/src/com/zerotier/sdk/VirtualNetworkConfig.java
@@ -54,6 +54,44 @@ public final class VirtualNetworkConfig {
}
+ public boolean equals(VirtualNetworkConfig cfg) {
+ boolean mcgEqual = true;
+ if(multicastSubscriptions.length == cfg.multicastSubscriptions.length) {
+ for(int i = 0; i < multicastSubscriptions.length; ++i) {
+ if(!multicastSubscriptions[i].equals(cfg.multicastSubscriptions[i]))
+ {
+ return false;
+ }
+ }
+ } else {
+ mcgEqual = false;
+ }
+
+ 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 &&
+ mcgEqual && aaEqual;
+ }
+
/**
* 64-bit ZeroTier network ID
*/
From 3013d90f579ac970e41000d6732e798589c3ac90 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 9 Jun 2015 22:38:31 -0700
Subject: [PATCH 05/27] ignore windows binary output
---
.gitignore | 1 +
1 file changed, 1 insertion(+)
diff --git a/.gitignore b/.gitignore
index 498119e3c..95f6ed5b6 100755
--- a/.gitignore
+++ b/.gitignore
@@ -71,3 +71,4 @@ java/doc/
java/build_win64/
java/build_win32/
/java/mac32_64/
+windows/ZeroTierOne/Debug/
From 4dc0ff8f13a2a5d852cda9302632d58b25d045ac Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 9 Jun 2015 23:12:44 -0700
Subject: [PATCH 06/27] Replace calls to GetArrayElements with
GetPrimitiveArrayCritical.
This puts code accessing the data in a critical section so that the GC cannot run while JNI has access to the array. This helps with stability somewhat, but I'm still getting some crashes in the GC
---
java/jni/com_zerotierone_sdk_Node.cpp | 54 ++++++++++++++-------------
1 file changed, 29 insertions(+), 25 deletions(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index 2a90bb85b..62fbba895 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -174,9 +174,9 @@ namespace {
return;
}
- jbyte *data = env->GetByteArrayElements(dataArray, NULL);
+ void *data = env->GetPrimitiveArrayCritical(dataArray, NULL);
memcpy(data, frameData, frameLength);
- env->ReleaseByteArrayElements(dataArray, data, 0);
+ env->ReleasePrimitiveArrayCritical(dataArray, data, 0);
if(env->ExceptionCheck())
{
@@ -356,13 +356,13 @@ namespace {
if(retval > 0)
{
- jbyte *data = env->GetByteArrayElements(bufferObj, NULL);
+ void *data = env->GetPrimitiveArrayCritical(bufferObj, NULL);
memcpy(buffer, data, retval);
- env->ReleaseByteArrayElements(bufferObj, data, JNI_ABORT);
+ env->ReleasePrimitiveArrayCritical(bufferObj, data, 0);
- jlong *objSize = env->GetLongArrayElements(objectSizeObj, NULL);
+ jlong *objSize = (jlong*)env->GetPrimitiveArrayCritical(objectSizeObj, NULL);
*out_objectSize = (unsigned long)objSize[0];
- env->ReleaseLongArrayElements(objectSizeObj, objSize, JNI_ABORT);
+ env->ReleasePrimitiveArrayCritical(objectSizeObj, objSize, 0);
}
LOGI("Out Object Size: %lu", *out_objectSize);
@@ -700,7 +700,10 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
unsigned int vlanId = (unsigned int)in_vlanId;
unsigned int frameLength = env->GetArrayLength(in_frameData);
- jbyte *frameData =env->GetByteArrayElements(in_frameData, NULL);
+ void *frameData = env->GetPrimitiveArrayCritical(in_frameData, NULL);
+ void *localData = malloc(frameLength);
+ memcpy(localData, frameData, frameLength);
+ env->ReleasePrimitiveArrayCritical(in_frameData, frameData, 0);
uint64_t nextBackgroundTaskDeadline = 0;
@@ -712,15 +715,15 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
destMac,
etherType,
vlanId,
- (const void*)frameData,
+ (const void*)localData,
frameLength,
&nextBackgroundTaskDeadline);
- jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
- outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
- env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
+
- env->ReleaseByteArrayElements(in_frameData, frameData, 0);
+ jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
+ outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
+ env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
return createResultObject(env, rc);
}
@@ -816,8 +819,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
unsigned int addrSize = env->GetArrayLength(addressArray);
// get the address bytes
- jbyte *addr = env->GetByteArrayElements(addressArray, NULL);
-
+ jbyte *addr = (jbyte*)env->GetPrimitiveArrayCritical(addressArray, NULL);
sockaddr_storage remoteAddress = {};
@@ -842,13 +844,16 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
else
{
// unknown address type
- env->ReleaseByteArrayElements(addressArray, addr, 0);
+ env->ReleasePrimitiveArrayCritical(addressArray, addr, 0);
return createResultObject(env, ZT1_RESULT_FATAL_ERROR_INTERNAL);
}
-
+ env->ReleasePrimitiveArrayCritical(addressArray, addr, 0);
unsigned int packetLength = env->GetArrayLength(in_packetData);
- jbyte *packetData = env->GetByteArrayElements(in_packetData, NULL);
+ void *packetData = env->GetPrimitiveArrayCritical(in_packetData, NULL);
+ void *localData = malloc(packetLength);
+ memcpy(localData, packetData, packetLength);
+ env->ReleasePrimitiveArrayCritical(in_packetData, packetData, 0);
uint64_t nextBackgroundTaskDeadline = 0;
@@ -856,7 +861,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
node,
now,
&remoteAddress,
- packetData,
+ localData,
packetLength,
&nextBackgroundTaskDeadline);
if(rc != ZT1_RESULT_OK)
@@ -864,12 +869,11 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processWirePacket(
LOGE("ZT1_Node_processWirePacket returned: %d", rc);
}
- jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
- outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
- env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
+ free(localData);
- env->ReleaseByteArrayElements(addressArray, addr, 0);
- env->ReleaseByteArrayElements(in_packetData, packetData, 0);
+ jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
+ outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
+ env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
return createResultObject(env, rc);
}
@@ -904,9 +908,9 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processBackgroundTasks(
ZT1_ResultCode rc = ZT1_Node_processBackgroundTasks(node, now, &nextBackgroundTaskDeadline);
- jlong *outDeadline = env->GetLongArrayElements(out_nextBackgroundTaskDeadline, NULL);
+ jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
- env->ReleaseLongArrayElements(out_nextBackgroundTaskDeadline, outDeadline, 0);
+ env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
return createResultObject(env, rc);
}
From 6889fcfc28ad7df236e9883c17d73c9badb8edd5 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 9 Jun 2015 23:24:47 -0700
Subject: [PATCH 07/27] Looks like it was the JNI cash causing the crash.
Forcing it to look up classes and methods instead of caching them stopped the crashes in the GC. Will investigate more later.
---
java/jni/ZT1_jnicache.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/java/jni/ZT1_jnicache.cpp b/java/jni/ZT1_jnicache.cpp
index 8d6305cb2..4deec61f9 100644
--- a/java/jni/ZT1_jnicache.cpp
+++ b/java/jni/ZT1_jnicache.cpp
@@ -111,7 +111,7 @@ jclass JniCache::findClass(const std::string &name)
jclass cls = (jclass)env->NewGlobalRef(localCls);
- m_classes.insert(std::make_pair(name, cls));
+ //m_classes.insert(std::make_pair(name, cls));
return cls;
}
@@ -143,7 +143,7 @@ jmethodID JniCache::findMethod(jclass cls, const std::string &methodName, const
return NULL;
}
- m_methods.insert(std::make_pair(id, mid));
+ //m_methods.insert(std::make_pair(id, mid));
return mid;
}
@@ -173,7 +173,7 @@ jmethodID JniCache::findStaticMethod(jclass cls, const std::string &methodName,
return NULL;
}
- m_staticMethods.insert(std::make_pair(id, mid));
+ //m_staticMethods.insert(std::make_pair(id, mid));
return mid;
}
@@ -203,7 +203,7 @@ jfieldID JniCache::findField(jclass cls, const std::string &fieldName, const std
return NULL;
}
- m_fields.insert(std::make_pair(id, fid));
+ //m_fields.insert(std::make_pair(id, fid));
return fid;
}
@@ -233,7 +233,7 @@ jfieldID JniCache::findStaticField(jclass cls, const std::string &fieldName, con
return NULL;
}
- m_staticFields.insert(std::make_pair(id, fid));
+ //m_staticFields.insert(std::make_pair(id, fid));
return fid;
}
From 7e84f5a7dbb50913a78311df4f748455be0e1097 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 9 Jun 2015 23:24:54 -0700
Subject: [PATCH 08/27] killing whitespace
---
java/jni/com_zerotierone_sdk_Node.cpp | 2 --
1 file changed, 2 deletions(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index 62fbba895..9516db413 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -719,8 +719,6 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_processVirtualNetworkFrame(
frameLength,
&nextBackgroundTaskDeadline);
-
-
jlong *outDeadline = (jlong*)env->GetPrimitiveArrayCritical(out_nextBackgroundTaskDeadline, NULL);
outDeadline[0] = (jlong)nextBackgroundTaskDeadline;
env->ReleasePrimitiveArrayCritical(out_nextBackgroundTaskDeadline, outDeadline, 0);
From 472206dfb23e8c2d285d5cdf19ba1444d07e4d52 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Wed, 10 Jun 2015 20:16:13 -0700
Subject: [PATCH 09/27] Rename JniCache to JniLookup
Removed caching capabilities as the cached methods, fields, and objects appears to be broken on Android
---
java/CMakeLists.txt | 2 +-
java/jni/Android.mk | 2 +-
java/jni/ZT1_jnicache.cpp | 242 -------------------
java/jni/ZT1_jnilookup.cpp | 158 ++++++++++++
java/jni/{ZT1_jnicache.h => ZT1_jnilookup.h} | 23 +-
java/jni/ZT1_jniutils.cpp | 132 +++++-----
java/jni/com_zerotierone_sdk_Node.cpp | 68 +++---
7 files changed, 266 insertions(+), 361 deletions(-)
delete mode 100644 java/jni/ZT1_jnicache.cpp
create mode 100644 java/jni/ZT1_jnilookup.cpp
rename java/jni/{ZT1_jnicache.h => ZT1_jnilookup.h} (78%)
diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt
index db3eec1ca..382fd3cdb 100644
--- a/java/CMakeLists.txt
+++ b/java/CMakeLists.txt
@@ -54,7 +54,7 @@ set(src_files
../osdep/OSUtils.cpp
jni/com_zerotierone_sdk_Node.cpp
jni/ZT1_jniutils.cpp
- jni/ZT1_jnicache.cpp
+ jni/ZT1_jnilookup.cpp
)
set(include_dirs
diff --git a/java/jni/Android.mk b/java/jni/Android.mk
index bbf143485..9986c2c3e 100644
--- a/java/jni/Android.mk
+++ b/java/jni/Android.mk
@@ -39,6 +39,6 @@ LOCAL_SRC_FILES := \
LOCAL_SRC_FILES += \
com_zerotierone_sdk_Node.cpp \
ZT1_jniutils.cpp \
- ZT1_jnicache.cpp
+ ZT1_jnilookup.cpp
include $(BUILD_SHARED_LIBRARY)
\ No newline at end of file
diff --git a/java/jni/ZT1_jnicache.cpp b/java/jni/ZT1_jnicache.cpp
deleted file mode 100644
index 4deec61f9..000000000
--- a/java/jni/ZT1_jnicache.cpp
+++ /dev/null
@@ -1,242 +0,0 @@
-/*
- * 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 .
- *
- * --
- *
- * 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 "ZT1_jnicache.h"
-#include "ZT1_jniutils.h"
-
-JniCache::JniCache()
- : m_jvm(NULL)
- , m_classes()
- , m_fields()
- , m_staticFields()
- , m_methods()
- , m_staticMethods()
-{
- LOGV("JNI Cache Created");
-}
-
-JniCache::JniCache(JavaVM *jvm)
- : m_jvm(jvm)
- , m_classes()
- , m_fields()
- , m_staticFields()
- , m_methods()
- , m_staticMethods()
-{
- LOGV("JNI Cache Created");
-}
-
-JniCache::~JniCache()
-{
- LOGV("JNI Cache Destroyed");
- clearCache();
-}
-
-void JniCache::clearCache()
-{
- if(m_jvm)
- {
- JNIEnv *env = NULL;
- if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
- return;
-
- for(ClassMap::iterator iter = m_classes.begin(), end = m_classes.end();
- iter != end; ++iter)
- {
- env->DeleteGlobalRef(iter->second);
- }
- }
-
- m_classes.clear();
- m_fields.clear();
- m_staticFields.clear();
- m_methods.clear();
- m_staticMethods.clear();
-}
-
-void JniCache::setJavaVM(JavaVM *jvm)
-{
- LOGV("Assigned JVM to object");
- m_jvm = jvm;
-}
-
-
-jclass JniCache::findClass(const std::string &name)
-{
- if(!m_jvm)
- return NULL;
-
- ClassMap::iterator found = m_classes.find(name);
-
- if(found == m_classes.end())
- {
- // 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 localCls = env->FindClass(name.c_str());
- if(env->ExceptionCheck())
- {
- LOGE("Error finding class: %s", name.c_str());
- return NULL;
- }
-
- jclass cls = (jclass)env->NewGlobalRef(localCls);
-
- //m_classes.insert(std::make_pair(name, cls));
-
- return cls;
- }
-
- LOGV("Returning cached %s", name.c_str());
- return found->second;
-}
-
-
-jmethodID JniCache::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
-{
- if(!m_jvm)
- return NULL;
-
- std::string id = methodName + methodSig;
-
- MethodMap::iterator found = m_methods.find(id);
- if(found == m_methods.end())
- {
- 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;
- }
-
- //m_methods.insert(std::make_pair(id, mid));
-
- return mid;
- }
-
- return found->second;
-}
-
-jmethodID JniCache::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
-{
- if(!m_jvm)
- return NULL;
-
- std::string id = methodName + methodSig;
-
- MethodMap::iterator found = m_staticMethods.find(id);
- if(found == m_staticMethods.end())
- {
- 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;
- }
-
- //m_staticMethods.insert(std::make_pair(id, mid));
-
- return mid;
- }
-
- return found->second;
-}
-
-jfieldID JniCache::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
-{
- if(!m_jvm)
- return NULL;
-
- std::string id = fieldName + typeStr;
-
- FieldMap::iterator found = m_fields.find(id);
- if(found == m_fields.end())
- {
- 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;
- }
-
- //m_fields.insert(std::make_pair(id, fid));
-
- return fid;
- }
-
- return found->second;
-}
-
-jfieldID JniCache::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
-{
- if(!m_jvm)
- return NULL;
-
- std::string id = fieldName + typeStr;
-
- FieldMap::iterator found = m_staticFields.find(id);
- if(found == m_staticFields.end())
- {
- 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;
- }
-
- //m_staticFields.insert(std::make_pair(id, fid));
-
- return fid;
- }
-
- return found->second;
-}
\ No newline at end of file
diff --git a/java/jni/ZT1_jnilookup.cpp b/java/jni/ZT1_jnilookup.cpp
new file mode 100644
index 000000000..d8f5cc7f1
--- /dev/null
+++ b/java/jni/ZT1_jnilookup.cpp
@@ -0,0 +1,158 @@
+/*
+ * ZeroTier One - Network Virtualization Everywhere
+ * Copyright (C) 2011-2015 ZeroTier, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ *
+ * --
+ *
+ * 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 "ZT1_jnilookup.h"
+#include "ZT1_jniutils.h"
+
+JniLookup::JniLookup()
+ : m_jvm(NULL)
+{
+ LOGV("JNI Cache Created");
+}
+
+JniLookup::JniLookup(JavaVM *jvm)
+ : m_jvm(jvm)
+{
+ LOGV("JNI Cache Created");
+}
+
+JniLookup::~JniLookup()
+{
+ LOGV("JNI Cache Destroyed");
+}
+
+
+void JniLookup::setJavaVM(JavaVM *jvm)
+{
+ LOGV("Assigned JVM to object");
+ m_jvm = jvm;
+}
+
+
+jclass JniLookup::findClass(const std::string &name)
+{
+ if(!m_jvm)
+ return NULL;
+
+ // get the class from the JVM
+ JNIEnv *env = NULL;
+ if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
+ {
+ LOGE("Error retreiving JNI Environment");
+ return NULL;
+ }
+
+ jclass cls = env->FindClass(name.c_str());
+ if(env->ExceptionCheck())
+ {
+ LOGE("Error finding class: %s", name.c_str());
+ return NULL;
+ }
+
+ return cls;
+}
+
+
+jmethodID JniLookup::findMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
+{
+ if(!m_jvm)
+ return NULL;
+
+ JNIEnv *env = NULL;
+ if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
+ {
+ return NULL;
+ }
+
+ jmethodID mid = env->GetMethodID(cls, methodName.c_str(), methodSig.c_str());
+ if(env->ExceptionCheck())
+ {
+ return NULL;
+ }
+
+ return mid;
+}
+
+jmethodID JniLookup::findStaticMethod(jclass cls, const std::string &methodName, const std::string &methodSig)
+{
+ if(!m_jvm)
+ return NULL;
+
+ JNIEnv *env = NULL;
+ if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
+ {
+ return NULL;
+ }
+
+ jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), methodSig.c_str());
+ if(env->ExceptionCheck())
+ {
+ return NULL;
+ }
+
+ return mid;
+}
+
+jfieldID JniLookup::findField(jclass cls, const std::string &fieldName, const std::string &typeStr)
+{
+ if(!m_jvm)
+ return NULL;
+
+ JNIEnv *env = NULL;
+ if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
+ {
+ return NULL;
+ }
+
+ jfieldID fid = env->GetFieldID(cls, fieldName.c_str(), typeStr.c_str());
+ if(env->ExceptionCheck())
+ {
+ return NULL;
+ }
+
+ return fid;
+}
+
+jfieldID JniLookup::findStaticField(jclass cls, const std::string &fieldName, const std::string &typeStr)
+{
+ if(!m_jvm)
+ return NULL;
+
+ JNIEnv *env = NULL;
+ if(m_jvm->GetEnv((void**)&env, JNI_VERSION_1_6) != JNI_OK)
+ {
+ return NULL;
+ }
+
+ jfieldID fid = env->GetStaticFieldID(cls, fieldName.c_str(), typeStr.c_str());
+ if(env->ExceptionCheck())
+ {
+ return NULL;
+ }
+
+ return fid;
+}
\ No newline at end of file
diff --git a/java/jni/ZT1_jnicache.h b/java/jni/ZT1_jnilookup.h
similarity index 78%
rename from java/jni/ZT1_jnicache.h
rename to java/jni/ZT1_jnilookup.h
index 43f43a08c..cf496f882 100644
--- a/java/jni/ZT1_jnicache.h
+++ b/java/jni/ZT1_jnilookup.h
@@ -25,8 +25,8 @@
* LLC. Start here: http://www.zerotier.com/
*/
-#ifndef ZT1_JNICACHE_H_
-#define ZT1_JNICACHE_H_
+#ifndef ZT1_JNILOOKUP_H_
+#define ZT1_JNILOOKUP_H_
#include
#include
*
- * @param addr {@link InetSocketAddress} to send to
+ * @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 addr,
+ InetSocketAddress localAddr,
+ InetSocketAddress remoteAddr,
byte[] packetData);
}
From 7c3be2b5c152e8631237c1050e0c90ad37dfcb1d Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Sat, 26 Sep 2015 14:10:16 -0700
Subject: [PATCH 24/27] fix function signature in lookup for
onSendPacketRequested function
---
java/jni/com_zerotierone_sdk_Node.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index e513cdf52..e1b69d1e4 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -462,7 +462,7 @@ namespace {
}
jmethodID packetSenderCallbackMethod = lookup.findMethod(packetSenderClass,
- "onSendPacketRequested", "(Ljava/net/InetSocketAddress;[B)I");
+ "onSendPacketRequested", "(Ljava/net/InetSocketAddress;Ljava/net/InetSocketAddress;[B)I");
if(packetSenderCallbackMethod == NULL)
{
LOGE("Couldn't find onSendPacketRequested method");
From 75a191a8564030f4d5e99aca76b980e2d69abd20 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Sat, 26 Sep 2015 14:10:45 -0700
Subject: [PATCH 25/27] don't create an InetSocketAddress on local address if
it's equal to ZT_SOCKADDR_NULL
---
java/jni/com_zerotierone_sdk_Node.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index e1b69d1e4..e60426491 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -469,7 +469,12 @@ namespace {
return -2;
}
- jobject localAddressObj = newInetSocketAddress(env, *localAddress);
+ jobject localAddressObj = NULL;
+ if(memcmp(localAddress, &ZT_SOCKADDR_NULL, sizeof(sockaddr_storage)) != 0)
+ {
+ localAddressObj = newInetSocketAddress(env, *localAddress);
+ }
+
jobject remoteAddressObj = newInetSocketAddress(env, *remoteAddress);
jbyteArray bufferObj = env->NewByteArray(bufferSize);
env->SetByteArrayRegion(bufferObj, 0, bufferSize, (jbyte*)buffer);
From b7df177f33fd7a98da18ed9f756ee7272982a65d Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Mon, 2 Nov 2015 19:18:55 -0800
Subject: [PATCH 26/27] updates for origin/edge
---
java/jni/Android.mk | 2 +-
java/jni/Application.mk | 2 +-
java/jni/ZT_jniutils.cpp | 18 ------
java/jni/com_zerotierone_sdk_Node.cpp | 64 ++++----------------
java/src/com/zerotier/sdk/Event.java | 25 --------
java/src/com/zerotier/sdk/EventListener.java | 15 -----
6 files changed, 15 insertions(+), 111 deletions(-)
diff --git a/java/jni/Android.mk b/java/jni/Android.mk
index 22dfe4f15..3da3b04b6 100644
--- a/java/jni/Android.mk
+++ b/java/jni/Android.mk
@@ -15,7 +15,6 @@ LOCAL_SRC_FILES := \
$(ZT1)/ext/http-parser/http_parser.c \
$(ZT1)/node/C25519.cpp \
$(ZT1)/node/CertificateOfMembership.cpp \
- $(ZT1)/node/Defaults.cpp \
$(ZT1)/node/Dictionary.cpp \
$(ZT1)/node/Identity.cpp \
$(ZT1)/node/IncomingPacket.cpp \
@@ -26,6 +25,7 @@ LOCAL_SRC_FILES := \
$(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 \
diff --git a/java/jni/Application.mk b/java/jni/Application.mk
index 2a7897870..608e94c0a 100644
--- a/java/jni/Application.mk
+++ b/java/jni/Application.mk
@@ -1,5 +1,5 @@
NDK_TOOLCHAIN_VERSION := clang
APP_STL := c++_static
-APP_CPPFLAGS := -O3 -fPIC -fPIE -fvectorize -Wall -fstack-protector -fexceptions -fno-strict-aliasing -DZT_NO_TYPE_PUNNING=1
+APP_CPPFLAGS := -O3 -fPIC -fPIE -fvectorize -Wall -fstack-protector -fexceptions -fno-strict-aliasing -Wno-deprecated-register -DZT_NO_TYPE_PUNNING=1
APP_PLATFORM := android-14
APP_ABI := all
diff --git a/java/jni/ZT_jniutils.cpp b/java/jni/ZT_jniutils.cpp
index bd8d87084..ae1aa9298 100644
--- a/java/jni/ZT_jniutils.cpp
+++ b/java/jni/ZT_jniutils.cpp
@@ -133,15 +133,6 @@ jobject createEvent(JNIEnv *env, ZT_Event event)
case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
fieldName = "EVENT_FATAL_ERROR_IDENTITY_COLLISION";
break;
- case ZT_EVENT_SAW_MORE_RECENT_VERSION:
- fieldName = "EVENT_SAW_MORE_RECENT_VERSION";
- break;
- case ZT_EVENT_AUTHENTICATION_FAILURE:
- fieldName = "EVENT_AUTHENTICATION_FAILURE";
- break;
- case ZT_EVENT_INVALID_PACKET:
- fieldName = "EVENT_INVALID_PACKET";
- break;
case ZT_EVENT_TRACE:
fieldName = "EVENT_TRACE";
break;
@@ -425,7 +416,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
jfieldID addressField = NULL;
jfieldID lastSendField = NULL;
jfieldID lastReceiveField = NULL;
- jfieldID fixedField = NULL;
jfieldID activeField = NULL;
jfieldID preferredField = NULL;
@@ -459,13 +449,6 @@ jobject newPeerPhysicalPath(JNIEnv *env, const ZT_PeerPhysicalPath &ppp)
return NULL;
}
- fixedField = lookup.findField(pppClass, "fixed", "Z");
- if(env->ExceptionCheck() || fixedField == NULL)
- {
- LOGE("Error finding fixed field");
- return NULL;
- }
-
activeField = lookup.findField(pppClass, "active", "Z");
if(env->ExceptionCheck() || activeField == NULL)
{
@@ -503,7 +486,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, fixedField, ppp.fixed);
env->SetBooleanField(pppObject, activeField, ppp.active);
env->SetBooleanField(pppObject, preferredField, ppp.preferred);
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index e60426491..a4c677b7c 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -134,7 +134,8 @@ namespace {
(jlong)nwid, operationObject, networkConfigObject);
}
- void VirtualNetworkFrameFunctionCallback(ZT_Node *node,void *userData,
+ void VirtualNetworkFrameFunctionCallback(ZT_Node *node,
+ void *userData,
uint64_t nwid,
uint64_t sourceMac,
uint64_t destMac,
@@ -189,7 +190,10 @@ namespace {
}
- void EventCallback(ZT_Node *node,void *userData,enum ZT_Event event, const void *data)
+ void EventCallback(ZT_Node *node,
+ void *userData,
+ enum ZT_Event event,
+ const void *data)
{
LOGV("EventCallback");
JniRef *ref = (JniRef*)userData;
@@ -217,25 +221,6 @@ namespace {
return;
}
-
- jmethodID onOutOfDateMethod = lookup.findMethod(eventListenerClass,
- "onOutOfDate", "(Lcom/zerotier/sdk/Version;)V");
- if(onOutOfDateMethod == NULL)
- {
- LOGE("Couldn't find onOutOfDate method");
- return;
- }
-
-
- jmethodID onNetworkErrorMethod = lookup.findMethod(eventListenerClass,
- "onNetworkError", "(Lcom/zerotier/sdk/Event;Ljava/net/InetSocketAddress;)V");
- if(onNetworkErrorMethod == NULL)
- {
- LOGE("Couldn't find onNetworkError method");
- return;
- }
-
-
jmethodID onTraceMethod = lookup.findMethod(eventListenerClass,
"onTrace", "(Ljava/lang/String;)V");
if(onTraceMethod == NULL)
@@ -263,31 +248,6 @@ namespace {
env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
}
break;
- case ZT_EVENT_SAW_MORE_RECENT_VERSION:
- {
- LOGV("Version Event");
- // call onOutOfDate()
- if(data != NULL)
- {
- int *version = (int*)data;
- jobject verisonObj = newVersion(env, version[0], version[1], version[2], 0);
- env->CallVoidMethod(ref->eventListener, onOutOfDateMethod, verisonObj);
- }
- }
- break;
- case ZT_EVENT_AUTHENTICATION_FAILURE:
- case ZT_EVENT_INVALID_PACKET:
- {
- LOGV("Network Error Event");
- // call onNetworkError()
- if(data != NULL)
- {
- sockaddr_storage *addr = (sockaddr_storage*)data;
- jobject addressObj = newInetSocketAddress(env, *addr);
- env->CallVoidMethod(ref->eventListener, onNetworkErrorMethod, addressObj);
- }
- }
- break;
case ZT_EVENT_TRACE:
{
LOGV("Trace Event");
@@ -303,7 +263,8 @@ namespace {
}
}
- long DataStoreGetFunction(ZT_Node *node,void *userData,
+ long DataStoreGetFunction(ZT_Node *node,
+ void *userData,
const char *objectName,
void *buffer,
unsigned long bufferSize,
@@ -375,7 +336,8 @@ namespace {
return retval;
}
- int DataStorePutFunction(ZT_Node *node,void *userData,
+ int DataStorePutFunction(ZT_Node *node,
+ void *userData,
const char *objectName,
const void *buffer,
unsigned long bufferSize,
@@ -440,7 +402,8 @@ namespace {
}
}
- int WirePacketSendFunction(ZT_Node *node,void *userData,\
+ int WirePacketSendFunction(ZT_Node *node,
+ void *userData,
const struct sockaddr_storage *localAddress,
const struct sockaddr_storage *remoteAddress,
const void *buffer,
@@ -625,8 +588,7 @@ JNIEXPORT jobject JNICALL Java_com_zerotier_sdk_Node_node_1init(
&WirePacketSendFunction,
&VirtualNetworkFrameFunctionCallback,
&VirtualNetworkConfigFunctionCallback,
- &EventCallback,
- NULL);
+ &EventCallback);
if(rc != ZT_RESULT_OK)
{
diff --git a/java/src/com/zerotier/sdk/Event.java b/java/src/com/zerotier/sdk/Event.java
index 436b1b226..22d350e1e 100644
--- a/java/src/com/zerotier/sdk/Event.java
+++ b/java/src/com/zerotier/sdk/Event.java
@@ -86,31 +86,6 @@ public enum Event {
* umbrellas prevent rain and smoke detectors prevent fires. They do, right?
*/
EVENT_FATAL_ERROR_IDENTITY_COLLISION,
-
- /**
- * A more recent version was observed on the network
- *
- * Right now this is only triggered if a hub or rootserver reports a
- * more recent version, and only once. It can be used to trigger a
- * software update check.
- *
- * Meta-data: {@link Version}, more recent version number
- */
- EVENT_SAW_MORE_RECENT_VERSION,
-
- /**
- * A packet failed authentication
- *
- * Meta-data: {@link InetSocketAddress} containing origin address of packet
- */
- EVENT_AUTHENTICATION_FAILURE,
-
- /**
- * A received packet was not valid
- *
- * Meta-data: {@link InetSocketAddress} containing origin address of packet
- */
- EVENT_INVALID_PACKET,
/**
* Trace (debugging) message
diff --git a/java/src/com/zerotier/sdk/EventListener.java b/java/src/com/zerotier/sdk/EventListener.java
index bb191c1d5..91050aaa9 100644
--- a/java/src/com/zerotier/sdk/EventListener.java
+++ b/java/src/com/zerotier/sdk/EventListener.java
@@ -41,21 +41,6 @@ public interface EventListener {
*/
public void onEvent(Event event);
- /**
- * Callback for network error events: {@link Event.EVENT_AUTHENTICATION_FAILURE}, {link Event.EVENT_INVALID_PACKET}
- *
- * @param event {@link Event} enum
- * @param source {@link InetSocketAddress} containing the origin address of the packet
- */
- public void onNetworkError(Event event, InetSocketAddress source);
-
- /**
- * Callback when the node detects that it's out of date.
- *
- * @param newVersion {@link Version} object with the latest version of ZeroTier One
- */
- public void onOutOfDate(Version newVersion);
-
/**
* Trace messages
*
From eadeac0a42888a6f2fa53e2a802c6c4e43c055b3 Mon Sep 17 00:00:00 2001
From: Grant Limberg
Date: Tue, 3 Nov 2015 19:14:11 -0800
Subject: [PATCH 27/27] logging of events
---
java/jni/com_zerotierone_sdk_Node.cpp | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/java/jni/com_zerotierone_sdk_Node.cpp b/java/jni/com_zerotierone_sdk_Node.cpp
index a4c677b7c..17a9917a9 100644
--- a/java/jni/com_zerotierone_sdk_Node.cpp
+++ b/java/jni/com_zerotierone_sdk_Node.cpp
@@ -238,12 +238,32 @@ namespace {
switch(event)
{
case ZT_EVENT_UP:
+ {
+ LOGD("Event Up");
+ env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
+ break;
+ }
case ZT_EVENT_OFFLINE:
+ {
+ LOGD("Event Offline");
+ env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
+ break;
+ }
case ZT_EVENT_ONLINE:
+ {
+ LOGD("Event Online");
+ env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
+ break;
+ }
case ZT_EVENT_DOWN:
+ {
+ LOGD("Event Down");
+ env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
+ break;
+ }
case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
{
- LOGV("Regular Event");
+ LOGV("Identity Collision");
// call onEvent()
env->CallVoidMethod(ref->eventListener, onEventMethod, eventObject);
}