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