23#ifndef _ASTERISK_UTILS_H 
   24#define _ASTERISK_UTILS_H 
   64#define ast_test_flag(p,flag)       ({ \ 
   65                    typeof ((p)->flags) __p = (p)->flags; \ 
   66                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
   67                    (void) (&__p == &__x); \ 
   68                    ((p)->flags & (flag)); \ 
 
   71#define ast_set_flag(p,flag)        do { \ 
   72                    typeof ((p)->flags) __p = (p)->flags; \ 
   73                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
   74                    (void) (&__p == &__x); \ 
   75                    ((p)->flags |= (flag)); \ 
 
   78#define ast_clear_flag(p,flag)      do { \ 
   79                    typeof ((p)->flags) __p = (p)->flags; \ 
   80                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
   81                    (void) (&__p == &__x); \ 
   82                    ((p)->flags &= ~(flag)); \ 
 
   85#define ast_copy_flags(dest,src,flagz)  do { \ 
   86                    typeof ((dest)->flags) __d = (dest)->flags; \ 
   87                    typeof ((src)->flags) __s = (src)->flags; \ 
   88                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
   89                    (void) (&__d == &__x); \ 
   90                    (void) (&__s == &__x); \ 
   91                    (dest)->flags &= ~(flagz); \ 
   92                    (dest)->flags |= ((src)->flags & (flagz)); \ 
 
   95#define ast_set2_flag(p,value,flag) do { \ 
   96                    typeof ((p)->flags) __p = (p)->flags; \ 
   97                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
   98                    (void) (&__p == &__x); \ 
  100                        (p)->flags |= (flag); \ 
  102                        (p)->flags &= ~(flag); \ 
 
  105#define ast_set_flags_to(p,flag,value)  do { \ 
  106                    typeof ((p)->flags) __p = (p)->flags; \ 
  107                    typeof (__unsigned_int_flags_dummy) __x = 0; \ 
  108                    (void) (&__p == &__x); \ 
  109                    (p)->flags &= ~(flag); \ 
  110                    (p)->flags |= (value); \ 
 
  130#if defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN) 
  131#define SWAP64_32(flags) (((uint64_t)flags << 32) | ((uint64_t)flags >> 32)) 
  132#elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN) 
  133#define SWAP64_32(flags) (flags) 
  135#error "Endianness not known - endian.h broken?" 
  140#define ast_test_flag64(p,flag)         ({ \ 
  141                    typeof ((p)->flags) __p = (p)->flags; \ 
  142                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  143                    (void) (&__p == &__x); \ 
  144                    ((p)->flags & SWAP64_32(flag)); \ 
 
  147#define ast_set_flag64(p,flag)      do { \ 
  148                    typeof ((p)->flags) __p = (p)->flags; \ 
  149                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  150                    (void) (&__p == &__x); \ 
  151                    ((p)->flags |= SWAP64_32(flag)); \ 
 
  154#define ast_clear_flag64(p,flag)        do { \ 
  155                    typeof ((p)->flags) __p = (p)->flags; \ 
  156                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  157                    (void) (&__p == &__x); \ 
  158                    ((p)->flags &= ~SWAP64_32(flag)); \ 
 
  161#define ast_copy_flags64(dest,src,flagz)    do { \ 
  162                    typeof ((dest)->flags) __d = (dest)->flags; \ 
  163                    typeof ((src)->flags) __s = (src)->flags; \ 
  164                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  165                    (void) (&__d == &__x); \ 
  166                    (void) (&__s == &__x); \ 
  167                    (dest)->flags &= ~SWAP64_32(flagz); \ 
  168                    (dest)->flags |= ((src)->flags & SWAP64_32(flagz)); \ 
 
  171#define ast_set2_flag64(p,value,flag)   do { \ 
  172                    typeof ((p)->flags) __p = (p)->flags; \ 
  173                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  174                    (void) (&__p == &__x); \ 
  176                        (p)->flags |= SWAP64_32(flag); \ 
  178                        (p)->flags &= ~SWAP64_32(flag); \ 
 
  181#define ast_set_flags_to64(p,flag,value)    do { \ 
  182                    typeof ((p)->flags) __p = (p)->flags; \ 
  183                    typeof (__unsigned_int_flags_dummy64) __x = 0; \ 
  184                    (void) (&__p == &__x); \ 
  185                    (p)->flags &= ~SWAP64_32(flag); \ 
  186                    (p)->flags |= SWAP64_32(value); \ 
 
  189#define AST_FLAGS64_ALL ULONG_MAX 
  194#define ast_test_flag_nonstd(p,flag) \ 
  195                    ((p)->flags & (flag)) 
 
  197#define ast_set_flag_nonstd(p,flag)         do { \ 
  198                    ((p)->flags |= (flag)); \ 
 
  201#define ast_clear_flag_nonstd(p,flag)       do { \ 
  202                    ((p)->flags &= ~(flag)); \ 
 
  205#define ast_copy_flags_nonstd(dest,src,flagz)   do { \ 
  206                    (dest)->flags &= ~(flagz); \ 
  207                    (dest)->flags |= ((src)->flags & (flagz)); \ 
 
  210#define ast_set2_flag_nonstd(p,value,flag)  do { \ 
  212                        (p)->flags |= (flag); \ 
  214                        (p)->flags &= ~(flag); \ 
 
  217#define AST_FLAGS_ALL UINT_MAX 
  252#define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);}) 
  254#define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);}) 
  256#define SWAP(a,b) do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 
  385#define AST_URI_ALPHANUM     (1 << 0) 
  386#define AST_URI_MARK         (1 << 1) 
  387#define AST_URI_UNRESERVED   (AST_URI_ALPHANUM | AST_URI_MARK) 
  388#define AST_URI_LEGACY_SPACE (1 << 2) 
  390#define AST_URI_SIP_USER_UNRESERVED (1 << 20) 
  450int ast_xml_escape(
const char *
string, 
char *outbuf, 
size_t buflen);
 
  488    res = (int) *input + *
value;
 
  491    else if (res < -32768)
 
  494        *input = (short) res;
 
 
  501    res = (int) *input - *
value;
 
  504    else if (res < -32768)
 
  507        *input = (short) res;
 
 
  514    res = (int) *input * *
value;
 
  517    else if (res < -32768)
 
  520        *input = (short) res;
 
 
  527    res = (float) *input * *
value;
 
  530    else if (res < -32768)
 
  533        *input = (short) (res + 0.5);
 
  535        *input = (short) (res - 0.5);
 
 
  546    float res = (float) *input / *
value;
 
  549    else if (res < -32768)
 
  552        *input = (short) (res + 0.5);
 
  554        *input = (short) (res - 0.5);
 
 
  561#define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__ 
  601#if defined(PTHREAD_STACK_MIN) 
  602# define AST_STACKSIZE     MAX((((sizeof(void *) * 8 * 8) - 16) * 1024), PTHREAD_STACK_MIN) 
  603# define AST_STACKSIZE_LOW MAX((((sizeof(void *) * 8 * 2) - 16) * 1024), PTHREAD_STACK_MIN) 
  605# define AST_STACKSIZE     (((sizeof(void *) * 8 * 8) - 16) * 1024) 
  606# define AST_STACKSIZE_LOW (((sizeof(void *) * 8 * 2) - 16) * 1024) 
  611#define AST_BACKGROUND_STACKSIZE ast_background_stacksize() 
  617                 void *data, 
size_t stacksize, 
const char *file, 
const char *caller,
 
  618                 int line, 
const char *start_fn);
 
  621                 void *data, 
size_t stacksize, 
const char *file, 
const char *caller,
 
  622                 int line, 
const char *start_fn);
 
  624#define ast_pthread_create(a, b, c, d)              \ 
  625    ast_pthread_create_stack(a, b, c, d,            \ 
  626        0, __FILE__, __FUNCTION__, __LINE__, #c) 
 
  628#define ast_pthread_create_detached(a, b, c, d)         \ 
  629    ast_pthread_create_detached_stack(a, b, c, d,       \ 
  630        0, __FILE__, __FUNCTION__, __LINE__, #c) 
 
  632#define ast_pthread_create_background(a, b, c, d)       \ 
  633    ast_pthread_create_stack(a, b, c, d,            \ 
  634        AST_BACKGROUND_STACKSIZE,           \ 
  635        __FILE__, __FUNCTION__, __LINE__, #c) 
 
  637#define ast_pthread_create_detached_background(a, b, c, d)  \ 
  638    ast_pthread_create_detached_stack(a, b, c, d,       \ 
  639        AST_BACKGROUND_STACKSIZE,           \ 
  640        __FILE__, __FUNCTION__, __LINE__, #c) 
 
  664#define ast_random_double() (((double)ast_random()) / RAND_MAX) 
  689int ast_mkdir(
const char *path, 
int mode);
 
  704int ast_safe_mkdir(
const char *base_path, 
const char *path, 
int mode);
 
  706#define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a])) 
  717#define IN_BOUNDS(v, min, max) ((v) >= (min)) && ((v) <= (max)) 
  727#define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS((int) (v), 0, ARRAY_LEN(a) - 1) 
  755#define DO_CRASH_NORETURN attribute_noreturn 
  757#define DO_CRASH_NORETURN 
  761    const char *file, 
int line, 
const char *function);
 
  764#define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__) 
  765#define ast_assert_return(a, ...) \ 
  767    if (__builtin_expect(!(a), 1)) { \ 
  768        _ast_assert(0, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ 
  769        return __VA_ARGS__; \ 
  772static void force_inline _ast_assert(
int condition, 
const char *condition_str, 
const char *file, 
int line, 
const char *function)
 
  774    if (__builtin_expect(!condition, 1)) {
 
  780#define ast_assert_return(a, ...) \ 
  782    if (__builtin_expect(!(a), 1)) { \ 
  783        return __VA_ARGS__; \ 
 
  805#define ast_alignof(type) __alignof__(type) 
  825#define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type)) 
  848#define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type)) 
  855} __attribute__((__packed__));
 
 
  921char *
ast_utils_which(
const char *binary, 
char *fullpath, 
size_t fullpath_size);
 
  970#if defined(__clang__) 
  971typedef void (^_raii_cleanup_block_t)(void);
 
  972static inline void _raii_cleanup_block(_raii_cleanup_block_t *
b) { (*b)(); }
 
  974#define RAII_VAR(vartype, varname, initval, dtor)                                                              \ 
  975    __block vartype varname = initval;                                                                         \ 
  976    _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) =     \ 
  977        ^{ {(void)dtor(varname);} }; 
  979#elif defined(__GNUC__) 
  981#define RAII_VAR(vartype, varname, initval, dtor)                              \ 
  982    auto void _dtor_ ## varname (vartype * v);                                 \ 
  983    void _dtor_ ## varname (vartype * v) { dtor(*v); }                         \ 
  984    vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval) 
 
  987    #error "Cannot compile Asterisk: unknown and unsupported compiler." 
 1004char *
ast_crypt(
const char *key, 
const char *salt);
 
 1079#define ast_fd_set_flags(fd, flags) \ 
 1080    __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_SET, __FILE__, __LINE__, __PRETTY_FUNCTION__) 
 
 1095#define ast_fd_clear_flags(fd, flags) \ 
 1096    __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_CLEAR, __FILE__, __LINE__, __PRETTY_FUNCTION__) 
 
 1099    const char *file, 
int lineno, 
const char *function);
 
 1112#ifdef HAVE_SOCK_NONBLOCK 
 1113# define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol)) 
 1130# define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK) 
static int request(void *obj)
Asterisk architecture endianess compatibility definitions.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Support for logging to various files, console and syslog Configuration in file logger....
Custom localtime functions for multiple timezones.
Asterisk locking-related definitions:
Wrapper for network related headers, masking differences between various operating systems....
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define AST_STRING_FIELD(name)
Declare a string field.
String manipulation functions.
An Entity ID is essentially a MAC address, brief and unique.
Structure used to handle a large number of boolean flags == used only in app_dial?
Structure used to handle boolean flags.
const ast_string_field response
const ast_string_field uri
const ast_string_field domain
const ast_string_field opaque
const ast_string_field cnonce
const ast_string_field nonce
const ast_string_field username
const ast_string_field realm
const ast_string_field nc
Time-related functions and macros.
int ast_thread_is_user_interface(void)
Indicates whether the current thread is a user interface.
void ast_unescape_quoted(char *quote_str)
Unescape quotes in a string.
static force_inline void ast_slinear_saturated_divide_float(short *input, float *value)
static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
int ast_base64url_encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
Same as ast_base64encode_full but for base64 URL.
int ast_base64_encode_file(FILE *inputfile, FILE *outputfile, const char *endl)
Performs a base 64 encode algorithm on the contents of a File.
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
int ast_check_command_in_path(const char *cmd)
Test for the presence of an executable command in $PATH.
int ast_safe_mkdir(const char *base_path, const char *path, int mode)
Recursively create directory path, but only if it resolves within the given base_path.
unsigned int __unsigned_int_flags_dummy
char * ast_base64url_encode_string(const char *src)
Encode string in base64 URL.
int ast_file_is_readable(const char *filename)
Test that a file exists and is readable by the effective user.
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.
#define DO_CRASH_NORETURN
int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op, const char *file, int lineno, const char *function)
int ast_background_stacksize(void)
static force_inline void ast_slinear_saturated_multiply_float(short *input, float *value)
char * ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
Resolve a binary to a full pathname.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
#define ast_socket_nonblock(domain, type, protocol)
Create a non-blocking socket.
void ast_set_default_eid(struct ast_eid *eid)
Fill in an ast_eid with the default eid of this machine.
int ast_thread_user_interface_set(int is_user_interface)
Set the current thread's user interface status.
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
char * ast_escape_quoted(const char *string, char *outbuf, int buflen)
Escape characters found in a quoted string.
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
int ast_wait_for_input(int fd, int ms)
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic)
Parse digest authorization header.
char * ast_base64decode_string(const char *src)
Same as ast_base64decode, but does the math for you and returns a decoded string.
int ast_crypt_validate(const char *key, const char *expected)
Asterisk wrapper around crypt(3) for validating passwords.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
char * ast_base64encode_string(const char *src)
Same as ast_base64encode, but does hte math for you and returns an encoded string.
uint64_t __unsigned_int_flags_dummy64
Swap the upper and lower 32 bits of a big-endian 64-bit integer.
int ast_get_tid(void)
Get current thread ID.
long int ast_random(void)
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
const struct ast_flags ast_uri_http
int ast_wait_for_output(int fd, int ms)
char * ast_crypt(const char *key, const char *salt)
Asterisk wrapper around crypt(3).
static force_inline void ast_slinear_saturated_add(short *input, short *value)
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
int ast_xml_escape(const char *string, char *outbuf, size_t buflen)
Escape reserved characters for use in XML.
int ast_base64url_decode(unsigned char *dst, const char *src, int max)
Decode data from base64 URL.
int ast_compare_versions(const char *version1, const char *version2)
Compare 2 major.minor.patch.extra version strings.
void ast_register_thread(char *name)
int ast_eid_is_empty(const struct ast_eid *eid)
Check if EID is empty.
int ast_base64url_encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64 URL.
void ast_unregister_thread(void *id)
void DO_CRASH_NORETURN ast_do_crash(void)
Force a crash if DO_CRASH is defined.
char * ast_process_quotes_and_slashes(char *start, char find, char replace_with)
Process a string to find and replace characters.
#define ast_pipe_nonblock(filedes)
Create a non-blocking pipe.
char * ast_escape_semicolons(const char *string, char *outbuf, int buflen)
Escape semicolons found in a string.
int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks)
encode text to BASE64 coding
void ast_sha1_hash_uint(uint8_t *digest, const char *input)
Produces SHA1 hash based on input string, stored in uint8_t array.
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.
int ast_uri_verify_encoded(const char *string)
Verify if a string is valid as a URI component.
int ast_check_ipv6(void)
Test that an OS supports IPv6 Networking.
char * ast_base64url_decode_string(const char *src)
Decode string from base64 URL.
struct ast_eid ast_eid_default
Global EID.
int ast_str_to_eid(struct ast_eid *eid, const char *s)
Convert a string into an EID.
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
int ast_base64_encode_file_path(const char *filename, FILE *outputfile, const char *endl)
Performs a base 64 encode algorithm on the contents of a File.
struct hostent * ast_gethostbyname(const char *host, struct ast_hostent *hp)
Thread-safe gethostbyname function to use in Asterisk.
void ast_replace_subargument_delimiter(char *s)
Replace '^' in a string with ','.
void DO_CRASH_NORETURN __ast_assert_failed(int condition, const char *condition_str, const char *file, int line, const char *function)
const struct ast_flags ast_uri_sip_user
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
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)
void ast_sha1_hash(char *output, const char *input)
Produces SHA1 hash based on input string.
static force_inline void ast_slinear_saturated_divide(short *input, short *value)
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)
const struct ast_flags ast_uri_http_legacy
char * ast_crypt_encrypt(const char *key)
Asterisk wrapper around crypt(3) for encrypting passwords.