Home | History | Annotate | Line # | Download | only in systat
icmp.c revision 1.2
      1 /*	$NetBSD: icmp.c,v 1.2 2000/01/08 23:12:37 itojun Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999 Andy Doran <ad (at) NetBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #ifndef lint
     32 __RCSID("$NetBSD: icmp.c,v 1.2 2000/01/08 23:12:37 itojun Exp $");
     33 #endif /* not lint */
     34 
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 #include <sys/socket.h>
     38 #include <sys/sysctl.h>
     39 
     40 #include <netinet/in.h>
     41 #include <netinet/in_systm.h>
     42 #include <netinet/ip.h>
     43 #include <netinet/ip_icmp.h>
     44 #include <netinet/icmp_var.h>
     45 
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <paths.h>
     49 #include <nlist.h>
     50 #include <kvm.h>
     51 #include "systat.h"
     52 #include "extern.h"
     53 
     54 #define LHD(row, str) mvwprintw(wnd, row, 10, str)
     55 #define RHD(row, str) mvwprintw(wnd, row, 45, str);
     56 #define BD(row, str) LHD(row, str); RHD(row, str)
     57 #define SHOW(stat, row, col) \
     58     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)stats.stat)
     59 #define SHOW2(type, row) SHOW(icps_inhist[type], row, 0); \
     60     SHOW(icps_outhist[type], row, 35)
     61 
     62 static struct icmpstat stats;
     63 
     64 static struct nlist namelist[] = {
     65 	{ "_icmpstat" },
     66 	{ "" }
     67 };
     68 
     69 WINDOW *
     70 openicmp(void)
     71 {
     72 
     73 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
     74 }
     75 
     76 void
     77 closeicmp(w)
     78 	WINDOW *w;
     79 {
     80 
     81 	if (w != NULL) {
     82 		wclear(w);
     83 		wrefresh(w);
     84 		delwin(w);
     85 	}
     86 }
     87 
     88 void
     89 labelicmp(void)
     90 {
     91 
     92 	wmove(wnd, 0, 0); wclrtoeol(wnd);
     93 
     94 	mvwprintw(wnd, 1, 0,  "------------ ICMP input -------------");
     95 	mvwprintw(wnd, 1, 38, "------------- ICMP output -------------");
     96 
     97 	mvwprintw(wnd, 8, 0,  "---------- Input histogram ----------");
     98 	mvwprintw(wnd, 8, 38, "---------- Output histogram -----------");
     99 
    100 	LHD(3, "with bad code");
    101 	LHD(4, "with bad length");
    102 	LHD(5, "with bad checksum");
    103 	LHD(6, "with insufficient data");
    104 
    105 	RHD(3, "errors generated");
    106 	RHD(4, "suppressed - original too short");
    107 	RHD(5, "suppressed - original was ICMP");
    108 	RHD(6, "responses sent");
    109 
    110 	BD(2, "total messages");
    111 	BD(9, "echo response");
    112 	BD(10, "echo request");
    113 	BD(11, "destination unreachable");
    114 	BD(12, "redirect");
    115 	BD(13, "time-to-live exceeded");
    116 	BD(14, "parameter problem");
    117 	LHD(15, "router advertisement");
    118 	RHD(15, "router solicitation");
    119 }
    120 
    121 void
    122 showicmp(void)
    123 {
    124 	u_long tin, tout;
    125 	int i;
    126 
    127 	for (i = tin = tout = 0; i <= ICMP_MAXTYPE; i++) {
    128 		tin += stats.icps_inhist[i];
    129 		tout += stats.icps_outhist[i];
    130 	}
    131 
    132 	tin += stats.icps_badcode + stats.icps_badlen + stats.icps_checksum +
    133 	    stats.icps_tooshort;
    134 	mvwprintw(wnd, 2, 0, "%9lu", tin);
    135 	mvwprintw(wnd, 2, 35, "%9lu", tout);
    136 
    137 	SHOW(icps_badcode, 3, 0);
    138 	SHOW(icps_badlen, 4, 0);
    139 	SHOW(icps_checksum, 5, 0);
    140 	SHOW(icps_tooshort, 6, 0);
    141 	SHOW(icps_error, 3, 35);
    142 	SHOW(icps_oldshort, 4, 35);
    143 	SHOW(icps_oldicmp, 5, 35);
    144 	SHOW(icps_reflect, 6, 35);
    145 
    146 	SHOW2(ICMP_ECHOREPLY, 9);
    147 	SHOW2(ICMP_ECHO, 10);
    148 	SHOW2(ICMP_UNREACH, 11);
    149 	SHOW2(ICMP_REDIRECT, 12);
    150 	SHOW2(ICMP_TIMXCEED, 13);
    151 	SHOW2(ICMP_PARAMPROB, 14);
    152 	SHOW(icps_inhist[ICMP_ROUTERADVERT], 15, 0);
    153 	SHOW(icps_outhist[ICMP_ROUTERSOLICIT], 15, 35);
    154 }
    155 
    156 int
    157 initicmp(void)
    158 {
    159 
    160 	if (namelist[0].n_type == 0) {
    161 		if (kvm_nlist(kd, namelist)) {
    162 			nlisterr(namelist);
    163 			return(0);
    164 		}
    165 		if (namelist[0].n_type == 0) {
    166 			error("No namelist");
    167 			return(0);
    168 		}
    169 	}
    170 
    171 	return (1);
    172 }
    173 
    174 void
    175 fetchicmp(void)
    176 {
    177 
    178 	KREAD((void *)namelist[0].n_value, &stats, sizeof(stats));
    179 }
    180