utils: Move thread-safe strerror replacement to a separate file
[strongswan.git] / src / libstrongswan / utils / utils.h
1 /*
2 * Copyright (C) 2008-2013 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 <arpa/inet.h>
30 #include <string.h>
31
32 #include "enum.h"
33 #include "utils/strerror.h"
34
35 /**
36 * strongSwan program return codes
37 */
38 #define SS_RC_LIBSTRONGSWAN_INTEGRITY 64
39 #define SS_RC_DAEMON_INTEGRITY 65
40 #define SS_RC_INITIALIZATION_FAILED 66
41
42 #define SS_RC_FIRST SS_RC_LIBSTRONGSWAN_INTEGRITY
43 #define SS_RC_LAST SS_RC_INITIALIZATION_FAILED
44
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 /**
56 * General purpose boolean type.
57 */
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 }
83
84 /**
85 * Helper function that compares two strings for equality, length limited
86 */
87 static inline bool strneq(const char *x, const char *y, size_t len)
88 {
89 return strncmp(x, y, len) == 0;
90 }
91
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
100 /**
101 * Helper function that compares two strings for equality ignoring case
102 */
103 static inline bool strcaseeq(const char *x, const char *y)
104 {
105 return strcasecmp(x, y) == 0;
106 }
107
108 /**
109 * Helper function that compares two strings for equality ignoring case, length limited
110 */
111 static inline bool strncaseeq(const char *x, const char *y, size_t len)
112 {
113 return strncasecmp(x, y, len) == 0;
114 }
115
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
124 /**
125 * NULL-safe strdup variant
126 */
127 static inline char *strdupnull(const char *s)
128 {
129 return s ? strdup(s) : NULL;
130 }
131
132 /**
133 * Helper function that compares two binary blobs for equality
134 */
135 static inline bool memeq(const void *x, const void *y, size_t len)
136 {
137 return memcmp(x, y, len) == 0;
138 }
139
140 /**
141 * Macro gives back larger of two values.
142 */
143 #define max(x,y) ({ \
144 typeof(x) _x = (x); \
145 typeof(y) _y = (y); \
146 _x > _y ? _x : _y; })
147
148
149 /**
150 * Macro gives back smaller of two values.
151 */
152 #define min(x,y) ({ \
153 typeof(x) _x = (x); \
154 typeof(y) _y = (y); \
155 _x < _y ? _x : _y; })
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
177 /**
178 * Object allocation/initialization macro, using designated initializer.
179 */
180 #define INIT(this, ...) { (this) = malloc(sizeof(*(this))); \
181 *(this) = (typeof(*(this))){ __VA_ARGS__ }; }
182
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, ...) \
192 static ret name(union {iface *_public; this;} \
193 __attribute__((transparent_union)), ##__VA_ARGS__); \
194 static typeof(name) *_##name = (typeof(name)*)name; \
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__); \
203 static typeof(name) *_##name = (typeof(name)*)name; \
204 static ret name(this, ##__VA_ARGS__)
205
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
228 /**
229 * Macro to allocate a sized type.
230 */
231 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
232
233 /**
234 * Get the number of elements in an array
235 */
236 #define countof(array) (sizeof(array)/sizeof(array[0]))
237
238 /**
239 * Ignore result of functions tagged with warn_unused_result attributes
240 */
241 #define ignore_result(call) { if(call){}; }
242
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
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
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
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,
281
282 /**
283 * Call failed.
284 */
285 FAILED,
286
287 /**
288 * Out of resources.
289 */
290 OUT_OF_RES,
291
292 /**
293 * The suggested operation is already done
294 */
295 ALREADY_DONE,
296
297 /**
298 * Not supported.
299 */
300 NOT_SUPPORTED,
301
302 /**
303 * One of the arguments is invalid.
304 */
305 INVALID_ARG,
306
307 /**
308 * Something could not be found.
309 */
310 NOT_FOUND,
311
312 /**
313 * Error while parsing.
314 */
315 PARSE_ERROR,
316
317 /**
318 * Error while verifying.
319 */
320 VERIFY_ERROR,
321
322 /**
323 * Object in invalid state.
324 */
325 INVALID_STATE,
326
327 /**
328 * Destroy object which called method belongs to.
329 */
330 DESTROY_ME,
331
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
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
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
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
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;
420 size_t m, i;
421
422 /* byte wise until long aligned */
423 for (i = 0; (uintptr_t)&c[i] % sizeof(long) && i < n; i++)
424 {
425 c[i] = 0;
426 }
427 /* word wise */
428 if (n >= sizeof(long))
429 {
430 for (m = n - sizeof(long); i <= m; i += sizeof(long))
431 {
432 *(volatile long*)&c[i] = 0;
433 }
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 {
447 if (!ptr)
448 {
449 return;
450 }
451 if (__builtin_constant_p(n))
452 {
453 memwipe_inline(ptr, n);
454 }
455 else
456 {
457 memwipe_noinline(ptr, n);
458 }
459 }
460
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
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
475 /**
476 * Replaces all occurrences of search in the given string with replace.
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
484 * @param replace string to replace found occurrences with
485 * @return allocated string, if anything got replaced, str otherwise
486 */
487 char *strreplace(const char *str, const char *search, const char *replace);
488
489 /**
490 * Creates a directory and all required parent directories.
491 *
492 * @param path path to the new directory
493 * @param mode permissions of the new directory/directories
494 * @return TRUE on success
495 */
496 bool mkdir_p(const char *path, mode_t mode);
497
498 #ifndef HAVE_CLOSEFROM
499 /**
500 * Close open file descriptors greater than or equal to lowfd.
501 *
502 * @param lowfd start closing file descriptors from here
503 */
504 void closefrom(int lowfd);
505 #endif
506
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
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;
528 while (tv->tv_usec >= 1000000 /* 1s */)
529 {
530 tv->tv_usec -= 1000000;
531 tv->tv_sec++;
532 }
533 }
534
535 /**
536 * returns null
537 */
538 void *return_null();
539
540 /**
541 * No-Operation function
542 */
543 void nop();
544
545 /**
546 * returns TRUE
547 */
548 bool return_true();
549
550 /**
551 * returns FALSE
552 */
553 bool return_false();
554
555 /**
556 * returns FAILED
557 */
558 status_t return_failed();
559
560 /**
561 * returns SUCCESS
562 */
563 status_t return_success();
564
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 {
573 char *unaligned = (char*)network;
574
575 host = htons(host);
576 memcpy(unaligned, &host, sizeof(host));
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 {
587 char *unaligned = (char*)network;
588
589 host = htonl(host);
590 memcpy((char*)unaligned, &host, sizeof(host));
591 }
592
593 /**
594 * Write a 64-bit host order value in network order to an unaligned address.
595 *
596 * @param host host order 64-bit value
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;
602
603 #ifdef be64toh
604 host = htobe64(host);
605 memcpy((char*)unaligned, &host, sizeof(host));
606 #else
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));
617 #endif
618 }
619
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 {
628 char *unaligned = (char*)network;
629 u_int16_t tmp;
630
631 memcpy(&tmp, unaligned, sizeof(tmp));
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 {
643 char *unaligned = (char*)network;
644 u_int32_t tmp;
645
646 memcpy(&tmp, unaligned, sizeof(tmp));
647 return ntohl(tmp);
648 }
649
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;
659
660 #ifdef be64toh
661 u_int64_t tmp;
662
663 memcpy(&tmp, unaligned, sizeof(tmp));
664 return be64toh(tmp);
665 #else
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;
676 #endif
677 }
678
679 /**
680 * Get the padding required to make size a multiple of alignment
681 */
682 static inline size_t pad_len(size_t size, size_t alignment)
683 {
684 size_t remainder;
685
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);
696 }
697
698 /**
699 * Round down size to be a multiple of alignment
700 */
701 static inline size_t round_down(size_t size, size_t alignment)
702 {
703 return size - (size % alignment);
704 }
705
706 /**
707 * Special type to count references
708 */
709 typedef u_int refcount_t;
710
711 #ifdef HAVE_GCC_ATOMIC_OPERATIONS
712
713 #define ref_get(ref) __sync_add_and_fetch(ref, 1)
714 #define ref_put(ref) (!__sync_sub_and_fetch(ref, 1))
715
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
721 #else /* !HAVE_GCC_ATOMIC_OPERATIONS */
722
723 /**
724 * Get a new reference.
725 *
726 * Increments the reference counter atomic.
727 *
728 * @param ref pointer to ref counter
729 * @return new value of ref
730 */
731 refcount_t ref_get(refcount_t *ref);
732
733 /**
734 * Put back a unused reference.
735 *
736 * Decrements the reference counter atomic and
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
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
764 #endif /* HAVE_GCC_ATOMIC_OPERATIONS */
765
766 #ifndef HAVE_FMEMOPEN
767 # ifdef HAVE_FUNOPEN
768 # define HAVE_FMEMOPEN
769 # define HAVE_FMEMOPEN_FALLBACK
770 # include <stdio.h>
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
783 /**
784 * printf hook for time_t.
785 *
786 * Arguments are:
787 * time_t* time, bool utc
788 */
789 int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
790 const void *const *args);
791
792 /**
793 * printf hook for time_t deltas.
794 *
795 * Arguments are:
796 * time_t* begin, time_t* end
797 */
798 int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
799 const void *const *args);
800
801 /**
802 * printf hook for memory areas.
803 *
804 * Arguments are:
805 * u_char *ptr, u_int len
806 */
807 int mem_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
808 const void *const *args);
809
810 #endif /** UTILS_H_ @}*/