示例#1
0
int ooGetLocalIPAddress(char * pIPAddrs)
{
   int ret;
   struct hostent *hp;
   struct ast_hostent phost;
   char hostname[100];

   if(pIPAddrs == NULL)
      return -1; /* Need to find suitable return value */
   ret = gethostname(hostname, 100);
   if(ret == 0)
   {
      if ((hp = ast_gethostbyname(hostname, &phost))) {
			if (hp->h_addrtype == AF_INET6) {
				struct in6_addr i;
				memcpy(&i, hp->h_addr, sizeof(i));
				strcpy(pIPAddrs, (inet_ntop(AF_INET6, &i, 
				hostname, sizeof(hostname))) == NULL ? "::1" : 
				inet_ntop(AF_INET6, &i, hostname, sizeof(hostname)));
			} else {
	  			struct in_addr i;
				memcpy(&i, hp->h_addr, sizeof(i));
			  	strcpy(pIPAddrs, (ast_inet_ntoa(i) == NULL) ? "127.0.0.1" : ast_inet_ntoa(i));
			}
      } else {
         return -1;
      }
   }
   else{
      return -1;
   }
   return ASN_OK;
}
示例#2
0
static int __ast_http_load(int reload)
{
	struct ast_config *cfg;
	struct ast_variable *v;
	int enabled=0;
	int newenablestatic=0;
	struct sockaddr_in sin;
	struct hostent *hp;
	struct ast_hostent ahp;
	char newprefix[MAX_PREFIX];

	memset(&sin, 0, sizeof(sin));
	sin.sin_port = 8088;
	strcpy(newprefix, DEFAULT_PREFIX);
	cfg = ast_config_load("http.conf");
	if (cfg) {
		v = ast_variable_browse(cfg, "general");
		while(v) {
			if (!strcasecmp(v->name, "enabled"))
				enabled = ast_true(v->value);
			else if (!strcasecmp(v->name, "enablestatic"))
				newenablestatic = ast_true(v->value);
			else if (!strcasecmp(v->name, "bindport"))
				sin.sin_port = ntohs(atoi(v->value));
			else if (!strcasecmp(v->name, "bindaddr")) {
				if ((hp = ast_gethostbyname(v->value, &ahp))) {
					memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
				} else {
					ast_log(LOG_WARNING, "Invalid bind address '%s'\n", v->value);
				}
			} else if (!strcasecmp(v->name, "prefix")) {
				if (!ast_strlen_zero(v->value)) {
					newprefix[0] = '/';
					ast_copy_string(newprefix + 1, v->value, sizeof(newprefix) - 1);
				} else {
					newprefix[0] = '\0';
				}
					
			}
			v = v->next;
		}
		ast_config_destroy(cfg);
	}
	if (enabled)
		sin.sin_family = AF_INET;
	if (strcmp(prefix, newprefix)) {
		ast_copy_string(prefix, newprefix, sizeof(prefix));
		prefix_len = strlen(prefix);
	}
	enablestatic = newenablestatic;
	http_server_start(&sin);
	return 0;
}
static int festival_exec(struct ast_channel *chan, void *vdata)
{
    int usecache;
    int res=0;
    struct localuser *u;
    struct sockaddr_in serv_addr;
    struct hostent *serverhost;
    struct ast_hostent ahp;
    int fd;
    FILE *fs;
    char *host;
    char *cachedir;
    char *temp;
    char *festivalcommand;
    int port=1314;
    int n;
    char ack[4];
    char *waveform;
    int filesize;
    int wave;
    char bigstring[MAXFESTLEN];
    int i;
    struct MD5Context md5ctx;
    unsigned char MD5Res[16];
    char MD5Hex[33] = "";
    char koko[4] = "";
    char cachefile[MAXFESTLEN]="";
    int readcache=0;
    int writecache=0;
    int strln;
    int fdesc = -1;
    char buffer[16384];
    int seekpos = 0;
    char *data;
    char *intstr;
    struct ast_config *cfg;
    char *newfestivalcommand;

    if (ast_strlen_zero(vdata)) {
        ast_log(LOG_WARNING, "festival requires an argument (text)\n");
        return -1;
    }

    LOCAL_USER_ADD(u);

    cfg = ast_config_load(FESTIVAL_CONFIG);
    if (!cfg) {
        ast_log(LOG_WARNING, "No such configuration file %s\n", FESTIVAL_CONFIG);
        LOCAL_USER_REMOVE(u);
        return -1;
    }
    if (!(host = ast_variable_retrieve(cfg, "general", "host"))) {
        host = "localhost";
    }
    if (!(temp = ast_variable_retrieve(cfg, "general", "port"))) {
        port = 1314;
    } else {
        port = atoi(temp);
    }
    if (!(temp = ast_variable_retrieve(cfg, "general", "usecache"))) {
        usecache=0;
    } else {
        usecache = ast_true(temp);
    }
    if (!(cachedir = ast_variable_retrieve(cfg, "general", "cachedir"))) {
        cachedir = "/tmp/";
    }
    if (!(festivalcommand = ast_variable_retrieve(cfg, "general", "festivalcommand"))) {
        festivalcommand = "(tts_textasterisk \"%s\" 'file)(quit)\n";
    } else { /* This else parses the festivalcommand that we're sent from the config file for \n's, etc */
        int i, j;
        newfestivalcommand = alloca(strlen(festivalcommand) + 1);

        for (i = 0, j = 0; i < strlen(festivalcommand); i++) {
            if (festivalcommand[i] == '\\' && festivalcommand[i + 1] == 'n') {
                newfestivalcommand[j++] = '\n';
                i++;
            } else if (festivalcommand[i] == '\\') {
                newfestivalcommand[j++] = festivalcommand[i + 1];
                i++;
            } else
                newfestivalcommand[j++] = festivalcommand[i];
        }
        newfestivalcommand[j] = '\0';
        festivalcommand = newfestivalcommand;
    }

    data = ast_strdupa(vdata);

    intstr = strchr(data, '|');
    if (intstr) {
        *intstr = '\0';
        intstr++;
        if (!strcasecmp(intstr, "any"))
            intstr = AST_DIGIT_ANY;
    }

    ast_log(LOG_DEBUG, "Text passed to festival server : %s\n",(char *)data);
    /* Connect to local festival server */

    fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (fd < 0) {
        ast_log(LOG_WARNING,"festival_client: can't get socket\n");
        ast_config_destroy(cfg);
        LOCAL_USER_REMOVE(u);
        return -1;
    }
    memset(&serv_addr, 0, sizeof(serv_addr));
    if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
        /* its a name rather than an ipnum */
        serverhost = ast_gethostbyname(host, &ahp);
        if (serverhost == (struct hostent *)0) {
            ast_log(LOG_WARNING,"festival_client: gethostbyname failed\n");
            ast_config_destroy(cfg);
            LOCAL_USER_REMOVE(u);
            return -1;
        }
        memmove(&serv_addr.sin_addr,serverhost->h_addr, serverhost->h_length);
    }
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);

    if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
        ast_log(LOG_WARNING,"festival_client: connect to server failed\n");
        ast_config_destroy(cfg);
        LOCAL_USER_REMOVE(u);
        return -1;
    }

    /* Compute MD5 sum of string */
    MD5Init(&md5ctx);
    MD5Update(&md5ctx,(unsigned char const *)data,strlen(data));
    MD5Final(MD5Res,&md5ctx);
    MD5Hex[0] = '\0';

    /* Convert to HEX and look if there is any matching file in the cache
    	directory */
    for (i=0; i<16; i++) {
        snprintf(koko, sizeof(koko), "%X",MD5Res[i]);
        strncat(MD5Hex, koko, sizeof(MD5Hex) - strlen(MD5Hex) - 1);
    }
    readcache=0;
    writecache=0;
    if (strlen(cachedir)+strlen(MD5Hex)+1<=MAXFESTLEN && (usecache==-1)) {
        snprintf(cachefile, sizeof(cachefile), "%s/%s", cachedir, MD5Hex);
        fdesc=open(cachefile,O_RDWR);
        if (fdesc==-1) {
            fdesc=open(cachefile,O_CREAT|O_RDWR,0777);
            if (fdesc!=-1) {
                writecache=1;
                strln=strlen((char *)data);
                ast_log(LOG_DEBUG,"line length : %d\n",strln);
                write(fdesc,&strln,sizeof(int));
                write(fdesc,data,strln);
                seekpos=lseek(fdesc,0,SEEK_CUR);
                ast_log(LOG_DEBUG,"Seek position : %d\n",seekpos);
            }
        } else {
            read(fdesc,&strln,sizeof(int));
            ast_log(LOG_DEBUG,"Cache file exists, strln=%d, strlen=%d\n",strln,(int)strlen((char *)data));
            if (strlen((char *)data)==strln) {
                ast_log(LOG_DEBUG,"Size OK\n");
                read(fdesc,&bigstring,strln);
                bigstring[strln] = 0;
                if (strcmp(bigstring,data)==0) {
                    readcache=1;
                } else {
                    ast_log(LOG_WARNING,"Strings do not match\n");
                }
            } else {
                ast_log(LOG_WARNING,"Size mismatch\n");
            }
        }
    }

    if (readcache==1) {
        close(fd);
        fd=fdesc;
        ast_log(LOG_DEBUG,"Reading from cache...\n");
    } else {
        ast_log(LOG_DEBUG,"Passing text to festival...\n");
        fs=fdopen(dup(fd),"wb");
        fprintf(fs,festivalcommand,(char *)data);
        fflush(fs);
        fclose(fs);
    }

    /* Write to cache and then pass it down */
    if (writecache==1) {
        ast_log(LOG_DEBUG,"Writing result to cache...\n");
        while ((strln=read(fd,buffer,16384))!=0) {
            write(fdesc,buffer,strln);
        }
        close(fd);
        close(fdesc);
        fd=open(cachefile,O_RDWR);
        lseek(fd,seekpos,SEEK_SET);
    }

    ast_log(LOG_DEBUG,"Passing data to channel...\n");

    /* Read back info from server */
    /* This assumes only one waveform will come back, also LP is unlikely */
    wave = 0;
    do {
        int read_data;
        for (n=0; n < 3; )
        {
            read_data = read(fd,ack+n,3-n);
            /* this avoids falling in infinite loop
             * in case that festival server goes down
             * */
            if ( read_data == -1 )
            {
                ast_log(LOG_WARNING,"Unable to read from cache/festival fd");
                close(fd);
                ast_config_destroy(cfg);
                LOCAL_USER_REMOVE(u);
                return -1;
            }
            n += read_data;
        }
        ack[3] = '\0';
        if (strcmp(ack,"WV\n") == 0) {         /* receive a waveform */
            ast_log(LOG_DEBUG,"Festival WV command\n");
            waveform = socket_receive_file_to_buff(fd,&filesize);
            res = send_waveform_to_channel(chan,waveform,filesize, intstr);
            free(waveform);
            break;
        }
        else if (strcmp(ack,"LP\n") == 0) {   /* receive an s-expr */
            ast_log(LOG_DEBUG,"Festival LP command\n");
            waveform = socket_receive_file_to_buff(fd,&filesize);
            waveform[filesize]='\0';
            ast_log(LOG_WARNING,"Festival returned LP : %s\n",waveform);
            free(waveform);
        } else if (strcmp(ack,"ER\n") == 0) {    /* server got an error */
            ast_log(LOG_WARNING,"Festival returned ER\n");
            res=-1;
            break;
        }
    } while (strcmp(ack,"OK\n") != 0);
    close(fd);
    ast_config_destroy(cfg);
    LOCAL_USER_REMOVE(u);
    return res;

}