use SS_RC_FIRST and SS_RC_LAST
[strongswan.git] / src / libstrongswan / utils.h
1 /*
2 * Copyright (C) 2008-2009 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 utils
19 * @{ @ingroup libstrongswan
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
29 #include <enum.h>
30
31 /**
32 * strongSwan program return codes
33 */
34 #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64
35 #define SS_RC_DAEMON_INTEGRITY 65
36 #define SS_RC_INITIALIZATION_FAILED 66
37
38 #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY
39 #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED
40
41 /**
42 * Number of bits in a byte
43 */
44 #define BITS_PER_BYTE 8
45
46 /**
47 * Default length for various auxiliary text buffers
48 */
49 #define BUF_LEN 512
50
51 /**
52 * Macro compares two strings for equality
53 */
54 #define streq(x,y) (strcmp(x, y) == 0)
55
56 /**
57 * Macro compares two strings for equality
58 */
59 #define strneq(x,y,len) (strncmp(x, y, len) == 0)
60
61 /**
62 * Macro compares two strings for equality ignoring case
63 */
64 #define strcaseeq(x,y) (strcasecmp(x, y) == 0)
65
66 /**
67 * Macro compares two binary blobs for equality
68 */
69 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
70
71 /**
72 * Macro gives back larger of two values.
73 */
74 #define max(x,y) ((x) > (y) ? (x):(y))
75
76 /**
77 * Macro gives back smaller of two values.
78 */
79 #define min(x,y) ((x) < (y) ? (x):(y))
80
81 /**
82 * Call destructor of an object, if object != NULL
83 */
84 #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj)
85
86 /**
87 * Call offset destructor of an object, if object != NULL
88 */
89 #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset);
90
91 /**
92 * Call function destructor of an object, if object != NULL
93 */
94 #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn);
95
96 /**
97 * Debug macro to follow control flow
98 */
99 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
100
101 /**
102 * Macro to allocate a sized type.
103 */
104 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
105
106 /**
107 * Get the number of elements in an array
108 */
109 #define countof(array) (sizeof(array)/sizeof(array[0]))
110
111 /**
112 * Ignore result of functions tagged with warn_unused_result attributes
113 */
114 #define ignore_result(call) { if(call); }
115
116 /**
117 * Assign a function as a class method
118 */
119 #define ASSIGN(method, function) (method = (typeof(method))function)
120
121 /**
122 * time_t not defined
123 */
124 #define UNDEFINED_TIME 0
125
126 /**
127 * General purpose boolean type.
128 */
129 #ifdef HAVE_STDBOOL_H
130 # include <stdbool.h>
131 #else
132 # ifndef HAVE__BOOL
133 # define _Bool signed char
134 # endif /* HAVE__BOOL */
135 # define bool _Bool
136 # define false 0
137 # define true 1
138 # define __bool_true_false_are_defined 1
139 #endif /* HAVE_STDBOOL_H */
140 #ifndef FALSE
141 # define FALSE false
142 #endif /* FALSE */
143 #ifndef TRUE
144 # define TRUE true
145 #endif /* TRUE */
146
147 typedef enum status_t status_t;
148
149 /**
150 * Return values of function calls.
151 */
152 enum status_t {
153 /**
154 * Call succeeded.
155 */
156 SUCCESS,
157
158 /**
159 * Call failed.
160 */
161 FAILED,
162
163 /**
164 * Out of resources.
165 */
166 OUT_OF_RES,
167
168 /**
169 * The suggested operation is already done
170 */
171 ALREADY_DONE,
172
173 /**
174 * Not supported.
175 */
176 NOT_SUPPORTED,
177
178 /**
179 * One of the arguments is invalid.
180 */
181 INVALID_ARG,
182
183 /**
184 * Something could not be found.
185 */
186 NOT_FOUND,
187
188 /**
189 * Error while parsing.
190 */
191 PARSE_ERROR,
192
193 /**
194 * Error while verifying.
195 */
196 VERIFY_ERROR,
197
198 /**
199 * Object in invalid state.
200 */
201 INVALID_STATE,
202
203 /**
204 * Destroy object which called method belongs to.
205 */
206 DESTROY_ME,
207
208 /**
209 * Another call to the method is required.
210 */
211 NEED_MORE,
212 };
213
214 /**
215 * enum_names for type status_t.
216 */
217 extern enum_name_t *status_names;
218
219 /**
220 * deprecated pluto style return value:
221 * error message, NULL for success
222 */
223 typedef const char *err_t;
224
225 /**
226 * Handle struct timeval like an own type.
227 */
228 typedef struct timeval timeval_t;
229
230 /**
231 * Handle struct timespec like an own type.
232 */
233 typedef struct timespec timespec_t;
234
235 /**
236 * Handle struct chunk_t like an own type.
237 */
238 typedef struct sockaddr sockaddr_t;
239
240 /**
241 * Clone a data to a newly allocated buffer
242 */
243 void *clalloc(void *pointer, size_t size);
244
245 /**
246 * Same as memcpy, but XORs src into dst instead of copy
247 */
248 void memxor(u_int8_t dest[], u_int8_t src[], size_t n);
249
250 /**
251 * A variant of strstr with the characteristics of memchr, where haystack is not
252 * a null-terminated string but simply a memory area of length n.
253 */
254 void *memstr(const void *haystack, const char *needle, size_t n);
255
256 /**
257 * Creates a directory and all required parent directories.
258 *
259 * @param path path to the new directory
260 * @param mode permissions of the new directory/directories
261 * @return TRUE on success
262 */
263 bool mkdir_p(const char *path, mode_t mode);
264
265 /**
266 * returns null
267 */
268 void *return_null();
269
270 /**
271 * No-Operation function
272 */
273 void nop();
274
275 /**
276 * returns TRUE
277 */
278 bool return_true();
279
280 /**
281 * returns FALSE
282 */
283 bool return_false();
284
285 /**
286 * Special type to count references
287 */
288 typedef volatile u_int refcount_t;
289
290
291 #ifdef HAVE_GCC_ATOMIC_OPERATIONS
292
293 #define ref_get(ref) {__sync_fetch_and_add(ref, 1); }
294 #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1))
295
296 #else /* !HAVE_GCC_ATOMIC_OPERATIONS */
297
298 /**
299 * Get a new reference.
300 *
301 * Increments the reference counter atomic.
302 *
303 * @param ref pointer to ref counter
304 */
305 void ref_get(refcount_t *ref);
306
307 /**
308 * Put back a unused reference.
309 *
310 * Decrements the reference counter atomic and
311 * says if more references available.
312 *
313 * @param ref pointer to ref counter
314 * @return TRUE if no more references counted
315 */
316 bool ref_put(refcount_t *ref);
317
318 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
319
320 /**
321 * printf hook for time_t.
322 *
323 * Arguments are:
324 * time_t* time, bool utc
325 */
326 int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
327 const void *const *args);
328
329 /**
330 * printf hook for time_t deltas.
331 *
332 * Arguments are:
333 * time_t* begin, time_t* end
334 */
335 int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
336 const void *const *args);
337
338 /**
339 * printf hook for memory areas.
340 *
341 * Arguments are:
342 * u_char *ptr, int len
343 */
344 int mem_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
345 const void *const *args);
346
347 #endif /** UTILS_H_ @}*/