]>
git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/utils/utils.h
2 * Copyright (C) 2008-2014 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_i utils
25 #include <sys/types.h>
32 # include "compat/windows.h"
35 # include <arpa/inet.h>
36 # include <sys/socket.h>
38 # include <netinet/in.h>
43 #include "utils/types.h"
45 #include "utils/atomics.h"
46 #include "utils/align.h"
47 #include "utils/byteorder.h"
48 #include "utils/string.h"
49 #include "utils/memory.h"
50 #include "utils/strerror.h"
51 #include "utils/status.h"
52 #include "utils/object.h"
53 #include "utils/path.h"
54 #include "utils/time.h"
55 #include "utils/tty.h"
57 # include "compat/apple.h"
61 * Initialize utility functions
66 * Deinitialize utility functions
71 * strongSwan program return codes
73 #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64
74 #define SS_RC_DAEMON_INTEGRITY 65
75 #define SS_RC_INITIALIZATION_FAILED 66
77 #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY
78 #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED
81 * Number of bits in a byte
83 #define BITS_PER_BYTE 8
86 * Default length for various auxiliary text buffers
91 * Build assertion macro for integer expressions, evaluates to 0
93 #define BUILD_ASSERT(x) (sizeof(char[(x) ? 0 : -1]))
96 * Build time check to assert a is an array, evaluates to 0
98 * The address of an array element has a pointer type, which is not compatible
101 #define BUILD_ASSERT_ARRAY(a) \
102 BUILD_ASSERT(!__builtin_types_compatible_p(typeof(a), typeof(&(a)[0])))
105 * Debug macro to follow control flow
107 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
110 * This macro allows counting the number of arguments passed to a macro.
111 * Combined with the VA_ARGS_DISPATCH() macro this can be used to implement
112 * macro overloading based on the number of arguments.
113 * 0 to 10 arguments are currently supported.
115 #define VA_ARGS_NUM(...) _VA_ARGS_NUM(0,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)
116 #define _VA_ARGS_NUM(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NUM,...) NUM
119 * This macro can be used to dispatch a macro call based on the number of given
120 * arguments, for instance:
123 * #define MY_MACRO(...) VA_ARGS_DISPATCH(MY_MACRO, __VA_ARGS__)(__VA_ARGS__)
124 * #define MY_MACRO1(arg) one_arg(arg)
125 * #define MY_MACRO2(arg1,arg2) two_args(arg1,arg2)
128 * MY_MACRO() can now be called with either one or two arguments, which will
129 * resolve to one_arg(arg) or two_args(arg1,arg2), respectively.
131 #define VA_ARGS_DISPATCH(func, ...) _VA_ARGS_DISPATCH(func, VA_ARGS_NUM(__VA_ARGS__))
132 #define _VA_ARGS_DISPATCH(func, num) __VA_ARGS_DISPATCH(func, num)
133 #define __VA_ARGS_DISPATCH(func, num) func ## num
136 * Macro to allocate a sized type.
138 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
141 * Get the number of elements in an array
143 #define countof(array) (sizeof(array)/sizeof((array)[0]) \
144 + BUILD_ASSERT_ARRAY(array))
147 * Ignore result of functions tagged with warn_unused_result attributes
149 #define ignore_result(call) { if(call){}; }
152 * Portable function to wait for SIGINT/SIGTERM (or equivalent).
156 #ifndef HAVE_CLOSEFROM
158 * Close open file descriptors greater than or equal to lowfd.
160 * @param lowfd start closing file descriptors from here
162 void closefrom(int lowfd
);
171 * No-Operation function
185 #endif /** UTILS_H_ @}*/