diff --git a/integrations/android/example_app/app/src/main/java/ZeroTier/SDK.java b/integrations/android/example_app/app/src/main/java/ZeroTier/SDK.java index d633a7e..cd6c9f2 100644 --- a/integrations/android/example_app/app/src/main/java/ZeroTier/SDK.java +++ b/integrations/android/example_app/app/src/main/java/ZeroTier/SDK.java @@ -26,17 +26,22 @@ */ package ZeroTier; -import java.net.SocketAddress; +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.FileOutputStream; + +import android.os.ParcelFileDescriptor; +import android.util.Pair; public class SDK { // Socket families - public int AF_UNIX = 1; - public int AF_INET = 2; + public static int AF_UNIX = 1; + public static int AF_INET = 2; // Socket types - public int SOCK_STREAM = 1; - public int SOCK_DGRAM = 2; + public static int SOCK_STREAM = 1; + public static int SOCK_DGRAM = 2; // Loads JNI code static { System.loadLibrary("ZeroTierOneJNI"); } @@ -53,10 +58,59 @@ public class SDK { public native int zt_connect(int fd, String addr, int port); public native int zt_bind(int fd, String addr, int port); public native int zt_accept4(int fd, String addr, int port); - public native int zt_accept(int fd, String addr, int port, int flags); + public native int zt_accept(int fd, ZeroTier.ZTAddress addr); public native int zt_listen(int fd, int backlog); - //public native int zt_getsockopt(int fd, int type, int protocol); - //public native int zt_setsockopt(int fd, int type, int protocol); - //public native int zt_getsockname(int fd, int type, int protocol); + //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); public native int zt_close(int fd); + public native int zt_read(int fd, byte[] buf, int len); + public native int zt_write(int fd, byte[] buf, int len); + + // --- Below is experimental + + // Converts a numerical fd to a FileDescriptor + public static native FileDescriptor zt_getFileDescriptor(int fd); + + // Returns a pair of I/O streams for the given fd + public static Pair zt_getFileStreams(int fd) + { + FileDescriptor fileDesc = zt_getFileDescriptor(fd); + FileOutputStream fos = new FileOutputStream(fileDesc); + FileInputStream fis = new FileInputStream(fileDesc); + return new Pair(fis, fos); + } + + // Example Usage: + // Get corresponding new I/O streams + /* + Pair streamPair; + streamPair = SDK.zt_getFileStreams(sock); + FileInputStream fis = streamPair.first; + FileOutputStream fos = streamPair.second; + */ + + /* + // Wrapper for TX + public static void zt_write(FileOutputStream fos, byte[] buf, int len) + { + try { + fos.write(buf, 0, len); + } + catch (java.io.IOException e) { + e.printStackTrace(); + } + } + + // Wrapper for RX + public static void zt_read(FileInputStream fis, byte[] buf, int len) + { + try { + fis.read(buf, 0, len); + } + catch (java.io.IOException e) { + e.printStackTrace(); + } + } + */ } \ No newline at end of file diff --git a/integrations/android/example_app/app/src/main/java/ZeroTier/ZTAddress.java b/integrations/android/example_app/app/src/main/java/ZeroTier/ZTAddress.java new file mode 100644 index 0000000..3c34d0b --- /dev/null +++ b/integrations/android/example_app/app/src/main/java/ZeroTier/ZTAddress.java @@ -0,0 +1,19 @@ +package ZeroTier; + +import java.net.InetSocketAddress; + +public class ZTAddress +{ + public String addr; + public int port; + + public InetSocketAddress ToInetSocketAddress() throws IllegalArgumentException { + InetSocketAddress sock_addr = null; + try { + sock_addr = new InetSocketAddress(addr, port); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + return sock_addr; + } +} \ No newline at end of file diff --git a/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java b/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java index 65a4524..da14fca 100644 --- a/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java +++ b/integrations/android/example_app/app/src/main/java/com/example/joseph/example_app/MainActivity.java @@ -2,12 +2,8 @@ package com.example.joseph.example_app; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import android.os.Environment; -import android.util.Log; import java.net.InetSocketAddress; -import java.net.*; -import android.content.Context; -import android.os.Handler; +import android.util.*; import ZeroTier.SDK; @@ -18,35 +14,60 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + // Set up service final SDK zt = new SDK(); final String homeDir = getApplicationContext().getFilesDir() + "/zerotier"; - - // Service thread new Thread(new Runnable() { public void run() { // Calls to JNI code zt.zt_start_service(homeDir); } }).start(); - while(!zt.zt_running()) { } zt.zt_join_network("XXXXXXXXXXXXXXXX"); // Create ZeroTier socket - int sock = zt.zt_socket(zt.AF_INET, zt.SOCK_STREAM, 0); + int sock = zt.zt_socket(SDK.AF_INET, SDK.SOCK_STREAM, 0); try { - Thread.sleep(10000); + Thread.sleep(5000); } catch(java.lang.InterruptedException e) { } - // Connect to remote host - Log.d("","zt_connect()\n"); - int err = zt.zt_connect(sock, "10.9.9.203", 8080); + int mode = 0; // client/server mode toggle + + // Establish outgoing connection + if(mode==0) + { + int err = zt.zt_connect(sock, "10.9.9.203", 7000); + Log.d("TEST", "err = " + err + "\n"); + SDK.zt_write(sock, "Welcome to the machine".getBytes(), 16); + byte[] buffer = null; + SDK.zt_read(sock, buffer, 16); + Log.d("TEST", "buffer = " + buffer); + } + + // Listen to incoming connections + if(mode==1) + { + int err; + zt.zt_bind(sock, "0.0.0.0", 8081); + zt.zt_listen(sock,1); + err = zt.zt_accept(sock,null); // Pass a ZTAddress to get remote host's address (if you want) + + // Example ZTAddress usage + /* + ZeroTier.ZTAddress za = new ZeroTier.ZTAddress(); + za.addr = "0.0.0.0"; + za.port = -1; + InetSocketAddress addr = za.ToInetSocketAddress(); + */ + } + // Set up example proxy connection to SDK proxy server /* - Log.d("ZTSDK-InJavaland", "Setting up connection to SDK proxy server"); + Log.d("ZTSDK", "Setting up connection to SDK proxy server"); Socket s = new Socket(); SocketAddress proxyAddr = new InetSocketAddress("0.0.0.0", 1337); Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr); diff --git a/src/SDK.h b/src/SDK.h index f8c6ece..11383cf 100644 --- a/src/SDK.h +++ b/src/SDK.h @@ -113,13 +113,18 @@ ssize_t zts_recvmsg(RECVMSG_SIG); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1socket(JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1connect(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1bind(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); - JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept4(JNIEnv *env, jobject thisObj, jint fd, jstring addrstr, jint port, jint flags); + JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1accept(JNIEnv *env, jobject thisObj, jint fd, jobject addr); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1listen(JNIEnv *env, jobject thisObj, jint fd, int backlog); //JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1setsockopt(JNIEnv *env, jobject thisObj); //JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1getsockopt(JNIEnv *env, jobject thisObj); //JNIEXPORT void JNICALL Java_ZeroTier_SDK_zt_1getsockname(JNIEnv *env, jobject thisObj); JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1close(JNIEnv *env, jobject thisObj, jint fd); + + // Takes a given numerical file descriptor and manufactures a java FileDescriptor object for use in javaland + JNIEXPORT jobject JNICALL Java_ZeroTier_SDK_zt_1getFileDescriptor(JNIEnv *env, jobject thisObj, jint fd); + + JNIEXPORT jint JNICALL Java_ZeroTier_SDK_zt_1write(JNIEnv *env, jobject thisObj, jint fd, jarray buf, jint len); #endif diff --git a/zerotierone/.DS_Store b/zerotierone/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/zerotierone/.DS_Store and /dev/null differ diff --git a/zerotierone/doc/.DS_Store b/zerotierone/doc/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/zerotierone/doc/.DS_Store and /dev/null differ