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

switch_utils.c File Reference


Data Structures

struct  switch_network_node
struct  switch_network_list

Defines

#define ESCAPE_META   '\\'
#define B64BUFFLEN   1024
#define switch_inet_ntop   inet_ntop

Typedefs

typedef switch_network_node switch_network_node_t

Functions

int switch_inet_pton (int af, const char *src, void *dst)
switch_status_t switch_network_list_create (switch_network_list_t **list, switch_bool_t default_type, switch_memory_pool_t *pool)
switch_bool_t switch_network_list_validate_ip (switch_network_list_t *list, uint32_t ip)
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)
int switch_parse_cidr (const char *string, uint32_t *ip, uint32_t *mask, uint32_t *bitp)
char * switch_find_end_paren (const char *s, char open, char close)
switch_size_t switch_fd_read_line (int fd, char *buf, switch_size_t len)
char * switch_amp_encode (char *s, char *buf, switch_size_t len)
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)
switch_bool_t switch_simple_email (const char *to, const char *from, const char *headers, const char *body, const char *file)
switch_bool_t switch_is_lan_addr (const char *ip)
switch_bool_t switch_ast2regex (char *pat, char *rbuf, size_t len)
char * switch_replace_char (char *str, char from, char to, switch_bool_t dup)
char * switch_strip_spaces (const char *str)
char * switch_separate_paren_args (char *str)
switch_bool_t switch_is_number (const char *str)
const char * switch_stristr (const char *instr, const char *str)
switch_status_t switch_find_local_ip (char *buf, int len, int family)
switch_time_t switch_str_time (const char *in)
 Converts a string representation of a date into a switch_time_t.
const char * switch_priority_name (switch_priority_t priority)
 Return a printable name of a switch_priority_t.
char * get_addr (char *buf, switch_size_t len, struct in_addr *in)
 find the char representation of an ip adress
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.
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.
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.
const char * switch_cut_path (const char *in)
 Create a pointer to the file name in a given file path eliminating the directory name.
switch_status_t switch_string_match (const char *string, size_t string_len, const char *search, size_t search_len)
char * switch_string_replace (const char *string, const char *search, const char *replace)
int switch_socket_waitfor (switch_pollfd_t *poll, int ms)
 Wait for a socket.
size_t switch_url_encode (const char *url, char *buf, size_t len)
char * switch_url_decode (char *s)


Define Documentation

#define B64BUFFLEN   1024
 

#define ESCAPE_META   '\\'
 

#define switch_inet_ntop   inet_ntop
 


Typedef Documentation

typedef struct switch_network_node switch_network_node_t
 


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 char *  buf,
int  len,
int  family
 

00669 {
00670         switch_status_t status = SWITCH_STATUS_FALSE;
00671         char *base;
00672 
00673 #ifdef WIN32
00674         SOCKET tmp_socket;
00675         SOCKADDR_STORAGE l_address;
00676         int l_address_len;
00677         struct addrinfo *address_info;
00678 #else
00679 #ifdef __Darwin__
00680         int ilen;
00681 #else
00682         unsigned int ilen;
00683 #endif
00684         int tmp_socket = -1, on = 1;
00685         char abuf[25] = "";
00686 #endif
00687 
00688         if (len < 16) {
00689                 return status;
00690         }
00691 
00692         switch_copy_string(buf, "127.0.0.1", len);
00693 
00694         switch (family) {
00695         case AF_INET:
00696                 base = "82.45.148.209";
00697                 break;
00698         case AF_INET6:
00699                 base = "52.2d.94.d1";
00700                 break;
00701         default:
00702                 base = "127.0.0.1";
00703                 break;
00704         }
00705 
00706 #ifdef WIN32
00707         tmp_socket = socket(family, SOCK_DGRAM, 0);
00708 
00709         getaddrinfo(base, NULL, NULL, &address_info);
00710 
00711         if (!address_info || WSAIoctl(tmp_socket,
00712                                  SIO_ROUTING_INTERFACE_QUERY,
00713                                  address_info->ai_addr, (DWORD) address_info->ai_addrlen, &l_address, sizeof(l_address), (LPDWORD) & l_address_len, NULL, NULL)) {
00714 
00715                 closesocket(tmp_socket);
00716                 if (address_info)
00717                         freeaddrinfo(address_info);
00718                 return status;
00719         }
00720 
00721         closesocket(tmp_socket);
00722         freeaddrinfo(address_info);
00723 
00724         if (!getnameinfo((const struct sockaddr *) &l_address, l_address_len, buf, len, NULL, 0, NI_NUMERICHOST)) {
00725 
00726                 status = SWITCH_STATUS_SUCCESS;
00727 
00728         }
00729 #else
00730 
00731         switch (family) {
00732         case AF_INET:
00733                 {
00734                         struct sockaddr_in iface_out;
00735                         struct sockaddr_in remote;
00736                         memset(&remote, 0, sizeof(struct sockaddr_in));
00737 
00738                         remote.sin_family = AF_INET;
00739                         remote.sin_addr.s_addr = inet_addr(base);
00740                         remote.sin_port = htons(4242);
00741 
00742                         memset(&iface_out, 0, sizeof(iface_out));
00743                         tmp_socket = socket(AF_INET, SOCK_DGRAM, 0);
00744 
00745                         if (setsockopt(tmp_socket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
00746                                 goto doh;
00747                         }
00748 
00749                         if (connect(tmp_socket, (struct sockaddr *) &remote, sizeof(struct sockaddr_in)) == -1) {
00750                                 goto doh;
00751                         }
00752 
00753                         ilen = sizeof(iface_out);
00754                         if (getsockname(tmp_socket, (struct sockaddr *) &iface_out, &ilen) == -1) {
00755                                 goto doh;
00756                         }
00757 
00758                         if (iface_out.sin_addr.s_addr == 0) {
00759                                 goto doh;
00760                         }
00761 
00762                         switch_copy_string(buf, get_addr(abuf, sizeof(abuf), &iface_out.sin_addr), len);
00763                         status = SWITCH_STATUS_SUCCESS;
00764                 }
00765                 break;
00766         case AF_INET6:
00767                 {
00768                         struct sockaddr_in6 iface_out;
00769                         struct sockaddr_in6 remote;
00770                         memset(&remote, 0, sizeof(struct sockaddr_in6));
00771 
00772                         remote.sin6_family = AF_INET6;
00773                         switch_inet_pton(AF_INET6, buf, &remote.sin6_addr);
00774                         remote.sin6_port = htons(4242);
00775 
00776                         memset(&iface_out, 0, sizeof(iface_out));
00777                         tmp_socket = socket(AF_INET6, SOCK_DGRAM, 0);
00778 
00779                         if (setsockopt(tmp_socket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
00780                                 goto doh;
00781                         }
00782 
00783                         if (connect(tmp_socket, (struct sockaddr *) &remote, sizeof(struct sockaddr_in)) == -1) {
00784                                 goto doh;
00785                         }
00786 
00787                         ilen = sizeof(iface_out);
00788                         if (getsockname(tmp_socket, (struct sockaddr *) &iface_out, &ilen) == -1) {
00789                                 goto doh;
00790                         }
00791 
00792                         if (iface_out.sin6_addr.s6_addr == 0) {
00793                                 goto doh;
00794                         }
00795 
00796                         inet_ntop(AF_INET6, (const void *) &iface_out.sin6_addr, buf, len - 1);
00797                         status = SWITCH_STATUS_SUCCESS;
00798                 }
00799                 break;
00800         }
00801 
00802   doh:
00803         if (tmp_socket > 0) {
00804                 close(tmp_socket);
00805                 tmp_socket = -1;
00806         }
00807 #endif
00808 
00809         return status;
00810 }

Here is the call graph for this function:

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

00058 {
00059         return inet_pton(af, src, dst);
00060 }

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:

int switch_socket_waitfor switch_pollfd_t poll,
int  ms
 

Wait for a socket.

Parameters:
poll the pollfd to wait on
ms the number of milliseconds to wait
Returns:
the requested condition
01393 {
01394         int nsds = 0;
01395 
01396         switch_poll(poll, 1, &nsds, ms);
01397 
01398         return nsds;
01399 }

Here is the call graph for this function:

switch_time_t switch_str_time const char *  in  ) 
 

Converts a string representation of a date into a switch_time_t.

Parameters:
in the string
Returns:
the epoch time in usec
00813 {
00814         switch_time_exp_t tm = { 0 };
00815         int proceed = 0, ovector[30];
00816         switch_regex_t *re = NULL;
00817         char replace[1024] = "";
00818         switch_time_t ret = 0;
00819         char *pattern = "^(\\d+)-(\\d+)-(\\d+)\\s*(\\d*):{0,1}(\\d*):{0,1}(\\d*)";
00820 
00821         switch_time_exp_lt(&tm, switch_timestamp_now());
00822         tm.tm_year = tm.tm_mon = tm.tm_mday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
00823 
00824         if ((proceed = switch_regex_perform(in, pattern, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
00825 
00826                 if (proceed > 1) {
00827                         switch_regex_copy_substring(in, ovector, proceed, 1, replace, sizeof(replace));
00828                         tm.tm_year = atoi(replace) - 1900;
00829                 }
00830 
00831                 if (proceed > 2) {
00832                         switch_regex_copy_substring(in, ovector, proceed, 2, replace, sizeof(replace));
00833                         tm.tm_mon = atoi(replace) - 1;
00834                 }
00835 
00836                 if (proceed > 3) {
00837                         switch_regex_copy_substring(in, ovector, proceed, 3, replace, sizeof(replace));
00838                         tm.tm_mday = atoi(replace);
00839                 }
00840 
00841                 if (proceed > 4) {
00842                         switch_regex_copy_substring(in, ovector, proceed, 4, replace, sizeof(replace));
00843                         tm.tm_hour = atoi(replace);
00844                 }
00845 
00846                 if (proceed > 5) {
00847                         switch_regex_copy_substring(in, ovector, proceed, 5, replace, sizeof(replace));
00848                         tm.tm_min = atoi(replace);
00849                 }
00850 
00851                 if (proceed > 6) {
00852                         switch_regex_copy_substring(in, ovector, proceed, 6, replace, sizeof(replace));
00853                         tm.tm_sec = atoi(replace);
00854                 }
00855 
00856                 switch_time_exp_gmt_get(&ret, &tm);
00857                 return ret;
00858         }
00859         /* possible else with more patterns later */
00860         return ret;
00861 }

Here is the call graph for this function:

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

01341 {
01342         size_t i;
01343 
01344         for (i = 0; (i < search_len) && (i < string_len); i++) {
01345                 if (string[i] != search[i]) {
01346                         return SWITCH_STATUS_FALSE;
01347                 }
01348         }
01349 
01350         if (i == search_len) {
01351                 return SWITCH_STATUS_SUCCESS;
01352         }
01353 
01354         return SWITCH_STATUS_FALSE;
01355 }

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

01358 {
01359         size_t string_len = strlen(string);
01360         size_t search_len = strlen(search);
01361         size_t replace_len = strlen(replace);
01362         size_t i, n;
01363         size_t dest_len = 0;
01364         char *dest, *tmp;
01365 
01366         dest = (char *) malloc(sizeof(char));
01367         switch_assert(dest);
01368 
01369         for (i = 0; i < string_len; i++) {
01370                 if (switch_string_match(string + i, string_len - i, search, search_len) == SWITCH_STATUS_SUCCESS) {
01371                         for (n = 0; n < replace_len; n++) {
01372                                 dest[dest_len] = replace[n];
01373                                 dest_len++;
01374                                 tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
01375                                 switch_assert(tmp);
01376                                 dest = tmp;
01377                         }
01378                         i += search_len - 1;
01379                 } else {
01380                         dest[dest_len] = string[i];
01381                         dest_len++;
01382                         tmp = (char *) realloc(dest, sizeof(char) * (dest_len + 1));
01383                         switch_assert(tmp);
01384                         dest = tmp;
01385                 }
01386         }
01387 
01388         dest[dest_len] = 0;
01389         return dest;
01390 }

Here is the call graph for this function:

char* switch_strip_spaces const char *  str  ) 
 

00561 {
00562         const char *sp = str;
00563         char *p, *s = NULL;
00564         
00565         if (!sp) return NULL;
00566 
00567         while(*sp == ' ') {
00568                 sp++;
00569         }
00570         
00571         s = strdup(sp);
00572 
00573         if (!s) return NULL;
00574 
00575         p = s + (strlen(s) - 1);
00576 
00577         while(*p == ' ') {
00578                 *p-- = '\0';
00579         }
00580         
00581         return s;
00582 }

const char* switch_stristr const char *  instr,
const char *  str
 

00629 {
00630 /*
00631 ** Rev History:  16/07/97  Greg Thayer          Optimized
00632 **               07/04/95  Bob Stout            ANSI-fy
00633 **               02/03/94  Fred Cole            Original
00634 **               09/01/03  Bob Stout            Bug fix (lines 40-41) per Fred Bulback
00635 **
00636 ** Hereby donated to public domain.
00637 */
00638         const char *pptr, *sptr, *start;
00639 
00640         if (!str || !instr)
00641                 return NULL;
00642 
00643         for (start = str; *start; start++) {
00644                 /* find start of pattern in string */
00645                 for ( ; ((*start) && (toupper(*start) != toupper(*instr))); start++);
00646 
00647                 if (!*start)
00648                         return NULL;
00649 
00650                 pptr = instr;
00651                 sptr = start;
00652 
00653                 while (toupper(*sptr) == toupper(*pptr)) {
00654                         sptr++;
00655                         pptr++;
00656 
00657                         /* if end of pattern then pattern was found */
00658                         if (!*pptr)
00659                                 return (start);
00660 
00661                         if (!*sptr)
00662                                 return NULL;
00663                 }
00664         }
00665         return NULL;
00666 }

char* switch_url_decode char *  s  ) 
 

01439 {
01440         char *o;
01441         unsigned int tmp;
01442 
01443         for (o = s; *s; s++, o++) {
01444                 if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
01445                         *o = (char) tmp;
01446                         s += 2;
01447                 } else {
01448                         *o = *s;
01449                 }
01450         }
01451         *o = '\0';
01452         return s;
01453 }

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

01402 {
01403         const char *p;
01404         size_t x = 0;
01405         const char urlunsafe[] = "\r\n \"#%&+:;<=>?@[\\]^`{|}";
01406         const char hex[] = "0123456789ABCDEF";
01407 
01408         if (!buf) {
01409                 return 0;
01410         }
01411 
01412         if (!url) {
01413                 return 0;
01414         }
01415 
01416         len--;
01417 
01418         for (p = url; *p; p++) {
01419                 if (x >= len) {
01420                         break;
01421                 }
01422                 if (*p < ' ' || *p > '~' || strchr(urlunsafe, *p)) {
01423                         if ((x + 3) >= len) {
01424                                 break;
01425                         }
01426                         buf[x++] = '%';
01427                         buf[x++] = hex[*p >> 4];
01428                         buf[x++] = hex[*p & 0x0f];
01429                 } else {
01430                         buf[x++] = *p;
01431                 }
01432         }
01433         buf[x] = '\0';
01434 
01435         return x;
01436 }


Generated on Mon May 26 22:06:53 2008 for FreeSWITCH by  doxygen 1.3.9.1