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