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

📄 ldp_nortel.c

📁 实现了MPLS中的标签分发协议(LDP 3036 )的基本功能
💻 C
📖 第 1 页 / 共 5 页
字号:
  /*    *  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, (u_char *) & (status->value), sizeof(u_int));  return (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN);}                               /* End: Mpls_encodeLdpExStatus *//*  * decode: */int Mpls_decodeLdpExStatus  (mplsLdpExStatusTlv_t * status, u_char * buff, int bufSize) {  if (MPLS_EXSTATUSLEN > bufSize) {    /* not enough data for ExStatus */    PRINT_ERR("failed decoding ExStatus\n");    return MPLS_DEC_BUFFTOOSMALL;  }  /*    *  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: */int Mpls_encodeLdpRetPdu  (mplsLdpRetPduTlv_t * retPdu, u_char * buff, int bufSize) {  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)(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: */int Mpls_decodeLdpRetPdu  (mplsLdpRetPduTlv_t * retPdu, u_char * buff, int bufSize, u_short tlvLength) {  u_char *tempBuf = buff;       /* no change for the buff ptr */  int decodedSize;  if ((int)tlvLength > bufSize) {    /* not enough data for ExStatus */    PRINT_ERR("failed decoding Ret pdu\n");    return MPLS_DEC_BUFFTOOSMALL;  }  /*    *  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 MPLS_DEC_HDRTLVERROR;  }  tempBuf += decodedSize;  MEM_COPY(retPdu->data, tempBuf, tlvLength);  return tlvLength;}                               /* End: Mpls_decodeLdpRetPdu *//* *      Encode-decode for Return Msg TLV  *//*  * encode: */int Mpls_encodeLdpRetMsg  (mplsLdpRetMsgTlv_t * retMsg, u_char * buff, int bufSize) {  u_char *retMsgPtr;  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)(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 = (u_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: */int Mpls_decodeLdpRetMsg  (mplsLdpRetMsgTlv_t * retMsg, u_char * buff, int bufSize, u_short tlvLength) {  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_char *retMsgPtr;  if ((int)tlvLength > bufSize) {    /* not enough data for ExStatus */    PRINT_ERR("failed decoding Ret msg\n");    return MPLS_DEC_BUFFTOOSMALL;  }  retMsgPtr = (u_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  */int Mpls_encodeLdpHelloMsg  (mplsLdpHelloMsg_t * helloMsg, u_char * buff, int bufSize) {  mplsLdpHelloMsg_t helloMsgCopy;  int encodedSize = 0;  u_int totalSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  /* check the length of the messageId + mandatory param +     optional param */  if ((int)(helloMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {    PRINT_ERR("failed to encode the hello msg: BUFFER TOO SMALL\n");    return MPLS_ENC_BUFFTOOSMALL;  }  helloMsgCopy = *helloMsg;  /*   *  encode the base part of the pdu message   */  encodedSize = Mpls_encodeLdpBaseMsg(&(helloMsgCopy.baseMsg),    tempBuf, bufSize);  if (encodedSize < 0) {    return MPLS_ENC_BASEMSGERROR;  }  PRINT_OUT("Encode BaseMsg for hello on %d bytes\n", encodedSize);  tempBuf += encodedSize;  totalSize += encodedSize;  /*   *  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_OUT("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_OUT("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_OUT("Encoded for CSN %d bytes\n", encodedSize);    tempBuf += encodedSize;    totalSize += encodedSize;  }  return totalSize;}                               /* End: Mpls_encodeLdpHelloMsg *//*  * decode for HELLO message  */int Mpls_decodeLdpHelloMsg  (mplsLdpHelloMsg_t * helloMsg, 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(helloMsg, 0, sizeof(mplsLdpHelloMsg_t));  decodedSize = Mpls_decodeLdpBaseMsg(&(helloMsg->baseMsg), tempBuf, bufSize);  if (decodedSize < 0) {    return MPLS_DEC_BASEMSGERROR;  }  PRINT_OUT("Decode BaseMsg for hello on %d bytes\n", decodedSize);  if (helloMsg->baseMsg.flags.flags.msgType != MPLS_HELLO_MSGTYPE) {    PRINT_ERR("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_OUT("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;  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("NOT msg decode failed for tlv\n");      return MPLS_DEC_TLVERROR;    }    tempBuf += decodedSize;    totalSize += decodedSize;    totalSizeParam += decodedSize;    switch (tlvTemp.flags.flags.tBit) {      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 MPLS_DEC_CHPERROR;          }          PRINT_OUT("Decoded for CHP %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          helloMsg->chpTlvExists = 1;          helloMsg->chp.baseTlv = tlvTemp;          break;        }      case MPLS_TRADR_TLVTYPE:        {          decodedSize = Mpls_decodeLdpTrAdr(&(helloMsg->trAdr),            tempBuf, bufSize - totalSize);          if (decodedSize < 0) {            PRINT_ERR("Failure when decoding TrAdr from hello msg\n");            return MPLS_DEC_TRADRERROR;          }          PRINT_OUT("Decoded for TrAdr %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          helloMsg->trAdrTlvExists = 1;          helloMsg->trAdr.baseTlv = tlvTemp;          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 MPLS_DEC_CSNERROR;          }          PRINT_OUT("Decoded for CSN %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          helloMsg->csnTlvExists = 1;          helloMsg->csn.baseTlv = tlvTemp;          break;        }      default:        {          PRINT_ERR("Found wrong tlv type while decoding hello msg (%d)\n",            tlvTemp.flags.flags.tBit);          if (tlvTemp.flags.flags.uBit == 1) {            /* 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_OUT("totalsize for Mpls_decodeLdpHelloMsg is %d\n", totalSize);  return totalSize;}                               /* End: Mpls_decodeLdpHelloMsg *//*  * Encode for Common Hello Parameters TLV *//* *  encode */int Mpls_encodeLdpChp(mplsLdpChpTlv_t * chp, u_char * buff, int bufSize){  int encodedSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_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;  }  /*    *  encode for tlv    */

⌨️ 快捷键说明

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