xref: /original-bsd/usr.bin/netstat/if.c (revision 957a0273)
1 #ifndef lint
2 static char sccsid[] = "@(#)if.c	4.3 82/10/07";
3 #endif
4 
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <net/in.h>
8 #include <net/if.h>
9 #include <stdio.h>
10 
11 extern	int kmem;
12 extern	int tflag;
13 extern	int nflag;
14 extern	char *routename();
15 
16 /*
17  * Print a description of the network interfaces.
18  */
19 intpr(interval, ifnetaddr)
20 	int interval;
21 	off_t ifnetaddr;
22 {
23 	struct ifnet ifnet;
24 	char name[16];
25 
26 	if (ifnetaddr == 0) {
27 		printf("ifnet: symbol not defined\n");
28 		return;
29 	}
30 	if (interval) {
31 		sidewaysintpr(interval, ifnetaddr);
32 		return;
33 	}
34 	klseek(kmem, ifnetaddr, 0);
35 	read(kmem, &ifnetaddr, sizeof ifnetaddr);
36 	printf("%-5.5s %-5.5s %-10.10s  %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
37 		"Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
38 		"Opkts", "Oerrs");
39 	printf(" %-6.6s", "Collis");
40 	if (tflag)
41 		printf(" %-6.6s", "Timer");
42 	putchar('\n');
43 	while (ifnetaddr) {
44 		struct sockaddr_in *sin;
45 		register char *cp;
46 		char *index();
47 		struct in_addr in, inet_makeaddr();
48 
49 		klseek(kmem, ifnetaddr, 0);
50 		read(kmem, &ifnet, sizeof ifnet);
51 		klseek(kmem, (int)ifnet.if_name, 0);
52 		read(kmem, name, 16);
53 		name[15] = '\0';
54 		cp = index(name, '\0');
55 		*cp++ = ifnet.if_unit + '0';
56 		if ((ifnet.if_flags&IFF_UP) == 0)
57 			*cp++ = '*';
58 		*cp = '\0';
59 		printf("%-5.5s %-5d ", name, ifnet.if_mtu);
60 		sin = (struct sockaddr_in *)&ifnet.if_addr;
61 		in = inet_makeaddr(ifnet.if_net, INADDR_ANY);
62 		printf("%-10.10s  ", routename(in));
63 		printf("%-12.12s %-7d %-5d %-7d %-5d %-6d",
64 		    routename(sin->sin_addr),
65 		    ifnet.if_ipackets, ifnet.if_ierrors,
66 		    ifnet.if_opackets, ifnet.if_oerrors,
67 		    ifnet.if_collisions);
68 		if (tflag)
69 			printf(" %-6d", ifnet.if_timer);
70 		putchar('\n');
71 		ifnetaddr = (off_t) ifnet.if_next;
72 	}
73 }
74 
75 #define	MAXIF	10
76 struct	iftot {
77 	char	ift_name[16];		/* interface name */
78 	int	ift_ip;			/* input packets */
79 	int	ift_ie;			/* input errors */
80 	int	ift_op;			/* output packets */
81 	int	ift_oe;			/* output errors */
82 	int	ift_co;			/* collisions */
83 } iftot[MAXIF];
84 
85 /*
86  * Print a running summary of interface statistics.
87  * Repeat display every interval seconds, showing
88  * statistics collected over that interval.  First
89  * line printed at top of screen is always cumulative.
90  */
91 sidewaysintpr(interval, off)
92 	int interval;
93 	off_t off;
94 {
95 	struct ifnet ifnet;
96 	off_t firstifnet;
97 	extern char _sobuf[];
98 	register struct iftot *ip, *total;
99 	register int line;
100 	struct iftot *lastif, *sum, *interesting;
101 	int maxtraffic;
102 
103 	setbuf(stdout, _sobuf);
104 	klseek(kmem, off, 0);
105 	read(kmem, &firstifnet, sizeof (off_t));
106 	lastif = iftot;
107 	sum = iftot + MAXIF - 1;
108 	total = sum - 1;
109 	for (off = firstifnet, ip = iftot; off;) {
110 		char *cp;
111 
112 		klseek(kmem, off, 0);
113 		read(kmem, &ifnet, sizeof ifnet);
114 		klseek(kmem, (int)ifnet.if_name, 0);
115 		ip->ift_name[0] = '(';
116 		read(kmem, ip->ift_name + 1, 15);
117 		ip->ift_name[15] = '\0';
118 		cp = index(ip->ift_name, '\0');
119 		sprintf(cp, "%d)", ifnet.if_unit);
120 		ip++;
121 		if (ip >= iftot + MAXIF - 2)
122 			break;
123 		off = (off_t) ifnet.if_next;
124 	}
125 	lastif = ip;
126 	interesting = iftot;
127 banner:
128 	printf("    input   %-6.6s    output       ", interesting->ift_name);
129 	if (lastif - iftot > 0)
130 		printf("    input   (Total)    output       ");
131 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
132 		ip->ift_ip = 0;
133 		ip->ift_ie = 0;
134 		ip->ift_op = 0;
135 		ip->ift_oe = 0;
136 		ip->ift_co = 0;
137 	}
138 	putchar('\n');
139 	printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
140 		"packets", "errs", "packets", "errs", "colls");
141 	if (lastif - iftot > 0)
142 		printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
143 			"packets", "errs", "packets", "errs", "colls");
144 	putchar('\n');
145 	fflush(stdout);
146 	line = 0;
147 loop:
148 	sum->ift_ip = 0;
149 	sum->ift_ie = 0;
150 	sum->ift_op = 0;
151 	sum->ift_oe = 0;
152 	sum->ift_co = 0;
153 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
154 		klseek(kmem, off, 0);
155 		read(kmem, &ifnet, sizeof ifnet);
156 		if (ip == interesting)
157 			printf("%-7d %-5d %-7d %-5d %-5d ",
158 				ifnet.if_ipackets - ip->ift_ip,
159 				ifnet.if_ierrors - ip->ift_ie,
160 				ifnet.if_opackets - ip->ift_op,
161 				ifnet.if_oerrors - ip->ift_oe,
162 				ifnet.if_collisions - ip->ift_co);
163 		ip->ift_ip = ifnet.if_ipackets;
164 		ip->ift_ie = ifnet.if_ierrors;
165 		ip->ift_op = ifnet.if_opackets;
166 		ip->ift_oe = ifnet.if_oerrors;
167 		ip->ift_co = ifnet.if_collisions;
168 		sum->ift_ip += ip->ift_ip;
169 		sum->ift_ie += ip->ift_ie;
170 		sum->ift_op += ip->ift_op;
171 		sum->ift_oe += ip->ift_oe;
172 		sum->ift_co += ip->ift_co;
173 		off = (off_t) ifnet.if_next;
174 	}
175 	if (lastif - iftot > 0)
176 		printf("%-7d %-5d %-7d %-5d %-5d\n",
177 			sum->ift_ip - total->ift_ip,
178 			sum->ift_ie - total->ift_ie,
179 			sum->ift_op - total->ift_op,
180 			sum->ift_oe - total->ift_oe,
181 			sum->ift_co - total->ift_co);
182 	*total = *sum;
183 	fflush(stdout);
184 	line++;
185 	if (interval)
186 		sleep(interval);
187 	if (line == 21)
188 		goto banner;
189 	goto loop;
190 	/*NOTREACHED*/
191 }
192