Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_utils.h File Reference


Detailed Description

Compatability and Helper Code.

Just a miscelaneaous set of general utility/helper functions.


Defines

#define switch_samples_per_frame(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 / 2; else if (n < SWITCH_SMIN) n = SWITCH_SMIN / 2;
#define switch_codec2str(codec, buf, len)
#define switch_is_file_path(file)   (file && ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR)))
#define switch_strlen_zero(s)   (!s || *s == '\0')
 Test for NULL or zero length string.
#define switch_true(expr)
 Evaluate the truthfullness of a string expression.
#define SWITCH_STATUS_IS_BREAK(x)   (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35)
#define is_dtmf(key)   ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119)
 determine if a character is a valid DTMF key
#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.
#define switch_set_flag(obj, flag)   (obj)->flags |= (flag)
 Set a flag on an arbitrary object.
#define switch_set_flag_locked(obj, flag)
 Set a flag on an arbitrary object while locked.
#define switch_clear_flag_locked(obj, flag)   switch_mutex_lock(obj->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock(obj->flag_mutex);
 Clear a flag on an arbitrary object.
#define switch_clear_flag(obj, flag)   (obj)->flags &= ~(flag)
 Clear a flag on an arbitrary object while locked.
#define switch_copy_flags(dest, src, flags)   (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))
 Copy flags from one arbitrary object to another.
#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.
#define switch_str_nil(s)   (s ? s : "")
 Make a null string a blank string instead.
#define switch_yield(ms)   switch_sleep(ms);
 Wait a desired number of microseconds and yield the CPU.
#define SWITCH_DECLARE_GLOBAL_STRING_FUNC(fname, vname)
 Declares a function designed to set a dymaic global string.
#define SWITCH_READ_ACCEPTABLE(status)   (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK)
#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 = malloc(len))),memset(ptr, 0, len))

Functions

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)
switch_size_t switch_fd_read_line (int fd, char *buf, switch_size_t len)
switch_status_t switch_find_local_ip (_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_ int family)
 find local ip of the box
char * get_addr (char *buf, switch_size_t len, struct in_addr *in)
 find the char representation of an ip adress
const char * switch_priority_name (switch_priority_t priority)
 Return a printable name of a switch_priority_t.
char switch_rfc2833_to_char (int event)
 Return the RFC2833 character based on an event id.
unsigned char switch_char_to_rfc2833 (char key)
 Return the RFC2833 event based on an key character.
switch_time_t switch_str_time (const char *in)
 Converts a string representation of a date into a switch_time_t.
unsigned int switch_separate_string (char *buf, char delim, char **array, unsigned int arraylen)
 Separate a string into an array based on a character delimeter.
switch_bool_t switch_is_number (const char *str)
char * switch_strip_spaces (const char *str)
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 (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.
int switch_socket_waitfor (switch_pollfd_t *poll, int ms)
 Wait for a socket.
const char * switch_cut_path (const char *in)
 Create a pointer to the file name in a given file path eliminating the directory name.
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)
size_t 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)
char * switch_find_end_paren (const char *s, char open, char close)
int switch_parse_cidr (const char *string, uint32_t *ip, uint32_t *mask, uint32_t *bitp)
switch_status_t switch_network_list_create (switch_network_list_t **list, switch_bool_t default_type, switch_memory_pool_t *pool)
switch_status_t switch_network_list_add_cidr (switch_network_list_t *list, const char *cidr_str, switch_bool_t ok)
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 (switch_network_list_t *list, uint32_t ip)
int switch_inet_pton (int af, const char *src, void *dst)


Define Documentation

#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)
 

determine if a character is a valid DTMF key

Parameters:
key the key to test
Returns:
TRUE or FALSE

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

Clear a flag on an arbitrary object while locked.

Parameters:
obj the object to test
flag the or'd list of flags to clear

#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:
obj the object to test
flag the or'd list of flags to clear

#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_frame / 1000)

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

Copy flags from one arbitrary object to another.

Parameters:
dest the object to copy the flags to
src the object to copy the flags from
flags the flags to copy

#define SWITCH_DECLARE_GLOBAL_STRING_FUNC fname,
vname   ) 
 

Value:

static void fname(char *string) { if (!string) return;\
                if (vname) {free(vname); vname = NULL;}vname = strdup(string);} static void fname(char *string)
Declares a function designed to set a dymaic global string.

Parameters:
fname the function name to declare
vname the name of the global pointer to modify with the new function

#define switch_is_file_path file   )     (file && ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR)))
 

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

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

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

#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:
it the pointer

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

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

Set a flag on an arbitrary object.

Parameters:
obj the object to set the flags on
flag the or'd list of flags to set

#define switch_set_flag_locked obj,
flag   ) 
 

Value:

assert(obj->flag_mutex != NULL);\
switch_mutex_lock(obj->flag_mutex);\
(obj)->flags |= (flag);\
switch_mutex_unlock(obj->flag_mutex);
Set a flag on an arbitrary object while locked.

Parameters:
obj the object to set the flags on
flag the or'd list of flags to set

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

#define SWITCH_SMAX   32767
 

#define SWITCH_SMIN   -32768
 

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

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

Make a null string a blank string instead.

Parameters:
s the string to test
Returns:
the original string or blank string.

#define switch_strlen_zero  )     (!s || *s == '\0')
 

Test for NULL or zero length string.

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

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

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

Parameters:
obj the object to test
flag the or'd list of flags to test
Returns:
true value if the object has the flags defined

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

#define switch_true expr   ) 
 

Value:

(expr && ( !strcasecmp(expr, "yes") ||\
!strcasecmp(expr, "on") ||\
!strcasecmp(expr, "true") ||\
!strcasecmp(expr, "enabled") ||\
!strcasecmp(expr, "active") ||\
!strcasecmp(expr, "allow") ||\
atoi(expr))) ? SWITCH_TRUE : SWITCH_FALSE
Evaluate the truthfullness of a string expression.

Parameters:
expr a string expression
Returns:
true or false

#define switch_yield ms   )     switch_sleep(ms);
 

Wait a desired number of microseconds and yield the CPU.

#define switch_zmalloc ptr,
len   )     (void)(assert((ptr = malloc(len))),memset(ptr, 0, len))
 


Function Documentation

char* get_addr char *  buf,
switch_size_t  len,
struct in_addr *  in
 

find the char representation of an ip adress

Parameters:
buf the buffer to write the ip adress found into
len the length of the buf
in the struct in_addr * to get the adress from
Returns:
the ip adress string
01062 {
01063         switch_assert(buf);
01064         *buf = '\0';
01065         if (in) {
01066                 switch_inet_ntop(AF_INET, in, buf, len);
01067         }
01068         return buf;
01069 }

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

00226 {
00227         char *p, *q;
00228         switch_size_t x = 0;
00229         switch_assert(s);
00230 
00231         q = buf;
00232 
00233         for(p = s; x < len; p++) {
00234                 switch(*p) {
00235                 case '<':
00236                         if (x + 4 > len -1) {
00237                                 goto end;
00238                         }
00239                         *q++ = '&';
00240                         *q++ = 'l';
00241                         *q++ = 't';
00242                         *q++ = ';';
00243                         x += 4;
00244                         break;
00245                 case '>':
00246                         if (x + 4 > len -1) {
00247                                 goto end;
00248                         }
00249                         *q++ = '&';
00250                         *q++ = 'g';
00251                         *q++ = 't';
00252                         *q++ = ';';
00253                         x += 4;
00254                         break;
00255                 default:
00256                         if (x + 1 > len -1) {
00257                                 goto end;
00258                         }
00259                         *q++ = *p;
00260                         x++;
00261                         if (*p == '\0') {
00262                                 goto end;
00263                         }
00264                         break;
00265                 }
00266         }
00267 
00268  end:
00269 
00270         return buf;
00271 }

switch_bool_t switch_ast2regex char *  pat,
char *  rbuf,
size_t  len
 

00510 {
00511         char *p = pat;
00512 
00513         if (!pat) {
00514                 return SWITCH_FALSE;
00515         }
00516 
00517         memset(rbuf, 0, len);
00518         
00519         *(rbuf + strlen(rbuf)) = '^';
00520 
00521         while(p && *p) {
00522                 if (*p == 'N') {
00523                         strncat(rbuf, "[2-9]", len - strlen(rbuf));
00524                 } else if (*p == 'X') {
00525                         strncat(rbuf, "[0-9]", len - strlen(rbuf));
00526                 } else if (*p == 'Z') {
00527                         strncat(rbuf, "[1-9]", len - strlen(rbuf));
00528                 } else if (*p == '.') {
00529                         strncat(rbuf, ".*", len - strlen(rbuf));
00530                 } else if (strlen(rbuf) < len - 1) {
00531                         *(rbuf + strlen(rbuf)) = *p;
00532                 }
00533                 p++;
00534         }
00535         *(rbuf + strlen(rbuf)) = '$';
00536 
00537         return strcmp(pat,rbuf) ? SWITCH_TRUE : SWITCH_FALSE;
00538 }

switch_size_t switch_b64_decode char *  in,
char *  out,
switch_size_t  olen
 

00307 {
00308 
00309         char l64[256];
00310         int b = 0, c, l = 0, i;
00311         char *ip, *op = out;
00312         size_t ol = 0;
00313 
00314         for (i=0; i<256; i++) {
00315                 l64[i] = -1;
00316         }
00317 
00318         for (i=0; i<64; i++) {
00319                 l64[(int)switch_b64_table[i]] = (char)i;
00320         }
00321 
00322         for (ip = in; ip && *ip; ip++) {
00323                 c = l64[(int)*ip];
00324                 if (c == -1) {
00325                         continue;
00326                 }
00327 
00328                 b = (b << 6) + c;
00329                 l += 6;
00330 
00331                 while (l >= 8) {
00332                         op[ol++] = (char)((b >> (l -= 8)) % 256);
00333                         if (ol >= olen -2) {
00334                                 goto end;
00335                         }
00336                 }
00337         }
00338 
00339  end:
00340 
00341         op[ol++] = '\0';
00342 
00343         return ol;
00344 }

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

00276 {
00277     int y = 0, bytes = 0;
00278     size_t x = 0;
00279     unsigned int b = 0,l = 0;
00280 
00281     for(x = 0; x < ilen; x++) {
00282         b = (b<<8) + in[x];
00283         l += 8;
00284         while (l >= 6) {
00285             out[bytes++] = switch_b64_table[(b>>(l-=6))%64];
00286             if (++y != 72) {
00287                 continue;
00288             }
00289             //out[bytes++] = '\n';
00290             y=0;
00291         }
00292     }
00293 
00294     if (l > 0) {
00295         out[bytes++] = switch_b64_table[((b%16)<<(6-l))%64];
00296     }
00297     if (l != 0) {
00298                 while (l < 6) {
00299                         out[bytes++] = '=', l += 2;
00300                 }
00301         }
00302 
00303     return SWITCH_STATUS_SUCCESS;
00304 }

unsigned char switch_char_to_rfc2833 char  key  ) 
 

Return the RFC2833 event based on an key character.

Parameters:
key the charecter to encode
Returns:
the event id for the specified character or -1 on an invalid input
01080 {
01081         char *c;
01082         unsigned char counter = 0;
01083 
01084         key = (char) toupper(key);
01085         for (c = RFC2833_CHARS; *c; c++) {
01086                 if (*c == key) {
01087                         return counter;
01088                 }
01089                 counter++;
01090         }
01091         return '\0';
01092 }

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
01322 {
01323         const char *p, *ret = in;
01324         const char delims[] = "/\\";
01325         const char *i;
01326 
01327         if (in) {
01328                 for (i = delims; *i; i++) {
01329                         p = in;
01330                         while ((p = strchr(p, *i)) != 0) {
01331                                 ret = ++p;
01332                         }
01333                 }
01334                 return ret;
01335         } else {
01336                 return NULL;
01337         }
01338 }

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:
pool a memory pool to use
in the string
delim the list of characters to escape
esc the escape character
Returns:
the escaped string
01095 {
01096         char *data;
01097         const char *p, *d;
01098         int count = 1, i = 0;
01099 
01100         p = in;
01101         while (*p) {
01102                 d = delim;
01103                 while (*d) {
01104                         if (*p == *d) {
01105                                 count++;
01106                         }
01107                         d++;
01108                 }
01109                 p++;
01110         }
01111 
01112         if (count == 1) {
01113                 return in;
01114         }
01115 
01116         data = switch_core_alloc(pool, strlen(in) + count);
01117 
01118         p = in;
01119         while (*p) {
01120                 d = delim;
01121                 while (*d) {
01122                         if (*p == *d) {
01123                                 data[i++] = esc;
01124                         }
01125                         d++;
01126                 }
01127                 data[i++] = *p;
01128                 p++;
01129         }
01130         return data;
01131 }

switch_size_t switch_fd_read_line int  fd,
char *  buf,
switch_size_t  len
 

00206 {
00207         char c, *p;
00208         int cur;
00209         switch_size_t total = 0;
00210 
00211         p = buf;
00212         while (total + 2 < len && (cur = read(fd, &c, 1)) == 1) {
00213                 total += cur;
00214                 *p++ = c;
00215                 if (c == '\r' || c == '\n') {
00216                         break;
00217                 }
00218         }
00219 
00220         *p++ = '\0';
00221         assert(total < len);
00222         return total;
00223 }

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

00180 {
00181         const char *e = NULL;
00182         int depth = 0;
00183         
00184         while (s && *s && *s == ' ') {
00185                 s++;
00186         }
00187 
00188         if (s && *s == open) {
00189                 depth++;
00190                 for (e = s + 1; e && *e; e++) {
00191                         if (*e == open) {
00192                                 depth++;
00193                         } else if (*e == close) {
00194                                 depth--;
00195                                 if (!depth) {
00196                                         break;
00197                                 }
00198                         }
00199                 }
00200         }
00201 
00202         return (char *)e;
00203 }

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

find local ip of the box

Parameters:
buf the buffer to write the ip adress found into
len the length of the buf
family the address family to return (AF_INET or AF_INET6)
Returns:
SWITCH_STATUS_SUCCESSS for success, otherwise failure

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

00071 {
00072   switch (af) {
00073   case AF_INET:
00074     return (inet_pton4(src, (unsigned char *)dst));
00075 #ifdef ENABLE_IPV6
00076 #ifndef AF_INET6
00077 #define AF_INET6        (AF_MAX+1)        /* just to let this compile */
00078 #endif
00079   case AF_INET6:
00080     return (inet_pton6(src, (unsigned char *)dst));
00081 #endif
00082   default:
00083     errno = EAFNOSUPPORT;
00084     return (-1);
00085   }
00086   /* NOTREACHED */
00087 }

switch_bool_t switch_is_lan_addr const char *  ip  ) 
 

00486 {
00487         if (switch_strlen_zero(ip)) return SWITCH_FALSE;
00488 
00489         return (
00490                         strncmp(ip, "10.", 3) &&
00491                         strncmp(ip, "192.168.", 8) &&
00492                         strncmp(ip, "127.", 4) &&
00493                         strncmp(ip, "255.", 4) &&
00494                         strncmp(ip, "0.", 2) &&
00495                         strncmp(ip, "1.", 2) &&
00496                         strncmp(ip, "2.", 2) &&
00497                         strncmp(ip, "172.16.", 7) &&
00498                         strncmp(ip, "172.17.", 7) &&
00499                         strncmp(ip, "172.18.", 7) &&
00500                         strncmp(ip, "172.19.", 7) &&
00501                         strncmp(ip, "172.2", 5) &&
00502                         strncmp(ip, "172.30.", 7) &&
00503                         strncmp(ip, "172.31.", 7) &&
00504                         strncmp(ip, "192.0.2.", 8) &&
00505                         strncmp(ip, "169.254.", 8)
00506                         ) ? SWITCH_FALSE : SWITCH_TRUE; 
00507 }

switch_bool_t switch_is_number const char *  str  ) 
 

00614 {
00615         const char *p;
00616         switch_bool_t r = SWITCH_TRUE;
00617 
00618         for (p = str; p && *p; p++) {
00619                 if (!(*p == '.' || (*p > 47 && *p < 58))) {
00620                         r = SWITCH_FALSE;
00621                         break;
00622                 }
00623         }
00624 
00625         return r;
00626 }

switch_status_t switch_network_list_add_cidr switch_network_list_t list,
const char *  cidr_str,
switch_bool_t  ok
 

00102 {
00103         uint32_t ip, mask, bits;
00104         switch_network_node_t *node;
00105         
00106         if (switch_parse_cidr(cidr_str, &ip, &mask, &bits)) {
00107                 return SWITCH_STATUS_GENERR;
00108         }
00109         
00110         node = switch_core_alloc(list->pool, sizeof(*node));
00111 
00112         node->ip = ip;
00113         node->mask = mask;
00114         node->ok = ok;
00115         node->bits = bits;
00116 
00117         node->next = list->node_head;
00118         list->node_head = node;
00119         
00120         return SWITCH_STATUS_SUCCESS;
00121 }

Here is the call graph for this function:

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
 

00125 {
00126         int ip, mask;
00127         switch_network_node_t *node;
00128 
00129         switch_inet_pton(AF_INET, host, &ip);
00130         switch_inet_pton(AF_INET, mask_str, &mask);
00131         
00132         node = switch_core_alloc(list->pool, sizeof(*node));
00133         
00134         node->ip = ip;
00135         node->mask = mask;
00136         node->ok = ok;
00137 
00138         /* http://graphics.stanford.edu/~seander/bithacks.html */
00139         mask = mask - ((mask >> 1) & 0x55555555);
00140     mask = (mask & 0x33333333) + ((mask >> 2) & 0x33333333);
00141         node->bits = (((mask + (mask >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
00142         
00143         node->next = list->node_head;
00144         list->node_head = node;
00145         
00146         return SWITCH_STATUS_SUCCESS;
00147 }

Here is the call graph for this function:

switch_status_t switch_network_list_create switch_network_list_t **  list,
switch_bool_t  default_type,
switch_memory_pool_t pool
 

00064 {
00065         switch_network_list_t *new_list;
00066         
00067         if (!pool) {
00068                 switch_core_new_memory_pool(&pool);
00069         }
00070 
00071         new_list = switch_core_alloc(pool, sizeof(**list));
00072         new_list->pool = pool;
00073         new_list->default_type = default_type;
00074 
00075         *list = new_list;
00076 
00077         return SWITCH_STATUS_SUCCESS;
00078 }

switch_bool_t switch_network_list_validate_ip switch_network_list_t list,
uint32_t  ip
 

00081 {
00082         switch_network_node_t *node;
00083         switch_bool_t ok = list->default_type;
00084         uint32_t bits = 0;
00085         
00086         for (node = list->node_head; node; node = node->next) {
00087                 if (node->bits > bits && switch_test_subnet(ip, node->ip, node->mask)) {
00088                         if (node->ok) {
00089                                 ok = SWITCH_TRUE;
00090                         } else {
00091                                 ok = SWITCH_FALSE;
00092                         }
00093                         bits = node->bits;
00094                 }
00095         }
00096         
00097         return ok;
00098 }

int switch_parse_cidr const char *  string,
uint32_t *  ip,
uint32_t *  mask,
uint32_t *  bitp
 

00151 {
00152         char host[128];
00153         char *bit_str;
00154         int32_t bits;
00155 
00156         switch_copy_string(host, string, sizeof(host));
00157         bit_str = strchr(host, '/');
00158 
00159         if (!bit_str) {
00160                 return -1;
00161         }
00162 
00163         *bit_str++ = '\0';
00164         bits = atoi(bit_str);
00165         
00166         if (bits < 0 || bits > 32) {
00167                 return -2;
00168         }
00169 
00170         bits = atoi(bit_str);
00171         switch_inet_pton(AF_INET, host, ip);
00172         *mask = 0xFFFFFFFF & ~(0xFFFFFFFF << bits);
00173         *bitp = bits;
00174 
00175         return 0;
00176 }

Here is the call graph for this function:

const char* switch_priority_name switch_priority_t  priority  ) 
 

Return a printable name of a switch_priority_t.

Parameters:
priority the priority to get the name of
Returns:
the printable form of the priority
00864 {
00865         switch (priority) {                     /*lol */
00866         case SWITCH_PRIORITY_NORMAL:
00867                 return "NORMAL";
00868         case SWITCH_PRIORITY_LOW:
00869                 return "LOW";
00870         case SWITCH_PRIORITY_HIGH:
00871                 return "HIGH";
00872         default:
00873                 return "INVALID";
00874         }
00875 }

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

00541 {
00542         char *p;
00543 
00544         if (dup) {
00545                 p = strdup(str);
00546                 switch_assert(p);
00547         } else {
00548                 p = str;
00549         }
00550 
00551         for(;p && *p; p++) {
00552                 if (*p == from) {
00553                         *p = to;
00554                 }
00555         }
00556 
00557         return p;
00558 }

char switch_rfc2833_to_char int  event  ) 
 

Return the RFC2833 character based on an event id.

Parameters:
event the event id to convert
Returns:
the character represented by the event or null for an invalid event
01072 {
01073         if (event > -1 && event < (int32_t) sizeof(RFC2833_CHARS)) {
01074                 return RFC2833_CHARS[event];
01075         }
01076         return '\0';
01077 }

char* switch_separate_paren_args char *  str  ) 
 

00585 {
00586         char *e, *args;
00587         switch_size_t br;
00588 
00589         if ((args = strchr(str, '('))) {
00590                 e = args - 1;
00591                 *args++ = '\0';
00592                 while(*e == ' ') {
00593                         *e-- = '\0';
00594                 }
00595                 e = args;
00596                 br = 1;
00597                 while(e && *e) {
00598                         if (*e == '(') {
00599                                 br++;
00600                         } else if (br > 1 && *e == ')') {
00601                                 br--;
00602                         } else if (br == 1 && *e == ')') {
00603                                 *e = '\0';
00604                                 break;
00605                         }
00606                         e++;
00607                 }
00608         }
00609 
00610         return args;
00611 }

unsigned int switch_separate_string char *  buf,
char  delim,
char **  array,
unsigned int  arraylen
 

Separate a string into an array based on a character delimeter.

Parameters:
buf the string to parse
delim the character delimeter
array the array to split the values into
arraylen the max number of elements in the array
Returns:
the number of elements added to the array
01309 {
01310         if (!buf || !array || !arraylen) {
01311                 return 0;
01312         }
01313 
01314         memset(array, 0, arraylen * sizeof (*array));
01315 
01316         return (delim == ' ' ?
01317                         separate_string_blank_delim(buf, array, arraylen) :
01318                         separate_string_char_delim(buf, delim, array, arraylen));
01319 }

switch_bool_t switch_simple_email const char *  to,
const char *  from,
const char *  headers,
const char *  body,
const char *  file
 

00359 {
00360         char *bound = "XXXX_boundary_XXXX";
00361         const char *mime_type = "audio/inline";
00362         char filename[80], buf[B64BUFFLEN];
00363         int fd = 0, ifd = 0;
00364         int x = 0, y = 0, bytes = 0, ilen = 0;
00365         unsigned int b = 0, l = 0;
00366         unsigned char in[B64BUFFLEN];
00367         unsigned char out[B64BUFFLEN + 512];
00368 
00369     switch_snprintf(filename, 80, "%smail.%d%04x", SWITCH_GLOBAL_dirs.temp_dir, (int)switch_timestamp(NULL), rand() & 0xffff);
00370     
00371     if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644))) {
00372         if (file) {
00373             if ((ifd = open(file, O_RDONLY)) < 1) {
00374                 return SWITCH_FALSE;
00375             }
00376         }
00377         switch_snprintf(buf, B64BUFFLEN, "MIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"%s\"\n", bound);
00378         if (!write_buf(fd, buf)) {
00379             return SWITCH_FALSE;
00380         }
00381 
00382         if (headers && !write_buf(fd, headers))
00383             return SWITCH_FALSE;
00384 
00385         if (!write_buf(fd, "\n\n"))
00386             return SWITCH_FALSE;
00387 
00388                 if (body && switch_stristr("content-type", body)) {
00389                         switch_snprintf(buf, B64BUFFLEN, "--%s\n", bound);
00390                 } else {
00391                         switch_snprintf(buf, B64BUFFLEN, "--%s\nContent-Type: text/plain\n\n", bound);
00392                 }
00393         if (!write_buf(fd, buf))
00394             return SWITCH_FALSE;
00395 
00396         if (body) {
00397             if (!write_buf(fd, body)) {
00398                 return SWITCH_FALSE;
00399             }
00400         }
00401 
00402         if (file) {
00403                         const char *stipped_file = switch_cut_path(file);
00404                         const char *new_type;
00405                         char *ext;
00406 
00407                         if ((ext = strrchr(stipped_file, '.'))) {
00408                                 ext++;
00409                                 if ((new_type = switch_core_mime_ext2type(ext))) {
00410                                         mime_type = new_type;
00411                                 }
00412                         }
00413 
00414                         switch_snprintf(buf, B64BUFFLEN,
00415                                          "\n\n--%s\nContent-Type: %s; name=\"%s\"\n"
00416                                          "Content-ID: <ATTACHED@freeswitch.org>\n"
00417                                          "Content-Transfer-Encoding: base64\n"
00418                                          "Content-Description: Sound attachment.\n"
00419                                          "Content-Disposition: attachment; filename=\"%s\"\n\n",
00420                                          bound, mime_type, stipped_file, stipped_file);
00421             if (!write_buf(fd, buf))
00422                 return SWITCH_FALSE;
00423 
00424             while ((ilen = read(ifd, in, B64BUFFLEN))) {
00425                 for (x = 0; x < ilen; x++) {
00426                     b = (b << 8) + in[x];
00427                     l += 8;
00428                     while (l >= 6) {
00429                         out[bytes++] = switch_b64_table[(b >> (l -= 6)) % 64];
00430                         if (++y != 72)
00431                             continue;
00432                         out[bytes++] = '\n';
00433                         y = 0;
00434                     }
00435                 }
00436                 if (write(fd, &out, bytes) != bytes) {
00437                     return -1;
00438                 } else
00439                     bytes = 0;
00440 
00441             }
00442 
00443             if (l > 0) {
00444                 out[bytes++] = switch_b64_table[((b % 16) << (6 - l)) % 64];
00445             }
00446             if (l != 0)
00447                 while (l < 6) {
00448                     out[bytes++] = '=', l += 2;
00449                 }
00450             if (write(fd, &out, bytes) != bytes) {
00451                 return -1;
00452             }
00453 
00454         }
00455 
00456         switch_snprintf(buf, B64BUFFLEN, "\n\n--%s--\n.\n", bound);
00457         if (!write_buf(fd, buf))
00458             return SWITCH_FALSE;
00459     }
00460 
00461     if (fd) {
00462         close(fd);
00463     }
00464     if (ifd) {
00465         close(ifd);
00466     }
00467     switch_snprintf(buf, B64BUFFLEN, "/bin/cat %s | %s %s %s", filename, runtime.mailer_app, runtime.mailer_app_args, to);
00468     if (system(buf)) {
00469         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to execute command: %s\n", buf);
00470     }
00471 
00472         if (unlink(filename) != 0) {
00473                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "failed to delete file [%s]\n", filename);
00474         }
00475 
00476     if (file) {
00477         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed file [%s] to [%s]\n", filename, to);
00478     } else {
00479         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Emailed data to [%s]\n", to);
00480     }
00481 
00482     return SWITCH_TRUE;
00483 }

Here is the call graph for this function: