Load libipsec in Android app.
[strongswan.git] / src / frontends / android / jni / libandroidbridge / charonservice.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <string.h>
17 #include <android/log.h>
18 #include <jni.h>
19
20 #include <hydra.h>
21 #include <ipsec.h>
22 #include <daemon.h>
23 #include <library.h>
24
25 #define JNI_PACKAGE org_strongswan_android
26
27 #define JNI_METHOD_PP(pack, klass, name, ret, ...) \
28 ret Java_##pack##_##klass##_##name(JNIEnv *env, jobject this, ##__VA_ARGS__)
29
30 #define JNI_METHOD_P(pack, klass, name, ret, ...) \
31 JNI_METHOD_PP(pack, klass, name, ret, ##__VA_ARGS__)
32
33 #define JNI_METHOD(klass, name, ret, ...) \
34 JNI_METHOD_P(JNI_PACKAGE, klass, name, ret, ##__VA_ARGS__)
35
36 /**
37 * hook in library for debugging messages
38 */
39 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
40
41 /**
42 * Logging hook for library logs, using android specific logging
43 */
44 static void dbg_android(debug_t group, level_t level, char *fmt, ...)
45 {
46 va_list args;
47
48 if (level <= 4)
49 {
50 char sgroup[16], buffer[8192];
51 char *current = buffer, *next;
52 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
53 va_start(args, fmt);
54 vsnprintf(buffer, sizeof(buffer), fmt, args);
55 va_end(args);
56 while (current)
57 { /* log each line separately */
58 next = strchr(current, '\n');
59 if (next)
60 {
61 *(next++) = '\0';
62 }
63 __android_log_print(ANDROID_LOG_INFO, "charon", "00[%s] %s\n",
64 sgroup, current);
65 current = next;
66 }
67 }
68 }
69
70 /**
71 * Initialize charon and the libraries via JNI
72 */
73 JNI_METHOD(CharonVpnService, initializeCharon, void)
74 {
75 /* logging for library during initialization, as we have no bus yet */
76 dbg = dbg_android;
77
78 /* initialize library */
79 if (!library_init(NULL))
80 {
81 library_deinit();
82 return;
83 }
84
85 if (!libhydra_init("charon"))
86 {
87 libhydra_deinit();
88 library_deinit();
89 return;
90 }
91
92 if (!libipsec_init())
93 {
94 libipsec_deinit();
95 libhydra_deinit();
96 library_deinit();
97 return;
98 }
99
100 if (!libcharon_init("charon") ||
101 !charon->initialize(charon, PLUGINS))
102 {
103 libcharon_deinit();
104 libipsec_deinit();
105 libhydra_deinit();
106 library_deinit();
107 return;
108 }
109
110 /* start daemon (i.e. the threads in the thread-pool) */
111 charon->start(charon);
112 }
113
114 /**
115 * Initialize charon and the libraries via JNI
116 */
117 JNI_METHOD(CharonVpnService, deinitializeCharon, void)
118 {
119 libcharon_deinit();
120 libipsec_deinit();
121 libhydra_deinit();
122 library_deinit();
123 }
124