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

📄 ldp_nortel.c

📁 实现了MPLS中的标签分发协议(LDP 3036 )的基本功能
💻 C
📖 第 1 页 / 共 5 页
字号:
  tlv->length = htons(tlv->length);  MEM_COPY(buff, (u_char *) tlv, MPLS_TLVFIXLEN);  return MPLS_TLVFIXLEN;}                               /* End: Mpls_encodeLdpTlv *//*  * decode:  */int Mpls_decodeLdpTlv(mplsLdpTlv_t * tlv, u_char * buff, int bufSize){  if (MPLS_TLVFIXLEN > bufSize) {    PRINT_ERR("Failed decoding TLV\n");    return MPLS_DEC_BUFFTOOSMALL;  }  MEM_COPY((u_char *) tlv, buff, MPLS_TLVFIXLEN);  tlv->flags.mark = ntohs(tlv->flags.mark);  tlv->length = ntohs(tlv->length);  return MPLS_TLVFIXLEN;}                               /* End: Mpls_decodeLdpTlv *//* *      Encode-decode for CSP (common session param) *//*  * encode:  */int Mpls_encodeLdpCsp(mplsLdpCspTlv_t * csp, u_char * buff, int bufSize){  int encodedSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_char *cspPtr;  if (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN > bufSize) {    /* not enough room */    return MPLS_ENC_BUFFTOOSMALL;  }  cspPtr = (u_char *) csp;  /*    *  encode for tlv    */  encodedSize = Mpls_encodeLdpTlv(&(csp->baseTlv), tempBuf, bufSize);  if (encodedSize < 0) {    PRINT_ERR("failed encoding the tlv in CSP\n");    return MPLS_ENC_TLVERROR;  }  tempBuf += encodedSize;  cspPtr += encodedSize;  /*    *  encode for the rest of the Csp    */  csp->protocolVersion = htons(csp->protocolVersion);  csp->holdTime = htons(csp->holdTime);  csp->flags.mark = htons(csp->flags.mark);  csp->maxPduLen = htons(csp->maxPduLen);  csp->rcvLsrAddress = htonl(csp->rcvLsrAddress);  csp->rcvLsId = htons(csp->rcvLsId);  MEM_COPY(tempBuf, cspPtr, MPLS_CSPFIXLEN);  return (MPLS_CSPFIXLEN + MPLS_TLVFIXLEN);}                               /* End: Mpls_encodeLdpCsp *//*  * decode:  */int Mpls_decodeLdpCsp(mplsLdpCspTlv_t * csp, u_char * buff, int bufSize){  u_char *cspPtr;  if (MPLS_CSPFIXLEN > bufSize) {    /* not enough data for Csp */    PRINT_ERR("failed decoding LdpCsp\n");    return MPLS_DEC_BUFFTOOSMALL;  }  cspPtr = (u_char *) csp;  cspPtr += MPLS_TLVFIXLEN;     /* we want to point to the flags since the                                   tlv was decoded before we reach here */  /*    *  decode for the rest of the Csp    */  MEM_COPY(cspPtr, buff, MPLS_CSPFIXLEN);  csp->protocolVersion = ntohs(csp->protocolVersion);  csp->holdTime = ntohs(csp->holdTime);  csp->flags.mark = ntohs(csp->flags.mark);  csp->maxPduLen = ntohs(csp->maxPduLen);  csp->rcvLsrAddress = ntohl(csp->rcvLsrAddress);  csp->rcvLsId = ntohs(csp->rcvLsId);  return MPLS_CSPFIXLEN;}                               /* Mpls_decodeLdpCsp *//* *      Encode-decode for Fr Session Parameters *//*  * encode */int Mpls_encodeLdpFsp(mplsLdpFspTlv_t * frFsp, u_char * buff, int bufSize){  int encodedSize = 0;  u_short totalSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_int i, numLblRng;  /* get the size of the frAsp to be encoded and check it against     the buffer size */  if (MPLS_TLVFIXLEN + (int)(frFsp->baseTlv.length) > bufSize) {    /* not enough room */    return MPLS_ENC_BUFFTOOSMALL;  }  /*    *  encode for tlv    */  encodedSize = Mpls_encodeLdpTlv(&(frFsp->baseTlv), tempBuf, bufSize);  if (encodedSize < 0) {    return MPLS_ENC_TLVERROR;  }  tempBuf += encodedSize;  totalSize += encodedSize;  /*    *  encode for M + N + dir + res    */  numLblRng = frFsp->flags.flags.numLblRng;  frFsp->flags.flags.res = 0;  frFsp->flags.mark = htonl(frFsp->flags.mark);  MEM_COPY(tempBuf, (u_char *) & (frFsp->flags.mark), MPLS_FSPFIXLEN);  tempBuf += MPLS_FSPFIXLEN;  totalSize += MPLS_FSPFIXLEN;  /*    *  encode for FR labels    */  for (i = 0; i < numLblRng; i++) {    encodedSize = Mpls_encodeLdpFrLblRng(&(frFsp->lblRngList[i]),      tempBuf, bufSize - totalSize);    if (encodedSize < 0) {      return MPLS_ENC_FSPLBLERROR;    }    tempBuf += encodedSize;    totalSize += encodedSize;  }  return totalSize;}                               /* End: Mpls_encodeLdpFsp *//*  * decode */int Mpls_decodeLdpFsp(mplsLdpFspTlv_t * frFsp, u_char * buff, int bufSize){  int decodedSize = 0;  u_short totalSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_int i;  if (MPLS_FSPFIXLEN > bufSize) {    /* the buffer does not contain even the required field */    PRINT_ERR("failed in decoding LdpFsp\n");    return MPLS_DEC_BUFFTOOSMALL;  }  /*    *  decode for M + N + res    */  MEM_COPY((u_char *) & (frFsp->flags.mark), tempBuf, MPLS_FSPFIXLEN);  tempBuf += MPLS_FSPFIXLEN;  totalSize += MPLS_FSPFIXLEN;  frFsp->flags.mark = ntohl(frFsp->flags.mark);  /*   *  decode for FR labels    */  for (i = 0; i < frFsp->flags.flags.numLblRng; i++) {    decodedSize = Mpls_decodeLdpFrLblRng(&(frFsp->lblRngList[i]),      tempBuf, bufSize - totalSize);    if (decodedSize < 0) {      PRINT_ERR("failed in decoding LdpFrLabel[%d] for LdpFsp\n", i);      return MPLS_DEC_FSPLBLERROR;    }    tempBuf += decodedSize;    totalSize += decodedSize;  }  return totalSize;}                               /* End: Mpls_decodeLdpFsp *//* *      Encode-decode for INIT msg  *//*  * encode for init message  */int Mpls_encodeLdpInitMsg  (mplsLdpInitMsg_t * initMsg, u_char * buff, int bufSize) {  mplsLdpInitMsg_t initMsgCopy;  int encodedSize, totalSize;  u_char *tempBuf = buff;       /* no change for the buff ptr */  initMsgCopy = *initMsg;  totalSize = 0;  /* check the length of the messageId + mandatory param +     optional param */  if ((int)(initMsgCopy.baseMsg.msgLength) + MPLS_TLVFIXLEN > bufSize) {    PRINT_ERR("failed to encode the init msg: BUFFER TOO SMALL\n");    return MPLS_ENC_BUFFTOOSMALL;  }  /*   *  encode the base part of the pdu message   */  encodedSize = Mpls_encodeLdpBaseMsg(&(initMsgCopy.baseMsg), tempBuf, bufSize);  if (encodedSize < 0) {    return MPLS_ENC_BASEMSGERROR;  }  PRINT_OUT("Encode BaseMsg for init on %d bytes\n", encodedSize);  tempBuf += encodedSize;  totalSize += encodedSize;  /*   *  encode the csp if any    */  if (initMsgCopy.cspExists) {    encodedSize = Mpls_encodeLdpCsp(&(initMsgCopy.csp),      tempBuf, bufSize - totalSize);    if (encodedSize < 0) {      return MPLS_ENC_CSPERROR;    }    PRINT_OUT("Encoded for CSP %d bytes\n", encodedSize);    tempBuf += encodedSize;    totalSize += encodedSize;  }  /*   *  encode the asp if any    */  if (initMsgCopy.aspExists) {    encodedSize = Mpls_encodeLdpAsp(&(initMsgCopy.asp),      tempBuf, bufSize - totalSize);    if (encodedSize < 0) {      return MPLS_ENC_ASPERROR;    }    PRINT_OUT("Encoded for ASP %d bytes\n", encodedSize);    tempBuf += encodedSize;    totalSize += encodedSize;  }  /*   *  encode the fsp if any    */  if (initMsgCopy.fspExists) {    encodedSize = Mpls_encodeLdpFsp(&(initMsgCopy.fsp),      tempBuf, bufSize - totalSize);    if (encodedSize < 0) {      return MPLS_ENC_FSPERROR;    }    tempBuf += encodedSize;    totalSize += encodedSize;  }  return totalSize;}                               /* End: Mpls_encodeLdpInitMsg *//*  * decode for unknown message  */int Mpls_decodeLdpUnknownMsg  (mplsLdpUnknownMsg_t * msg, u_char * buff, int bufSize) {  int decodedSize = 0;  u_int totalSize = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  /*   *  decode the base part of the pdu message   */  memset(msg, 0, sizeof(mplsLdpMsg_t));  decodedSize = Mpls_decodeLdpBaseMsg(&(msg->baseMsg), tempBuf, bufSize);  if (decodedSize < 0) {    return MPLS_DEC_BASEMSGERROR;  }  PRINT_OUT("Decode BaseMsg for unknown on %d bytes\n", decodedSize);  tempBuf += decodedSize;  totalSize += decodedSize;  if (bufSize - totalSize <= 0) {    /* nothing left for decoding */    PRINT_ERR("Init msg does not have anything beside base msg\n");    return totalSize;  }  if (msg->baseMsg.msgLength > MPLS_NOT_MAXSIZE) {    PRINT_ERR("Message is too big for unknow message buffer.\n");    return MPLS_DEC_BASEMSGERROR;  }  memcpy(msg->data, tempBuf, msg->baseMsg.msgLength);  decodedSize = msg->baseMsg.msgLength;  tempBuf += decodedSize;  totalSize += decodedSize;  PRINT_OUT("totalsize for Mpls_decodeLdpUnknowntMsg is %d\n", totalSize);  return totalSize;}/*  * decode for init message  */int Mpls_decodeLdpInitMsg  (mplsLdpInitMsg_t * initMsg, u_char * buff, int bufSize) {  int decodedSize = 0;  u_int totalSize = 0;  u_int stopLength = 0;  u_char *tempBuf = buff;       /* no change for the buff ptr */  u_int totalSizeParam = 0;  mplsLdpTlv_t tlvTemp;  /*   *  decode the base part of the pdu message   */  memset(initMsg, 0, sizeof(mplsLdpInitMsg_t));  decodedSize = Mpls_decodeLdpBaseMsg(&(initMsg->baseMsg), tempBuf, bufSize);  if (decodedSize < 0) {    return MPLS_DEC_BASEMSGERROR;  }  PRINT_OUT("Decode BaseMsg for init on %d bytes\n", decodedSize);  if (initMsg->baseMsg.flags.flags.msgType != MPLS_INIT_MSGTYPE) {    PRINT_ERR("Not the right message type; expected init and got %x\n",      initMsg->baseMsg.flags.flags.msgType);    return MPLS_MSGTYPEERROR;  }  tempBuf += decodedSize;  totalSize += decodedSize;  if (bufSize - totalSize <= 0) {    /* nothing left for decoding */    PRINT_ERR("Init msg does not have anything beside base msg\n");    return totalSize;  }  PRINT_OUT("bufSize = %d,  totalSize = %d, initMsg->baseMsg.msgLength = %d\n",    bufSize, totalSize, initMsg->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 = initMsg->baseMsg.msgLength - MPLS_MSGIDFIXLEN;  while (stopLength > totalSizeParam) {    /*     *  decode the tlv to check what's next     */    decodedSize = Mpls_decodeLdpTlv(&tlvTemp, tempBuf, bufSize - totalSize);    if (decodedSize < 0) {      /* something wrong */      PRINT_ERR("INIT msg decode failed for tlv\n");      return MPLS_DEC_TLVERROR;    }    tempBuf += decodedSize;    totalSize += decodedSize;    totalSizeParam += decodedSize;    switch (tlvTemp.flags.flags.tBit) {      case MPLS_CSP_TLVTYPE:        {          decodedSize = Mpls_decodeLdpCsp(&(initMsg->csp),            tempBuf, bufSize - totalSize);          if (decodedSize < 0) {            PRINT_ERR("Failure when decoding Csp from init msg\n");            return MPLS_DEC_CSPERROR;          }          PRINT_OUT("Decoded for CSP %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          initMsg->cspExists = 1;          initMsg->csp.baseTlv = tlvTemp;          break;        }      case MPLS_ASP_TLVTYPE:        {          decodedSize = Mpls_decodeLdpAsp(&(initMsg->asp),            tempBuf, bufSize - totalSize);          if (decodedSize < 0) {            PRINT_ERR("Failure when decoding Asp from init msg\n");            return MPLS_DEC_ASPERROR;          }          PRINT_OUT("Decoded for ASP %d bytes\n", decodedSize);          tempBuf += decodedSize;          totalSize += decodedSize;          totalSizeParam += decodedSize;          initMsg->aspExists = 1;          initMsg->asp.baseTlv = tlvTemp;          break;        }      case MPLS_FSP_TLVTYPE:        {          decodedSize = Mpls_decodeLdpFsp(&(initMsg->fsp),            tempBuf, bufSize - totalSize);          if (decodedSize < 0) {

⌨️ 快捷键说明

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