2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
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>
29 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
31 typedef struct private_library_t private_library_t
;
34 * private data of library
36 struct private_library_t
{
44 * Hashtable with registered objects (name => object)
55 * Deinitialize library
59 private_library_t
*this = (private_library_t
*)lib
;
62 detailed
= lib
->settings
->get_bool(lib
->settings
,
63 "libstrongswan.leak_detective.detailed", TRUE
);
65 /* make sure the cache is clear before unloading plugins */
66 lib
->credmgr
->flush_cache(lib
->credmgr
, CERT_ANY
);
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
)
84 this->public.integrity
->destroy(this->public.integrity
);
87 if (lib
->leak_detective
)
89 lib
->leak_detective
->report(lib
->leak_detective
, detailed
);
90 lib
->leak_detective
->destroy(lib
->leak_detective
);
100 METHOD(library_t
, get
, void*,
101 private_library_t
*this, char *name
)
103 return this->objects
->get(this->objects
, name
);
106 METHOD(library_t
, set
, bool,
107 private_library_t
*this, char *name
, void *object
)
111 if (this->objects
->get(this->objects
, name
))
115 this->objects
->put(this->objects
, name
, object
);
118 return this->objects
->remove(this->objects
, name
) != NULL
;
122 * Hashtable hash function
124 static u_int
hash(char *key
)
126 return chunk_hash(chunk_create(key
, strlen(key
)));
130 * Hashtable equals function
132 static bool equals(char *a
, char *b
)
140 bool library_init(char *settings
)
142 private_library_t
*this;
156 #ifdef LEAK_DETECTIVE
157 lib
->leak_detective
= leak_detective_create();
158 #endif /* LEAK_DETECTIVE */
160 pfh
= printf_hook_create();
161 this->public.printf_hook
= pfh
;
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
);
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();
199 if (lib
->settings
->get_bool(lib
->settings
,
200 "libstrongswan.integrity_test", FALSE
))
202 #ifdef INTEGRITY_TEST
203 this->public.integrity
= integrity_checker_create(CHECKSUM_LIBRARY
);
204 if (!lib
->integrity
->check(lib
->integrity
, "libstrongswan", library_init
))
206 DBG1(DBG_LIB
, "integrity check of libstrongswan failed");
209 #else /* !INTEGRITY_TEST */
210 DBG1(DBG_LIB
, "integrity test enabled, but not supported");
212 #endif /* INTEGRITY_TEST */