added doxygen comments
[strongswan.git] / src / libstrongswan / library.h
1 /**
2 * @file library.h
3 *
4 * @brief Helper functions and definitions.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef LIBRARY_H_
24 #define LIBRARY_H_
25
26 /**
27 * @defgroup libstrongswan libstrongswan
28 *
29 * libstrongswan: library with various cryptographic, X.509 trust chain and
30 * identity management functions.
31 */
32
33 /**
34 * @defgroup asn1 asn1
35 *
36 * ASN.1 definitions, parser and generator functions.
37 *
38 * @ingroup libstrongswan
39 */
40
41 /**
42 * @defgroup crypto crypto
43 *
44 * Various cryptographic algorithms.
45 *
46 * @ingroup libstrongswan
47 */
48
49 /**
50 * @defgroup crypters crypters
51 *
52 * Symmetric encryption algorithms, used for
53 * encryption and decryption.
54 *
55 * @ingroup crypto
56 */
57
58 /**
59 * @defgroup hashers hashers
60 *
61 * Hashing algorithms, such as MD5 or SHA1
62 *
63 * @ingroup crypto
64 */
65
66 /**
67 * @defgroup prfs prfs
68 *
69 * Pseudo random functions, used to generate
70 * pseude random byte sequences.
71 *
72 * @ingroup crypto
73 */
74
75 /**
76 * @defgroup rsa rsa
77 *
78 * RSA private/public key algorithm.
79 *
80 * @ingroup crypto
81 */
82
83 /**
84 * @defgroup signers signers
85 *
86 * Symmetric signing algorithms,
87 * used to ensure message integrity.
88 *
89 * @ingroup crypto
90 */
91
92 /**
93 * @defgroup utils utils
94 *
95 * Generic helper classes.
96 *
97 * @ingroup libstrongswan
98 */
99
100 #include <gmp.h>
101 #include <sys/types.h>
102 #include <stdlib.h>
103 #include <stddef.h>
104 #include <printf.h>
105
106 #include <enum.h>
107
108 /**
109 * Number of bits in a byte
110 */
111 #define BITS_PER_BYTE 8
112
113 /**
114 * Default length for various auxiliary text buffers
115 */
116 #define BUF_LEN 512
117
118 /**
119 * Macro compares two strings for equality
120 */
121 #define streq(x,y) (strcmp(x, y) == 0)
122
123 /**
124 * Macro compares two binary blobs for equality
125 */
126 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
127
128 /**
129 * Macro gives back larger of two values.
130 */
131 #define max(x,y) ((x) > (y) ? (x):(y))
132
133 /**
134 * Macro gives back smaller of two values.
135 */
136 #define min(x,y) ((x) < (y) ? (x):(y))
137
138 /**
139 * Call destructor of a object if object != NULL
140 */
141 #define DESTROY_IF(obj) if (obj) obj->destroy(obj)
142
143 /**
144 * Debug macro to follow control flow
145 */
146 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
147
148 /**
149 * Macro to allocate a sized type.
150 */
151 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
152
153 /**
154 * Assign a function as a class method
155 */
156 #define ASSIGN(method, function) (method = (typeof(method))function)
157
158 /**
159 * time_t not defined
160 */
161 #define UNDEFINED_TIME 0
162
163 /**
164 * General purpose boolean type.
165 */
166 typedef int bool;
167 #define FALSE 0
168 #define TRUE 1
169
170 typedef enum status_t status_t;
171
172 /**
173 * Return values of function calls.
174 */
175 enum status_t {
176 /**
177 * Call succeeded.
178 */
179 SUCCESS,
180
181 /**
182 * Call failed.
183 */
184 FAILED,
185
186 /**
187 * Out of resources.
188 */
189 OUT_OF_RES,
190
191 /**
192 * The suggested operation is already done
193 */
194 ALREADY_DONE,
195
196 /**
197 * Not supported.
198 */
199 NOT_SUPPORTED,
200
201 /**
202 * One of the arguments is invalid.
203 */
204 INVALID_ARG,
205
206 /**
207 * Something could not be found.
208 */
209 NOT_FOUND,
210
211 /**
212 * Error while parsing.
213 */
214 PARSE_ERROR,
215
216 /**
217 * Error while verifying.
218 */
219 VERIFY_ERROR,
220
221 /**
222 * Object in invalid state.
223 */
224 INVALID_STATE,
225
226 /**
227 * Destroy object which called method belongs to.
228 */
229 DESTROY_ME,
230
231 /**
232 * Another call to the method is required.
233 */
234 NEED_MORE,
235 };
236
237 /**
238 * used by strict_crl_policy
239 */
240 typedef enum {
241 STRICT_NO,
242 STRICT_YES,
243 STRICT_IFURI
244 } strict_t;
245
246 /**
247 * enum_names for type status_t.
248 */
249 extern enum_name_t *status_names;
250
251 /**
252 * deprecated pluto style return value:
253 * error message, NULL for success
254 */
255 typedef const char *err_t;
256
257 /**
258 * Handle struct timeval like an own type.
259 */
260 typedef struct timeval timeval_t;
261
262 /**
263 * Handle struct timespec like an own type.
264 */
265 typedef struct timespec timespec_t;
266
267 /**
268 * Handle struct chunk_t like an own type.
269 */
270 typedef struct sockaddr sockaddr_t;
271
272 /**
273 * Clone a data to a newly allocated buffer
274 */
275 void *clalloc(void *pointer, size_t size);
276
277 /**
278 * Same as memcpy, but XORs src into dst instead of copy
279 */
280 void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
281
282 /**
283 * Special type to count references
284 */
285 typedef volatile u_int refcount_t;
286
287 /**
288 * @brief Get a new reference.
289 *
290 * Increments the reference counter atomic.
291 *
292 * @param ref pointer to ref counter
293 */
294 void ref_get(refcount_t *ref);
295
296 /**
297 * @brief Put back a unused reference.
298 *
299 * Decrements the reference counter atomic and
300 * says if more references available.
301 *
302 * @param ref pointer to ref counter
303 * @return TRUE if no more references counted
304 */
305 bool ref_put(refcount_t *ref);
306
307
308 #include <chunk.h>
309 #include <printf_hook.h>
310
311 #endif /* LIBRARY_H_ */