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

📄 _ospf.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * OSPF support contributed by Jeffrey Honig (jch@mitchell.cit.cornell.edu)
 */

#include <stdio.h>
#include <ctype.h>
#include <time.h>

#include "interfac.h"
#include "a2name.h"
#include "ip.h"
#include "ospf.h"

struct bits {
       u_int bit;
       const char *str;
     };

static const struct bits ospf_option_bits[] = {
                    { OSPF_OPTION_T,  "T"  },
                    { OSPF_OPTION_E,  "E"  },
                    { OSPF_OPTION_MC, "MC" },
                    { 0,              NULL }
                  };

static const struct bits ospf_rla_flag_bits[] = {
                    { RLA_FLAG_B,  "B"  },
                    { RLA_FLAG_E,  "E"  },
                    { RLA_FLAG_W1, "W1" },
                    { RLA_FLAG_W2, "W2" },
                    { 0,           NULL }
                  };

static const char *ospf_types[OSPF_TYPE_MAX] = {
                   NULL, "hello", "dd",
                   "ls_req", "ls_upd", "ls_ack"
                 };

static void ospf_print_seqage (u_int seq, time_t us)
{
  time_t sec  = us % 60;
  time_t mins = (us / 60) % 60;
  time_t hour = us / 3600;

  PRINTF (" S %X age ", seq);
  if (hour)
       PRINTF ("%u:%02u:%02u", (u_int)hour, (u_int)mins, (u_int)sec);
  else if (mins)
       PRINTF ("%u:%02u", (u_int)mins, (u_int)sec);
  else PRINTF ("%u", (u_int)sec);
}


static void ospf_print_bits (const struct bits *bp, u_char options)
{
  char sep = ' ';

  do
  {
    if (options & bp->bit)
    {
      PRINTF ("%c%s", sep, bp->str);
      sep = '/';
    }
  }
  while ((++bp)->bit);
}


#define LS_PRINT(lsp, type) \
  switch (type) {           \
    case LS_TYPE_ROUTER:    \
         PRINTF (" rtr %s ", ipaddr_string(&lsp->ls_router)); \
         break;             \
    case LS_TYPE_NETWORK:   \
         PRINTF (" net dr %s if %s", ipaddr_string(&lsp->ls_router), ipaddr_string(&lsp->ls_stateid)); \
         break;             \
    case LS_TYPE_SUM_IP:    \
         PRINTF (" sum %s abr %s", ipaddr_string(&lsp->ls_stateid), ipaddr_string(&lsp->ls_router)); \
         break;             \
    case LS_TYPE_SUM_ABR:   \
         PRINTF (" abr %s rtr %s", ipaddr_string(&lsp->ls_router), ipaddr_string(&lsp->ls_stateid)); \
         break;             \
    case LS_TYPE_ASE:       \
         PRINTF (" ase %s asbr %s", ipaddr_string(&lsp->ls_stateid), ipaddr_string(&lsp->ls_router)); \
         break;             \
    case LS_TYPE_GROUP:     \
         PRINTF (" group %s rtr %s", ipaddr_string(&lsp->ls_stateid), ipaddr_string(&lsp->ls_router)); \
         break;             \
  }

static int ospf_print_lshdr (const struct lsa_hdr *lshp, const caddr_t end)
{
  if ((caddr_t) (lshp + 1) > end)
     return (1);

  PUTS (" {");

  if (!lshp->ls_type || lshp->ls_type >= LS_TYPE_MAX)
  {
    PRINTF (" ??LS type %d?? }", lshp->ls_type);
    return (1);
  }

  ospf_print_bits (ospf_option_bits, lshp->ls_options);
  ospf_print_seqage (ntohl (lshp->ls_seq), ntohs (lshp->ls_age));

  LS_PRINT (lshp, lshp->ls_type);
  return (0);
}


/* 
 * Print a single link state advertisement.  If truncated return 1, else 0.
 */

static int ospf_print_lsa (const struct lsa *lsap, const caddr_t end)
{
  const char              *ls_end;
  const struct rlalink    *rlp;
  const struct tos_metric *tosp;
  const struct in_addr    *ap;
  const struct aslametric *almp;
  const struct mcla       *mcp;
  const u_int32_t         *lp;
  int   j, k;

  if (ospf_print_lshdr (&lsap->ls_hdr, end))
     return (1);

  ls_end = (const char *) lsap + ntohs (lsap->ls_hdr.ls_length);
  if (ls_end > (const char *) end)
  {
    PUTS (" }");
    return (1);
  }

  switch (lsap->ls_hdr.ls_type)
  {
    case LS_TYPE_ROUTER:
         ospf_print_bits (ospf_rla_flag_bits, lsap->lsa_un.un_rla.rla_flags);

         j = ntohs (lsap->lsa_un.un_rla.rla_count);
         rlp = lsap->lsa_un.un_rla.rla_link;
         while (j--)
         {
           struct rlalink *rln = (struct rlalink*) ((caddr_t)(rlp + 1) +
                                 ((rlp->link_toscount) * sizeof(struct tos_metric)));

           if ((const char *) rln > ls_end)
              break;
           PUTS (" {");

           switch (rlp->link_type)
           {
             case RLA_TYPE_VIRTUAL:
                  PUTS (" virt");
                  /* Fall through */

             case RLA_TYPE_ROUTER:
                  PRINTF (" nbrid %s if %s",
                          ipaddr_string (&rlp->link_id),
                          ipaddr_string (&rlp->link_data));
                  break;

             case RLA_TYPE_TRANSIT:
                  PRINTF (" dr %s if %s",
                          ipaddr_string (&rlp->link_id),
                          ipaddr_string (&rlp->link_data));
                  break;

             case RLA_TYPE_STUB:
                  PRINTF (" net %s mask %s",
                          ipaddr_string (&rlp->link_id),
                          ipaddr_string (&rlp->link_data));
                  break;

             default:
                  PRINTF (" ??RouterLinksType %d?? }", rlp->link_type);
                  return 0;
           }
           PRINTF (" tos 0 metric %d", ntohs (rlp->link_tos0metric));
           tosp = (struct tos_metric *) (sizeof(rlp->link_tos0metric) + (caddr_t)rlp);
           for (k = 0; k < rlp->link_toscount; k++, tosp++)
           {
             PRINTF (" tos %d metric %d",
                     tosp->tos_type, ntohs (tosp->tos_metric));
           }
           PUTS (" }");
           rlp = rln;
         }
         break;

    case LS_TYPE_NETWORK:
         PRINTF (" mask %s rtrs",
                 ipaddr_string (&lsap->lsa_un.un_nla.nla_mask));
         for (ap = lsap->lsa_un.un_nla.nla_router;
              (const char *) (ap + 1) <= ls_end;
              ap++)
         {
           PUTCHAR (' ');
           PUTS (ipaddr_string (ap));
         }
         break;

    case LS_TYPE_SUM_IP:
         PRINTF (" mask %s", ipaddr_string (&lsap->lsa_un.un_sla.sla_mask));
         /* Fall through */

    case LS_TYPE_SUM_ABR:
         for (lp = lsap->lsa_un.un_sla.sla_tosmetric;
              (const char *) (lp + 1) <= ls_end;
              lp++)
         {
           u_int ul = ntohl (*lp);
           PRINTF (" tos %d metric %d",
                   (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS,
                   ul & SLA_MASK_METRIC);
         }
         break;

    case LS_TYPE_ASE:
         PRINTF (" mask %s",
                 ipaddr_string (&lsap->lsa_un.un_asla.asla_mask));

         for (almp = lsap->lsa_un.un_asla.asla_metric;
              (const char *) (almp + 1) <= ls_end;
              almp++)
         {
           u_int ul = ntohl (almp->asla_tosmetric);

           PRINTF (" type %d tos %d metric %d",
                   (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1,
                   (ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS,
                   (ul & ASLA_MASK_METRIC));
           if (almp->asla_forward.s_addr)
              PRINTF (" forward %s", ipaddr_string (&almp->asla_forward));

           if (almp->asla_tag.s_addr)
              PRINTF (" tag %s", ipaddr_string (&almp->asla_tag));
         }
         break;

    case LS_TYPE_GROUP:
         /* Multicast extensions as of 23 July 1991 */
         for (mcp = lsap->lsa_un.un_mcla;
              (const char *) (mcp + 1) <= ls_end;
              mcp++)
         {
           switch (ntohl (mcp->mcla_vtype))
           {
             case MCLA_VERTEX_ROUTER:

⌨️ 快捷键说明

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