From 8ece167ffd105300f4aaa684d061c540a3c15021 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Fri, 27 Jun 2014 14:58:11 +0800 Subject: [PATCH] add missing files --- jni/main-jni.cpp | 84 +++++++++++++++++++ .../java/com/github/shadowsocks/Core.java | 51 +++++++++++ 2 files changed, 135 insertions(+) create mode 100644 jni/main-jni.cpp create mode 100755 src/main/java/com/github/shadowsocks/Core.java diff --git a/jni/main-jni.cpp b/jni/main-jni.cpp new file mode 100644 index 00000000..fa0298ac --- /dev/null +++ b/jni/main-jni.cpp @@ -0,0 +1,84 @@ +#include +#include +#include + +typedef unsigned short char16_t; + +class String8 { +public: + String8() { + mString = 0; + } + + ~String8() { + if (mString) { + free(mString); + } + } + + void set(const char16_t* o, size_t numChars) { + if (mString) { + free(mString); + } + mString = (char*) malloc(numChars + 1); + if (!mString) { + return; + } + for (size_t i = 0; i < numChars; i++) { + mString[i] = (char) o[i]; + } + mString[numChars] = '\0'; + } + + const char* string() { + return mString; + } +private: + char* mString; +}; + +extern "C" { + +int main(int argc, char* args[]); + +#if defined(PDNSD_JNI) +jint Java_com_github_shadowsocks_Core_pdnsd(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(SSLOCAL_JNI) +jint Java_com_github_shadowsocks_Core_sslocal(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(SSTUNNEL_JNI) +jint Java_com_github_shadowsocks_Core_sstunnel(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(TUN2SOCKS_JNI) +jint Java_com_github_shadowsocks_Core_tun2socks(JNIEnv *env, jobject thiz, jobjectArray argv) { +#else +#error "invalid header" +#endif + + int argc = argv ? env->GetArrayLength(argv) : 0; + char **args = NULL; + String8 tmp_8; + + if (argc > 0) { + args = (char **)malloc((argc+1)*sizeof(char *)); + for (int i = 0; i < argc; ++i) { + jstring arg = reinterpret_cast(env->GetObjectArrayElement(argv, i)); + const jchar *str = env->GetStringCritical(arg, 0); + tmp_8.set(str, env->GetStringLength(arg)); + env->ReleaseStringCritical(arg, str); + args[i] = strdup(tmp_8.string()); + } + args[argc] = NULL; + + pid_t pid; + pid = fork(); + if (pid == 0) { + int ret = main(argc, args); + return ret; + } + + if (args != NULL) free(args); + } + + return 0; +} + +} diff --git a/src/main/java/com/github/shadowsocks/Core.java b/src/main/java/com/github/shadowsocks/Core.java new file mode 100755 index 00000000..7af88993 --- /dev/null +++ b/src/main/java/com/github/shadowsocks/Core.java @@ -0,0 +1,51 @@ +/* Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2012 + * + * 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 . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + +package com.github.shadowsocks; + +public class Core { + static { + java.lang.System.loadLibrary("ss-local-jni"); + java.lang.System.loadLibrary("tun2socks-jni"); + java.lang.System.loadLibrary("pdnsd-jni"); + } + + public static native void sslocal(String[] cmd); + public static native void tun2socks(String[] cmd); + public static native void pdnsd(String[] cmd); +}