aboutsummaryrefslogtreecommitdiffstats
path: root/src/frontends/android/jni/libandroidbridge/android_jni.c
blob: a6412bdf75c580bdd4675b3b5831e30c2f13516d (plain)
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
/*
 * Copyright (C) 2012-2015 Tobias Brunner
 * Copyright (C) 2012 Giuliano Grassi
 * Copyright (C) 2012 Ralf Sager
 * Hochschule fuer Technik Rapperswil
 *
 * 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 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * 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.
 */

#include <dlfcn.h>

#include "android_jni.h"

#include <library.h>
#include <threading/thread_value.h>

/**
 * JVM
 */
static JavaVM *android_jvm;

static struct {
	char name[32];
	void *handle;
} libs[] = {
	{ "libstrongswan.so", NULL },
#ifdef USE_BYOD
	{ "libtncif.so", NULL },
	{ "libtnccs.so", NULL },
	{ "libimcv.so", NULL },
#endif
	{ "libhydra.so", NULL },
	{ "libcharon.so", NULL },
	{ "libipsec.so", NULL },
};

jclass *android_charonvpnservice_class;
jclass *android_charonvpnservice_builder_class;
android_sdk_version_t android_sdk_version;

/**
 * Thread-local variable. Only used because of the destructor
 */
static thread_value_t *androidjni_threadlocal;

/**
 * Thread-local destructor to ensure that a native thread is detached
 * from the JVM even if androidjni_detach_thread() is not called.
 */
static void attached_thread_cleanup(void *arg)
{
	(*android_jvm)->DetachCurrentThread(android_jvm);
}

/*
 * Described in header
 */
void androidjni_attach_thread(JNIEnv **env)
{
	if ((*android_jvm)->GetEnv(android_jvm, (void**)env,
							   JNI_VERSION_1_6) == JNI_OK)
	{	/* already attached or even a Java thread */
		return;
	}
	(*android_jvm)->AttachCurrentThread(android_jvm, env, NULL);
	/* use a thread-local value with a destructor that automatically detaches
	 * the thread from the JVM before it terminates, if not done manually */
	androidjni_threadlocal->set(androidjni_threadlocal, (void*)*env);
}

/*
 * Described in header
 */
void androidjni_detach_thread()
{
	if (androidjni_threadlocal->get(androidjni_threadlocal))
	{	/* only do this if we actually attached this thread */
		androidjni_threadlocal->set(androidjni_threadlocal, NULL);
		(*android_jvm)->DetachCurrentThread(android_jvm);
	}
}

/**
 * Called when this library is loaded by the JVM
 */
jint JNI_OnLoad(JavaVM *vm, void *reserved)
{
	JNIEnv *env;
	jclass jversion;
	jfieldID jsdk_int;
	int i;

	android_jvm = vm;

	if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK)
	{
		return -1;
	}

	for (i = 0; i < countof(libs); i++)
	{
		libs[i].handle = dlopen(libs[i].name, RTLD_GLOBAL);
		if (!libs[i].handle)
		{
			return -1;
		}
	}

	androidjni_threadlocal = thread_value_create(attached_thread_cleanup);

	android_charonvpnservice_class =
				(*env)->NewGlobalRef(env, (*env)->FindClass(env,
						JNI_PACKAGE_STRING "/CharonVpnService"));
	android_charonvpnservice_builder_class =
				(*env)->NewGlobalRef(env, (*env)->FindClass(env,
						JNI_PACKAGE_STRING "/CharonVpnService$BuilderAdapter"));

	jversion = (*env)->FindClass(env, "android/os/Build$VERSION");
	jsdk_int = (*env)->GetStaticFieldID(env, jversion, "SDK_INT", "I");
	android_sdk_version = (*env)->GetStaticIntField(env, jversion, jsdk_int);

	return JNI_VERSION_1_6;
}

/**
 * Called when this library is unloaded by the JVM (which never happens on
 * Android)
 */
void JNI_OnUnload(JavaVM *vm, void *reserved)
{
	int i;

	androidjni_threadlocal->destroy(androidjni_threadlocal);

	for (i = countof(libs) - 1; i >= 0; i--)
	{
		if (libs[i].handle)
		{
			dlclose(libs[i].handle);
		}
	}
}