Added possibility to register custom proposal keywords
[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 <debug.h>
22 #include <threading/thread.h>
23 #include <utils/identification.h>
24 #include <utils/host.h>
25 #include <utils/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 /**
50 * library instance
51 */
52 library_t *lib;
53
54 /**
55 * Deinitialize library
56 */
57 void library_deinit()
58 {
59 private_library_t *this = (private_library_t*)lib;
60 bool detailed;
61
62 detailed = lib->settings->get_bool(lib->settings,
63 "libstrongswan.leak_detective.detailed", TRUE);
64
65 /* make sure the cache is clear before unloading plugins */
66 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
67
68 this->public.scheduler->destroy(this->public.scheduler);
69 this->public.processor->destroy(this->public.processor);
70 this->public.plugins->destroy(this->public.plugins);
71 this->public.settings->destroy(this->public.settings);
72 this->public.credmgr->destroy(this->public.credmgr);
73 this->public.creds->destroy(this->public.creds);
74 this->public.encoding->destroy(this->public.encoding);
75 this->public.crypto->destroy(this->public.crypto);
76 this->public.proposal->destroy(this->public.proposal);
77 this->public.fetcher->destroy(this->public.fetcher);
78 this->public.db->destroy(this->public.db);
79 this->public.printf_hook->destroy(this->public.printf_hook);
80 this->objects->destroy(this->objects);
81 if (this->public.integrity)
82 {
83 this->public.integrity->destroy(this->public.integrity);
84 }
85
86 if (lib->leak_detective)
87 {
88 lib->leak_detective->report(lib->leak_detective, detailed);
89 lib->leak_detective->destroy(lib->leak_detective);
90 }
91
92 threads_deinit();
93 backtrace_deinit();
94
95 free(this);
96 lib = NULL;
97 }
98
99 METHOD(library_t, get, void*,
100 private_library_t *this, char *name)
101 {
102 return this->objects->get(this->objects, name);
103 }
104
105 METHOD(library_t, set, bool,
106 private_library_t *this, char *name, void *object)
107 {
108 if (object)
109 {
110 if (this->objects->get(this->objects, name))
111 {
112 return FALSE;
113 }
114 this->objects->put(this->objects, name, object);
115 return TRUE;
116 }
117 return this->objects->remove(this->objects, name) != NULL;
118 }
119
120 /**
121 * Hashtable hash function
122 */
123 static u_int hash(char *key)
124 {
125 return chunk_hash(chunk_create(key, strlen(key)));
126 }
127
128 /**
129 * Hashtable equals function
130 */
131 static bool equals(char *a, char *b)
132 {
133 return streq(a, b);
134 }
135
136 /*
137 * see header file
138 */
139 bool library_init(char *settings)
140 {
141 private_library_t *this;
142 printf_hook_t *pfh;
143
144 INIT(this,
145 .public = {
146 .get = _get,
147 .set = _set,
148 },
149 );
150 lib = &this->public;
151
152 backtrace_init();
153 threads_init();
154
155 #ifdef LEAK_DETECTIVE
156 lib->leak_detective = leak_detective_create();
157 #endif /* LEAK_DETECTIVE */
158
159 pfh = printf_hook_create();
160 this->public.printf_hook = pfh;
161
162 pfh->add_handler(pfh, 'b', mem_printf_hook,
163 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
164 PRINTF_HOOK_ARGTYPE_END);
165 pfh->add_handler(pfh, 'B', chunk_printf_hook,
166 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
167 pfh->add_handler(pfh, 'H', host_printf_hook,
168 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
169 pfh->add_handler(pfh, 'N', enum_printf_hook,
170 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
171 PRINTF_HOOK_ARGTYPE_END);
172 pfh->add_handler(pfh, 'T', time_printf_hook,
173 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
174 PRINTF_HOOK_ARGTYPE_END);
175 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
176 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
177 PRINTF_HOOK_ARGTYPE_END);
178 pfh->add_handler(pfh, 'Y', identification_printf_hook,
179 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
180 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
181 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
182
183 this->objects = hashtable_create((hashtable_hash_t)hash,
184 (hashtable_equals_t)equals, 4);
185 this->public.settings = settings_create(settings);
186 this->public.proposal = proposal_keywords_create();
187 this->public.crypto = crypto_factory_create();
188 this->public.creds = credential_factory_create();
189 this->public.credmgr = credential_manager_create();
190 this->public.encoding = cred_encoding_create();
191 this->public.fetcher = fetcher_manager_create();
192 this->public.db = database_factory_create();
193 this->public.processor = processor_create();
194 this->public.scheduler = scheduler_create();
195 this->public.plugins = plugin_loader_create();
196
197 if (lib->settings->get_bool(lib->settings,
198 "libstrongswan.integrity_test", FALSE))
199 {
200 #ifdef INTEGRITY_TEST
201 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
202 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
203 {
204 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
205 return FALSE;
206 }
207 #else /* !INTEGRITY_TEST */
208 DBG1(DBG_LIB, "integrity test enabled, but not supported");
209 return FALSE;
210 #endif /* INTEGRITY_TEST */
211 }
212
213 return TRUE;
214 }
215