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