001f5380966c88fae54e5667dfafeb91c09c0a8d
[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 <utils.h>
22 #include <chunk.h>
23 #include <debug.h>
24 #include <utils/identification.h>
25 #include <utils/host.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 #ifdef LEAK_DETECTIVE
45 /**
46 * Memory leak detective, if enabled
47 */
48 leak_detective_t *detective;
49 #endif /* LEAK_DETECTIVE */
50 };
51
52 /**
53 * library instance
54 */
55 library_t *lib;
56
57 /**
58 * Implementation of library_t.destroy
59 */
60 void library_deinit()
61 {
62 private_library_t *this = (private_library_t*)lib;
63
64 this->public.plugins->destroy(this->public.plugins);
65 this->public.settings->destroy(this->public.settings);
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 #ifdef LEAK_DETECTIVE
78 if (this->detective)
79 {
80 this->detective->destroy(this->detective);
81 }
82 #endif /* LEAK_DETECTIVE */
83 free(this);
84 lib = NULL;
85 }
86
87 /*
88 * see header file
89 */
90 bool library_init(char *settings)
91 {
92 printf_hook_t *pfh;
93 private_library_t *this = malloc_thing(private_library_t);
94 lib = &this->public;
95
96 lib->leak_detective = FALSE;
97
98 #ifdef LEAK_DETECTIVE
99 this->detective = leak_detective_create();
100 #endif /* LEAK_DETECTIVE */
101
102 pfh = printf_hook_create();
103 this->public.printf_hook = pfh;
104
105 pfh->add_handler(pfh, 'b', mem_printf_hook,
106 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
107 PRINTF_HOOK_ARGTYPE_END);
108 pfh->add_handler(pfh, 'B', chunk_printf_hook,
109 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
110 pfh->add_handler(pfh, 'H', host_printf_hook,
111 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
112 pfh->add_handler(pfh, 'N', enum_printf_hook,
113 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
114 PRINTF_HOOK_ARGTYPE_END);
115 pfh->add_handler(pfh, 'T', time_printf_hook,
116 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
117 PRINTF_HOOK_ARGTYPE_END);
118 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
119 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
120 PRINTF_HOOK_ARGTYPE_END);
121 pfh->add_handler(pfh, 'Y', identification_printf_hook,
122 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
123
124 this->public.settings = settings_create(settings);
125 this->public.crypto = crypto_factory_create();
126 this->public.creds = credential_factory_create();
127 this->public.encoding = key_encoding_create();
128 this->public.fetcher = fetcher_manager_create();
129 this->public.db = database_factory_create();
130 this->public.plugins = plugin_loader_create();
131 this->public.integrity = NULL;
132
133 if (lib->settings->get_bool(lib->settings,
134 "libstrongswan.integrity_test", FALSE))
135 {
136 #ifdef INTEGRITY_TEST
137 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
138 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
139 {
140 DBG1("integrity check of libstrongswan failed");
141 return FALSE;
142 }
143 #else /* !INTEGRITY_TEST */
144 DBG1("integrity test enabled, but not supported");
145 return FALSE;
146 #endif /* INTEGRITY_TEST */
147 }
148 return TRUE;
149 }
150