FreeSWITCH API Documentation  1.7.0
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

union  ip_t
 
struct  switch_http_request_s
 
struct  switch_cputime
 

Macros

#define SWITCH_URL_UNSAFE   "\r\n #%&+:;<=>?@[\\]^`{|}\""
 
#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_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_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)(assert(((ptr) = malloc((len)))),ptr)
 
#define switch_zmalloc(ptr, len)   (void)(assert((ptr = calloc(1, (len)))),ptr)
 
#define switch_strdup(ptr, s)   (void)(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_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 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 (char *in, char *out, switch_size_t olen)
 
char * switch_amp_encode (char *s, char *buf, switch_size_t len)
 
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_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 int 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)
 
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 a number (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)
 
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_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, int quality, double fps)
 
static int32_t switch_parse_bandwidth_string (const char *bwv)
 
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)
 
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 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_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...
 
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)
 
void switch_getcputime (switch_cputime *t)
 

Detailed Description

Compatability and Helper Code.

Just a miscelaneaous set of general utility/helper functions.

Definition in file switch_utils.h.

Macro Definition Documentation

#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 1211 of file switch_utils.h.

Referenced by switch_ivr_uuid_bridge().

#define end_of (   _s)    *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
#define end_of_p (   _s)    (*_s == '\0' ? _s : _s + strlen(_s) - 1)
#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 614 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().

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

Definition at line 341 of file switch_utils.h.

Referenced by switch_dow_int2str(), and switch_dow_str2int().

#define switch_clear_flag (   obj,
  flag 
)    (obj)->flags &= ~(flag)
#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 648 of file switch_utils.h.

Referenced by switch_core_file_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(), and unicast_thread_run().

#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 262 of file switch_utils.h.

Referenced by switch_core_session_outgoing_channel().

#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 663 of file switch_utils.h.

#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)
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)

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 923 of file switch_utils.h.

#define switch_errno ( )    errno

Definition at line 578 of file switch_utils.h.

Referenced by switch_wait_sock(), and switch_wait_socklist().

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

Definition at line 257 of file switch_utils.h.

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

Definition at line 1214 of file switch_utils.h.

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

Definition at line 344 of file switch_utils.h.

Referenced by switch_ivr_record_file(), and switch_ivr_record_session().

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

Definition at line 1205 of file switch_utils.h.

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

Definition at line 1158 of file switch_utils.h.

Referenced by switch_load_network_lists().

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

Definition at line 1164 of file switch_utils.h.

#define switch_normalize_to_16bit (   n)    if (n > SWITCH_SMAX) n = SWITCH_SMAX; else if (n < SWITCH_SMIN) n = SWITCH_SMIN;
#define SWITCH_READ_ACCEPTABLE (   status)    (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE)
#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 789 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(), API::executeString(), fs_encode_cleanup(), CoreSession::getXMLCDR(), hashtable_expand(), load_mime_types(), CoreSession::originate(), parse_presence_data_cols(), play_and_collect(), preprocess(), preprocess_exec_set(), preprocess_glob(), record_callback(), Event::serialize(), CoreSession::set_tts_params(), CoreSession::set_tts_parms(), setup_ringback(), speech_thread(), sql_in_thread(), 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_uninit(), switch_check_network_list_ip_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_load_grammar(), switch_core_destroy(), switch_core_file_close(), switch_core_gen_certs(), switch_core_hash_empty(), switch_core_media_ext_address_lookup(), switch_core_media_gen_local_sdp(), switch_core_media_process_sdp_filter(), switch_core_media_set_r_sdp_codec_string(), 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_reporting_state(), switch_core_session_reset(), switch_core_speech_feed_tts(), switch_core_sqldb_start(), 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(), switch_ivr_set_json_chan_vars(), switch_ivr_sleep(), switch_ivr_speak_text_handle(), switch_ivr_wait_for_answer(), switch_json_add_presence_data_cols(), switch_loadable_module_enumerate_available(), switch_loadable_module_get_codecs(), switch_loadable_module_load_file(), switch_log_node_free(), switch_log_vprintf(), switch_nat_init(), switch_nat_multicast_runtime(), switch_nat_republish(), switch_nat_shutdown(), switch_network_list_add_cidr_token(), switch_network_list_perform_add_cidr_token(), switch_odbc_handle_destroy(), switch_odbc_handle_new(), switch_pgsql_handle_callback_exec_detailed(), switch_pgsql_handle_connect(), switch_pgsql_handle_destroy(), switch_pgsql_handle_disconnect(), switch_pgsql_handle_new(), switch_pgsql_next_result_timed(), switch_pgsql_send_query(), switch_pgsql_SQLEndTran(), 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_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_user_merged(), switch_xml_parse_file(), switch_xml_tohtml(), switch_xml_user_cache(), task_thread_loop(), tone_detect_set_total_time(), and Stream::~Stream().

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

Definition at line 258 of file switch_utils.h.

Referenced by switch_core_speech_open().

#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 631 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_core_asr_close(), switch_core_asr_open(), switch_core_codec_init_with_bitrate(), switch_core_destroy(), switch_core_directory_open(), 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_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_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_record_session(), 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().

#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);
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285

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 638 of file switch_utils.h.

Referenced by 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().

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

Definition at line 259 of file switch_utils.h.

Referenced by switch_core_media_bug_read().

#define SWITCH_SMIN   -32768

Definition at line 260 of file switch_utils.h.

Referenced by switch_core_media_bug_read().

#define switch_split (   _data,
  _delim,
  _array 
)    switch_separate_string(_data, _delim, _array, switch_arraylen(_array))
#define SWITCH_STATUS_IS_BREAK (   x)    (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)
#define switch_str_nil (   s)    (s ? s : "")
#define switch_strdup (   ptr,
 
)    (void)(assert(((ptr) = strdup((s)))),ptr)
#define switch_strlen_zero (   x)    zstr(x)

Definition at line 283 of file switch_utils.h.

Referenced by main().

#define switch_strlen_zero_buf (   x)    zstr_buf(x)

Definition at line 284 of file switch_utils.h.

#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 624 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(), 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_test_reactive(), switch_caller_get_field_by_name(), switch_caller_profile_event_set_data(), switch_channel_clear_state_handler(), switch_channel_dequeue_dtmf(), switch_core_add_registration(), switch_core_asr_close(), 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_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_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_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_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_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_file(), switch_ivr_process_fh(), switch_ivr_record_file(), switch_ivr_sound_test(), switch_ivr_speak_text_handle(), switch_ivr_wait_for_answer(), switch_jb_put_packet(), 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(), unicast_thread_run(), video_bug_thread(), video_helper_thread(), and video_write_thread().

#define switch_test_subnet (   _ip,
  _net,
  _mask 
)    (_mask ? ((_net & _mask) == (_ip & _mask)) : _net ? _net == _ip : 1)
#define switch_time_from_sec (   sec)    ((switch_time_t)(sec) * 1000000)

Definition at line 916 of file switch_utils.h.

#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 467 of file switch_utils.h.

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

Definition at line 47 of file switch_utils.h.

Referenced by switch_needs_url_encode(), and switch_url_encode_opt().

#define switch_yield (   ms)    switch_sleep(ms);

Wait a desired number of microseconds and yield the CPU.

Definition at line 908 of file switch_utils.h.

Referenced by _switch_cache_db_get_db_handle(), audio_bridge_thread(), media_thread_run(), originate_on_consume_media_transmit(), pool_thread(), record_callback(), recording_thread(), 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_core_db_exec(), switch_core_db_persistant_execute(), switch_core_db_persistant_execute_trans(), switch_core_media_end_video_function(), switch_core_media_ext_address_lookup(), 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_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_nat_multicast_runtime(), switch_nat_thread_stop(), 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_thread_rwlock_trywrlock_timeout(), switch_user_sql_thread(), uuid_bridge_on_soft_execute(), video_helper_thread(), and wait_for_cause().

#define switch_zmalloc (   ptr,
  len 
)    (void)(assert((ptr = calloc(1, (len)))),ptr)
#define zset (   _a,
  _b 
)    if (!zstr(_b)) _a = _b

Definition at line 295 of file switch_utils.h.

#define zstr (   x)    _zstr(x)

Definition at line 281 of file switch_utils.h.

Referenced by __switch_xml_open_root(), _switch_core_db_handle(), add_audio_codec(), api_hook(), 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_trans(), enterprise_originate_ringback_thread(), Event::Event(), EventConsumer::EventConsumer(), API::execute(), CoreSession::execute(), API::executeString(), fs_core_init(), 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(), 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_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_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_var_check(), switch_channel_str2cause(), switch_channel_transfer_variable_prefix(), switch_color_set_rgb(), switch_console_complete(), switch_console_list_uuid(), 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_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_find_zrtp_hash(), switch_core_media_gen_local_sdp(), switch_core_media_get_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_strdup(), 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_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_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_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_file(), switch_ivr_process_fh(), switch_ivr_read(), switch_ivr_say(), switch_ivr_session_transfer(), switch_ivr_set_json_chan_vars(), switch_ivr_set_user(), 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_init(), switch_log_node_dup(), switch_log_vprintf(), switch_lookup_timezone(), switch_media_bug_parse_spy_fmt(), switch_media_handle_create(), switch_network_list_perform_add_cidr_token(), switch_odbc_handle_callback_exec_detailed(), switch_odbc_handle_exec(), switch_pgsql_finish_results_real(), switch_pgsql_handle_callback_exec_detailed(), switch_pgsql_handle_exec_base_detailed(), switch_play_and_get_digits(), 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_strftime_tz(), switch_strip_spaces(), switch_strip_whitespace(), 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(), and transfer_after_bridge().

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

Definition at line 285 of file switch_utils.h.

Referenced by get_upnp_pubaddr(), and switch_strftime_tz().

Typedef Documentation

Enumeration Type Documentation

flags to be used with switch_build_uri()

Enumerator
SWITCH_URI_NUMERIC_HOST 
SWITCH_URI_NUMERIC_PORT 
SWITCH_URI_NO_SCOPE 

Definition at line 546 of file switch_utils.h.

Function Documentation

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 274 of file switch_utils.h.

275 {
276  return !s || *s == '\0';
277 }
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 2273 of file switch_utils.c.

References buf, and switch_assert.

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

2274 {
2275  switch_assert(buf);
2276  *buf = '\0';
2277 
2278  if (sa) {
2279  getnameinfo(sa, salen, buf, (socklen_t) len, NULL, 0, NI_NUMERICHOST);
2280  }
2281  return buf;
2282 }
switch_byte_t switch_byte_t * buf
#define switch_assert(expr)
char* get_addr6 ( char *  buf,
switch_size_t  len,
struct sockaddr_in6 *  sa,
socklen_t  salen 
)

Definition at line 2257 of file switch_utils.c.

References buf, and switch_assert.

Referenced by switch_get_addr().

2258 {
2259  switch_assert(buf);
2260  *buf = '\0';
2261 
2262  if (sa) {
2263 #if defined(NTDDI_VERSION)
2264  switch_inet_ntop6((unsigned char*)&(sa->sin6_addr), buf, len);
2265 #else
2266  inet_ntop(AF_INET6, &(sa->sin6_addr), buf, len);
2267 #endif
2268  }
2269 
2270  return buf;
2271 }
switch_byte_t switch_byte_t * buf
#define switch_assert(expr)
int get_addr_int ( switch_sockaddr_t sa)

Definition at line 2150 of file switch_utils.c.

2151 {
2152  struct sockaddr_in *s = (struct sockaddr_in *) &sa->sa;
2153 
2154  return ntohs((unsigned short) s->sin_addr.s_addr);
2155 }
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 2284 of file switch_utils.c.

2285 {
2286  unsigned short port = 0;
2287  if (sa) {
2288  switch (sa->sa_family) {
2289  case AF_INET:
2290  port = ntohs(((struct sockaddr_in *) sa)->sin_port);
2291  break;
2292  case AF_INET6:
2293  port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
2294  break;
2295  }
2296  }
2297  return port;
2298 }
int old_switch_tolower ( int  c)

Definition at line 3509 of file switch_utils.c.

3510 {
3511  if ((unsigned int) c > 255)
3512  return (c);
3513  if (c < -1)
3514  return EOF;
3515  return ((_switch_tolower_tab_ + 1)[c]);
3516 }
const short * _switch_tolower_tab_
int old_switch_toupper ( int  c)

Definition at line 3462 of file switch_utils.c.

3463 {
3464  if ((unsigned int) c > 255)
3465  return (c);
3466  if (c < -1)
3467  return EOF;
3468  return ((_switch_toupper_tab_ + 1)[c]);
3469 }
const short * _switch_toupper_tab_
char* switch_amp_encode ( char *  s,
char *  buf,
switch_size_t  len 
)

Definition at line 804 of file switch_utils.c.

References buf, and switch_assert.

805 {
806  char *p, *q;
807  switch_size_t x = 0;
808  switch_assert(s);
809 
810  q = buf;
811 
812  for (p = s; x < len; p++) {
813  switch (*p) {
814 
815  case '"':
816  if (x + 6 > len - 1) {
817  goto end;
818  }
819  *q++ = '&';
820  *q++ = 'q';
821  *q++ = 'u';
822  *q++ = 'o';
823  *q++ = 't';
824  *q++ = ';';
825  x += 6;
826  break;
827  case '\'':
828  if (x + 6 > len - 1) {
829  goto end;
830  }
831  *q++ = '&';
832  *q++ = 'a';
833  *q++ = 'p';
834  *q++ = 'o';
835  *q++ = 's';
836  *q++ = ';';
837  x += 6;
838  break;
839  case '&':
840  if (x + 5 > len - 1) {
841  goto end;
842  }
843  *q++ = '&';
844  *q++ = 'a';
845  *q++ = 'm';
846  *q++ = 'p';
847  *q++ = ';';
848  x += 5;
849  break;
850  case '<':
851  if (x + 4 > len - 1) {
852  goto end;
853  }
854  *q++ = '&';
855  *q++ = 'l';
856  *q++ = 't';
857  *q++ = ';';
858  x += 4;
859  break;
860  case '>':
861  if (x + 4 > len - 1) {
862  goto end;
863  }
864  *q++ = '&';
865  *q++ = 'g';
866  *q++ = 't';
867  *q++ = ';';
868  x += 4;
869  break;
870  default:
871  if (x + 1 > len - 1) {
872  goto end;
873  }
874  *q++ = *p;
875  x++;
876  if (*p == '\0') {
877  goto end;
878  }
879  break;
880  }
881  }
882 
883  end:
884 
885  return buf;
886 }
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define switch_assert(expr)
switch_bool_t switch_ast2regex ( const char *  pat,
char *  rbuf,
size_t  len 
)

Definition at line 1250 of file switch_utils.c.

References memset(), SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_regex_perform().

1251 {
1252  const char *p = pat;
1253 
1254  if (!pat) {
1255  return SWITCH_FALSE;
1256  }
1257 
1258  memset(rbuf, 0, len);
1259 
1260  *(rbuf + strlen(rbuf)) = '^';
1261 
1262  while (p && *p) {
1263  if (*p == 'N') {
1264  strncat(rbuf, "[2-9]", len - strlen(rbuf));
1265  } else if (*p == 'X') {
1266  strncat(rbuf, "[0-9]", len - strlen(rbuf));
1267  } else if (*p == 'Z') {
1268  strncat(rbuf, "[1-9]", len - strlen(rbuf));
1269  } else if (*p == '.') {
1270  strncat(rbuf, ".*", len - strlen(rbuf));
1271  } else if (strlen(rbuf) < len - 1) {
1272  *(rbuf + strlen(rbuf)) = *p;
1273  }
1274  p++;
1275  }
1276  *(rbuf + strlen(rbuf)) = '$';
1277 
1278  return strcmp(pat, rbuf) ? SWITCH_TRUE : SWITCH_FALSE;
1279 }
memset(buf, 0, buflen)
unsigned int switch_atoui ( const char *  nptr)

Definition at line 3925 of file switch_utils.c.

Referenced by switch_ivr_process_fh().

3926 {
3927  int tmp = atoi(nptr);
3928  if (tmp < 0) return 0;
3929  else return (unsigned int) tmp;
3930 }
unsigned long switch_atoul ( const char *  nptr)

Definition at line 3932 of file switch_utils.c.

Referenced by switch_load_core_config().

3933 {
3934  long tmp = atol(nptr);
3935  if (tmp < 0) return 0;
3936  else return (unsigned long) tmp;
3937 }
switch_size_t switch_b64_decode ( char *  in,
char *  out,
switch_size_t  olen 
)

Definition at line 929 of file switch_utils.c.

References switch_b64_table.

Referenced by switch_core_media_add_crypto().

930 {
931 
932  char l64[256];
933  int b = 0, c, l = 0, i;
934  char *ip, *op = out;
935  size_t ol = 0;
936 
937  for (i = 0; i < 256; i++) {
938  l64[i] = -1;
939  }
940 
941  for (i = 0; i < 64; i++) {
942  l64[(int) switch_b64_table[i]] = (char) i;
943  }
944 
945  for (ip = in; ip && *ip; ip++) {
946  c = l64[(int) *ip];
947  if (c == -1) {
948  continue;
949  }
950 
951  b = (b << 6) + c;
952  l += 6;
953 
954  while (l >= 8) {
955  op[ol++] = (char) ((b >> (l -= 8)) % 256);
956  if (ol >= olen - 2) {
957  goto end;
958  }
959  }
960  }
961 
962  end:
963 
964  op[ol++] = '\0';
965 
966  return ol;
967 }
static const char switch_b64_table[65]
Definition: switch_utils.c:888
switch_byte_t in
switch_status_t switch_b64_encode ( unsigned char *  in,
switch_size_t  ilen,
unsigned char *  out,
switch_size_t  olen 
)

Definition at line 890 of file switch_utils.c.

References switch_b64_table, and SWITCH_STATUS_SUCCESS.

Referenced by switch_core_media_build_crypto(), and switch_rtp_add_crypto_key().

891 {
892  int y = 0, bytes = 0;
893  size_t x = 0;
894  unsigned int b = 0, l = 0;
895 
896  for (x = 0; x < ilen; x++) {
897  b = (b << 8) + in[x];
898  l += 8;
899 
900  while (l >= 6) {
901  out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64];
902  if (bytes >= (int)olen - 1) {
903  goto end;
904  }
905  if (++y != 72) {
906  continue;
907  }
908  /* out[bytes++] = '\n'; */
909  y = 0;
910  }
911  }
912 
913  if (l > 0) {
914  out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64];
915  }
916  if (l != 0) {
917  while (l < 6 && bytes < (int)olen - 1) {
918  out[bytes++] = '=', l += 2;
919  }
920  }
921 
922  end:
923 
924  out[bytes] = '\0';
925 
926  return SWITCH_STATUS_SUCCESS;
927 }
static const char switch_b64_table[65]
Definition: switch_utils.c:888
switch_byte_t in
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 2300 of file switch_utils.c.

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

2301 {
2302  char host[NI_MAXHOST], serv[NI_MAXSERV];
2303  struct sockaddr_in6 si6;
2304  const struct sockaddr *addr;
2305  const char *colon;
2306 
2307  if (flags & SWITCH_URI_NO_SCOPE && sa->family == AF_INET6) {
2308  memcpy(&si6, &sa->sa, sa->salen);
2309  si6.sin6_scope_id = 0;
2310 
2311  addr = (const struct sockaddr *) &si6;
2312  } else {
2313  addr = (const struct sockaddr *) (intptr_t) & sa->sa;
2314  }
2315 
2316  if (getnameinfo(addr, sa->salen, host, sizeof(host), serv, sizeof(serv),
2317  ((flags & SWITCH_URI_NUMERIC_HOST) ? NI_NUMERICHOST : 0) | ((flags & SWITCH_URI_NUMERIC_PORT) ? NI_NUMERICSERV : 0)) != 0) {
2318  return 0;
2319  }
2320 
2321  colon = strchr(host, ':');
2322 
2323  return switch_snprintf(uri, size, "%s:%s%s%s%s%s%s%s", scheme,
2324  user ? user : "", user ? "@" : "", colon ? "[" : "", host, colon ? "]" : "", serv[0] ? ":" : "", serv[0] ? serv : "");
2325 }
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
static int32_t switch_calc_bitrate ( int  w,
int  h,
int  quality,
double  fps 
)
inlinestatic

Definition at line 998 of file switch_utils.h.

999 {
1000  int r;
1001 
1002  /* KUSH GAUGE*/
1003 
1004  if (!fps) fps = 15;
1005 
1006  r = (int32_t)((double)(w * h * fps * (quality ? quality : 1)) * 0.07) / 1000;
1007 
1008  if (!quality) r /= 2;
1009 
1010  return r;
1011 
1012 }
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 2335 of file switch_utils.c.

References switch_toupper().

Referenced by do_2833().

2336 {
2337  char *c;
2338  unsigned char counter = 0;
2339 
2340  key = (char) switch_toupper(key);
2341  for (c = RFC2833_CHARS; *c; c++) {
2342  if (*c == key) {
2343  return counter;
2344  }
2345  counter++;
2346  }
2347  return '\0';
2348 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
static char RFC2833_CHARS[]
static char* switch_clean_name_string ( char *  s)
inlinestatic

Definition at line 755 of file switch_utils.h.

756 {
757  char *p;
758  for (p = s; p && *p; p++) {
759  uint8_t x = (uint8_t) * p;
760  if ((x < 32) || x == '\'' || x == '"' || x == '<' || x == '>' || x == '\\' || x == ':' || x == '@' || x == '/') {
761  *p = ' ';
762  }
763  if ( (p == s) && (*p == ' ') ) {
764  s++;
765  }
766  }
767 
768  return s;
769 }
static char* switch_clean_string ( char *  s)
inlinestatic

Definition at line 741 of file switch_utils.h.

742 {
743  char *p;
744  for (p = s; p && *p; p++) {
745  uint8_t x = (uint8_t) * p;
746  if ((x < 32) && x != '\n' && x != '\r') {
747  *p = ' ';
748  }
749  }
750 
751  return s;
752 }
int switch_cmp_addr ( switch_sockaddr_t sa1,
switch_sockaddr_t sa2 
)

Definition at line 2157 of file switch_utils.c.

Referenced by handle_ice(), and rtp_common_read().

2158 {
2159  struct sockaddr_in *s1;
2160  struct sockaddr_in *s2;
2161 
2162  struct sockaddr_in6 *s16;
2163  struct sockaddr_in6 *s26;
2164 
2165  struct sockaddr *ss1;
2166  struct sockaddr *ss2;
2167 
2168  if (!(sa1 && sa2))
2169  return 0;
2170 
2171  s1 = (struct sockaddr_in *) &sa1->sa;
2172  s2 = (struct sockaddr_in *) &sa2->sa;
2173 
2174  s16 = (struct sockaddr_in6 *) &sa1->sa;
2175  s26 = (struct sockaddr_in6 *) &sa2->sa;
2176 
2177  ss1 = (struct sockaddr *) &sa1->sa;
2178  ss2 = (struct sockaddr *) &sa2->sa;
2179 
2180  if (ss1->sa_family != ss2->sa_family)
2181  return 0;
2182 
2183  switch (ss1->sa_family) {
2184  case AF_INET:
2185  return (s1->sin_addr.s_addr == s2->sin_addr.s_addr && s1->sin_port == s2->sin_port);
2186  case AF_INET6:
2187  if (s16->sin6_addr.s6_addr && s26->sin6_addr.s6_addr) {
2188  int i;
2189 
2190  if (s16->sin6_port != s26->sin6_port)
2191  return 0;
2192 
2193  for (i = 0; i < 4; i++) {
2194  if (*((int32_t *) s16->sin6_addr.s6_addr + i) != *((int32_t *) s26->sin6_addr.s6_addr + i))
2195  return 0;
2196  }
2197 
2198  return 1;
2199  }
2200  }
2201 
2202  return 0;
2203 }
int switch_cp_addr ( switch_sockaddr_t sa1,
switch_sockaddr_t sa2 
)

Definition at line 2206 of file switch_utils.c.

Referenced by read_rtp_packet(), and switch_rtp_set_remote_address().

2207 {
2208  struct sockaddr_in *s1;
2209  struct sockaddr_in *s2;
2210 
2211  struct sockaddr_in6 *s16;
2212  struct sockaddr_in6 *s26;
2213 
2214  struct sockaddr *ss1;
2215  //struct sockaddr *ss2;
2216 
2217  if (!(sa1 && sa2))
2218  return 0;
2219 
2220  s1 = (struct sockaddr_in *) &sa1->sa;
2221  s2 = (struct sockaddr_in *) &sa2->sa;
2222 
2223  s16 = (struct sockaddr_in6 *) &sa1->sa;
2224  s26 = (struct sockaddr_in6 *) &sa2->sa;
2225 
2226  ss1 = (struct sockaddr *) &sa1->sa;
2227  //ss2 = (struct sockaddr *) &sa2->sa;
2228 
2229  sa1->port = sa2->port;
2230  sa1->family = sa2->family;
2231 
2232  sa1->sa.sin.sin_family = sa2->family;
2233 
2234  switch (ss1->sa_family) {
2235  case AF_INET:
2236  s1->sin_addr.s_addr = s2->sin_addr.s_addr;
2237  s1->sin_port = s2->sin_port;
2238 
2239  return 1;
2240  case AF_INET6:
2241  if (s16->sin6_addr.s6_addr && s26->sin6_addr.s6_addr) {
2242  int i;
2243 
2244  s16->sin6_port = s26->sin6_port;
2245 
2246  for (i = 0; i < 4; i++) {
2247  *((int32_t *) s16->sin6_addr.s6_addr + i) = *((int32_t *) s26->sin6_addr.s6_addr + i);
2248  }
2249 
2250  return 1;
2251  }
2252  }
2253 
2254  return 0;
2255 }
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 2658 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_vprintf(), and switch_simple_email().

2659 {
2660  const char *p, *ret = in;
2661  const char delims[] = "/\\";
2662  const char *i;
2663 
2664  if (in) {
2665  for (i = delims; *i; i++) {
2666  p = in;
2667  while ((p = strchr(p, *i)) != 0) {
2668  ret = ++p;
2669  }
2670  }
2671  return ret;
2672  } else {
2673  return NULL;
2674  }
2675 }
switch_byte_t in
switch_bool_t switch_dow_cmp ( const char *  exp,
int  val 
)

Definition at line 3732 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().

3733 {
3734  dow_t cur, prev = DOW_EOF, range_start = DOW_EOF;
3735  const char *p = exp;
3736 
3737  while ((cur = _dow_read_token(&p)) != DOW_EOF) {
3738  if (cur == DOW_COMA) {
3739  /* Reset state */
3740  cur = prev = DOW_EOF;
3741  } else if (cur == DOW_HYPHEN) {
3742  /* Save the previous token and move to the next one */
3743  range_start = prev;
3744  } else if (cur == DOW_ERR) {
3745  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse error for [%s] at position %ld (%.6s)\n", exp, (long) (p - exp), p);
3746  break;
3747  } else {
3748  /* Valid day found */
3749  if (range_start != DOW_EOF) { /* Evaluating a range */
3750  if (range_start <= cur ? (val >= range_start && val <= cur) : (val >= range_start || val <= cur)) {
3751  return SWITCH_TRUE;
3752  }
3753  range_start = DOW_EOF;
3754  } else if (val == cur) {
3755  return SWITCH_TRUE;
3756  }
3757  }
3758 
3759  prev = cur;
3760  }
3761 
3762  return SWITCH_FALSE;
3763 }
#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
const char* switch_dow_int2str ( int  val)

Definition at line 3674 of file switch_utils.c.

References switch_arraylen.

Referenced by switch_xml_std_datetime_check().

3674  {
3675  if (val >= switch_arraylen(DOW)) {
3676  val = val % switch_arraylen(DOW);
3677  }
3678  return DOW[val];
3679 }
#define switch_arraylen(_a)
Definition: switch_utils.h:341
static const char * DOW[]
int switch_dow_str2int ( const char *  exp)

Definition at line 3681 of file switch_utils.c.

References switch_arraylen.

Referenced by _dow_read_token().

3681  {
3682  int ret = -1;
3683  int x;
3684 
3685  for (x = 0; x < switch_arraylen(DOW); x++) {
3686  if (!strncasecmp(DOW[x], exp, 3)) {
3687  ret = x + 1;
3688  break;
3689  }
3690  }
3691  return ret;
3692 }
#define switch_arraylen(_a)
Definition: switch_utils.h:341
static const char * DOW[]
static int switch_dtmftoi ( char *  s)
inlinestatic

Definition at line 402 of file switch_utils.h.

References switch_assert.

Referenced by meta_on_dtmf(), and switch_ivr_bind_dtmf_meta_session().

403 {
404  int r;
405 
406  switch_assert(s);
407 
408  if (!(r = atoi(s))) {
409  int l = tolower((unsigned char)*s);
410  if (l > 96 && l < 101) {
411  r = l - 87;
412  }
413  }
414 
415  return r;
416 }
#define switch_assert(expr)
static int switch_errno_is_break ( int  errcode)
inlinestatic

Definition at line 580 of file switch_utils.h.

Referenced by switch_wait_sock(), and switch_wait_socklist().

581 {
582  return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
583 }
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 2350 of file switch_utils.c.

References in, and switch_core_alloc.

2351 {
2352  char *data;
2353  const char *p, *d;
2354  int count = 1, i = 0;
2355 
2356  p = in;
2357  while (*p) {
2358  d = delim;
2359  while (*d) {
2360  if (*p == *d) {
2361  count++;
2362  }
2363  d++;
2364  }
2365  p++;
2366  }
2367 
2368  if (count == 1) {
2369  return in;
2370  }
2371 
2372  data = switch_core_alloc(pool, strlen(in) + count);
2373 
2374  p = in;
2375  while (*p) {
2376  d = delim;
2377  while (*d) {
2378  if (*p == *d) {
2379  data[i++] = esc;
2380  }
2381  d++;
2382  }
2383  data[i++] = *p;
2384  p++;
2385  }
2386  return data;
2387 }
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:682
char* switch_escape_string ( const char *  in,
char *  out,
switch_size_t  outlen 
)

Definition at line 2421 of file switch_utils.c.

Referenced by switch_escape_string_pool().

2422 {
2423  const char *p;
2424  char *o = out;
2425 
2426  for (p = in; *p; p++) {
2427  switch (*p) {
2428  case '\n':
2429  *o++ = '\\';
2430  *o++ = 'n';
2431  break;
2432  case '\r':
2433  *o++ = '\\';
2434  *o++ = 'r';
2435  break;
2436  case '\t':
2437  *o++ = '\\';
2438  *o++ = 't';
2439  break;
2440  case ' ':
2441  *o++ = '\\';
2442  *o++ = 's';
2443  break;
2444  case '$':
2445  *o++ = '\\';
2446  *o++ = '$';
2447  break;
2448  default:
2449  *o++ = *p;
2450  break;
2451  }
2452  }
2453 
2454  *o++ = '\0';
2455 
2456  return out;
2457 }
switch_byte_t in
char* switch_escape_string_pool ( const char *  in,
switch_memory_pool_t pool 
)

Definition at line 2459 of file switch_utils.c.

References buf, switch_core_alloc, and switch_escape_string().

2460 {
2461  size_t len = strlen(in) * 2 + 1;
2462  char *buf = switch_core_alloc(pool, len);
2463  return switch_escape_string(in, buf, len);
2464 }
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:682
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 482 of file switch_utils.h.

References SWITCH_FALSE, switch_is_number(), and SWITCH_TRUE.

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

483 {
484  return ((expr && ( !strcasecmp(expr, "no") ||
485  !strcasecmp(expr, "off") ||
486  !strcasecmp(expr, "false") ||
487  !strcasecmp(expr, "f") ||
488  !strcasecmp(expr, "disabled") ||
489  !strcasecmp(expr, "inactive") ||
490  !strcasecmp(expr, "disallow") ||
491  (switch_is_number(expr) && !atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
492 }
switch_bool_t switch_is_number(const char *str)
switch_size_t switch_fd_read_dline ( int  fd,
char **  buf,
switch_size_t len 
)

Definition at line 709 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().

710 {
711  char c, *p;
712  int cur;
713  switch_size_t total = 0;
714  char *data = *buf;
715  switch_size_t ilen = *len;
716 
717  if (!data) {
718  *len = ilen = DLINE_BLOCK_SIZE;
719  data = malloc(ilen);
720  memset(data, 0, ilen);
721  }
722 
723  p = data;
724  while ((cur = read(fd, &c, 1)) == 1) {
725 
726  if (total + 2 >= ilen) {
727  if (ilen + DLINE_BLOCK_SIZE > DLINE_MAX_SIZE) {
728  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Single line limit reached!\n");
729  break;
730  }
731 
732  ilen += DLINE_BLOCK_SIZE;
733  data = realloc(data, ilen);
734  switch_assert(data);
735  p = data + total;
736 
737  }
738 
739  total += cur;
740  *p++ = c;
741 
742  if (c == '\r' || c == '\n') {
743  break;
744  }
745  }
746 
747  *p++ = '\0';
748 
749  *len = ilen;
750  *buf = data;
751 
752  return total;
753 }
#define SWITCH_CHANNEL_LOG
#define DLINE_BLOCK_SIZE
Definition: switch_utils.c:707
pack cur
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define DLINE_MAX_SIZE
Definition: switch_utils.c:708
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_size_t switch_fd_read_line ( int  fd,
char *  buf,
switch_size_t  len 
)

Definition at line 687 of file switch_utils.c.

References buf, and cur.

688 {
689  char c, *p;
690  int cur;
691  switch_size_t total = 0;
692 
693  p = buf;
694  while (total + 2 < len && (cur = read(fd, &c, 1)) == 1) {
695  total += cur;
696  *p++ = c;
697  if (c == '\r' || c == '\n') {
698  break;
699  }
700  }
701 
702  *p++ = '\0';
703  assert(total < len);
704  return total;
705 }
pack cur
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
char* switch_find_end_paren ( const char *  s,
char  open,
char  close 
)

Definition at line 661 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_is_file_path(), switch_ivr_displace_session(), switch_ivr_originate(), switch_ivr_play_file(), switch_ivr_record_file(), switch_ivr_record_session(), and switch_separate_file_params().

662 {
663  const char *e = NULL;
664  int depth = 0;
665 
666  while (s && *s && *s == ' ') {
667  s++;
668  }
669 
670  if (s && *s == open) {
671  depth++;
672  for (e = s + 1; e && *e; e++) {
673  if (*e == open && open != close) {
674  depth++;
675  } else if (*e == close) {
676  depth--;
677  if (!depth) {
678  break;
679  }
680  }
681  }
682  }
683 
684  return (e && *e == close) ? (char *) e : NULL;
685 }
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
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_load_network_lists(), and switch_nat_init().

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

Definition at line 360 of file switch_utils.c.

References switch_core_alloc, and switch_snprintf().

361 {
362  char *e, *r = NULL, *ptr = NULL, *next = NULL;
363  size_t len;
364 
365  ptr = (char *) str;
366 
367  while (ptr) {
368  len = strlen(param);
369  e = ptr+len;
370  next = strchr(ptr, ';');
371 
372  if (!strncasecmp(ptr, param, len) && *e == '=') {
373  size_t mlen;
374 
375  ptr = ++e;
376 
377  if (next) {
378  e = next;
379  } else {
380  e = ptr + strlen(ptr);
381  }
382 
383  mlen = (e - ptr) + 1;
384 
385  if (pool) {
386  r = switch_core_alloc(pool, mlen);
387  } else {
388  r = malloc(mlen);
389  }
390 
391  switch_snprintf(r, mlen, "%s", ptr);
392 
393  break;
394  }
395 
396  if (next) {
397  ptr = next + 1;
398  } else break;
399  }
400 
401  return r;
402 }
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:682
char* switch_format_number ( const char *  num)

Definition at line 3888 of file switch_utils.c.

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

3889 {
3890  char *r;
3891  size_t len;
3892  const char *p = num;
3893 
3894  if (!p) {
3895  return (char*)p;
3896  }
3897 
3898  if (zstr(p)) {
3899  return strdup(p);
3900  }
3901 
3902  if (*p == '+') {
3903  p++;
3904  }
3905 
3906  if (!switch_is_number(p)) {
3907  return strdup(p);
3908  }
3909 
3910  len = strlen(p);
3911 
3912  /* region 1, TBD add more....*/
3913  if (len == 11 && p[0] == '1') {
3914  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]);
3915  } else if (len == 10) {
3916  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]);
3917  } else {
3918  r = strdup(num);
3919  }
3920 
3921  return r;
3922 }
#define zstr(x)
Definition: switch_utils.h:281
switch_bool_t switch_is_number(const char *str)
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
switch_size_t switch_fp_read_dline ( FILE *  fd,
char **  buf,
switch_size_t len 
)

Definition at line 757 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(), and switch_stream_write_file_contents().

758 {
759  char c, *p;
760  switch_size_t total = 0;
761  char *data = *buf;
762  switch_size_t ilen = *len;
763 
764  if (!data) {
765  *len = ilen = DLINE_BLOCK_SIZE;
766  data = malloc(ilen);
767  memset(data, 0, ilen);
768  }
769 
770  p = data;
771  //while ((c = fgetc(fd)) != EOF) {
772 
773  while (fread(&c, 1, 1, fd) == 1) {
774 
775  if (total + 2 >= ilen) {
776  if (ilen + DLINE_BLOCK_SIZE > DLINE_MAX_SIZE) {
777  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Single line limit reached!\n");
778  break;
779  }
780 
781  ilen += DLINE_BLOCK_SIZE;
782  data = realloc(data, ilen);
783  switch_assert(data);
784  p = data + total;
785 
786  }
787 
788  total++;
789  *p++ = c;
790 
791  if (c == '\r' || c == '\n') {
792  break;
793  }
794  }
795 
796  *p++ = '\0';
797 
798  *len = ilen;
799  *buf = data;
800 
801  return total;
802 }
#define SWITCH_CHANNEL_LOG
#define DLINE_BLOCK_SIZE
Definition: switch_utils.c:707
switch_byte_t switch_byte_t * buf
uintptr_t switch_size_t
#define DLINE_MAX_SIZE
Definition: switch_utils.c:708
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_status_t switch_frame_alloc ( switch_frame_t **  frame,
switch_size_t  size 
)

Definition at line 83 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.

84 {
85  switch_frame_t *new_frame;
86 
87  switch_zmalloc(new_frame, sizeof(*new_frame));
88 
89  switch_set_flag(new_frame, SFF_DYNAMIC);
90  new_frame->buflen = (uint32_t)size;
91  new_frame->data = malloc(size);
92  switch_assert(new_frame->data);
93 
94  *frame = new_frame;
95 
96  return SWITCH_STATUS_SUCCESS;
97 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
uint32_t buflen
Definition: switch_frame.h:59
#define switch_zmalloc(ptr, len)
An abstraction of a data frame.
Definition: switch_frame.h:43
#define switch_assert(expr)
switch_status_t switch_frame_buffer_create ( switch_frame_buffer_t **  fbP)

Definition at line 254 of file switch_utils.c.

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

255 {
258 
260  fb = switch_core_alloc(pool, sizeof(*fb));
261  fb->pool = pool;
263  *fbP = fb;
264 
265  return SWITCH_STATUS_SUCCESS;
266 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
switch_memory_pool_t * pool
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
switch_mutex_t * mutex
Definition: switch_utils.c:110
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
struct apr_pool_t switch_memory_pool_t
switch_memory_pool_t * pool
Definition: switch_utils.c:109
switch_status_t switch_frame_buffer_destroy ( switch_frame_buffer_t **  fbP)

Definition at line 243 of file switch_utils.c.

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

244 {
245  switch_frame_buffer_t *fb = *fbP;
247  *fbP = NULL;
248  pool = fb->pool;
250 
251  return SWITCH_STATUS_SUCCESS;
252 }
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:640
switch_memory_pool_t * pool
struct apr_pool_t switch_memory_pool_t
switch_memory_pool_t * pool
Definition: switch_utils.c:109
switch_status_t switch_frame_buffer_dup ( switch_frame_buffer_t fb,
switch_frame_t orig,
switch_frame_t **  clone 
)

Definition at line 226 of file switch_utils.c.

References find_free_frame(), switch_assert, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

227 {
228  switch_frame_t *new_frame;
229 
230  if (!orig) {
231  return SWITCH_STATUS_FALSE;
232  }
233 
234  switch_assert(orig->buflen);
235 
236  new_frame = find_free_frame(fb, orig);
237 
238  *clone = new_frame;
239 
240  return SWITCH_STATUS_SUCCESS;
241 }
static switch_frame_t * find_free_frame(switch_frame_buffer_t *fb, switch_frame_t *orig)
Definition: switch_utils.c:114
uint32_t buflen
Definition: switch_frame.h:59
An abstraction of a data frame.
Definition: switch_frame.h:43
#define switch_assert(expr)
switch_status_t switch_frame_buffer_free ( switch_frame_buffer_t fb,
switch_frame_t **  frameP 
)

Definition at line 193 of file switch_utils.c.

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

194 {
195  switch_frame_t *old_frame;
196  switch_frame_node_t *node;
197 
199 
200  old_frame = *frameP;
201  *frameP = NULL;
202 
203  node = (switch_frame_node_t *) old_frame->extra_data;
204  node->inuse = 0;
205  switch_img_free(&node->frame->img);
206 
207  fb->total++;
208 
209  if (fb->head) {
210  fb->head->prev = node;
211  }
212 
213  node->next = fb->head;
214  node->prev = NULL;
215  fb->head = node;
216 
217  switch_assert(node->next != node);
218  switch_assert(node->prev != node);
219 
220 
222 
223  return SWITCH_STATUS_SUCCESS;
224 }
void switch_img_free(switch_image_t **img)
Close an image descriptor.
switch_frame_t * frame
Definition: switch_utils.c:101
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_mutex_t * mutex
Definition: switch_utils.c:110
switch_frame_node_t * head
Definition: switch_utils.c:108
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
void * extra_data
Definition: switch_frame.h:53
struct switch_frame_node_s * next
Definition: switch_utils.c:104
An abstraction of a data frame.
Definition: switch_frame.h:43
struct switch_frame_node_s * prev
Definition: switch_utils.c:103
switch_image_t * img
Definition: switch_frame.h:77
#define switch_assert(expr)
switch_status_t switch_frame_dup ( switch_frame_t orig,
switch_frame_t **  clone 
)

Definition at line 269 of file switch_utils.c.

References switch_frame::buflen, switch_frame::codec, switch_frame::data, switch_frame::img, switch_frame::packet, 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.

270 {
271  switch_frame_t *new_frame;
272 
273  if (!orig) {
274  return SWITCH_STATUS_FALSE;
275  }
276 
277  switch_assert(orig->buflen);
278 
279  new_frame = malloc(sizeof(*new_frame));
280  switch_assert(new_frame);
281 
282  *new_frame = *orig;
283  switch_set_flag(new_frame, SFF_DYNAMIC);
284 
285  if (orig->packet) {
286  new_frame->packet = malloc(SWITCH_RTP_MAX_BUF_LEN);
287  memcpy(new_frame->packet, orig->packet, orig->packetlen);
288  new_frame->data = ((unsigned char *)new_frame->packet) + 12;
289  } else {
290  new_frame->packet = NULL;
291  new_frame->data = malloc(new_frame->buflen);
292  switch_assert(new_frame->data);
293  memcpy(new_frame->data, orig->data, orig->datalen);
294  }
295 
296 
297  new_frame->codec = NULL;
298  new_frame->pmap = NULL;
299  new_frame->img = NULL;
300  if (orig->img && !switch_test_flag(orig, SFF_ENCODED)) {
301  switch_img_copy(orig->img, &new_frame->img);
302  }
303  *clone = new_frame;
304 
305  return SWITCH_STATUS_SUCCESS;
306 }
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
#define SWITCH_RTP_MAX_BUF_LEN
Definition: switch_rtp.h:44
switch_codec_t * codec
Definition: switch_frame.h:45
uint32_t buflen
Definition: switch_frame.h:59
uint32_t datalen
Definition: switch_frame.h:57
uint32_t packetlen
Definition: switch_frame.h:51
payload_map_t * pmap
Definition: switch_frame.h:76
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:43
void * packet
Definition: switch_frame.h:49
switch_image_t * img
Definition: switch_frame.h:77
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
#define switch_assert(expr)
switch_status_t switch_frame_free ( switch_frame_t **  frame)

Definition at line 308 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.

309 {
310  switch_frame_t * f;
311 
312  if (!frame) {
313  return SWITCH_STATUS_FALSE;
314  }
315 
316  f = *frame;
317 
318  if (!f || !switch_test_flag(f, SFF_DYNAMIC)) {
319  return SWITCH_STATUS_FALSE;
320  }
321 
322  *frame = NULL;
323 
324  if (f->img) {
325  switch_img_free(&(f->img));
326  }
327 
328  if (f->packet) {
330  } else {
332  }
333 
334  free(f);
335 
336  return SWITCH_STATUS_SUCCESS;
337 }
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:789
An abstraction of a data frame.
Definition: switch_frame.h:43
void * packet
Definition: switch_frame.h:49
switch_image_t * img
Definition: switch_frame.h:77
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
int switch_fulldate_cmp ( const char *  exp,
switch_time_t ts 
)

Definition at line 3337 of file switch_utils.c.

References cur, memset(), switch_assert, switch_safe_free, switch_split_date(), and switch_split_time().

Referenced by switch_xml_std_datetime_check().

3338 {
3339  char *dup = strdup(exp);
3340  char *sStart;
3341  char *sEnd;
3342  char *cur;
3343  char *p;
3344 
3345  switch_assert(dup);
3346 
3347  cur = dup;
3348  if ((p = strchr(cur, ','))) {
3349  *p++ = '\0';
3350  }
3351 
3352  while (cur) {
3353  sStart = cur;
3354  if ((sEnd=strchr(cur, '~'))) {
3355  char *sDate = sStart;
3356  char *sTime;
3357  *sEnd++ = '\0';
3358  if ((sTime=strchr(sStart, ' '))) {
3359  switch_time_t tsStart;
3360  struct tm tmTmp;
3361  int year = 1970, month = 1, day = 1;
3362  int hour = 0, min = 0, sec = 0;
3363  *sTime++ = '\0';
3364 
3365  memset(&tmTmp, 0, sizeof(tmTmp));
3366  switch_split_date(sDate, &year, &month, &day);
3367  switch_split_time(sTime, &hour, &min, &sec);
3368  tmTmp.tm_year = year-1900;
3369  tmTmp.tm_mon = month-1;
3370  tmTmp.tm_mday = day;
3371 
3372  tmTmp.tm_hour = hour;
3373  tmTmp.tm_min = min;
3374  tmTmp.tm_sec = sec;
3375  tmTmp.tm_isdst = 0;
3376  tsStart = mktime(&tmTmp);
3377 
3378  sDate = sEnd;
3379  if ((sTime=strchr(sEnd, ' '))) {
3380  switch_time_t tsEnd;
3381  struct tm tmTmp;
3382  int year = 1970, month = 1, day = 1;
3383  int hour = 0, min = 0, sec = 0;
3384  *sTime++ = '\0';
3385 
3386  memset(&tmTmp, 0, sizeof(tmTmp));
3387  switch_split_date(sDate, &year, &month, &day);
3388  switch_split_time(sTime, &hour, &min, &sec);
3389  tmTmp.tm_year = year-1900;
3390  tmTmp.tm_mon = month-1;
3391  tmTmp.tm_mday = day;
3392 
3393  tmTmp.tm_hour = hour;
3394  tmTmp.tm_min = min;
3395  tmTmp.tm_sec = sec;
3396  tmTmp.tm_isdst = 0;
3397  tsEnd = mktime(&tmTmp);
3398 
3399  if (tsStart <= *ts/1000000 && tsEnd > *ts/1000000) {
3400  switch_safe_free(dup);
3401  return 1;
3402  }
3403  }
3404  }
3405  }
3406 
3407  cur = p;
3408  if (p) {
3409  if ((p = strchr(p, ','))) {
3410  *p++ = '\0';
3411  }
3412  }
3413 
3414  }
3415  switch_safe_free(dup);
3416  return 0;
3417 
3418 }
void switch_split_date(const char *exp, int *year, int *month, int *day)
pack cur
void switch_split_time(const char *exp, int *hour, int *min, int *sec)
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:789
#define switch_assert(expr)
memset(buf, 0, buflen)
void switch_getcputime ( switch_cputime t)

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

Definition at line 4225 of file switch_utils.c.

4226 {
4227 #if defined(_WIN32)
4228  FILETIME ct, et, kt, ut; // Times are in 100-ns ticks (div 10000 to get ms)
4229  GetProcessTimes(GetCurrentProcess(), &ct, &et, &kt, &ut);
4230  t->userms = ((int64_t)ut.dwLowDateTime | ((int64_t)ut.dwHighDateTime << 32)) / 10000;
4231  t->kernelms = ((int64_t)kt.dwLowDateTime | ((int64_t)kt.dwHighDateTime << 32)) / 10000;
4232 #elif defined(HAVE_GETRUSAGE)
4233  struct rusage r;
4234  getrusage(RUSAGE_SELF, &r);
4235  t->userms = r.ru_utime.tv_sec * 1000 + r.ru_utime.tv_usec / 1000;
4236  t->kernelms = r.ru_stime.tv_sec * 1000 + r.ru_stime.tv_usec / 1000;
4237 #else
4238  t->userms = -1;
4239  t->kernelms = -1;
4240 #endif
4241 }
void switch_http_dump_request ( switch_http_request_t request)

Definition at line 4195 of file switch_utils.c.

References switch_event_header::name, switch_event_header::next, switch_assert, SWITCH_SIZE_T_FMT, and switch_event_header::value.

4196 {
4197  switch_assert(request->method);
4198 
4199  printf("method: %s\n", request->method);
4200 
4201  if (request->uri) printf("uri: %s\n", request->uri);
4202  if (request->qs) printf("qs: %s\n", request->qs);
4203  if (request->host) printf("host: %s\n", request->host);
4204  if (request->port) printf("port: %d\n", request->port);
4205  if (request->from) printf("from: %s\n", request->from);
4206  if (request->user_agent) printf("user_agent: %s\n", request->user_agent);
4207  if (request->referer) printf("referer: %s\n", request->referer);
4208  if (request->user) printf("user: %s\n", request->user);
4209  if (request->keepalive) printf("uri: %d\n", request->keepalive);
4210  if (request->content_type) printf("uri: %s\n", request->content_type);
4211  if (request->content_length) printf("uri: %" SWITCH_SIZE_T_FMT "\n", request->content_length);
4212 
4213  {
4214  switch_event_header_t *header = request->headers->headers;
4215 
4216  printf("headers:\n-------------------------\n");
4217 
4218  while(header) {
4219  printf("%s: %s\n", header->name, header->value);
4220  header = header->next;
4221  }
4222  }
4223 }
An event Header.
Definition: switch_event.h:65
const char * content_type
const char * user_agent
switch_size_t content_length
struct switch_event_header * next
Definition: switch_event.h:76
switch_bool_t keepalive
#define switch_assert(expr)
switch_event_t * headers
#define SWITCH_SIZE_T_FMT
switch_event_header_t * headers
Definition: switch_event.h:90
void switch_http_free_request ( switch_http_request_t request)

Definition at line 4186 of file switch_utils.c.

References switch_event_destroy().

Referenced by switch_http_parse_header().

4187 {
4188  if (request->_buffer) free(request->_buffer);
4189  if (request->_destroy_headers && request->headers) {
4190  switch_event_destroy(&request->headers);
4191  }
4192 }
switch_bool_t _destroy_headers
void switch_event_destroy(switch_event_t **event)
Destroy an event.
switch_event_t * headers
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 4044 of file switch_utils.c.

References clean_uri(), 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, and SWITCH_TRUE.

4045 {
4047  char *p = buffer;
4048  int i = 10;
4049  char *http = NULL;
4050  int header_count;
4051  char *headers[64] = { 0 };
4052  int argc;
4053  char *argv[2] = { 0 };
4054  char *body = NULL;
4055 
4056  if (datalen < 16) return status; /* minimum GET / HTTP/1.1\r\n */
4057 
4058  while(i--) { // sanity check
4059  if (*p++ == ' ') break;
4060  }
4061 
4062  if (i == 0) return status;
4063 
4064  if ((body = strstr(buffer, "\r\n\r\n"))) {
4065  *body = '\0';
4066  body += 4;
4067  } else if (( body = strstr(buffer, "\n\n"))) {
4068  *body = '\0';
4069  body += 2;
4070  } else {
4071  return status;
4072  }
4073 
4074  request->_buffer = strdup(buffer);
4075  request->method = request->_buffer;
4076  request->bytes_buffered = datalen;
4077  if (body) {
4078  request->bytes_header = body - buffer;
4079  request->bytes_read = body - buffer;
4080  }
4081 
4082  p = strchr(request->method, ' ');
4083 
4084  if (!p) goto err;
4085 
4086  *p++ = '\0';
4087 
4088  if (*p != '/') goto err; /* must start from '/' */
4089 
4090  request->uri = p;
4091  p = strchr(request->uri, ' ');
4092 
4093  if (!p) goto err;
4094 
4095  *p++ = '\0';
4096  http = p;
4097 
4098  p = strchr(request->uri, '?');
4099 
4100  if (p) {
4101  *p++ = '\0';
4102  request->qs = p;
4103  }
4104 
4105  if (clean_uri((char *)request->uri) != SWITCH_STATUS_SUCCESS) {
4106  goto err;
4107  }
4108 
4109  if (!strncmp(http, "HTTP/1.1", 8)) {
4110  request->keepalive = SWITCH_TRUE;
4111  } else if (strncmp(http, "HTTP/1.0", 8)) {
4112  goto err;
4113  }
4114 
4115  if (!request->headers) {
4117  goto err;
4118  }
4119  request->_destroy_headers = SWITCH_TRUE;
4120  }
4121 
4122  p = strchr(http, '\n');
4123 
4124  if (p) {
4125  *p++ = '\0'; // now the first header
4126  } else {
4127  goto noheader;
4128  }
4129 
4130  header_count = switch_separate_string(p, '\n', headers, sizeof(headers)/ sizeof(headers[0]));
4131 
4132  if (header_count < 1) goto err;
4133 
4134  for (i = 0; i < header_count; i++) {
4135  char *header, *value;
4136  int len;
4137 
4138  argc = switch_separate_string(headers[i], ':', argv, 2);
4139 
4140  if (argc != 2) goto err;
4141 
4142  header = argv[0];
4143  value = argv[1];
4144 
4145  if (*value == ' ') value++;
4146 
4147  len = strlen(value);
4148 
4149  if (len && *(value + len - 1) == '\r') *(value + len - 1) = '\0';
4150 
4152 
4153  if (!strncasecmp(header, "User-Agent", 10)) {
4154  request->user_agent = value;
4155  } else if (!strncasecmp(header, "Host", 4)) {
4156  request->host = value;
4157  p = strchr(value, ':');
4158 
4159  if (p) {
4160  *p++ = '\0';
4161 
4162  if (*p) request->port = (switch_port_t)atoi(p);
4163  }
4164  } else if (!strncasecmp(header, "Content-Type", 12)) {
4165  request->content_type = value;
4166  } else if (!strncasecmp(header, "Content-Length", 14)) {
4167  request->content_length = atoi(value);
4168  } else if (!strncasecmp(header, "Referer", 7)) {
4169  request->referer = value;
4170  }
4171  }
4172 
4173 noheader:
4174 
4175  if (request->qs) {
4176  switch_http_parse_qs(request, NULL);
4177  }
4178 
4179  return SWITCH_STATUS_SUCCESS;
4180 
4181 err:
4182  switch_http_free_request(request);
4183  return status;
4184 }
switch_size_t bytes_read
void switch_http_free_request(switch_http_request_t *request)
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:383
unsigned int switch_separate_string(char *buf, char delim, char **array, unsigned int arraylen)
switch_status_t clean_uri(char *uri)
switch_bool_t keepalive
switch_event_t * headers
switch_size_t bytes_header
switch_size_t bytes_buffered
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 3969 of file switch_utils.c.

References switch_assert, switch_event_add_header_string(), switch_safe_free, SWITCH_STACK_BOTTOM, and switch_url_decode().

Referenced by switch_http_parse_header().

3970 {
3971  char *q;
3972  char *next;
3973  char *name, *val;
3974 
3975  if (qs) {
3976  q = qs;
3977  } else { /*parse our own qs, dup to avoid modify the original string */
3978  q = strdup(request->qs);
3979  }
3980 
3981  switch_assert(q);
3982  next = q;
3983 
3984  do {
3985  char *p;
3986 
3987  if ((next = strchr(next, '&'))) {
3988  *next++ = '\0';
3989  }
3990 
3991  for (p = q; p && *p; p++) {
3992  if (*p == '+') *p = ' ';
3993  }
3994 
3995  switch_url_decode(q);
3996 
3997  name = q;
3998  if ((val = strchr(name, '='))) {
3999  *val++ = '\0';
4001  }
4002  q = next;
4003  } while (q);
4004 
4005  if (!qs) {
4006  switch_safe_free(q);
4007  }
4008 }
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:789
char * switch_url_decode(char *s)
#define switch_assert(expr)
switch_event_t * headers
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 switch_check_network_list_ip_token(), switch_find_local_ip(), switch_network_list_add_host_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
static switch_bool_t switch_is_digit_string ( const char *  s)
inlinestatic

Definition at line 376 of file switch_utils.h.

References SWITCH_FALSE, and SWITCH_TRUE.

377 {
378 
379  while (s && *s) {
380  if (*s < 48 || *s > 57) {
381  return SWITCH_FALSE;
382  }
383  s++;
384  }
385 
386  return SWITCH_TRUE;
387 }
static switch_bool_t switch_is_file_path ( const char *  file)
inlinestatic

Definition at line 1098 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_gen_certs(), switch_img_txt_handle_create(), switch_ivr_displace_session(), switch_ivr_play_file(), switch_ivr_record_file(), switch_ivr_record_session(), switch_ivr_wait_for_answer(), switch_loadable_module_load_module_ex(), and switch_stream_write_file_contents().

1099 {
1100  const char *e;
1101  int r;
1102 
1103  if (zstr(file)) {
1104  return SWITCH_FALSE;
1105  }
1106 
1107  while(*file == '{') {
1108  if ((e = switch_find_end_paren(file, '{', '}'))) {
1109  file = e + 1;
1110  while(*file == ' ') file++;
1111  }
1112  }
1113 
1114 #ifdef WIN32
1115  r = (*file == '\\' || *(file + 1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR));
1116 #else
1117  r = ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR));
1118 #endif
1119 
1120  return r ? SWITCH_TRUE : SWITCH_FALSE;
1121 }
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:661
#define SWITCH_URL_SEPARATOR
Definition: switch_types.h:124
#define zstr(x)
Definition: switch_utils.h:281
switch_bool_t switch_is_lan_addr ( const char *  ip)

Definition at line 1217 of file switch_utils.c.

References SWITCH_FALSE, SWITCH_TRUE, and zstr.

1218 {
1219  if (zstr(ip))
1220  return SWITCH_FALSE;
1221 
1222  return (strncmp(ip, "10.", 3) && /* 10.0.0.0 - 10.255.255.255 (10/8 prefix) */
1223  strncmp(ip, "192.168.", 8) && /* 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) */
1224  strncmp(ip, "127.", 4) && /* 127.0.0.0 - 127.255.255.255 (127/8 prefix) */
1225  strncmp(ip, "255.", 4) &&
1226  strncmp(ip, "0.", 2) &&
1227  strncmp(ip, "1.", 2) &&
1228  strncmp(ip, "2.", 2) &&
1229  strncmp(ip, "172.16.", 7) && /* 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) */
1230  strncmp(ip, "172.17.", 7) &&
1231  strncmp(ip, "172.18.", 7) &&
1232  strncmp(ip, "172.19.", 7) &&
1233  strncmp(ip, "172.20.", 7) &&
1234  strncmp(ip, "172.21.", 7) &&
1235  strncmp(ip, "172.22.", 7) &&
1236  strncmp(ip, "172.23.", 7) &&
1237  strncmp(ip, "172.24.", 7) &&
1238  strncmp(ip, "172.25.", 7) &&
1239  strncmp(ip, "172.26.", 7) &&
1240  strncmp(ip, "172.27.", 7) &&
1241  strncmp(ip, "172.28.", 7) &&
1242  strncmp(ip, "172.29.", 7) &&
1243  strncmp(ip, "172.30.", 7) &&
1244  strncmp(ip, "172.31.", 7) &&
1245  strncmp(ip, "192.0.2.", 8) && /* 192.0.2.0 - 192.0.2.255 (192.0.2/24 prefix) */
1246  strncmp(ip, "169.254.", 8) /* 169.254.0.0 - 169.254.255.255 (169.254/16 prefix) */
1248 }
#define zstr(x)
Definition: switch_utils.h:281
switch_bool_t switch_is_leading_number ( const char *  str)

Definition at line 1460 of file switch_utils.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by switch_core_media_filter_sdp().

1461 {
1462  const char *p;
1464 
1465  if (*str == '-' || *str == '+') {
1466  str++;
1467  }
1468 
1469  for (p = str; p && *p; p++) {
1470  if ((*p == '.' || (*p > 47 && *p < 58))) {
1471  r = SWITCH_TRUE;
1472  break;
1473  }
1474  }
1475 
1476  return r;
1477 }
switch_bool_t
Definition: switch_types.h:405
static switch_bool_t switch_is_moh ( const char *  s)
inlinestatic

Definition at line 286 of file switch_utils.h.

References SWITCH_FALSE, SWITCH_TRUE, and zstr.

Referenced by switch_ivr_parse_event().

287 {
288  if (zstr(s) || !strcasecmp(s, "silence") || !strcasecmp(s, "indicate_hold")) {
289  return SWITCH_FALSE;
290  }
291  return SWITCH_TRUE;
292 }
#define zstr(x)
Definition: switch_utils.h:281
switch_bool_t switch_is_number ( const char *  str)

Definition at line 1441 of file switch_utils.c.

References SWITCH_FALSE, and SWITCH_TRUE.

Referenced by load_configuration(), switch_channel_perform_mark_answered(), switch_false(), switch_format_number(), 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().

1442 {
1443  const char *p;
1445 
1446  if (*str == '-' || *str == '+') {
1447  str++;
1448  }
1449 
1450  for (p = str; p && *p; p++) {
1451  if (!(*p == '.' || (*p > 47 && *p < 58))) {
1452  r = SWITCH_FALSE;
1453  break;
1454  }
1455  }
1456 
1457  return r;
1458 }
switch_bool_t
Definition: switch_types.h:405
int switch_isalnum ( int  c)

Definition at line 3610 of file switch_utils.c.

References _L, _N, and _U.

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

Definition at line 3615 of file switch_utils.c.

References _L, and _U.

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

Definition at line 3620 of file switch_utils.c.

References _C.

3621 {
3622  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _C));
3623 }
#define _C
const int * _switch_ctype_
int switch_isdigit ( int  c)

Definition at line 3625 of file switch_utils.c.

References _N.

3626 {
3627  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _N));
3628 }
#define _N
const int * _switch_ctype_
int switch_isgraph ( int  c)

Definition at line 3630 of file switch_utils.c.

References _L, _N, _P, and _U.

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

Definition at line 3635 of file switch_utils.c.

References _L.

3636 {
3637  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _L));
3638 }
#define _L
const int * _switch_ctype_
int switch_isprint ( int  c)

Definition at line 3640 of file switch_utils.c.

References _B, _L, _N, _P, and _U.

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

Definition at line 3645 of file switch_utils.c.

References _P.

3646 {
3647  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _P));
3648 }
#define _P
const int * _switch_ctype_
int switch_isspace ( int  c)

Definition at line 3650 of file switch_utils.c.

References _S.

3651 {
3652  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _S));
3653 }
#define _S
const int * _switch_ctype_
int switch_isupper ( int  c)

Definition at line 3655 of file switch_utils.c.

References _U.

3656 {
3657  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & _U));
3658 }
#define _U
const int * _switch_ctype_
int switch_isxdigit ( int  c)

Definition at line 3660 of file switch_utils.c.

References _N, and _X.

3661 {
3662  return (c < 0 ? 0 : c > 255 ? 0 : ((_switch_ctype_ + 1)[(unsigned char) c] & (_N | _X)));
3663 }
#define _X
#define _N
const int * _switch_ctype_
static char switch_itodtmf ( char  i)
inlinestatic

Definition at line 389 of file switch_utils.h.

Referenced by switch_ivr_bind_dtmf_meta_session().

390 {
391  char r = i;
392 
393  if (i > 9 && i < 14) {
394  r += 55;
395  } else {
396  r += 48;
397  }
398 
399  return r;
400 }
static uint32_t switch_known_bitrate ( switch_payload_t  payload)
inlinestatic

Definition at line 418 of file switch_utils.h.

Referenced by add_audio_codec(), and switch_core_media_negotiate_sdp().

419 {
420  switch(payload) {
421  case 0: /* PCMU */ return 64000;
422  case 3: /* GSM */ return 13200;
423  case 4: /* G723 */ return 6300;
424  case 7: /* LPC */ return 2400;
425  case 8: /* PCMA */ return 64000;
426  case 9: /* G722 */ return 64000;
427  case 18: /* G729 */ return 8000;
428  default: break;
429  }
430 
431  return 0;
432 }
static char* switch_lc_strdup ( const char *  it)
inlinestatic

Definition at line 804 of file switch_utils.h.

References switch_tolower().

805 {
806  char *dup;
807  char *p;
808 
809  if (it) {
810  dup = strdup(it);
811  for (p = dup; p && *p; p++) {
812  *p = (char) switch_tolower(*p);
813  }
814  return dup;
815  }
816 
817  return NULL;
818 }
static uint32_t switch_tolower(uint32_t eax)
Definition: switch_utils.h:81
static int switch_needs_url_encode ( const char *  s)
inlinestatic

Definition at line 1039 of file switch_utils.h.

References end_of_p, and SWITCH_URL_UNSAFE.

1040 {
1041  const char hex[] = "0123456789ABCDEF";
1042  const char *p, *e = end_of_p(s);
1043 
1044 
1045  for(p = s; p && *p; p++) {
1046  if (*p == '%' && e-p > 1) {
1047  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
1048  p++;
1049  continue;
1050  }
1051  }
1052 
1053  if (strchr(SWITCH_URL_UNSAFE, *p)) {
1054  return 1;
1055  }
1056  }
1057 
1058  return 0;
1059 }
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
#define end_of_p(_s)
Definition: switch_utils.h:617
char* switch_network_ipv4_mapped_ipv6_addr ( const char *  ip_str)

Definition at line 494 of file switch_utils.c.

Referenced by switch_check_network_list_ip_token(), and switch_network_list_perform_add_cidr_token().

495 {
496  /* ipv4 mapped ipv6 address */
497 
498  if (strncasecmp(ip_str, "::ffff:", 7)) {
499  return NULL;
500  }
501 
502  return strdup(ip_str + 7);
503 }
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 552 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().

553 {
554  char *cidr_str_dup = NULL;
556 
557  if (strchr(cidr_str, ',')) {
558  char *argv[32] = { 0 };
559  int i, argc;
560  cidr_str_dup = strdup(cidr_str);
561 
562  switch_assert(cidr_str_dup);
563  if ((argc = switch_separate_string(cidr_str_dup, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
564  for (i = 0; i < argc; i++) {
565  switch_status_t this_status;
566  if ((this_status = switch_network_list_perform_add_cidr_token(list, argv[i], ok, token)) != SWITCH_STATUS_SUCCESS) {
567  status = this_status;
568  }
569  }
570  }
571  } else {
572  status = switch_network_list_perform_add_cidr_token(list, cidr_str, ok, token);
573  }
574 
575  switch_safe_free(cidr_str_dup);
576  return status;
577 }
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)
Definition: switch_utils.c:505
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
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_add_host_mask ( switch_network_list_t list,
const char *  host,
const char *  mask_str,
switch_bool_t  ok 
)

Definition at line 579 of file switch_utils.c.

References switch_network_node::bits, switch_network_node::ip, switch_network_node::mask, switch_network_node::next, switch_network_node::ok, 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().

580 {
581  ip_t ip, mask;
582  switch_network_node_t *node;
583 
584  switch_inet_pton(AF_INET, host, &ip);
585  switch_inet_pton(AF_INET, mask_str, &mask);
586 
587  node = switch_core_alloc(list->pool, sizeof(*node));
588 
589  node->ip.v4 = ntohl(ip.v4);
590  node->mask.v4 = ntohl(mask.v4);
591  node->ok = ok;
592 
593  /* http://graphics.stanford.edu/~seander/bithacks.html */
594  mask.v4 = mask.v4 - ((mask.v4 >> 1) & 0x55555555);
595  mask.v4 = (mask.v4 & 0x33333333) + ((mask.v4 >> 2) & 0x33333333);
596  node->bits = (((mask.v4 + (mask.v4 >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
597 
598  node->str = switch_core_sprintf(list->pool, "%s:%s", host, mask_str);
599 
600  node->next = list->node_head;
601  list->node_head = node;
602 
603  return SWITCH_STATUS_SUCCESS;
604 }
struct switch_network_node * node_head
Definition: switch_utils.c:59
struct switch_network_node * next
Definition: switch_utils.c:54
uint32_t v4
Definition: switch_utils.h:247
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_memory_pool_t * pool
Definition: switch_utils.c:61
int switch_inet_pton(int af, const char *src, void *dst)
Definition: switch_utils.c:66
switch_bool_t ok
Definition: switch_utils.c:51
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_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 404 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().

406 {
407  switch_network_list_t *new_list;
408 
409  if (!pool) {
411  }
412 
413  new_list = switch_core_alloc(pool, sizeof(**list));
414  new_list->pool = pool;
415  new_list->default_type = default_type;
416  new_list->name = switch_core_strdup(new_list->pool, name);
417 
418  *list = new_list;
419 
420  return SWITCH_STATUS_SUCCESS;
421 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core's master pool.
Definition: switch_core.h:631
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_memory_pool_t * pool
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
switch_memory_pool_t * pool
Definition: switch_utils.c:61
switch_bool_t default_type
Definition: switch_utils.c:60
switch_bool_t switch_network_list_validate_ip6_token ( switch_network_list_t list,
ip_t  ip,
const char **  token 
)

Definition at line 441 of file switch_utils.c.

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

Referenced by switch_check_network_list_ip_token().

442 {
443  switch_network_node_t *node;
444  switch_bool_t ok = list->default_type;
445  uint32_t bits = 0;
446 
447  for (node = list->node_head; node; node = node->next) {
448  if (node->family == AF_INET) continue;
449 
450  if (node->bits >= bits && switch_testv6_subnet(ip, node->ip, node->mask)) {
451  if (node->ok) {
452  ok = SWITCH_TRUE;
453  } else {
454  ok = SWITCH_FALSE;
455  }
456 
457  bits = node->bits;
458 
459  if (token) {
460  *token = node->token;
461  }
462  }
463  }
464 
465  return ok;
466 }
struct switch_network_node * node_head
Definition: switch_utils.c:59
switch_bool_t
Definition: switch_types.h:405
struct switch_network_node * next
Definition: switch_utils.c:54
switch_bool_t switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask)
Definition: switch_utils.c:428
switch_bool_t default_type
Definition: switch_utils.c:60
switch_bool_t ok
Definition: switch_utils.c:51
switch_bool_t switch_network_list_validate_ip_token ( switch_network_list_t list,
uint32_t  ip,
const char **  token 
)

Definition at line 468 of file switch_utils.c.

References switch_network_node::bits, switch_network_node::family, switch_network_node::ip, switch_network_node::mask, switch_network_node::next, 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_token().

469 {
470  switch_network_node_t *node;
471  switch_bool_t ok = list->default_type;
472  uint32_t bits = 0;
473 
474  for (node = list->node_head; node; node = node->next) {
475  if (node->family == AF_INET6) continue; /* want AF_INET */
476  if (node->bits >= bits && switch_test_subnet(ip, node->ip.v4, node->mask.v4)) {
477  if (node->ok) {
478  ok = SWITCH_TRUE;
479  } else {
480  ok = SWITCH_FALSE;
481  }
482 
483  bits = node->bits;
484 
485  if (token) {
486  *token = node->token;
487  }
488  }
489  }
490 
491  return ok;
492 }
struct switch_network_node * node_head
Definition: switch_utils.c:59
#define switch_test_subnet(_ip, _net, _mask)
switch_bool_t
Definition: switch_types.h:405
struct switch_network_node * next
Definition: switch_utils.c:54
uint32_t v4
Definition: switch_utils.h:247
switch_bool_t default_type
Definition: switch_utils.c:60
switch_bool_t ok
Definition: switch_utils.c:51
int switch_number_cmp ( const char *  exp,
int  val 
)

Definition at line 3765 of file switch_utils.c.

Referenced by switch_xml_std_datetime_check().

3766 {
3767  // Expression exp must be a comma separated list of numbers or ranges.
3768  // To match numbers not in range 9-17, enter the reversed range 18-8.
3769  for (;; ++exp) {
3770  int a = strtol(exp, (char **)&exp, 10);
3771  if (*exp != '-') {
3772  if (a == val)
3773  return 1;
3774  } else {
3775  int b = strtol(++exp, (char **)&exp, 10);
3776  if (a <= b ? (val >= a && val <=b ) : (val >= a || val <= b))
3777  return 1;
3778  }
3779  if (*exp != ',')
3780  return 0;
3781  }
3782 }
static const char* switch_parse_audio_col ( switch_audio_col_t  col)
inlinestatic

Definition at line 1124 of file switch_utils.h.

References 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, and SWITCH_AUDIO_COL_STR_TITLE.

1125 {
1126  const char *field = NULL;
1127 
1128  switch (col) {
1130  field = "title";
1131  break;
1133  field = "comment";
1134  break;
1136  field = "artist";
1137  break;
1139  field = "date";
1140  break;
1142  field = "software";
1143  break;
1145  field = "copyright";
1146  break;
1147  default:
1148  break;
1149  }
1150 
1151  return field;
1152 }
static int32_t switch_parse_bandwidth_string ( const char *  bwv)
inlinestatic

Definition at line 1014 of file switch_utils.h.

Referenced by switch_core_media_gen_local_sdp(), and switch_core_session_parse_codec_settings().

1015 {
1016  float bw = 0;
1017 
1018  if (!bwv) return 0;
1019 
1020  if (!strcasecmp(bwv, "auto")) {
1021  return -1;
1022  }
1023 
1024  if ((bw = (float) atof(bwv))) {
1025  if (bw < 0) return 0;
1026 
1027  if (strstr(bwv, "KB")) {
1028  bw *= 8;
1029  } else if (strstr(bwv, "mb")) {
1030  bw *= 1024;
1031  } else if (strstr(bwv, "MB")) {
1032  bw *= 8192;
1033  }
1034  }
1035 
1036  return (int32_t) roundf(bw);
1037 }
int switch_parse_cidr ( const char *  string,
ip_t ip,
ip_t mask,
uint32_t *  bitp 
)

Definition at line 607 of file switch_utils.c.

References switch_copy_string(), switch_inet_pton(), ip_t::v4, and ip_t::v6.

Referenced by switch_check_network_list_ip_token(), and switch_network_list_perform_add_cidr_token().

608 {
609  char host[128];
610  char *bit_str;
611  int32_t bits;
612  const char *ipv6;
613  ip_t *maskv = mask;
614  ip_t *ipv = ip;
615 
616  switch_copy_string(host, string, sizeof(host)-1);
617  bit_str = strchr(host, '/');
618 
619  if (!bit_str) {
620  return -1;
621  }
622 
623  *bit_str++ = '\0';
624  bits = atoi(bit_str);
625  ipv6 = strchr(string, ':');
626  if (ipv6) {
627  int i,n;
628  if (bits < 0 || bits > 128) {
629  return -2;
630  }
631  bits = atoi(bit_str);
632  switch_inet_pton(AF_INET6, host, (unsigned char *)ip);
633  for (n=bits,i=0 ;i < 16; i++){
634  if (n >= 8) {
635  maskv->v6.s6_addr[i] = 0xFF;
636  n -= 8;
637  } else if (n < 8) {
638  maskv->v6.s6_addr[i] = 0xFF & ~(0xFF >> n);
639  n -= n;
640  } else if (n == 0) {
641  maskv->v6.s6_addr[i] = 0x00;
642  }
643  }
644  } else {
645  if (bits < 0 || bits > 32) {
646  return -2;
647  }
648 
649  bits = atoi(bit_str);
650  switch_inet_pton(AF_INET, host, (unsigned char *)ip);
651  ipv->v4 = htonl(ipv->v4);
652 
653  maskv->v4 = 0xFFFFFFFF & ~(0xFFFFFFFF >> bits);
654  }
655  *bitp = bits;
656 
657  return 0;
658 }
uint32_t v4
Definition: switch_utils.h:247
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
struct in6_addr v6
Definition: switch_utils.h:248
int switch_inet_pton(int af, const char *src, void *dst)
Definition: switch_utils.c:66
char* switch_print_host ( switch_sockaddr_t addr,
char *  buf,
switch_size_t  len 
)

Definition at line 72 of file switch_utils.c.

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

73 {
74  switch_port_t port;
75 
76  switch_get_addr(buf, len, addr);
77  port = switch_sockaddr_get_port(addr);
78 
79  snprintf(buf + strlen(buf), len - strlen(buf), ":%d", port);
80  return buf;
81 }
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
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:894
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 1956 of file switch_utils.c.

References SWITCH_PRIORITY_HIGH, SWITCH_PRIORITY_LOW, and SWITCH_PRIORITY_NORMAL.

Referenced by switch_event_set_priority().

1957 {
1958  switch (priority) { /*lol */
1960  return "NORMAL";
1961  case SWITCH_PRIORITY_LOW:
1962  return "LOW";
1963  case SWITCH_PRIORITY_HIGH:
1964  return "HIGH";
1965  default:
1966  return "INVALID";
1967  }
1968 }
char* switch_replace_char ( char *  str,
char  from,
char  to,
switch_bool_t  dup 
)

Definition at line 1281 of file switch_utils.c.

References switch_assert.

1282 {
1283  char *p;
1284 
1285  if (dup) {
1286  p = strdup(str);
1287  switch_assert(p);
1288  } else {
1289  p = str;
1290  }
1291 
1292  for (; p && *p; p++) {
1293  if (*p == from) {
1294  *p = to;
1295  }
1296  }
1297 
1298  return p;
1299 }
#define switch_assert(expr)
switch_status_t switch_resolve_host ( const char *  host,
char *  buf,
size_t  buflen 
)

Definition at line 1647 of file switch_utils.c.

References get_addr(), SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

1648 {
1649 
1650  struct addrinfo *ai;
1651  int err;
1652 
1653  if ((err = getaddrinfo(host, 0, 0, &ai))) {
1654  return SWITCH_STATUS_FALSE;
1655  }
1656 
1657  get_addr(buf, buflen, ai->ai_addr, sizeof(*ai->ai_addr));
1658 
1659  freeaddrinfo(ai);
1660 
1661  return SWITCH_STATUS_SUCCESS;
1662 }
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
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 2327 of file switch_utils.c.

Referenced by handle_rfc2833().

2328 {
2329  if (event > -1 && event < (int32_t) sizeof(RFC2833_CHARS)) {
2330  return RFC2833_CHARS[event];
2331  }
2332  return '\0';
2333 }
static char RFC2833_CHARS[]
static uint32_t switch_round_to_step ( uint32_t  num,
uint32_t  step 
)
inlinestatic

Definition at line 50 of file switch_utils.h.

Referenced by switch_core_media_get_video_fps().

51 {
52  uint32_t r;
53  uint32_t x;
54 
55  if (!num) return 0;
56 
57  r = (num % step);
58  x = num - r;
59 
60  if (r > step / 2) {
61  x += step;
62  }
63 
64  return x;
65 }
static int switch_safe_atoi ( const char *  nptr,
int  dft 
)
inlinestatic

Turn a string into a number (default if NULL)

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

Definition at line 779 of file switch_utils.h.

Referenced by switch_core_media_process_udptl().

780 {
781  return nptr ? atoi(nptr) : dft;
782 }
static char* switch_safe_strdup ( const char *  it)
inlinestatic

Definition at line 791 of file switch_utils.h.

792 {
793  if (it) {
794  return strdup(it);
795  }
796 
797  return NULL;
798 }
static char* switch_sanitize_number ( char *  number)
inlinestatic

Definition at line 668 of file switch_utils.h.

References switch_assert.

Referenced by switch_ivr_originate().

669 {
670  char *p = number, *q;
671  char warp[] = "/:";
672  int i;
673 
674  switch_assert(number);
675 
676  if (!(strchr(p, '/') || strchr(p, ':') || strchr(p, '@'))) {
677  return number;
678  }
679 
680  while ((q = strrchr(p, '@')))
681  *q = '\0';
682 
683  for (i = 0; i < (int) strlen(warp); i++) {
684  while (p && (q = strchr(p, warp[i])))
685  p = q + 1;
686  }
687 
688  return p;
689 }
#define switch_assert(expr)
static void switch_separate_file_params ( const char *  file,
char **  file_portion,
char **  params_portion 
)
inlinestatic

Definition at line 1070 of file switch_utils.h.

References switch_find_end_paren().

1071 {
1072  char *e = NULL;
1073  char *space = strdup(file);
1074 
1075  file = space;
1076 
1077  *file_portion = NULL;
1078  *params_portion = NULL;
1079 
1080  while (*file == '{') {
1081  e = switch_find_end_paren(file, '{', '}');
1082  file = e + 1;
1083  while(*file == ' ') file++;
1084  }
1085 
1086 
1087  if (e) {
1088  *file_portion = strdup((char *)file);
1089  *++e = '\0';
1090  *params_portion = (char *)space;
1091  } else {
1092  *file_portion = (char *)space;
1093  }
1094 
1095  return;
1096 }
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:661
char* switch_separate_paren_args ( char *  str)

Definition at line 1412 of file switch_utils.c.

Referenced by switch_ivr_phrase_macro_event().

1413 {
1414  char *e, *args;
1415  switch_size_t br;
1416 
1417  if ((args = strchr(str, '('))) {
1418  e = args - 1;
1419  *args++ = '\0';
1420  while (*e == ' ') {
1421  *e-- = '\0';
1422  }
1423  e = args;
1424  br = 1;
1425  while (e && *e) {
1426  if (*e == '(') {
1427  br++;
1428  } else if (br > 1 && *e == ')') {
1429  br--;
1430  } else if (br == 1 && *e == ')') {
1431  *e = '\0';
1432  break;
1433  }
1434  e++;
1435  }
1436  }
1437 
1438  return args;
1439 }
uintptr_t switch_size_t
unsigned int switch_separate_string ( _In_ char *  buf,
char  delim,
_Post_count_(return) char **  array,
unsigned int  arraylen 
)
unsigned int switch_separate_string_string ( char *  buf,
char *  delim,
_Post_count_(return) char **  array,
unsigned int  arraylen 
)
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 981 of file switch_utils.c.

References 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(), and zstr.

Referenced by email().

985 {
986  char *bound = "XXXX_boundary_XXXX";
987  const char *mime_type = "audio/inline";
988  char filename[80], buf[B64BUFFLEN];
989  int fd = -1, ifd = -1;
990  int x = 0, y = 0, bytes = 0, ilen = 0;
991  unsigned int b = 0, l = 0;
992  unsigned char in[B64BUFFLEN];
993  unsigned char out[B64BUFFLEN + 512];
994  char *dupfile = NULL, *ext = NULL;
995  char *newfile = NULL;
997  const char *err = NULL;
998 
999  if (zstr(to)) {
1000  err = "No to address specified";
1001  goto end;
1002  }
1003 
1004  if (!zstr(file) && !zstr(convert_cmd) && !zstr(convert_ext)) {
1005  if ((ext = strrchr(file, '.'))) {
1006  dupfile = strdup(file);
1007  if ((ext = strrchr(dupfile, '.'))) {
1008  *ext++ = '\0';
1009  newfile = switch_mprintf("%s.%s", dupfile, convert_ext);
1010  }
1011  }
1012 
1013  if (newfile) {
1014  char cmd[1024] = "";
1015  switch_snprintf(cmd, sizeof(cmd), "%s %s %s", convert_cmd, file, newfile);
1016  switch_system(cmd, SWITCH_TRUE);
1017  if (strcmp(file, newfile)) {
1018  file = newfile;
1019  } else {
1020  switch_safe_free(newfile);
1021  }
1022  }
1023 
1024  switch_safe_free(dupfile);
1025  }
1026 
1027  switch_snprintf(filename, 80, "%s%smail.%d%04x", SWITCH_GLOBAL_dirs.temp_dir, SWITCH_PATH_SEPARATOR, (int) switch_epoch_time_now(NULL), rand() & 0xffff);
1028 
1029  if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644)) > -1) {
1030  if (file) {
1031  if ((ifd = open(file, O_RDONLY | O_BINARY)) < 0) {
1032  rval = SWITCH_FALSE;
1033  err = "Cannot open tmp file\n";
1034  goto end;
1035  }
1036  }
1037 
1038  if (!file && (!body || !switch_stristr("content-type", body))) {
1039  bound = NULL;
1040  }
1041 
1042  if (bound) {
1043  switch_snprintf(buf, B64BUFFLEN, "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n", bound);
1044  if (!write_buf(fd, buf)) {
1045  rval = SWITCH_FALSE;
1046  err = "write error.";
1047  goto end;
1048  }
1049  }
1050 
1051  if (headers && !write_buf(fd, headers)) {
1052  rval = SWITCH_FALSE;
1053  err = "write error.";
1054  goto end;
1055  }
1056 
1057  if (!write_buf(fd, "\n\n")) {
1058  rval = SWITCH_FALSE;
1059  err = "write error.";
1060  goto end;
1061  }
1062 
1063  if (bound) {
1064  if (body && switch_stristr("content-type", body)) {
1065  switch_snprintf(buf, B64BUFFLEN, "--%s\n", bound);
1066  } else {
1067  switch_snprintf(buf, B64BUFFLEN, "--%s\nContent-Type: text/plain\n\n", bound);
1068  }
1069  if (!write_buf(fd, buf)) {
1070  rval = SWITCH_FALSE;
1071  err = "write error.";
1072  goto end;
1073  }
1074  }
1075 
1076  if (body) {
1077  if (!write_buf(fd, body)) {
1078  rval = SWITCH_FALSE;
1079  err = "write error.";
1080  goto end;
1081  }
1082  }
1083 
1084  if (file && bound) {
1085  const char *stipped_file = switch_cut_path(file);
1086  const char *new_type;
1087  char *ext;
1088 
1089  if ((ext = strrchr(stipped_file, '.'))) {
1090  ext++;
1091  if ((new_type = switch_core_mime_ext2type(ext))) {
1092  mime_type = new_type;
1093  }
1094  }
1095 
1097  "\n\n--%s\nContent-Type: %s; name=\"%s\"\n"
1098  "Content-ID: <ATTACHED@freeswitch.org>\n"
1099  "Content-Transfer-Encoding: base64\n"
1100  "Content-Description: Sound attachment.\n"
1101  "Content-Disposition: attachment; filename=\"%s\"\n\n", bound, mime_type, stipped_file, stipped_file);
1102  if (!write_buf(fd, buf)) {
1103  rval = SWITCH_FALSE;
1104  err = "write error.";
1105  goto end;
1106  }
1107 
1108  while ((ilen = read(ifd, in, B64BUFFLEN))) {
1109  for (x = 0; x < ilen; x++) {
1110  b = (b << 8) + in[x];
1111  l += 8;
1112  while (l >= 6) {
1113  out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64];
1114  if (++y != 72)
1115  continue;
1116  out[bytes++] = '\n';
1117  y = 0;
1118  }
1119  }
1120  if (write(fd, &out, bytes) != bytes) {
1121  rval = -1;
1122  break;
1123  } else {
1124  bytes = 0;
1125  }
1126 
1127  }
1128 
1129  if (l > 0) {
1130  out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64];
1131  }
1132  if (l != 0)
1133  while (l < 6) {
1134  out[bytes++] = '=', l += 2;
1135  }
1136  if (write(fd, &out, bytes) != bytes) {
1137  rval = -1;
1138  }
1139 
1140  }
1141 
1142  if (bound) {
1143  switch_snprintf(buf, B64BUFFLEN, "\n\n--%s--\n.\n", bound);
1144 
1145  if (!write_buf(fd, buf)) {
1146  rval = SWITCH_FALSE;
1147  err = "write error.";
1148  goto end;
1149  }
1150  }
1151  }
1152 
1153  if (fd > -1) {
1154  close(fd);
1155  fd = -1;
1156  }
1157 
1158  if (zstr(from)) {
1159  from = "freeswitch";
1160  }
1161 
1162  {
1163  char *to_arg = switch_util_quote_shell_arg(to);
1164  char *from_arg = switch_util_quote_shell_arg(from);
1165 #ifdef WIN32
1166  switch_snprintf(buf, B64BUFFLEN, "\"\"%s\" -f %s %s %s < \"%s\"\"", runtime.mailer_app, from_arg, runtime.mailer_app_args, to_arg, filename);
1167 #else
1168  switch_snprintf(buf, B64BUFFLEN, "/bin/cat %s | %s -f %s %s %s", filename, runtime.mailer_app, from_arg, runtime.mailer_app_args, to_arg);
1169 #endif
1170  switch_safe_free(to_arg); switch_safe_free(from_arg);
1171  }
1172  if (switch_system(buf, SWITCH_TRUE) < 0) {
1173  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf);
1174  err = "execute error";
1175  rval = SWITCH_FALSE;
1176  }
1177 
1178  if (zstr(err)) {
1179  if (file) {
1180  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed file [%s] to [%s]\n", filename, to);
1181  } else {
1182  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed data to [%s]\n", to);
1183  }
1184 
1185  rval = SWITCH_TRUE;
1186  }
1187 
1188  end:
1189 
1190  if (fd > -1) {
1191  close(fd);
1192  }
1193 
1194  if (unlink(filename) != 0) {
1195  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to delete file [%s]\n", filename);
1196  }
1197 
1198  if (ifd > -1) {
1199  close(ifd);
1200  }
1201 
1202 
1203  if (newfile) {
1204  unlink(newfile);
1205  free(newfile);
1206  }
1207 
1208  if (rval != SWITCH_TRUE) {
1209  if (zstr(err)) err = "Unknown Error";
1210 
1211  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "EMAIL NOT SENT, error [%s]\n", err);
1212  }
1213 
1214  return rval;
1215 }
#define SWITCH_CHANNEL_LOG
#define O_BINARY
static const char switch_b64_table[65]
Definition: switch_utils.c:888
switch_bool_t
Definition: switch_types.h:405
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:64
#define zstr(x)
Definition: switch_utils.h:281
int switch_system(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3194
#define SWITCH_PATH_SEPARATOR
Definition: switch_types.h:122
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:789
char * switch_util_quote_shell_arg(const char *string)
Quote shell argument.
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
static int write_buf(int fd, const char *buf)
Definition: switch_utils.c:969
#define B64BUFFLEN
Definition: switch_utils.c:889
const char * switch_core_mime_ext2type(const char *ext)
Definition: switch_core.c:1199
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
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)
char * filename
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
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 3193 of file switch_utils.c.

References switch_poll().

3194 {
3195  int nsds = 0;
3196 
3197  switch_poll(poll, 1, &nsds, ms);
3198 
3199  return nsds;
3200 }
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1015
void switch_split_date ( const char *  exp,
int *  year,
int *  month,
int *  day 
)

Definition at line 3306 of file switch_utils.c.

References switch_assert, and switch_safe_free.

Referenced by switch_fulldate_cmp().

3307 {
3308  char *dup = strdup(exp);
3309  char *syear = NULL;
3310  char *smonth = NULL;
3311  char *sday = NULL;
3312 
3313  switch_assert(dup);
3314 
3315  syear = dup;
3316  if ((smonth=strchr(dup, '-'))) {
3317  *smonth++ = '\0';
3318  if ((sday=strchr(smonth, '-'))) {
3319  *sday++ = '\0';
3320  if (year && syear) {
3321  *year = atol(syear);
3322  }
3323  if (month && smonth) {
3324  *month = atol(smonth);
3325  }
3326  if (day && sday) {
3327  *day = atol(sday);
3328  }
3329  }
3330  }
3331  switch_safe_free(dup);
3332  return;
3333 
3334 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_assert(expr)
void switch_split_time ( const char *  exp,
int *  hour,
int *  min,
int *  sec 
)

Definition at line 3273 of file switch_utils.c.

References switch_assert, and switch_safe_free.

Referenced by switch_fulldate_cmp().

3274 {
3275  char *dup = strdup(exp);
3276  char *shour = NULL;
3277  char *smin = NULL;
3278  char *ssec = NULL;
3279 
3280  switch_assert(dup);
3281 
3282  shour = dup;
3283  if ((smin=strchr(dup, ':'))) {
3284  *smin++ = '\0';
3285  if ((ssec=strchr(smin, ':'))) {
3286  *ssec++ = '\0';
3287  } else {
3288  ssec = "00";
3289  }
3290  if (hour && shour) {
3291  *hour = atol(shour);
3292  }
3293  if (min && smin) {
3294  *min = atol(smin);
3295  }
3296  if (sec && ssec) {
3297  *sec = atol(ssec);
3298  }
3299 
3300  }
3301  switch_safe_free(dup);
3302  return;
3303 
3304 }
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_assert(expr)
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 3848 of file switch_utils.c.

References in.

3849 {
3850  char *p = NULL, *h = NULL, *u = NULL;
3851 
3852  if (!in) return 0;
3853 
3854  /* Remove URL scheme */
3855  if (!strncasecmp(in, "sip:", 4)) in += 4;
3856  else if (!strncasecmp(in, "sips:", 5)) in += 5;
3857 
3858  /* Isolate the host part from the user part */
3859  if ((h = in, p = strchr(h, '@'))) *p = '\0', u = in, h = p+1;
3860 
3861  /* Clean out the host part of any suffix */
3862  for (p = h; *p; p++)
3863  if (*p == ':' || *p == ';' || *p == ' ') {
3864  *p = '\0'; break;
3865  }
3866 
3867  if (user) *user = u;
3868  if (domain) *domain = h;
3869  return 1;
3870 }
switch_byte_t in
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 1890 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.

1891 {
1892  switch_time_exp_t tm = { 0 }, local_tm = { 0 };
1893  int proceed = 0, ovector[30];
1894  switch_regex_t *re = NULL;
1895  char replace[1024] = "";
1896  switch_time_t ret = 0, local_time = 0;
1897  char *pattern = "^(\\d+)-(\\d+)-(\\d+)\\s*(\\d*):{0,1}(\\d*):{0,1}(\\d*)";
1898  char *pattern2 = "^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
1899 
1901  tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = tm.tm_usec = 0;
1902 
1903  if (!(proceed = switch_regex_perform(in, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
1905  proceed = switch_regex_perform(in, pattern2, &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
1906  }
1907 
1908  if (proceed) {
1909 
1910  if (proceed > 1) {
1911  switch_regex_copy_substring(in, ovector, proceed, 1, replace, sizeof(replace));
1912  tm.tm_year = atoi(replace) - 1900;
1913  }
1914 
1915  if (proceed > 2) {
1916  switch_regex_copy_substring(in, ovector, proceed, 2, replace, sizeof(replace));
1917  tm.tm_mon = atoi(replace) - 1;
1918  }
1919 
1920  if (proceed > 3) {
1921  switch_regex_copy_substring(in, ovector, proceed, 3, replace, sizeof(replace));
1922  tm.tm_mday = atoi(replace);
1923  }
1924 
1925  if (proceed > 4) {
1926  switch_regex_copy_substring(in, ovector, proceed, 4, replace, sizeof(replace));
1927  tm.tm_hour = atoi(replace);
1928  }
1929 
1930  if (proceed > 5) {
1931  switch_regex_copy_substring(in, ovector, proceed, 5, replace, sizeof(replace));
1932  tm.tm_min = atoi(replace);
1933  }
1934 
1935  if (proceed > 6) {
1936  switch_regex_copy_substring(in, ovector, proceed, 6, replace, sizeof(replace));
1937  tm.tm_sec = atoi(replace);
1938  }
1939 
1941 
1942  switch_time_exp_get(&local_time, &tm);
1943  switch_time_exp_lt(&local_tm, local_time);
1944  tm.tm_isdst = local_tm.tm_isdst;
1945  tm.tm_gmtoff = local_tm.tm_gmtoff;
1946 
1947  switch_time_exp_gmt_get(&ret, &tm);
1948  return ret;
1949  }
1950 
1952 
1953  return ret;
1954 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
#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:318
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:323
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:313
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
int switch_strcasecmp_any ( const char *  str,
  ... 
)

Definition at line 339 of file switch_utils.c.

340 {
341  va_list ap;
342  const char *next_str = 0;
343  int r = 0;
344 
345  va_start(ap, str);
346 
347  while ((next_str = va_arg(ap, const char *))) {
348  if (!strcasecmp(str, next_str)) {
349  r = 1;
350  break;
351  }
352  }
353 
354  va_end(ap);
355 
356  return r;
357 }
static char* switch_strchr_strict ( const char *  in,
char  find,
const char *  allowed 
)
inlinestatic

Definition at line 301 of file switch_utils.h.

References in, and switch_assert.

Referenced by switch_event_create_brackets(), and switch_ivr_originate().

302 {
303  const char *p;
304 
305  switch_assert(in);
306 
307  p = in;
308 
309  if (!*p) return NULL;
310 
311  while(p && *p) {
312  const char *a = allowed;
313  int acceptable = 0;
314 
315  if (*p == find) break;
316 
317  if (!a) {
318  acceptable = 1;
319  } else {
320 
321  while(a && *a) {
322 
323  if (*p == *a) {
324  acceptable = 1;
325  break;
326  }
327 
328  a++;
329  }
330 
331  }
332 
333  if (!acceptable) return NULL;
334 
335  p++;
336  }
337 
338  return (char *) p;
339 }
switch_byte_t in
#define switch_assert(expr)
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
both]buf Buffer for error message
[in]buflenSize of message buffer
Returns
Pointer to message buffer, returning error message or "Unknown error xxx" if none found

Definition at line 3940 of file switch_utils.c.

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

Referenced by reincarnate_protect(), and rtp_common_read().

3941 {
3942 #ifdef HAVE_STRERROR_R
3943 #ifdef STRERROR_R_CHAR_P
3944  /* GNU variant returning char *, avoids warn-unused-result error */
3945  return strerror_r(errnum, buf, buflen);
3946 #else
3947  /*
3948  * XSI variant returning int, with GNU compatible error string,
3949  * if no message could be found
3950  */
3951  if (strerror_r(errnum, buf, buflen)) {
3952  switch_snprintf(buf, buflen, "Unknown error %d", errnum);
3953  }
3954  return buf;
3955 #endif /* STRERROR_R_CHAR_P */
3956 #elif defined(WIN32)
3957  /* WIN32 variant */
3958  if (strerror_s(buf, buflen, errnum)) {
3959  switch_snprintf(buf, buflen, "Unknown error %d", errnum);
3960  }
3961  return buf;
3962 #else
3963  /* Fallback, copy string into private buffer */
3964  switch_copy_string(buf, strerror(errnum), buflen);
3965  return buf;
3966 #endif
3967 }
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)
static int switch_string_has_escaped_data ( const char *  in)
inlinestatic

Definition at line 350 of file switch_utils.h.

References switch_assert.

Referenced by switch_channel_expand_variables_check(), and switch_event_expand_headers_check().

351 {
352  const char *i;
353 
354  switch_assert(in);
355 
356  i = strchr(in, '\\');
357 
358  while (i && *i == '\\') {
359  i++;
360  if (*i == '\\' || *i == 'n' || *i == 's' || *i == 't' || *i == '\'') {
361  return 1;
362  }
363  i = strchr(i, '\\');
364  }
365 
366  return 0;
367 }
switch_byte_t in
#define switch_assert(expr)
switch_status_t switch_string_match ( const char *  string,
size_t  string_len,
const char *  search,
size_t  search_len 
)

Definition at line 2677 of file switch_utils.c.

References SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_string_replace().

2678 {
2679  size_t i;
2680 
2681  for (i = 0; (i < search_len) && (i < string_len); i++) {
2682  if (string[i] != search[i]) {
2683  return SWITCH_STATUS_FALSE;
2684  }
2685  }
2686 
2687  if (i == search_len) {
2688  return SWITCH_STATUS_SUCCESS;
2689  }
2690 
2691  return SWITCH_STATUS_FALSE;
2692 }
char* switch_string_replace ( const char *  string,
const char *  search,
const char *  replace 
)

Definition at line 2694 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().

2695 {
2696  size_t string_len = strlen(string);
2697  size_t search_len = strlen(search);
2698  size_t replace_len = strlen(replace);
2699  size_t i, n;
2700  size_t dest_len = 0;
2701  char *dest, *tmp;
2702 
2703  dest = (char *) malloc(sizeof(char));
2704  switch_assert(dest);
2705 
2706  for (i = 0; i < string_len; i++) {
2707  if (switch_string_match(string + i, string_len - i, search, search_len) == SWITCH_STATUS_SUCCESS) {
2708  for (n = 0; n < replace_len; n++) {
2709  dest[dest_len] = replace[n];
2710  dest_len++;
2711  tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
2712  switch_assert(tmp);
2713  dest = tmp;
2714  }
2715  i += search_len - 1;
2716  } else {
2717  dest[dest_len] = string[i];
2718  dest_len++;
2719  tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
2720  switch_assert(tmp);
2721  dest = tmp;
2722  }
2723  }
2724 
2725  dest[dest_len] = 0;
2726  return dest;
2727 }
switch_status_t switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len)
#define switch_assert(expr)
static switch_bool_t switch_string_var_check ( char *  s,
switch_bool_t  disable 
)
inlinestatic

Definition at line 691 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().

692 {
693  char *p;
694  char *dol = NULL;
695 
696  for (p = s; p && *p; p++) {
697  if (*p == '$') {
698  dol = p;
699  } else if (dol) {
700  if (*p == '{') {
701  if (disable) {
702  *dol = '%';
703  dol = NULL;
704  } else {
705  return SWITCH_TRUE;
706  }
707  } else if (*p != '\\') {
708  dol = NULL;
709  }
710  }
711  }
712  return SWITCH_FALSE;
713 }
static switch_bool_t switch_string_var_check_const ( const char *  s)
inlinestatic

Definition at line 716 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_var_check(), and switch_event_expand_headers_check().

717 {
718  const char *p;
719  int dol = 0;
720 
721  for (p = s; p && *p; p++) {
722  if (*p == '$') {
723  dol = 1;
724  } else if (dol) {
725  if (*p == '{') {
726  return SWITCH_TRUE;
727  } else if (*p != '\\') {
728  dol = 0;
729  }
730  }
731  }
732  return SWITCH_FALSE;
733 }
char* switch_strip_commas ( char *  in,
char *  out,
switch_size_t  len 
)

Definition at line 1369 of file switch_utils.c.

References in.

1370 {
1371  char *p = in, *q = out;
1372  char *ret = out;
1373  switch_size_t x = 0;
1374 
1375  for (; p && *p; p++) {
1376  if ((*p > 47 && *p < 58)) {
1377  *q++ = *p;
1378  } else if (*p != ',') {
1379  ret = NULL;
1380  break;
1381  }
1382 
1383  if (++x > len) {
1384  ret = NULL;
1385  break;
1386  }
1387  }
1388 
1389  return ret;
1390 }
switch_byte_t in
uintptr_t switch_size_t
char* switch_strip_nonnumerics ( char *  in,
char *  out,
switch_size_t  len 
)

Definition at line 1392 of file switch_utils.c.

References in.

1393 {
1394  char *p = in, *q = out;
1395  char *ret = out;
1396  switch_size_t x = 0;
1397  /* valid are 0 - 9, period (.), minus (-), and plus (+) - remove all others */
1398  for (; p && *p; p++) {
1399  if ((*p > 47 && *p < 58) || *p == '.' || *p == '-' || *p == '+') {
1400  *q++ = *p;
1401  }
1402 
1403  if (++x > len) {
1404  ret = NULL;
1405  break;
1406  }
1407  }
1408 
1409  return ret;
1410 }
switch_byte_t in
uintptr_t switch_size_t
char* switch_strip_spaces ( char *  str,
switch_bool_t  dup 
)

Definition at line 1333 of file switch_utils.c.

References switch_assert, SWITCH_BLANK_STRING, and zstr.

1334 {
1335  char *sp = str;
1336  char *p, *s = NULL;
1337  size_t len;
1338 
1339  if (zstr(sp)) {
1340  return dup ? strdup(SWITCH_BLANK_STRING) : sp;
1341  }
1342 
1343  while (*sp == ' ') {
1344  sp++;
1345  }
1346 
1347  if (dup) {
1348  s = strdup(sp);
1349  switch_assert(s);
1350  } else {
1351  s = sp;
1352  }
1353 
1354  if (zstr(s)) {
1355  return s;
1356  }
1357 
1358  if ((len = strlen(s)) > 0) {
1359  p = s + (len - 1);
1360 
1361  while (p && *p && (p >= s) && *p == ' ') {
1362  *p-- = '\0';
1363  }
1364  }
1365 
1366  return s;
1367 }
#define zstr(x)
Definition: switch_utils.h:281
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
#define switch_assert(expr)
char* switch_strip_whitespace ( const char *  str)

Definition at line 1301 of file switch_utils.c.

References switch_assert, SWITCH_BLANK_STRING, and zstr.

Referenced by switch_api_execute().

1302 {
1303  const char *sp = str;
1304  char *p, *s = NULL;
1305  size_t len;
1306 
1307  if (zstr(sp)) {
1308  return strdup(SWITCH_BLANK_STRING);
1309  }
1310 
1311  while ((*sp == 13 ) || (*sp == 10 ) || (*sp == 9 ) || (*sp == 32) || (*sp == 11) ) {
1312  sp++;
1313  }
1314 
1315  if (zstr(sp)) {
1316  return strdup(SWITCH_BLANK_STRING);
1317  }
1318 
1319  s = strdup(sp);
1320  switch_assert(s);
1321 
1322  if ((len = strlen(s)) > 0) {
1323  p = s + (len - 1);
1324 
1325  while ((p >= s) && ((*p == 13 ) || (*p == 10 ) || (*p == 9 ) || (*p == 32) || (*p == 11))) {
1326  *p-- = '\0';
1327  }
1328  }
1329 
1330  return s;
1331 }
#define zstr(x)
Definition: switch_utils.h:281
#define SWITCH_BLANK_STRING
Definition: switch_types.h:47
#define switch_assert(expr)
const char* switch_stristr ( const char *  instr,
const char *  str 
)

Definition at line 1479 of file switch_utils.c.

References switch_toupper().

Referenced by audio_bridge_thread(), check_ice(), core_event_handler(), do_chat_send(), find_pt(), inherit_codec(), init_upnp(), main(), 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_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_session_check_incoming_crypto(), switch_core_session_findall_matching_var(), switch_core_sqldb_start(), switch_ivr_originate(), switch_odbc_handle_exec(), switch_pgsql_finish_results_real(), switch_pgsql_handle_exec_base_detailed(), and switch_simple_email().

1480 {
1481 /*
1482 ** Rev History: 16/07/97 Greg Thayer Optimized
1483 ** 07/04/95 Bob Stout ANSI-fy
1484 ** 02/03/94 Fred Cole Original
1485 ** 09/01/03 Bob Stout Bug fix (lines 40-41) per Fred Bulback
1486 **
1487 ** Hereby donated to public domain.
1488 */
1489  const char *pptr, *sptr, *start;
1490 
1491  if (!str || !instr)
1492  return NULL;
1493 
1494  for (start = str; *start; start++) {
1495  /* find start of pattern in string */
1496  for (; ((*start) && (switch_toupper(*start) != switch_toupper(*instr))); start++);
1497 
1498  if (!*start)
1499  return NULL;
1500 
1501  pptr = instr;
1502  sptr = start;
1503 
1504  while (switch_toupper(*sptr) == switch_toupper(*pptr)) {
1505  sptr++;
1506  pptr++;
1507 
1508  /* if end of pattern then pattern was found */
1509  if (!*pptr)
1510  return (start);
1511 
1512  if (!*sptr)
1513  return NULL;
1514  }
1515  }
1516  return NULL;
1517 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
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 847 of file switch_utils.h.

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

848 {
849  char *p, *S = NULL, *Q = NULL;
851 
852  if (!s || !q) {
853  return SWITCH_FALSE;
854  }
855 
856  if (strstr(s, q)) {
857  return SWITCH_TRUE;
858  }
859 
860  S = strdup(s);
861 
862  switch_assert(S != NULL);
863 
864  for (p = S; p && *p; p++) {
865  *p = (char) switch_toupper(*p);
866  }
867 
868  if (strstr(S, q)) {
869  tf = SWITCH_TRUE;
870  goto done;
871  }
872 
873  Q = strdup(q);
874  switch_assert(Q != NULL);
875 
876  for (p = Q; p && *p; p++) {
877  *p = (char) switch_toupper(*p);
878  }
879 
880  if (strstr(s, Q)) {
881  tf = SWITCH_TRUE;
882  goto done;
883  }
884 
885  if (strstr(S, Q)) {
886  tf = SWITCH_TRUE;
887  goto done;
888  }
889 
890  done:
891  switch_safe_free(S);
892  switch_safe_free(Q);
893 
894  return tf;
895 }
switch_bool_t
Definition: switch_types.h:405
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
#define switch_assert(expr)
switch_bool_t switch_testv6_subnet ( ip_t  _ip,
ip_t  _net,
ip_t  _mask 
)

Definition at line 428 of file switch_utils.c.

References IN6_AND_MASK, and SWITCH_TRUE.

Referenced by switch_check_network_list_ip_token(), and switch_network_list_validate_ip6_token().

428  {
429  if (!IN6_IS_ADDR_UNSPECIFIED(&_mask.v6)) {
430  struct in6_addr a, b;
431  IN6_AND_MASK(&a, &_net, &_mask);
432  IN6_AND_MASK(&b, &_ip, &_mask);
433  return !memcmp(&a,&b, sizeof(struct in6_addr));
434  } else {
435  if (!IN6_IS_ADDR_UNSPECIFIED(&_net.v6)) {
436  return !memcmp(&_net,&_ip,sizeof(struct in6_addr));
437  }
438  else return SWITCH_TRUE;
439  }
440 }
#define IN6_AND_MASK(result, ip, mask)
Definition: switch_utils.c:423
struct in6_addr v6
Definition: switch_utils.h:248
int switch_tod_cmp ( const char *  exp,
int  val 
)

Definition at line 3784 of file switch_utils.c.

References cur, switch_assert, and switch_safe_free.

Referenced by switch_xml_std_datetime_check().

3785 {
3786  char *dup = strdup(exp);
3787  char *minh;
3788  char *minm;
3789  char *mins;
3790  char *maxh;
3791  char *maxm;
3792  char *maxs;
3793  char *cur;
3794  char *p;
3795  int range_start, range_end;
3796 
3797  switch_assert(dup);
3798 
3799  cur = dup;
3800  if ((p = strchr(cur, ','))) {
3801  *p++ = '\0';
3802  }
3803 
3804  while (cur) {
3805  minh = cur;
3806  if ((minm=strchr(cur, ':'))) {
3807  *minm++ = '\0';
3808  if ((maxh=strchr(minm, '-'))) {
3809  if ((maxm=strchr(maxh, ':'))) {
3810  *maxh++ = '\0';
3811  *maxm++ = '\0';
3812  /* Check if min/max seconds are present */
3813  if ((mins=strchr(minm, ':'))) {
3814  *mins++ = '\0';
3815  } else {
3816  mins = "00";
3817  }
3818  if ((maxs=strchr(maxm, ':'))) {
3819  *maxs++ = '\0';
3820  } else {
3821  maxs = "00";
3822  }
3823 
3824  range_start = (atol(minh) * 60 * 60) + (atol(minm) * 60) + atol(mins);
3825  range_end = (atol(maxh) * 60 * 60) + (atol(maxm) * 60) + atol(maxs);
3826  if (range_start <= range_end ? (val >= range_start && val <= range_end) : (val >= range_start || val <= range_end)) {
3827  switch_safe_free(dup);
3828  return 1;
3829  }
3830  }
3831  }
3832  }
3833 
3834  cur = p;
3835  if (p) {
3836  if ((p = strchr(p, ','))) {
3837  *p++ = '\0';
3838  }
3839  }
3840 
3841  }
3842 
3843  switch_safe_free(dup);
3844  return 0;
3845 
3846 }
pack cur
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
#define switch_assert(expr)
static uint32_t switch_tolower ( uint32_t  eax)
inlinestatic

Definition at line 81 of file switch_utils.h.

Referenced by switch_hash_default_ci(), switch_lc_strdup(), and switch_tolower_max().

82 {
83  uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul;
84  ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
85  ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
86  return eax + ebx;
87 }
static void switch_tolower_max ( char *  s)
inlinestatic

Definition at line 200 of file switch_utils.h.

References switch_tolower().

201 {
202  uint32_t *b,*p;
203  char *c;
204  size_t l;
205 
206  l = strlen(s);
207 
208  p = (uint32_t *) s;
209 
210  while (l > 4) {
211  b = p;
212  *b = (uint32_t) switch_tolower(*b);
213  b++;
214  p++;
215  l -= 4;
216  }
217 
218  c = (char *)p;
219 
220  while(l > 0) {
221  *c = (char) switch_tolower(*c);
222  c++;
223  l--;
224  }
225 
226 }
static uint32_t switch_tolower(uint32_t eax)
Definition: switch_utils.h:81
static uint32_t switch_toupper ( uint32_t  eax)
inlinestatic

Definition at line 70 of file switch_utils.h.

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

71 {
72 uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
73 ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
74  ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
75  return eax - ebx;
76 }
static void switch_toupper_max ( char *  s)
inlinestatic

Definition at line 172 of file switch_utils.h.

References switch_toupper().

173 {
174  uint32_t *b,*p;
175  char *c;
176  size_t l;
177 
178  l = strlen(s);
179 
180  p = (uint32_t *) s;
181 
182  while (l > 4) {
183  b = p;
184  *b = (uint32_t) switch_toupper(*b);
185  b++;
186  p++;
187  l -= 4;
188  }
189 
190  c = (char *)p;
191 
192  while(l > 0) {
193  *c = (char) switch_toupper(*c);
194  c++;
195  l--;
196  }
197 
198 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
static int switch_true ( const char *  expr)
inlinestatic

Evaluate the truthfullness of a string expression.

Parameters
expra string expression
Returns
true or false

Definition at line 450 of file switch_utils.h.

References SWITCH_FALSE, switch_is_number(), and SWITCH_TRUE.

Referenced by audio_bridge_on_exchange_media(), audio_bridge_thread(), core_event_handler(), do_chat_send(), get_channels(), inherit_codec(), limit_state_handler(), load_configuration(), record_callback(), signal_bridge_on_hangup(), speech_thread(), 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_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_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_intercept_session(), switch_ivr_multi_threaded_bridge(), switch_ivr_originate(), switch_ivr_parse_event(), switch_ivr_phrase_macro_event(), switch_ivr_play_and_detect_speech(), switch_ivr_play_file(), switch_ivr_preprocess_session(), switch_ivr_record_file(), switch_ivr_record_session(), switch_ivr_say(), switch_ivr_say_string(), switch_ivr_session_echo(), 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_create(), switch_rtp_enable_vad(), switch_rtp_init(), switch_speex_fmtp_parse(), switch_true_byte(), switch_xml_config_parse_event(), switch_xml_std_datetime_check(), and uuid_bridge_on_soft_execute().

451 {
452  return ((expr && ( !strcasecmp(expr, "yes") ||
453  !strcasecmp(expr, "on") ||
454  !strcasecmp(expr, "true") ||
455  !strcasecmp(expr, "t") ||
456  !strcasecmp(expr, "enabled") ||
457  !strcasecmp(expr, "active") ||
458  !strcasecmp(expr, "allow") ||
459  (switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
460 }
switch_bool_t switch_is_number(const char *str)
static switch_byte_t switch_true_byte ( const char *  expr)
inlinestatic

Definition at line 462 of file switch_utils.h.

References switch_true().

463 {
464  return (switch_byte_t)switch_true(expr);
465 }
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
uint8_t switch_byte_t
Definition: switch_types.h:246
static char* switch_uc_strdup ( const char *  it)
inlinestatic

Definition at line 821 of file switch_utils.h.

References switch_toupper().

822 {
823  char *dup;
824  char *p;
825 
826  if (it) {
827  dup = strdup(it);
828  for (p = dup; p && *p; p++) {
829  *p = (char) switch_toupper(*p);
830  }
831  return dup;
832  }
833 
834  return NULL;
835 }
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
char* switch_url_decode ( char *  s)

Definition at line 3252 of file switch_utils.c.

References zstr.

Referenced by switch_core_session_request_xml(), and switch_http_parse_qs().

3253 {
3254  char *o;
3255  unsigned int tmp;
3256 
3257  if (zstr(s)) {
3258  return s;
3259  }
3260 
3261  for (o = s; *s; s++, o++) {
3262  if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
3263  *o = (char) tmp;
3264  s += 2;
3265  } else {
3266  *o = *s;
3267  }
3268  }
3269  *o = '\0';
3270  return s;
3271 }
#define zstr(x)
Definition: switch_utils.h:281
char* switch_url_encode ( const char *  url,
char *  buf,
size_t  len 
)

Definition at line 3247 of file switch_utils.c.

References SWITCH_FALSE, and switch_url_encode_opt().

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

3248 {
3249  return switch_url_encode_opt(url, buf, len, SWITCH_FALSE);
3250 }
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
char* switch_url_encode_opt ( const char *  url,
char *  buf,
size_t  len,
switch_bool_t  double_encode 
)

Definition at line 3202 of file switch_utils.c.

References buf, end_of_p, and SWITCH_URL_UNSAFE.

Referenced by switch_url_encode().

3203 {
3204  const char *p, *e = end_of_p(url);
3205  size_t x = 0;
3206  const char hex[] = "0123456789ABCDEF";
3207 
3208  if (!buf) {
3209  return 0;
3210  }
3211 
3212  if (!url) {
3213  return 0;
3214  }
3215 
3216  len--;
3217 
3218  for (p = url; *p; p++) {
3219  int ok = 0;
3220 
3221  if (x >= len) {
3222  break;
3223  }
3224 
3225  if (!double_encode && *p == '%' && e-p > 1) {
3226  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
3227  ok = 1;
3228  }
3229  }
3230 
3231  if (!ok && (*p < ' ' || *p > '~' || strchr(SWITCH_URL_UNSAFE, *p))) {
3232  if ((x + 3) > len) {
3233  break;
3234  }
3235  buf[x++] = '%';
3236  buf[x++] = hex[(*p >> 4) & 0x0f];
3237  buf[x++] = hex[*p & 0x0f];
3238  } else {
3239  buf[x++] = *p;
3240  }
3241  }
3242  buf[x] = '\0';
3243 
3244  return buf;
3245 }
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
#define end_of_p(_s)
Definition: switch_utils.h:617
switch_byte_t switch_byte_t * buf
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 2729 of file switch_utils.c.

References switch_util_quote_shell_arg_pool().

Referenced by switch_simple_email().

2730 {
2731  return switch_util_quote_shell_arg_pool(string, NULL);
2732 }
char * switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool)
Quote shell argument, allocating from pool if provided.
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 2734 of file switch_utils.c.

References switch_assert, and switch_core_alloc.

Referenced by switch_util_quote_shell_arg().

2735 {
2736  size_t string_len = strlen(string);
2737  size_t i;
2738  size_t n = 0;
2739  size_t dest_len = 0;
2740  char *dest;
2741 
2742  /* first pass through, figure out how large to make the allocation */
2743  dest_len = strlen(string) + 1; /* string + null */
2744  dest_len += 1; /* opening quote */
2745  for (i = 0; i < string_len; i++) {
2746  switch (string[i]) {
2747 #ifndef WIN32
2748  case '\'':
2749  /* We replace ' by sq backslace sq sq, so need 3 additional bytes */
2750  dest_len += 3;
2751  break;
2752 #endif
2753  }
2754  }
2755  dest_len += 1; /* closing quote */
2756 
2757  /* if we're given a pool, allocate from it, otherwise use malloc */
2758  if (pool) {
2759  dest = switch_core_alloc(pool, sizeof(char) * dest_len);
2760  } else {
2761  dest = (char *) malloc(sizeof(char) * dest_len);
2762  }
2763  switch_assert(dest);
2764 
2765 #ifdef WIN32
2766  dest[n++] = '"';
2767 #else
2768  dest[n++] = '\'';
2769 #endif
2770 
2771  for (i = 0; i < string_len; i++) {
2772  switch (string[i]) {
2773 #ifdef WIN32
2774  case '"':
2775  case '%':
2776  dest[n++] = ' ';
2777  break;
2778 #else
2779  case '\'':
2780  /* We replace ' by sq backslash sq sq */
2781  dest[n++] = '\'';
2782  dest[n++] = '\\';
2783  dest[n++] = '\'';
2784  dest[n++] = '\'';
2785  break;
2786 #endif
2787  default:
2788  dest[n++] = string[i];
2789  }
2790  }
2791 
2792 #ifdef WIN32
2793  dest[n++] = '"';
2794 #else
2795  dest[n++] = '\'';
2796 #endif
2797  dest[n++] = 0;
2798  switch_assert(n == dest_len);
2799  return dest;
2800 }
switch_memory_pool_t * pool
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
#define switch_assert(expr)
char* switch_uuid_str ( char *  buf,
switch_size_t  len 
)

Definition at line 3873 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(), and switch_core_session_exec().

3874 {
3875  switch_uuid_t uuid;
3876 
3877  if (len < (SWITCH_UUID_FORMATTED_LENGTH + 1)) {
3878  switch_snprintf(buf, len, "INVALID");
3879  } else {
3880  switch_uuid_get(&uuid);
3881  switch_uuid_format(buf, &uuid);
3882  }
3883 
3884  return buf;
3885 }
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:1055
void switch_uuid_get(switch_uuid_t *uuid)
Definition: switch_apr.c:1067
#define SWITCH_UUID_FORMATTED_LENGTH
Definition: switch_apr.h:545
static char* switch_var_clean_string ( char *  s)
inlinestatic

Definition at line 735 of file switch_utils.h.

References switch_string_var_check(), and SWITCH_TRUE.

736 {
738  return s;
739 }
static switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
Definition: switch_utils.h:691
int switch_wait_sock ( switch_os_socket_t  sock,
uint32_t  ms,
switch_poll_t  flags 
)

Definition at line 2985 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().

2986 {
2987  int s = 0, r = 0;
2988  fd_set *rfds;
2989  fd_set *wfds;
2990  fd_set *efds;
2991  struct timeval tv;
2992 
2993  if (sock == SWITCH_SOCK_INVALID) {
2994  return SWITCH_SOCK_INVALID;
2995  }
2996 
2997  rfds = malloc(sizeof(fd_set));
2998  wfds = malloc(sizeof(fd_set));
2999  efds = malloc(sizeof(fd_set));
3000 
3001  FD_ZERO(rfds);
3002  FD_ZERO(wfds);
3003  FD_ZERO(efds);
3004 
3005 #ifndef WIN32
3006  /* Wouldn't you rather know?? */
3007  assert(sock <= FD_SETSIZE);
3008 #endif
3009 
3010  if ((flags & SWITCH_POLL_READ)) {
3011 
3012 #ifdef WIN32
3013 #pragma warning( push )
3014 #pragma warning( disable : 4127 )
3015  FD_SET(sock, rfds);
3016 #pragma warning( pop )
3017 #else
3018  FD_SET(sock, rfds);
3019 #endif
3020  }
3021 
3022  if ((flags & SWITCH_POLL_WRITE)) {
3023 
3024 #ifdef WIN32
3025 #pragma warning( push )
3026 #pragma warning( disable : 4127 )
3027  FD_SET(sock, wfds);
3028 #pragma warning( pop )
3029 #else
3030  FD_SET(sock, wfds);
3031 #endif
3032  }
3033 
3034  if ((flags & SWITCH_POLL_ERROR)) {
3035 
3036 #ifdef WIN32
3037 #pragma warning( push )
3038 #pragma warning( disable : 4127 )
3039  FD_SET(sock, efds);
3040 #pragma warning( pop )
3041 #else
3042  FD_SET(sock, efds);
3043 #endif
3044  }
3045 
3046  tv.tv_sec = ms / 1000;
3047  tv.tv_usec = (ms % 1000) * ms;
3048 
3049  s = select(sock + 1, (flags & SWITCH_POLL_READ) ? rfds : NULL, (flags & SWITCH_POLL_WRITE) ? wfds : NULL, (flags & SWITCH_POLL_ERROR) ? efds : NULL, &tv);
3050 
3051  if (s < 0) {
3053  s = 0;
3054  }
3055  }
3056 
3057  if (s < 0) {
3058  r = s;
3059  } else if (s > 0) {
3060  if ((flags & SWITCH_POLL_READ) && FD_ISSET(sock, rfds)) {
3061  r |= SWITCH_POLL_READ;
3062  }
3063 
3064  if ((flags & SWITCH_POLL_WRITE) && FD_ISSET(sock, wfds)) {
3065  r |= SWITCH_POLL_WRITE;
3066  }
3067 
3068  if ((flags & SWITCH_POLL_ERROR) && FD_ISSET(sock, efds)) {
3069  r |= SWITCH_POLL_ERROR;
3070  }
3071  }
3072 
3073  free(rfds);
3074  free(wfds);
3075  free(efds);
3076 
3077  return r;
3078 
3079 }
static int switch_errno_is_break(int errcode)
Definition: switch_utils.h:580
#define SWITCH_SOCK_INVALID
#define switch_errno()
Definition: switch_utils.h:578
int switch_wait_socklist ( switch_waitlist_t waitlist,
uint32_t  len,
uint32_t  ms 
)

Definition at line 3081 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.

3082 {
3083  int s = 0, r = 0;
3084  fd_set *rfds;
3085  fd_set *wfds;
3086  fd_set *efds;
3087  struct timeval tv;
3088  unsigned int i;
3089  switch_os_socket_t max_fd = 0;
3090  int flags = 0;
3091 
3092  rfds = malloc(sizeof(fd_set));
3093  wfds = malloc(sizeof(fd_set));
3094  efds = malloc(sizeof(fd_set));
3095 
3096  FD_ZERO(rfds);
3097  FD_ZERO(wfds);
3098  FD_ZERO(efds);
3099 
3100  for (i = 0; i < len; i++) {
3101  if (waitlist[i].sock == SWITCH_SOCK_INVALID) {
3102  break;
3103  }
3104 
3105  if (waitlist[i].sock > max_fd) {
3106  max_fd = waitlist[i].sock;
3107  }
3108 
3109 #ifndef WIN32
3110  /* Wouldn't you rather know?? */
3111  assert(waitlist[i].sock <= FD_SETSIZE);
3112 #endif
3113  flags |= waitlist[i].events;
3114 
3115  if ((waitlist[i].events & SWITCH_POLL_READ)) {
3116 
3117 #ifdef WIN32
3118 #pragma warning( push )
3119 #pragma warning( disable : 4127 )
3120  FD_SET(waitlist[i].sock, rfds);
3121 #pragma warning( pop )
3122 #else
3123  FD_SET(waitlist[i].sock, rfds);
3124 #endif
3125  }
3126 
3127  if ((waitlist[i].events & SWITCH_POLL_WRITE)) {
3128 
3129 #ifdef WIN32
3130 #pragma warning( push )
3131 #pragma warning( disable : 4127 )
3132  FD_SET(waitlist[i].sock, wfds);
3133 #pragma warning( pop )
3134 #else
3135  FD_SET(waitlist[i].sock, wfds);
3136 #endif
3137  }
3138 
3139  if ((waitlist[i].events & SWITCH_POLL_ERROR)) {
3140 
3141 #ifdef WIN32
3142 #pragma warning( push )
3143 #pragma warning( disable : 4127 )
3144  FD_SET(waitlist[i].sock, efds);
3145 #pragma warning( pop )
3146 #else
3147  FD_SET(waitlist[i].sock, efds);
3148 #endif
3149  }
3150  }
3151 
3152  tv.tv_sec = ms / 1000;
3153  tv.tv_usec = (ms % 1000) * ms;
3154 
3155  s = select(max_fd + 1, (flags & SWITCH_POLL_READ) ? rfds : NULL, (flags & SWITCH_POLL_WRITE) ? wfds : NULL, (flags & SWITCH_POLL_ERROR) ? efds : NULL, &tv);
3156 
3157  if (s < 0) {
3159  s = 0;
3160  }
3161  }
3162 
3163  if (s < 0) {
3164  r = s;
3165  } else if (s > 0) {
3166  for (i = 0; i < len; i++) {
3167  if ((waitlist[i].events & SWITCH_POLL_READ) && FD_ISSET(waitlist[i].sock, rfds)) {
3168  r |= SWITCH_POLL_READ;
3169  waitlist[i].revents |= SWITCH_POLL_READ;
3170  }
3171 
3172  if ((waitlist[i].events & SWITCH_POLL_WRITE) && FD_ISSET(waitlist[i].sock, wfds)) {
3173  r |= SWITCH_POLL_WRITE;
3174  waitlist[i].revents |= SWITCH_POLL_WRITE;
3175  }
3176 
3177  if ((waitlist[i].events & SWITCH_POLL_ERROR) && FD_ISSET(waitlist[i].sock, efds)) {
3178  r |= SWITCH_POLL_ERROR;
3179  waitlist[i].revents |= SWITCH_POLL_ERROR;
3180  }
3181  }
3182  }
3183 
3184  free(rfds);
3185  free(wfds);
3186  free(efds);
3187 
3188  return r;
3189 
3190 }
switch_os_socket_t sock
static int switch_errno_is_break(int errcode)
Definition: switch_utils.h:580
#define SWITCH_SOCK_INVALID
int switch_os_socket_t
#define switch_errno()
Definition: switch_utils.h:578