Skip to content
Snippets Groups Projects
eagi-sphinx-test.c 5.34 KiB
/*
 * Extended AGI test application
 *
 * This code is released into public domain
 * without any warranty of any kind.
 *
 */

/*! \file
 * Extended AGI test application
 *
 * This code is released into public domain
 * without any warranty of any kind.
 *
 *	\ingroup agi
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "asterisk.h"

#include "asterisk/compat.h"

#define AUDIO_FILENO (STDERR_FILENO + 1)

#define SPHINX_HOST "192.168.1.108"
#define SPHINX_PORT 3460

static int sphinx_sock = -1;

static int connect_sphinx(void)
{
	struct hostent *hp;
	struct sockaddr_in sin;
	int res;
	hp = gethostbyname(SPHINX_HOST);
	if (!hp) {
		fprintf(stderr, "Unable to resolve '%s'\n", SPHINX_HOST);
		return -1;
	}
	sphinx_sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sphinx_sock < 0) {
		fprintf(stderr, "Unable to allocate socket: %s\n", strerror(errno));
		return -1;
	}
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(SPHINX_PORT);
	memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
	if (connect(sphinx_sock, (struct sockaddr *)&sin, sizeof(sin))) {
		fprintf(stderr, "Unable to connect on socket: %s\n", strerror(errno));
		close(sphinx_sock);
		sphinx_sock = -1;
		return -1;
	}
	res = fcntl(sphinx_sock, F_GETFL);
	if ((res < 0) || (fcntl(sphinx_sock, F_SETFL, res | O_NONBLOCK) < 0)) {
		fprintf(stderr, "Unable to set flags on socket: %s\n", strerror(errno));
		close(sphinx_sock);
		sphinx_sock = -1;