API and docs update
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user