ecc24348118cd6b373c69c310a8295a2b3066539
[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 <daemon.h>
22 #include <library.h>
23
24 #define JNI_PACKAGE org_strongswan_android
25
26 #define JNI_METHOD_PP(pack, klass, name, ret, ...) \
27 ret Java_##pack##_##klass##_##name(JNIEnv *env, jobject this, ##__VA_ARGS__)
28
29 #define JNI_METHOD_P(pack, klass, name, ret, ...) \
30 JNI_METHOD_PP(pack, klass, name, ret, ##__VA_ARGS__)
31
32 #define JNI_METHOD(klass, name, ret, ...) \
33 JNI_METHOD_P(JNI_PACKAGE, klass, name, ret, ##__VA_ARGS__)
34
35 /**
36 * hook in library for debugging messages
37 */
38 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
39
40 /**
41 * Logging hook for library logs, using android specific logging
42 */
43 static void dbg_android(debug_t group, level_t level, char *fmt, ...)
44 {
45 va_list args;
46
47 if (level <= 4)
48 {
49 char sgroup[16], buffer[8192];
50 char *current = buffer, *next;
51 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
52 va_start(args, fmt);
53 vsnprintf(buffer, sizeof(buffer), fmt, args);
54 va_end(args);
55 while (current)
56 { /* log each line separately */
57 next = strchr(current, '\n');
58 if (next)
59 {
60 *(next++) = '\0';
61 }
62 __android_log_print(ANDROID_LOG_INFO, "charon", "00[%s] %s\n",
63 sgroup, current);
64 current = next;
65 }
66 }
67 }
68
69 /**
70 * Initialize charon and the libraries via JNI
71 */
72 JNI_METHOD(CharonVpnService, initializeCharon, void)
73 {
74 /* logging for library during initialization, as we have no bus yet */
75 dbg = dbg_android;
76
77 /* initialize library */
78 if (!library_init(NULL))
79 {
80 library_deinit();
81 return;
82 }
83
84 if (!libhydra_init("charon"))
85 {
86 libhydra_deinit();
87 library_deinit();
88 return;
89 }
90
91 if (!libcharon_init("charon") ||
92 !charon->initialize(charon, PLUGINS))
93 {
94 libcharon_deinit();
95 libhydra_deinit();
96 library_deinit();
97 return;
98 }
99
100 /* start daemon (i.e. the threads in the thread-pool) */
101 charon->start(charon);
102 }
103
104 /**
105 * Initialize charon and the libraries via JNI
106 */
107 JNI_METHOD(CharonVpnService, deinitializeCharon, void)
108 {
109 libcharon_deinit();
110 libhydra_deinit();
111 library_deinit();
112 }
113