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

📄 encdec.c

📁 技术文件名称:MPLSv1.0软件模块测试规程
💻 C
📖 第 1 页 / 共 5 页
字号:
		        PRINT_ERR("Failure when decoding Asp from init msg\n");
	            return decodedSize;
              }
/*       PRINT_2("Decoded for ASP %d bytes\n", decodedSize); */
              tempBuf        += decodedSize;
              totalSize      += decodedSize;
              totalSizeParam += decodedSize;
	          initMsg->aspExists   = 1;
	          initMsg->asp.baseTlv = tlvTemp;
	          if(initMsg->asp.flags.flags.numLblRng * MPLS_ATMLRGFIXLEN !=
			         initMsg->asp.baseTlv.length - MPLS_ASPFIXLEN)
			   {
			        return MPLS_TLV_VAL_ERROR;
			   }
			  if(initMsg->asp.baseTlv.length != decodedSize)
			  {
			  	return MPLS_TLV_LENGTH_ERROR;
				}
				
	          DEBUG_CALL(printAspTlv(&(initMsg->asp)));
	          break;
	    }
        case MPLS_FSP_TLVTYPE:
	    {
              decodedSize = Mpls_decodeLdpFsp( &(initMsg->fsp), 
				                                tempBuf, 
				                                bufSize-totalSize
				                              );
              if (decodedSize < 0)
              {
		        PRINT_ERR("Failure when decoding Fsp from init msg\n");
	            return decodedSize;
              }
              tempBuf        += decodedSize;
              totalSize      += decodedSize;
              totalSizeParam += decodedSize;
	          initMsg->fspExists   = 1;
	          initMsg->fsp.baseTlv = tlvTemp;
	          if(initMsg->fsp.flags.flags.numLblRng * MPLS_ATMLRGFIXLEN !=
		         initMsg->fsp.baseTlv.length - MPLS_ASPFIXLEN)
			   {
			        return MPLS_TLV_VAL_ERROR;
			   }
			   if(initMsg->fsp.baseTlv.length != decodedSize)
			   {
			   		return MPLS_TLV_LENGTH_ERROR;
				}
			
	          DEBUG_CALL(printFspTlv(&(initMsg->fsp)));
	          break;
	    }
        default:
	    {
	      PRINT_ERR_2("Found wrong tlv type while decoding init msg (%d)\n",
	             tlvTemp.flags.flags.type);
	      if (tlvTemp.flags.flags.uBit == 1)
	      {
	      		if(tlvTemp.flags.flags.fBit == 1)
	      		{
	      			decodedSize = Mpls_decodeLdpUnknownTlv(	initMsg->baseTlv,
	      										tlvTemp,
	      										initMsg->unknownValue,
	      										&initMsg->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_decodeLdpInitMsg is %d\n", totalSize);
   if (stopLength != totalSizeParam ) return MPLS_MSG_LENGTH_ERROR;
   return totalSize;

} /* End: Mpls_decodeLdpInitMsg */



/*
 *      Encode-decode for Fr Label Range 
 */

/* 
 * encode
 */ 
long Mpls_encodeLdpFrLblRng 
(
   mplsLdpFrLblRng_t * frLabel, 
   unsigned char            * buff, 
   long                 bufSize
)
{
   if (MPLS_FRLRGFIXLEN > bufSize)
   {
      /* not enough room for label */
      return MPLS_ENC_BUFFTOOSMALL; 
   }

   frLabel->flags.flags.res_min = 0;
   frLabel->flags.flags.res_max = 0;
   frLabel->flags.mark[0] = htonl(frLabel->flags.mark[0]);
   frLabel->flags.mark[1] = htonl(frLabel->flags.mark[1]);

   MEM_COPY(buff, (unsigned char *)frLabel, MPLS_FRLRGFIXLEN);

   return MPLS_FRLRGFIXLEN;

} /* End: Mpls_encodeLdpFrLblRng */



/* 
 * decode
 */ 
long Mpls_decodeLdpFrLblRng 
(
   mplsLdpFrLblRng_t * frLabel, 
   unsigned char            * buff, 
   long                 bufSize
)
{
   if (MPLS_FRLRGFIXLEN > bufSize)
   {
      PRINT_ERR("failed decoding the Fr Lbl Rng\n");
      return MPLS_TLV_VAL_ERROR; 
   }

   MEM_COPY((unsigned char *)frLabel, buff, MPLS_FRLRGFIXLEN);

   frLabel->flags.mark[0] = ntohl(frLabel->flags.mark[0]);
   frLabel->flags.mark[1] = ntohl(frLabel->flags.mark[1]);

   return MPLS_FRLRGFIXLEN;

} /* End: Mpls_decodeLdpFrLblRng */




/*
 *      Encode-decode for NOTIFICATION msg 
 */

/* 
 * encode for notification message 
 */ 
long Mpls_encodeLdpNotMsg
(
   mplsLdpNotifMsg_t * notMsg, 
   unsigned char            * buff, 
   long                 bufSize
)
{
/*
   mplsLdpNotifMsg_t notMsgCopy;
*/
   unsigned long             encodedSize = 0;
   unsigned long             totalSize   = 0;
   unsigned char *          tempBuf     = buff; /* no change for the buff ptr */

   /* check the length of the messageId + mandatory param +
      optional param */
   	tempBuf += (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);
   	bufSize -= (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);
   	/*
   if ((long)(notMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) 
   {
      PRINT_ERR("failed to encode the not msg: BUFFER TOO SMALL\n");
      return MPLS_ENC_BUFFTOOSMALL;
   }
	*/
/*
   notMsgCopy = *notMsg;
*/
   memcpy(&notMsgCopy,notMsg,sizeof(mplsLdpNotifMsg_t));
   if (notMsgCopy.statusTlvExists)
   {
      encodedSize = Mpls_encodeLdpStatus( &(notMsgCopy.status), 
				                          tempBuf, 
				                          bufSize-totalSize
				                         );
      if (encodedSize < 0)
      {
	    return MPLS_ENC_STATUSERROR;
      }
      PRINT_2("Encoded for STATUS %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if (notMsgCopy.exStatusTlvExists)
   {
      encodedSize = Mpls_encodeLdpExStatus( &(notMsgCopy.exStatus), 
				                            tempBuf, 
				                            bufSize-totalSize
				                           );
      if (encodedSize < 0)
      {
	    return MPLS_ENC_EXSTATERROR;
      }
      PRINT_2("Encoded for EXTENDED STATUS %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if (notMsgCopy.retPduTlvExists)
   {
      encodedSize = Mpls_encodeLdpRetPdu( &(notMsgCopy.retPdu), 
				          tempBuf, 
				          bufSize-totalSize);
      if (encodedSize < 0)
      {
	    return MPLS_ENC_RETPDUERROR;
      }
      PRINT_2("Encoded for RET PDU %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if (notMsgCopy.retMsgTlvExists)
   {
      encodedSize = Mpls_encodeLdpRetMsg( &(notMsgCopy.retMsg), 
				                          tempBuf, 
				                          bufSize-totalSize
				                         );
      if (encodedSize < 0)
      {
	    return MPLS_ENC_RETMSGERROR;
      }
      PRINT_2("Encoded for RET MSG %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if (notMsgCopy.lspidTlvExists)
   {
      encodedSize = Mpls_encodeLdpLspIdTlv( &(notMsgCopy.lspidTlv), 
				                            tempBuf, 
				                            bufSize-totalSize
				                           );
      if (encodedSize < 0)
      {
         return MPLS_ENC_LSPIDERROR;
      }
      PRINT_2("Encoded for LSPID Tlv %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if (notMsgCopy.lblMsgIdTlvExists)
   {
      encodedSize = Mpls_encodeLdpLblMsgIdTlv( &(notMsgCopy.lblMsgIdTlv), 
				                            tempBuf, 
				                            bufSize-totalSize
				                           );
      if (encodedSize < 0)
      {
         return MPLS_ENC_LBLMSGIDERROR;
      }
      PRINT_2("Encoded for LSPID Tlv %d bytes\n", encodedSize);
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   if(notMsgCopy.unknownNo > 0)
   {
   		encodedSize = Mpls_encodeLdpUnknownTlv( (notMsgCopy.baseTlv),
   												notMsgCopy.unknownValue,
   												notMsgCopy.unknownNo,
   												tempBuf,
   												bufSize-totalSize
   											   );
  		if (encodedSize < 0)
  		{
  			return MPLS_ENC_UNKERROR;
  		}
  		tempBuf   += encodedSize;
      	totalSize += encodedSize;
   }
	/*
    *  encode the base part of the pdu message
    */
    notMsgCopy.baseMsg.msgLength		=	totalSize;
   encodedSize = Mpls_encodeLdpBaseMsg( &(notMsgCopy.baseMsg), 
				                        buff, 
				                        (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN)
				                       );
   if (encodedSize < 0)
   {
      return MPLS_ENC_BASEMSGERROR;
   }
   PRINT_2("Encode BaseMsg for not on %d bytes\n", encodedSize);
   totalSize += encodedSize;
   return totalSize;

} /* End: Mpls_encodeLdpNotMsg */


/* 
 * decode for notification message 
 */ 
long Mpls_decodeLdpNotMsg
(
   mplsLdpNotifMsg_t * notMsg, 
   unsigned char            * buff, 
   long                 bufSize
)
{
   unsigned long           decodedSize    = 0;
   unsigned long           totalSize      = 0;
   long           stopLength     = 0;
   unsigned long           totalSizeParam = 0;
   unsigned char *         tempBuf        = buff; /* no change for the buff ptr */
   mplsLdpTlv_t    tlvTemp;

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

   if (notMsg->baseMsg.flags.flags.msgType != MPLS_NOT_MSGTYPE)
   {
      PRINT_ERR_2("Not the right message type; expected not and got %x\n",
		            notMsg->baseMsg.flags.flags.msgType);
      return MPLS_MSGTYPEERROR;
   }

   tempBuf   += decodedSize;
   totalSize += decodedSize;

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

   PRINT_4("bufSize = %d,  totalSize = %d, notMsg->baseMsg.msgLength = %d\n",
	        bufSize, totalSize, notMsg->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 = notMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;
   if ( stopLength < MPLS_TLVFIXLEN )	return MPLS_MSG_LENGTH_ERROR;
   while (stopLength > totalSizeParam)
   {
      /*
       *  decode the tlv to check what's next
       */
      bzero(&tlvTemp, MPLS_TLVFIXLEN);
      decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize-totalSize);
      if (decodedSize < 0)
      {
         /* something wrong */
         PRINT_ERR("NOT msg decode failed for tlv\n");
         return decodedSize;
      }

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

      switch (tlvTemp.flags.flags.type)
      {
         case MPLS_NOT_ST_TLVTYPE:
         {
            decodedSize = Mpls_decodeLdpStatus( &(notMsg->status), 
				                                tempBuf, 
				                                bufSize-totalSize
				                               );
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when decoding Status from not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for STATUS %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

	        notMsg->statusTlvExists = 1;
	        notMsg->status.baseTlv  = tlvTemp;
	        DEBUG_CALL(printStatusTlv(&(notMsg->status)));
	        break;
         }
         case MPLS_NOT_ES_TLVTYPE:
	    {
            decodedSize = Mpls_decodeLdpExStatus( &(notMsg->exStatus), 
				                  tempBuf, 
				                  bufSize-totalSize);
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when decoding Extended Status from not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for EX_STATUS %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

	        notMsg->exStatusTlvExists = 1;
	        notMsg->exStatus.baseTlv  = tlvTemp;
	        DEBUG_CALL(printExStatusTlv(&(notMsg->exStatus)));
	        break;
	    }
         case MPLS_NOT_RP_TLVTYPE:
	    {
            decodedSize = Mpls_decodeLdpRetPdu( &(notMsg->retPdu), 
				                tempBuf, 
				                bufSize-totalSize,
						        tlvTemp.length
						       );
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when decoding Returned PDU from not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for RET_PDU %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

	        notMsg->retPduTlvExists = 1;
	        notMsg->retPdu.baseTlv  = tlvTemp;
	        DEBUG_CALL(printRetPduTlv(&(notMsg->retPdu)));
	        break;
	    }
         case MPLS_NOT_RM_TLVTYPE:
	    {
            decodedSize = Mpls_decodeLdpRetMsg( &(notMsg->retMsg), 
				                                tempBuf, 
				                                bufSize-totalSize,
						                        tlvTemp.length
						                       );
            if (decodedSize < 0)
            {
	            PRINT_ERR("Failure when decoding Returned MSG from not msg\n");
	            return decodedSize;
            }
	        PRINT_2("Decoded for RET_MSG %d bytes\n", decodedSize);
            tempBuf        += decodedSize;
            totalSize      += decodedSize;
            totalSizeParam += decodedSize;

⌨️ 快捷键说明

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