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