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

📄 _sl.c

📁 This directory contains source code for tcpdump, a tool for network monitoring and data acquisition
💻 C
字号:
/* 
 * This file has been slightly modified by NRL for use with IPv6+IPsec.
 * Search for USE_INET6 and/or IPSEC to see the blocks where this happened.
 * See the NRL Copyright notice for conditions on the modifications.
 */
/*
 * Copyright (c) 1989, 1990, 1991, 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.
 */

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <sys/socket.h>
#include <net/if.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <netinet/ip_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/tcp.h>
#include <netinet/tcpip.h>

#include <net/slcompre.h>
#include <net/slip.h>
#include <netdb.h>

#include "interfac.h"
#include "a2name.h"
#include "extract.h"                /* must come after interface.h */

static unsigned lastlen[2][256];
static unsigned lastconn = 255;

static void sliplink_print (const u_char *, const struct ip *, u_int);
static void compressed_sl_print (const u_char *, const struct ip *, u_int, int);

/* XXX BSD/OS 2.1 compatibility */
#if !defined(SLIP_HDRLEN) && defined(SLC_BPFHDR)
#  define SLIP_HDRLEN SLC_BPFHDR
#  define SLX_DIR     0
#  define SLX_CHDR    (SLC_BPFHDRLEN - 1)
#  define CHDR_LEN    (SLC_BPFHDR - SLC_BPFHDRLEN)
#endif

void sl_if_print (u_char *user, const struct pcap_pkthdr *h, const u_char *p)
{
  static u_int32_t num_pkt = 0;
  u_int  caplen = h->caplen;
  u_int  length = h->len;
  const  struct ip *ip;

  ts_print (&h->ts);

  if (caplen < SLIP_HDRLEN)
  {
    PUTS ("[|slip]");
    goto out;
  }
  /*
   * Some printers want to get back at the link level addresses,
   * and/or check that they're not walking off the end of the packet.
   * Rather than pass them all the way down, we set these globals.
   */
  packetp = p;
  snapend = p + caplen;

  length -= SLIP_HDRLEN;

  ip = (struct ip*) (p + SLIP_HDRLEN);

  if (eflag)
     sliplink_print (p, ip, length);

#ifdef USE_INET6
  if (ip->ip_v == 4)
       ip_print ((const u_char*)ip, length);
  else ip6_print ((const u_char*)ip, length);
#else
  ip_print ((const u_char*)ip, length);
#endif

  if (xflag)
     default_print ((u_char*)ip, caplen - SLIP_HDRLEN);

  if (aflag)
     ascii_print ((u_char*)ip, caplen - SLIP_HDRLEN);

out:
  PUTS ("\n");
  status_write ("Slp: %lu (%lu)", ++num_pkt, pcap_mac_packets());
}


void sl_bs_if_print (u_char * user, const struct pcap_pkthdr *h, const u_char * p)
{
  static u_int32_t num_pkt = 0;
  u_int caplen = h->caplen;
  u_int length = h->len;
  const struct ip *ip;

  ts_print (&h->ts);

  if (caplen < SLIP_HDRLEN)
  {
    PUTS ("[|slip]");
    goto out;
  }
  /*
   * Some printers want to get back at the link level addresses,
   * and/or check that they're not walking off the end of the packet.
   * Rather than pass them all the way down, we set these globals.
   */
  packetp = p;
  snapend = p + caplen;
  length -= SLIP_HDRLEN;

  ip = (struct ip*) (p + SLIP_HDRLEN);

#ifdef notdef
  if (eflag)
     sliplink_print (p, ip, length);
#endif

#ifdef USE_INET6
  if (ip->ip_v == 4)
       ip_print ((const u_char*)ip, length);
  else ip6_print ((const u_char*)ip, length);
#else
  ip_print ((const u_char*)ip, length);
#endif

  if (xflag)
     default_print ((u_char*)ip, caplen - SLIP_HDRLEN);

  if (aflag)
     ascii_print((u_char*)ip, caplen - SLIP_HDRLEN);

out:
  PUTS ("\n");
  status_write ("Sbs: %lu (%lu)", ++num_pkt, pcap_mac_packets());
}


static void sliplink_print (const u_char *p, const struct ip *ip, u_int length)
{
  int   dir;
  u_int hlen;

  dir = p[SLX_DIR];
  PUTCHAR (dir == SLIPDIR_IN ? 'I' : 'O');
  PUTCHAR (' ');

  if (nflag)
  {
    /* XXX just dump the header */
    int i;

    for (i = SLX_CHDR; i < SLX_CHDR + CHDR_LEN - 1; ++i)
       PRINTF ("%02x.", p[i]);
    PRINTF ("%02x: ", p[SLX_CHDR + CHDR_LEN - 1]);
    return;
  }
  switch (p[SLX_CHDR] & 0xf0)
  {
    case TYPE_IP:
         PRINTF ("ip %d: ", length + SLIP_HDRLEN);
         break;

    case TYPE_UNCOMPRESSED_TCP:
         /*
          * The connection id is stored in the IP protcol field.
          * Get it from the link layer since sl_uncompress_tcp()
          * has restored the IP header copy to IPPROTO_TCP.
          */
         lastconn = ((struct ip*) &p[SLX_CHDR])->ip_p;
         hlen  = ip->ip_hl;
         hlen += ((struct tcphdr*) &((int*)ip)[hlen])->th_off;
         lastlen[dir][lastconn] = length - (hlen << 2);
         PRINTF ("utcp %u: ", lastconn);
         break;

    default:
         if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP)
         {
           compressed_sl_print (&p[SLX_CHDR], ip, length, dir);
           PUTS (": ");
         }
         else
           PRINTF ("slip-%d!: ", p[SLX_CHDR]);
  }
}

static const u_char *print_sl_change (const char *str, const u_char * cp)
{
  u_int i;

  if ((i = *cp++) == 0)
  {
    i = EXTRACT_16BITS (cp);
    cp += 2;
  }
  PRINTF (" %s%d", str, i);
  return (cp);
}

static const u_char *print_sl_winchange (const u_char * cp)
{
  short i;

  if ((i = *cp++) == 0)
  {
    i = EXTRACT_16BITS (cp);
    cp += 2;
  }
  if (i >= 0)
       PRINTF (" W+%d", i);
  else PRINTF (" W%d", i);
  return (cp);
}

static void compressed_sl_print (const u_char * chdr, const struct ip *ip,
                                 u_int length, int dir)
{
  const u_char *cp = chdr;
  u_int flags, hlen;

  flags = *cp++;
  if (flags & NEW_C)
  {
    lastconn = *cp++;
    PRINTF ("ctcp %u", lastconn);
  }
  else
    PUTS ("ctcp *");

  /* skip tcp checksum */
  cp += 2;

  switch (flags & SPECIALS_MASK)
  {
    case SPECIAL_I:
         PRINTF (" *SA+%u", lastlen[dir][lastconn]);
         break;

    case SPECIAL_D:
         PRINTF (" *S+%u", lastlen[dir][lastconn]);
         break;

    default:
         if (flags & NEW_U)
            cp = print_sl_change ("U=", cp);
         if (flags & NEW_W)
            cp = print_sl_winchange (cp);
         if (flags & NEW_A)
            cp = print_sl_change ("A+", cp);
         if (flags & NEW_S)
            cp = print_sl_change ("S+", cp);
         break;
  }
  if (flags & NEW_I)
     cp = print_sl_change ("I+", cp);

  /*
   * 'hlen' is the length of the uncompressed TCP/IP header (in words).
   * 'cp - chdr' is the length of the compressed header.
   * 'length - hlen' is the amount of data in the packet.
   */
  hlen  = ip->ip_hl;
  hlen += ((struct tcphdr*) &((long*)ip)[hlen])->th_off;
  lastlen[dir][lastconn] = length - (hlen << 2);
  PRINTF (" %u (%d)", lastlen[dir][lastconn], (int)(cp-chdr));
}

⌨️ 快捷键说明

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