Preparation for C--, removed classes, namespaces, advanced build options are now controlled via include/libztDefs.h

This commit is contained in:
Joseph Henry
2017-11-06 13:50:20 -08:00
parent 920afa079e
commit e0d4b84dd4
65 changed files with 12208 additions and 2609 deletions

View File

@@ -27,6 +27,7 @@
// Simple Java example for libzt using JNI
import zerotier.ZeroTier;
import java.net.*;
public class ExampleApp {
@@ -39,25 +40,28 @@ public class ExampleApp {
}
public static void main(String[] args) {
final ZeroTier libzt = new ZeroTier();
new Thread(new Runnable() {
public void run() {
System.out.println("starting libzt");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212");
//libzt.startjoin("config_path", "123456789abcdeff");
libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "17d709436c2c5367");
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) {
InetSocketAddress addr = 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,4 +11,11 @@ To get this example project to work, do the following:
Notes:
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.
***
More resources on JNI usage:
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html

View File

@@ -1,106 +0,0 @@
/*
* 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;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;
/*
The ZTAddress object is merely a convenience object for moving address information
across the JNI memory border.
*/
public class Address
{
// int -> byte array
static public byte[] toIPByteArray(long addr){
return new byte[]{(byte)addr,(byte)(addr>>>8),(byte)(addr>>>16),(byte)(addr>>>24)};
}
// byte array -> int
long toIPInt(String _addr) {
long result = 0;
for(String part: _addr.split(Pattern.quote("."))) {
result = result << 8;
result |= Integer.parseInt(part);
}
return result;
}
public int port;
public int Port() {
return port;
}
public long _rawAddr;
public String Address()
{
try {
return InetAddress.getByAddress(toIPByteArray(_rawAddr)).getHostAddress();
} catch (UnknownHostException e) {
// should never happen
return null;
}
}
public String toString() {
return Address() + ":" + Port();
}
public Address()
{
port = -1;
_rawAddr = -1;
}
public Address(String _addr, int _port)
{
_rawAddr = toIPInt(_addr);
port = _port;
}
public void ZTAddress(InetSocketAddress ins)
{
port = ins.getPort();
_rawAddr = toIPInt(ins.getAddress().getHostAddress());
}
public InetSocketAddress ToInetSocketAddress() throws IllegalArgumentException {
InetSocketAddress sock_addr = null;
try {
sock_addr = new InetSocketAddress(Address(), port);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return sock_addr;
}
}

View File

@@ -26,6 +26,8 @@
package zerotier;
import java.net.*;
public class ZeroTier {
// socket families
@@ -58,7 +60,7 @@ public class ZeroTier {
// 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 bind(int fd, InetSocketAddress 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);