298253fdd4a684bb1180adcd46516b6fc4c7f1bb
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
20 * @defgroup utils utils
21 * @{ @ingroup libstrongswan
27 #include <sys/types.h>
34 * Number of bits in a byte
36 #define BITS_PER_BYTE 8
39 * Default length for various auxiliary text buffers
44 * Macro compares two strings for equality
46 #define streq(x,y) (strcmp(x, y) == 0)
49 * Macro compares two strings for equality
51 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
54 * Macro compares two binary blobs for equality
56 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
59 * Macro gives back larger of two values.
61 #define max(x,y) ((x) > (y) ? (x):(y))
64 * Macro gives back smaller of two values.
66 #define min(x,y) ((x) < (y) ? (x):(y))
69 * Call destructor of an object, if object != NULL
71 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
74 * Call offset destructor of an object, if object != NULL
76 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
79 * Call function destructor of an object, if object != NULL
81 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
84 * Debug macro to follow control flow
86 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
89 * Macro to allocate a sized type.
91 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
94 * Get the number of elements in an array
96 #define countof(array) (sizeof(array)/sizeof(array[0]))
99 * Ignore result of functions tagged with warn_unused_result attributes
101 #define ignore_result(call) { if(call); }
104 * Assign a function as a class method
106 #define ASSIGN(method, function) (method = (typeof(method))function)
111 #define UNDEFINED_TIME 0
114 * General purpose boolean type.
124 typedef enum status_t status_t
;
127 * Return values of function calls.
146 * The suggested operation is already done
156 * One of the arguments is invalid.
161 * Something could not be found.
166 * Error while parsing.
171 * Error while verifying.
176 * Object in invalid state.
181 * Destroy object which called method belongs to.
186 * Another call to the method is required.
192 * enum_names for type status_t.
194 extern enum_name_t
*status_names
;
197 * deprecated pluto style return value:
198 * error message, NULL for success
200 typedef const char *err_t
;
203 * Handle struct timeval like an own type.
205 typedef struct timeval timeval_t
;
208 * Handle struct timespec like an own type.
210 typedef struct timespec timespec_t
;
213 * Handle struct chunk_t like an own type.
215 typedef struct sockaddr sockaddr_t
;
218 * Clone a data to a newly allocated buffer
220 void *clalloc(void *pointer
, size_t size
);
223 * Same as memcpy, but XORs src into dst instead of copy
225 void memxor(u_int8_t dest
[], u_int8_t src
[], size_t n
);
228 * Creates a directory and all required parent directories.
230 * @param path path to the new directory
231 * @param mode permissions of the new directory/directories
232 * @return TRUE on success
234 bool mkdir_p(const char *path
, mode_t mode
);
242 * No-Operation function
247 * Special type to count references
249 typedef volatile u_int refcount_t
;
252 #ifdef HAVE_GCC_ATOMIC_OPERATIONS
254 #define ref_get(ref) {__sync_fetch_and_add(ref, 1); }
255 #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1))
257 #else /* !HAVE_GCC_ATOMIC_OPERATIONS */
260 * Get a new reference.
262 * Increments the reference counter atomic.
264 * @param ref pointer to ref counter
266 void ref_get(refcount_t
*ref
);
269 * Put back a unused reference.
271 * Decrements the reference counter atomic and
272 * says if more references available.
274 * @param ref pointer to ref counter
275 * @return TRUE if no more references counted
277 bool ref_put(refcount_t
*ref
);
279 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
282 * Get printf hooks for time.
286 * Arguments using #-specificer
287 * time_t* time, bool utc
289 printf_hook_functions_t
time_get_printf_hooks();
292 * Get printf hooks for time deltas.
296 * Arguments using #-specificer
297 * time_t* begin, time_t* end
299 printf_hook_functions_t
time_delta_get_printf_hooks();
302 * Get printf hooks for time deltas.
305 * u_char *ptr, int len
307 printf_hook_functions_t
mem_get_printf_hooks();
309 #endif /* UTILS_H_ @}*/