Moved JNI helper macros to a separate file
[strongswan.git] / src / frontends / android / jni / libandroidbridge / charonservice.c
1 /*
2 * Copyright (C) 2012 Giuliano Grassi
3 * Copyright (C) 2012 Ralf Sager
4 * Copyright (C) 2012 Tobias Brunner
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 <string.h>
19 #include <android/log.h>
20
21 #include "android_jni.h"
22
23 #include <daemon.h>
24 #include <hydra.h>
25 #include <ipsec.h>
26 #include <library.h>
27
28
29 /**
30 * hook in library for debugging messages
31 */
32 extern void (*dbg)(debug_t group, level_t level, char *fmt, ...);
33
34 /**
35 * Logging hook for library logs, using android specific logging
36 */
37 static void dbg_android(debug_t group, level_t level, char *fmt, ...)
38 {
39 va_list args;
40
41 if (level <= 4)
42 {
43 char sgroup[16], buffer[8192];
44 char *current = buffer, *next;
45 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
46 va_start(args, fmt);
47 vsnprintf(buffer, sizeof(buffer), fmt, args);
48 va_end(args);
49 while (current)
50 { /* log each line separately */
51 next = strchr(current, '\n');
52 if (next)
53 {
54 *(next++) = '\0';
55 }
56 __android_log_print(ANDROID_LOG_INFO, "charon", "00[%s] %s\n",
57 sgroup, current);
58 current = next;
59 }
60 }
61 }
62
63 /**
64 * Initialize charon and the libraries via JNI
65 */
66 JNI_METHOD(CharonVpnService, initializeCharon, void)
67 {
68 /* logging for library during initialization, as we have no bus yet */
69 dbg = dbg_android;
70
71 /* initialize library */
72 if (!library_init(NULL))
73 {
74 library_deinit();
75 return;
76 }
77
78 if (!libhydra_init("charon"))
79 {
80 libhydra_deinit();
81 library_deinit();
82 return;
83 }
84
85 if (!libipsec_init())
86 {
87 libipsec_deinit();
88 libhydra_deinit();
89 library_deinit();
90 return;
91 }
92
93 if (!libcharon_init("charon") ||
94 !charon->initialize(charon, PLUGINS))
95 {
96 libcharon_deinit();
97 libipsec_deinit();
98 libhydra_deinit();
99 library_deinit();
100 return;
101 }
102
103 /* start daemon (i.e. the threads in the thread-pool) */
104 charon->start(charon);
105 }
106
107 /**
108 * Initialize charon and the libraries via JNI
109 */
110 JNI_METHOD(CharonVpnService, deinitializeCharon, void)
111 {
112 libcharon_deinit();
113 libipsec_deinit();
114 libhydra_deinit();
115 library_deinit();
116 }
117