RTS API Documentation  1.10.11
Data Structures | Macros | Typedefs | Enumerations | Functions
switch_utils.h File Reference

Compatability and Helper Code. More...

#include <switch.h>
#include <math.h>
+ Include dependency graph for switch_utils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  switch_network_port_range
 
union  ip_t
 
struct  switch_http_request_s
 
struct  switch_cputime
 

Macros

#define SWITCH_URL_UNSAFE   "\r\n #%&+:;<=>?@[\\]^`{|}\""
 
#define MAX_NETWORK_PORTS   10
 
#define switch_goto_status(_status, _label)   status = _status; goto _label
 
#define switch_goto_int(_n, _i, _label)   _n = _i; goto _label
 
#define switch_samples_per_packet(rate, interval)   ((uint32_t)((float)rate / (1000.0f / (float)interval)))
 
#define SWITCH_SMAX   32767
 
#define SWITCH_SMIN   -32768
 
#define switch_normalize_to_16bit(n)   if (n > SWITCH_SMAX) n = SWITCH_SMAX; else if (n < SWITCH_SMIN) n = SWITCH_SMIN;
 
#define switch_codec2str(codec, buf, len)
 
#define zstr(x)   _zstr(x)
 
#define switch_strlen_zero(x)   zstr(x)
 
#define switch_strlen_zero_buf(x)   zstr_buf(x)
 
#define zstr_buf(s)   (*(s) == '\0')
 
#define zset(_a, _b)   if (!zstr(_b)) _a = _b
 
#define switch_arraylen(_a)   (sizeof(_a) / sizeof(_a[0]))
 
#define switch_split(_data, _delim, _array)   switch_separate_string(_data, _delim, _array, switch_arraylen(_array))
 
#define switch_split_cheap(_data, _delim, _array, _larray)   switch_separate_string_cheap(_data, _delim, (const char **)_array, _larray, switch_arraylen(_array))
 
#define switch_is_valid_rate(_tmp)   (_tmp == 8000 || _tmp == 12000 || _tmp == 16000 || _tmp == 24000 || _tmp == 32000 || _tmp == 11025 || _tmp == 22050 || _tmp == 44100 || _tmp == 48000)
 
#define switch_true_buf(expr)
 
#define SWITCH_STATUS_IS_BREAK(x)   (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)
 
#define switch_errno()   errno
 
#define is_dtmf(key)   ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119 || key == 70 || key == 102)
 determine if a character is a valid DTMF key More...
 
#define end_of(_s)   *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
 
#define end_of_p(_s)   (*_s == '\0' ? _s : _s + strlen(_s) - 1)
 
#define switch_test_flag(obj, flag)   ((obj)->flags & flag)
 Test for the existance of a flag on an arbitary object. More...
 
#define switch_set_flag(obj, flag)   (obj)->flags |= (flag)
 Set a flag on an arbitrary object. More...
 
#define switch_set_flag_locked(obj, flag)
 Set a flag on an arbitrary object while locked. More...
 
#define switch_clear_flag_locked(obj, flag)   switch_mutex_lock((obj)->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock((obj)->flag_mutex);
 Clear a flag on an arbitrary object. More...
 
#define switch_clear_flag(obj, flag)   (obj)->flags &= ~(flag)
 Clear a flag on an arbitrary object while locked. More...
 
#define switch_copy_flags(dest, src, flags)   (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))
 Copy flags from one arbitrary object to another. More...
 
#define switch_set_string(_dst, _src)   switch_copy_string(_dst, _src, sizeof(_dst))
 
#define switch_safe_free(it)   if (it) {free(it);it=NULL;}
 Free a pointer and set it to NULL unless it already is NULL. More...
 
#define switch_str_nil(s)   (s ? s : "")
 Make a null string a blank string instead. More...
 
#define switch_yield(ms)   switch_sleep(ms);
 Wait a desired number of microseconds and yield the CPU. More...
 
#define switch_time_from_sec(sec)   ((switch_time_t)(sec) * 1000000)
 
#define SWITCH_DECLARE_GLOBAL_STRING_FUNC(fname, vname)
 Declares a function designed to set a dynamic global string. More...
 
#define SWITCH_READ_ACCEPTABLE(status)   (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE)
 
#define switch_calc_video_fps(fpsP, fps)   switch_calc_fps(fpsP, fps, 90000)
 
#define switch_network_list_add_cidr(_list, _cidr_str, _ok)   switch_network_list_add_cidr_token(_list, _cidr_str, _ok, NULL)
 
#define switch_network_list_validate_ip(_list, _ip)   switch_network_list_validate_ip_token(_list, _ip, NULL);
 
#define switch_test_subnet(_ip, _net, _mask)   (_mask ? ((_net & _mask) == (_ip & _mask)) : _net ? _net == _ip : 1)
 
#define switch_malloc(ptr, len)   (void)(switch_assert(((ptr) = malloc((len)))),ptr)
 
#define switch_zmalloc(ptr, len)   (void)(switch_assert((ptr = calloc(1, (len)))),ptr)
 
#define switch_strdup(ptr, s)   (void)(switch_assert(((ptr) = strdup((s)))),ptr)
 
#define DUMP_EVENT(_e)   {char *event_str;switch_event_serialize(_e, &event_str, SWITCH_FALSE);switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DUMP\n%s\n", event_str);free(event_str);}
 
#define switch_inet_ntop   inet_ntop
 

Typedefs

typedef struct switch_network_port_range switch_network_port_range_t
 
typedef switch_network_port_range_tswitch_network_port_range_p
 
typedef struct switch_http_request_s switch_http_request_t
 

Enumerations

enum  switch_uri_flags { SWITCH_URI_NUMERIC_HOST = 1, SWITCH_URI_NUMERIC_PORT = 2, SWITCH_URI_NO_SCOPE = 4 }
 flags to be used with switch_build_uri() More...
 

Functions

static char * switch_get_hex_bytes (switch_byte_t *buf, switch_size_t datalen, char *new_buf, switch_size_t new_datalen)
 
static uint32_t switch_round_to_step (uint32_t num, uint32_t step)
 
static uint32_t switch_toupper (uint32_t eax)
 
static uint32_t switch_tolower (uint32_t eax)
 
static void switch_toupper_max (char *s)
 
static void switch_tolower_max (char *s)
 
int old_switch_toupper (int c)
 
int old_switch_tolower (int c)
 
int switch_isalnum (int c)
 
int switch_isalpha (int c)
 
int switch_iscntrl (int c)
 
int switch_isdigit (int c)
 
int switch_isgraph (int c)
 
int switch_islower (int c)
 
int switch_isprint (int c)
 
int switch_ispunct (int c)
 
int switch_isspace (int c)
 
int switch_isupper (int c)
 
int switch_isxdigit (int c)
 
switch_bool_t switch_testv6_subnet (ip_t _ip, ip_t _net, ip_t _mask)
 
char * switch_print_host (switch_sockaddr_t *addr, char *buf, switch_size_t len)
 
static _Check_return_ int _zstr (_In_opt_z_ const char *s)
 Test for NULL or zero length string. More...
 
static switch_bool_t switch_is_moh (const char *s)
 
static char * switch_strchr_strict (const char *in, char find, const char *allowed)
 
static int switch_string_has_escaped_data (const char *in)
 
switch_status_t switch_b64_encode (unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen)
 
switch_size_t switch_b64_decode (const char *in, char *out, switch_size_t olen)
 
char * switch_amp_encode (char *s, char *buf, switch_size_t len)
 
static char * switch_print_bits (const unsigned char *byte, char *buf, switch_size_t buflen)
 
static switch_bool_t switch_is_digit_string (const char *s)
 
static char switch_itodtmf (char i)
 
static int switch_dtmftoi (char *s)
 
static uint32_t switch_known_bitrate (switch_payload_t payload)
 
switch_size_t switch_fd_read_line (int fd, char *buf, switch_size_t len)
 
switch_size_t switch_fd_read_dline (int fd, char **buf, switch_size_t *len)
 
switch_size_t switch_fp_read_dline (FILE *fd, char **buf, switch_size_t *len)
 
switch_status_t switch_frame_alloc (switch_frame_t **frame, switch_size_t size)
 
switch_status_t switch_frame_dup (switch_frame_t *orig, switch_frame_t **clone)
 
switch_status_t switch_frame_free (switch_frame_t **frame)
 
switch_bool_t switch_is_uint_in_range (const char *str, unsigned int from, unsigned int to)
 Check if a 32 bit unsigned number is in a range. More...
 
switch_bool_t switch_is_number (const char *str)
 
switch_bool_t switch_is_leading_number (const char *str)
 
char * switch_find_parameter (const char *str, const char *param, switch_memory_pool_t *pool)
 
static switch_bool_t switch_true (const char *expr)
 Evaluate the truthfullness of a string expression. More...
 
static switch_byte_t switch_true_byte (const char *expr)
 
static int switch_false (const char *expr)
 Evaluate the falsefullness of a string expression. More...
 
switch_status_t switch_resolve_host (const char *host, char *buf, size_t buflen)
 
switch_status_t switch_find_local_ip (_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family)
 find local ip of the box More...
 
switch_status_t switch_find_interface_ip (_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ const char *ifname, _In_ int family)
 find primary ip of the specified interface More...
 
char * get_addr (char *buf, switch_size_t len, struct sockaddr *sa, socklen_t salen)
 find the char representation of an ip adress More...
 
char * get_addr6 (char *buf, switch_size_t len, struct sockaddr_in6 *sa, socklen_t salen)
 
int get_addr_int (switch_sockaddr_t *sa)
 
int switch_cmp_addr (switch_sockaddr_t *sa1, switch_sockaddr_t *sa2, switch_bool_t ip_only)
 
int switch_cp_addr (switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
 
unsigned short get_port (struct sockaddr *sa)
 get the port number of an ip address More...
 
int switch_build_uri (char *uri, switch_size_t size, const char *scheme, const char *user, const switch_sockaddr_t *sa, int flags)
 build a URI string from components More...
 
static int switch_errno_is_break (int errcode)
 
const char * switch_priority_name (switch_priority_t priority)
 Return a printable name of a switch_priority_t. More...
 
char switch_rfc2833_to_char (int event)
 Return the RFC2833 character based on an event id. More...
 
unsigned char switch_char_to_rfc2833 (char key)
 Return the RFC2833 event based on an key character. More...
 
static char * switch_sanitize_number (char *number)
 
static switch_bool_t switch_string_var_check (char *s, switch_bool_t disable)
 
static switch_bool_t switch_string_var_check_const (const char *s)
 
static char * switch_var_clean_string (char *s)
 
static char * switch_clean_string (char *s)
 
static char * switch_clean_name_string (char *s)
 
static int switch_safe_atoi (const char *nptr, int dft)
 Turn a string into an integer (default if NULL) More...
 
static long int switch_safe_atol (const char *nptr, long int dft)
 Turn a string into a long integer (default if NULL) More...
 
static long long int switch_safe_atoll (const char *nptr, long long int dft)
 Turn a string into a long long integer (default if NULL) More...
 
static char * switch_safe_strdup (const char *it)
 
static char * switch_lc_strdup (const char *it)
 
static char * switch_uc_strdup (const char *it)
 
static switch_bool_t switch_strstr (char *s, char *q)
 Test if one string is inside another with extra case checking. More...
 
switch_time_t switch_str_time (const char *in)
 Converts a string representation of a date into a switch_time_t. More...
 
unsigned int switch_separate_string (_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
 Separate a string into an array based on a character delimiter. More...
 
unsigned int switch_separate_string_string (char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen)
 
unsigned int switch_separate_string_cheap (_In_ const char *buf, char delim, _Post_count_(return) const char **array, _Post_count_(return) unsigned int larray[], unsigned int arraylen)
 Separate a string into an array based on a character delimiter, no memory corruption! DOESN'T support removing quotes like what's in switch_separate_string()! More...
 
char * switch_strip_spaces (char *str, switch_bool_t dup)
 
char * switch_strip_whitespace (const char *str)
 
char * switch_strip_commas (char *in, char *out, switch_size_t len)
 
char * switch_strip_nonnumerics (char *in, char *out, switch_size_t len)
 
char * switch_separate_paren_args (char *str)
 
const char * switch_stristr (const char *instr, const char *str)
 
switch_bool_t switch_is_lan_addr (const char *ip)
 
char * switch_replace_char (char *str, char from, char to, switch_bool_t dup)
 
switch_bool_t switch_ast2regex (const char *pat, char *rbuf, size_t len)
 
char * switch_pool_strip_whitespace (switch_memory_pool_t *pool, const char *str)
 
char * switch_escape_char (switch_memory_pool_t *pool, char *in, const char *delim, char esc)
 Escape a string by prefixing a list of characters with an escape character. More...
 
char * switch_escape_string (const char *in, char *out, switch_size_t outlen)
 
char * switch_escape_string_pool (const char *in, switch_memory_pool_t *pool)
 
int switch_socket_waitfor (switch_pollfd_t *poll, int ms)
 Wait for a socket. More...
 
const char * switch_cut_path (const char *in)
 Create a pointer to the file name in a given file path eliminating the directory name. More...
 
char * switch_string_replace (const char *string, const char *search, const char *replace)
 
switch_status_t switch_string_match (const char *string, size_t string_len, const char *search, size_t search_len)
 
int switch_strcasecmp_any (const char *str,...)
 
char * switch_util_quote_shell_arg (const char *string)
 Quote shell argument. More...
 
char * switch_util_quote_shell_arg_pool (const char *string, switch_memory_pool_t *pool)
 Quote shell argument, allocating from pool if provided. More...
 
static int32_t switch_calc_bitrate (int w, int h, float quality, double fps)
 
static void switch_calc_fps (switch_fps_t *fpsP, float fps, int samplerate)
 
static int32_t switch_parse_bandwidth_string (const char *bwv)
 
static uint32_t switch_parse_cpu_string (const char *cpu)
 
static int switch_needs_url_encode (const char *s)
 
char * switch_url_encode_opt (const char *url, char *buf, size_t len, switch_bool_t double_encode)
 
char * switch_url_encode (const char *url, char *buf, size_t len)
 
char * switch_url_decode (char *s)
 
char * switch_core_url_encode_opt (switch_memory_pool_t *pool, const char *url, switch_bool_t double_encode)
 
char * switch_core_url_encode (switch_memory_pool_t *pool, const char *url)
 
char * switch_core_session_url_encode_opt (switch_core_session_t *session, const char *url, switch_bool_t double_encode)
 
char * switch_core_session_url_encode (switch_core_session_t *session, const char *url)
 
switch_bool_t switch_simple_email (const char *to, const char *from, const char *headers, const char *body, const char *file, const char *convert_cmd, const char *convert_ext)
 
char * switch_find_end_paren (const char *s, char open, char close)
 
static void switch_separate_file_params (const char *file, char **file_portion, char **params_portion)
 
static switch_bool_t switch_is_file_path (const char *file)
 
static int switch_filecmp (const char *a, const char *b)
 
static const char * switch_parse_audio_col (switch_audio_col_t col)
 
int switch_parse_cidr (const char *string, ip_t *ip, ip_t *mask, uint32_t *bitp)
 
switch_status_t switch_network_list_create (switch_network_list_t **list, const char *name, switch_bool_t default_type, switch_memory_pool_t *pool)
 
switch_status_t switch_network_list_add_cidr_token (switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token)
 
char * switch_network_ipv4_mapped_ipv6_addr (const char *ip_str)
 
switch_status_t switch_network_list_add_host_mask (switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok)
 
switch_status_t switch_network_list_add_cidr_port_token (switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token, switch_network_port_range_p port)
 
switch_status_t switch_network_list_add_host_port_mask (switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok, switch_network_port_range_p port)
 
switch_bool_t switch_network_list_validate_ip_port_token (switch_network_list_t *list, uint32_t ip, int port, const char **token)
 
switch_bool_t switch_network_list_validate_ip6_port_token (switch_network_list_t *list, ip_t ip, int port, const char **token)
 
switch_bool_t switch_network_list_validate_ip_token (switch_network_list_t *list, uint32_t ip, const char **token)
 
switch_bool_t switch_network_list_validate_ip6_token (switch_network_list_t *list, ip_t ip, const char **token)
 
int switch_inet_pton (int af, const char *src, void *dst)
 
const char * switch_dow_int2str (int val)
 
int switch_dow_str2int (const char *exp)
 
switch_bool_t switch_dow_cmp (const char *exp, int val)
 
int switch_number_cmp (const char *exp, int val)
 
int switch_tod_cmp (const char *exp, int val)
 
int switch_fulldate_cmp (const char *exp, switch_time_t *ts)
 
void switch_split_date (const char *exp, int *year, int *month, int *day)
 
void switch_split_time (const char *exp, int *hour, int *min, int *sec)
 
int switch_split_user_domain (char *in, char **user, char **domain)
 Split a user string as user and domain. More...
 
void * switch_calloc (size_t nmemb, size_t size)
 
char * switch_uuid_str (char *buf, switch_size_t len)
 
char * switch_format_number (const char *num)
 
unsigned int switch_atoui (const char *nptr)
 
unsigned long switch_atoul (const char *nptr)
 
char * switch_strerror_r (int errnum, char *buf, switch_size_t buflen)
 
int switch_wait_sock (switch_os_socket_t sock, uint32_t ms, switch_poll_t flags)
 
int switch_wait_socklist (switch_waitlist_t *waitlist, uint32_t len, uint32_t ms)
 
switch_status_t switch_http_parse_header (char *buffer, uint32_t datalen, switch_http_request_t *request)
 
void switch_http_free_request (switch_http_request_t *request)
 
void switch_http_dump_request (switch_http_request_t *request)
 
void switch_http_parse_qs (switch_http_request_t *request, char *qs)
 
switch_status_t switch_frame_buffer_free (switch_frame_buffer_t *fb, switch_frame_t **frameP)
 
switch_status_t switch_frame_buffer_dup (switch_frame_buffer_t *fb, switch_frame_t *orig, switch_frame_t **clone)
 
switch_status_t switch_frame_buffer_destroy (switch_frame_buffer_t **fbP)
 
switch_status_t switch_frame_buffer_create (switch_frame_buffer_t **fbP, switch_size_t qlen)
 
switch_status_t switch_frame_buffer_push (switch_frame_buffer_t *fb, void *ptr)
 
switch_status_t switch_frame_buffer_trypush (switch_frame_buffer_t *fb, void *ptr)
 
switch_status_t switch_frame_buffer_pop (switch_frame_buffer_t *fb, void **ptr)
 
switch_status_t switch_frame_buffer_trypop (switch_frame_buffer_t *fb, void **ptr)
 
int switch_frame_buffer_size (switch_frame_buffer_t *fb)
 
void switch_getcputime (switch_cputime *t)
 
char * switch_html_strip (const char *str)
 
unsigned long switch_getpid (void)
 
switch_status_t switch_digest (const char *digest_name, unsigned char **digest, const void *input, switch_size_t inputLen, unsigned int *outputlen)
 
switch_status_t switch_digest_string (const char *digest_name, char **digest_str, const void *input, switch_size_t inputLen, unsigned int *outputlen)
 
cJSONswitch_jwt_verify (const char *secret, const char *token)
 
char * switch_jwt_sign (const char *secret, const uint8_t *payload, switch_size_t size)
 
char * switch_must_strdup (const char *_s)
 
const char * switch_memory_usage_stream (switch_stream_handle_t *stream)
 

Detailed Description

Compatability and Helper Code.

Just a miscelaneaous set of general utility/helper functions.

Definition in file switch_utils.h.

Macro Definition Documentation

◆ DUMP_EVENT

#define DUMP_EVENT (   _e)    {char *event_str;switch_event_serialize(_e, &event_str, SWITCH_FALSE);switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DUMP\n%s\n", event_str);free(event_str);}

Definition at line 1431 of file switch_utils.h.

Referenced by switch_ivr_uuid_bridge().

◆ end_of

#define end_of (   _s)    *(*_s == '\0' ? _s : _s + strlen(_s) - 1)

◆ end_of_p

#define end_of_p (   _s)    (*_s == '\0' ? _s : _s + strlen(_s) - 1)

◆ is_dtmf

#define is_dtmf (   key)    ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119 || key == 70 || key == 102)

determine if a character is a valid DTMF key

Parameters
keythe key to test
Returns
TRUE or FALSE

Definition at line 683 of file switch_utils.h.

Referenced by meta_on_dtmf(), speech_thread(), switch_channel_queue_dtmf(), switch_channel_queue_dtmf_string(), switch_core_session_send_dtmf(), switch_core_session_send_dtmf_string(), and switch_ivr_bind_dtmf_meta_session().

◆ MAX_NETWORK_PORTS

#define MAX_NETWORK_PORTS   10

◆ switch_arraylen

#define switch_arraylen (   _a)    (sizeof(_a) / sizeof(_a[0]))

Definition at line 374 of file switch_utils.h.

Referenced by switch_dow_int2str(), and switch_dow_str2int().

◆ switch_calc_video_fps

#define switch_calc_video_fps (   fpsP,
  fps 
)    switch_calc_fps(fpsP, fps, 90000)

Definition at line 1135 of file switch_utils.h.

Referenced by video_bug_thread(), and video_write_thread().

◆ switch_clear_flag

#define switch_clear_flag (   obj,
  flag 
)    (obj)->flags &= ~(flag)

◆ switch_clear_flag_locked

#define switch_clear_flag_locked (   obj,
  flag 
)    switch_mutex_lock((obj)->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock((obj)->flag_mutex);

Clear a flag on an arbitrary object.

Parameters
objthe object to test
flagthe or'd list of flags to clear

Definition at line 717 of file switch_utils.h.

Referenced by switch_core_file_close(), switch_core_file_pre_close(), switch_core_file_read(), switch_core_perform_file_open(), switch_ivr_deactivate_unicast(), switch_ivr_play_file(), switch_ivr_process_fh(), switch_ivr_record_file_event(), and unicast_thread_run().

◆ switch_codec2str

#define switch_codec2str (   codec,
  buf,
  len 
)
Value:
snprintf(buf, len, "%s@%uh@%ui", \
codec->implementation->iananame, \
codec->implementation->samples_per_second, \
codec->implementation->microseconds_per_packet / 1000)
switch_byte_t switch_byte_t * buf

Definition at line 293 of file switch_utils.h.

Referenced by switch_core_session_outgoing_channel().

◆ switch_copy_flags

#define switch_copy_flags (   dest,
  src,
  flags 
)    (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))

Copy flags from one arbitrary object to another.

Parameters
destthe object to copy the flags to
srcthe object to copy the flags from
flagsthe flags to copy

Definition at line 732 of file switch_utils.h.

◆ SWITCH_DECLARE_GLOBAL_STRING_FUNC

#define SWITCH_DECLARE_GLOBAL_STRING_FUNC (   fname,
  vname 
)
Value:
static void fname(const char *string) { if (!string) return;\
if (vname) {free(vname); vname = NULL;}vname = strdup(string);} static void fname(const char *string)

Declares a function designed to set a dynamic global string.

Parameters
fnamethe function name to declare
vnamethe name of the global pointer to modify with the new function

Definition at line 1013 of file switch_utils.h.

◆ switch_errno

#define switch_errno ( )    errno

◆ switch_goto_int

#define switch_goto_int (   _n,
  _i,
  _label 
)    _n = _i; goto _label

Definition at line 288 of file switch_utils.h.

◆ switch_goto_status

#define switch_goto_status (   _status,
  _label 
)    status = _status; goto _label

◆ switch_inet_ntop

#define switch_inet_ntop   inet_ntop

Definition at line 1434 of file switch_utils.h.

◆ switch_is_valid_rate

#define switch_is_valid_rate (   _tmp)    (_tmp == 8000 || _tmp == 12000 || _tmp == 16000 || _tmp == 24000 || _tmp == 32000 || _tmp == 11025 || _tmp == 22050 || _tmp == 44100 || _tmp == 48000)

◆ switch_malloc

#define switch_malloc (   ptr,
  len 
)    (void)(switch_assert(((ptr) = malloc((len)))),ptr)

Definition at line 1425 of file switch_utils.h.

Referenced by switch_msrp_msg_set_payload().

◆ switch_network_list_add_cidr

#define switch_network_list_add_cidr (   _list,
  _cidr_str,
  _ok 
)    switch_network_list_add_cidr_token(_list, _cidr_str, _ok, NULL)

Definition at line 1366 of file switch_utils.h.

Referenced by switch_load_network_lists().

◆ switch_network_list_validate_ip

#define switch_network_list_validate_ip (   _list,
  _ip 
)    switch_network_list_validate_ip_token(_list, _ip, NULL);

Definition at line 1378 of file switch_utils.h.

◆ switch_normalize_to_16bit

#define switch_normalize_to_16bit (   n)    if (n > SWITCH_SMAX) n = SWITCH_SMAX; else if (n < SWITCH_SMIN) n = SWITCH_SMIN;

◆ SWITCH_READ_ACCEPTABLE

#define SWITCH_READ_ACCEPTABLE (   status)    (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE)

◆ switch_safe_free

#define switch_safe_free (   it)    if (it) {free(it);it=NULL;}

Free a pointer and set it to NULL unless it already is NULL.

Parameters
itthe pointer

Definition at line 885 of file switch_utils.h.

Referenced by _switch_hashtable_remove(), api_hook(), check_channel_status(), comp_callback(), core_event_handler(), CoreSession::destroy(), destroy_ecd(), do_chat_send(), do_flush(), do_trans(), ecd_deliver(), API::executeString(), fs_encode_cleanup(), fs_tts_cleanup(), CoreSession::getXMLCDR(), hashtable_expand(), load_mime_types(), main(), msrp_msg_serialize(), CoreSession::originate(), parse_presence_data_cols(), play_and_collect(), preprocess(), preprocess_exec_set(), preprocess_glob(), record_helper_post_process(), Event::serialize(), CoreSession::set_tts_params(), CoreSession::set_tts_parms(), setup_ringback(), speech_thread(), sql_in_thread(), Stream::Stream(), switch_api_execute(), switch_buffer_destroy(), switch_cache_db_execute_sql_real(), switch_cache_db_persistant_execute(), switch_cache_db_persistant_execute_trans_full(), switch_caller_extension_add_application_printf(), switch_channel_build_param_string(), switch_channel_expand_variables_check(), switch_channel_export_variable_var_check(), switch_channel_pass_sdp(), switch_channel_set_presence_data_vals(), switch_channel_set_timestamps(), switch_channel_set_variable_strip_quotes_var_check(), switch_channel_uninit(), switch_check_network_list_ip_port_token(), switch_console_complete(), switch_console_execute(), switch_console_expand_alias(), switch_console_process(), switch_console_set_alias(), switch_console_set_complete(), switch_core_asr_close(), switch_core_asr_load_grammar(), switch_core_check_dtls_pem(), switch_core_destroy(), switch_core_file_close(), switch_core_gen_certs(), switch_core_hash_insert_alloc_destructor(), switch_core_hash_insert_auto_free(), switch_core_hash_insert_destructor(), switch_core_hash_insert_dup_auto_free(), switch_core_hash_insert_dup_destructor(), switch_core_hash_insert_pointer(), switch_core_media_bug_destroy(), switch_core_media_ext_address_lookup(), switch_core_media_gen_local_sdp(), switch_core_media_media_params(), switch_core_media_negotiate_sdp(), switch_core_media_process_sdp_filter(), switch_core_media_set_r_sdp_codec_string(), switch_core_perform_file_open(), switch_core_recovery_flush(), switch_core_recovery_recover(), switch_core_recovery_track(), switch_core_session_ctl(), switch_core_session_exec(), switch_core_session_free_message(), switch_core_session_printf(), switch_core_session_reporting_state(), switch_core_speech_feed_tts(), switch_core_sqldb_start(), switch_dial_handle_list_create_json_obj(), switch_digest(), switch_digest_string(), switch_event_add_body(), switch_event_add_presence_data_cols(), switch_event_base_add_header(), switch_event_build_param_string(), switch_event_expand_headers_check(), switch_event_serialize(), switch_event_set_body(), switch_event_set_subclass_name(), switch_find_local_ip(), switch_frame_free(), switch_fulldate_cmp(), switch_hashtable_destroy(), switch_http_parse_qs(), switch_img_free(), switch_img_write_text_img(), switch_ivr_broadcast(), switch_ivr_collect_digits_count(), switch_ivr_digit_stream_destroy(), switch_ivr_eavesdrop_session(), switch_ivr_insert_file(), switch_ivr_multi_threaded_bridge(), switch_ivr_originate(), switch_ivr_park(), switch_ivr_phrase_macro_event(), switch_ivr_play_file(), switch_ivr_preprocess_session(), switch_ivr_record_file_event(), switch_ivr_set_json_chan_vars(), switch_ivr_sleep(), switch_ivr_speak_text_handle(), switch_ivr_wait_for_answer(), switch_jb_pop_nack(), switch_json_add_presence_data_cols(), switch_jwt_verify(), switch_loadable_module_enumerate_available(), switch_loadable_module_get_codecs(), switch_loadable_module_load_file(), switch_log_meta_vprintf(), switch_log_node_free(), switch_log_node_to_json(), switch_msrp_destroy(), switch_msrp_msg_destroy(), switch_nat_init(), switch_nat_multicast_runtime(), switch_nat_republish(), switch_nat_shutdown(), switch_network_list_add_cidr_port_token(), switch_network_list_perform_add_cidr_token(), switch_odbc_handle_callback_exec_detailed(), switch_odbc_handle_destroy(), switch_odbc_handle_disconnect(), switch_odbc_handle_new(), switch_play_and_get_digits(), switch_regex_match_partial(), switch_regex_perform(), switch_rtp_destroy(), switch_say_file_handle_destroy(), switch_simple_email(), switch_split_date(), switch_split_time(), SWITCH_STANDARD_APP(), switch_stream_spawn(), switch_stream_write_file_contents(), switch_strstr(), switch_tod_cmp(), switch_xml_clear_user_cache(), switch_xml_config_cleanup(), switch_xml_config_parse_event(), switch_xml_free(), switch_xml_locate_language(), switch_xml_locate_user_merged(), switch_xml_parse_file(), switch_xml_tohtml_ex(), switch_xml_user_cache(), task_thread_loop(), text_bridge_thread(), tone_detect_set_total_time(), and unsub_all_switch_event_channel().

◆ switch_samples_per_packet

#define switch_samples_per_packet (   rate,
  interval 
)    ((uint32_t)((float)rate / (1000.0f / (float)interval)))

Definition at line 289 of file switch_utils.h.

Referenced by switch_core_speech_open().

◆ switch_set_flag

#define switch_set_flag (   obj,
  flag 
)    (obj)->flags |= (flag)

Set a flag on an arbitrary object.

Parameters
objthe object to set the flags on
flagthe or'd list of flags to set

Definition at line 700 of file switch_utils.h.

Referenced by CoreSession::CoreSession(), find_free_frame(), perform_write(), rtp_common_write(), CoreSession::setAutoHangup(), switch_buffer_create_dynamic(), switch_buffer_create_partition(), switch_caller_profile_new(), switch_channel_queue_dtmf(), switch_collect_input_callback(), switch_core_asr_close(), switch_core_asr_open(), switch_core_codec_init_with_bitrate(), switch_core_destroy(), switch_core_directory_open(), switch_core_file_handle_dup(), switch_core_init(), switch_core_media_bug_add(), switch_core_media_bug_patch_video(), switch_core_media_bug_pop(), switch_core_media_bug_remove(), switch_core_media_bug_set_flag(), switch_core_media_read_frame(), switch_core_media_receive_message(), switch_core_perform_file_open(), switch_core_session_ctl(), switch_core_session_hangup_state(), switch_core_session_queue_indication(), switch_core_session_read_frame(), switch_core_session_read_text_frame(), switch_core_session_read_video_frame(), switch_core_session_run(), switch_core_session_sync_clock(), switch_core_session_thread(), switch_core_session_thread_launch(), switch_core_session_thread_pool_launch(), switch_core_session_write_frame(), switch_core_session_write_video_frame(), switch_core_speech_open(), switch_core_speech_read_tts(), switch_core_sqldb_start(), switch_core_timer_init(), switch_frame_alloc(), switch_frame_dup(), switch_ivr_activate_unicast(), switch_ivr_detect_speech(), switch_ivr_detect_speech_init(), switch_ivr_eavesdrop_session(), switch_ivr_menu_execute(), switch_ivr_menu_init(), switch_ivr_menu_stack_xml_build(), switch_ivr_originate(), switch_ivr_play_and_collect_input(), switch_ivr_record_session_event(), switch_ivr_sleep(), switch_jb_set_flag(), switch_load_core_config(), switch_media_handle_create(), switch_rtp_enable_vad(), switch_rtp_zerocopy_read_frame(), switch_speex_encode(), switch_xml_set_root(), and video_bug_thread().

◆ switch_set_flag_locked

#define switch_set_flag_locked (   obj,
  flag 
)
Value:
assert((obj)->flag_mutex != NULL); \
switch_mutex_lock((obj)->flag_mutex); \
(obj)->flags |= (flag);\
switch_mutex_unlock((obj)->flag_mutex);

Set a flag on an arbitrary object while locked.

Parameters
objthe object to set the flags on
flagthe or'd list of flags to set

Definition at line 707 of file switch_utils.h.

Referenced by switch_core_file_pre_close(), switch_core_file_read(), switch_core_file_seek(), switch_core_perform_file_open(), switch_ivr_activate_unicast(), switch_ivr_play_file(), switch_ivr_process_fh(), unicast_thread_launch(), and video_write_thread().

◆ switch_set_string

#define switch_set_string (   _dst,
  _src 
)    switch_copy_string(_dst, _src, sizeof(_dst))

◆ SWITCH_SMAX

#define SWITCH_SMAX   32767

Definition at line 290 of file switch_utils.h.

Referenced by switch_core_media_bug_read().

◆ SWITCH_SMIN

#define SWITCH_SMIN   -32768

Definition at line 291 of file switch_utils.h.

Referenced by switch_core_media_bug_read().

◆ switch_split

#define switch_split (   _data,
  _delim,
  _array 
)    switch_separate_string(_data, _delim, _array, switch_arraylen(_array))

◆ switch_split_cheap

#define switch_split_cheap (   _data,
  _delim,
  _array,
  _larray 
)    switch_separate_string_cheap(_data, _delim, (const char **)_array, _larray, switch_arraylen(_array))

Definition at line 376 of file switch_utils.h.

◆ SWITCH_STATUS_IS_BREAK

#define SWITCH_STATUS_IS_BREAK (   x)    (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)

◆ switch_str_nil

#define switch_str_nil (   s)    (s ? s : "")

Make a null string a blank string instead.

Parameters
sthe string to test
Returns
the original string or blank string.

Definition at line 993 of file switch_utils.h.

Referenced by api_hook(), EventConsumer::bind(), console_clean_log(), console_log(), console_log2(), CoreSession::consoleLog(), CoreSession::consoleLog2(), core_event_handler(), do_api_on(), is_valid_action(), msrp_reply(), msrp_report(), CoreSession::print(), CoreSession::set_tts_params(), switch_channel_flip_cid(), switch_channel_set_timestamps(), switch_console_complete(), switch_console_set_complete(), switch_core_add_registration(), switch_core_media_activate_rtp(), switch_core_media_negotiate_sdp(), switch_core_recovery_track(), switch_core_session_exec(), switch_core_session_execute_application_get_flags(), switch_core_session_execute_exten(), switch_event_create_array_pair(), switch_ivr_displace_session(), switch_ivr_generate_json_cdr(), switch_ivr_originate(), switch_ivr_parse_event(), switch_ivr_phrase_macro_event(), switch_ivr_play_file(), switch_ivr_record_file_event(), switch_ivr_record_session_event(), switch_loadable_module_get_codecs_sorted(), switch_loadable_module_load_module_ex(), switch_loadable_module_process(), switch_loadable_module_unprocess(), switch_network_list_perform_add_cidr_token(), switch_odbc_handle_callback_exec_detailed(), switch_odbc_handle_disconnect(), switch_odbc_handle_exec(), switch_scheduler_add_task_ex(), switch_scheduler_execute(), switch_stun_host_lookup(), switch_xml_set_attr_d(), and task_thread_loop().

◆ switch_strdup

#define switch_strdup (   ptr,
 
)    (void)(switch_assert(((ptr) = strdup((s)))),ptr)

Definition at line 1427 of file switch_utils.h.

Referenced by switch_cache_db_execute_sql_real().

◆ switch_strlen_zero

#define switch_strlen_zero (   x)    zstr(x)

Definition at line 316 of file switch_utils.h.

Referenced by main().

◆ switch_strlen_zero_buf

#define switch_strlen_zero_buf (   x)    zstr_buf(x)

Definition at line 317 of file switch_utils.h.

◆ switch_test_flag

#define switch_test_flag (   obj,
  flag 
)    ((obj)->flags & flag)

Test for the existance of a flag on an arbitary object.

Parameters
objthe object to test
flagthe or'd list of flags to test
Returns
true value if the object has the flags defined

Definition at line 693 of file switch_utils.h.

Referenced by audio_bridge_thread(), CoreSession::destroy(), early_thread_run(), eavesdrop_callback(), find_free_frame(), get_handle(), main(), perform_write(), record_callback(), recording_thread(), rtp_common_write(), setup_ringback(), speech_thread(), sql_close(), switch_buffer_destroy(), switch_buffer_freespace(), switch_buffer_reset_partition_data(), switch_buffer_set_partition_data(), switch_buffer_slide_write(), switch_buffer_write(), switch_buffer_zwrite(), switch_cache_db_create_schema(), switch_cache_db_status(), switch_cache_db_test_reactive_ex(), switch_caller_get_field_by_name(), switch_caller_profile_event_set_data(), switch_channel_clear_state_handler(), switch_channel_dequeue_dtmf(), switch_collect_input_callback(), switch_core_add_registration(), switch_core_asr_close(), switch_core_asr_open(), switch_core_codec_decode(), switch_core_codec_decode_video(), switch_core_codec_destroy(), switch_core_codec_encode(), switch_core_codec_encode_video(), switch_core_db_test_reactive(), switch_core_del_registration(), switch_core_destroy(), switch_core_directory_close(), switch_core_execute_chat_app(), switch_core_expire_registration(), switch_core_file_close(), switch_core_file_command(), switch_core_file_get_string(), switch_core_file_has_video(), switch_core_file_pre_close(), switch_core_file_read(), switch_core_file_read_video(), switch_core_file_seek(), switch_core_file_set_string(), switch_core_file_truncate(), switch_core_file_write(), switch_core_file_write_video(), switch_core_init(), switch_core_init_and_modload(), switch_core_media_bug_add(), switch_core_media_bug_close(), switch_core_media_bug_count(), switch_core_media_bug_destroy(), switch_core_media_bug_exec_all(), switch_core_media_bug_inuse(), switch_core_media_bug_patch_video(), switch_core_media_bug_read(), switch_core_media_bug_remove(), switch_core_media_bug_remove_all_function(), switch_core_media_bug_test_flag(), switch_core_media_codec_get_cap(), switch_core_media_read_frame(), switch_core_media_receive_message(), switch_core_media_write_frame(), switch_core_perform_file_open(), switch_core_port_allocator_free_port(), switch_core_port_allocator_new(), switch_core_port_allocator_request_port(), switch_core_ready(), switch_core_ready_inbound(), switch_core_ready_outbound(), switch_core_session_ctl(), switch_core_session_execute_application_get_flags(), switch_core_session_free_message(), switch_core_session_hangup_state(), switch_core_session_media_handle_ready(), switch_core_session_perform_destroy(), switch_core_session_perform_force_locate(), switch_core_session_read_frame(), switch_core_session_read_lock(), switch_core_session_read_lock_hangup(), switch_core_session_read_text_frame(), switch_core_session_read_video_frame(), switch_core_session_recv_dtmf(), switch_core_session_running(), switch_core_session_send_dtmf(), switch_core_session_started(), switch_core_session_thread_launch(), switch_core_session_thread_pool_launch(), switch_core_session_write_frame(), switch_core_session_write_video_frame(), switch_core_speech_close(), switch_core_speech_read_tts(), switch_core_sql_db_thread(), switch_core_sql_exec(), switch_core_sqldb_destroy(), switch_core_sqldb_init(), switch_core_sqldb_start(), switch_core_test_flag(), switch_core_timer_destroy(), switch_event_base_add_header(), switch_event_check_permission_list(), switch_frame_dup(), switch_frame_free(), switch_ivr_activate_unicast(), switch_ivr_deactivate_unicast(), switch_ivr_delay_echo(), switch_ivr_eavesdrop_session(), switch_ivr_insert_file(), switch_ivr_menu_execute(), switch_ivr_menu_stack_free(), switch_ivr_park(), switch_ivr_parse_event(), switch_ivr_play_and_collect_input(), switch_ivr_play_file(), switch_ivr_process_fh(), switch_ivr_record_file_event(), switch_ivr_sound_test(), switch_ivr_speak_text_handle(), switch_ivr_wait_for_answer(), switch_jb_destroy(), switch_jb_put_packet(), switch_jb_set_session(), switch_loadable_module_load_file(), SWITCH_MODULE_LOAD_FUNCTION(), switch_rtp_enable_vad(), switch_rtp_write_frame(), switch_rtp_zerocopy_read_frame(), switch_scheduler_del_task_group(), switch_scheduler_del_task_id(), switch_speex_encode(), switch_system(), switch_user_sql_thread(), switch_xml_config_item_print_doc(), switch_xml_config_parse_event(), switch_xml_free(), task_thread_loop(), text_bridge_thread(), text_helper_thread(), unicast_thread_run(), video_bug_thread(), video_helper_thread(), and video_write_thread().

◆ switch_test_subnet

#define switch_test_subnet (   _ip,
  _net,
  _mask 
)    (_mask ? ((_net & _mask) == (_ip & _mask)) : _net ? _net == _ip : 1)

◆ switch_time_from_sec

#define switch_time_from_sec (   sec)    ((switch_time_t)(sec) * 1000000)

Definition at line 1006 of file switch_utils.h.

◆ switch_true_buf

#define switch_true_buf (   expr)
Value:
((( !strcasecmp(expr, "yes") ||\
!strcasecmp(expr, "on") ||\
!strcasecmp(expr, "true") ||\
!strcasecmp(expr, "t") ||\
!strcasecmp(expr, "enabled") ||\
!strcasecmp(expr, "active") ||\
!strcasecmp(expr, "allow") ||\
(switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE)
switch_bool_t switch_is_number(const char *str)

Definition at line 536 of file switch_utils.h.

◆ SWITCH_URL_UNSAFE

#define SWITCH_URL_UNSAFE   "\r\n #%&+:;<=>?@[\\]^`{|}\""

◆ switch_yield

#define switch_yield (   ms)    switch_sleep(ms);

Wait a desired number of microseconds and yield the CPU.

Definition at line 998 of file switch_utils.h.

Referenced by _switch_cache_db_get_db_handle(), audio_bridge_thread(), media_thread_run(), msrp_worker(), originate_on_consume_media_transmit(), pool_thread(), record_callback(), rtp_common_read(), signal_bridge_on_hibernate(), switch_cache_db_persistant_execute(), switch_cache_db_persistant_execute_trans_full(), switch_channel_clear_device_record(), switch_console_save_history(), switch_core_db_exec(), switch_core_db_persistant_execute(), switch_core_db_persistant_execute_trans(), switch_core_media_end_engine_function(), switch_core_media_ext_address_lookup(), switch_core_media_read_frame(), switch_core_media_set_codec(), switch_core_media_toggle_hold(), switch_core_media_write_frame(), switch_core_runtime_loop(), switch_core_session_ctl(), switch_core_session_read_frame(), switch_core_session_run(), switch_core_session_send_dtmf(), switch_core_session_thread(), switch_core_session_write_blank_video(), switch_core_sql_db_thread(), switch_core_standard_on_reset(), switch_event_launch_dispatch_threads(), switch_event_shutdown(), switch_ivr_3p_media(), switch_ivr_3p_nomedia(), switch_ivr_deactivate_unicast(), switch_ivr_eavesdrop_session(), switch_ivr_enterprise_originate(), switch_ivr_media(), switch_ivr_nomedia(), switch_ivr_originate(), switch_ivr_park(), switch_ivr_play_file(), switch_ivr_sleep(), switch_ivr_speak_text_handle(), switch_ivr_wait_for_answer(), switch_loadable_module_shutdown(), switch_loadable_module_unload_module(), switch_msrp_session_destroy(), switch_msrp_session_pop_msg(), switch_nat_multicast_runtime(), switch_nat_thread_stop(), switch_odbc_handle_disconnect(), switch_scheduler_task_thread_stop(), switch_socket_send(), switch_sql_queue_manager_push(), switch_sql_queue_manager_push_confirm(), switch_sql_queue_manager_stop(), SWITCH_STANDARD_APP(), switch_thread_rwlock_trywrlock_timeout(), switch_user_sql_thread(), uuid_bridge_on_soft_execute(), video_helper_thread(), and wait_for_cause().

◆ switch_zmalloc

#define switch_zmalloc (   ptr,
  len 
)    (void)(switch_assert((ptr = calloc(1, (len)))),ptr)

◆ zset

#define zset (   _a,
  _b 
)    if (!zstr(_b)) _a = _b

Definition at line 328 of file switch_utils.h.

◆ zstr

#define zstr (   x)    _zstr(x)

Definition at line 314 of file switch_utils.h.

Referenced by __switch_xml_open_root(), _switch_core_db_handle(), add_audio_codec(), api_hook(), asr_set_json_text_params(), audio_bridge_thread(), EventConsumer::bind(), Event::chat_send(), check_channel_status(), check_dtls_reinvite(), check_ice(), check_ip(), check_jb(), collect_thread_run(), comp_callback(), core_event_handler(), Event::delHeader(), do_api_on(), do_trans(), enterprise_originate_ringback_thread(), Event::Event(), EventConsumer::EventConsumer(), API::execute(), CoreSession::execute(), API::executeString(), fs_core_init(), fst_init_core_and_modload(), gen_ice(), generate_m(), get_channels(), Event::getHeader(), globalSetVariable(), handle_ice(), inherit_codec(), ip_choose_family(), ip_possible(), is_valid_action(), IVRMenu::IVRMenu(), limit_state_handler(), load_configuration(), main(), parse_array(), parse_presence_data_cols(), play_and_collect(), play_and_detect_input_callback(), preprocess_exec_set(), preprocess_stun_set(), record_callback(), send_display(), send_record_stop_event(), setup_ringback(), signal_bridge_on_hangup(), switch_cache_db_persistant_execute_trans_full(), switch_caller_profile_event_set_data(), switch_caller_profile_new(), switch_channel_add_variable_var_check(), switch_channel_build_param_string(), switch_channel_del_variable_prefix(), switch_channel_execute_on_value(), switch_channel_expand_variables_check(), switch_channel_get_hold_music(), switch_channel_get_name(), switch_channel_get_variable_dup(), switch_channel_get_variable_partner(), switch_channel_invert_cid(), switch_channel_pass_callee_id(), switch_channel_process_export(), switch_channel_queue_dtmf_string(), switch_channel_set_log_tag(), switch_channel_set_name(), switch_channel_set_presence_data_vals(), switch_channel_set_profile_var(), switch_channel_set_timestamps(), switch_channel_set_variable_partner_var_check(), switch_channel_set_variable_printf(), switch_channel_set_variable_strip_quotes_var_check(), switch_channel_set_variable_var_check(), switch_channel_str2cause(), switch_channel_transfer_variable_prefix(), switch_collect_input_callback(), switch_color_set_rgb(), switch_console_complete(), switch_console_list_uuid(), switch_console_save_history(), switch_console_set_complete(), switch_core_add_registration(), switch_core_asr_load_grammar(), switch_core_codec_parse_fmtp(), switch_core_del_registration(), switch_core_get_switchname(), switch_core_media_activate_rtp(), switch_core_media_add_crypto(), switch_core_media_add_payload_map(), switch_core_media_bug_add(), switch_core_media_bug_remove_all_function(), switch_core_media_choose_port(), switch_core_media_choose_ports(), switch_core_media_deactivate_rtp(), switch_core_media_ext_address_lookup(), switch_core_media_gen_local_sdp(), switch_core_media_get_codec_string(), switch_core_media_merge_sdp_codec_string(), switch_core_media_negotiate_sdp(), switch_core_media_patch_sdp(), switch_core_media_process_sdp_filter(), switch_core_media_proxy_remote_addr(), switch_core_media_receive_message(), switch_core_media_recover_session(), switch_core_media_set_r_sdp_codec_string(), switch_core_perform_file_open(), switch_core_perform_permanent_strdup(), switch_core_perform_session_strdup(), switch_core_perform_strndup(), switch_core_recovery_flush(), switch_core_recovery_recover(), switch_core_session_check_incoming_crypto(), switch_core_session_ctl(), switch_core_session_exec(), switch_core_session_execute_exten(), switch_core_session_get_payload_code(), switch_core_session_outgoing_channel(), switch_core_session_parse_crypto_prefs(), switch_core_session_perform_receive_message(), switch_core_session_read_text_frame(), switch_core_session_reporting_state(), switch_core_session_send_dtmf(), switch_core_session_send_dtmf_string(), switch_core_set_var_conditional(), switch_core_speech_feed_tts(), switch_core_speech_open(), switch_core_sqldb_start_thread(), switch_core_standard_on_routing(), switch_dial_handle_create_json(), switch_dial_handle_list_create_json(), switch_event_base_add_header(), switch_event_build_param_string(), switch_event_create_array_pair(), switch_event_del_header_val(), switch_event_expand_headers_check(), switch_event_xmlize(), switch_file_exists(), switch_filecmp(), switch_format_number(), switch_img_txt_handle_create(), switch_img_txt_handle_render(), switch_img_write_text_img(), switch_is_file_path(), switch_is_lan_addr(), switch_is_moh(), switch_ivr_bind_dtmf_meta_session(), switch_ivr_blind_transfer_ack(), switch_ivr_check_presence_mapping(), switch_ivr_collect_digits_count(), switch_ivr_displace_session(), switch_ivr_dmachine_bind(), switch_ivr_dmachine_check_match(), switch_ivr_dmachine_clear_realm(), switch_ivr_dmachine_feed(), switch_ivr_dmachine_ping(), switch_ivr_eavesdrop_session(), switch_ivr_enterprise_originate(), switch_ivr_generate_json_cdr(), switch_ivr_generate_xml_cdr(), switch_ivr_intercept_session(), switch_ivr_kill_uuid(), switch_ivr_menu_execute(), switch_ivr_menu_init(), switch_ivr_menu_stack_xml_build(), switch_ivr_menu_str2action(), switch_ivr_originate(), switch_ivr_parse_event(), switch_ivr_phrase_macro_event(), switch_ivr_play_and_collect_input(), switch_ivr_play_file(), switch_ivr_process_fh(), switch_ivr_read(), switch_ivr_record_session_event(), switch_ivr_say(), switch_ivr_session_transfer(), switch_ivr_set_json_chan_vars(), switch_ivr_set_user_extended(), switch_ivr_set_user_xml(), switch_ivr_set_xml_chan_var(), switch_ivr_soft_hold(), switch_ivr_tone_detect_session(), switch_ivr_wait_for_answer(), switch_load_core_config(), switch_load_network_lists(), switch_load_timezones(), switch_loadable_module_exists(), switch_loadable_module_get_codecs_sorted(), switch_loadable_module_init(), switch_loadable_module_protect(), switch_log_meta_vprintf(), switch_log_node_to_json(), switch_lookup_timezone(), switch_media_bug_parse_spy_fmt(), switch_media_handle_create(), switch_msrp_do_send(), switch_network_list_perform_add_cidr_token(), switch_odbc_handle_callback_exec_detailed(), switch_odbc_handle_exec(), switch_play_and_get_digits(), switch_pool_strip_whitespace(), switch_rtp_add_dtls(), switch_rtp_create(), switch_rtp_get_remote_host(), switch_rtp_new(), switch_rtp_set_local_address(), switch_say_file_handle_create(), switch_scheduler_del_task_group(), switch_simple_email(), switch_speex_fmtp_parse(), SWITCH_STANDARD_API(), SWITCH_STANDARD_APP(), switch_stream_spawn(), switch_strftime_tz(), switch_strip_spaces(), switch_strip_whitespace(), switch_stun_ip_lookup(), switch_time_exp_tz_name(), switch_url_decode(), switch_xml_config_parse_event(), switch_xml_locate(), switch_xml_locate_user_merged(), switch_xml_merge_user(), switch_xml_std_datetime_check(), text_callback(), and transfer_after_bridge().

◆ zstr_buf

#define zstr_buf (   s)    (*(s) == '\0')

Typedef Documentation

◆ switch_http_request_t

◆ switch_network_port_range_p

Definition at line 58 of file switch_utils.h.

◆ switch_network_port_range_t

Definition at line 57 of file switch_utils.h.

Enumeration Type Documentation

◆ switch_uri_flags

flags to be used with switch_build_uri()

Enumerator
SWITCH_URI_NUMERIC_HOST 
SWITCH_URI_NUMERIC_PORT 
SWITCH_URI_NO_SCOPE 

Definition at line 615 of file switch_utils.h.

Function Documentation

◆ _zstr()

static _Check_return_ int _zstr ( _In_opt_z_ const char *  s)
inlinestatic

Test for NULL or zero length string.

Parameters
sthe string to test
Returns
true value if the string is NULL or zero length

Definition at line 305 of file switch_utils.h.

306 {
307  if (!s) return 1;
308  if (*s == '\0') return 1;
309  return 0;
310 }

◆ get_addr()

char* get_addr ( char *  buf,
switch_size_t  len,
struct sockaddr *  sa,
socklen_t  salen 
)

find the char representation of an ip adress

Parameters
bufthe buffer to write the ip adress found into
lenthe length of the buf
sathe struct sockaddr * to get the adress from
salenthe length of sa
Returns
the ip adress string

Definition at line 2494 of file switch_utils.c.

References buf, and switch_assert.

Referenced by switch_false(), switch_find_local_ip(), switch_get_addr(), and switch_resolve_host().

2495 {
2496  switch_assert(buf);
2497  *buf = '\0';
2498 
2499  if (sa) {
2500  getnameinfo(sa, salen, buf, (socklen_t) len, NULL, 0, NI_NUMERICHOST);
2501  }
2502  return buf;
2503 }
switch_byte_t switch_byte_t * buf
#define switch_assert(expr)

◆ get_addr6()

char* get_addr6 ( char *  buf,
switch_size_t  len,
struct sockaddr_in6 *  sa,
socklen_t  salen 
)

Definition at line 2478 of file switch_utils.c.

References buf, and switch_assert.

Referenced by switch_false(), and switch_get_addr().

2479 {
2480  switch_assert(buf);
2481  *buf = '\0';
2482 
2483  if (sa) {
2484 #if defined(NTDDI_VERSION)
2485  switch_inet_ntop6((unsigned char*)&(sa->sin6_addr), buf, len);
2486 #else
2487  inet_ntop(AF_INET6, &(sa->sin6_addr), buf, len);
2488 #endif
2489  }
2490 
2491  return buf;
2492 }
switch_byte_t switch_byte_t * buf
#define switch_assert(expr)

◆ get_addr_int()

int get_addr_int ( switch_sockaddr_t sa)

Definition at line 2367 of file switch_utils.c.

Referenced by switch_false().

2368 {
2369  struct sockaddr_in *s = (struct sockaddr_in *) &sa->sa;
2370 
2371  return ntohs((unsigned short) s->sin_addr.s_addr);
2372 }

◆ get_port()

unsigned short get_port ( struct sockaddr *  sa)

get the port number of an ip address

Parameters
sathe struct sockaddr * to get the port from
Returns
the ip adress string

Definition at line 2505 of file switch_utils.c.

Referenced by switch_false().

2506 {
2507  unsigned short port = 0;
2508  if (sa) {
2509  switch (sa->sa_family) {
2510  case AF_INET:
2511  port = ntohs(((struct sockaddr_in *) sa)->sin_port);
2512  break;
2513  case AF_INET6:
2514  port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
2515  break;
2516  }
2517  }
2518  return port;
2519 }

◆ old_switch_tolower()

int old_switch_tolower ( int  c)

Definition at line 3877 of file switch_utils.c.

Referenced by switch_tolower_max().

3878 {
3879  if ((unsigned int) c > 255)
3880  return (c);
3881  if (c < -1)
3882  return EOF;
3883  return ((_switch_tolower_tab_ + 1)[c]);
3884 }
const short * _switch_tolower_tab_

◆ old_switch_toupper()

int old_switch_toupper ( int  c)

Definition at line 3830 of file switch_utils.c.

Referenced by switch_tolower_max().

3831 {
3832  if ((unsigned int) c > 255)
3833  return (c);
3834  if (c < -1)
3835  return EOF;
3836  return ((_switch_toupper_tab_ + 1)[c]);
3837 }
const short * _switch_toupper_tab_

◆ switch_amp_encode()

char* switch_amp_encode ( char *  s,
char *  buf,
switch_size_t  len 
)

Definition at line 939 of file switch_utils.c.

References buf, and switch_assert.

Referenced by switch_string_has_escaped_data().

940 {
941  char *p, *q;
942  switch_size_t x = 0;
943  switch_assert(s);
944 
945  q = buf;
946 
947  for (p = s; x < len; p++) {
948  switch (*p) {
949 
950  case '"':
951  if (x + 6 > len - 1) {
952  goto end;
953  }
954  *q++ = '&';
955  *q++ = 'q';
956  *q++ = 'u';
957  *q++ = 'o';
958  *q++ = 't';
959  *q++ = ';';
960  x += 6;
961  break;
962  case '\'':
963  if (x + 6 > len - 1) {
964  goto end;
965  }
966  *q++ = '&';
967  *q++ = 'a';
968  *q++ = 'p';
969  *q++ = 'o';
970  *q++ = 's';
971  *q++ = ';';
972  x += 6;
973  break;
974  case '&':
975  if (x + 5 > len - 1) {
976  goto end;
977  }
978  *q++ = '&';
979  *q++ = 'a';
980  *q++ = 'm';
981  *q++ = 'p';
982  *q++ = ';';
983  x += 5;
984  break;
985  case '<':
986  if (x + 4 > len - 1) {
987  goto end;
988  }
989  *q++ = '&';
990  *q++ = 'l';
991  *q++ = 't';
992  *q++ = ';';
993  x += 4;
994  break;
995  case '>':
996  if (x + 4 > len - 1) {
997  goto end;
998  }
999  *q++ = '&';
1000  *q++ = 'g';
1001  *q++ = 't';
1002  *q++ = ';';
1003  x += 4;
1004  break;
1005  default:
1006  if (x + 1 > len - 1) {
1007  goto end;
1008  }
1009  *q++ = *p;
1010  x++;
1011  if (*p == '\0') {
1012  goto end;
1013  }
1014  break;
1015  }
1016  }
1017 
1018  end:
1019 
1020  return buf;
1021 }
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define switch_assert(expr)

◆ switch_ast2regex()

switch_bool_t switch_ast2regex ( const char *  pat,
char *  rbuf,
size_t  len 
)

Definition at line 1388 of file switch_utils.c.

References memset(), SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_regex_perform().

1389 {
1390  const char *p = pat;
1391 
1392  if (!pat) {
1393  return SWITCH_FALSE;
1394  }
1395 
1396  memset(rbuf, 0, len);
1397 
1398  *(rbuf + strlen(rbuf)) = '^';
1399 
1400  while (p && *p) {
1401  if (*p == 'N') {
1402  strncat(rbuf, "[2-9]", len - strlen(rbuf));
1403  } else if (*p == 'X') {
1404  strncat(rbuf, "[0-9]", len - strlen(rbuf));
1405  } else if (*p == 'Z') {
1406  strncat(rbuf, "[1-9]", len - strlen(rbuf));
1407  } else if (*p == '.') {
1408  strncat(rbuf, ".*", len - strlen(rbuf));
1409  } else if (strlen(rbuf) < len - 1) {
1410  *(rbuf + strlen(rbuf)) = *p;
1411  }
1412  p++;
1413  }
1414  *(rbuf + strlen(rbuf)) = '$';
1415 
1416  return strcmp(pat, rbuf) ? SWITCH_TRUE : SWITCH_FALSE;
1417 }
memset(buf, 0, buflen)

◆ switch_atoui()

unsigned int switch_atoui ( const char *  nptr)

Definition at line 4293 of file switch_utils.c.

Referenced by switch_ivr_process_fh().

4294 {
4295  int tmp = atoi(nptr);
4296  if (tmp < 0) return 0;
4297  else return (unsigned int) tmp;
4298 }

◆ switch_atoul()

unsigned long switch_atoul ( const char *  nptr)

Definition at line 4300 of file switch_utils.c.

Referenced by switch_load_core_config().

4301 {
4302  long tmp = atol(nptr);
4303  if (tmp < 0) return 0;
4304  else return (unsigned long) tmp;
4305 }

◆ switch_b64_decode()

switch_size_t switch_b64_decode ( const char *  in,
char *  out,
switch_size_t  olen 
)

Definition at line 1064 of file switch_utils.c.

References b, switch_network_node::ip, and switch_b64_table.

Referenced by switch_core_media_add_crypto(), switch_jwt_verify(), and switch_string_has_escaped_data().

1065 {
1066 
1067  char l64[256];
1068  int b = 0, c, l = 0, i;
1069  const char *ip;
1070  char *op = out;
1071  size_t ol = 0;
1072 
1073  for (i = 0; i < 256; i++) {
1074  l64[i] = -1;
1075  }
1076 
1077  for (i = 0; i < 64; i++) {
1078  l64[(int) switch_b64_table[i]] = (char) i;
1079  }
1080 
1081  for (ip = in; ip && *ip; ip++) {
1082  c = l64[(int) *ip];
1083  if (c == -1) {
1084  continue;
1085  }
1086 
1087  b = (b << 6) + c;
1088  l += 6;
1089 
1090  while (l >= 8) {
1091  op[ol++] = (char) ((b >> (l -= 8)) % 256);
1092  if (ol >= olen - 1) {
1093  goto end;
1094  }
1095  }
1096  }
1097 
1098  end:
1099 
1100  op[ol+1] = '\0';
1101 
1102  return ol;
1103 }
static const char switch_b64_table[65]
const cJSON *const b
Definition: switch_cJSON.h:243
switch_byte_t in
char * ip
Definition: switch_msrp.c:60

◆ switch_b64_encode()

switch_status_t switch_b64_encode ( unsigned char *  in,
switch_size_t  ilen,
unsigned char *  out,
switch_size_t  olen 
)

Definition at line 1025 of file switch_utils.c.

References b, switch_b64_table, and SWITCH_STATUS_SUCCESS.

Referenced by switch_core_media_build_crypto(), switch_img_data_url(), switch_jwt_sign(), switch_rtp_add_crypto_key(), and switch_string_has_escaped_data().

1026 {
1027  int y = 0, bytes = 0;
1028  size_t x = 0;
1029  unsigned int b = 0, l = 0;
1030 
1031  for (x = 0; x < ilen; x++) {
1032  b = (b << 8) + in[x];
1033  l += 8;
1034 
1035  while (l >= 6) {
1036  out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64];
1037  if (bytes >= (int)olen - 1) {
1038  goto end;
1039  }
1040  if (++y != 72) {
1041  continue;
1042  }
1043  /* out[bytes++] = '\n'; */
1044  y = 0;
1045  }
1046  }
1047 
1048  if (l > 0) {
1049  out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64];
1050  }
1051  if (l != 0) {
1052  while (l < 6 && bytes < (int)olen - 1) {
1053  out[bytes++] = '=', l += 2;
1054  }
1055  }
1056 
1057  end:
1058 
1059  out[bytes] = '\0';
1060 
1061  return SWITCH_STATUS_SUCCESS;
1062 }
static const char switch_b64_table[65]
const cJSON *const b
Definition: switch_cJSON.h:243
switch_byte_t in

◆ switch_build_uri()

int switch_build_uri ( char *  uri,
switch_size_t  size,
const char *  scheme,
const char *  user,
const switch_sockaddr_t sa,
int  flags 
)

build a URI string from components

Parameters
urioutput string
sizemaximum size of output string (including trailing null)
schemeURI scheme
useruser part or null if none
sahost address
flagslogical OR-ed combination of flags from switch_uri_flags
Returns
number of characters printed (not including the trailing null)

Definition at line 2521 of file switch_utils.c.

References switch_snprintf(), SWITCH_URI_NO_SCOPE, SWITCH_URI_NUMERIC_HOST, and SWITCH_URI_NUMERIC_PORT.

2522 {
2523  char host[NI_MAXHOST], serv[NI_MAXSERV];
2524  struct sockaddr_in6 si6;
2525  const struct sockaddr *addr;
2526  const char *colon;
2527 
2528  if (flags & SWITCH_URI_NO_SCOPE && sa->family == AF_INET6) {
2529  memcpy(&si6, &sa->sa, sa->salen);
2530  si6.sin6_scope_id = 0;
2531 
2532  addr = (const struct sockaddr *) &si6;
2533  } else {
2534  addr = (const struct sockaddr *) (intptr_t) & sa->sa;
2535  }
2536 
2537  if (getnameinfo(addr, sa->salen, host, sizeof(host), serv, sizeof(serv),
2538  ((flags & SWITCH_URI_NUMERIC_HOST) ? NI_NUMERICHOST : 0) | ((flags & SWITCH_URI_NUMERIC_PORT) ? NI_NUMERICSERV : 0)) != 0) {
2539  return 0;
2540  }
2541 
2542  colon = strchr(host, ':');
2543 
2544  return switch_snprintf(uri, size, "%s:%s%s%s%s%s%s%s", scheme,
2545  user ? user : "", user ? "@" : "", colon ? "[" : "", host, colon ? "]" : "", serv[0] ? ":" : "", serv[0] ? serv : "");
2546 }
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)

◆ switch_calc_bitrate()

static int32_t switch_calc_bitrate ( int  w,
int  h,
float  quality,
double  fps 
)
inlinestatic

Definition at line 1100 of file switch_utils.h.

Referenced by video_write_thread().

1101 {
1102  int r;
1103 
1104  if (quality == 0) {
1105  quality = 1;
1106  }
1107 
1108  /* KUSH GAUGE*/
1109 
1110  if (!fps) fps = 15;
1111 
1112  r = (int32_t)((double)(w * h * fps * quality) * 0.07) / 1000;
1113 
1114  if (!quality) r /= 2;
1115 
1116  if (quality < 0.0f) {
1117  r = (int) ((float)r * quality);
1118  }
1119 
1120  return r;
1121 
1122 }

◆ switch_calc_fps()

static void switch_calc_fps ( switch_fps_t fpsP,
float  fps,
int  samplerate 
)
inlinestatic

Definition at line 1124 of file switch_utils.h.

References switch_fps_s::fps, switch_fps_s::ms, and switch_fps_s::samples.

1125 {
1126  /*
1127  implicit/truncf() - leave us with equal-or-smaller ms and thus equal-or-bigger fps, which is better for quality (than roundf()).
1128  also equal-or-bigger fps is better for things like (int)fps
1129  */
1130  fpsP->ms = (int)(1000.0f / fps);
1131  fpsP->fps = 1000.0f / fpsP->ms;
1132  fpsP->samples = (int)(samplerate / 1000 * fpsP->ms); // samplerate 99.99% is a factor of 1000, so we safe here with integer div by 1000
1133  return;
1134 }

◆ switch_calloc()

void* switch_calloc ( size_t  nmemb,
size_t  size 
)

Definition at line 86 of file switch_utils.c.

87 {
88  return calloc(nmemb, size);
89 }

◆ switch_char_to_rfc2833()

unsigned char switch_char_to_rfc2833 ( char  key)

Return the RFC2833 event based on an key character.

Parameters
keythe charecter to encode
Returns
the event id for the specified character or -1 on an invalid input

Definition at line 2556 of file switch_utils.c.

References RFC2833_CHARS, and switch_toupper().

Referenced by do_2833(), and switch_errno_is_break().

2557 {
2558  char *c;
2559  unsigned char counter = 0;
2560 
2561  key = (char) switch_toupper(key);
2562  for (c = RFC2833_CHARS; *c; c++) {
2563  if (*c == key) {
2564  return counter;
2565  }
2566  counter++;
2567  }
2568  return '\0';
2569 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:101
static char RFC2833_CHARS[]
char * key
Definition: switch_msrp.c:64

◆ switch_clean_name_string()

static char* switch_clean_name_string ( char *  s)
inlinestatic

Definition at line 827 of file switch_utils.h.

828 {
829  char *p;
830  for (p = s; p && *p; p++) {
831  uint8_t x = (uint8_t) * p;
832  if ((x < 32) || x == '\'' || x == '"' || x == '<' || x == '>' || x == '\\' || x == ':' || x == '@' || x == '/') {
833  *p = ' ';
834  }
835  if ( (p == s) && (*p == ' ') ) {
836  s++;
837  }
838  }
839 
840  return s;
841 }

◆ switch_clean_string()

static char* switch_clean_string ( char *  s)
inlinestatic

Definition at line 813 of file switch_utils.h.

814 {
815  char *p;
816  for (p = s; p && *p; p++) {
817  uint8_t x = (uint8_t) * p;
818  if ((x < 32) && x != '\n' && x != '\r') {
819  *p = ' ';
820  }
821  }
822 
823  return s;
824 }

◆ switch_cmp_addr()

int switch_cmp_addr ( switch_sockaddr_t sa1,
switch_sockaddr_t sa2,
switch_bool_t  ip_only 
)

Definition at line 2374 of file switch_utils.c.

Referenced by do_dtls(), handle_ice(), read_rtcp_packet(), rtp_common_read(), and switch_false().

2375 {
2376  struct sockaddr_in *s1;
2377  struct sockaddr_in *s2;
2378 
2379  struct sockaddr_in6 *s16;
2380  struct sockaddr_in6 *s26;
2381 
2382  struct sockaddr *ss1;
2383  struct sockaddr *ss2;
2384 
2385  if (!(sa1 && sa2))
2386  return 0;
2387 
2388  s1 = (struct sockaddr_in *) &sa1->sa;
2389  s2 = (struct sockaddr_in *) &sa2->sa;
2390 
2391  s16 = (struct sockaddr_in6 *) &sa1->sa;
2392  s26 = (struct sockaddr_in6 *) &sa2->sa;
2393 
2394  ss1 = (struct sockaddr *) &sa1->sa;
2395  ss2 = (struct sockaddr *) &sa2->sa;
2396 
2397  if (ss1->sa_family != ss2->sa_family)
2398  return 0;
2399 
2400  switch (ss1->sa_family) {
2401  case AF_INET:
2402  if (ip_only) {
2403  return (s1->sin_addr.s_addr == s2->sin_addr.s_addr);
2404  } else {
2405  return (s1->sin_addr.s_addr == s2->sin_addr.s_addr && s1->sin_port == s2->sin_port);
2406  }
2407  case AF_INET6:
2408  {
2409  int i;
2410 
2411  if (!ip_only) {
2412  if (s16->sin6_port != s26->sin6_port) return 0;
2413  }
2414 
2415  for (i = 0; i < 4; i++) {
2416  if (*((int32_t *) s16->sin6_addr.s6_addr + i) != *((int32_t *) s26->sin6_addr.s6_addr + i)) return 0;
2417  }
2418 
2419  return 1;
2420  }
2421  }
2422 
2423  return 0;
2424 }

◆ switch_core_session_url_encode()

char* switch_core_session_url_encode ( switch_core_session_t session,
const char *  url 
)

Definition at line 3550 of file switch_utils.c.

References switch_core_session_get_pool(), switch_core_url_encode_opt(), and SWITCH_FALSE.

Referenced by switch_needs_url_encode().

3551 {
3553 }
char * switch_core_url_encode_opt(switch_memory_pool_t *pool, const char *url, switch_bool_t double_encode)
switch_memory_pool_t * switch_core_session_get_pool(_In_ switch_core_session_t *session)
Retrieve the memory pool from a session.

◆ switch_core_session_url_encode_opt()

char* switch_core_session_url_encode_opt ( switch_core_session_t session,
const char *  url,
switch_bool_t  double_encode 
)

Definition at line 3555 of file switch_utils.c.

References switch_core_session_get_pool(), and switch_core_url_encode_opt().

Referenced by switch_needs_url_encode().

3556 {
3557  return switch_core_url_encode_opt(switch_core_session_get_pool(session), url, double_encode);
3558 }
char * switch_core_url_encode_opt(switch_memory_pool_t *pool, const char *url, switch_bool_t double_encode)
switch_memory_pool_t * switch_core_session_get_pool(_In_ switch_core_session_t *session)
Retrieve the memory pool from a session.

◆ switch_core_url_encode()

char* switch_core_url_encode ( switch_memory_pool_t pool,
const char *  url 
)

Definition at line 3560 of file switch_utils.c.

References switch_core_url_encode_opt(), and SWITCH_FALSE.

Referenced by switch_needs_url_encode().

3561 {
3563 }
char * switch_core_url_encode_opt(switch_memory_pool_t *pool, const char *url, switch_bool_t double_encode)
switch_memory_pool_t * pool

◆ switch_core_url_encode_opt()

char* switch_core_url_encode_opt ( switch_memory_pool_t pool,
const char *  url,
switch_bool_t  double_encode 
)

Definition at line 3565 of file switch_utils.c.

References end_of_p, switch_network_node::ok, switch_core_alloc, switch_core_strdup, switch_url_encode_opt(), and SWITCH_URL_UNSAFE.

Referenced by switch_core_session_url_encode(), switch_core_session_url_encode_opt(), switch_core_url_encode(), and switch_needs_url_encode().

3566 {
3567  const char hex[] = "0123456789ABCDEF";
3568  switch_size_t len = 0;
3569  switch_size_t slen = 0;
3570  const char *p, *e;
3571 
3572  if (!url) return NULL;
3573  if (!pool) return NULL;
3574 
3575  e = end_of_p(url);
3576 
3577  for (p = url; *p; p++) {
3578  int ok = 0;
3579 
3580  len++;
3581  slen++;
3582 
3583  if (!double_encode && *p == '%' && e-p > 1) {
3584  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
3585  ok = 1;
3586  }
3587  }
3588 
3589  if (!ok && (*p < ' ' || *p > '~' || strchr(SWITCH_URL_UNSAFE, *p))) {
3590  len += 2;
3591  }
3592  }
3593 
3594  slen++;
3595  len++; /* NULL Terminatior */
3596 
3597  if (slen == len) {
3598  return switch_core_strdup(pool, url);
3599  } else {
3600  return switch_url_encode_opt(url, switch_core_alloc(pool, sizeof(char) * len), len, double_encode);
3601  }
3602 }
char * switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode)
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
switch_memory_pool_t * pool
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
#define end_of_p(_s)
Definition: switch_utils.h:686
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
uintptr_t switch_size_t

◆ switch_cp_addr()

int switch_cp_addr ( switch_sockaddr_t sa1,
switch_sockaddr_t sa2 
)

Definition at line 2427 of file switch_utils.c.

Referenced by read_rtp_packet(), and switch_false().

2428 {
2429  struct sockaddr_in *s1;
2430  struct sockaddr_in *s2;
2431 
2432  struct sockaddr_in6 *s16;
2433  struct sockaddr_in6 *s26;
2434 
2435  struct sockaddr *ss1;
2436  //struct sockaddr *ss2;
2437 
2438  if (!(sa1 && sa2))
2439  return 0;
2440 
2441  s1 = (struct sockaddr_in *) &sa1->sa;
2442  s2 = (struct sockaddr_in *) &sa2->sa;
2443 
2444  s16 = (struct sockaddr_in6 *) &sa1->sa;
2445  s26 = (struct sockaddr_in6 *) &sa2->sa;
2446 
2447  ss1 = (struct sockaddr *) &sa1->sa;
2448  //ss2 = (struct sockaddr *) &sa2->sa;
2449 
2450  sa1->port = sa2->port;
2451  sa1->family = sa2->family;
2452 
2453  sa1->sa.sin.sin_family = sa2->family;
2454 
2455  switch (ss1->sa_family) {
2456  case AF_INET:
2457  s1->sin_addr.s_addr = s2->sin_addr.s_addr;
2458  s1->sin_port = s2->sin_port;
2459 
2460  return 1;
2461  case AF_INET6:
2462  {
2463  int i;
2464 
2465  s16->sin6_port = s26->sin6_port;
2466 
2467  for (i = 0; i < 4; i++) {
2468  *((int32_t *) s16->sin6_addr.s6_addr + i) = *((int32_t *) s26->sin6_addr.s6_addr + i);
2469  }
2470 
2471  return 1;
2472  }
2473  }
2474 
2475  return 0;
2476 }

◆ switch_cut_path()

const char* switch_cut_path ( const char *  in)

Create a pointer to the file name in a given file path eliminating the directory name.

Returns
the pointer to the next character after the final / or \ characters

Definition at line 3005 of file switch_utils.c.

References in.

Referenced by switch_console_printf(), switch_event_prep_for_delivery_detailed(), switch_loadable_module_load_module_ex(), switch_log_meta_vprintf(), and switch_simple_email().

3006 {
3007  const char *p, *ret = in;
3008  const char delims[] = "/\\";
3009  const char *i;
3010 
3011  if (in) {
3012  for (i = delims; *i; i++) {
3013  p = in;
3014  while ((p = strchr(p, *i)) != 0) {
3015  ret = ++p;
3016  }
3017  }
3018  return ret;
3019  } else {
3020  return NULL;
3021  }
3022 }
switch_byte_t in

◆ switch_digest()

switch_status_t switch_digest ( const char *  digest_name,
unsigned char **  digest,
const void *  input,
switch_size_t  inputLen,
unsigned int *  outputlen 
)

Definition at line 4715 of file switch_utils.c.

References switch_assert, SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_digest_string().

4716 {
4717 #if defined(HAVE_OPENSSL)
4718  EVP_MD_CTX *mdctx;
4719  const EVP_MD *md;
4720  int size;
4721 
4722  switch_assert(digest);
4723 
4724  if (!digest_name) {
4725  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Message digest is not set\n");
4726  return SWITCH_STATUS_FALSE;
4727  }
4728 
4729  md = EVP_get_digestbyname(digest_name);
4730 
4731  if (!md) {
4732  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown message digest %s\n", digest_name);
4733  return SWITCH_STATUS_FALSE;
4734  }
4735 
4736  size = EVP_MD_size(md);
4737  if (!size || !(*digest = malloc(size))) {
4738  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Zero digest size or can't allocate memory to store results %s\n", digest_name);
4739  return SWITCH_STATUS_FALSE;
4740  }
4741 
4742 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4743  mdctx = EVP_MD_CTX_new();
4744 #else
4745  mdctx = EVP_MD_CTX_create();
4746 #endif
4747 
4748  if (!mdctx) {
4749  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "EVP_MD_CTX_new error\n");
4750  switch_safe_free(*digest);
4751  return SWITCH_STATUS_FALSE;
4752  }
4753 
4754  EVP_MD_CTX_init(mdctx);
4755  EVP_DigestInit_ex(mdctx, md, NULL);
4756  EVP_DigestUpdate(mdctx, input, inputLen);
4757  EVP_DigestFinal_ex(mdctx, *digest, outputlen);
4758 
4759 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4760  EVP_MD_CTX_free(mdctx);
4761 #else
4762  EVP_MD_CTX_destroy(mdctx);
4763 #endif
4764 
4765  return SWITCH_STATUS_SUCCESS;
4766 #else
4767  return SWITCH_STATUS_FALSE;
4768 #endif
4769 }
#define SWITCH_CHANNEL_LOG
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)

◆ switch_digest_string()

switch_status_t switch_digest_string ( const char *  digest_name,
char **  digest_str,
const void *  input,
switch_size_t  inputLen,
unsigned int *  outputlen 
)

Definition at line 4771 of file switch_utils.c.

References b, switch_digest(), switch_safe_free, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

4772 {
4773  unsigned char *digest = NULL;
4774  switch_status_t status;
4775  short i = 0, x;
4776  uint8_t b;
4777 
4778  status = switch_digest(digest_name, &digest, input, inputLen, outputlen);
4779 
4780  if (status == SWITCH_STATUS_SUCCESS) {
4781  if ((*digest_str = malloc(*outputlen * 2 + 1))) {
4782  for (x = i = 0; x < *outputlen; x++) {
4783  b = (digest[x] >> 4) & 15;
4784  (*digest_str)[i++] = b + (b > 9 ? 'a' - 10 : '0');
4785  b = digest[x] & 15;
4786  (*digest_str)[i++] = b + (b > 9 ? 'a' - 10 : '0');
4787  }
4788 
4789  (*digest_str)[i] = '\0';
4790  } else {
4791  switch_safe_free(digest);
4792  *outputlen = 0;
4793  return SWITCH_STATUS_FALSE;
4794  }
4795  }
4796 
4797  switch_safe_free(digest);
4798  *outputlen = i;
4799 
4800  return status;
4801 }
const cJSON *const b
Definition: switch_cJSON.h:243
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
switch_status_t
Common return values.
switch_status_t switch_digest(const char *digest_name, unsigned char **digest, const void *input, switch_size_t inputLen, unsigned int *outputlen)

◆ switch_dow_cmp()

switch_bool_t switch_dow_cmp ( const char *  exp,
int  val 
)

Definition at line 4100 of file switch_utils.c.

References _dow_read_token(), cur, DOW_COMA, DOW_EOF, DOW_ERR, DOW_HYPHEN, SWITCH_CHANNEL_LOG, SWITCH_FALSE, SWITCH_LOG_ERROR, switch_log_printf(), and SWITCH_TRUE.

Referenced by switch_xml_std_datetime_check().

4101 {
4102  dow_t cur, prev = DOW_EOF, range_start = DOW_EOF;
4103  const char *p = exp;
4104 
4105  while ((cur = _dow_read_token(&p)) != DOW_EOF) {
4106  if (cur == DOW_COMA) {
4107  /* Reset state */
4108  cur = DOW_EOF;
4109  } else if (cur == DOW_HYPHEN) {
4110  /* Save the previous token and move to the next one */
4111  range_start = prev;
4112  } else if (cur == DOW_ERR) {
4113  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse error for [%s] at position %ld (%.6s)\n", exp, (long) (p - exp), p);
4114  break;
4115  } else {
4116  /* Valid day found */
4117  if (range_start != DOW_EOF) { /* Evaluating a range */
4118  if (range_start <= cur ? (val >= range_start && val <= cur) : (val >= range_start || val <= cur)) {
4119  return SWITCH_TRUE;
4120  }
4121  range_start = DOW_EOF;
4122  } else if (val == cur) {
4123  return SWITCH_TRUE;
4124  }
4125  }
4126 
4127  prev = cur;
4128  }
4129 
4130  return SWITCH_FALSE;
4131 }
#define SWITCH_CHANNEL_LOG
pack cur
static dow_t _dow_read_token(const char **s)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
dow_t

◆ switch_dow_int2str()

const char* switch_dow_int2str ( int  val)

Definition at line 4042 of file switch_utils.c.

References switch_arraylen.

Referenced by switch_xml_std_datetime_check().

4042  {
4043  if (val >= switch_arraylen(DOW)) {
4044  val = val % switch_arraylen(DOW);
4045  }
4046  return DOW[val];
4047 }
#define switch_arraylen(_a)
Definition: switch_utils.h:374
static const char * DOW[]

◆ switch_dow_str2int()

int switch_dow_str2int ( const char *  exp)

Definition at line 4049 of file switch_utils.c.

References switch_arraylen.

Referenced by _dow_read_token().

4049  {
4050  int ret = -1;
4051  int x;
4052 
4053  for (x = 0; x < switch_arraylen(DOW); x++) {
4054  if (!strncasecmp(DOW[x], exp, 3)) {
4055  ret = x + 1;
4056  break;
4057  }
4058  }
4059  return ret;
4060 }
#define switch_arraylen(_a)
Definition: switch_utils.h:374
static const char * DOW[]

◆ switch_dtmftoi()

static int switch_dtmftoi ( char *  s)
inlinestatic

Definition at line 463 of file switch_utils.h.

References switch_assert.

Referenced by meta_on_dtmf(), and switch_ivr_bind_dtmf_meta_session().

464 {
465  int r;
466 
467  switch_assert(s);
468 
469  if (!(r = atoi(s))) {
470  int l = tolower((unsigned char)*s);
471  if (l > 96 && l < 101) {
472  r = l - 87;
473  }
474  }
475 
476  return r;
477 }
#define switch_assert(expr)

◆ switch_errno_is_break()

static int switch_errno_is_break ( int  errcode)
inlinestatic

Definition at line 649 of file switch_utils.h.

References key, switch_char_to_rfc2833(), SWITCH_DECLARE, switch_priority_name(), and switch_rfc2833_to_char().

Referenced by switch_util_quote_shell_arg_pool(), switch_wait_sock(), and switch_wait_socklist().

650 {
651  return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
652 }

◆ switch_escape_char()

char* switch_escape_char ( switch_memory_pool_t pool,
char *  in,
const char *  delim,
char  esc 
)

Escape a string by prefixing a list of characters with an escape character.

Parameters
poola memory pool to use
inthe string
delimthe list of characters to escape
escthe escape character
Returns
the escaped string

Definition at line 2571 of file switch_utils.c.

References count, in, and switch_core_alloc.

2572 {
2573  char *data;
2574  const char *p, *d;
2575  int count = 1, i = 0;
2576 
2577  p = in;
2578  while (*p) {
2579  d = delim;
2580  while (*d) {
2581  if (*p == *d) {
2582  count++;
2583  }
2584  d++;
2585  }
2586  p++;
2587  }
2588 
2589  if (count == 1) {
2590  return in;
2591  }
2592 
2593  data = switch_core_alloc(pool, strlen(in) + count);
2594 
2595  p = in;
2596  while (*p) {
2597  d = delim;
2598  while (*d) {
2599  if (*p == *d) {
2600  data[i++] = esc;
2601  }
2602  d++;
2603  }
2604  data[i++] = *p;
2605  p++;
2606  }
2607  return data;
2608 }
switch_memory_pool_t * pool
switch_byte_t in
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
int count
Definition: switch_cJSON.h:204

◆ switch_escape_string()

char* switch_escape_string ( const char *  in,
char *  out,
switch_size_t  outlen 
)

Definition at line 2642 of file switch_utils.c.

Referenced by switch_escape_string_pool().

2643 {
2644  const char *p;
2645  char *o = out;
2646 
2647  for (p = in; *p; p++) {
2648  switch (*p) {
2649  case '\n':
2650  *o++ = '\\';
2651  *o++ = 'n';
2652  break;
2653  case '\r':
2654  *o++ = '\\';
2655  *o++ = 'r';
2656  break;
2657  case '\t':
2658  *o++ = '\\';
2659  *o++ = 't';
2660  break;
2661  case ' ':
2662  *o++ = '\\';
2663  *o++ = 's';
2664  break;
2665  case '$':
2666  *o++ = '\\';
2667  *o++ = '$';
2668  break;
2669  default:
2670  *o++ = *p;
2671  break;
2672  }
2673  }
2674 
2675  *o++ = '\0';
2676 
2677  return out;
2678 }
switch_byte_t in

◆ switch_escape_string_pool()

char* switch_escape_string_pool ( const char *  in,
switch_memory_pool_t pool 
)

Definition at line 2680 of file switch_utils.c.

References buf, switch_core_alloc, and switch_escape_string().

2681 {
2682  size_t len = strlen(in) * 2 + 1;
2683  char *buf = switch_core_alloc(pool, len);
2684  return switch_escape_string(in, buf, len);
2685 }
switch_memory_pool_t * pool
char * switch_escape_string(const char *in, char *out, switch_size_t outlen)
switch_byte_t switch_byte_t * buf
switch_byte_t in
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684

◆ switch_false()

static int switch_false ( const char *  expr)
inlinestatic

Evaluate the falsefullness of a string expression.

Parameters
expra string expression
Returns
true or false

Definition at line 551 of file switch_utils.h.

References _In_, _In_opt_, _Out_opt_bytecapcount_, buf, buflen, get_addr(), get_addr6(), get_addr_int(), get_port(), switch_cmp_addr(), switch_cp_addr(), SWITCH_DECLARE, SWITCH_FALSE, switch_find_interface_ip(), switch_find_local_ip(), switch_is_number(), switch_resolve_host(), and SWITCH_TRUE.

Referenced by check_jb(), switch_channel_var_false(), switch_core_media_gen_local_sdp(), switch_core_media_negotiate_sdp(), switch_core_media_receive_message(), switch_core_perform_file_open(), switch_core_session_parse_crypto_prefs(), switch_ivr_originate(), switch_xml_config_parse_event(), and video_helper_thread().

552 {
553  return ((expr && ( !strcasecmp(expr, "no") ||
554  !strcasecmp(expr, "off") ||
555  !strcasecmp(expr, "false") ||
556  !strcasecmp(expr, "f") ||
557  !strcasecmp(expr, "disabled") ||
558  !strcasecmp(expr, "inactive") ||
559  !strcasecmp(expr, "disallow") ||
560  (switch_is_number(expr) && !atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
561 }
switch_bool_t switch_is_number(const char *str)

◆ switch_fd_read_dline()

switch_size_t switch_fd_read_dline ( int  fd,
char **  buf,
switch_size_t len 
)

Definition at line 844 of file switch_utils.c.

References buf, cur, DLINE_BLOCK_SIZE, DLINE_MAX_SIZE, memset(), switch_assert, SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, and switch_log_printf().

Referenced by switch_known_bitrate().

845 {
846  char c, *p;
847  int cur;
848  switch_size_t total = 0;
849  char *data = *buf;
850  switch_size_t ilen = *len;
851 
852  if (!data) {
853  *len = ilen = DLINE_BLOCK_SIZE;
854  data = malloc(ilen);
855  memset(data, 0, ilen);
856  }
857 
858  p = data;
859  while ((cur = read(fd, &c, 1)) == 1) {
860 
861  if (total + 2 >= ilen) {
862  if (ilen + DLINE_BLOCK_SIZE > DLINE_MAX_SIZE) {
863  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Single line limit reached!\n");
864  break;
865  }
866 
867  ilen += DLINE_BLOCK_SIZE;
868  data = realloc(data, ilen);
869  switch_assert(data);
870  p = data + total;
871 
872  }
873 
874  total += cur;
875  *p++ = c;
876 
877  if (c == '\r' || c == '\n') {
878  break;
879  }
880  }
881 
882  *p++ = '\0';
883 
884  *len = ilen;
885  *buf = data;
886 
887  return total;
888 }
#define SWITCH_CHANNEL_LOG
#define DLINE_BLOCK_SIZE
Definition: switch_utils.c:842
pack cur
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define DLINE_MAX_SIZE
Definition: switch_utils.c:843
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
memset(buf, 0, buflen)

◆ switch_fd_read_line()

switch_size_t switch_fd_read_line ( int  fd,
char *  buf,
switch_size_t  len 
)

Definition at line 822 of file switch_utils.c.

References buf, and cur.

Referenced by switch_known_bitrate().

823 {
824  char c, *p;
825  int cur;
826  switch_size_t total = 0;
827 
828  p = buf;
829  while (total + 2 < len && (cur = read(fd, &c, 1)) == 1) {
830  total += cur;
831  *p++ = c;
832  if (c == '\r' || c == '\n') {
833  break;
834  }
835  }
836 
837  *p++ = '\0';
838  assert(total < len);
839  return total;
840 }
pack cur
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t

◆ switch_filecmp()

static int switch_filecmp ( const char *  a,
const char *  b 
)
inlinestatic

Definition at line 1306 of file switch_utils.h.

References switch_find_end_paren(), and zstr.

1307 {
1308  const char *e;
1309 
1310  if (zstr(a) || zstr(b)) {
1311  return -1;
1312  }
1313 
1314  while(*a == '{') {
1315  if ((e = switch_find_end_paren(a, '{', '}'))) {
1316  a = e + 1;
1317  while(*a == ' ') a++;
1318  }
1319  }
1320 
1321  while(*b == '{') {
1322  if ((e = switch_find_end_paren(b, '{', '}'))) {
1323  b = e + 1;
1324  while(*b == ' ') b++;
1325  }
1326  }
1327 
1328  return strcmp(a, b);
1329 }
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:796
const cJSON *const b
Definition: switch_cJSON.h:243
#define zstr(x)
Definition: switch_utils.h:314

◆ switch_find_end_paren()

char* switch_find_end_paren ( const char *  s,
char  open,
char  close 
)

Definition at line 796 of file switch_utils.c.

Referenced by expand_vars(), switch_core_asr_load_grammar(), switch_core_media_process_sdp_filter(), switch_core_speech_feed_tts(), switch_event_create_brackets(), switch_filecmp(), switch_is_file_path(), switch_ivr_displace_session(), switch_ivr_originate(), switch_ivr_play_file(), switch_ivr_record_file_event(), switch_ivr_record_session_event(), switch_needs_url_encode(), and switch_separate_file_params().

797 {
798  const char *e = NULL;
799  int depth = 0;
800 
801  while (s && *s && *s == ' ') {
802  s++;
803  }
804 
805  if (s && *s == open) {
806  depth++;
807  for (e = s + 1; e && *e; e++) {
808  if (*e == open && open != close) {
809  depth++;
810  } else if (*e == close) {
811  depth--;
812  if (!depth) {
813  break;
814  }
815  }
816  }
817  }
818 
819  return (e && *e == close) ? (char *) e : NULL;
820 }

◆ switch_find_interface_ip()

switch_status_t switch_find_interface_ip ( _Out_opt_bytecapcount_(len) char *  buf,
_In_ int  len,
_In_opt_ int *  mask,
_In_ const char *  ifname,
_In_ int  family 
)

find primary ip of the specified interface

Parameters
bufthe buffer to write the ip address found into
lenthe length of the buf
maskthe CIDR found (AF_INET only)
ifnameinterface name to check
familythe address family to return (AF_INET or AF_INET6)
Returns
SWITCH_STATUS_SUCCESSS for success, otherwise failure

Referenced by switch_false().

◆ switch_find_local_ip()

switch_status_t switch_find_local_ip ( _Out_opt_bytecapcount_(len) char *  buf,
_In_ int  len,
_In_opt_ int *  mask,
_In_ int  family 
)

find local ip of the box

Parameters
bufthe buffer to write the ip address found into
lenthe length of the buf
maskthe CIDR found (AF_INET only)
familythe address family to return (AF_INET or AF_INET6)
Returns
SWITCH_STATUS_SUCCESSS for success, otherwise failure

Referenced by check_ip(), switch_core_init(), switch_event_init(), switch_false(), switch_load_network_lists(), switch_nat_init(), and switch_stun_ip_lookup().

◆ switch_find_parameter()

char* switch_find_parameter ( const char *  str,
const char *  param,
switch_memory_pool_t pool 
)

Definition at line 415 of file switch_utils.c.

References switch_network_node::next, switch_core_alloc, and switch_snprintf().

Referenced by switch_known_bitrate().

416 {
417  char *e, *r = NULL, *ptr = NULL, *next = NULL;
418  size_t len;
419 
420  ptr = (char *) str;
421 
422  while (ptr) {
423  len = strlen(param);
424  e = ptr+len;
425  next = strchr(ptr, ';');
426 
427  if (!strncasecmp(ptr, param, len) && *e == '=') {
428  size_t mlen;
429 
430  ptr = ++e;
431 
432  if (next) {
433  e = next;
434  } else {
435  e = ptr + strlen(ptr);
436  }
437 
438  mlen = (e - ptr) + 1;
439 
440  if (pool) {
441  r = switch_core_alloc(pool, mlen);
442  } else {
443  r = malloc(mlen);
444  }
445 
446  switch_snprintf(r, mlen, "%s", ptr);
447 
448  break;
449  }
450 
451  if (next) {
452  ptr = next + 1;
453  } else break;
454  }
455 
456  return r;
457 }
switch_memory_pool_t * pool
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684

◆ switch_format_number()

char* switch_format_number ( const char *  num)

Definition at line 4256 of file switch_utils.c.

References switch_is_number(), switch_mprintf(), and zstr.

4257 {
4258  char *r;
4259  size_t len;
4260  const char *p = num;
4261 
4262  if (!p) {
4263  return (char*)p;
4264  }
4265 
4266  if (zstr(p)) {
4267  return strdup(p);
4268  }
4269 
4270  if (*p == '+') {
4271  p++;
4272  }
4273 
4274  if (!switch_is_number(p)) {
4275  return strdup(p);
4276  }
4277 
4278  len = strlen(p);
4279 
4280  /* region 1, TBD add more....*/
4281  if (len == 11 && p[0] == '1') {
4282  r = switch_mprintf("%c (%c%c%c) %c%c%c-%c%c%c%c", p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10]);
4283  } else if (len == 10) {
4284  r = switch_mprintf("1 (%c%c%c) %c%c%c-%c%c%c%c", p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9]);
4285  } else {
4286  r = strdup(num);
4287  }
4288 
4289  return r;
4290 }
#define zstr(x)
Definition: switch_utils.h:314
switch_bool_t switch_is_number(const char *str)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)

◆ switch_fp_read_dline()

switch_size_t switch_fp_read_dline ( FILE *  fd,
char **  buf,
switch_size_t len 
)

Definition at line 892 of file switch_utils.c.

References buf, DLINE_BLOCK_SIZE, DLINE_MAX_SIZE, memset(), switch_assert, SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, and switch_log_printf().

Referenced by load_mime_types(), preprocess(), switch_known_bitrate(), and switch_stream_write_file_contents().

893 {
894  char c, *p;
895  switch_size_t total = 0;
896  char *data = *buf;
897  switch_size_t ilen = *len;
898 
899  if (!data) {
900  *len = ilen = DLINE_BLOCK_SIZE;
901  data = malloc(ilen);
902  memset(data, 0, ilen);
903  }
904 
905  p = data;
906  //while ((c = fgetc(fd)) != EOF) {
907 
908  while (fread(&c, 1, 1, fd) == 1) {
909 
910  if (total + 2 >= ilen) {
911  if (ilen + DLINE_BLOCK_SIZE > DLINE_MAX_SIZE) {
912  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Single line limit reached!\n");
913  break;
914  }
915 
916  ilen += DLINE_BLOCK_SIZE;
917  data = realloc(data, ilen);
918  switch_assert(data);
919  p = data + total;
920 
921  }
922 
923  total++;
924  *p++ = c;
925 
926  if (c == '\r' || c == '\n') {
927  break;
928  }
929  }
930 
931  *p++ = '\0';
932 
933  *len = ilen;
934  *buf = data;
935 
936  return total;
937 }
#define SWITCH_CHANNEL_LOG
#define DLINE_BLOCK_SIZE
Definition: switch_utils.c:842
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define DLINE_MAX_SIZE
Definition: switch_utils.c:843
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)
memset(buf, 0, buflen)

◆ switch_frame_alloc()

switch_status_t switch_frame_alloc ( switch_frame_t **  frame,
switch_size_t  size 
)

Definition at line 109 of file switch_utils.c.

References switch_frame::buflen, switch_frame::data, SFF_DYNAMIC, switch_assert, switch_set_flag, SWITCH_STATUS_SUCCESS, and switch_zmalloc.

Referenced by switch_known_bitrate().

110 {
111  switch_frame_t *new_frame;
112 
113  switch_zmalloc(new_frame, sizeof(*new_frame));
114 
115  switch_set_flag(new_frame, SFF_DYNAMIC);
116  new_frame->buflen = (uint32_t)size;
117  new_frame->data = malloc(size);
118  switch_assert(new_frame->data);
119 
120  *frame = new_frame;
121 
122  return SWITCH_STATUS_SUCCESS;
123 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
uint32_t buflen
Definition: switch_frame.h:70
#define switch_zmalloc(ptr, len)
An abstraction of a data frame.
Definition: switch_frame.h:54
#define switch_assert(expr)

◆ switch_frame_buffer_create()

switch_status_t switch_frame_buffer_create ( switch_frame_buffer_t **  fbP,
switch_size_t  qlen 
)

Definition at line 303 of file switch_utils.c.

References switch_frame_buffer_s::mutex, pool, switch_frame_buffer_s::pool, switch_frame_buffer_s::queue, switch_core_alloc, switch_core_new_memory_pool, switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), and SWITCH_STATUS_SUCCESS.

Referenced by audio_write_thread().

304 {
307 
308  if (!qlen) qlen = 500;
309 
311  fb = switch_core_alloc(pool, sizeof(*fb));
312  fb->pool = pool;
313  switch_queue_create(&fb->queue, qlen, fb->pool);
315  *fbP = fb;
316 
317  return SWITCH_STATUS_SUCCESS;
318 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
switch_queue_t * queue
Definition: switch_utils.c:136
switch_memory_pool_t * pool
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
switch_mutex_t * mutex
Definition: switch_utils.c:137
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
struct fspr_pool_t switch_memory_pool_t
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1233
switch_memory_pool_t * pool
Definition: switch_utils.c:135

◆ switch_frame_buffer_destroy()

switch_status_t switch_frame_buffer_destroy ( switch_frame_buffer_t **  fbP)

Definition at line 292 of file switch_utils.c.

References pool, switch_frame_buffer_s::pool, switch_core_destroy_memory_pool, and SWITCH_STATUS_SUCCESS.

Referenced by switch_media_handle_destroy().

293 {
294  switch_frame_buffer_t *fb = *fbP;
296  *fbP = NULL;
297  pool = fb->pool;
299 
300  return SWITCH_STATUS_SUCCESS;
301 }
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
switch_memory_pool_t * pool
struct fspr_pool_t switch_memory_pool_t
switch_memory_pool_t * pool
Definition: switch_utils.c:135

◆ switch_frame_buffer_dup()

switch_status_t switch_frame_buffer_dup ( switch_frame_buffer_t fb,
switch_frame_t orig,
switch_frame_t **  clone 
)

Definition at line 250 of file switch_utils.c.

References switch_frame::buflen, find_free_frame(), switch_assert, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by perform_write().

251 {
252  switch_frame_t *new_frame;
253 
254  if (!orig) {
255  return SWITCH_STATUS_FALSE;
256  }
257 
258  switch_assert(orig->buflen);
259 
260  new_frame = find_free_frame(fb, orig);
261 
262  *clone = new_frame;
263 
264  return SWITCH_STATUS_SUCCESS;
265 }
static switch_frame_t * find_free_frame(switch_frame_buffer_t *fb, switch_frame_t *orig)
Definition: switch_utils.c:141
uint32_t buflen
Definition: switch_frame.h:70
An abstraction of a data frame.
Definition: switch_frame.h:54
#define switch_assert(expr)

◆ switch_frame_buffer_free()

switch_status_t switch_frame_buffer_free ( switch_frame_buffer_t fb,
switch_frame_t **  frameP 
)

Definition at line 217 of file switch_utils.c.

References switch_frame::extra_data, switch_frame_node_s::frame, switch_frame_buffer_s::head, switch_frame::img, switch_frame_node_s::inuse, switch_frame_buffer_s::mutex, switch_frame_node_s::next, switch_frame_node_s::prev, switch_assert, switch_img_free(), switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_SUCCESS, and switch_frame_buffer_s::total.

Referenced by audio_write_thread().

218 {
219  switch_frame_t *old_frame;
220  switch_frame_node_t *node;
221 
223 
224  old_frame = *frameP;
225  *frameP = NULL;
226 
227  node = (switch_frame_node_t *) old_frame->extra_data;
228  node->inuse = 0;
229  switch_img_free(&node->frame->img);
230 
231  fb->total++;
232 
233  if (fb->head) {
234  fb->head->prev = node;
235  }
236 
237  node->next = fb->head;
238  node->prev = NULL;
239  fb->head = node;
240 
241  switch_assert(node->next != node);
242  switch_assert(node->prev != node);
243 
244 
246 
247  return SWITCH_STATUS_SUCCESS;
248 }
void switch_img_free(switch_image_t **img)
Close an image descriptor.
switch_frame_t * frame
Definition: switch_utils.c:127
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_mutex_t * mutex
Definition: switch_utils.c:137
switch_frame_node_t * head
Definition: switch_utils.c:134
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
void * extra_data
Definition: switch_frame.h:64
struct switch_frame_node_s * next
Definition: switch_utils.c:130
An abstraction of a data frame.
Definition: switch_frame.h:54
struct switch_frame_node_s * prev
Definition: switch_utils.c:129
switch_image_t * img
Definition: switch_frame.h:88
#define switch_assert(expr)

◆ switch_frame_buffer_pop()

switch_status_t switch_frame_buffer_pop ( switch_frame_buffer_t fb,
void **  ptr 
)

Definition at line 277 of file switch_utils.c.

References switch_frame_buffer_s::queue, and switch_queue_pop().

278 {
279  return switch_queue_pop(fb->queue, ptr);
280 }
switch_queue_t * queue
Definition: switch_utils.c:136
switch_status_t switch_queue_pop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1243

◆ switch_frame_buffer_push()

switch_status_t switch_frame_buffer_push ( switch_frame_buffer_t fb,
void *  ptr 
)

Definition at line 267 of file switch_utils.c.

References switch_frame_buffer_s::queue, and switch_queue_push().

Referenced by perform_write(), and switch_core_media_deactivate_rtp().

268 {
269  return switch_queue_push(fb->queue, ptr);
270 }
switch_queue_t * queue
Definition: switch_utils.c:136
switch_status_t switch_queue_push(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1253

◆ switch_frame_buffer_size()

int switch_frame_buffer_size ( switch_frame_buffer_t fb)

Definition at line 287 of file switch_utils.c.

References switch_frame_buffer_s::queue, and switch_queue_size().

288 {
289  return switch_queue_size(fb->queue);
290 }
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1238
switch_queue_t * queue
Definition: switch_utils.c:136

◆ switch_frame_buffer_trypop()

switch_status_t switch_frame_buffer_trypop ( switch_frame_buffer_t fb,
void **  ptr 
)

Definition at line 282 of file switch_utils.c.

References switch_frame_buffer_s::queue, and switch_queue_trypop().

Referenced by audio_write_thread().

283 {
284  return switch_queue_trypop(fb->queue, ptr);
285 }
switch_queue_t * queue
Definition: switch_utils.c:136
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264

◆ switch_frame_buffer_trypush()

switch_status_t switch_frame_buffer_trypush ( switch_frame_buffer_t fb,
void *  ptr 
)

Definition at line 272 of file switch_utils.c.

References switch_frame_buffer_s::queue, and switch_queue_trypush().

273 {
274  return switch_queue_trypush(fb->queue, ptr);
275 }
switch_queue_t * queue
Definition: switch_utils.c:136
switch_status_t switch_queue_trypush(switch_queue_t *queue, void *data)
Definition: switch_apr.c:1279

◆ switch_frame_dup()

switch_status_t switch_frame_dup ( switch_frame_t orig,
switch_frame_t **  clone 
)

Definition at line 321 of file switch_utils.c.

References switch_frame::buflen, switch_frame::codec, switch_frame::data, switch_frame::datalen, switch_frame::img, switch_frame::packet, switch_frame::packetlen, switch_frame::pmap, SFF_DYNAMIC, SFF_ENCODED, switch_assert, switch_img_copy(), SWITCH_RTP_MAX_BUF_LEN, switch_set_flag, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_test_flag.

Referenced by switch_known_bitrate().

322 {
323  switch_frame_t *new_frame;
324 
325  if (!orig) {
326  return SWITCH_STATUS_FALSE;
327  }
328 
329  switch_assert(orig->buflen);
330 
331  new_frame = malloc(sizeof(*new_frame));
332  switch_assert(new_frame);
333 
334  *new_frame = *orig;
335  switch_set_flag(new_frame, SFF_DYNAMIC);
336 
337  if (orig->packet) {
338  new_frame->packet = malloc(SWITCH_RTP_MAX_BUF_LEN);
339  switch_assert(new_frame->packet);
340  memcpy(new_frame->packet, orig->packet, orig->packetlen);
341  new_frame->data = ((unsigned char *)new_frame->packet) + 12;
342  } else {
343  new_frame->packet = NULL;
344  new_frame->data = malloc(new_frame->buflen);
345  switch_assert(new_frame->data);
346  memcpy(new_frame->data, orig->data, orig->datalen);
347  }
348 
349 
350  new_frame->codec = orig->codec;
351  new_frame->pmap = orig->pmap;
352  new_frame->img = NULL;
353 
354 
355  if (orig->img && !switch_test_flag(orig, SFF_ENCODED)) {
356  switch_img_copy(orig->img, &new_frame->img);
357  }
358  *clone = new_frame;
359 
360  return SWITCH_STATUS_SUCCESS;
361 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
#define SWITCH_RTP_MAX_BUF_LEN
Definition: switch_rtp.h:45
switch_codec_t * codec
Definition: switch_frame.h:56
uint32_t buflen
Definition: switch_frame.h:70
uint32_t datalen
Definition: switch_frame.h:68
uint32_t packetlen
Definition: switch_frame.h:62
payload_map_t * pmap
Definition: switch_frame.h:87
void switch_img_copy(switch_image_t *img, switch_image_t **new_img)
Copy image to a new image.
An abstraction of a data frame.
Definition: switch_frame.h:54
void * packet
Definition: switch_frame.h:60
switch_image_t * img
Definition: switch_frame.h:88
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
#define switch_assert(expr)

◆ switch_frame_free()

switch_status_t switch_frame_free ( switch_frame_t **  frame)

Definition at line 363 of file switch_utils.c.

References switch_frame::data, switch_frame::img, switch_frame::packet, SFF_DYNAMIC, switch_img_free(), switch_safe_free, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_test_flag.

Referenced by switch_known_bitrate().

364 {
365  switch_frame_t * f;
366 
367  if (!frame) {
368  return SWITCH_STATUS_FALSE;
369  }
370 
371  f = *frame;
372 
373  if (!f || !switch_test_flag(f, SFF_DYNAMIC)) {
374  return SWITCH_STATUS_FALSE;
375  }
376 
377  *frame = NULL;
378 
379  if (f->img) {
380  switch_img_free(&(f->img));
381  }
382 
383  if (f->packet) {
385  } else {
387  }
388 
389  free(f);
390 
391  return SWITCH_STATUS_SUCCESS;
392 }
void switch_img_free(switch_image_t **img)
Close an image descriptor.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
An abstraction of a data frame.
Definition: switch_frame.h:54
void * packet
Definition: switch_frame.h:60
switch_image_t * img
Definition: switch_frame.h:88
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693

◆ switch_fulldate_cmp()

int switch_fulldate_cmp ( const char *  exp,
switch_time_t ts 
)

Definition at line 3739 of file switch_utils.c.

References cur, switch_assert, SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_safe_free, and switch_str_time().

Referenced by switch_xml_std_datetime_check().

3740 {
3741  char *dup = strdup(exp);
3742  char *sStart;
3743  char *sEnd;
3744  char *cur;
3745  char *p;
3746  switch_time_t tsStart = 0;
3747  switch_time_t tsEnd = 0;
3748  int ret = 0;
3749 
3750  switch_assert(dup);
3751 
3752  cur = dup;
3753  if ((p = strchr(cur, ','))) {
3754  *p++ = '\0';
3755  }
3756 
3757  while (cur) {
3758  sStart = cur;
3759  if ((sEnd = strchr(cur, '~'))) {
3760  *sEnd++ = '\0';
3761 
3762  tsStart = switch_str_time(sStart);
3763  tsEnd = switch_str_time(sEnd);
3764 
3765 
3766  if (tsStart == 0 || tsEnd == 0) {
3767  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse error for date time range (%s~%s)\n", sStart, sEnd);
3768  break;
3769  }
3770 
3771  if (tsStart <= *ts && tsEnd > *ts) {
3772  ret = 1;
3773  break;
3774  }
3775  }
3776 
3777  if ((cur = p)) {
3778  if ((p = strchr(p, ','))) {
3779  *p++ = '\0';
3780  }
3781  }
3782  }
3783 
3784  switch_safe_free(dup);
3785  return ret;
3786 }
switch_time_t switch_str_time(const char *in)
Converts a string representation of a date into a switch_time_t.
#define SWITCH_CHANNEL_LOG
pack cur
int64_t switch_time_t
Definition: switch_apr.h:188
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define switch_assert(expr)

◆ switch_get_hex_bytes()

static char* switch_get_hex_bytes ( switch_byte_t buf,
switch_size_t  datalen,
char *  new_buf,
switch_size_t  new_datalen 
)
inlinestatic

Definition at line 60 of file switch_utils.h.

61 {
62  switch_byte_t *p, *e;
63  char *pp, *ee;
64 
65  e = buf + datalen;
66  ee = new_buf + new_datalen;
67  pp = new_buf;
68 
69  for (p = buf; p < e && pp < ee - 4; p++) {
70  if (snprintf(pp, 4, "%.2x ", (int)*p) < 0) {
71  return NULL;
72  }
73  pp += 3;
74  }
75  *(pp-1) = '\0';
76 
77  return new_buf;
78 }
uint8_t switch_byte_t
Definition: switch_types.h:256
switch_byte_t switch_byte_t * buf

◆ switch_getcputime()

void switch_getcputime ( switch_cputime t)

/ Return used CPU time in this process for user and kernel code

Definition at line 4591 of file switch_utils.c.

References switch_cputime::kernelms, switch_cputime::userms, and switch_stream_handle::write_function.

4592 {
4593 #if defined(_WIN32)
4594  FILETIME ct, et, kt, ut; // Times are in 100-ns ticks (div 10000 to get ms)
4595  GetProcessTimes(GetCurrentProcess(), &ct, &et, &kt, &ut);
4596  t->userms = ((int64_t)ut.dwLowDateTime | ((int64_t)ut.dwHighDateTime << 32)) / 10000;
4597  t->kernelms = ((int64_t)kt.dwLowDateTime | ((int64_t)kt.dwHighDateTime << 32)) / 10000;
4598 #elif defined(HAVE_GETRUSAGE)
4599  struct rusage r;
4600  getrusage(RUSAGE_SELF, &r);
4601  t->userms = r.ru_utime.tv_sec * 1000 + r.ru_utime.tv_usec / 1000;
4602  t->kernelms = r.ru_stime.tv_sec * 1000 + r.ru_stime.tv_usec / 1000;
4603 #else
4604  t->userms = -1;
4605  t->kernelms = -1;
4606 #endif
4607 }

◆ switch_getpid()

unsigned long switch_getpid ( void  )

Definition at line 4704 of file switch_utils.c.

Referenced by fst_init_core_and_modload().

4705 {
4706 #ifndef WIN32
4707  pid_t pid = getpid();
4708 #else
4709  int pid = _getpid();
4710 #endif
4711 
4712  return (unsigned long)pid;
4713 }

◆ switch_html_strip()

char* switch_html_strip ( const char *  str)

Definition at line 4658 of file switch_utils.c.

References switch_stream_handle::data, SWITCH_CHANNEL_LOG, switch_log_printf(), SWITCH_LOG_WARNING, switch_safe_strdup(), and SWITCH_STANDARD_STREAM.

4659 {
4660  char *p, *html = NULL, *text = NULL;
4661  int x = 0, got_ct = 0;
4662 #ifdef SWITCH_HAVE_GUMBO
4663  GumboOutput *output;
4664  switch_stream_handle_t stream;
4665 
4666  SWITCH_STANDARD_STREAM(stream);
4667 #endif
4668 
4669  for(p = (char *)str; p && *p; p++) {
4670 
4671  if (!strncasecmp(p, "Content-Type:", 13)) {
4672  got_ct++;
4673  }
4674 
4675  if (!got_ct) continue;
4676 
4677  if (*p == '\n') {
4678  x++;
4679  if (x == 2) {
4680  break;
4681  }
4682  } else if (x && (*p != '\r')) {
4683  x = 0;
4684  }
4685  }
4686 
4687  html = p;
4688 
4689 #ifdef SWITCH_HAVE_GUMBO
4690  if ((output = gumbo_parse_with_options(&kGumboDefaultOptions, html, strlen(html)))) {
4691  process(output->root, &stream);
4692  gumbo_destroy_output(&kGumboDefaultOptions, output);
4693  }
4694 
4695  text = (char *)stream.data;
4696 #else
4697  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Support for html parser is not compiled.\n");
4698  text = switch_safe_strdup(html);
4699 #endif
4700 
4701  return text;
4702 }
#define SWITCH_CHANNEL_LOG
static char * switch_safe_strdup(const char *it)
Definition: switch_utils.h:887
#define SWITCH_STANDARD_STREAM(s)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_http_dump_request()

void switch_http_dump_request ( switch_http_request_t request)

Definition at line 4561 of file switch_utils.c.

References switch_http_request_s::content_length, switch_http_request_s::content_type, switch_http_request_s::from, switch_event::headers, switch_http_request_s::headers, switch_http_request_s::host, switch_http_request_s::keepalive, switch_http_request_s::method, switch_event_header::name, switch_event_header::next, switch_http_request_s::port, switch_http_request_s::qs, switch_http_request_s::referer, switch_assert, SWITCH_SIZE_T_FMT, switch_http_request_s::uri, switch_http_request_s::user, switch_http_request_s::user_agent, and switch_event_header::value.

4562 {
4563  switch_assert(request->method);
4564 
4565  printf("method: %s\n", request->method);
4566 
4567  if (request->uri) printf("uri: %s\n", request->uri);
4568  if (request->qs) printf("qs: %s\n", request->qs);
4569  if (request->host) printf("host: %s\n", request->host);
4570  if (request->port) printf("port: %d\n", request->port);
4571  if (request->from) printf("from: %s\n", request->from);
4572  if (request->user_agent) printf("user_agent: %s\n", request->user_agent);
4573  if (request->referer) printf("referer: %s\n", request->referer);
4574  if (request->user) printf("user: %s\n", request->user);
4575  if (request->keepalive) printf("uri: %d\n", request->keepalive);
4576  if (request->content_type) printf("uri: %s\n", request->content_type);
4577  if (request->content_length) printf("uri: %" SWITCH_SIZE_T_FMT "\n", request->content_length);
4578 
4579  {
4580  switch_event_header_t *header = request->headers->headers;
4581 
4582  printf("headers:\n-------------------------\n");
4583 
4584  while(header) {
4585  printf("%s: %s\n", header->name, header->value);
4586  header = header->next;
4587  }
4588  }
4589 }
An event Header.
Definition: switch_event.h:65
const char * content_type
const char * user_agent
switch_size_t content_length
#define SWITCH_SIZE_T_FMT
struct switch_event_header * next
Definition: switch_event.h:76
switch_bool_t keepalive
#define switch_assert(expr)
switch_event_t * headers
switch_event_header_t * headers
Definition: switch_event.h:90

◆ switch_http_free_request()

void switch_http_free_request ( switch_http_request_t request)

Definition at line 4552 of file switch_utils.c.

References switch_http_request_s::_buffer, switch_http_request_s::_destroy_headers, switch_http_request_s::headers, and switch_event_destroy().

Referenced by switch_http_parse_header().

4553 {
4554  if (request->_buffer) free(request->_buffer);
4555  if (request->_destroy_headers && request->headers) {
4556  switch_event_destroy(&request->headers);
4557  }
4558 }
switch_bool_t _destroy_headers
void switch_event_destroy(switch_event_t **event)
Destroy an event.
switch_event_t * headers

◆ switch_http_parse_header()

switch_status_t switch_http_parse_header ( char *  buffer,
uint32_t  datalen,
switch_http_request_t request 
)

parse http headers in a buffer return status of success or not

Parameters
[in]bufferthe buffer start from the very begining of the http request, e.g. 'GET '
[in]datalenthe buffer length
[out]thehttp request pointer or null, need destroy later if got non-NULL pointer
Returns
SWITCH_STATUS_SUCCESS | SWITCH_STATUS_FALSE

Definition at line 4411 of file switch_utils.c.

References switch_http_request_s::_buffer, switch_http_request_s::_destroy_headers, buffer, switch_http_request_s::bytes_buffered, switch_http_request_s::bytes_header, switch_http_request_s::bytes_read, clean_uri(), switch_http_request_s::content_length, switch_http_request_s::content_type, switch_http_request_s::headers, switch_http_request_s::host, switch_http_request_s::keepalive, switch_http_request_s::method, switch_http_request_s::port, switch_http_request_s::qs, switch_http_request_s::referer, switch_assert, switch_event_add_header_string(), SWITCH_EVENT_CHANNEL_DATA, switch_event_create, switch_http_free_request(), switch_http_parse_qs(), switch_separate_string(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, SWITCH_TRUE, switch_http_request_s::uri, switch_http_request_s::user_agent, and value.

4412 {
4414  char *p = buffer;
4415  int i = 10;
4416  char *http = NULL;
4417  int header_count;
4418  char *headers[64] = { 0 };
4419  int argc;
4420  char *argv[2] = { 0 };
4421  char *body = NULL;
4422 
4423  if (datalen < 16) return status; /* minimum GET / HTTP/1.1\r\n */
4424 
4425  while(i--) { // sanity check
4426  if (*p++ == ' ') break;
4427  }
4428 
4429  if (i == 0) return status;
4430 
4431  if ((body = strstr(buffer, "\r\n\r\n"))) {
4432  *body = '\0';
4433  body += 4;
4434  } else if (( body = strstr(buffer, "\n\n"))) {
4435  *body = '\0';
4436  body += 2;
4437  } else {
4438  return status;
4439  }
4440 
4441  request->_buffer = strdup(buffer);
4442  switch_assert(request->_buffer);
4443  request->method = request->_buffer;
4444  request->bytes_buffered = datalen;
4445  request->bytes_header = body - buffer;
4446  request->bytes_read = body - buffer;
4447 
4448  p = strchr(request->method, ' ');
4449 
4450  if (!p) goto err;
4451 
4452  *p++ = '\0';
4453 
4454  if (*p != '/') goto err; /* must start from '/' */
4455 
4456  request->uri = p;
4457  p = strchr(request->uri, ' ');
4458 
4459  if (!p) goto err;
4460 
4461  *p++ = '\0';
4462  http = p;
4463 
4464  p = strchr(request->uri, '?');
4465 
4466  if (p) {
4467  *p++ = '\0';
4468  request->qs = p;
4469  }
4470 
4471  if (clean_uri((char *)request->uri) != SWITCH_STATUS_SUCCESS) {
4472  goto err;
4473  }
4474 
4475  if (!strncmp(http, "HTTP/1.1", 8)) {
4476  request->keepalive = SWITCH_TRUE;
4477  } else if (strncmp(http, "HTTP/1.0", 8)) {
4478  goto err;
4479  }
4480 
4481  if (!request->headers) {
4483  goto err;
4484  }
4485  request->_destroy_headers = SWITCH_TRUE;
4486  }
4487 
4488  p = strchr(http, '\n');
4489 
4490  if (p) {
4491  *p++ = '\0'; // now the first header
4492  } else {
4493  goto noheader;
4494  }
4495 
4496  header_count = switch_separate_string(p, '\n', headers, sizeof(headers)/ sizeof(headers[0]));
4497 
4498  if (header_count < 1) goto err;
4499 
4500  for (i = 0; i < header_count; i++) {
4501  char *header, *value;
4502  int len;
4503 
4504  argc = switch_separate_string(headers[i], ':', argv, 2);
4505 
4506  if (argc != 2) goto err;
4507 
4508  header = argv[0];
4509  value = argv[1];
4510 
4511  if (*value == ' ') value++;
4512 
4513  len = strlen(value);
4514 
4515  if (len && *(value + len - 1) == '\r') *(value + len - 1) = '\0';
4516 
4518 
4519  if (!strncasecmp(header, "User-Agent", 10)) {
4520  request->user_agent = value;
4521  } else if (!strncasecmp(header, "Host", 4)) {
4522  request->host = value;
4523  p = strchr(value, ':');
4524 
4525  if (p) {
4526  *p++ = '\0';
4527 
4528  if (*p) request->port = (switch_port_t)atoi(p);
4529  }
4530  } else if (!strncasecmp(header, "Content-Type", 12)) {
4531  request->content_type = value;
4532  } else if (!strncasecmp(header, "Content-Length", 14)) {
4533  request->content_length = atoi(value);
4534  } else if (!strncasecmp(header, "Referer", 7)) {
4535  request->referer = value;
4536  }
4537  }
4538 
4539 noheader:
4540 
4541  if (request->qs) {
4542  switch_http_parse_qs(request, NULL);
4543  }
4544 
4545  return SWITCH_STATUS_SUCCESS;
4546 
4547 err:
4548  switch_http_free_request(request);
4549  return status;
4550 }
switch_size_t bytes_read
void switch_http_free_request(switch_http_request_t *request)
const char *const const char *const const cJSON *const value
const char * content_type
const char * user_agent
switch_bool_t _destroy_headers
void switch_http_parse_qs(switch_http_request_t *request, char *qs)
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
uint16_t switch_port_t
switch_size_t content_length
switch_status_t
Common return values.
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
unsigned int switch_separate_string(char *buf, char delim, char **array, unsigned int arraylen)
char * buffer
Definition: switch_cJSON.h:153
switch_status_t clean_uri(char *uri)
switch_bool_t keepalive
#define switch_assert(expr)
switch_event_t * headers
switch_size_t bytes_header
switch_size_t bytes_buffered

◆ switch_http_parse_qs()

void switch_http_parse_qs ( switch_http_request_t request,
char *  qs 
)

parse http query string

Parameters
[in]requestthe http request object
[in]qsthe query string buffer

qs buffer will be modified, so be sure to dup the qs before passing into this function if you want to keep the original string untouched if qs is NULL, the it will parse request->qs, request->qs will be duplicated before parse to avoid being modified

Definition at line 4337 of file switch_utils.c.

References switch_http_request_s::headers, name, switch_network_node::next, switch_http_request_s::qs, switch_assert, switch_event_add_header_string(), switch_safe_free, SWITCH_STACK_BOTTOM, and switch_url_decode().

Referenced by switch_http_parse_header().

4338 {
4339  char *q;
4340  char *next;
4341  char *name, *val;
4342  char *dup = NULL;
4343 
4344  if (qs) {
4345  q = qs;
4346  } else { /*parse our own qs, dup to avoid modify the original string */
4347  dup = q = strdup(request->qs);
4348  }
4349 
4350  switch_assert(q);
4351  next = q;
4352 
4353  do {
4354  char *p;
4355 
4356  if ((next = strchr(next, '&'))) {
4357  *next++ = '\0';
4358  }
4359 
4360  for (p = q; p && *p; p++) {
4361  if (*p == '+') *p = ' ';
4362  }
4363 
4364  switch_url_decode(q);
4365 
4366  name = q;
4367  if ((val = strchr(name, '='))) {
4368  *val++ = '\0';
4370  }
4371  q = next;
4372  } while (q);
4373 
4374  switch_safe_free(dup);
4375 }
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
char * switch_url_decode(char *s)
const char *const name
Definition: switch_cJSON.h:250
#define switch_assert(expr)
switch_event_t * headers

◆ switch_inet_pton()

int switch_inet_pton ( int  af,
const char *  src,
void *  dst 
)

Definition at line 74 of file inet_pton.c.

References inet_pton4().

Referenced by check_ice(), switch_check_network_list_ip_port_token(), switch_find_local_ip(), switch_network_list_add_host_port_mask(), and switch_parse_cidr().

75 {
76  switch (af) {
77  case AF_INET:
78  return (inet_pton4(src, (unsigned char *) dst));
79 #ifdef ENABLE_IPV6
80 #ifndef AF_INET6
81 #define AF_INET6 (AF_MAX+1) /* just to let this compile */
82 #endif
83  case AF_INET6:
84  return (inet_pton6(src, (unsigned char *) dst));
85 #endif
86  default:
87  errno = EAFNOSUPPORT;
88  return (-1);
89  }
90  /* NOTREACHED */
91 }
static int inet_pton4(const char *src, unsigned char *dst)
Definition: inet_pton.c:103

◆ switch_is_digit_string()

static switch_bool_t switch_is_digit_string ( const char *  s)
inlinestatic

Definition at line 437 of file switch_utils.h.

References SWITCH_FALSE, and SWITCH_TRUE.

438 {
439 
440  while (s && *s) {
441  if (*s < 48 || *s > 57) {
442  return SWITCH_FALSE;
443  }
444  s++;
445  }
446 
447  return SWITCH_TRUE;
448 }

◆ switch_is_file_path()

static switch_bool_t switch_is_file_path ( const char *  file)
inlinestatic

Definition at line 1281 of file switch_utils.h.

References SWITCH_FALSE, switch_find_end_paren(), SWITCH_TRUE, SWITCH_URL_SEPARATOR, and zstr.

Referenced by db_pick_path(), enterprise_originate_ringback_thread(), preprocess_glob(), setup_ringback(), switch_config_open_file(), switch_core_check_dtls_pem(), switch_core_gen_certs(), switch_img_txt_handle_create(), switch_ivr_displace_session(), switch_ivr_play_file(), switch_ivr_record_file_event(), switch_ivr_record_session_event(), switch_ivr_wait_for_answer(), switch_loadable_module_load_module_ex(), and switch_stream_write_file_contents().

1282 {
1283  const char *e;
1284  int r;
1285 
1286  if (zstr(file)) {
1287  return SWITCH_FALSE;
1288  }
1289 
1290  while(*file == '{') {
1291  if ((e = switch_find_end_paren(file, '{', '}'))) {
1292  file = e + 1;
1293  while(*file == ' ') file++;
1294  }
1295  }
1296 
1297 #ifdef WIN32
1298  r = (*file == '\\' || *(file + 1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR));
1299 #else
1300  r = ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR));
1301 #endif
1302 
1303  return r ? SWITCH_TRUE : SWITCH_FALSE;
1304 }
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:796
#define SWITCH_URL_SEPARATOR
Definition: switch_types.h:126
#define zstr(x)
Definition: switch_utils.h:314

◆ switch_is_lan_addr()

switch_bool_t switch_is_lan_addr ( const char *  ip)

Definition at line 1355 of file switch_utils.c.

References SWITCH_FALSE, SWITCH_TRUE, and zstr.

1356 {
1357  if (zstr(ip))
1358  return SWITCH_FALSE;
1359 
1360  return (strncmp(ip, "10.", 3) && /* 10.0.0.0 - 10.255.255.255 (10/8 prefix) */
1361  strncmp(ip, "192.168.", 8) && /* 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) */
1362  strncmp(ip, "127.", 4) && /* 127.0.0.0 - 127.255.255.255 (127/8 prefix) */
1363  strncmp(ip, "255.", 4) &&
1364  strncmp(ip, "0.", 2) &&
1365  strncmp(ip, "1.", 2) &&
1366  strncmp(ip, "2.", 2) &&
1367  strncmp(ip, "172.16.", 7) && /* 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) */
1368  strncmp(ip, "172.17.", 7) &&
1369  strncmp(ip, "172.18.", 7) &&
1370  strncmp(ip, "172.19.", 7) &&
1371  strncmp(ip, "172.20.", 7) &&
1372  strncmp(ip, "172.21.", 7) &&
1373  strncmp(ip, "172.22.", 7) &&
1374  strncmp(ip, "172.23.", 7) &&
1375  strncmp(ip, "172.24.", 7) &&
1376  strncmp(ip, "172.25.", 7) &&
1377  strncmp(ip, "172.26.", 7) &&
1378  strncmp(ip, "172.27.", 7) &&
1379  strncmp(ip, "172.28.", 7) &&
1380  strncmp(ip, "172.29.", 7) &&
1381  strncmp(ip, "172.30.", 7) &&
1382  strncmp(ip, "172.31.", 7) &&
1383  strncmp(ip, "192.0.2.", 8) && /* 192.0.2.0 - 192.0.2.255 (192.0.2/24 prefix) */
1384  strncmp(ip, "169.254.", 8) /* 169.254.0.0 - 169.254.255.255 (169.254/16 prefix) */
1386 }
#define zstr(x)
Definition: switch_utils.h:314
char * ip
Definition: switch_msrp.c:60

◆ switch_is_leading_number()

switch_bool_t switch_is_leading_number ( const char *  str)

Definition at line 1654 of file switch_utils.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_core_media_filter_sdp(), and switch_known_bitrate().

1655 {
1656  const char *p;
1658 
1659  if (*str == '-' || *str == '+') {
1660  str++;
1661  }
1662 
1663  for (p = str; p && *p; p++) {
1664  if ((*p == '.' || (*p > 47 && *p < 58))) {
1665  r = SWITCH_TRUE;
1666  break;
1667  }
1668  }
1669 
1670  return r;
1671 }
switch_bool_t
Definition: switch_types.h:437

◆ switch_is_moh()

static switch_bool_t switch_is_moh ( const char *  s)
inlinestatic

Definition at line 319 of file switch_utils.h.

References SWITCH_FALSE, SWITCH_TRUE, and zstr.

Referenced by switch_ivr_parse_event().

320 {
321  if (zstr(s) || !strcasecmp(s, "silence") || !strcasecmp(s, "indicate_hold")) {
322  return SWITCH_FALSE;
323  }
324  return SWITCH_TRUE;
325 }
#define zstr(x)
Definition: switch_utils.h:314

◆ switch_is_number()

switch_bool_t switch_is_number ( const char *  str)

Definition at line 1635 of file switch_utils.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by check_channel_status(), load_configuration(), switch_channel_perform_mark_answered(), switch_false(), switch_format_number(), switch_ivr_originate(), switch_ivr_phrase_macro_event(), switch_known_bitrate(), switch_log_str2level(), switch_speex_fmtp_parse(), switch_true(), switch_xml_config_parse_event(), switch_xml_locate_user_merged(), and switch_xml_std_datetime_check().

1636 {
1637  const char *p;
1639 
1640  if (*str == '-' || *str == '+') {
1641  str++;
1642  }
1643 
1644  for (p = str; p && *p; p++) {
1645  if (!(*p == '.' || (*p > 47 && *p < 58))) {
1646  r = SWITCH_FALSE;
1647  break;
1648  }
1649  }
1650 
1651  return r;
1652 }
switch_bool_t
Definition: switch_types.h:437

◆ switch_is_uint_in_range()

switch_bool_t switch_is_uint_in_range ( const char *  str,
unsigned int  from,
unsigned int  to 
)

Check if a 32 bit unsigned number is in a range.

Parameters
strstring to check. Should not contain non-digit characters.
fromstart of range including this number
toend of range including this number
Returns
true or false

Definition at line 1611 of file switch_utils.c.

References number, switch_network_node::str, SWITCH_FALSE, and SWITCH_TRUE.

Referenced by check_ice(), and switch_known_bitrate().

1612 {
1613  unsigned int number;
1614  const char *original_str = str;
1615 
1616  if (str == NULL || *str == '\0' || from > to) {
1617  return SWITCH_FALSE;
1618  }
1619 
1620  for (; *str != '\0'; str++) {
1621  if (!isdigit(*str)) {
1622  return SWITCH_FALSE;
1623  }
1624  }
1625 
1626  number = atoi(original_str);
1627 
1628  if (number < from || number > to) {
1629  return SWITCH_FALSE;
1630  }
1631 
1632  return SWITCH_TRUE;
1633 }
const char *const const double number
Definition: switch_cJSON.h:254
cJSON *const to

◆ switch_isalnum()

int switch_isalnum ( int  c)

Definition at line 3978 of file switch_utils.c.

References _L, _N, and _U.

Referenced by switch_tolower_max().

3979 {
3980  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_U | _L | _N)));
3981 }
#define _L
#define _U
#define _N
const int * _switch_ctype_

◆ switch_isalpha()

int switch_isalpha ( int  c)

Definition at line 3983 of file switch_utils.c.

References _L, and _U.

Referenced by switch_tolower_max().

3984 {
3985  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_U | _L)));
3986 }
#define _L
#define _U
const int * _switch_ctype_

◆ switch_iscntrl()

int switch_iscntrl ( int  c)

Definition at line 3988 of file switch_utils.c.

References _C.

Referenced by switch_tolower_max().

3989 {
3990  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _C));
3991 }
#define _C
const int * _switch_ctype_

◆ switch_isdigit()

int switch_isdigit ( int  c)

Definition at line 3993 of file switch_utils.c.

References _N.

Referenced by switch_tolower_max().

3994 {
3995  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _N));
3996 }
#define _N
const int * _switch_ctype_

◆ switch_isgraph()

int switch_isgraph ( int  c)

Definition at line 3998 of file switch_utils.c.

References _L, _N, and _U.

Referenced by switch_tolower_max().

3999 {
4000  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_P | _U | _L | _N)));
4001 }
#define _L
#define _U
#define _P
#define _N
const int * _switch_ctype_

◆ switch_islower()

int switch_islower ( int  c)

Definition at line 4003 of file switch_utils.c.

References _L.

Referenced by switch_tolower_max().

4004 {
4005  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _L));
4006 }
#define _L
const int * _switch_ctype_

◆ switch_isprint()

int switch_isprint ( int  c)

Definition at line 4008 of file switch_utils.c.

References _B, _L, _N, and _U.

Referenced by switch_tolower_max().

4009 {
4010  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_P | _U | _L | _N | _B)));
4011 }
#define _L
#define _U
#define _P
#define _B
#define _N
const int * _switch_ctype_

◆ switch_ispunct()

int switch_ispunct ( int  c)

Definition at line 4013 of file switch_utils.c.

Referenced by switch_tolower_max().

4014 {
4015  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _P));
4016 }
#define _P
const int * _switch_ctype_

◆ switch_isspace()

int switch_isspace ( int  c)

Definition at line 4018 of file switch_utils.c.

References _S.

Referenced by switch_tolower_max().

4019 {
4020  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _S));
4021 }
#define _S
const int * _switch_ctype_

◆ switch_isupper()

int switch_isupper ( int  c)

Definition at line 4023 of file switch_utils.c.

References _U.

Referenced by switch_tolower_max().

4024 {
4025  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _U));
4026 }
#define _U
const int * _switch_ctype_

◆ switch_isxdigit()

int switch_isxdigit ( int  c)

Definition at line 4028 of file switch_utils.c.

References _N, and _X.

Referenced by switch_tolower_max().

4029 {
4030  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_N | _X)));
4031 }
#define _X
#define _N
const int * _switch_ctype_

◆ switch_itodtmf()

static char switch_itodtmf ( char  i)
inlinestatic

Definition at line 450 of file switch_utils.h.

Referenced by switch_ivr_bind_dtmf_meta_session().

451 {
452  char r = i;
453 
454  if (i > 9 && i < 14) {
455  r += 55;
456  } else {
457  r += 48;
458  }
459 
460  return r;
461 }

◆ switch_jwt_sign()

char* switch_jwt_sign ( const char *  secret,
const uint8_t *  payload,
switch_size_t  size 
)
Parameters
[in]secretthe secret string
[in]payloadthe payload
[in]sizethe payload size
Returns
NULL or signed token

Definition at line 5007 of file switch_utils.c.

References SHA256_LENGTH, switch_assert, switch_b64_encode(), switch_mprintf(), and switch_must_malloc().

5008 {
5009  cJSON *json;
5010  switch_size_t need;
5011  char *header;
5012  uint8_t *bheader;
5013  uint8_t *bpayload;
5014  char *to_sign;
5015  unsigned int len = 0;
5016  unsigned char signature[SHA256_LENGTH];
5017  unsigned char *bsignature;
5018  char *result = NULL;
5019 
5020  if (!secret || !payload) return NULL;
5021 
5022  json = cJSON_CreateObject();
5023  switch_assert(json);
5024  cJSON_AddStringToObject(json, "alg", "HS256");
5025  cJSON_AddStringToObject(json, "typ", "JWT");
5026  header = cJSON_Print(json);
5027  cJSON_Delete(json);
5028  if (!header) return NULL;
5029 
5030  need = ((strlen(header) + 3 - 1) / 3) * 4 + 1; // with extra NULL
5031  bheader = (uint8_t *)switch_must_malloc(need);
5032  switch_b64_encode((unsigned char *)header, strlen(header), bheader, need);
5033  free(header);
5034  header = NULL;
5035 
5036  need = ((size + 3 - 1) / 3) * 4 + 1; // with extra NULL
5037  bpayload = (uint8_t *)switch_must_malloc(need);
5038  switch_b64_encode((unsigned char *)payload, size, bpayload, need);
5039 
5040  to_sign = switch_mprintf("%s.%s", (char *)bheader, (char *)bpayload);
5041  switch_assert(to_sign);
5042 
5043  HMAC(EVP_sha256(), secret, strlen(secret), (unsigned char *)to_sign, strlen(to_sign), signature, &len);
5044  switch_assert(len == SHA256_LENGTH);
5045  free(to_sign);
5046  to_sign = NULL;
5047 
5048  need = ((len + 3 - 1) / 3) * 4 + 1; // with extra NULL
5049  bsignature = (uint8_t *)switch_must_malloc(need);
5050  switch_b64_encode((unsigned char *)signature, len, bsignature, need);
5051  result = switch_mprintf("%s.%s.%s", bheader, bpayload, bsignature);
5052 
5053  free(bheader);
5054  free(bpayload);
5055  free(bsignature);
5056 
5057  return result;
5058 }
static void * switch_must_malloc(size_t _b)
Definition: switch_core.h:233
switch_status_t switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen)
#define SHA256_LENGTH
uintptr_t switch_size_t
#define switch_assert(expr)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)

◆ switch_jwt_verify()

cJSON* switch_jwt_verify ( const char *  secret,
const char *  token 
)
Parameters
[in]secretthe secret string
[in]tokenthe jwt token, only alg: HS256 is supported
Returns
NULL or payload in cJSON

Definition at line 4943 of file switch_utils.c.

References cJSON_GetObjectCstr(), switch_network_node::ok, SHA256_LENGTH, switch_assert, switch_b64_decode(), switch_must_malloc(), and switch_safe_free.

4944 {
4945  int ok = 0;
4946  cJSON *json = NULL;
4947  cJSON *payload = NULL;
4948  char *out = NULL;
4949  unsigned int len = 0;
4950  unsigned char signature[SHA256_LENGTH];
4951  const char *alg = NULL;
4952  const char *typ = NULL;
4953  char *header = strdup(token);
4954  char *dot;
4955 
4956  switch_assert(header);
4957  dot = strchr(header, '.');
4958  if (!dot) goto end;
4959  *dot = '\0';
4960  out = (char *)switch_must_malloc(strlen(header) + 1);
4961  len = switch_b64_decode((const char *)header, out, strlen(header) + 1);
4962  if (len == 0) goto end;
4963  json = cJSON_Parse(out); // header
4964  free(out);
4965  out = NULL;
4966  if (!json) goto end;
4967  alg = cJSON_GetObjectCstr(json, "alg");
4968  if (!alg || strcmp(alg, "HS256")) goto end;
4969  typ = cJSON_GetObjectCstr(json, "typ");
4970  if (!typ || strcmp(typ, "JWT")) goto end;
4971  cJSON_Delete(json);
4972  json = NULL;
4973  dot++; // payload
4974  dot = strrchr(token, '.');
4975  if (!dot) goto end;
4976  HMAC(EVP_sha256(), secret, strlen(secret), (unsigned char *)token, dot - token, signature, &len);
4977  if (len != SHA256_LENGTH) goto end;
4978  dot++; // signature
4979  out = (char *)switch_must_malloc(strlen(dot) + 1);
4980  len = switch_b64_decode((const char *)dot, out, strlen(dot) + 1);
4981  if (len != SHA256_LENGTH) goto end;
4982  ok = !memcmp(out, signature, len);
4983  free(out);
4984  out = NULL;
4985 
4986  if (ok) {
4987  char *p = strchr(token, '.');
4988 
4989  if (p) {
4990  p++;
4991  out = (char *)switch_must_malloc(strlen(p));
4992  len = switch_b64_decode((const char *)p, out, strlen(p));
4993  payload = cJSON_Parse(out);
4994  free(out);
4995  out = NULL;
4996  }
4997  }
4998 
4999 end:
5000  switch_safe_free(header);
5001  switch_safe_free(out);
5002  if (json) cJSON_Delete(json);
5003 
5004  return payload;
5005 }
static void * switch_must_malloc(size_t _b)
Definition: switch_core.h:233
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define SHA256_LENGTH
const char * cJSON_GetObjectCstr(const cJSON *object, const char *string)
Definition: switch_json.c:22
switch_size_t switch_b64_decode(const char *in, char *out, switch_size_t olen)
#define switch_assert(expr)

◆ switch_known_bitrate()

static uint32_t switch_known_bitrate ( switch_payload_t  payload)
inlinestatic

Definition at line 479 of file switch_utils.h.

References buf, pool, SWITCH_DECLARE, switch_fd_read_dline(), switch_fd_read_line(), switch_find_parameter(), switch_fp_read_dline(), switch_frame_alloc(), switch_frame_dup(), switch_frame_free(), switch_is_leading_number(), switch_is_number(), switch_is_uint_in_range(), and to.

Referenced by add_audio_codec(), and switch_core_media_negotiate_sdp().

480 {
481  switch(payload) {
482  case 0: /* PCMU */ return 64000;
483  case 3: /* GSM */ return 13200;
484  case 4: /* G723 */ return 6300;
485  case 7: /* LPC */ return 2400;
486  case 8: /* PCMA */ return 64000;
487  case 9: /* G722 */ return 64000;
488  case 18: /* G729 */ return 8000;
489  default: break;
490  }
491 
492  return 0;
493 }

◆ switch_lc_strdup()

static char* switch_lc_strdup ( const char *  it)
inlinestatic

Definition at line 900 of file switch_utils.h.

References switch_tolower_max().

901 {
902  char *dup;
903 
904  if (it) {
905  dup = strdup(it);
906  switch_tolower_max(dup);
907  return dup;
908  }
909 
910  return NULL;
911 }
static void switch_tolower_max(char *s)
Definition: switch_utils.h:231

◆ switch_memory_usage_stream()

const char* switch_memory_usage_stream ( switch_stream_handle_t stream)

Definition at line 4810 of file switch_utils.c.

References FALSE, switch_goto_status, SWITCH_SIZE_T_FMT, and switch_stream_handle::write_function.

4811 {
4812  const char *status = NULL;
4813 #ifdef __GLIBC__
4814 /*
4815  * The mallinfo2() function was added in glibc 2.33.
4816  * https://man7.org/linux/man-pages/man3/mallinfo.3.html
4817  */
4818 #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 33)
4819  struct mallinfo2 mi;
4820 
4821  mi = mallinfo2();
4822 
4823  stream->write_function(stream, "Total non-mmapped bytes (arena): %" SWITCH_SIZE_T_FMT "\n", mi.arena);
4824  stream->write_function(stream, "# of free chunks (ordblks): %" SWITCH_SIZE_T_FMT "\n", mi.ordblks);
4825  stream->write_function(stream, "# of free fastbin blocks (smblks): %" SWITCH_SIZE_T_FMT "\n", mi.smblks);
4826  stream->write_function(stream, "# of mapped regions (hblks): %" SWITCH_SIZE_T_FMT "\n", mi.hblks);
4827  stream->write_function(stream, "Bytes in mapped regions (hblkhd): %" SWITCH_SIZE_T_FMT "\n", mi.hblkhd);
4828  stream->write_function(stream, "Max. total allocated space (usmblks): %" SWITCH_SIZE_T_FMT "\n", mi.usmblks);
4829  stream->write_function(stream, "Free bytes held in fastbins (fsmblks): %" SWITCH_SIZE_T_FMT "\n", mi.fsmblks);
4830  stream->write_function(stream, "Total allocated space (uordblks): %" SWITCH_SIZE_T_FMT "\n", mi.uordblks);
4831  stream->write_function(stream, "Total free space (fordblks): %" SWITCH_SIZE_T_FMT "\n", mi.fordblks);
4832  stream->write_function(stream, "Topmost releasable block (keepcost): %" SWITCH_SIZE_T_FMT "\n", mi.keepcost);
4833 #else
4834  struct mallinfo mi;
4835 
4836  mi = mallinfo();
4837 
4838  stream->write_function(stream, "Total non-mmapped bytes (arena): %u\n", mi.arena);
4839  stream->write_function(stream, "# of free chunks (ordblks): %u\n", mi.ordblks);
4840  stream->write_function(stream, "# of free fastbin blocks (smblks): %u\n", mi.smblks);
4841  stream->write_function(stream, "# of mapped regions (hblks): %u\n", mi.hblks);
4842  stream->write_function(stream, "Bytes in mapped regions (hblkhd): %u\n", mi.hblkhd);
4843  stream->write_function(stream, "Max. total allocated space (usmblks): %u\n", mi.usmblks);
4844  stream->write_function(stream, "Free bytes held in fastbins (fsmblks): %u\n", mi.fsmblks);
4845  stream->write_function(stream, "Total allocated space (uordblks): %u\n", mi.uordblks);
4846  stream->write_function(stream, "Total free space (fordblks): %u\n", mi.fordblks);
4847  stream->write_function(stream, "Topmost releasable block (keepcost): %u\n", mi.keepcost);
4848 
4849 #endif
4850 
4851  switch_goto_status(NULL, done);
4852 #else
4853 #ifdef WIN32
4854  /* Based on: https://docs.microsoft.com/en-us/windows/win32/memory/enumerating-a-heap and https://docs.microsoft.com/en-us/windows/win32/memory/getting-process-heaps */
4855  PHANDLE aHeaps;
4856  SIZE_T BytesToAllocate;
4857  DWORD HeapsIndex;
4858  DWORD HeapsLength;
4859  DWORD NumberOfHeaps;
4860  HRESULT Result;
4861  HANDLE hDefaultProcessHeap;
4862  size_t CommittedSizeTotal = 0;
4863  size_t UnCommittedSizeTotal = 0;
4864  size_t SizeTotal = 0;
4865  size_t OverheadTotal = 0;
4866 
4867  NumberOfHeaps = GetProcessHeaps(0, NULL);
4868  Result = SIZETMult(NumberOfHeaps, sizeof(*aHeaps), &BytesToAllocate);
4869  if (Result != S_OK) {
4870  switch_goto_status("SIZETMult failed.", done);
4871  }
4872 
4873  hDefaultProcessHeap = GetProcessHeap();
4874  if (hDefaultProcessHeap == NULL) {
4875  switch_goto_status("Failed to retrieve the default process heap", done);
4876  }
4877 
4878  aHeaps = (PHANDLE)HeapAlloc(hDefaultProcessHeap, 0, BytesToAllocate);
4879  if (aHeaps == NULL) {
4880  switch_goto_status("HeapAlloc failed to allocate space for heaps", done);
4881  }
4882 
4883  HeapsLength = NumberOfHeaps;
4884  NumberOfHeaps = GetProcessHeaps(HeapsLength, aHeaps);
4885 
4886  if (NumberOfHeaps == 0) {
4887  switch_goto_status("Failed to retrieve heaps", cleanup);
4888  } else if (NumberOfHeaps > HeapsLength) {
4889  /*
4890  * Compare the latest number of heaps with the original number of heaps.
4891  * If the latest number is larger than the original number, another
4892  * component has created a new heap and the buffer is too small.
4893  */
4894  switch_goto_status("Another component created a heap between calls.", cleanup);
4895  }
4896 
4897  stream->write_function(stream, "Process has %d heaps.\n", HeapsLength);
4898  for (HeapsIndex = 0; HeapsIndex < HeapsLength; ++HeapsIndex) {
4899  PROCESS_HEAP_ENTRY Entry;
4900  HANDLE hHeap = aHeaps[HeapsIndex];
4901 
4902  stream->write_function(stream, "Heap %d at address: %#p.\n", HeapsIndex, aHeaps[HeapsIndex]);
4903 
4904  /* Lock the heap to prevent other threads from accessing the heap during enumeration. */
4905  if (HeapLock(hHeap) == FALSE) {
4906  switch_goto_status("Failed to lock heap.", cleanup);
4907  }
4908 
4909  Entry.lpData = NULL;
4910  while (HeapWalk(hHeap, &Entry) != FALSE) {
4911  if ((Entry.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
4912  } else if ((Entry.wFlags & PROCESS_HEAP_REGION) != 0) {
4913  CommittedSizeTotal += Entry.Region.dwCommittedSize;
4914  UnCommittedSizeTotal += Entry.Region.dwUnCommittedSize;
4915  }
4916 
4917  SizeTotal += Entry.cbData;
4918  OverheadTotal += Entry.cbOverhead;
4919  }
4920 
4921  /* Unlock the heap to allow other threads to access the heap after enumeration has completed. */
4922  if (HeapUnlock(hHeap) == FALSE) {
4923  abort();
4924  }
4925  }
4926 
4927  stream->write_function(stream, "Committed bytes: %" SWITCH_SIZE_T_FMT "\n", CommittedSizeTotal);
4928  stream->write_function(stream, "Uncommited bytes: %" SWITCH_SIZE_T_FMT "\n", UnCommittedSizeTotal);
4929  stream->write_function(stream, "Size: %" SWITCH_SIZE_T_FMT "\n", SizeTotal);
4930  stream->write_function(stream, "Overhead: %" SWITCH_SIZE_T_FMT"\n", OverheadTotal);
4931 
4932 cleanup:
4933  HeapFree(hDefaultProcessHeap, 0, aHeaps);
4934 #else
4935  switch_goto_status("Memory usage statistics is not implemented on the current platform.", done);
4936 #endif
4937 #endif
4938 done:
4939  return status;
4940 }
switch_stream_handle_write_function_t write_function
#define SWITCH_SIZE_T_FMT
#define switch_goto_status(_status, _label)
Definition: switch_utils.h:287
#define FALSE

◆ switch_must_strdup()

char* switch_must_strdup ( const char *  _s)

Definition at line 4803 of file switch_utils.c.

References switch_assert.

Referenced by preprocess_glob(), switch_xml_locate_user_merged(), switch_xml_parse_str_dynamic(), switch_xml_proc_inst(), switch_xml_set_attr(), switch_xml_set_attr_d(), and switch_xml_set_attr_d_buf().

4804 {
4805  char *s = strdup(_s);
4806  switch_assert(s);
4807  return s;
4808 }
#define switch_assert(expr)

◆ switch_needs_url_encode()

static int switch_needs_url_encode ( const char *  s)
inlinestatic

Definition at line 1214 of file switch_utils.h.

References end_of_p, pool, switch_core_session_url_encode(), switch_core_session_url_encode_opt(), switch_core_url_encode(), switch_core_url_encode_opt(), SWITCH_DECLARE, switch_find_end_paren(), switch_simple_email(), switch_url_decode(), switch_url_encode(), switch_url_encode_opt(), SWITCH_URL_UNSAFE, and to.

1215 {
1216  const char hex[] = "0123456789ABCDEF";
1217  const char *p, *e = end_of_p(s);
1218 
1219 
1220  for(p = s; p && *p; p++) {
1221  if (*p == '%' && e-p > 1) {
1222  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
1223  p++;
1224  continue;
1225  }
1226  }
1227 
1228  if (strchr(SWITCH_URL_UNSAFE, *p)) {
1229  return 1;
1230  }
1231  }
1232 
1233  return 0;
1234 }
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
#define end_of_p(_s)
Definition: switch_utils.h:686

◆ switch_network_ipv4_mapped_ipv6_addr()

char* switch_network_ipv4_mapped_ipv6_addr ( const char *  ip_str)

Definition at line 582 of file switch_utils.c.

Referenced by switch_check_network_list_ip_port_token(), and switch_network_list_perform_add_cidr_token().

583 {
584  /* ipv4 mapped ipv6 address */
585 
586  if (strncasecmp(ip_str, "::ffff:", 7)) {
587  return NULL;
588  }
589 
590  return strdup(ip_str + 7);
591 }

◆ switch_network_list_add_cidr_port_token()

switch_status_t switch_network_list_add_cidr_port_token ( switch_network_list_t list,
const char *  cidr_str,
switch_bool_t  ok,
const char *  token,
switch_network_port_range_p  port 
)

Definition at line 675 of file switch_utils.c.

References switch_assert, switch_network_list_perform_add_cidr_token(), switch_safe_free, switch_separate_string(), and SWITCH_STATUS_SUCCESS.

Referenced by switch_load_network_lists(), and switch_network_list_add_cidr_token().

676 {
677  char *cidr_str_dup = NULL;
679 
680  if (strchr(cidr_str, ',')) {
681  char *argv[32] = { 0 };
682  int i, argc;
683  cidr_str_dup = strdup(cidr_str);
684 
685  switch_assert(cidr_str_dup);
686  if ((argc = switch_separate_string(cidr_str_dup, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
687  for (i = 0; i < argc; i++) {
688  switch_status_t this_status;
689  if ((this_status = switch_network_list_perform_add_cidr_token(list, argv[i], ok, token, port)) != SWITCH_STATUS_SUCCESS) {
690  status = this_status;
691  }
692  }
693  }
694  } else {
695  status = switch_network_list_perform_add_cidr_token(list, cidr_str, ok, token, port);
696  }
697 
698  switch_safe_free(cidr_str_dup);
699  return status;
700 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
switch_status_t
Common return values.
unsigned int switch_separate_string(char *buf, char delim, char **array, unsigned int arraylen)
#define switch_assert(expr)
switch_status_t switch_network_list_perform_add_cidr_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token, switch_network_port_range_p port)
Definition: switch_utils.c:619

◆ switch_network_list_add_cidr_token()

switch_status_t switch_network_list_add_cidr_token ( switch_network_list_t list,
const char *  cidr_str,
switch_bool_t  ok,
const char *  token 
)

Definition at line 702 of file switch_utils.c.

References switch_network_list_add_cidr_port_token().

Referenced by switch_load_network_lists(), and switch_parse_audio_col().

703 {
704  return switch_network_list_add_cidr_port_token(list, cidr_str, ok, token, NULL);
705 }
switch_status_t switch_network_list_add_cidr_port_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token, switch_network_port_range_p port)
Definition: switch_utils.c:675

◆ switch_network_list_add_host_mask()

switch_status_t switch_network_list_add_host_mask ( switch_network_list_t list,
const char *  host,
const char *  mask_str,
switch_bool_t  ok 
)

Definition at line 737 of file switch_utils.c.

References switch_network_list_add_host_port_mask().

Referenced by switch_load_network_lists().

738 {
739  return switch_network_list_add_host_port_mask(list, host, mask_str, ok, NULL);
740 }
switch_status_t switch_network_list_add_host_port_mask(switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok, switch_network_port_range_p port)
Definition: switch_utils.c:707

◆ switch_network_list_add_host_port_mask()

switch_status_t switch_network_list_add_host_port_mask ( switch_network_list_t list,
const char *  host,
const char *  mask_str,
switch_bool_t  ok,
switch_network_port_range_p  port 
)

Definition at line 707 of file switch_utils.c.

References switch_network_node::bits, switch_network_node::ip, switch_network_node::mask, switch_network_node::next, switch_network_list::node_head, switch_network_node::ok, switch_network_list::pool, switch_network_node::port_range, switch_network_node::str, switch_core_alloc, switch_core_sprintf(), switch_inet_pton(), SWITCH_STATUS_SUCCESS, and ip_t::v4.

Referenced by switch_load_network_lists(), and switch_network_list_add_host_mask().

708 {
709  ip_t ip, mask;
710  switch_network_node_t *node;
711 
712  switch_inet_pton(AF_INET, host, &ip);
713  switch_inet_pton(AF_INET, mask_str, &mask);
714 
715  node = switch_core_alloc(list->pool, sizeof(*node));
716 
717  node->ip.v4 = ntohl(ip.v4);
718  node->mask.v4 = ntohl(mask.v4);
719  node->ok = ok;
720  if(port) {
721  memcpy(&node->port_range, port, sizeof(switch_network_port_range_t));
722  }
723 
724  /* http://graphics.stanford.edu/~seander/bithacks.html */
725  mask.v4 = mask.v4 - ((mask.v4 >> 1) & 0x55555555);
726  mask.v4 = (mask.v4 & 0x33333333) + ((mask.v4 >> 2) & 0x33333333);
727  node->bits = (((mask.v4 + (mask.v4 >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
728 
729  node->str = switch_core_sprintf(list->pool, "%s:%s", host, mask_str);
730 
731  node->next = list->node_head;
732  list->node_head = node;
733 
734  return SWITCH_STATUS_SUCCESS;
735 }
struct switch_network_node * node_head
Definition: switch_utils.c:80
struct switch_network_node * next
Definition: switch_utils.c:75
uint32_t v4
Definition: switch_utils.h:278
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
char * ip
Definition: switch_msrp.c:60
switch_memory_pool_t * pool
Definition: switch_utils.c:82
switch_network_port_range_t port_range
Definition: switch_utils.c:74
int switch_inet_pton(int af, const char *src, void *dst)
Definition: switch_utils.c:92
switch_bool_t ok
Definition: switch_utils.c:71
char * switch_core_sprintf(_In_ switch_memory_pool_t *pool, _In_z_ _Printf_format_string_ const char *fmt,...)
printf-style style printing routine. The data is output to a string allocated from the pool ...

◆ switch_network_list_create()

switch_status_t switch_network_list_create ( switch_network_list_t **  list,
const char *  name,
switch_bool_t  default_type,
switch_memory_pool_t pool 
)

Definition at line 459 of file switch_utils.c.

References switch_network_list::default_type, switch_network_list::name, pool, switch_network_list::pool, switch_core_alloc, switch_core_new_memory_pool, switch_core_strdup, and SWITCH_STATUS_SUCCESS.

Referenced by switch_load_network_lists(), and switch_parse_audio_col().

461 {
462  switch_network_list_t *new_list;
463 
464  if (!pool) {
466  }
467 
468  new_list = switch_core_alloc(pool, sizeof(**list));
469  new_list->pool = pool;
470  new_list->default_type = default_type;
471  new_list->name = switch_core_strdup(new_list->pool, name);
472 
473  *list = new_list;
474 
475  return SWITCH_STATUS_SUCCESS;
476 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
switch_memory_pool_t * pool
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
switch_memory_pool_t * pool
Definition: switch_utils.c:82
switch_bool_t default_type
Definition: switch_utils.c:81
const char *const name
Definition: switch_cJSON.h:250

◆ switch_network_list_validate_ip6_port_token()

switch_bool_t switch_network_list_validate_ip6_port_token ( switch_network_list_t list,
ip_t  ip,
int  port,
const char **  token 
)

Definition at line 497 of file switch_utils.c.

References switch_network_node::bits, switch_network_list::default_type, switch_network_node::family, switch_network_node::ip, switch_network_node::mask, switch_network_node::next, switch_network_list::node_head, switch_network_node::ok, SWITCH_FALSE, switch_testv6_subnet(), SWITCH_TRUE, and switch_network_node::token.

Referenced by switch_check_network_list_ip_port_token(), and switch_network_list_validate_ip6_token().

498 {
499  switch_network_node_t *node;
500  switch_bool_t ok = list->default_type;
501  uint32_t bits = 0;
502 
503  for (node = list->node_head; node; node = node->next) {
504  if (node->family == AF_INET) continue;
505 
506  if (node->bits >= bits && switch_testv6_subnet(ip, node->ip, node->mask)) {
507  if (node->ok) {
508  ok = SWITCH_TRUE;
509  } else {
510  ok = SWITCH_FALSE;
511  }
512 
513  bits = node->bits;
514 
515  if (token) {
516  *token = node->token;
517  }
518  }
519  }
520 
521  return ok;
522 }
struct switch_network_node * node_head
Definition: switch_utils.c:80
switch_bool_t
Definition: switch_types.h:437
struct switch_network_node * next
Definition: switch_utils.c:75
switch_bool_t switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask)
Definition: switch_utils.c:483
switch_bool_t default_type
Definition: switch_utils.c:81
switch_bool_t ok
Definition: switch_utils.c:71

◆ switch_network_list_validate_ip6_token()

switch_bool_t switch_network_list_validate_ip6_token ( switch_network_list_t list,
ip_t  ip,
const char **  token 
)

Definition at line 572 of file switch_utils.c.

References switch_network_list_validate_ip6_port_token().

573 {
574  return switch_network_list_validate_ip6_port_token(list, ip, 0, token);
575 }
switch_bool_t switch_network_list_validate_ip6_port_token(switch_network_list_t *list, ip_t ip, int port, const char **token)
Definition: switch_utils.c:497

◆ switch_network_list_validate_ip_port_token()

switch_bool_t switch_network_list_validate_ip_port_token ( switch_network_list_t list,
uint32_t  ip,
int  port,
const char **  token 
)

Definition at line 546 of file switch_utils.c.

References switch_network_node::bits, switch_network_list::default_type, switch_network_node::family, switch_network_node::ip, is_port_in_node(), switch_network_node::mask, switch_network_node::next, switch_network_list::node_head, switch_network_node::ok, SWITCH_FALSE, switch_test_subnet, SWITCH_TRUE, switch_network_node::token, and ip_t::v4.

Referenced by switch_check_network_list_ip_port_token(), and switch_network_list_validate_ip_token().

547 {
548  switch_network_node_t *node;
549  switch_bool_t ok = list->default_type;
550  uint32_t bits = 0;
551 
552  for (node = list->node_head; node; node = node->next) {
553  if (node->family == AF_INET6) continue; /* want AF_INET */
554  if (node->bits >= bits && switch_test_subnet(ip, node->ip.v4, node->mask.v4) && is_port_in_node(port, node)) {
555  if (node->ok) {
556  ok = SWITCH_TRUE;
557  } else {
558  ok = SWITCH_FALSE;
559  }
560 
561  bits = node->bits;
562 
563  if (token) {
564  *token = node->token;
565  }
566  }
567  }
568 
569  return ok;
570 }
struct switch_network_node * node_head
Definition: switch_utils.c:80
#define switch_test_subnet(_ip, _net, _mask)
switch_bool_t
Definition: switch_types.h:437
switch_bool_t is_port_in_node(int port, switch_network_node_t *node)
Definition: switch_utils.c:524
struct switch_network_node * next
Definition: switch_utils.c:75
uint32_t v4
Definition: switch_utils.h:278
char * ip
Definition: switch_msrp.c:60
switch_bool_t default_type
Definition: switch_utils.c:81
switch_bool_t ok
Definition: switch_utils.c:71

◆ switch_network_list_validate_ip_token()

switch_bool_t switch_network_list_validate_ip_token ( switch_network_list_t list,
uint32_t  ip,
const char **  token 
)

Definition at line 577 of file switch_utils.c.

References switch_network_list_validate_ip_port_token().

578 {
579  return switch_network_list_validate_ip_port_token(list, ip, 0, token);
580 }
switch_bool_t switch_network_list_validate_ip_port_token(switch_network_list_t *list, uint32_t ip, int port, const char **token)
Definition: switch_utils.c:546
char * ip
Definition: switch_msrp.c:60

◆ switch_number_cmp()

int switch_number_cmp ( const char *  exp,
int  val 
)

Definition at line 4133 of file switch_utils.c.

References b.

Referenced by switch_xml_std_datetime_check().

4134 {
4135  // Expression exp must be a comma separated list of numbers or ranges.
4136  // To match numbers not in range 9-17, enter the reversed range 18-8.
4137  for (;; ++exp) {
4138  int a = strtol(exp, (char **)&exp, 10);
4139  if (*exp != '-') {
4140  if (a == val)
4141  return 1;
4142  } else {
4143  int b = strtol(++exp, (char **)&exp, 10);
4144  if (a <= b ? (val >= a && val <=b ) : (val >= a || val <= b))
4145  return 1;
4146  }
4147  if (*exp != ',')
4148  return 0;
4149  }
4150 }
const cJSON *const b
Definition: switch_cJSON.h:243

◆ switch_parse_audio_col()

static const char* switch_parse_audio_col ( switch_audio_col_t  col)
inlinestatic

Definition at line 1332 of file switch_utils.h.

References ip, name, SWITCH_AUDIO_COL_STR_ARTIST, SWITCH_AUDIO_COL_STR_COMMENT, SWITCH_AUDIO_COL_STR_COPYRIGHT, SWITCH_AUDIO_COL_STR_DATE, SWITCH_AUDIO_COL_STR_SOFTWARE, SWITCH_AUDIO_COL_STR_TITLE, SWITCH_DECLARE, switch_network_list_add_cidr_token(), switch_network_list_create(), and switch_parse_cidr().

1333 {
1334  const char *field = NULL;
1335 
1336  switch (col) {
1338  field = "title";
1339  break;
1341  field = "comment";
1342  break;
1344  field = "artist";
1345  break;
1347  field = "date";
1348  break;
1350  field = "software";
1351  break;
1353  field = "copyright";
1354  break;
1355  default:
1356  break;
1357  }
1358 
1359  return field;
1360 }

◆ switch_parse_bandwidth_string()

static int32_t switch_parse_bandwidth_string ( const char *  bwv)
inlinestatic

Definition at line 1137 of file switch_utils.h.

Referenced by switch_core_media_gen_local_sdp(), and switch_core_session_parse_codec_settings().

1138 {
1139  float bw = 0;
1140 
1141  if (!bwv) return 0;
1142 
1143  if (!strcasecmp(bwv, "auto")) {
1144  return -1;
1145  }
1146 
1147  if ((bw = (float) atof(bwv))) {
1148  if (bw < 0) return 0;
1149 
1150  if (strstr(bwv, "KB")) {
1151  bw *= 8;
1152  } else if (strstr(bwv, "mb")) {
1153  bw *= 1024;
1154  } else if (strstr(bwv, "MB")) {
1155  bw *= 8192;
1156  }
1157  }
1158 
1159  return (int32_t) roundf(bw);
1160 }

◆ switch_parse_cidr()

int switch_parse_cidr ( const char *  string,
ip_t ip,
ip_t mask,
uint32_t *  bitp 
)

Definition at line 743 of file switch_utils.c.

References switch_network_node::bits, switch_network_node::ip, switch_network_node::mask, switch_copy_string(), switch_inet_pton(), ip_t::v4, and ip_t::v6.

Referenced by switch_check_network_list_ip_port_token(), switch_network_list_perform_add_cidr_token(), and switch_parse_audio_col().

744 {
745  char host[128];
746  char *bit_str;
747  int32_t bits;
748  const char *ipv6;
749  ip_t *maskv = mask;
750  ip_t *ipv = ip;
751 
752  switch_copy_string(host, string, sizeof(host) - 1);
753  bit_str = strchr(host, '/');
754 
755  if (!bit_str) {
756  return -1;
757  }
758 
759  *bit_str++ = '\0';
760  bits = atoi(bit_str);
761  ipv6 = strchr(string, ':');
762  if (ipv6) {
763  int32_t i, n;
764  uint32_t k;
765 
766  if (bits < 0 || bits > 128) {
767  return -2;
768  }
769 
770  bits = atoi(bit_str);
771  switch_inet_pton(AF_INET6, host, (unsigned char *)ip);
772 
773  for (n = bits, i = 0; i < 16; i++) {
774  k = (n > 8) ? 8 : n;
775  maskv->v6.s6_addr[i] = 0xFF & ~(0xFF >> k); /* k = 0 gives 0x00, k = 8 gives 0xFF */
776  n -= k;
777  }
778  } else {
779  if (bits < 0 || bits > 32) {
780  return -2;
781  }
782 
783  bits = atoi(bit_str);
784  switch_inet_pton(AF_INET, host, (unsigned char *)ip);
785  ipv->v4 = htonl(ipv->v4);
786 
787  maskv->v4 = 0xFFFFFFFF & ~(0xFFFFFFFF >> bits);
788  }
789 
790  *bitp = bits;
791 
792  return 0;
793 }
uint32_t v4
Definition: switch_utils.h:278
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
char * ip
Definition: switch_msrp.c:60
struct in6_addr v6
Definition: switch_utils.h:279
int switch_inet_pton(int af, const char *src, void *dst)
Definition: switch_utils.c:92

◆ switch_parse_cpu_string()

static uint32_t switch_parse_cpu_string ( const char *  cpu)
inlinestatic

Definition at line 1162 of file switch_utils.h.

References switch_core_cpu_count().

1163 {
1164  int cpu_count = switch_core_cpu_count();
1165  int ncpu;
1166 
1167  if (!cpu) return 1;
1168 
1169  if (!strcasecmp(cpu, "auto")) {
1170  return (uint32_t)((cpu_count * 3) / 2);
1171  }
1172 
1173  if (!strncasecmp(cpu, "cpu/", 4)) { /* cpu/2 or cpu/2/<max> */
1174  const char *has_max = cpu;
1175  float divisor;
1176  int max = cpu_count;
1177 
1178  cpu +=4;
1179 
1180  has_max = strchr(cpu, '/');
1181 
1182  if (has_max > cpu) {
1183  max = atoi(has_max + 1);
1184  }
1185 
1186  divisor = (float)atof(cpu);
1187 
1188  if (divisor <= 0) divisor = 1;
1189 
1190  ncpu = (int)(cpu_count / divisor);
1191 
1192  if (ncpu <= 0) return 1;
1193 
1194  if (ncpu > max) return (uint32_t)max;
1195 
1196  return (uint32_t)ncpu;
1197  } else if (!strcasecmp(cpu, "cpu")) {
1198  ncpu = cpu_count;
1199  } else {
1200  ncpu = atoi(cpu);
1201 
1202  if (strrchr(cpu, '%')) {
1203  ncpu = (int) (cpu_count * ((float)ncpu / 100));
1204  }
1205  }
1206 
1207  if (ncpu > cpu_count) return (uint32_t)cpu_count;
1208 
1209  if (ncpu <= 0) return 1;
1210 
1211  return ncpu;
1212 }
uint32_t switch_core_cpu_count(void)
Definition: switch_core.c:1055

◆ switch_pool_strip_whitespace()

char* switch_pool_strip_whitespace ( switch_memory_pool_t pool,
const char *  str 
)

Definition at line 1439 of file switch_utils.c.

References switch_network_node::str, switch_assert, SWITCH_BLANK_STRING, switch_core_strdup, and zstr.

1440 {
1441  const char *sp = str;
1442  char *p, *s = NULL;
1443  size_t len;
1444 
1445  if (zstr(sp)) {
1447  }
1448 
1449  while ((*sp == 13 ) || (*sp == 10 ) || (*sp == 9 ) || (*sp == 32) || (*sp == 11) ) {
1450  sp++;
1451  }
1452 
1453  if (zstr(sp)) {
1455  }
1456 
1457  s = switch_core_strdup(pool, sp);
1458  switch_assert(s);
1459 
1460  if ((len = strlen(s)) > 0) {
1461  p = s + (len - 1);
1462 
1463  while ((p >= s) && ((*p == 13 ) || (*p == 10 ) || (*p == 9 ) || (*p == 32) || (*p == 11))) {
1464  *p-- = '\0';
1465  }
1466  }
1467 
1468  return s;
1469 }
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
switch_memory_pool_t * pool
#define zstr(x)
Definition: switch_utils.h:314
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
#define switch_assert(expr)

◆ switch_print_bits()

static char* switch_print_bits ( const unsigned char *  byte,
char *  buf,
switch_size_t  buflen 
)
inlinestatic

Definition at line 412 of file switch_utils.h.

References buf.

413 {
414  int i;
415  switch_size_t j = 0, k = 0, l = 0;
416 
417  while(k < buflen) {
418  l = 0;
419  for (i = 7; i >= 0; i--) {
420  buf[j++] = (*byte & (1 << i)) ? '1' : '0';
421  if (++l % 4 == 0) {
422  buf[j++] = ' ';
423  }
424  }
425  k++;
426  byte++;
427  }
428 
429  if (buf[j-1] == ' ') j--;
430  buf[j++] = '\0';
431  return buf;
432 }
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
switch_byte_t switch_byte_t uint32_t buflen

◆ switch_print_host()

char* switch_print_host ( switch_sockaddr_t addr,
char *  buf,
switch_size_t  len 
)

Definition at line 98 of file switch_utils.c.

References buf, switch_get_addr(), and switch_sockaddr_get_port().

99 {
100  switch_port_t port;
101 
102  switch_get_addr(buf, len, addr);
103  port = switch_sockaddr_get_port(addr);
104 
105  snprintf(buf + strlen(buf), len - strlen(buf), ":%d", port);
106  return buf;
107 }
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_byte_t switch_byte_t * buf
uint16_t switch_port_t
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979

◆ switch_priority_name()

const char* switch_priority_name ( switch_priority_t  priority)

Return a printable name of a switch_priority_t.

Parameters
prioritythe priority to get the name of
Returns
the printable form of the priority

Definition at line 2173 of file switch_utils.c.

References SWITCH_PRIORITY_HIGH, SWITCH_PRIORITY_LOW, and SWITCH_PRIORITY_NORMAL.

Referenced by switch_errno_is_break(), and switch_event_set_priority().

2174 {
2175  switch (priority) { /*lol */
2177  return "NORMAL";
2178  case SWITCH_PRIORITY_LOW:
2179  return "LOW";
2180  case SWITCH_PRIORITY_HIGH:
2181  return "HIGH";
2182  default:
2183  return "INVALID";
2184  }
2185 }

◆ switch_replace_char()

char* switch_replace_char ( char *  str,
char  from,
char  to,
switch_bool_t  dup 
)

Definition at line 1419 of file switch_utils.c.

References switch_network_node::str, switch_assert, and to.

1420 {
1421  char *p;
1422 
1423  if (dup) {
1424  p = strdup(str);
1425  switch_assert(p);
1426  } else {
1427  p = str;
1428  }
1429 
1430  for (; p && *p; p++) {
1431  if (*p == from) {
1432  *p = to;
1433  }
1434  }
1435 
1436  return p;
1437 }
cJSON *const to
#define switch_assert(expr)

◆ switch_resolve_host()

switch_status_t switch_resolve_host ( const char *  host,
char *  buf,
size_t  buflen 
)

Definition at line 1841 of file switch_utils.c.

References get_addr(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by check_ice(), and switch_false().

1842 {
1843 
1844  struct addrinfo *ai;
1845 
1846  if (getaddrinfo(host, 0, 0, &ai)) {
1847  return SWITCH_STATUS_FALSE;
1848  }
1849 
1850  get_addr(buf, buflen, ai->ai_addr, sizeof(struct sockaddr_storage));
1851 
1852  freeaddrinfo(ai);
1853 
1854  return SWITCH_STATUS_SUCCESS;
1855 }
switch_byte_t switch_byte_t * buf
switch_byte_t switch_byte_t uint32_t buflen
char * get_addr(char *buf, switch_size_t len, struct sockaddr *sa, socklen_t salen)
find the char representation of an ip adress

◆ switch_rfc2833_to_char()

char switch_rfc2833_to_char ( int  event)

Return the RFC2833 character based on an event id.

Parameters
eventthe event id to convert
Returns
the character represented by the event or null for an invalid event

Definition at line 2548 of file switch_utils.c.

References RFC2833_CHARS.

Referenced by handle_rfc2833(), and switch_errno_is_break().

2549 {
2550  if (event > -1 && event < (int32_t) sizeof(RFC2833_CHARS)) {
2551  return RFC2833_CHARS[event];
2552  }
2553  return '\0';
2554 }
static char RFC2833_CHARS[]

◆ switch_round_to_step()

static uint32_t switch_round_to_step ( uint32_t  num,
uint32_t  step 
)
inlinestatic

Definition at line 81 of file switch_utils.h.

Referenced by switch_core_media_get_video_fps().

82 {
83  uint32_t r;
84  uint32_t x;
85 
86  if (!num) return 0;
87 
88  r = (num % step);
89  x = num - r;
90 
91  if (r > step / 2) {
92  x += step;
93  }
94 
95  return x;
96 }

◆ switch_safe_atoi()

static int switch_safe_atoi ( const char *  nptr,
int  dft 
)
inlinestatic

Turn a string into an integer (default if NULL)

Parameters
nptrthe string
dftthe default
Returns
the integer version of the string or the default

Definition at line 851 of file switch_utils.h.

Referenced by switch_core_media_process_udptl().

852 {
853  return nptr ? atoi(nptr) : dft;
854 }

◆ switch_safe_atol()

static long int switch_safe_atol ( const char *  nptr,
long int  dft 
)
inlinestatic

Turn a string into a long integer (default if NULL)

Parameters
nptrthe string
dftthe default
Returns
the long integer version of the string or the default

Definition at line 863 of file switch_utils.h.

864 {
865  return nptr ? atol(nptr) : dft;
866 }

◆ switch_safe_atoll()

static long long int switch_safe_atoll ( const char *  nptr,
long long int  dft 
)
inlinestatic

Turn a string into a long long integer (default if NULL)

Parameters
nptrthe string
dftthe default
Returns
the long long integer version of the string or the default

Definition at line 875 of file switch_utils.h.

876 {
877  return nptr ? atoll(nptr) : dft;
878 }

◆ switch_safe_strdup()

static char* switch_safe_strdup ( const char *  it)
inlinestatic

Definition at line 887 of file switch_utils.h.

Referenced by switch_html_strip().

888 {
889  if (it) {
890  return strdup(it);
891  }
892 
893  return NULL;
894 }

◆ switch_sanitize_number()

static char* switch_sanitize_number ( char *  number)
inlinestatic

Definition at line 737 of file switch_utils.h.

References number, and switch_assert.

Referenced by switch_ivr_originate().

738 {
739  char *p = number, *q;
740  char warp[] = "/:";
741  int i;
742 
744 
745  if (!(strchr(p, '/') || strchr(p, ':') || strchr(p, '@') || strchr(p, '%'))) {
746  return number;
747  }
748 
749  while ((q = strrchr(p, '@')))
750  *q = '\0';
751 
752  while ((q = strrchr(p, '%')))
753  *q = '\0';
754 
755  for (i = 0; i < (int) strlen(warp); i++) {
756  while (p && (q = strchr(p, warp[i])))
757  p = q + 1;
758  }
759 
760  return p;
761 }
const char *const const double number
Definition: switch_cJSON.h:254
#define switch_assert(expr)

◆ switch_separate_file_params()

static void switch_separate_file_params ( const char *  file,
char **  file_portion,
char **  params_portion 
)
inlinestatic

Definition at line 1252 of file switch_utils.h.

References switch_assert, and switch_find_end_paren().

1253 {
1254  char *e = NULL;
1255  char *space = strdup(file);
1256 
1257  switch_assert(space);
1258  file = space;
1259 
1260  *file_portion = NULL;
1261  *params_portion = NULL;
1262 
1263  while (*file == '{') {
1264  e = switch_find_end_paren(file, '{', '}');
1265  file = e + 1;
1266  while(*file == ' ') file++;
1267  }
1268 
1269 
1270  if (e) {
1271  *file_portion = strdup((char *)file);
1272  *++e = '\0';
1273  *params_portion = (char *)space;
1274  } else {
1275  *file_portion = (char *)space;
1276  }
1277 
1278  return;
1279 }
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:796
#define switch_assert(expr)

◆ switch_separate_paren_args()

char* switch_separate_paren_args ( char *  str)

Definition at line 1582 of file switch_utils.c.

Referenced by switch_ivr_phrase_macro_event().

1583 {
1584  char *e, *args;
1585  switch_size_t br;
1586 
1587  if ((args = strchr(str, '('))) {
1588  e = args - 1;
1589  *args++ = '\0';
1590  while (*e == ' ') {
1591  *e-- = '\0';
1592  }
1593  e = args;
1594  br = 1;
1595  while (e && *e) {
1596  if (*e == '(') {
1597  br++;
1598  } else if (br > 1 && *e == ')') {
1599  br--;
1600  } else if (br == 1 && *e == ')') {
1601  *e = '\0';
1602  break;
1603  }
1604  e++;
1605  }
1606  }
1607 
1608  return args;
1609 }
uintptr_t switch_size_t

◆ switch_separate_string()

unsigned int switch_separate_string ( _In_ char *  buf,
char  delim,
_Post_count_(return) char **  array,
unsigned int  arraylen 
)

◆ switch_separate_string_cheap()

unsigned int switch_separate_string_cheap ( _In_ const char *  buf,
char  delim,
_Post_count_(return) const char **  array,
_Post_count_(return) unsigned int  larray[],
unsigned int  arraylen 
)

Separate a string into an array based on a character delimiter, no memory corruption! DOESN'T support removing quotes like what's in switch_separate_string()!

Parameters
bufthe string to parse
delimthe character delimiter
arraythe array to split the values into
larraythe array that hold the lenth of each element
arraylenthe max number of elements in the array
Returns
the number of elements added to the array

◆ switch_separate_string_string()

unsigned int switch_separate_string_string ( char *  buf,
char *  delim,
_Post_count_(return) char **  array,
unsigned int  arraylen 
)

◆ switch_simple_email()

switch_bool_t switch_simple_email ( const char *  to,
const char *  from,
const char *  headers,
const char *  body,
const char *  file,
const char *  convert_cmd,
const char *  convert_ext 
)

Definition at line 1117 of file switch_utils.c.

References b, B64BUFFLEN, buf, filename, in, switch_runtime::mailer_app, switch_runtime::mailer_app_args, O_BINARY, runtime, switch_b64_table, SWITCH_CHANNEL_LOG, switch_core_mime_ext2type(), switch_cut_path(), switch_epoch_time_now(), SWITCH_FALSE, SWITCH_GLOBAL_dirs, SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_mprintf(), SWITCH_PATH_SEPARATOR, switch_safe_free, switch_snprintf(), switch_stristr(), switch_system(), SWITCH_TRUE, switch_util_quote_shell_arg(), switch_directories::temp_dir, write_buf(), zstr, and zstr_buf.

Referenced by email(), and switch_needs_url_encode().

1121 {
1122  char *bound = "XXXX_boundary_XXXX";
1123  const char *mime_type = "audio/inline";
1124  char filename[80], buf[B64BUFFLEN];
1125  int fd = -1, ifd = -1;
1126  int x = 0, y = 0, bytes = 0, ilen = 0;
1127  unsigned int b = 0, l = 0;
1128  unsigned char in[B64BUFFLEN];
1129  unsigned char out[B64BUFFLEN + 512];
1130  char *dupfile = NULL, *ext = NULL;
1131  char *newfile = NULL;
1132  switch_bool_t rval = SWITCH_FALSE;
1133  const char *err = NULL;
1134 
1135  filename[0] = '\0';
1136 
1137  if (zstr(to)) {
1138  err = "No to address specified";
1139  goto end;
1140  }
1141 
1142  if (!zstr(file) && !zstr(convert_cmd) && !zstr(convert_ext)) {
1143  if (strrchr(file, '.')) {
1144  dupfile = strdup(file);
1145  if ((ext = strrchr(dupfile, '.'))) {
1146  *ext++ = '\0';
1147  newfile = switch_mprintf("%s.%s", dupfile, convert_ext);
1148  }
1149  }
1150 
1151  if (newfile) {
1152  char cmd[1024] = "";
1153  switch_snprintf(cmd, sizeof(cmd), "%s %s %s", convert_cmd, file, newfile);
1154  switch_system(cmd, SWITCH_TRUE);
1155  if (strcmp(file, newfile)) {
1156  file = newfile;
1157  } else {
1158  switch_safe_free(newfile);
1159  }
1160  }
1161 
1162  switch_safe_free(dupfile);
1163  }
1164 
1165  switch_snprintf(filename, 80, "%s%smail.%d%04x", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, (int)(switch_time_t) switch_epoch_time_now(NULL), rand() & 0xffff);
1166 
1167  if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644)) > -1) {
1168  if (file) {
1169  if ((ifd = open(file, O_RDONLY | O_BINARY)) < 0) {
1170  rval = SWITCH_FALSE;
1171  err = "Cannot open tmp file\n";
1172  goto end;
1173  }
1174  }
1175 
1176  if (!file && (!body || !switch_stristr("content-type", body))) {
1177  bound = NULL;
1178  }
1179 
1180  if (bound) {
1181  switch_snprintf(buf, B64BUFFLEN, "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n", bound);
1182  if (!write_buf(fd, buf)) {
1183  rval = SWITCH_FALSE;
1184  err = "write error.";
1185  goto end;
1186  }
1187  }
1188 
1189  if (headers && !write_buf(fd, headers)) {
1190  rval = SWITCH_FALSE;
1191  err = "write error.";
1192  goto end;
1193  }
1194 
1195  if (!write_buf(fd, "\n\n")) {
1196  rval = SWITCH_FALSE;
1197  err = "write error.";
1198  goto end;
1199  }
1200 
1201  if (bound) {
1202  if (body && switch_stristr("content-type", body)) {
1203  switch_snprintf(buf, B64BUFFLEN, "--%s\n", bound);
1204  } else {
1205  switch_snprintf(buf, B64BUFFLEN, "--%s\nContent-Type: text/plain\n\n", bound);
1206  }
1207  if (!write_buf(fd, buf)) {
1208  rval = SWITCH_FALSE;
1209  err = "write error.";
1210  goto end;
1211  }
1212  }
1213 
1214  if (body) {
1215  if (!write_buf(fd, body)) {
1216  rval = SWITCH_FALSE;
1217  err = "write error.";
1218  goto end;
1219  }
1220  }
1221 
1222  if (file && bound) {
1223  const char *stipped_file = switch_cut_path(file);
1224  const char *new_type;
1225  char *ext;
1226 
1227  if ((ext = strrchr(stipped_file, '.'))) {
1228  ext++;
1229  if ((new_type = switch_core_mime_ext2type(ext))) {
1230  mime_type = new_type;
1231  }
1232  }
1233 
1235  "\n\n--%s\nContent-Type: %s; name=\"%s\"\n"
1236  "Content-ID: <ATTACHED@freeswitch.org>\n"
1237  "Content-Transfer-Encoding: base64\n"
1238  "Content-Description: Sound attachment.\n"
1239  "Content-Disposition: attachment; filename=\"%s\"\n\n", bound, mime_type, stipped_file, stipped_file);
1240  if (!write_buf(fd, buf)) {
1241  rval = SWITCH_FALSE;
1242  err = "write error.";
1243  goto end;
1244  }
1245 
1246  while ((ilen = read(ifd, in, B64BUFFLEN))) {
1247  for (x = 0; x < ilen; x++) {
1248  b = (b << 8) + in[x];
1249  l += 8;
1250  while (l >= 6) {
1251  out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64];
1252  if (++y != 72)
1253  continue;
1254  out[bytes++] = '\n';
1255  y = 0;
1256  }
1257  }
1258  if (write(fd, &out, bytes) != bytes) {
1259  rval = -1;
1260  break;
1261  } else {
1262  bytes = 0;
1263  }
1264 
1265  }
1266 
1267  if (l > 0) {
1268  out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64];
1269  }
1270  if (l != 0)
1271  while (l < 6) {
1272  out[bytes++] = '=', l += 2;
1273  }
1274  if (write(fd, &out, bytes) != bytes) {
1275  rval = -1;
1276  }
1277 
1278  }
1279 
1280  if (bound) {
1281  switch_snprintf(buf, B64BUFFLEN, "\n\n--%s--\n.\n", bound);
1282 
1283  if (!write_buf(fd, buf)) {
1284  rval = SWITCH_FALSE;
1285  err = "write error.";
1286  goto end;
1287  }
1288  }
1289  }
1290 
1291  if (fd > -1) {
1292  close(fd);
1293  fd = -1;
1294  }
1295 
1296  if (zstr(from)) {
1297  from = "freeswitch";
1298  }
1299 
1300  {
1301  char *to_arg = switch_util_quote_shell_arg(to);
1302  char *from_arg = switch_util_quote_shell_arg(from);
1303 #ifdef WIN32
1304  switch_snprintf(buf, B64BUFFLEN, "\"\"%s\" -f %s %s %s < \"%s\"\"", runtime.mailer_app, from_arg, runtime.mailer_app_args, to_arg, filename);
1305 #else
1306  switch_snprintf(buf, B64BUFFLEN, "/bin/cat %s | %s -f %s %s %s", filename, runtime.mailer_app, from_arg, runtime.mailer_app_args, to_arg);
1307 #endif
1308  switch_safe_free(to_arg); switch_safe_free(from_arg);
1309  }
1310  if (switch_system(buf, SWITCH_TRUE) < 0) {
1311  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf);
1312  err = "execute error";
1313  rval = SWITCH_FALSE;
1314  }
1315 
1316  if (zstr(err)) {
1317  if (file) {
1318  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed file [%s] to [%s]\n", filename, to);
1319  } else {
1320  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed data to [%s]\n", to);
1321  }
1322 
1323  rval = SWITCH_TRUE;
1324  }
1325 
1326  end:
1327 
1328  if (fd > -1) {
1329  close(fd);
1330  }
1331 
1332  if (!zstr_buf(filename) && unlink(filename) != 0) {
1333  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete file [%s]\n", filename);
1334  }
1335 
1336  if (ifd > -1) {
1337  close(ifd);
1338  }
1339 
1340 
1341  if (newfile) {
1342  unlink(newfile);
1343  free(newfile);
1344  }
1345 
1346  if (rval != SWITCH_TRUE) {
1347  if (zstr(err)) err = "Unknown Error";
1348 
1349  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "EMAIL NOT SENT, error [%s]\n", err);
1350  }
1351 
1352  return rval;
1353 }
#define SWITCH_CHANNEL_LOG
#define O_BINARY
cJSON *const to
static const char switch_b64_table[65]
switch_bool_t
Definition: switch_types.h:437
const cJSON *const b
Definition: switch_cJSON.h:243
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
struct switch_runtime runtime
Definition: switch_core.c:86
#define zstr(x)
Definition: switch_utils.h:314
int switch_system(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3342
#define SWITCH_PATH_SEPARATOR
Definition: switch_types.h:124
int64_t switch_time_t
Definition: switch_apr.h:188
switch_byte_t switch_byte_t * buf
switch_byte_t in
const char * switch_cut_path(const char *in)
Create a pointer to the file name in a given file path eliminating the directory name.
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
char * switch_util_quote_shell_arg(const char *string)
Quote shell argument.
#define zstr_buf(s)
Definition: switch_utils.h:318
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:82
static int write_buf(int fd, const char *buf)
#define B64BUFFLEN
const char * filename
const char * switch_core_mime_ext2type(const char *ext)
Definition: switch_core.c:1211
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:322
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_stristr(const char *instr, const char *str)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)

◆ switch_socket_waitfor()

int switch_socket_waitfor ( switch_pollfd_t poll,
int  ms 
)

Wait for a socket.

Parameters
pollthe pollfd to wait on
msthe number of milliseconds to wait
Returns
the requested condition

Definition at line 3541 of file switch_utils.c.

References switch_poll().

3542 {
3543  int nsds = 0;
3544 
3545  switch_poll(poll, 1, &nsds, ms);
3546 
3547  return nsds;
3548 }
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1100

◆ switch_split_date()

void switch_split_date ( const char *  exp,
int *  year,
int *  month,
int *  day 
)

Definition at line 3708 of file switch_utils.c.

References switch_assert, and switch_safe_free.

3709 {
3710  char *dup = strdup(exp);
3711  char *syear = NULL;
3712  char *smonth = NULL;
3713  char *sday = NULL;
3714 
3715  switch_assert(dup);
3716 
3717  syear = dup;
3718  if ((smonth=strchr(dup, '-'))) {
3719  *smonth++ = '\0';
3720  if ((sday=strchr(smonth, '-'))) {
3721  *sday++ = '\0';
3722  if (year) {
3723  *year = atol(syear);
3724  }
3725  if (month) {
3726  *month = atol(smonth);
3727  }
3728  if (day) {
3729  *day = atol(sday);
3730  }
3731  }
3732  }
3733  switch_safe_free(dup);
3734  return;
3735 
3736 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define switch_assert(expr)

◆ switch_split_time()

void switch_split_time ( const char *  exp,
int *  hour,
int *  min,
int *  sec 
)

Definition at line 3675 of file switch_utils.c.

References switch_assert, and switch_safe_free.

3676 {
3677  char *dup = strdup(exp);
3678  char *shour = NULL;
3679  char *smin = NULL;
3680  char *ssec = NULL;
3681 
3682  switch_assert(dup);
3683 
3684  shour = dup;
3685  if ((smin=strchr(dup, ':'))) {
3686  *smin++ = '\0';
3687  if ((ssec=strchr(smin, ':'))) {
3688  *ssec++ = '\0';
3689  } else {
3690  ssec = "00";
3691  }
3692  if (hour) {
3693  *hour = atol(shour);
3694  }
3695  if (min) {
3696  *min = atol(smin);
3697  }
3698  if (sec) {
3699  *sec = atol(ssec);
3700  }
3701 
3702  }
3703  switch_safe_free(dup);
3704  return;
3705 
3706 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define switch_assert(expr)

◆ switch_split_user_domain()

int switch_split_user_domain ( char *  in,
char **  user,
char **  domain 
)

Split a user string as user and domain.

Parameters
inthe input string
userthe string to put the user into
domainthe string to put the domain into
Returns
1 if successfull
Note
Extended formats protocol:user:port (Example: sip:toto@.nosp@m.exam.nosp@m.ple.o.nosp@m.rg)

Definition at line 4216 of file switch_utils.c.

References in.

4217 {
4218  char *p = NULL, *h = NULL, *u = NULL;
4219 
4220  if (!in) return 0;
4221 
4222  /* Remove URL scheme */
4223  if (!strncasecmp(in, "sip:", 4)) in += 4;
4224  else if (!strncasecmp(in, "sips:", 5)) in += 5;
4225 
4226  /* Isolate the host part from the user part */
4227  if ((h = in, p = strchr(h, '@'))) *p = '\0', u = in, h = p+1;
4228 
4229  /* Clean out the host part of any suffix */
4230  for (p = h; *p; p++)
4231  if (*p == ':' || *p == ';' || *p == ' ') {
4232  *p = '\0'; break;
4233  }
4234 
4235  if (user) *user = u;
4236  if (domain) *domain = h;
4237  return 1;
4238 }
switch_byte_t in

◆ switch_str_time()

switch_time_t switch_str_time ( const char *  in)

Converts a string representation of a date into a switch_time_t.

Parameters
inthe string
Returns
the epoch time in usec

Definition at line 2083 of file switch_utils.c.

References switch_micro_time_now(), switch_regex_copy_substring(), switch_regex_perform(), switch_regex_safe_free, switch_time_exp_get(), switch_time_exp_gmt_get(), switch_time_exp_lt(), switch_time_exp_t::tm_gmtoff, switch_time_exp_t::tm_hour, switch_time_exp_t::tm_isdst, switch_time_exp_t::tm_mday, switch_time_exp_t::tm_min, switch_time_exp_t::tm_mon, switch_time_exp_t::tm_sec, switch_time_exp_t::tm_usec, and switch_time_exp_t::tm_year.

Referenced by switch_fulldate_cmp().

2084 {
2085  switch_time_exp_t tm = { 0 }, local_tm = { 0 };
2086  int proceed = 0, ovector[30], time_only = 0;
2087  switch_regex_t *re = NULL;
2088  char replace[1024] = "";
2089  switch_time_t ret = 0, local_time = 0;
2090  char *pattern = "^(\\d+)-(\\d+)-(\\d+)\\s*(\\d*):{0,1}(\\d*):{0,1}(\\d*)";
2091  char *pattern2 = "^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
2092  char *pattern3 = "^(\\d*):{0,1}(\\d*):{0,1}(\\d*)$";
2093 
2095 
2096 
2097  if ((time_only = switch_regex_perform(in, pattern3, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
2098  tm.tm_hour = 0;
2099  tm.tm_min = 0;
2100  tm.tm_sec = 0;
2101  } else {
2102  tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = tm.tm_usec = 0;
2103 
2104  if (!(proceed = switch_regex_perform(in, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
2106  proceed = switch_regex_perform(in, pattern2, &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
2107  }
2108  }
2109 
2110  if (proceed || time_only) {
2111 
2112  if (time_only > 1) {
2113  switch_regex_copy_substring(in, ovector, time_only, 1, replace, sizeof(replace));
2114  tm.tm_hour = atoi(replace);
2115  }
2116 
2117  if (time_only > 2) {
2118  switch_regex_copy_substring(in, ovector, time_only, 2, replace, sizeof(replace));
2119  tm.tm_min = atoi(replace);
2120  }
2121 
2122  if (time_only > 3) {
2123  switch_regex_copy_substring(in, ovector, time_only, 3, replace, sizeof(replace));
2124  tm.tm_sec = atoi(replace);
2125  }
2126 
2127  if (proceed > 1) {
2128  switch_regex_copy_substring(in, ovector, proceed, 1, replace, sizeof(replace));
2129  tm.tm_year = atoi(replace) - 1900;
2130  }
2131 
2132  if (proceed > 2) {
2133  switch_regex_copy_substring(in, ovector, proceed, 2, replace, sizeof(replace));
2134  tm.tm_mon = atoi(replace) - 1;
2135  }
2136 
2137  if (proceed > 3) {
2138  switch_regex_copy_substring(in, ovector, proceed, 3, replace, sizeof(replace));
2139  tm.tm_mday = atoi(replace);
2140  }
2141 
2142  if (proceed > 4) {
2143  switch_regex_copy_substring(in, ovector, proceed, 4, replace, sizeof(replace));
2144  tm.tm_hour = atoi(replace);
2145  }
2146 
2147  if (proceed > 5) {
2148  switch_regex_copy_substring(in, ovector, proceed, 5, replace, sizeof(replace));
2149  tm.tm_min = atoi(replace);
2150  }
2151 
2152  if (proceed > 6) {
2153  switch_regex_copy_substring(in, ovector, proceed, 6, replace, sizeof(replace));
2154  tm.tm_sec = atoi(replace);
2155  }
2156 
2158 
2159  switch_time_exp_get(&local_time, &tm);
2160  switch_time_exp_lt(&local_tm, local_time);
2161  tm.tm_isdst = local_tm.tm_isdst;
2162  tm.tm_gmtoff = local_tm.tm_gmtoff;
2163 
2164  switch_time_exp_gmt_get(&ret, &tm);
2165  return ret;
2166  }
2167 
2169 
2170  return ret;
2171 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_regex_safe_free(re)
Definition: switch_regex.h:79
switch_status_t switch_time_exp_get(switch_time_t *result, switch_time_exp_t *input)
Definition: switch_apr.c:341
int switch_regex_copy_substring(const char *subject, int *ovector, int stringcount, int stringnumber, char *buffer, int size)
Definition: switch_regex.c:44
switch_status_t switch_time_exp_lt(switch_time_exp_t *result, switch_time_t input)
Definition: switch_apr.c:346
struct real_pcre switch_regex_t
Definition: switch_regex.h:43
int64_t switch_time_t
Definition: switch_apr.h:188
switch_byte_t in
switch_status_t switch_time_exp_gmt_get(switch_time_t *result, switch_time_exp_t *input)
Definition: switch_apr.c:336
int switch_regex_perform(const char *field, const char *expression, switch_regex_t **new_re, int *ovector, uint32_t olen)
Definition: switch_regex.c:55

◆ switch_strcasecmp_any()

int switch_strcasecmp_any ( const char *  str,
  ... 
)

Definition at line 394 of file switch_utils.c.

395 {
396  va_list ap;
397  const char *next_str = 0;
398  int r = 0;
399 
400  va_start(ap, str);
401 
402  while ((next_str = va_arg(ap, const char *))) {
403  if (!strcasecmp(str, next_str)) {
404  r = 1;
405  break;
406  }
407  }
408 
409  va_end(ap);
410 
411  return r;
412 }

◆ switch_strchr_strict()

static char* switch_strchr_strict ( const char *  in,
char  find,
const char *  allowed 
)
inlinestatic

Definition at line 334 of file switch_utils.h.

References in, and switch_assert.

Referenced by switch_event_create_brackets().

335 {
336  const char *p;
337 
338  switch_assert(in);
339 
340  p = in;
341 
342  if (!*p) return NULL;
343 
344  while(p && *p) {
345  const char *a = allowed;
346  int acceptable = 0;
347 
348  if (*p == find) break;
349 
350  if (!a) {
351  acceptable = 1;
352  } else {
353 
354  while(a && *a) {
355 
356  if (*p == *a) {
357  acceptable = 1;
358  break;
359  }
360 
361  a++;
362  }
363 
364  }
365 
366  if (!acceptable) return NULL;
367 
368  p++;
369  }
370 
371  return (char *) p;
372 }
switch_byte_t in
#define switch_assert(expr)

◆ switch_strerror_r()

char* switch_strerror_r ( int  errnum,
char *  buf,
switch_size_t  buflen 
)

Portable version of strerror_r(), work around for the incompatible return type of GNU and XSI variants.

Parameters
[in]errnumError number

Definition at line 4308 of file switch_utils.c.

References buf, switch_copy_string(), and switch_snprintf().

Referenced by reincarnate_protect(), and rtp_common_read().

4309 {
4310 #ifdef HAVE_STRERROR_R
4311 #ifdef STRERROR_R_CHAR_P
4312  /* GNU variant returning char *, avoids warn-unused-result error */
4313  return strerror_r(errnum, buf, buflen);
4314 #else
4315  /*
4316  * XSI variant returning int, with GNU compatible error string,
4317  * if no message could be found
4318  */
4319  if (strerror_r(errnum, buf, buflen)) {
4320  switch_snprintf(buf, buflen, "Unknown error %d", errnum);
4321  }
4322  return buf;
4323 #endif /* STRERROR_R_CHAR_P */
4324 #elif defined(WIN32)
4325  /* WIN32 variant */
4326  if (strerror_s(buf, buflen, errnum)) {
4327  switch_snprintf(buf, buflen, "Unknown error %d", errnum);
4328  }
4329  return buf;
4330 #else
4331  /* Fallback, copy string into private buffer */
4332  switch_copy_string(buf, strerror(errnum), buflen);
4333  return buf;
4334 #endif
4335 }
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_byte_t switch_byte_t * buf
switch_byte_t switch_byte_t uint32_t buflen
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)

◆ switch_string_has_escaped_data()

static int switch_string_has_escaped_data ( const char *  in)
inlinestatic

Definition at line 384 of file switch_utils.h.

References buf, in, switch_amp_encode(), switch_assert, switch_b64_decode(), switch_b64_encode(), and SWITCH_DECLARE.

Referenced by switch_channel_expand_variables_check(), and switch_event_expand_headers_check().

385 {
386  const char *i;
387 
388  switch_assert(in);
389 
390  i = strchr(in, '\\');
391 
392  while (i && *i == '\\') {
393  i++;
394  if (*i == '\\' || *i == 'n' || *i == 's' || *i == 't' || *i == '\'') {
395  return 1;
396  }
397  i = strchr(i, '\\');
398  }
399 
400  return 0;
401 }
switch_byte_t in
#define switch_assert(expr)

◆ switch_string_match()

switch_status_t switch_string_match ( const char *  string,
size_t  string_len,
const char *  search,
size_t  search_len 
)

Definition at line 3024 of file switch_utils.c.

References SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_string_replace().

3025 {
3026  size_t i;
3027 
3028  for (i = 0; (i < search_len) && (i < string_len); i++) {
3029  if (string[i] != search[i]) {
3030  return SWITCH_STATUS_FALSE;
3031  }
3032  }
3033 
3034  if (i == search_len) {
3035  return SWITCH_STATUS_SUCCESS;
3036  }
3037 
3038  return SWITCH_STATUS_FALSE;
3039 }

◆ switch_string_replace()

char* switch_string_replace ( const char *  string,
const char *  search,
const char *  replace 
)

Definition at line 3041 of file switch_utils.c.

References switch_assert, SWITCH_STATUS_SUCCESS, and switch_string_match().

Referenced by switch_channel_set_timestamps(), switch_core_set_globals(), and switch_core_sqldb_start().

3042 {
3043  size_t string_len = strlen(string);
3044  size_t search_len = strlen(search);
3045  size_t replace_len = strlen(replace);
3046  size_t i, n;
3047  size_t dest_len = 0;
3048  char *dest, *tmp;
3049 
3050  dest = (char *) malloc(sizeof(char));
3051  switch_assert(dest);
3052 
3053  for (i = 0; i < string_len; i++) {
3054  if (switch_string_match(string + i, string_len - i, search, search_len) == SWITCH_STATUS_SUCCESS) {
3055  for (n = 0; n < replace_len; n++) {
3056  dest[dest_len] = replace[n];
3057  dest_len++;
3058  tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
3059  switch_assert(tmp);
3060  dest = tmp;
3061  }
3062  i += search_len - 1;
3063  } else {
3064  dest[dest_len] = string[i];
3065  dest_len++;
3066  tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
3067  switch_assert(tmp);
3068  dest = tmp;
3069  }
3070  }
3071 
3072  dest[dest_len] = 0;
3073  return dest;
3074 }
switch_status_t switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len)
#define switch_assert(expr)

◆ switch_string_var_check()

static switch_bool_t switch_string_var_check ( char *  s,
switch_bool_t  disable 
)
inlinestatic

Definition at line 763 of file switch_utils.h.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_core_set_var_conditional(), switch_core_set_variable(), and switch_var_clean_string().

764 {
765  char *p;
766  char *dol = NULL;
767 
768  for (p = s; p && *p; p++) {
769  if (*p == '$') {
770  dol = p;
771  } else if (dol) {
772  if (*p == '{') {
773  if (disable) {
774  *dol = '%';
775  dol = NULL;
776  } else {
777  return SWITCH_TRUE;
778  }
779  } else if (*p != '\\') {
780  dol = NULL;
781  }
782  }
783  }
784  return SWITCH_FALSE;
785 }

◆ switch_string_var_check_const()

static switch_bool_t switch_string_var_check_const ( const char *  s)
inlinestatic

Definition at line 788 of file switch_utils.h.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_channel_add_variable_var_check(), switch_channel_expand_variables_check(), switch_channel_set_variable_strip_quotes_var_check(), switch_channel_set_variable_var_check(), and switch_event_expand_headers_check().

789 {
790  const char *p;
791  int dol = 0;
792 
793  for (p = s; p && *p; p++) {
794  if (*p == '$') {
795  dol = 1;
796  } else if (dol) {
797  if (*p == '{') {
798  return SWITCH_TRUE;
799  } else if (*p != '\\') {
800  dol = 0;
801  }
802  }
803  }
804  return SWITCH_FALSE;
805 }

◆ switch_strip_commas()

char* switch_strip_commas ( char *  in,
char *  out,
switch_size_t  len 
)

Definition at line 1539 of file switch_utils.c.

References in.

1540 {
1541  char *p = in, *q = out;
1542  char *ret = out;
1543  switch_size_t x = 0;
1544 
1545  for (; p && *p; p++) {
1546  if ((*p > 47 && *p < 58)) {
1547  *q++ = *p;
1548 
1549  if (++x > len) {
1550  ret = NULL;
1551  break;
1552  }
1553  } else if (*p != ',') {
1554  ret = NULL;
1555  break;
1556  }
1557  }
1558 
1559  return ret;
1560 }
switch_byte_t in
uintptr_t switch_size_t

◆ switch_strip_nonnumerics()

char* switch_strip_nonnumerics ( char *  in,
char *  out,
switch_size_t  len 
)

Definition at line 1562 of file switch_utils.c.

References in.

1563 {
1564  char *p = in, *q = out;
1565  char *ret = out;
1566  switch_size_t x = 0;
1567  /* valid are 0 - 9, period (.), minus (-), and plus (+) - remove all others */
1568  for (; p && *p; p++) {
1569  if ((*p > 47 && *p < 58) || *p == '.' || *p == '-' || *p == '+') {
1570  *q++ = *p;
1571 
1572  if (++x > len) {
1573  ret = NULL;
1574  break;
1575  }
1576  }
1577  }
1578 
1579  return ret;
1580 }
switch_byte_t in
uintptr_t switch_size_t

◆ switch_strip_spaces()

char* switch_strip_spaces ( char *  str,
switch_bool_t  dup 
)

Definition at line 1503 of file switch_utils.c.

References switch_network_node::str, switch_assert, SWITCH_BLANK_STRING, and zstr.

Referenced by switch_core_media_add_crypto().

1504 {
1505  char *sp = str;
1506  char *p, *s = NULL;
1507  size_t len;
1508 
1509  if (zstr(sp)) {
1510  return dup ? strdup(SWITCH_BLANK_STRING) : sp;
1511  }
1512 
1513  while (*sp == ' ') {
1514  sp++;
1515  }
1516 
1517  if (dup) {
1518  s = strdup(sp);
1519  switch_assert(s);
1520  } else {
1521  s = sp;
1522  }
1523 
1524  if (zstr(s)) {
1525  return s;
1526  }
1527 
1528  if ((len = strlen(s)) > 0) {
1529  p = s + (len - 1);
1530 
1531  while (p && *p && (p >= s) && *p == ' ') {
1532  *p-- = '\0';
1533  }
1534  }
1535 
1536  return s;
1537 }
#define zstr(x)
Definition: switch_utils.h:314
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
#define switch_assert(expr)

◆ switch_strip_whitespace()

char* switch_strip_whitespace ( const char *  str)

Definition at line 1471 of file switch_utils.c.

References switch_network_node::str, switch_assert, SWITCH_BLANK_STRING, and zstr.

Referenced by switch_api_execute().

1472 {
1473  const char *sp = str;
1474  char *p, *s = NULL;
1475  size_t len;
1476 
1477  if (zstr(sp)) {
1478  return strdup(SWITCH_BLANK_STRING);
1479  }
1480 
1481  while ((*sp == 13 ) || (*sp == 10 ) || (*sp == 9 ) || (*sp == 32) || (*sp == 11) ) {
1482  sp++;
1483  }
1484 
1485  if (zstr(sp)) {
1486  return strdup(SWITCH_BLANK_STRING);
1487  }
1488 
1489  s = strdup(sp);
1490  switch_assert(s);
1491 
1492  if ((len = strlen(s)) > 0) {
1493  p = s + (len - 1);
1494 
1495  while ((p >= s) && ((*p == 13 ) || (*p == 10 ) || (*p == 9 ) || (*p == 32) || (*p == 11))) {
1496  *p-- = '\0';
1497  }
1498  }
1499 
1500  return s;
1501 }
#define zstr(x)
Definition: switch_utils.h:314
#define SWITCH_BLANK_STRING
Definition: switch_types.h:48
#define switch_assert(expr)

◆ switch_stristr()

const char* switch_stristr ( const char *  instr,
const char *  str 
)

Definition at line 1673 of file switch_utils.c.

References get_netmask(), switch_network_node::mask, memset(), and switch_toupper().

Referenced by audio_bridge_thread(), check_ice(), core_event_handler(), do_chat_send(), find_pt(), inherit_codec(), init_upnp(), main(), msrp_parse_buffer(), msrp_worker(), preprocess(), speech_thread(), switch_cache_db_execute_sql_real(), switch_core_gen_certs(), switch_core_gen_encoded_silence(), switch_core_media_check_outgoing_proxy(), switch_core_media_media_params(), switch_core_media_negotiate_sdp(), switch_core_media_parse_rtp_bugs(), switch_core_media_patch_sdp(), switch_core_media_proxy_remote_addr(), switch_core_media_toggle_hold(), switch_core_perform_file_open(), switch_core_session_check_incoming_crypto(), switch_core_session_findall_matching_var(), switch_core_sqldb_start(), switch_core_video_parse_filter_string(), switch_ivr_originate(), switch_odbc_handle_exec(), and switch_simple_email().

1674 {
1675 /*
1676 ** Rev History: 16/07/97 Greg Thayer Optimized
1677 ** 07/04/95 Bob Stout ANSI-fy
1678 ** 02/03/94 Fred Cole Original
1679 ** 09/01/03 Bob Stout Bug fix (lines 40-41) per Fred Bulback
1680 **
1681 ** Hereby donated to public domain.
1682 */
1683  const char *pptr, *sptr, *start;
1684 
1685  if (!str || !instr)
1686  return NULL;
1687 
1688  for (start = str; *start; start++) {
1689  /* find start of pattern in string */
1690  for (; ((*start) && (switch_toupper(*start) != switch_toupper(*instr))); start++);
1691 
1692  if (!*start)
1693  return NULL;
1694 
1695  pptr = instr;
1696  sptr = start;
1697 
1698  while (switch_toupper(*sptr) == switch_toupper(*pptr)) {
1699  sptr++;
1700  pptr++;
1701 
1702  /* if end of pattern then pattern was found */
1703  if (!*pptr)
1704  return (start);
1705 
1706  if (!*sptr)
1707  return NULL;
1708  }
1709  }
1710  return NULL;
1711 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:101

◆ switch_strstr()

static switch_bool_t switch_strstr ( char *  s,
char *  q 
)
inlinestatic

Test if one string is inside another with extra case checking.

Parameters
sthe inner string
qthe big string
Returns
SWITCH_TRUE or SWITCH_FALSE

Definition at line 937 of file switch_utils.h.

References switch_assert, SWITCH_FALSE, switch_safe_free, switch_toupper(), and SWITCH_TRUE.

938 {
939  char *p, *S = NULL, *Q = NULL;
941 
942  if (!s || !q) {
943  return SWITCH_FALSE;
944  }
945 
946  if (strstr(s, q)) {
947  return SWITCH_TRUE;
948  }
949 
950  S = strdup(s);
951 
952  switch_assert(S != NULL);
953 
954  for (p = S; p && *p; p++) {
955  *p = (char) switch_toupper(*p);
956  }
957 
958  if (strstr(S, q)) {
959  tf = SWITCH_TRUE;
960  goto done;
961  }
962 
963  Q = strdup(q);
964  switch_assert(Q != NULL);
965 
966  for (p = Q; p && *p; p++) {
967  *p = (char) switch_toupper(*p);
968  }
969 
970  if (strstr(s, Q)) {
971  tf = SWITCH_TRUE;
972  goto done;
973  }
974 
975  if (strstr(S, Q)) {
976  tf = SWITCH_TRUE;
977  goto done;
978  }
979 
980  done:
981  switch_safe_free(S);
982  switch_safe_free(Q);
983 
984  return tf;
985 }
switch_bool_t
Definition: switch_types.h:437
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:101
#define switch_assert(expr)

◆ switch_testv6_subnet()

switch_bool_t switch_testv6_subnet ( ip_t  _ip,
ip_t  _net,
ip_t  _mask 
)

Definition at line 483 of file switch_utils.c.

References IN6_AND_MASK, SWITCH_TRUE, and ip_t::v6.

Referenced by switch_check_network_list_ip_port_token(), and switch_network_list_validate_ip6_port_token().

483  {
484  if (!IN6_IS_ADDR_UNSPECIFIED(&_mask.v6)) {
485  struct in6_addr a, b;
486  IN6_AND_MASK(&a, &_net, &_mask);
487  IN6_AND_MASK(&b, &_ip, &_mask);
488  return !memcmp(&a,&b, sizeof(struct in6_addr));
489  } else {
490  if (!IN6_IS_ADDR_UNSPECIFIED(&_net.v6)) {
491  return !memcmp(&_net,&_ip,sizeof(struct in6_addr));
492  }
493  else return SWITCH_TRUE;
494  }
495 }
#define IN6_AND_MASK(result, ip, mask)
Definition: switch_utils.c:478
const cJSON *const b
Definition: switch_cJSON.h:243
struct in6_addr v6
Definition: switch_utils.h:279

◆ switch_tod_cmp()

int switch_tod_cmp ( const char *  exp,
int  val 
)

Definition at line 4152 of file switch_utils.c.

References cur, switch_assert, and switch_safe_free.

Referenced by switch_xml_std_datetime_check().

4153 {
4154  char *dup = strdup(exp);
4155  char *minh;
4156  char *minm;
4157  char *mins;
4158  char *maxh;
4159  char *maxm;
4160  char *maxs;
4161  char *cur;
4162  char *p;
4163  int range_start, range_end;
4164 
4165  switch_assert(dup);
4166 
4167  cur = dup;
4168  if ((p = strchr(cur, ','))) {
4169  *p++ = '\0';
4170  }
4171 
4172  while (cur) {
4173  minh = cur;
4174  if ((minm=strchr(cur, ':'))) {
4175  *minm++ = '\0';
4176  if ((maxh=strchr(minm, '-'))) {
4177  if ((maxm=strchr(maxh, ':'))) {
4178  *maxh++ = '\0';
4179  *maxm++ = '\0';
4180  /* Check if min/max seconds are present */
4181  if ((mins=strchr(minm, ':'))) {
4182  *mins++ = '\0';
4183  } else {
4184  mins = "00";
4185  }
4186  if ((maxs=strchr(maxm, ':'))) {
4187  *maxs++ = '\0';
4188  } else {
4189  maxs = "00";
4190  }
4191 
4192  range_start = (atol(minh) * 60 * 60) + (atol(minm) * 60) + atol(mins);
4193  range_end = (atol(maxh) * 60 * 60) + (atol(maxm) * 60) + atol(maxs);
4194  if (range_start <= range_end ? (val >= range_start && val <= range_end) : (val >= range_start || val <= range_end)) {
4195  switch_safe_free(dup);
4196  return 1;
4197  }
4198  }
4199  }
4200  }
4201 
4202  cur = p;
4203  if (p) {
4204  if ((p = strchr(p, ','))) {
4205  *p++ = '\0';
4206  }
4207  }
4208 
4209  }
4210 
4211  switch_safe_free(dup);
4212  return 0;
4213 
4214 }
pack cur
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
#define switch_assert(expr)

◆ switch_tolower()

static uint32_t switch_tolower ( uint32_t  eax)
inlinestatic

Definition at line 112 of file switch_utils.h.

References b, switch_tolower_max(), switch_toupper(), and switch_toupper_max().

Referenced by switch_hash_default_ci(), and switch_tolower_max().

113 {
114  uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul;
115  ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
116  ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
117  return eax + ebx;
118 }

◆ switch_tolower_max()

static void switch_tolower_max ( char *  s)
inlinestatic

Definition at line 231 of file switch_utils.h.

References b, old_switch_tolower(), old_switch_toupper(), SWITCH_DECLARE, switch_isalnum(), switch_isalpha(), switch_iscntrl(), switch_isdigit(), switch_isgraph(), switch_islower(), switch_isprint(), switch_ispunct(), switch_isspace(), switch_isupper(), switch_isxdigit(), and switch_tolower().

Referenced by switch_lc_strdup(), and switch_tolower().

232 {
233  uint32_t *b,*p;
234  char *c;
235  size_t l;
236 
237  l = strlen(s);
238 
239  p = (uint32_t *) s;
240 
241  while (l > 4) {
242  b = p;
243  *b = (uint32_t) switch_tolower(*b);
244  b++;
245  p++;
246  l -= 4;
247  }
248 
249  c = (char *)p;
250 
251  while(l > 0) {
252  *c = (char) switch_tolower(*c);
253  c++;
254  l--;
255  }
256 
257 }
const cJSON *const b
Definition: switch_cJSON.h:243
static uint32_t switch_tolower(uint32_t eax)
Definition: switch_utils.h:112

◆ switch_toupper()

static uint32_t switch_toupper ( uint32_t  eax)
inlinestatic

Definition at line 101 of file switch_utils.h.

Referenced by switch_char_to_rfc2833(), switch_stristr(), switch_strstr(), switch_tolower(), and switch_toupper_max().

102 {
103 uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
104 ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
105  ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
106  return eax - ebx;
107 }

◆ switch_toupper_max()

static void switch_toupper_max ( char *  s)
inlinestatic

Definition at line 203 of file switch_utils.h.

References b, and switch_toupper().

Referenced by switch_tolower(), and switch_uc_strdup().

204 {
205  uint32_t *b,*p;
206  char *c;
207  size_t l;
208 
209  l = strlen(s);
210 
211  p = (uint32_t *) s;
212 
213  while (l > 4) {
214  b = p;
215  *b = (uint32_t) switch_toupper(*b);
216  b++;
217  p++;
218  l -= 4;
219  }
220 
221  c = (char *)p;
222 
223  while(l > 0) {
224  *c = (char) switch_toupper(*c);
225  c++;
226  l--;
227  }
228 
229 }
const cJSON *const b
Definition: switch_cJSON.h:243
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:101

◆ switch_true()

static switch_bool_t switch_true ( const char *  expr)
inlinestatic

Evaluate the truthfullness of a string expression.

Parameters
expra string expression
Returns
true or false

Definition at line 519 of file switch_utils.h.

References SWITCH_FALSE, switch_is_number(), and SWITCH_TRUE.

Referenced by audio_bridge_on_exchange_media(), audio_bridge_thread(), check_ice(), core_event_handler(), do_chat_send(), get_channels(), inherit_codec(), limit_state_handler(), load_config(), load_configuration(), record_callback(), recording_var_true(), signal_bridge_on_hangup(), speech_thread(), switch_channel_flip_cid(), switch_channel_handle_cause(), switch_channel_mark_hold(), switch_channel_perform_mark_answered(), switch_channel_perform_mark_pre_answered(), switch_channel_perform_presence(), switch_channel_queue_dtmf(), switch_channel_var_true(), switch_core_media_activate_rtp(), switch_core_media_bug_add(), switch_core_media_build_crypto(), switch_core_media_check_autoadj(), switch_core_media_gen_local_sdp(), switch_core_media_negotiate_sdp(), switch_core_media_parse_media_flags(), switch_core_media_prepare_codecs(), switch_core_media_proxy_remote_addr(), switch_core_media_receive_message(), switch_core_media_set_r_sdp_codec_string(), switch_core_media_set_udptl_image_sdp(), switch_core_media_start_udptl(), switch_core_media_toggle_hold(), switch_core_perform_file_open(), switch_core_session_enable_heartbeat(), switch_core_session_exec(), switch_core_session_hangup_state(), switch_core_session_outgoing_channel(), switch_core_session_parse_codec_settings(), switch_core_session_parse_crypto_prefs(), switch_core_session_perform_receive_message(), switch_core_session_reporting_state(), switch_core_session_sched_heartbeat(), switch_core_session_thread(), switch_core_standard_on_hangup(), switch_event_channel_permission_verify(), switch_ivr_detect_speech(), switch_ivr_detect_speech_init(), switch_ivr_displace_session(), switch_ivr_eavesdrop_session(), switch_ivr_enterprise_orig_and_bridge(), switch_ivr_intercept_session(), switch_ivr_multi_threaded_bridge(), switch_ivr_orig_and_bridge(), switch_ivr_originate(), switch_ivr_parse_event(), switch_ivr_phrase_macro_event(), switch_ivr_play_file(), switch_ivr_preprocess_session(), switch_ivr_record_file_event(), switch_ivr_record_session_event(), switch_ivr_say(), switch_ivr_say_string(), switch_ivr_set_xml_call_stats(), switch_ivr_speak_text(), switch_load_core_config(), switch_load_network_lists(), switch_loadable_module_init(), switch_rtp_activate_jitter_buffer(), switch_rtp_add_crypto_key(), switch_rtp_enable_vad(), switch_speex_fmtp_parse(), switch_stream_system(), switch_system(), switch_true_byte(), switch_xml_config_parse_event(), switch_xml_std_datetime_check(), text_callback(), and uuid_bridge_on_soft_execute().

520 {
521  return ((expr && ( !strcasecmp(expr, "yes") ||
522  !strcasecmp(expr, "on") ||
523  !strcasecmp(expr, "true") ||
524  !strcasecmp(expr, "t") ||
525  !strcasecmp(expr, "enabled") ||
526  !strcasecmp(expr, "active") ||
527  !strcasecmp(expr, "allow") ||
528  (switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
529 }
switch_bool_t switch_is_number(const char *str)

◆ switch_true_byte()

static switch_byte_t switch_true_byte ( const char *  expr)
inlinestatic

Definition at line 531 of file switch_utils.h.

References switch_true().

532 {
533  return (switch_byte_t)switch_true(expr);
534 }
static switch_bool_t switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:519
uint8_t switch_byte_t
Definition: switch_types.h:256

◆ switch_uc_strdup()

static char* switch_uc_strdup ( const char *  it)
inlinestatic

Definition at line 914 of file switch_utils.h.

References switch_toupper_max().

915 {
916  char *dup;
917 
918  if (it) {
919  dup = strdup(it);
920  switch_toupper_max(dup);
921  return dup;
922  }
923 
924  return NULL;
925 }
static void switch_toupper_max(char *s)
Definition: switch_utils.h:203

◆ switch_url_decode()

char* switch_url_decode ( char *  s)

Definition at line 3654 of file switch_utils.c.

References zstr.

Referenced by switch_core_session_request_xml(), switch_http_parse_qs(), switch_img_write_text_img(), and switch_needs_url_encode().

3655 {
3656  char *o;
3657  unsigned int tmp;
3658 
3659  if (zstr(s) || !strchr(s, '%')) {
3660  return s;
3661  }
3662 
3663  for (o = s; *s; s++, o++) {
3664  if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
3665  *o = (char) tmp;
3666  s += 2;
3667  } else {
3668  *o = *s;
3669  }
3670  }
3671  *o = '\0';
3672  return s;
3673 }
#define zstr(x)
Definition: switch_utils.h:314

◆ switch_url_encode()

char* switch_url_encode ( const char *  url,
char *  buf,
size_t  len 
)

Definition at line 3649 of file switch_utils.c.

References SWITCH_FALSE, and switch_url_encode_opt().

Referenced by add_xml_header(), switch_channel_build_param_string(), switch_core_perform_file_open(), switch_event_build_param_string(), switch_event_serialize(), switch_ivr_set_json_chan_vars(), switch_ivr_set_xml_chan_var(), and switch_needs_url_encode().

3650 {
3651  return switch_url_encode_opt(url, buf, len, SWITCH_FALSE);
3652 }
char * switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode)
switch_byte_t switch_byte_t * buf

◆ switch_url_encode_opt()

char* switch_url_encode_opt ( const char *  url,
char *  buf,
size_t  len,
switch_bool_t  double_encode 
)

Definition at line 3604 of file switch_utils.c.

References buf, end_of_p, switch_network_node::ok, and SWITCH_URL_UNSAFE.

Referenced by switch_core_url_encode_opt(), switch_needs_url_encode(), and switch_url_encode().

3605 {
3606  const char *p, *e = end_of_p(url);
3607  size_t x = 0;
3608  const char hex[] = "0123456789ABCDEF";
3609 
3610  if (!buf) {
3611  return 0;
3612  }
3613 
3614  if (!url) {
3615  return 0;
3616  }
3617 
3618  len--;
3619 
3620  for (p = url; *p; p++) {
3621  int ok = 0;
3622 
3623  if (x >= len) {
3624  break;
3625  }
3626 
3627  if (!double_encode && *p == '%' && e-p > 1) {
3628  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
3629  ok = 1;
3630  }
3631  }
3632 
3633  if (!ok && (*p < ' ' || *p > '~' || strchr(SWITCH_URL_UNSAFE, *p))) {
3634  if ((x + 3) > len) {
3635  break;
3636  }
3637  buf[x++] = '%';
3638  buf[x++] = hex[(*p >> 4) & 0x0f];
3639  buf[x++] = hex[*p & 0x0f];
3640  } else {
3641  buf[x++] = *p;
3642  }
3643  }
3644  buf[x] = '\0';
3645 
3646  return buf;
3647 }
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
#define end_of_p(_s)
Definition: switch_utils.h:686
switch_byte_t switch_byte_t * buf

◆ switch_util_quote_shell_arg()

char* switch_util_quote_shell_arg ( const char *  string)

Quote shell argument.

Parameters
stringthe string to quote (example: a ' b"' c) \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), should be freed

Definition at line 3076 of file switch_utils.c.

References switch_util_quote_shell_arg_pool().

Referenced by switch_simple_email().

3077 {
3078  return switch_util_quote_shell_arg_pool(string, NULL);
3079 }
char * switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool)
Quote shell argument, allocating from pool if provided.

◆ switch_util_quote_shell_arg_pool()

char* switch_util_quote_shell_arg_pool ( const char *  string,
switch_memory_pool_t pool 
)

Quote shell argument, allocating from pool if provided.

Parameters
stringthe string to quote (example: a ' b"' c)
poola memory pool to use
Returns
the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), if pool not provided, returned value should be freed

Definition at line 3081 of file switch_utils.c.

References switch_waitlist_s::revents, switch_waitlist_s::sock, switch_assert, switch_core_alloc, SWITCH_DECLARE, switch_errno, switch_errno_is_break(), SWITCH_POLL_ERROR, SWITCH_POLL_HUP, SWITCH_POLL_INVALID, SWITCH_POLL_PRI, SWITCH_POLL_RDBAND, SWITCH_POLL_RDNORM, SWITCH_POLL_READ, SWITCH_POLL_WRITE, SWITCH_SOCK_INVALID, switch_wait_sock(), and switch_wait_socklist().

Referenced by switch_util_quote_shell_arg().

3082 {
3083  size_t string_len = strlen(string);
3084  size_t i;
3085  size_t n = 0;
3086  size_t dest_len = 0;
3087  char *dest;
3088 
3089  /* first pass through, figure out how large to make the allocation */
3090  dest_len = strlen(string) + 1; /* string + null */
3091  dest_len += 1; /* opening quote */
3092  for (i = 0; i < string_len; i++) {
3093  switch (string[i]) {
3094 #ifndef WIN32
3095  case '\'':
3096  /* We replace ' by sq backslace sq sq, so need 3 additional bytes */
3097  dest_len += 3;
3098  break;
3099 #endif
3100  }
3101  }
3102  dest_len += 1; /* closing quote */
3103 
3104  /* if we're given a pool, allocate from it, otherwise use malloc */
3105  if (pool) {
3106  dest = switch_core_alloc(pool, sizeof(char) * dest_len);
3107  } else {
3108  dest = (char *) malloc(sizeof(char) * dest_len);
3109  }
3110  switch_assert(dest);
3111 
3112 #ifdef WIN32
3113  dest[n++] = '"';
3114 #else
3115  dest[n++] = '\'';
3116 #endif
3117 
3118  for (i = 0; i < string_len; i++) {
3119  switch (string[i]) {
3120 #ifdef WIN32
3121  case '"':
3122  case '%':
3123  dest[n++] = ' ';
3124  break;
3125 #else
3126  case '\'':
3127  /* We replace ' by sq backslash sq sq */
3128  dest[n++] = '\'';
3129  dest[n++] = '\\';
3130  dest[n++] = '\'';
3131  dest[n++] = '\'';
3132  break;
3133 #endif
3134  default:
3135  dest[n++] = string[i];
3136  }
3137  }
3138 
3139 #ifdef WIN32
3140  dest[n++] = '"';
3141 #else
3142  dest[n++] = '\'';
3143 #endif
3144  dest[n++] = 0;
3145  switch_assert(n == dest_len);
3146  return dest;
3147 }
switch_memory_pool_t * pool
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
#define switch_assert(expr)

◆ switch_uuid_str()

char* switch_uuid_str ( char *  buf,
switch_size_t  len 
)

Definition at line 4241 of file switch_utils.c.

References buf, switch_snprintf(), switch_uuid_format(), SWITCH_UUID_FORMATTED_LENGTH, and switch_uuid_get().

Referenced by switch_caller_profile_dup(), switch_caller_profile_new(), switch_core_session_exec(), and switch_msrp_do_send().

4242 {
4243  switch_uuid_t uuid;
4244 
4245  if (len < (SWITCH_UUID_FORMATTED_LENGTH + 1)) {
4246  switch_snprintf(buf, len, "INVALID");
4247  } else {
4248  switch_uuid_get(&uuid);
4249  switch_uuid_format(buf, &uuid);
4250  }
4251 
4252  return buf;
4253 }
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_byte_t switch_byte_t * buf
void switch_uuid_format(char *buffer, const switch_uuid_t *uuid)
Definition: switch_apr.c:1140
void switch_uuid_get(switch_uuid_t *uuid)
Definition: switch_apr.c:1152
#define SWITCH_UUID_FORMATTED_LENGTH
Definition: switch_apr.h:545

◆ switch_var_clean_string()

static char* switch_var_clean_string ( char *  s)
inlinestatic

Definition at line 807 of file switch_utils.h.

References switch_string_var_check(), and SWITCH_TRUE.

808 {
810  return s;
811 }
static switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
Definition: switch_utils.h:763

◆ switch_wait_sock()

int switch_wait_sock ( switch_os_socket_t  sock,
uint32_t  ms,
switch_poll_t  flags 
)

Definition at line 3333 of file switch_utils.c.

References switch_errno, switch_errno_is_break(), SWITCH_POLL_ERROR, SWITCH_POLL_READ, SWITCH_POLL_WRITE, and SWITCH_SOCK_INVALID.

Referenced by get_pmp_pubaddr(), and switch_util_quote_shell_arg_pool().

3334 {
3335  int s = 0, r = 0;
3336  fd_set *rfds;
3337  fd_set *wfds;
3338  fd_set *efds;
3339  struct timeval tv;
3340 
3341  if (sock == SWITCH_SOCK_INVALID) {
3342  return SWITCH_SOCK_INVALID;
3343  }
3344 
3345  rfds = malloc(sizeof(fd_set));
3346  wfds = malloc(sizeof(fd_set));
3347  efds = malloc(sizeof(fd_set));
3348 
3349  FD_ZERO(rfds);
3350  FD_ZERO(wfds);
3351  FD_ZERO(efds);
3352 
3353 #ifndef WIN32
3354  /* Wouldn't you rather know?? */
3355  assert(sock <= FD_SETSIZE);
3356 #endif
3357 
3358  if ((flags & SWITCH_POLL_READ)) {
3359 
3360 #ifdef WIN32
3361 #pragma warning( push )
3362 #pragma warning( disable : 4127 )
3363  FD_SET(sock, rfds);
3364 #pragma warning( pop )
3365 #else
3366  FD_SET(sock, rfds);
3367 #endif
3368  }
3369 
3370  if ((flags & SWITCH_POLL_WRITE)) {
3371 
3372 #ifdef WIN32
3373 #pragma warning( push )
3374 #pragma warning( disable : 4127 )
3375  FD_SET(sock, wfds);
3376 #pragma warning( pop )
3377 #else
3378  FD_SET(sock, wfds);
3379 #endif
3380  }
3381 
3382  if ((flags & SWITCH_POLL_ERROR)) {
3383 
3384 #ifdef WIN32
3385 #pragma warning( push )
3386 #pragma warning( disable : 4127 )
3387  FD_SET(sock, efds);
3388 #pragma warning( pop )
3389 #else
3390  FD_SET(sock, efds);
3391 #endif
3392  }
3393 
3394  tv.tv_sec = ms / 1000;
3395  tv.tv_usec = (ms % 1000) * 1000;
3396 
3397  s = select(sock + 1, (flags & SWITCH_POLL_READ) ? rfds : NULL, (flags & SWITCH_POLL_WRITE) ? wfds : NULL, (flags & SWITCH_POLL_ERROR) ? efds : NULL, &tv);
3398 
3399  if (s < 0) {
3401  s = 0;
3402  }
3403  }
3404 
3405  if (s < 0) {
3406  r = s;
3407  } else if (s > 0) {
3408  if ((flags & SWITCH_POLL_READ) && FD_ISSET(sock, rfds)) {
3409  r |= SWITCH_POLL_READ;
3410  }
3411 
3412  if ((flags & SWITCH_POLL_WRITE) && FD_ISSET(sock, wfds)) {
3413  r |= SWITCH_POLL_WRITE;
3414  }
3415 
3416  if ((flags & SWITCH_POLL_ERROR) && FD_ISSET(sock, efds)) {
3417  r |= SWITCH_POLL_ERROR;
3418  }
3419  }
3420 
3421  free(rfds);
3422  free(wfds);
3423  free(efds);
3424 
3425  return r;
3426 
3427 }
static int switch_errno_is_break(int errcode)
Definition: switch_utils.h:649
#define SWITCH_SOCK_INVALID
#define switch_errno()
Definition: switch_utils.h:647

◆ switch_wait_socklist()

int switch_wait_socklist ( switch_waitlist_t waitlist,
uint32_t  len,
uint32_t  ms 
)

Definition at line 3429 of file switch_utils.c.

References switch_waitlist_s::events, switch_waitlist_s::revents, switch_waitlist_s::sock, switch_errno, switch_errno_is_break(), SWITCH_POLL_ERROR, SWITCH_POLL_READ, SWITCH_POLL_WRITE, and SWITCH_SOCK_INVALID.

Referenced by switch_util_quote_shell_arg_pool().

3430 {
3431  int s = 0, r = 0;
3432  fd_set *rfds;
3433  fd_set *wfds;
3434  fd_set *efds;
3435  struct timeval tv;
3436  unsigned int i;
3437  switch_os_socket_t max_fd = 0;
3438  int flags = 0;
3439 
3440  rfds = malloc(sizeof(fd_set));
3441  wfds = malloc(sizeof(fd_set));
3442  efds = malloc(sizeof(fd_set));
3443 
3444  FD_ZERO(rfds);
3445  FD_ZERO(wfds);
3446  FD_ZERO(efds);
3447 
3448  for (i = 0; i < len; i++) {
3449  if (waitlist[i].sock == SWITCH_SOCK_INVALID) {
3450  break;
3451  }
3452 
3453  if (waitlist[i].sock > max_fd) {
3454  max_fd = waitlist[i].sock;
3455  }
3456 
3457 #ifndef WIN32
3458  /* Wouldn't you rather know?? */
3459  assert(waitlist[i].sock <= FD_SETSIZE);
3460 #endif
3461  flags |= waitlist[i].events;
3462 
3463  if ((waitlist[i].events & SWITCH_POLL_READ)) {
3464 
3465 #ifdef WIN32
3466 #pragma warning( push )
3467 #pragma warning( disable : 4127 )
3468  FD_SET(waitlist[i].sock, rfds);
3469 #pragma warning( pop )
3470 #else
3471  FD_SET(waitlist[i].sock, rfds);
3472 #endif
3473  }
3474 
3475  if ((waitlist[i].events & SWITCH_POLL_WRITE)) {
3476 
3477 #ifdef WIN32
3478 #pragma warning( push )
3479 #pragma warning( disable : 4127 )
3480  FD_SET(waitlist[i].sock, wfds);
3481 #pragma warning( pop )
3482 #else
3483  FD_SET(waitlist[i].sock, wfds);
3484 #endif
3485  }
3486 
3487  if ((waitlist[i].events & SWITCH_POLL_ERROR)) {
3488 
3489 #ifdef WIN32
3490 #pragma warning( push )
3491 #pragma warning( disable : 4127 )
3492  FD_SET(waitlist[i].sock, efds);
3493 #pragma warning( pop )
3494 #else
3495  FD_SET(waitlist[i].sock, efds);
3496 #endif
3497  }
3498  }
3499 
3500  tv.tv_sec = ms / 1000;
3501  tv.tv_usec = (ms % 1000) * 1000;
3502 
3503  s = select(max_fd + 1, (flags & SWITCH_POLL_READ) ? rfds : NULL, (flags & SWITCH_POLL_WRITE) ? wfds : NULL, (flags & SWITCH_POLL_ERROR) ? efds : NULL, &tv);
3504 
3505  if (s < 0) {
3507  s = 0;
3508  }
3509  }
3510 
3511  if (s < 0) {
3512  r = s;
3513  } else if (s > 0) {
3514  for (i = 0; i < len; i++) {
3515  if ((waitlist[i].events & SWITCH_POLL_READ) && FD_ISSET(waitlist[i].sock, rfds)) {
3516  r |= SWITCH_POLL_READ;
3517  waitlist[i].revents |= SWITCH_POLL_READ;
3518  }
3519 
3520  if ((waitlist[i].events & SWITCH_POLL_WRITE) && FD_ISSET(waitlist[i].sock, wfds)) {
3521  r |= SWITCH_POLL_WRITE;
3522  waitlist[i].revents |= SWITCH_POLL_WRITE;
3523  }
3524 
3525  if ((waitlist[i].events & SWITCH_POLL_ERROR) && FD_ISSET(waitlist[i].sock, efds)) {
3526  r |= SWITCH_POLL_ERROR;
3527  waitlist[i].revents |= SWITCH_POLL_ERROR;
3528  }
3529  }
3530  }
3531 
3532  free(rfds);
3533  free(wfds);
3534  free(efds);
3535 
3536  return r;
3537 
3538 }
switch_os_socket_t sock
static int switch_errno_is_break(int errcode)
Definition: switch_utils.h:649
#define SWITCH_SOCK_INVALID
int switch_os_socket_t
#define switch_errno()
Definition: switch_utils.h:647