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);}) 230 #define SWAP(a,b) do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 339 #define AST_URI_ALPHANUM (1 << 0) 340 #define AST_URI_MARK (1 << 1) 341 #define AST_URI_UNRESERVED (AST_URI_ALPHANUM | AST_URI_MARK) 342 #define AST_URI_LEGACY_SPACE (1 << 2) 344 #define AST_URI_SIP_USER_UNRESERVED (1 << 20) 391 int ast_xml_escape(
const char *
string,
char *outbuf,
size_t buflen);
429 res = (int) *input + *value;
432 else if (res < -32768)
435 *input = (short) res;
442 res = (int) *input - *value;
445 else if (res < -32768)
448 *input = (short) res;
455 res = (int) *input * *value;
458 else if (res < -32768)
461 *input = (short) res;
468 res = (float) *input * *value;
471 else if (res < -32768)
474 *input = (short) res;
484 float res = (float) *input / *value;
487 else if (res < -32768)
490 *input = (short) res;
496 #define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__ 536 #if defined(PTHREAD_STACK_MIN) 537 # define AST_STACKSIZE MAX((((sizeof(void *) * 8 * 8) - 16) * 1024), PTHREAD_STACK_MIN) 538 # define AST_STACKSIZE_LOW MAX((((sizeof(void *) * 8 * 2) - 16) * 1024), PTHREAD_STACK_MIN) 540 # define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024) 541 # define AST_STACKSIZE_LOW (((sizeof(void *) * 8 * 2) - 16) * 1024) 546 #define AST_BACKGROUND_STACKSIZE ast_background_stacksize() 552 void *data,
size_t stacksize,
const char *
file,
const char *caller,
553 int line,
const char *start_fn);
556 void *data,
size_t stacksize,
const char *
file,
const char *caller,
557 int line,
const char *start_fn);
559 #define ast_pthread_create(a, b, c, d) \ 560 ast_pthread_create_stack(a, b, c, d, \ 561 0, __FILE__, __FUNCTION__, __LINE__, #c) 563 #define ast_pthread_create_detached(a, b, c, d) \ 564 ast_pthread_create_detached_stack(a, b, c, d, \ 565 0, __FILE__, __FUNCTION__, __LINE__, #c) 567 #define ast_pthread_create_background(a, b, c, d) \ 568 ast_pthread_create_stack(a, b, c, d, \ 569 AST_BACKGROUND_STACKSIZE, \ 570 __FILE__, __FUNCTION__, __LINE__, #c) 572 #define ast_pthread_create_detached_background(a, b, c, d) \ 573 ast_pthread_create_detached_stack(a, b, c, d, \ 574 AST_BACKGROUND_STACKSIZE, \ 575 __FILE__, __FUNCTION__, __LINE__, #c) 599 #define ast_random_double() (((double)ast_random()) / RAND_MAX) 624 int ast_mkdir(
const char *path,
int mode);
637 int ast_safe_mkdir(
const char *base_path,
const char *path,
int mode);
639 #define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a])) 649 #define IN_BOUNDS(v, min, max) ((v) >= (min)) && ((v) <= (max)) 658 #define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS((int) (v), 0, ARRAY_LEN(a) - 1) 686 #define DO_CRASH_NORETURN attribute_noreturn 688 #define DO_CRASH_NORETURN 692 const char *
file,
int line,
const char *
function);
695 #define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__) 696 #define ast_assert_return(a, ...) \ 698 if (__builtin_expect(!(a), 1)) { \ 699 _ast_assert(0, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ 700 return __VA_ARGS__; \ 705 if (__builtin_expect(!condition, 1)) {
710 #define ast_assert(a) 711 #define ast_assert_return(a, ...) \ 713 if (__builtin_expect(!(a), 1)) { \ 714 return __VA_ARGS__; \ 738 #define ast_alignof(type) __alignof__(type) 758 #define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type)) 781 #define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type)) 787 unsigned char eid[6];
788 } __attribute__((__packed__));
851 char *
ast_utils_which(
const char *binary,
char *fullpath,
size_t fullpath_size);
900 #if defined(__clang__) 901 typedef void (^_raii_cleanup_block_t)(void);
902 static inline void _raii_cleanup_block(_raii_cleanup_block_t *
b) { (*b)(); }
904 #define RAII_VAR(vartype, varname, initval, dtor) \ 905 __block vartype varname = initval; \ 906 _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = \ 907 ^{ {(void)dtor(varname);} }; 909 #elif defined(__GNUC__) 911 #define RAII_VAR(vartype, varname, initval, dtor) \ 912 auto void _dtor_ ## varname (vartype * v); \ 913 void _dtor_ ## varname (vartype * v) { dtor(*v); } \ 914 vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval) 917 #error "Cannot compile Asterisk: unknown and unsupported compiler." 934 char *
ast_crypt(
const char *key,
const char *salt);
1009 #define ast_fd_set_flags(fd, flags) \ 1010 __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_SET, __FILE__, __LINE__, __PRETTY_FUNCTION__) 1025 #define ast_fd_clear_flags(fd, flags) \ 1026 __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_CLEAR, __FILE__, __LINE__, __PRETTY_FUNCTION__) 1029 const char *
file,
int lineno,
const char *
function);
1042 #ifdef HAVE_SOCK_NONBLOCK 1043 # define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol)) 1060 # define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK) void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
int ast_background_stacksize(void)
void ast_register_thread(char *name)
static void force_inline _ast_assert(int condition, const char *condition_str, const char *file, int line, const char *function)
char * ast_base64url_decode_string(const char *src)
Decode string from base64 URL.
void ast_enable_packet_fragmentation(int sock)
Disable PMTU discovery on a socket.
Asterisk locking-related definitions:
String manipulation functions.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
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.
#define ast_pipe_nonblock(filedes)
Create a non-blocking pipe.
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.
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.
int ast_file_is_readable(const char *filename)
Test that a file exists and is readable by the effective user.
char * ast_utils_which(const char *binary, char *fullpath, size_t fullpath_size)
Resolve a binary to a full pathname.
#define ast_socket_nonblock(domain, type, protocol)
Create a non-blocking socket.
char * ast_uri_encode(const char *string, char *outbuf, int buflen, struct ast_flags spec)
Turn text string to URI-encoded XX version.
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_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.
char * ast_escape_semicolons(const char *string, char *outbuf, int buflen)
Escape semicolons found in a string.
int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op, const char *file, int lineno, const char *function)
char * ast_process_quotes_and_slashes(char *start, char find, char replace_with)
Process a string to find and replace characters.
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.
char * ast_base64decode_string(const char *src)
Same as ast_base64decode, but does the math for you and returns a decoded string. ...
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
char * ast_base64encode_string(const char *src)
Same as ast_base64encode, but does hte math for you and returns an encoded string.
Custom localtime functions for multiple timezones.
int ast_thread_is_user_interface(void)
Indicates whether the current thread is a user interface.
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
void ast_replace_subargument_delimiter(char *s)
Replace '^' in a string with ','.
int ast_base64url_decode(unsigned char *dst, const char *src, int max)
Decode data from base64 URL.
void ast_unregister_thread(void *id)
int ast_check_ipv6(void)
Test that an OS supports IPv6 Networking.
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_base64url_encode_string(const char *src)
Encode string in base64 URL.
void DO_CRASH_NORETURN __ast_assert_failed(int condition, const char *condition_str, const char *file, int line, const char *function)
void ast_sha1_hash_uint(uint8_t *digest, const char *input)
Produces SHA1 hash based on input string, stored in uint8_t array.
Wrapper for network related headers, masking differences between various operating systems...
static force_inline void ast_slinear_saturated_divide_float(short *input, float *value)
void ast_sha1_hash(char *output, const char *input)
Produces SHA1 hash based on input string.
const struct ast_flags ast_uri_http_legacy
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)
int ast_base64url_encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64 URL.
void DO_CRASH_NORETURN ast_do_crash(void)
Force a crash if DO_CRASH is defined.
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)
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.
static int request(void *obj)
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.
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)
#define DO_CRASH_NORETURN
static force_inline void ast_slinear_saturated_multiply_float(short *input, float *value)
int ast_crypt_validate(const char *key, const char *expected)
Asterisk wrapper around crypt(3) for validating passwords.
int ast_thread_user_interface_set(int is_user_interface)
Set the current thread's user interface status.
const struct ast_flags ast_uri_sip_user
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic)
Parse digest authorization header.
const struct ast_flags ast_uri_http
static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
int ast_wait_for_input(int fd, int ms)
int ast_compare_versions(const char *version1, const char *version2)
Compare 2 major.minor.patch.extra version strings.
int ast_eid_is_empty(const struct ast_eid *eid)
Check if EID is empty.
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
char * ast_crypt(const char *key, const char *salt)
Asterisk wrapper around crypt(3).
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
char * ast_crypt_encrypt(const char *key)
Asterisk wrapper around crypt(3) for encrypting passwords.
unsigned int __unsigned_int_flags_dummy
int ast_mkdir(const char *path, int mode)
Recursively create directory path.