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 * Assign a function as a class method
101 #define ASSIGN(method, function) (method = (typeof(method))function)
106 #define UNDEFINED_TIME 0
109 * General purpose boolean type.
119 typedef enum status_t status_t
;
122 * Return values of function calls.
141 * The suggested operation is already done
151 * One of the arguments is invalid.
156 * Something could not be found.
161 * Error while parsing.
166 * Error while verifying.
171 * Object in invalid state.
176 * Destroy object which called method belongs to.
181 * Another call to the method is required.
187 * enum_names for type status_t.
189 extern enum_name_t
*status_names
;
192 * deprecated pluto style return value:
193 * error message, NULL for success
195 typedef const char *err_t
;
198 * Handle struct timeval like an own type.
200 typedef struct timeval timeval_t
;
203 * Handle struct timespec like an own type.
205 typedef struct timespec timespec_t
;
208 * Handle struct chunk_t like an own type.
210 typedef struct sockaddr sockaddr_t
;
213 * Clone a data to a newly allocated buffer
215 void *clalloc(void *pointer
, size_t size
);
218 * Same as memcpy, but XORs src into dst instead of copy
220 void memxor(u_int8_t dest
[], u_int8_t src
[], size_t n
);
223 * Creates a directory and all required parent directories.
225 * @param path path to the new directory
226 * @param mode permissions of the new directory/directories
227 * @return TRUE on success
229 bool mkdir_p(const char *path
, mode_t mode
);
237 * No-Operation function
242 * Special type to count references
244 typedef volatile u_int refcount_t
;
247 * Get a new reference.
249 * Increments the reference counter atomic.
251 * @param ref pointer to ref counter
253 void ref_get(refcount_t
*ref
);
256 * Put back a unused reference.
258 * Decrements the reference counter atomic and
259 * says if more references available.
261 * @param ref pointer to ref counter
262 * @return TRUE if no more references counted
264 bool ref_put(refcount_t
*ref
);
267 * Get printf hooks for time.
271 * Arguments using #-specificer
272 * time_t* time, bool utc
274 printf_hook_functions_t
time_get_printf_hooks();
277 * Get printf hooks for time deltas.
281 * Arguments using #-specificer
282 * time_t* begin, time_t* end
284 printf_hook_functions_t
time_delta_get_printf_hooks();
287 * Get printf hooks for time deltas.
290 * u_char *ptr, int len
292 printf_hook_functions_t
mem_get_printf_hooks();
294 #endif /* UTILS_H_ @}*/