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