⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encdec.c

📁 技术文件名称:MPLSv1.0软件模块测试规程
💻 C
📖 第 1 页 / 共 5 页
字号:

   MEM_COPY((unsigned char *)tlv, buff, MPLS_TLVFIXLEN);

   tlv->flags.mark = ntohs(tlv->flags.mark); 
   tlv->length     = ntohs(tlv->length); 
   return MPLS_TLVFIXLEN;

} /* End: Mpls_decodeLdpTlv */




/*
 *      Encode-decode for CSP (common session param)
 */

/* 
 * encode: 
 */ 
long Mpls_encodeLdpCsp
(
   mplsLdpCspTlv_t  * csp, 
   unsigned char           * buff, 
   long                bufSize
)
{
   unsigned long           encodedSize = 0;
   unsigned char *        tempBuf     = buff; /* no change for the buff ptr */
   unsigned char *        cspPtr;

   if (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN > bufSize)
   {
      /* not enough room */
      return MPLS_ENC_BUFFTOOSMALL; 
   }
	csp->baseTlv.length = MPLS_CSPFIXLEN;
   cspPtr  = (unsigned char *)csp;
   /* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(csp->baseTlv),
                                    buff, 
				    				MPLS_TLVFIXLEN); 
   if (encodedSize < 0)
   {
      PRINT_ERR("failed encoding the tlv in CSP\n");
      return MPLS_ENC_TLVERROR;
   }
   tempBuf += encodedSize; 
   cspPtr  += encodedSize;

   /* 
    *  encode for the rest of the Csp 
    */
   csp->protocolVersion = htons(csp->protocolVersion);
   csp->holdTime        = htons(csp->holdTime);
   csp->flags.mark      = htons(csp->flags.mark);
   csp->maxPduLen       = htons(csp->maxPduLen);
   csp->rcvLsrAddress   = htonl(csp->rcvLsrAddress);
   csp->rcvLsId         = htons(csp->rcvLsId);
   
   MEM_COPY(tempBuf, 
            cspPtr, 
            MPLS_CSPFIXLEN);
   return (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN);

} /* End: Mpls_encodeLdpCsp*/


/* 
 * decode: 
 */ 
long Mpls_decodeLdpCsp
(
   mplsLdpCspTlv_t  * csp, 
   unsigned char           * buff, 
   long                bufSize
)
{
   unsigned char * cspPtr;

   if ((MPLS_CSPFIXLEN > bufSize)||(csp->baseTlv.length > bufSize))
   {
      /* not enough data for Csp */
      PRINT_ERR("failed decoding LdpCsp\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

   cspPtr  = (unsigned char *)csp;
   cspPtr  += MPLS_TLVFIXLEN; /* we want to point to the flags since the
				 tlv was decoded before we reach here */

   /* 
    *  decode for the rest of the Csp 
    */
   MEM_COPY( cspPtr, 
	     buff, 
             MPLS_CSPFIXLEN);

   csp->protocolVersion = ntohs(csp->protocolVersion);
   csp->holdTime        = ntohs(csp->holdTime);
   csp->flags.mark      = ntohs(csp->flags.mark);
   csp->maxPduLen       = ntohs(csp->maxPduLen);
   csp->rcvLsrAddress   = ntohl(csp->rcvLsrAddress);
   csp->rcvLsId         = ntohs(csp->rcvLsId);
   
   return MPLS_CSPFIXLEN;

} /* Mpls_decodeLdpCsp*/



/*
 *      Encode-decode for Fr Session Parameters
 */

/* 
 * encode
 */ 
long Mpls_encodeLdpFsp
(
   mplsLdpFspTlv_t * frFsp, 
   unsigned char          * buff, 
   long               bufSize
)
{
   unsigned long    encodedSize = 0;
   unsigned short  totalSize   = 0;
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */
   unsigned long    i, numLblRng;

   /* get the size of the frAsp to be encoded and check it against
      the buffer size */

   if (MPLS_TLVFIXLEN + (long)(frFsp->baseTlv.length)> bufSize)
   {
      /* not enough room */
      return MPLS_ENC_BUFFTOOSMALL; 
   }
   	tempBuf += MPLS_TLVFIXLEN;
	bufSize -= MPLS_TLVFIXLEN;
   /* 
    *  encode for M + N + dir + res 
    */
   numLblRng              = frFsp->flags.flags.numLblRng;
   frFsp->flags.flags.res = 0;
   frFsp->flags.mark      = htonl(frFsp->flags.mark);

   MEM_COPY( tempBuf, 
	     (unsigned char*)&(frFsp->flags.mark),
             MPLS_FSPFIXLEN );
   tempBuf   += MPLS_FSPFIXLEN; 
   totalSize += MPLS_FSPFIXLEN;

   /* 
    *  encode for FR labels 
    */
   for (i = 0; i < numLblRng; i++)
   {
      encodedSize = Mpls_encodeLdpFrLblRng( &(frFsp->lblRngList[i]),
		                            tempBuf,
		                            bufSize-totalSize);
      if (encodedSize < 0)
      {
	 return MPLS_ENC_FSPLBLERROR;
      }
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
	frFsp->baseTlv.length = totalSize;
	/* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(frFsp->baseTlv),
                                    buff, 
				    				MPLS_TLVFIXLEN);
   if (encodedSize < 0)
   {
      return MPLS_ENC_TLVERROR;
   }
   tempBuf   += encodedSize; 
   totalSize += encodedSize;

   return totalSize;

} /* End: Mpls_encodeLdpFsp */


/* 
 * decode
 */ 
long Mpls_decodeLdpFsp
(
   mplsLdpFspTlv_t * frFsp, 
   unsigned char          * buff, 
   long               bufSize
)
{ 
   long      decodedSize = 0;
   unsigned short  totalSize   = 0;
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */
   unsigned long    i;

   if ((MPLS_FSPFIXLEN > bufSize)||(frFsp->baseTlv.length > bufSize))
   {
      /* the buffer does not contain even the required field*/
      PRINT_ERR("failed in decoding LdpFsp\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

   /* 
    *  decode for M + N + res 
    */
   MEM_COPY((unsigned char *)&(frFsp->flags.mark), tempBuf, MPLS_FSPFIXLEN);
   tempBuf   += MPLS_FSPFIXLEN;
   totalSize += MPLS_FSPFIXLEN;

   frFsp->flags.mark = ntohl(frFsp->flags.mark);

   /*
    *  decode for FR labels 
    */
   for (i = 0; i < frFsp->flags.flags.numLblRng; i++)
   {
      decodedSize = Mpls_decodeLdpFrLblRng( &(frFsp->lblRngList[i]),
		                            tempBuf,
			                    bufSize - totalSize);
      if (decodedSize < 0)
      {
         PRINT_ERR_2("failed in decoding LdpFrLabel[%d] for LdpFsp\n", i);
	        return MPLS_TLV_VAL_ERROR;
      }
      tempBuf   += decodedSize;
      totalSize += decodedSize;
   }

   return  totalSize; 

} /* End: Mpls_decodeLdpFsp */



/*
 *      Encode-decode for INIT msg 
 */

/* 
 * encode for init message 
 */ 
long Mpls_encodeLdpInitMsg
(
   mplsLdpInitMsg_t * initMsg, 
   unsigned char           * buff, 
   long                bufSize
)
{
/*
   mplsLdpInitMsg_t initMsgCopy;
*/
   unsigned long            encodedSize, totalSize;
   unsigned char *         tempBuf = buff; /* no change for the buff ptr */
/*
   initMsgCopy = *initMsg;
*/
   memcpy(&initMsgCopy,initMsg,sizeof(mplsLdpInitMsg_t));
   totalSize   = 0;

   /* check the length of the messageId + mandatory param +
      optional param */
	tempBuf += (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);
   	bufSize -= (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);
   	/*
   if ((long)(initMsgCopy.baseMsg.msgLength) + MPLS_MSGHDRFIXLEN > bufSize) 
   {
      PRINT_ERR("failed to encode the init msg: BUFFER TOO SMALL\n");
      return MPLS_ENC_BUFFTOOSMALL;
   }
	*/
   /*
    *  encode the csp if any 
    */
   if (initMsgCopy.cspExists)
   {
      encodedSize = Mpls_encodeLdpCsp( &(initMsgCopy.csp), 
				                       tempBuf, 
				                       bufSize-totalSize
				                      );
      if (encodedSize < 0)
      {
    	 return MPLS_ENC_CSPERROR;
      }
/*      PRINT_2("Encoded for CSP %d bytes\n", encodedSize); */
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }

   /*
    *  encode the asp if any 
    */
   if (initMsgCopy.aspExists)
   {

      encodedSize = Mpls_encodeLdpAsp( &(initMsgCopy.asp), 
				                       tempBuf, 
				                       bufSize-totalSize
				                      );
      if (encodedSize < 0)
      {
	    return MPLS_ENC_ASPERROR;
      }
/*      PRINT_2("Encoded for ASP %d bytes\n", encodedSize); */
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }

   /*
    *  encode the fsp if any 
    */
   if (initMsgCopy.fspExists)
   {
      encodedSize = Mpls_encodeLdpFsp( &(initMsgCopy.fsp), 
				                       tempBuf, 
				                       bufSize-totalSize
				                      );
      if (encodedSize < 0)
      {
	    return MPLS_ENC_FSPERROR;
      }
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if(initMsgCopy.unknownNo > 0)
   {
   		encodedSize = Mpls_encodeLdpUnknownTlv( (initMsgCopy.baseTlv),
   												initMsgCopy.unknownValue,
   												initMsgCopy.unknownNo,
   												tempBuf,
   												bufSize-totalSize
   											   );
  		if (encodedSize < 0)
  		{
  			return MPLS_ENC_UNKERROR;
  		}
  		tempBuf   += encodedSize;
      	totalSize += encodedSize;
   }

   /*
    *  encode the base part of the pdu message
    */
    initMsgCopy.baseMsg.msgLength		=	(unsigned short) totalSize;
   	encodedSize = Mpls_encodeLdpBaseMsg( &(initMsgCopy.baseMsg), 
				                        buff, 
				                        (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN)
				                       );
   	if (encodedSize < 0)
   	{
      	return MPLS_ENC_BASEMSGERROR;
   	}
	/*   PRINT_2("Encode BaseMsg for init on %d bytes\n", encodedSize); */
   	totalSize += encodedSize;
   return totalSize;

} /* End: Mpls_encodeLdpInitMsg */


/* 
 * decode for init message 
 */ 
long Mpls_decodeLdpInitMsg
(
   mplsLdpInitMsg_t * initMsg, 
   unsigned char           * buff, 
   long                bufSize
)
{
   long          decodedSize    = 0;
   unsigned long         totalSize      = 0;
   long         stopLength     = 0;
   unsigned char *       tempBuf        = buff; /* no change for the buff ptr */
   unsigned long         totalSizeParam = 0;
   mplsLdpTlv_t  tlvTemp;

   /*
    *  decode the base part of the pdu message
    */
   bzero(initMsg, sizeof(mplsLdpInitMsg_t));
   decodedSize = Mpls_decodeLdpBaseMsg( &(initMsg->baseMsg), 
				                        tempBuf, 
				                        bufSize
				                       );
   if (decodedSize < 0)
   {
      return decodedSize;
   }
/*   PRINT_2("Decode BaseMsg for init on %d bytes\n", decodedSize); */

   if (initMsg->baseMsg.flags.flags.msgType != MPLS_INIT_MSGTYPE)
   {
      PRINT_ERR_2("Not the right message type; expected init and got %x\n",
		           initMsg->baseMsg.flags.flags.msgType);
      return MPLS_MSGTYPEERROR;
   }
   tempBuf   += decodedSize;
   totalSize += decodedSize;

   if (bufSize-totalSize <= 0)
   {
      /* nothing left for decoding */
      PRINT_ERR("Init msg does not have anything beside base msg\n");
      return totalSize;
   }

/*   PRINT_4("bufSize = %d,  totalSize = %d, initMsg->baseMsg.msgLength = %d\n",
	   bufSize, totalSize, initMsg->baseMsg.msgLength);
*/

   /* Have to check the baseMsg.msgLength to know when to finish.
    * We finsh when the totalSizeParam is >= to the base message length - the
    * message id length (4) 
    */
    
   stopLength = initMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
   if ( stopLength < MPLS_TLVFIXLEN )	return MPLS_MSG_LENGTH_ERROR;
   while (stopLength > totalSizeParam)
   {
      /*
       *  decode the tlv to check what's next
       */
      decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize-totalSize);
      if (decodedSize < 0)
      {
         /* something wrong */
         PRINT_ERR("INIT msg decode failed for tlv\n");
         return decodedSize;
      }

      tempBuf        += decodedSize;
      totalSize      += decodedSize;
      totalSizeParam += decodedSize;

      switch (tlvTemp.flags.flags.type)
      {
        case MPLS_CSP_TLVTYPE:
	    {
              decodedSize = Mpls_decodeLdpCsp( &(initMsg->csp), 
				               tempBuf, 
				               bufSize-totalSize);
              if (decodedSize < 0)
              {
		        PRINT_ERR("Failure when decoding Csp from init msg\n");
	            return decodedSize;
              }
/*	      PRINT_2("Decoded for CSP %d bytes\n", decodedSize); */
              tempBuf        += decodedSize;
              totalSize      += decodedSize;
              totalSizeParam += decodedSize;
	          initMsg->cspExists   = 1;
	          initMsg->csp.baseTlv = tlvTemp;
	          if (initMsg->csp.baseTlv.length != decodedSize )
			   {
			      /* not enough data for Chp */
			      PRINT_ERR("failed decoding hello Chp\n");
			      return MPLS_TLV_LENGTH_ERROR;
			   }
	          DEBUG_CALL(printCspTlv(&(initMsg->csp)));
	          break;
	    }
        case MPLS_ASP_TLVTYPE:
	    {
              decodedSize = Mpls_decodeLdpAsp( &(initMsg->asp), 
				               tempBuf, 
				               bufSize-totalSize);
              if (decodedSize < 0)
              {

⌨️ 快捷键说明

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