home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sys / tests / netccitt / pk_dump.c next >
Encoding:
C/C++ Source or Header  |  1991-05-09  |  7.5 KB  |  347 lines

  1. /*-
  2.  * Copyright (c) 1988, 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1988, 1991 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)pk_dump.c    7.2 (Berkeley) 5/9/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * This is a kernel debugging aid.
  46.  * dumps out a cache of mbufs.
  47.  */
  48.  
  49. #include <sys/param.h>
  50. #include <sys/mbuf.h>
  51. #include <sys/socket.h>
  52. #include <sys/socketvar.h>
  53. #include <net/if.h>
  54. #include <netccitt/x25.h>
  55. #include <netccitt/pk.h>
  56. #include <netccitt/pk_var.h>
  57.  
  58. #include <errno.h>
  59. #include <netdb.h>
  60. #include <nlist.h>
  61. #include <kvm.h>
  62. #include <paths.h>
  63. #include <stdio.h>
  64. /* 
  65.  *  This procedure decodes the X.25 level 3 packet returning a 
  66.  *  code to be used in switchs or arrays.
  67.  */
  68.  
  69. pk_decode (xp)
  70. register struct x25_packet *xp;
  71. {
  72.     register int type;
  73.  
  74.     if (xp -> fmt_identifier != 1)
  75.         return (INVALID_PACKET);
  76. #ifdef ancient_history
  77.     /* 
  78.      *  Make sure that the logical channel group number is 0.
  79.      *  This restriction may be removed at some later date.
  80.      */
  81.     if (xp -> lc_group_number != 0)
  82.         return (INVALID_PACKET);
  83. #endif
  84.     /* 
  85.      *  Test for data packet first.
  86.      */
  87.     if (!(xp -> packet_type & DATA_PACKET_DESIGNATOR))
  88.         return (DATA);
  89.  
  90.     /* 
  91.      *  Test if flow control packet (RR or RNR).
  92.      */
  93.     if (!(xp -> packet_type & RR_OR_RNR_PACKET_DESIGNATOR))
  94.         switch (xp -> packet_type & 0x1f) {
  95.         case X25_RR:
  96.             return (RR);
  97.         case X25_RNR:
  98.             return (RNR);
  99.         case X25_REJECT:
  100.             return (REJECT);
  101.         }
  102.  
  103.     /* 
  104.      *  Determine the rest of the packet types.
  105.      */
  106.     switch (xp -> packet_type) {
  107.     case X25_CALL: 
  108.         type = CALL;
  109.         break;
  110.  
  111.     case X25_CALL_ACCEPTED: 
  112.         type = CALL_ACCEPTED;
  113.         break;
  114.  
  115.     case X25_CLEAR: 
  116.         type = CLEAR;
  117.         break;
  118.  
  119.     case X25_CLEAR_CONFIRM: 
  120.         type = CLEAR_CONF;
  121.         break;
  122.  
  123.     case X25_INTERRUPT: 
  124.         type = INTERRUPT;
  125.         break;
  126.  
  127.     case X25_INTERRUPT_CONFIRM: 
  128.         type = INTERRUPT_CONF;
  129.         break;
  130.  
  131.     case X25_RESET: 
  132.         type = RESET;
  133.         break;
  134.  
  135.     case X25_RESET_CONFIRM: 
  136.         type = RESET_CONF;
  137.         break;
  138.  
  139.     case X25_RESTART: 
  140.         type = RESTART;
  141.         break;
  142.  
  143.     case X25_RESTART_CONFIRM: 
  144.         type = RESTART_CONF;
  145.         break;
  146.  
  147.     case X25_DIAGNOSTIC:
  148.         type = DIAG_TYPE;
  149.         break;
  150.  
  151.     default: 
  152.         type = INVALID_PACKET;
  153.     }
  154.     return (type);
  155. }
  156.  
  157. char    *pk_state[] = {
  158.     "Listen",    "Ready",    "Received-Call",
  159.     "Sent-Call",    "Data-Transfer","Received-Clear",
  160.     "Sent-Clear",
  161. };
  162.  
  163. char   *pk_name[] = {
  164.     "Call",        "Call-Conf",    "Clear",
  165.     "Clear-Conf",    "Data",        "Intr",        "Intr-Conf",
  166.     "Rr",        "Rnr",        "Reset",    "Reset-Conf",
  167.     "Restart",    "Restart-Conf",    "Reject",    "Diagnostic",
  168.     "Invalid"
  169. };
  170.  
  171. int pk_lengths[] = {0, 0, 0,
  172. 0, 3, 5, 3,
  173. 3, 3, 5, 5,
  174. 5, 5, 5, 0,
  175. 0, 0};
  176.  
  177. pk_trace (m, dir)
  178. register struct mbuf *m;
  179. char *dir;
  180. {
  181.     register char *s;
  182.     struct x25_packet *xp = mtod(m, struct x25_packet *);
  183.     register int i, len = 0, cnt = 0;
  184.  
  185.     i = pk_decode (xp) / MAXSTATES;
  186.     if ((len = pk_lengths[i]) || (len = m -> m_len))
  187.         if (len > 5)
  188.             len = 5;
  189.  
  190.     printf ("%s LCN=%d: %s (", dir, LCN(xp), pk_name[i]);
  191.   
  192.     for (s = (char *) xp, i = 0; i < len; ++i, ++s)
  193.         printf ("%x ", (int) * s & 0xff);
  194.     printf (")\n");
  195. }
  196.  
  197. bprintf(fp, b, s)
  198.     register FILE *fp;
  199.     register int b;
  200.     register u_char *s;
  201. {
  202.     register int i;
  203.     int gotsome = 0;
  204.  
  205.     if (b == 0)
  206.         return;
  207.     while (i = *s++) {
  208.         if (b & (1 << (i-1))) {
  209.             if (gotsome == 0)
  210.                 i = '<';
  211.             else
  212.                 i = ',';
  213.             (void) putc(i, fp);
  214.             gotsome = 1;
  215.             for (; (i = *s) > 32; s++)
  216.                 (void) putc(i, fp);
  217.         } else
  218.             while (*s > 32)
  219.                 s++;
  220.     }
  221.     if (gotsome)
  222.         (void) putc('>', fp);
  223. }
  224.  
  225. int verbose = 0; /* so you can adb -w the binary */
  226. int tflag = 0;
  227. int Iflag = 0;
  228. int Aflag = 0;
  229. char *vmunix = _PATH_UNIX;
  230. char *kmemf;
  231. struct nlist nl[] = {
  232. {"_pk_output_cache"},
  233. {"_pk_input_cache"},
  234. 0
  235. };
  236.  
  237. main(argc, argv)
  238.     int argc;
  239.     char **argv;
  240. {
  241.  
  242.     if (kvm_openfiles(vmunix, kmemf, NULL) == -1) {
  243.         fprintf(stderr, "netstat: kvm_openfiles: %s\n", kvm_geterr());
  244.         exit(1);
  245.     }
  246.     if (kvm_nlist(nl) < 0 || nl[0].n_type == 0) {
  247.         fprintf(stderr, "%s: no namelist\n", vmunix);
  248.         exit(1);
  249.     }
  250.     mbuf_cache_dump(nl);
  251.     mbuf_cache_dump(nl + 1);
  252. }
  253. struct mbuf_cache c;
  254. struct mbuf **mbvec;
  255. #define kget(p, d) \
  256.     (kvm_read((void *)(p), &(d), sizeof (d)))
  257.  
  258. mbuf_cache_dump(nl)
  259. struct nlist *nl;
  260. {
  261.     register struct mbuf *m;
  262.     unsigned cache_size;
  263.     int i;
  264.  
  265.     printf("Dumping %s:\n", nl->n_name);
  266.     kget(nl->n_value, c);
  267.     if (cache_size = c.mbc_size * sizeof(m))
  268.         mbvec = (struct mbuf **)malloc(cache_size);
  269.     if (mbvec == 0 || c.mbc_cache == 0)
  270.         return;
  271.     kvm_read(c.mbc_cache, mbvec, cache_size);
  272.     for (i = c.mbc_num;;) {
  273.         if (i == 0)
  274.             i = c.mbc_size;
  275.         i--;
  276.         if (m = mbvec[i])
  277.             mbuf_dump(m);
  278.         if (i == c.mbc_num)
  279.             break;
  280.     }
  281. }
  282.  
  283.  
  284. mbuf_dump(m)
  285. register struct mbuf *m;
  286. {
  287.     int virgin = 1;
  288.     register struct x25_packet *xp;
  289.     struct mbuf n;
  290.     char extbuf[1024];
  291.  
  292.     putchar('\n');
  293.     for (; m; m = n.m_next) {
  294.         kget(m, n);
  295.         printf("m %x", m);
  296.         if (n.m_flags) {
  297.             printf(" flags ");
  298.             bprintf(stdout, n.m_flags,
  299.                 "\1M_EXT\2M_PKTHDR\3M_EOR\4M_BCAST\5M_MCAST");
  300.         }
  301.         if (Aflag)
  302.             printf(" chained %x", n.m_nextpkt);
  303.         printf(" next %x len %d", n.m_next, n.m_len);
  304.         if (n.m_flags & M_PKTHDR) {
  305.             printf(" total %d", n.m_pkthdr.len);
  306.             if (Iflag)
  307.                 printf(" rcvif %x", n.m_pkthdr.rcvif);
  308.         }
  309.         putchar('\n');
  310.         if (n.m_flags & M_EXT) {
  311.             kvm_read(n.m_ext.ext_buf, extbuf, sizeof(extbuf));
  312.             n.m_data = extbuf + (n.m_data - n.m_ext.ext_buf);
  313.         } else if (n.m_data <  m->m_dat + MLEN)
  314.             n.m_data = n.m_dat + (n.m_data - m->m_dat);
  315.         else {
  316.             printf("mbuf screwup\n");
  317.             continue;
  318.         }
  319.         if (virgin) {
  320.             virgin = 0;
  321.             pk_trace(&n, "  X.25: ");
  322.         }
  323.         dumpit("data: ",n.m_data, n.m_len);
  324.     }
  325. }
  326.  
  327. dumpit(what, where, n)
  328. char *what; unsigned short *where; int n;
  329. {
  330.     unsigned short *s = where;
  331.     unsigned short *z = where + (n+1)/2;
  332.     int count = 0;
  333.  
  334.     if (verbose == 0)
  335.         return;
  336.     printf(what);
  337.     while(s < z) {
  338.         count++;
  339.         printf("%x ",*s++);
  340.         if ((count & 15) == 0)
  341.             putchar('\n');
  342.     }
  343.     if (count & 15)
  344.         putchar('\n');
  345.     fflush(stdout);
  346. }
  347.