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