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

📄 encdec.c

📁 技术文件名称:MPLSv1.0软件模块测试规程
💻 C
📖 第 1 页 / 共 5 页
字号:
/********************************************************************/          
/*	Product Name:	ZXB10                                       	*/          
/*	Module  Name:	A075    										*/
/*	File    Name:   Encdec.c							        	*/          
/*	Author  Name:   Jing Xiaoyuan, Li Jianping						*/          
/*	Creat   Date:	12/20/99										*/
/*	Version		:	1.0                                         	*/          
/*	Function    :	所有的编码和解码函数                             */ 
/* wengqing add lspidTlv in abort msg encode and decode 2003.1.16 */
/********************************************************************/    


#include "Encdec.h"


/*
 *      Encode-decode for Ldp Msg Header 
 */

/*****************************************************************************/
/* for stack 
*/
/*****************************************************************************/
union 
{
	mplsLdpHelloMsg_t       helloMsg;
	mplsLdpNotifMsg_t		notifMsg;
	mplsLdpInitMsg_t		initMsg;		
	mplsLdpKeepAlMsg_t		keepAliveMsg;	
	mplsLdpAdrMsg_t			addrMsg;
    mplsLdpAdrMsg_t			addrWithMsg;
	mplsLdpLblReqMsg_t		lblReqMsg;		
	mplsLdpLblMapMsg_t		lblMapMsg;		
	mplsLdpLbl_W_R_Msg_t	lblRelMsg;
    mplsLdpLbl_W_R_Msg_t	lblWithMsg;	
	mplsLdpLblAbortMsg_t	lblAbortMsg;	
}ldp_encdec_msg;

#define helloMsgCopy	(ldp_encdec_msg.helloMsg)
#define notMsgCopy		(ldp_encdec_msg.notifMsg)
#define initMsgCopy		(ldp_encdec_msg.initMsg)
#define keepAliveCopy	(ldp_encdec_msg.keepAliveMsg)
#define addrMsgCopy		(ldp_encdec_msg.addrMsg)
#define lblReqMsgCopy	(ldp_encdec_msg.lblReqMsg)
#define lblMapMsgCopy	(ldp_encdec_msg.lblMapMsg)
#define lbl_W_R_MsgCopy	(ldp_encdec_msg.lblRelMsg)
#define lblAbortMsgCopy	(ldp_encdec_msg.lblAbortMsg)
/*
#define initMsgCopy		(ldp_encdec_msg.lblWithMsg)
#define initMsgCopy		(ldp_encdec_msg.addrWithMsg)
*/
/*****************************************************************************/
/* for stack end
*/
/*****************************************************************************/

/* 
 * Encode:
 */
long Mpls_encodeLdpMsgHeader
(
   mplsLdpHeader_t * header, 
   unsigned char          * buff, 
   long               bufSize
)
{
   mplsLdpHeader_t headerCopy;  

   if (MPLS_LDP_HDRSIZE > bufSize)
   {
      /* not enough room for header */
      return MPLS_ENC_BUFFTOOSMALL; 
   }

   headerCopy                 = *header;
   headerCopy.protocolVersion = htons(headerCopy.protocolVersion);
   headerCopy.pduLength       = htons(headerCopy.pduLength);
   headerCopy.lsrAddress      = htonl(headerCopy.lsrAddress);
   headerCopy.labelSpace      = htons(headerCopy.labelSpace);

   MEM_COPY(buff, (unsigned char *)&headerCopy, MPLS_LDP_HDRSIZE);

   return MPLS_LDP_HDRSIZE;

} /* End : Mpls_encodeLdpMsgHeader */

/* 
 * Decode: 
 */
long Mpls_decodeLdpMsgHeader
(
   mplsLdpHeader_t * header, 
   unsigned char          * buff, 
   long               bufSize
)
{
   if (MPLS_LDP_HDRSIZE > bufSize)
   {
      return MPLS_PDU_LENGTH_ERROR; 
   }

   MEM_COPY((unsigned char *)header, buff, MPLS_LDP_HDRSIZE);

   header->protocolVersion = ntohs(header->protocolVersion);
   header->pduLength       = ntohs(header->pduLength);
   header->lsrAddress      = ntohl(header->lsrAddress);
   header->labelSpace      = ntohs(header->labelSpace);

   /* check if the length is over the max length */
   if (header->pduLength > MPLS_PDUMAXLEN)
   { 
      return MPLS_PDU_LENGTH_ERROR;
   }

   return MPLS_LDP_HDRSIZE;

} /* End: Mpls_decodeLdpMsgHeader */




/*
 *      Encode-decode for Ldp Base Message
 */

/* 
 * Encode:
 */
long Mpls_encodeLdpBaseMsg
(
   mplsLdpMsg_t * ldpMsg, 
   unsigned char       * buff, 
   long            bufSize
)
{
   if (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN > bufSize)
   {
      /* not enough room for header */
      return MPLS_ENC_BUFFTOOSMALL; 
   }
	ldpMsg->msgLength +=	MPLS_MSGIDFIXLEN;
   ldpMsg->flags.mark = htons(ldpMsg->flags.mark);
   ldpMsg->msgLength  = htons(ldpMsg->msgLength);
   ldpMsg->msgId      = htonl(ldpMsg->msgId);

   MEM_COPY(buff, (unsigned char *)ldpMsg, MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);

   return (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);

} /* End : Mpls_encodeLdpBaseMsg*/

/* 
 * Decode: 
 */
long Mpls_decodeLdpBaseMsg 
(
   mplsLdpMsg_t * ldpMsg, 
   unsigned char       * buff, 
   long            bufSize
)
{
   if (MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN > bufSize)
   {
      return MPLS_MSG_LENGTH_ERROR; 
   }

   MEM_COPY((unsigned char *)ldpMsg, buff, MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN);

   ldpMsg->flags.mark = ntohs(ldpMsg->flags.mark);
   ldpMsg->msgLength  = ntohs(ldpMsg->msgLength);
   ldpMsg->msgId      = ntohl(ldpMsg->msgId);
   return MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN;

} /* End: Mpls_decodeLdpBaseMsg */


/* encode - decode for ldp unknown tlv */
long Mpls_encodeLdpUnknownTlv ( struct mplsLdpTlv_s *baseTlv,
                                unsigned char * unknownBuff,
                                unsigned char    unknownNo, 
                                unsigned char * buff, 
                                long bufSize)
{
    long encodedSize, totalSize;
    unsigned char * tempBuff;
    int i ;
    tempBuff = buff;
    totalSize =0;
    for ( i = 0; i < unknownNo ; i++ )
    {
        encodedSize = Mpls_encodeLdpTlv(&(baseTlv[i]),buff,bufSize);
        if( encodedSize < 0 )
        {
            printf("====encode unknown TLV error====\n");
            return MPLS_ENC_TLVERROR;
        }
        tempBuff    +=  encodedSize;
        totalSize   +=  encodedSize;
        MEM_COPY( tempBuff, 
	              unknownBuff,
                  baseTlv[i].length );
        tempBuff    +=  baseTlv[i].length;
        totalSize   +=  baseTlv[i].length;
    }
    return totalSize;
}

long Mpls_decodeLdpUnknownTlv( struct mplsLdpTlv_s *baseTlv,
							   struct mplsLdpTlv_s tlvTemp,
                               unsigned char * unknownBuff,
                               unsigned char *  unknownNo,
                               unsigned char * buff,
                               long bufSize)
{
	int i = 0, offset = 0;
	struct mplsLdpTlv_s tempBaseTlv;
	if (tlvTemp.length > bufSize)
   	{
		PRINT_ERR("failed decoding the unknown tlv \n");
		return MPLS_TLV_LENGTH_ERROR; 
   	}
   	for(i = 0; i< *unknownNo; i++)
   	{
   		tempBaseTlv = *((mplsLdpTlv_t *)(baseTlv + sizeof(mplsLdpTlv_t)*i));
   		offset += tempBaseTlv.length;
   	}
    if(tlvTemp.flags.flags.fBit == 1)
    {
        MEM_COPY( baseTlv+sizeof(mplsLdpTlv_t)*(*unknownNo), 
	              &tlvTemp,
                  sizeof(mplsLdpTlv_t) );
        MEM_COPY( unknownBuff+offset, 
	              buff,
                  tlvTemp.length );
        (*unknownNo) ++;   
    }
    return tlvTemp.length;
}
/* End Mpls_decodeLdpUnknownTlv */
/*
 *      Encode-decode for ATM Label Range Component
 */

/* 
 * encode: 
 */ 
long Mpls_encodeLdpAtmLblRng
(
   mplsLdpAtmLblRng_t * atmLbl, 
   unsigned char             * buff, 
   long                  bufSize
)
{
   if (MPLS_ATMLRGFIXLEN > bufSize)
   {
      /* not enough room for label */
      return MPLS_ENC_BUFFTOOSMALL; 
   }

   atmLbl->flags.flags.res1 = 0;
   atmLbl->flags.flags.res2 = 0;
   atmLbl->flags.mark[0] = htonl(atmLbl->flags.mark[0]);
   atmLbl->flags.mark[1] = htonl(atmLbl->flags.mark[1]);

   MEM_COPY(buff, (unsigned char *)atmLbl, MPLS_ATMLRGFIXLEN);

   return MPLS_ATMLRGFIXLEN;
   
} /* End Mpls_encodeLdpAtmLblRng */


/* 
 * decode: 
 */ 
long Mpls_decodeLdpAtmLblRng
(
   mplsLdpAtmLblRng_t * atmLbl, 
   unsigned char             * buff, 
   long                  bufSize
)
{
   if (MPLS_ATMLRGFIXLEN > bufSize)
   {
      PRINT_ERR("failed decoding the Atm Lbl Rng\n");
      return MPLS_TLV_VAL_ERROR; 
   }

   MEM_COPY((unsigned char *)atmLbl, buff, MPLS_ATMLRGFIXLEN);

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

   return MPLS_ATMLRGFIXLEN;

} /* End Mpls_decodeLdpAtmLblRng */




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

/* 
 * encode: 
 */ 
long Mpls_encodeLdpAsp
(
   mplsLdpAspTlv_t * atmAsp, 
   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 atmAsp to be encoded and check it against
      the buffer size */

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

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

   /* 
    *  encode for ATM labels 
    */
   for (i = 0; i < numLblRng; i++)
   {
      encodedSize = Mpls_encodeLdpAtmLblRng( &(atmAsp->lblRngList[i]),
		                             tempBuf,
		                             bufSize-totalSize);
      if (encodedSize < 0)
      {
	 return MPLS_ENC_ATMLBLERROR;
      }
      tempBuf   += encodedSize;
      totalSize += encodedSize;
   }
   atmAsp->baseTlv.length = totalSize;
	/* 
    *  encode for tlv 
    */
   encodedSize = Mpls_encodeLdpTlv( &(atmAsp->baseTlv),
                                    buff, 
				    MPLS_TLVFIXLEN);
   if (encodedSize < 0)
   {
      return MPLS_ENC_TLVERROR;
   }
//   tempBuf   += encodedSize; 
   totalSize += encodedSize;
   return totalSize; 
   
} /* End Mpls_encodeLdpAsp */


/* 
 * decode: 
 */ 
long Mpls_decodeLdpAsp
(
   mplsLdpAspTlv_t * atmAsp, 
   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_ASPFIXLEN > bufSize) // TLV Type + Len 已解开? wengqing 2k/11/24
	if ( (MPLS_ASPFIXLEN > bufSize)||(atmAsp->baseTlv.length > bufSize) )
   {
      /* the buffer does not contain even the required field*/
      PRINT_ERR("failed in decoding LdpAsp\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

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

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

   /*
    *  decode for ATM labels 
    */
   for (i = 0; i < atmAsp->flags.flags.numLblRng; i++)
   {
      decodedSize = Mpls_decodeLdpAtmLblRng( &(atmAsp->lblRngList[i]),
		                             tempBuf,
			                     bufSize - totalSize);
      if (decodedSize < 0)
      {
         PRINT_ERR_2("failed in decoding LdpAtmLabel[%d] for LdpAsp\n", i);
	    return decodedSize;
      }
      tempBuf   += decodedSize;
      totalSize += decodedSize;
   }

   return  totalSize; 

} /* End Mpls_decodeLdpAsp */



/*
 *      Encode-decode for TLV
 */

/* 
 * encode: 
 */ 
long Mpls_encodeLdpTlv 
(
   mplsLdpTlv_t * tlv, 
   unsigned char       * buff, 
   long            bufSize
)
{
   if (MPLS_TLVFIXLEN > bufSize)
   {
      /* not enough room for label */
      return MPLS_ENC_BUFFTOOSMALL; 
   }
   
   tlv->flags.mark = htons(tlv->flags.mark); 
   tlv->length     = htons(tlv->length); 

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

   return MPLS_TLVFIXLEN;

} /* End: Mpls_encodeLdpTlv */


/* 
 * decode: 
 */ 
long Mpls_decodeLdpTlv 
(
   mplsLdpTlv_t * tlv, 
   unsigned char       * buff, 
   long            bufSize
)
{
   if (MPLS_TLVFIXLEN > bufSize)
   {
      PRINT_ERR("Failed decoding TLV\n");
      return MPLS_TLV_LENGTH_ERROR; 
   }

⌨️ 快捷键说明

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