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