30a7774dfacf985cd0b281f1df85f26e99a6b9ef
[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.scheduler->destroy(this->public.scheduler);
84 this->public.processor->destroy(this->public.processor);
85 this->public.plugins->destroy(this->public.plugins);
86 this->public.hosts->destroy(this->public.hosts);
87 this->public.settings->destroy(this->public.settings);
88 this->public.credmgr->destroy(this->public.credmgr);
89 this->public.creds->destroy(this->public.creds);
90 this->public.encoding->destroy(this->public.encoding);
91 this->public.crypto->destroy(this->public.crypto);
92 this->public.proposal->destroy(this->public.proposal);
93 this->public.fetcher->destroy(this->public.fetcher);
94 this->public.db->destroy(this->public.db);
95 this->public.printf_hook->destroy(this->public.printf_hook);
96 this->objects->destroy(this->objects);
97 if (this->public.integrity)
98 {
99 this->public.integrity->destroy(this->public.integrity);
100 }
101
102 if (lib->leak_detective)
103 {
104 lib->leak_detective->report(lib->leak_detective, detailed);
105 lib->leak_detective->destroy(lib->leak_detective);
106 }
107
108 threads_deinit();
109 backtrace_deinit();
110
111 free(this);
112 lib = NULL;
113 }
114
115 METHOD(library_t, get, void*,
116 private_library_t *this, char *name)
117 {
118 return this->objects->get(this->objects, name);
119 }
120
121 METHOD(library_t, set, bool,
122 private_library_t *this, char *name, void *object)
123 {
124 if (object)
125 {
126 if (this->objects->get(this->objects, name))
127 {
128 return FALSE;
129 }
130 this->objects->put(this->objects, name, object);
131 return TRUE;
132 }
133 return this->objects->remove(this->objects, name) != NULL;
134 }
135
136 /**
137 * Hashtable hash function
138 */
139 static u_int hash(char *key)
140 {
141 return chunk_hash(chunk_create(key, strlen(key)));
142 }
143
144 /**
145 * Hashtable equals function
146 */
147 static bool equals(char *a, char *b)
148 {
149 return streq(a, b);
150 }
151
152 /*
153 * see header file
154 */
155 bool library_init(char *settings)
156 {
157 private_library_t *this;
158 printf_hook_t *pfh;
159
160 if (lib)
161 { /* already initialized, increase refcount */
162 this = (private_library_t*)lib;
163 ref_get(&this->ref);
164 return !this->integrity_failed;
165 }
166
167 INIT(this,
168 .public = {
169 .get = _get,
170 .set = _set,
171 },
172 .ref = 1,
173 );
174 lib = &this->public;
175
176 backtrace_init();
177 threads_init();
178
179 #ifdef LEAK_DETECTIVE
180 lib->leak_detective = leak_detective_create();
181 #endif /* LEAK_DETECTIVE */
182
183 pfh = printf_hook_create();
184 this->public.printf_hook = pfh;
185
186 pfh->add_handler(pfh, 'b', mem_printf_hook,
187 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
188 PRINTF_HOOK_ARGTYPE_END);
189 pfh->add_handler(pfh, 'B', chunk_printf_hook,
190 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
191 pfh->add_handler(pfh, 'H', host_printf_hook,
192 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
193 pfh->add_handler(pfh, 'N', enum_printf_hook,
194 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
195 PRINTF_HOOK_ARGTYPE_END);
196 pfh->add_handler(pfh, 'T', time_printf_hook,
197 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
198 PRINTF_HOOK_ARGTYPE_END);
199 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
200 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
201 PRINTF_HOOK_ARGTYPE_END);
202 pfh->add_handler(pfh, 'Y', identification_printf_hook,
203 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
204 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
205 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
206
207 this->objects = hashtable_create((hashtable_hash_t)hash,
208 (hashtable_equals_t)equals, 4);
209 this->public.settings = settings_create(settings);
210 this->public.hosts = host_resolver_create();
211 this->public.proposal = proposal_keywords_create();
212 this->public.crypto = crypto_factory_create();
213 this->public.creds = credential_factory_create();
214 this->public.credmgr = credential_manager_create();
215 this->public.encoding = cred_encoding_create();
216 this->public.fetcher = fetcher_manager_create();
217 this->public.db = database_factory_create();
218 this->public.processor = processor_create();
219 this->public.scheduler = scheduler_create();
220 this->public.plugins = plugin_loader_create();
221
222 if (lib->settings->get_bool(lib->settings,
223 "libstrongswan.integrity_test", FALSE))
224 {
225 #ifdef INTEGRITY_TEST
226 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
227 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
228 {
229 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
230 this->integrity_failed = TRUE;
231 }
232 #else /* !INTEGRITY_TEST */
233 DBG1(DBG_LIB, "integrity test enabled, but not supported");
234 this->integrity_failed = TRUE;
235 #endif /* INTEGRITY_TEST */
236 }
237
238 return !this->integrity_failed;
239 }
240