fb973a8df437aa888a0eef0395074108619122be
[strongswan.git] / src / frontends / android / app / src / main / jni / libandroidbridge / android_jni.c
1 /*
2 * Copyright (C) 2012-2015 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <dlfcn.h>
19
20 #include "android_jni.h"
21
22 #include <library.h>
23 #include <threading/thread_value.h>
24
25 /**
26 * JVM
27 */
28 static JavaVM *android_jvm;
29
30 static struct {
31 char name[32];
32 void *handle;
33 } libs[] = {
34 { "libstrongswan.so", NULL },
35 #ifdef USE_BYOD
36 { "libtpmtss.so", NULL },
37 { "libtncif.so", NULL },
38 { "libtnccs.so", NULL },
39 { "libimcv.so", NULL },
40 #endif
41 { "libcharon.so", NULL },
42 { "libipsec.so", NULL },
43 };
44
45 jclass *android_charonvpnservice_class;
46 jclass *android_charonvpnservice_builder_class;
47 android_sdk_version_t android_sdk_version;
48
49 /**
50 * Thread-local variable. Only used because of the destructor
51 */
52 static thread_value_t *androidjni_threadlocal;
53
54 /**
55 * Thread-local destructor to ensure that a native thread is detached
56 * from the JVM even if androidjni_detach_thread() is not called.
57 */
58 static void attached_thread_cleanup(void *arg)
59 {
60 (*android_jvm)->DetachCurrentThread(android_jvm);
61 }
62
63 /*
64 * Described in header
65 */
66 void androidjni_attach_thread(JNIEnv **env)
67 {
68 if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
69 JNI_VERSION_1_6) == JNI_OK)
70 { /* already attached or even a Java thread */
71 return;
72 }
73 (*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
74 /* use a thread-local value with a destructor that automatically detaches
75 * the thread from the JVM before it terminates, if not done manually */
76 androidjni_threadlocal->set(androidjni_threadlocal, (void*)*env);
77 }
78
79 /*
80 * Described in header
81 */
82 void androidjni_detach_thread()
83 {
84 if (androidjni_threadlocal->get(androidjni_threadlocal))
85 { /* only do this if we actually attached this thread */
86 androidjni_threadlocal->set(androidjni_threadlocal, NULL);
87 (*android_jvm)->DetachCurrentThread(android_jvm);
88 }
89 }
90
91 /**
92 * Called when this library is loaded by the JVM
93 */
94 jint JNI_OnLoad(JavaVM *vm, void *reserved)
95 {
96 JNIEnv *env;
97 jclass jversion;
98 jfieldID jsdk_int;
99 int i;
100
101 android_jvm = vm;
102
103 if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
104 {
105 return -1;
106 }
107
108 for (i = 0; i < countof(libs); i++)
109 {
110 libs[i].handle = dlopen(libs[i].name, RTLD_GLOBAL);
111 if (!libs[i].handle)
112 {
113 return -1;
114 }
115 }
116
117 androidjni_threadlocal = thread_value_create(attached_thread_cleanup);
118
119 android_charonvpnservice_class =
120 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
121 JNI_PACKAGE_STRING "/CharonVpnService"));
122 android_charonvpnservice_builder_class =
123 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
124 JNI_PACKAGE_STRING "/CharonVpnService$BuilderAdapter"));
125
126 jversion = (*env)->FindClass(env, "android/os/Build$VERSION");
127 jsdk_int = (*env)->GetStaticFieldID(env, jversion, "SDK_INT", "I");
128 android_sdk_version = (*env)->GetStaticIntField(env, jversion, jsdk_int);
129
130 return JNI_VERSION_1_6;
131 }
132
133 /**
134 * Called when this library is unloaded by the JVM (which never happens on
135 * Android)
136 */
137 void JNI_OnUnload(JavaVM *vm, void *reserved)
138 {
139 int i;
140
141 androidjni_threadlocal->destroy(androidjni_threadlocal);
142
143 for (i = countof(libs) - 1; i >= 0; i--)
144 {
145 if (libs[i].handle)
146 {
147 dlclose(libs[i].handle);
148 }
149 }
150 }
151