]>
Commit | Line | Data |
---|---|---|
552cc11b | 1 | /* |
ccb6758e | 2 | * Copyright (C) 2008-2013 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 MW |
29 | #include <arpa/inet.h> |
30 | #include <string.h> | |
552cc11b | 31 | |
bca34c37 | 32 | #include "enum.h" |
ba10cd3c | 33 | #include "utils/strerror.h" |
552cc11b | 34 | |
eab241fb AS |
35 | /** |
36 | * strongSwan program return codes | |
37 | */ | |
38 | #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64 | |
39 | #define SS_RC_DAEMON_INTEGRITY 65 | |
3646c8a1 | 40 | #define SS_RC_INITIALIZATION_FAILED 66 |
eab241fb | 41 | |
5d8306de AS |
42 | #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY |
43 | #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED | |
44 | ||
552cc11b MW |
45 | /** |
46 | * Number of bits in a byte | |
47 | */ | |
48 | #define BITS_PER_BYTE 8 | |
49 | ||
50 | /** | |
51 | * Default length for various auxiliary text buffers | |
52 | */ | |
53 | #define BUF_LEN 512 | |
54 | ||
55 | /** | |
a2a28d90 | 56 | * General purpose boolean type. |
552cc11b | 57 | */ |
a2a28d90 TB |
58 | #ifdef HAVE_STDBOOL_H |
59 | # include <stdbool.h> | |
60 | #else | |
61 | # ifndef HAVE__BOOL | |
62 | # define _Bool signed char | |
63 | # endif /* HAVE__BOOL */ | |
64 | # define bool _Bool | |
65 | # define false 0 | |
66 | # define true 1 | |
67 | # define __bool_true_false_are_defined 1 | |
68 | #endif /* HAVE_STDBOOL_H */ | |
69 | #ifndef FALSE | |
70 | # define FALSE false | |
71 | #endif /* FALSE */ | |
72 | #ifndef TRUE | |
73 | # define TRUE true | |
74 | #endif /* TRUE */ | |
75 | ||
76 | /** | |
77 | * Helper function that compares two strings for equality | |
78 | */ | |
79 | static inline bool streq(const char *x, const char *y) | |
80 | { | |
81 | return strcmp(x, y) == 0; | |
82 | } | |
552cc11b MW |
83 | |
84 | /** | |
985dcab1 | 85 | * Helper function that compares two strings for equality, length limited |
552cc11b | 86 | */ |
985dcab1 TB |
87 | static inline bool strneq(const char *x, const char *y, size_t len) |
88 | { | |
89 | return strncmp(x, y, len) == 0; | |
90 | } | |
552cc11b | 91 | |
f460facd TB |
92 | /** |
93 | * Helper function that checks if a string starts with a given prefix | |
94 | */ | |
95 | static inline bool strpfx(const char *x, const char *prefix) | |
96 | { | |
97 | return strneq(x, prefix, strlen(prefix)); | |
98 | } | |
99 | ||
63176bbc | 100 | /** |
a2a28d90 | 101 | * Helper function that compares two strings for equality ignoring case |
63176bbc | 102 | */ |
a2a28d90 TB |
103 | static inline bool strcaseeq(const char *x, const char *y) |
104 | { | |
105 | return strcasecmp(x, y) == 0; | |
106 | } | |
63176bbc | 107 | |
7a3e0a63 | 108 | /** |
985dcab1 | 109 | * Helper function that compares two strings for equality ignoring case, length limited |
7a3e0a63 | 110 | */ |
985dcab1 TB |
111 | static inline bool strncaseeq(const char *x, const char *y, size_t len) |
112 | { | |
113 | return strncasecmp(x, y, len) == 0; | |
114 | } | |
7a3e0a63 | 115 | |
32a145fd TB |
116 | /** |
117 | * Helper function that checks if a string starts with a given prefix | |
118 | */ | |
119 | static inline bool strcasepfx(const char *x, const char *prefix) | |
120 | { | |
121 | return strncaseeq(x, prefix, strlen(prefix)); | |
122 | } | |
123 | ||
1038d9fe MW |
124 | /** |
125 | * NULL-safe strdup variant | |
126 | */ | |
753ca22f TB |
127 | static inline char *strdupnull(const char *s) |
128 | { | |
129 | return s ? strdup(s) : NULL; | |
130 | } | |
1038d9fe | 131 | |
552cc11b | 132 | /** |
985dcab1 | 133 | * Helper function that compares two binary blobs for equality |
552cc11b | 134 | */ |
985dcab1 TB |
135 | static inline bool memeq(const void *x, const void *y, size_t len) |
136 | { | |
137 | return memcmp(x, y, len) == 0; | |
138 | } | |
552cc11b MW |
139 | |
140 | /** | |
141 | * Macro gives back larger of two values. | |
142 | */ | |
e822fc57 TB |
143 | #define max(x,y) ({ \ |
144 | typeof(x) _x = (x); \ | |
145 | typeof(y) _y = (y); \ | |
146 | _x > _y ? _x : _y; }) | |
147 | ||
552cc11b MW |
148 | |
149 | /** | |
150 | * Macro gives back smaller of two values. | |
151 | */ | |
e822fc57 TB |
152 | #define min(x,y) ({ \ |
153 | typeof(x) _x = (x); \ | |
154 | typeof(y) _y = (y); \ | |
155 | _x < _y ? _x : _y; }) | |
552cc11b MW |
156 | |
157 | /** | |
158 | * Call destructor of an object, if object != NULL | |
159 | */ | |
160 | #define DESTROY_IF(obj) if (obj) (obj)->destroy(obj) | |
161 | ||
162 | /** | |
163 | * Call offset destructor of an object, if object != NULL | |
164 | */ | |
165 | #define DESTROY_OFFSET_IF(obj, offset) if (obj) obj->destroy_offset(obj, offset); | |
166 | ||
167 | /** | |
168 | * Call function destructor of an object, if object != NULL | |
169 | */ | |
170 | #define DESTROY_FUNCTION_IF(obj, fn) if (obj) obj->destroy_function(obj, fn); | |
171 | ||
172 | /** | |
173 | * Debug macro to follow control flow | |
174 | */ | |
175 | #define POS printf("%s, line %d\n", __FILE__, __LINE__) | |
176 | ||
74eed73a MW |
177 | /** |
178 | * Object allocation/initialization macro, using designated initializer. | |
179 | */ | |
2e1f4a46 MW |
180 | #define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \ |
181 | *(this) = (typeof(*(this))){ __VA_ARGS__ }; } | |
74eed73a | 182 | |
1a1ff9d1 MW |
183 | /** |
184 | * Method declaration/definition macro, providing private and public interface. | |
185 | * | |
186 | * Defines a method name with this as first parameter and a return value ret, | |
187 | * and an alias for this method with a _ prefix, having the this argument | |
188 | * safely casted to the public interface iface. | |
189 | * _name is provided a function pointer, but will get optimized out by GCC. | |
190 | */ | |
191 | #define METHOD(iface, name, ret, this, ...) \ | |
23d2bf84 MW |
192 | static ret name(union {iface *_public; this;} \ |
193 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
d185b6ac | 194 | static typeof(name) *_##name = (typeof(name)*)name; \ |
23d2bf84 MW |
195 | static ret name(this, ##__VA_ARGS__) |
196 | ||
197 | /** | |
198 | * Same as METHOD(), but is defined for two public interfaces. | |
199 | */ | |
200 | #define METHOD2(iface1, iface2, name, ret, this, ...) \ | |
201 | static ret name(union {iface1 *_public1; iface2 *_public2; this;} \ | |
202 | __attribute__((transparent_union)), ##__VA_ARGS__); \ | |
d185b6ac | 203 | static typeof(name) *_##name = (typeof(name)*)name; \ |
1a1ff9d1 MW |
204 | static ret name(this, ##__VA_ARGS__) |
205 | ||
7ba89ccd MW |
206 | /** |
207 | * Architecture independent bitfield definition helpers (at least with GCC). | |
208 | * | |
209 | * Defines a bitfield with a type t and a fixed size of bitfield members, e.g.: | |
210 | * BITFIELD2(u_int8_t, | |
211 | * low: 4, | |
212 | * high: 4, | |
213 | * ) flags; | |
214 | * The member defined first placed at bit 0. | |
215 | */ | |
216 | #if BYTE_ORDER == LITTLE_ENDIAN | |
217 | #define BITFIELD2(t, a, b,...) struct { t a; t b; __VA_ARGS__} | |
218 | #define BITFIELD3(t, a, b, c,...) struct { t a; t b; t c; __VA_ARGS__} | |
219 | #define BITFIELD4(t, a, b, c, d,...) struct { t a; t b; t c; t d; __VA_ARGS__} | |
220 | #define BITFIELD5(t, a, b, c, d, e,...) struct { t a; t b; t c; t d; t e; __VA_ARGS__} | |
221 | #elif BYTE_ORDER == BIG_ENDIAN | |
222 | #define BITFIELD2(t, a, b,...) struct { t b; t a; __VA_ARGS__} | |
223 | #define BITFIELD3(t, a, b, c,...) struct { t c; t b; t a; __VA_ARGS__} | |
224 | #define BITFIELD4(t, a, b, c, d,...) struct { t d; t c; t b; t a; __VA_ARGS__} | |
225 | #define BITFIELD5(t, a, b, c, d, e,...) struct { t e; t d; t c; t b; t a; __VA_ARGS__} | |
226 | #endif | |
227 | ||
552cc11b MW |
228 | /** |
229 | * Macro to allocate a sized type. | |
230 | */ | |
231 | #define malloc_thing(thing) ((thing*)malloc(sizeof(thing))) | |
232 | ||
fca4d3ee MW |
233 | /** |
234 | * Get the number of elements in an array | |
235 | */ | |
236 | #define countof(array) (sizeof(array)/sizeof(array[0])) | |
237 | ||
479f2950 MW |
238 | /** |
239 | * Ignore result of functions tagged with warn_unused_result attributes | |
240 | */ | |
d0230850 | 241 | #define ignore_result(call) { if(call){}; } |
479f2950 | 242 | |
552cc11b MW |
243 | /** |
244 | * Assign a function as a class method | |
245 | */ | |
246 | #define ASSIGN(method, function) (method = (typeof(method))function) | |
247 | ||
248 | /** | |
249 | * time_t not defined | |
250 | */ | |
251 | #define UNDEFINED_TIME 0 | |
252 | ||
9f0327e6 AS |
253 | /** |
254 | * Maximum time since epoch causing wrap-around on Jan 19 03:14:07 UTC 2038 | |
255 | */ | |
256 | #define TIME_32_BIT_SIGNED_MAX 0x7fffffff | |
257 | ||
cc396286 TB |
258 | /** |
259 | * define some missing fixed width int types on OpenSolaris. | |
260 | * TODO: since the uintXX_t types are defined by the C99 standard we should | |
261 | * probably use those anyway | |
262 | */ | |
263 | #ifdef __sun | |
264 | #include <stdint.h> | |
265 | typedef uint8_t u_int8_t; | |
266 | typedef uint16_t u_int16_t; | |
267 | typedef uint32_t u_int32_t; | |
268 | typedef uint64_t u_int64_t; | |
269 | #endif | |
270 | ||
552cc11b MW |
271 | typedef enum status_t status_t; |
272 | ||
273 | /** | |
274 | * Return values of function calls. | |
275 | */ | |
276 | enum status_t { | |
277 | /** | |
278 | * Call succeeded. | |
279 | */ | |
280 | SUCCESS, | |
7daf5226 | 281 | |
552cc11b MW |
282 | /** |
283 | * Call failed. | |
284 | */ | |
285 | FAILED, | |
7daf5226 | 286 | |
552cc11b MW |
287 | /** |
288 | * Out of resources. | |
289 | */ | |
290 | OUT_OF_RES, | |
7daf5226 | 291 | |
552cc11b MW |
292 | /** |
293 | * The suggested operation is already done | |
294 | */ | |
295 | ALREADY_DONE, | |
7daf5226 | 296 | |
552cc11b MW |
297 | /** |
298 | * Not supported. | |
299 | */ | |
300 | NOT_SUPPORTED, | |
7daf5226 | 301 | |
552cc11b MW |
302 | /** |
303 | * One of the arguments is invalid. | |
304 | */ | |
305 | INVALID_ARG, | |
7daf5226 | 306 | |
552cc11b MW |
307 | /** |
308 | * Something could not be found. | |
309 | */ | |
310 | NOT_FOUND, | |
7daf5226 | 311 | |
552cc11b MW |
312 | /** |
313 | * Error while parsing. | |
314 | */ | |
315 | PARSE_ERROR, | |
7daf5226 | 316 | |
552cc11b MW |
317 | /** |
318 | * Error while verifying. | |
319 | */ | |
320 | VERIFY_ERROR, | |
7daf5226 | 321 | |
552cc11b MW |
322 | /** |
323 | * Object in invalid state. | |
324 | */ | |
325 | INVALID_STATE, | |
7daf5226 | 326 | |
552cc11b MW |
327 | /** |
328 | * Destroy object which called method belongs to. | |
329 | */ | |
330 | DESTROY_ME, | |
7daf5226 | 331 | |
552cc11b MW |
332 | /** |
333 | * Another call to the method is required. | |
334 | */ | |
335 | NEED_MORE, | |
336 | }; | |
337 | ||
338 | /** | |
339 | * enum_names for type status_t. | |
340 | */ | |
341 | extern enum_name_t *status_names; | |
342 | ||
4d174272 MW |
343 | typedef enum tty_escape_t tty_escape_t; |
344 | ||
345 | /** | |
346 | * Excape codes for tty colors | |
347 | */ | |
348 | enum tty_escape_t { | |
349 | /** text properties */ | |
350 | TTY_RESET, | |
351 | TTY_BOLD, | |
352 | TTY_UNDERLINE, | |
353 | TTY_BLINKING, | |
354 | ||
355 | /** foreground colors */ | |
356 | TTY_FG_BLACK, | |
357 | TTY_FG_RED, | |
358 | TTY_FG_GREEN, | |
359 | TTY_FG_YELLOW, | |
360 | TTY_FG_BLUE, | |
361 | TTY_FG_MAGENTA, | |
362 | TTY_FG_CYAN, | |
363 | TTY_FG_WHITE, | |
364 | TTY_FG_DEF, | |
365 | ||
366 | /** background colors */ | |
367 | TTY_BG_BLACK, | |
368 | TTY_BG_RED, | |
369 | TTY_BG_GREEN, | |
370 | TTY_BG_YELLOW, | |
371 | TTY_BG_BLUE, | |
372 | TTY_BG_MAGENTA, | |
373 | TTY_BG_CYAN, | |
374 | TTY_BG_WHITE, | |
375 | TTY_BG_DEF, | |
376 | }; | |
377 | ||
378 | /** | |
379 | * Get the escape string for a given TTY color, empty string on non-tty fd | |
380 | */ | |
381 | char* tty_escape_get(int fd, tty_escape_t escape); | |
382 | ||
552cc11b MW |
383 | /** |
384 | * deprecated pluto style return value: | |
385 | * error message, NULL for success | |
386 | */ | |
387 | typedef const char *err_t; | |
388 | ||
389 | /** | |
390 | * Handle struct timeval like an own type. | |
391 | */ | |
392 | typedef struct timeval timeval_t; | |
393 | ||
394 | /** | |
395 | * Handle struct timespec like an own type. | |
396 | */ | |
397 | typedef struct timespec timespec_t; | |
398 | ||
399 | /** | |
400 | * Handle struct chunk_t like an own type. | |
401 | */ | |
402 | typedef struct sockaddr sockaddr_t; | |
403 | ||
552cc11b MW |
404 | /** |
405 | * Same as memcpy, but XORs src into dst instead of copy | |
406 | */ | |
407 | void memxor(u_int8_t dest[], u_int8_t src[], size_t n); | |
408 | ||
ed678b52 MW |
409 | /** |
410 | * Safely overwrite n bytes of memory at ptr with zero, non-inlining variant. | |
411 | */ | |
412 | void memwipe_noinline(void *ptr, size_t n); | |
413 | ||
414 | /** | |
415 | * Safely overwrite n bytes of memory at ptr with zero, inlining variant. | |
416 | */ | |
417 | static inline void memwipe_inline(void *ptr, size_t n) | |
418 | { | |
419 | volatile char *c = (volatile char*)ptr; | |
d45b242b | 420 | size_t m, i; |
ed678b52 MW |
421 | |
422 | /* byte wise until long aligned */ | |
d45b242b | 423 | for (i = 0; (uintptr_t)&c[i] % sizeof(long) && i < n; i++) |
ed678b52 MW |
424 | { |
425 | c[i] = 0; | |
426 | } | |
d45b242b MW |
427 | /* word wise */ |
428 | if (n >= sizeof(long)) | |
ed678b52 | 429 | { |
d45b242b MW |
430 | for (m = n - sizeof(long); i <= m; i += sizeof(long)) |
431 | { | |
432 | *(volatile long*)&c[i] = 0; | |
433 | } | |
ed678b52 MW |
434 | } |
435 | /* byte wise of the rest */ | |
436 | for (; i < n; i++) | |
437 | { | |
438 | c[i] = 0; | |
439 | } | |
440 | } | |
441 | ||
442 | /** | |
443 | * Safely overwrite n bytes of memory at ptr with zero, auto-inlining variant. | |
444 | */ | |
445 | static inline void memwipe(void *ptr, size_t n) | |
446 | { | |
c480b5f4 TB |
447 | if (!ptr) |
448 | { | |
449 | return; | |
450 | } | |
ed678b52 MW |
451 | if (__builtin_constant_p(n)) |
452 | { | |
453 | memwipe_inline(ptr, n); | |
454 | } | |
455 | else | |
456 | { | |
457 | memwipe_noinline(ptr, n); | |
458 | } | |
459 | } | |
460 | ||
81736d7d TB |
461 | /** |
462 | * A variant of strstr with the characteristics of memchr, where haystack is not | |
463 | * a null-terminated string but simply a memory area of length n. | |
464 | */ | |
465 | void *memstr(const void *haystack, const char *needle, size_t n); | |
466 | ||
d543d9ca TB |
467 | /** |
468 | * Translates the characters in the given string, searching for characters | |
469 | * in 'from' and mapping them to characters in 'to'. | |
470 | * The two characters sets 'from' and 'to' must contain the same number of | |
471 | * characters. | |
472 | */ | |
473 | char *translate(char *str, const char *from, const char *to); | |
474 | ||
ccb6758e | 475 | /** |
4ab38d98 | 476 | * Replaces all occurrences of search in the given string with replace. |
ccb6758e TB |
477 | * |
478 | * Allocates memory only if anything is replaced in the string. The original | |
479 | * string is also returned if any of the arguments are invalid (e.g. if search | |
480 | * is empty or any of them are NULL). | |
481 | * | |
482 | * @param str original string | |
483 | * @param search string to search for and replace | |
4ab38d98 | 484 | * @param replace string to replace found occurrences with |
ccb6758e TB |
485 | * @return allocated string, if anything got replaced, str otherwise |
486 | */ | |
487 | char *strreplace(const char *str, const char *search, const char *replace); | |
488 | ||
6c20579a | 489 | /** |
7daf5226 | 490 | * Creates a directory and all required parent directories. |
6c20579a | 491 | * |
3f310c0d | 492 | * @param path path to the new directory |
7daf5226 | 493 | * @param mode permissions of the new directory/directories |
6c20579a TB |
494 | * @return TRUE on success |
495 | */ | |
496 | bool mkdir_p(const char *path, mode_t mode); | |
497 | ||
9a8fdc15 TB |
498 | #ifndef HAVE_CLOSEFROM |
499 | /** | |
500 | * Close open file descriptors greater than or equal to lowfd. | |
501 | * | |
4ab38d98 | 502 | * @param lowfd start closing file descriptors from here |
9a8fdc15 TB |
503 | */ |
504 | void closefrom(int lowfd); | |
505 | #endif | |
506 | ||
3f310c0d MW |
507 | /** |
508 | * Get a timestamp from a monotonic time source. | |
509 | * | |
510 | * While the time()/gettimeofday() functions are affected by leap seconds | |
511 | * and system time changes, this function returns ever increasing monotonic | |
512 | * time stamps. | |
513 | * | |
514 | * @param tv timeval struct receiving monotonic timestamps, or NULL | |
515 | * @return monotonic timestamp in seconds | |
516 | */ | |
517 | time_t time_monotonic(timeval_t *tv); | |
518 | ||
eecd41e3 TB |
519 | /** |
520 | * Add the given number of milliseconds to the given timeval struct | |
521 | * | |
522 | * @param tv timeval struct to modify | |
523 | * @param ms number of milliseconds | |
524 | */ | |
525 | static inline void timeval_add_ms(timeval_t *tv, u_int ms) | |
526 | { | |
527 | tv->tv_usec += ms * 1000; | |
819c02db | 528 | while (tv->tv_usec >= 1000000 /* 1s */) |
eecd41e3 TB |
529 | { |
530 | tv->tv_usec -= 1000000; | |
531 | tv->tv_sec++; | |
532 | } | |
533 | } | |
534 | ||
081ae2eb MW |
535 | /** |
536 | * returns null | |
537 | */ | |
538 | void *return_null(); | |
539 | ||
233b853d MW |
540 | /** |
541 | * No-Operation function | |
542 | */ | |
543 | void nop(); | |
544 | ||
da17b016 MW |
545 | /** |
546 | * returns TRUE | |
547 | */ | |
548 | bool return_true(); | |
549 | ||
550 | /** | |
551 | * returns FALSE | |
552 | */ | |
553 | bool return_false(); | |
554 | ||
502edf42 MW |
555 | /** |
556 | * returns FAILED | |
557 | */ | |
558 | status_t return_failed(); | |
559 | ||
4755ab50 MW |
560 | /** |
561 | * returns SUCCESS | |
562 | */ | |
563 | status_t return_success(); | |
564 | ||
0be12e35 MW |
565 | /** |
566 | * Write a 16-bit host order value in network order to an unaligned address. | |
567 | * | |
568 | * @param host host order 16-bit value | |
569 | * @param network unaligned address to write network order value to | |
570 | */ | |
571 | static inline void htoun16(void *network, u_int16_t host) | |
572 | { | |
dbee988e MW |
573 | char *unaligned = (char*)network; |
574 | ||
0be12e35 | 575 | host = htons(host); |
dbee988e | 576 | memcpy(unaligned, &host, sizeof(host)); |
0be12e35 MW |
577 | } |
578 | ||
579 | /** | |
580 | * Write a 32-bit host order value in network order to an unaligned address. | |
581 | * | |
582 | * @param host host order 32-bit value | |
583 | * @param network unaligned address to write network order value to | |
584 | */ | |
585 | static inline void htoun32(void *network, u_int32_t host) | |
586 | { | |
dbee988e MW |
587 | char *unaligned = (char*)network; |
588 | ||
0be12e35 | 589 | host = htonl(host); |
dbee988e | 590 | memcpy((char*)unaligned, &host, sizeof(host)); |
0be12e35 MW |
591 | } |
592 | ||
fbeb9454 AS |
593 | /** |
594 | * Write a 64-bit host order value in network order to an unaligned address. | |
595 | * | |
a91462df | 596 | * @param host host order 64-bit value |
fbeb9454 AS |
597 | * @param network unaligned address to write network order value to |
598 | */ | |
599 | static inline void htoun64(void *network, u_int64_t host) | |
600 | { | |
601 | char *unaligned = (char*)network; | |
f4e25e60 MW |
602 | |
603 | #ifdef be64toh | |
604 | host = htobe64(host); | |
605 | memcpy((char*)unaligned, &host, sizeof(host)); | |
606 | #else | |
fbeb9454 AS |
607 | u_int32_t high_part, low_part; |
608 | ||
609 | high_part = host >> 32; | |
610 | high_part = htonl(high_part); | |
611 | low_part = host & 0xFFFFFFFFLL; | |
612 | low_part = htonl(low_part); | |
613 | ||
614 | memcpy(unaligned, &high_part, sizeof(high_part)); | |
615 | unaligned += sizeof(high_part); | |
616 | memcpy(unaligned, &low_part, sizeof(low_part)); | |
f4e25e60 | 617 | #endif |
fbeb9454 AS |
618 | } |
619 | ||
0be12e35 MW |
620 | /** |
621 | * Read a 16-bit value in network order from an unaligned address to host order. | |
622 | * | |
623 | * @param network unaligned address to read network order value from | |
624 | * @return host order value | |
625 | */ | |
626 | static inline u_int16_t untoh16(void *network) | |
627 | { | |
dbee988e | 628 | char *unaligned = (char*)network; |
0be12e35 MW |
629 | u_int16_t tmp; |
630 | ||
dbee988e | 631 | memcpy(&tmp, unaligned, sizeof(tmp)); |
0be12e35 MW |
632 | return ntohs(tmp); |
633 | } | |
634 | ||
635 | /** | |
636 | * Read a 32-bit value in network order from an unaligned address to host order. | |
637 | * | |
638 | * @param network unaligned address to read network order value from | |
639 | * @return host order value | |
640 | */ | |
641 | static inline u_int32_t untoh32(void *network) | |
642 | { | |
dbee988e | 643 | char *unaligned = (char*)network; |
0be12e35 MW |
644 | u_int32_t tmp; |
645 | ||
dbee988e | 646 | memcpy(&tmp, unaligned, sizeof(tmp)); |
f8f4f31a | 647 | return ntohl(tmp); |
0be12e35 MW |
648 | } |
649 | ||
fbeb9454 AS |
650 | /** |
651 | * Read a 64-bit value in network order from an unaligned address to host order. | |
652 | * | |
653 | * @param network unaligned address to read network order value from | |
654 | * @return host order value | |
655 | */ | |
656 | static inline u_int64_t untoh64(void *network) | |
657 | { | |
658 | char *unaligned = (char*)network; | |
f4e25e60 MW |
659 | |
660 | #ifdef be64toh | |
661 | u_int64_t tmp; | |
662 | ||
663 | memcpy(&tmp, unaligned, sizeof(tmp)); | |
664 | return be64toh(tmp); | |
665 | #else | |
fbeb9454 AS |
666 | u_int32_t high_part, low_part; |
667 | ||
668 | memcpy(&high_part, unaligned, sizeof(high_part)); | |
669 | unaligned += sizeof(high_part); | |
670 | memcpy(&low_part, unaligned, sizeof(low_part)); | |
671 | ||
672 | high_part = ntohl(high_part); | |
673 | low_part = ntohl(low_part); | |
674 | ||
675 | return (((u_int64_t)high_part) << 32) + low_part; | |
f4e25e60 | 676 | #endif |
fbeb9454 AS |
677 | } |
678 | ||
84044f9c | 679 | /** |
812ae898 | 680 | * Get the padding required to make size a multiple of alignment |
84044f9c | 681 | */ |
812ae898 | 682 | static inline size_t pad_len(size_t size, size_t alignment) |
84044f9c | 683 | { |
812ae898 | 684 | size_t remainder; |
84044f9c | 685 | |
812ae898 TB |
686 | remainder = size % alignment; |
687 | return remainder ? alignment - remainder : 0; | |
688 | } | |
689 | ||
690 | /** | |
691 | * Round up size to be multiple of alignment | |
692 | */ | |
693 | static inline size_t round_up(size_t size, size_t alignment) | |
694 | { | |
695 | return size + pad_len(size, alignment); | |
84044f9c MW |
696 | } |
697 | ||
698 | /** | |
812ae898 | 699 | * Round down size to be a multiple of alignment |
84044f9c | 700 | */ |
812ae898 | 701 | static inline size_t round_down(size_t size, size_t alignment) |
84044f9c | 702 | { |
812ae898 | 703 | return size - (size % alignment); |
84044f9c MW |
704 | } |
705 | ||
552cc11b MW |
706 | /** |
707 | * Special type to count references | |
708 | */ | |
4d04e2c6 | 709 | typedef u_int refcount_t; |
efd0fe21 MW |
710 | |
711 | #ifdef HAVE_GCC_ATOMIC_OPERATIONS | |
712 | ||
3160b92a | 713 | #define ref_get(ref) __sync_add_and_fetch(ref, 1) |
efd0fe21 MW |
714 | #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1)) |
715 | ||
5317dd68 TB |
716 | #define cas_bool(ptr, oldval, newval) \ |
717 | (__sync_bool_compare_and_swap(ptr, oldval, newval)) | |
718 | #define cas_ptr(ptr, oldval, newval) \ | |
719 | (__sync_bool_compare_and_swap(ptr, oldval, newval)) | |
720 | ||
efd0fe21 MW |
721 | #else /* !HAVE_GCC_ATOMIC_OPERATIONS */ |
722 | ||
552cc11b MW |
723 | /** |
724 | * Get a new reference. | |
725 | * | |
726 | * Increments the reference counter atomic. | |
727 | * | |
728 | * @param ref pointer to ref counter | |
3160b92a | 729 | * @return new value of ref |
552cc11b | 730 | */ |
3160b92a | 731 | refcount_t ref_get(refcount_t *ref); |
552cc11b MW |
732 | |
733 | /** | |
734 | * Put back a unused reference. | |
735 | * | |
7daf5226 | 736 | * Decrements the reference counter atomic and |
552cc11b MW |
737 | * says if more references available. |
738 | * | |
739 | * @param ref pointer to ref counter | |
740 | * @return TRUE if no more references counted | |
741 | */ | |
742 | bool ref_put(refcount_t *ref); | |
743 | ||
5317dd68 TB |
744 | /** |
745 | * Atomically replace value of ptr with newval if it currently equals oldval. | |
746 | * | |
747 | * @param ptr pointer to variable | |
748 | * @param oldval old value of the variable | |
749 | * @param newval new value set if possible | |
750 | * @return TRUE if value equaled oldval and newval was written | |
751 | */ | |
752 | bool cas_bool(bool *ptr, bool oldval, bool newval); | |
753 | ||
754 | /** | |
755 | * Atomically replace value of ptr with newval if it currently equals oldval. | |
756 | * | |
757 | * @param ptr pointer to variable | |
758 | * @param oldval old value of the variable | |
759 | * @param newval new value set if possible | |
760 | * @return TRUE if value equaled oldval and newval was written | |
761 | */ | |
762 | bool cas_ptr(void **ptr, void *oldval, void *newval); | |
763 | ||
efd0fe21 MW |
764 | #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ |
765 | ||
2077d996 MW |
766 | #ifndef HAVE_FMEMOPEN |
767 | # ifdef HAVE_FUNOPEN | |
9df621d2 MW |
768 | # define HAVE_FMEMOPEN |
769 | # define HAVE_FMEMOPEN_FALLBACK | |
5ac29360 | 770 | # include <stdio.h> |
2077d996 MW |
771 | /** |
772 | * fmemopen(3) fallback using BSD funopen. | |
773 | * | |
774 | * We could also provide one using fopencookie(), but should we have it we | |
775 | * most likely have fmemopen(). | |
776 | * | |
777 | * fseek() is currently not supported. | |
778 | */ | |
779 | FILE *fmemopen(void *buf, size_t size, const char *mode); | |
780 | # endif /* FUNOPEN */ | |
781 | #endif /* FMEMOPEN */ | |
782 | ||
552cc11b | 783 | /** |
d25ce370 | 784 | * printf hook for time_t. |
552cc11b | 785 | * |
7daf5226 | 786 | * Arguments are: |
323f9f99 | 787 | * time_t* time, bool utc |
552cc11b | 788 | */ |
1b40b74d | 789 | int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 790 | const void *const *args); |
552cc11b MW |
791 | |
792 | /** | |
d25ce370 | 793 | * printf hook for time_t deltas. |
552cc11b | 794 | * |
7daf5226 | 795 | * Arguments are: |
323f9f99 | 796 | * time_t* begin, time_t* end |
552cc11b | 797 | */ |
1b40b74d | 798 | int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 799 | const void *const *args); |
552cc11b MW |
800 | |
801 | /** | |
d25ce370 | 802 | * printf hook for memory areas. |
552cc11b | 803 | * |
7daf5226 | 804 | * Arguments are: |
817ab8a8 | 805 | * u_char *ptr, u_int len |
552cc11b | 806 | */ |
1b40b74d | 807 | int mem_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec, |
d25ce370 | 808 | const void *const *args); |
552cc11b | 809 | |
1490ff4d | 810 | #endif /** UTILS_H_ @}*/ |