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