API and docs update

This commit is contained in:
Joseph Henry
2016-08-25 17:27:09 -07:00
parent e8b840d7ce
commit b38ca7d8ee
11 changed files with 372 additions and 172 deletions

2
.gitignore vendored
View File

@@ -16,6 +16,7 @@ integrations/android/example_app/local.properties
integrations/Unity3D/Library
integrations/Unity3D/Temp
integrations/Unity3D/obj
integrations/Unity3D/Plugins
# Docker test
*.tmp
@@ -26,3 +27,4 @@ zerotierone
# Other
.depend
.buildnum
build

View File

@@ -52,7 +52,7 @@ The following APIs are available for this integration:
**Step 5: Join a network!**
Simply call `zt_join_network("XXXXXXXXXXXXXXXX")`
Simply call `zt_join_network("nwid")`
***
**NSStream and SOCKS Proxy:**

View File

@@ -7,8 +7,6 @@ We want your Unity apps to talk *directly* over a flat, secure, no-config virtua
Our implementation currently intends to be the bare minimum required to get your Unity application to talk over ZeroTier virtual networks. As a result, we've created an API that is very similar to the classic BSD-style sockets API. With this basic API it is possible to construct more abstracted network layers much like Unity's LLAPI and HLAPI.
Our example project can be found [here]()
***
## API
@@ -51,7 +49,7 @@ public class Example
public void example_server()
{
zt = new ZeroTierNetworkInterface (); // Start interface
zt.Join("565799d8f6e1c11a"); // Join your network
zt.Join("8056c2e21c000001"); // Join your network
Thread connectThread = new Thread(() => {
// Create ZeroTier-administered socket

View File

@@ -83,7 +83,7 @@ The following APIs are available for this integration:
**Step 5: Join a network!**
Simply call `zt_join_network("XXXXXXXXXXXXXXXX")`
Simply call `zt_join_network("nwid")`
***
**NSStream and SOCKS Proxy:**

View File

@@ -1,46 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildMachineOSBuild</key>
<string>15F34</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>ZeroTierSDK_Unity3D_OSX</string>
<key>CFBundleIdentifier</key>
<string>zerotier.ZeroTierSDK-Unity3D-OSX</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>ZeroTierSDK_Unity3D_OSX</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>MacOSX</string>
</array>
<key>CFBundleVersion</key>
<string>1</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>7D1014</string>
<key>DTPlatformVersion</key>
<string>GM</string>
<key>DTSDKBuild</key>
<string>15E60</string>
<key>DTSDKName</key>
<string>macosx10.11</string>
<key>DTXcode</key>
<string>0731</string>
<key>DTXcodeBuild</key>
<string>7D1014</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016 ZeroTier Inc. All rights reserved.</string>
</dict>
</plist>

View File

@@ -1,105 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>files</key>
<dict/>
<key>files2</key>
<dict/>
<key>rules</key>
<dict>
<key>^Resources/</key>
<true/>
<key>^Resources/.*\.lproj/</key>
<dict>
<key>optional</key>
<true/>
<key>weight</key>
<real>1000</real>
</dict>
<key>^Resources/.*\.lproj/locversion.plist$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>1100</real>
</dict>
<key>^version.plist$</key>
<true/>
</dict>
<key>rules2</key>
<dict>
<key>.*\.dSYM($|/)</key>
<dict>
<key>weight</key>
<real>11</real>
</dict>
<key>^(.*/)?\.DS_Store$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>2000</real>
</dict>
<key>^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/</key>
<dict>
<key>nested</key>
<true/>
<key>weight</key>
<real>10</real>
</dict>
<key>^.*</key>
<true/>
<key>^Info\.plist$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>20</real>
</dict>
<key>^PkgInfo$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>20</real>
</dict>
<key>^Resources/</key>
<dict>
<key>weight</key>
<real>20</real>
</dict>
<key>^Resources/.*\.lproj/</key>
<dict>
<key>optional</key>
<true/>
<key>weight</key>
<real>1000</real>
</dict>
<key>^Resources/.*\.lproj/locversion.plist$</key>
<dict>
<key>omit</key>
<true/>
<key>weight</key>
<real>1100</real>
</dict>
<key>^[^/]+$</key>
<dict>
<key>nested</key>
<true/>
<key>weight</key>
<real>10</real>
</dict>
<key>^embedded\.provisionprofile$</key>
<dict>
<key>weight</key>
<real>20</real>
</dict>
<key>^version\.plist$</key>
<dict>
<key>weight</key>
<real>20</real>
</dict>
</dict>
</dict>
</plist>

View File

@@ -234,7 +234,7 @@ public class ZeroTierSockets_Demo : MonoBehaviour
input.text = "Welcome to the machine";
// Create new instance of ZeroTier in separate thread
zt = new ZTSDK (".", "8056c2e21c000001");
zt = new ZTSDK ("zerotier/", "8056c2e21c000001");
}
// Terminate the ZeroTier service when the application quits

View File

@@ -0,0 +1,84 @@
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 ZTAddress
{
// 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 ZTAddress()
{
port = -1;
_rawAddr = -1;
}
public ZTAddress(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;
}
public boolean isValid() {
return port != -1 && !Address().startsWith("-1.-1.-1.-1/-1");
}
}

View File

@@ -0,0 +1,275 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*
* --
*
* 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 ZeroTier;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipError;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.util.Pair;
public class ZTSDK {
// Socket families
public static int AF_UNIX = 1;
public static int AF_INET = 2;
// Socket types
public static int SOCK_STREAM = 1;
public static int SOCK_DGRAM = 2;
// fcntl flags
public static int O_APPEND = 1024;
public static int O_NONBLOCK = 2048;
public static int O_ASYNC = 8192;
public static int O_DIRECT = 65536;
public static int O_NOATIME = 262144;
// fcntl cmds
public static int F_GETFL = 3;
public static int F_SETFL = 4;
// Loads JNI code
static { System.loadLibrary("ZeroTierOneJNI"); }
// ZeroTier service controls
public native void zt_start_service(String homeDir);
public void start_service(String homeDir) {
zt_start_service(homeDir);
}
public native void zt_join_network(String nwid);
public void join_network(String nwid) {
zt_join_network(nwid);
}
public native void zt_leave_network(String nwid);
public void leave_network(String nwid) {
zt_leave_network(nwid);
}
// ------------------------------------------------------------------------------
// ------------------------------- get_addresses() ------------------------------
// ------------------------------------------------------------------------------
public native ArrayList<String> zt_get_addresses(String nwid);
public ArrayList<String> get_addresses(String nwid) {
int err = -1;
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
addresses = zt_get_addresses(nwid);
if (addresses.size() > 0) {
return addresses;
}
}
return null;
}
public native int zt_get_proxy_port(String nwid);
public int get_proxy_port(String nwid) {
return zt_get_proxy_port(nwid);
}
public native boolean zt_running();
public boolean running() {
return zt_running();
}
// ------------------------------------------------------------------------------
// ----------------------------------- socket() ---------------------------------
// ------------------------------------------------------------------------------
public native int zt_socket(int family, int type, int protocol);
public int socket(int family, int type, int protocol) {
return zt_socket(family, type, protocol);
}
// ------------------------------------------------------------------------------
// ----------------------------------- connect() --------------------------------
// ------------------------------------------------------------------------------
public native int zt_connect(int fd, String addr, int port);
public int connect(int sock, ZTAddress zaddr, String nwid) {
return connect(sock, zaddr.Address(), zaddr.Port(), nwid);
}
public int connect(int sock, String addr, int port, String nwid)
{
int err = -1;
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
addresses = zt_get_addresses(nwid);
if (addresses.size() > 0) {
if(!addresses.get(0).startsWith("-1.-1.-1.-1/-1")) {
err = zt_connect(sock, addr, port);
}
}
}
return err;
}
// ------------------------------------------------------------------------------
// ------------------------------------ bind() ----------------------------------
// ------------------------------------------------------------------------------
public native int zt_bind(int fd, String addr, int port);
public int bind(int sock, ZTAddress zaddr, String nwid) {
return bind(sock, zaddr.Address(), zaddr.Port(), nwid);
}
public int bind(int sock, String addr, int port, String nwid) {
int err = -1;
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
addresses = zt_get_addresses(nwid);
if (addresses.size() > 0) {
if(!addresses.get(0).startsWith("-1.-1.-1.-1/-1")) {
err = zt_bind(sock, addr, port);
}
}
}
return err;
}
// ------------------------------------------------------------------------------
// ---------------------------------- accept4() ---------------------------------
// ------------------------------------------------------------------------------
public native int zt_accept4(int fd, String addr, int port);
public int accept4(int fd, String addr, int port) {
return zt_accept4(fd,addr,port);
}
// ------------------------------------------------------------------------------
// ---------------------------------- accept() ----------------------------------
// ------------------------------------------------------------------------------
public native int zt_accept(int fd, ZeroTier.ZTAddress addr);
public int accept(int fd, ZeroTier.ZTAddress addr) {
return zt_accept(fd, addr);
}
// ------------------------------------------------------------------------------
// ----------------------------------- listen() ---------------------------------
// ------------------------------------------------------------------------------
public native int zt_listen(int fd, int backlog);
public int listen(int fd, int backlog) {
return zt_listen(fd,backlog);
}
// ------------------------------------------------------------------------------
// ----------------------------------- close() ----------------------------------
// ------------------------------------------------------------------------------
public native int zt_close(int fd);
public int close(int fd) {
return close(fd);
}
// ------------------------------------------------------------------------------
// ------------------------------------ read() ----------------------------------
// ------------------------------------------------------------------------------
public native int zt_read(int fd, byte[] buf, int len);
public int read(int fd, byte[] buf, int len) {
return zt_read(fd, buf, len);
}
// ------------------------------------------------------------------------------
// ----------------------------------- write() ----------------------------------
// ------------------------------------------------------------------------------
public native int zt_write(int fd, byte[] buf, int len);
public int write(int fd, byte[] buf, int len) {
return zt_write(fd, buf, len);
}
// ------------------------------------------------------------------------------
// ----------------------------------- sendto() ---------------------------------
// ------------------------------------------------------------------------------
public native int zt_sendto(int fd, byte[] buf, int len, int flags, ZeroTier.ZTAddress addr);
public int sendto(int fd, byte[] buf, int len, int flags, ZeroTier.ZTAddress addr){
return zt_sendto(fd,buf,len,flags,addr);
}
// ------------------------------------------------------------------------------
// ----------------------------------- send() -----------------------------------
// ------------------------------------------------------------------------------
public native int zt_send(int fd, byte[] buf, int len, int flags);
public int send(int fd, byte[] buf, int len, int flags) {
return zt_send(fd, buf, len, flags);
}
// ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------
public native int zt_recvfrom(int fd, byte[] buf, int len, int flags, ZeroTier.ZTAddress addr);
public int recvfrom(int fd, byte[] buf, int len, int flags, ZeroTier.ZTAddress addr){
return zt_recvfrom(fd,buf,len,flags,addr);
}
// ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------
public native int zt_fcntl(int sock, int cmd, int flag);
public int fcntl(int sock, int cmd, int flag) {
return zt_fcntl(sock, F_SETFL, O_NONBLOCK);
}
//public static native int zt_getsockopt(int fd, int type, int protocol);
//public static native int zt_setsockopt(int fd, int type, int protocol);
//public static native int zt_getsockname(int fd, int type, int protocol);
}

View File

@@ -14,7 +14,7 @@ import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import ZeroTier.SDK;
import ZeroTier.ZTSDK;
import ZeroTier.ZTAddress;
public class MainActivity extends AppCompatActivity {
@@ -26,7 +26,7 @@ public class MainActivity extends AppCompatActivity {
String nwid = "8056c2e21c000001";
// Set up service
final SDK zt = new SDK();
final ZTSDK zt = new ZTSDK();
final String homeDir = getApplicationContext().getFilesDir() + "/zerotier";
new Thread(new Runnable() {
public void run() {
@@ -43,17 +43,13 @@ public class MainActivity extends AppCompatActivity {
if(mode==1)
{
zt.join_network(nwid);
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_STREAM, 0);
int sock = zt.socket(ZTSDK.AF_INET, ZTSDK.SOCK_STREAM, 0);
if((err = zt.bind(sock, "0.0.0.0", 8080, nwid)) < 0)
Log.d("ZTSDK", "bind_err = " + err + "\n");
if((err = zt.listen(sock,1)) < 0)
Log.d("ZTSDK", "listen_err = " + err);
if((err = zt.accept(sock,null)) < 0)
Log.d("ZTSDK", "accept_err = " + err);
Log.d("ZTSDK", "Waiting to accept connection...");
// ...
@@ -64,14 +60,11 @@ public class MainActivity extends AppCompatActivity {
{
Log.d("ZTSDK", "\n\nStarting TCP Echo ZTSDK\n\n");
zt.join_network(nwid);
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_STREAM, 0);
int sock = zt.socket(ZTSDK.AF_INET, ZTSDK.SOCK_STREAM, 0);
String msg = "Welcome to the machine!";
err = zt.connect(sock, "28.206.65.211", 8099, nwid);
Log.d("ZTSDK", "err = " + err + "\n");
//return;
// ECHO
while(true)
{
@@ -79,7 +72,6 @@ public class MainActivity extends AppCompatActivity {
if((err = zt.write(sock, msg.getBytes(), msg.length())) > 0) {
Log.d("ZTSDK", "TX: " + msg + " --- " + err + " bytes");
}
// RX
byte[] buffer = new byte[32];
Arrays.fill(buffer, (byte)0);
@@ -96,7 +88,7 @@ public class MainActivity extends AppCompatActivity {
if(mode==3)
{
zt.join_network(nwid);
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_STREAM, 0);
int sock = zt.socket(ZTSDK.AF_INET, ZTSDK.SOCK_STREAM, 0);
int proxyPort = zt.get_proxy_port(nwid);
Log.d("ZTSDK", "Setting up connection to SDK proxy server");
@@ -143,7 +135,7 @@ public class MainActivity extends AppCompatActivity {
Log.d("ZTSDK", "\n\nStarting UDP Echo ZTSDK\n\n");
nwid = "8056c2e21c000001";
zt.join_network(nwid);
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_DGRAM, 0);
int sock = zt.socket(ZTSDK.AF_INET, ZTSDK.SOCK_DGRAM, 0);
Log.d("ZTSDK", "binding...");
if((err = zt.bind(sock, bindAddr, nwid)) < 0)
@@ -162,7 +154,7 @@ public class MainActivity extends AppCompatActivity {
String bufStr;
byte[] buffer = new byte[1024];
zt.fcntl(sock, zt.F_SETFL, zt.O_NONBLOCK);
zt.fcntl(sock, ZTSDK.F_SETFL, ZTSDK.O_NONBLOCK);
// ECHO
while(true) {