xref: /freebsd/usr.bin/netstat/if.c (revision 0b87c9157d9d4082e7350fe12048729b8e2224f3)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
3505ddff6eSPeter Wemm static char sccsid[] = "@(#)if.c	8.3 (Berkeley) 4/28/95";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <sys/types.h>
399b50d902SRodney W. Grimes #include <sys/protosw.h>
409b50d902SRodney W. Grimes #include <sys/socket.h>
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <net/if.h>
439b50d902SRodney W. Grimes #include <net/if_dl.h>
449b50d902SRodney W. Grimes #include <netinet/in.h>
459b50d902SRodney W. Grimes #include <netinet/in_var.h>
46cc6a66f2SJulian Elischer #include <netipx/ipx.h>
47cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
48cbc17e71SGarrett Wollman #ifdef NS
499b50d902SRodney W. Grimes #include <netns/ns.h>
509b50d902SRodney W. Grimes #include <netns/ns_if.h>
51cbc17e71SGarrett Wollman #endif
520761cb29SGarrett Wollman #ifdef ISO
539b50d902SRodney W. Grimes #include <netiso/iso.h>
549b50d902SRodney W. Grimes #include <netiso/iso_var.h>
550761cb29SGarrett Wollman #endif
569b50d902SRodney W. Grimes #include <arpa/inet.h>
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes #include <signal.h>
599b50d902SRodney W. Grimes #include <stdio.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <unistd.h>
629b50d902SRodney W. Grimes 
639b50d902SRodney W. Grimes #include "netstat.h"
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes #define	YES	1
669b50d902SRodney W. Grimes #define	NO	0
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes static void sidewaysintpr __P((u_int, u_long));
699b50d902SRodney W. Grimes static void catchalarm __P((int));
709b50d902SRodney W. Grimes 
719b50d902SRodney W. Grimes /*
729b50d902SRodney W. Grimes  * Print a description of the network interfaces.
739b50d902SRodney W. Grimes  */
749b50d902SRodney W. Grimes void
759b50d902SRodney W. Grimes intpr(interval, ifnetaddr)
769b50d902SRodney W. Grimes 	int interval;
779b50d902SRodney W. Grimes 	u_long ifnetaddr;
789b50d902SRodney W. Grimes {
799b50d902SRodney W. Grimes 	struct ifnet ifnet;
809b50d902SRodney W. Grimes 	union {
819b50d902SRodney W. Grimes 		struct ifaddr ifa;
829b50d902SRodney W. Grimes 		struct in_ifaddr in;
83cc6a66f2SJulian Elischer 		struct ipx_ifaddr ipx;
84cbc17e71SGarrett Wollman #ifdef NS
859b50d902SRodney W. Grimes 		struct ns_ifaddr ns;
86cbc17e71SGarrett Wollman #endif
870761cb29SGarrett Wollman #ifdef ISO
889b50d902SRodney W. Grimes 		struct iso_ifaddr iso;
890761cb29SGarrett Wollman #endif
909b50d902SRodney W. Grimes 	} ifaddr;
919b50d902SRodney W. Grimes 	u_long ifaddraddr;
929b50d902SRodney W. Grimes 	struct sockaddr *sa;
931b72e71cSDavid Greenman 	char name[32], tname[16];
949b50d902SRodney W. Grimes 
959b50d902SRodney W. Grimes 	if (ifnetaddr == 0) {
969b50d902SRodney W. Grimes 		printf("ifnet: symbol not defined\n");
979b50d902SRodney W. Grimes 		return;
989b50d902SRodney W. Grimes 	}
999b50d902SRodney W. Grimes 	if (interval) {
1009b50d902SRodney W. Grimes 		sidewaysintpr((unsigned)interval, ifnetaddr);
1019b50d902SRodney W. Grimes 		return;
1029b50d902SRodney W. Grimes 	}
1039b50d902SRodney W. Grimes 	if (kread(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr))
1049b50d902SRodney W. Grimes 		return;
105cc6a66f2SJulian Elischer 	printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
106e1e293a5SDavid Greenman 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
107e1e293a5SDavid Greenman 	if (bflag)
108e1e293a5SDavid Greenman 		printf(" %10.10s","Ibytes");
109e1e293a5SDavid Greenman 	printf(" %8.8s %5.5s", "Opkts", "Oerrs");
110e1e293a5SDavid Greenman 	if (bflag)
111e1e293a5SDavid Greenman 		printf(" %10.10s","Obytes");
1129b50d902SRodney W. Grimes 	printf(" %5s", "Coll");
1139b50d902SRodney W. Grimes 	if (tflag)
1149b50d902SRodney W. Grimes 		printf(" %s", "Time");
1159b50d902SRodney W. Grimes 	if (dflag)
1169b50d902SRodney W. Grimes 		printf(" %s", "Drop");
1179b50d902SRodney W. Grimes 	putchar('\n');
1189b50d902SRodney W. Grimes 	ifaddraddr = 0;
1199b50d902SRodney W. Grimes 	while (ifnetaddr || ifaddraddr) {
1209b50d902SRodney W. Grimes 		struct sockaddr_in *sin;
1219b50d902SRodney W. Grimes 		register char *cp;
1229b50d902SRodney W. Grimes 		int n, m;
1239b50d902SRodney W. Grimes 
1249b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
1259b50d902SRodney W. Grimes 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
1261b72e71cSDavid Greenman 			    kread((u_long)ifnet.if_name, tname, 16))
1279b50d902SRodney W. Grimes 				return;
1281b72e71cSDavid Greenman 			tname[15] = '\0';
1299b50d902SRodney W. Grimes 			ifnetaddr = (u_long)ifnet.if_next;
1301b72e71cSDavid Greenman 			snprintf(name, 32, "%s%d", tname, ifnet.if_unit);
1311b72e71cSDavid Greenman 			if (interface != 0 && (strcmp(name, interface) != 0))
1329b50d902SRodney W. Grimes 				continue;
1339b50d902SRodney W. Grimes 			cp = index(name, '\0');
1349b50d902SRodney W. Grimes 			if ((ifnet.if_flags&IFF_UP) == 0)
1359b50d902SRodney W. Grimes 				*cp++ = '*';
1369b50d902SRodney W. Grimes 			*cp = '\0';
1379b50d902SRodney W. Grimes 			ifaddraddr = (u_long)ifnet.if_addrlist;
1389b50d902SRodney W. Grimes 		}
1399b50d902SRodney W. Grimes 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
1409b50d902SRodney W. Grimes 		if (ifaddraddr == 0) {
141cc6a66f2SJulian Elischer 			printf("%-13.13s ", "none");
1429b50d902SRodney W. Grimes 			printf("%-15.15s ", "none");
1439b50d902SRodney W. Grimes 		} else {
1449b50d902SRodney W. Grimes 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
1459b50d902SRodney W. Grimes 				ifaddraddr = 0;
1469b50d902SRodney W. Grimes 				continue;
1479b50d902SRodney W. Grimes 			}
1489b50d902SRodney W. Grimes #define CP(x) ((char *)(x))
1499b50d902SRodney W. Grimes 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
1509b50d902SRodney W. Grimes 				CP(&ifaddr); sa = (struct sockaddr *)cp;
1519b50d902SRodney W. Grimes 			switch (sa->sa_family) {
1529b50d902SRodney W. Grimes 			case AF_UNSPEC:
153cc6a66f2SJulian Elischer 				printf("%-13.13s ", "none");
1549b50d902SRodney W. Grimes 				printf("%-15.15s ", "none");
1559b50d902SRodney W. Grimes 				break;
1569b50d902SRodney W. Grimes 			case AF_INET:
1579b50d902SRodney W. Grimes 				sin = (struct sockaddr_in *)sa;
1589b50d902SRodney W. Grimes #ifdef notdef
1599b50d902SRodney W. Grimes 				/* can't use inet_makeaddr because kernel
1609b50d902SRodney W. Grimes 				 * keeps nets unshifted.
1619b50d902SRodney W. Grimes 				 */
1629b50d902SRodney W. Grimes 				in = inet_makeaddr(ifaddr.in.ia_subnet,
1639b50d902SRodney W. Grimes 					INADDR_ANY);
16409a67ffaSStefan Eßer 				printf("%-13.13s ", netname(in.s_addr,
1659b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
1669b50d902SRodney W. Grimes #else
16709a67ffaSStefan Eßer 				printf("%-13.13s ",
1689b50d902SRodney W. Grimes 				    netname(htonl(ifaddr.in.ia_subnet),
1699b50d902SRodney W. Grimes 				    ifaddr.in.ia_subnetmask));
1709b50d902SRodney W. Grimes #endif
1719b50d902SRodney W. Grimes 				printf("%-15.15s ",
1729b50d902SRodney W. Grimes 				    routename(sin->sin_addr.s_addr));
1739b50d902SRodney W. Grimes 				break;
174cc6a66f2SJulian Elischer 			case AF_IPX:
175cc6a66f2SJulian Elischer 				{
176cc6a66f2SJulian Elischer 				struct sockaddr_ipx *sipx =
177cc6a66f2SJulian Elischer 					(struct sockaddr_ipx *)sa;
178cc6a66f2SJulian Elischer 				u_long net;
179cc6a66f2SJulian Elischer 				char netnum[10];
180cc6a66f2SJulian Elischer 
181cc6a66f2SJulian Elischer 				*(union ipx_net *) &net = sipx->sipx_addr.x_net;
182cc6a66f2SJulian Elischer 				sprintf(netnum, "%lx", ntohl(net));
183cc6a66f2SJulian Elischer 				printf("ipx:%-8s ", netnum);
184cc6a66f2SJulian Elischer /*				printf("ipx:%-8s ", netname(net, 0L)); */
185cc6a66f2SJulian Elischer 				printf("%-15s ",
186cc6a66f2SJulian Elischer 				    ipx_phost((struct sockaddr *)sipx));
187cc6a66f2SJulian Elischer 				}
188cc6a66f2SJulian Elischer 				break;
189cbc17e71SGarrett Wollman #ifdef NS
1909b50d902SRodney W. Grimes 			case AF_NS:
1919b50d902SRodney W. Grimes 				{
1929b50d902SRodney W. Grimes 				struct sockaddr_ns *sns =
1939b50d902SRodney W. Grimes 					(struct sockaddr_ns *)sa;
1949b50d902SRodney W. Grimes 				u_long net;
195cc6a66f2SJulian Elischer 				char netnum[10];
1969b50d902SRodney W. Grimes 
1979b50d902SRodney W. Grimes 				*(union ns_net *) &net = sns->sns_addr.x_net;
1989b50d902SRodney W. Grimes 				sprintf(netnum, "%lxH", ntohl(net));
1999b50d902SRodney W. Grimes 				upHex(netnum);
2009b50d902SRodney W. Grimes 				printf("ns:%-8s ", netnum);
2019b50d902SRodney W. Grimes 				printf("%-15s ",
2029b50d902SRodney W. Grimes 				    ns_phost((struct sockaddr *)sns));
2039b50d902SRodney W. Grimes 				}
2049b50d902SRodney W. Grimes 				break;
205cbc17e71SGarrett Wollman #endif
2069b50d902SRodney W. Grimes 			case AF_LINK:
2079b50d902SRodney W. Grimes 				{
2089b50d902SRodney W. Grimes 				struct sockaddr_dl *sdl =
2099b50d902SRodney W. Grimes 					(struct sockaddr_dl *)sa;
2109b50d902SRodney W. Grimes 				    cp = (char *)LLADDR(sdl);
2119b50d902SRodney W. Grimes 				    n = sdl->sdl_alen;
2129b50d902SRodney W. Grimes 				}
21305ddff6eSPeter Wemm 				m = printf("%-11.11s ", "<Link>");
2149b50d902SRodney W. Grimes 				goto hexprint;
2159b50d902SRodney W. Grimes 			default:
2169b50d902SRodney W. Grimes 				m = printf("(%d)", sa->sa_family);
2179b50d902SRodney W. Grimes 				for (cp = sa->sa_len + (char *)sa;
2189b50d902SRodney W. Grimes 					--cp > sa->sa_data && (*cp == 0);) {}
2199b50d902SRodney W. Grimes 				n = cp - sa->sa_data + 1;
2209b50d902SRodney W. Grimes 				cp = sa->sa_data;
2219b50d902SRodney W. Grimes 			hexprint:
2229b50d902SRodney W. Grimes 				while (--n >= 0)
223541f2562SDavid Greenman 					m += printf("%02x%c", *cp++ & 0xff,
2249b50d902SRodney W. Grimes 						    n > 0 ? '.' : ' ');
225cc6a66f2SJulian Elischer 				m = 30 - m;
2269b50d902SRodney W. Grimes 				while (m-- > 0)
2279b50d902SRodney W. Grimes 					putchar(' ');
2289b50d902SRodney W. Grimes 				break;
2299b50d902SRodney W. Grimes 			}
2309b50d902SRodney W. Grimes 			ifaddraddr = (u_long)ifaddr.ifa.ifa_next;
2319b50d902SRodney W. Grimes 		}
232e1e293a5SDavid Greenman 		printf("%8d %5d ",
233e1e293a5SDavid Greenman 		    ifnet.if_ipackets, ifnet.if_ierrors);
234e1e293a5SDavid Greenman 		if (bflag)
235e1e293a5SDavid Greenman 			printf("%10d ", ifnet.if_ibytes);
236e1e293a5SDavid Greenman 		printf("%8d %5d ",
237e1e293a5SDavid Greenman 		    ifnet.if_opackets, ifnet.if_oerrors);
238e1e293a5SDavid Greenman 		if (bflag)
239e1e293a5SDavid Greenman 			printf("%10d ", ifnet.if_obytes);
240e1e293a5SDavid Greenman 		printf("%5d", ifnet.if_collisions);
2419b50d902SRodney W. Grimes 		if (tflag)
2429b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_timer);
2439b50d902SRodney W. Grimes 		if (dflag)
2449b50d902SRodney W. Grimes 			printf(" %3d", ifnet.if_snd.ifq_drops);
2459b50d902SRodney W. Grimes 		putchar('\n');
2469b50d902SRodney W. Grimes 	}
2479b50d902SRodney W. Grimes }
2489b50d902SRodney W. Grimes 
2499b50d902SRodney W. Grimes #define	MAXIF	10
2509b50d902SRodney W. Grimes struct	iftot {
2519b50d902SRodney W. Grimes 	char	ift_name[16];		/* interface name */
252e1e293a5SDavid Greenman 	u_int	ift_ip;			/* input packets */
253e1e293a5SDavid Greenman 	u_int	ift_ie;			/* input errors */
254e1e293a5SDavid Greenman 	u_int	ift_op;			/* output packets */
255e1e293a5SDavid Greenman 	u_int	ift_oe;			/* output errors */
256e1e293a5SDavid Greenman 	u_int	ift_co;			/* collisions */
257e1e293a5SDavid Greenman 	u_int	ift_dr;			/* drops */
258e1e293a5SDavid Greenman 	u_int	ift_ib;			/* input bytes */
259e1e293a5SDavid Greenman 	u_int	ift_ob;			/* output bytes */
2609b50d902SRodney W. Grimes } iftot[MAXIF];
2619b50d902SRodney W. Grimes 
2629b50d902SRodney W. Grimes u_char	signalled;			/* set if alarm goes off "early" */
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes /*
2659b50d902SRodney W. Grimes  * Print a running summary of interface statistics.
2669b50d902SRodney W. Grimes  * Repeat display every interval seconds, showing statistics
2679b50d902SRodney W. Grimes  * collected over that interval.  Assumes that interval is non-zero.
2689b50d902SRodney W. Grimes  * First line printed at top of screen is always cumulative.
2699b50d902SRodney W. Grimes  */
2709b50d902SRodney W. Grimes static void
2719b50d902SRodney W. Grimes sidewaysintpr(interval, off)
2729b50d902SRodney W. Grimes 	unsigned interval;
2739b50d902SRodney W. Grimes 	u_long off;
2749b50d902SRodney W. Grimes {
2759b50d902SRodney W. Grimes 	struct ifnet ifnet;
2769b50d902SRodney W. Grimes 	u_long firstifnet;
2779b50d902SRodney W. Grimes 	register struct iftot *ip, *total;
2789b50d902SRodney W. Grimes 	register int line;
2799b50d902SRodney W. Grimes 	struct iftot *lastif, *sum, *interesting;
2800b87c915SDavid Greenman 	int oldmask, first;
2810b87c915SDavid Greenman 	u_long interesting_off;
2829b50d902SRodney W. Grimes 
2839b50d902SRodney W. Grimes 	if (kread(off, (char *)&firstifnet, sizeof (u_long)))
2849b50d902SRodney W. Grimes 		return;
2859b50d902SRodney W. Grimes 	lastif = iftot;
2869b50d902SRodney W. Grimes 	sum = iftot + MAXIF - 1;
2879b50d902SRodney W. Grimes 	total = sum - 1;
2880b87c915SDavid Greenman 	interesting = NULL;
2890b87c915SDavid Greenman 	interesting_off = 0;
2909b50d902SRodney W. Grimes 	for (off = firstifnet, ip = iftot; off;) {
2919b50d902SRodney W. Grimes 		char *cp;
2921b72e71cSDavid Greenman 		char name[16], tname[16];
2939b50d902SRodney W. Grimes 
2949b50d902SRodney W. Grimes 		if (kread(off, (char *)&ifnet, sizeof ifnet))
2959b50d902SRodney W. Grimes 			break;
2961b72e71cSDavid Greenman 		if (kread((u_long)ifnet.if_name, tname, 16))
2979b50d902SRodney W. Grimes 			break;
2981b72e71cSDavid Greenman 		tname[15] = '\0';
2991b72e71cSDavid Greenman 		snprintf(name, 16, "%s%d", tname, ifnet.if_unit);
3000b87c915SDavid Greenman 		if (interface && strcmp(name, interface) == 0) {
3019b50d902SRodney W. Grimes 			interesting = ip;
3020b87c915SDavid Greenman 			interesting_off = off;
3030b87c915SDavid Greenman 		}
3041b72e71cSDavid Greenman 		snprintf(ip->ift_name, 16, "(%s)", name);;
3059b50d902SRodney W. Grimes 		ip++;
3069b50d902SRodney W. Grimes 		if (ip >= iftot + MAXIF - 2)
3079b50d902SRodney W. Grimes 			break;
3089b50d902SRodney W. Grimes 		off = (u_long) ifnet.if_next;
3099b50d902SRodney W. Grimes 	}
3109b50d902SRodney W. Grimes 	lastif = ip;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	(void)signal(SIGALRM, catchalarm);
3139b50d902SRodney W. Grimes 	signalled = NO;
3149b50d902SRodney W. Grimes 	(void)alarm(interval);
3159b50d902SRodney W. Grimes 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
3169b50d902SRodney W. Grimes 		ip->ift_ip = 0;
3179b50d902SRodney W. Grimes 		ip->ift_ie = 0;
3180b87c915SDavid Greenman 		ip->ift_ib = 0;
3199b50d902SRodney W. Grimes 		ip->ift_op = 0;
3209b50d902SRodney W. Grimes 		ip->ift_oe = 0;
3210b87c915SDavid Greenman 		ip->ift_ob = 0;
3229b50d902SRodney W. Grimes 		ip->ift_co = 0;
3239b50d902SRodney W. Grimes 		ip->ift_dr = 0;
3249b50d902SRodney W. Grimes 	}
3250b87c915SDavid Greenman 	first = 1;
3260b87c915SDavid Greenman banner:
3270b87c915SDavid Greenman 	printf("%17s %14s %16s", "input",
3280b87c915SDavid Greenman 	    interesting ? interesting->ift_name : "(Total)", "output");
3299b50d902SRodney W. Grimes 	putchar('\n');
3300b87c915SDavid Greenman 	printf("%10s %5s %10s %10s %5s %10s %5s",
3310b87c915SDavid Greenman 	    "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
3329b50d902SRodney W. Grimes 	if (dflag)
3339b50d902SRodney W. Grimes 		printf(" %5.5s", "drops");
3349b50d902SRodney W. Grimes 	putchar('\n');
3359b50d902SRodney W. Grimes 	fflush(stdout);
3369b50d902SRodney W. Grimes 	line = 0;
3379b50d902SRodney W. Grimes loop:
3380b87c915SDavid Greenman 	if (interesting != NULL) {
3390b87c915SDavid Greenman 		ip = interesting;
3400b87c915SDavid Greenman 		if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
3410b87c915SDavid Greenman 			printf("???\n");
3420b87c915SDavid Greenman 			exit(1);
3430b87c915SDavid Greenman 		};
3440b87c915SDavid Greenman 		if (!first) {
3450b87c915SDavid Greenman 			printf("%10u %5u %10u %10u %5u %10u %5u",
3460b87c915SDavid Greenman 				ifnet.if_ipackets - ip->ift_ip,
3470b87c915SDavid Greenman 				ifnet.if_ierrors - ip->ift_ie,
3480b87c915SDavid Greenman 				ifnet.if_ibytes - ip->ift_ib,
3490b87c915SDavid Greenman 				ifnet.if_opackets - ip->ift_op,
3500b87c915SDavid Greenman 				ifnet.if_oerrors - ip->ift_oe,
3510b87c915SDavid Greenman 				ifnet.if_obytes - ip->ift_ob,
3520b87c915SDavid Greenman 				ifnet.if_collisions - ip->ift_co);
3530b87c915SDavid Greenman 			if (dflag)
3540b87c915SDavid Greenman 				printf(" %5u", ifnet.if_snd.ifq_drops - ip->ift_dr);
3550b87c915SDavid Greenman 		}
3560b87c915SDavid Greenman 		ip->ift_ip = ifnet.if_ipackets;
3570b87c915SDavid Greenman 		ip->ift_ie = ifnet.if_ierrors;
3580b87c915SDavid Greenman 		ip->ift_ib = ifnet.if_ibytes;
3590b87c915SDavid Greenman 		ip->ift_op = ifnet.if_opackets;
3600b87c915SDavid Greenman 		ip->ift_oe = ifnet.if_oerrors;
3610b87c915SDavid Greenman 		ip->ift_ob = ifnet.if_obytes;
3620b87c915SDavid Greenman 		ip->ift_co = ifnet.if_collisions;
3630b87c915SDavid Greenman 		ip->ift_dr = ifnet.if_snd.ifq_drops;
3640b87c915SDavid Greenman 	} else {
3659b50d902SRodney W. Grimes 		sum->ift_ip = 0;
3669b50d902SRodney W. Grimes 		sum->ift_ie = 0;
3670b87c915SDavid Greenman 		sum->ift_ib = 0;
3689b50d902SRodney W. Grimes 		sum->ift_op = 0;
3699b50d902SRodney W. Grimes 		sum->ift_oe = 0;
3700b87c915SDavid Greenman 		sum->ift_ob = 0;
3719b50d902SRodney W. Grimes 		sum->ift_co = 0;
3729b50d902SRodney W. Grimes 		sum->ift_dr = 0;
3739b50d902SRodney W. Grimes 		for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
3749b50d902SRodney W. Grimes 			if (kread(off, (char *)&ifnet, sizeof ifnet)) {
3759b50d902SRodney W. Grimes 				off = 0;
3769b50d902SRodney W. Grimes 				continue;
3779b50d902SRodney W. Grimes 			}
3780b87c915SDavid Greenman 			sum->ift_ip += ifnet.if_ipackets;
3790b87c915SDavid Greenman 			sum->ift_ie += ifnet.if_ierrors;
3800b87c915SDavid Greenman 			sum->ift_ib += ifnet.if_ibytes;
3810b87c915SDavid Greenman 			sum->ift_op += ifnet.if_opackets;
3820b87c915SDavid Greenman 			sum->ift_oe += ifnet.if_oerrors;
3830b87c915SDavid Greenman 			sum->ift_ob += ifnet.if_obytes;
3840b87c915SDavid Greenman 			sum->ift_co += ifnet.if_collisions;
3850b87c915SDavid Greenman 			sum->ift_dr += ifnet.if_snd.ifq_drops;
3869b50d902SRodney W. Grimes 			off = (u_long) ifnet.if_next;
3879b50d902SRodney W. Grimes 		}
3880b87c915SDavid Greenman 		if (!first) {
3890b87c915SDavid Greenman 			printf("%10u %5u %10u %10u %5u %10u %5u",
3909b50d902SRodney W. Grimes 				sum->ift_ip - total->ift_ip,
3910b87c915SDavid Greenman 				sum->ift_ie - total->ift_ie,
3920b87c915SDavid Greenman 				sum->ift_ib - total->ift_ib,
3939b50d902SRodney W. Grimes 				sum->ift_op - total->ift_op,
3940b87c915SDavid Greenman 				sum->ift_oe - total->ift_oe,
3950b87c915SDavid Greenman 				sum->ift_ob - total->ift_ob,
3960b87c915SDavid Greenman 				sum->ift_co - total->ift_co);
3979b50d902SRodney W. Grimes 			if (dflag)
3983aa80b1dSDavid Greenman 				printf(" %5u", sum->ift_dr - total->ift_dr);
3999b50d902SRodney W. Grimes 		}
4009b50d902SRodney W. Grimes 		*total = *sum;
4010b87c915SDavid Greenman 	}
4020b87c915SDavid Greenman 	if (!first)
4039b50d902SRodney W. Grimes 		putchar('\n');
4049b50d902SRodney W. Grimes 	fflush(stdout);
4059b50d902SRodney W. Grimes 	oldmask = sigblock(sigmask(SIGALRM));
4069b50d902SRodney W. Grimes 	if (! signalled) {
4079b50d902SRodney W. Grimes 		sigpause(0);
4089b50d902SRodney W. Grimes 	}
4099b50d902SRodney W. Grimes 	sigsetmask(oldmask);
4109b50d902SRodney W. Grimes 	signalled = NO;
4119b50d902SRodney W. Grimes 	(void)alarm(interval);
4120b87c915SDavid Greenman 	line++;
4130b87c915SDavid Greenman 	first = 0;
4149b50d902SRodney W. Grimes 	if (line == 21)
4159b50d902SRodney W. Grimes 		goto banner;
4160b87c915SDavid Greenman 	else
4179b50d902SRodney W. Grimes 		goto loop;
4189b50d902SRodney W. Grimes 	/*NOTREACHED*/
4199b50d902SRodney W. Grimes }
4209b50d902SRodney W. Grimes 
4219b50d902SRodney W. Grimes /*
4229b50d902SRodney W. Grimes  * Called if an interval expires before sidewaysintpr has completed a loop.
4239b50d902SRodney W. Grimes  * Sets a flag to not wait for the alarm.
4249b50d902SRodney W. Grimes  */
4259b50d902SRodney W. Grimes static void
4269b50d902SRodney W. Grimes catchalarm(signo)
4279b50d902SRodney W. Grimes 	int signo;
4289b50d902SRodney W. Grimes {
4299b50d902SRodney W. Grimes 	signalled = YES;
4309b50d902SRodney W. Grimes }
431