android: Add a simple HTTP(S) fetcher for CRLs
[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 jclass *android_simple_fetcher_class;
48 android_sdk_version_t android_sdk_version;
49
50 /**
51 * Thread-local variable. Only used because of the destructor
52 */
53 static thread_value_t *androidjni_threadlocal;
54
55 /**
56 * Thread-local destructor to ensure that a native thread is detached
57 * from the JVM even if androidjni_detach_thread() is not called.
58 */
59 static void attached_thread_cleanup(void *arg)
60 {
61 (*android_jvm)->DetachCurrentThread(android_jvm);
62 }
63
64 /*
65 * Described in header
66 */
67 void androidjni_attach_thread(JNIEnv **env)
68 {
69 if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
70 JNI_VERSION_1_6) == JNI_OK)
71 { /* already attached or even a Java thread */
72 return;
73 }
74 (*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
75 /* use a thread-local value with a destructor that automatically detaches
76 * the thread from the JVM before it terminates, if not done manually */
77 androidjni_threadlocal->set(androidjni_threadlocal, (void*)*env);
78 }
79
80 /*
81 * Described in header
82 */
83 void androidjni_detach_thread()
84 {
85 if (androidjni_threadlocal->get(androidjni_threadlocal))
86 { /* only do this if we actually attached this thread */
87 androidjni_threadlocal->set(androidjni_threadlocal, NULL);
88 (*android_jvm)->DetachCurrentThread(android_jvm);
89 }
90 }
91
92 /**
93 * Called when this library is loaded by the JVM
94 */
95 jint JNI_OnLoad(JavaVM *vm, void *reserved)
96 {
97 JNIEnv *env;
98 jclass jversion;
99 jfieldID jsdk_int;
100 int i;
101
102 android_jvm = vm;
103
104 if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
105 {
106 return -1;
107 }
108
109 for (i = 0; i < countof(libs); i++)
110 {
111 libs[i].handle = dlopen(libs[i].name, RTLD_GLOBAL);
112 if (!libs[i].handle)
113 {
114 return -1;
115 }
116 }
117
118 androidjni_threadlocal = thread_value_create(attached_thread_cleanup);
119
120 android_charonvpnservice_class =
121 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
122 JNI_PACKAGE_STRING "/CharonVpnService"));
123 android_charonvpnservice_builder_class =
124 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
125 JNI_PACKAGE_STRING "/CharonVpnService$BuilderAdapter"));
126 android_simple_fetcher_class =
127 (*env)->NewGlobalRef(env, (*env)->FindClass(env,
128 JNI_PACKAGE_STRING "/SimpleFetcher"));
129
130 jversion = (*env)->FindClass(env, "android/os/Build$VERSION");
131 jsdk_int = (*env)->GetStaticFieldID(env, jversion, "SDK_INT", "I");
132 android_sdk_version = (*env)->GetStaticIntField(env, jversion, jsdk_int);
133
134 return JNI_VERSION_1_6;
135 }
136
137 /**
138 * Called when this library is unloaded by the JVM (which never happens on
139 * Android)
140 */
141 void JNI_OnUnload(JavaVM *vm, void *reserved)
142 {
143 int i;
144
145 androidjni_threadlocal->destroy(androidjni_threadlocal);
146
147 for (i = countof(libs) - 1; i >= 0; i--)
148 {
149 if (libs[i].handle)
150 {
151 dlclose(libs[i].handle);
152 }
153 }
154 }
155