Updated bindings

This commit is contained in:
Joseph Henry
2017-11-21 15:24:51 -08:00
parent d6fcdf5277
commit a4a5ea5c31
14 changed files with 636 additions and 33 deletions

View File

@@ -28,6 +28,7 @@
import zerotier.ZeroTier;
import java.net.*;
import java.lang.Thread;
public class ExampleApp {
@@ -45,23 +46,108 @@ public class ExampleApp {
new Thread(new Runnable() {
public void run() {
System.out.println("starting libzt");
//libzt.startjoin("config_path", "123456789abcdeff");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "17d709436c2c5367");
System.out.println("started.");
String path = "/Users/joseph/op/zt/libzt/ztjni";
long nwid = 0xa09acf0233ac70fdL;
// METHOD 1 (easy)
// Blocking call that waits for the core, userspace stack and IP assignment before unblocking
if (true)
{
libzt.startjoin(path, nwid);
}
// METHOD 2
// Optionally-nonblocking call. You'll have to use the below process to determine when you
// are allowed to stack making socket calls. The advantage of this method is that you can
// get your nodeId before joining the network.
if (false) {
libzt.start(path, true);
// Wait for core service to start
while(!libzt.core_running()) {
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
System.out.println("core started");
long nodeId = libzt.get_node_id();
System.out.println("nodeId=" + Long.toHexString(nodeId));
libzt.join(nwid);
// Wait for userspace stack to start, we trigger this by joining a network
while(!libzt.stack_running()) {
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
System.out.println("core and stack started, now ready for socket API calls");
int num_addresses = libzt.get_num_assigned_addresses(nwid);
System.out.println("number of assigned addresses for this node on this network = " + String.valueOf(num_addresses));
// get IPv4 address
//InetAddress assigned = libzt.get_address(nwid, libzt.AF_INET6);
//System.out.println("assigned address = " + assigned.toString());
// get address at arbitrary (index < num_addresses)
//assigned = libzt.get_address_at_index(nwid, 0);
//System.out.println("assigned address = " + assigned.toString());
// get IPv6 address
//assigned = libzt.get_address(nwid, libzt.AF_INET6);
//System.out.println("assigned address = " + assigned.toString());
String homePath = libzt.get_path();
System.out.println("homePath=" + homePath);
while(!libzt.has_address(nwid)) {
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
//InetAddress assigned = libzt.get_address(nwid);
//System.out.println("assigned address = " + assigned.toString());
int fd = 0, err = 0;
if ((fd = libzt.socket(libzt.AF_INET, libzt.SOCK_STREAM, 0)) < 0) {
System.out.println("error creating socket");
return;
}
System.out.println("Created socket");
InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 3434);
while(true)
{
try { Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
/*
InetSocketAddress remoteAddr = new InetSocketAddress("172.27.54.9", 3434);
if ((err = libzt.connect(fd, remoteAddr)) < 0) {
System.out.println("error connecting");
return;
}
*/
/*
InetSocketAddress localAddr = new InetSocketAddress("0.0.0.0", 3434);
if ((err = libzt.bind(fd, addr)) < 0) {
System.out.println("error binding socket to virtual interface");
return;
}
*/
}
}).start();

View File

@@ -11,8 +11,15 @@ endif
example_java_app:
javac *.java
example_java_app_1.6:
javac -source 1.6 -bootclasspath /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.41.x86_64/jre/lib/rt.jar -target 1.6 *.java
copy_dynamic_lib:
cp ../../../$(BUILD)/$(SHARED_LIB) .
jar:
jar cf libzt.jar libzt.dylib zerotier/ZeroTier.class
clean:
-find . -type f \( -name '*.class' \) -delete
-rm -rf *.jar *.dylib *.so
-find . -type f \( -name '*.class' \) -delete

View File

@@ -1,19 +1,24 @@
## ZeroTier with Java via JNI
***
To get this example project to work, do the following:
### Example App
- From libzt main directory, build shared library: `make shared_jni_lib`
- Copy the resultant dynamic library (`*.so` or `*.dylib`) from `build/` to this current directory
- Change to this directory and `make example_java_app`
- Run: `java -cp "." ExampleApp`
### JAR file (with embedded C++ dynamic library)
```
make example_app
make jar
```
Notes:
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.
***
More resources on JNI usage:

View File

@@ -29,10 +29,9 @@ package zerotier;
import java.net.*;
public class ZeroTier {
// socket families
public static int AF_UNIX = 1;
public static int AF_INET = 2;
public static int AF_INET6 = 30;
// socket types
public static int SOCK_STREAM = 1;
public static int SOCK_DGRAM = 2;
@@ -45,33 +44,38 @@ public class ZeroTier {
// fcntl cmds
public static int F_GETFL = 3;
public static int F_SETFL = 4;
// basic service controls
public native void start(String homeDir, boolean blocking);
public native void startjoin(String homeDir, String nwid);
// service controls
public native void start(String homePath, boolean blocking);
public native void startjoin(String homePath, long nwid);
public native void stop();
public native boolean running();
public native void join(String nwid);
public native void leave(String nwid);
// advanced service controls
public native void get_path();
public native int get_id();
public native boolean core_running();
public native boolean stack_running();
public native boolean ready();
public native int join(long nwid);
public native int leave(long nwid);
public native String get_path();
public native long get_node_id();
public native int get_num_assigned_addresses(long nwid);
public native InetAddress get_address_at_index(long nwid, int index);
public native boolean has_address(long nwid);
public native InetAddress get_address(long nwid, int address_family);
public native void get_6plane_addr();
public native void get_rfc4193_addr();
// socket API
public native int socket(int family, int type, int protocol);
public native int connect(int fd, String addr, int port);
public native int connect(int fd, InetSocketAddress addr);
public native int bind(int fd, InetSocketAddress addr);
public native int listen(int fd, int backlog);
public native int accept(int fd, Address addr);
public native int accept(int fd, InetSocketAddress addr);
public native int accept4(int fd, String addr, int port);
public native int close(int fd);
//public native int setsockopt();
//public native int getsockopt();
public native int sendto(int fd, byte[] buf, int len, int flags, InetSocketAddress addr);
public native int send(int fd, byte[] buf, int len, int flags);
public native int recvfrom(int fd, byte[] buf, int len, int flags, InetSocketAddress addr);
public native int read(int fd, byte[] buf, int len);
public native int write(int fd, byte[] buf, int len);
public native int sendto(int fd, byte[] buf, int len, int flags, Address addr);
public native int send(int fd, byte[] buf, int len, int flags);
public native int recvfrom(int fd, byte[] buf, int len, int flags, Address addr);
public native int shutdown(int fd, int how);
//public native int getsockname();
//public native int getpeername();