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

📄 ospf6_lsa.c

📁 router source code for the ipv6 ospdf ,it can use for wireless.
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * LSA function * Copyright (C) 1999 Yasuhiro Ohara * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING.  If not, write to the  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,  * Boston, MA 02111-1307, USA.   */#include <zebra.h>/* Include other stuffs */#include "version.h"#include "log.h"#include "getopt.h"#include "linklist.h"#include "thread.h"#include "command.h"#include "memory.h"#include "sockunion.h"#include "if.h"#include "prefix.h"#include "stream.h"#include "thread.h"#include "filter.h"#include "zclient.h"#include "table.h"#include "plist.h"#include "ospf6_proto.h"#include "ospf6_prefix.h"#include "ospf6_lsa.h"#include "ospf6_lsdb.h"#include "ospf6_message.h"#include "ospf6_dump.h"#include "ospf6_top.h"#include "ospf6_area.h"#include "ospf6_interface.h"#include "ospf6_neighbor.h"#include "ospf6_ism.h"#include "ospf6_nsm.h"#include "ospf6_dbex.h"#define HEADER_DEPENDENCY#include "ospf6d.h"#undef HEADER_DEPENDENCY/* test LSAs identity */static intospf6_lsa_issame (struct ospf6_lsa_header__ *lsh1,                  struct ospf6_lsa_header__ *lsh2){  assert (lsh1 && lsh2);  if (lsh1->adv_router != lsh2->adv_router)    return 0;  if (lsh1->id != lsh2->id)    return 0;  if (lsh1->type != lsh2->type)    return 0;  return 1;}/* RFC2328: Section 13.2 */intospf6_lsa_differ (struct ospf6_lsa *lsa1,                  struct ospf6_lsa *lsa2){  int diff, cmplen;  if (! ospf6_lsa_issame (lsa1->header, lsa2->header))    return 1;  /* check Options field */  /* xxx */  ospf6_lsa_age_current (lsa1);  ospf6_lsa_age_current (lsa2);  if (ntohs (lsa1->header->age) == MAXAGE &&      ntohs (lsa2->header->age) != MAXAGE)    return 1;  if (ntohs (lsa1->header->age) != MAXAGE &&      ntohs (lsa2->header->age) == MAXAGE)    return 1;  /* compare body */  if (ntohs (lsa1->header->length) != ntohs (lsa2->header->length))    return 1;  cmplen = ntohs (lsa1->header->length) - sizeof (struct ospf6_lsa_header);  diff = memcmp (lsa1->header + 1, lsa2->header + 1, cmplen);  return diff;}intospf6_lsa_match (u_int16_t type, u_int32_t id, u_int32_t adv_router,                 struct ospf6_lsa_header *lsh){  if (lsh->advrtr != adv_router)    return 0;  if (lsh->ls_id != id)    return 0;  if (lsh->type != type)    return 0;  return 1;}/* ospf6 age functions *//* calculate birth and set expire timer */static voidospf6_lsa_age_set (struct ospf6_lsa *lsa){  struct timeval now;  assert (lsa && lsa->header);  if (gettimeofday (&now, (struct timezone *)NULL) < 0)    zlog_warn ("LSA: gettimeofday failed, may fail LSA AGEs: %s",               strerror (errno));  lsa->birth.tv_sec = now.tv_sec - ntohs (lsa->header->age);  lsa->birth.tv_usec = now.tv_usec;  if (ntohs (lsa->header->age) != MAXAGE)    lsa->expire = thread_add_timer (master, ospf6_lsa_expire, lsa,                                    lsa->birth.tv_sec + MAXAGE - now.tv_sec);  else    lsa->expire = NULL;  return;}/* this function calculates current age from its birth,   then update age field of LSA header. return value is current age */u_int16_tospf6_lsa_age_current (struct ospf6_lsa *lsa){  struct timeval now;  u_int32_t ulage;  u_int16_t age;  assert (lsa);  assert (lsa->header);  /* current time */  if (gettimeofday (&now, (struct timezone *)NULL) < 0)    zlog_warn ("LSA: gettimeofday failed, may fail ages: %s",               strerror (errno));  /* calculate age */  ulage = now.tv_sec - lsa->birth.tv_sec;  /* if over MAXAGE, set to it */  if (ulage > MAXAGE)    age = MAXAGE;  else    age = ulage;  lsa->header->age = htons (age);  return age;}/* update age field of LSA header with adding InfTransDelay */voidospf6_lsa_age_update_to_send (struct ospf6_lsa *lsa, u_int32_t transdelay){  unsigned short age;  age = ospf6_lsa_age_current (lsa) + transdelay;  if (age > MAXAGE)    age = MAXAGE;  lsa->header->age = htons (age);  return;}voidospf6_lsa_premature_aging (struct ospf6_lsa *lsa){  /* log */  if (IS_OSPF6_DUMP_LSA)    zlog_info ("LSA: Premature aging: %s", lsa->str);  if (lsa->expire)    thread_cancel (lsa->expire);  lsa->expire = (struct thread *) NULL;  if (lsa->refresh)    thread_cancel (lsa->refresh);  lsa->refresh = (struct thread *) NULL;  memset (&lsa->birth, 0, sizeof (struct timeval));  thread_execute (master, ospf6_lsa_expire, lsa, 0);}/* check which is more recent. if a is more recent, return -1;   if the same, return 0; otherwise(b is more recent), return 1 */intospf6_lsa_check_recent (struct ospf6_lsa *a, struct ospf6_lsa *b){  signed long seqnuma, seqnumb;  u_int16_t cksuma, cksumb;  u_int16_t agea, ageb;  assert (a && a->header);  assert (b && b->header);  assert (ospf6_lsa_issame (a->header, b->header));  seqnuma = ((signed long) ntohl (a->header->seqnum))             - (signed long) INITIAL_SEQUENCE_NUMBER;  seqnumb = ((signed long) ntohl (b->header->seqnum))             - (signed long) INITIAL_SEQUENCE_NUMBER;  /* compare by sequence number */    /* xxx, care about LS sequence number wrapping */  recent_reason = "seqnum";  if (seqnuma > seqnumb)    return -1;  else if (seqnuma < seqnumb)    return 1;  /* Checksum */  cksuma = ntohs (a->header->checksum);  cksumb = ntohs (b->header->checksum);  if (cksuma > cksumb)    return -1;  if (cksuma < cksumb)    return 0;  /* Age check */  agea = ospf6_lsa_age_current (a);  ageb = ospf6_lsa_age_current (b);    /* MaxAge check */  recent_reason = "max age";  if (agea == OSPF6_LSA_MAXAGE && ageb != OSPF6_LSA_MAXAGE)    return -1;  else if (agea != OSPF6_LSA_MAXAGE && ageb == OSPF6_LSA_MAXAGE)    return 1;  recent_reason = "age differ";  if (agea > ageb && agea - ageb >= OSPF6_LSA_MAXAGEDIFF)    return 1;  else if (agea < ageb && ageb - agea >= OSPF6_LSA_MAXAGEDIFF)    return -1;  /* neither recent */  recent_reason = "the same instance";  return 0;}intospf6_lsa_lsd_num (struct ospf6_lsa_header *lsa_header){  int ldnum = 0;  u_int16_t len;  len = ntohs (lsa_header->length);  len -= sizeof (struct ospf6_lsa_header);  if (lsa_header->type == htons (OSPF6_LSA_TYPE_ROUTER))    {      len -= sizeof (struct ospf6_router_lsa);      ldnum = len / sizeof (struct ospf6_router_lsd);    }  else /* (lsa_header->type == htons (OSPF6_LSA_TYPE_NETWORK)) */    {      len -= sizeof (struct ospf6_network_lsa);      ldnum = len / sizeof (u_int32_t);    }  return ldnum;}void *ospf6_lsa_lsd_get (int index, struct ospf6_lsa_header *lsa_header){  void *p;  struct ospf6_router_lsa *router_lsa;  struct ospf6_router_lsd *router_lsd;  struct ospf6_network_lsa *network_lsa;  struct ospf6_network_lsd *network_lsd;  if (lsa_header->type == htons (OSPF6_LSA_TYPE_ROUTER))    {      router_lsa = (struct ospf6_router_lsa *) (lsa_header + 1);      router_lsd = (struct ospf6_router_lsd *) (router_lsa + 1);      router_lsd += index;      p = (void *) router_lsd;    }  else if (lsa_header->type == htons (OSPF6_LSA_TYPE_NETWORK))    {      network_lsa = (struct ospf6_network_lsa *) (lsa_header + 1);      network_lsd = (struct ospf6_network_lsd *) (network_lsa + 1);      network_lsd += index;      p = (void *) network_lsd;    }  else    {      p = (void *) NULL;    }  return p;}/* network_lsd <-> router_lsd */static intospf6_lsa_lsd_network_reference_match (struct ospf6_network_lsd *network_lsd1,                                       struct ospf6_lsa_header *lsa_header1,                                       struct ospf6_router_lsd *router_lsd2,                                       struct ospf6_lsa_header *lsa_header2){  if (network_lsd1->adv_router != lsa_header2->advrtr)    return 0;  if (router_lsd2->type != OSPF6_ROUTER_LSD_TYPE_TRANSIT_NETWORK)    return 0;  if (router_lsd2->neighbor_router_id != lsa_header1->advrtr)    return 0;  if (router_lsd2->neighbor_interface_id != lsa_header1->ls_id)    return 0;  return 1;}/* router_lsd <-> router_lsd */static intospf6_lsa_lsd_router_reference_match (struct ospf6_router_lsd *router_lsd1,                                      struct ospf6_lsa_header *lsa_header1,                                      struct ospf6_router_lsd *router_lsd2,                                      struct ospf6_lsa_header *lsa_header2){  if (router_lsd1->type != OSPF6_ROUTER_LSD_TYPE_POINTTOPOINT)    return 0;  if (router_lsd2->type != OSPF6_ROUTER_LSD_TYPE_POINTTOPOINT)    return 0;  if (router_lsd1->neighbor_router_id != lsa_header2->advrtr)    return 0;  if (router_lsd2->neighbor_router_id != lsa_header1->advrtr)    return 0;  if (router_lsd1->neighbor_interface_id != router_lsd2->interface_id)    return 0;  if (router_lsd2->neighbor_interface_id != router_lsd1->interface_id)    return 0;  return 1;}intospf6_lsa_lsd_is_refer_ok (int index1, struct ospf6_lsa_header *lsa_header1,                           int index2, struct ospf6_lsa_header *lsa_header2){  struct ospf6_router_lsd *r1, *r2;  struct ospf6_network_lsd *n;  r1 = (struct ospf6_router_lsd *) NULL;  r2 = (struct ospf6_router_lsd *) NULL;  n = (struct ospf6_network_lsd *) NULL;  if (lsa_header1->type == htons (OSPF6_LSA_TYPE_ROUTER))    r1 = (struct ospf6_router_lsd *) ospf6_lsa_lsd_get (index1, lsa_header1);  else    n = (struct ospf6_network_lsd *) ospf6_lsa_lsd_get (index1, lsa_header1);  if (lsa_header2->type == htons (OSPF6_LSA_TYPE_ROUTER))    r2 = (struct ospf6_router_lsd *) ospf6_lsa_lsd_get (index2, lsa_header2);  else    n = (struct ospf6_network_lsd *) ospf6_lsa_lsd_get (index2, lsa_header2);  if (r1 && r2)    return ospf6_lsa_lsd_router_reference_match (r1, lsa_header1,                                                 r2, lsa_header2);  else if (r1 && n)    return ospf6_lsa_lsd_network_reference_match (n, lsa_header2,                                                  r1, lsa_header1);  else if (n && r2)    return ospf6_lsa_lsd_network_reference_match (n, lsa_header1,                                                 r2, lsa_header2);  return 0;}voidospf6_lsa_show (struct vty *vty, struct ospf6_lsa *lsa){  char adv_router[64], id[64], type[32];  assert (lsa);  assert (lsa->header);  ospf6_lsa_type_string (lsa->header->type, type, sizeof (type));  inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));  inet_ntop (AF_INET, &lsa->header->adv_router,             adv_router, sizeof (adv_router));  vty_out (vty, "%s", VTY_NEWLINE);  vty_out (vty, "Age: %4hu Type: %s%s", ospf6_lsa_age_current (lsa),           type, VTY_NEWLINE);  vty_out (vty, "Link State ID: %s%s", id, VTY_NEWLINE);  vty_out (vty, "Advertising Router: %s%s", adv_router, VTY_NEWLINE);  vty_out (vty, "LS Sequence Number: %#lx%s", (u_long)ntohl (lsa->header->seqnum),           VTY_NEWLINE);  vty_out (vty, "CheckSum: %#hx Length: %hu%s", ntohs (lsa->header->checksum),           ntohs (lsa->header->length), VTY_NEWLINE);  {    struct ospf6_lsa_slot *slot;    slot = ospf6_lsa_slot_get (lsa->header->type);    if (slot)      {        (*slot->func_show) (vty, lsa);        vty_out (vty, "%s", VTY_NEWLINE);        return;      }  }  vty_out (vty, "%sUnknown LSA type ...%s", VTY_NEWLINE, VTY_NEWLINE);}voidospf6_lsa_show_summary_header (struct vty *vty){  vty_out (vty, "%-12s %-15s %-15s %4s %8s %4s %4s %-8s%s",           "Type", "LSId", "AdvRouter", "Age", "SeqNum",           "Cksm", "Len", "Duration", VTY_NEWLINE);}voidospf6_lsa_show_summary (struct vty *vty, struct ospf6_lsa *lsa){  char adv_router[16], id[16], type[16];  struct timeval now, res;  char duration[16];  assert (lsa);  assert (lsa->header);  memset (type, 0, sizeof (type));  ospf6_lsa_type_string (lsa->header->type, type, 13);  inet_ntop (AF_INET, &lsa->header->id, id, sizeof (id));  inet_ntop (AF_INET, &lsa->header->adv_router, adv_router,             sizeof (adv_router));  gettimeofday (&now, NULL);  ospf6_timeval_sub (&now, &lsa->installed, &res);  ospf6_timeval_string_summary (&res, duration, sizeof (duration));  vty_out (vty, "%-12s %-15s %-15s %4hu %8lx %04hx %4hu %8s%s",           type, id, adv_router, ospf6_lsa_age_current (lsa),           (u_long) ntohl (lsa->header->seqnum),           ntohs (lsa->header->checksum), ntohs (lsa->header->length),           duration, VTY_NEWLINE);}voidospf6_lsa_show_dump (struct vty *vty, struct ospf6_lsa *lsa){  u_char *start, *end, *current;  char byte[4];  start = (char *) lsa->header;  end = (char *) lsa->header + ntohs (lsa->header->length);  vty_out (vty, "%s", VTY_NEWLINE);  vty_out (vty, "%s:%s", lsa->str, VTY_NEWLINE);  for (current = start; current < end; current ++)    {      if ((current - start) % 16 == 0)        vty_out (vty, "%s        ", VTY_NEWLINE);      else if ((current - start) % 4 == 0)

⌨️ 快捷键说明

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