xref: /csrg-svn/usr.bin/netstat/if.c (revision 8331)
17905Ssam #ifndef lint
2*8331Ssam static char sccsid[] = "@(#)if.c	4.2 82/10/06";
37905Ssam #endif
47905Ssam 
57905Ssam #include <sys/types.h>
67905Ssam #include <sys/socket.h>
77905Ssam #include <net/in.h>
87905Ssam #include <net/if.h>
9*8331Ssam #include <stdio.h>
107905Ssam 
117905Ssam extern	int kmem;
127905Ssam extern	int tflag;
137905Ssam extern	int nflag;
147905Ssam extern	char *routename();
157905Ssam 
167905Ssam /*
177905Ssam  * Print a description of the network interfaces.
187905Ssam  */
197905Ssam intpr(interval, ifnetaddr)
207905Ssam 	int interval;
217905Ssam 	off_t ifnetaddr;
227905Ssam {
237905Ssam 	struct ifnet ifnet;
247905Ssam 	char name[16];
257905Ssam 
267905Ssam 	if (ifnetaddr == 0) {
277905Ssam 		printf("ifnet: symbol not defined\n");
287905Ssam 		return;
297905Ssam 	}
307905Ssam 	if (interval) {
317905Ssam 		sidewaysintpr(interval, ifnetaddr);
327905Ssam 		return;
337905Ssam 	}
347905Ssam 	klseek(kmem, ifnetaddr, 0);
357905Ssam 	read(kmem, &ifnetaddr, sizeof ifnetaddr);
36*8331Ssam 	printf("%-5.5s %-5.5s %-8.8s  %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
377905Ssam 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
387905Ssam 		"Opkts", "Oerrs");
397905Ssam 	printf(" %-6.6s", "Collis");
407905Ssam 	if (tflag)
417905Ssam 		printf(" %-6.6s", "Timer");
427905Ssam 	putchar('\n');
437905Ssam 	while (ifnetaddr) {
447905Ssam 		struct sockaddr_in *sin;
457905Ssam 		register char *cp;
467905Ssam 		char *index();
477905Ssam 
487905Ssam 		klseek(kmem, ifnetaddr, 0);
497905Ssam 		read(kmem, &ifnet, sizeof ifnet);
507905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
517905Ssam 		read(kmem, name, 16);
527905Ssam 		name[15] = '\0';
537905Ssam 		cp = index(name, '\0');
547905Ssam 		*cp++ = ifnet.if_unit + '0';
557905Ssam 		if ((ifnet.if_flags&IFF_UP) == 0)
567905Ssam 			*cp++ = '*';
577905Ssam 		*cp = '\0';
587905Ssam 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
597905Ssam 		sin = (struct sockaddr_in *)&ifnet.if_addr;
60*8331Ssam 		printf("%-8.8s  ", routename(ifnet.if_net));
617905Ssam 		printf("%-12.12s %-7d %-5d %-7d %-5d %-6d",
627905Ssam 		    routename(sin->sin_addr),
637905Ssam 		    ifnet.if_ipackets, ifnet.if_ierrors,
647905Ssam 		    ifnet.if_opackets, ifnet.if_oerrors,
657905Ssam 		    ifnet.if_collisions);
667905Ssam 		if (tflag)
677905Ssam 			printf(" %-6d", ifnet.if_timer);
687905Ssam 		putchar('\n');
697905Ssam 		ifnetaddr = (off_t) ifnet.if_next;
707905Ssam 	}
717905Ssam }
727905Ssam 
73*8331Ssam #define	MAXIF	10
74*8331Ssam struct	iftot {
75*8331Ssam 	char	ift_name[16];		/* interface name */
76*8331Ssam 	int	ift_ip;			/* input packets */
77*8331Ssam 	int	ift_ie;			/* input errors */
78*8331Ssam 	int	ift_op;			/* output packets */
79*8331Ssam 	int	ift_oe;			/* output errors */
80*8331Ssam 	int	ift_co;			/* collisions */
81*8331Ssam } iftot[MAXIF];
82*8331Ssam 
83*8331Ssam /*
84*8331Ssam  * Print a running summary of interface statistics.
85*8331Ssam  * Repeat display every interval seconds, showing
86*8331Ssam  * statistics collected over that interval.  First
87*8331Ssam  * line printed at top of screen is always cumulative.
88*8331Ssam  */
897905Ssam sidewaysintpr(interval, off)
907905Ssam 	int interval;
917905Ssam 	off_t off;
927905Ssam {
937905Ssam 	struct ifnet ifnet;
94*8331Ssam 	off_t firstifnet;
95*8331Ssam 	extern char _sobuf[];
96*8331Ssam 	register struct iftot *ip, *total;
97*8331Ssam 	register int line;
98*8331Ssam 	struct iftot *lastif, *sum, *interesting;
99*8331Ssam 	int maxtraffic;
1007905Ssam 
101*8331Ssam 	setbuf(stdout, _sobuf);
1027905Ssam 	klseek(kmem, off, 0);
103*8331Ssam 	read(kmem, &firstifnet, sizeof (off_t));
104*8331Ssam 	lastif = iftot;
105*8331Ssam 	sum = iftot + MAXIF - 1;
106*8331Ssam 	total = sum - 1;
107*8331Ssam 	for (off = firstifnet, ip = iftot; off;) {
108*8331Ssam 		char *cp;
1097905Ssam 
1107905Ssam 		klseek(kmem, off, 0);
1117905Ssam 		read(kmem, &ifnet, sizeof ifnet);
1127905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
113*8331Ssam 		ip->ift_name[0] = '(';
114*8331Ssam 		read(kmem, ip->ift_name + 1, 15);
115*8331Ssam 		ip->ift_name[15] = '\0';
116*8331Ssam 		cp = index(ip->ift_name, '\0');
117*8331Ssam 		sprintf(cp, "%d)", ifnet.if_unit);
118*8331Ssam 		ip++;
119*8331Ssam 		if (ip >= iftot + MAXIF - 2)
120*8331Ssam 			break;
1217905Ssam 		off = (off_t) ifnet.if_next;
1227905Ssam 	}
123*8331Ssam 	lastif = ip;
124*8331Ssam 	interesting = iftot;
125*8331Ssam banner:
126*8331Ssam 	printf("    input   %-6.6s    output       ", interesting->ift_name);
127*8331Ssam 	if (lastif - iftot > 0)
128*8331Ssam 		printf("    input   (Total)    output       ");
129*8331Ssam 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
130*8331Ssam 		ip->ift_ip = 0;
131*8331Ssam 		ip->ift_ie = 0;
132*8331Ssam 		ip->ift_op = 0;
133*8331Ssam 		ip->ift_oe = 0;
134*8331Ssam 		ip->ift_co = 0;
135*8331Ssam 	}
136*8331Ssam 	putchar('\n');
137*8331Ssam 	printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
138*8331Ssam 		"packets", "errs", "packets", "errs", "colls");
139*8331Ssam 	if (lastif - iftot > 0)
140*8331Ssam 		printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
141*8331Ssam 			"packets", "errs", "packets", "errs", "colls");
142*8331Ssam 	putchar('\n');
143*8331Ssam 	fflush(stdout);
144*8331Ssam 	line = 0;
145*8331Ssam loop:
146*8331Ssam 	sum->ift_ip = 0;
147*8331Ssam 	sum->ift_ie = 0;
148*8331Ssam 	sum->ift_op = 0;
149*8331Ssam 	sum->ift_oe = 0;
150*8331Ssam 	sum->ift_co = 0;
151*8331Ssam 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
152*8331Ssam 		klseek(kmem, off, 0);
153*8331Ssam 		read(kmem, &ifnet, sizeof ifnet);
154*8331Ssam 		if (ip == interesting)
155*8331Ssam 			printf("%-7d %-5d %-7d %-5d %-5d ",
156*8331Ssam 				ifnet.if_ipackets - ip->ift_ip,
157*8331Ssam 				ifnet.if_ierrors - ip->ift_ie,
158*8331Ssam 				ifnet.if_opackets - ip->ift_op,
159*8331Ssam 				ifnet.if_oerrors - ip->ift_oe,
160*8331Ssam 				ifnet.if_collisions - ip->ift_co);
161*8331Ssam 		ip->ift_ip = ifnet.if_ipackets;
162*8331Ssam 		ip->ift_ie = ifnet.if_ierrors;
163*8331Ssam 		ip->ift_op = ifnet.if_opackets;
164*8331Ssam 		ip->ift_oe = ifnet.if_oerrors;
165*8331Ssam 		ip->ift_co = ifnet.if_collisions;
166*8331Ssam 		sum->ift_ip += ip->ift_ip;
167*8331Ssam 		sum->ift_ie += ip->ift_ie;
168*8331Ssam 		sum->ift_op += ip->ift_op;
169*8331Ssam 		sum->ift_oe += ip->ift_oe;
170*8331Ssam 		sum->ift_co += ip->ift_co;
171*8331Ssam 		off = (off_t) ifnet.if_next;
172*8331Ssam 	}
173*8331Ssam 	if (lastif - iftot > 0)
174*8331Ssam 		printf("%-7d %-5d %-7d %-5d %-5d\n",
175*8331Ssam 			sum->ift_ip - total->ift_ip,
176*8331Ssam 			sum->ift_ie - total->ift_ie,
177*8331Ssam 			sum->ift_op - total->ift_op,
178*8331Ssam 			sum->ift_oe - total->ift_oe,
179*8331Ssam 			sum->ift_co - total->ift_co);
180*8331Ssam 	*total = *sum;
181*8331Ssam 	fflush(stdout);
182*8331Ssam 	line++;
183*8331Ssam 	if (interval)
184*8331Ssam 		sleep(interval);
185*8331Ssam 	if (line == 21)
186*8331Ssam 		goto banner;
187*8331Ssam 	goto loop;
188*8331Ssam 	/*NOTREACHED*/
1897905Ssam }
190