2005-11-19 20:07:43 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_utils.c -- Compatability and Helper Code
|
|
|
|
*
|
|
|
|
*/
|
2006-02-26 00:12:17 +00:00
|
|
|
#include <switch.h>
|
2006-04-14 16:45:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-02-26 00:12:17 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_priority_name(switch_priority_t priority)
|
|
|
|
{
|
|
|
|
switch(priority) { /*lol*/
|
|
|
|
case SWITCH_PRIORITY_NORMAL:
|
|
|
|
return "NORMAL";
|
|
|
|
case SWITCH_PRIORITY_LOW:
|
|
|
|
return "LOW";
|
|
|
|
case SWITCH_PRIORITY_HIGH:
|
|
|
|
return "HIGH";
|
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
2005-12-06 17:18:56 +00:00
|
|
|
|
2006-02-23 22:41:08 +00:00
|
|
|
static char RFC2833_CHARS[] = "0123456789*#ABCDF";
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char) switch_rfc2833_to_char(int event)
|
|
|
|
{
|
2006-03-30 23:02:50 +00:00
|
|
|
if (event > -1 && event < sizeof(RFC2833_CHARS)) {
|
|
|
|
return RFC2833_CHARS[event];
|
|
|
|
}
|
|
|
|
return '\0';
|
2006-02-23 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-24 00:02:02 +00:00
|
|
|
SWITCH_DECLARE(unsigned char) switch_char_to_rfc2833(char key)
|
2006-02-23 22:41:08 +00:00
|
|
|
{
|
|
|
|
char *c;
|
2006-03-30 23:02:50 +00:00
|
|
|
unsigned char counter = 0;
|
2006-02-23 22:41:08 +00:00
|
|
|
|
|
|
|
for (c = RFC2833_CHARS; *c ; c++) {
|
|
|
|
if (*c == key) {
|
2006-03-30 23:02:50 +00:00
|
|
|
return counter;
|
2006-02-23 22:41:08 +00:00
|
|
|
}
|
2006-03-30 23:02:50 +00:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
return '\0';
|
2006-02-23 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2005-12-06 17:18:56 +00:00
|
|
|
SWITCH_DECLARE(unsigned int) switch_separate_string(char *buf, char delim, char **array, int arraylen)
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char *scan;
|
|
|
|
int paren = 0;
|
|
|
|
|
|
|
|
if (!buf || !array || !arraylen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(array, 0, arraylen * sizeof(*array));
|
|
|
|
|
|
|
|
scan = buf;
|
|
|
|
|
|
|
|
for (argc = 0; *scan && (argc < arraylen - 1); argc++) {
|
|
|
|
array[argc] = scan;
|
|
|
|
for (; *scan; scan++) {
|
|
|
|
if (*scan == '(')
|
|
|
|
paren++;
|
|
|
|
else if (*scan == ')') {
|
|
|
|
if (paren)
|
|
|
|
paren--;
|
|
|
|
} else if ((*scan == delim) && !paren) {
|
|
|
|
*scan++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*scan) {
|
|
|
|
array[argc++] = scan;
|
|
|
|
}
|
|
|
|
|
|
|
|
return argc;
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
|
2006-01-06 02:01:11 +00:00
|
|
|
SWITCH_DECLARE(char *) switch_cut_path(char *in)
|
|
|
|
{
|
|
|
|
char *p, *ret = in;
|
|
|
|
char delims[] = "/\\";
|
|
|
|
char *i;
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
for (i = delims; *i; i++) {
|
2006-01-06 02:01:11 +00:00
|
|
|
p = in;
|
2006-02-20 00:23:25 +00:00
|
|
|
while ((p = strchr(p, *i)) != 0) {
|
2006-01-06 02:01:11 +00:00
|
|
|
ret = ++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_socket_create_pollfd(switch_pollfd_t *poll, switch_socket_t *sock,
|
2006-02-20 00:23:25 +00:00
|
|
|
switch_int16_t flags, switch_memory_pool *pool)
|
2005-11-19 20:07:43 +00:00
|
|
|
{
|
|
|
|
switch_pollset_t *pollset;
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
if (switch_pollset_create(&pollset, 1, pool, flags) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
poll->desc_type = SWITCH_POLL_SOCKET;
|
|
|
|
poll->reqevents = flags;
|
|
|
|
poll->desc.s = sock;
|
|
|
|
poll->client_data = sock;
|
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
if (switch_pollset_add(pollset, poll) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
|
|
|
|
2006-04-14 16:45:31 +00:00
|
|
|
SWITCH_DECLARE(switch_status) switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; (i < search_len) && (i < string_len); i++) {
|
|
|
|
if (string[i] != search[i]) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == search_len) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(char *) switch_string_replace(const char *string, const char *search, const char *replace)
|
|
|
|
{
|
|
|
|
size_t string_len = strlen(string);
|
|
|
|
size_t search_len = strlen(search);
|
|
|
|
size_t replace_len = strlen(replace);
|
|
|
|
size_t i, n;
|
|
|
|
size_t dest_len = 0;
|
|
|
|
char *dest;
|
|
|
|
|
|
|
|
dest = (char *)malloc(sizeof(char));
|
|
|
|
|
|
|
|
for (i = 0; i < string_len; i++) {
|
|
|
|
if (switch_string_match(string + i, string_len - i, search, search_len) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
for (n = 0; n < replace_len; n++) {
|
|
|
|
dest[dest_len] = replace[n];
|
|
|
|
dest_len++;
|
|
|
|
dest = (char *)realloc(dest, sizeof(char)*(dest_len+1));
|
|
|
|
}
|
|
|
|
i += search_len-1;
|
|
|
|
} else {
|
|
|
|
dest[dest_len] = string[i];
|
|
|
|
dest_len++;
|
|
|
|
dest = (char *)realloc(dest, sizeof(char)*(dest_len+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dest[dest_len] = 0;
|
|
|
|
return dest;
|
|
|
|
}
|
2005-11-19 20:07:43 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_socket_waitfor(switch_pollfd_t *poll, int ms)
|
|
|
|
{
|
|
|
|
int nsds = 0;
|
2006-01-03 01:17:59 +00:00
|
|
|
|
2006-03-30 23:02:50 +00:00
|
|
|
if (switch_poll(poll, 1, &nsds, ms) != SWITCH_STATUS_SUCCESS) {
|
2005-11-19 20:07:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-01-03 01:17:59 +00:00
|
|
|
return nsds;
|
2005-11-19 20:07:43 +00:00
|
|
|
}
|
2005-12-23 03:39:33 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
//this forces certain symbols to not be optimized out of the dll
|
|
|
|
void include_me(void)
|
|
|
|
{
|
|
|
|
apr_socket_shutdown(NULL, 0);
|
2006-01-20 15:05:05 +00:00
|
|
|
apr_socket_recvfrom(NULL, NULL, 0, NULL, NULL);
|
2006-02-14 22:58:07 +00:00
|
|
|
apr_mcast_join(NULL, NULL, NULL, NULL);
|
|
|
|
apr_socket_opt_set(NULL, 0, 0);
|
2005-12-23 03:39:33 +00:00
|
|
|
}
|
|
|
|
#endif
|