Freeswitch-svn
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
December 2008
- 13 participants
- 473 discussions
21 Dec '08
Author: mikej
Date: Sun Dec 21 17:13:20 2008
New Revision: 10899
Log:
start of windows port
Modified:
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/esl_config.c
freeswitch/trunk/libs/esl/src/include/esl.h
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sun Dec 21 17:13:20 2008
@@ -32,13 +32,16 @@
*/
#include <esl.h>
+#ifndef WIN32
+#define closesocket(x) close(x)
+#endif
#ifndef HAVE_GETHOSTBYNAME_R
extern int gethostbyname_r (const char *__name,
struct hostent *__result_buf,
char *__buf, size_t __buflen,
struct hostent **__result,
- int *__h_errnop);
+ int *__h_errnump);
#endif
@@ -440,7 +443,7 @@
end:
if (server_sock != ESL_SOCK_INVALID) {
- close(server_sock);
+ closesocket(server_sock);
server_sock = ESL_SOCK_INVALID;
}
@@ -474,14 +477,14 @@
memset(&handle->hostent, 0, sizeof(handle->hostent));
#ifdef HAVE_GETHOSTBYNAME_R_FIVE
- rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &handle->errno);
+ rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &handle->errnum);
result = handle->hostent;
#else
- rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &result, &handle->errno);
+ rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &result, &handle->errnum);
#endif
if (rval) {
- strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ strerror_r(handle->errnum, handle->err, sizeof(handle->err));
goto fail;
}
@@ -546,7 +549,7 @@
}
if (handle->sock != ESL_SOCK_INVALID) {
- close(handle->sock);
+ closesocket(handle->sock);
handle->sock = ESL_SOCK_INVALID;
return ESL_SUCCESS;
}
@@ -604,7 +607,7 @@
char *hname, *hval;
char *col;
char *cl;
- ssize_t len;
+ esl_ssize_t len;
int zc = 0;
esl_mutex_lock(handle->mutex);
@@ -625,7 +628,7 @@
return ESL_FAIL;
}
} else if (rrval < 0) {
- strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ strerror_r(handle->errnum, handle->err, sizeof(handle->err));
goto fail;
} else {
zc = 0;
@@ -685,7 +688,7 @@
do {
esl_ssize_t r;
if ((r = recv(handle->sock, body + sofar, len - sofar, 0)) < 0) {
- strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ strerror_r(handle->errnum, handle->err, sizeof(handle->err));
goto fail;
}
sofar += r;
@@ -717,7 +720,7 @@
char *hname, *hval;
char *col;
char *cl;
- ssize_t len;
+ esl_ssize_t len;
char *c;
esl_event_safe_destroy(&handle->last_ievent);
@@ -775,7 +778,7 @@
do {
esl_ssize_t r;
if ((r = recv(handle->sock, body + sofar, len - sofar, 0)) < 0) {
- strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ strerror_r(handle->errnum, handle->err, sizeof(handle->err));
goto fail;
}
sofar += r;
Modified: freeswitch/trunk/libs/esl/src/esl_config.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl_config.c (original)
+++ freeswitch/trunk/libs/esl/src/esl_config.c Sun Dec 21 17:13:20 2008
@@ -214,19 +214,24 @@
char cas_bits[5];
unsigned char bit = 0x8;
char *double_colon = strchr(strvalue, ':');
+ int x = 0;
+
if (!double_colon) {
esl_log(ESL_LOG_ERROR, "No CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
return -1;
}
+
double_colon++;
*outbits = 0;
cas_bits[4] = 0;
+
if (sscanf(double_colon, "%c%c%c%c", &cas_bits[0], &cas_bits[1], &cas_bits[2], &cas_bits[3]) != 4) {
esl_log(ESL_LOG_ERROR, "Invalid CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
return -1;
}
+
esl_log(ESL_LOG_DEBUG, "CAS bits specification found: %s\n", cas_bits);
- int x = 0;
+
for (; cas_bits[x]; x++) {
if ('1' == cas_bits[x]) {
*outbits |= bit;
Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h (original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Sun Dec 21 17:13:20 2008
@@ -153,9 +153,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#ifndef WIN32
#include <sys/signal.h>
#include <unistd.h>
#include <ctype.h>
+#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
@@ -168,8 +170,9 @@
#define esl_strlen_zero_buf(s) (*(s) == '\0')
#ifdef WIN32
+#include <winsock2.h>
#include <windows.h>
-typedef HANDLE esl_socket_t;
+typedef SOCKET esl_socket_t;
typedef unsigned __int64 uint64_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int16 uint16_t;
@@ -180,6 +183,7 @@
typedef __int8 int8_t;
typedef intptr_t esl_ssize_t;
typedef int esl_filehandle_t;
+#define ESL_SOCK_INVALID INVALID_SOCKET
#else
#include <stdint.h>
#include <sys/types.h>
@@ -211,7 +215,7 @@
char hostbuf[256];
esl_socket_t sock;
char err[256];
- int errno;
+ int errnum;
char header_buf[4196];
char last_reply[1024];
char last_sr_reply[1024];
1
0
21 Dec '08
Author: anthm
Date: Sun Dec 21 16:54:43 2008
New Revision: 10898
Log:
update
Modified:
freeswitch/trunk/libs/esl/Makefile
freeswitch/trunk/libs/esl/fs_cli.c
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/esl_event.c
freeswitch/trunk/libs/esl/src/include/esl.h
freeswitch/trunk/libs/esl/testclient.c
Modified: freeswitch/trunk/libs/esl/Makefile
==============================================================================
--- freeswitch/trunk/libs/esl/Makefile (original)
+++ freeswitch/trunk/libs/esl/Makefile Sun Dec 21 16:54:43 2008
@@ -1,15 +1,19 @@
PWD=$(shell pwd)
INCS=-I$(PWD)/src/include
LIBEDIT_DIR=../../libs/libedit
-CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
+DEBUG=-g -ggdb
+PICKY=-O2 -ffast-math -Wall -Werror -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes
+CFLAGS=$(INCS) -D_GNU_SOURCE $(DEBUG) -I$(LIBEDIT_DIR)/src/ $(PICKY)
MYLIB=libesl.a
LIBS=-lesl -lncurses -lpthread
LDFLAGS=-L.
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
+SRC=src/esl.c src/esl_event.c src/esl_threadmutex.c src/esl_config.c
+HEADERS=src/include/esl_config.h src/include/esl_event.h src/include/esl.h src/include/esl_threadmutex.h
all: $(MYLIB) fs_cli testclient testserver
-$(MYLIB): $(OBJS) $(HEADERS)
+$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
ar rcs $(MYLIB) $(OBJS)
ranlib $(MYLIB)
Modified: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- freeswitch/trunk/libs/esl/fs_cli.c (original)
+++ freeswitch/trunk/libs/esl/fs_cli.c Sun Dec 21 16:54:43 2008
@@ -5,28 +5,21 @@
#include <sys/select.h>
#include <histedit.h>
+
static char prompt_str[512] = "";
static char hostname[512] = "";
-char *prompt(EditLine * e)
+static char *prompt(EditLine * e)
{
- if (*prompt_str == '\0') {
- gethostname(hostname, sizeof(hostname));
- snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
- }
-
return prompt_str;
-
}
static EditLine *el;
static History *myhistory;
static HistEvent ev;
-static char *hfile = NULL;
static int running = 1;
static int thread_running = 0;
-
static void handle_SIGINT(int sig)
{
if (sig);
@@ -89,8 +82,6 @@
usleep(1000);
}
- done:
-
thread_running = 0;
esl_log(ESL_LOG_DEBUG, "Thread Done\n");
@@ -131,17 +122,33 @@
}
typedef struct {
+ char name[128];
char host[128];
- char pass[128];
esl_port_t port;
+ char pass[128];
} cli_profile_t;
-static cli_profile_t profiles[128] = { 0 };
+static cli_profile_t profiles[128] = {{{0}}};
static int pcount;
+
+static int get_profile(const char *name, cli_profile_t **profile)
+{
+ int x;
+
+ for (x = 0; x < pcount; x++) {
+ if (!strcmp(profiles[x].name, name)) {
+ *profile = &profiles[x];
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
int main(int argc, char *argv[])
{
- esl_handle_t handle = {0};
+ esl_handle_t handle = {{0}};
int count;
const char *line;
char cmd_str[1024] = "";
@@ -150,11 +157,13 @@
char *home = getenv("HOME");
esl_config_t cfg;
cli_profile_t *profile = &profiles[0];
+ int cur = 0;
strncpy(profiles[0].host, "localhost", sizeof(profiles[0].host));
strncpy(profiles[0].pass, "ClueCon", sizeof(profiles[0].pass));
+ strncpy(profiles[0].name, "default", sizeof(profiles[0].name));
profiles[0].port = 8021;
- pcount = 1;
+ pcount++;
if (home) {
snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
@@ -165,27 +174,58 @@
gethostname(hostname, sizeof(hostname));
handle.debug = 0;
-
+ esl_global_set_default_logger(7);
if (esl_config_open_file(&cfg, cfile)) {
char *var, *val;
+ char cur_cat[128] = "";
+
while (esl_config_next_pair(&cfg, &var, &val)) {
+ if (strcmp(cur_cat, cfg.category)) {
+ cur++;
+ esl_set_string(cur_cat, cfg.category);
+ esl_set_string(profiles[cur].name, cur_cat);
+ esl_set_string(profiles[cur].host, "localhost");
+ esl_set_string(profiles[cur].pass, "ClueCon");
+ profiles[cur].port = 8021;
+ esl_log(ESL_LOG_INFO, "Found Profile [%s]\n", profiles[cur].name);
+ pcount++;
+ }
+ if (!strcasecmp(var, "host")) {
+ esl_set_string(profiles[cur].host, val);
+ } else if (!strcasecmp(var, "password")) {
+ esl_set_string(profiles[cur].pass, val);
+ } else if (!strcasecmp(var, "port")) {
+ int pt = atoi(val);
+ if (pt > 0) {
+ profiles[cur].port = pt;
+ }
+ }
}
esl_config_close_file(&cfg);
}
-
- if (esl_connect(&handle, profile->host, profile->port, profile->pass)) {
- printf("Error Connecting [%s]\n", handle.err);
- return -1;
+ if (argv[1]) {
+ if (get_profile(argv[1], &profile)) {
+ esl_log(ESL_LOG_INFO, "Chosen profile %s does not exist using builtin default\n", argv[1]);
+ profile = &profiles[0];
+ } else {
+ esl_log(ESL_LOG_INFO, "Chosen profile %s\n", profile->name);
+ }
}
+ esl_log(ESL_LOG_INFO, "Using profile %s\n", profile->name);
+
+ gethostname(hostname, sizeof(hostname));
+ snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", profile->name);
- if (handle.debug) {
- esl_global_set_default_logger(7);
+
+ if (esl_connect(&handle, profile->host, profile->port, profile->pass)) {
+ esl_log(ESL_LOG_ERROR, "Error Connecting [%s]\n", handle.err);
+ return -1;
}
-
+
esl_thread_create_detached(msg_thread_run, &handle);
el = el_init(__FILE__, stdout, stdout, stdout);
@@ -194,7 +234,7 @@
myhistory = history_init();
if (myhistory == 0) {
- fprintf(stderr, "history could not be initialized\n");
+ esl_log(ESL_LOG_ERROR, "history could not be initialized\n");
goto done;
}
@@ -202,10 +242,11 @@
el_set(el, EL_HIST, history, myhistory);
history(myhistory, &ev, H_LOAD, hfile);
-
snprintf(cmd_str, sizeof(cmd_str), "log info\n\n");
esl_send_recv(&handle, cmd_str);
+ esl_log(ESL_LOG_INFO, "FS CLI Ready.\n");
+
while (running) {
line = el_gets(el, &count);
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sun Dec 21 16:54:43 2008
@@ -32,7 +32,6 @@
*/
#include <esl.h>
-#include <sys/signal.h>
#ifndef HAVE_GETHOSTBYNAME_R
extern int gethostbyname_r (const char *__name,
@@ -367,6 +366,8 @@
send(handle->sock, "\n\n", 2, 0);
free(txt);
+
+ return ESL_SUCCESS;
}
esl_status_t esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid)
@@ -390,7 +391,7 @@
snprintf(send_buf, sizeof(send_buf), "%s\ncall-command: execute\n%s%s\n", cmd_buf, app_buf, arg_buf);
- esl_send_recv(handle, send_buf);
+ return esl_send_recv(handle, send_buf);
}
esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback)
@@ -452,7 +453,6 @@
struct hostent *result;
char sendbuf[256];
- char recvbuf[256];
int rval;
const char *hval;
@@ -560,7 +560,7 @@
{
fd_set rfds, efds;
struct timeval tv = { 0, ms * 1000 };
- int max, activity, i = 0;
+ int max, activity;
esl_status_t status = ESL_SUCCESS;
esl_mutex_lock(handle->mutex);
@@ -820,21 +820,31 @@
esl_log(ESL_LOG_DEBUG, "SEND\n%s\n", cmd);
}
- send(handle->sock, cmd, strlen(cmd), 0);
+ if (send(handle->sock, cmd, strlen(cmd), 0)) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ return ESL_FAIL;
+ }
if (!(*e == '\n' && *(e-1) == '\n')) {
- send(handle->sock, "\n\n", 2, 0);
+ if (send(handle->sock, "\n\n", 2, 0)) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ return ESL_FAIL;
+ }
}
+
+ return ESL_SUCCESS;
+
}
esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd)
{
const char *hval;
-
+ esl_status_t status;
+
esl_mutex_lock(handle->mutex);
esl_send(handle, cmd);
- esl_recv_event(handle, &handle->last_sr_event);
+ status = esl_recv_event(handle, &handle->last_sr_event);
if (handle->last_sr_event) {
hval = esl_event_get_header(handle->last_sr_event, "reply-text");
@@ -845,6 +855,8 @@
}
esl_mutex_unlock(handle->mutex);
+
+ return status;
}
Modified: freeswitch/trunk/libs/esl/src/esl_event.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl_event.c (original)
+++ freeswitch/trunk/libs/esl/src/esl_event.c Sun Dec 21 16:54:43 2008
@@ -31,6 +31,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+
#include <esl.h>
#include <esl_event.h>
@@ -56,7 +57,7 @@
/* make sure this is synced with the esl_event_types_t enum in esl_types.h
also never put any new ones before EVENT_ALL
*/
-static char *EVENT_NAMES[] = {
+static const char *EVENT_NAMES[] = {
"CUSTOM",
"CHANNEL_CREATE",
"CHANNEL_DESTROY",
@@ -277,7 +278,7 @@
return status;
}
-esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data)
+static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data)
{
esl_event_header_t *header;
esl_ssize_t hlen = -1;
Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h (original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Sun Dec 21 16:54:43 2008
@@ -149,15 +149,19 @@
#include <sys/time.h>
#endif
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/signal.h>
+#include <unistd.h>
+#include <ctype.h>
+
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <assert.h>
-
#define esl_assert(_x) assert(_x)
#define esl_safe_free(_x) if (_x) free(_x); _x = NULL
#define esl_strlen_zero(s) (!s || *(s) == '\0')
@@ -261,6 +265,7 @@
size_t esl_url_encode(const char *url, char *buf, size_t len);
char *esl_url_decode(char *s);
+const char *esl_stristr(const char *instr, const char *str);
int esl_toupper(int c);
int esl_tolower(int c);
Modified: freeswitch/trunk/libs/esl/testclient.c
==============================================================================
--- freeswitch/trunk/libs/esl/testclient.c (original)
+++ freeswitch/trunk/libs/esl/testclient.c Sun Dec 21 16:54:43 2008
@@ -5,7 +5,7 @@
int main(void)
{
- esl_handle_t handle = {0};
+ esl_handle_t handle = {{0}};
handle.debug = 1;
1
0
Author: brian
Date: Sun Dec 21 14:48:33 2008
New Revision: 10897
Log:
update vars
Modified:
freeswitch/trunk/conf/vars.xml
Modified: freeswitch/trunk/conf/vars.xml
==============================================================================
--- freeswitch/trunk/conf/vars.xml (original)
+++ freeswitch/trunk/conf/vars.xml Sun Dec 21 14:48:33 2008
@@ -35,6 +35,8 @@
speex@32000h@20i - Speex 32kHz using 20ms ptime.
G7221@16000h - G722.1 16kHz (aka Siren 7)
G7221@32000h - G722.1C 32kHz (aka Siren 14)
+ CELT@32000h - CELT 32kHz, only 10ms supported
+ CELT@48000h - CELT 48kHz, only 10ms supported
GSM@40i - GSM 8kHz using 40ms ptime. (GSM is done in multiples of 20, Default is 20ms)
G722 - G722 16kHz using default 20ms ptime. (multiples of 10)
PCMU - G711 8kHz ulaw using default 20ms ptime. (multiples of 10)
1
0
20 Dec '08
Author: anthm
Date: Sat Dec 20 13:34:34 2008
New Revision: 10896
Log:
update
Modified:
freeswitch/trunk/libs/esl/fs_cli.c
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/include/esl.h
Modified: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- freeswitch/trunk/libs/esl/fs_cli.c (original)
+++ freeswitch/trunk/libs/esl/fs_cli.c Sat Dec 20 13:34:34 2008
@@ -25,7 +25,7 @@
static char *hfile = NULL;
static int running = 1;
static int thread_running = 0;
-static esl_mutex_t *global_mutex;
+
static void handle_SIGINT(int sig)
{
@@ -45,30 +45,12 @@
thread_running = 1;
while(thread_running && handle->connected) {
- fd_set rfds, efds;
- struct timeval tv = { 0, 10 * 1000 };
- int max, activity, i = 0;
-
- esl_mutex_lock(global_mutex);
- FD_ZERO(&rfds);
- FD_ZERO(&efds);
- FD_SET(handle->sock, &rfds);
- FD_SET(handle->sock, &efds);
-
- max = handle->sock + 1;
+ esl_status_t status = esl_recv_timed(handle, 10);
- if ((activity = select(max, &rfds, NULL, &efds, &tv)) < 0) {
- esl_mutex_unlock(global_mutex);
- goto done;
- }
- if (activity && FD_ISSET(handle->sock, &rfds)) {
- if (esl_recv(handle)) {
- running = thread_running = 0;
- esl_mutex_unlock(global_mutex);
- esl_log(ESL_LOG_WARNING, "Disconnected.\n");
- goto done;
- }
-
+ if (status == ESL_FAIL) {
+ esl_log(ESL_LOG_WARNING, "Disconnected.\n");
+ running = thread_running = 0;
+ } else if (status == ESL_SUCCESS) {
if (handle->last_event) {
const char *type = esl_event_get_header(handle->last_event, "content-type");
int known = 0;
@@ -102,10 +84,8 @@
printf("INCOMING DATA [%s]\n%s", type, handle->last_event->body);
}
}
-
}
- esl_mutex_unlock(global_mutex);
usleep(1000);
}
@@ -135,10 +115,10 @@
!strncasecmp(cmd, "nolog", 5) ||
!strncasecmp(cmd, "filter", 6)
) {
- esl_mutex_lock(global_mutex);
+
esl_send_recv(handle, cmd);
- printf("%s\n", handle->last_reply);
- esl_mutex_unlock(global_mutex);
+ printf("%s\n", handle->last_sr_reply);
+
goto end;
}
@@ -181,8 +161,6 @@
snprintf(cfile, sizeof(cfile), "%s/.fs_cli_config", home);
}
- esl_mutex_create(&global_mutex);
-
signal(SIGINT, handle_SIGINT);
gethostname(hostname, sizeof(hostname));
@@ -226,9 +204,7 @@
snprintf(cmd_str, sizeof(cmd_str), "log info\n\n");
- esl_mutex_lock(global_mutex);
esl_send_recv(&handle, cmd_str);
- esl_mutex_unlock(global_mutex);
while (running) {
@@ -254,10 +230,10 @@
}
} else {
snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", cmd);
- esl_mutex_lock(global_mutex);
esl_send_recv(&handle, cmd_str);
- printf("%s\n", handle.last_event->body);
- esl_mutex_unlock(global_mutex);
+ if (handle.last_sr_event) {
+ printf("%s\n", handle.last_sr_event->body);
+ }
}
el_deletestr(el, strlen(foo) + 1);
@@ -283,7 +259,5 @@
thread_running = 0;
- esl_mutex_destroy(&global_mutex);
-
return 0;
}
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sat Dec 20 13:34:34 2008
@@ -322,6 +322,76 @@
return s;
}
+esl_status_t esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in addr)
+{
+ handle->sock = socket;
+ handle->addr = addr;
+
+ if (handle->sock == ESL_SOCK_INVALID) {
+ return ESL_FAIL;
+ }
+
+
+ if (!handle->mutex) {
+ esl_mutex_create(&handle->mutex);
+ }
+
+ handle->connected = 1;
+
+ esl_send_recv(handle, "connect\n\n");
+
+
+ if (handle->last_sr_event) {
+ handle->info_event = handle->last_sr_event;
+ handle->last_sr_event = NULL;
+ return ESL_SUCCESS;
+ }
+
+ handle->connected = 0;
+
+ return ESL_FAIL;
+}
+
+esl_status_t esl_sendevent(esl_handle_t *handle, esl_event_t *event)
+{
+ char *txt;
+
+ esl_event_serialize(handle->last_ievent, &txt, ESL_TRUE);
+
+ if (handle->debug) {
+ esl_log(ESL_LOG_DEBUG, "SEND EVENT\n%s\n", txt);
+ }
+
+ send(handle->sock, "sendevent\n", 10, 0);
+ send(handle->sock, txt, strlen(txt), 0);
+ send(handle->sock, "\n\n", 2, 0);
+
+ free(txt);
+}
+
+esl_status_t esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid)
+{
+ char cmd_buf[128] = "sendmsg";
+ char app_buf[512] = "";
+ char arg_buf[512] = "";
+ char send_buf[1292] = "";
+
+ if (uuid) {
+ snprintf(cmd_buf, sizeof(cmd_buf), "sendmsg %s", uuid);
+ }
+
+ if (app) {
+ snprintf(app_buf, sizeof(app_buf), "execute-app-name: %s\n", app);
+ }
+
+ if (arg) {
+ snprintf(arg_buf, sizeof(arg_buf), "execute-app-arg: %s\n", arg);
+ }
+
+ snprintf(send_buf, sizeof(send_buf), "%s\ncall-command: execute\n%s%s\n", cmd_buf, app_buf, arg_buf);
+
+ esl_send_recv(handle, send_buf);
+}
esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback)
{
@@ -386,6 +456,10 @@
int rval;
const char *hval;
+ if (!handle->mutex) {
+ esl_mutex_create(&handle->mutex);
+ }
+
handle->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (handle->sock == ESL_SOCK_INVALID) {
@@ -450,21 +524,27 @@
snprintf(handle->err, sizeof(handle->err), "Connection Error");
goto fail;
}
-
+
return ESL_SUCCESS;
fail:
esl_disconnect(handle);
+
return ESL_FAIL;
}
esl_status_t esl_disconnect(esl_handle_t *handle)
{
esl_event_safe_destroy(&handle->last_event);
+ esl_event_safe_destroy(&handle->last_sr_event);
esl_event_safe_destroy(&handle->last_ievent);
esl_event_safe_destroy(&handle->info_event);
+ if (handle->mutex) {
+ esl_mutex_destroy(&handle->mutex);
+ }
+
if (handle->sock != ESL_SOCK_INVALID) {
close(handle->sock);
handle->sock = ESL_SOCK_INVALID;
@@ -476,7 +556,45 @@
return ESL_FAIL;
}
-esl_status_t esl_recv(esl_handle_t *handle)
+esl_status_t esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, esl_event_t **save_event)
+{
+ fd_set rfds, efds;
+ struct timeval tv = { 0, ms * 1000 };
+ int max, activity, i = 0;
+ esl_status_t status = ESL_SUCCESS;
+
+ esl_mutex_lock(handle->mutex);
+ FD_ZERO(&rfds);
+ FD_ZERO(&efds);
+ FD_SET(handle->sock, &rfds);
+ FD_SET(handle->sock, &efds);
+
+ max = handle->sock + 1;
+
+ if ((activity = select(max, &rfds, NULL, &efds, &tv)) < 0) {
+ status = ESL_FAIL;
+ goto done;
+ }
+
+ if (activity && FD_ISSET(handle->sock, &rfds)) {
+ if (esl_recv_event(handle, save_event)) {
+ status = ESL_FAIL;
+ goto done;
+ }
+ } else {
+ status = ESL_BREAK;
+ }
+
+ done:
+
+ esl_mutex_unlock(handle->mutex);
+
+ return status;
+
+}
+
+
+esl_status_t esl_recv_event(esl_handle_t *handle, esl_event_t **save_event)
{
char *c;
esl_ssize_t rrval;
@@ -489,6 +607,8 @@
ssize_t len;
int zc = 0;
+ esl_mutex_lock(handle->mutex);
+
esl_event_safe_destroy(&handle->last_event);
memset(handle->header_buf, 0, sizeof(handle->header_buf));
@@ -501,6 +621,7 @@
if (rrval == 0) {
if (++zc >= 100) {
esl_disconnect(handle);
+ esl_mutex_unlock(handle->mutex);
return ESL_FAIL;
}
} else if (rrval < 0) {
@@ -530,6 +651,7 @@
*c = '\0';
if (hname && hval) {
+ esl_url_decode(hval);
if (handle->debug > 1) {
esl_log(ESL_LOG_DEBUG, "RECV HEADER [%s] = [%s]\n", hname, hval);
}
@@ -572,23 +694,25 @@
revent->body = body;
}
-
-
- handle->last_event = revent;
+ if (save_event) {
+ *save_event = revent;
+ } else {
+ handle->last_event = revent;
+ }
- if (handle->last_event) {
- const char *hval = esl_event_get_header(handle->last_event, "reply-text");
+ if (revent) {
+ const char *hval = esl_event_get_header(revent, "reply-text");
if (!esl_strlen_zero(hval)) {
strncpy(handle->last_reply, hval, sizeof(handle->last_reply));
}
- hval = esl_event_get_header(handle->last_event, "content-type");
+ hval = esl_event_get_header(revent, "content-type");
- if (!esl_strlen_zero(hval) && !strcasecmp(hval, "text/event-plain") && handle->last_event->body) {
+ if (!esl_strlen_zero(hval) && !strcasecmp(hval, "text/event-plain") && revent->body) {
const char *en;
esl_event_types_t et = ESL_EVENT_COMMAND;
- char *body = strdup(handle->last_event->body);
+ char *body = strdup(revent->body);
char *beg;
char *hname, *hval;
char *col;
@@ -669,15 +793,15 @@
}
}
+ if (handle->debug) {
+ char *foo;
+ esl_event_serialize(revent, &foo, ESL_FALSE);
+ esl_log(ESL_LOG_DEBUG, "RECV MESSAGE\n%s\n", foo);
+ free(foo);
+ }
}
-
- if (handle->debug) {
- char *foo;
- esl_event_serialize(handle->last_event, &foo, ESL_FALSE);
- esl_log(ESL_LOG_DEBUG, "RECV MESSAGE\n%s\n", foo);
- free(foo);
- }
+ esl_mutex_unlock(handle->mutex);
return ESL_SUCCESS;
@@ -706,8 +830,21 @@
esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd)
{
+ const char *hval;
+
+ esl_mutex_lock(handle->mutex);
esl_send(handle, cmd);
- esl_recv(handle);
+ esl_recv_event(handle, &handle->last_sr_event);
+
+ if (handle->last_sr_event) {
+ hval = esl_event_get_header(handle->last_sr_event, "reply-text");
+
+ if (!esl_strlen_zero(hval)) {
+ strncpy(handle->last_sr_reply, hval, sizeof(handle->last_sr_reply));
+ }
+ }
+
+ esl_mutex_unlock(handle->mutex);
}
Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h (original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Sat Dec 20 13:34:34 2008
@@ -34,6 +34,7 @@
#ifndef _ESL_H_
#define _ESL_H_
+
#define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1)
#define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x))
@@ -193,10 +194,13 @@
typedef enum {
ESL_SUCCESS,
- ESL_FAIL
+ ESL_FAIL,
+ ESL_BREAK
} esl_status_t;
+#include <esl_threadmutex.h>
+
typedef struct {
struct sockaddr_in sockaddr;
struct hostent hostent;
@@ -206,11 +210,15 @@
int errno;
char header_buf[4196];
char last_reply[1024];
+ char last_sr_reply[1024];
esl_event_t *last_event;
+ esl_event_t *last_sr_event;
esl_event_t *last_ievent;
esl_event_t *info_event;
int debug;
int connected;
+ struct sockaddr_in addr;
+ esl_mutex_t *mutex;
} esl_handle_t;
typedef enum {
@@ -258,13 +266,19 @@
typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr);
+esl_status_t esl_attach_handle(esl_handle_t *handle, esl_socket_t socket, struct sockaddr_in addr);
esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback);
+esl_status_t esl_execute(esl_handle_t *handle, const char *app, const char *arg, const char *uuid);
+esl_status_t esl_sendevent(esl_handle_t *handle, esl_event_t *event);
esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
esl_status_t esl_disconnect(esl_handle_t *handle);
esl_status_t esl_send(esl_handle_t *handle, const char *cmd);
-esl_status_t esl_recv(esl_handle_t *handle);
+esl_status_t esl_recv_event(esl_handle_t *handle, esl_event_t **save_event);
+esl_status_t esl_recv_event_timed(esl_handle_t *handle, uint32_t ms, esl_event_t **save_event);
esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd);
+#define esl_recv(_h) esl_recv_event(_h, NULL)
+#define esl_recv_timed(_h, _ms) esl_recv_event_timed(_h, _ms, NULL)
#endif
1
0
20 Dec '08
Author: anthm
Date: Sat Dec 20 10:40:33 2008
New Revision: 10895
Log:
add more code for listener
Modified:
freeswitch/trunk/libs/esl/Makefile
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/include/esl.h
Modified: freeswitch/trunk/libs/esl/Makefile
==============================================================================
--- freeswitch/trunk/libs/esl/Makefile (original)
+++ freeswitch/trunk/libs/esl/Makefile Sat Dec 20 10:40:33 2008
@@ -3,24 +3,28 @@
LIBEDIT_DIR=../../libs/libedit
CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
MYLIB=libesl.a
-
+LIBS=-lesl -lncurses -lpthread
+LDFLAGS=-L.
OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
-all: $(MYLIB) fs_cli
+all: $(MYLIB) fs_cli testclient testserver
$(MYLIB): $(OBJS) $(HEADERS)
ar rcs $(MYLIB) $(OBJS)
ranlib $(MYLIB)
-%.o: %.c
- $(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
+testserver: $(MYLIB) testserver.c
+ $(CC) $(CC_CFLAGS) $(CFLAGS) testserver.c -o testserver $(LDFLAGS) $(LIBS)
-test: $(MYLIB)
- $(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient -L. -lesl
+testclient: $(MYLIB) testclient.c
+ $(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient $(LDFLAGS) $(LIBS)
fs_cli: $(MYLIB) fs_cli.c
- $(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli -L. -L$(LIBEDIT_DIR)/src/.libs -lesl -ledit -lncurses -lpthread
+ $(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli $(LDFLAGS) -L$(LIBEDIT_DIR)/src/.libs $(LIBS) -ledit
+
+%.o: %.c
+ $(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
clean:
- rm -f *.o src/*.o testclient fs_cli libesl.a *~ src/*~ src/include/*~
+ rm -f *.o src/*.o testclient testserver fs_cli libesl.a *~ src/*~ src/include/*~
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sat Dec 20 10:40:33 2008
@@ -323,6 +323,59 @@
}
+esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback)
+{
+ esl_socket_t server_sock = ESL_SOCK_INVALID;
+ int reuse_addr = 1;
+ struct sockaddr_in addr;
+ esl_status_t status = ESL_SUCCESS;
+
+ if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+ return ESL_FAIL;
+ }
+
+ setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ addr.sin_port = htons(port);
+
+ if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ status = ESL_FAIL;
+ goto end;
+ }
+
+ if (listen(server_sock, 10000) < 0) {
+ status = ESL_FAIL;
+ goto end;
+ }
+
+ for (;;) {
+ int client_sock;
+ struct sockaddr_in echoClntAddr;
+ unsigned int clntLen;
+
+ clntLen = sizeof(echoClntAddr);
+
+ if ((client_sock = accept(server_sock, (struct sockaddr *) &echoClntAddr, &clntLen)) == ESL_SOCK_INVALID) {
+ status = ESL_FAIL;
+ goto end;
+ }
+
+ callback(server_sock, client_sock, echoClntAddr);
+ }
+
+ end:
+
+ if (server_sock != ESL_SOCK_INVALID) {
+ close(server_sock);
+ server_sock = ESL_SOCK_INVALID;
+ }
+
+ return status;
+
+}
esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password)
{
Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h (original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Sat Dec 20 10:40:33 2008
@@ -256,6 +256,9 @@
int esl_toupper(int c);
int esl_tolower(int c);
+typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr);
+
+esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback);
esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
esl_status_t esl_disconnect(esl_handle_t *handle);
1
0
Author: anthm
Date: Sat Dec 20 09:57:31 2008
New Revision: 10894
Log:
update
Modified:
freeswitch/trunk/libs/esl/fs_cli.c
Modified: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- freeswitch/trunk/libs/esl/fs_cli.c (original)
+++ freeswitch/trunk/libs/esl/fs_cli.c Sat Dec 20 09:57:31 2008
@@ -46,7 +46,7 @@
while(thread_running && handle->connected) {
fd_set rfds, efds;
- struct timeval tv = { 0, 50 * 1000 };
+ struct timeval tv = { 0, 10 * 1000 };
int max, activity, i = 0;
esl_mutex_lock(global_mutex);
@@ -186,7 +186,7 @@
signal(SIGINT, handle_SIGINT);
gethostname(hostname, sizeof(hostname));
- handle.debug = 1;
+ handle.debug = 0;
if (esl_config_open_file(&cfg, cfile)) {
1
0
[commit] r10893 - in freeswitch/trunk: libs/esl libs/esl/src src src/include src/mod/event_handlers/mod_event_socket
by FreeSWITCH SVN 20 Dec '08
by FreeSWITCH SVN 20 Dec '08
20 Dec '08
Author: anthm
Date: Sat Dec 20 09:56:08 2008
New Revision: 10893
Log:
change event_socket to have more info about log lines
Modified:
freeswitch/trunk/libs/esl/fs_cli.c
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/src/include/switch_log.h
freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
freeswitch/trunk/src/switch_log.c
Modified: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- freeswitch/trunk/libs/esl/fs_cli.c (original)
+++ freeswitch/trunk/libs/esl/fs_cli.c Sat Dec 20 09:56:08 2008
@@ -71,30 +71,34 @@
if (handle->last_event) {
const char *type = esl_event_get_header(handle->last_event, "content-type");
- if (!strcasecmp(type, "log/data")) {
- int level = 0;
- if (strstr(handle->last_event->body, "[CONSOLE]")) {
- level = 0;
- } else if (strstr(handle->last_event->body, "[ALERT]")) {
- level = 1;
- } else if (strstr(handle->last_event->body, "[CRIT]")) {
- level = 2;
- } else if (strstr(handle->last_event->body, "[ERROR]")) {
- level = 3;
- } else if (strstr(handle->last_event->body, "[WARNING]")) {
- level = 4;
- } else if (strstr(handle->last_event->body, "[NOTICE]")) {
- level = 5;
- } else if (strstr(handle->last_event->body, "[INFO]")) {
- level = 6;
- } else if (strstr(handle->last_event->body, "[DEBUG]")) {
- level = 7;
- }
+ int known = 0;
- printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
- } else if (0 && !strcasecmp(type, "text/disconnect-notice")) {
- running = thread_running = 0;
- } else {
+ if (!esl_strlen_zero(type)) {
+ if (!strcasecmp(type, "log/data")) {
+ int level = 0, tchannel = 0;
+ const char *lname = esl_event_get_header(handle->last_event, "log-level");
+ const char *channel = esl_event_get_header(handle->last_event, "text-channel");
+ const char *file = esl_event_get_header(handle->last_event, "log-file");
+
+ if (channel) {
+ tchannel = atoi(channel);
+ }
+
+ if (lname) {
+ level = atoi(lname);
+ }
+
+ if (tchannel == 0 || (file && !strcmp(file, "switch_console.c"))) {
+ printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
+ }
+ known++;
+ } else if (!strcasecmp(type, "text/disconnect-notice")) {
+ running = thread_running = 0;
+ known++;
+ }
+ }
+
+ if (!known) {
printf("INCOMING DATA [%s]\n%s", type, handle->last_event->body);
}
}
@@ -108,6 +112,7 @@
done:
thread_running = 0;
+ esl_log(ESL_LOG_DEBUG, "Thread Done\n");
return NULL;
}
@@ -181,7 +186,7 @@
signal(SIGINT, handle_SIGINT);
gethostname(hostname, sizeof(hostname));
- handle.debug = 0;
+ handle.debug = 1;
if (esl_config_open_file(&cfg, cfile)) {
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sat Dec 20 09:56:08 2008
@@ -446,7 +446,6 @@
rrval = recv(handle->sock, c, 1, 0);
if (rrval == 0) {
-
if (++zc >= 100) {
esl_disconnect(handle);
return ESL_FAIL;
@@ -588,6 +587,27 @@
free(body);
+ if ((cl = esl_event_get_header(handle->last_ievent, "content-length"))) {
+ esl_ssize_t sofar = 0;
+
+ len = atol(cl);
+ body = malloc(len+1);
+ esl_assert(body);
+ *(body + len) = '\0';
+
+ do {
+ esl_ssize_t r;
+ if ((r = recv(handle->sock, body + sofar, len - sofar, 0)) < 0) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ goto fail;
+ }
+ sofar += r;
+ } while (sofar < len);
+
+ handle->last_ievent->body = body;
+ }
+
+
if (handle->debug) {
char *foo;
esl_event_serialize(handle->last_ievent, &foo, ESL_FALSE);
Modified: freeswitch/trunk/src/include/switch_log.h
==============================================================================
--- freeswitch/trunk/src/include/switch_log.h (original)
+++ freeswitch/trunk/src/include/switch_log.h Sat Dec 20 09:56:08 2008
@@ -63,7 +63,7 @@
char *content;
const char *userdata;
/* To maintain abi, only add new elements to the end of this struct and do not delete any elements */
-
+ switch_text_channel_t channel;
} switch_log_node_t;
typedef switch_status_t (*switch_log_function_t) (const switch_log_node_t *node, switch_log_level_t level);
Modified: freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c (original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c Sat Dec 20 09:56:08 2008
@@ -134,29 +134,32 @@
static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_level_t level)
{
listener_t *l;
-
+
switch_mutex_lock(globals.listener_mutex);
for (l = listen_list.listeners; l; l = l->next) {
if (switch_test_flag(l, LFLAG_LOG) && l->level >= node->level) {
- char *data = strdup(node->data);
- if (data) {
- if (switch_queue_trypush(l->log_queue, data) == SWITCH_STATUS_SUCCESS) {
- if (l->lost_logs) {
- int ll = l->lost_logs;
- switch_event_t *event;
- l->lost_logs = 0;
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost %d log lines!\n", ll);
- if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
- switch_event_add_header(event, SWITCH_STACK_BOTTOM, "info", "lost %d log lines", ll);
- switch_event_fire(&event);
- }
+ switch_log_node_t *dnode = malloc(sizeof(*node));
+
+ switch_assert(dnode);
+ *dnode = *node;
+ dnode->data = strdup(node->data);
+ switch_assert(dnode->data);
+
+ if (switch_queue_trypush(l->log_queue, dnode) == SWITCH_STATUS_SUCCESS) {
+ if (l->lost_logs) {
+ int ll = l->lost_logs;
+ switch_event_t *event;
+ l->lost_logs = 0;
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost %d log lines!\n", ll);
+ if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "info", "lost %d log lines", ll);
+ switch_event_fire(&event);
}
- } else {
- switch_safe_free(data);
- l->lost_logs++;
}
} else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
+ switch_safe_free(dnode->data);
+ switch_safe_free(dnode);
+ l->lost_logs++;
}
}
}
@@ -171,7 +174,11 @@
if (listener->log_queue) {
while (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
- if (pop) free(pop);
+ switch_log_node_t *dnode = (switch_log_node_t *) pop;
+ if (dnode) {
+ free(dnode->data);
+ free(dnode);
+ }
}
}
@@ -981,17 +988,32 @@
if (!*mbuf) {
if (switch_test_flag(listener, LFLAG_LOG)) {
if (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
- char *data = (char *) pop;
-
-
- if (data) {
- switch_snprintf(buf, sizeof(buf), "Content-Type: log/data\nContent-Length: %" SWITCH_SSIZE_T_FMT "\n\n", strlen(data));
+ switch_log_node_t *dnode = (switch_log_node_t *) pop;
+
+ if (dnode->data) {
+ switch_snprintf(buf, sizeof(buf),
+ "Content-Type: log/data\n"
+ "Content-Length: %" SWITCH_SSIZE_T_FMT "\n"
+ "Log-Level: %d\n"
+ "Text-Channel: %d\n"
+ "Log-File: %s\n"
+ "Log-Func: %s\n"
+ "Log->Line: %d\n"
+ "\n",
+ strlen(dnode->data),
+ dnode->level,
+ dnode->channel,
+ dnode->file,
+ dnode->func,
+ dnode->line
+ );
len = strlen(buf);
switch_socket_send(listener->sock, buf, &len);
- len = strlen(data);
- switch_socket_send(listener->sock, data, &len);
+ len = strlen(dnode->data);
+ switch_socket_send(listener->sock, dnode->data, &len);
- free(data);
+ free(dnode->data);
+ free(dnode);
}
do_sleep = 0;
}
Modified: freeswitch/trunk/src/switch_log.c
==============================================================================
--- freeswitch/trunk/src/switch_log.c (original)
+++ freeswitch/trunk/src/switch_log.c Sat Dec 20 09:56:08 2008
@@ -352,7 +352,7 @@
node = malloc(sizeof(*node));
switch_assert(node);
}
-
+
node->data = data;
data = NULL;
switch_set_string(node->file, filep);
@@ -361,7 +361,7 @@
node->level = level;
node->content = content;
node->timestamp = now;
-
+ node->channel = channel;
if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
free(node->data);
1
0
20 Dec '08
Author: anthm
Date: Sat Dec 20 00:24:51 2008
New Revision: 10892
Log:
work in progress
Added:
freeswitch/trunk/libs/esl/src/esl_config.c
freeswitch/trunk/libs/esl/src/include/esl_config.h
Modified:
freeswitch/trunk/libs/esl/Makefile
freeswitch/trunk/libs/esl/fs_cli.c
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/include/esl.h
Modified: freeswitch/trunk/libs/esl/Makefile
==============================================================================
--- freeswitch/trunk/libs/esl/Makefile (original)
+++ freeswitch/trunk/libs/esl/Makefile Sat Dec 20 00:24:51 2008
@@ -4,7 +4,7 @@
CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
MYLIB=libesl.a
-OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o
+OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o
all: $(MYLIB) fs_cli
Modified: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- freeswitch/trunk/libs/esl/fs_cli.c (original)
+++ freeswitch/trunk/libs/esl/fs_cli.c Sat Dec 20 00:24:51 2008
@@ -61,14 +61,18 @@
esl_mutex_unlock(global_mutex);
goto done;
}
-
-
if (activity && FD_ISSET(handle->sock, &rfds)) {
- esl_recv(handle);
+ if (esl_recv(handle)) {
+ running = thread_running = 0;
+ esl_mutex_unlock(global_mutex);
+ esl_log(ESL_LOG_WARNING, "Disconnected.\n");
+ goto done;
+ }
+
if (handle->last_event) {
const char *type = esl_event_get_header(handle->last_event, "content-type");
if (!strcasecmp(type, "log/data")) {
- int level;
+ int level = 0;
if (strstr(handle->last_event->body, "[CONSOLE]")) {
level = 0;
} else if (strstr(handle->last_event->body, "[ALERT]")) {
@@ -88,6 +92,10 @@
}
printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
+ } else if (0 && !strcasecmp(type, "text/disconnect-notice")) {
+ running = thread_running = 0;
+ } else {
+ printf("INCOMING DATA [%s]\n%s", type, handle->last_event->body);
}
}
@@ -106,28 +114,29 @@
static int process_command(esl_handle_t *handle, const char *cmd)
{
- if (!strcasecmp(cmd, "exit")) {
+ if (
+ !strcasecmp(cmd, "exit") ||
+ !strcasecmp(cmd, "quit") ||
+ !strcasecmp(cmd, "bye")
+ ) {
return -1;
}
- if (!strncasecmp(cmd, "loglevel", 8)) {
- const char *level = cmd + 8;
-
- while(*level == ' ') level++;
- if (!esl_strlen_zero(level)) {
- char cb[128] = "";
-
- snprintf(cb, sizeof(cb), "log %s\n\n", level);
- esl_mutex_lock(global_mutex);
- esl_send_recv(handle, cb);
- printf("%s\n", handle->last_reply);
- esl_mutex_unlock(global_mutex);
- }
-
+ if (
+ !strncasecmp(cmd, "event", 5) ||
+ !strncasecmp(cmd, "noevent", 7) ||
+ !strncasecmp(cmd, "nixevent", 8) ||
+ !strncasecmp(cmd, "log", 3) ||
+ !strncasecmp(cmd, "nolog", 5) ||
+ !strncasecmp(cmd, "filter", 6)
+ ) {
+ esl_mutex_lock(global_mutex);
+ esl_send_recv(handle, cmd);
+ printf("%s\n", handle->last_reply);
+ esl_mutex_unlock(global_mutex);
goto end;
}
-
-
+
printf("Unknown command [%s]\n", cmd);
end:
@@ -136,17 +145,35 @@
}
-int main(void)
+typedef struct {
+ char host[128];
+ char pass[128];
+ esl_port_t port;
+} cli_profile_t;
+
+static cli_profile_t profiles[128] = { 0 };
+static int pcount;
+
+int main(int argc, char *argv[])
{
esl_handle_t handle = {0};
int count;
const char *line;
char cmd_str[1024] = "";
char hfile[512] = "/tmp/fs_cli_history";
+ char cfile[512] = "/tmp/fs_cli_config";
char *home = getenv("HOME");
-
+ esl_config_t cfg;
+ cli_profile_t *profile = &profiles[0];
+
+ strncpy(profiles[0].host, "localhost", sizeof(profiles[0].host));
+ strncpy(profiles[0].pass, "ClueCon", sizeof(profiles[0].pass));
+ profiles[0].port = 8021;
+ pcount = 1;
+
if (home) {
snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
+ snprintf(cfile, sizeof(cfile), "%s/.fs_cli_config", home);
}
esl_mutex_create(&global_mutex);
@@ -155,15 +182,27 @@
gethostname(hostname, sizeof(hostname));
handle.debug = 0;
-
- // um ya add some command line parsing for host port and pass
+
+ if (esl_config_open_file(&cfg, cfile)) {
+ char *var, *val;
+ while (esl_config_next_pair(&cfg, &var, &val)) {
+
+ }
+ esl_config_close_file(&cfg);
+ }
- if (esl_connect(&handle, "localhost", 8021, "ClueCon")) {
+
+ if (esl_connect(&handle, profile->host, profile->port, profile->pass)) {
printf("Error Connecting [%s]\n", handle.err);
- goto done;
+ return -1;
}
-
+
+
+ if (handle.debug) {
+ esl_global_set_default_logger(7);
+ }
+
esl_thread_create_detached(msg_thread_run, &handle);
el = el_init(__FILE__, stdout, stdout, stdout);
Modified: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- freeswitch/trunk/libs/esl/src/esl.c (original)
+++ freeswitch/trunk/libs/esl/src/esl.c Sat Dec 20 00:24:51 2008
@@ -32,6 +32,7 @@
*/
#include <esl.h>
+#include <sys/signal.h>
#ifndef HAVE_GETHOSTBYNAME_R
extern int gethostbyname_r (const char *__name,
@@ -43,6 +44,230 @@
+/* Written by Marc Espie, public domain */
+#define ESL_CTYPE_NUM_CHARS 256
+
+const short _esl_C_toupper_[1 + ESL_CTYPE_NUM_CHARS] = {
+ EOF,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
+ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
+ 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
+ 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
+ 'X', 'Y', 'Z', 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
+ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
+};
+
+const short *_esl_toupper_tab_ = _esl_C_toupper_;
+
+int esl_toupper(int c)
+{
+ if ((unsigned int)c > 255)
+ return(c);
+ if (c < -1)
+ return EOF;
+ return((_esl_toupper_tab_ + 1)[c]);
+}
+
+const short _esl_C_tolower_[1 + ESL_CTYPE_NUM_CHARS] = {
+ EOF,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+ 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+ 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+ 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
+ 0x40, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+ 'x', 'y', 'z', 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
+ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+ 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
+ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
+ 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
+ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
+ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
+ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
+ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
+ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
+ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
+ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
+ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
+ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
+ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
+ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
+ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
+ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
+};
+
+const short *_esl_tolower_tab_ = _esl_C_tolower_;
+
+int esl_tolower(int c)
+{
+ if ((unsigned int)c > 255)
+ return(c);
+ if (c < -1)
+ return EOF;
+ return((_esl_tolower_tab_ + 1)[c]);
+}
+
+const char *esl_stristr(const char *instr, const char *str)
+{
+/*
+** Rev History: 16/07/97 Greg Thayer Optimized
+** 07/04/95 Bob Stout ANSI-fy
+** 02/03/94 Fred Cole Original
+** 09/01/03 Bob Stout Bug fix (lines 40-41) per Fred Bulback
+**
+** Hereby donated to public domain.
+*/
+ const char *pptr, *sptr, *start;
+
+ if (!str || !instr)
+ return NULL;
+
+ for (start = str; *start; start++) {
+ /* find start of pattern in string */
+ for (; ((*start) && (esl_toupper(*start) != esl_toupper(*instr))); start++);
+
+ if (!*start)
+ return NULL;
+
+ pptr = instr;
+ sptr = start;
+
+ while (esl_toupper(*sptr) == esl_toupper(*pptr)) {
+ sptr++;
+ pptr++;
+
+ /* if end of pattern then pattern was found */
+ if (!*pptr)
+ return (start);
+
+ if (!*sptr)
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+
+static void null_logger(const char *file, const char *func, int line, int level, const char *fmt, ...)
+{
+ if (file && func && line && level && fmt) {
+ return;
+ }
+ return;
+}
+
+
+static const char *LEVEL_NAMES[] = {
+ "EMERG",
+ "ALERT",
+ "CRIT",
+ "ERROR",
+ "WARNING",
+ "NOTICE",
+ "INFO",
+ "DEBUG",
+ NULL
+};
+
+static int esl_log_level = 7;
+
+static const char *cut_path(const char *in)
+{
+ const char *p, *ret = in;
+ char delims[] = "/\\";
+ char *i;
+
+ for (i = delims; *i; i++) {
+ p = in;
+ while ((p = strchr(p, *i)) != 0) {
+ ret = ++p;
+ }
+ }
+ return ret;
+}
+
+
+static void default_logger(const char *file, const char *func, int line, int level, const char *fmt, ...)
+{
+ const char *fp;
+ char data[1024];
+ va_list ap;
+
+ if (level < 0 || level > 7) {
+ level = 7;
+ }
+ if (level > esl_log_level) {
+ return;
+ }
+
+ fp = cut_path(file);
+
+ va_start(ap, fmt);
+
+ vsnprintf(data, sizeof(data), fmt, ap);
+
+
+ fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], file, line, func, data);
+
+ va_end(ap);
+
+}
+
+esl_logger_t esl_log = null_logger;
+
+void esl_global_set_logger(esl_logger_t logger)
+{
+ if (logger) {
+ esl_log = logger;
+ } else {
+ esl_log = null_logger;
+ }
+}
+
+void esl_global_set_default_logger(int level)
+{
+ if (level < 0 || level > 7) {
+ level = 7;
+ }
+
+ esl_log = default_logger;
+ esl_log_level = level;
+}
+
size_t esl_url_encode(const char *url, char *buf, size_t len)
{
const char *p;
@@ -138,10 +363,12 @@
rval = connect(handle->sock, (struct sockaddr *) &handle->sockaddr, sizeof(handle->sockaddr));
if (rval) {
- strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
goto fail;
}
-
+
+ handle->connected = 1;
+
if (esl_recv(handle)) {
snprintf(handle->err, sizeof(handle->err), "Connection Error");
goto fail;
@@ -171,8 +398,6 @@
goto fail;
}
- handle->connected = 1;
-
return ESL_SUCCESS;
fail:
@@ -184,7 +409,9 @@
esl_status_t esl_disconnect(esl_handle_t *handle)
{
esl_event_safe_destroy(&handle->last_event);
-
+ esl_event_safe_destroy(&handle->last_ievent);
+ esl_event_safe_destroy(&handle->info_event);
+
if (handle->sock != ESL_SOCK_INVALID) {
close(handle->sock);
handle->sock = ESL_SOCK_INVALID;
@@ -207,20 +434,29 @@
char *col;
char *cl;
ssize_t len;
-
+ int zc = 0;
+
esl_event_safe_destroy(&handle->last_event);
memset(handle->header_buf, 0, sizeof(handle->header_buf));
c = handle->header_buf;
beg = c;
- for(;;) {
+ while(handle->connected) {
rrval = recv(handle->sock, c, 1, 0);
- if (rrval < 0) {
+ if (rrval == 0) {
+
+ if (++zc >= 100) {
+ esl_disconnect(handle);
+ return ESL_FAIL;
+ }
+ } else if (rrval < 0) {
strerror_r(handle->errno, handle->err, sizeof(handle->err));
goto fail;
- } else if (rrval > 0) {
+ } else {
+ zc = 0;
+
if (*c == '\n') {
if (++crc == 2) {
break;
@@ -243,7 +479,7 @@
if (hname && hval) {
if (handle->debug > 1) {
- printf("RECV HEADER [%s] = [%s]\n", hname, hval);
+ esl_log(ESL_LOG_DEBUG, "RECV HEADER [%s] = [%s]\n", hname, hval);
}
esl_event_add_header_string(revent, ESL_STACK_BOTTOM, hname, hval);
}
@@ -289,17 +525,84 @@
handle->last_event = revent;
if (handle->last_event) {
- const char *reply = esl_event_get_header(handle->last_event, "reply-text");
- if (!esl_strlen_zero(reply)) {
- strncpy(handle->last_reply, reply, sizeof(handle->last_reply));
+ const char *hval = esl_event_get_header(handle->last_event, "reply-text");
+
+ if (!esl_strlen_zero(hval)) {
+ strncpy(handle->last_reply, hval, sizeof(handle->last_reply));
+ }
+
+ hval = esl_event_get_header(handle->last_event, "content-type");
+
+ if (!esl_strlen_zero(hval) && !strcasecmp(hval, "text/event-plain") && handle->last_event->body) {
+ const char *en;
+ esl_event_types_t et = ESL_EVENT_COMMAND;
+ char *body = strdup(handle->last_event->body);
+ char *beg;
+ char *hname, *hval;
+ char *col;
+ char *cl;
+ ssize_t len;
+ char *c;
+
+ esl_event_safe_destroy(&handle->last_ievent);
+
+ if ((en = esl_stristr("event-name:", body))) {
+ en++;
+ while(*en == ' ') en++;
+ if (en) {
+ esl_name_event(en, &et);
+ }
+ }
+
+ esl_event_create(&handle->last_ievent, et);
+
+ beg = body;
+
+ while(beg) {
+ if (!(c = strchr(beg, '\n'))) {
+ break;
+ }
+
+ hname = beg;
+ hval = col = NULL;
+
+ if (hname && (col = strchr(hname, ':'))) {
+ hval = col + 1;
+ *col = '\0';
+ while(*hval == ' ') hval++;
+ }
+
+ *c = '\0';
+
+ if (hname && hval) {
+ esl_url_decode(hval);
+
+ if (handle->debug > 1) {
+ esl_log(ESL_LOG_DEBUG, "RECV INNER HEADER [%s] = [%s]\n", hname, hval);
+ }
+ esl_event_add_header_string(handle->last_ievent, ESL_STACK_BOTTOM, hname, hval);
+ }
+
+ beg = c + 1;
+ }
+
+ free(body);
+
+ if (handle->debug) {
+ char *foo;
+ esl_event_serialize(handle->last_ievent, &foo, ESL_FALSE);
+ esl_log(ESL_LOG_DEBUG, "RECV EVENT\n%s\n", foo);
+ free(foo);
+ }
}
+
}
if (handle->debug) {
char *foo;
esl_event_serialize(handle->last_event, &foo, ESL_FALSE);
- printf("RECV MESSAGE\n%s\n", foo);
+ esl_log(ESL_LOG_DEBUG, "RECV MESSAGE\n%s\n", foo);
free(foo);
}
@@ -314,11 +617,17 @@
esl_status_t esl_send(esl_handle_t *handle, const char *cmd)
{
+ const char *e = cmd + strlen(cmd) -1;
+
if (handle->debug) {
- printf("SEND\n%s\n", cmd);
+ esl_log(ESL_LOG_DEBUG, "SEND\n%s\n", cmd);
}
send(handle->sock, cmd, strlen(cmd), 0);
+
+ if (!(*e == '\n' && *(e-1) == '\n')) {
+ send(handle->sock, "\n\n", 2, 0);
+ }
}
Added: freeswitch/trunk/libs/esl/src/esl_config.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/esl_config.c Sat Dec 20 00:24:51 2008
@@ -0,0 +1,251 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "esl.h"
+#include "esl_config.h"
+
+int esl_config_open_file(esl_config_t *cfg, const char *file_path)
+{
+ FILE *f;
+ const char *path = NULL;
+ char path_buf[1024];
+
+ if (file_path[0] == '/') {
+ path = file_path;
+ } else {
+ snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path);
+ path = path_buf;
+ }
+
+ if (!path) {
+ return 0;
+ }
+
+ memset(cfg, 0, sizeof(*cfg));
+ cfg->lockto = -1;
+ esl_log(ESL_LOG_DEBUG, "Configuration file is %s.\n", path);
+ f = fopen(path, "r");
+
+ if (!f) {
+ if (file_path[0] != '/') {
+ int last = -1;
+ char *var, *val;
+
+ snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR);
+ path = path_buf;
+
+ if ((f = fopen(path, "r")) == 0) {
+ return 0;
+ }
+
+ cfg->file = f;
+ esl_set_string(cfg->path, path);
+
+ while (esl_config_next_pair(cfg, &var, &val)) {
+ if ((cfg->sectno != last) && !strcmp(cfg->section, file_path)) {
+ cfg->lockto = cfg->sectno;
+ return 1;
+ }
+ }
+
+ esl_config_close_file(cfg);
+ memset(cfg, 0, sizeof(*cfg));
+ return 0;
+ }
+
+ return 0;
+ } else {
+ cfg->file = f;
+ esl_set_string(cfg->path, path);
+ return 1;
+ }
+}
+
+void esl_config_close_file(esl_config_t *cfg)
+{
+
+ if (cfg->file) {
+ fclose(cfg->file);
+ }
+
+ memset(cfg, 0, sizeof(*cfg));
+}
+
+
+
+int esl_config_next_pair(esl_config_t *cfg, char **var, char **val)
+{
+ int ret = 0;
+ char *p, *end;
+
+ *var = *val = NULL;
+
+ if (!cfg->path) {
+ return 0;
+ }
+
+ for (;;) {
+ cfg->lineno++;
+
+ if (!fgets(cfg->buf, sizeof(cfg->buf), cfg->file)) {
+ ret = 0;
+ break;
+ }
+ *var = cfg->buf;
+
+ if (**var == '[' && (end = strchr(*var, ']')) != 0) {
+ *end = '\0';
+ (*var)++;
+ if (**var == '+') {
+ (*var)++;
+ esl_copy_string(cfg->section, *var, sizeof(cfg->section));
+ cfg->sectno++;
+
+ if (cfg->lockto > -1 && cfg->sectno != cfg->lockto) {
+ break;
+ }
+ cfg->catno = 0;
+ cfg->lineno = 0;
+ *var = (char *) "";
+ *val = (char *) "";
+ return 1;
+ } else {
+ esl_copy_string(cfg->category, *var, sizeof(cfg->category));
+ cfg->catno++;
+ }
+ continue;
+ }
+
+
+
+ if (**var == '#' || **var == ';' || **var == '\n' || **var == '\r') {
+ continue;
+ }
+
+ if (!strncmp(*var, "__END__", 7)) {
+ break;
+ }
+
+
+ if ((end = strchr(*var, ';')) && *(end+1) == *end) {
+ *end = '\0';
+ end--;
+ } else if ((end = strchr(*var, '\n')) != 0) {
+ if (*(end - 1) == '\r') {
+ end--;
+ }
+ *end = '\0';
+ }
+
+ p = *var;
+ while ((*p == ' ' || *p == '\t') && p != end) {
+ *p = '\0';
+ p++;
+ }
+ *var = p;
+
+
+ if ((*val = strchr(*var, '=')) == 0) {
+ ret = -1;
+ /* log_printf(0, server.log, "Invalid syntax on %s: line %d\n", cfg->path, cfg->lineno); */
+ continue;
+ } else {
+ p = *val - 1;
+ *(*val) = '\0';
+ (*val)++;
+ if (*(*val) == '>') {
+ *(*val) = '\0';
+ (*val)++;
+ }
+
+ while ((*p == ' ' || *p == '\t') && p != *var) {
+ *p = '\0';
+ p--;
+ }
+
+ p = *val;
+ while ((*p == ' ' || *p == '\t') && p != end) {
+ *p = '\0';
+ p++;
+ }
+ *val = p;
+ ret = 1;
+ break;
+ }
+ }
+
+
+ return ret;
+
+}
+
+int esl_config_get_cas_bits(char *strvalue, unsigned char *outbits)
+{
+ char cas_bits[5];
+ unsigned char bit = 0x8;
+ char *double_colon = strchr(strvalue, ':');
+ if (!double_colon) {
+ esl_log(ESL_LOG_ERROR, "No CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
+ return -1;
+ }
+ double_colon++;
+ *outbits = 0;
+ cas_bits[4] = 0;
+ if (sscanf(double_colon, "%c%c%c%c", &cas_bits[0], &cas_bits[1], &cas_bits[2], &cas_bits[3]) != 4) {
+ esl_log(ESL_LOG_ERROR, "Invalid CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
+ return -1;
+ }
+ esl_log(ESL_LOG_DEBUG, "CAS bits specification found: %s\n", cas_bits);
+ int x = 0;
+ for (; cas_bits[x]; x++) {
+ if ('1' == cas_bits[x]) {
+ *outbits |= bit;
+ } else if ('0' != cas_bits[x]) {
+ esl_log(ESL_LOG_ERROR, "Invalid CAS pattern specified: %s, just 0 or 1 allowed for each bit\n");
+ return -1;
+ }
+ bit >>= 1;
+ }
+ return 0;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
Modified: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- freeswitch/trunk/libs/esl/src/include/esl.h (original)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Sat Dec 20 00:24:51 2008
@@ -34,6 +34,9 @@
#ifndef _ESL_H_
#define _ESL_H_
+#define esl_copy_string(_x, _y, _z) strncpy(_x, _y, _z - 1)
+#define esl_set_string(_x, _y) esl_copy_string(_x, _y, sizeof(_x))
+
typedef struct esl_event_header esl_event_header_t;
typedef struct esl_event esl_event_t;
@@ -204,6 +207,8 @@
char header_buf[4196];
char last_reply[1024];
esl_event_t *last_event;
+ esl_event_t *last_ievent;
+ esl_event_t *info_event;
int debug;
int connected;
} esl_handle_t;
@@ -213,12 +218,44 @@
ESL_FALSE = 0
} esl_bool_t;
+#ifndef __FUNCTION__
+#define __FUNCTION__ (const char *)__func__
+#endif
+
+#define ESL_PRE __FILE__, __FUNCTION__, __LINE__
+#define ESL_LOG_LEVEL_DEBUG 7
+#define ESL_LOG_LEVEL_INFO 6
+#define ESL_LOG_LEVEL_NOTICE 5
+#define ESL_LOG_LEVEL_WARNING 4
+#define ESL_LOG_LEVEL_ERROR 3
+#define ESL_LOG_LEVEL_CRIT 2
+#define ESL_LOG_LEVEL_ALERT 1
+#define ESL_LOG_LEVEL_EMERG 0
+
+#define ESL_LOG_DEBUG ESL_PRE, ESL_LOG_LEVEL_DEBUG
+#define ESL_LOG_INFO ESL_PRE, ESL_LOG_LEVEL_INFO
+#define ESL_LOG_NOTICE ESL_PRE, ESL_LOG_LEVEL_NOTICE
+#define ESL_LOG_WARNING ESL_PRE, ESL_LOG_LEVEL_WARNING
+#define ESL_LOG_ERROR ESL_PRE, ESL_LOG_LEVEL_ERROR
+#define ESL_LOG_CRIT ESL_PRE, ESL_LOG_LEVEL_CRIT
+#define ESL_LOG_ALERT ESL_PRE, ESL_LOG_LEVEL_ALERT
+#define ESL_LOG_EMERG ESL_PRE, ESL_LOG_LEVEL_EMERG
+typedef void (*esl_logger_t)(const char *file, const char *func, int line, int level, const char *fmt, ...);
+
+extern esl_logger_t esl_log;
+
+void esl_global_set_logger(esl_logger_t logger);
+void esl_global_set_default_logger(int level);
#include "esl_event.h"
#include "esl_threadmutex.h"
+#include "esl_config.h"
size_t esl_url_encode(const char *url, char *buf, size_t len);
char *esl_url_decode(char *s);
+int esl_toupper(int c);
+int esl_tolower(int c);
+
esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
esl_status_t esl_disconnect(esl_handle_t *handle);
@@ -226,7 +263,6 @@
esl_status_t esl_recv(esl_handle_t *handle);
esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd);
-
#endif
Added: freeswitch/trunk/libs/esl/src/include/esl_config.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/include/esl_config.h Sat Dec 20 00:24:51 2008
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @defgroup config Config File Parser
+ * @ingroup config
+ * This module implements a basic interface and file format parser
+ *
+ * <pre>
+ *
+ * EXAMPLE
+ *
+ * [category1]
+ * var1 => val1
+ * var2 => val2
+ * \# lines that begin with \# are comments
+ * \#var3 => val3
+ * </pre>
+ * @{
+ */
+
+#ifndef ESL_CONFIG_H
+#define ESL_CONFIG_H
+
+#include "esl.h"
+#define ESL_URL_SEPARATOR "://"
+
+
+#ifdef WIN32
+#define ESL_PATH_SEPARATOR "\\"
+#ifndef ESL_CONFIG_DIR
+#define ESL_CONFIG_DIR "c:\\openesl"
+#endif
+#define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
+#else
+#define ESL_PATH_SEPARATOR "/"
+#ifndef ESL_CONFIG_DIR
+#define ESL_CONFIG_DIR "/etc/openesl"
+#endif
+#define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
+#endif
+
+typedef struct esl_config esl_config_t;
+
+/*! \brief A simple file handle representing an open configuration file **/
+struct esl_config {
+ /*! FILE stream buffer to the opened file */
+ FILE *file;
+ /*! path to the file */
+ char path[512];
+ /*! current category */
+ char category[256];
+ /*! current section */
+ char section[256];
+ /*! buffer of current line being read */
+ char buf[1024];
+ /*! current line number in file */
+ int lineno;
+ /*! current category number in file */
+ int catno;
+ /*! current section number in file */
+ int sectno;
+
+ int lockto;
+};
+
+/*!
+ \brief Open a configuration file
+ \param cfg (esl_config_t *) config handle to use
+ \param file_path path to the file
+ \return 1 (true) on success 0 (false) on failure
+*/
+int esl_config_open_file(esl_config_t * cfg, const char *file_path);
+
+/*!
+ \brief Close a previously opened configuration file
+ \param cfg (esl_config_t *) config handle to use
+*/
+void esl_config_close_file(esl_config_t * cfg);
+
+/*!
+ \brief Retrieve next name/value pair from configuration file
+ \param cfg (esl_config_t *) config handle to use
+ \param var pointer to aim at the new variable name
+ \param val pointer to aim at the new value
+*/
+int esl_config_next_pair(esl_config_t * cfg, char **var, char **val);
+
+/*!
+ \brief Retrieve the CAS bits from a configuration string value
+ \param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
+ \param outbits pointer to aim at the CAS bits
+*/
+int esl_config_get_cas_bits(char *strvalue, unsigned char *outbits);
+
+
+/** @} */
+#endif
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
1
0
20 Dec '08
Author: mrene
Date: Fri Dec 19 21:14:10 2008
New Revision: 10891
Log:
Fix leak and remove extra \n
Modified:
freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c
Modified: freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c (original)
+++ freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c Fri Dec 19 21:14:10 2008
@@ -240,17 +240,17 @@
switch_core_db_test_reactive(db, "select * from limit_data", NULL, limit_sql);
switch_core_db_test_reactive(db, "select * from db_data", NULL, db_sql);
switch_core_db_test_reactive(db, "select * from group_data", NULL, group_sql);
-
- switch_core_db_exec(db, "create index if not exists ld_hostname on limit_data (hostname)", NULL, NULL, NULL);
- switch_core_db_exec(db, "create index if not exists ld_uuid on limit_data (uuid)", NULL, NULL, NULL);
- switch_core_db_exec(db, "create index if not exists ld_realm on limit_data (realm)", NULL, NULL, NULL);
- switch_core_db_exec(db, "create index if not exists ld_id on limit_data (id)", NULL, NULL, NULL);
-
- switch_core_db_exec(db, "create index if not exists dd_realm on db_data (realm)", NULL, NULL, NULL);
- switch_core_db_exec(db, "create index if not exists dd_data_key on db_data (data_key)", NULL, NULL, NULL);
-
- switch_core_db_exec(db, "create index if not exists gd_groupname on group_data (groupname)", NULL, NULL, NULL);
- switch_core_db_exec(db, "create index if not exists gd_url on group_data (url)", NULL, NULL, NULL);
+
+ switch_core_db_exec(db, "create index if not exists ld_hostname on limit_data (hostname)", NULL, NULL, NULL);
+ switch_core_db_exec(db, "create index if not exists ld_uuid on limit_data (uuid)", NULL, NULL, NULL);
+ switch_core_db_exec(db, "create index if not exists ld_realm on limit_data (realm)", NULL, NULL, NULL);
+ switch_core_db_exec(db, "create index if not exists ld_id on limit_data (id)", NULL, NULL, NULL);
+
+ switch_core_db_exec(db, "create index if not exists dd_realm on db_data (realm)", NULL, NULL, NULL);
+ switch_core_db_exec(db, "create index if not exists dd_data_key on db_data (data_key)", NULL, NULL, NULL);
+
+ switch_core_db_exec(db, "create index if not exists gd_groupname on group_data (groupname)", NULL, NULL, NULL);
+ switch_core_db_exec(db, "create index if not exists gd_url on group_data (url)", NULL, NULL, NULL);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
@@ -773,7 +773,7 @@
if (argc < 2) {
stream->write_function(stream, "USAGE: limit_usage %s\n", LIMIT_USAGE_USAGE);
- return SWITCH_STATUS_SUCCESS;
+ goto end;
}
@@ -786,7 +786,10 @@
limit_execute_sql_callback(NULL, sql, sql2str_callback, &cbt);
switch_safe_free(sql);
- stream->write_function(stream, "%s\n", buf);
+ stream->write_function(stream, "%s", buf);
+
+end:
+ switch_safe_free(mydata);
return SWITCH_STATUS_SUCCESS;
}
1
0
20 Dec '08
Author: anthm
Date: Fri Dec 19 19:35:09 2008
New Revision: 10890
Log:
add esl and fs_cli
Added:
freeswitch/trunk/libs/esl/
freeswitch/trunk/libs/esl/Makefile
freeswitch/trunk/libs/esl/fs_cli.c
freeswitch/trunk/libs/esl/src/
freeswitch/trunk/libs/esl/src/esl.c
freeswitch/trunk/libs/esl/src/esl_event.c
freeswitch/trunk/libs/esl/src/esl_threadmutex.c
freeswitch/trunk/libs/esl/src/include/
freeswitch/trunk/libs/esl/src/include/esl.h
freeswitch/trunk/libs/esl/src/include/esl_event.h
freeswitch/trunk/libs/esl/src/include/esl_threadmutex.h
freeswitch/trunk/libs/esl/testclient.c
Added: freeswitch/trunk/libs/esl/Makefile
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/Makefile Fri Dec 19 19:35:09 2008
@@ -0,0 +1,26 @@
+PWD=$(shell pwd)
+INCS=-I$(PWD)/src/include
+LIBEDIT_DIR=../../libs/libedit
+CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/
+MYLIB=libesl.a
+
+OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o
+
+all: $(MYLIB) fs_cli
+
+$(MYLIB): $(OBJS) $(HEADERS)
+ ar rcs $(MYLIB) $(OBJS)
+ ranlib $(MYLIB)
+
+%.o: %.c
+ $(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
+
+test: $(MYLIB)
+ $(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient -L. -lesl
+
+fs_cli: $(MYLIB) fs_cli.c
+ $(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli -L. -L$(LIBEDIT_DIR)/src/.libs -lesl -ledit -lncurses -lpthread
+
+clean:
+ rm -f *.o src/*.o testclient fs_cli libesl.a *~ src/*~ src/include/*~
+
Added: freeswitch/trunk/libs/esl/fs_cli.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/fs_cli.c Fri Dec 19 19:35:09 2008
@@ -0,0 +1,245 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <esl.h>
+#include <signal.h>
+#include <sys/select.h>
+
+#include <histedit.h>
+static char prompt_str[512] = "";
+static char hostname[512] = "";
+
+char *prompt(EditLine * e)
+{
+ if (*prompt_str == '\0') {
+ gethostname(hostname, sizeof(hostname));
+ snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname);
+ }
+
+ return prompt_str;
+
+}
+
+static EditLine *el;
+static History *myhistory;
+static HistEvent ev;
+static char *hfile = NULL;
+static int running = 1;
+static int thread_running = 0;
+static esl_mutex_t *global_mutex;
+
+static void handle_SIGINT(int sig)
+{
+ if (sig);
+ return;
+}
+
+static const char* COLORS[] = { ESL_SEQ_DEFAULT_COLOR, ESL_SEQ_FRED, ESL_SEQ_FRED,
+ ESL_SEQ_FRED, ESL_SEQ_FMAGEN, ESL_SEQ_FCYAN, ESL_SEQ_FGREEN, ESL_SEQ_FYELLOW };
+
+
+static void *msg_thread_run(esl_thread_t *me, void *obj)
+{
+
+ esl_handle_t *handle = (esl_handle_t *) obj;
+
+ thread_running = 1;
+
+ while(thread_running && handle->connected) {
+ fd_set rfds, efds;
+ struct timeval tv = { 0, 50 * 1000 };
+ int max, activity, i = 0;
+
+ esl_mutex_lock(global_mutex);
+ FD_ZERO(&rfds);
+ FD_ZERO(&efds);
+ FD_SET(handle->sock, &rfds);
+ FD_SET(handle->sock, &efds);
+
+ max = handle->sock + 1;
+
+ if ((activity = select(max, &rfds, NULL, &efds, &tv)) < 0) {
+ esl_mutex_unlock(global_mutex);
+ goto done;
+ }
+
+
+ if (activity && FD_ISSET(handle->sock, &rfds)) {
+ esl_recv(handle);
+ if (handle->last_event) {
+ const char *type = esl_event_get_header(handle->last_event, "content-type");
+ if (!strcasecmp(type, "log/data")) {
+ int level;
+ if (strstr(handle->last_event->body, "[CONSOLE]")) {
+ level = 0;
+ } else if (strstr(handle->last_event->body, "[ALERT]")) {
+ level = 1;
+ } else if (strstr(handle->last_event->body, "[CRIT]")) {
+ level = 2;
+ } else if (strstr(handle->last_event->body, "[ERROR]")) {
+ level = 3;
+ } else if (strstr(handle->last_event->body, "[WARNING]")) {
+ level = 4;
+ } else if (strstr(handle->last_event->body, "[NOTICE]")) {
+ level = 5;
+ } else if (strstr(handle->last_event->body, "[INFO]")) {
+ level = 6;
+ } else if (strstr(handle->last_event->body, "[DEBUG]")) {
+ level = 7;
+ }
+
+ printf("%s%s%s", COLORS[level], handle->last_event->body, ESL_SEQ_DEFAULT_COLOR);
+ }
+ }
+
+ }
+
+ esl_mutex_unlock(global_mutex);
+ usleep(1000);
+ }
+
+ done:
+
+ thread_running = 0;
+
+ return NULL;
+}
+
+static int process_command(esl_handle_t *handle, const char *cmd)
+{
+ if (!strcasecmp(cmd, "exit")) {
+ return -1;
+ }
+
+ if (!strncasecmp(cmd, "loglevel", 8)) {
+ const char *level = cmd + 8;
+
+ while(*level == ' ') level++;
+ if (!esl_strlen_zero(level)) {
+ char cb[128] = "";
+
+ snprintf(cb, sizeof(cb), "log %s\n\n", level);
+ esl_mutex_lock(global_mutex);
+ esl_send_recv(handle, cb);
+ printf("%s\n", handle->last_reply);
+ esl_mutex_unlock(global_mutex);
+ }
+
+ goto end;
+ }
+
+
+ printf("Unknown command [%s]\n", cmd);
+
+ end:
+
+ return 0;
+
+}
+
+int main(void)
+{
+ esl_handle_t handle = {0};
+ int count;
+ const char *line;
+ char cmd_str[1024] = "";
+ char hfile[512] = "/tmp/fs_cli_history";
+ char *home = getenv("HOME");
+
+ if (home) {
+ snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
+ }
+
+ esl_mutex_create(&global_mutex);
+
+ signal(SIGINT, handle_SIGINT);
+ gethostname(hostname, sizeof(hostname));
+
+ handle.debug = 0;
+
+
+ // um ya add some command line parsing for host port and pass
+
+ if (esl_connect(&handle, "localhost", 8021, "ClueCon")) {
+ printf("Error Connecting [%s]\n", handle.err);
+ goto done;
+ }
+
+ esl_thread_create_detached(msg_thread_run, &handle);
+
+ el = el_init(__FILE__, stdout, stdout, stdout);
+ el_set(el, EL_PROMPT, &prompt);
+ el_set(el, EL_EDITOR, "emacs");
+ myhistory = history_init();
+
+ if (myhistory == 0) {
+ fprintf(stderr, "history could not be initialized\n");
+ goto done;
+ }
+
+ history(myhistory, &ev, H_SETSIZE, 800);
+ el_set(el, EL_HIST, history, myhistory);
+ history(myhistory, &ev, H_LOAD, hfile);
+
+
+ snprintf(cmd_str, sizeof(cmd_str), "log info\n\n");
+ esl_mutex_lock(global_mutex);
+ esl_send_recv(&handle, cmd_str);
+ esl_mutex_unlock(global_mutex);
+
+ while (running) {
+
+ line = el_gets(el, &count);
+
+ if (count > 1) {
+ if (!esl_strlen_zero(line)) {
+ char *cmd = strdup(line);
+ char *p;
+ const LineInfo *lf = el_line(el);
+ char *foo = (char *) lf->buffer;
+ if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
+ *p = '\0';
+ }
+ assert(cmd != NULL);
+ history(myhistory, &ev, H_ENTER, line);
+
+ if (!strncasecmp(cmd, "...", 3)) {
+ goto done;
+ } else if (*cmd == '/') {
+ if (process_command(&handle, cmd + 1)) {
+ running = 0;
+ }
+ } else {
+ snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", cmd);
+ esl_mutex_lock(global_mutex);
+ esl_send_recv(&handle, cmd_str);
+ printf("%s\n", handle.last_event->body);
+ esl_mutex_unlock(global_mutex);
+ }
+
+ el_deletestr(el, strlen(foo) + 1);
+ memset(foo, 0, strlen(foo));
+ free(cmd);
+ }
+ }
+
+ usleep(1000);
+
+ }
+
+
+ done:
+
+ history(myhistory, &ev, H_SAVE, hfile);
+
+ /* Clean up our memory */
+ history_end(myhistory);
+ el_end(el);
+
+ esl_disconnect(&handle);
+
+ thread_running = 0;
+
+ esl_mutex_destroy(&global_mutex);
+
+ return 0;
+}
Added: freeswitch/trunk/libs/esl/src/esl.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/esl.c Fri Dec 19 19:35:09 2008
@@ -0,0 +1,333 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <esl.h>
+
+#ifndef HAVE_GETHOSTBYNAME_R
+extern int gethostbyname_r (const char *__name,
+ struct hostent *__result_buf,
+ char *__buf, size_t __buflen,
+ struct hostent **__result,
+ int *__h_errnop);
+#endif
+
+
+
+size_t esl_url_encode(const char *url, char *buf, size_t len)
+{
+ const char *p;
+ size_t x = 0;
+ const char urlunsafe[] = "\r\n \"#%&+:;<=>?@[\\]^`{|}";
+ const char hex[] = "0123456789ABCDEF";
+
+ if (!buf) {
+ return 0;
+ }
+
+ if (!url) {
+ return 0;
+ }
+
+ len--;
+
+ for (p = url; *p; p++) {
+ if (x >= len) {
+ break;
+ }
+ if (*p < ' ' || *p > '~' || strchr(urlunsafe, *p)) {
+ if ((x + 3) >= len) {
+ break;
+ }
+ buf[x++] = '%';
+ buf[x++] = hex[*p >> 4];
+ buf[x++] = hex[*p & 0x0f];
+ } else {
+ buf[x++] = *p;
+ }
+ }
+ buf[x] = '\0';
+
+ return x;
+}
+
+char *esl_url_decode(char *s)
+{
+ char *o;
+ unsigned int tmp;
+
+ for (o = s; *s; s++, o++) {
+ if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) {
+ *o = (char) tmp;
+ s += 2;
+ } else {
+ *o = *s;
+ }
+ }
+ *o = '\0';
+ return s;
+}
+
+
+
+esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password)
+{
+
+ struct hostent *result;
+ char sendbuf[256];
+ char recvbuf[256];
+ int rval;
+ const char *hval;
+
+ handle->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+ if (handle->sock == ESL_SOCK_INVALID) {
+ snprintf(handle->err, sizeof(handle->err), "Socket Error");
+ return ESL_FAIL;
+ }
+
+ memset(&handle->sockaddr, 0, sizeof(handle->sockaddr));
+ handle->sockaddr.sin_family = AF_INET;
+ handle->sockaddr.sin_port = htons(port);
+
+ memset(&handle->hostent, 0, sizeof(handle->hostent));
+
+#ifdef HAVE_GETHOSTBYNAME_R_FIVE
+ rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &handle->errno);
+ result = handle->hostent;
+#else
+ rval = gethostbyname_r(host, &handle->hostent, handle->hostbuf, sizeof(handle->hostbuf), &result, &handle->errno);
+#endif
+
+ if (rval) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ goto fail;
+ }
+
+ memcpy(&handle->sockaddr.sin_addr, result->h_addr, result->h_length);
+
+ rval = connect(handle->sock, (struct sockaddr *) &handle->sockaddr, sizeof(handle->sockaddr));
+
+ if (rval) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ goto fail;
+ }
+
+ if (esl_recv(handle)) {
+ snprintf(handle->err, sizeof(handle->err), "Connection Error");
+ goto fail;
+ }
+
+ hval = esl_event_get_header(handle->last_event, "content-type");
+
+ if (strcasecmp(hval, "auth/request")) {
+ snprintf(handle->err, sizeof(handle->err), "Connection Error");
+ goto fail;
+ }
+
+ snprintf(sendbuf, sizeof(sendbuf), "auth %s\n\n", password);
+ esl_send(handle, sendbuf);
+
+
+ if (esl_recv(handle)) {
+ snprintf(handle->err, sizeof(handle->err), "Connection Error");
+ goto fail;
+ }
+
+
+ hval = esl_event_get_header(handle->last_event, "reply-text");
+
+ if (strcasecmp(hval, "+OK accepted")) {
+ snprintf(handle->err, sizeof(handle->err), "Connection Error");
+ goto fail;
+ }
+
+ handle->connected = 1;
+
+ return ESL_SUCCESS;
+
+ fail:
+
+ esl_disconnect(handle);
+ return ESL_FAIL;
+}
+
+esl_status_t esl_disconnect(esl_handle_t *handle)
+{
+ esl_event_safe_destroy(&handle->last_event);
+
+ if (handle->sock != ESL_SOCK_INVALID) {
+ close(handle->sock);
+ handle->sock = ESL_SOCK_INVALID;
+ return ESL_SUCCESS;
+ }
+
+ handle->connected = 0;
+
+ return ESL_FAIL;
+}
+
+esl_status_t esl_recv(esl_handle_t *handle)
+{
+ char *c;
+ esl_ssize_t rrval;
+ int crc = 0;
+ esl_event_t *revent = NULL;
+ char *beg;
+ char *hname, *hval;
+ char *col;
+ char *cl;
+ ssize_t len;
+
+ esl_event_safe_destroy(&handle->last_event);
+ memset(handle->header_buf, 0, sizeof(handle->header_buf));
+
+ c = handle->header_buf;
+ beg = c;
+
+ for(;;) {
+ rrval = recv(handle->sock, c, 1, 0);
+
+ if (rrval < 0) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ goto fail;
+ } else if (rrval > 0) {
+ if (*c == '\n') {
+ if (++crc == 2) {
+ break;
+ }
+
+ if (!revent) {
+ esl_event_create(&revent, ESL_EVENT_COMMAND);
+ }
+
+ hname = beg;
+ hval = col = NULL;
+
+ if (hname && (col = strchr(hname, ':'))) {
+ hval = col + 1;
+ *col = '\0';
+ while(*hval == ' ') hval++;
+ }
+
+ *c = '\0';
+
+ if (hname && hval) {
+ if (handle->debug > 1) {
+ printf("RECV HEADER [%s] = [%s]\n", hname, hval);
+ }
+ esl_event_add_header_string(revent, ESL_STACK_BOTTOM, hname, hval);
+ }
+
+ beg = c+1;
+
+
+ } else {
+ crc = 0;
+ }
+
+ c++;
+ }
+ }
+
+ if (!revent) {
+ goto fail;
+ }
+
+ if ((cl = esl_event_get_header(revent, "content-length"))) {
+ char *body;
+ esl_ssize_t sofar = 0;
+
+ len = atol(cl);
+ body = malloc(len+1);
+ esl_assert(body);
+ *(body + len) = '\0';
+
+ do {
+ esl_ssize_t r;
+ if ((r = recv(handle->sock, body + sofar, len - sofar, 0)) < 0) {
+ strerror_r(handle->errno, handle->err, sizeof(handle->err));
+ goto fail;
+ }
+ sofar += r;
+ } while (sofar < len);
+
+ revent->body = body;
+ }
+
+
+
+ handle->last_event = revent;
+
+ if (handle->last_event) {
+ const char *reply = esl_event_get_header(handle->last_event, "reply-text");
+ if (!esl_strlen_zero(reply)) {
+ strncpy(handle->last_reply, reply, sizeof(handle->last_reply));
+ }
+ }
+
+
+ if (handle->debug) {
+ char *foo;
+ esl_event_serialize(handle->last_event, &foo, ESL_FALSE);
+ printf("RECV MESSAGE\n%s\n", foo);
+ free(foo);
+ }
+
+ return ESL_SUCCESS;
+
+ fail:
+
+ esl_disconnect(handle);
+ return ESL_FAIL;
+
+}
+
+esl_status_t esl_send(esl_handle_t *handle, const char *cmd)
+{
+ if (handle->debug) {
+ printf("SEND\n%s\n", cmd);
+ }
+
+ send(handle->sock, cmd, strlen(cmd), 0);
+}
+
+
+esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd)
+{
+ esl_send(handle, cmd);
+ esl_recv(handle);
+}
+
+
+
+
Added: freeswitch/trunk/libs/esl/src/esl_event.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/esl_event.c Fri Dec 19 19:35:09 2008
@@ -0,0 +1,553 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <esl.h>
+#include <esl_event.h>
+
+static char *my_dup(const char *s)
+{
+ size_t len = strlen(s) + 1;
+ void *new = malloc(len);
+ esl_assert(new);
+
+ return (char *) memcpy(new, s, len);
+}
+
+#ifndef ALLOC
+#define ALLOC(size) malloc(size)
+#endif
+#ifndef DUP
+#define DUP(str) my_dup(str)
+#endif
+#ifndef FREE
+#define FREE(ptr) esl_safe_free(ptr)
+#endif
+
+/* make sure this is synced with the esl_event_types_t enum in esl_types.h
+ also never put any new ones before EVENT_ALL
+*/
+static char *EVENT_NAMES[] = {
+ "CUSTOM",
+ "CHANNEL_CREATE",
+ "CHANNEL_DESTROY",
+ "CHANNEL_STATE",
+ "CHANNEL_ANSWER",
+ "CHANNEL_HANGUP",
+ "CHANNEL_EXECUTE",
+ "CHANNEL_EXECUTE_COMPLETE",
+ "CHANNEL_BRIDGE",
+ "CHANNEL_UNBRIDGE",
+ "CHANNEL_PROGRESS",
+ "CHANNEL_PROGRESS_MEDIA",
+ "CHANNEL_OUTGOING",
+ "CHANNEL_PARK",
+ "CHANNEL_UNPARK",
+ "CHANNEL_APPLICATION",
+ "CHANNEL_ORIGINATE",
+ "CHANNEL_UUID",
+ "API",
+ "LOG",
+ "INBOUND_CHAN",
+ "OUTBOUND_CHAN",
+ "STARTUP",
+ "SHUTDOWN",
+ "PUBLISH",
+ "UNPUBLISH",
+ "TALK",
+ "NOTALK",
+ "SESSION_CRASH",
+ "MODULE_LOAD",
+ "MODULE_UNLOAD",
+ "DTMF",
+ "MESSAGE",
+ "PRESENCE_IN",
+ "NOTIFY_IN",
+ "PRESENCE_OUT",
+ "PRESENCE_PROBE",
+ "MESSAGE_WAITING",
+ "MESSAGE_QUERY",
+ "ROSTER",
+ "CODEC",
+ "BACKGROUND_JOB",
+ "DETECTED_SPEECH",
+ "DETECTED_TONE",
+ "PRIVATE_COMMAND",
+ "HEARTBEAT",
+ "TRAP",
+ "ADD_SCHEDULE",
+ "DEL_SCHEDULE",
+ "EXE_SCHEDULE",
+ "RE_SCHEDULE",
+ "RELOADXML",
+ "NOTIFY",
+ "SEND_MESSAGE",
+ "RECV_MESSAGE",
+ "REQUEST_PARAMS",
+ "CHANNEL_DATA",
+ "GENERAL",
+ "COMMAND",
+ "SESSION_HEARTBEAT",
+ "ALL"
+};
+
+const char *esl_event_name(esl_event_types_t event)
+{
+ return EVENT_NAMES[event];
+}
+
+esl_status_t esl_name_event(const char *name, esl_event_types_t *type)
+{
+ esl_event_types_t x;
+
+ for (x = 0; x <= ESL_EVENT_ALL; x++) {
+ if ((strlen(name) > 13 && !strcasecmp(name + 13, EVENT_NAMES[x])) || !strcasecmp(name, EVENT_NAMES[x])) {
+ *type = x;
+ return ESL_SUCCESS;
+ }
+ }
+
+ return ESL_FAIL;
+}
+
+
+esl_status_t esl_event_create_subclass(esl_event_t **event, esl_event_types_t event_id, const char *subclass_name)
+{
+ *event = NULL;
+
+ if (event_id != ESL_EVENT_CUSTOM && subclass_name) {
+ return ESL_FAIL;
+ }
+
+ *event = ALLOC(sizeof(esl_event_t));
+ esl_assert(*event);
+
+
+ memset(*event, 0, sizeof(esl_event_t));
+
+ (*event)->event_id = event_id;
+
+ if (subclass_name) {
+ (*event)->subclass_name = DUP(subclass_name);
+ esl_event_add_header_string(*event, ESL_STACK_BOTTOM, "Event-Subclass", subclass_name);
+ }
+
+ return ESL_SUCCESS;
+}
+
+
+const char *esl_priority_name(esl_priority_t priority)
+{
+ switch (priority) { /*lol */
+ case ESL_PRIORITY_NORMAL:
+ return "NORMAL";
+ case ESL_PRIORITY_LOW:
+ return "LOW";
+ case ESL_PRIORITY_HIGH:
+ return "HIGH";
+ default:
+ return "INVALID";
+ }
+}
+
+esl_status_t esl_event_set_priority(esl_event_t *event, esl_priority_t priority)
+{
+ event->priority = priority;
+ esl_event_add_header_string(event, ESL_STACK_TOP, "priority", esl_priority_name(priority));
+ return ESL_SUCCESS;
+}
+
+#define ESL_HASH_KEY_STRING -1
+
+static unsigned int esl_ci_hashfunc_default(const char *char_key, esl_ssize_t *klen)
+
+{
+ unsigned int hash = 0;
+ const unsigned char *key = (const unsigned char *)char_key;
+ const unsigned char *p;
+ esl_ssize_t i;
+
+ if (*klen == ESL_HASH_KEY_STRING) {
+ for (p = key; *p; p++) {
+ hash = hash * 33 + tolower(*p);
+ }
+ *klen = p - key;
+ }
+ else {
+ for (p = key, i = *klen; i; i--, p++) {
+ hash = hash * 33 + tolower(*p);
+ }
+ }
+
+ return hash;
+}
+
+
+char *esl_event_get_header(esl_event_t *event, const char *header_name)
+{
+ esl_event_header_t *hp;
+ esl_ssize_t hlen = -1;
+ unsigned long hash = 0;
+
+ esl_assert(event);
+
+ if (!header_name) return NULL;
+
+ hash = esl_ci_hashfunc_default(header_name, &hlen);
+
+ for (hp = event->headers; hp; hp = hp->next) {
+ if ((!hp->hash || hash == hp->hash) && !strcasecmp(hp->name, header_name) ) {
+ return hp->value;
+ }
+ }
+ return NULL;
+}
+
+char *esl_event_get_body(esl_event_t *event)
+{
+ return (event ? event->body : NULL);
+}
+
+esl_status_t esl_event_del_header(esl_event_t *event, const char *header_name)
+{
+ esl_event_header_t *hp, *lp = NULL, *tp;
+ esl_status_t status = ESL_FAIL;
+ int x = 0;
+ esl_ssize_t hlen = -1;
+ unsigned long hash = 0;
+
+ tp = event->headers;
+ while (tp) {
+ hp = tp;
+ tp = tp->next;
+
+ x++;
+ esl_assert(x < 1000);
+ hash = esl_ci_hashfunc_default(header_name, &hlen);
+
+ if ((!hp->hash || hash == hp->hash) && !strcasecmp(header_name, hp->name)) {
+ if (lp) {
+ lp->next = hp->next;
+ } else {
+ event->headers = hp->next;
+ }
+ if (hp == event->last_header || !hp->next) {
+ event->last_header = lp;
+ }
+ FREE(hp->name);
+ FREE(hp->value);
+ memset(hp, 0, sizeof(*hp));
+ FREE(hp);
+
+ status = ESL_SUCCESS;
+ } else {
+ lp = hp;
+ }
+ }
+
+ return status;
+}
+
+esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data)
+{
+ esl_event_header_t *header;
+ esl_ssize_t hlen = -1;
+
+ header = ALLOC(sizeof(*header));
+ esl_assert(header);
+
+ memset(header, 0, sizeof(*header));
+
+ header->name = DUP(header_name);
+ header->value = data;
+ header->hash = esl_ci_hashfunc_default(header->name, &hlen);
+
+ if (stack == ESL_STACK_TOP) {
+ header->next = event->headers;
+ event->headers = header;
+ if (!event->last_header) {
+ event->last_header = header;
+ }
+ } else {
+ if (event->last_header) {
+ event->last_header->next = header;
+ } else {
+ event->headers = header;
+ header->next = NULL;
+ }
+ event->last_header = header;
+ }
+
+ return ESL_SUCCESS;
+}
+
+esl_status_t esl_event_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *fmt, ...)
+{
+ int ret = 0;
+ char *data;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = vasprintf(&data, fmt, ap);
+ va_end(ap);
+
+ if (ret == -1) {
+ return ESL_FAIL;
+ }
+
+ return esl_event_base_add_header(event, stack, header_name, data);
+}
+
+esl_status_t esl_event_add_header_string(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *data)
+{
+ if (data) {
+ return esl_event_base_add_header(event, stack, header_name, DUP(data));
+ }
+ return ESL_FAIL;
+}
+
+esl_status_t esl_event_add_body(esl_event_t *event, const char *fmt, ...)
+{
+ int ret = 0;
+ char *data;
+
+ va_list ap;
+ if (fmt) {
+ va_start(ap, fmt);
+ ret = vasprintf(&data, fmt, ap);
+ va_end(ap);
+
+ if (ret == -1) {
+ return ESL_FAIL;
+ } else {
+ esl_safe_free(event->body);
+ event->body = data;
+ return ESL_SUCCESS;
+ }
+ } else {
+ return ESL_FAIL;
+ }
+}
+
+void esl_event_destroy(esl_event_t **event)
+{
+ esl_event_t *ep = *event;
+ esl_event_header_t *hp, *this;
+
+ if (ep) {
+ for (hp = ep->headers; hp;) {
+ this = hp;
+ hp = hp->next;
+ FREE(this->name);
+ FREE(this->value);
+ memset(this, 0, sizeof(*this));
+ FREE(this);
+ }
+ FREE(ep->body);
+ FREE(ep->subclass_name);
+ memset(ep, 0, sizeof(*ep));
+ FREE(ep);
+ }
+ *event = NULL;
+}
+
+
+
+esl_status_t esl_event_dup(esl_event_t **event, esl_event_t *todup)
+{
+ esl_event_header_t *header, *hp, *hp2, *last = NULL;
+
+ if (esl_event_create_subclass(event, todup->event_id, todup->subclass_name) != ESL_SUCCESS) {
+ return ESL_FAIL;
+ }
+
+ if (todup->subclass_name) {
+ (*event)->subclass_name = DUP(todup->subclass_name);
+ }
+ (*event)->event_user_data = todup->event_user_data;
+ (*event)->bind_user_data = todup->bind_user_data;
+
+ hp2 = (*event)->headers;
+
+ for (hp = todup->headers; hp; hp = hp->next) {
+ header = ALLOC(sizeof(*header));
+ esl_assert(header);
+
+ memset(header, 0, sizeof(*header));
+
+ header->name = DUP(hp->name);
+ header->value = DUP(hp->value);
+
+ if (last) {
+ last->next = header;
+ } else {
+ (*event)->headers = header;
+ }
+
+ (*event)->last_header = last = header;
+ }
+
+ if (todup->body) {
+ (*event)->body = DUP(todup->body);
+ }
+
+ (*event)->key = todup->key;
+
+ return ESL_SUCCESS;
+}
+
+esl_status_t esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode)
+{
+ size_t len = 0;
+ esl_event_header_t *hp;
+ size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0;
+ char *buf;
+ char *encode_buf = NULL; /* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */
+
+ *str = NULL;
+
+ dlen = blocksize * 2;
+
+ if (!(buf = malloc(dlen))) {
+ return ESL_FAIL;
+ }
+
+ /* go ahead and give ourselves some space to work with, should save a few reallocs */
+ if (!(encode_buf = malloc(encode_len))) {
+ esl_safe_free(buf);
+ return ESL_FAIL;
+ }
+
+ /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */
+ for (hp = event->headers; hp; hp = hp->next) {
+ /*
+ * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX)
+ * so we could end up with a string that is 3 times the originals length, unlikely but rather
+ * be safe than destroy the string, also add one for the null. And try to be smart about using
+ * the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU
+ * destroying loop.
+ */
+ new_len = (strlen(hp->value) * 3) + 1;
+
+ if (encode_len < new_len) {
+ char *tmp;
+ /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); */
+ /* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */
+
+ /* keep track of the size of our allocation */
+ encode_len = new_len;
+
+ if (!(tmp = realloc(encode_buf, encode_len))) {
+ /* oh boy, ram's gone, give back what little we grabbed and bail */
+ esl_safe_free(buf);
+ esl_safe_free(encode_buf);
+ return ESL_FAIL;
+ }
+
+ encode_buf = tmp;
+ }
+
+ /* handle any bad things in the string like newlines : etc that screw up the serialized format */
+ if (encode) {
+ esl_url_encode(hp->value, encode_buf, encode_len);
+ } else {
+ snprintf(encode_buf, encode_len, "[%s]", hp->value);
+ }
+
+ llen = strlen(hp->name) + strlen(encode_buf) + 8;
+
+ if ((len + llen) > dlen) {
+ char *m;
+ dlen += (blocksize + (len + llen));
+ if ((m = realloc(buf, dlen))) {
+ buf = m;
+ } else {
+ /* we seem to be out of memory trying to resize the serialize string, give back what we already have and give up */
+ esl_safe_free(buf);
+ esl_safe_free(encode_buf);
+ return ESL_FAIL;
+ }
+ }
+
+ snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf);
+ len = strlen(buf);
+ }
+
+ /* we are done with the memory we used for encoding, give it back */
+ esl_safe_free(encode_buf);
+
+ if (event->body) {
+ int blen = (int) strlen(event->body);
+ llen = blen;
+
+ if (blen) {
+ llen += 25;
+ } else {
+ llen += 5;
+ }
+
+ if ((len + llen) > dlen) {
+ char *m;
+ dlen += (blocksize + (len + llen));
+ if ((m = realloc(buf, dlen))) {
+ buf = m;
+ } else {
+ esl_safe_free(buf);
+ return ESL_FAIL;
+ }
+ }
+
+ if (blen) {
+ snprintf(buf + len, dlen - len, "\n%s", event->body);
+ } else {
+ snprintf(buf + len, dlen - len, "\n");
+ }
+ } else {
+ snprintf(buf + len, dlen - len, "\n");
+ }
+
+ *str = buf;
+
+ return ESL_SUCCESS;
+}
+
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
Added: freeswitch/trunk/libs/esl/src/esl_threadmutex.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/esl_threadmutex.c Fri Dec 19 19:35:09 2008
@@ -0,0 +1,237 @@
+/*
+ * Cross Platform Thread/Mutex abstraction
+ * Copyright(C) 2007 Michael Jerris
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so.
+ *
+ * This work is provided under this license on an "as is" basis, without warranty of any kind,
+ * either expressed or implied, including, without limitation, warranties that the covered code
+ * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
+ * risk as to the quality and performance of the covered code is with you. Should any covered
+ * code prove defective in any respect, you (not the initial developer or any other contributor)
+ * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
+ * constitutes an essential part of this license. No use of any covered code is authorized hereunder
+ * except under this disclaimer.
+ *
+ */
+
+#ifdef WIN32
+/* required for TryEnterCriticalSection definition. Must be defined before windows.h include */
+#define _WIN32_WINNT 0x0400
+#endif
+
+#include "esl.h"
+#include "esl_threadmutex.h"
+
+#ifdef WIN32
+#include <process.h>
+
+#define ESL_THREAD_CALLING_CONVENTION __stdcall
+
+struct esl_mutex {
+ CRITICAL_SECTION mutex;
+};
+
+#else
+
+#include <pthread.h>
+
+#define ESL_THREAD_CALLING_CONVENTION
+
+struct esl_mutex {
+ pthread_mutex_t mutex;
+};
+
+#endif
+
+struct esl_thread {
+#ifdef WIN32
+ void *handle;
+#else
+ pthread_t handle;
+#endif
+ void *private_data;
+ esl_thread_function_t function;
+ size_t stack_size;
+#ifndef WIN32
+ pthread_attr_t attribute;
+#endif
+};
+
+size_t thread_default_stacksize = 0;
+
+void esl_thread_override_default_stacksize(size_t size)
+{
+ thread_default_stacksize = size;
+}
+
+static void * ESL_THREAD_CALLING_CONVENTION thread_launch(void *args)
+{
+ void *exit_val;
+ esl_thread_t *thread = (esl_thread_t *)args;
+ exit_val = thread->function(thread, thread->private_data);
+#ifndef WIN32
+ pthread_attr_destroy(&thread->attribute);
+#endif
+ free(thread);
+
+ return exit_val;
+}
+
+esl_status_t esl_thread_create_detached(esl_thread_function_t func, void *data)
+{
+ return esl_thread_create_detached_ex(func, data, thread_default_stacksize);
+}
+
+esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size)
+{
+ esl_thread_t *thread = NULL;
+ esl_status_t status = ESL_FAIL;
+
+ if (!func || !(thread = (esl_thread_t *)malloc(sizeof(esl_thread_t)))) {
+ goto done;
+ }
+
+ thread->private_data = data;
+ thread->function = func;
+ thread->stack_size = stack_size;
+
+#if defined(WIN32)
+ thread->handle = (void *)_beginthreadex(NULL, (unsigned)thread->stack_size, (unsigned int (__stdcall *)(void *))thread_launch, thread, 0, NULL);
+ if (!thread->handle) {
+ goto fail;
+ }
+ CloseHandle(thread->handle);
+
+ status = ESL_SUCCESS;
+ goto done;
+#else
+
+ if (pthread_attr_init(&thread->attribute) != 0) goto fail;
+
+ if (pthread_attr_setdetachstate(&thread->attribute, PTHREAD_CREATE_DETACHED) != 0) goto failpthread;
+
+ if (thread->stack_size && pthread_attr_setstacksize(&thread->attribute, thread->stack_size) != 0) goto failpthread;
+
+ if (pthread_create(&thread->handle, &thread->attribute, thread_launch, thread) != 0) goto failpthread;
+
+ status = ESL_SUCCESS;
+ goto done;
+ failpthread:
+ pthread_attr_destroy(&thread->attribute);
+#endif
+
+ fail:
+ if (thread) {
+ free(thread);
+ }
+ done:
+ return status;
+}
+
+
+esl_status_t esl_mutex_create(esl_mutex_t **mutex)
+{
+ esl_status_t status = ESL_FAIL;
+#ifndef WIN32
+ pthread_mutexattr_t attr;
+#endif
+ esl_mutex_t *check = NULL;
+
+ check = (esl_mutex_t *)malloc(sizeof(**mutex));
+ if (!check)
+ goto done;
+#ifdef WIN32
+ InitializeCriticalSection(&check->mutex);
+#else
+ if (pthread_mutexattr_init(&attr))
+ goto done;
+
+ if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
+ goto fail;
+
+ if (pthread_mutex_init(&check->mutex, &attr))
+ goto fail;
+
+ goto success;
+
+ fail:
+ pthread_mutexattr_destroy(&attr);
+ goto done;
+
+ success:
+#endif
+ *mutex = check;
+ status = ESL_SUCCESS;
+
+ done:
+ return status;
+}
+
+esl_status_t esl_mutex_destroy(esl_mutex_t **mutex)
+{
+ esl_mutex_t *mp = *mutex;
+ *mutex = NULL;
+ if (!mp) {
+ return ESL_FAIL;
+ }
+#ifdef WIN32
+ DeleteCriticalSection(&mp->mutex);
+#else
+ if (pthread_mutex_destroy(&mp->mutex))
+ return ESL_FAIL;
+#endif
+ free(mp);
+ return ESL_SUCCESS;
+}
+
+esl_status_t esl_mutex_lock(esl_mutex_t *mutex)
+{
+#ifdef WIN32
+ EnterCriticalSection(&mutex->mutex);
+#else
+ if (pthread_mutex_lock(&mutex->mutex))
+ return ESL_FAIL;
+#endif
+ return ESL_SUCCESS;
+}
+
+esl_status_t esl_mutex_trylock(esl_mutex_t *mutex)
+{
+#ifdef WIN32
+ if (!TryEnterCriticalSection(&mutex->mutex))
+ return ESL_FAIL;
+#else
+ if (pthread_mutex_trylock(&mutex->mutex))
+ return ESL_FAIL;
+#endif
+ return ESL_SUCCESS;
+}
+
+esl_status_t esl_mutex_unlock(esl_mutex_t *mutex)
+{
+#ifdef WIN32
+ LeaveCriticalSection(&mutex->mutex);
+#else
+ if (pthread_mutex_unlock(&mutex->mutex))
+ return ESL_FAIL;
+#endif
+ return ESL_SUCCESS;
+}
+
+
+
+
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
Added: freeswitch/trunk/libs/esl/src/include/esl.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/include/esl.h Fri Dec 19 19:35:09 2008
@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _ESL_H_
+#define _ESL_H_
+
+typedef struct esl_event_header esl_event_header_t;
+typedef struct esl_event esl_event_t;
+
+
+#define ESL_SEQ_ESC "\033["
+/* Ansi Control character suffixes */
+#define ESL_SEQ_HOME_CHAR 'H'
+#define ESL_SEQ_HOME_CHAR_STR "H"
+#define ESL_SEQ_CLEARLINE_CHAR '1'
+#define ESL_SEQ_CLEARLINE_CHAR_STR "1"
+#define ESL_SEQ_CLEARLINEEND_CHAR "K"
+#define ESL_SEQ_CLEARSCR_CHAR0 '2'
+#define ESL_SEQ_CLEARSCR_CHAR1 'J'
+#define ESL_SEQ_CLEARSCR_CHAR "2J"
+#define ESL_SEQ_DEFAULT_COLOR ESL_SEQ_ESC ESL_SEQ_END_COLOR /* Reset to Default fg/bg color */
+#define ESL_SEQ_AND_COLOR ";" /* To add multiple color definitions */
+#define ESL_SEQ_END_COLOR "m" /* To end color definitions */
+/* Foreground colors values */
+#define ESL_SEQ_F_BLACK "30"
+#define ESL_SEQ_F_RED "31"
+#define ESL_SEQ_F_GREEN "32"
+#define ESL_SEQ_F_YELLOW "33"
+#define ESL_SEQ_F_BLUE "34"
+#define ESL_SEQ_F_MAGEN "35"
+#define ESL_SEQ_F_CYAN "36"
+#define ESL_SEQ_F_WHITE "37"
+/* Background colors values */
+#define ESL_SEQ_B_BLACK "40"
+#define ESL_SEQ_B_RED "41"
+#define ESL_SEQ_B_GREEN "42"
+#define ESL_SEQ_B_YELLOW "43"
+#define ESL_SEQ_B_BLUE "44"
+#define ESL_SEQ_B_MAGEN "45"
+#define ESL_SEQ_B_CYAN "46"
+#define ESL_SEQ_B_WHITE "47"
+/* Preset escape sequences - Change foreground colors only */
+#define ESL_SEQ_FBLACK ESL_SEQ_ESC ESL_SEQ_F_BLACK ESL_SEQ_END_COLOR
+#define ESL_SEQ_FRED ESL_SEQ_ESC ESL_SEQ_F_RED ESL_SEQ_END_COLOR
+#define ESL_SEQ_FGREEN ESL_SEQ_ESC ESL_SEQ_F_GREEN ESL_SEQ_END_COLOR
+#define ESL_SEQ_FYELLOW ESL_SEQ_ESC ESL_SEQ_F_YELLOW ESL_SEQ_END_COLOR
+#define ESL_SEQ_FBLUE ESL_SEQ_ESC ESL_SEQ_F_BLUE ESL_SEQ_END_COLOR
+#define ESL_SEQ_FMAGEN ESL_SEQ_ESC ESL_SEQ_F_MAGEN ESL_SEQ_END_COLOR
+#define ESL_SEQ_FCYAN ESL_SEQ_ESC ESL_SEQ_F_CYAN ESL_SEQ_END_COLOR
+#define ESL_SEQ_FWHITE ESL_SEQ_ESC ESL_SEQ_F_WHITE ESL_SEQ_END_COLOR
+#define ESL_SEQ_BBLACK ESL_SEQ_ESC ESL_SEQ_B_BLACK ESL_SEQ_END_COLOR
+#define ESL_SEQ_BRED ESL_SEQ_ESC ESL_SEQ_B_RED ESL_SEQ_END_COLOR
+#define ESL_SEQ_BGREEN ESL_SEQ_ESC ESL_SEQ_B_GREEN ESL_SEQ_END_COLOR
+#define ESL_SEQ_BYELLOW ESL_SEQ_ESC ESL_SEQ_B_YELLOW ESL_SEQ_END_COLOR
+#define ESL_SEQ_BBLUE ESL_SEQ_ESC ESL_SEQ_B_BLUE ESL_SEQ_END_COLOR
+#define ESL_SEQ_BMAGEN ESL_SEQ_ESC ESL_SEQ_B_MAGEN ESL_SEQ_END_COLOR
+#define ESL_SEQ_BCYAN ESL_SEQ_ESC ESL_SEQ_B_CYAN ESL_SEQ_END_COLOR
+#define ESL_SEQ_BWHITE ESL_SEQ_ESC ESL_SEQ_B_WHITE ESL_SEQ_END_COLOR
+/* Preset escape sequences */
+#define ESL_SEQ_HOME ESL_SEQ_ESC ESL_SEQ_HOME_CHAR_STR
+#define ESL_SEQ_CLEARLINE ESL_SEQ_ESC ESL_SEQ_CLEARLINE_CHAR_STR
+#define ESL_SEQ_CLEARLINEEND ESL_SEQ_ESC ESL_SEQ_CLEARLINEEND_CHAR
+#define ESL_SEQ_CLEARSCR ESL_SEQ_ESC ESL_SEQ_CLEARSCR_CHAR ESL_SEQ_HOME
+
+#if !defined(_XOPEN_SOURCE) && !defined(__FreeBSD__) && !defined(__NetBSD__)
+#define _XOPEN_SOURCE 600
+#endif
+
+#ifndef HAVE_STRINGS_H
+#define HAVE_STRINGS_H 1
+#endif
+#ifndef HAVE_SYS_SOCKET_H
+#define HAVE_SYS_SOCKET_H 1
+#endif
+
+#ifndef __WINDOWS__
+#if defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)
+#define __WINDOWS__
+#endif
+#endif
+
+#ifdef _MSC_VER
+#ifndef __inline__
+#define __inline__ __inline
+#endif
+#if (_MSC_VER >= 1400) /* VC8+ */
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+#ifndef _CRT_NONSTDC_NO_DEPRECATE
+#define _CRT_NONSTDC_NO_DEPRECATE
+#endif
+#endif
+#ifndef strcasecmp
+#define strcasecmp(s1, s2) _stricmp(s1, s2)
+#endif
+#ifndef strncasecmp
+#define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
+#endif
+#ifndef snprintf
+#define snprintf _snprintf
+#endif
+#ifndef S_IRUSR
+#define S_IRUSR _S_IREAD
+#endif
+#ifndef S_IWUSR
+#define S_IWUSR _S_IWRITE
+#endif
+#undef HAVE_STRINGS_H
+#undef HAVE_SYS_SOCKET_H
+#endif
+
+#include <time.h>
+#ifndef WIN32
+#include <sys/time.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#include <assert.h>
+
+
+#define esl_assert(_x) assert(_x)
+#define esl_safe_free(_x) if (_x) free(_x); _x = NULL
+#define esl_strlen_zero(s) (!s || *(s) == '\0')
+#define esl_strlen_zero_buf(s) (*(s) == '\0')
+
+#ifdef WIN32
+#include <windows.h>
+typedef HANDLE esl_socket_t;
+typedef unsigned __int64 uint64_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int8 uint8_t;
+typedef __int64 int64_t;
+typedef __int32 int32_t;
+typedef __int16 int16_t;
+typedef __int8 int8_t;
+typedef intptr_t esl_ssize_t;
+typedef int esl_filehandle_t;
+#else
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#define ESL_SOCK_INVALID -1
+typedef int esl_socket_t;
+typedef ssize_t esl_ssize_t;
+typedef int esl_filehandle_t;
+#endif
+
+typedef int16_t esl_port_t;
+
+typedef enum {
+ ESL_SUCCESS,
+ ESL_FAIL
+} esl_status_t;
+
+
+typedef struct {
+ struct sockaddr_in sockaddr;
+ struct hostent hostent;
+ char hostbuf[256];
+ esl_socket_t sock;
+ char err[256];
+ int errno;
+ char header_buf[4196];
+ char last_reply[1024];
+ esl_event_t *last_event;
+ int debug;
+ int connected;
+} esl_handle_t;
+
+typedef enum {
+ ESL_TRUE = 1,
+ ESL_FALSE = 0
+} esl_bool_t;
+
+
+#include "esl_event.h"
+#include "esl_threadmutex.h"
+
+size_t esl_url_encode(const char *url, char *buf, size_t len);
+char *esl_url_decode(char *s);
+
+esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password);
+esl_status_t esl_disconnect(esl_handle_t *handle);
+esl_status_t esl_send(esl_handle_t *handle, const char *cmd);
+esl_status_t esl_recv(esl_handle_t *handle);
+esl_status_t esl_send_recv(esl_handle_t *handle, const char *cmd);
+
+
+#endif
+
+
+
Added: freeswitch/trunk/libs/esl/src/include/esl_event.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/include/esl_event.h Fri Dec 19 19:35:09 2008
@@ -0,0 +1,284 @@
+/*
+ * Copyright (c) 2007, Anthony Minessale II
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the original author; nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef ESL_EVENT_H
+#define ESL_EVENT_H
+
+#include <esl.h>
+
+typedef enum {
+ ESL_STACK_BOTTOM,
+ ESL_STACK_TOP
+} esl_stack_t;
+
+typedef enum {
+ ESL_EVENT_CUSTOM,
+ ESL_EVENT_CHANNEL_CREATE,
+ ESL_EVENT_CHANNEL_DESTROY,
+ ESL_EVENT_CHANNEL_STATE,
+ ESL_EVENT_CHANNEL_ANSWER,
+ ESL_EVENT_CHANNEL_HANGUP,
+ ESL_EVENT_CHANNEL_EXECUTE,
+ ESL_EVENT_CHANNEL_EXECUTE_COMPLETE,
+ ESL_EVENT_CHANNEL_BRIDGE,
+ ESL_EVENT_CHANNEL_UNBRIDGE,
+ ESL_EVENT_CHANNEL_PROGRESS,
+ ESL_EVENT_CHANNEL_PROGRESS_MEDIA,
+ ESL_EVENT_CHANNEL_OUTGOING,
+ ESL_EVENT_CHANNEL_PARK,
+ ESL_EVENT_CHANNEL_UNPARK,
+ ESL_EVENT_CHANNEL_APPLICATION,
+ ESL_EVENT_CHANNEL_ORIGINATE,
+ ESL_EVENT_CHANNEL_UUID,
+ ESL_EVENT_API,
+ ESL_EVENT_LOG,
+ ESL_EVENT_INBOUND_CHAN,
+ ESL_EVENT_OUTBOUND_CHAN,
+ ESL_EVENT_STARTUP,
+ ESL_EVENT_SHUTDOWN,
+ ESL_EVENT_PUBLISH,
+ ESL_EVENT_UNPUBLISH,
+ ESL_EVENT_TALK,
+ ESL_EVENT_NOTALK,
+ ESL_EVENT_SESSION_CRASH,
+ ESL_EVENT_MODULE_LOAD,
+ ESL_EVENT_MODULE_UNLOAD,
+ ESL_EVENT_DTMF,
+ ESL_EVENT_MESSAGE,
+ ESL_EVENT_PRESENCE_IN,
+ ESL_EVENT_NOTIFY_IN,
+ ESL_EVENT_PRESENCE_OUT,
+ ESL_EVENT_PRESENCE_PROBE,
+ ESL_EVENT_MESSAGE_WAITING,
+ ESL_EVENT_MESSAGE_QUERY,
+ ESL_EVENT_ROSTER,
+ ESL_EVENT_CODEC,
+ ESL_EVENT_BACKGROUND_JOB,
+ ESL_EVENT_DETECTED_SPEECH,
+ ESL_EVENT_DETECTED_TONE,
+ ESL_EVENT_PRIVATE_COMMAND,
+ ESL_EVENT_HEARTBEAT,
+ ESL_EVENT_TRAP,
+ ESL_EVENT_ADD_SCHEDULE,
+ ESL_EVENT_DEL_SCHEDULE,
+ ESL_EVENT_EXE_SCHEDULE,
+ ESL_EVENT_RE_SCHEDULE,
+ ESL_EVENT_RELOADXML,
+ ESL_EVENT_NOTIFY,
+ ESL_EVENT_SEND_MESSAGE,
+ ESL_EVENT_RECV_MESSAGE,
+ ESL_EVENT_REQUEST_PARAMS,
+ ESL_EVENT_CHANNEL_DATA,
+ ESL_EVENT_GENERAL,
+ ESL_EVENT_COMMAND,
+ ESL_EVENT_SESSION_HEARTBEAT,
+ ESL_EVENT_ALL
+} esl_event_types_t;
+
+typedef enum {
+ ESL_PRIORITY_NORMAL,
+ ESL_PRIORITY_LOW,
+ ESL_PRIORITY_HIGH
+} esl_priority_t;
+
+/*! \brief An event Header */
+ struct esl_event_header {
+ /*! the header name */
+ char *name;
+ /*! the header value */
+ char *value;
+ /*! hash of the header name */
+ unsigned long hash;
+ struct esl_event_header *next;
+};
+
+
+/*! \brief Representation of an event */
+struct esl_event {
+ /*! the event id (descriptor) */
+ esl_event_types_t event_id;
+ /*! the priority of the event */
+ esl_priority_t priority;
+ /*! the owner of the event */
+ char *owner;
+ /*! the subclass of the event */
+ char *subclass_name;
+ /*! the event headers */
+ esl_event_header_t *headers;
+ /*! the event headers tail pointer */
+ esl_event_header_t *last_header;
+ /*! the body of the event */
+ char *body;
+ /*! user data from the subclass provider */
+ void *bind_user_data;
+ /*! user data from the event sender */
+ void *event_user_data;
+ /*! unique key */
+ unsigned long key;
+ struct esl_event *next;
+};
+
+
+
+#define ESL_EVENT_SUBCLASS_ANY NULL
+
+/*!
+ \brief Create an event
+ \param event a NULL pointer on which to create the event
+ \param event_id the event id enumeration of the desired event
+ \param subclass_name the subclass name for custom event (only valid when event_id is ESL_EVENT_CUSTOM)
+ \return ESL_STATUS_SUCCESS on success
+*/
+esl_status_t esl_event_create_subclass(esl_event_t **event, esl_event_types_t event_id, const char *subclass_name);
+
+/*!
+ \brief Set the priority of an event
+ \param event the event to set the priority on
+ \param priority the event priority
+ \return ESL_STATUS_SUCCESS
+*/
+esl_status_t esl_event_set_priority(esl_event_t *event, esl_priority_t priority);
+
+/*!
+ \brief Retrieve a header value from an event
+ \param event the event to read the header from
+ \param header_name the name of the header to read
+ \return the value of the requested header
+*/
+char *esl_event_get_header(esl_event_t *event, const char *header_name);
+
+/*!
+ \brief Retrieve the body value from an event
+ \param event the event to read the body from
+ \return the value of the body or NULL
+*/
+char *esl_event_get_body(esl_event_t *event);
+
+/*!
+ \brief Add a header to an event
+ \param event the event to add the header to
+ \param stack the stack sense (stack it on the top or on the bottom)
+ \param header_name the name of the header to add
+ \param fmt the value of the header (varargs see standard sprintf family)
+ \return ESL_STATUS_SUCCESS if the header was added
+*/
+esl_status_t esl_event_add_header(esl_event_t *event, esl_stack_t stack,
+ const char *header_name, const char *fmt, ...); //PRINTF_FUNCTION(4, 5);
+
+/*!
+ \brief Add a string header to an event
+ \param event the event to add the header to
+ \param stack the stack sense (stack it on the top or on the bottom)
+ \param header_name the name of the header to add
+ \param data the value of the header
+ \return ESL_STATUS_SUCCESS if the header was added
+*/
+esl_status_t esl_event_add_header_string(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *data);
+
+esl_status_t esl_event_del_header(esl_event_t *event, const char *header_name);
+
+/*!
+ \brief Destroy an event
+ \param event pointer to the pointer to event to destroy
+*/
+void esl_event_destroy(esl_event_t **event);
+#define esl_event_safe_destroy(_event) if (_event) esl_event_destroy(_event)
+
+/*!
+ \brief Duplicate an event
+ \param event a NULL pointer on which to duplicate the event
+ \param todup an event to duplicate
+ \return ESL_STATUS_SUCCESS if the event was duplicated
+*/
+esl_status_t esl_event_dup(esl_event_t **event, esl_event_t *todup);
+
+/*!
+ \brief Render the name of an event id enumeration
+ \param event the event id to render the name of
+ \return the rendered name
+*/
+const char *esl_event_name(esl_event_types_t event);
+
+/*!
+ \brief return the event id that matches a given event name
+ \param name the name of the event
+ \param type the event id to return
+ \return ESL_STATUS_SUCCESS if there was a match
+*/
+esl_status_t esl_name_event(const char *name, esl_event_types_t *type);
+
+/*!
+ \brief Render a string representation of an event sutable for printing or network transport
+ \param event the event to render
+ \param str a string pointer to point at the allocated data
+ \param encode url encode the headers
+ \return ESL_STATUS_SUCCESS if the operation was successful
+ \note you must free the resulting string when you are finished with it
+*/
+esl_status_t esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode);
+
+/*!
+ \brief Add a body to an event
+ \param event the event to add to body to
+ \param fmt optional body of the event (varargs see standard sprintf family)
+ \return ESL_STATUS_SUCCESS if the body was added to the event
+ \note the body parameter can be shadowed by the esl_event_reserve_subclass_detailed function
+*/
+esl_status_t esl_event_add_body(esl_event_t *event, const char *fmt, ...);
+
+/*!
+ \brief Create a new event assuming it will not be custom event and therefore hiding the unused parameters
+ \param event a NULL pointer on which to create the event
+ \param id the event id enumeration of the desired event
+ \return ESL_STATUS_SUCCESS on success
+*/
+#define esl_event_create(event, id) esl_event_create_subclass(event, id, ESL_EVENT_SUBCLASS_ANY)
+
+const char *esl_priority_name(esl_priority_t priority);
+
+///\}
+
+
+#endif
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
+
Added: freeswitch/trunk/libs/esl/src/include/esl_threadmutex.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/src/include/esl_threadmutex.h Fri Dec 19 19:35:09 2008
@@ -0,0 +1,51 @@
+/*
+ * Cross Platform Thread/Mutex abstraction
+ * Copyright(C) 2007 Michael Jerris
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so.
+ *
+ * This work is provided under this license on an "as is" basis, without warranty of any kind,
+ * either expressed or implied, including, without limitation, warranties that the covered code
+ * is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
+ * risk as to the quality and performance of the covered code is with you. Should any covered
+ * code prove defective in any respect, you (not the initial developer or any other contributor)
+ * assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
+ * constitutes an essential part of this license. No use of any covered code is authorized hereunder
+ * except under this disclaimer.
+ *
+ */
+
+
+#ifndef _ESL_THREADMUTEX_H
+#define _ESL_THREADMUTEX_H
+
+#include "esl.h"
+
+typedef struct esl_mutex esl_mutex_t;
+typedef struct esl_thread esl_thread_t;
+typedef void *(*esl_thread_function_t) (esl_thread_t *, void *);
+
+esl_status_t esl_thread_create_detached(esl_thread_function_t func, void *data);
+esl_status_t esl_thread_create_detached_ex(esl_thread_function_t func, void *data, size_t stack_size);
+void esl_thread_override_default_stacksize(size_t size);
+esl_status_t esl_mutex_create(esl_mutex_t **mutex);
+esl_status_t esl_mutex_destroy(esl_mutex_t **mutex);
+esl_status_t esl_mutex_lock(esl_mutex_t *mutex);
+esl_status_t esl_mutex_trylock(esl_mutex_t *mutex);
+esl_status_t esl_mutex_unlock(esl_mutex_t *mutex);
+
+#endif
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
+
Added: freeswitch/trunk/libs/esl/testclient.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/esl/testclient.c Fri Dec 19 19:35:09 2008
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <esl.h>
+
+
+int main(void)
+{
+ esl_handle_t handle = {0};
+
+ handle.debug = 1;
+
+ esl_connect(&handle, "localhost", 8021, "ClueCon");
+
+ esl_send_recv(&handle, "api status\n\n");
+
+ esl_disconnect(&handle);
+
+ return 0;
+}
1
0