stream: create library instance of stream-manager
[strongswan.git] / src / libstrongswan / library.c
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "library.h"
18
19 #include <stdlib.h>
20
21 #include <utils/debug.h>
22 #include <threading/thread.h>
23 #include <utils/identification.h>
24 #include <networking/host.h>
25 #include <collections/hashtable.h>
26 #include <utils/backtrace.h>
27 #include <selectors/traffic_selector.h>
28
29 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
30
31 typedef struct private_library_t private_library_t;
32
33 /**
34 * private data of library
35 */
36 struct private_library_t {
37
38 /**
39 * public functions
40 */
41 library_t public;
42
43 /**
44 * Hashtable with registered objects (name => object)
45 */
46 hashtable_t *objects;
47
48 /**
49 * Integrity check failed?
50 */
51 bool integrity_failed;
52
53 /**
54 * Number of times we have been initialized
55 */
56 refcount_t ref;
57 };
58
59 /**
60 * library instance
61 */
62 library_t *lib = NULL;
63
64 /**
65 * Deinitialize library
66 */
67 void library_deinit()
68 {
69 private_library_t *this = (private_library_t*)lib;
70 bool detailed;
71
72 if (!this || !ref_put(&this->ref))
73 { /* have more users */
74 return;
75 }
76
77 detailed = lib->settings->get_bool(lib->settings,
78 "libstrongswan.leak_detective.detailed", TRUE);
79
80 /* make sure the cache is clear before unloading plugins */
81 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
82
83 this->public.streams->destroy(this->public.streams);
84 this->public.watcher->destroy(this->public.watcher);
85 this->public.scheduler->destroy(this->public.scheduler);
86 this->public.processor->destroy(this->public.processor);
87 this->public.plugins->destroy(this->public.plugins);
88 this->public.hosts->destroy(this->public.hosts);
89 this->public.settings->destroy(this->public.settings);
90 this->public.credmgr->destroy(this->public.credmgr);
91 this->public.creds->destroy(this->public.creds);
92 this->public.encoding->destroy(this->public.encoding);
93 this->public.crypto->destroy(this->public.crypto);
94 this->public.caps->destroy(this->public.caps);
95 this->public.proposal->destroy(this->public.proposal);
96 this->public.fetcher->destroy(this->public.fetcher);
97 this->public.resolver->destroy(this->public.resolver);
98 this->public.db->destroy(this->public.db);
99 this->public.printf_hook->destroy(this->public.printf_hook);
100 this->objects->destroy(this->objects);
101 if (this->public.integrity)
102 {
103 this->public.integrity->destroy(this->public.integrity);
104 }
105
106 if (lib->leak_detective)
107 {
108 lib->leak_detective->report(lib->leak_detective, detailed);
109 lib->leak_detective->destroy(lib->leak_detective);
110 }
111
112 threads_deinit();
113 backtrace_deinit();
114
115 free(this);
116 lib = NULL;
117 }
118
119 METHOD(library_t, get, void*,
120 private_library_t *this, char *name)
121 {
122 return this->objects->get(this->objects, name);
123 }
124
125 METHOD(library_t, set, bool,
126 private_library_t *this, char *name, void *object)
127 {
128 if (object)
129 {
130 if (this->objects->get(this->objects, name))
131 {
132 return FALSE;
133 }
134 this->objects->put(this->objects, name, object);
135 return TRUE;
136 }
137 return this->objects->remove(this->objects, name) != NULL;
138 }
139
140 /**
141 * Hashtable hash function
142 */
143 static u_int hash(char *key)
144 {
145 return chunk_hash(chunk_create(key, strlen(key)));
146 }
147
148 /**
149 * Hashtable equals function
150 */
151 static bool equals(char *a, char *b)
152 {
153 return streq(a, b);
154 }
155
156 /**
157 * Number of words we write and memwipe() in memwipe check
158 */
159 #define MEMWIPE_WIPE_WORDS 16
160
161 /**
162 * Write magic to memory, and try to clear it with memwipe()
163 */
164 __attribute__((noinline))
165 static void do_magic(int *magic, int **out)
166 {
167 int buf[MEMWIPE_WIPE_WORDS], i;
168
169 *out = buf;
170 for (i = 0; i < countof(buf); i++)
171 {
172 buf[i] = *magic;
173 }
174 /* passing buf to dbg should make sure the compiler can't optimize out buf.
175 * we use directly dbg(3), as DBG3() might be stripped with DEBUG_LEVEL. */
176 dbg(DBG_LIB, 3, "memwipe() pre: %b", buf, sizeof(buf));
177 memwipe(buf, sizeof(buf));
178 }
179
180 /**
181 * Check if memwipe works as expected
182 */
183 static bool check_memwipe()
184 {
185 int magic = 0xCAFEBABE, *buf, i;
186
187 do_magic(&magic, &buf);
188
189 for (i = 0; i < MEMWIPE_WIPE_WORDS; i++)
190 {
191 if (buf[i] == magic)
192 {
193 DBG1(DBG_LIB, "memwipe() check failed: stackdir: %b",
194 buf, MEMWIPE_WIPE_WORDS * sizeof(int));
195 return FALSE;
196 }
197 }
198 return TRUE;
199 }
200
201 /*
202 * see header file
203 */
204 bool library_init(char *settings)
205 {
206 private_library_t *this;
207 printf_hook_t *pfh;
208
209 if (lib)
210 { /* already initialized, increase refcount */
211 this = (private_library_t*)lib;
212 ref_get(&this->ref);
213 return !this->integrity_failed;
214 }
215
216 INIT(this,
217 .public = {
218 .get = _get,
219 .set = _set,
220 },
221 .ref = 1,
222 );
223 lib = &this->public;
224
225 backtrace_init();
226 threads_init();
227
228 #ifdef LEAK_DETECTIVE
229 lib->leak_detective = leak_detective_create();
230 #endif /* LEAK_DETECTIVE */
231
232 pfh = printf_hook_create();
233 this->public.printf_hook = pfh;
234
235 pfh->add_handler(pfh, 'b', mem_printf_hook,
236 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
237 PRINTF_HOOK_ARGTYPE_END);
238 pfh->add_handler(pfh, 'B', chunk_printf_hook,
239 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
240 pfh->add_handler(pfh, 'H', host_printf_hook,
241 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
242 pfh->add_handler(pfh, 'N', enum_printf_hook,
243 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
244 PRINTF_HOOK_ARGTYPE_END);
245 pfh->add_handler(pfh, 'T', time_printf_hook,
246 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
247 PRINTF_HOOK_ARGTYPE_END);
248 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
249 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
250 PRINTF_HOOK_ARGTYPE_END);
251 pfh->add_handler(pfh, 'Y', identification_printf_hook,
252 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
253 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
254 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
255
256 this->objects = hashtable_create((hashtable_hash_t)hash,
257 (hashtable_equals_t)equals, 4);
258 this->public.settings = settings_create(settings);
259 this->public.hosts = host_resolver_create();
260 this->public.proposal = proposal_keywords_create();
261 this->public.caps = capabilities_create();
262 this->public.crypto = crypto_factory_create();
263 this->public.creds = credential_factory_create();
264 this->public.credmgr = credential_manager_create();
265 this->public.encoding = cred_encoding_create();
266 this->public.fetcher = fetcher_manager_create();
267 this->public.resolver = resolver_manager_create();
268 this->public.db = database_factory_create();
269 this->public.processor = processor_create();
270 this->public.scheduler = scheduler_create();
271 this->public.watcher = watcher_create();
272 this->public.streams = stream_manager_create();
273 this->public.plugins = plugin_loader_create();
274
275 if (!check_memwipe())
276 {
277 return FALSE;
278 }
279
280 if (lib->settings->get_bool(lib->settings,
281 "libstrongswan.integrity_test", FALSE))
282 {
283 #ifdef INTEGRITY_TEST
284 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
285 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
286 {
287 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
288 this->integrity_failed = TRUE;
289 }
290 #else /* !INTEGRITY_TEST */
291 DBG1(DBG_LIB, "integrity test enabled, but not supported");
292 this->integrity_failed = TRUE;
293 #endif /* INTEGRITY_TEST */
294 }
295
296 return !this->integrity_failed;
297 }