Home | History | Annotate | Line # | Download | only in systat
ip.c revision 1.3
      1 /*	$NetBSD: ip.c,v 1.3 2000/01/04 15:17:00 msaitoh 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: ip.c,v 1.3 2000/01/04 15:17:00 msaitoh 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_var.h>
     44 #include <netinet/udp.h>
     45 #include <netinet/udp_var.h>
     46 
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <paths.h>
     50 #include <nlist.h>
     51 #include <kvm.h>
     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 SHOW(stat, row, col)	mvwprintw(wnd, row, col, "%9llu", curstat.stat)
     58 
     59 struct mystat {
     60 	struct ipstat i;
     61 	struct udpstat u;
     62 };
     63 
     64 static struct mystat curstat;
     65 
     66 static struct nlist namelist[] = {
     67 	{ "_ipstat" },
     68 	{ "_udpstat" },
     69 	{ "" }
     70 };
     71 
     72 WINDOW *
     73 openip(void)
     74 {
     75 
     76 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
     77 }
     78 
     79 void
     80 closeip(w)
     81 	WINDOW *w;
     82 {
     83 
     84 	if (w != NULL) {
     85 		wclear(w);
     86 		wrefresh(w);
     87 		delwin(w);
     88 	}
     89 }
     90 
     91 void
     92 labelip(void)
     93 {
     94 
     95 	wmove(wnd, 0, 0); wclrtoeol(wnd);
     96 
     97 	LHD(0,  "total packets received");
     98 	LHD(1,  "  passed to upper layers");
     99 	LHD(2,  "  with bad checksums");
    100 	LHD(3,  "  too short for header");
    101 	LHD(4,  "  too short for data");
    102 	LHD(5,  "  with invalid hlen");
    103 	LHD(6,  "  with invalid length");
    104 	LHD(7,  "  with invalid version");
    105 	LHD(8,  "  too large");
    106 	LHD(9,  "  option errors");
    107 	LHD(10, "  fragments received");
    108 	LHD(11, "  fragments dropped");
    109 	LHD(12, "  fragments timed out");
    110 	LHD(13, "  packets reassembled ok");
    111 
    112 	LHD(15, "packets forwarded");
    113 	LHD(16, "  fast forwarded");
    114 	LHD(17, "  unreachable dests");
    115 	LHD(18, "  redirects generated");
    116 
    117 	RHD(0,  "total packets sent");
    118 	RHD(1,  "  generated locally");
    119 	RHD(2,  "  output drops");
    120 	RHD(3,  "  output fragments generated");
    121 	RHD(4,  "  fragmentation failed");
    122 	RHD(5,  "  destinations unreachable");
    123 	RHD(6,  "  packets output via raw IP");
    124 	RHD(7,  "  total UDP packets sent");
    125 
    126 	RHD(9, "total UDP packets recieved");
    127 	RHD(10, "  too short for header");
    128 	RHD(11, "  invalid checksum");
    129 	RHD(12, "  invalid length");
    130 	RHD(13, "  no socket for dest port");
    131 	RHD(14, "  no socket for broadcast");
    132 	RHD(15, "  socket buffer full");
    133 }
    134 
    135 void
    136 showip(void)
    137 {
    138 	u_quad_t totalout;
    139 
    140 	totalout = curstat.i.ips_forward + curstat.i.ips_localout;
    141 
    142 	SHOW(i.ips_total, 0, 0);
    143 	mvwprintw(wnd, 0, 35, "%9llu", totalout);
    144 	SHOW(i.ips_delivered, 1, 0);
    145 	SHOW(i.ips_badsum, 2, 0);
    146 	SHOW(i.ips_tooshort, 3, 0);
    147 	SHOW(i.ips_toosmall, 4, 0);
    148 	SHOW(i.ips_badhlen, 5, 0);
    149 	SHOW(i.ips_badlen, 6, 0);
    150 	SHOW(i.ips_badvers, 7, 0);
    151 	SHOW(i.ips_toolong, 8, 0);
    152 	SHOW(i.ips_badoptions, 9, 0);
    153 
    154 	SHOW(i.ips_localout, 1, 35);
    155 	SHOW(i.ips_odropped, 2, 35);
    156 	SHOW(i.ips_ofragments, 3, 35);
    157 	SHOW(i.ips_cantfrag, 4, 35);
    158 	SHOW(i.ips_noroute, 5, 35);
    159 	SHOW(i.ips_rawout, 6, 35);
    160 	SHOW(u.udps_opackets, 7, 35);
    161 
    162 	SHOW(i.ips_fragments, 10, 0);
    163 	SHOW(i.ips_fragdropped, 11, 0);
    164 	SHOW(i.ips_fragtimeout, 12, 0);
    165 	SHOW(i.ips_reassembled, 13, 0);
    166 
    167 	SHOW(i.ips_forward, 15, 0);
    168 	SHOW(i.ips_fastforward, 16, 0);
    169 	SHOW(i.ips_cantforward, 17, 0);
    170 	SHOW(i.ips_redirectsent, 18, 0);
    171 
    172 	SHOW(u.udps_ipackets, 9, 35);
    173 	SHOW(u.udps_hdrops, 10, 35);
    174 	SHOW(u.udps_badsum, 11, 35);
    175 	SHOW(u.udps_badlen, 12, 35);
    176 	SHOW(u.udps_noport, 13, 35);
    177 	SHOW(u.udps_noportbcast, 14, 35);
    178 	SHOW(u.udps_fullsock, 15, 35);
    179 }
    180 
    181 int
    182 initip(void)
    183 {
    184 
    185 	if (namelist[0].n_type == 0) {
    186 		if (kvm_nlist(kd, namelist)) {
    187 			nlisterr(namelist);
    188 			return(0);
    189 		}
    190 		if ((namelist[0].n_type | namelist[1].n_type) == 0) {
    191 			error("No namelist");
    192 			return(0);
    193 		}
    194 	}
    195 	return 1;
    196 }
    197 
    198 void
    199 fetchip(void)
    200 {
    201 
    202 	KREAD((void *)namelist[0].n_value, &curstat.i, sizeof(curstat.i));
    203 	KREAD((void *)namelist[1].n_value, &curstat.u, sizeof(curstat.u));
    204 }
    205