📄 ldp_nortel.c
字号:
encodedSize = Mpls_encodeLdpTlv(&(chp->baseTlv), tempBuf, MPLS_TLVFIXLEN); if (encodedSize < 0) { return MPLS_ENC_TLVERROR; } chpPtr = (u_char *) chp; tempBuf += encodedSize; chpPtr += encodedSize; /* * 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); return (MPLS_TLVFIXLEN + MPLS_CHPFIXLEN);} /* End: Mpls_encodeLdpChp *//* * decode */int Mpls_decodeLdpChp(mplsLdpChpTlv_t * chp, u_char * buff, int bufSize){ u_char *chpPtr; if (MPLS_CHPFIXLEN > bufSize) { /* not enough data for Chp */ PRINT_ERR("failed decoding hello Chp\n"); return MPLS_DEC_BUFFTOOSMALL; } chpPtr = (u_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 */int Mpls_encodeLdpCsn(mplsLdpCsnTlv_t * csn, u_char * buff, int bufSize){ int encodedSize = 0; u_char *tempBuf = buff; /* no change for the buff ptr */ if (MPLS_CSNFIXLEN + MPLS_TLVFIXLEN > bufSize) { /* not enough room */ return MPLS_ENC_BUFFTOOSMALL; } /* * encode for tlv */ encodedSize = Mpls_encodeLdpTlv(&(csn->baseTlv), tempBuf, bufSize); if (encodedSize < 0) { PRINT_ERR("failed encoding the tlv in hello Csn\n"); return MPLS_ENC_TLVERROR; } tempBuf += encodedSize; csn->seqNumber = htonl(csn->seqNumber); MEM_COPY(tempBuf, (u_char *) & (csn->seqNumber), MPLS_CSNFIXLEN); return (MPLS_CSNFIXLEN + MPLS_TLVFIXLEN);} /* End: Mpls_encodeLdpCsn *//* * decode */int Mpls_decodeLdpCsn(mplsLdpCsnTlv_t * csn, u_char * buff, int bufSize){ u_char *tempBuf = buff; /* no change for the buff ptr */ if (MPLS_CSNFIXLEN > bufSize) { /* not enough data for csn data */ PRINT_ERR("failed decoding hello Csn\n"); return MPLS_DEC_BUFFTOOSMALL; } /* * decode for the rest of the Csn */ MEM_COPY(&(csn->seqNumber), tempBuf, MPLS_CSNFIXLEN); csn->seqNumber = ntohl(csn->seqNumber); return MPLS_CSNFIXLEN;} /* End: Mpls_decodeLdpCsn *//* * Encode for Transport Address TLV *//* * encode */int Mpls_encodeLdpTrAdr(mplsLdpTrAdrTlv_t * trAdr, u_char * buff, int bufSize){ int encodedSize = 0; u_char *tempBuf = buff; /* no change for the buff ptr */ if (MPLS_TRADRFIXLEN + MPLS_TLVFIXLEN > bufSize) { /* not enough room */ return MPLS_ENC_BUFFTOOSMALL; } /* * encode for tlv */ encodedSize = Mpls_encodeLdpTlv(&(trAdr->baseTlv), tempBuf, bufSize); if (encodedSize < 0) { PRINT_ERR("failed encoding the tlv in hello TrAdr\n"); return MPLS_ENC_TLVERROR; } tempBuf += encodedSize; trAdr->address = htonl(trAdr->address); MEM_COPY(tempBuf, (u_char *) & (trAdr->address), MPLS_TRADRFIXLEN); return (MPLS_TRADRFIXLEN + MPLS_TLVFIXLEN);} /* End: Mpls_encodeLdpTrAdr *//* * decode */int Mpls_decodeLdpTrAdr(mplsLdpTrAdrTlv_t * trAdr, u_char * buff, int bufSize){ u_char *tempBuf = buff; /* no change for the buff ptr */ if (MPLS_TRADRFIXLEN > bufSize) { /* not enough data for csn data */ PRINT_ERR("failed decoding hello TrAdr\n"); return MPLS_DEC_BUFFTOOSMALL; } /* * decode for the rest of the TrAdr */ MEM_COPY(&(trAdr->address), tempBuf, MPLS_TRADRFIXLEN); trAdr->address = ntohl(trAdr->address); return MPLS_TRADRFIXLEN;} /* End: Mpls_decodeLdpTrAdr *//* * Encode for KeepAlive Message *//* * encode */int Mpls_encodeLdpKeepAliveMsg (mplsLdpKeepAlMsg_t * keepAlive, u_char * buff, int bufSize) { mplsLdpKeepAlMsg_t keepAliveCopy; int encodedSize = 0; if (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN > bufSize) { PRINT_ERR("failed to encode the keep alive msg: BUFFER TOO SMALL\n"); return MPLS_ENC_BUFFTOOSMALL; } keepAliveCopy = *keepAlive; encodedSize = Mpls_encodeLdpBaseMsg(&(keepAliveCopy.baseMsg), buff, bufSize); if (encodedSize < 0) { return MPLS_ENC_BASEMSGERROR; } PRINT_OUT("Encode BaseMsg for keep alive on %d bytes\n", encodedSize); return (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);} /* End: Mpls_encodeLdpKeepAliveMsg *//* * decode */int Mpls_decodeLdpKeepAliveMsg (mplsLdpKeepAlMsg_t * keepAlive, u_char * buff, int bufSize) { int decodedSize = 0; memset(keepAlive, 0, MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN); decodedSize = Mpls_decodeLdpBaseMsg(&(keepAlive->baseMsg), buff, bufSize); if (decodedSize < 0) { return MPLS_DEC_BASEMSGERROR; } PRINT_OUT("Decode BaseMsg for keep alive on %d bytes\n", decodedSize); if (keepAlive->baseMsg.flags.flags.msgType != MPLS_KEEPAL_MSGTYPE) { PRINT_ERR("Not the right message type; expected keep alive and got %x\n", keepAlive->baseMsg.flags.flags.msgType); return MPLS_MSGTYPEERROR; } return (MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN);} /* End: Mpls_decodeLdpKeepAliveMsg *//* * Encode for Address List TLV *//* * encode */int Mpls_encodeLdpAdrTlv(mplsLdpAdrTlv_t * adrList, u_char * buff, int bufSize){ int i, numberAdr; int encodedSize = 0; u_char *tempBuf = buff; /* no change for the buff ptr */ u_short tempLength; /* to store the tlv length for later use */ if (MPLS_TLVFIXLEN + (int)(adrList->baseTlv.length) > bufSize) { /* not enough room */ return MPLS_ENC_BUFFTOOSMALL; } tempLength = adrList->baseTlv.length; /* * encode for tlv */ encodedSize = Mpls_encodeLdpTlv(&(adrList->baseTlv), tempBuf, bufSize); if (encodedSize < 0) { PRINT_ERR("failed encoding the tlv in AdrList\n"); return MPLS_ENC_TLVERROR; } tempBuf += encodedSize; adrList->addrFamily = htons(adrList->addrFamily); numberAdr = (tempLength - sizeof(u_short)) / sizeof(u_int); for (i = 0; i < numberAdr; i++) { adrList->address[i] = htonl(adrList->address[i]); } MEM_COPY(tempBuf, (u_char *) & adrList->addrFamily, sizeof(u_short)); tempBuf += sizeof(u_short); MEM_COPY(tempBuf, (u_char *) & adrList->address, tempLength - sizeof(u_short)); return (tempLength + MPLS_TLVFIXLEN);} /* End: Mpls_encodeLdpAdrTlv *//* * decode * * Note: the tlvLength is used to specify what is the length of the * encoding in the AdrTlv. */int Mpls_decodeLdpAdrTlv (mplsLdpAdrTlv_t * adrList, u_char * buff, int bufSize, u_short tlvLength) { u_char *tempBuf = buff; /* no change for the buff ptr */ int i, numberAdr; if ((int)tlvLength > bufSize) { /* not enough data for Adr list tlv */ PRINT_ERR("failed decoding AddrList tlv\n"); return MPLS_DEC_BUFFTOOSMALL; } /* * decode for the addressFamily and addresses of the address list */ MEM_COPY((u_char *) & adrList->addrFamily, tempBuf, sizeof(u_short)); tempBuf += sizeof(u_short); adrList->addrFamily = ntohs(adrList->addrFamily); MEM_COPY((u_char *) & adrList->address, tempBuf, tlvLength - sizeof(u_short)); numberAdr = (tlvLength - sizeof(u_short)) / sizeof(u_int); for (i = 0; i < numberAdr; i++) { adrList->address[i] = ntohl(adrList->address[i]); } return tlvLength;} /* End: Mpls_decodeLdpAdrTlv *//* * Encode for Address / Address Withdraw messages *//* * encode */int Mpls_encodeLdpAdrMsg(mplsLdpAdrMsg_t * addrMsg, u_char * buff, int bufSize){ mplsLdpAdrMsg_t addrMsgCopy; int encodedSize = 0; u_int totalSize = 0; u_char *tempBuf = buff; /* no change for the buff ptr */ /* check the length of the messageId + param */ if ((int)(addrMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) { PRINT_ERR("failed to encode the address msg: BUFFER TOO SMALL\n"); return MPLS_ENC_BUFFTOOSMALL; } addrMsgCopy = *addrMsg; /* * encode the base part of the pdu message */ encodedSize = Mpls_encodeLdpBaseMsg(&(addrMsgCopy.baseMsg), tempBuf, bufSize); if (encodedSize < 0) { return MPLS_ENC_BASEMSGERROR; } PRINT_OUT("Encode BaseMsg for address on %d bytes\n", encodedSize); tempBuf += encodedSize; totalSize += encodedSize; /* * encode the address list tlv if any */ if (addrMsg->adrListTlvExists) { encodedSize = Mpls_encodeLdpAdrTlv(&(addrMsgCopy.addressList), tempBuf, bufSize - totalSize); if (encodedSize < 0) { return MPLS_ENC_ADRLISTERROR; } PRINT_OUT("Encoded for AddressList Tlv %d bytes\n", encodedSize); } return (addrMsg->baseMsg.msgLength + MPLS_TLVFIXLEN);} /* End: *//* * decode */int Mpls_decodeLdpAdrMsg(mplsLdpAdrMsg_t * addrMsg, u_char * buff, int bufSize){ int decodedSize = 0; u_int totalSize = 0; u_int stopLength = 0; u_int totalSizeParam = 0; u_char *tempBuf = buff; /* no change for the buff ptr */ mplsLdpTlv_t tlvTemp; /* * decode the base part of the pdu message */ memset(addrMsg, 0, sizeof(mplsLdpAdrMsg_t)); decodedSize = Mpls_decodeLdpBaseMsg(&(addrMsg->baseMsg), tempBuf, bufSize); if (decodedSize < 0) { return MPLS_DEC_BASEMSGERROR; } PRINT_OUT("Decode BaseMsg for address msg on %d bytes\n", decodedSize); if ((addrMsg->baseMsg.flags.flags.msgType != MPLS_ADDR_MSGTYPE) && (addrMsg->baseMsg.flags.flags.msgType != MPLS_ADDRWITH_MSGTYPE)) { PRINT_ERR("Not the right message type; expected adr and got %x\n", addrMsg->baseMsg.flags.flags.msgType); return MPLS_MSGTYPEERROR; } tempBuf += decodedSize; totalSize += decodedSize; if (bufSize - totalSize <= 0) { /* nothing left for decoding */ PRINT_ERR("Adr msg does not have anything beside base msg\n"); return totalSize; } PRINT_OUT("bufSize = %d, totalSize = %d, addrMsg->baseMsg.msgLength = %d\n", bufSize, totalSize, addrMsg->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 = addrMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN; while (stopLength > totalSizeParam) { /* * decode the tlv to check what's next */ memset(&tlvTemp, 0, MPLS_TLVFIXLEN); decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize); if (decodedSize < 0) { /* something wrong */ PRINT_ERR("ADR msg decode failed for tlv\n"); return MPLS_DEC_TLVERROR; } tempBuf += decodedSize; totalSize += decodedSize; totalSizeParam += decodedSize; switch (tlvTemp.flags.flags.tBit) { case MPLS_ADDRLIST_TLVTYPE: { decodedSize = Mpls_decodeLdpAdrTlv(&(addrMsg->addressList), tempBuf, bufSize - totalSize, tlvTemp.length); if (decodedSize < 0) { PRINT_ERR("Failure when decoding AdrList tlv from adr msg\n"); return MPLS_DEC_ADRLISTERROR; } PRINT_OUT("Decoded for ADRLIST %d bytes\n", decodedSize); tempBuf += decodedSi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -