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