freeswitch/libs/esl/ivrd.c
2012-03-27 14:24:58 -05:00

95 lines
2.9 KiB
C

/*
* Copyright (c) 2009, 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 <stdio.h>
#include <stdlib.h>
#include <esl.h>
#include <errno.h>
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr)
{
esl_handle_t handle = {{0}};
const char *path;
if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS || !handle.info_event) {
close(client_sock);
esl_log(ESL_LOG_ERROR, "Socket Error\n");
return;
}
if (!(path = esl_event_get_header(handle.info_event, "variable_ivr_path"))) {
esl_disconnect(&handle);
esl_log(ESL_LOG_ERROR, "Missing ivr_path param!\n");
return;
}
/* hotwire the socket to STDIN/STDOUT */
if (!(dup2(client_sock, STDIN_FILENO)) && !(dup2(client_sock, STDOUT_FILENO))){
esl_disconnect(&handle);
esl_log(ESL_LOG_ERROR, "Socket Error hotwiring socket to STDIN/STDOUT!\n");
return;
}
if(system(path)) {
esl_log(ESL_LOG_ERROR, "System Call Failed! [%s]\n", strerror(errno));
}
esl_disconnect(&handle);
}
int main(int argc, char *argv[])
{
int i;
char *ip = NULL;
int port = 0;
for (i = 1; i + 1 < argc; ) {
if (!strcasecmp(argv[i], "-h")) {
ip = argv[++i];
} else if (!strcasecmp(argv[i], "-p")) {
port = atoi(argv[++i]);
} else {
i++;
}
}
if (!(ip && port)) {
fprintf(stderr, "Usage %s -h <host> -p <port>\n", argv[0]);
return -1;
}
esl_listen(ip, port, mycallback, 100000);
return 0;
}