make IMC/IMV pairs independent of libcharon
[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 <selectors/traffic_selector.h>
26 #ifdef LEAK_DETECTIVE
27 #include <utils/leak_detective.h>
28 #endif
29
30 #define CHECKSUM_LIBRARY IPSEC_DIR"/libchecksum.so"
31
32 typedef struct private_library_t private_library_t;
33
34 /**
35 * private data of library
36 */
37 struct private_library_t {
38
39 /**
40 * public functions
41 */
42 library_t public;
43 };
44
45 /**
46 * library instance
47 */
48 library_t *lib;
49
50 /**
51 * Implementation of library_t.destroy
52 */
53 void library_deinit()
54 {
55 private_library_t *this = (private_library_t*)lib;
56 bool detailed;
57
58 detailed = lib->settings->get_bool(lib->settings,
59 "libstrongswan.leak_detective.detailed", TRUE);
60
61 this->public.scheduler->destroy(this->public.scheduler);
62 this->public.processor->destroy(this->public.processor);
63 this->public.plugins->destroy(this->public.plugins);
64 this->public.settings->destroy(this->public.settings);
65 this->public.credmgr->destroy(this->public.credmgr);
66 this->public.creds->destroy(this->public.creds);
67 this->public.encoding->destroy(this->public.encoding);
68 this->public.crypto->destroy(this->public.crypto);
69 this->public.fetcher->destroy(this->public.fetcher);
70 this->public.db->destroy(this->public.db);
71 this->public.printf_hook->destroy(this->public.printf_hook);
72 if (this->public.integrity)
73 {
74 this->public.integrity->destroy(this->public.integrity);
75 }
76
77 if (lib->leak_detective)
78 {
79 lib->leak_detective->report(lib->leak_detective, detailed);
80 lib->leak_detective->destroy(lib->leak_detective);
81 }
82
83 threads_deinit();
84
85 free(this);
86 lib = NULL;
87 }
88
89 /*
90 * see header file
91 */
92 bool library_init(char *settings)
93 {
94 printf_hook_t *pfh;
95 private_library_t *this = malloc_thing(private_library_t);
96 lib = &this->public;
97
98 threads_init();
99
100 lib->leak_detective = NULL;
101 #ifdef LEAK_DETECTIVE
102 lib->leak_detective = leak_detective_create();
103 #endif /* LEAK_DETECTIVE */
104
105 pfh = printf_hook_create();
106 this->public.printf_hook = pfh;
107
108 pfh->add_handler(pfh, 'b', mem_printf_hook,
109 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
110 PRINTF_HOOK_ARGTYPE_END);
111 pfh->add_handler(pfh, 'B', chunk_printf_hook,
112 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
113 pfh->add_handler(pfh, 'H', host_printf_hook,
114 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
115 pfh->add_handler(pfh, 'N', enum_printf_hook,
116 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
117 PRINTF_HOOK_ARGTYPE_END);
118 pfh->add_handler(pfh, 'T', time_printf_hook,
119 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
120 PRINTF_HOOK_ARGTYPE_END);
121 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
122 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
123 PRINTF_HOOK_ARGTYPE_END);
124 pfh->add_handler(pfh, 'Y', identification_printf_hook,
125 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
126 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
127 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
128
129 this->public.settings = settings_create(settings);
130 this->public.crypto = crypto_factory_create();
131 this->public.creds = credential_factory_create();
132 this->public.credmgr = credential_manager_create();
133 this->public.encoding = cred_encoding_create();
134 this->public.fetcher = fetcher_manager_create();
135 this->public.db = database_factory_create();
136 this->public.processor = processor_create();
137 this->public.scheduler = scheduler_create();
138 this->public.plugins = plugin_loader_create();
139 this->public.integrity = NULL;
140
141 if (lib->settings->get_bool(lib->settings,
142 "libstrongswan.integrity_test", FALSE))
143 {
144 #ifdef INTEGRITY_TEST
145 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
146 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
147 {
148 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
149 return FALSE;
150 }
151 #else /* !INTEGRITY_TEST */
152 DBG1(DBG_LIB, "integrity test enabled, but not supported");
153 return FALSE;
154 #endif /* INTEGRITY_TEST */
155 }
156 return TRUE;
157 }
158