2004-05-03 04:31:11 +00:00
|
|
|
/*
|
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
|
*
|
|
|
|
|
* Utility functions
|
|
|
|
|
*
|
2005-01-21 07:06:25 +00:00
|
|
|
* Copyright (C) 2004 - 2005, Digium, Inc.
|
2004-05-03 04:31:11 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
|
* the GNU General Public License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _ASTERISK_UTIL_H
|
|
|
|
|
#define _ASTERISK_UTIL_H
|
|
|
|
|
|
2004-08-08 17:15:02 +00:00
|
|
|
#include <netinet/in.h>
|
2004-05-09 08:22:15 +00:00
|
|
|
#include <netdb.h>
|
2004-08-08 17:15:02 +00:00
|
|
|
#include <pthread.h>
|
2004-10-17 22:13:05 +00:00
|
|
|
#include <asterisk/lock.h>
|
2004-12-23 11:32:22 +00:00
|
|
|
#include <limits.h>
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
/* 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.
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
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:
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
warning: comparison of distinct pointer types lacks cast
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
will be generated.
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
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)
|
2004-12-16 03:15:20 +00:00
|
|
|
|
2005-01-10 14:46:59 +00:00
|
|
|
/* 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)
|
|
|
|
|
|
2004-12-23 11:32:22 +00:00
|
|
|
#define AST_FLAGS_ALL UINT_MAX
|
|
|
|
|
|
2005-01-08 19:00:46 +00:00
|
|
|
struct ast_flags {
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
};
|
|
|
|
|
|
2004-05-03 04:31:11 +00:00
|
|
|
static inline int ast_strlen_zero(const char *s)
|
|
|
|
|
{
|
|
|
|
|
return (*s == '\0');
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-09 08:22:15 +00:00
|
|
|
struct ast_hostent {
|
|
|
|
|
struct hostent hp;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
};
|
|
|
|
|
|
2004-10-05 06:46:11 +00:00
|
|
|
extern char *ast_strip(char *buf);
|
2004-05-09 08:22:15 +00:00
|
|
|
extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
|
2005-02-18 05:29:31 +00:00
|
|
|
/* ast_md5_hash: Produces MD5 hash based on input string */
|
|
|
|
|
extern void ast_md5_hash(char *output, char *input);
|
2004-06-25 03:59:07 +00:00
|
|
|
extern int ast_base64encode(char *dst, unsigned char *src, int srclen, int max);
|
|
|
|
|
extern int ast_base64decode(unsigned char *dst, char *src, int max);
|
2004-05-09 08:22:15 +00:00
|
|
|
|
2004-06-22 17:42:14 +00:00
|
|
|
extern int test_for_thread_safety(void);
|
2005-02-18 05:29:31 +00:00
|
|
|
|
2004-06-29 12:56:46 +00:00
|
|
|
extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
|
2004-06-25 03:59:07 +00:00
|
|
|
extern int ast_utils_init(void);
|
2005-03-05 03:20:55 +00:00
|
|
|
extern int ast_wait_for_input(int fd, int ms);
|
2004-06-22 17:42:14 +00:00
|
|
|
|
2005-01-20 04:53:17 +00:00
|
|
|
/* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
|
|
|
|
|
|
|
|
|
|
struct ast_realloca {
|
|
|
|
|
char *ptr;
|
|
|
|
|
int alloclen;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define ast_restrdupa(ra, s) \
|
|
|
|
|
({ \
|
|
|
|
|
if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \
|
|
|
|
|
strcpy((ra)->ptr, s); \
|
|
|
|
|
} else { \
|
|
|
|
|
(ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \
|
|
|
|
|
if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \
|
|
|
|
|
} \
|
|
|
|
|
(ra)->ptr; \
|
|
|
|
|
})
|
|
|
|
|
|
2004-06-29 17:54:25 +00:00
|
|
|
#ifdef inet_ntoa
|
|
|
|
|
#undef inet_ntoa
|
|
|
|
|
#endif
|
2004-06-29 12:56:46 +00:00
|
|
|
#define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
|
|
|
|
|
|
2005-03-04 22:37:25 +00:00
|
|
|
#define AST_STACKSIZE 256 * 1024
|
2005-03-04 00:01:41 +00:00
|
|
|
#define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0)
|
2004-10-17 22:13:05 +00:00
|
|
|
#ifdef __linux__
|
2004-08-27 04:21:09 +00:00
|
|
|
#define ast_strcasestr strcasestr
|
2004-08-08 17:15:02 +00:00
|
|
|
#else
|
2005-01-26 02:39:22 +00:00
|
|
|
extern char *ast_strcasestr(const char *, const char *);
|
2004-10-17 22:13:05 +00:00
|
|
|
#endif /* __linux__ */
|
2005-03-04 00:01:41 +00:00
|
|
|
extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
|
2004-05-03 04:31:11 +00:00
|
|
|
#endif
|