a334954aedc9eba143867654bc2d25f5f1b60b04
2 * Copyright (C) 2008-2010 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
18 * @defgroup utils utils
19 * @{ @ingroup libstrongswan
25 #include <sys/types.h>
29 #include <arpa/inet.h>
35 * strongSwan program return codes
37 #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64
38 #define SS_RC_DAEMON_INTEGRITY 65
39 #define SS_RC_INITIALIZATION_FAILED 66
41 #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY
42 #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED
45 * Number of bits in a byte
47 #define BITS_PER_BYTE 8
50 * Default length for various auxiliary text buffers
55 * Macro compares two strings for equality
57 #define streq(x,y) (strcmp(x, y) == 0)
60 * Macro compares two strings for equality, length limited
62 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
65 * Macro compares two strings for equality ignoring case
67 #define strcaseeq(x,y) (strcasecmp(x, y) == 0)
70 * Macro compares two strings for equality ignoring case, length limited
72 #define strncaseeq(x,y,len) (strncasecmp(x, y, len) == 0)
75 * NULL-safe strdup variant
77 #define strdupnull(x) ({ char *_x = x; _x ? strdup(_x) : NULL; })
80 * Macro compares two binary blobs for equality
82 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
85 * Macro gives back larger of two values.
94 * Macro gives back smaller of two values.
102 * Call destructor of an object, if object != NULL
104 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
107 * Call offset destructor of an object, if object != NULL
109 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
112 * Call function destructor of an object, if object != NULL
114 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
117 * Debug macro to follow control flow
119 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
122 * Object allocation/initialization macro, using designated initializer.
124 #define INIT(this, ...) { (this) = malloc(sizeof(*this)); \
125 *(this) = (typeof(*this)){ __VA_ARGS__ }; }
128 * Method declaration/definition macro, providing private and public interface.
130 * Defines a method name with this as first parameter and a return value ret,
131 * and an alias for this method with a _ prefix, having the this argument
132 * safely casted to the public interface iface.
133 * _name is provided a function pointer, but will get optimized out by GCC.
135 #define METHOD(iface, name, ret, this, ...) \
136 static ret name(union {iface *_public; this;} \
137 __attribute__((transparent_union)), ##__VA_ARGS__); \
138 static const typeof(name) *_##name = (const typeof(name)*)name; \
139 static ret name(this, ##__VA_ARGS__)
142 * Same as METHOD(), but is defined for two public interfaces.
144 #define METHOD2(iface1, iface2, name, ret, this, ...) \
145 static ret name(union {iface1 *_public1; iface2 *_public2; this;} \
146 __attribute__((transparent_union)), ##__VA_ARGS__); \
147 static const typeof(name) *_##name = (const typeof(name)*)name; \
148 static ret name(this, ##__VA_ARGS__)
151 * Architecture independent bitfield definition helpers (at least with GCC).
153 * Defines a bitfield with a type t and a fixed size of bitfield members, e.g.:
154 * BITFIELD2(u_int8_t,
158 * The member defined first placed at bit 0.
160 #if BYTE_ORDER == LITTLE_ENDIAN
161 #define BITFIELD2(t, a, b,...) struct { t a; t b; __VA_ARGS__}
162 #define BITFIELD3(t, a, b, c,...) struct { t a; t b; t c; __VA_ARGS__}
163 #define BITFIELD4(t, a, b, c, d,...) struct { t a; t b; t c; t d; __VA_ARGS__}
164 #define BITFIELD5(t, a, b, c, d, e,...) struct { t a; t b; t c; t d; t e; __VA_ARGS__}
165 #elif BYTE_ORDER == BIG_ENDIAN
166 #define BITFIELD2(t, a, b,...) struct { t b; t a; __VA_ARGS__}
167 #define BITFIELD3(t, a, b, c,...) struct { t c; t b; t a; __VA_ARGS__}
168 #define BITFIELD4(t, a, b, c, d,...) struct { t d; t c; t b; t a; __VA_ARGS__}
169 #define BITFIELD5(t, a, b, c, d, e,...) struct { t e; t d; t c; t b; t a; __VA_ARGS__}
173 * Macro to allocate a sized type.
175 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
178 * Get the number of elements in an array
180 #define countof(array) (sizeof(array)/sizeof(array[0]))
183 * Ignore result of functions tagged with warn_unused_result attributes
185 #define ignore_result(call) { if(call){}; }
188 * Assign a function as a class method
190 #define ASSIGN(method, function) (method = (typeof(method))function)
195 #define UNDEFINED_TIME 0
198 * Maximum time since epoch causing wrap-around on Jan 19 03:14:07 UTC 2038
200 #define TIME_32_BIT_SIGNED_MAX 0x7fffffff
203 * General purpose boolean type.
205 #ifdef HAVE_STDBOOL_H
206 # include <stdbool.h>
209 # define _Bool signed char
210 # endif /* HAVE__BOOL */
214 # define __bool_true_false_are_defined 1
215 #endif /* HAVE_STDBOOL_H */
224 * define some missing fixed width int types on OpenSolaris.
225 * TODO: since the uintXX_t types are defined by the C99 standard we should
226 * probably use those anyway
230 typedef uint8_t u_int8_t
;
231 typedef uint16_t u_int16_t
;
232 typedef uint32_t u_int32_t
;
233 typedef uint64_t u_int64_t
;
236 typedef enum status_t status_t
;
239 * Return values of function calls.
258 * The suggested operation is already done
268 * One of the arguments is invalid.
273 * Something could not be found.
278 * Error while parsing.
283 * Error while verifying.
288 * Object in invalid state.
293 * Destroy object which called method belongs to.
298 * Another call to the method is required.
304 * enum_names for type status_t.
306 extern enum_name_t
*status_names
;
309 * deprecated pluto style return value:
310 * error message, NULL for success
312 typedef const char *err_t
;
315 * Handle struct timeval like an own type.
317 typedef struct timeval timeval_t
;
320 * Handle struct timespec like an own type.
322 typedef struct timespec timespec_t
;
325 * Handle struct chunk_t like an own type.
327 typedef struct sockaddr sockaddr_t
;
330 * Clone a data to a newly allocated buffer
332 void *clalloc(void *pointer
, size_t size
);
335 * Same as memcpy, but XORs src into dst instead of copy
337 void memxor(u_int8_t dest
[], u_int8_t src
[], size_t n
);
340 * Safely overwrite n bytes of memory at ptr with zero, non-inlining variant.
342 void memwipe_noinline(void *ptr
, size_t n
);
345 * Safely overwrite n bytes of memory at ptr with zero, inlining variant.
347 static inline void memwipe_inline(void *ptr
, size_t n
)
349 volatile char *c
= (volatile char*)ptr
;
352 /* byte wise until long aligned */
353 for (i
= 0; (uintptr_t)&c
% sizeof(long) && i
< n
; i
++)
358 for (m
= n
- sizeof(long); i
<= m
; i
+= sizeof(long))
360 *(volatile long*)&c
[i
] = 0;
362 /* byte wise of the rest */
370 * Safely overwrite n bytes of memory at ptr with zero, auto-inlining variant.
372 static inline void memwipe(void *ptr
, size_t n
)
374 if (__builtin_constant_p(n
))
376 memwipe_inline(ptr
, n
);
380 memwipe_noinline(ptr
, n
);
385 * A variant of strstr with the characteristics of memchr, where haystack is not
386 * a null-terminated string but simply a memory area of length n.
388 void *memstr(const void *haystack
, const char *needle
, size_t n
);
391 * Translates the characters in the given string, searching for characters
392 * in 'from' and mapping them to characters in 'to'.
393 * The two characters sets 'from' and 'to' must contain the same number of
396 char *translate(char *str
, const char *from
, const char *to
);
399 * Creates a directory and all required parent directories.
401 * @param path path to the new directory
402 * @param mode permissions of the new directory/directories
403 * @return TRUE on success
405 bool mkdir_p(const char *path
, mode_t mode
);
408 * Get a timestamp from a monotonic time source.
410 * While the time()/gettimeofday() functions are affected by leap seconds
411 * and system time changes, this function returns ever increasing monotonic
414 * @param tv timeval struct receiving monotonic timestamps, or NULL
415 * @return monotonic timestamp in seconds
417 time_t time_monotonic(timeval_t
*tv
);
425 * No-Operation function
442 status_t
return_failed();
445 * Write a 16-bit host order value in network order to an unaligned address.
447 * @param host host order 16-bit value
448 * @param network unaligned address to write network order value to
450 static inline void htoun16(void *network
, u_int16_t host
)
452 char *unaligned
= (char*)network
;
455 memcpy(unaligned
, &host
, sizeof(host
));
459 * Write a 32-bit host order value in network order to an unaligned address.
461 * @param host host order 32-bit value
462 * @param network unaligned address to write network order value to
464 static inline void htoun32(void *network
, u_int32_t host
)
466 char *unaligned
= (char*)network
;
469 memcpy((char*)unaligned
, &host
, sizeof(host
));
473 * Read a 16-bit value in network order from an unaligned address to host order.
475 * @param network unaligned address to read network order value from
476 * @return host order value
478 static inline u_int16_t
untoh16(void *network
)
480 char *unaligned
= (char*)network
;
483 memcpy(&tmp
, unaligned
, sizeof(tmp
));
488 * Read a 32-bit value in network order from an unaligned address to host order.
490 * @param network unaligned address to read network order value from
491 * @return host order value
493 static inline u_int32_t
untoh32(void *network
)
495 char *unaligned
= (char*)network
;
498 memcpy(&tmp
, unaligned
, sizeof(tmp
));
503 * Special type to count references
505 typedef volatile u_int refcount_t
;
508 #ifdef HAVE_GCC_ATOMIC_OPERATIONS
510 #define ref_get(ref) {__sync_fetch_and_add(ref, 1); }
511 #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1))
513 #else /* !HAVE_GCC_ATOMIC_OPERATIONS */
516 * Get a new reference.
518 * Increments the reference counter atomic.
520 * @param ref pointer to ref counter
522 void ref_get(refcount_t
*ref
);
525 * Put back a unused reference.
527 * Decrements the reference counter atomic and
528 * says if more references available.
530 * @param ref pointer to ref counter
531 * @return TRUE if no more references counted
533 bool ref_put(refcount_t
*ref
);
535 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
538 * printf hook for time_t.
541 * time_t* time, bool utc
543 int time_printf_hook(char *dst
, size_t len
, printf_hook_spec_t
*spec
,
544 const void *const *args
);
547 * printf hook for time_t deltas.
550 * time_t* begin, time_t* end
552 int time_delta_printf_hook(char *dst
, size_t len
, printf_hook_spec_t
*spec
,
553 const void *const *args
);
556 * printf hook for memory areas.
559 * u_char *ptr, int len
561 int mem_printf_hook(char *dst
, size_t len
, printf_hook_spec_t
*spec
,
562 const void *const *args
);
564 #endif /** UTILS_H_ @}*/