oogkclient.c

来自「一个非常美妙的proxy。功能强大。基于sip的协议。如果还要的话」· C语言 代码 · 共 1,899 行 · 第 1/5 页

C
1,899
字号
/* * Copyright (C) 2005 by Page Iberica, S.A. * Copyright (C) 2005 by Objective Systems, Inc. * * This software is furnished under an open source license and may be  * used and copied only in accordance with the terms of this license.  * The text of the license may generally be found in the root  * directory of this installation in the COPYING file.  It  * can also be viewed online at the following URL: * *   http://www.obj-sys.com/open/license.html * * Any redistributions of this file including modified versions must  * maintain this copyright notice. * *****************************************************************************//** * @file ooGkClient.c  * This file contains functions to support RAS protocol.  * */#include "ooGkClient.h"#include "ootypes.h"#include "ootrace.h"#include "ooports.h"#include "ooasn1.h"#include "oochannels.h"#include "printHandler.h"#include "ooCalls.h"#include "H323-MESSAGES.h"#include "ooDateTime.h"#include "ooq931.h"#include "ooh323.h"#include "ooh323ep.h"#include "ooTimer.h"#include "ooSocket.h"#include "ooUtils.h"/** Global endpoint structure */extern OOH323EndPoint gH323ep;static ASN1OBJID gProtocolID = {   6, { 0, 0, 8, 2250, 0, 4 }};int ooGkClientInit(enum RasGatekeeperMode eGkMode,              char *szGkAddr, int iGkPort ){   ooGkClient *pGkClient=NULL;   OOInterface *cur=NULL;   pGkClient = (ooGkClient*)                         memAlloc(&gH323ep.ctxt, sizeof(ooGkClient));   if(!pGkClient)   {      OOTRACEERR1("Error: Failed to allocate memory to Gatekeeper Client.\n");      return OO_FAILED;   }    memset(pGkClient, 0, sizeof(ooGkClient));   gH323ep.gkClient = pGkClient;   initContext(&(pGkClient->ctxt));   initContext(&(pGkClient->msgCtxt));   pGkClient->rrqRetries = 0;   pGkClient->grqRetries = 0;   strcpy(pGkClient->localRASIP, gH323ep.signallingIP);#ifndef _WIN32   if(!strcmp(pGkClient->localRASIP, "0.0.0.0") ||      !strcmp(pGkClient->localRASIP, "127.0.0.1"))   {      if(!gH323ep.ifList)      {         if(ooSocketGetInterfaceList(&gH323ep.ctxt, &gH323ep.ifList)!= ASN_OK)         {            OOTRACEERR1("Error:Failed to retrieve interface addresses\n");            return OO_FAILED;         }      }      for(cur = gH323ep.ifList; cur; cur = cur->next)      {         if(!strcmp(cur->name, "lo") || !strcmp(cur->addr, "127.0.0.1"))            continue;         break;      }      if(cur)      {         OOTRACEINFO2("Using local RAS Ip address %s\n", cur->addr);         strcpy(pGkClient->localRASIP, cur->addr);      }else{         OOTRACEERR1("Error:Failed to assign a local RAS IP address\n");         return OO_FAILED;      }   }#endif      if(OO_OK != ooGkClientSetGkMode(pGkClient, eGkMode, szGkAddr, iGkPort))   {      OOTRACEERR1("Error:Failed to set Gk mode\n");      memReset(&gH323ep.ctxt);      return OO_FAILED;   }     /* Create default parameter set */   pGkClient->grqTimeout = DEFAULT_GRQ_TIMEOUT;   pGkClient->rrqTimeout = DEFAULT_RRQ_TIMEOUT;   pGkClient->regTimeout = DEFAULT_REG_TTL;   pGkClient->arqTimeout = DEFAULT_ARQ_TIMEOUT;   pGkClient->drqTimeout = DEFAULT_DRQ_TIMEOUT;   dListInit(&pGkClient->callsPendingList);   dListInit(&pGkClient->callsAdmittedList);   dListInit(&pGkClient->timerList);   pGkClient->state = GkClientIdle;   return OO_OK;}int ooGkClientSetCallbacks   (ooGkClient *pGkClient, OOGKCLIENTCALLBACKS callbacks){   pGkClient->callbacks.onReceivedRegistrationConfirm =                                       callbacks.onReceivedRegistrationConfirm;   pGkClient->callbacks.onReceivedUnregistrationConfirm =                                      callbacks.onReceivedUnregistrationConfirm;   pGkClient->callbacks.onReceivedUnregistrationRequest =                                      callbacks.onReceivedUnregistrationRequest;   return OO_OK;}int ooGkClientReInit(ooGkClient *pGkClient){   ooGkClientCloseChannel(pGkClient);   pGkClient->gkRasIP[0]='\0';   pGkClient->gkCallSignallingIP[0]='\0';   pGkClient->gkRasPort = 0;   pGkClient->gkCallSignallingPort = 0;   pGkClient->rrqRetries = 0;   pGkClient->grqRetries = 0;   pGkClient->requestSeqNum = 0;      dListFreeAll(&pGkClient->ctxt, &pGkClient->callsPendingList);   dListFreeAll(&pGkClient->ctxt, &pGkClient->callsAdmittedList);   dListFreeAll(&pGkClient->ctxt, &pGkClient->timerList);   pGkClient->state = GkClientIdle;   return OO_OK;}void ooGkClientPrintConfig(ooGkClient *pGkClient){   OOTRACEINFO1("Gatekeeper Client Configuration:\n");   if(pGkClient->gkMode == RasUseSpecificGatekeeper)   {      OOTRACEINFO1("\tGatekeeper mode - UseSpecificGatekeeper\n");      OOTRACEINFO3("\tGatekeeper To Use - %s:%d\n", pGkClient->gkRasIP,                                                     pGkClient->gkRasPort);   }else if(pGkClient->gkMode == RasDiscoverGatekeeper)   {      OOTRACEINFO1("\tGatekeeper mode - UseSpecificGatekeeper\n");   }else{      OOTRACEERR1("Invalid GatekeeperMode\n");   }}int ooGkClientDestroy(void){   if(gH323ep.gkClient)   {      if(gH323ep.gkClient->state == GkClientRegistered)      {         OOTRACEINFO1("Unregistering from Gatekeeper\n");         if(ooGkClientSendURQ(gH323ep.gkClient, NULL)!=OO_OK)            OOTRACEERR1("Error:Failed to send URQ to gatekeeper\n");      }      OOTRACEINFO1("Destroying Gatekeeper Client\n");      ooGkClientCloseChannel(gH323ep.gkClient);      freeContext(&gH323ep.gkClient->msgCtxt);      freeContext(&gH323ep.gkClient->ctxt);      memFreePtr(&gH323ep.ctxt, gH323ep.gkClient);      gH323ep.gkClient = NULL;   }   return OO_OK;}int ooGkClientStart(ooGkClient *pGkClient){   int iRet=0;   iRet = ooGkClientCreateChannel(pGkClient);   if(iRet != OO_OK)   {      OOTRACEERR1("Error: GkClient Channel Creation failed\n");      return OO_FAILED;   }      pGkClient->discoveryComplete = FALSE;   iRet = ooGkClientSendGRQ(pGkClient);   if(iRet != OO_OK)   {      OOTRACEERR1("Error:Failed to send GRQ message\n");      pGkClient->state = GkClientFailed;      return OO_FAILED;   }   return OO_OK;}   int ooGkClientSetGkMode(ooGkClient *pGkClient, enum RasGatekeeperMode eGkMode,                         char *szGkAddr, int iGkPort ){   pGkClient->gkMode = eGkMode;   if(eGkMode == RasUseSpecificGatekeeper)   {      OOTRACEINFO1("Gatekeeper Mode - RasUseSpecificGatekeeper\n");      if(szGkAddr)      {	 if(strlen(szGkAddr)>MAX_IP_LEN)         {             OOTRACEERR2("Error:Invalid IP address specified - %s\n", szGkAddr);            return OO_FAILED;         }         strcpy(pGkClient->gkRasIP, szGkAddr);      }      if(iGkPort)         pGkClient->gkRasPort = iGkPort;      else         pGkClient->gkRasPort = DEFAULT_GKPORT;      OOTRACEINFO3("Gatekeeper IP:port set to - %s:%d\n",                     szGkAddr,  pGkClient->gkRasPort);   }else if(eGkMode == RasDiscoverGatekeeper)   {      OOTRACEINFO1("Gatekeeper Mode - RasDiscoverGatekeeper\n");   }else if(eGkMode == RasNoGatekeeper)   {      OOTRACEINFO1("Gatekeeper Mode - RasNoGatekeeper\n");   }   return OO_OK;}   /** * Create the RAS channel (socket). * */int ooGkClientCreateChannel(ooGkClient *pGkClient){   int ret=0;   OOIPADDR ipaddrs;   /* Create socket */   if((ret=ooSocketCreateUDP(&pGkClient->rasSocket))!=ASN_OK)   {      OOTRACEERR1("Failed to create RAS socket\n");      pGkClient->state = GkClientFailed;      return OO_FAILED;   }   if(pGkClient->localRASPort)   {      ret= ooSocketStrToAddr (pGkClient->localRASIP, &ipaddrs);      if( (ret=ooSocketBind( pGkClient->rasSocket, ipaddrs,            pGkClient->localRASPort))!=ASN_OK )       {         OOTRACEERR1("ERROR:Failed to create RAS channel\n");         pGkClient->state = GkClientFailed;         return OO_FAILED;      }   }   else {      ret = ooBindPort (OOUDP, pGkClient->rasSocket, pGkClient->localRASIP);      if(ret == OO_FAILED)      {         OOTRACEERR1("ERROR: Failed to bind port to RAS socket\n");         pGkClient->state = GkClientFailed;         return OO_FAILED;      }      pGkClient->localRASPort = ret;   }   /* Test Code NOTE- This doesn't work..:(( Have to fix this */   /* If multihomed, get ip from socket */   if(!strcmp(pGkClient->localRASIP, "0.0.0.0"))   {      OOTRACEDBGA1("Determining ip address for RAS channel "                   "multihomed mode. \n");      ret = ooSocketGetIpAndPort(pGkClient->rasSocket, pGkClient->localRASIP,                                  20, &pGkClient->localRASPort);      if(ret != ASN_OK)      {         OOTRACEERR1("Error:Failed to retrieve local ip and port from "                     "socket for RAS channel(multihomed).\n");         pGkClient->state = GkClientFailed;         return OO_FAILED;      }      OOTRACEDBGA3("Using local ip %s and port %d for RAS channel"                   "(multihomedMode).\n", pGkClient->localRASIP,                    pGkClient->localRASPort);   }   /* End of Test code */   OOTRACEINFO1("H323 RAS channel creation - successful\n");   return OO_OK;}int ooGkClientCloseChannel(ooGkClient *pGkClient){   int ret;   if(pGkClient->rasSocket != 0)   {      ret = ooSocketClose(pGkClient->rasSocket);      if(ret != ASN_OK)      {         OOTRACEERR1("Error: failed to close RAS channel\n");         pGkClient->rasSocket = 0;         return OO_FAILED;      }      pGkClient->rasSocket = 0;   }   OOTRACEINFO1("Closed RAS channel\n");   return OO_OK;}/** * Fill vendor data in RAS message structure. */void ooGkClientFillVendor   (ooGkClient *pGkClient, H225VendorIdentifier *pVendor ){   pVendor->vendor.t35CountryCode = gH323ep.t35CountryCode;   pVendor->vendor.t35Extension = gH323ep.t35Extension;   pVendor->vendor.manufacturerCode = gH323ep.manufacturerCode;   pVendor->enterpriseNumber.numids=0;   if(gH323ep.productID)   {      pVendor->m.productIdPresent = TRUE;      pVendor->productId.numocts = ASN1MIN(strlen(gH323ep.productID),                                              sizeof(pVendor->productId.data));      memcpy(pVendor->productId.data, gH323ep.productID,                                                   pVendor->productId.numocts);   }   if(gH323ep.versionID)   {      pVendor->m.versionIdPresent = 1;      pVendor->versionId.numocts = ASN1MIN(strlen(gH323ep.versionID),                                              sizeof(pVendor->versionId.data));      memcpy(pVendor->versionId.data, gH323ep.versionID,                                                  pVendor->versionId.numocts);    }}   int ooGkClientReceive(ooGkClient *pGkClient){   ASN1OCTET recvBuf[1024];   int recvLen;   char remoteHost[32];   int iFromPort=0;   OOCTXT *pctxt=NULL;   H225RasMessage *pRasMsg=NULL;   int iRet=OO_OK;      pctxt = &pGkClient->msgCtxt;   recvLen = ooSocketRecvFrom(pGkClient->rasSocket, recvBuf, 1024, remoteHost,                              32, &iFromPort);   if(recvLen <0)   {      OOTRACEERR1("Error:Failed to receive RAS message\n");      return OO_FAILED;   }   OOTRACEDBGA1("GkClient Received RAS Message\n");     /* Verify the gk */   if(pGkClient->discoveryComplete)   {      if((strncmp(pGkClient->gkRasIP, remoteHost,strlen(pGkClient->gkRasIP)))||         (pGkClient->gkRasPort!= iFromPort) )      {         OOTRACEWARN3("WARN:Ignoring message received from unknown gatekeeper "                      "%s:%d\n", remoteHost, iFromPort);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?