00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _ASTERISK_UTILS_H
00024 #define _ASTERISK_UTILS_H
00025
00026 #include "asterisk/compat.h"
00027
00028 #include <netinet/in.h>
00029 #include <arpa/inet.h>
00030 #include <netdb.h>
00031 #include <limits.h>
00032
00033 #include "asterisk/lock.h"
00034 #include "asterisk/time.h"
00035 #include "asterisk/strings.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 extern unsigned int __unsigned_int_flags_dummy;
00061
00062 #define ast_test_flag(p,flag) ({ \
00063 typeof ((p)->flags) __p = (p)->flags; \
00064 typeof (__unsigned_int_flags_dummy) __x = 0; \
00065 (void) (&__p == &__x); \
00066 ((p)->flags & (flag)); \
00067 })
00068
00069 #define ast_set_flag(p,flag) do { \
00070 typeof ((p)->flags) __p = (p)->flags; \
00071 typeof (__unsigned_int_flags_dummy) __x = 0; \
00072 (void) (&__p == &__x); \
00073 ((p)->flags |= (flag)); \
00074 } while(0)
00075
00076 #define ast_clear_flag(p,flag) do { \
00077 typeof ((p)->flags) __p = (p)->flags; \
00078 typeof (__unsigned_int_flags_dummy) __x = 0; \
00079 (void) (&__p == &__x); \
00080 ((p)->flags &= ~(flag)); \
00081 } while(0)
00082
00083 #define ast_copy_flags(dest,src,flagz) do { \
00084 typeof ((dest)->flags) __d = (dest)->flags; \
00085 typeof ((src)->flags) __s = (src)->flags; \
00086 typeof (__unsigned_int_flags_dummy) __x = 0; \
00087 (void) (&__d == &__x); \
00088 (void) (&__s == &__x); \
00089 (dest)->flags &= ~(flagz); \
00090 (dest)->flags |= ((src)->flags & (flagz)); \
00091 } while (0)
00092
00093 #define ast_set2_flag(p,value,flag) do { \
00094 typeof ((p)->flags) __p = (p)->flags; \
00095 typeof (__unsigned_int_flags_dummy) __x = 0; \
00096 (void) (&__p == &__x); \
00097 if (value) \
00098 (p)->flags |= (flag); \
00099 else \
00100 (p)->flags &= ~(flag); \
00101 } while (0)
00102
00103
00104
00105
00106 #define ast_test_flag_nonstd(p,flag) ({ \
00107 ((p)->flags & (flag)); \
00108 })
00109
00110 #define ast_set_flag_nonstd(p,flag) do { \
00111 ((p)->flags |= (flag)); \
00112 } while(0)
00113
00114 #define ast_clear_flag_nonstd(p,flag) do { \
00115 ((p)->flags &= ~(flag)); \
00116 } while(0)
00117
00118 #define ast_copy_flags_nonstd(dest,src,flagz) do { \
00119 (dest)->flags &= ~(flagz); \
00120 (dest)->flags |= ((src)->flags & (flagz)); \
00121 } while (0)
00122
00123 #define ast_set2_flag_nonstd(p,value,flag) do { \
00124 if (value) \
00125 (p)->flags |= (flag); \
00126 else \
00127 (p)->flags &= ~(flag); \
00128 } while (0)
00129
00130 #define AST_FLAGS_ALL UINT_MAX
00131
00132 struct ast_flags {
00133 unsigned int flags;
00134 };
00135
00136 struct ast_hostent {
00137 struct hostent hp;
00138 char buf[1024];
00139 };
00140
00141 struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
00142
00143
00144
00145 void ast_md5_hash(char *output, char *input);
00146
00147 int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max);
00148 int ast_base64decode(unsigned char *dst, const char *src, int max);
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 char *ast_uri_encode(char *string, char *outbuf, int buflen, int doreserved);
00166
00167
00168
00169
00170 void ast_uri_decode(char *s);
00171
00172 static force_inline void ast_slinear_saturated_add(short *input, short *value)
00173 {
00174 int res;
00175
00176 res = (int) *input + *value;
00177 if (res > 32767)
00178 *input = 32767;
00179 else if (res < -32767)
00180 *input = -32767;
00181 else
00182 *input = (short) res;
00183 }
00184
00185 static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
00186 {
00187 int res;
00188
00189 res = (int) *input * *value;
00190 if (res > 32767)
00191 *input = 32767;
00192 else if (res < -32767)
00193 *input = -32767;
00194 else
00195 *input = (short) res;
00196 }
00197
00198 static force_inline void ast_slinear_saturated_divide(short *input, short *value)
00199 {
00200 *input /= *value;
00201 }
00202
00203 int test_for_thread_safety(void);
00204
00205 const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
00206
00207 #ifdef inet_ntoa
00208 #undef inet_ntoa
00209 #endif
00210 #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
00211
00212 int ast_utils_init(void);
00213 int ast_wait_for_input(int fd, int ms);
00214
00215
00216 static force_inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2)
00217 {
00218 return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
00219 || (sin1->sin_port != sin2->sin_port));
00220 }
00221
00222 #define AST_STACKSIZE 256 * 1024
00223 #define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0)
00224 int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
00225
00226
00227
00228
00229
00230
00231
00232 char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
00233
00234 #ifndef HAVE_GETLOADAVG
00235 int getloadavg(double *list, int nelem);
00236 #endif
00237
00238 #endif