Experimental android API changes

This commit is contained in:
Joseph Henry
2016-07-29 09:41:09 -07:00
parent 4666b7a2b8
commit 82d50d1e1e
6 changed files with 123 additions and 24 deletions

View File

@@ -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<FileInputStream, FileOutputStream> zt_getFileStreams(int fd)
{
FileDescriptor fileDesc = zt_getFileDescriptor(fd);
FileOutputStream fos = new FileOutputStream(fileDesc);
FileInputStream fis = new FileInputStream(fileDesc);
return new Pair<FileInputStream, FileOutputStream>(fis, fos);
}
// Example Usage:
// Get corresponding new I/O streams
/*
Pair<FileInputStream, FileOutputStream> 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();
}
}
*/
}

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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

BIN
zerotierone/.DS_Store vendored

Binary file not shown.

Binary file not shown.