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

📄 ldp_nortel.c

📁 实现了MPLS中的标签分发协议(LDP 3036 )的基本功能
💻 C
📖 第 1 页 / 共 5 页
字号:
            PRINT_ERR("Failure when decoding Fsp from init msg\n");            return MPLS_DEC_FSPERROR;          }          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          initMsg->fspExists = 1;          initMsg->fsp.baseTlv = tlvTemp;          break;        }      default:        {          PRINT_ERR("Found wrong tlv type while decoding init 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_decodeLdpInitMsg is %d\n", totalSize);  return totalSize;}                               /* End: Mpls_decodeLdpInitMsg *//* *      Encode-decode for Fr Label Range  *//*  * encode */int Mpls_encodeLdpFrLblRng  (mplsLdpFrLblRng_t * frLabel, u_char * buff, int 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, (u_char *) frLabel, MPLS_FRLRGFIXLEN);  return MPLS_FRLRGFIXLEN;}                               /* End: Mpls_encodeLdpFrLblRng *//*  * decode */int Mpls_decodeLdpFrLblRng  (mplsLdpFrLblRng_t * frLabel, u_char * buff, int bufSize) {  if (MPLS_FRLRGFIXLEN > bufSize) {    PRINT_ERR("failed decoding the Fr Lbl Rng\n");    return MPLS_DEC_BUFFTOOSMALL;  }  MEM_COPY((u_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  */int Mpls_encodeLdpNotMsg(mplsLdpNotifMsg_t * notMsg, u_char * buff, int bufSize){  mplsLdpNotifMsg_t notMsgCopy;  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)(notMsg->baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {    PRINT_ERR("failed to encode the not msg: BUFFER TOO SMALL\n");    return MPLS_ENC_BUFFTOOSMALL;  }  notMsgCopy = *notMsg;  /*   *  encode the base part of the pdu message   */  encodedSize = Mpls_encodeLdpBaseMsg(&(notMsgCopy.baseMsg), tempBuf, bufSize);  if (encodedSize < 0) {    return MPLS_ENC_BASEMSGERROR;  }  PRINT_OUT("Encode BaseMsg for not on %d bytes\n", encodedSize);  tempBuf += encodedSize;  totalSize += encodedSize;  if (notMsgCopy.statusTlvExists) {    encodedSize = Mpls_encodeLdpStatus(&(notMsgCopy.status),      tempBuf, bufSize - totalSize);    if (encodedSize < 0) {      return MPLS_ENC_STATUSERROR;    }    PRINT_OUT("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_OUT("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_OUT("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_OUT("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_OUT("Encoded for LSPID Tlv %d bytes\n", encodedSize);    tempBuf += encodedSize;    totalSize += encodedSize;  }  return totalSize;}                               /* End: Mpls_encodeLdpNotMsg *//*  * decode for notification message  */int Mpls_decodeLdpNotMsg(mplsLdpNotifMsg_t * notMsg, 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(notMsg, 0, sizeof(mplsLdpNotifMsg_t));  decodedSize = Mpls_decodeLdpBaseMsg(&(notMsg->baseMsg), tempBuf, bufSize);  if (decodedSize < 0) {    return MPLS_DEC_BASEMSGERROR;  }  PRINT_OUT("Decode BaseMsg for not on %d bytes\n", decodedSize);  if (notMsg->baseMsg.flags.flags.msgType != MPLS_NOT_MSGTYPE) {    PRINT_ERR("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_OUT("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;  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_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 MPLS_DEC_STATUSERROR;          }          PRINT_OUT("Decoded for STATUS %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          notMsg->statusTlvExists = 1;          notMsg->status.baseTlv = tlvTemp;          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 MPLS_DEC_EXSTATERROR;          }          PRINT_OUT("Decoded for EX_STATUS %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          notMsg->exStatusTlvExists = 1;          notMsg->exStatus.baseTlv = tlvTemp;          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 MPLS_DEC_RETPDUERROR;          }          PRINT_OUT("Decoded for RET_PDU %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          notMsg->retPduTlvExists = 1;          notMsg->retPdu.baseTlv = tlvTemp;          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 MPLS_DEC_RETMSGERROR;          }          PRINT_OUT("Decoded for RET_MSG %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          notMsg->retMsgTlvExists = 1;          notMsg->retMsg.baseTlv = tlvTemp;          break;        }      case MPLS_LSPID_TLVTYPE:        {          decodedSize = Mpls_decodeLdpLspIdTlv(&(notMsg->lspidTlv),            tempBuf, bufSize - totalSize);          if (decodedSize < 0) {            PRINT_ERR("Failure when dec LSPID tlv from Not msg\n");            return MPLS_DEC_LSPIDERROR;          }          PRINT_OUT("Decoded for lspid tlv %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          notMsg->lspidTlvExists = 1;          notMsg->lspidTlv.baseTlv = tlvTemp;          break;        }      default:        {          PRINT_ERR("Found wrong tlv type while decoding not 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_decodeLdpNotMsg is %d\n", totalSize);  return totalSize;}                               /* End: Mpls_decodeLdpNotMsg *//* *      Encode-decode for Status TLV  *//*  * encode: */int Mpls_encodeLdpStatus  (mplsLdpStatusTlv_t * status, u_char * buff, int bufSize) {  int encodedSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_char *statusPtr;  if (MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN > bufSize) {    /* not enough room */    return MPLS_ENC_BUFFTOOSMALL;  }  /*    *  encode for tlv    */  encodedSize = Mpls_encodeLdpTlv(&(status->baseTlv), tempBuf, bufSize);  if (encodedSize < 0) {    PRINT_ERR("failed encoding the tlv in STATUS\n");    return MPLS_ENC_TLVERROR;  }  statusPtr = (u_char *) status;  tempBuf += encodedSize;  statusPtr += encodedSize;  /*    *  encode for the rest of the  Status   */  status->flags.mark = htonl(status->flags.mark);  status->msgId = htonl(status->msgId);  status->msgType = htons(status->msgType);  MEM_COPY(tempBuf, statusPtr, MPLS_STATUSFIXLEN);  return (MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN);}                               /* End: Mpls_encodeLdpStatus *//*  * decode: */int Mpls_decodeLdpStatus  (mplsLdpStatusTlv_t * status, u_char * buff, int bufSize) {  u_char *statusPtr;  if (MPLS_STATUSFIXLEN > bufSize) {    /* not enough data for Status */    PRINT_ERR("failed decoding Status\n");    return MPLS_DEC_BUFFTOOSMALL;  }  statusPtr = (u_char *) status;  statusPtr += MPLS_TLVFIXLEN;  /* we want to point to the flags since the                                   tlv was decoded before we reach here */  /*    *  decode for the rest of the Status   */  MEM_COPY(statusPtr, buff, MPLS_STATUSFIXLEN);  status->flags.mark = ntohl(status->flags.mark);  status->msgId = ntohl(status->msgId);  status->msgType = ntohs(status->msgType);  return MPLS_STATUSFIXLEN;}                               /* End: Mpls_decodeLdpStatus *//* *      Encode-decode for Extended Status TLV  *//*  * encode: */int Mpls_encodeLdpExStatus  (mplsLdpExStatusTlv_t * status, u_char * buff, int bufSize) {  int encodedSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  if (MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN > bufSize) {    /* not enough room */    return MPLS_ENC_BUFFTOOSMALL;  }

⌨️ 快捷键说明

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