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

📄 _anep.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
字号:
/* 
 * Copyright (c) 2000 The University of Utah and the Flux Group.
 * All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software is hereby
 * granted provided that (1) source code retains these copyright, permission,
 * and disclaimer notices, and (2) redistributions including binaries
 * reproduce the notices in supporting documentation.
 *
 * THE UNIVERSITY OF UTAH ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  THE UNIVERSITY OF UTAH DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * ---------------------------
 *
 * Filename: print-anep.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * Extensions to tcpdump for ANEP version 1 packets.
 *
 * See http://www.cis.upenn.edu/~switchware/ANEP/
 * for information on ANEP.
 *
 * ---------------------------
 */

/*
 * Parse ANEP packets. For more information on ANEP, see 
 * 
 *     http://www.cis.upenn.edu/~switchware/ANEP/. 
 * 
 * ANEP packet headers have the following form:
 * 
 *    0                   1                   2                   3
 *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |   Version     |     Flags     |            Type ID            |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |      ANEP Header Length       |     ANEP Packet Length        |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   ~                            Options                            ~
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |                               .                               |
 *   ~                            Payload                            ~
 *   |                               .                               |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * 
 * Options take the following form:
 *
 *    0                   1                   2                   3
 *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |FLG|       Option Type         |         Option Length         |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |                               .                               |
 *   ~               Option Payload (Option Value)                   ~
 *   |                               .                               |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */

#if 0
static const char rcsid[] =
  "@(#) $Header: /usr/lsrc/flux/CVS/tcpdump/print-anep.c,v 1.4 2000/02/17 18:06:57 kwright Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <ctype.h>

#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcpip.h>

#include "interfac.h"

struct anep_cmnhdr {
       u_int8_t  ch_version;   /* Version */
       u_int8_t  ch_flags;     /* Flags */
       u_int16_t ch_type;      /* EE */
       u_int16_t ch_hlen;      /* Header length in 32-bit words */
       u_int16_t ch_plen;      /* Packet length in octets */
     };

struct anep_opthdr {
       u_short oh_ft;      /* Option type and flag */
       u_short oh_len;     /* Option length in 32-bit words, including hdr */
     };


/* Reserved type IDS
 */
#define ANEP_TYPEID_UKANS_USER         11
#define ANEP_TYPEID_UKANS_MGT          12
#define ANEP_TYPEID_UKANS_TEST1        13
#define ANEP_TYPEID_UKANS_TEST2        14
#define ANEP_TYPEID_ISI                15
#define ANEP_TYPEID_ANTS               18
#define ANEP_TYPEID_PLAN               19
#define ANEP_TYPEID_UPENN1             20
#define ANEP_TYPEID_UPENN2             21
#define ANEP_TYPEID_UPENN3             22
#define ANEP_TYPEID_SPANNER            24
#define ANEP_TYPEID_NETSCRIPT_DEPLOY   31
#define ANEP_TYPEID_NETSCRIPT_PACKET   32
#define ANEP_TYPEID_NETSCRIPT_MGT      33
#define ANEP_TYPEID_COLUMBIA1          34
#define ANEP_TYPEID_COLUMBIA2          35
#define ANEP_TYPEID_TASCUMASS_MCDATA   41
#define ANEP_TYPEID_TASCUMASS_MCCTRL   42
#define ANEP_TYPEID_TASCUMASS_MCSESS   43
#define ANEP_TYPEID_TASCUMASS          44
#define ANEP_TYPEID_MIT_DS             47
#define ANEP_TYPEID_SRI_ANCORS         51
#define ANEP_TYPEID_SRI1               52
#define ANEP_TYPEID_SRI2               53
#define ANEP_TYPEID_SRI3               54
#define ANEP_TYPEID_SRI4               55
#define ANEP_TYPEID_LUCENT1            61
#define ANEP_TYPEID_LUCENT2            62
#define ANEP_TYPEID_LUCENT3            63
#define ANEP_TYPEID_LUCENT4            64
#define ANEP_TYPEID_ANAIS_MGT1         71
#define ANEP_TYPEID_ANAIS_MGT2         72
#define ANEP_TYPEID_ANAIS_DATA         73
#define ANEP_TYPEID_ANAIS_MCAST        74
#define ANEP_TYPEID_ANAIS1             75
#define ANEP_TYPEID_ICSI_MO            77
#define ANEP_TYPEID_ETHZUR_ANET        81
#define ANEP_TYPEID_ETHZUR_ANETSIM     82
#define ANEP_TYPEID_DAN                86
#define ANEP_TYPEID_3COM1              91
#define ANEP_TYPEID_3COM2              92
#define ANEP_TYPEID_3COM3              93
#define ANEP_TYPEID_3COM4              94
#define ANEP_TYPEID_SANE1              101
#define ANEP_TYPEID_SANE2              102
#define ANEP_TYPEID_SANE3              103
#define ANEP_TYPEID_SANE4              104
#define ANEP_TYPEID_ANGLE1             111
#define ANEP_TYPEID_ANGLE2             112
#define ANEP_TYPEID_ANGLE3             113
#define ANEP_TYPEID_ANTSR1             121
#define ANEP_TYPEID_ANTSR2             122
#define ANEP_TYPEID_ANTSR3             123
#define ANEP_TYPEID_ANTSR4             124
#define ANEP_TYPEID_ANTSR5             125

/* Reserved option types
 */
#define ANEP_SRCID   1
#define ANEP_DSTID   2
#define ANEP_CHKSUM  3
#define ANEP_NNAUTH  4

/*
 * Format for option types ANEP_SRCID, ANEP_DSTID and ANDP_NNAUTH:
 *
 *   0                   1                   2                   3
 *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |                       Scheme Identifier                       |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *   |                               .                               |
 *   ~                        Option Payload                         ~
 *   |                               .                               |
 *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 */

/* 
 * Reserved Scheme ID values. Used for 
 * both ANEP_DSTID and ANEP_SRCID values.
 */
#define ANEP_IPV4 1
#define ANEP_IPV6 2
#define ANEP_8023 3

static void anep_optprint (const u_char *hp, u_int hlen)
{
  const struct anep_opthdr *oh = (const struct anep_opthdr*)hp;
  u_int   olen, oid;
  u_short type, flags;

  for (; hlen > 0; (u_char*)oh += olen, hlen -= olen)
  {
    const char *schm;

    if (hlen < sizeof(*oh) || (u_char*)(oh + 1) > snapend)
    {
      PUTS ("|anep]");
      return;
    }
    olen  = ntohs (oh->oh_len) << 2;
    type  = ntohs (oh->oh_ft) & 0x3FFF;
    flags = (ntohs (oh->oh_ft) & 0xc000) >> 14;

    /* option length for anep options MUST be greater than 1
     */
    if (olen < 1)
    {
      PUTS ("bad packet: option len 0");
      return;
    }

    PUTCHAR ('[');
    if (flags & 2)
    {
      /* EE-private option */
      PUTS ("priv]");
      continue;
    }

    if (flags & 1)
    {
      /* IF option type unknown, ignore or discard?
       */
      PUTCHAR ('D');
    }
    else
    {
      PUTCHAR ('I');
    }

    oid = ntohl (*(u_int*)(oh + 1));

    switch (type)
    {
      case ANEP_SRCID:
           switch (oid)
           {
             case 1:
                  schm = "IPv4";
                  break;
             case 2:
                  schm = "IPv6";
                  break;
             case 3:
                  schm = "802.3";
                  break;
             default:
                  schm = "UNK";
           }
           PRINTF (":srcid:%s", schm);
           break;

      case ANEP_DSTID:
           switch (oid)
           {
             case 1:
                  schm = "IPv4";
                  break;
             case 2:
                  schm = "IPv6";
                  break;
             case 3:
                  schm = "802.3";
                  break;
             default:
                  schm = "UNK";
           }
           PRINTF (":dstid:%s", schm);
           break;

      case ANEP_NNAUTH:
           switch (oid)
           {
             case 1:
                  schm = "SPKI";
                  break;
             case 2:
                  schm = "X.509";
                  break;
             default:
                  schm = "UNK";
           }
           PRINTF (":nnauth:%s", schm);
           break;

      case ANEP_CHKSUM:
           PUTS (":cksm");
           break;

      default:
           PUTS (":unk");
    }
    PUTCHAR (']');
  }
}

void anep_print (const u_char *hdr, u_int len)
{
  const  struct anep_cmnhdr *ch;
  const  char  *typeid = "??";
  u_int  hlen;
  u_char df;

  ch = (const struct anep_cmnhdr*) hdr;

  if (len < sizeof(*ch) || (u_char*)(ch+1) > snapend)
  {
    PUTS ("[|anep]\n");
    return;
  }
  hlen = ntohs (ch->ch_hlen) << 2;

  PRINTF (" v%d", ch->ch_version);

  switch (ntohs (ch->ch_type))
  {
    case ANEP_TYPEID_UKANS_USER:
         typeid = "UKans user";
         break;
    case ANEP_TYPEID_UKANS_MGT:
         typeid = "UKans mgt";
         break;
    case ANEP_TYPEID_UKANS_TEST1:
         typeid = "UKans test1";
         break;
    case ANEP_TYPEID_UKANS_TEST2:
         typeid = "UKans test2";
         break;
    case ANEP_TYPEID_ISI:
         typeid = "ISI";
         break;
    case ANEP_TYPEID_ANTS:
         typeid = "Ants";
         break;
    case ANEP_TYPEID_PLAN:
         typeid = "Plan";
         break;
    case ANEP_TYPEID_UPENN1:
    case ANEP_TYPEID_UPENN2:
    case ANEP_TYPEID_UPENN3:
         typeid = "UPenn";
         break;
    case ANEP_TYPEID_SPANNER:
         typeid = "Spanner";
         break;
    case ANEP_TYPEID_NETSCRIPT_DEPLOY:
         typeid = "Netscript deplymnt";
         break;
    case ANEP_TYPEID_NETSCRIPT_PACKET:
         typeid = "Netscript data";
         break;
    case ANEP_TYPEID_NETSCRIPT_MGT:
         typeid = "Netscript mgt";
         break;
    case ANEP_TYPEID_COLUMBIA1:
    case ANEP_TYPEID_COLUMBIA2:
         typeid = "Columbia";
         break;
    case ANEP_TYPEID_TASCUMASS_MCDATA:
         typeid = "TASC/UMass data";
         break;
    case ANEP_TYPEID_TASCUMASS_MCCTRL:
         typeid = "TASC/UMass ctrl";
         break;
    case ANEP_TYPEID_TASCUMASS_MCSESS:
         typeid = "TASC/UMass session";
         break;
    case ANEP_TYPEID_TASCUMASS:
         typeid = "TASC/UMass";
         break;
    case ANEP_TYPEID_MIT_DS:
         typeid = "MIT DS";
         break;
    case ANEP_TYPEID_SRI_ANCORS:
         typeid = "Ancors";
         break;
    case ANEP_TYPEID_SRI1:
    case ANEP_TYPEID_SRI2:
    case ANEP_TYPEID_SRI3:
    case ANEP_TYPEID_SRI4:
         typeid = "SRI";
         break;
    case ANEP_TYPEID_LUCENT1:
    case ANEP_TYPEID_LUCENT2:
    case ANEP_TYPEID_LUCENT3:
    case ANEP_TYPEID_LUCENT4:
         typeid = "Lucent";
         break;
    case ANEP_TYPEID_ANAIS_MGT1:
         typeid = "Anais mgt1";
         break;
    case ANEP_TYPEID_ANAIS_MGT2:
         typeid = "Anais mgt2";
         break;
    case ANEP_TYPEID_ANAIS_DATA:
         typeid = "Anais data";
         break;
    case ANEP_TYPEID_ANAIS_MCAST:
         typeid = "Anais mcast";
         break;
    case ANEP_TYPEID_ANAIS1:
         typeid = "Anais";
         break;
    case ANEP_TYPEID_ICSI_MO:
         typeid = "ICSI MO";
         break;
    case ANEP_TYPEID_ETHZUR_ANET:
         typeid = "ANet";
         break;
    case ANEP_TYPEID_ETHZUR_ANETSIM:
         typeid = "ANet sim";
         break;
    case ANEP_TYPEID_DAN:
         typeid = "DAN";
         break;
    case ANEP_TYPEID_3COM1:
    case ANEP_TYPEID_3COM2:
    case ANEP_TYPEID_3COM3:
    case ANEP_TYPEID_3COM4:
         typeid = "3COM";
         break;
    case ANEP_TYPEID_SANE1:
    case ANEP_TYPEID_SANE2:
    case ANEP_TYPEID_SANE3:
    case ANEP_TYPEID_SANE4:
         typeid = "Sane";
         break;
    case ANEP_TYPEID_ANGLE1:
    case ANEP_TYPEID_ANGLE2:
    case ANEP_TYPEID_ANGLE3:
         typeid = "Angle";
         break;
    case ANEP_TYPEID_ANTSR1:
    case ANEP_TYPEID_ANTSR2:
    case ANEP_TYPEID_ANTSR3:
    case ANEP_TYPEID_ANTSR4:
    case ANEP_TYPEID_ANTSR5:
         typeid = "Antsr";
         break;
  }

  PRINTF (" %s (%d)", typeid, ntohs(ch->ch_plen));

  /* 
   * From the anetd RFC DRAFT:
   * 
   * The Flags field is 8 bits long. In version 1 of this protocol, only
   * the most significant bit is used, to indicate what the node should do
   * if it does not recognize the Type ID. If the value is 0, the node
   * could try to forward the packet using the default routing mechanism
   * (if one is in use), if the necessary information is available in the
   * Options part of the header. If the value is 1, the node should 
   * discard the packet. The rest of the bits in this field should be 
   * ignored by the node.  It is recommended that they be set to zero by
   * the packet originator.
   */

  df = ch->ch_flags >> 7;
  switch (df)
  {
    case 0:
         PUTS (" F");
         break;
    case 1:
         PUTS (" D");
         break;
  }

  hlen -= sizeof(struct anep_cmnhdr);
  if (hlen > 0)
     anep_optprint ((u_char*)(ch+1), hlen);

  PUTCHAR ('\n');
}

⌨️ 快捷键说明

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