📄 initloop.c
字号:
/////////////////////////////////////////////////////////////////////// initloop.c//// Initialization modules for loopback application running on Linux, Unix//// Copyright Netergy Microelectronics 2000, 2001//////////////////////////////////////////////////////////////////////#include <syslog.h>#include <sys/types.h> /* for socket() */#include <sys/socket.h> /* for socket() */#include <unistd.h> /* for fcntl() */#include <fcntl.h> /* for fcntl() */#include <netinet/in.h>#include <arpa/inet.h>#include <sys/ioctl.h>#include <netdb.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <sys/utsname.h>#ifdef _SUNOS#include <sys/sockio.h>#endif //_SUNOS#include <net/if.h>#include <net/if_arp.h>#include "protocol.h"#include "common.h"#include "endpoint.h"#include "h323message.h"#include "dllist.h"static LONG_InitializeCodec(PROTOCOL_CAP *pCap, char *pchCodecType, OCTET oPayload);////////////////////////////////////////////////////////////////////// _InitializeNetwork//// Initialize IP Address, MAC Address, etc.////////////////////////////////////////////////////////////////////void _InitializeNetwork(){ struct sockaddr_in* psin; int sockfd,len,flags,myflags; int lastlen=0; char *ptr, *buf; struct ifconf ifc; struct ifreq *ifr, ifrcopy; BOOL b_ipDone=FALSE; sockfd = socket(AF_INET, SOCK_DGRAM, 0); // initial allocation for structure len = 100 * sizeof(struct ifreq); // get reliable result from SIOCGIFCONF operation while(1) { buf=(char *)calloc(1,len); ifc.ifc_len=len; ifc.ifc_buf=buf; if (ioctl(sockfd,SIOCGIFCONF, &ifc) <0) { if (lastlen !=0) printf("ioctl error\n"); } else { if (ifc.ifc_len == lastlen) break; // success lastlen=ifc.ifc_len; } // increase allocation len += 10 * sizeof(struct ifreq); free(buf); } // find interface to use which is up and running for (ptr=buf; ptr<buf+ifc.ifc_len && b_ipDone!=TRUE; ) { ifr=(struct ifreq *)ptr; switch (ifr->ifr_addr.sa_family) {#ifndef _SUNOS case AF_INET6: len=sizeof(struct sockaddr_in6); break;#endif //!_SUNOS case AF_INET: default: len=sizeof(struct sockaddr); break; } ptr += sizeof(ifr->ifr_name) +len; myflags=0; ifrcopy=*ifr; ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy); flags=ifrcopy.ifr_flags; if (((flags & IFF_UP) != 0) && ((flags & IFF_LOOPBACK)==0)){ // interface is up psin=(struct sockaddr_in *) &ifr->ifr_addr; g_dwLocalIpAddr=ntohl(psin->sin_addr.s_addr); b_ipDone=TRUE; } } free(buf); close(sockfd); sprintf(g_szLocalIpAddr, "%d.%d.%d.%d", ((OCTET)(g_dwLocalIpAddr>>24)&0xff), ((OCTET)(g_dwLocalIpAddr>>16)&0xff), ((OCTET)(g_dwLocalIpAddr>>8)&0xff), ((OCTET)(g_dwLocalIpAddr)&0xff)); APPLOG(LOG_INFO, "IP address is %s\n", g_szLocalIpAddr);}////////////////////////////////////////////////////////////////////// GetParam//////////////////////////////////////////////////////////////////////char* GetParam(char *pchParam, char *szParamName){ char *pchEnd; if ((pchParam = strstr(pchParam, szParamName))) { pchParam += strlen(szParamName); do { pchParam++; } while (*pchParam == '\t' || *pchParam == ' '); pchEnd = pchParam; while (*pchEnd != '\n' && *pchEnd != '\0') pchEnd++; *pchEnd = '\0'; } return pchParam;}////////////////////////////////////////////////////////////////////// _InitializeParam//// Initialize system (config) parameters//// i.e. Dial Plan, Domain Name, Server IP, Server Port////////////////////////////////////////////////////////////////////BOOL _InitializeParam(long lRv){ int it; PROTOCOL_CAP xCap; char sScanf[50]; OCTET *poParam; FILE *fpParam=NULL; // get number of endpoints g_oInstMax = 2; // number of endpoints // allocate endpoint data structure xEndPoint=(END_POINT*)calloc(g_oInstMax,sizeof(END_POINT)); if (xEndPoint==NULL) { ASSERT(0); while(1); } // initialize endpoints for (it=0; it < END_POINT_NUM; it++) { EndPointInit(&xEndPoint[it], it+1); } // set default g_dwGatekeeperIP=ntohl(inet_addr("207.82.37.252")); strcpy(g_szDialPlan, DEFAULT_DIALPLAN); g_bSendInbandDTMF=TRUE; g_bSendOutofbandDTMF=TRUE; g_eKeypadType=KEYPADTYPE_H225; if (g_pchCfgFile) { fpParam=fopen(g_pchCfgFile, "r"); ASSERT(fpParam); while (fgets(sScanf, sizeof(sScanf), fpParam)) { if ((poParam = GetParam(sScanf, "GATEKEEPERIP"))) { g_dwGatekeeperIP=ntohl(inet_addr(poParam)); } else if ((poParam = GetParam(sScanf, "VLANTAG_RTP"))) { RTPProtocolSetVLanTag(strtoul(poParam,NULL,0)); } else if ((poParam = GetParam(sScanf, "PRIORITYTAG_RTP"))) { RTPProtocolSetPriorityTag(strtoul(poParam,NULL,0)); } else if ((poParam = GetParam(sScanf, "VLANTAG_CALL"))) { H323ProtocolSetVLanTag(strtoul(poParam,NULL,0)); } else if ((poParam = GetParam(sScanf, "PRIORITYTAG_CALL"))) { H323ProtocolSetPriorityTag(strtoul(poParam,NULL,0)); } else if ((poParam = GetParam(sScanf, "DIALPLAN"))) { strcpy(g_szDialPlan, poParam); } else if ((poParam = GetParam(sScanf, "INBANDDTMF"))) { if (strcmp(poParam,"DISABLED")==0) { g_bSendInbandDTMF=FALSE; } } else if ((poParam = GetParam(sScanf, "OUTOFBANDDTMF"))) { if (strcmp(poParam,"DISABLED")==0) { g_bSendOutofbandDTMF=FALSE; } } else if ((poParam = GetParam(sScanf, "KEYPADTYPE"))) { g_eKeypadType=strtoul(poParam,NULL,0); } } fclose(fpParam); } sprintf(g_szGatekeeperIP, HANDSET_MSG_GKADDRESS, ((OCTET)(g_dwGatekeeperIP>>24)&0xff), ((OCTET)(g_dwGatekeeperIP>>16)&0xff), ((OCTET)(g_dwGatekeeperIP>>8)&0xff), ((OCTET)(g_dwGatekeeperIP)&0xff)); printf("******************************************************\n"); printf("* Gatekeeper IP is %s\n",g_szGatekeeperIP); printf("* Dial Plan is %s\n",g_szDialPlan); printf("* In band DTMF is %s\n",g_bSendInbandDTMF?"enabled":"disabled"); printf("* Out of band DTMF is %s\n",g_bSendOutofbandDTMF?"enabled":"disabled"); printf("* OOB DTMF type : %d\n",g_eKeypadType); printf("******************************************************\n"); for (it=0;it<END_POINT_NUM;it++) { _InitLineParam(&xEndPoint[it]); } printf(" %d endpoint enabled\n",END_POINT_NUM); /* initialize cap dllist */ pdllCap = new_DLLIST(); /* Initialize the MediaStream module */ MediaStreamInit(g_dwLocalIpAddr, g_oMacAddress); g_bMediaStream=TRUE; /* Initialize CODEC information */ memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_AUDIO; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; if (0 == _InitializeCodec(&xCap, "G723", PROTOCOL_RTPPAYLOAD_G723)) { MediaStreamAddCap(&xCap); } memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_AUDIO; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; if (0 == _InitializeCodec(&xCap, "G726", PROTOCOL_RTPPAYLOAD_G726)) { MediaStreamAddCap(&xCap); } memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_AUDIO; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; if (0 == _InitializeCodec(&xCap, "G729", PROTOCOL_RTPPAYLOAD_G729)) { MediaStreamAddCap(&xCap); } memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_AUDIO; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; if (0 != _InitializeCodec(&xCap, "G711U", PROTOCOL_RTPPAYLOAD_PCMU)) { xCap.xCmn.oPayload = PROTOCOL_RTPPAYLOAD_PCMU; xCap.u.xAudio.oPacketization = 20; xCap.u.xAudio.bSilenceSuppression = TRUE; } MediaStreamAddCap(&xCap); memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_AUDIO; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; if (0 != _InitializeCodec(&xCap, "G711A", PROTOCOL_RTPPAYLOAD_PCMA)) { xCap.xCmn.oPayload = PROTOCOL_RTPPAYLOAD_PCMA; xCap.u.xAudio.oPacketization = 20; xCap.u.xAudio.bSilenceSuppression = TRUE; } MediaStreamAddCap(&xCap);#ifdef _H323V4_#ifdef _USE_T38_ memset(&xCap, 0, sizeof(PROTOCOL_CAP)); xCap.eMedia = PROTOCOL_MEDIA_DATA; xCap.xCmn.eTxRx = PROTOCOL_TXRX_TXRX; xCap.xCmn.oPayload = PROTOCOL_PAYLOAD_T38; xCap.u.xData.wMaxBitRate = 14400; xCap.u.xData.wMaxBuffer = 400; xCap.u.xData.oTransport = PROTOCOL_TRANSPORT_UDPTL; if (g_pchCfgFile) { fpParam=fopen(g_pchCfgFile, "r"); ASSERT(fpParam); while (fgets(sScanf, sizeof(sScanf), fpParam)) { if ((poParam=GetParam(sScanf,"T38_ECC_TYPE"))) { if(strcmp(poParam, "REDUNCY") == 0) { xCap.u.xData.oUdpEC = T38_UDPEC_REDUNDANCY; } else if (strcmp(poParam, "FEC") == 0) { xCap.u.xData.oUdpEC = T38_UDPEC_FEC; } else { xCap.u.xData.oUdpEC = T38_UDPEC_REDUNDANCY; // default } } if ((poParam=GetParam(sScanf,"T38_RATE_MANAGEMENT"))) { if(strcmp(poParam, "TRANSFERRED") == 0) { xCap.u.xData.oRateManagement = T38_TCF_TRANSFERRED; } else if (strcmp(poParam, "LOCAL") == 0) { xCap.u.xData.oRateManagement = T38_TCF_LOCAL; } else { xCap.u.xData.oRateManagement = T38_TCF_UNKNOWN; } } } fclose(fpParam); } MediaStreamAddCap(&xCap);#endif /* _USE_T38_ */#endif /* _H323V4_ */ return TRUE;}////////////////////////////////////////////////////////////////////// _InitLineParam//// Initialize line (endpoint) parameters; //// i.e. Phone #, Caller ID, Local Port, Transport Type, etc. ////////////////////////////////////////////////////////////////////void _InitLineParam(END_POINT *pEndPoint){ char szCfgAlias[30]; char szCfgNumber[30]; char szCfgCidName[30]; char szCfgPort[30]; char szCfgCtl[30]; char szCfgPark[30]; char szCfgAlertPark[30]; char sScanf[50]; OCTET *poParam; FILE *fpParam=NULL; pEndPoint->dwGatekeeper = g_dwGatekeeperIP; // set default sprintf(pEndPoint->szAlias, "T2phone-%lx-%d",g_dwLocalIpAddr,pEndPoint->wChannel); sprintf(pEndPoint->szPhoneNumber, "555%03d%d",(unsigned int)g_dwLocalIpAddr&0xff, pEndPoint->wChannel); sprintf(pEndPoint->szCallerIDName,"H323LoopEP%d",pEndPoint->wChannel); // pEndPoint->wCallSigPort=4002+pEndPoint->wChannel-1; pEndPoint->bCallParkAlerting=FALSE; if (g_pchCfgFile) { fpParam=fopen(g_pchCfgFile, "r"); ASSERT(fpParam); // get parameters sprintf(szCfgAlias, "H323ALIAS%d", pEndPoint->wChannel); sprintf(szCfgNumber, "LINE%dNUMBER", pEndPoint->wChannel); sprintf(szCfgCidName, "CALLERIDNAME%d", pEndPoint->wChannel); sprintf(szCfgPort, "CALLSIGPORT%d", pEndPoint->wChannel); sprintf(szCfgCtl, "LINE%dCTLPORT", pEndPoint->wChannel); sprintf(szCfgPark, "PARKTONUMBER%d", pEndPoint->wChannel); sprintf(szCfgAlertPark, "ALERTINGPARK%d", pEndPoint->wChannel); while (fgets(sScanf, sizeof(sScanf), fpParam)) { if ((poParam = GetParam(sScanf, szCfgAlias))) { strcpy(pEndPoint->szAlias, poParam); } else if ((poParam = GetParam(sScanf, szCfgNumber))) { strcpy(pEndPoint->szPhoneNumber, poParam); } else if ((poParam = GetParam(sScanf, szCfgCidName))) { strcpy(pEndPoint->szCallerIDName, poParam); } else if ((poParam = GetParam(sScanf, szCfgPort))) { pEndPoint->wCallSigPort = atoi(poParam);#ifdef HAMMER_TESTING } else if ((poParam = GetParam(sScanf, szCfgCtl))) { pEndPoint->wCtlPort = atoi(poParam);#endif } else if ((poParam = GetParam(sScanf, szCfgPark))) { strcpy(pEndPoint->szParkedToNumber,poParam); } else if ((poParam = GetParam(sScanf, szCfgAlertPark))) { if (strcmp(poParam,"ENABLED")==0) { pEndPoint->bCallParkAlerting=TRUE; } } } fclose(fpParam); } // Initialize CFNR wait time pEndPoint->dwCFNRDelay=1900; printf("******************************************************\n"); printf("* H323 alias is %s\n",pEndPoint->szAlias); printf("* Phone number is [[%s]]\n",pEndPoint->szPhoneNumber); printf("* Caller id name is %s\n",pEndPoint->szCallerIDName); printf("* Call sig port is %d\n",pEndPoint->wCallSigPort); printf("* Call park destination is %s\n",pEndPoint->szParkedToNumber); printf("* Automatic Call Park for incoming is %s\n",pEndPoint->bCallParkAlerting?"enabled":"disabled"); printf("******************************************************\n");}///////////////////////////////////////////////////////////////////// _InitializeCodec////////////////////////////////////////////////////////////////////* return 0 if the codec is set */static LONG_InitializeCodec(PROTOCOL_CAP *pCap, char *pchCodecType, OCTET oPayload){ char szCodecON[10]; char szCodecSS[10]; char szCodecEC[10]; char szCodecPACK[10]; char sScanf[50]; OCTET *poParam; LONG lRv = -1; FILE *fpParam=NULL; sprintf(szCodecON, "%sON", pchCodecType); sprintf(szCodecSS, "%sSS", pchCodecType); sprintf(szCodecEC, "%sEC", pchCodecType); sprintf(szCodecPACK, "%sPACK", pchCodecType); // set defaults pCap->u.xAudio.oPacketization = 30; if (g_pchCfgFile) { fpParam=fopen(g_pchCfgFile, "r"); ASSERT(fpParam); while (fgets(sScanf, sizeof(sScanf), fpParam)) { if ((poParam = GetParam(sScanf, szCodecON))) { if (strcmp(poParam,"YES")) { lRv = 0; SYSLOG(LOG_INFO, "%s, ", szCodecON); pCap->xCmn.oPayload = oPayload; } } else if ((poParam = GetParam(sScanf, szCodecSS))) { if (strcmp(poParam,"YES")) { SYSLOG(LOG_INFO, "%s, ", szCodecSS); pCap->u.xAudio.bSilenceSuppression=TRUE; } } else if ((poParam = GetParam(sScanf, szCodecPACK))) { if (oPayload == PROTOCOL_RTPPAYLOAD_G723) { pCap->u.xAudio.oPacketization = 30; SYSLOG(LOG_INFO, "Forcing G723 packetization to %d\n " , pCap->u.xAudio.oPacketization); } else if (strlen(poParam)) { pCap->u.xAudio.oPacketization = (OCTET)strtol(poParam, NULL, 0); } } } if (pCap->u.xAudio.oPacketization == 0) pCap->u.xAudio.oPacketization = 30; SYSLOG(LOG_INFO, "%s packetization = %d\n", szCodecPACK, pCap->u.xAudio.oPacketization); fclose(fpParam); } return lRv;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -