icmp.c revision 1.3 1 /* $NetBSD: icmp.c,v 1.3 2000/01/13 12:39:04 ad 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.3 2000/01/13 12:39:04 ad 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
52 #include "systat.h"
53 #include "extern.h"
54
55 #define LHD(row, str) mvwprintw(wnd, row, 10, str)
56 #define RHD(row, str) mvwprintw(wnd, row, 45, str);
57 #define BD(row, str) LHD(row, str); RHD(row, str)
58 #define SHOW(stat, row, col) \
59 mvwprintw(wnd, row, col, "%9llu", (unsigned long long)stats.stat)
60 #define SHOW2(type, row) SHOW(icps_inhist[type], row, 0); \
61 SHOW(icps_outhist[type], row, 35)
62
63 static struct icmpstat stats;
64
65 static struct nlist namelist[] = {
66 { "_icmpstat" },
67 { "" }
68 };
69
70 WINDOW *
71 openicmp(void)
72 {
73
74 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
75 }
76
77 void
78 closeicmp(w)
79 WINDOW *w;
80 {
81
82 if (w != NULL) {
83 wclear(w);
84 wrefresh(w);
85 delwin(w);
86 }
87 }
88
89 void
90 labelicmp(void)
91 {
92
93 wmove(wnd, 0, 0); wclrtoeol(wnd);
94
95 mvwprintw(wnd, 1, 0, "------------ ICMP input -------------");
96 mvwprintw(wnd, 1, 38, "------------- ICMP output -------------");
97
98 mvwprintw(wnd, 8, 0, "---------- Input histogram ----------");
99 mvwprintw(wnd, 8, 38, "---------- Output histogram -----------");
100
101 LHD(3, "with bad code");
102 LHD(4, "with bad length");
103 LHD(5, "with bad checksum");
104 LHD(6, "with insufficient data");
105
106 RHD(3, "errors generated");
107 RHD(4, "suppressed - original too short");
108 RHD(5, "suppressed - original was ICMP");
109 RHD(6, "responses sent");
110
111 BD(2, "total messages");
112 BD(9, "echo response");
113 BD(10, "echo request");
114 BD(11, "destination unreachable");
115 BD(12, "redirect");
116 BD(13, "time-to-live exceeded");
117 BD(14, "parameter problem");
118 LHD(15, "router advertisement");
119 RHD(15, "router solicitation");
120 }
121
122 void
123 showicmp(void)
124 {
125 u_long tin, tout;
126 int i;
127
128 for (i = tin = tout = 0; i <= ICMP_MAXTYPE; i++) {
129 tin += stats.icps_inhist[i];
130 tout += stats.icps_outhist[i];
131 }
132
133 tin += stats.icps_badcode + stats.icps_badlen + stats.icps_checksum +
134 stats.icps_tooshort;
135 mvwprintw(wnd, 2, 0, "%9lu", tin);
136 mvwprintw(wnd, 2, 35, "%9lu", tout);
137
138 SHOW(icps_badcode, 3, 0);
139 SHOW(icps_badlen, 4, 0);
140 SHOW(icps_checksum, 5, 0);
141 SHOW(icps_tooshort, 6, 0);
142 SHOW(icps_error, 3, 35);
143 SHOW(icps_oldshort, 4, 35);
144 SHOW(icps_oldicmp, 5, 35);
145 SHOW(icps_reflect, 6, 35);
146
147 SHOW2(ICMP_ECHOREPLY, 9);
148 SHOW2(ICMP_ECHO, 10);
149 SHOW2(ICMP_UNREACH, 11);
150 SHOW2(ICMP_REDIRECT, 12);
151 SHOW2(ICMP_TIMXCEED, 13);
152 SHOW2(ICMP_PARAMPROB, 14);
153 SHOW(icps_inhist[ICMP_ROUTERADVERT], 15, 0);
154 SHOW(icps_outhist[ICMP_ROUTERSOLICIT], 15, 35);
155 }
156
157 int
158 initicmp(void)
159 {
160
161 if (namelist[0].n_type == 0) {
162 if (kvm_nlist(kd, namelist)) {
163 nlisterr(namelist);
164 return(0);
165 }
166 if (namelist[0].n_type == 0) {
167 error("No namelist");
168 return(0);
169 }
170 }
171
172 return (1);
173 }
174
175 void
176 fetchicmp(void)
177 {
178
179 KREAD((void *)namelist[0].n_value, &stats, sizeof(stats));
180 }
181