Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Error code : 87 #27

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include(CTest)
# --------------------
option(ENABLE_CXX "Enable the C++ wrapper library" ON)
option(ENABLE_PYTHON "Enable the Python wrapper library" OFF)
option(ENABLE_JAVA "Enable the Java wrapper library" OFF)

include(CMakeLists.txt.local OPTIONAL)
include(GenerateExportHeader)
Expand Down Expand Up @@ -123,6 +124,12 @@ if(ENABLE_PYTHON)
add_subdirectory(bindings/python/)
endif (ENABLE_PYTHON)

# Java Binding definition
# -------------------------
if(ENABLE_JAVA)
add_subdirectory(bindings/java/)
endif (ENABLE_JAVA)

# Install rules
# -------------
if(UNIX)
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,19 @@ Example cmake invocation:
Up to version 0.3 the libtuntap `CMakeFiles.txt` exported two libraries: one shared, one static. To simplify the building of the wrapper libraries it was decided to only build one. The default is to build a static library but this behaviour can be changed with the option `BUILD_SHARED_LIBS`.

Example cmake invocation:

$ mkdir build; cd build
$ cmake -D BUILD_SHARED_LIBS=ON ../
$ make

cmake windows x64:

$ cmake -A x64 -D BUILD_SHARED_LIBS=ON ../

cmake Linux x64:

$ cmake -m64 -D BUILD_SHARED_LIBS=ON ../

#### Local configurations

The main `CMakeFiles.txt` includes an optional `CMakeFiles.txt.local` which can be used to store persistent options across builds.
Expand Down
24 changes: 24 additions & 0 deletions bindings/java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# libtuntap java binding CMakeLists.txt
# =====================================

find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${JAVA_INCLUDE_PATH})
include_directories(${JAVA_INCLUDE_PATH2})

add_library(tuntap4j
com_github_smallru8_driver_tuntap_TunTap.h
com_github_smallru8_driver_tuntap_TunTap.cpp
converter.cpp
)
target_include_directories(tuntap4j PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../..
${CMAKE_CURRENT_BINARY_DIR}/../..
)
target_sources(tuntap4j PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/../../tuntap-export.h
)

set_target_properties(tuntap4j PROPERTIES
JAVA_VISIBILITY_PRESET hidden)
6 changes: 6 additions & 0 deletions bindings/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# tuntap4J
Tuntap java interface.

### Usage
import ```com.github.smallru8.driver.tuntap``` to your project.
Create lib32/lib64 and put TunTap.dll/TunTap.so file into it.
238 changes: 238 additions & 0 deletions bindings/java/com_github_smallru8_driver_tuntap_TunTap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
#include "com_github_smallru8_driver_tuntap_TunTap.h"
#include "tuntap.h"

struct device *dev;

JNIEXPORT void JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1init
(JNIEnv *env, jobject obj){
dev = tuntap_init();
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_version
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1version
(JNIEnv *env, jobject obj){
return tuntap_version();
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_destroy
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1destroy
(JNIEnv *env, jobject obj){
tuntap_destroy(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_release
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1release
(JNIEnv *env, jobject obj){
tuntap_release(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_start
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1start
(JNIEnv *env, jobject obj, jint mode, jint id){//0x0001 257
return tuntap_start(dev,mode,id);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_ifname
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1ifname
(JNIEnv *env, jobject obj){
return charTojstring(env,tuntap_get_ifname(dev));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_ifname
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1ifname
(JNIEnv *env, jobject obj, jstring ifname){
return tuntap_set_ifname(dev,jstringToChar(env,ifname));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_hwaddr
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1hwaddr
(JNIEnv *env, jobject obj){
return charTojstring(env,tuntap_get_hwaddr(dev));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_hwaddr
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1hwaddr
(JNIEnv *env, jobject obj, jstring hwaddr){
return tuntap_set_hwaddr(dev,jstringToChar(env,hwaddr));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_descr
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1descr
(JNIEnv *env, jobject obj, jstring descr){
return tuntap_set_descr(dev,jstringToChar(env,descr));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_descr
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1descr
(JNIEnv *env, jobject obj){
return charTojstring(env,tuntap_get_descr(dev));
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_up
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1up
(JNIEnv *env, jobject obj){
return tuntap_up(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_down
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1down
(JNIEnv *env, jobject obj){
return tuntap_down(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_mtu
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1mtu
(JNIEnv *env, jobject obj){
return tuntap_get_mtu(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_mtu
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1mtu
(JNIEnv *env, jobject obj,jint mtu){
return tuntap_set_mtu(dev,mtu);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_ip
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1ip
(JNIEnv *env, jobject obj, jstring ipaddr, jint mask){
return tuntap_set_ip(dev,jstringToChar(env,ipaddr),mask);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_read
* Signature: (I)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1read
(JNIEnv *env, jobject obj, jint len){
int ret = tuntap_get_mtu(dev);
if(len>0)
ret = len;
char *cData = (char*)malloc(sizeof(char)*ret);

ret = tuntap_read(dev,cData,ret);
if(ret==-1)
ret = 0;
jbyteArray jData = env->NewByteArray(ret);
env->SetByteArrayRegion(jData, 0, ret, (jbyte*)cData);
free(cData);
return jData;
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_write
* Signature: ([BI)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1write
(JNIEnv *env, jobject obj, jbyteArray data, jint len){
jbyte *jData;
jData = env->GetByteArrayElements(data, 0);
char *cData = (char*)malloc(sizeof(char)*len);

memcpy(cData, jData, len);
env->ReleaseByteArrayElements(data, jData, 0);
int ret = tuntap_write(dev,cData,len);
free(cData);
return ret;
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_readable
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1readable
(JNIEnv *env, jobject obj){
return tuntap_get_readable(dev);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_nonblocking
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1nonblocking
(JNIEnv *env, jobject obj, jint set){
return tuntap_set_nonblocking(dev,set);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_set_debug
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1set_1debug
(JNIEnv *env, jobject obj, jint set){
return tuntap_set_debug(dev,set);
}

/*
* Class: com_github_smallru8_driver_tuntap_TunTap
* Method: tuntap_get_fd
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_github_smallru8_driver_tuntap_TunTap_tuntap_1get_1fd
(JNIEnv *env, jobject obj){
return (int)tuntap_get_fd(dev);
}
Loading