xref: /csrg-svn/usr.bin/netstat/if.c (revision 17402)
17905Ssam #ifndef lint
2*17402Sedward static char sccsid[] = "@(#)if.c	4.6 84/11/20";
37905Ssam #endif
47905Ssam 
57905Ssam #include <sys/types.h>
67905Ssam #include <sys/socket.h>
79203Ssam 
87905Ssam #include <net/if.h>
99203Ssam #include <netinet/in.h>
109203Ssam 
118331Ssam #include <stdio.h>
127905Ssam 
137905Ssam extern	int kmem;
147905Ssam extern	int tflag;
157905Ssam extern	int nflag;
16*17402Sedward extern	char *interface;
17*17402Sedward extern	int unit;
187905Ssam extern	char *routename();
197905Ssam 
207905Ssam /*
217905Ssam  * Print a description of the network interfaces.
227905Ssam  */
237905Ssam intpr(interval, ifnetaddr)
247905Ssam 	int interval;
257905Ssam 	off_t ifnetaddr;
267905Ssam {
277905Ssam 	struct ifnet ifnet;
287905Ssam 	char name[16];
297905Ssam 
307905Ssam 	if (ifnetaddr == 0) {
317905Ssam 		printf("ifnet: symbol not defined\n");
327905Ssam 		return;
337905Ssam 	}
347905Ssam 	if (interval) {
357905Ssam 		sidewaysintpr(interval, ifnetaddr);
367905Ssam 		return;
377905Ssam 	}
387905Ssam 	klseek(kmem, ifnetaddr, 0);
397905Ssam 	read(kmem, &ifnetaddr, sizeof ifnetaddr);
408372Ssam 	printf("%-5.5s %-5.5s %-10.10s  %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
417905Ssam 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
427905Ssam 		"Opkts", "Oerrs");
437905Ssam 	printf(" %-6.6s", "Collis");
447905Ssam 	if (tflag)
457905Ssam 		printf(" %-6.6s", "Timer");
467905Ssam 	putchar('\n');
477905Ssam 	while (ifnetaddr) {
487905Ssam 		struct sockaddr_in *sin;
497905Ssam 		register char *cp;
507905Ssam 		char *index();
518372Ssam 		struct in_addr in, inet_makeaddr();
527905Ssam 
537905Ssam 		klseek(kmem, ifnetaddr, 0);
547905Ssam 		read(kmem, &ifnet, sizeof ifnet);
557905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
567905Ssam 		read(kmem, name, 16);
577905Ssam 		name[15] = '\0';
58*17402Sedward 		ifnetaddr = (off_t) ifnet.if_next;
59*17402Sedward 		if (interface != 0 &&
60*17402Sedward 		    (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
61*17402Sedward 			continue;
627905Ssam 		cp = index(name, '\0');
637905Ssam 		*cp++ = ifnet.if_unit + '0';
647905Ssam 		if ((ifnet.if_flags&IFF_UP) == 0)
657905Ssam 			*cp++ = '*';
667905Ssam 		*cp = '\0';
677905Ssam 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
687905Ssam 		sin = (struct sockaddr_in *)&ifnet.if_addr;
698372Ssam 		in = inet_makeaddr(ifnet.if_net, INADDR_ANY);
708372Ssam 		printf("%-10.10s  ", routename(in));
717905Ssam 		printf("%-12.12s %-7d %-5d %-7d %-5d %-6d",
727905Ssam 		    routename(sin->sin_addr),
737905Ssam 		    ifnet.if_ipackets, ifnet.if_ierrors,
747905Ssam 		    ifnet.if_opackets, ifnet.if_oerrors,
757905Ssam 		    ifnet.if_collisions);
767905Ssam 		if (tflag)
777905Ssam 			printf(" %-6d", ifnet.if_timer);
787905Ssam 		putchar('\n');
797905Ssam 	}
807905Ssam }
817905Ssam 
828331Ssam #define	MAXIF	10
838331Ssam struct	iftot {
848331Ssam 	char	ift_name[16];		/* interface name */
858331Ssam 	int	ift_ip;			/* input packets */
868331Ssam 	int	ift_ie;			/* input errors */
878331Ssam 	int	ift_op;			/* output packets */
888331Ssam 	int	ift_oe;			/* output errors */
898331Ssam 	int	ift_co;			/* collisions */
908331Ssam } iftot[MAXIF];
918331Ssam 
928331Ssam /*
938331Ssam  * Print a running summary of interface statistics.
948331Ssam  * Repeat display every interval seconds, showing
958331Ssam  * statistics collected over that interval.  First
968331Ssam  * line printed at top of screen is always cumulative.
978331Ssam  */
987905Ssam sidewaysintpr(interval, off)
997905Ssam 	int interval;
1007905Ssam 	off_t off;
1017905Ssam {
1027905Ssam 	struct ifnet ifnet;
1038331Ssam 	off_t firstifnet;
1048331Ssam 	register struct iftot *ip, *total;
1058331Ssam 	register int line;
1068331Ssam 	struct iftot *lastif, *sum, *interesting;
1078331Ssam 	int maxtraffic;
1087905Ssam 
1097905Ssam 	klseek(kmem, off, 0);
1108331Ssam 	read(kmem, &firstifnet, sizeof (off_t));
1118331Ssam 	lastif = iftot;
1128331Ssam 	sum = iftot + MAXIF - 1;
1138331Ssam 	total = sum - 1;
114*17402Sedward 	interesting = iftot;
1158331Ssam 	for (off = firstifnet, ip = iftot; off;) {
1168331Ssam 		char *cp;
1177905Ssam 
1187905Ssam 		klseek(kmem, off, 0);
1197905Ssam 		read(kmem, &ifnet, sizeof ifnet);
1207905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
1218331Ssam 		ip->ift_name[0] = '(';
1228331Ssam 		read(kmem, ip->ift_name + 1, 15);
123*17402Sedward 		if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
124*17402Sedward 		    unit == ifnet.if_unit)
125*17402Sedward 			interesting = ip;
1268331Ssam 		ip->ift_name[15] = '\0';
1278331Ssam 		cp = index(ip->ift_name, '\0');
1288331Ssam 		sprintf(cp, "%d)", ifnet.if_unit);
1298331Ssam 		ip++;
1308331Ssam 		if (ip >= iftot + MAXIF - 2)
1318331Ssam 			break;
1327905Ssam 		off = (off_t) ifnet.if_next;
1337905Ssam 	}
1348331Ssam 	lastif = ip;
1358331Ssam banner:
1368331Ssam 	printf("    input   %-6.6s    output       ", interesting->ift_name);
1378331Ssam 	if (lastif - iftot > 0)
138*17402Sedward 		printf("   input  (Total)    output       ");
1398331Ssam 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
1408331Ssam 		ip->ift_ip = 0;
1418331Ssam 		ip->ift_ie = 0;
1428331Ssam 		ip->ift_op = 0;
1438331Ssam 		ip->ift_oe = 0;
1448331Ssam 		ip->ift_co = 0;
1458331Ssam 	}
1468331Ssam 	putchar('\n');
1478331Ssam 	printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
1488331Ssam 		"packets", "errs", "packets", "errs", "colls");
1498331Ssam 	if (lastif - iftot > 0)
1508331Ssam 		printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
1518331Ssam 			"packets", "errs", "packets", "errs", "colls");
1528331Ssam 	putchar('\n');
1538331Ssam 	fflush(stdout);
1548331Ssam 	line = 0;
1558331Ssam loop:
1568331Ssam 	sum->ift_ip = 0;
1578331Ssam 	sum->ift_ie = 0;
1588331Ssam 	sum->ift_op = 0;
1598331Ssam 	sum->ift_oe = 0;
1608331Ssam 	sum->ift_co = 0;
1618331Ssam 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
1628331Ssam 		klseek(kmem, off, 0);
1638331Ssam 		read(kmem, &ifnet, sizeof ifnet);
1648331Ssam 		if (ip == interesting)
1658331Ssam 			printf("%-7d %-5d %-7d %-5d %-5d ",
1668331Ssam 				ifnet.if_ipackets - ip->ift_ip,
1678331Ssam 				ifnet.if_ierrors - ip->ift_ie,
1688331Ssam 				ifnet.if_opackets - ip->ift_op,
1698331Ssam 				ifnet.if_oerrors - ip->ift_oe,
1708331Ssam 				ifnet.if_collisions - ip->ift_co);
1718331Ssam 		ip->ift_ip = ifnet.if_ipackets;
1728331Ssam 		ip->ift_ie = ifnet.if_ierrors;
1738331Ssam 		ip->ift_op = ifnet.if_opackets;
1748331Ssam 		ip->ift_oe = ifnet.if_oerrors;
1758331Ssam 		ip->ift_co = ifnet.if_collisions;
1768331Ssam 		sum->ift_ip += ip->ift_ip;
1778331Ssam 		sum->ift_ie += ip->ift_ie;
1788331Ssam 		sum->ift_op += ip->ift_op;
1798331Ssam 		sum->ift_oe += ip->ift_oe;
1808331Ssam 		sum->ift_co += ip->ift_co;
1818331Ssam 		off = (off_t) ifnet.if_next;
1828331Ssam 	}
1838331Ssam 	if (lastif - iftot > 0)
1848331Ssam 		printf("%-7d %-5d %-7d %-5d %-5d\n",
1858331Ssam 			sum->ift_ip - total->ift_ip,
1868331Ssam 			sum->ift_ie - total->ift_ie,
1878331Ssam 			sum->ift_op - total->ift_op,
1888331Ssam 			sum->ift_oe - total->ift_oe,
1898331Ssam 			sum->ift_co - total->ift_co);
1908331Ssam 	*total = *sum;
1918331Ssam 	fflush(stdout);
1928331Ssam 	line++;
1938331Ssam 	if (interval)
1948331Ssam 		sleep(interval);
1958331Ssam 	if (line == 21)
1968331Ssam 		goto banner;
1978331Ssam 	goto loop;
1988331Ssam 	/*NOTREACHED*/
1997905Ssam }
200