updated Doxyfile
[strongswan.git] / src / libstrongswan / utils.h
1 /*
2 * Copyright (C) 2008-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 * $Id$
17 */
18
19 /**
20 * @defgroup utils utils
21 * @{ @ingroup libstrongswan
22 */
23
24 #ifndef UTILS_H_
25 #define UTILS_H_
26
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <stddef.h>
30
31 #include <enum.h>
32
33 /**
34 * Number of bits in a byte
35 */
36 #define BITS_PER_BYTE 8
37
38 /**
39 * Default length for various auxiliary text buffers
40 */
41 #define BUF_LEN 512
42
43 /**
44 * Macro compares two strings for equality
45 */
46 #define streq(x,y) (strcmp(x, y) == 0)
47
48 /**
49 * Macro compares two strings for equality
50 */
51 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
52
53 /**
54 * Macro compares two binary blobs for equality
55 */
56 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
57
58 /**
59 * Macro gives back larger of two values.
60 */
61 #define max(x,y) ((x) > (y) ? (x):(y))
62
63 /**
64 * Macro gives back smaller of two values.
65 */
66 #define min(x,y) ((x) < (y) ? (x):(y))
67
68 /**
69 * Call destructor of an object, if object != NULL
70 */
71 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
72
73 /**
74 * Call offset destructor of an object, if object != NULL
75 */
76 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
77
78 /**
79 * Call function destructor of an object, if object != NULL
80 */
81 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
82
83 /**
84 * Debug macro to follow control flow
85 */
86 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
87
88 /**
89 * Macro to allocate a sized type.
90 */
91 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
92
93 /**
94 * Get the number of elements in an array
95 */
96 #define countof(array) (sizeof(array)/sizeof(array[0]))
97
98 /**
99 * Ignore result of functions tagged with warn_unused_result attributes
100 */
101 #define ignore_result(call) { if(call); }
102
103 /**
104 * Assign a function as a class method
105 */
106 #define ASSIGN(method, function) (method = (typeof(method))function)
107
108 /**
109 * time_t not defined
110 */
111 #define UNDEFINED_TIME 0
112
113 /**
114 * General purpose boolean type.
115 */
116 typedef int bool;
117 #ifndef FALSE
118 # define FALSE 0
119 #endif /* FALSE */
120 #ifndef TRUE
121 # define TRUE 1
122 #endif /* TRUE */
123
124 typedef enum status_t status_t;
125
126 /**
127 * Return values of function calls.
128 */
129 enum status_t {
130 /**
131 * Call succeeded.
132 */
133 SUCCESS,
134
135 /**
136 * Call failed.
137 */
138 FAILED,
139
140 /**
141 * Out of resources.
142 */
143 OUT_OF_RES,
144
145 /**
146 * The suggested operation is already done
147 */
148 ALREADY_DONE,
149
150 /**
151 * Not supported.
152 */
153 NOT_SUPPORTED,
154
155 /**
156 * One of the arguments is invalid.
157 */
158 INVALID_ARG,
159
160 /**
161 * Something could not be found.
162 */
163 NOT_FOUND,
164
165 /**
166 * Error while parsing.
167 */
168 PARSE_ERROR,
169
170 /**
171 * Error while verifying.
172 */
173 VERIFY_ERROR,
174
175 /**
176 * Object in invalid state.
177 */
178 INVALID_STATE,
179
180 /**
181 * Destroy object which called method belongs to.
182 */
183 DESTROY_ME,
184
185 /**
186 * Another call to the method is required.
187 */
188 NEED_MORE,
189 };
190
191 /**
192 * enum_names for type status_t.
193 */
194 extern enum_name_t *status_names;
195
196 /**
197 * deprecated pluto style return value:
198 * error message, NULL for success
199 */
200 typedef const char *err_t;
201
202 /**
203 * Handle struct timeval like an own type.
204 */
205 typedef struct timeval timeval_t;
206
207 /**
208 * Handle struct timespec like an own type.
209 */
210 typedef struct timespec timespec_t;
211
212 /**
213 * Handle struct chunk_t like an own type.
214 */
215 typedef struct sockaddr sockaddr_t;
216
217 /**
218 * Clone a data to a newly allocated buffer
219 */
220 void *clalloc(void *pointer, size_t size);
221
222 /**
223 * Same as memcpy, but XORs src into dst instead of copy
224 */
225 void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
226
227 /**
228 * A variant of strstr with the characteristics of memchr, where haystack is not
229 * a null-terminated string but simply a memory area of length n.
230 */
231 void *memstr(const void *haystack, const char *needle, size_t n);
232
233 /**
234 * Creates a directory and all required parent directories.
235 *
236 * @param path path to the new directory
237 * @param mode permissions of the new directory/directories
238 * @return TRUE on success
239 */
240 bool mkdir_p(const char *path, mode_t mode);
241
242 /**
243 * returns null
244 */
245 void *return_null();
246
247 /**
248 * No-Operation function
249 */
250 void nop();
251
252 /**
253 * Special type to count references
254 */
255 typedef volatile u_int refcount_t;
256
257
258 #ifdef HAVE_GCC_ATOMIC_OPERATIONS
259
260 #define ref_get(ref) {__sync_fetch_and_add(ref, 1); }
261 #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1))
262
263 #else /* !HAVE_GCC_ATOMIC_OPERATIONS */
264
265 /**
266 * Get a new reference.
267 *
268 * Increments the reference counter atomic.
269 *
270 * @param ref pointer to ref counter
271 */
272 void ref_get(refcount_t *ref);
273
274 /**
275 * Put back a unused reference.
276 *
277 * Decrements the reference counter atomic and
278 * says if more references available.
279 *
280 * @param ref pointer to ref counter
281 * @return TRUE if no more references counted
282 */
283 bool ref_put(refcount_t *ref);
284
285 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
286
287 /**
288 * printf hook for time_t.
289 *
290 * Arguments are:
291 * time_t* time, bool utc
292 */
293 int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
294 const void *const *args);
295
296 /**
297 * printf hook for time_t deltas.
298 *
299 * Arguments are:
300 * time_t* begin, time_t* end
301 */
302 int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
303 const void *const *args);
304
305 /**
306 * printf hook for memory areas.
307 *
308 * Arguments are:
309 * u_char *ptr, int len
310 */
311 int mem_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
312 const void *const *args);
313
314 #endif /** UTILS_H_ @}*/