oochannels.c

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

C
1,774
字号
   }   if (0 == call->pH245Channel) {      call->pH245Channel = 	 (OOH323Channel*) memAllocZ (call->pctxt, sizeof(OOH323Channel));   }   call->pH245Channel->sock = h245Channel;    call->h245SessionState = OO_H245SESSION_ACTIVE;   OOTRACEINFO3("H.245 connection established (%s, %s)\n",                 call->callType, call->callToken);   /* Start terminal capability exchange and master slave determination */   ret = ooSendTermCapMsg(call);   if(ret != OO_OK)   {      OOTRACEERR3("ERROR:Sending Terminal capability message (%s, %s)\n",                   call->callType, call->callToken);      return ret;   }   ret = ooSendMasterSlaveDetermination(call);   if(ret != OO_OK)   {      OOTRACEERR3("ERROR:Sending Master-slave determination message "                  "(%s, %s)\n", call->callType, call->callToken);      return ret;   }      return OO_OK;}int ooSetFDSETs(fd_set *pReadfds, fd_set *pWritefds, int *nfds){   OOH323CallData *call=NULL;   if(gH323ep.gkClient && gH323ep.gkClient->rasSocket != 0)   {      FD_SET(gH323ep.gkClient->rasSocket, pReadfds );      if ( *nfds < (int)gH323ep.gkClient->rasSocket)         *nfds = (int)gH323ep.gkClient->rasSocket;   }   if(gH323ep.listener)   {      FD_SET(*(gH323ep.listener), pReadfds);      if(*nfds < (int)*(gH323ep.listener))         *nfds = *((int*)gH323ep.listener);   }   if(gH323ep.cmdListener)   {      FD_SET(gH323ep.cmdListener, pReadfds);      if(*nfds < (int)gH323ep.cmdListener)         *nfds = (int)gH323ep.cmdListener;   }   if(gH323ep.cmdSock)   {      FD_SET(gH323ep.cmdSock, pReadfds);      if(*nfds < (int)gH323ep.cmdSock)         *nfds = (int)gH323ep.cmdSock;   }         if(gH323ep.callList)   {      call = gH323ep.callList;      while(call)      {         if (0 != call->pH225Channel && 0 != call->pH225Channel->sock)         {            FD_SET (call->pH225Channel->sock, pReadfds);            if (call->pH225Channel->outQueue.count > 0 ||               (OO_TESTFLAG (call->flags, OO_M_TUNNELING) &&                 0 != call->pH245Channel &&                 call->pH245Channel->outQueue.count>0))               FD_SET (call->pH225Channel->sock, pWritefds);            if(*nfds < (int)call->pH225Channel->sock)               *nfds = call->pH225Channel->sock;         }                    if (0 != call->pH245Channel &&  call->pH245Channel->sock != 0)         {            FD_SET(call->pH245Channel->sock, pReadfds);            if (call->pH245Channel->outQueue.count>0)               FD_SET(call->pH245Channel->sock, pWritefds);            if(*nfds < (int)call->pH245Channel->sock)               *nfds = call->pH245Channel->sock;         }         else if(call->h245listener)         {            OOTRACEINFO3("H.245 Listerner socket being monitored "                         "(%s, %s)\n", call->callType, call->callToken);             FD_SET(*(call->h245listener), pReadfds);             if(*nfds < (int)*(call->h245listener))               *nfds = *(call->h245listener);          }          call = call->next;                  }/* while(call) */   }/*if(gH323ep.callList) */   if(*nfds != 0) *nfds = *nfds+1;   return OO_OK;}int ooProcessFDSETsAndTimers   (fd_set *pReadfds, fd_set *pWritefds, struct timeval *pToMin){   OOH323CallData *call, *prev=NULL;   struct timeval toNext;   /* Process gatekeeper client timers */   if(gH323ep.gkClient)   {         ooTimerFireExpired(&gH323ep.gkClient->ctxt,                                         &gH323ep.gkClient->timerList);      if(ooTimerNextTimeout(&gH323ep.gkClient->timerList, &toNext))      {         if(ooCompareTimeouts(pToMin, &toNext)>0)         {            pToMin->tv_sec = toNext.tv_sec;            pToMin->tv_usec = toNext.tv_usec;         }      }      if(gH323ep.gkClient->state == GkClientFailed ||         gH323ep.gkClient->state == GkClientGkErr)      {         if(ooGkClientHandleClientOrGkFailure(gH323ep.gkClient)!=OO_OK)         {	   //ooStopMonitorCalls(); //Function calling ooProcessFDSETsAndTimers is responsible for this.            return OO_FAILED;         }      }   }   if(gH323ep.cmdListener)   {      if(FD_ISSET(gH323ep.cmdListener, pReadfds))      {         if(ooAcceptCmdConnection() != OO_OK){            OOTRACEERR1("Error:Failed to accept command connection\n");            return OO_FAILED;         }      }   }   if(gH323ep.cmdSock)   {      if(FD_ISSET(gH323ep.cmdSock, pReadfds))      {        ooReadAndProcessStackCommand();      }   }   /* Manage ready descriptors after select */   if(0 != gH323ep.gkClient && 0 != gH323ep.gkClient->rasSocket)   {      if(FD_ISSET( gH323ep.gkClient->rasSocket, pReadfds) )      {         ooGkClientReceive(gH323ep.gkClient);         if(gH323ep.gkClient->state == GkClientFailed ||            gH323ep.gkClient->state == GkClientGkErr)	   ooGkClientHandleClientOrGkFailure(gH323ep.gkClient);      }   }   if(gH323ep.listener)   {      if(FD_ISSET(*(gH323ep.listener), pReadfds))      {         OOTRACEDBGA1("New connection at H225 receiver\n");         ooAcceptH225Connection();      }         }          if(gH323ep.callList)   {      call = gH323ep.callList;      while(call)      {         ooTimerFireExpired(call->pctxt, &call->timerList);         if (0 != call->pH225Channel && 0 != call->pH225Channel->sock)         {            if(FD_ISSET(call->pH225Channel->sock, pReadfds))            {               if(ooH2250Receive(call) != OO_OK)               {                  OOTRACEERR3("ERROR:Failed ooH2250Receive - Clearing call "                             "(%s, %s)\n", call->callType, call->callToken);                  if(call->callState < OO_CALL_CLEAR)                  {                     call->callEndReason = OO_REASON_INVALIDMESSAGE;                     call->callState = OO_CALL_CLEAR;                   }               }            }         }	                     if (0 != call->pH245Channel && 0 != call->pH245Channel->sock)         {            if(FD_ISSET(call->pH245Channel->sock, pReadfds))            {                                          ooH245Receive(call);            }         }                     if (0 != call->pH245Channel && 0 != call->pH245Channel->sock)         {            if(FD_ISSET(call->pH245Channel->sock, pWritefds))            {                                          if(call->pH245Channel->outQueue.count>0)                  ooSendMsg(call, OOH245MSG);            }         }         else if(call->h245listener)         {            if(FD_ISSET(*(call->h245listener), pReadfds))            {               OOTRACEDBGC3("Incoming H.245 connection (%s, %s)\n",                            call->callType, call->callToken);               ooAcceptH245Connection(call);            }                                    }         if (0 != call->pH225Channel && 0 != call->pH225Channel->sock)         {            if(FD_ISSET(call->pH225Channel->sock, pWritefds))            {               if(call->pH225Channel->outQueue.count>0)               {                  OOTRACEDBGC3("Sending H225 message (%s, %s)\n",                               call->callType, call->callToken);                  ooSendMsg(call, OOQ931MSG);               }               if(call->pH245Channel &&                   call->pH245Channel->outQueue.count>0 &&                   OO_TESTFLAG (call->flags, OO_M_TUNNELING))               {                  OOTRACEDBGC3("H245 message needs to be tunneled. "                               "(%s, %s)\n", call->callType,                                call->callToken);                  ooSendMsg(call, OOH245MSG);               }            }                                         }         if(ooTimerNextTimeout(&call->timerList, &toNext))         {            if(ooCompareTimeouts(pToMin, &toNext) > 0)            {               pToMin->tv_sec = toNext.tv_sec;               pToMin->tv_usec = toNext.tv_usec;            }         }         prev = call;         call = call->next;         if(prev->callState >= OO_CALL_CLEAR)            ooEndCall(prev);      }/* while(call) */   }/* if(gH323ep.callList) */   return OO_OK;}   int ooMonitorChannels(){   int ret=0, nfds=0;   struct timeval toMin, toNext;   fd_set readfds, writefds;   gMonitor = TRUE;   toMin.tv_sec = 3;   toMin.tv_usec = 0;   ooH323EpPrintConfig();      if(gH323ep.gkClient)   {      ooGkClientPrintConfig(gH323ep.gkClient);      if(OO_OK != ooGkClientStart(gH323ep.gkClient))      {         OOTRACEERR1("Error:Failed to start Gatekeeper client\n");         ooGkClientDestroy();      }   }      while(1)   {      FD_ZERO(&readfds);      FD_ZERO(&writefds);      nfds = 0;      ooSetFDSETs(&readfds, &writefds, &nfds);      if(!gMonitor)      {         OOTRACEINFO1("Ending Monitor thread\n");         break;      }            if(nfds == 0)#ifdef _WIN32         Sleep(10);#else      {         toMin.tv_sec = 0;         toMin.tv_usec = 10000;         ooSocketSelect(1, 0, 0, 0, &toMin);      }#endif      else         ret = ooSocketSelect(nfds, &readfds, &writefds,                            NULL, &toMin);            if(ret == -1)      {                  OOTRACEERR1("Error in select ...exiting\n");         exit(-1);      }      toMin.tv_sec = 0;      toMin.tv_usec = 100000; /* 100ms*/      /*This is for test application. Not part of actual stack */        ooTimerFireExpired(&gH323ep.ctxt, &g_TimerList);      if(ooTimerNextTimeout(&g_TimerList, &toNext))      {         if(ooCompareTimeouts(&toMin, &toNext)>0)         {            toMin.tv_sec = toNext.tv_sec;            toMin.tv_usec = toNext.tv_usec;         }      }      if(ooProcessFDSETsAndTimers(&readfds, &writefds, &toMin) != OO_OK)      {         ooStopMonitorCalls();         continue;      }   }/* while(1)*/   return OO_OK;}int ooH2250Receive(OOH323CallData *call){   int  recvLen=0, total=0, ret=0;   ASN1OCTET message[MAXMSGLEN], message1[MAXMSGLEN];   int len;   Q931Message *pmsg;   OOCTXT *pctxt = &gH323ep.msgctxt;   struct timeval timeout;   fd_set readfds;      pmsg = (Q931Message*)memAlloc(pctxt, sizeof(Q931Message));   if(!pmsg)   {      OOTRACEERR3("ERROR:Failed to allocate memory for incoming H.2250 message"                  " (%s, %s)\n", call->callType, call->callToken);      memReset(&gH323ep.msgctxt);      return OO_FAILED;   }   memset(pmsg, 0, sizeof(Q931Message));   /* First read just TPKT header which is four bytes */   recvLen = ooSocketRecv (call->pH225Channel->sock, message, 4);   if(recvLen <= 0)   {      if(recvLen == 0)         OOTRACEWARN3("Warn:RemoteEndpoint closed connection (%s, %s)\n",                      call->callType, call->callToken);      else         OOTRACEERR3("Error:Transport failure while reading Q931 "                     "message (%s, %s)\n", call->callType, call->callToken);      ooCloseH225Connection(call);      if(call->callState < OO_CALL_CLEARED)      {         if(call->callState < OO_CALL_CLEAR)            call->callEndReason = OO_REASON_TRANSPORTFAILURE;         call->callState = OO_CALL_CLEARED;               }      ooFreeQ931Message(pmsg);      return OO_OK;   }   OOTRACEDBGC3("Receiving H.2250 message (%s, %s)\n",                call->callType, call->callToken);      /* Since we are working with TCP, need to determine the      message boundary. Has to be done at channel level, as channels      know the message formats and can determine boundaries   */   if(recvLen != 4)   {      OOTRACEERR4("Error: Reading TPKT header for H225 message "                  "recvLen= %d (%s, %s)\n", recvLen, call->callType,                   call->callToken);      ooFreeQ931Message(pmsg);      if(call->callState < OO_CALL_CLEAR)      {         call->callEndReason = OO_REASON_INVALIDMESSAGE;         call->callState = OO_CALL_CLEAR;      }      return OO_FAILED;   }      len = message[2];   len = len<<8;   len = len | message[3];   /* Remaining message length is length - tpkt length */   len = len - 4;   /* Now read actual Q931 message body. We should make sure that we      receive complete message as indicated by len. If we don't then there       is something wrong. The loop below receives message, then checks whether      complete message is received. If not received, then uses select to peek      for remaining bytes of the message. If message is not received in 3      seconds, then we have a problem. Report an error and exit.   */   while(total < len)   {      recvLen = ooSocketRecv (call->pH225Channel->sock, message1, len-total);      memcpy(message+total, message1, recvLen);      total = total + recvLen;      if(total == len) break; /* Complete message is received */            FD_ZERO(&readfds);      FD_SET(call->pH225Channel->sock, &readfds);      timeout.tv_sec = 3;      timeout.tv_usec = 0;      ret = ooSocketSelect(call->pH225Channel->sock+1, &readfds, NULL, 

⌨️ 快捷键说明

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