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,11 +46,21 @@ 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();
while(true)
{
try { Thread.sleep(3000); }

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
}