23 #ifndef _ASTERISK_UTILS_H
24 #define _ASTERISK_UTILS_H
63 #define ast_test_flag(p,flag) ({ \
64 typeof ((p)->flags) __p = (p)->flags; \
65 typeof (__unsigned_int_flags_dummy) __x = 0; \
66 (void) (&__p == &__x); \
67 ((p)->flags & (flag)); \
70 #define ast_set_flag(p,flag) do { \
71 typeof ((p)->flags) __p = (p)->flags; \
72 typeof (__unsigned_int_flags_dummy) __x = 0; \
73 (void) (&__p == &__x); \
74 ((p)->flags |= (flag)); \
77 #define ast_clear_flag(p,flag) do { \
78 typeof ((p)->flags) __p = (p)->flags; \
79 typeof (__unsigned_int_flags_dummy) __x = 0; \
80 (void) (&__p == &__x); \
81 ((p)->flags &= ~(flag)); \
84 #define ast_copy_flags(dest,src,flagz) do { \
85 typeof ((dest)->flags) __d = (dest)->flags; \
86 typeof ((src)->flags) __s = (src)->flags; \
87 typeof (__unsigned_int_flags_dummy) __x = 0; \
88 (void) (&__d == &__x); \
89 (void) (&__s == &__x); \
90 (dest)->flags &= ~(flagz); \
91 (dest)->flags |= ((src)->flags & (flagz)); \
94 #define ast_set2_flag(p,value,flag) do { \
95 typeof ((p)->flags) __p = (p)->flags; \
96 typeof (__unsigned_int_flags_dummy) __x = 0; \
97 (void) (&__p == &__x); \
99 (p)->flags |= (flag); \
101 (p)->flags &= ~(flag); \
104 #define ast_set_flags_to(p,flag,value) do { \
105 typeof ((p)->flags) __p = (p)->flags; \
106 typeof (__unsigned_int_flags_dummy) __x = 0; \
107 (void) (&__p == &__x); \
108 (p)->flags &= ~(flag); \
109 (p)->flags |= (value); \
120 #define ast_test_flag64(p,flag) ({ \
121 typeof ((p)->flags) __p = (p)->flags; \
122 typeof (__unsigned_int_flags_dummy64) __x = 0; \
123 (void) (&__p == &__x); \
124 ((p)->flags & (flag)); \
127 #define ast_set_flag64(p,flag) do { \
128 typeof ((p)->flags) __p = (p)->flags; \
129 typeof (__unsigned_int_flags_dummy64) __x = 0; \
130 (void) (&__p == &__x); \
131 ((p)->flags |= (flag)); \
134 #define ast_clear_flag64(p,flag) do { \
135 typeof ((p)->flags) __p = (p)->flags; \
136 typeof (__unsigned_int_flags_dummy64) __x = 0; \
137 (void) (&__p == &__x); \
138 ((p)->flags &= ~(flag)); \
141 #define ast_copy_flags64(dest,src,flagz) do { \
142 typeof ((dest)->flags) __d = (dest)->flags; \
143 typeof ((src)->flags) __s = (src)->flags; \
144 typeof (__unsigned_int_flags_dummy64) __x = 0; \
145 (void) (&__d == &__x); \
146 (void) (&__s == &__x); \
147 (dest)->flags &= ~(flagz); \
148 (dest)->flags |= ((src)->flags & (flagz)); \
151 #define ast_set2_flag64(p,value,flag) do { \
152 typeof ((p)->flags) __p = (p)->flags; \
153 typeof (__unsigned_int_flags_dummy64) __x = 0; \
154 (void) (&__p == &__x); \
156 (p)->flags |= (flag); \
158 (p)->flags &= ~(flag); \
161 #define ast_set_flags_to64(p,flag,value) do { \
162 typeof ((p)->flags) __p = (p)->flags; \
163 typeof (__unsigned_int_flags_dummy64) __x = 0; \
164 (void) (&__p == &__x); \
165 (p)->flags &= ~(flag); \
166 (p)->flags |= (value); \
173 #define ast_test_flag_nonstd(p,flag) \
174 ((p)->flags & (flag))
176 #define ast_set_flag_nonstd(p,flag) do { \
177 ((p)->flags |= (flag)); \
180 #define ast_clear_flag_nonstd(p,flag) do { \
181 ((p)->flags &= ~(flag)); \
184 #define ast_copy_flags_nonstd(dest,src,flagz) do { \
185 (dest)->flags &= ~(flagz); \
186 (dest)->flags |= ((src)->flags & (flagz)); \
189 #define ast_set2_flag_nonstd(p,value,flag) do { \
191 (p)->flags |= (flag); \
193 (p)->flags &= ~(flag); \
196 #define AST_FLAGS_ALL UINT_MAX
226 #define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);})
228 #define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);})
238 int ast_base64encode(
char *dst,
const unsigned char *src,
int srclen,
int max);
269 char *
ast_uri_encode(
const char *
string,
char *outbuf,
int buflen,
int do_special_char);
288 int ast_xml_escape(
const char *
string,
char *outbuf,
size_t buflen);
316 res = (int) *input + *value;
319 else if (res < -32768)
322 *input = (short) res;
329 res = (int) *input - *value;
332 else if (res < -32768)
335 *input = (short) res;
342 res = (int) *input * *value;
345 else if (res < -32768)
348 *input = (short) res;
359 #define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
399 #define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024)
401 #if defined(LOW_MEMORY)
402 #define AST_BACKGROUND_STACKSIZE (((sizeof(void *) * 8 * 2) - 16) * 1024)
404 #define AST_BACKGROUND_STACKSIZE AST_STACKSIZE
411 void *data,
size_t stacksize,
const char *file,
const char *caller,
412 int line,
const char *start_fn);
415 void *data,
size_t stacksize,
const char *file,
const char *caller,
416 int line,
const char *start_fn);
418 #define ast_pthread_create(a, b, c, d) \
419 ast_pthread_create_stack(a, b, c, d, \
420 0, __FILE__, __FUNCTION__, __LINE__, #c)
422 #define ast_pthread_create_detached(a, b, c, d) \
423 ast_pthread_create_detached_stack(a, b, c, d, \
424 0, __FILE__, __FUNCTION__, __LINE__, #c)
426 #define ast_pthread_create_background(a, b, c, d) \
427 ast_pthread_create_stack(a, b, c, d, \
428 AST_BACKGROUND_STACKSIZE, \
429 __FILE__, __FUNCTION__, __LINE__, #c)
431 #define ast_pthread_create_detached_background(a, b, c, d) \
432 ast_pthread_create_detached_stack(a, b, c, d, \
433 AST_BACKGROUND_STACKSIZE, \
434 __FILE__, __FUNCTION__, __LINE__, #c)
449 #ifndef __AST_DEBUG_MALLOC
450 #define ast_std_malloc malloc
451 #define ast_std_calloc calloc
452 #define ast_std_realloc realloc
453 #define ast_std_free free
461 #define ast_free free
462 #define ast_free_ptr ast_free
464 #define MALLOC_FAILURE_MSG \
465 ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
474 #define ast_malloc(len) \
475 _ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
498 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
505 if (!(p =
calloc(num, len)))
523 _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
533 #define ast_realloc(p, len) \
534 _ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
569 if (!(newstr =
strdup(str)))
598 if (!(newstr =
strndup(str, len)))
615 _ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)
617 int __attribute__((
format(printf, 5, 6)))
618 _ast_asprintf(
char **ret, const
char *file,
int lineno, const
char *func, const
char *fmt, ...);
628 #define ast_vasprintf(ret, fmt, ap) \
629 _ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))
632 __attribute__((
format(printf, 5, 0)))
633 int _ast_vasprintf(
char **ret,
const char *file,
int lineno,
const char *func,
const char *fmt, va_list ap),
637 if ((res =
vasprintf(ret, fmt, ap)) == -1)
655 #if !defined(ast_strdupa) && defined(__GNUC__)
663 #define ast_strdupa(s) \
666 const char *__old = (s); \
667 size_t __len = strlen(__old) + 1; \
668 char *__new = __builtin_alloca(__len); \
669 memcpy (__new, __old, __len); \
697 int ast_mkdir(
const char *path,
int mode);
699 #define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a]))
729 void __ast_assert_failed(
int condition,
const char *condition_str,
const char *file,
int line,
const char *
function);
730 #define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
731 static void force_inline _ast_assert(
int condition,
const char *condition_str,
const char *file,
int line,
const char *
function)
733 if (__builtin_expect(!condition, 1)) {
734 __ast_assert_failed(condition, condition_str, file, line,
function);
738 #define ast_assert(a)
760 #define ast_alignof(type) __alignof__(type)
780 #define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type))
803 #define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type))
810 } __attribute__((__packed__));
867 char *
ast_utils_which(
const char *binary,
char *fullpath,
size_t fullpath_size);
915 #define RAII_VAR(vartype, varname, initval, dtor) \
917 auto void _dtor_ ## varname (vartype * v); \
918 void _dtor_ ## varname (vartype * v) { dtor(*v); } \
919 vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
const ast_string_field cnonce
#define ast_strndup(str, len)
A wrapper for strndup()
int vasprintf(char **strp, const char *fmt, va_list ap)
void ast_register_thread(char *name)
void * _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func)
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Asterisk locking-related definitions:
String manipulation functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
int ast_careful_fwrite(FILE *f, int fd, const char *s, size_t len, int timeoutms)
Write data to a file stream with a timeout.
Time-related functions and macros.
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
Try to write string, but wait no more than ms milliseconds before timing out.
void ast_uri_decode(char *s)
Decode URI, URN, URL (overwrite string)
char * ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
Resolve a binary to a full pathname.
const ast_string_field opaque
const ast_string_field domain
int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn)
#define MALLOC_FAILURE_MSG
void ast_set_default_eid(struct ast_eid *eid)
Fill in an ast_eid with the default eid of this machine.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
const ast_string_field username
char * ast_uri_encode(const char *string, char *outbuf, int buflen, int do_special_char)
Turn text string to URI-encoded XX version.
char * ast_process_quotes_and_slashes(char *start, char find, char replace_with)
Process a string to find and replace characters.
#define ast_strdup(str)
A wrapper for strdup()
#define ast_calloc_cache(num, len)
A wrapper for calloc() for use in cache pools.
static int input(yyscan_t yyscanner)
int ast_xml_escape(const char *string, char *outbuf, size_t buflen)
Escape reserved characters for use in XML.
Structure used to handle a large number of boolean flags == used only in app_dial?
An Entity ID is essentially a MAC address, brief and unique.
int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap)
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
String fields in structures.
#define ast_calloc(num, len)
A wrapper for calloc()
Custom localtime functions for multiple timezones.
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
const ast_string_field uri
void ast_unregister_thread(void *id)
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
const ast_string_field response
int ast_get_tid(void)
Get current thread ID.
void ast_unescape_quoted(char *quote_str)
Unescape quotes in a string.
#define AST_STRING_FIELD(name)
Declare a string field.
long int ast_random(void)
static force_inline void ast_slinear_saturated_add(short *input, short *value)
int ast_str_to_eid(struct ast_eid *eid, const char *s)
Convert a string into an EID.
static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
char * _ast_strdup(const char *str, const char *file, int lineno, const char *func)
Wrapper for network related headers, masking differences between various operating systems...
void ast_sha1_hash(char *output, const char *input)
Produces SHA1 hash based on input string.
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field nc
char * ast_escape_quoted(const char *string, char *outbuf, int buflen)
Escape characters found in a quoted string.
static force_inline void ast_slinear_saturated_divide(short *input, short *value)
const ast_string_field realm
Structure used to handle boolean flags.
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct ast_eid ast_eid_default
Global EID.
void ast_do_crash(void)
Force a crash if DO_CRASH is defined.
char * _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func)
const ast_string_field nonce
struct hostent * ast_gethostbyname(const char *host, struct ast_hostent *hp)
Thread-safe gethostbyname function to use in Asterisk.
int ast_wait_for_output(int fd, int ms)
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic)
Parse digest authorization header.
static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
int ast_wait_for_input(int fd, int ms)
void * _ast_malloc(size_t len, const char *file, int lineno, const char *func)
int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn)
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
static struct hostent * hp
static snd_pcm_format_t format
char * strndup(const char *, size_t)
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
encode text to BASE64 coding
uint64_t __unsigned_int_flags_dummy64
unsigned int __unsigned_int_flags_dummy
int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt,...)
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
#define AST_INLINE_API(hdr, body)
void * _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func)