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); \
118 extern uint64_t __unsigned_int_flags_dummy64;
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)
245 int ast_base64encode(
char *dst,
const unsigned char *src,
int srclen,
int max);
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)
371 extern const struct ast_flags ast_uri_http;
372 extern const struct ast_flags ast_uri_http_legacy;
373 extern const struct ast_flags ast_uri_sip_user;
416 int ast_xml_escape(
const char *
string,
char *outbuf,
size_t buflen);
450 static force_inline
void ast_slinear_saturated_add(
short *input,
short *value)
454 res = (int) *input + *value;
457 else if (res < -32768)
460 *input = (short) res;
463 static force_inline
void ast_slinear_saturated_subtract(
short *input,
short *value)
467 res = (int) *input - *value;
470 else if (res < -32768)
473 *input = (short) res;
476 static force_inline
void ast_slinear_saturated_multiply(
short *input,
short *value)
480 res = (int) *input * *value;
483 else if (res < -32768)
486 *input = (short) res;
489 static force_inline
void ast_slinear_saturated_multiply_float(
short *input,
float *value)
493 res = (float) *input * *value;
496 else if (res < -32768)
499 *input = (short) res;
502 static force_inline
void ast_slinear_saturated_divide(
short *input,
short *value)
507 static force_inline
void ast_slinear_saturated_divide_float(
short *input,
float *value)
509 float res = (float) *input / *value;
512 else if (res < -32768)
515 *input = (short) res;
521 #define localtime_r __dont_use_localtime_r_use_ast_localtime_instead__
523 int ast_utils_init(
void);
524 int ast_wait_for_input(
int fd,
int ms);
525 int ast_wait_for_output(
int fd,
int ms);
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)
569 int ast_background_stacksize(
void);
571 #define AST_BACKGROUND_STACKSIZE ast_background_stacksize()
573 void ast_register_thread(
char *name);
574 void ast_unregister_thread(
void *
id);
576 int ast_pthread_create_stack(pthread_t *
thread, pthread_attr_t *attr,
void *(*start_routine)(
void *),
577 void *data,
size_t stacksize,
const char *file,
const char *caller,
578 int line,
const char *start_fn);
580 int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr,
void*(*start_routine)(
void *),
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)
618 long int ast_random(
void);
624 #define ast_random_double() (((double)ast_random()) / RAND_MAX)
649 int ast_mkdir(
const char *path,
int mode);
664 int 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
720 void DO_CRASH_NORETURN __ast_assert_failed(
int condition,
const char *condition_str,
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__; \
732 static 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)) {
735 __ast_assert_failed(condition, condition_str, file, line,
function);
739 #define ast_assert(a)
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))
814 unsigned char eid[6];
815 } __attribute__((__packed__));
881 char *
ast_utils_which(
const char *binary,
char *fullpath,
size_t fullpath_size);
930 #if defined(__clang__)
931 typedef void (^_raii_cleanup_block_t)(void);
932 static 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."
964 char *
ast_crypt(
const char *key,
const char *salt);
1021 enum ast_fd_flag_operation {
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__)
1058 int __ast_fd_set_flags(
int fd,
int flags,
enum ast_fd_flag_operation op,
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)
void ast_uri_decode(char *s, struct ast_flags spec)
Decode URI, URN, URL (overwrite string)
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_base64_encode_file(FILE *inputfile, FILE *outputfile, const char *endl)
Performs a base 64 encode algorithm on the contents of a File.
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.
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.
char * ast_process_quotes_and_slashes(char *start, char find, char replace_with)
Process a string to find and replace characters.
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.
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.
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.
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.
int ast_str_to_eid(struct ast_eid *eid, const char *s)
Convert a string into an EID.
char * ast_base64url_encode_string(const char *src)
Encode string in base64 URL.
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...
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.
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.
int ast_check_command_in_path(const char *cmd)
Test for the presence of an executable command in $PATH.
Support for logging to various files, console and syslog Configuration in file logger.conf.
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.
Structure used to handle boolean flags.
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_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.
int ast_parse_digest(const char *digest, struct ast_http_digest *d, int request, int pedantic)
Parse digest authorization header.
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.
void ast_md5_hash(char *output, const char *input)
Produces MD5 hash based on input string.
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
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.