home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tcpdumpb.zip / print-llc.c < prev    next >
C/C++ Source or Header  |  1996-10-07  |  5KB  |  202 lines

  1. /*
  2.  * Copyright (c) 1992, 1993, 1994, 1995, 1996
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. /*
  23.  * Code by Matt Thomas, Digital Equipment Corporation
  24.  *    with an awful lot of hacking by Jeffrey Mogul, DECWRL
  25.  */
  26.  
  27. #ifndef lint
  28. static  char rcsid[] =
  29.     "@(#)$Header: print-llc.c,v 1.20 96/07/23 14:17:25 leres Exp $";
  30. #endif
  31.  
  32. #include <sys/param.h>
  33. #include <sys/time.h>
  34.  
  35. #include <netinet/in.h>
  36.  
  37. #include <ctype.h>
  38. #include <netdb.h>
  39. #include <signal.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. #include "interface.h"
  44. #include "addrtoname.h"
  45. #include "extract.h"            /* must come after interface.h */
  46.  
  47. #include "llc.h"
  48.  
  49. static struct tok cmd2str[] = {
  50.     { LLC_UI,    "ui" },
  51.     { LLC_TEST,    "test" },
  52.     { LLC_XID,    "xid" },
  53.     { LLC_UA,    "ua" },
  54.     { LLC_DISC,    "disc" },
  55.     { LLC_DM,    "dm" },
  56.     { LLC_SABME,    "sabme" },
  57.     { LLC_FRMR,    "frmr" },
  58.     { 0,        NULL }
  59. };
  60.  
  61. /*
  62.  * Returns non-zero IFF it succeeds in printing the header
  63.  */
  64. int
  65. llc_print(const u_char *p, u_int length, u_int caplen,
  66.       const u_char *esrc, const u_char *edst)
  67. {
  68.     struct llc llc;
  69.     register u_short et;
  70.     register int ret;
  71.  
  72.     if (caplen < 3) {
  73.         (void)printf("[|llc]");
  74.         default_print((u_char *)p, caplen);
  75.         return(0);
  76.     }
  77.  
  78.     /* Watch out for possible alignment problems */
  79.     memcpy((char *)&llc, (char *)p, min(caplen, sizeof(llc)));
  80.  
  81.     if (llc.ssap == LLCSAP_GLOBAL && llc.dsap == LLCSAP_GLOBAL) {
  82.         ipx_print(p, length);
  83.         return (1);
  84.     }
  85.     else if (p[0] == 0xf0 && p[1] == 0xf0) {
  86.       /* we don't actually have a full netbeui parser yet, but the
  87.          smb parser can handle many smb-in-netbeui packets, which
  88.          is very useful, so we call that */
  89.       netbeui_print(p+2,p+min(caplen,length));
  90.     }
  91.     if (llc.ssap == LLCSAP_ISONS && llc.dsap == LLCSAP_ISONS
  92.         && llc.llcui == LLC_UI) {
  93.         isoclns_print(p + 3, length - 3, caplen - 3, esrc, edst);
  94.         return (1);
  95.     }
  96.  
  97.     if (llc.ssap == LLCSAP_SNAP && llc.dsap == LLCSAP_SNAP
  98.         && llc.llcui == LLC_UI) {
  99.         if (caplen < sizeof(llc)) {
  100.             (void)printf("[|llc-snap]");
  101.             default_print((u_char *)p, caplen);
  102.             return (0);
  103.         }
  104.         if (vflag)
  105.             (void)printf("snap %s ", protoid_string(llc.llcpi));
  106.  
  107.         caplen -= sizeof(llc);
  108.         length -= sizeof(llc);
  109.         p += sizeof(llc);
  110.  
  111.         /* This is an encapsulated Ethernet packet */
  112.         et = EXTRACT_16BITS(&llc.ethertype[0]);
  113.         ret = ether_encap_print(et, p, length, caplen);
  114.         if (ret)
  115.             return (ret);
  116.     }
  117.  
  118.     if ((llc.ssap & ~LLC_GSAP) == llc.dsap) {
  119.         if (eflag)
  120.             (void)printf("%s ", llcsap_string(llc.dsap));
  121.         else
  122.             (void)printf("%s > %s %s ",
  123.                     etheraddr_string(esrc),
  124.                     etheraddr_string(edst),
  125.                     llcsap_string(llc.dsap));
  126.     } else {
  127.         if (eflag)
  128.             (void)printf("%s > %s ",
  129.                 llcsap_string(llc.ssap & ~LLC_GSAP),
  130.                 llcsap_string(llc.dsap));
  131.         else
  132.             (void)printf("%s %s > %s %s ",
  133.                 etheraddr_string(esrc),
  134.                 llcsap_string(llc.ssap & ~LLC_GSAP),
  135.                 etheraddr_string(edst),
  136.                 llcsap_string(llc.dsap));
  137.     }
  138.  
  139.     if ((llc.llcu & LLC_U_FMT) == LLC_U_FMT) {
  140.         const char *m;
  141.         char f;
  142.         m = tok2str(cmd2str, "%02x", LLC_U_CMD(llc.llcu));
  143.         switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
  144.             case 0:            f = 'C'; break;
  145.             case LLC_GSAP:        f = 'R'; break;
  146.             case LLC_U_POLL:        f = 'P'; break;
  147.             case LLC_GSAP|LLC_U_POLL:    f = 'F'; break;
  148.             default:            f = '?'; break;
  149.         }
  150.  
  151.         printf("%s/%c", m, f);
  152.  
  153.         p += 3;
  154.         length -= 3;
  155.         caplen -= 3;
  156.  
  157.         if ((llc.llcu & ~LLC_U_POLL) == LLC_XID) {
  158.             if (*p == LLC_XID_FI) {
  159.             printf(": %02x %02x", p[1], p[2]);
  160.             p += 3;
  161.             length -= 3;
  162.             caplen -= 3;
  163.             }
  164.         }
  165.  
  166.         if (!strcmp(m,"ui") && f=='C') {
  167.           /* we don't have a proper ipx decoder yet, but there
  168.                      is a partial one in the smb code */
  169.           ipx_netbios_print(p,p+min(caplen,length));
  170.         }
  171.  
  172.     } else {
  173.         char f;
  174.         llc.llcis = ntohs(llc.llcis);
  175.         switch ((llc.ssap & LLC_GSAP) | (llc.llcu & LLC_U_POLL)) {
  176.             case 0:            f = 'C'; break;
  177.             case LLC_GSAP:        f = 'R'; break;
  178.             case LLC_U_POLL:        f = 'P'; break;
  179.             case LLC_GSAP|LLC_U_POLL:    f = 'F'; break;
  180.             default:            f = '?'; break;
  181.         }
  182.  
  183.         if ((llc.llcu & LLC_S_FMT) == LLC_S_FMT) {
  184.             static char *llc_s[] = { "rr", "rej", "rnr", "03" };
  185.             (void)printf("%s (r=%d,%c)",
  186.                 llc_s[LLC_S_CMD(llc.llcis)],
  187.                 LLC_IS_NR(llc.llcis),
  188.                 f);
  189.         } else {
  190.             (void)printf("I (s=%d,r=%d,%c)",
  191.                 LLC_I_NS(llc.llcis),
  192.                 LLC_IS_NR(llc.llcis),
  193.                 f);
  194.         }
  195.         p += 4;
  196.         length -= 4;
  197.         caplen -= 4;
  198.     }
  199.     (void)printf(" len=%d", length);
  200.     return(1);
  201. }
  202.