]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2008-2014 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 | #include <sys/types.h> | |
26 | #include <stdlib.h> | |
27 | #include <stddef.h> | |
28 | #include <sys/time.h> | |
29 | #include <string.h> | |
30 | ||
31 | #ifdef WIN32 | |
32 | # include "compat/windows.h" | |
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> | |
39 | # include <sched.h> | |
40 | # include <poll.h> | |
41 | #endif | |
42 | ||
43 | /** | |
44 | * strongSwan program return codes | |
45 | */ | |
46 | #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64 | |
47 | #define SS_RC_DAEMON_INTEGRITY 65 | |
48 | #define SS_RC_INITIALIZATION_FAILED 66 | |
49 | ||
50 | #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY | |
51 | #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED | |
52 | ||
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 | ||
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 | ||
77 | #include "utils/types.h" | |
78 | #include "enum.h" | |
79 | #include "utils/atomics.h" | |
80 | #include "utils/byteorder.h" | |
81 | #include "utils/string.h" | |
82 | #include "utils/strerror.h" | |
83 | #ifdef __APPLE__ | |
84 | # include "compat/apple.h" | |
85 | #endif | |
86 | ||
87 | /** | |
88 | * Directory separator character in paths on this platform | |
89 | */ | |
90 | #ifdef WIN32 | |
91 | # define DIRECTORY_SEPARATOR "\\" | |
92 | #else | |
93 | # define DIRECTORY_SEPARATOR "/" | |
94 | #endif | |
95 | ||
96 | /** | |
97 | * Initialize utility functions | |
98 | */ | |
99 | void utils_init(); | |
100 | ||
101 | /** | |
102 | * Deinitialize utility functions | |
103 | */ | |
104 | void utils_deinit(); | |
105 | ||
106 | /** | |
107 | * Helper function that compares two binary blobs for equality | |
108 | */ | |
109 | static inline bool memeq(const void *x, const void *y, size_t len) | |
110 | { | |
111 | return memcmp(x, y, len) == 0; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Same as memeq(), but with a constant runtime, safe for cryptographic use. | |
116 | */ | |
117 | bool memeq_const(const void *x, const void *y, size_t len); | |
118 | ||
119 | /** | |
120 | * Calling memcpy() with NULL pointers, even with n == 0, results in undefined | |
121 | * behavior according to the C standard. This version is guaranteed to not | |
122 | * access the pointers if n is 0. | |
123 | */ | |
124 | static inline void *memcpy_noop(void *dst, const void *src, size_t n) | |
125 | { | |
126 | return n ? memcpy(dst, src, n) : dst; | |
127 | } | |
128 | #ifdef memcpy | |
129 | # undef memcpy | |
130 | #endif | |
131 | #define memcpy(d,s,n) memcpy_noop(d,s,n) | |
132 | ||
133 | /** | |
134 | * Calling memmove() with NULL pointers, even with n == 0, results in undefined | |
135 | * behavior according to the C standard. This version is guaranteed to not | |
136 | * access the pointers if n is 0. | |
137 | */ | |
138 | static inline void *memmove_noop(void *dst, const void *src, size_t n) | |
139 | { | |
140 | return n ? memmove(dst, src, n) : dst; | |
141 | } | |
142 | #ifdef memmove | |
143 | # undef memmove | |
144 | #endif | |
145 | #define memmove(d,s,n) memmove_noop(d,s,n) | |
146 | ||
147 | /** | |
148 | * Calling memset() with a NULL pointer, even with n == 0, results in undefined | |
149 | * behavior according to the C standard. This version is guaranteed to not | |
150 | * access the pointer if n is 0. | |
151 | */ | |
152 | static inline void *memset_noop(void *s, int c, size_t n) | |
153 | { | |
154 | return n ? memset(s, c, n) : s; | |
155 | } | |
156 | #ifdef memset | |
157 | # undef memset | |
158 | #endif | |
159 | #define memset(s,c,n) memset_noop(s,c,n) | |
160 | ||
161 | /** | |
162 | * Macro gives back larger of two values. | |
163 | */ | |
164 | #define max(x,y) ({ \ | |
165 | typeof(x) _x = (x); \ | |
166 | typeof(y) _y = (y); \ | |
167 | _x > _y ? _x : _y; }) | |
168 | ||
169 | /** | |
170 | * Macro gives back smaller of two values. | |
171 | */ | |
172 | #define min(x,y) ({ \ | |
173 | typeof(x) _x = (x); \ | |
174 | typeof(y) _y = (y); \ | |
175 | _x < _y ? _x : _y; }) | |
176 | ||
177 | /** | |
178 | * Call destructor of an object, if object != NULL | |
179 | */ | |
180 | #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj) | |
181 | ||
182 | /** | |
183 | * Call offset destructor of an object, if object != NULL | |
184 | */ | |
185 | #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset); | |
186 | ||
187 | /** | |
188 | * Call function destructor of an object, if object != NULL | |
189 | */ | |
190 | #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn); | |
191 | ||
192 | /** | |
193 | * Debug macro to follow control flow | |
194 | */ | |
195 | #define POS printf("%s, line %d\n", __FILE__, __LINE__) | |
196 | ||
197 | /** | |
198 | * Object allocation/initialization macro, using designated initializer. | |
199 | */ | |
200 | #define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \ | |
201 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; } | |
202 | ||
203 | /** | |
204 | * Aligning version of INIT(). | |
205 | * | |
206 | * The returned pointer must be freed using free_align(), not free(). | |
207 | * | |
208 | * @param this object to allocate/initialize | |
209 | * @param align alignment for allocation, in bytes | |
210 | * @param ... initializer | |
211 | */ | |
212 | #define INIT_ALIGN(this, align, ...) { \ | |
213 | (this) = malloc_align(sizeof(*(this)), align); \ | |
214 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; } | |
215 | ||
216 | /** | |
217 | * Object allocation/initialization macro, with extra allocated bytes at tail. | |
218 | * | |
219 | * The extra space gets zero-initialized. | |
220 | * | |
221 | * @param this pointer to object to allocate memory for | |
222 | * @param extra number of bytes to allocate at end of this | |
223 | * @param ... initializer | |
224 | */ | |
225 | #define INIT_EXTRA(this, extra, ...) { \ | |
226 | typeof(extra) _extra = (extra); \ | |
227 | (this) = malloc(sizeof(*(this)) + _extra); \ | |
228 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ | |
229 | memset((this) + 1, 0, _extra); } | |
230 | ||
231 | /** | |
232 | * Aligning version of INIT_EXTRA(). | |
233 | * | |
234 | * The returned pointer must be freed using free_align(), not free(). | |
235 | * | |
236 | * @param this object to allocate/initialize | |
237 | * @param extra number of bytes to allocate at end of this | |
238 | * @param align alignment for allocation, in bytes | |
239 | * @param ... initializer | |
240 | */ | |
241 | #define INIT_EXTRA_ALIGN(this, extra, align, ...) { \ | |
242 | typeof(extra) _extra = (extra); \ | |
243 | (this) = malloc_align(sizeof(*(this)) + _extra, align); \ | |
244 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; \ | |
245 | memset((this) + 1, 0, _extra); } | |
246 | ||
247 | /** | |
248 | * Method declaration/definition macro, providing private and public interface. | |
249 | * | |
250 | * Defines a method name with this as first parameter and a return value ret, | |
251 | * and an alias for this method with a _ prefix, having the this argument | |
252 | * safely casted to the public interface iface. | |
253 | * _name is provided a function pointer, but will get optimized out by GCC. | |
254 | */ | |
255 | #define METHOD(iface, name, ret, this, ...) \ | |
256 | static ret name(union {iface *_public; this;} \ | |
257 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
258 | static typeof(name) *_##name = (typeof(name)*)name; \ | |
259 | static ret name(this, ##__VA_ARGS__) | |
260 | ||
261 | /** | |
262 | * Same as METHOD(), but is defined for two public interfaces. | |
263 | */ | |
264 | #define METHOD2(iface1, iface2, name, ret, this, ...) \ | |
265 | static ret name(union {iface1 *_public1; iface2 *_public2; this;} \ | |
266 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
267 | static typeof(name) *_##name = (typeof(name)*)name; \ | |
268 | static ret name(this, ##__VA_ARGS__) | |
269 | ||
270 | /** | |
271 | * Callback declaration/definition macro, allowing casted first parameter. | |
272 | * | |
273 | * This is very similar to METHOD, but instead of casting the first parameter | |
274 | * to a public interface, it uses a void*. This allows type safe definition | |
275 | * of a callback function, while using the real type for the first parameter. | |
276 | */ | |
277 | #define CALLBACK(name, ret, param1, ...) \ | |
278 | static ret _cb_##name(union {void *_generic; param1;} \ | |
279 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
280 | static typeof(_cb_##name) *name = (typeof(_cb_##name)*)_cb_##name; \ | |
281 | static ret _cb_##name(param1, ##__VA_ARGS__) | |
282 | ||
283 | /** | |
284 | * This macro allows counting the number of arguments passed to a macro. | |
285 | * Combined with the VA_ARGS_DISPATCH() macro this can be used to implement | |
286 | * macro overloading based on the number of arguments. | |
287 | * 0 to 10 arguments are currently supported. | |
288 | */ | |
289 | #define VA_ARGS_NUM(...) _VA_ARGS_NUM(0,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0) | |
290 | #define _VA_ARGS_NUM(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NUM,...) NUM | |
291 | ||
292 | /** | |
293 | * This macro can be used to dispatch a macro call based on the number of given | |
294 | * arguments, for instance: | |
295 | * | |
296 | * @code | |
297 | * #define MY_MACRO(...) VA_ARGS_DISPATCH(MY_MACRO, __VA_ARGS__)(__VA_ARGS__) | |
298 | * #define MY_MACRO1(arg) one_arg(arg) | |
299 | * #define MY_MACRO2(arg1,arg2) two_args(arg1,arg2) | |
300 | * @endcode | |
301 | * | |
302 | * MY_MACRO() can now be called with either one or two arguments, which will | |
303 | * resolve to one_arg(arg) or two_args(arg1,arg2), respectively. | |
304 | */ | |
305 | #define VA_ARGS_DISPATCH(func, ...) _VA_ARGS_DISPATCH(func, VA_ARGS_NUM(__VA_ARGS__)) | |
306 | #define _VA_ARGS_DISPATCH(func, num) __VA_ARGS_DISPATCH(func, num) | |
307 | #define __VA_ARGS_DISPATCH(func, num) func ## num | |
308 | ||
309 | /** | |
310 | * Macro to allocate a sized type. | |
311 | */ | |
312 | #define malloc_thing(thing) ((thing*)malloc(sizeof(thing))) | |
313 | ||
314 | /** | |
315 | * Get the number of elements in an array | |
316 | */ | |
317 | #define countof(array) (sizeof(array)/sizeof((array)[0]) \ | |
318 | + BUILD_ASSERT_ARRAY(array)) | |
319 | ||
320 | /** | |
321 | * Ignore result of functions tagged with warn_unused_result attributes | |
322 | */ | |
323 | #define ignore_result(call) { if(call){}; } | |
324 | ||
325 | /** | |
326 | * Assign a function as a class method | |
327 | */ | |
328 | #define ASSIGN(method, function) (method = (typeof(method))function) | |
329 | ||
330 | /** | |
331 | * time_t not defined | |
332 | */ | |
333 | #define UNDEFINED_TIME 0 | |
334 | ||
335 | /** | |
336 | * Maximum time since epoch causing wrap-around on Jan 19 03:14:07 UTC 2038 | |
337 | */ | |
338 | #define TIME_32_BIT_SIGNED_MAX 0x7fffffff | |
339 | ||
340 | typedef enum status_t status_t; | |
341 | ||
342 | /** | |
343 | * Return values of function calls. | |
344 | */ | |
345 | enum status_t { | |
346 | /** | |
347 | * Call succeeded. | |
348 | */ | |
349 | SUCCESS, | |
350 | ||
351 | /** | |
352 | * Call failed. | |
353 | */ | |
354 | FAILED, | |
355 | ||
356 | /** | |
357 | * Out of resources. | |
358 | */ | |
359 | OUT_OF_RES, | |
360 | ||
361 | /** | |
362 | * The suggested operation is already done | |
363 | */ | |
364 | ALREADY_DONE, | |
365 | ||
366 | /** | |
367 | * Not supported. | |
368 | */ | |
369 | NOT_SUPPORTED, | |
370 | ||
371 | /** | |
372 | * One of the arguments is invalid. | |
373 | */ | |
374 | INVALID_ARG, | |
375 | ||
376 | /** | |
377 | * Something could not be found. | |
378 | */ | |
379 | NOT_FOUND, | |
380 | ||
381 | /** | |
382 | * Error while parsing. | |
383 | */ | |
384 | PARSE_ERROR, | |
385 | ||
386 | /** | |
387 | * Error while verifying. | |
388 | */ | |
389 | VERIFY_ERROR, | |
390 | ||
391 | /** | |
392 | * Object in invalid state. | |
393 | */ | |
394 | INVALID_STATE, | |
395 | ||
396 | /** | |
397 | * Destroy object which called method belongs to. | |
398 | */ | |
399 | DESTROY_ME, | |
400 | ||
401 | /** | |
402 | * Another call to the method is required. | |
403 | */ | |
404 | NEED_MORE, | |
405 | }; | |
406 | ||
407 | /** | |
408 | * enum_names for type status_t. | |
409 | */ | |
410 | extern enum_name_t *status_names; | |
411 | ||
412 | typedef enum tty_escape_t tty_escape_t; | |
413 | ||
414 | /** | |
415 | * Excape codes for tty colors | |
416 | */ | |
417 | enum tty_escape_t { | |
418 | /** text properties */ | |
419 | TTY_RESET, | |
420 | TTY_BOLD, | |
421 | TTY_UNDERLINE, | |
422 | TTY_BLINKING, | |
423 | ||
424 | /** foreground colors */ | |
425 | TTY_FG_BLACK, | |
426 | TTY_FG_RED, | |
427 | TTY_FG_GREEN, | |
428 | TTY_FG_YELLOW, | |
429 | TTY_FG_BLUE, | |
430 | TTY_FG_MAGENTA, | |
431 | TTY_FG_CYAN, | |
432 | TTY_FG_WHITE, | |
433 | TTY_FG_DEF, | |
434 | ||
435 | /** background colors */ | |
436 | TTY_BG_BLACK, | |
437 | TTY_BG_RED, | |
438 | TTY_BG_GREEN, | |
439 | TTY_BG_YELLOW, | |
440 | TTY_BG_BLUE, | |
441 | TTY_BG_MAGENTA, | |
442 | TTY_BG_CYAN, | |
443 | TTY_BG_WHITE, | |
444 | TTY_BG_DEF, | |
445 | }; | |
446 | ||
447 | /** | |
448 | * Get the escape string for a given TTY color, empty string on non-tty fd | |
449 | */ | |
450 | char* tty_escape_get(int fd, tty_escape_t escape); | |
451 | ||
452 | /** | |
453 | * Handle struct timeval like an own type. | |
454 | */ | |
455 | typedef struct timeval timeval_t; | |
456 | ||
457 | /** | |
458 | * Handle struct timespec like an own type. | |
459 | */ | |
460 | typedef struct timespec timespec_t; | |
461 | ||
462 | /** | |
463 | * malloc(), but returns aligned memory. | |
464 | * | |
465 | * The returned pointer must be freed using free_align(), not free(). | |
466 | * | |
467 | * @param size size of allocated data | |
468 | * @param align alignment, up to 255 bytes, usually a power of 2 | |
469 | * @return allocated hunk, aligned to align bytes | |
470 | */ | |
471 | void* malloc_align(size_t size, u_int8_t align); | |
472 | ||
473 | /** | |
474 | * Free a hunk allocated by malloc_align(). | |
475 | * | |
476 | * @param ptr hunk to free | |
477 | */ | |
478 | void free_align(void *ptr); | |
479 | ||
480 | /** | |
481 | * Same as memcpy, but XORs src into dst instead of copy | |
482 | */ | |
483 | void memxor(u_int8_t dest[], u_int8_t src[], size_t n); | |
484 | ||
485 | /** | |
486 | * Safely overwrite n bytes of memory at ptr with zero, non-inlining variant. | |
487 | */ | |
488 | void memwipe_noinline(void *ptr, size_t n); | |
489 | ||
490 | /** | |
491 | * Safely overwrite n bytes of memory at ptr with zero, inlining variant. | |
492 | */ | |
493 | static inline void memwipe_inline(void *ptr, size_t n) | |
494 | { | |
495 | volatile char *c = (volatile char*)ptr; | |
496 | size_t m, i; | |
497 | ||
498 | /* byte wise until long aligned */ | |
499 | for (i = 0; (uintptr_t)&c[i] % sizeof(long) && i < n; i++) | |
500 | { | |
501 | c[i] = 0; | |
502 | } | |
503 | /* word wise */ | |
504 | if (n >= sizeof(long)) | |
505 | { | |
506 | for (m = n - sizeof(long); i <= m; i += sizeof(long)) | |
507 | { | |
508 | *(volatile long*)&c[i] = 0; | |
509 | } | |
510 | } | |
511 | /* byte wise of the rest */ | |
512 | for (; i < n; i++) | |
513 | { | |
514 | c[i] = 0; | |
515 | } | |
516 | } | |
517 | ||
518 | /** | |
519 | * Safely overwrite n bytes of memory at ptr with zero, auto-inlining variant. | |
520 | */ | |
521 | static inline void memwipe(void *ptr, size_t n) | |
522 | { | |
523 | if (!ptr) | |
524 | { | |
525 | return; | |
526 | } | |
527 | if (__builtin_constant_p(n)) | |
528 | { | |
529 | memwipe_inline(ptr, n); | |
530 | } | |
531 | else | |
532 | { | |
533 | memwipe_noinline(ptr, n); | |
534 | } | |
535 | } | |
536 | ||
537 | /** | |
538 | * A variant of strstr with the characteristics of memchr, where haystack is not | |
539 | * a null-terminated string but simply a memory area of length n. | |
540 | */ | |
541 | void *memstr(const void *haystack, const char *needle, size_t n); | |
542 | ||
543 | /** | |
544 | * Replacement for memrchr(3) if it is not provided by the C library. | |
545 | * | |
546 | * @param s start of the memory area to search | |
547 | * @param c character to search | |
548 | * @param n length of memory area to search | |
549 | * @return pointer to the found character or NULL | |
550 | */ | |
551 | void *utils_memrchr(const void *s, int c, size_t n); | |
552 | ||
553 | #ifndef HAVE_MEMRCHR | |
554 | #define memrchr(s,c,n) utils_memrchr(s,c,n) | |
555 | #endif | |
556 | ||
557 | /** | |
558 | * Portable function to wait for SIGINT/SIGTERM (or equivalent). | |
559 | */ | |
560 | void wait_sigint(); | |
561 | ||
562 | /** | |
563 | * Like dirname(3) returns the directory part of the given null-terminated | |
564 | * pathname, up to but not including the final '/' (or '.' if no '/' is found). | |
565 | * Trailing '/' are not counted as part of the pathname. | |
566 | * | |
567 | * The difference is that it does this in a thread-safe manner (i.e. it does not | |
568 | * use static buffers) and does not modify the original path. | |
569 | * | |
570 | * @param path original pathname | |
571 | * @return allocated directory component | |
572 | */ | |
573 | char *path_dirname(const char *path); | |
574 | ||
575 | /** | |
576 | * Like basename(3) returns the filename part of the given null-terminated path, | |
577 | * i.e. the part following the final '/' (or '.' if path is empty or NULL). | |
578 | * Trailing '/' are not counted as part of the pathname. | |
579 | * | |
580 | * The difference is that it does this in a thread-safe manner (i.e. it does not | |
581 | * use static buffers) and does not modify the original path. | |
582 | * | |
583 | * @param path original pathname | |
584 | * @return allocated filename component | |
585 | */ | |
586 | char *path_basename(const char *path); | |
587 | ||
588 | /** | |
589 | * Check if a given path is absolute. | |
590 | * | |
591 | * @param path path to check | |
592 | * @return TRUE if absolute, FALSE if relative | |
593 | */ | |
594 | bool path_absolute(const char *path); | |
595 | ||
596 | /** | |
597 | * Creates a directory and all required parent directories. | |
598 | * | |
599 | * @param path path to the new directory | |
600 | * @param mode permissions of the new directory/directories | |
601 | * @return TRUE on success | |
602 | */ | |
603 | bool mkdir_p(const char *path, mode_t mode); | |
604 | ||
605 | #ifndef HAVE_CLOSEFROM | |
606 | /** | |
607 | * Close open file descriptors greater than or equal to lowfd. | |
608 | * | |
609 | * @param lowfd start closing file descriptors from here | |
610 | */ | |
611 | void closefrom(int lowfd); | |
612 | #endif | |
613 | ||
614 | /** | |
615 | * Get a timestamp from a monotonic time source. | |
616 | * | |
617 | * While the time()/gettimeofday() functions are affected by leap seconds | |
618 | * and system time changes, this function returns ever increasing monotonic | |
619 | * time stamps. | |
620 | * | |
621 | * @param tv timeval struct receiving monotonic timestamps, or NULL | |
622 | * @return monotonic timestamp in seconds | |
623 | */ | |
624 | time_t time_monotonic(timeval_t *tv); | |
625 | ||
626 | /** | |
627 | * Add the given number of milliseconds to the given timeval struct | |
628 | * | |
629 | * @param tv timeval struct to modify | |
630 | * @param ms number of milliseconds | |
631 | */ | |
632 | static inline void timeval_add_ms(timeval_t *tv, u_int ms) | |
633 | { | |
634 | tv->tv_usec += ms * 1000; | |
635 | while (tv->tv_usec >= 1000000 /* 1s */) | |
636 | { | |
637 | tv->tv_usec -= 1000000; | |
638 | tv->tv_sec++; | |
639 | } | |
640 | } | |
641 | ||
642 | /** | |
643 | * returns null | |
644 | */ | |
645 | void *return_null(); | |
646 | ||
647 | /** | |
648 | * No-Operation function | |
649 | */ | |
650 | void nop(); | |
651 | ||
652 | /** | |
653 | * returns TRUE | |
654 | */ | |
655 | bool return_true(); | |
656 | ||
657 | /** | |
658 | * returns FALSE | |
659 | */ | |
660 | bool return_false(); | |
661 | ||
662 | /** | |
663 | * returns FAILED | |
664 | */ | |
665 | status_t return_failed(); | |
666 | ||
667 | /** | |
668 | * returns SUCCESS | |
669 | */ | |
670 | status_t return_success(); | |
671 | ||
672 | /** | |
673 | * Get the padding required to make size a multiple of alignment | |
674 | */ | |
675 | static inline size_t pad_len(size_t size, size_t alignment) | |
676 | { | |
677 | size_t remainder; | |
678 | ||
679 | remainder = size % alignment; | |
680 | return remainder ? alignment - remainder : 0; | |
681 | } | |
682 | ||
683 | /** | |
684 | * Round up size to be multiple of alignment | |
685 | */ | |
686 | static inline size_t round_up(size_t size, size_t alignment) | |
687 | { | |
688 | return size + pad_len(size, alignment); | |
689 | } | |
690 | ||
691 | /** | |
692 | * Round down size to be a multiple of alignment | |
693 | */ | |
694 | static inline size_t round_down(size_t size, size_t alignment) | |
695 | { | |
696 | return size - (size % alignment); | |
697 | } | |
698 | ||
699 | #ifndef HAVE_FMEMOPEN | |
700 | # ifdef HAVE_FUNOPEN | |
701 | # define HAVE_FMEMOPEN | |
702 | # define HAVE_FMEMOPEN_FALLBACK | |
703 | # include <stdio.h> | |
704 | /** | |
705 | * fmemopen(3) fallback using BSD funopen. | |
706 | * | |
707 | * We could also provide one using fopencookie(), but should we have it we | |
708 | * most likely have fmemopen(). | |
709 | * | |
710 | * fseek() is currently not supported. | |
711 | */ | |
712 | FILE *fmemopen(void *buf, size_t size, const char *mode); | |
713 | # endif /* FUNOPEN */ | |
714 | #endif /* FMEMOPEN */ | |
715 | ||
716 | /** | |
717 | * printf hook for time_t. | |
718 | * | |
719 | * Arguments are: | |
720 | * time_t* time, bool utc | |
721 | */ | |
722 | int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, | |
723 | const void *const *args); | |
724 | ||
725 | /** | |
726 | * printf hook for time_t deltas. | |
727 | * | |
728 | * Arguments are: | |
729 | * time_t* begin, time_t* end | |
730 | */ | |
731 | int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, | |
732 | const void *const *args); | |
733 | ||
734 | /** | |
735 | * printf hook for memory areas. | |
736 | * | |
737 | * Arguments are: | |
738 | * u_char *ptr, u_int len | |
739 | */ | |
740 | int mem_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, | |
741 | const void *const *args); | |
742 | ||
743 | #endif /** UTILS_H_ @}*/ |