ooq931.c

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

C
1,944
字号
int ooGenerateCallToken (char *callToken, size_t size){   static int counter = 1;   char aCallToken[200];   int  ret = 0;   sprintf (aCallToken, "ooh323c_%d", counter++);   if (counter > OO_MAX_CALL_TOKEN)      counter = 1;   if ((strlen(aCallToken)+1) < size)      strcpy (callToken, aCallToken);   else {      OOTRACEERR1 ("Error: Insufficient buffer size to generate call token");      ret = OO_FAILED;   }   return ret;}/* CallReference is a two octet field, thus max value can be 0xffff   or 65535 decimal. We restrict max value to 32760, however, this should   not cause any problems as there won't be those many simultaneous calls   CallRef has to be locally unique and generated by caller.*/ASN1USINT ooGenerateCallReference(){   static ASN1USINT lastCallRef=0;   ASN1USINT newCallRef=0;   if(lastCallRef == 0)   {      /* Generate a new random callRef */      srand((unsigned)time(0));      lastCallRef = (ASN1USINT)(rand()%100);   }   else      lastCallRef++;   /* Note callReference can be at the most 15 bits that is from 0 to 32767.      if we generate number bigger than that, bring it in range.   */   if(lastCallRef>=32766)      lastCallRef=1;   newCallRef = lastCallRef;   OOTRACEDBGC2("Generated callRef %d\n", newCallRef);   return newCallRef;}int ooGenerateCallIdentifier(H225CallIdentifier *callid){   ASN1INT64 timestamp;   int i=0;#ifdef _WIN32      SYSTEMTIME systemTime;   GetLocalTime(&systemTime);   SystemTimeToFileTime(&systemTime, (LPFILETIME)&timestamp);#else   struct timeval systemTime;   gettimeofday(&systemTime, NULL);   timestamp = systemTime.tv_sec * 10000000 + systemTime.tv_usec*10;#endif   callid->guid.numocts = 16;   callid->guid.data[0] = 'o';   callid->guid.data[1] = 'o';   callid->guid.data[2] = 'h';   callid->guid.data[3] = '3';   callid->guid.data[4] = '2';   callid->guid.data[5] = '3';   callid->guid.data[6] = 'c';   callid->guid.data[7] = '-';   for (i = 8; i < 16; i++)       callid->guid.data[i] = (ASN1OCTET)((timestamp>>((i-8+1)*8))&0xff);   return OO_OK;}int ooFreeQ931Message(Q931Message *q931Msg){   if(!q931Msg)   {      memReset(&gH323ep.msgctxt);   }   return OO_OK;}int ooEncodeUUIE(Q931Message *q931msg){   ASN1OCTET msgbuf[1024];   ASN1OCTET * msgptr=NULL;   int  len;   ASN1BOOL aligned = TRUE;   Q931InformationElement* ie=NULL;   OOCTXT *pctxt = &gH323ep.msgctxt;   /*   memset(msgbuf, 0, sizeof(msgbuf));*/   if(!q931msg)   {      OOTRACEERR1("ERROR: Invalid Q931 message in add user-user IE\n");      return OO_FAILED;   }           if(!q931msg->userInfo)   {      OOTRACEERR1("ERROR:No User-User IE to encode\n");      return OO_FAILED;   }   setPERBuffer(pctxt, msgbuf, sizeof(msgbuf), aligned);      if(asn1PE_H225H323_UserInformation (pctxt,                                            q931msg->userInfo)==ASN_OK)   {      OOTRACEDBGC1("UserInfo encoding - successful\n");   }   else{      OOTRACEERR1("Error: UserInfo encoding failed\n");      return OO_FAILED;   }   msgptr = encodeGetMsgPtr(pctxt, &len);   /* Allocate memory to hold complete UserUser Information */   ie = (Q931InformationElement*)memAlloc (pctxt,                                     sizeof(*ie) - sizeof(ie->data) + len);   if(ie == NULL)   {      OOTRACEERR1("Error:Memory -  ooEncodeUUIE - ie\n");      return OO_FAILED;   }   ie->discriminator = Q931UserUserIE;   ie->length = len;   memcpy(ie->data, msgptr, len);   /* Add the user to user IE NOTE: ALL IEs SHOULD BE IN ASCENDING ORDER OF       THEIR DISCRIMINATOR AS PER SPEC.    */   dListInit (&(q931msg->ies));   if((dListAppend (pctxt,                       &(q931msg->ies), ie)) == NULL)   {      OOTRACEERR1("Error: Failed to add UUIE in outgoing message\n");      return OO_FAILED;   }   return OO_OK;}int ooDecodeUUIE(Q931Message *q931Msg){   DListNode* curNode;   unsigned int i;   ASN1BOOL aligned=TRUE;   int stat;   Q931InformationElement *ie;   OOCTXT *pctxt = &gH323ep.msgctxt;   if(q931Msg ==NULL)   {      OOTRACEERR1("Error: ooDecodeUUIE failed - NULL q931 message\n");      return OO_FAILED;   }           /* Search for UserUser IE */   for(i = 0, curNode = q931Msg->ies.head; i < q931Msg->ies.count;                                              i++, curNode = curNode->next)    {      ie = (Q931InformationElement*) curNode->data;      if(ie->discriminator == Q931UserUserIE)         break;   }   if(i == q931Msg->ies.count)   {      OOTRACEERR1("No UserUser IE found in ooDecodeUUIE\n");      return OO_FAILED;   }           /* Decode user-user ie */   q931Msg->userInfo = (H225H323_UserInformation *) memAlloc(pctxt,                                             sizeof(H225H323_UserInformation));   if(!q931Msg->userInfo)   {      OOTRACEERR1("ERROR:Memory - ooDecodeUUIE - userInfo\n");      return OO_FAILED;   }   memset(q931Msg->userInfo, 0, sizeof(H225H323_UserInformation));   setPERBuffer (pctxt, ie->data, ie->length, aligned);   stat = asn1PD_H225H323_UserInformation (pctxt, q931Msg->userInfo);   if(stat != ASN_OK)   {      OOTRACEERR1("Error: UserUser IE decode failed\n");      return OO_FAILED;   }   OOTRACEDBGC1("UUIE decode successful\n");   return OO_OK;}#ifndef _COMPACTstatic void ooQ931PrintMessage    (OOH323CallData* call, ASN1OCTET *msgbuf, ASN1UINT msglen){   OOCTXT *pctxt = &gH323ep.msgctxt;   Q931Message q931Msg;   int ret;   initializePrintHandler(&printHandler, "Q931 Message");   /* Set event handler */   setEventHandler (pctxt, &printHandler);   setPERBuffer (pctxt, msgbuf, msglen, TRUE);   ret = ooQ931Decode (call, &q931Msg, msglen, msgbuf);   if(ret != OO_OK)   {      OOTRACEERR3("Error:Failed decoding Q931 message. (%s, %s)\n", 		  call->callType, call->callToken);   }   finishPrint();   removeEventHandler(pctxt);}#endifint ooEncodeH225Message(OOH323CallData *call, Q931Message *pq931Msg,                         char *msgbuf, int size){   int len=0, i=0, j=0, ieLen=0;   int stat=0;   DListNode* curNode=NULL;   if(!msgbuf || size<200)   {      OOTRACEERR3("Error: Invalid message buffer/size for ooEncodeH245Message."                  " (%s, %s)\n", call->callType, call->callToken);      return OO_FAILED;   }   if(pq931Msg->messageType == Q931SetupMsg){      msgbuf[i++] = OOSetup;   }   else if(pq931Msg->messageType == Q931ConnectMsg){      msgbuf[i++] = OOConnect;   }   else if(pq931Msg->messageType == Q931CallProceedingMsg){      msgbuf[i++] = OOCallProceeding;   }   else if(pq931Msg->messageType == Q931AlertingMsg){      msgbuf[i++] = OOAlert;   }   else if(pq931Msg->messageType == Q931ReleaseCompleteMsg){      msgbuf[i++] = OOReleaseComplete;   }   else if(pq931Msg->messageType == Q931InformationMsg){      msgbuf[i++] = OOInformationMessage;   }   else if(pq931Msg->messageType == Q931FacilityMsg){      msgbuf[i++] = OOFacility;      msgbuf[i++] = pq931Msg->tunneledMsgType;      msgbuf[i++] = pq931Msg->logicalChannelNo>>8;      msgbuf[i++] = pq931Msg->logicalChannelNo;   }   else{      OOTRACEERR3("Error:Unknow Q931 message type. (%s, %s)\n", call->callType,                   call->callToken);      return OO_FAILED;   }   stat = ooEncodeUUIE(pq931Msg);   if(stat != OO_OK)   {      OOTRACEERR3("Error:Failed to encode uuie. (%s, %s)\n", call->callType,                    call->callToken);      return OO_FAILED;   }      msgbuf[i++] = 3; /* TPKT version */   msgbuf[i++] = 0; /* TPKT resevred */   /* 1st octet of length, will be populated once len is determined */   msgbuf[i++] = 0;    /* 2nd octet of length, will be populated once len is determined */   msgbuf[i++] = 0;    /* Q931 protocol discriminator */   msgbuf[i++] = pq931Msg->protocolDiscriminator;   msgbuf[i++] = 2; /* length of call ref is two octets */   msgbuf[i] = (pq931Msg->callReference >> 8); /* populate 1st octet */   if(!strcmp(call->callType, "incoming"))      msgbuf[i++] |= 0x80;   /* fromDestination*/   else      i++;   /* fromOriginator*/     msgbuf[i++] = pq931Msg->callReference; /* populate 2nd octet */   msgbuf[i++] = pq931Msg->messageType; /* type of q931 message */   /* Note: the order in which ies are added is important. It is in the      ascending order of ie codes.    */   /* Add bearer IE */   if(pq931Msg->bearerCapabilityIE)   {         msgbuf[i++] = Q931BearerCapabilityIE; /* ie discriminator */      msgbuf[i++] = pq931Msg->bearerCapabilityIE->length;      memcpy(msgbuf+i, pq931Msg->bearerCapabilityIE->data,                        pq931Msg->bearerCapabilityIE->length);      i += pq931Msg->bearerCapabilityIE->length;   }      /* Add cause IE */   if(pq931Msg->causeIE)   {      msgbuf[i++] = Q931CauseIE;      msgbuf[i++] = pq931Msg->causeIE->length;      memcpy(msgbuf+i, pq931Msg->causeIE->data, pq931Msg->causeIE->length);      i += pq931Msg->causeIE->length;   }          /*Add display ie. */   if(!ooUtilsIsStrEmpty(call->ourCallerId))   {      msgbuf[i++] = Q931DisplayIE;      ieLen = strlen(call->ourCallerId)+1;      msgbuf[i++] = ieLen;      memcpy(msgbuf+i, call->ourCallerId, ieLen-1);      i += ieLen-1;      msgbuf[i++] = '\0';   }   /* Add calling Party ie */   if(pq931Msg->callingPartyNumberIE)   {      msgbuf[i++] = Q931CallingPartyNumberIE;      msgbuf[i++] = pq931Msg->callingPartyNumberIE->length;      memcpy(msgbuf+i, pq931Msg->callingPartyNumberIE->data,                       pq931Msg->callingPartyNumberIE->length);      i += pq931Msg->callingPartyNumberIE->length;   }    /* Add called Party ie */   if(pq931Msg->calledPartyNumberIE)   {      msgbuf[i++] = Q931CalledPartyNumberIE;      msgbuf[i++] = pq931Msg->calledPartyNumberIE->length;      memcpy(msgbuf+i, pq931Msg->calledPartyNumberIE->data,                       pq931Msg->calledPartyNumberIE->length);      i += pq931Msg->calledPartyNumberIE->length;   }      /* Add keypad ie */   if(pq931Msg->keypadIE)   {      msgbuf[i++] = Q931KeypadIE;      msgbuf[i++] = pq931Msg->keypadIE->length;      memcpy(msgbuf+i, pq931Msg->keypadIE->data, pq931Msg->keypadIE->length);      i += pq931Msg->keypadIE->length;   }   /* Note: Have to fix this, though it works. Need to get rid of ie list.       Right now we only put UUIE in ie list. Can be easily removed.   */   for(j = 0, curNode = pq931Msg->ies.head; j < (int)pq931Msg->ies.count; j++)    {      Q931InformationElement *ie = (Q931InformationElement*) curNode->data;                ieLen = ie->length;      /* Add the ie discriminator in message buffer */      msgbuf[i++] = ie->discriminator;                 /* For user-user IE, we have to add protocol discriminator */      if (ie->discriminator == Q931UserUserIE)      {         ieLen++; /* length includes protocol discriminator octet. */         msgbuf[i++] = (ieLen>>8); /* 1st octet for length */

⌨️ 快捷键说明

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