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

📄 encdec.c

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

	        notMsg->retMsgTlvExists = 1;
	        notMsg->retMsg.baseTlv  = tlvTemp;
	        DEBUG_CALL(printRetMsgTlv(&(notMsg->retMsg)));
	        break;
	    }
         case MPLS_LSPID_TLVTYPE:
	    {
            decodedSize = Mpls_decodeLdpLspIdTlv( &(notMsg->lspidTlv), 
				                                  tempBuf, 
				                                  bufSize-totalSize
				                                 );
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when dec LSPID tlv from Not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for lspid tlv %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

	        notMsg->lspidTlvExists   = 1;
	        notMsg->lspidTlv.baseTlv = tlvTemp;
	        if (notMsg->lspidTlv.baseTlv.length != decodedSize )
		   {
		      /* not enough data for Chp */
		      PRINT_ERR("failed decoding hello Chp\n");
		      return MPLS_TLV_LENGTH_ERROR;
		   }
	        DEBUG_CALL(printLspIdTlv(&(notMsg->lspidTlv)));
	        break;
	    }
	    case MPLS_REQMSGID_TLVTYPE:
	    {
            decodedSize = Mpls_decodeLdpLblMsgIdTlv( &(notMsg->lblMsgIdTlv), 
				                                  tempBuf, 
				                                  bufSize-totalSize
				                                 );
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when dec LSPID tlv from Not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for lspid tlv %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

	        notMsg->lblMsgIdTlvExists   = 1;
	        notMsg->lblMsgIdTlv.baseTlv = tlvTemp;
	        if (notMsg->lblMsgIdTlv.baseTlv.length != decodedSize )
		   {
		      /* not enough data for Chp */
		      PRINT_ERR("failed decoding hello Chp\n");
		      return MPLS_TLV_LENGTH_ERROR;
		   }
	        DEBUG_CALL(printLblMsgIdTlv(&(notMsg->lblMsgIdTlv)));
	        break;
	    }
         default:
	    {
	      PRINT_ERR_2("Found wrong tlv type while decoding not msg (%d)\n",
	                    tlvTemp.flags.flags.type);
	      if (tlvTemp.flags.flags.uBit == 1)
	      {
			if(tlvTemp.flags.flags.fBit == 1)
	      		{
	      			decodedSize = Mpls_decodeLdpUnknownTlv(	notMsg->baseTlv,
	      										tlvTemp,
	      										notMsg->unknownValue,
	      										&notMsg->unknownNo,
		      									tempBuf, 
					                            bufSize-totalSize
	      									);
	      			if(decodedSize < 0)
	      			{
	      				PRINT_ERR("Failure when decoding unknown from init msg\n");
	            		return decodedSize;
	      			}
	      			tempBuf        += decodedSize;
              		totalSize      += decodedSize;
              		totalSizeParam += decodedSize;
	      			break;
	      		}
	      		else
	      		{
	      			
			 		/* ignore the Tlv and continue processing */
	                 tempBuf        += tlvTemp.length;
	                 totalSize      += tlvTemp.length;
	                 totalSizeParam += tlvTemp.length;
	                 break;
	             }
	      }
	      else
	      {
		    /* drop the message; return error */
		    return MPLS_TLVTYPEERROR;
	      }
	   }
      } /* switch type */

   } /* while */

   PRINT_2("totalsize for Mpls_decodeLdpNotMsg is %d\n", totalSize);
	if (stopLength != totalSizeParam ) return MPLS_MSG_LENGTH_ERROR;
   return totalSize;

} /* End: Mpls_decodeLdpNotMsg */



/*
 *      Encode-decode for Status TLV 
 */

/* 
 * encode:
 */ 
long Mpls_encodeLdpStatus 
(
   mplsLdpStatusTlv_t * status, 
   unsigned char             * buff, 
   long                  bufSize
) 
{
   unsigned long    encodedSize = 0;
   unsigned char * tempBuf      = buff; /* no change for the buff ptr */
   unsigned char * statusPtr;

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

   statusPtr  = (unsigned char *)status;
   tempBuf    += encodedSize; 
   statusPtr  += encodedSize;

   /* 
    *  encode for the rest of the  Status
    */
   status->flags.mark = htonl(status->flags.mark);
   status->msgId      = htonl(status->msgId);
   status->msgType    = htons(status->msgType);

   MEM_COPY(tempBuf, 
            statusPtr, 
            MPLS_STATUSFIXLEN
           );
           
   return (MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN);

} /* End: Mpls_encodeLdpStatus */


/* 
 * decode:
 */ 
long Mpls_decodeLdpStatus 
(
   mplsLdpStatusTlv_t * status, 
   unsigned char             * buff, 
   long                  bufSize
)
{
   unsigned char * statusPtr;

   if (MPLS_STATUSFIXLEN> bufSize)
   {
      /* not enough data for Status*/
      PRINT_ERR("failed decoding Status\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

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

   /* 
    *  decode for the rest of the Status
    */
   MEM_COPY( statusPtr, 
	         buff, 
             MPLS_STATUSFIXLEN);

   status->flags.mark = ntohl(status->flags.mark);
   status->msgId      = ntohl(status->msgId);
   status->msgType    = ntohs(status->msgType);
   
   return MPLS_STATUSFIXLEN;

} /* End: Mpls_decodeLdpStatus */


/*
 *      Encode-decode for Extended Status TLV 
 */


/* 
 * encode:
 */ 
long Mpls_encodeLdpExStatus 
(
   mplsLdpExStatusTlv_t * status, 
   unsigned char               * buff, 
   long                    bufSize
)
{
   unsigned long    encodedSize = 0;
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */

   if (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN > bufSize)
   {
      /* not enough room */
      return MPLS_ENC_BUFFTOOSMALL; 
   }
	status->baseTlv.length = MPLS_EXSTATUSLEN;
   /* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(status->baseTlv),
                                    tempBuf, 
				                    bufSize
				                   ); 
   if (encodedSize < 0)
   {
      PRINT_ERR("failed encoding the tlv in EX_STATUS\n");
      return MPLS_ENC_TLVERROR;
   }
   tempBuf   += encodedSize; 
   status->value = htonl(status->value);
   MEM_COPY(tempBuf, 
	        (unsigned char *)&(status->value),
            sizeof(unsigned long)
           );
   return (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN);

} /* End: Mpls_encodeLdpExStatus */


/* 
 * decode:
 */ 
long Mpls_decodeLdpExStatus 
(
   mplsLdpExStatusTlv_t * status, 
   unsigned char                * buff, 
   long                   bufSize
)
{
   if (MPLS_EXSTATUSLEN > bufSize)
   {
      /* not enough data for ExStatus*/
      PRINT_ERR("failed decoding ExStatus\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

   /* 
    *  decode for the rest of the Status
    */
   MEM_COPY( &(status->value), 
	         buff, 
             MPLS_EXSTATUSLEN
            );

   status->value = ntohl(status->value);

   return MPLS_EXSTATUSLEN;

} /* End: Mpls_decodeLdpExStatus */




/*
 *      Encode-decode for Return PDU TLV
 */

/* 
 * encode:
 */ 
long Mpls_encodeLdpRetPdu 
(
   mplsLdpRetPduTlv_t   * retPdu, 
   unsigned char               * buff, 
   long                    bufSize
)
{
   unsigned long    encodedSize = 0;
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */
   unsigned short  tempLength;         /* to store the tlv length for
				   later use */

   if ( MPLS_TLVFIXLEN + (long)(retPdu->baseTlv.length) > bufSize )
   {
      /* not enough room */
      return MPLS_ENC_BUFFTOOSMALL; 
   }

   tempLength = retPdu->baseTlv.length;
   /* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(retPdu->baseTlv),
                                    tempBuf, 
				                    bufSize
				                   ); 
   if (encodedSize < 0)
   {
      PRINT_ERR("failed encoding the tlv in RET_PDU\n");
      return MPLS_ENC_TLVERROR;
   }
   tempBuf += encodedSize; 

   /* 
    *  encode the data of the ret pdu
    */

   encodedSize = Mpls_encodeLdpMsgHeader( &(retPdu->headerTlv),
					                      tempBuf,
					                      bufSize-encodedSize
					                    );
   if (encodedSize < 0)
   {
      PRINT_ERR("failed encoding the header Tlv in RET_PDU\n");
      return MPLS_ENC_HDRTLVERROR;
   }
   tempBuf += encodedSize; 

   MEM_COPY(tempBuf, 
            retPdu->data, 
            tempLength);

   return (MPLS_TLVFIXLEN + tempLength);

} /* End: Mpls_encodeLdpRetPdu */


/* 
 * decode:
 */ 
long Mpls_decodeLdpRetPdu 
(
   mplsLdpRetPduTlv_t   * retPdu, 
   unsigned char               * buff, 
   long                    bufSize,
   unsigned short                tlvLength
)
{
   unsigned char * tempBuf = buff; /* no change for the buff ptr */
   unsigned long    decodedSize;

   if ((long)tlvLength > bufSize)
   {
      /* not enough data for ExStatus*/
      PRINT_ERR("failed decoding Ret pdu\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

   /* 
    *  decode data for ret pdu
    */
   decodedSize = Mpls_decodeLdpMsgHeader( &(retPdu->headerTlv),
					                      tempBuf,
					                      bufSize
					                     );
   if (decodedSize < 0)
   {
      PRINT_ERR("failed decoding the header Tlv in RET_PDU\n");
      return decodedSize;
   }
   tempBuf += decodedSize; 

   MEM_COPY( retPdu->data, 
	         tempBuf, 
             tlvLength);

   return tlvLength;

} /* End: Mpls_decodeLdpRetPdu */



/*
 *      Encode-decode for Return Msg TLV 
 */

/* 
 * encode:
 */ 
long Mpls_encodeLdpRetMsg 
(
   mplsLdpRetMsgTlv_t   * retMsg, 
   unsigned char               * buff, 
   long                    bufSize
)
{
   unsigned char * retMsgPtr;
   unsigned long    encodedSize = 0;
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */
   unsigned short  tempLength;         /* to store the tlv length for
				   later use */

   if ( MPLS_TLVFIXLEN + (long)(retMsg->baseTlv.length) > bufSize )
   {
      /* not enough room */
      return MPLS_ENC_BUFFTOOSMALL; 
   }

   tempLength = retMsg->baseTlv.length;
   /* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(retMsg->baseTlv),
                                    tempBuf, 
				                    bufSize
				                   ); 
   if (encodedSize < 0)
   {
      PRINT_ERR("failed encoding the tlv in RET_MSG\n");
      return MPLS_ENC_TLVERROR;
   }

   retMsgPtr = (unsigned char *)retMsg;
   tempBuf   += encodedSize; 
   retMsgPtr += encodedSize;

   /* 
    *  encode the data of the ret pdu
    */

   retMsg->msgType   = htons(retMsg->msgType);
   retMsg->msgLength = htons(retMsg->msgLength);

   MEM_COPY(tempBuf, 
            retMsgPtr, 
            tempLength);

   return (MPLS_TLVFIXLEN + tempLength);

} /* End: Mpls_encodeLdpRetMsg */


/* 
 * decode:
 */ 
long Mpls_decodeLdpRetMsg 
(
   mplsLdpRetMsgTlv_t  * retMsg, 
   unsigned char              * buff, 
   long                   bufSize,
   unsigned short               tlvLength
)
{
   unsigned char * tempBuf     = buff; /* no change for the buff ptr */
   unsigned char * retMsgPtr;

   if ((long)tlvLength > bufSize)

⌨️ 快捷键说明

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