android: Replace AndroidConfigLocal.h with a header in utils/compat
[strongswan.git] / src / libstrongswan / utils / utils.h
1 /*
2 * Copyright (C) 2008-2014 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 /**
18 * @defgroup utils_i utils
19 * @{ @ingroup utils
20 */
21
22 #ifndef UTILS_H_
23 #define UTILS_H_
24
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <stddef.h>
28 #include <sys/time.h>
29 #include <string.h>
30
31 #ifdef WIN32
32 # include "compat/windows.h"
33 #else
34 # define _GNU_SOURCE
35 # include <arpa/inet.h>
36 # include <sys/socket.h>
37 # include <netdb.h>
38 # include <netinet/in.h>
39 # include <sched.h>
40 # include <poll.h>
41 #endif
42
43 #include "utils/types.h"
44 #include "enum.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"
56 #ifdef __APPLE__
57 # include "compat/apple.h"
58 #endif
59 #ifdef __ANDROID__
60 # include "compat/android.h"
61 #endif
62
63 /**
64 * Initialize utility functions
65 */
66 void utils_init();
67
68 /**
69 * Deinitialize utility functions
70 */
71 void utils_deinit();
72
73 /**
74 * strongSwan program return codes
75 */
76 #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64
77 #define SS_RC_DAEMON_INTEGRITY 65
78 #define SS_RC_INITIALIZATION_FAILED 66
79
80 #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY
81 #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED
82
83 /**
84 * Number of bits in a byte
85 */
86 #define BITS_PER_BYTE 8
87
88 /**
89 * Default length for various auxiliary text buffers
90 */
91 #define BUF_LEN 512
92
93 /**
94 * Build assertion macro for integer expressions, evaluates to 0
95 */
96 #define BUILD_ASSERT(x) (sizeof(char[(x) ? 0 : -1]))
97
98 /**
99 * Build time check to assert a is an array, evaluates to 0
100 *
101 * The address of an array element has a pointer type, which is not compatible
102 * to the array type.
103 */
104 #define BUILD_ASSERT_ARRAY(a) \
105 BUILD_ASSERT(!__builtin_types_compatible_p(typeof(a), typeof(&(a)[0])))
106
107 /**
108 * Debug macro to follow control flow
109 */
110 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
111
112 /**
113 * This macro allows counting the number of arguments passed to a macro.
114 * Combined with the VA_ARGS_DISPATCH() macro this can be used to implement
115 * macro overloading based on the number of arguments.
116 * 0 to 10 arguments are currently supported.
117 */
118 #define VA_ARGS_NUM(...) _VA_ARGS_NUM(0,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)
119 #define _VA_ARGS_NUM(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NUM,...) NUM
120
121 /**
122 * This macro can be used to dispatch a macro call based on the number of given
123 * arguments, for instance:
124 *
125 * @code
126 * #define MY_MACRO(...) VA_ARGS_DISPATCH(MY_MACRO, __VA_ARGS__)(__VA_ARGS__)
127 * #define MY_MACRO1(arg) one_arg(arg)
128 * #define MY_MACRO2(arg1,arg2) two_args(arg1,arg2)
129 * @endcode
130 *
131 * MY_MACRO() can now be called with either one or two arguments, which will
132 * resolve to one_arg(arg) or two_args(arg1,arg2), respectively.
133 */
134 #define VA_ARGS_DISPATCH(func, ...) _VA_ARGS_DISPATCH(func, VA_ARGS_NUM(__VA_ARGS__))
135 #define _VA_ARGS_DISPATCH(func, num) __VA_ARGS_DISPATCH(func, num)
136 #define __VA_ARGS_DISPATCH(func, num) func ## num
137
138 /**
139 * Macro to allocate a sized type.
140 */
141 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
142
143 /**
144 * Get the number of elements in an array
145 */
146 #define countof(array) (sizeof(array)/sizeof((array)[0]) \
147 + BUILD_ASSERT_ARRAY(array))
148
149 /**
150 * Ignore result of functions tagged with warn_unused_result attributes
151 */
152 #define ignore_result(call) { if(call){}; }
153
154 /**
155 * Portable function to wait for SIGINT/SIGTERM (or equivalent).
156 */
157 void wait_sigint();
158
159 #ifndef HAVE_CLOSEFROM
160 /**
161 * Close open file descriptors greater than or equal to lowfd.
162 *
163 * @param lowfd start closing file descriptors from here
164 */
165 void closefrom(int lowfd);
166 #endif
167
168 /**
169 * returns null
170 */
171 void *return_null();
172
173 /**
174 * No-Operation function
175 */
176 void nop();
177
178 /**
179 * returns TRUE
180 */
181 bool return_true();
182
183 /**
184 * returns FALSE
185 */
186 bool return_false();
187
188 #endif /** UTILS_H_ @}*/