API usability update, normalized zt_ naming convention
This commit is contained in:
@@ -1,3 +1,29 @@
|
||||
/*
|
||||
* 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.net.SocketAddress;
|
||||
@@ -16,21 +42,21 @@ public class SDK {
|
||||
static { System.loadLibrary("ZeroTierOneJNI"); }
|
||||
|
||||
// ZeroTier service controls
|
||||
public native void startOneService(String homeDir);
|
||||
public native void joinNetwork(String nwid);
|
||||
public native void leaveNetwork(String nwid);
|
||||
public native boolean isRunning();
|
||||
public native void zt_start_service(String homeDir);
|
||||
public native void zt_join_network(String nwid);
|
||||
public native void zt_leave_network(String nwid);
|
||||
public native boolean zt_running();
|
||||
|
||||
// Direct-call API
|
||||
// --- These calls skip the intercept and interface directly via the RPC mechanism ---
|
||||
public native int ztjniSocket(int family, int type, int protocol);
|
||||
public native int ztjniConnect(int fd, String addr, int port);
|
||||
public native int ztjniBind(int fd, String addr, int port);
|
||||
public native int ztjniAccept4(int fd, String addr, int port);
|
||||
public native int ztjniAccept(int fd, String addr, int port, int flags);
|
||||
public native int ztjniListen(int fd, int backlog);
|
||||
//public native int ztjniGetsockopt(int fd, int type, int protocol);
|
||||
//public native int ztjniSetsockopt(int fd, int type, int protocol);
|
||||
//public native int ztjniGetsockname(int fd, int type, int protocol);
|
||||
public native int ztjniClose(int fd);
|
||||
// --- These calls skip the intercept and interface directly via the RPC mechanism
|
||||
public native int zt_socket(int family, int type, int protocol);
|
||||
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_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 native int zt_close(int fd);
|
||||
}
|
||||
@@ -25,15 +25,15 @@ public class MainActivity extends AppCompatActivity {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
// Calls to JNI code
|
||||
zt.startOneService(homeDir);
|
||||
zt.zt_start_service(homeDir);
|
||||
}
|
||||
}).start();
|
||||
|
||||
while(!zt.isRunning()) { }
|
||||
zt.joinNetwork("XXXXXXXXXXXXXXXX");
|
||||
while(!zt.zt_running()) { }
|
||||
zt.zt_join_network("XXXXXXXXXXXXXXXX");
|
||||
|
||||
// Create ZeroTier socket
|
||||
int sock = zt.ztjniSocket(zt.AF_INET, zt.SOCK_STREAM, 0);
|
||||
int sock = zt.zt_socket(zt.AF_INET, zt.SOCK_STREAM, 0);
|
||||
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
@@ -41,8 +41,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
catch(java.lang.InterruptedException e) { }
|
||||
|
||||
// Connect to remote host
|
||||
Log.d("","ztjniConnect()\n");
|
||||
int err = zt.ztjniConnect(sock, "10.9.9.203", 8080);
|
||||
Log.d("","zt_connect()\n");
|
||||
int err = zt.zt_connect(sock, "10.9.9.203", 8080);
|
||||
|
||||
// Set up example proxy connection to SDK proxy server
|
||||
/*
|
||||
|
||||
@@ -3,14 +3,13 @@ Android + ZeroTier SDK
|
||||
|
||||
Welcome!
|
||||
|
||||
Imagine a flat, encrypted, no-configuration LAN for all of the instances of your Android app.
|
||||
Imagine a flat, encrypted, no-configuration LAN for all of the instances of your Android app. This short tutorial will show you how to enable ZeroTier functionality for your Android app with little to no code modification. Check out our [ZeroTier SDK](https://www.zerotier.com/blog) page for more info on how the integration works. In this example we aim to set up a minimal [Android Studio](https://developer.android.com/studio/index.html) project which contains all of the components necessary to enable ZeroTier for your app.
|
||||
|
||||
This short tutorial will show you how to enable ZeroTier functionality for your Android app with little to no code modification. Check out our [ZeroTier SDK](https://www.zerotier.com/blog) page for more info on how the integration works and [Shim Techniques](https://www.zerotier.com/blog) for a discussion of shims available for your app/technology.
|
||||
*NOTE: For Android JNI libraries to build you'll need to install [Android Studio](https://developer.android.com/studio/index.html) the [Android NDK](https://developer.android.com/ndk/index.html). Currently only Android NDK r10e is supported and can be found [here for OSX](http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip) and [here for Linux](http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip). You'll need to tell our project where you put it by putting the path in [this file](android/proj/local.properties), you'll need to install the Android Build-Tools (this can typically be done through the editor the first time you start it up), and finally you should probably upgrade your Gradle plugin if it asks you to. If you don't have these things installed and configured we will detect that and just skip those builds automatically.*
|
||||
|
||||
In this example we aim to set up a minimal [Android Studio](https://developer.android.com/studio/index.html) project which contains all of the components necessary to enable ZeroTier for your app. If you'd rather skip all of these steps and grab the code, look in the [sdk/android](https://github.com/zerotier/ZeroTierOne/tree/dev/sdk/integrations/android/example_app) folder in the source tree. Otherwise, let's get started!
|
||||
|
||||
*NOTE: For Android JNI libraries to build you'll need to install [Android Studio](https://developer.android.com/studio/index.html) the [Android NDK](https://developer.android.com/ndk/index.html). Currently only Android NDK r10e is supported and can be found [here for OSX](http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip) and [here for Linux](http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip). You'll need to tell our project where you put it by putting the path in [this file](Android/proj/local.properties), you'll need to install the Android Build-Tools (this can typically be done through the editor the first time you start it up), and finally you should probably upgrade your Gradle plugin if it asks you to. If you don't have these things installed and configured we will detect that and just skip those builds automatically.*
|
||||
If you want to skip these steps and just take a look at the project, go [here](example_app).
|
||||
|
||||
***
|
||||
**Step 1: Select build targets**
|
||||
- Specify the target architectures you want to build in [Application.mk](android/java/jni/Application.mk). By default it will build `arm64-v8a`, `armeabi`, `armeabi-v7a`, `mips`, `mips64`, `x86`, and `x86_64`.
|
||||
|
||||
@@ -18,7 +17,16 @@ In this example we aim to set up a minimal [Android Studio](https://developer.an
|
||||
- `make android_jni_lib`
|
||||
- The resultant `build/android_jni_lib_YOUR_ARCH/libZeroTierOneJNI.so` is what you want to import into your own project to provide the API to your app. Select your architecture and copy the shared library into your project's JNI directory, possibly `/src/main/jniLibs/YOUR_ARCH/`. Selecting only the architectures you need will *significantly* reduce overall build time.
|
||||
|
||||
**Step 3: App Code Modifications**
|
||||
**Step 3: App permissions**
|
||||
|
||||
- In order for your application to write the auth keys and network files to the internal storage you'll need to set a few permissions in your `AndroidManifest.xml` file at the same scope level as `<application>`:
|
||||
|
||||
```
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
```
|
||||
|
||||
**Step 4: App Code Modifications**
|
||||
- Create new package called `ZeroTierSDK` in your project and add a new file called `ZeroTierSDK.java` containing:
|
||||
|
||||
```
|
||||
@@ -32,36 +40,33 @@ public class ZeroTierSDK {
|
||||
}
|
||||
```
|
||||
|
||||
- And now, start the service in your app with:
|
||||
- Start the service
|
||||
|
||||
```
|
||||
final SDK zt = new SDK();
|
||||
final String homeDir = getApplicationContext().getFilesDir() + "/zerotier";
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
ZeroTierSDK wrapper = new ZeroTierSDK();
|
||||
wrapper.startOneService(); // Calls to JNI code
|
||||
}
|
||||
public void run() {
|
||||
// Calls to JNI code
|
||||
zt.startOneService(homeDir);
|
||||
}
|
||||
}).start();
|
||||
```
|
||||
|
||||
**Step 4: App permissions**
|
||||
|
||||
- In order for your application to write the auth keys and network files to the internal storage you'll need to set a few permissions in your `AndroidManifest.xml` file at the same scope level as `<application>`:
|
||||
- Join network and perform network call
|
||||
|
||||
```
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
while(!zt.isRunning()) { }
|
||||
zt.joinNetwork("XXXXXXXXXXXXXXXX");
|
||||
|
||||
// Create ZeroTier socket
|
||||
int sock = zt.ztjniSocket(zt.AF_INET, zt.SOCK_STREAM, 0);
|
||||
|
||||
// Connect to remote host
|
||||
int err = zt.ztjniConnect(sock, "10.9.9.203", 8080);
|
||||
```
|
||||
|
||||
**Step 5: Pick an API**
|
||||
|
||||
- If functional interposition isn't available for the API or library you've chosen to use, ZeroTier offers a SOCKS5 proxy server which can allow connectivity to your virtual network as long as your client API supports the SOCKS5 protocol. This proxy service will run alongside the tap service and can be turned on by compiling with the `-DUSE_SOCKS_PROXY` flag. By default, the proxy service is available at `0.0.0.0:1337`.
|
||||
|
||||
**Step 6: Join a network!**
|
||||
|
||||
- Simply call `wrapper.joinNetwork("XXXXXXXXXXXXXXXX")`
|
||||
|
||||
|
||||
|
||||
***
|
||||
|
||||
*Note for the curious on JNI naming conventions: In order to reference a symbol in the JNI library you need to structure the package and class in your Android Studio project in a very particular way. For example, in the ZeroTierSDK we define a function called `Java_ZeroTier_SDK_startOneService`, the name can be broken down as follows: `Java_PACKAGENAME_CLASSNAME_startOneService`, so as we've defined it, you must create a package called `ZeroTier` and add a class called `SDK`.*
|
||||
|
||||
Reference in New Issue
Block a user