1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include <jni.h>
#include <android/log.h>
#include <stdlib.h>
#include <setjmp.h>
#include "jniglue.h"
JNIEXPORT jint Java_de_blinkt_OpenVPN_startOpenVPNThread(JNIEnv* env, jclass jc);
extern int main (int argc, char *argv[]);
static jmp_buf jump_buffer;
int callmain (int argc, char *argv[]) {
if(!setjmp(jump_buffer))
main(argc,argv);
}
void android_openvpn_exit(int status) {
longjmp(jump_buffer,status+1);
}
void testmsg(char* m1, ...) {
va_list arglist;
va_start(arglist,m1);
// vsnprintf(m1, 100, "foo bbaz",arglist);
__android_log_vprint(ANDROID_LOG_DEBUG, "openvpn",m1, arglist);
va_end(arglist);
}
// Store env and class, we allow only one instance
// so make these variables global for now
jclass openvpnclass;
JNIEnv* openvpnjenv;
//Lde/blinkt/openvpn/OpenVPN startOpenVPNThread startOpenVPNThread
jint Java_de_blinkt_openvpn_OpenVPN_startOpenVPNThread(JNIEnv* env, jclass jc){
char* argv[] = {"openvpn", "--client",
"--dev","tun",
"--comp-lzo",
// "--redirect-gateway","def1",
// "--pkcs12","/mnt/sdcard/Network_Certificate.p12",
"--remote-cert-eku", "TLS Web Server Authentication",
"--remote","openvpn.uni-paderborn.de",
"--ca","/mnt/sdcard/ca.pem",
"--key","/mnt/sdcard/schwabe.key",
"--cert","/mnt/sdcard/schwabe.pem",
"--verb","4"
};
openvpnclass = jc;
openvpnjenv= env;
int argc=17;
return callmain(argc,argv);
}
void Java_de_blinkt_openvpn_OpenVPN_startOpenVPNThreadArgs(JNIEnv *env,jclass jc, jobjectArray stringArray) {
openvpnclass = jc;
openvpnjenv= env;
int stringCount = (*env)->GetArrayLength(env, stringArray);
const char** argv = calloc(stringCount,sizeof(const char*));
int i;
for (i=0; i<stringCount; i++) {
jstring string = (jstring) (*env)->GetObjectArrayElement(env, stringArray, i);
jboolean isCopy;
const char* rawString = (*env)->GetStringUTFChars(env, string, &isCopy);
// Copy the string to able to release it
argv[i] = rawString;
}
// Call main
callmain(stringCount,argv);
// Release the Strings
for(i=0; i<stringCount;i++){
jstring string = (jstring) (*env)->GetObjectArrayElement(env, stringArray, i);
(*env)->ReleaseStringUTFChars(env,string,argv[i]);
}
free(argv);
}
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
__android_log_write(ANDROID_LOG_DEBUG,"openvpn", "Loading openvpn native library $id$ compiled on " __DATE__ " " __TIME__ );
return JNI_VERSION_1_2;
}
void addInterfaceInformation(int mtu,const char* ifconfig_local, const char* ifconfig_remote)
{
jstring jlocal = (*openvpnjenv)->NewStringUTF(openvpnjenv, ifconfig_local);
jstring jremote = (*openvpnjenv)->NewStringUTF(openvpnjenv, ifconfig_remote);
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "addInterfaceInfo",
"(ILjava/lang/String;Ljava/lang/String;)V");
(*openvpnjenv)->CallStaticVoidMethod(openvpnjenv,openvpnclass,aMethodID,mtu,jlocal,jremote);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jlocal);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jremote);
}
void android_openvpn_log(int level,const char* prefix,const char* prefix_sep,const char* m1)
{
__android_log_print(ANDROID_LOG_DEBUG,"openvpn","%s%s%s",prefix,prefix_sep,m1);
jstring jprefix = (*openvpnjenv)->NewStringUTF(openvpnjenv, prefix);
jstring jmessage = (*openvpnjenv)->NewStringUTF(openvpnjenv, m1);
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "logMessage",
"(ILjava/lang/String;Ljava/lang/String;)V");
(*openvpnjenv)->CallStaticVoidMethod(openvpnjenv,openvpnclass,aMethodID,level,jprefix,jmessage);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jprefix);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jmessage);
}
unsigned char android_protect_socket(int sockfd) {
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "protectSocket",
"(I)Z");
return (*openvpnjenv)->CallStaticBooleanMethod(openvpnjenv,openvpnclass,aMethodID,sockfd);
}
int android_open_tun () {
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "openTunDevice",
"()I");
return (*openvpnjenv)->CallStaticIntMethod(openvpnjenv,openvpnclass,aMethodID);
}
void android_set_dns(const char* dns) {
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "addDns",
"(Ljava/lang/String;)V");
jstring jdns = (*openvpnjenv)->NewStringUTF(openvpnjenv,dns);
(*openvpnjenv)->CallStaticVoidMethod(openvpnjenv,openvpnclass,aMethodID,jdns);
}
void android_set_domain(const char* domain) {
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "addDomain",
"(Ljava/lang/String;)V");
jstring jdomain = (*openvpnjenv)->NewStringUTF(openvpnjenv,domain);
(*openvpnjenv)->CallStaticVoidMethod(openvpnjenv,openvpnclass,aMethodID,jdomain);
}
void addRouteInformation(const char* dest, const char* mask, const char* gw) {
jstring jmask = (*openvpnjenv)->NewStringUTF(openvpnjenv, mask);
jstring jdest = (*openvpnjenv)->NewStringUTF(openvpnjenv, dest);
jstring jgw = (*openvpnjenv)->NewStringUTF(openvpnjenv, gw);
jmethodID aMethodID = (*openvpnjenv)->GetStaticMethodID(openvpnjenv, openvpnclass, "addRoute",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
(*openvpnjenv)->CallStaticVoidMethod(openvpnjenv,openvpnclass,aMethodID,jdest,jmask,jgw);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jmask);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jdest);
(*openvpnjenv)->DeleteLocalRef(openvpnjenv,jgw);
}
|