02ac0cb31395587d775c16b888a46514b12c9ac8
[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 #ifdef LEAK_DETECTIVE
26 #include "utils/leak_detective.h"
27 #endif
28
29 #define CHECKSUM_LIBRARY IPSEC_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 #ifdef LEAK_DETECTIVE
44 /**
45 * Memory leak detective, if enabled
46 */
47 leak_detective_t *detective;
48 #endif /* LEAK_DETECTIVE */
49 };
50
51 /**
52 * library instance
53 */
54 library_t *lib;
55
56 /**
57 * Implementation of library_t.destroy
58 */
59 void library_deinit()
60 {
61 private_library_t *this = (private_library_t*)lib;
62
63 this->public.plugins->destroy(this->public.plugins);
64 this->public.settings->destroy(this->public.settings);
65 this->public.creds->destroy(this->public.creds);
66 this->public.encoding->destroy(this->public.encoding);
67 this->public.crypto->destroy(this->public.crypto);
68 this->public.fetcher->destroy(this->public.fetcher);
69 this->public.db->destroy(this->public.db);
70 this->public.printf_hook->destroy(this->public.printf_hook);
71 if (this->public.integrity)
72 {
73 this->public.integrity->destroy(this->public.integrity);
74 }
75
76 #ifdef LEAK_DETECTIVE
77 if (this->detective)
78 {
79 this->detective->destroy(this->detective);
80 }
81 #endif /* LEAK_DETECTIVE */
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 = FALSE;
101
102 #ifdef LEAK_DETECTIVE
103 this->detective = leak_detective_create();
104 #endif /* LEAK_DETECTIVE */
105
106 pfh = printf_hook_create();
107 this->public.printf_hook = pfh;
108
109 pfh->add_handler(pfh, 'b', mem_printf_hook,
110 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
111 PRINTF_HOOK_ARGTYPE_END);
112 pfh->add_handler(pfh, 'B', chunk_printf_hook,
113 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
114 pfh->add_handler(pfh, 'H', host_printf_hook,
115 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
116 pfh->add_handler(pfh, 'N', enum_printf_hook,
117 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
118 PRINTF_HOOK_ARGTYPE_END);
119 pfh->add_handler(pfh, 'T', time_printf_hook,
120 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
121 PRINTF_HOOK_ARGTYPE_END);
122 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
123 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
124 PRINTF_HOOK_ARGTYPE_END);
125 pfh->add_handler(pfh, 'Y', identification_printf_hook,
126 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
127
128 this->public.settings = settings_create(settings);
129 this->public.crypto = crypto_factory_create();
130 this->public.creds = credential_factory_create();
131 this->public.encoding = key_encoding_create();
132 this->public.fetcher = fetcher_manager_create();
133 this->public.db = database_factory_create();
134 this->public.plugins = plugin_loader_create();
135 this->public.integrity = NULL;
136
137 if (lib->settings->get_bool(lib->settings,
138 "libstrongswan.integrity_test", FALSE))
139 {
140 #ifdef INTEGRITY_TEST
141 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
142 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
143 {
144 DBG1("integrity check of libstrongswan failed");
145 return FALSE;
146 }
147 #else /* !INTEGRITY_TEST */
148 DBG1("integrity test enabled, but not supported");
149 return FALSE;
150 #endif /* INTEGRITY_TEST */
151 }
152 return TRUE;
153 }
154