Set default log level in libandroidbridge
[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 "charonservice.h"
22 #include "android_jni.h"
23
24 #include <daemon.h>
25 #include <hydra.h>
26 #include <ipsec.h>
27 #include <library.h>
28
29 #define ANDROID_DEBUG_LEVEL 1
30
31 typedef struct private_charonservice_t private_charonservice_t;
32
33 /**
34 * private data of charonservice
35 */
36 struct private_charonservice_t {
37
38 /**
39 * public interface
40 */
41 charonservice_t public;
42 };
43
44 /**
45 * Single instance of charonservice_t.
46 */
47 charonservice_t *charonservice;
48
49 /**
50 * hook in library for debugging messages
51 */
52 extern void (*dbg)(debug_t group, level_t level, char *fmt, ...);
53
54 /**
55 * Logging hook for library logs, using android specific logging
56 */
57 static void dbg_android(debug_t group, level_t level, char *fmt, ...)
58 {
59 va_list args;
60
61 if (level <= ANDROID_DEBUG_LEVEL)
62 {
63 char sgroup[16], buffer[8192];
64 char *current = buffer, *next;
65
66 snprintf(sgroup, sizeof(sgroup), "%N", debug_names, group);
67 va_start(args, fmt);
68 vsnprintf(buffer, sizeof(buffer), fmt, args);
69 va_end(args);
70 while (current)
71 { /* log each line separately */
72 next = strchr(current, '\n');
73 if (next)
74 {
75 *(next++) = '\0';
76 }
77 __android_log_print(ANDROID_LOG_INFO, "charon", "00[%s] %s\n",
78 sgroup, current);
79 current = next;
80 }
81 }
82 }
83
84 /**
85 * Initialize the charonservice object
86 */
87 static void charonservice_init()
88 {
89 private_charonservice_t *this;
90
91 INIT(this,
92 .public = {
93 },
94 );
95 charonservice = &this->public;
96
97 lib->settings->set_int(lib->settings,
98 "charon.plugins.android_log.loglevel", ANDROID_DEBUG_LEVEL);
99 }
100
101 /**
102 * Deinitialize the charonservice object
103 */
104 static void charonservice_deinit()
105 {
106 private_charonservice_t *this = (private_charonservice_t*)charonservice;
107
108 free(this);
109 charonservice = NULL;
110 }
111
112 /**
113 * Initialize charon and the libraries via JNI
114 */
115 JNI_METHOD(CharonVpnService, initializeCharon, void)
116 {
117 /* logging for library during initialization, as we have no bus yet */
118 dbg = dbg_android;
119
120 /* initialize library */
121 if (!library_init(NULL))
122 {
123 library_deinit();
124 return;
125 }
126
127 if (!libhydra_init("charon"))
128 {
129 libhydra_deinit();
130 library_deinit();
131 return;
132 }
133
134 if (!libipsec_init())
135 {
136 libipsec_deinit();
137 libhydra_deinit();
138 library_deinit();
139 return;
140 }
141
142 charonservice_init();
143
144 if (!libcharon_init("charon") ||
145 !charon->initialize(charon, PLUGINS))
146 {
147 libcharon_deinit();
148 charonservice_deinit();
149 libipsec_deinit();
150 libhydra_deinit();
151 library_deinit();
152 return;
153 }
154
155 /* start daemon (i.e. the threads in the thread-pool) */
156 charon->start(charon);
157 }
158
159 /**
160 * Deinitialize charon and all libraries
161 */
162 JNI_METHOD(CharonVpnService, deinitializeCharon, void)
163 {
164 libcharon_deinit();
165 charonservice_deinit();
166 libipsec_deinit();
167 libhydra_deinit();
168 library_deinit();
169 }
170