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
231#define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);})
233#define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);})
235#define SWAP(a,b) do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
364#define AST_URI_ALPHANUM (1 << 0)
365#define AST_URI_MARK (1 << 1)
366#define AST_URI_UNRESERVED (AST_URI_ALPHANUM | AST_URI_MARK)
367#define AST_URI_LEGACY_SPACE (1 << 2)
369#define AST_URI_SIP_USER_UNRESERVED (1 << 20)
416int ast_xml_escape(
const char *
string,
char *outbuf,
size_t buflen);
457 else if (res < -32768)
460 *
input = (short) res;
470 else if (res < -32768)
473 *
input = (short) res;
483 else if (res < -32768)
486 *
input = (short) res;
496 else if (res < -32768)
499 *
input = (short) res;
512 else if (res < -32768)
515 *
input = (short) res;
521#define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
561#if defined(PTHREAD_STACK_MIN)
562# define AST_STACKSIZE MAX((((sizeof(void *) * 8 * 8) - 16) * 1024), PTHREAD_STACK_MIN)
563# define AST_STACKSIZE_LOW MAX((((sizeof(void *) * 8 * 2) - 16) * 1024), PTHREAD_STACK_MIN)
565# define AST_STACKSIZE (((sizeof(void *) * 8 * 8) - 16) * 1024)
566# define AST_STACKSIZE_LOW (((sizeof(void *) * 8 * 2) - 16) * 1024)
571#define AST_BACKGROUND_STACKSIZE ast_background_stacksize()
577 void *data,
size_t stacksize,
const char *
file,
const char *caller,
578 int line,
const char *start_fn);
581 void *data,
size_t stacksize,
const char *
file,
const char *caller,
582 int line,
const char *start_fn);
584#define ast_pthread_create(a, b, c, d) \
585 ast_pthread_create_stack(a, b, c, d, \
586 0, __FILE__, __FUNCTION__, __LINE__, #c)
588#define ast_pthread_create_detached(a, b, c, d) \
589 ast_pthread_create_detached_stack(a, b, c, d, \
590 0, __FILE__, __FUNCTION__, __LINE__, #c)
592#define ast_pthread_create_background(a, b, c, d) \
593 ast_pthread_create_stack(a, b, c, d, \
594 AST_BACKGROUND_STACKSIZE, \
595 __FILE__, __FUNCTION__, __LINE__, #c)
597#define ast_pthread_create_detached_background(a, b, c, d) \
598 ast_pthread_create_detached_stack(a, b, c, d, \
599 AST_BACKGROUND_STACKSIZE, \
600 __FILE__, __FUNCTION__, __LINE__, #c)
624#define ast_random_double() (((double)ast_random()) / RAND_MAX)
649int ast_mkdir(
const char *path,
int mode);
664int ast_safe_mkdir(
const char *base_path,
const char *path,
int mode);
666#define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a]))
677#define IN_BOUNDS(v, min, max) ((v) >= (min)) && ((v) <= (max))
687#define ARRAY_IN_BOUNDS(v, a) IN_BOUNDS((int) (v), 0, ARRAY_LEN(a) - 1)
715#define DO_CRASH_NORETURN attribute_noreturn
717#define DO_CRASH_NORETURN
721 const char *
file,
int line,
const char *function);
724#define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
725#define ast_assert_return(a, ...) \
727 if (__builtin_expect(!(a), 1)) { \
728 _ast_assert(0, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
729 return __VA_ARGS__; \
732static void force_inline _ast_assert(
int condition,
const char *condition_str,
const char *
file,
int line,
const char *function)
734 if (__builtin_expect(!condition, 1)) {
740#define ast_assert_return(a, ...) \
742 if (__builtin_expect(!(a), 1)) { \
743 return __VA_ARGS__; \
765#define ast_alignof(type) __alignof__(type)
785#define ast_align_for(offset, type) (((offset + __alignof__(type) - 1) / __alignof__(type)) * __alignof__(type))
808#define ast_make_room_for(offset, type) (((offset + (2 * __alignof__(type) - 1)) / __alignof__(type)) * __alignof__(type))
815} __attribute__((__packed__));
881char *
ast_utils_which(
const char *binary,
char *fullpath,
size_t fullpath_size);
930#if defined(__clang__)
931typedef void (^_raii_cleanup_block_t)(void);
932static inline void _raii_cleanup_block(_raii_cleanup_block_t *
b) { (*b)(); }
934#define RAII_VAR(vartype, varname, initval, dtor) \
935 __block vartype varname = initval; \
936 _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = \
937 ^{ {(void)dtor(varname);} };
939#elif defined(__GNUC__)
941#define RAII_VAR(vartype, varname, initval, dtor) \
942 auto void _dtor_ ## varname (vartype * v); \
943 void _dtor_ ## varname (vartype * v) { dtor(*v); } \
944 vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
947 #error "Cannot compile Asterisk: unknown and unsupported compiler."
964char *
ast_crypt(
const char *key,
const char *salt);
1039#define ast_fd_set_flags(fd, flags) \
1040 __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_SET, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1055#define ast_fd_clear_flags(fd, flags) \
1056 __ast_fd_set_flags((fd), (flags), AST_FD_FLAG_CLEAR, __FILE__, __LINE__, __PRETTY_FUNCTION__)
1059 const char *
file,
int lineno,
const char *function);
1072#ifdef HAVE_SOCK_NONBLOCK
1073# define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol))
1090# define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK)
static int input(yyscan_t yyscanner)
static int request(void *obj)
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
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_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.