2006-07-22 21:49:52 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
2006-07-24 15:45:36 +00:00
|
|
|
* mod_event_socket.c -- Socket Controled Event Handler
|
2006-07-22 21:49:52 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch.h>
|
|
|
|
#define CMD_BUFLEN 1024 * 1000
|
|
|
|
|
|
|
|
static const char modname[] = "mod_event_socket";
|
|
|
|
static char *MARKER = "1";
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
LFLAG_AUTHED = (1 << 0),
|
|
|
|
LFLAG_RUNNING = (1 << 1),
|
|
|
|
LFLAG_EVENTS = (1 << 2),
|
2007-01-06 17:06:18 +00:00
|
|
|
LFLAG_LOG = (1 << 3),
|
|
|
|
LFLAG_FULL = (1 << 4),
|
|
|
|
LFLAG_MYEVENTS = (1 << 5),
|
|
|
|
LFLAG_SESSION = (1 << 6),
|
|
|
|
LFLAG_ASYNC = (1 << 7)
|
2006-07-22 21:49:52 +00:00
|
|
|
} event_flag_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EVENT_FORMAT_PLAIN,
|
|
|
|
EVENT_FORMAT_XML
|
|
|
|
} event_format_t;
|
|
|
|
|
|
|
|
struct listener {
|
|
|
|
switch_socket_t *sock;
|
|
|
|
switch_queue_t *event_queue;
|
|
|
|
switch_queue_t *log_queue;
|
|
|
|
switch_memory_pool_t *pool;
|
|
|
|
event_format_t format;
|
|
|
|
switch_mutex_t *flag_mutex;
|
|
|
|
uint32_t flags;
|
|
|
|
switch_log_level_t level;
|
2006-08-29 23:06:12 +00:00
|
|
|
char *ebuf;
|
2006-08-30 04:58:13 +00:00
|
|
|
uint8_t event_list[SWITCH_EVENT_ALL+1];
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_hash_t *event_hash;
|
2006-09-07 03:58:01 +00:00
|
|
|
switch_thread_rwlock_t *rwlock;
|
2007-01-06 17:06:18 +00:00
|
|
|
switch_core_session_t *session;
|
2006-07-22 21:49:52 +00:00
|
|
|
struct listener *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct listener listener_t;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
switch_socket_t *sock;
|
|
|
|
switch_mutex_t *mutex;
|
2006-08-29 20:27:43 +00:00
|
|
|
switch_mutex_t *sock_mutex;
|
2006-07-22 21:49:52 +00:00
|
|
|
listener_t *listeners;
|
|
|
|
uint8_t ready;
|
|
|
|
} listen_list;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
char *ip;
|
|
|
|
uint16_t port;
|
|
|
|
char *password;
|
2006-11-09 05:39:04 +00:00
|
|
|
int done;
|
2006-07-22 21:49:52 +00:00
|
|
|
} prefs;
|
|
|
|
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_ip, prefs.ip)
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_pass, prefs.password)
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj);
|
|
|
|
static void launch_listener_thread(listener_t *listener);
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_level_t level)
|
|
|
|
{
|
|
|
|
listener_t *l;
|
|
|
|
|
|
|
|
switch_mutex_lock(listen_list.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) {
|
|
|
|
switch_queue_push(l->log_queue, data);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(listen_list.mutex);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler(switch_event_t *event)
|
|
|
|
{
|
|
|
|
switch_event_t *clone = NULL;
|
|
|
|
listener_t *l;
|
|
|
|
|
|
|
|
assert(event != NULL);
|
|
|
|
|
|
|
|
if (!listen_list.ready) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_mutex_lock(listen_list.mutex);
|
|
|
|
for (l = listen_list.listeners; l; l = l->next) {
|
2006-07-25 14:14:07 +00:00
|
|
|
uint8_t send = 0;
|
|
|
|
|
|
|
|
if (!switch_test_flag(l, LFLAG_EVENTS)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (l->event_list[(uint8_t)SWITCH_EVENT_ALL]) {
|
2006-07-25 14:14:07 +00:00
|
|
|
send = 1;
|
|
|
|
} else if ((l->event_list[(uint8_t)event->event_id])) {
|
2006-07-24 15:45:36 +00:00
|
|
|
if (event->event_id != SWITCH_EVENT_CUSTOM || (event->subclass && switch_core_hash_find(l->event_hash, event->subclass->name))) {
|
2006-07-25 14:14:07 +00:00
|
|
|
send = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (send && switch_test_flag(l, LFLAG_MYEVENTS)) {
|
|
|
|
char *uuid = switch_event_get_header(event, "unique-id");
|
|
|
|
if (!uuid || strcmp(uuid, switch_core_session_get_uuid(l->session))) {
|
|
|
|
send = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (send) {
|
|
|
|
if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_queue_push(l->event_queue, clone);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-25 14:14:07 +00:00
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
switch_mutex_unlock(listen_list.mutex);
|
|
|
|
}
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
static void socket_function(switch_core_session_t *session, char *data)
|
|
|
|
{
|
|
|
|
char *host, *port_name;
|
|
|
|
switch_socket_t *new_sock;
|
|
|
|
switch_sockaddr_t *sa;
|
2007-01-06 20:24:37 +00:00
|
|
|
switch_port_t port = 8084;
|
2007-01-06 17:06:18 +00:00
|
|
|
listener_t *listener;
|
|
|
|
int argc = 0, x = 0;
|
|
|
|
char *argv[80] = {0};
|
|
|
|
char *mydata;
|
|
|
|
|
|
|
|
if (data && (mydata = switch_core_session_strdup(session, data))) {
|
|
|
|
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse Error!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
host = argv[0];
|
|
|
|
|
|
|
|
if (switch_strlen_zero(host)) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing Host!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((port_name = strchr(host, ':'))) {
|
|
|
|
*port_name++ = '\0';
|
2007-01-06 20:24:37 +00:00
|
|
|
port = (switch_port_t)atoi(port_name);
|
2007-01-06 17:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_sockaddr_info_get(&sa, host, AF_INET, port, 0, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
if (switch_socket_create(&new_sock, AF_INET, SOCK_STREAM, SWITCH_PROTO_TCP, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
|
2007-01-06 17:06:18 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_socket_opt_set(new_sock, SWITCH_SO_KEEPALIVE, 1);
|
|
|
|
|
|
|
|
if (switch_socket_connect(new_sock, sa) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!(listener = switch_core_session_alloc(session, sizeof(*listener)))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_thread_rwlock_create(&listener->rwlock, switch_core_session_get_pool(session));
|
|
|
|
switch_queue_create(&listener->event_queue, SWITCH_CORE_QUEUE_LEN, switch_core_session_get_pool(session));
|
|
|
|
switch_queue_create(&listener->log_queue, SWITCH_CORE_QUEUE_LEN, switch_core_session_get_pool(session));
|
|
|
|
|
|
|
|
listener->sock = new_sock;
|
|
|
|
listener->pool = switch_core_session_get_pool(session);
|
|
|
|
listener->format = EVENT_FORMAT_PLAIN;
|
|
|
|
listener->session = session;
|
|
|
|
|
|
|
|
switch_mutex_init(&listener->flag_mutex, SWITCH_MUTEX_NESTED, listener->pool);
|
|
|
|
switch_core_hash_init(&listener->event_hash, listener->pool);
|
|
|
|
switch_set_flag(listener, LFLAG_AUTHED);
|
|
|
|
for(x = 1; x < argc; x++) {
|
|
|
|
if (argv[x] && !strcasecmp(argv[x], "full")) {
|
|
|
|
switch_set_flag(listener, LFLAG_FULL);
|
|
|
|
} else if (argv[x] && !strcasecmp(argv[x], "async")) {
|
|
|
|
switch_set_flag(listener, LFLAG_ASYNC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(listener, LFLAG_ASYNC)) {
|
|
|
|
launch_listener_thread(listener);
|
2007-02-16 23:12:20 +00:00
|
|
|
switch_ivr_park(session, NULL);
|
2007-01-06 17:06:18 +00:00
|
|
|
} else {
|
|
|
|
listener_run(NULL, (void*) listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(switch_test_flag(listener, LFLAG_SESSION)) {
|
|
|
|
switch_yield(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const switch_application_interface_t socket_application_interface = {
|
|
|
|
/*.interface_name */ "socket",
|
|
|
|
/*.application_function */ socket_function,
|
|
|
|
/* long_desc */ "Connect to a socket",
|
|
|
|
/* short_desc */ "Connect to a socket",
|
|
|
|
/* syntax */ "<ip>[:<port>]",
|
2007-02-26 21:38:10 +00:00
|
|
|
/* flags */ SAF_SUPPORT_NOMEDIA,
|
2007-01-06 17:06:18 +00:00
|
|
|
/*.next */ NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
static switch_loadable_module_interface_t event_socket_module_interface = {
|
|
|
|
/*.module_name */ modname,
|
|
|
|
/*.endpoint_interface */ NULL,
|
|
|
|
/*.timer_interface */ NULL,
|
|
|
|
/*.dialplan_interface */ NULL,
|
|
|
|
/*.codec_interface */ NULL,
|
2007-01-06 17:06:18 +00:00
|
|
|
/*.application_interface */ &socket_application_interface
|
2006-07-22 21:49:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void close_socket(switch_socket_t **sock) {
|
2006-08-29 20:27:43 +00:00
|
|
|
switch_mutex_lock(listen_list.sock_mutex);
|
2006-07-22 21:49:52 +00:00
|
|
|
if (*sock) {
|
2007-03-09 20:44:13 +00:00
|
|
|
switch_socket_shutdown(*sock, SWITCH_SHUTDOWN_READWRITE);
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_socket_close(*sock);
|
|
|
|
*sock = NULL;
|
|
|
|
}
|
2006-08-29 20:27:43 +00:00
|
|
|
switch_mutex_unlock(listen_list.sock_mutex);
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_shutdown(void)
|
|
|
|
{
|
2006-07-25 14:14:07 +00:00
|
|
|
listener_t *l;
|
|
|
|
|
2006-11-09 05:39:04 +00:00
|
|
|
prefs.done = 1;
|
|
|
|
|
2006-08-29 20:27:43 +00:00
|
|
|
close_socket(&listen_list.sock);
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_mutex_lock(listen_list.mutex);
|
|
|
|
for (l = listen_list.listeners; l; l = l->next) {
|
|
|
|
close_socket(&l->sock);
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(listen_list.mutex);
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2006-08-29 20:27:43 +00:00
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
|
|
|
|
{
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
|
|
|
*module_interface = &event_socket_module_interface;
|
|
|
|
|
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_listener(listener_t *listener)
|
|
|
|
{
|
|
|
|
/* add me to the listeners so I get events */
|
|
|
|
switch_mutex_lock(listen_list.mutex);
|
|
|
|
listener->next = listen_list.listeners;
|
|
|
|
listen_list.listeners = listener;
|
|
|
|
switch_mutex_unlock(listen_list.mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_listener(listener_t *listener)
|
|
|
|
{
|
|
|
|
listener_t *l, *last = NULL;
|
|
|
|
|
|
|
|
switch_mutex_lock(listen_list.mutex);
|
|
|
|
for (l = listen_list.listeners; l; l = l->next) {
|
|
|
|
if (l == listener) {
|
|
|
|
if (last) {
|
|
|
|
last->next = l->next;
|
|
|
|
} else {
|
|
|
|
listen_list.listeners = l->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = l;
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(listen_list.mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strip_cr(char *s)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
if ((p = strchr(s, '\r')) || (p = strchr(s, '\n'))) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
static switch_status_t read_packet(listener_t *listener, switch_event_t **event, uint32_t timeout)
|
|
|
|
{
|
2006-09-28 01:39:42 +00:00
|
|
|
switch_size_t mlen, bytes = 0;
|
2006-09-25 20:04:40 +00:00
|
|
|
char mbuf[2048] = "";
|
2006-07-25 14:14:07 +00:00
|
|
|
char buf[1024] = "";
|
|
|
|
switch_size_t len;
|
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2006-09-28 01:39:42 +00:00
|
|
|
int count = 0;
|
2006-07-25 14:14:07 +00:00
|
|
|
uint32_t elapsed = 0;
|
|
|
|
time_t start = 0;
|
|
|
|
void *pop;
|
|
|
|
char *ptr;
|
|
|
|
uint8_t crcount = 0;
|
2006-09-25 20:04:40 +00:00
|
|
|
uint32_t max_len = sizeof(mbuf);
|
2006-07-25 14:14:07 +00:00
|
|
|
*event = NULL;
|
|
|
|
start = time(NULL);
|
|
|
|
ptr = mbuf;
|
|
|
|
|
|
|
|
while(listener->sock) {
|
|
|
|
uint8_t do_sleep = 1;
|
|
|
|
mlen = 1;
|
|
|
|
status = switch_socket_recv(listener->sock, ptr, &mlen);
|
|
|
|
|
2006-08-11 18:20:04 +00:00
|
|
|
if (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_FALSE;
|
2006-07-25 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (listener->session && !switch_channel_ready(switch_core_session_get_channel(listener->session))) {
|
|
|
|
status = SWITCH_STATUS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-08-11 18:20:04 +00:00
|
|
|
if (mlen) {
|
2006-09-25 20:04:40 +00:00
|
|
|
bytes += mlen;
|
2006-08-11 18:20:04 +00:00
|
|
|
do_sleep = 0;
|
2006-07-25 14:14:07 +00:00
|
|
|
|
|
|
|
if (*mbuf == '\r' || *mbuf == '\n') { /* bah */
|
|
|
|
ptr = mbuf;
|
2006-08-11 18:59:25 +00:00
|
|
|
mbuf[0] = '\0';
|
2006-09-25 20:04:40 +00:00
|
|
|
bytes = 0;
|
2006-07-25 14:14:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*ptr == '\n') {
|
|
|
|
crcount++;
|
|
|
|
} else if (*ptr != '\r') {
|
|
|
|
crcount = 0;
|
|
|
|
}
|
|
|
|
ptr++;
|
2006-09-25 20:04:40 +00:00
|
|
|
|
|
|
|
if (bytes >= max_len) {
|
|
|
|
crcount = 2;
|
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (crcount == 2) {
|
|
|
|
char *next;
|
|
|
|
char *cur = mbuf;
|
2006-09-25 20:04:40 +00:00
|
|
|
bytes = 0;
|
2006-07-25 14:14:07 +00:00
|
|
|
while(cur) {
|
|
|
|
if ((next = strchr(cur, '\r')) || (next = strchr(cur, '\n'))) {
|
|
|
|
while (*next == '\r' || *next == '\n') {
|
|
|
|
next++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
if (count == 1) {
|
|
|
|
switch_event_create(event, SWITCH_EVENT_MESSAGE);
|
2007-02-13 04:43:49 +00:00
|
|
|
switch_event_add_header(*event, SWITCH_STACK_BOTTOM, "Command", "%s", mbuf);
|
2006-09-07 03:58:01 +00:00
|
|
|
} else if (cur) {
|
2006-07-25 14:14:07 +00:00
|
|
|
char *var, *val;
|
2006-09-07 03:58:01 +00:00
|
|
|
var = cur;
|
|
|
|
strip_cr(var);
|
|
|
|
if (!switch_strlen_zero(var)) {
|
|
|
|
if ((val = strchr(var, ':'))) {
|
|
|
|
*val++ = '\0';
|
|
|
|
while(*val == ' ') {
|
|
|
|
val++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (var && val) {
|
2007-02-13 04:43:49 +00:00
|
|
|
switch_event_add_header(*event, SWITCH_STACK_BOTTOM, var, "%s", val);
|
2006-07-25 14:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-01-06 17:06:18 +00:00
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (timeout) {
|
|
|
|
elapsed = (uint32_t)(time(NULL) - start);
|
|
|
|
if (elapsed >= timeout) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2006-08-11 18:20:04 +00:00
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
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) {
|
2007-03-09 20:44:13 +00:00
|
|
|
snprintf(buf, sizeof(buf), "Content-Type: log/data\nContent-Length: %"SWITCH_SSIZE_T_FMT"\n\n", strlen(data));
|
2006-10-13 14:32:38 +00:00
|
|
|
len = strlen(buf);
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_socket_send(listener->sock, buf, &len);
|
2006-10-13 14:32:38 +00:00
|
|
|
len = strlen(data);
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_socket_send(listener->sock, data, &len);
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
do_sleep = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_test_flag(listener, LFLAG_EVENTS)) {
|
|
|
|
if (switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
char hbuf[512];
|
|
|
|
switch_event_t *event = (switch_event_t *) pop;
|
2006-11-11 19:32:45 +00:00
|
|
|
char *etype;
|
2006-08-29 23:06:12 +00:00
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
do_sleep = 0;
|
|
|
|
if (listener->format == EVENT_FORMAT_PLAIN) {
|
|
|
|
etype = "plain";
|
2006-11-11 19:32:45 +00:00
|
|
|
switch_event_serialize(event, &listener->ebuf);
|
2006-07-25 14:14:07 +00:00
|
|
|
} else {
|
|
|
|
switch_xml_t xml;
|
|
|
|
etype = "xml";
|
|
|
|
|
2007-03-19 00:41:23 +00:00
|
|
|
if ((xml = switch_event_xmlize(event, "%s", ""))) {
|
2006-11-11 19:32:45 +00:00
|
|
|
listener->ebuf = switch_xml_toxml(xml);
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XML ERROR!\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-11 19:32:45 +00:00
|
|
|
len = strlen(listener->ebuf);
|
2006-07-25 14:14:07 +00:00
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
snprintf(hbuf, sizeof(hbuf), "Content-Length: %"SWITCH_SSIZE_T_FMT"\n"
|
2006-07-25 14:14:07 +00:00
|
|
|
"Content-Type: text/event-%s\n"
|
|
|
|
"\n", len, etype);
|
|
|
|
|
2006-10-13 14:32:38 +00:00
|
|
|
len = strlen(hbuf);
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_socket_send(listener->sock, hbuf, &len);
|
|
|
|
|
2006-11-11 19:32:45 +00:00
|
|
|
len = strlen(listener->ebuf);
|
|
|
|
switch_socket_send(listener->sock, listener->ebuf, &len);
|
2006-07-25 14:14:07 +00:00
|
|
|
|
2006-11-11 19:32:45 +00:00
|
|
|
switch_safe_free(listener->ebuf);
|
2006-07-25 14:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (do_sleep) {
|
|
|
|
switch_yield(1000);
|
|
|
|
}
|
|
|
|
}
|
2006-11-11 19:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
struct api_command_struct {
|
|
|
|
char *api_cmd;
|
|
|
|
char *arg;
|
|
|
|
listener_t *listener;
|
|
|
|
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
|
|
|
|
uint8_t bg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct api_command_struct *acs = (struct api_command_struct *) obj;
|
|
|
|
switch_stream_handle_t stream = {0};
|
2007-02-06 21:42:22 +00:00
|
|
|
char *reply, *freply = NULL;
|
|
|
|
switch_status_t status;
|
2006-09-07 03:58:01 +00:00
|
|
|
|
|
|
|
if (switch_thread_rwlock_tryrdlock(acs->listener->rwlock) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error! cannot get read lock.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_STANDARD_STREAM(stream);
|
|
|
|
|
2007-02-06 21:42:22 +00:00
|
|
|
if ((status = switch_api_execute(acs->api_cmd, acs->arg, NULL, &stream)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
reply = stream.data;
|
|
|
|
} else {
|
|
|
|
freply = switch_mprintf("%s: Command not found!", acs->api_cmd);
|
|
|
|
reply = freply;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reply) {
|
|
|
|
reply = "Command returned no output!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acs->bg) {
|
|
|
|
switch_event_t *event;
|
|
|
|
|
|
|
|
if (switch_event_create(&event, SWITCH_EVENT_BACKGROUND_JOB) == SWITCH_STATUS_SUCCESS) {
|
2007-02-13 04:43:49 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Job-UUID", "%s", acs->uuid_str);
|
2007-02-14 03:45:10 +00:00
|
|
|
switch_event_add_body(event, "%s", reply);
|
2007-02-06 21:42:22 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
}
|
|
|
|
} else {
|
2007-02-23 16:43:07 +00:00
|
|
|
switch_size_t rlen, blen;
|
|
|
|
char buf[1024] = "";
|
|
|
|
rlen = strlen(reply);
|
2007-03-09 20:44:13 +00:00
|
|
|
snprintf(buf, sizeof(buf), "Content-Type: api/response\nContent-Length: %"SWITCH_SSIZE_T_FMT"\n\n", rlen);
|
2007-02-23 16:43:07 +00:00
|
|
|
blen = strlen(buf);
|
|
|
|
switch_socket_send(acs->listener->sock, buf, &blen);
|
|
|
|
switch_socket_send(acs->listener->sock, reply, &rlen);
|
2007-02-06 21:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_safe_free(stream.data);
|
|
|
|
switch_safe_free(freply);
|
2006-09-07 03:58:01 +00:00
|
|
|
|
|
|
|
switch_thread_rwlock_unlock(acs->listener->rwlock);
|
|
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (acs && acs->bg) {
|
|
|
|
if (acs->api_cmd) {
|
|
|
|
free(acs->api_cmd);
|
|
|
|
}
|
|
|
|
if (acs->arg) {
|
|
|
|
free(acs->arg);
|
|
|
|
}
|
|
|
|
free(acs);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
2006-07-25 14:14:07 +00:00
|
|
|
static switch_status_t parse_command(listener_t *listener, switch_event_t *event, char *reply, uint32_t reply_len)
|
2006-07-22 21:49:52 +00:00
|
|
|
{
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
|
|
|
char *cmd = switch_event_get_header(event, "command");
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
*reply = '\0';
|
|
|
|
|
|
|
|
if (!strncasecmp(cmd, "exit", 4)) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
snprintf(reply, reply_len, "+OK bye");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_test_flag(listener, LFLAG_AUTHED)) {
|
|
|
|
if (!strncasecmp(cmd, "auth ", 5)) {
|
2006-07-22 22:16:06 +00:00
|
|
|
char *pass;
|
2006-07-22 21:49:52 +00:00
|
|
|
strip_cr(cmd);
|
|
|
|
|
2006-07-22 22:16:06 +00:00
|
|
|
pass = cmd + 5;
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
if (!strcmp(prefs.password, pass)) {
|
|
|
|
switch_set_flag_locked(listener, LFLAG_AUTHED);
|
|
|
|
snprintf(reply, reply_len, "+OK accepted");
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR invalid");
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
2006-09-22 13:52:36 +00:00
|
|
|
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (listener->session) {
|
2007-01-09 00:39:11 +00:00
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(listener->session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (!strncasecmp(cmd, "connect", 7)) {
|
|
|
|
snprintf(reply, reply_len, "+OK");
|
|
|
|
goto done;
|
|
|
|
} else if (!strncasecmp(cmd, "sendmsg", 4)) {
|
|
|
|
if (switch_test_flag(listener, LFLAG_ASYNC)) {
|
|
|
|
if ((status = switch_core_session_queue_private_event(listener->session, &event)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
snprintf(reply, reply_len, "+OK");
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR memory error");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch_ivr_parse_event(listener->session, event);
|
|
|
|
snprintf(reply, reply_len, "+OK");
|
|
|
|
}
|
|
|
|
goto done;
|
2007-01-09 00:39:11 +00:00
|
|
|
} else if (!strncasecmp(cmd, "getvar", 6)) {
|
|
|
|
char *arg;
|
|
|
|
char *val = "";
|
|
|
|
|
|
|
|
strip_cr(cmd);
|
|
|
|
|
|
|
|
if ((arg = strchr(cmd, ' '))) {
|
|
|
|
*arg++ = '\0';
|
|
|
|
if (!(val = switch_channel_get_variable(channel, arg))) {
|
|
|
|
val = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-02-14 03:45:10 +00:00
|
|
|
snprintf(reply, reply_len, "%s", val);
|
2007-01-09 00:39:11 +00:00
|
|
|
goto done;
|
2007-01-06 17:06:18 +00:00
|
|
|
} else if (!strncasecmp(cmd, "myevents", 8)) {
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_CREATE] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_DESTROY] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_STATE] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_ANSWER] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_HANGUP] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_EXECUTE] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_BRIDGE] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_UNBRIDGE] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_PROGRESS] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_OUTGOING] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_PARK] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_CHANNEL_UNPARK] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_TALK] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_DTMF] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_NOTALK] = 1;
|
|
|
|
listener->event_list[SWITCH_EVENT_DETECTED_SPEECH] = 1;
|
|
|
|
switch_set_flag_locked(listener, LFLAG_MYEVENTS);
|
|
|
|
switch_set_flag_locked(listener, LFLAG_EVENTS);
|
|
|
|
if (strstr(cmd, "xml") || strstr(cmd, "XML")) {
|
|
|
|
listener->format = EVENT_FORMAT_XML;
|
|
|
|
}
|
|
|
|
snprintf(reply, reply_len, "+OK Events Enabled");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_test_flag(listener, LFLAG_FULL)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-22 13:52:36 +00:00
|
|
|
if (!strncasecmp(cmd, "sendevent", 9)) {
|
2006-10-23 22:32:50 +00:00
|
|
|
char *ename;
|
|
|
|
strip_cr(cmd);
|
|
|
|
|
|
|
|
ename = cmd + 9;
|
|
|
|
if (*ename == '\r' || *ename == '\n') {
|
|
|
|
ename = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ename) {
|
|
|
|
switch_event_types_t etype;
|
|
|
|
if (switch_name_event(ename, &etype) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
event->event_id = etype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-22 13:52:36 +00:00
|
|
|
switch_event_fire(&event);
|
|
|
|
snprintf(reply, reply_len, "+OK");
|
|
|
|
goto done;
|
|
|
|
} else if (!strncasecmp(cmd, "sendmsg", 7)) {
|
2006-09-07 03:58:01 +00:00
|
|
|
switch_core_session_t *session;
|
|
|
|
char *uuid = cmd + 8;
|
|
|
|
|
|
|
|
if (uuid) {
|
|
|
|
while(*uuid == ' ') {
|
|
|
|
uuid++;
|
|
|
|
}
|
2006-10-19 19:09:51 +00:00
|
|
|
|
|
|
|
if (*uuid == '\r' || *uuid == '\n') {
|
|
|
|
uuid = NULL;
|
|
|
|
} else {
|
|
|
|
strip_cr(uuid);
|
|
|
|
}
|
2006-09-07 03:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!uuid) {
|
|
|
|
uuid = switch_event_get_header(event, "session-id");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((session = switch_core_session_locate(uuid))) {
|
|
|
|
switch_channel_t *channel = switch_core_session_get_channel(session);
|
|
|
|
if (!switch_channel_test_flag(channel, CF_CONTROLLED)) {
|
|
|
|
switch_core_session_rwunlock(session);
|
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session) {
|
|
|
|
if ((status = switch_core_session_queue_private_event(session, &event)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
snprintf(reply, reply_len, "+OK");
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR memory error");
|
|
|
|
}
|
|
|
|
switch_core_session_rwunlock(session);
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR invalid session id [%s]", uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
} else if (!strncasecmp(cmd, "api ", 4)) {
|
|
|
|
struct api_command_struct acs = {0};
|
2006-07-22 21:49:52 +00:00
|
|
|
char *api_cmd = cmd + 4;
|
2006-09-07 03:58:01 +00:00
|
|
|
char *arg = NULL;
|
|
|
|
strip_cr(api_cmd);
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
if ((arg = strchr(api_cmd, ' '))) {
|
|
|
|
*arg++ = '\0';
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
acs.listener = listener;
|
|
|
|
acs.api_cmd = api_cmd;
|
|
|
|
acs.arg = arg;
|
|
|
|
acs.bg = 0;
|
|
|
|
|
|
|
|
api_exec(NULL, (void *) &acs);
|
2006-10-21 03:19:48 +00:00
|
|
|
//snprintf(reply, reply_len, "+OK");
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
} else if (!strncasecmp(cmd, "bgapi ", 6)) {
|
|
|
|
struct api_command_struct *acs;
|
|
|
|
char *api_cmd = cmd + 6;
|
|
|
|
char *arg = NULL;
|
2007-01-21 19:36:55 +00:00
|
|
|
char *uuid_str = NULL;
|
2006-07-22 21:49:52 +00:00
|
|
|
strip_cr(api_cmd);
|
|
|
|
|
|
|
|
if ((arg = strchr(api_cmd, ' '))) {
|
|
|
|
*arg++ = '\0';
|
|
|
|
}
|
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
if ((acs = malloc(sizeof(*acs)))) {
|
|
|
|
switch_thread_t *thread;
|
|
|
|
switch_threadattr_t *thd_attr = NULL;
|
|
|
|
switch_uuid_t uuid;
|
|
|
|
|
|
|
|
memset(acs, 0, sizeof(*acs));
|
|
|
|
acs->listener = listener;
|
|
|
|
if (api_cmd) {
|
|
|
|
acs->api_cmd = strdup(api_cmd);
|
|
|
|
}
|
|
|
|
if (arg) {
|
|
|
|
acs->arg = strdup(arg);
|
|
|
|
}
|
|
|
|
acs->bg = 1;
|
|
|
|
switch_threadattr_create(&thd_attr, listener->pool);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
|
|
|
switch_thread_create(&thread, thd_attr, api_exec, acs, listener->pool);
|
2007-01-21 19:36:55 +00:00
|
|
|
if ((uuid_str = switch_event_get_header(event, "job-uuid"))) {
|
|
|
|
switch_copy_string(acs->uuid_str, uuid_str, sizeof(acs->uuid_str));
|
|
|
|
} else {
|
|
|
|
switch_uuid_get(&uuid);
|
|
|
|
switch_uuid_format(acs->uuid_str, &uuid);
|
|
|
|
}
|
2006-09-07 03:58:01 +00:00
|
|
|
snprintf(reply, reply_len, "+OK Job-UUID: %s", acs->uuid_str);
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR memory error!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-07-22 21:49:52 +00:00
|
|
|
} else if (!strncasecmp(cmd, "log", 3)) {
|
|
|
|
|
|
|
|
char *level_s;
|
2006-10-19 18:52:46 +00:00
|
|
|
|
|
|
|
//pull off the first newline/carriage return
|
2006-07-22 21:49:52 +00:00
|
|
|
strip_cr(cmd);
|
|
|
|
|
2006-10-19 18:52:46 +00:00
|
|
|
//move past the command
|
|
|
|
level_s = cmd + 3;
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2006-10-19 18:52:46 +00:00
|
|
|
//see if we got an argument
|
|
|
|
if(!switch_strlen_zero(level_s)) {
|
|
|
|
//if so move to the argument
|
|
|
|
level_s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//see if we lined up on an argument or not
|
2006-07-22 21:49:52 +00:00
|
|
|
if (switch_strlen_zero(level_s)) {
|
|
|
|
level_s = "debug";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((listener->level = switch_log_str2level(level_s))) {
|
|
|
|
switch_set_flag(listener, LFLAG_LOG);
|
|
|
|
snprintf(reply, reply_len, "+OK log level %s [%d]", level_s, listener->level);
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR invalid log level");
|
|
|
|
}
|
|
|
|
} else if (!strncasecmp(cmd, "nolog", 5)) {
|
2006-08-11 18:59:25 +00:00
|
|
|
void *pop;
|
|
|
|
while (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS);
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
if (switch_test_flag(listener, LFLAG_LOG)) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_LOG);
|
|
|
|
snprintf(reply, reply_len, "+OK no longer logging");
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR not loging");
|
|
|
|
}
|
|
|
|
} else if (!strncasecmp(cmd, "event", 5)) {
|
|
|
|
char *next, *cur;
|
|
|
|
uint32_t count = 0, key_count = 0;
|
|
|
|
uint8_t custom = 0;
|
|
|
|
|
|
|
|
strip_cr(cmd);
|
|
|
|
cur = cmd + 5;
|
|
|
|
|
|
|
|
if (cur && (cur = strchr(cur, ' '))) {
|
|
|
|
for(cur++; cur; count++) {
|
|
|
|
switch_event_types_t type;
|
|
|
|
|
|
|
|
if ((next = strchr(cur, ' '))) {
|
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count) {
|
|
|
|
if (!strcasecmp(cur, "xml")) {
|
|
|
|
listener->format = EVENT_FORMAT_XML;
|
|
|
|
goto end;
|
|
|
|
} else if (!strcasecmp(cur, "plain")) {
|
|
|
|
listener->format = EVENT_FORMAT_PLAIN;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (custom) {
|
|
|
|
switch_core_hash_insert_dup(listener->event_hash, cur, MARKER);
|
|
|
|
} else if (switch_name_event(cur, &type) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
key_count++;
|
2006-09-20 14:57:13 +00:00
|
|
|
if (type == SWITCH_EVENT_ALL) {
|
|
|
|
uint32_t x = 0;
|
|
|
|
for (x = 0; x < SWITCH_EVENT_ALL; x++) {
|
|
|
|
listener->event_list[x] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listener->event_list[type] = 1;
|
2006-07-22 21:49:52 +00:00
|
|
|
if (type == SWITCH_EVENT_CUSTOM) {
|
|
|
|
custom++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key_count) {
|
|
|
|
snprintf(reply, reply_len, "-ERR no keywords supplied");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_test_flag(listener, LFLAG_EVENTS)) {
|
|
|
|
switch_set_flag_locked(listener, LFLAG_EVENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(reply, reply_len, "+OK event listener enabled %s", listener->format == EVENT_FORMAT_XML ? "xml" : "plain");
|
2006-09-20 14:57:13 +00:00
|
|
|
|
|
|
|
} else if (!strncasecmp(cmd, "nixevent", 8)) {
|
|
|
|
char *next, *cur;
|
|
|
|
uint32_t count = 0, key_count = 0;
|
|
|
|
uint8_t custom = 0;
|
|
|
|
|
|
|
|
strip_cr(cmd);
|
|
|
|
cur = cmd + 5;
|
|
|
|
|
|
|
|
if (cur && (cur = strchr(cur, ' '))) {
|
|
|
|
for(cur++; cur; count++) {
|
|
|
|
switch_event_types_t type;
|
|
|
|
|
|
|
|
if ((next = strchr(cur, ' '))) {
|
|
|
|
*next++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (custom) {
|
|
|
|
switch_core_hash_delete(listener->event_hash, cur);
|
|
|
|
} else if (switch_name_event(cur, &type) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
uint32_t x = 0;
|
|
|
|
key_count++;
|
|
|
|
|
|
|
|
if (type == SWITCH_EVENT_ALL) {
|
|
|
|
|
|
|
|
for (x = 0; x <= SWITCH_EVENT_ALL; x++) {
|
|
|
|
listener->event_list[x] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (listener->event_list[SWITCH_EVENT_ALL]) {
|
|
|
|
listener->event_list[SWITCH_EVENT_ALL] = 0;
|
|
|
|
for (x = 0; x < SWITCH_EVENT_ALL; x++) {
|
|
|
|
listener->event_list[x] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listener->event_list[type] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == SWITCH_EVENT_CUSTOM) {
|
|
|
|
custom++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key_count) {
|
|
|
|
snprintf(reply, reply_len, "-ERR no keywords supplied");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!switch_test_flag(listener, LFLAG_EVENTS)) {
|
|
|
|
switch_set_flag_locked(listener, LFLAG_EVENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(reply, reply_len, "+OK events nixed");
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
} else if (!strncasecmp(cmd, "noevents", 8)) {
|
2006-08-11 18:59:25 +00:00
|
|
|
void *pop;
|
|
|
|
while (switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS);
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
if (switch_test_flag(listener, LFLAG_EVENTS)) {
|
|
|
|
uint8_t x = 0;
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_EVENTS);
|
|
|
|
for (x = 0; x <= SWITCH_EVENT_ALL; x++) {
|
|
|
|
listener->event_list[x] = 0;
|
|
|
|
}
|
|
|
|
/* wipe the hash */
|
|
|
|
switch_core_hash_init(&listener->event_hash, listener->pool);
|
|
|
|
snprintf(reply, reply_len, "+OK no longer listening for events");
|
|
|
|
} else {
|
|
|
|
snprintf(reply, reply_len, "-ERR not listening for events");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2006-07-25 14:14:07 +00:00
|
|
|
if (event) {
|
|
|
|
switch_event_destroy(&event);
|
|
|
|
}
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
if (switch_strlen_zero(reply)) {
|
|
|
|
snprintf(reply, reply_len, "-ERR command not found");
|
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
return status;
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
|
|
|
|
{
|
|
|
|
listener_t *listener = (listener_t *) obj;
|
|
|
|
char buf[1024];
|
|
|
|
switch_size_t len;
|
|
|
|
switch_status_t status;
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_event_t *event;
|
2006-07-22 21:49:52 +00:00
|
|
|
char reply[512] = "";
|
2007-01-06 17:06:18 +00:00
|
|
|
switch_core_session_t *session = NULL;
|
|
|
|
switch_channel_t *channel = NULL;
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
assert(listener != NULL);
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if ((session = listener->session)) {
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
switch_core_session_read_lock(session);
|
|
|
|
}
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connection Open\n");
|
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, TRUE);
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_set_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
add_listener(listener);
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (session && switch_test_flag(listener, LFLAG_AUTHED)) {
|
|
|
|
switch_event_t *event = NULL, *call_event;
|
|
|
|
char *event_str;
|
|
|
|
|
|
|
|
|
|
|
|
switch_set_flag_locked(listener, LFLAG_SESSION);
|
|
|
|
status = read_packet(listener, &event, 25);
|
|
|
|
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS || !event) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Socket Error!\n");
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_event_create(&call_event, SWITCH_EVENT_MESSAGE) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_command(listener, event, reply, sizeof(reply)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_caller_profile_event_set_data(switch_channel_get_caller_profile(channel), "Channel", call_event);
|
|
|
|
switch_channel_event_set_data(channel, call_event);
|
|
|
|
switch_event_add_header(call_event, SWITCH_STACK_BOTTOM, "Content-Type", "command/reply");
|
|
|
|
|
|
|
|
switch_event_add_header(call_event, SWITCH_STACK_BOTTOM, "Socket-Mode", switch_test_flag(listener, LFLAG_ASYNC) ? "async" : "static");
|
|
|
|
switch_event_add_header(call_event, SWITCH_STACK_BOTTOM, "Control", switch_test_flag(listener, LFLAG_FULL) ? "full" : "single-channel");
|
|
|
|
|
|
|
|
switch_event_serialize(call_event, &event_str);
|
|
|
|
if (!event_str) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
len = strlen(event_str);
|
|
|
|
switch_socket_send(listener->sock, event_str, &len);
|
|
|
|
|
|
|
|
switch_safe_free(event_str);
|
|
|
|
} else {
|
|
|
|
snprintf(buf, sizeof(buf), "Content-Type: auth/request\n\n");
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
len = strlen(buf);
|
|
|
|
switch_socket_send(listener->sock, buf, &len);
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
while (!switch_test_flag(listener, LFLAG_AUTHED)) {
|
|
|
|
status = read_packet(listener, &event, 25);
|
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!event) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (parse_command(listener, event, reply, sizeof(reply)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!switch_strlen_zero(reply)) {
|
|
|
|
snprintf(buf, sizeof(buf), "Content-Type: command/reply\nReply-Text: %s\n\n", reply);
|
|
|
|
len = strlen(buf);
|
|
|
|
switch_socket_send(listener->sock, buf, &len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
while(switch_test_flag(listener, LFLAG_RUNNING) && listen_list.ready) {
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_event_t *event;
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
len = sizeof(buf);
|
|
|
|
memset(buf, 0, len);
|
2006-07-25 14:14:07 +00:00
|
|
|
status = read_packet(listener, &event, 0);
|
2006-07-22 21:49:52 +00:00
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (status != SWITCH_STATUS_SUCCESS) {
|
2006-07-22 21:49:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-07-25 14:14:07 +00:00
|
|
|
|
|
|
|
if (!event) {
|
2006-07-22 21:49:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (parse_command(listener, event, reply, sizeof(reply)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_RUNNING);
|
|
|
|
break;
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
if (!switch_strlen_zero(reply)) {
|
|
|
|
snprintf(buf, sizeof(buf), "Content-Type: command/reply\nReply-Text: %s\n\n", reply);
|
2006-10-13 14:32:38 +00:00
|
|
|
len = strlen(buf);
|
2006-07-25 14:14:07 +00:00
|
|
|
switch_socket_send(listener->sock, buf, &len);
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
2006-07-25 14:14:07 +00:00
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
2006-07-25 14:14:07 +00:00
|
|
|
done:
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
remove_listener(listener);
|
|
|
|
close_socket(&listener->sock);
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Session complete, waiting for children\n");
|
2006-09-07 03:58:01 +00:00
|
|
|
|
|
|
|
switch_thread_rwlock_wrlock(listener->rwlock);
|
|
|
|
switch_thread_rwlock_unlock(listener->rwlock);
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connection Closed\n");
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
if (session) {
|
|
|
|
switch_channel_clear_flag(switch_core_session_get_channel(session), CF_CONTROLLED);
|
|
|
|
switch_clear_flag_locked(listener, LFLAG_SESSION);
|
|
|
|
switch_core_session_rwunlock(session);
|
|
|
|
} else if (listener->pool) {
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_memory_pool_t *pool = listener->pool;
|
|
|
|
switch_core_destroy_memory_pool(&pool);
|
|
|
|
}
|
|
|
|
|
2007-01-06 17:06:18 +00:00
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-29 20:27:43 +00:00
|
|
|
/* Create a thread for the socket and launch it */
|
2006-07-22 21:49:52 +00:00
|
|
|
static void launch_listener_thread(listener_t *listener)
|
|
|
|
{
|
|
|
|
switch_thread_t *thread;
|
|
|
|
switch_threadattr_t *thd_attr = NULL;
|
|
|
|
|
|
|
|
switch_threadattr_create(&thd_attr, listener->pool);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
|
|
|
switch_thread_create(&thread, thd_attr, listener_run, listener, listener->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config(void)
|
|
|
|
{
|
|
|
|
char *cf = "event_socket.conf";
|
|
|
|
switch_xml_t cfg, xml, settings, param;
|
|
|
|
|
|
|
|
memset(&prefs, 0, sizeof(prefs));
|
|
|
|
|
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
|
|
|
|
} else {
|
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
|
|
|
|
|
|
|
if (!strcmp(var, "listen-ip")) {
|
|
|
|
set_pref_ip(val);
|
|
|
|
} else if (!strcmp(var, "listen-port")) {
|
2006-07-22 22:16:06 +00:00
|
|
|
prefs.port = (uint16_t)atoi(val);
|
2006-07-22 21:49:52 +00:00
|
|
|
} else if (!strcmp(var, "password")) {
|
|
|
|
set_pref_pass(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_xml_free(xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_strlen_zero(prefs.ip)) {
|
|
|
|
set_pref_ip("127.0.0.1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (switch_strlen_zero(prefs.password)) {
|
|
|
|
set_pref_pass("ClueCon");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!prefs.port) {
|
|
|
|
prefs.port = 8021;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_runtime(void)
|
|
|
|
{
|
|
|
|
switch_memory_pool_t *pool = NULL, *listener_pool = NULL;
|
|
|
|
switch_status_t rv;
|
|
|
|
switch_sockaddr_t *sa;
|
|
|
|
switch_socket_t *inbound_socket = NULL;
|
|
|
|
listener_t *listener;
|
|
|
|
|
|
|
|
memset(&listen_list, 0, sizeof(listen_list));
|
|
|
|
config();
|
|
|
|
|
|
|
|
if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_mutex_init(&listen_list.mutex, SWITCH_MUTEX_NESTED, pool);
|
2006-08-29 20:27:43 +00:00
|
|
|
switch_mutex_init(&listen_list.sock_mutex, SWITCH_MUTEX_NESTED, pool);
|
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
|
|
|
|
for(;;) {
|
2007-03-09 20:44:13 +00:00
|
|
|
rv = switch_sockaddr_info_get(&sa, prefs.ip, SWITCH_INET, prefs.port, 0, pool);
|
2006-07-22 21:49:52 +00:00
|
|
|
if (rv) goto fail;
|
2007-03-09 20:44:13 +00:00
|
|
|
rv = switch_socket_create(&listen_list.sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, pool);
|
2006-07-22 21:49:52 +00:00
|
|
|
if (rv) goto sock_fail;
|
2006-09-07 03:58:01 +00:00
|
|
|
rv = switch_socket_opt_set(listen_list.sock, SWITCH_SO_REUSEADDR, 1);
|
|
|
|
if (rv) goto sock_fail;
|
2006-07-22 21:49:52 +00:00
|
|
|
rv = switch_socket_bind(listen_list.sock, sa);
|
|
|
|
if (rv) goto sock_fail;
|
|
|
|
rv = switch_socket_listen(listen_list.sock, 5);
|
|
|
|
if (rv) goto sock_fail;
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", prefs.ip, prefs.port);
|
|
|
|
break;
|
|
|
|
sock_fail:
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error!\n");
|
2006-09-07 03:58:01 +00:00
|
|
|
switch_yield(100000);
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listen_list.ready = 1;
|
|
|
|
|
|
|
|
if (switch_event_bind((char *) modname, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_log_bind_logger(socket_logger, SWITCH_LOG_DEBUG);
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (switch_core_new_memory_pool(&listener_pool) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((rv = switch_socket_accept(&inbound_socket, listen_list.sock, listener_pool))) {
|
2006-11-09 05:39:04 +00:00
|
|
|
if (prefs.done) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting Down\n");
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error\n");
|
|
|
|
}
|
2006-07-22 21:49:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(listener = switch_core_alloc(listener_pool, sizeof(*listener)))) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-09-07 03:58:01 +00:00
|
|
|
switch_thread_rwlock_create(&listener->rwlock, listener_pool);
|
2006-07-22 21:49:52 +00:00
|
|
|
switch_queue_create(&listener->event_queue, SWITCH_CORE_QUEUE_LEN, listener_pool);
|
|
|
|
switch_queue_create(&listener->log_queue, SWITCH_CORE_QUEUE_LEN, listener_pool);
|
|
|
|
|
|
|
|
listener->sock = inbound_socket;
|
|
|
|
listener->pool = listener_pool;
|
2006-08-29 20:27:43 +00:00
|
|
|
listener_pool = NULL;
|
2006-07-22 21:49:52 +00:00
|
|
|
listener->format = EVENT_FORMAT_PLAIN;
|
2006-08-29 20:27:43 +00:00
|
|
|
switch_mutex_init(&listener->flag_mutex, SWITCH_MUTEX_NESTED, listener->pool);
|
|
|
|
switch_core_hash_init(&listener->event_hash, listener->pool);
|
2006-07-22 21:49:52 +00:00
|
|
|
launch_listener_thread(listener);
|
2006-08-29 20:27:43 +00:00
|
|
|
|
2006-07-22 21:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close_socket(&listen_list.sock);
|
|
|
|
|
|
|
|
if (pool) {
|
|
|
|
switch_core_destroy_memory_pool(&pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener_pool) {
|
|
|
|
switch_core_destroy_memory_pool(&listener_pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:30:48 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2007-02-09 02:36:03 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|