]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
766141bc | 2 | * Copyright (C) 2008-2014 Tobias Brunner |
552cc11b MW |
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. | |
552cc11b MW |
15 | */ |
16 | ||
17 | /** | |
bca34c37 TB |
18 | * @defgroup utils_i utils |
19 | * @{ @ingroup utils | |
552cc11b MW |
20 | */ |
21 | ||
22 | #ifndef UTILS_H_ | |
23 | #define UTILS_H_ | |
24 | ||
25 | #include <sys/types.h> | |
26 | #include <stdlib.h> | |
27 | #include <stddef.h> | |
3f310c0d | 28 | #include <sys/time.h> |
0be12e35 | 29 | #include <string.h> |
552cc11b | 30 | |
922ee2c5 | 31 | #ifdef WIN32 |
89f19ef8 | 32 | # include "compat/windows.h" |
922ee2c5 MW |
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> | |
266ee0a1 | 39 | # include <sched.h> |
fc829ec9 | 40 | # include <poll.h> |
922ee2c5 MW |
41 | #endif |
42 | ||
eab241fb AS |
43 | /** |
44 | * strongSwan program return codes | |
45 | */ | |
46 | #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64 | |
47 | #define SS_RC_DAEMON_INTEGRITY 65 | |
3646c8a1 | 48 | #define SS_RC_INITIALIZATION_FAILED 66 |
eab241fb | 49 | |
5d8306de AS |
50 | #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY |
51 | #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED | |
52 | ||
552cc11b MW |
53 | /** |
54 | * Number of bits in a byte | |
55 | */ | |
56 | #define BITS_PER_BYTE 8 | |
57 | ||
58 | /** | |
59 | * Default length for various auxiliary text buffers | |
60 | */ | |
61 | #define BUF_LEN 512 | |
62 | ||
bb6be261 MW |
63 | /** |
64 | * Build assertion macro for integer expressions, evaluates to 0 | |
65 | */ | |
66 | #define BUILD_ASSERT(x) (sizeof(char[(x) ? 0 : -1])) | |
67 | ||
68 | /** | |
69 | * Build time check to assert a is an array, evaluates to 0 | |
70 | * | |
71 | * The address of an array element has a pointer type, which is not compatible | |
72 | * to the array type. | |
73 | */ | |
74 | #define BUILD_ASSERT_ARRAY(a) \ | |
75 | BUILD_ASSERT(!__builtin_types_compatible_p(typeof(a), typeof(&(a)[0]))) | |
76 | ||
552cc11b | 77 | /** |
a2a28d90 | 78 | * General purpose boolean type. |
552cc11b | 79 | */ |
a2a28d90 TB |
80 | #ifdef HAVE_STDBOOL_H |
81 | # include <stdbool.h> | |
82 | #else | |
83 | # ifndef HAVE__BOOL | |
84 | # define _Bool signed char | |
85 | # endif /* HAVE__BOOL */ | |
86 | # define bool _Bool | |
87 | # define false 0 | |
88 | # define true 1 | |
89 | # define __bool_true_false_are_defined 1 | |
90 | #endif /* HAVE_STDBOOL_H */ | |
91 | #ifndef FALSE | |
92 | # define FALSE false | |
93 | #endif /* FALSE */ | |
94 | #ifndef TRUE | |
95 | # define TRUE true | |
96 | #endif /* TRUE */ | |
97 | ||
9ee8b3b4 | 98 | #include "enum.h" |
717313c5 | 99 | #include "utils/atomics.h" |
9ee8b3b4 | 100 | #include "utils/strerror.h" |
f65779dd MW |
101 | #ifdef __APPLE__ |
102 | # include "compat/apple.h" | |
103 | #endif | |
9ee8b3b4 | 104 | |
a43f1e56 MW |
105 | /** |
106 | * Directory separator character in paths on this platform | |
107 | */ | |
108 | #ifdef WIN32 | |
109 | # define DIRECTORY_SEPARATOR "\\" | |
110 | #else | |
111 | # define DIRECTORY_SEPARATOR "/" | |
112 | #endif | |
113 | ||
87a79e6a MW |
114 | /** |
115 | * Initialize utility functions | |
116 | */ | |
117 | void utils_init(); | |
118 | ||
119 | /** | |
120 | * Deinitialize utility functions | |
121 | */ | |
122 | void utils_deinit(); | |
123 | ||
a2a28d90 TB |
124 | /** |
125 | * Helper function that compares two strings for equality | |
126 | */ | |
127 | static inline bool streq(const char *x, const char *y) | |
128 | { | |
129 | return strcmp(x, y) == 0; | |
130 | } | |
552cc11b MW |
131 | |
132 | /** | |
985dcab1 | 133 | * Helper function that compares two strings for equality, length limited |
552cc11b | 134 | */ |
985dcab1 TB |
135 | static inline bool strneq(const char *x, const char *y, size_t len) |
136 | { | |
137 | return strncmp(x, y, len) == 0; | |
138 | } | |
552cc11b | 139 | |
f460facd TB |
140 | /** |
141 | * Helper function that checks if a string starts with a given prefix | |
142 | */ | |
143 | static inline bool strpfx(const char *x, const char *prefix) | |
144 | { | |
145 | return strneq(x, prefix, strlen(prefix)); | |
146 | } | |
147 | ||
63176bbc | 148 | /** |
a2a28d90 | 149 | * Helper function that compares two strings for equality ignoring case |
63176bbc | 150 | */ |
a2a28d90 TB |
151 | static inline bool strcaseeq(const char *x, const char *y) |
152 | { | |
153 | return strcasecmp(x, y) == 0; | |
154 | } | |
63176bbc | 155 | |
7a3e0a63 | 156 | /** |
985dcab1 | 157 | * Helper function that compares two strings for equality ignoring case, length limited |
7a3e0a63 | 158 | */ |
985dcab1 TB |
159 | static inline bool strncaseeq(const char *x, const char *y, size_t len) |
160 | { | |
161 | return strncasecmp(x, y, len) == 0; | |
162 | } | |
7a3e0a63 | 163 | |
32a145fd TB |
164 | /** |
165 | * Helper function that checks if a string starts with a given prefix | |
166 | */ | |
167 | static inline bool strcasepfx(const char *x, const char *prefix) | |
168 | { | |
169 | return strncaseeq(x, prefix, strlen(prefix)); | |
170 | } | |
171 | ||
1038d9fe MW |
172 | /** |
173 | * NULL-safe strdup variant | |
174 | */ | |
753ca22f TB |
175 | static inline char *strdupnull(const char *s) |
176 | { | |
177 | return s ? strdup(s) : NULL; | |
178 | } | |
1038d9fe | 179 | |
552cc11b | 180 | /** |
985dcab1 | 181 | * Helper function that compares two binary blobs for equality |
552cc11b | 182 | */ |
985dcab1 TB |
183 | static inline bool memeq(const void *x, const void *y, size_t len) |
184 | { | |
185 | return memcmp(x, y, len) == 0; | |
186 | } | |
552cc11b | 187 | |
b8339632 MW |
188 | /** |
189 | * Same as memeq(), but with a constant runtime, safe for cryptographic use. | |
190 | */ | |
191 | bool memeq_const(const void *x, const void *y, size_t len); | |
192 | ||
6d4654b9 TB |
193 | /** |
194 | * Calling memcpy() with NULL pointers, even with n == 0, results in undefined | |
195 | * behavior according to the C standard. This version is guaranteed to not | |
196 | * access the pointers if n is 0. | |
197 | */ | |
198 | static inline void *memcpy_noop(void *dst, const void *src, size_t n) | |
199 | { | |
200 | return n ? memcpy(dst, src, n) : dst; | |
201 | } | |
920d466f MW |
202 | #ifdef memcpy |
203 | # undef memcpy | |
204 | #endif | |
6d4654b9 TB |
205 | #define memcpy(d,s,n) memcpy_noop(d,s,n) |
206 | ||
207 | /** | |
208 | * Calling memmove() with NULL pointers, even with n == 0, results in undefined | |
209 | * behavior according to the C standard. This version is guaranteed to not | |
210 | * access the pointers if n is 0. | |
211 | */ | |
212 | static inline void *memmove_noop(void *dst, const void *src, size_t n) | |
213 | { | |
214 | return n ? memmove(dst, src, n) : dst; | |
215 | } | |
920d466f MW |
216 | #ifdef memmove |
217 | # undef memmove | |
218 | #endif | |
6d4654b9 TB |
219 | #define memmove(d,s,n) memmove_noop(d,s,n) |
220 | ||
221 | /** | |
222 | * Calling memset() with a NULL pointer, even with n == 0, results in undefined | |
223 | * behavior according to the C standard. This version is guaranteed to not | |
224 | * access the pointer if n is 0. | |
225 | */ | |
226 | static inline void *memset_noop(void *s, int c, size_t n) | |
227 | { | |
228 | return n ? memset(s, c, n) : s; | |
229 | } | |
920d466f MW |
230 | #ifdef memset |
231 | # undef memset | |
232 | #endif | |
6d4654b9 TB |
233 | #define memset(s,c,n) memset_noop(s,c,n) |
234 | ||
552cc11b MW |
235 | /** |
236 | * Macro gives back larger of two values. | |
237 | */ | |
e822fc57 TB |
238 | #define max(x,y) ({ \ |
239 | typeof(x) _x = (x); \ | |
240 | typeof(y) _y = (y); \ | |
241 | _x > _y ? _x : _y; }) | |
242 | ||
552cc11b MW |
243 | /** |
244 | * Macro gives back smaller of two values. | |
245 | */ | |
e822fc57 TB |
246 | #define min(x,y) ({ \ |
247 | typeof(x) _x = (x); \ | |
248 | typeof(y) _y = (y); \ | |
249 | _x < _y ? _x : _y; }) | |
552cc11b MW |
250 | |
251 | /** | |
252 | * Call destructor of an object, if object != NULL | |
253 | */ | |
254 | #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj) | |
255 | ||
256 | /** | |
257 | * Call offset destructor of an object, if object != NULL | |
258 | */ | |
259 | #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset); | |
260 | ||
261 | /** | |
262 | * Call function destructor of an object, if object != NULL | |
263 | */ | |
264 | #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn); | |
265 | ||
266 | /** | |
267 | * Debug macro to follow control flow | |
268 | */ | |
269 | #define POS printf("%s, line %d\n", __FILE__, __LINE__) | |
270 | ||
74eed73a MW |
271 | /** |
272 | * Object allocation/initialization macro, using designated initializer. | |
273 | */ | |
2e1f4a46 MW |
274 | #define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \ |
275 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; } | |
74eed73a | 276 | |
0c832258 MW |
277 | /** |
278 | * Aligning version of INIT(). | |
279 | * | |
280 | * The returned pointer must be freed using free_align(), not free(). | |
281 | * | |
282 | * @param this object to allocate/initialize | |
283 | * @param align alignment for allocation, in bytes | |
284 | * @param ... initializer | |
285 | */ | |
286 | #define INIT_ALIGN(this, align, ...) { \ | |
287 | (this) = malloc_align(sizeof(*(this)), align); \ | |
288 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; } | |
289 | ||
1ef6c0ef MW |
290 | /** |
291 | * Object allocation/initialization macro, with extra allocated bytes at tail. | |
292 | * | |
293 | * The extra space gets zero-initialized. | |
294 | * | |
295 | * @param this pointer to object to allocate memory for | |
296 | * @param extra number of bytes to allocate at end of this | |
297 | * @param ... initializer | |
298 | */ | |
299 | #define INIT_EXTRA(this, extra, ...) { \ | |
300 | typeof(extra) _extra = (extra); \ | |
301 | (this) = malloc(sizeof(*(this)) + _extra); \ | |
302 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ | |
303 | memset((this) + 1, 0, _extra); } | |
304 | ||
0c832258 MW |
305 | /** |
306 | * Aligning version of INIT_EXTRA(). | |
307 | * | |
308 | * The returned pointer must be freed using free_align(), not free(). | |
309 | * | |
310 | * @param this object to allocate/initialize | |
311 | * @param extra number of bytes to allocate at end of this | |
312 | * @param align alignment for allocation, in bytes | |
313 | * @param ... initializer | |
314 | */ | |
315 | #define INIT_EXTRA_ALIGN(this, extra, align, ...) { \ | |
316 | typeof(extra) _extra = (extra); \ | |
317 | (this) = malloc_align(sizeof(*(this)) + _extra, align); \ | |
318 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ | |
319 | memset((this) + 1, 0, _extra); } | |
320 | ||
1a1ff9d1 MW |
321 | /** |
322 | * Method declaration/definition macro, providing private and public interface. | |
323 | * | |
324 | * Defines a method name with this as first parameter and a return value ret, | |
325 | * and an alias for this method with a _ prefix, having the this argument | |
326 | * safely casted to the public interface iface. | |
327 | * _name is provided a function pointer, but will get optimized out by GCC. | |
328 | */ | |
329 | #define METHOD(iface, name, ret, this, ...) \ | |
23d2bf84 MW |
330 | static ret name(union {iface *_public; this;} \ |
331 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
d185b6ac | 332 | static typeof(name) *_##name = (typeof(name)*)name; \ |
23d2bf84 MW |
333 | static ret name(this, ##__VA_ARGS__) |
334 | ||
335 | /** | |
336 | * Same as METHOD(), but is defined for two public interfaces. | |
337 | */ | |
338 | #define METHOD2(iface1, iface2, name, ret, this, ...) \ | |
339 | static ret name(union {iface1 *_public1; iface2 *_public2; this;} \ | |
340 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
d185b6ac | 341 | static typeof(name) *_##name = (typeof(name)*)name; \ |
1a1ff9d1 MW |
342 | static ret name(this, ##__VA_ARGS__) |
343 | ||
9e932513 MW |
344 | /** |
345 | * Callback declaration/definition macro, allowing casted first parameter. | |
346 | * | |
347 | * This is very similar to METHOD, but instead of casting the first parameter | |
348 | * to a public interface, it uses a void*. This allows type safe definition | |
349 | * of a callback function, while using the real type for the first parameter. | |
350 | */ | |
351 | #define CALLBACK(name, ret, param1, ...) \ | |
352 | static ret _cb_##name(union {void *_generic; param1;} \ | |
353 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
354 | static typeof(_cb_##name) *name = (typeof(_cb_##name)*)_cb_##name; \ | |
355 | static ret _cb_##name(param1, ##__VA_ARGS__) | |
356 | ||
e79dbda3 TB |
357 | /** |
358 | * This macro allows counting the number of arguments passed to a macro. | |
359 | * Combined with the VA_ARGS_DISPATCH() macro this can be used to implement | |
360 | * macro overloading based on the number of arguments. | |
361 | * 0 to 10 arguments are currently supported. | |
362 | */ | |
363 | #define VA_ARGS_NUM(...) _VA_ARGS_NUM(0,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0) | |
364 | #define _VA_ARGS_NUM(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NUM,...) NUM | |
365 | ||
366 | /** | |
367 | * This macro can be used to dispatch a macro call based on the number of given | |
368 | * arguments, for instance: | |
369 | * | |
370 | * @code | |
371 | * #define MY_MACRO(...) VA_ARGS_DISPATCH(MY_MACRO, __VA_ARGS__)(__VA_ARGS__) | |
372 | * #define MY_MACRO1(arg) one_arg(arg) | |
373 | * #define MY_MACRO2(arg1,arg2) two_args(arg1,arg2) | |
374 | * @endcode | |
375 | * | |
376 | * MY_MACRO() can now be called with either one or two arguments, which will | |
377 | * resolve to one_arg(arg) or two_args(arg1,arg2), respectively. | |
378 | */ | |
379 | #define VA_ARGS_DISPATCH(func, ...) _VA_ARGS_DISPATCH(func, VA_ARGS_NUM(__VA_ARGS__)) | |
380 | #define _VA_ARGS_DISPATCH(func, num) __VA_ARGS_DISPATCH(func, num) | |
381 | #define __VA_ARGS_DISPATCH(func, num) func ## num | |
382 | ||
7ba89ccd MW |
383 | /** |
384 | * Architecture independent bitfield definition helpers (at least with GCC). | |
385 | * | |
386 | * Defines a bitfield with a type t and a fixed size of bitfield members, e.g.: | |
387 | * BITFIELD2(u_int8_t, | |
388 | * low: 4, | |
389 | * high: 4, | |
390 | * ) flags; | |
391 | * The member defined first placed at bit 0. | |
392 | */ | |
393 | #if BYTE_ORDER == LITTLE_ENDIAN | |
394 | #define BITFIELD2(t, a, b,...) struct { t a; t b; __VA_ARGS__} | |
395 | #define BITFIELD3(t, a, b, c,...) struct { t a; t b; t c; __VA_ARGS__} | |
396 | #define BITFIELD4(t, a, b, c, d,...) struct { t a; t b; t c; t d; __VA_ARGS__} | |
397 | #define BITFIELD5(t, a, b, c, d, e,...) struct { t a; t b; t c; t d; t e; __VA_ARGS__} | |
398 | #elif BYTE_ORDER == BIG_ENDIAN | |
399 | #define BITFIELD2(t, a, b,...) struct { t b; t a; __VA_ARGS__} | |
400 | #define BITFIELD3(t, a, b, c,...) struct { t c; t b; t a; __VA_ARGS__} | |
401 | #define BITFIELD4(t, a, b, c, d,...) struct { t d; t c; t b; t a; __VA_ARGS__} | |
402 | #define BITFIELD5(t, a, b, c, d, e,...) struct { t e; t d; t c; t b; t a; __VA_ARGS__} | |
403 | #endif | |
404 | ||
552cc11b MW |
405 | /** |
406 | * Macro to allocate a sized type. | |
407 | */ | |
408 | #define malloc_thing(thing) ((thing*)malloc(sizeof(thing))) | |
409 | ||
fca4d3ee MW |
410 | /** |
411 | * Get the number of elements in an array | |
412 | */ | |
11bf11c1 MW |
413 | #define countof(array) (sizeof(array)/sizeof((array)[0]) \ |
414 | + BUILD_ASSERT_ARRAY(array)) | |
fca4d3ee | 415 | |
479f2950 MW |
416 | /** |
417 | * Ignore result of functions tagged with warn_unused_result attributes | |
418 | */ | |
d0230850 | 419 | #define ignore_result(call) { if(call){}; } |
479f2950 | 420 | |
552cc11b MW |
421 | /** |
422 | * Assign a function as a class method | |
423 | */ | |
424 | #define ASSIGN(method, function) (method = (typeof(method))function) | |
425 | ||
426 | /** | |
427 | * time_t not defined | |
428 | */ | |
429 | #define UNDEFINED_TIME 0 | |
430 | ||
9f0327e6 AS |
431 | /** |
432 | * Maximum time since epoch causing wrap-around on Jan 19 03:14:07 UTC 2038 | |
433 | */ | |
434 | #define TIME_32_BIT_SIGNED_MAX 0x7fffffff | |
435 | ||
cc396286 TB |
436 | /** |
437 | * define some missing fixed width int types on OpenSolaris. | |
438 | * TODO: since the uintXX_t types are defined by the C99 standard we should | |
439 | * probably use those anyway | |
440 | */ | |
922ee2c5 | 441 | #if defined __sun || defined WIN32 |
cc396286 TB |
442 | #include <stdint.h> |
443 | typedef uint8_t u_int8_t; | |
444 | typedef uint16_t u_int16_t; | |
445 | typedef uint32_t u_int32_t; | |
446 | typedef uint64_t u_int64_t; | |
447 | #endif | |
448 | ||
051064ac MW |
449 | #ifdef HAVE_INT128 |
450 | /** | |
451 | * 128 bit wide signed integer, if supported | |
452 | */ | |
453 | typedef __int128 int128_t; | |
454 | /** | |
455 | * 128 bit wide unsigned integer, if supported | |
456 | */ | |
457 | typedef unsigned __int128 u_int128_t; | |
3ba3be97 MW |
458 | |
459 | # define MAX_INT_TYPE int128_t | |
460 | # define MAX_UINT_TYPE u_int128_t | |
461 | #else | |
462 | # define MAX_INT_TYPE int64_t | |
463 | # define MAX_UINT_TYPE u_int64_t | |
051064ac MW |
464 | #endif |
465 | ||
552cc11b MW |
466 | typedef enum status_t status_t; |
467 | ||
468 | /** | |
469 | * Return values of function calls. | |
470 | */ | |
471 | enum status_t { | |
472 | /** | |
473 | * Call succeeded. | |
474 | */ | |
475 | SUCCESS, | |
7daf5226 | 476 | |
552cc11b MW |
477 | /** |
478 | * Call failed. | |
479 | */ | |
480 | FAILED, | |
7daf5226 | 481 | |
552cc11b MW |
482 | /** |
483 | * Out of resources. | |
484 | */ | |
485 | OUT_OF_RES, | |
7daf5226 | 486 | |
552cc11b MW |
487 | /** |
488 | * The suggested operation is already done | |
489 | */ | |
490 | ALREADY_DONE, | |
7daf5226 | 491 | |
552cc11b MW |
492 | /** |
493 | * Not supported. | |
494 | */ | |
495 | NOT_SUPPORTED, | |
7daf5226 | 496 | |
552cc11b MW |
497 | /** |
498 | * One of the arguments is invalid. | |
499 | */ | |
500 | INVALID_ARG, | |
7daf5226 | 501 | |
552cc11b MW |
502 | /** |
503 | * Something could not be found. | |
504 | */ | |
505 | NOT_FOUND, | |
7daf5226 | 506 | |
552cc11b MW |
507 | /** |
508 | * Error while parsing. | |
509 | */ | |
510 | PARSE_ERROR, | |
7daf5226 | 511 | |
552cc11b MW |
512 | /** |
513 | * Error while verifying. | |
514 | */ | |
515 | VERIFY_ERROR, | |
7daf5226 | 516 | |
552cc11b MW |
517 | /** |
518 | * Object in invalid state. | |
519 | */ | |
520 | INVALID_STATE, | |
7daf5226 | 521 | |
552cc11b MW |
522 | /** |
523 | * Destroy object which called method belongs to. | |
524 | */ | |
525 | DESTROY_ME, | |
7daf5226 | 526 | |
552cc11b MW |
527 | /** |
528 | * Another call to the method is required. | |
529 | */ | |
530 | NEED_MORE, | |
531 | }; | |
532 | ||
533 | /** | |
534 | * enum_names for type status_t. | |
535 | */ | |
536 | extern enum_name_t *status_names; | |
537 | ||
4d174272 MW |
538 | typedef enum tty_escape_t tty_escape_t; |
539 | ||
540 | /** | |
541 | * Excape codes for tty colors | |
542 | */ | |
543 | enum tty_escape_t { | |
544 | /** text properties */ | |
545 | TTY_RESET, | |
546 | TTY_BOLD, | |
547 | TTY_UNDERLINE, | |
548 | TTY_BLINKING, | |
549 | ||
550 | /** foreground colors */ | |
551 | TTY_FG_BLACK, | |
552 | TTY_FG_RED, | |
553 | TTY_FG_GREEN, | |
554 | TTY_FG_YELLOW, | |
555 | TTY_FG_BLUE, | |
556 | TTY_FG_MAGENTA, | |
557 | TTY_FG_CYAN, | |
558 | TTY_FG_WHITE, | |
559 | TTY_FG_DEF, | |
560 | ||
561 | /** background colors */ | |
562 | TTY_BG_BLACK, | |
563 | TTY_BG_RED, | |
564 | TTY_BG_GREEN, | |
565 | TTY_BG_YELLOW, | |
566 | TTY_BG_BLUE, | |
567 | TTY_BG_MAGENTA, | |
568 | TTY_BG_CYAN, | |
569 | TTY_BG_WHITE, | |
570 | TTY_BG_DEF, | |
571 | }; | |
572 | ||
573 | /** | |
574 | * Get the escape string for a given TTY color, empty string on non-tty fd | |
575 | */ | |
576 | char* tty_escape_get(int fd, tty_escape_t escape); | |
577 | ||
552cc11b MW |
578 | /** |
579 | * deprecated pluto style return value: | |
580 | * error message, NULL for success | |
581 | */ | |
582 | typedef const char *err_t; | |
583 | ||
584 | /** | |
585 | * Handle struct timeval like an own type. | |
586 | */ | |
587 | typedef struct timeval timeval_t; | |
588 | ||
589 | /** | |
590 | * Handle struct timespec like an own type. | |
591 | */ | |
592 | typedef struct timespec timespec_t; | |
593 | ||
594 | /** | |
595 | * Handle struct chunk_t like an own type. | |
596 | */ | |
597 | typedef struct sockaddr sockaddr_t; | |
598 | ||
f206bb74 MW |
599 | /** |
600 | * malloc(), but returns aligned memory. | |
601 | * | |
602 | * The returned pointer must be freed using free_align(), not free(). | |
603 | * | |
604 | * @param size size of allocated data | |
605 | * @param align alignment, up to 255 bytes, usually a power of 2 | |
606 | * @return allocated hunk, aligned to align bytes | |
607 | */ | |
608 | void* malloc_align(size_t size, u_int8_t align); | |
609 | ||
610 | /** | |
611 | * Free a hunk allocated by malloc_align(). | |
612 | * | |
613 | * @param ptr hunk to free | |
614 | */ | |
615 | void free_align(void *ptr); | |
616 | ||
552cc11b MW |
617 | /** |
618 | * Same as memcpy, but XORs src into dst instead of copy | |
619 | */ | |
620 | void memxor(u_int8_t dest[], u_int8_t src[], size_t n); | |
621 | ||
ed678b52 MW |
622 | /** |
623 | * Safely overwrite n bytes of memory at ptr with zero, non-inlining variant. | |
624 | */ | |
625 | void memwipe_noinline(void *ptr, size_t n); | |
626 | ||
627 | /** | |
628 | * Safely overwrite n bytes of memory at ptr with zero, inlining variant. | |
629 | */ | |
630 | static inline void memwipe_inline(void *ptr, size_t n) | |
631 | { | |
632 | volatile char *c = (volatile char*)ptr; | |
d45b242b | 633 | size_t m, i; |
ed678b52 MW |
634 | |
635 | /* byte wise until long aligned */ | |
d45b242b | 636 | for (i = 0; (uintptr_t)&c[i] % sizeof(long) && i < n; i++) |
ed678b52 MW |
637 | { |
638 | c[i] = 0; | |
639 | } | |
d45b242b MW |
640 | /* word wise */ |
641 | if (n >= sizeof(long)) | |
ed678b52 | 642 | { |
d45b242b MW |
643 | for (m = n - sizeof(long); i <= m; i += sizeof(long)) |
644 | { | |
645 | *(volatile long*)&c[i] = 0; | |
646 | } | |
ed678b52 MW |
647 | } |
648 | /* byte wise of the rest */ | |
649 | for (; i < n; i++) | |
650 | { | |
651 | c[i] = 0; | |
652 | } | |
653 | } | |
654 | ||
655 | /** | |
656 | * Safely overwrite n bytes of memory at ptr with zero, auto-inlining variant. | |
657 | */ | |
658 | static inline void memwipe(void *ptr, size_t n) | |
659 | { | |
c480b5f4 TB |
660 | if (!ptr) |
661 | { | |
662 | return; | |
663 | } | |
ed678b52 MW |
664 | if (__builtin_constant_p(n)) |
665 | { | |
666 | memwipe_inline(ptr, n); | |
667 | } | |
668 | else | |
669 | { | |
670 | memwipe_noinline(ptr, n); | |
671 | } | |
672 | } | |
673 | ||
81736d7d TB |
674 | /** |
675 | * A variant of strstr with the characteristics of memchr, where haystack is not | |
676 | * a null-terminated string but simply a memory area of length n. | |
677 | */ | |
678 | void *memstr(const void *haystack, const char *needle, size_t n); | |
679 | ||
2ed241ae TB |
680 | /** |
681 | * Replacement for memrchr(3) if it is not provided by the C library. | |
682 | * | |
683 | * @param s start of the memory area to search | |
684 | * @param c character to search | |
685 | * @param n length of memory area to search | |
686 | * @return pointer to the found character or NULL | |
687 | */ | |
688 | void *utils_memrchr(const void *s, int c, size_t n); | |
689 | ||
690 | #ifndef HAVE_MEMRCHR | |
691 | #define memrchr(s,c,n) utils_memrchr(s,c,n) | |
692 | #endif | |
693 | ||
d543d9ca TB |
694 | /** |
695 | * Translates the characters in the given string, searching for characters | |
696 | * in 'from' and mapping them to characters in 'to'. | |
697 | * The two characters sets 'from' and 'to' must contain the same number of | |
698 | * characters. | |
699 | */ | |
700 | char *translate(char *str, const char *from, const char *to); | |
701 | ||
ccb6758e | 702 | /** |
4ab38d98 | 703 | * Replaces all occurrences of search in the given string with replace. |
ccb6758e TB |
704 | * |
705 | * Allocates memory only if anything is replaced in the string. The original | |
706 | * string is also returned if any of the arguments are invalid (e.g. if search | |
707 | * is empty or any of them are NULL). | |
708 | * | |
709 | * @param str original string | |
710 | * @param search string to search for and replace | |
4ab38d98 | 711 | * @param replace string to replace found occurrences with |
ccb6758e TB |
712 | * @return allocated string, if anything got replaced, str otherwise |
713 | */ | |
714 | char *strreplace(const char *str, const char *search, const char *replace); | |
715 | ||
66c0801d MW |
716 | /** |
717 | * Portable function to wait for SIGINT/SIGTERM (or equivalent). | |
718 | */ | |
719 | void wait_sigint(); | |
720 | ||
766141bc TB |
721 | /** |
722 | * Like dirname(3) returns the directory part of the given null-terminated | |
723 | * pathname, up to but not including the final '/' (or '.' if no '/' is found). | |
724 | * Trailing '/' are not counted as part of the pathname. | |
725 | * | |
726 | * The difference is that it does this in a thread-safe manner (i.e. it does not | |
727 | * use static buffers) and does not modify the original path. | |
728 | * | |
729 | * @param path original pathname | |
730 | * @return allocated directory component | |
731 | */ | |
732 | char *path_dirname(const char *path); | |
733 | ||
734 | /** | |
735 | * Like basename(3) returns the filename part of the given null-terminated path, | |
736 | * i.e. the part following the final '/' (or '.' if path is empty or NULL). | |
737 | * Trailing '/' are not counted as part of the pathname. | |
738 | * | |
739 | * The difference is that it does this in a thread-safe manner (i.e. it does not | |
740 | * use static buffers) and does not modify the original path. | |
741 | * | |
742 | * @param path original pathname | |
743 | * @return allocated filename component | |
744 | */ | |
745 | char *path_basename(const char *path); | |
746 | ||
67b3bcd1 MW |
747 | /** |
748 | * Check if a given path is absolute. | |
749 | * | |
750 | * @param path path to check | |
751 | * @return TRUE if absolute, FALSE if relative | |
752 | */ | |
753 | bool path_absolute(const char *path); | |
754 | ||
6c20579a | 755 | /** |
7daf5226 | 756 | * Creates a directory and all required parent directories. |
6c20579a | 757 | * |
3f310c0d | 758 | * @param path path to the new directory |
7daf5226 | 759 | * @param mode permissions of the new directory/directories |
6c20579a TB |
760 | * @return TRUE on success |
761 | */ | |
762 | bool mkdir_p(const char *path, mode_t mode); | |
763 | ||
9a8fdc15 TB |
764 | #ifndef HAVE_CLOSEFROM |
765 | /** | |
766 | * Close open file descriptors greater than or equal to lowfd. | |
767 | * | |
4ab38d98 | 768 | * @param lowfd start closing file descriptors from here |
9a8fdc15 TB |
769 | */ |
770 | void closefrom(int lowfd); | |
771 | #endif | |
772 | ||
3f310c0d MW |
773 | /** |
774 | * Get a timestamp from a monotonic time source. | |
775 | * | |
776 | * While the time()/gettimeofday() functions are affected by leap seconds | |
777 | * and system time changes, this function returns ever increasing monotonic | |
778 | * time stamps. | |
779 | * | |
780 | * @param tv timeval struct receiving monotonic timestamps, or NULL | |
781 | * @return monotonic timestamp in seconds | |
782 | */ | |
783 | time_t time_monotonic(timeval_t *tv); | |
784 | ||
eecd41e3 TB |
785 | /** |
786 | * Add the given number of milliseconds to the given timeval struct | |
787 | * | |
788 | * @param tv timeval struct to modify | |
789 | * @param ms number of milliseconds | |
790 | */ | |
791 | static inline void timeval_add_ms(timeval_t *tv, u_int ms) | |
792 | { | |
793 | tv->tv_usec += ms * 1000; | |
819c02db | 794 | while (tv->tv_usec >= 1000000 /* 1s */) |
eecd41e3 TB |
795 | { |
796 | tv->tv_usec -= 1000000; | |
797 | tv->tv_sec++; | |
798 | } | |
799 | } | |
800 | ||
081ae2eb MW |
801 | /** |
802 | * returns null | |
803 | */ | |
804 | void *return_null(); | |
805 | ||
233b853d MW |
806 | /** |
807 | * No-Operation function | |
808 | */ | |
809 | void nop(); | |
810 | ||
da17b016 MW |
811 | /** |
812 | * returns TRUE | |
813 | */ | |
814 | bool return_true(); | |
815 | ||
816 | /** | |
817 | * returns FALSE | |
818 | */ | |
819 | bool return_false(); | |
820 | ||
502edf42 MW |
821 | /** |
822 | * returns FAILED | |
823 | */ | |
824 | status_t return_failed(); | |
825 | ||
4755ab50 MW |
826 | /** |
827 | * returns SUCCESS | |
828 | */ | |
829 | status_t return_success(); | |
830 | ||
0be12e35 MW |
831 | /** |
832 | * Write a 16-bit host order value in network order to an unaligned address. | |
833 | * | |
834 | * @param host host order 16-bit value | |
835 | * @param network unaligned address to write network order value to | |
836 | */ | |
837 | static inline void htoun16(void *network, u_int16_t host) | |
838 | { | |
dbee988e MW |
839 | char *unaligned = (char*)network; |
840 | ||
0be12e35 | 841 | host = htons(host); |
dbee988e | 842 | memcpy(unaligned, &host, sizeof(host)); |
0be12e35 MW |
843 | } |
844 | ||
845 | /** | |
846 | * Write a 32-bit host order value in network order to an unaligned address. | |
847 | * | |
848 | * @param host host order 32-bit value | |
849 | * @param network unaligned address to write network order value to | |
850 | */ | |
851 | static inline void htoun32(void *network, u_int32_t host) | |
852 | { | |
dbee988e MW |
853 | char *unaligned = (char*)network; |
854 | ||
0be12e35 | 855 | host = htonl(host); |
dbee988e | 856 | memcpy((char*)unaligned, &host, sizeof(host)); |
0be12e35 MW |
857 | } |
858 | ||
fbeb9454 AS |
859 | /** |
860 | * Write a 64-bit host order value in network order to an unaligned address. | |
861 | * | |
a91462df | 862 | * @param host host order 64-bit value |
fbeb9454 AS |
863 | * @param network unaligned address to write network order value to |
864 | */ | |
865 | static inline void htoun64(void *network, u_int64_t host) | |
866 | { | |
867 | char *unaligned = (char*)network; | |
f4e25e60 MW |
868 | |
869 | #ifdef be64toh | |
870 | host = htobe64(host); | |
871 | memcpy((char*)unaligned, &host, sizeof(host)); | |
872 | #else | |
fbeb9454 AS |
873 | u_int32_t high_part, low_part; |
874 | ||
875 | high_part = host >> 32; | |
876 | high_part = htonl(high_part); | |
877 | low_part = host & 0xFFFFFFFFLL; | |
878 | low_part = htonl(low_part); | |
879 | ||
880 | memcpy(unaligned, &high_part, sizeof(high_part)); | |
881 | unaligned += sizeof(high_part); | |
882 | memcpy(unaligned, &low_part, sizeof(low_part)); | |
f4e25e60 | 883 | #endif |
fbeb9454 AS |
884 | } |
885 | ||
0be12e35 MW |
886 | /** |
887 | * Read a 16-bit value in network order from an unaligned address to host order. | |
888 | * | |
889 | * @param network unaligned address to read network order value from | |
890 | * @return host order value | |
891 | */ | |
892 | static inline u_int16_t untoh16(void *network) | |
893 | { | |
dbee988e | 894 | char *unaligned = (char*)network; |
0be12e35 MW |
895 | u_int16_t tmp; |
896 | ||
dbee988e | 897 | memcpy(&tmp, unaligned, sizeof(tmp)); |
0be12e35 MW |
898 | return ntohs(tmp); |
899 | } | |
900 | ||
901 | /** | |
902 | * Read a 32-bit value in network order from an unaligned address to host order. | |
903 | * | |
904 | * @param network unaligned address to read network order value from | |
905 | * @return host order value | |
906 | */ | |
907 | static inline u_int32_t untoh32(void *network) | |
908 | { | |
dbee988e | 909 | char *unaligned = (char*)network; |
0be12e35 MW |
910 | u_int32_t tmp; |
911 | ||
dbee988e | 912 | memcpy(&tmp, unaligned, sizeof(tmp)); |
f8f4f31a | 913 | return ntohl(tmp); |
0be12e35 MW |
914 | } |
915 | ||
fbeb9454 AS |
916 | /** |
917 | * Read a 64-bit value in network order from an unaligned address to host order. | |
918 | * | |
919 | * @param network unaligned address to read network order value from | |
920 | * @return host order value | |
921 | */ | |
922 | static inline u_int64_t untoh64(void *network) | |
923 | { | |
924 | char *unaligned = (char*)network; | |
f4e25e60 MW |
925 | |
926 | #ifdef be64toh | |
927 | u_int64_t tmp; | |
928 | ||
929 | memcpy(&tmp, unaligned, sizeof(tmp)); | |
930 | return be64toh(tmp); | |
931 | #else | |
fbeb9454 AS |
932 | u_int32_t high_part, low_part; |
933 | ||
934 | memcpy(&high_part, unaligned, sizeof(high_part)); | |
935 | unaligned += sizeof(high_part); | |
936 | memcpy(&low_part, unaligned, sizeof(low_part)); | |
937 | ||
938 | high_part = ntohl(high_part); | |
939 | low_part = ntohl(low_part); | |
940 | ||
941 | return (((u_int64_t)high_part) << 32) + low_part; | |
f4e25e60 | 942 | #endif |
fbeb9454 AS |
943 | } |
944 | ||
84044f9c | 945 | /** |
812ae898 | 946 | * Get the padding required to make size a multiple of alignment |
84044f9c | 947 | */ |
812ae898 | 948 | static inline size_t pad_len(size_t size, size_t alignment) |
84044f9c | 949 | { |
812ae898 | 950 | size_t remainder; |
84044f9c | 951 | |
812ae898 TB |
952 | remainder = size % alignment; |
953 | return remainder ? alignment - remainder : 0; | |
954 | } | |
955 | ||
956 | /** | |
957 | * Round up size to be multiple of alignment | |
958 | */ | |
959 | static inline size_t round_up(size_t size, size_t alignment) | |
960 | { | |
961 | return size + pad_len(size, alignment); | |
84044f9c MW |
962 | } |
963 | ||
964 | /** | |
812ae898 | 965 | * Round down size to be a multiple of alignment |
84044f9c | 966 | */ |
812ae898 | 967 | static inline size_t round_down(size_t size, size_t alignment) |
84044f9c | 968 | { |
812ae898 | 969 | return size - (size % alignment); |
84044f9c MW |
970 | } |
971 | ||
2077d996 MW |
972 | #ifndef HAVE_FMEMOPEN |
973 | # ifdef HAVE_FUNOPEN | |
9df621d2 MW |
974 | # define HAVE_FMEMOPEN |
975 | # define HAVE_FMEMOPEN_FALLBACK | |
5ac29360 | 976 | # include <stdio.h> |
2077d996 MW |
977 | /** |
978 | * fmemopen(3) fallback using BSD funopen. | |
979 | * | |
980 | * We could also provide one using fopencookie(), but should we have it we | |
981 | * most likely have fmemopen(). | |
982 | * | |
983 | * fseek() is currently not supported. | |
984 | */ | |
985 | FILE *fmemopen(void *buf, size_t size, const char *mode); | |
986 | # endif /* FUNOPEN */ | |
987 | #endif /* FMEMOPEN */ | |
988 | ||
552cc11b | 989 | /** |
d25ce370 | 990 | * printf hook for time_t. |
552cc11b | 991 | * |
7daf5226 | 992 | * Arguments are: |
323f9f99 | 993 | * time_t* time, bool utc |
552cc11b | 994 | */ |
1b40b74d | 995 | int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 996 | const void *const *args); |
552cc11b MW |
997 | |
998 | /** | |
d25ce370 | 999 | * printf hook for time_t deltas. |
552cc11b | 1000 | * |
7daf5226 | 1001 | * Arguments are: |
323f9f99 | 1002 | * time_t* begin, time_t* end |
552cc11b | 1003 | */ |
1b40b74d | 1004 | int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 1005 | const void *const *args); |
552cc11b MW |
1006 | |
1007 | /** | |
d25ce370 | 1008 | * printf hook for memory areas. |
552cc11b | 1009 | * |
7daf5226 | 1010 | * Arguments are: |
817ab8a8 | 1011 | * u_char *ptr, u_int len |
552cc11b | 1012 | */ |
1b40b74d | 1013 | int mem_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 1014 | const void *const *args); |
552cc11b | 1015 | |
1490ff4d | 1016 | #endif /** UTILS_H_ @}*/ |