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