tpjack.c
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- /*----------------------------------------------------------------------*
- tpjack.c
-
- Version: 1.0 3/99
- 1.1 4/99
- 1.2 4/27/99 - select() support enabled
- 1.3 4/28/99 - echo cancellation added
- 1.4 5/25/99 - added support for new module
- 1.5 6/10/99 - changed license to LGPL
-
- This module is the client to connect to tpjackd daemons.
-
- Copyright (c) 1999 Quicknet Technologies, Inc.
-
- Written by Ed Okerson <eokerson@quicknet.net> and
- Greg Herlein <gherlein@quicknet.net>
-
- * This is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License (LGPL) as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA. It is also available online at
- * http://www.gnu.org/copyleft/lesser.html
-
- ----------------------------------------------------------------------*/
- /*-------------------------------< RCS >--------------------------------*/
- static char RCS_ID[] = "$Id: tpjack.c,v 1.1.1.1 1999/08/05 21:17:32 luan Exp $";
- /*----------------------------< Defines >-------------------------------*/
- /* stdlib */
- #include <stdio.h>
- #include <varargs.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <errno.h>
- #include <syslog.h>
- /* other */
- #include "ixjuser.h"
- #include "udp.h"
- /*----------------------------< Includes >------------------------------*/
- /*---------------------------< Definitions >----------------------------*/
- #define LINELEN 128
- #define BUFSIZE 480
- /*--------------------------< Declarations >----------------------------*/
- /*------------------------< Global Variables >--------------------------*/
- static char szDevice[32];
- /*-------------------------< Local Variables >--------------------------*/
- /*----------------------------------------------------------------------*/
- /*----------------------------------------------------------------------*/
- int
- main(int argc, char *argv[])
- {
- char *host="localhost";
- int nPort=7000;
- switch(argc)
- {
- case 2:
- strncpy(szDevice,argv[1],sizeof(szDevice));
- break;
- case 3:
- strncpy(szDevice,argv[1],sizeof(szDevice));
- host=argv[2];
- break;
- case 4:
- strncpy(szDevice,argv[1],sizeof(szDevice));
- host=argv[2];
- nPort=atoi(argv[3]);
- break;
- default:
- fprintf(stderr,"usage: tpjack dev [host [port]]n");
- fprintf(stderr," - dev is probably /dev/ixj0n");
- exit(1);
- }
- UDPjack(host,nPort);
- exit(0);
- }
- /*----------------------------------------------------------------------*/
- int
- UDPjack(char *host,int nPort)
- {
- char inbuf[BUFSIZE], outbuf[BUFSIZE], serv[16];
- int fd,read_fd, send_fd, cc, ixj, hook,retval,nMax;
- fd_set rfds;
- struct timeval tv;
- printf("Opening Phone Jack (%s)n",szDevice);
- ixj = open(szDevice, O_RDWR);
- if(ixj<0)
- {
- printf("Error opening voice card, exitingn");
- return -1;
- }
- printf("Waiting for hookswitchn");
- hook = ioctl(ixj, IXJCTL_HOOKSTATE);
- while(hook!=1)
- {
- hook = ioctl(ixj, IXJCTL_HOOKSTATE);
- }
- /* connect the signalling port */
- sprintf(serv,"%d",nPort);
- fd=connectTCP(host,serv);
- printf("connecting to %sn", host);
- printf(" - signalling on port %d with tcp socket %dn",nPort,fd);
- /* connect the data ports */
- read_fd=GetRecvSocket(nPort);
- send_fd=GetSendSocket(host,nPort);
- if((read_fd<0)||(send_fd<0)) errexit("failure on udp socketn");
- printf(" - voice on port %d using udp sockets %d and %dn",
- nPort+1,read_fd,send_fd);
- tv.tv_sec = 0;
- tv.tv_usec = 300;
- if(nMax<ixj) nMax=ixj;
- if(nMax<fd) nMax=fd;
- if(nMax<read_fd) nMax=read_fd;
- if(nMax<send_fd) nMax=send_fd;
- nMax++;
-
- /* do the loop */
- if(read_fd != 0)
- {
- ioctl(ixj,IXJCTL_PLAY_CODEC,LINEAR16);
- ioctl(ixj,IXJCTL_REC_CODEC,LINEAR16);
- ioctl(ixj,IXJCTL_REC_START);
- ioctl(ixj,IXJCTL_PLAY_START);
- ioctl(ixj,IXJCTL_AEC_START);
- while(hook=ioctl(ixj,IXJCTL_HOOKSTATE))
- {
- FD_ZERO(&rfds);
- FD_SET(read_fd, &rfds);
- FD_SET(ixj, &rfds);
- tv.tv_sec = 0;
- tv.tv_usec = 300;
- retval = select(nMax,&rfds,NULL, NULL,&tv);
- if(FD_ISSET(ixj,&rfds))
- {
- cc = read(ixj, outbuf, 480);
- // printf("ixj --> %dn",cc);
- if(cc > 0)
- {
- write(send_fd,outbuf,cc);
- // printf("udp <-- %dn",cc);
- }
- }
- if(FD_ISSET(read_fd,&rfds))
- {
- cc = ReadUDP(read_fd,inbuf,sizeof inbuf);
- // printf("udp --> %dn",cc);
- if(cc > 0)
- {
- write(ixj, inbuf, cc);
- // printf("ixj <-- %dn",cc);
- }
- if(cc < 0)
- {
- if(errno!=EAGAIN)
- {
- ioctl(ixj, IXJCTL_AEC_STOP);
- ioctl(ixj, IXJCTL_REC_STOP);
- ioctl(ixj, IXJCTL_PLAY_STOP);
- errexit("echo read: %sn",sys_errlist[errno]);
- }
- }
- } /* end if(retval) */
- } /* end while() */
- }
- close(read_fd);
- close(send_fd);
- close(fd);
- close(ixj);
- return 0;
- }
- /*----------------------------------------------------------------------*/
- int
- connectTCP(char *host,char *service)
- {
- return connectsock(host,service,"tcp");
- }
- /*----------------------------------------------------------------------*/
- int
- connectsock(char *host, char *service, char *protocol)
- {
- struct hostent *phe;
- struct servent *pse;
- struct protoent *ppe;
- struct sockaddr_in sin;
- int s,type;
- bzero((char *)&sin, sizeof(sin));
- sin.sin_family=AF_INET;
- if (pse=getservbyname(service,protocol))
- sin.sin_port=pse->s_port;
- else if ((sin.sin_port=htons((u_short)atoi(service)))==0)
- errexit("can't get "%s" service entryn",service);
- if (phe=gethostbyname(host))
- bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
- else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
- errexit("can't get "%s" host entryn",host);
- if ((ppe=getprotobyname(protocol))==0)
- errexit("can't get "%s" protocol entryn",protocol);
- if (strcmp(protocol,"udp")==0)
- type=SOCK_DGRAM;
- else
- type=SOCK_STREAM;
- s=socket(PF_INET,type,ppe->p_proto);
- if (s<0)
- errexit("can't create socket: %sn",sys_errlist[errno]);
- if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0)
- errexit("can't connect to %s.%s: %sn",host,service,sys_errlist[errno]);
- return s;
- }
- /*----------------------------------------------------------------------*/
- int
- errexit(format, va_alist)
- char *format;
- va_dcl
- {
- va_list args;
- char szMsg[512];
- va_start(args);
- vsprintf(szMsg,format,args);
- va_end(args);
- openlog("tpjack",LOG_CONS,LOG_DAEMON);
- syslog(LOG_ERR,szMsg);
- closelog();
- exit(1);
- }
- /*-------------------------------< End >--------------------------------*/