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