]>
Commit | Line | Data |
---|---|---|
6b3292da | 1 | /* |
d25ce370 | 2 | * Copyright (C) 2009 Tobias Brunner |
552cc11b | 3 | * Copyright (C) 2008 Martin Willi |
6b3292da MW |
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 | ||
60356f33 | 17 | #include "library.h" |
6b3292da | 18 | |
552cc11b | 19 | #include <stdlib.h> |
db7ef624 | 20 | |
5493ffde MW |
21 | #include <debug.h> |
22 | #include <threading/thread.h> | |
23 | #include <utils/identification.h> | |
24 | #include <utils/host.h> | |
2ce569cc | 25 | #include <utils/hashtable.h> |
5493ffde | 26 | #include <selectors/traffic_selector.h> |
db7ef624 | 27 | |
60d62b9e | 28 | #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so" |
0179d468 | 29 | |
552cc11b | 30 | typedef struct private_library_t private_library_t; |
db7ef624 | 31 | |
f27f6296 | 32 | /** |
552cc11b | 33 | * private data of library |
f27f6296 | 34 | */ |
552cc11b | 35 | struct private_library_t { |
f27f6296 | 36 | |
552cc11b MW |
37 | /** |
38 | * public functions | |
39 | */ | |
40 | library_t public; | |
2ce569cc MW |
41 | |
42 | /** | |
43 | * Hashtable with registered objects (name => object) | |
44 | */ | |
45 | hashtable_t *objects; | |
552cc11b | 46 | }; |
60356f33 | 47 | |
db7ef624 | 48 | /** |
552cc11b | 49 | * library instance |
db7ef624 | 50 | */ |
552cc11b | 51 | library_t *lib; |
db7ef624 MW |
52 | |
53 | /** | |
2ce569cc | 54 | * Deinitialize library |
db7ef624 | 55 | */ |
552cc11b | 56 | void library_deinit() |
db7ef624 | 57 | { |
552cc11b | 58 | private_library_t *this = (private_library_t*)lib; |
091d1780 MW |
59 | bool detailed; |
60 | ||
61 | detailed = lib->settings->get_bool(lib->settings, | |
62 | "libstrongswan.leak_detective.detailed", TRUE); | |
552cc11b | 63 | |
2ce569cc | 64 | this->objects->destroy(this->objects); |
e18556e9 TB |
65 | this->public.scheduler->destroy(this->public.scheduler); |
66 | this->public.processor->destroy(this->public.processor); | |
552cc11b MW |
67 | this->public.plugins->destroy(this->public.plugins); |
68 | this->public.settings->destroy(this->public.settings); | |
2ccc02a4 | 69 | this->public.credmgr->destroy(this->public.credmgr); |
552cc11b | 70 | this->public.creds->destroy(this->public.creds); |
d9b24887 | 71 | this->public.encoding->destroy(this->public.encoding); |
552cc11b MW |
72 | this->public.crypto->destroy(this->public.crypto); |
73 | this->public.fetcher->destroy(this->public.fetcher); | |
74 | this->public.db->destroy(this->public.db); | |
75 | this->public.printf_hook->destroy(this->public.printf_hook); | |
960e0c10 MW |
76 | if (this->public.integrity) |
77 | { | |
78 | this->public.integrity->destroy(this->public.integrity); | |
79 | } | |
7daf5226 | 80 | |
f37e8252 | 81 | if (lib->leak_detective) |
db7ef624 | 82 | { |
f37e8252 MW |
83 | lib->leak_detective->report(lib->leak_detective, detailed); |
84 | lib->leak_detective->destroy(lib->leak_detective); | |
db7ef624 | 85 | } |
4a5a5dd2 TB |
86 | |
87 | threads_deinit(); | |
88 | ||
552cc11b MW |
89 | free(this); |
90 | lib = NULL; | |
db7ef624 MW |
91 | } |
92 | ||
2ce569cc MW |
93 | METHOD(library_t, get, void*, |
94 | private_library_t *this, char *name) | |
95 | { | |
96 | return this->objects->get(this->objects, name); | |
97 | } | |
98 | ||
99 | METHOD(library_t, set, bool, | |
100 | private_library_t *this, char *name, void *object) | |
101 | { | |
102 | if (object) | |
103 | { | |
104 | if (this->objects->get(this->objects, name)) | |
105 | { | |
106 | return FALSE; | |
107 | } | |
108 | this->objects->put(this->objects, name, object); | |
109 | return TRUE; | |
110 | } | |
111 | return this->objects->remove(this->objects, name) != NULL; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Hashtable hash function | |
116 | */ | |
117 | static u_int hash(char *key) | |
118 | { | |
119 | return chunk_hash(chunk_create(key, strlen(key))); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Hashtable equals function | |
124 | */ | |
125 | static bool equals(char *a, char *b) | |
126 | { | |
127 | return streq(a, b); | |
128 | } | |
129 | ||
552cc11b MW |
130 | /* |
131 | * see header file | |
db7ef624 | 132 | */ |
a0fc8979 | 133 | bool library_init(char *settings) |
db7ef624 | 134 | { |
2ce569cc | 135 | private_library_t *this; |
552cc11b | 136 | printf_hook_t *pfh; |
2ce569cc MW |
137 | |
138 | INIT(this, | |
139 | .public = { | |
140 | .get = _get, | |
141 | .set = _set, | |
142 | }, | |
143 | ); | |
552cc11b | 144 | lib = &this->public; |
7daf5226 | 145 | |
4a5a5dd2 TB |
146 | threads_init(); |
147 | ||
552cc11b | 148 | #ifdef LEAK_DETECTIVE |
f37e8252 | 149 | lib->leak_detective = leak_detective_create(); |
552cc11b | 150 | #endif /* LEAK_DETECTIVE */ |
2f5914a3 | 151 | |
552cc11b MW |
152 | pfh = printf_hook_create(); |
153 | this->public.printf_hook = pfh; | |
7daf5226 | 154 | |
d25ce370 TB |
155 | pfh->add_handler(pfh, 'b', mem_printf_hook, |
156 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT, | |
157 | PRINTF_HOOK_ARGTYPE_END); | |
158 | pfh->add_handler(pfh, 'B', chunk_printf_hook, | |
159 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END); | |
d25ce370 TB |
160 | pfh->add_handler(pfh, 'H', host_printf_hook, |
161 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END); | |
162 | pfh->add_handler(pfh, 'N', enum_printf_hook, | |
163 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT, | |
164 | PRINTF_HOOK_ARGTYPE_END); | |
165 | pfh->add_handler(pfh, 'T', time_printf_hook, | |
166 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT, | |
167 | PRINTF_HOOK_ARGTYPE_END); | |
168 | pfh->add_handler(pfh, 'V', time_delta_printf_hook, | |
169 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER, | |
170 | PRINTF_HOOK_ARGTYPE_END); | |
d24a74c5 TB |
171 | pfh->add_handler(pfh, 'Y', identification_printf_hook, |
172 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END); | |
5493ffde MW |
173 | pfh->add_handler(pfh, 'R', traffic_selector_printf_hook, |
174 | PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END); | |
7daf5226 | 175 | |
2ce569cc MW |
176 | this->objects = hashtable_create((hashtable_hash_t)hash, |
177 | (hashtable_equals_t)equals, 4); | |
28a0728b | 178 | this->public.settings = settings_create(settings); |
552cc11b MW |
179 | this->public.crypto = crypto_factory_create(); |
180 | this->public.creds = credential_factory_create(); | |
2ccc02a4 | 181 | this->public.credmgr = credential_manager_create(); |
da9724e6 | 182 | this->public.encoding = cred_encoding_create(); |
552cc11b MW |
183 | this->public.fetcher = fetcher_manager_create(); |
184 | this->public.db = database_factory_create(); | |
e18556e9 TB |
185 | this->public.processor = processor_create(); |
186 | this->public.scheduler = scheduler_create(); | |
552cc11b | 187 | this->public.plugins = plugin_loader_create(); |
7daf5226 | 188 | |
960e0c10 MW |
189 | if (lib->settings->get_bool(lib->settings, |
190 | "libstrongswan.integrity_test", FALSE)) | |
191 | { | |
bef50875 | 192 | #ifdef INTEGRITY_TEST |
0179d468 | 193 | this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY); |
6a8c8815 | 194 | if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init)) |
059c479a | 195 | { |
8b0e0910 | 196 | DBG1(DBG_LIB, "integrity check of libstrongswan failed"); |
a0fc8979 | 197 | return FALSE; |
059c479a | 198 | } |
bef50875 | 199 | #else /* !INTEGRITY_TEST */ |
8b0e0910 | 200 | DBG1(DBG_LIB, "integrity test enabled, but not supported"); |
bef50875 MW |
201 | return FALSE; |
202 | #endif /* INTEGRITY_TEST */ | |
960e0c10 | 203 | } |
a0fc8979 | 204 | return TRUE; |
db7ef624 MW |
205 | } |
206 |