ff0c2cb7e0c3547ccb2d59b6dffa762778587b18
[asterisk/asterisk.git] / include / asterisk / utils.h
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Utility functions
5  *
6  * Copyright (C) 2004, Digium
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #ifndef _ASTERISK_UTIL_H
13 #define _ASTERISK_UTIL_H
14
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <pthread.h>
18 #include <asterisk/lock.h>
19 #include <limits.h>
20
21 /* Note:
22    It is very important to use only unsigned variables to hold
23    bit flags, as otherwise you can fall prey to the compiler's
24    sign-extension antics if you try to use the top two bits in
25    your variable.
26
27    The flag macros below use a set of compiler tricks to verify
28    that the caller is using an "unsigned int" variable to hold
29    the flags, and nothing else. If the caller uses any other
30    type of variable, a warning message similar to this:
31
32    warning: comparison of distinct pointer types lacks cast
33
34    will be generated.
35
36    The "dummy" variable below is used to make these comparisons.
37
38    Also note that at -O2 or above, this type-safety checking
39    does _not_ produce any additional object code at all.
40 */
41
42 extern unsigned int __unsigned_int_flags_dummy;
43
44 #define ast_test_flag(p,flag)           ({ \
45                                         typeof ((p)->flags) __p = (p)->flags; \
46                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
47                                         (void) (&__p == &__x); \
48                                         ((p)->flags & (flag)); \
49                                         })
50
51 #define ast_set_flag(p,flag)            do { \
52                                         typeof ((p)->flags) __p = (p)->flags; \
53                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
54                                         (void) (&__p == &__x); \
55                                         ((p)->flags |= (flag)); \
56                                         } while(0)
57
58 #define ast_clear_flag(p,flag)          do { \
59                                         typeof ((p)->flags) __p = (p)->flags; \
60                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
61                                         (void) (&__p == &__x); \
62                                         ((p)->flags &= ~(flag)); \
63                                         } while(0)
64
65 #define ast_copy_flags(dest,src,flagz)  do { \
66                                         typeof ((dest)->flags) __d = (dest)->flags; \
67                                         typeof ((src)->flags) __s = (src)->flags; \
68                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
69                                         (void) (&__d == &__x); \
70                                         (void) (&__s == &__x); \
71                                         (dest)->flags &= ~(flagz); \
72                                         (dest)->flags |= ((src)->flags & (flagz)); \
73                                         } while (0)
74
75 #define ast_set2_flag(p,value,flag)     do { \
76                                         typeof ((p)->flags) __p = (p)->flags; \
77                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
78                                         (void) (&__p == &__x); \
79                                         if (value) \
80                                                 (p)->flags |= (flag); \
81                                         else \
82                                                 (p)->flags &= ~(flag); \
83                                         } while (0)
84
85 /* Non-type checking variations for non-unsigned int flags.  You
86    should only use non-unsigned int flags where required by 
87    protocol etc and if you know what you're doing :)  */
88 #define ast_test_flag_nonstd(p,flag)            ({ \
89                                         ((p)->flags & (flag)); \
90                                         })
91
92 #define ast_set_flag_nonstd(p,flag)             do { \
93                                         ((p)->flags |= (flag)); \
94                                         } while(0)
95
96 #define ast_clear_flag_nonstd(p,flag)           do { \
97                                         ((p)->flags &= ~(flag)); \
98                                         } while(0)
99
100 #define ast_copy_flags_nonstd(dest,src,flagz)   do { \
101                                         (dest)->flags &= ~(flagz); \
102                                         (dest)->flags |= ((src)->flags & (flagz)); \
103                                         } while (0)
104
105 #define ast_set2_flag_nonstd(p,value,flag)      do { \
106                                         if (value) \
107                                                 (p)->flags |= (flag); \
108                                         else \
109                                                 (p)->flags &= ~(flag); \
110                                         } while (0)
111
112 #define AST_FLAGS_ALL UINT_MAX
113
114 struct ast_flags {
115         unsigned int flags;
116 };
117
118 static inline int ast_strlen_zero(const char *s)
119 {
120         return (*s == '\0');
121 }
122
123 struct ast_hostent {
124         struct hostent hp;
125         char buf[1024];
126 };
127
128 extern char *ast_strip(char *buf);
129 extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
130 extern int ast_base64encode(char *dst, unsigned char *src, int srclen, int max);
131 extern int ast_base64decode(unsigned char *dst, char *src, int max);
132
133 extern int test_for_thread_safety(void);
134 extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
135 extern int ast_utils_init(void);
136
137 #ifdef inet_ntoa
138 #undef inet_ntoa
139 #endif
140 #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
141
142 #ifdef __linux__
143 #define ast_pthread_create pthread_create
144 #define ast_strcasestr strcasestr
145 #else
146 /* Linux threads have a default 2MB stack size. */
147 #ifndef PTHREAD_ATTR_STACKSIZE
148 #define PTHREAD_ATTR_STACKSIZE          2097152
149 #endif /* PTHREAD_ATTR_STACKSIZE */
150 extern int ast_pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data);
151 #endif /* __linux__ */
152
153 extern char *ast_strcasestr(const char *, const char *);
154
155 #endif
156
157 /* print call- and pickup groups into buffer */
158 char *ast_print_group(char *buf, int buflen, unsigned int group);