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

📄 ospfh_te.c

📁 实现禁位自动排列在禁位中具体实现了此方法所以这是一个创新很有应用价值在VC平台上调试通过
💻 C
字号:
/*  填充TLV  */

#include "ospfh.h"
#include "ospfh_patch.h"

#define DEFALT_IP_ADDRESS   "0.0.0.0"

static void
set_linkparams_link_type (struct mpls_te_link *lp)
{
  lp->link_type.header.type   = htons (TE_LINK_SUBTLV_LINK_TYPE);
  lp->link_type.header.length = htons (sizeof (lp->link_type.value.link_type));
  lp->link_type.value.link_type = LINK_TYPE_SUBTLV_VALUE_PTP;
  return;
}

static void
set_linkparams_link_id (struct mpls_te_link *lp)
{
  lp->link_id.header.type   = htons (TE_LINK_SUBTLV_LINK_ID);
  lp->link_id.header.length = htons (sizeof (lp->link_id.value));
  lp->link_id.value.s_addr = inet_addr(DEFALT_IP_ADDRESS);
}

static void
set_linkparams_te_metric (struct mpls_te_link *lp, u_int32_t te_metric)
{
  lp->te_metric.header.type   = htons (TE_LINK_SUBTLV_TE_METRIC);
  lp->te_metric.header.length = htons (sizeof (lp->te_metric.value));
  lp->te_metric.value =htonl(te_metric);
  return;
}



static void
set_linkparams_rsc_clsclr (struct mpls_te_link *lp)
{
  lp->rsc_clsclr.header.type   = htons (TE_LINK_SUBTLV_RSC_CLSCLR);
  lp->rsc_clsclr.header.length = htons (sizeof (lp->rsc_clsclr.value));
  lp->rsc_clsclr.value=htonl(DEFALT_RC_ID);
  return;
}

static void
set_linkparams_id_identifier(struct mpls_te_link *lp)
{
	lp->id_identifier.header.type   = htons (TE_LINK_SUBTLV_ID_IDENTIFIER);
	lp->id_identifier.header.length = htons (sizeof (lp->id_identifier.value));
	lp->id_identifier.value[0].s_addr =inet_addr(DEFALT_IP_ADDRESS);
	lp->id_identifier.value[1].s_addr =inet_addr(DEFALT_IP_ADDRESS);
}

static void 
set_linkparams_local_node_id(struct mpls_te_link *lp,struct in_addr local_id)
{
	lp->local_id.header.type   = htons (TE_LINK_SUBTLV_LOCAL_NODE_ID);
	lp->local_id.header.length = htons(sizeof (lp->local_id.value));
	lp->local_id.value=local_id;
}
  
static void 
set_linkparams_remote_node_id(struct mpls_te_link *lp,struct in_addr remote_id)
{
	lp->remote_id.header.type   = htons (TE_LINK_SUBTLV_REMOTE_NODE_ID);
	lp->remote_id.header.length = htons(sizeof (lp->remote_id.value));
	lp->remote_id.value=remote_id;
}
  
static void
set_linkparams_switch_cap(struct mpls_te_link *lp)
{

	lp->ifswitch_cap.header.type   = htons (TE_LINK_SUBTLV_IFSWITCH_CAP);
	lp->ifswitch_cap.header.length = htons (sizeof (lp->ifswitch_cap.value));
	lp->ifswitch_cap.value.switchcap = IFSWITCH_CAP_SUBTLV_VALUE_LSC;
	lp->ifswitch_cap.value.encoding = IFSWITCH_CAP_SUBTLV_ENCODING_SDH;
	lp->ifswitch_cap.value.snum =htonl(0);

}

static void
set_linkparams_ancestor_node_id(struct mpls_te_link *lp)
{ 
	lp->ancestor_id.header.type   = htons (TE_LINK_SUBTLV_ANCESTOR_NODE_ID);
	lp->ancestor_id.header.length = htons(sizeof (lp->ancestor_id.value));
	lp->ancestor_id.value.s_addr=inet_addr("255.255.255.255");
}

static void
set_linkparams_hierarchy_list(struct  mpls_te_link *lp/* struct in_addr high_rc_id, struct in_addr middle_rc_id,struct in_addr low_rc_id*/)
{
	lp->hierarchy_list.header.type   = htons (TE_LINK_SUBTLV_HIERARCHY_LIST);
	lp->hierarchy_list.header.length = htons (sizeof(lp->hierarchy_list.value));
	lp->hierarchy_list.value[0].s_addr=inet_addr(DEFALT_IP_ADDRESS);
    lp->hierarchy_list.value[1].s_addr=inet_addr(DEFALT_IP_ADDRESS);
    lp->hierarchy_list.value[2].s_addr=inet_addr(DEFALT_IP_ADDRESS);
}

void
initialize_linkparams (struct mpls_te_link *lp,int cost, struct in_addr local_id,struct in_addr remote_id)
{  

  
  set_linkparams_link_type (lp);
  set_linkparams_link_id (lp);
  set_linkparams_te_metric(lp,cost);
  set_linkparams_rsc_clsclr (lp);
  set_linkparams_id_identifier(lp);
  set_linkparams_local_node_id(lp,local_id);
  set_linkparams_remote_node_id(lp,remote_id);
  set_linkparams_switch_cap(lp);
  set_linkparams_ancestor_node_id(lp);
  set_linkparams_hierarchy_list(lp);
  set_linkparams_link_header (lp);
   
}


static int
is_mandated_params_set (struct mpls_te_link *lp)
{
  int rc = 0;

  if (ntohs (lp->link_type.header.type) == 0)
    return rc;

  if (ntohs (lp->link_id.header.type) == 0)
    return rc;

  if (ntohs (lp->te_metric.header.type) == 0)
    return rc;

  if (ntohs (lp->rsc_clsclr.header.type) == 0)
    return rc;

  if (ntohs (lp->id_identifier.header.type) == 0)
    return rc;

  if (ntohs (lp->local_id.header.type) == 0)
    return rc;

  if (ntohs (lp->remote_id.header.type) == 0)
    return rc;

  if (ntohs (lp->ifswitch_cap.header.type) == 0)
    return rc;

  rc = 1;

  return rc;
}



/*------------------------------------------------------------------------*
 * Followings are OSPF protocol processing functions for MPLS-TE.
 *------------------------------------------------------------------------*/
#define LINK_TLV 1


static void
set_linkparams_link_header (struct mpls_te_link *lp)
{
//  struct te_tlv_header *tlvh;
  u_int16_t length = 0;

  /* TE_LINK_SUBTLV_LINK_TYPE */
  if (ntohs (lp->link_type.header.type) != 0)
    length += TLV_SIZE (&lp->link_type.header);

  /* TE_LINK_SUBTLV_LINK_ID */
  if (ntohs (lp->link_id.header.type) != 0)
    length += TLV_SIZE (&lp->link_id.header);

  /* TE_LINK_SUBTLV_TE_METRIC */
  if (ntohs (lp->te_metric.header.type) != 0)
    length += TLV_SIZE (&lp->te_metric.header);

  /*  Resource Class/Color */
  if (ntohs (lp->rsc_clsclr.header.type) != 0)
     length += TLV_SIZE (&lp->rsc_clsclr.header);

  if (ntohs (lp->id_identifier.header.type) != 0)
     length += TLV_SIZE (&lp->id_identifier.header);

  if (ntohs (lp->local_id.header.type) != 0)
     length += TLV_SIZE (&lp->local_id.header);

  if (ntohs (lp->remote_id.header.type) != 0)
     length += TLV_SIZE (&lp->remote_id.header);

  if (ntohs (lp->ancestor_id.header.type) != 0)
   length += TLV_SIZE (&lp->ancestor_id.header);	  

  /* TE_LINK_SUBTLV_SWITCH_CAP */
  if (ntohs (lp->ifswitch_cap.header.type) != 0)
    length += TLV_SIZE (&lp->ifswitch_cap.header);

  if (ntohs (lp->hierarchy_list.header.type) != 0)
    length += TLV_SIZE (&lp->hierarchy_list.header);


  lp->link_header.header.type   = htons (TE_TLV_LINK);
  lp->link_header.header.length = htons(length);

  return;
}


static void
build_tlv_header (struct stream *s, struct te_tlv_header *tlvh)
{
  stream_put (s, tlvh, sizeof (struct te_tlv_header));
  return;
}


static void
build_link_subtlv_link_type (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->link_type.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_link_id (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->link_id.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}


static void
build_link_subtlv_te_metric (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->te_metric.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_rsc_clsclr (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->rsc_clsclr.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_id_identifier (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->id_identifier.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_local_id (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->local_id.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_remote_id (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->remote_id.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}


static void
build_link_subtlv_switch_cap (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->ifswitch_cap.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_ancestor_id (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->ancestor_id.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
}

static void
build_link_subtlv_hierarchy_list (struct stream *s, struct mpls_te_link *lp)
{
  struct te_tlv_header *tlvh = &lp->hierarchy_list.header;
  if (ntohs (tlvh->type) != 0)
    {
      build_tlv_header (s, tlvh);
      stream_put (s, tlvh+1, TLV_BODY_SIZE (tlvh));
    }
  return;
} 

static void
build_link_tlv (struct stream *s, struct mpls_te_link *lp)
{
 // set_linkparams_link_header (lp);
  build_tlv_header (s, &lp->link_header.header);

  build_link_subtlv_link_type (s, lp);
  build_link_subtlv_link_id (s, lp);
  build_link_subtlv_te_metric (s, lp);
  build_link_subtlv_rsc_clsclr (s,lp);
  build_link_subtlv_id_identifier (s,lp);
  build_link_subtlv_local_id (s,lp);
  build_link_subtlv_remote_id (s,lp);
  build_link_subtlv_switch_cap (s, lp);
  build_link_subtlv_ancestor_id (s,lp);
  build_link_subtlv_hierarchy_list (s,lp);
  return;
}

static void
ospf_mpls_te_lsa_body_set (struct stream *s, struct mpls_te_link *lp)
{
  build_link_tlv (s, lp);
  return;
}

/* Create new opaque-LSA. */

#define LSA_HEADER_OPTIONS   0
#define LSA_HEADER_LS_TYPE   10
#define LSA_HEADER_LINK_ID   0x01000000
#define LSA_HEADER_LS_CHECKSUM    0
#define LSA_HEADER_LENGTH     113

struct ospf_lsa *
ospf_mpls_te_lsa_new (struct ospf *top, struct mpls_te_link *lp)
{
  struct lsa_header *lsah;
  struct ospf_lsa *newlsa = NULL;

//  u_int16_t length;

   
 
    lsah = malloc(sizeof(struct lsa_header));
    lsah->ls_age = htons (0);
    lsah->options = LSA_HEADER_OPTIONS;
    lsah->type =LSA_HEADER_LS_TYPE;
    lsah->id =htonl(LSA_HEADER_LINK_ID);
    lsah->adv_router = top->self_rc_id;
    lsah->checksum=htons(LSA_HEADER_LS_CHECKSUM);
  //  lsah->length =sizeof(struct mpls_te_link);
  lsah->length =htons(sizeof(struct lsa_header)+sizeof(struct mpls_te_link));



    newlsa=malloc(sizeof(struct ospf_lsa));
    ospf_lsa_new(newlsa);
    newlsa->lp=lp;
    newlsa->data=lsah;


  return newlsa;
}

⌨️ 快捷键说明

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