Modification to blocking bahavior of zts_start, is now settable. Improved address family handling in JNI

This commit is contained in:
Joseph Henry
2017-10-16 16:45:46 -07:00
parent ce22940f57
commit 45260c4e69
13 changed files with 199 additions and 242 deletions

View File

@@ -33,6 +33,7 @@ public class ExampleApp {
public native int loadsymbols();
public native void startOneService();
// load libzt.dylib or libzt.so
static {
System.loadLibrary("zt");
}
@@ -45,8 +46,18 @@ public class ExampleApp {
public void run() {
System.out.println("starting libzt");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212");
System.out.println("started.");
// start(path) will not block
// startjoin(path, nwid) will block
int fd = 0, err = 0;
if ((fd = libzt.socket(libzt.AF_INET, libzt.SOCK_STREAM, 0)) < 0) {
System.out.println("error creating socket");
return;
}
if ((err = libzt.bind(fd, "0.0.0.0", 3000)) < 0) {
System.out.println("error binding socket to virtual interface");
return;
}
}
}).start();

View File

@@ -103,8 +103,4 @@ public class Address
}
return sock_addr;
}
public boolean isValid() {
return port != -1 && !Address().startsWith("-1.-1.-1.-1/-1");
}
}

View File

@@ -26,22 +26,12 @@
package zerotier;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipError;
public class ZeroTier {
public static String Version() {
return "1.1.5";
}
// Socket families
// socket families
public static int AF_UNIX = 1;
public static int AF_INET = 2;
// Socket types
// socket types
public static int SOCK_STREAM = 1;
public static int SOCK_DGRAM = 2;
// fcntl flags
@@ -53,23 +43,44 @@ public class ZeroTier {
// fcntl cmds
public static int F_GETFL = 3;
public static int F_SETFL = 4;
public native void start(String homeDir);
// basic service controls
public native void start(String homeDir, boolean blocking);
public native void startjoin(String homeDir, String 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 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 bind(int fd, String addr, int port);
public native int accept4(int fd, String addr, int port);
public native int accept(int fd, Address addr);
public native int listen(int fd, int backlog);
public native int accept(int fd, Address 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 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();
//public native int gethostname();
///public native int sethostname();
//public native int gethostbyname();
//public native int poll();
//public native int select();
public native int fcntl(int sock, int cmd, int flag);
//public native int ioctl(int fd, long request, ? );
// stack controls
public native int add_dns();
public native int del_dns();
}

View File

@@ -1,9 +1,34 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 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/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
import zerotier.ZeroTier
object ExampleApp extends App {
// load libzt.dylib or libzt.so
System.loadLibrary("zt")
val libzt = new ZeroTier
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212")
val fd = libzt.socket(2, 1, 0)

View File

@@ -1,44 +1,76 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 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/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
package zerotier;
class ZeroTier {
@native def start(path: String): Int
// socket families
// socket types
// basic service controls
@native def start(path: String, blocking: Boolean): Int
@native def startjoin(path: String, nwid: String): Int
@native def stop(): Unit
@native def running(): Int
@native def join(nwid: String): Unit
@native def leave(nwid: String): Unit
//@native def path():
//@native def id(): Int
// advanced service controls
//@native def get_path(): Unit
//@native def get_id(): Int
//@native def get_6plane_addr(): Unit
//@native def get_rfc4193_addr(): Unit
// socket API
@native def socket(socket_family: Int, socket_type: Int, protocol: Int): Int
@native def connect(fd: Int, addr: Object, addrlen: Int): Int
@native def bind(fd: Int, addr: Object, addrlen: Int): Int
@native def connect(fd: Int, addr: String, port: Int): Int
@native def bind(fd: Int, addr: String, port: Int): Int
@native def listen(fd: Int, backlog: Int): Int
@native def accept(fd: Int, addr: Object, addrlen: Int): Int
@native def accept4(fd: Int, addr: Object, addrlen: Int, flags: Int): Int
@native def close(fd: Int): Int
@native def setsockopt(fd: Int, level: Int, optname: Int, optval: Object, optlen: Int): Int
@native def getsockopt(fd: Int, level: Int, optname: Int, optval: Object, optlen: Int): Int
//@native def getsockname(): Int
//@native def getpeername(): Int
//@native def gethostname(): Int
//@native def sethostname(): Int
//@native def gethostbyname(): Object
@native def close(fd: Int): Int
//@native def poll(): Int
//@native def select(): Int
@native def fcntl(fd: Int, cmd: Int, flags: Int): Int
@native def ioctl(fd: Int, request: Long, argp: Object): Int
@native def read(fd: Int, buf: Object, len: Int): Int
@native def write(fd: Int, buf: Object, len: Int): Int
@native def send(fd: Int, buf: Object, len: Int, flags: Int): Int
@native def sendto(fd: Int, buf: Object, len: Int, addr: Object, addrlen: Int): Int
@native def sendmsg(fd: Int, msg: Object, flags: Int): Int
@native def recv(fd: Int, buf: Object, len: Int, flags: Int): Int
@native def recvfrom(fd: Int, buf: Object, len: Int, addr: Object, addrlen: Int): Int
@native def recvmsg(fd: Int, msg: Object, flags: Int): Int
@native def read(fd: Int, buf: Object, len: Int): Int
@native def write(fd: Int, buf: Object, len: Int): Int
@native def shutdown(fd: Int, how: Int): Int
//@native def getsockname(): Int
//@native def getpeername(): Int
//@native def gethostname(): Int
//@native def sethostname(): Int
//@native def gethostbyname(): Object
//@native def poll(): Int
//@native def select(): Int
@native def fcntl(fd: Int, cmd: Int, flags: Int): Int
@native def ioctl(fd: Int, request: Long, argp: Object): Int
// stack controls
//@native def add_dns(): Int
//@native def del_dns(): Int
}

View File

@@ -200,7 +200,7 @@ int zts_running();
* @param path Where this instance of ZeroTier will store its identity and configuration files
* @return Returns 1 if ZeroTier is currently running, and 0 if it is not
*/
int zts_start(const char *path);
int zts_start(const char *path, bool blocking);
/**
* @brief Alternative to zts_start(). Start an instance of libzt, wait for an address to be issues, and join

View File

@@ -1,165 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ZeroTier */
#ifndef _Included_ZeroTier
#define _Included_ZeroTier
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ZeroTier
* Method: ztjni_start
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ZeroTier_ztjni_1start
(JNIEnv *, jobject, jstring);
/*
* Class: ZeroTier
* Method: ztjni_startjoin
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ZeroTier_ztjni_1startjoin
(JNIEnv *, jobject, jstring, jstring);
/*
* Class: ZeroTier
* Method: ztjni_join
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ZeroTier_ztjni_1join
(JNIEnv *, jobject, jstring);
/*
* Class: ZeroTier
* Method: ztjni_leave
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ZeroTier_ztjni_1leave
(JNIEnv *, jobject, jstring);
/*
* Class: ZeroTier
* Method: ztjni_get_addresses
* Signature: (Ljava/lang/String;)Ljava/util/ArrayList;
*/
JNIEXPORT jobject JNICALL Java_ZeroTier_ztjni_1get_1addresses
(JNIEnv *, jobject, jstring);
/*
* Class: ZeroTier
* Method: ztjni_running
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_ZeroTier_ztjni_1running
(JNIEnv *, jobject);
/*
* Class: ZeroTier
* Method: ztjni_socket
* Signature: (III)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1socket
(JNIEnv *, jobject, jint, jint, jint);
/*
* Class: ZeroTier
* Method: ztjni_connect
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1connect
(JNIEnv *, jobject, jint, jstring, jint);
/*
* Class: ZeroTier
* Method: ztjni_bind
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1bind
(JNIEnv *, jobject, jint, jstring, jint);
/*
* Class: ZeroTier
* Method: ztjni_accept4
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1accept4
(JNIEnv *, jobject, jint, jstring, jint);
/*
* Class: ZeroTier
* Method: ztjni_accept
* Signature: (ILAddress;)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1accept
(JNIEnv *, jobject, jint, jobject);
/*
* Class: ZeroTier
* Method: ztjni_listen
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1listen
(JNIEnv *, jobject, jint, jint);
/*
* Class: ZeroTier
* Method: ztjni_close
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1close
(JNIEnv *, jobject, jint);
/*
* Class: ZeroTier
* Method: ztjni_read
* Signature: (I[BI)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1read
(JNIEnv *, jobject, jint, jbyteArray, jint);
/*
* Class: ZeroTier
* Method: ztjni_write
* Signature: (I[BI)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1write
(JNIEnv *, jobject, jint, jbyteArray, jint);
/*
* Class: ZeroTier
* Method: ztjni_sendto
* Signature: (I[BIILAddress;)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1sendto
(JNIEnv *, jobject, jint, jbyteArray, jint, jint, jobject);
/*
* Class: ZeroTier
* Method: ztjni_send
* Signature: (I[BII)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1send
(JNIEnv *, jobject, jint, jbyteArray, jint, jint);
/*
* Class: ZeroTier
* Method: ztjni_recvfrom
* Signature: (I[BIILAddress;)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1recvfrom
(JNIEnv *, jobject, jint, jbyteArray, jint, jint, jobject);
/*
* Class: ZeroTier
* Method: ztjni_fcntl
* Signature: (III)I
*/
JNIEXPORT jint JNICALL Java_ZeroTier_ztjni_1fcntl
(JNIEnv *, jobject, jint, jint, jint);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -89,7 +89,7 @@ ZT_SOCKET_API void ZTCALL init_network_stack();
* @param nwid A 16-digit hexidecimal network identifier (e.g. Earth: `8056c2e21c000001`)
* @return 0 if successful; or 1 if failed
*/
ZT_SOCKET_API int ZTCALL zts_start(const char *path);
ZT_SOCKET_API int ZTCALL zts_start(const char *path, bool blocking);
/**
* @brief Starts libzt

View File

@@ -410,7 +410,7 @@ int zts_running()
return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
}
int zts_start(const char *path)
int zts_start(const char *path, bool blocking = false)
{
DEBUG_EXTRA();
if (ZeroTier::zt1Service) {
@@ -423,14 +423,9 @@ int zts_start(const char *path)
WSAStartup(MAKEWORD(2, 2), &wsaData); // initialize WinSock. Used in Phy for loopback pipe
#endif
pthread_t service_thread;
return pthread_create(&service_thread, NULL, zts_start_service, NULL);
}
int zts_startjoin(const char *path, const char *nwid)
{
DEBUG_EXTRA();
int err = pthread_create(&service_thread, NULL, zts_start_service, NULL);
if (blocking) { // block to prevent service calls before we're ready
ZT_NodeStatus status;
int err = zts_start(path);
while (zts_running() == false || ZeroTier::zt1Service->getNode() == NULL) {
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
}
@@ -441,6 +436,13 @@ int zts_startjoin(const char *path, const char *nwid)
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
ZeroTier::zt1Service->getNode()->status(&status);
}
}
}
int zts_startjoin(const char *path, const char *nwid)
{
DEBUG_EXTRA();
int err = zts_start(path, true);
// only now can we attempt a join
while (true) {
try {

View File

@@ -59,26 +59,71 @@ namespace ZeroTier {
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_connect(JNIEnv *env, jobject thisObj,
jint fd, jstring addrstr, jint port)
{
struct sockaddr_in addr;
const char *str = (*env).GetStringUTFChars( addrstr, 0);
addr.sin_addr.s_addr = inet_addr(str);
addr.sin_family = AF_INET;
addr.sin_port = htons( port );
struct sockaddr_storage ss;
socklen_t namelen = sizeof(ss);
int err = 0;
if ((err = zts_getsockname(fd, (struct sockaddr*)&ss, &namelen)) < 0) {
DEBUG_ERROR("error while determining socket family");
return -1;
}
const char *str;
#if defined(LIBZT_IPV4)
if (ss.ss_family == AF_INET) {
struct sockaddr_in in_addr;
str = (*env).GetStringUTFChars(addrstr, 0);
in_addr.sin_addr.s_addr = inet_addr(str);
in_addr.sin_family = AF_INET;
in_addr.sin_port = htons(port);
(*env).ReleaseStringUTFChars(addrstr, str);
return zts_connect(fd, (struct sockaddr *)&addr, sizeof(addr));
}
#endif // LIBZT_IPV4
#if defined(LIBZT_IPV6)
if (ss.ss_family == AF_INET6) {
struct sockaddr_in6 in_addr;
str = (*env).GetStringUTFChars(addrstr, 0);
//in_addr.sin_addr.s_addr = inet_addr(str);
in_addr.sin6_family = AF_INET6;
in_addr.sin6_port = htons(port);
(*env).ReleaseStringUTFChars(addrstr, str);
}
#endif // LIBZT_IPV6
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
return zts_connect(fd, (struct sockaddr *)&ss, sizeof(in_addr));
}
JNIEXPORT jint JNICALL Java_zerotier_ZeroTier_bind(JNIEnv *env, jobject thisObj,
jint fd, jstring addrstr, jint port)
{
struct sockaddr_in addr;
const char *str = (*env).GetStringUTFChars( addrstr, 0);
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
addr.sin_addr.s_addr = inet_addr(str);
addr.sin_family = AF_INET;
addr.sin_port = htons( port );
struct sockaddr_storage ss;
socklen_t namelen = sizeof(ss);
int err = 0;
if ((err = zts_getsockname(fd, (struct sockaddr*)&ss, &namelen)) < 0) {
DEBUG_ERROR("error while determining socket family");
return -1;
}
const char *str;
#if defined(LIBZT_IPV4)
if (ss.ss_family == AF_INET) {
struct sockaddr_in in_addr;
str = (*env).GetStringUTFChars(addrstr, 0);
in_addr.sin_addr.s_addr = inet_addr(str);
in_addr.sin_family = AF_INET;
in_addr.sin_port = htons(port);
(*env).ReleaseStringUTFChars(addrstr, str);
return zts_bind(fd, (struct sockaddr *)&addr, sizeof(addr));
}
#endif // LIBZT_IPV4
#if defined(LIBZT_IPV6)
if (ss.ss_family == AF_INET6) {
struct sockaddr_in6 in_addr;
str = (*env).GetStringUTFChars(addrstr, 0);
//in_addr.sin_addr.s_addr = inet_addr(str);
in_addr.sin6_family = AF_INET6;
in_addr.sin6_port = htons(port);
(*env).ReleaseStringUTFChars(addrstr, str);
}
#endif // LIBZT_IPV6
DEBUG_INFO("fd=%d, addr=%s, port=%d", fd, str, port);
return zts_bind(fd, (struct sockaddr *)&ss, sizeof(in_addr));
}
#if defined(__linux__)
@@ -242,10 +287,10 @@ namespace ZeroTier {
/* ZeroTier service controls (for JNI wrapper) */
/****************************************************************************/
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_start(JNIEnv *env, jobject thisObj, jstring path)
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_start(JNIEnv *env, jobject thisObj, jstring path, jboolean blocking)
{
if (path) {
zts_start(env->GetStringUTFChars(path, NULL));
zts_start(env->GetStringUTFChars(path, NULL), blocking);
}
}

View File

@@ -1932,7 +1932,7 @@ int ZT_control_semantics_test(bool *passed)
DEBUG_TEST("---\n");
sleep(1);
*/
zts_start(path);
zts_start(path, false);
zts_join(nwid);
zts_leave(nwid);
zts_stop();