]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/systat/ip.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / systat / ip.c
1 /*-
2  * Copyright (c) 1980, 1992, 1993
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 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 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD$");
37
38 #ifdef lint
39 static const char sccsid[] = "@(#)mbufs.c       8.1 (Berkeley) 6/6/93";
40 #endif
41
42 /* From:
43         "Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
44 */
45
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/udp.h>
56 #include <netinet/udp_var.h>
57
58 #include <stdlib.h>
59 #include <string.h>
60 #include <paths.h>
61
62 #include "systat.h"
63 #include "extern.h"
64 #include "mode.h"
65
66 struct stat {
67         struct ipstat i;
68         struct udpstat u;
69 };
70
71 static struct stat curstat, initstat, oldstat;
72
73 /*-
74 --0         1         2         3         4         5         6         7
75 --0123456789012345678901234567890123456789012345678901234567890123456789012345
76 00          IP Input                           IP Output
77 01999999999 total packets received   999999999 total packets sent
78 02999999999 - with bad checksums     999999999 - generated locally
79 03999999999 - too short for header   999999999 - output drops
80 04999999999 - too short for data     999999999 output fragments generated
81 05999999999 - with invalid hlen      999999999 - fragmentation failed
82 06999999999 - with invalid length    999999999 destinations unreachable
83 07999999999 - with invalid version   999999999 packets output via raw IP
84 08999999999 - jumbograms
85 09999999999 total fragments received           UDP Statistics
86 10999999999 - fragments dropped      999999999 total input packets
87 11999999999 - fragments timed out    999999999 - too short for header
88 12999999999 - packets reassembled ok 999999999 - invalid checksum
89 13999999999 packets forwarded        999999999 - no checksum
90 14999999999 - unreachable dests      999999999 - invalid length
91 15999999999 - redirects generated    999999999 - no socket for dest port
92 16999999999 option errors            999999999 - no socket for broadcast
93 17999999999 unwanted multicasts      999999999 - socket buffer full
94 18999999999 delivered to upper layer 999999999 total output packets
95 --0123456789012345678901234567890123456789012345678901234567890123456789012345
96 --0         1         2         3         4         5         6         7
97 */
98
99 WINDOW *
100 openip(void)
101 {
102         return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
103 }
104
105 void
106 closeip(w)
107         WINDOW *w;
108 {
109         if (w == NULL)
110                 return;
111         wclear(w);
112         wrefresh(w);
113         delwin(w);
114 }
115
116 void
117 labelip(void)
118 {
119         wmove(wnd, 0, 0); wclrtoeol(wnd);
120 #define L(row, str) mvwprintw(wnd, row, 10, str)
121 #define R(row, str) mvwprintw(wnd, row, 45, str);
122         L(0, "IP Input");               R(0, "IP Output");
123         L(1, "total packets received"); R(1, "total packets sent");
124         L(2, "- with bad checksums");   R(2, "- generated locally");
125         L(3, "- too short for header"); R(3, "- output drops");
126         L(4, "- too short for data");   R(4, "output fragments generated");
127         L(5, "- with invalid hlen");    R(5, "- fragmentation failed");
128         L(6, "- with invalid length");  R(6, "destinations unreachable");
129         L(7, "- with invalid version"); R(7, "packets output via raw IP");
130         L(8, "- jumbograms");
131         L(9, "total fragments received");       R(9, "UDP Statistics");
132         L(10, "- fragments dropped");   R(10, "total input packets");
133         L(11, "- fragments timed out"); R(11, "- too short for header");
134         L(12, "- packets reassembled ok");      R(12, "- invalid checksum");
135         L(13, "packets forwarded");     R(13, "- no checksum");
136         L(14, "- unreachable dests");   R(14, "- invalid length");
137         L(15, "- redirects generated"); R(15, "- no socket for dest port");
138         L(16, "option errors");         R(16, "- no socket for broadcast");
139         L(17, "unwanted multicasts");   R(17, "- socket buffer full");
140         L(18, "delivered to upper layer");      R(18, "total output packets");
141 #undef L
142 #undef R
143 }
144
145 static void
146 domode(struct stat *ret)
147 {
148         const struct stat *sub;
149         int divisor = 1;
150
151         switch(currentmode) {
152         case display_RATE:
153                 sub = &oldstat;
154                 divisor = naptime;
155                 break;
156         case display_DELTA:
157                 sub = &oldstat;
158                 break;
159         case display_SINCE:
160                 sub = &initstat;
161                 break;
162         default:
163                 *ret = curstat;
164                 return;
165         }
166 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
167         DO(i.ips_total);
168         DO(i.ips_badsum);
169         DO(i.ips_tooshort);
170         DO(i.ips_toosmall);
171         DO(i.ips_badhlen);
172         DO(i.ips_badlen);
173         DO(i.ips_fragments);
174         DO(i.ips_fragdropped);
175         DO(i.ips_fragtimeout);
176         DO(i.ips_forward);
177         DO(i.ips_cantforward);
178         DO(i.ips_redirectsent);
179         DO(i.ips_noproto);
180         DO(i.ips_delivered);
181         DO(i.ips_localout);
182         DO(i.ips_odropped);
183         DO(i.ips_reassembled);
184         DO(i.ips_fragmented);
185         DO(i.ips_ofragments);
186         DO(i.ips_cantfrag);
187         DO(i.ips_badoptions);
188         DO(i.ips_noroute);
189         DO(i.ips_badvers);
190         DO(i.ips_rawout);
191         DO(i.ips_toolong);
192         DO(i.ips_notmember);
193         DO(u.udps_ipackets);
194         DO(u.udps_hdrops);
195         DO(u.udps_badsum);
196         DO(u.udps_nosum);
197         DO(u.udps_badlen);
198         DO(u.udps_noport);
199         DO(u.udps_noportbcast);
200         DO(u.udps_fullsock);
201         DO(u.udps_opackets);
202 #undef DO
203 }
204
205 void
206 showip(void)
207 {
208         struct stat stats;
209         u_long totalout;
210
211         domode(&stats);
212         totalout = stats.i.ips_forward + stats.i.ips_localout;
213
214 #define DO(stat, row, col) \
215         mvwprintw(wnd, row, col, "%9lu", stats.stat)
216
217         DO(i.ips_total, 1, 0);
218         mvwprintw(wnd, 1, 35, "%9lu", totalout);
219         DO(i.ips_badsum, 2, 0);
220         DO(i.ips_localout, 2, 35);
221         DO(i.ips_tooshort, 3, 0);
222         DO(i.ips_odropped, 3, 35);
223         DO(i.ips_toosmall, 4, 0);
224         DO(i.ips_ofragments, 4, 35);
225         DO(i.ips_badhlen, 5, 0);
226         DO(i.ips_cantfrag, 5, 35);
227         DO(i.ips_badlen, 6, 0);
228         DO(i.ips_noroute, 6, 35);
229         DO(i.ips_badvers, 7, 0);
230         DO(i.ips_rawout, 7, 35);
231         DO(i.ips_toolong, 8, 0);
232         DO(i.ips_fragments, 9, 0);
233         DO(i.ips_fragdropped, 10, 0);
234         DO(u.udps_ipackets, 10, 35);
235         DO(i.ips_fragtimeout, 11, 0);
236         DO(u.udps_hdrops, 11, 35);
237         DO(i.ips_reassembled, 12, 0);
238         DO(u.udps_badsum, 12, 35);
239         DO(i.ips_forward, 13, 0);
240         DO(u.udps_nosum, 13, 35);
241         DO(i.ips_cantforward, 14, 0);
242         DO(u.udps_badlen, 14, 35);
243         DO(i.ips_redirectsent, 15, 0);
244         DO(u.udps_noport, 15, 35);
245         DO(i.ips_badoptions, 16, 0);
246         DO(u.udps_noportbcast, 16, 35);
247         DO(i.ips_notmember, 17, 0);
248         DO(u.udps_fullsock, 17, 35);
249         DO(i.ips_delivered, 18, 0);
250         DO(u.udps_opackets, 18, 35);
251 #undef DO
252 }
253
254 int
255 initip(void)
256 {
257         size_t len;
258         int name[4];
259
260         name[0] = CTL_NET;
261         name[1] = PF_INET;
262         name[2] = IPPROTO_IP;
263         name[3] = IPCTL_STATS;
264
265         len = 0;
266         if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
267                 error("sysctl getting ipstat size failed");
268                 return 0;
269         }
270         if (len > sizeof curstat.i) {
271                 error("ipstat structure has grown--recompile systat!");
272                 return 0;
273         }
274         if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
275                 error("sysctl getting ipstat failed");
276                 return 0;
277         }
278         name[2] = IPPROTO_UDP;
279         name[3] = UDPCTL_STATS;
280
281         len = 0;
282         if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
283                 error("sysctl getting udpstat size failed");
284                 return 0;
285         }
286         if (len > sizeof curstat.u) {
287                 error("ipstat structure has grown--recompile systat!");
288                 return 0;
289         }
290         if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
291                 error("sysctl getting udpstat failed");
292                 return 0;
293         }
294         oldstat = initstat;
295         return 1;
296 }
297
298 void
299 resetip(void)
300 {
301         size_t len;
302         int name[4];
303
304         name[0] = CTL_NET;
305         name[1] = PF_INET;
306         name[2] = IPPROTO_IP;
307         name[3] = IPCTL_STATS;
308
309         len = sizeof initstat.i;
310         if (sysctl(name, 4, &initstat.i, &len, 0, 0) < 0) {
311                 error("sysctl getting ipstat failed");
312         }
313         name[2] = IPPROTO_UDP;
314         name[3] = UDPCTL_STATS;
315
316         len = sizeof initstat.u;
317         if (sysctl(name, 4, &initstat.u, &len, 0, 0) < 0) {
318                 error("sysctl getting udpstat failed");
319         }
320         oldstat = initstat;
321 }
322
323 void
324 fetchip(void)
325 {
326         int name[4];
327         size_t len;
328
329         oldstat = curstat;
330         name[0] = CTL_NET;
331         name[1] = PF_INET;
332         name[2] = IPPROTO_IP;
333         name[3] = IPCTL_STATS;
334         len = sizeof curstat.i;
335
336         if (sysctl(name, 4, &curstat.i, &len, 0, 0) < 0)
337                 return;
338         name[2] = IPPROTO_UDP;
339         name[3] = UDPCTL_STATS;
340         len = sizeof curstat.u;
341
342         if (sysctl(name, 4, &curstat.u, &len, 0, 0) < 0)
343                 return;
344 }
345