/* * Asterisk -- A telephony toolkit for Linux. * * Utility functions * * Copyright (C) 2004 - 2005, Digium, Inc. * * This program is free software, distributed under the terms of * the GNU General Public License */ #ifndef _ASTERISK_UTILS_H #define _ASTERISK_UTILS_H #ifdef SOLARIS #include #endif #include #include /* we want to override inet_ntoa */ #include #include #include "asterisk/lock.h" #include "asterisk/time.h" #include "asterisk/strings.h" /* Note: It is very important to use only unsigned variables to hold bit flags, as otherwise you can fall prey to the compiler's sign-extension antics if you try to use the top two bits in your variable. The flag macros below use a set of compiler tricks to verify that the caller is using an "unsigned int" variable to hold the flags, and nothing else. If the caller uses any other type of variable, a warning message similar to this: warning: comparison of distinct pointer types lacks cast will be generated. The "dummy" variable below is used to make these comparisons. Also note that at -O2 or above, this type-safety checking does _not_ produce any additional object code at all. */ extern unsigned int __unsigned_int_flags_dummy; #define ast_test_flag(p,flag) ({ \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags & (flag)); \ }) #define ast_set_flag(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags |= (flag)); \ } while(0) #define ast_clear_flag(p,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ ((p)->flags &= ~(flag)); \ } while(0) #define ast_copy_flags(dest,src,flagz) do { \ typeof ((dest)->flags) __d = (dest)->flags; \ typeof ((src)->flags) __s = (src)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__d == &__x); \ (void) (&__s == &__x); \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0) #define ast_set2_flag(p,value,flag) do { \ typeof ((p)->flags) __p = (p)->flags; \ typeof (__unsigned_int_flags_dummy) __x = 0; \ (void) (&__p == &__x); \ if (value) \ (p)->flags |= (flag); \ else \ (p)->flags &= ~(flag); \ } while (0) /* Non-type checking variations for non-unsigned int flags. You should only use non-unsigned int flags where required by protocol etc and if you know what you're doing :) */ #define ast_test_flag_nonstd(p,flag) ({ \ ((p)->flags & (flag)); \ }) #define ast_set_flag_nonstd(p,flag) do { \ ((p)->flags |= (flag)); \ } while(0) #define ast_clear_flag_nonstd(p,flag) do { \ ((p)->flags &= ~(flag)); \ } while(0) #define ast_copy_flags_nonstd(dest,src,flagz) do { \ (dest)->flags &= ~(flagz); \ (dest)->flags |= ((src)->flags & (flagz)); \ } while (0) #define ast_set2_flag_nonstd(p,value,flag) do { \ if (value) \ (p)->flags |= (flag); \ else \ (p)->flags &= ~(flag); \ } while (0) #define AST_FLAGS_ALL UINT_MAX struct ast_flags { unsigned int flags; }; struct ast_hostent { struct hostent hp; char buf[1024]; }; extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp); /* ast_md5_hash \brief Produces MD5 hash based on input string */ extern void ast_md5_hash(char *output, char *input); extern int ast_base64encode(char *dst, unsigned char *src, int srclen, int max); extern int ast_base64decode(unsigned char *dst, char *src, int max); /*! ast_uri_encode \brief Turn text string to URI-encoded %XX version At this point, we're converting from ISO-8859-x (8-bit), not UTF8 as in the SIP protocol spec If doreserved == 1 we will convert reserved characters also. RFC 2396, section 2.4 outbuf needs to have more memory allocated than the instring to have room for the expansion. Every char that is converted is replaced by three ASCII characters. \param string String to be converted \param outbuf Resulting encoded string \param buflen Size of output buffer \param doreserved Convert reserved characters */ char *ast_uri_encode(char *string, char *outbuf, int buflen, int doreserved); /*! \brief Decode URI, URN, URL (overwrite string) \param s String to be decoded */ void ast_uri_decode(char *s); extern int test_for_thread_safety(void); extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia); #ifdef inet_ntoa #undef inet_ntoa #endif #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__ extern int ast_utils_init(void); extern int ast_wait_for_input(int fd, int ms); /*! Compares the source address and port of two sockaddr_in */ static inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2) { return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) || (sin1->sin_port != sin2->sin_port)); } #define AST_STACKSIZE 256 * 1024 #define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0) extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize); #endif /* _ASTERISK_UTILS_H */