📄 encdec.c
字号:
{
/* not enough data for ExStatus*/
PRINT_ERR("failed decoding Ret msg\n");
return MPLS_TLV_LENGTH_ERROR;
}
retMsgPtr = (unsigned char *)retMsg;
retMsgPtr += MPLS_TLVFIXLEN;
/*
* decode data for ret msg
*/
MEM_COPY( retMsgPtr,
tempBuf,
tlvLength);
retMsg->msgType = ntohs(retMsg->msgType);
retMsg->msgLength = ntohs(retMsg->msgLength);
return tlvLength;
} /* End: Mpls_decodeLdpRetMsg */
/*
* Encode-decode for HELLO msg
*/
/*
* encode for HELLO message
*/
/* Hello Message Enc & Dec not handle Ipv6TrAddress Tlv Now */
long Mpls_encodeLdpHelloMsg
(
mplsLdpHelloMsg_t * helloMsg,
unsigned char * buff,
long bufSize
)
{
/*
mplsLdpHelloMsg_t helloMsgCopy;
*/
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)(helloMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize)
{
PRINT_ERR("failed to encode the hello msg: BUFFER TOO SMALL\n");
return MPLS_ENC_BUFFTOOSMALL;
}
*/
memcpy(&helloMsgCopy,helloMsg,sizeof(mplsLdpHelloMsg_t));
/*
* encode the status tlv if any
*/
if (helloMsgCopy.chpTlvExists)
{
encodedSize = Mpls_encodeLdpChp( &(helloMsgCopy.chp),
tempBuf,
bufSize-totalSize);
if (encodedSize < 0)
{
return MPLS_ENC_CHPERROR;
}
/* PRINT_2("Encoded for CHP %d bytes\n", encodedSize); */
tempBuf += encodedSize;
totalSize += encodedSize;
}
if (helloMsgCopy.trAdrTlvExists)
{
encodedSize = Mpls_encodeLdpTrAdr( &(helloMsgCopy.trAdr),
tempBuf,
bufSize-totalSize
);
if (encodedSize < 0)
{
return MPLS_ENC_TRADRERROR;
}
/* PRINT_2("Encoded for TR ADDR %d bytes\n", encodedSize); */
tempBuf += encodedSize;
totalSize += encodedSize;
}
if (helloMsgCopy.csnTlvExists)
{
encodedSize = Mpls_encodeLdpCsn( &(helloMsgCopy.csn),
tempBuf,
bufSize-totalSize
);
if (encodedSize < 0)
{
return MPLS_ENC_CSNERROR;
}
/* PRINT_2("Encoded for CSN %d bytes\n", encodedSize); */
tempBuf += encodedSize;
totalSize += encodedSize;
}
/*
if(helloMsgCopy.unknownNo > 0)
{
encodedSize = Mpls_encodeLdpUnknownTlv( (helloMsgCopy.baseTlv),
helloMsgCopy.unknownValue,
helloMsgCopy.unknownNo,
tempBuf,
bufSize-totalSize
);
if (encodedSize < 0)
{
return MPLS_ENC_UNKERROR;
}
tempBuf += encodedSize;
totalSize += encodedSize;
}
*/
/*
* encode the base part of the pdu message
*/
helloMsgCopy.baseMsg.msgLength = totalSize;
encodedSize = Mpls_encodeLdpBaseMsg( &(helloMsgCopy.baseMsg),
buff,
(MPLS_MSGIDFIXLEN + MPLS_MSGHDRFIXLEN)
);
if (encodedSize < 0)
{
return MPLS_ENC_BASEMSGERROR;
}
/* PRINT_2("Encode BaseMsg for hello on %d bytes\n", encodedSize); */
totalSize += encodedSize;
return totalSize;
} /* End: Mpls_encodeLdpHelloMsg */
/*
* decode for HELLO message
*/
long Mpls_decodeLdpHelloMsg
(
mplsLdpHelloMsg_t * helloMsg,
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(helloMsg, sizeof(mplsLdpHelloMsg_t));
decodedSize = Mpls_decodeLdpBaseMsg( &(helloMsg->baseMsg),
tempBuf,
bufSize);
if (decodedSize < 0)
{
return decodedSize;
}
/* PRINT_2("Decode BaseMsg for hello on %d bytes\n", decodedSize); */
if (helloMsg->baseMsg.flags.flags.msgType != MPLS_HELLO_MSGTYPE)
{
PRINT_ERR_2("Not the right message type; expected hello and got %x\n",
helloMsg->baseMsg.flags.flags.msgType);
return MPLS_MSGTYPEERROR;
}
tempBuf += decodedSize;
totalSize += decodedSize;
if (bufSize-totalSize <= 0)
{
/* nothing left for decoding */
PRINT_ERR("Hello msg does not have anything beside base msg\n");
return totalSize;
}
/* PRINT_4("bufSize = %d, totalSize = %d, helloMsg->baseMsg.msgLength = %d\n",
bufSize, totalSize, helloMsg->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 = helloMsg->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_CHP_TLVTYPE:
{
decodedSize = Mpls_decodeLdpChp( &(helloMsg->chp),
tempBuf,
bufSize-totalSize
);
if (decodedSize < 0)
{
PRINT_ERR("Failure when decoding Chp from hello msg\n");
return decodedSize;
}
/* PRINT_2("Decoded for CHP %d bytes\n", decodedSize); */
tempBuf += decodedSize;
totalSize += decodedSize;
totalSizeParam += decodedSize;
helloMsg->chpTlvExists = 1;
helloMsg->chp.baseTlv = tlvTemp;
if (helloMsg->chp.baseTlv.length != decodedSize )
{
/* not enough data for Chp */
PRINT_ERR("failed decoding hello Chp\n");
return MPLS_TLV_LENGTH_ERROR;
}
DEBUG_CALL(printChpTlv(&(helloMsg->chp)));
break;
}
case MPLS_IPV4_TRADR_TLVTYPE:
{
decodedSize = Mpls_decodeLdpTrAdr( &(helloMsg->trAdr),
tempBuf,
bufSize-totalSize
);
if (decodedSize < 0)
{
PRINT_ERR("Failure when decoding TrAdr from hello msg\n");
return decodedSize;
}
/* PRINT_2("Decoded for TrAdr %d bytes\n", decodedSize); */
tempBuf += decodedSize;
totalSize += decodedSize;
totalSizeParam += decodedSize;
helloMsg->trAdrTlvExists = 1;
helloMsg->trAdr.baseTlv = tlvTemp;
if (helloMsg->trAdr.baseTlv.length != decodedSize )
{
/* not enough data for Chp */
PRINT_ERR("failed decoding hello Chp\n");
return MPLS_TLV_LENGTH_ERROR;
}
DEBUG_CALL(printTrAdrTlv(&(helloMsg->trAdr)));
break;
}
case MPLS_CSN_TLVTYPE:
{
decodedSize = Mpls_decodeLdpCsn( &(helloMsg->csn),
tempBuf,
bufSize-totalSize
);
if (decodedSize < 0)
{
PRINT_ERR("Failure when decoding Csn from hello msg\n");
return decodedSize;
}
/* PRINT_2("Decoded for CSN %d bytes\n", decodedSize); */
tempBuf += decodedSize;
totalSize += decodedSize;
totalSizeParam += decodedSize;
helloMsg->csnTlvExists = 1;
helloMsg->csn.baseTlv = tlvTemp;
if (helloMsg->csn.baseTlv.length != decodedSize )
{
/* not enough data for Chp */
PRINT_ERR("failed decoding hello Chp\n");
return MPLS_TLV_LENGTH_ERROR;
}
DEBUG_CALL(printCsnTlv(&(helloMsg->csn)));
break;
}
default:
{
PRINT_ERR_2("Found wrong tlv type while decoding hello msg (%d)\n",
tlvTemp.flags.flags.type);
if (tlvTemp.flags.flags.uBit == 1)
{
if(tlvTemp.flags.flags.fBit == 1)
{/*
decodedSize = Mpls_decodeLdpUnknownTlv( helloMsg->baseTlv,
tlvTemp,
helloMsg->unknownValue,
&helloMsg->unknownNo,
tempBuf,
bufSize-totalSize
);
if(decodedSize < 0)
{
PRINT_ERR("Failure when decoding unknown from init msg\n");
return MPLS_DEC_UNKERROR;
}
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;
}
}/* case */
} /* switch type */
} /* while */
PRINT_2("totalsize for Mpls_decodeLdpHelloMsg is %d\n", totalSize);
if (stopLength != totalSizeParam ) return MPLS_MSG_LENGTH_ERROR;
return totalSize;
} /* End: Mpls_decodeLdpHelloMsg */
/*
* Encode for Common Hello Parameters TLV
*/
/*
* encode
*/
long Mpls_encodeLdpChp
(
mplsLdpChpTlv_t * chp,
unsigned char * buff,
long bufSize
)
{
long encodedSize = 0;
unsigned char * tempBuf = buff; /* no change for the buff ptr */
unsigned char * chpPtr;
/* get the size of the chp to be encoded and check it against
the buffer size */
if (MPLS_TLVFIXLEN + MPLS_CHPFIXLEN > bufSize)
{
/* not enough room */
return MPLS_ENC_BUFFTOOSMALL;
}
chpPtr = (unsigned char *)chp;
tempBuf += MPLS_TLVFIXLEN;
chpPtr += MPLS_TLVFIXLEN;
/*
* encode for hold time + T + R + res
*/
chp->flags.flags.res = 0;
chp->flags.mark = htons(chp->flags.mark);
chp->holdTime = htons(chp->holdTime);
MEM_COPY( tempBuf,
chpPtr,
MPLS_CHPFIXLEN
);
/*
* encode for tlv
*/
encodedSize = Mpls_encodeLdpTlv( &(chp->baseTlv),
buff,
MPLS_TLVFIXLEN
);
if (encodedSize < 0)
{
return MPLS_ENC_TLVERROR;
}
return (MPLS_TLVFIXLEN + MPLS_CHPFIXLEN);
} /* End: Mpls_encodeLdpChp */
/*
* decode
*/
long Mpls_decodeLdpChp
(
mplsLdpChpTlv_t * chp,
unsigned char * buff,
long bufSize
)
{
unsigned char * chpPtr;
if (MPLS_CHPFIXLEN > bufSize)
{
/* not enough data for Chp */
PRINT_ERR("failed decoding hello Chp\n");
return MPLS_TLV_LENGTH_ERROR;
}
chpPtr = (unsigned char *)chp;
chpPtr += MPLS_TLVFIXLEN; /* we want to point to the flags since the
tlv was decoded before we reach here */
/*
* decode for the rest of the Chp
*/
MEM_COPY( chpPtr,
buff,
MPLS_CHPFIXLEN);
chp->holdTime = ntohs(chp->holdTime);
chp->flags.mark = ntohs(chp->flags.mark);
return MPLS_CHPFIXLEN;
} /* End: Mpls_decodeLdpChp */
/*
* Encode for Configuration Sequence Number TLV
*/
/*
* encode
*/
long Mpls_encodeLdpCsn
(
mplsLdpCsnTlv_t * csn,
unsigned char * buff,
long bufSize
)
{
long encodedSize = 0;
unsigned char * tempBuf = buff; /* no change for the buff ptr */
if (MPLS_CSNFIXLEN + MPLS_TLVFIXLEN > bufSize)
{
/* not enough room */
return MPLS_ENC_BUFFTOOSMALL;
}
csn->baseTlv.length = MPLS_CSNFIXLEN;
/*
* encode for tlv
*/
encodedSize = Mpls_encodeLdpTlv( &(csn->baseTlv),
tempBuf,
bufSize
);
if (encodedSize < 0)
{
PRINT_ERR("failed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -