usability fixes and removal of unfinished test items

This commit is contained in:
Joseph Henry
2016-07-17 16:00:24 -07:00
parent cac337ee1f
commit e352c2a613
22 changed files with 100 additions and 861 deletions

View File

@@ -9,7 +9,7 @@ This short tutorial will show you how to enable ZeroTier functionality for your
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! 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) and the [Android NDK](https://developer.android.com/ndk/index.html), and you'll need to tell our project where you put it by putting the path in [this file](Android/proj/local.properties), if you don't have these things installed and configured we will detect that and just skip those builds automatically.* *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.*
**Step 1: Select build targets** **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`. - 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`.

View File

@@ -5,9 +5,10 @@ APP_CFLAGS := -g -DSDK_BUNDLED
APP_PLATFORM := android-14 APP_PLATFORM := android-14
# Architectures # Architectures
# APP_ABI := all APP_ABI := all
#APP_ABI += arm64-v8a #APP_ABI += arm64-v8a
APP_ABI += armeabi #APP_ABI += armeabi
#APP_ABI += armeabi-v7a #APP_ABI += armeabi-v7a
#APP_ABI += mips #APP_ABI += mips
#APP_ABI += mips64 #APP_ABI += mips64

View File

@@ -67,14 +67,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
@@ -83,19 +75,23 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-analytics/8.4.0/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-analytics/8.4.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-basement/8.4.0/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.google.android.gms/play-services-basement/8.4.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/mockable-android-23.jar" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
@@ -111,8 +107,8 @@
<orderEntry type="library" exported="" name="cling-core-2.0.1" level="project" /> <orderEntry type="library" exported="" name="cling-core-2.0.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" /> <orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="play-services-analytics-8.4.0" level="project" /> <orderEntry type="library" exported="" name="play-services-analytics-8.4.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="seamless-http-1.1.0" level="project" /> <orderEntry type="library" exported="" name="seamless-http-1.1.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" /> <orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="play-services-basement-8.4.0" level="project" /> <orderEntry type="library" exported="" name="play-services-basement-8.4.0" level="project" />
</component> </component>

View File

@@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.google.gms:google-services:1.3.0-beta1' classpath 'com.google.gms:google-services:1.3.0-beta1'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View File

@@ -0,0 +1,69 @@
Android + ZeroTier SDK
====
Welcome!
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 and [Shim Techniques](https://www.zerotier.com/blog) for a discussion of shims available for your app/technology.
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), and 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.*
**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`.
**Step 2: Build Shared Library**
- `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**
- Create new package called `ZeroTierSDK` in your project and add a new file called `ZeroTierSDK.java` containing:
```
package ZeroTier;
public class ZeroTierSDK {
public native void startOneService(String homeDir);
public native void joinNetwork(String nwid);
public native void leaveNetwork(String nwid);
public native boolean isRunning();
static { System.loadLibrary("ZeroTierOneJNI"); } // Loads JNI code
}
```
- And now, start the service in your app with:
```
new Thread(new Runnable() {
public void run() {
ZeroTierSDK wrapper = new ZeroTierSDK();
wrapper.startOneService(); // Calls to JNI code
}
}).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>`:
```
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
**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`.*

View File

@@ -73,6 +73,7 @@ osx_app_framework:
ios_app_framework: ios_app_framework:
cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_iOS build SYMROOT="../../../$(BUILD)/ios_app_framework" cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_iOS build SYMROOT="../../../$(BUILD)/ios_app_framework"
cp docs/ios_zt_sdk.md $(BUILD)/ios_app_framework/README.md cp docs/ios_zt_sdk.md $(BUILD)/ios_app_framework/README.md
# Build bundles for Unity integrations # Build bundles for Unity integrations
osx_unity3d_bundle: osx_unity3d_bundle:
cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_Unity3D_OSX build SYMROOT="../../../$(BUILD)/osx_unity3d_bundle" cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_Unity3D_OSX build SYMROOT="../../../$(BUILD)/osx_unity3d_bundle"
@@ -80,16 +81,16 @@ osx_unity3d_bundle:
ios_unity3d_bundle: ios_unity3d_bundle:
cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_Unity3D_iOS build SYMROOT="../../../$(BUILD)/ios_unity3d_bundle" cd $(INT)/apple/ZeroTierSDK_Apple; xcodebuild -scheme ZeroTierSDK_Unity3D_iOS build SYMROOT="../../../$(BUILD)/ios_unity3d_bundle"
cp docs/ios_unity3d_zt_sdk.md $(BUILD)/ios_unity3d_bundle/README.md cp docs/ios_unity3d_zt_sdk.md $(BUILD)/ios_unity3d_bundle/README.md
# TODO: CHECK if ANDROID/GRADLE TOOLS are installed # TODO: CHECK if ANDROID/GRADLE TOOLS are installed
# Build library for Android Unity integrations # Build library for Android Unity integrations
# Build JNI library for Android app integration # Build JNI library for Android app integration
android_jni_lib: android_jni_lib:
cd $(INT)/android/android_jni_lib/proj; ./gradlew assembleDebug cd $(INT)/android/android_jni_lib/proj; ./gradlew assembleDebug
# copy binary into example android project dir mkdir -p $(BUILD)/android_jni_lib
# mv $(INT)/android/android_jni_lib/java/libs/* build cp docs/android_zt_sdk.md $(BUILD)/android_jni_lib/README.md
mv -f $(INT)/android/android_jni_lib/java/libs/* $(INT)/android/example_app/app/src/main/jniLibs mv -f $(INT)/android/android_jni_lib/java/libs/* $(BUILD)/android_jni_lib
# cd build; for res_f in *; do mv "$res_f" "android_jni_lib_$res_f"; done cp -R $(BUILD)/android_jni_lib/* $(INT)/android/example_app/app/src/main/jniLibs
# cp docs/android_zt_sdk.md $(BUILD)/README.md
remove_only_intermediates: remove_only_intermediates:
-find . -type f \( -name '*.o' -o -name '*.so' \) -delete -find . -type f \( -name '*.o' -o -name '*.so' \) -delete
@@ -141,12 +142,19 @@ $(TEST_OBJDIR):
tests: $(TEST_OBJDIR) $(TEST_TARGETS) tests: $(TEST_OBJDIR) $(TEST_TARGETS)
mkdir -p $(BUILD)/tests; mkdir -p $(BUILD)/tests;
JAVAC := $(shell which javac)
clean: clean:
-rm -rf $(BUILD)/* -rm -rf $(BUILD)/*
-rm -rf zerotier-cli zerotier-idtool -rm -rf zerotier-cli zerotier-idtool
-find . -type f \( -name 'zerotier-one' -o -name 'zerotier-sdk-service' \) -delete -find . -type f \( -name 'zerotier-one' -o -name 'zerotier-sdk-service' \) -delete
-find . -type f \( -name '*.o' -o -name '*.so' -o -name '*.o.d' -o -name '*.out' -o -name '*.log' \) -delete -find . -type f \( -name '*.o' -o -name '*.so' -o -name '*.o.d' -o -name '*.out' -o -name '*.log' \) -delete
# android JNI lib project # android JNI lib project
test -s /usr/bin/javac || { echo "Javac not found"; exit 1; }
-cd $(INT)/android/android_jni_lib/proj; ./gradlew clean -cd $(INT)/android/android_jni_lib/proj; ./gradlew clean
-rm -rf $(INT)/android/android_jni_lib/proj/.gradle -rm -rf $(INT)/android/android_jni_lib/proj/.gradle
-rm -rf $(INT)/android/android_jni_lib/proj/.idea -rm -rf $(INT)/android/android_jni_lib/proj/.idea

View File

@@ -1,74 +0,0 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("cpp_udp_socket_client_test():\n");
ssize_t n_sent;
int sock = -1;
struct sockaddr_in server;
char buf[64];
if(sock == -1) {
sock = socket(AF_INET , SOCK_DGRAM , 0);
if (sock == -1) {
printf("error creating socket\n");
return 1;
}
}
server.sin_addr.s_addr = inet_addr("10.5.5.47");
server.sin_family = AF_INET;
server.sin_port = htons(8888);
//char *buf = "Welcome to the Machine\n";
memcpy(buf, "Welcome to the Machine", sizeof("Welcome to the Machine"));
printf("sizeof(buf) = %d\n", sizeof(buf));
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) {
printf("api_test: error while connecting.\n");
return 1;
}
// Repeatedly send UDP datagram
char data[1024];
memset(data, 0, sizeof(data));
int count =0;
while(1)
{
count++;
usleep(1000);
n_sent = send(sock,data,sizeof(data),0);
if (n_sent < 0) {
perror("Problem sending data");
return 1;
}
if (n_sent != sizeof(buf))
printf("sendto sent %d bytes\n",(int)n_sent);
printf("n_sent = %d, count = %d\n", n_sent,count);
}
return 1;
socklen_t recv_addr_len;
// Clear address info for RX test
server.sin_addr.s_addr = inet_addr("");
server.sin_port = htons(-1);
while (1) {
n_sent=recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&server,&recv_addr_len);
printf("Got a datagram from %s port %d\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
if (n_sent<0) {
perror("Error receiving data");
}
else {
printf("RXed: %s\n", buf);
}
}
return 1;
}

View File

@@ -1,71 +0,0 @@
#include <stdio.h> /* standard C i/o facilities */
#include <stdlib.h> /* needed for atoi() */
#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gethostbyname */
/* this routine echos any messages (UDP datagrams) received */
#define MAXBUF 1024*10
void echo( int sd ) {
int len,n;
char bufin[MAXBUF];
struct sockaddr_in remote;
/* need to know how big address struct is, len must be set before the
call to recvfrom!!! */
len = sizeof(remote);
long count = 0;
while (1) {
sleep(1);
count++;
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
/* print out the address of the sender */
printf("Got a datagram from %s port %d\n",
inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
if (n<0) {
perror("Error receiving data");
} else {
printf("GOT %d BYTES (count = %d)\n",n, count);
sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
}
}
}
int main() {
int ld, length;
struct sockaddr_in skaddr;
if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
printf("Problem creating socket\n");
exit(1);
}
skaddr.sin_family = AF_INET;
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
skaddr.sin_port = htons(8888);
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
printf("Problem binding\n");
exit(0);
}
/*
length = sizeof( skaddr );
if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) {
printf("Error getsockname\n");
exit(1);
}
*/
printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));
/* Go echo every datagram we get */
echo(ld);
return 0;
}

View File

@@ -1,5 +0,0 @@
gcc kq_test_client.c ../../kq.c ../../zt_api.c ../../RPC.c -DSDK_DEBUG -DNETCON_INTERCEPT -o kq_test_client
#gcc kq_test_server.c ../../kq.c -o kq_test_server
gcc server.c -o server
#gcc client.c -o client

View File

@@ -1,124 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/event.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
void monitor_fds();
#define MIN_FD 0
#define MAX_FD_SCAN_SZ 10
void die(const char *str) {
perror(str);
exit(EXIT_FAILURE);
}
int make_nonblocking(int fd) {
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL)))
return -1;
flags |= O_NONBLOCK;
if (-1 == fcntl(fd, F_SETFL, flags))
return -1;
return 0;
}
void changeling()
{
pthread_t thread;
printf("changeling()\n");
int i = 7;
if(pthread_create(&thread, NULL, monitor_fds, (void *)i)) {
die("unable to start changeling thread\n");
}
}
struct timespec tmout = { 0, /* block for 5 seconds at most */
500000 }; /* nanoseconds */
void monitor_fds(){
//sleep(3);
printf("monitor_fds()...\n");
struct kevent change[MAX_FD_SCAN_SZ], event[MAX_FD_SCAN_SZ];
int sockfd, nev, kq;
ssize_t nbytes;
int error;
char buf[BUFSIZ];
if (-1 == (kq = kqueue()))
die("kqueue()");
for(int i=MIN_FD;i<MAX_FD_SCAN_SZ; i++)
{
printf("registering (%d)\n", i);
EV_SET(&change[i-MIN_FD], i, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
//if (-1 == kevent(kq, change, 2, NULL, 0, NULL))
// printf(" unable to register (%d)\n", i);
}
//EV_SET(&change[0], STDIN_FILENO, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
//EV_SET(&change[1], sockfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
if (-1 == kevent(kq, change, 2, NULL, 0, NULL))
die("1kevent()");
for (;;)
{
//usleep(50);
//printf("blocking in kevent()\n");
if (-1 == (nev = kevent(kq, NULL, 0, event, 2, NULL)))
die("2kevent()");
if(nev)
{
printf("kevent() returned %d\n", nev);
for (int i = 0; i < nev; i++)
{
printf("event[%d].ident = %d\n", i, event[i].ident);
/*
if (event[i].ident == STDIN_FILENO)
{
fgets(buf, sizeof(buf), stdin);
nbytes = send(sockfd, buf, strlen(buf), 0);
if (-1 == nbytes)
if (EWOULDBLOCK != errno)
die("send()");
}
else
{
while ((nbytes = recv(sockfd, buf, sizeof(buf), 0)) > 0)
{
if (nbytes != write(STDOUT_FILENO, buf, nbytes))
{
die("write()");
}
}
}
*/
}
}
}
}

View File

@@ -1 +0,0 @@
int changeling();

View File

@@ -1,62 +0,0 @@
/*
C ECHO client example using sockets
*/
#include <stdio.h> //printf
#include <string.h> //strlen
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8889 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
while(1)
{
printf("Enter message : ");
scanf("%s" , message);
//Send some data
if(send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if(recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}
puts("Server reply :");
puts(server_reply);
}
close(sock);
return 0;
}

View File

@@ -1,72 +0,0 @@
/*
C ECHO client example using sockets
*/
#include <stdio.h> //printf
#include <string.h> //strlen
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
#include "../../kq.h"
int main(int argc , char *argv[])
{
changeling();
sleep(2);
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
printf("socket() = %d\n", sock);
sleep(5);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8889 );
printf("attempting to connect..\n");
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
printf("Connected\n");
//keep communicating with server
while(1)
{
printf("Enter message : ");
scanf("%s" , message);
//Send some data
if(send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if(recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}
puts("Server reply :");
puts(server_reply);
}
close(sock);
return 0;
}

View File

@@ -1,79 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "../../kq.h"
#include <time.h>
int main(int argc , char *argv[])
{
changeling();
sleep(3);
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
printf("Socket (%d)\n", socket_desc);
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8889 );
//Bind
int bind_fd;
if( (bind_fd = bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0))
{
//print the error message
perror("bind failed. Error");
return 1;
}
printf("bound\n");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
printf("Connection accepted (%d)\n", client_sock);
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
sleep(1);
//fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}

View File

@@ -1,73 +0,0 @@
/*
C socket server example
*/
#include <stdio.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8889 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}

View File

@@ -14,7 +14,7 @@ int main(int argc, char * argv[])
printf("usage: udp_client <addr> <port>\n"); printf("usage: udp_client <addr> <port>\n");
return 0; return 0;
} }
int sock = -1, int port = atoi(argv[1]); int sock = -1, port = atoi(argv[1]);
ssize_t n_sent; ssize_t n_sent;
struct sockaddr_in server; struct sockaddr_in server;
char buf[64]; char buf[64];
@@ -36,7 +36,7 @@ int main(int argc, char * argv[])
} }
// TX // TX
char *msg = "Welcome to the Machine" char *msg = "Welcome to the Machine";
int count = 0; int count = 0;
while(1) { while(1) {

View File

@@ -42,7 +42,7 @@ int main(int argc, char *argv[]) {
printf("usage: udp_server <port>\n"); printf("usage: udp_server <port>\n");
exit(0); exit(0);
} }
int ld, length, int port = atoi(argv[1]); int ld, length, port = atoi(argv[1]);
struct sockaddr_in skaddr; struct sockaddr_in skaddr;
struct sockaddr_in skaddr2; struct sockaddr_in skaddr2;
@@ -67,7 +67,7 @@ int main(int argc, char *argv[]) {
exit(1); exit(1);
} }
// Display address:port to verify it was sent over RPC correctly // Display address:port to verify it was sent over RPC correctly
int port = ntohs(skaddr2.sin_port); port = ntohs(skaddr2.sin_port);
int ip = skaddr2.sin_addr.s_addr; int ip = skaddr2.sin_addr.s_addr;
unsigned char d[4]; unsigned char d[4];
d[0] = ip & 0xFF; d[0] = ip & 0xFF;

View File

@@ -1,62 +0,0 @@
/*
C ECHO client example using sockets
*/
#include<stdio.h> //printf
#include<string.h> //strlen
#include<sys/socket.h> //socket
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
while(1)
{
printf("Enter message : ");
scanf("%s" , message);
//Send some data
if(send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if(recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}
puts("Server reply :");
puts(server_reply);
}
close(sock);
return 0;
}

View File

View File

@@ -1,73 +0,0 @@
/*
C socket server example
*/
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}

View File

@@ -1,64 +0,0 @@
/*
C ECHO client example using sockets
*/
#include <stdio.h> //printf
#include <string.h> //strlen
#include <sys/socket.h> //socket
#include <arpa/inet.h> //inet_addr
#include "../../sdk.h"
int main(int argc , char *argv[])
{
int sock;
struct sockaddr_in server;
char message[1000] , server_reply[2000];
//Create socket
sock = zt_socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 8888 );
//Connect to remote server
if (zt_connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("connect failed. Error");
return 1;
}
puts("Connected\n");
//keep communicating with server
while(1)
{
printf("Enter message : ");
scanf("%s" , message);
//Send some data
if(zt_send(sock , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if(zt_recv(sock , server_reply , 2000 , 0) < 0)
{
puts("recv failed");
break;
}
puts("Server reply :");
puts(server_reply);
}
close(sock);
return 0;
}

View File

@@ -1,75 +0,0 @@
/*
C socket server example
*/
#include <stdio.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include "../../sdk.h"
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = zt_socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( zt_bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
zt_listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = zt_accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = zt_recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
zt_write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}