xref: /csrg-svn/usr.bin/netstat/if.c (revision 9203)
17905Ssam #ifndef lint
2*9203Ssam static char sccsid[] = "@(#)if.c	4.4 82/11/14";
37905Ssam #endif
47905Ssam 
57905Ssam #include <sys/types.h>
67905Ssam #include <sys/socket.h>
7*9203Ssam 
87905Ssam #include <net/if.h>
9*9203Ssam #include <netinet/in.h>
10*9203Ssam 
118331Ssam #include <stdio.h>
127905Ssam 
137905Ssam extern	int kmem;
147905Ssam extern	int tflag;
157905Ssam extern	int nflag;
167905Ssam extern	char *routename();
177905Ssam 
187905Ssam /*
197905Ssam  * Print a description of the network interfaces.
207905Ssam  */
217905Ssam intpr(interval, ifnetaddr)
227905Ssam 	int interval;
237905Ssam 	off_t ifnetaddr;
247905Ssam {
257905Ssam 	struct ifnet ifnet;
267905Ssam 	char name[16];
277905Ssam 
287905Ssam 	if (ifnetaddr == 0) {
297905Ssam 		printf("ifnet: symbol not defined\n");
307905Ssam 		return;
317905Ssam 	}
327905Ssam 	if (interval) {
337905Ssam 		sidewaysintpr(interval, ifnetaddr);
347905Ssam 		return;
357905Ssam 	}
367905Ssam 	klseek(kmem, ifnetaddr, 0);
377905Ssam 	read(kmem, &ifnetaddr, sizeof ifnetaddr);
388372Ssam 	printf("%-5.5s %-5.5s %-10.10s  %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
397905Ssam 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
407905Ssam 		"Opkts", "Oerrs");
417905Ssam 	printf(" %-6.6s", "Collis");
427905Ssam 	if (tflag)
437905Ssam 		printf(" %-6.6s", "Timer");
447905Ssam 	putchar('\n');
457905Ssam 	while (ifnetaddr) {
467905Ssam 		struct sockaddr_in *sin;
477905Ssam 		register char *cp;
487905Ssam 		char *index();
498372Ssam 		struct in_addr in, inet_makeaddr();
507905Ssam 
517905Ssam 		klseek(kmem, ifnetaddr, 0);
527905Ssam 		read(kmem, &ifnet, sizeof ifnet);
537905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
547905Ssam 		read(kmem, name, 16);
557905Ssam 		name[15] = '\0';
567905Ssam 		cp = index(name, '\0');
577905Ssam 		*cp++ = ifnet.if_unit + '0';
587905Ssam 		if ((ifnet.if_flags&IFF_UP) == 0)
597905Ssam 			*cp++ = '*';
607905Ssam 		*cp = '\0';
617905Ssam 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
627905Ssam 		sin = (struct sockaddr_in *)&ifnet.if_addr;
638372Ssam 		in = inet_makeaddr(ifnet.if_net, INADDR_ANY);
648372Ssam 		printf("%-10.10s  ", routename(in));
657905Ssam 		printf("%-12.12s %-7d %-5d %-7d %-5d %-6d",
667905Ssam 		    routename(sin->sin_addr),
677905Ssam 		    ifnet.if_ipackets, ifnet.if_ierrors,
687905Ssam 		    ifnet.if_opackets, ifnet.if_oerrors,
697905Ssam 		    ifnet.if_collisions);
707905Ssam 		if (tflag)
717905Ssam 			printf(" %-6d", ifnet.if_timer);
727905Ssam 		putchar('\n');
737905Ssam 		ifnetaddr = (off_t) ifnet.if_next;
747905Ssam 	}
757905Ssam }
767905Ssam 
778331Ssam #define	MAXIF	10
788331Ssam struct	iftot {
798331Ssam 	char	ift_name[16];		/* interface name */
808331Ssam 	int	ift_ip;			/* input packets */
818331Ssam 	int	ift_ie;			/* input errors */
828331Ssam 	int	ift_op;			/* output packets */
838331Ssam 	int	ift_oe;			/* output errors */
848331Ssam 	int	ift_co;			/* collisions */
858331Ssam } iftot[MAXIF];
868331Ssam 
878331Ssam /*
888331Ssam  * Print a running summary of interface statistics.
898331Ssam  * Repeat display every interval seconds, showing
908331Ssam  * statistics collected over that interval.  First
918331Ssam  * line printed at top of screen is always cumulative.
928331Ssam  */
937905Ssam sidewaysintpr(interval, off)
947905Ssam 	int interval;
957905Ssam 	off_t off;
967905Ssam {
977905Ssam 	struct ifnet ifnet;
988331Ssam 	off_t firstifnet;
998331Ssam 	extern char _sobuf[];
1008331Ssam 	register struct iftot *ip, *total;
1018331Ssam 	register int line;
1028331Ssam 	struct iftot *lastif, *sum, *interesting;
1038331Ssam 	int maxtraffic;
1047905Ssam 
1058331Ssam 	setbuf(stdout, _sobuf);
1067905Ssam 	klseek(kmem, off, 0);
1078331Ssam 	read(kmem, &firstifnet, sizeof (off_t));
1088331Ssam 	lastif = iftot;
1098331Ssam 	sum = iftot + MAXIF - 1;
1108331Ssam 	total = sum - 1;
1118331Ssam 	for (off = firstifnet, ip = iftot; off;) {
1128331Ssam 		char *cp;
1137905Ssam 
1147905Ssam 		klseek(kmem, off, 0);
1157905Ssam 		read(kmem, &ifnet, sizeof ifnet);
1167905Ssam 		klseek(kmem, (int)ifnet.if_name, 0);
1178331Ssam 		ip->ift_name[0] = '(';
1188331Ssam 		read(kmem, ip->ift_name + 1, 15);
1198331Ssam 		ip->ift_name[15] = '\0';
1208331Ssam 		cp = index(ip->ift_name, '\0');
1218331Ssam 		sprintf(cp, "%d)", ifnet.if_unit);
1228331Ssam 		ip++;
1238331Ssam 		if (ip >= iftot + MAXIF - 2)
1248331Ssam 			break;
1257905Ssam 		off = (off_t) ifnet.if_next;
1267905Ssam 	}
1278331Ssam 	lastif = ip;
1288331Ssam 	interesting = iftot;
1298331Ssam banner:
1308331Ssam 	printf("    input   %-6.6s    output       ", interesting->ift_name);
1318331Ssam 	if (lastif - iftot > 0)
1328331Ssam 		printf("    input   (Total)    output       ");
1338331Ssam 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
1348331Ssam 		ip->ift_ip = 0;
1358331Ssam 		ip->ift_ie = 0;
1368331Ssam 		ip->ift_op = 0;
1378331Ssam 		ip->ift_oe = 0;
1388331Ssam 		ip->ift_co = 0;
1398331Ssam 	}
1408331Ssam 	putchar('\n');
1418331Ssam 	printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
1428331Ssam 		"packets", "errs", "packets", "errs", "colls");
1438331Ssam 	if (lastif - iftot > 0)
1448331Ssam 		printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
1458331Ssam 			"packets", "errs", "packets", "errs", "colls");
1468331Ssam 	putchar('\n');
1478331Ssam 	fflush(stdout);
1488331Ssam 	line = 0;
1498331Ssam loop:
1508331Ssam 	sum->ift_ip = 0;
1518331Ssam 	sum->ift_ie = 0;
1528331Ssam 	sum->ift_op = 0;
1538331Ssam 	sum->ift_oe = 0;
1548331Ssam 	sum->ift_co = 0;
1558331Ssam 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
1568331Ssam 		klseek(kmem, off, 0);
1578331Ssam 		read(kmem, &ifnet, sizeof ifnet);
1588331Ssam 		if (ip == interesting)
1598331Ssam 			printf("%-7d %-5d %-7d %-5d %-5d ",
1608331Ssam 				ifnet.if_ipackets - ip->ift_ip,
1618331Ssam 				ifnet.if_ierrors - ip->ift_ie,
1628331Ssam 				ifnet.if_opackets - ip->ift_op,
1638331Ssam 				ifnet.if_oerrors - ip->ift_oe,
1648331Ssam 				ifnet.if_collisions - ip->ift_co);
1658331Ssam 		ip->ift_ip = ifnet.if_ipackets;
1668331Ssam 		ip->ift_ie = ifnet.if_ierrors;
1678331Ssam 		ip->ift_op = ifnet.if_opackets;
1688331Ssam 		ip->ift_oe = ifnet.if_oerrors;
1698331Ssam 		ip->ift_co = ifnet.if_collisions;
1708331Ssam 		sum->ift_ip += ip->ift_ip;
1718331Ssam 		sum->ift_ie += ip->ift_ie;
1728331Ssam 		sum->ift_op += ip->ift_op;
1738331Ssam 		sum->ift_oe += ip->ift_oe;
1748331Ssam 		sum->ift_co += ip->ift_co;
1758331Ssam 		off = (off_t) ifnet.if_next;
1768331Ssam 	}
1778331Ssam 	if (lastif - iftot > 0)
1788331Ssam 		printf("%-7d %-5d %-7d %-5d %-5d\n",
1798331Ssam 			sum->ift_ip - total->ift_ip,
1808331Ssam 			sum->ift_ie - total->ift_ie,
1818331Ssam 			sum->ift_op - total->ift_op,
1828331Ssam 			sum->ift_oe - total->ift_oe,
1838331Ssam 			sum->ift_co - total->ift_co);
1848331Ssam 	*total = *sum;
1858331Ssam 	fflush(stdout);
1868331Ssam 	line++;
1878331Ssam 	if (interval)
1888331Ssam 		sleep(interval);
1898331Ssam 	if (line == 21)
1908331Ssam 		goto banner;
1918331Ssam 	goto loop;
1928331Ssam 	/*NOTREACHED*/
1937905Ssam }
194