326047644230abe66cc432deae184f2867789d2f
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 * Entropy in bits of secret Diffie-Hellman exponents
46 #define DH_EXPONENT_ENTROPY_SIZE 512
49 * Macro compares two strings for equality
51 #define streq(x,y) (strcmp(x, y) == 0)
54 * Macro compares two strings for equality
56 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
59 * Macro compares two binary blobs for equality
61 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
64 * Macro gives back larger of two values.
66 #define max(x,y) ((x) > (y) ? (x):(y))
69 * Macro gives back smaller of two values.
71 #define min(x,y) ((x) < (y) ? (x):(y))
74 * Call destructor of an object, if object != NULL
76 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
79 * Call offset destructor of an object, if object != NULL
81 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
84 * Call function destructor of an object, if object != NULL
86 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
89 * Debug macro to follow control flow
91 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
94 * Macro to allocate a sized type.
96 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
99 * Get the number of elements in an array
101 #define countof(array) (sizeof(array)/sizeof(array[0]))
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 * Get a new reference.
254 * Increments the reference counter atomic.
256 * @param ref pointer to ref counter
258 void ref_get(refcount_t
*ref
);
261 * Put back a unused reference.
263 * Decrements the reference counter atomic and
264 * says if more references available.
266 * @param ref pointer to ref counter
267 * @return TRUE if no more references counted
269 bool ref_put(refcount_t
*ref
);
272 * Get printf hooks for time.
276 * Arguments using #-specificer
277 * time_t* time, bool utc
279 printf_hook_functions_t
time_get_printf_hooks();
282 * Get printf hooks for time deltas.
286 * Arguments using #-specificer
287 * time_t* begin, time_t* end
289 printf_hook_functions_t
time_delta_get_printf_hooks();
292 * Get printf hooks for time deltas.
295 * u_char *ptr, int len
297 printf_hook_functions_t
mem_get_printf_hooks();
299 #endif /* UTILS_H_ @}*/