1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #if defined(DO_SCCS) && !defined(lint)
8: static char sccsid[] = "@(#)if.c 5.3 (Berkeley) 4/23/86";
9: #endif
10:
11: #include <sys/types.h>
12: #include <sys/socket.h>
13:
14: #include <net/if.h>
15: #include <netdb.h>
16: #include <netinet/in.h>
17: #include <netinet/in_var.h>
18: #include <netns/ns.h>
19:
20: #include <stdio.h>
21: #include "net.h"
22:
23: extern int kmem;
24: extern int tflag;
25: extern int nflag;
26: extern char *interface;
27: extern int unit;
28: extern char *routename(), *netname();
29:
30: /*
31: * Print a description of the network interfaces.
32: */
33: intpr(interval, ifnetaddr)
34: int interval;
35: off_t ifnetaddr;
36: {
37: struct ifnet ifnet;
38: union {
39: struct ifaddr ifa;
40: struct in_ifaddr in;
41: } ifaddr;
42: off_t ifaddraddr;
43: char name[16];
44:
45: if (ifnetaddr == 0) {
46: printf("ifnet: symbol not defined\n");
47: return;
48: }
49: #ifndef CRASH
50: if (interval) {
51: sidewaysintpr(interval, ifnetaddr);
52: return;
53: }
54: #endif !CRASH
55: klseek(kmem, (off_t)ifnetaddr, 0);
56: #if pdp11
57: { unsigned x;
58: read(kmem, &x, sizeof x);
59: ifnetaddr = (long)x;
60: }
61: #else
62: read(kmem, &ifnetaddr, sizeof ifnetaddr);
63: #endif
64: printf("%-5.5s %-5.5s %-10.10s %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
65: "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
66: "Opkts", "Oerrs");
67: printf(" %-6.6s", "Collis");
68: if (tflag)
69: printf(" %-6.6s", "Timer");
70: putchar('\n');
71: ifaddraddr = 0;
72: while (ifnetaddr || ifaddraddr) {
73: struct sockaddr_in *sin;
74: register char *cp;
75: int n;
76: char *index();
77: struct in_addr in, inet_makeaddr();
78:
79: if (ifaddraddr == 0) {
80: klseek(kmem, (off_t)ifnetaddr, 0);
81: read(kmem, &ifnet, sizeof ifnet);
82: klseek(kmem, (off_t)ifnet.if_name, 0);
83: read(kmem, name, 16);
84: name[15] = '\0';
85: ifnetaddr = (off_t) ifnet.if_next;
86: if (interface != 0 &&
87: (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
88: continue;
89: cp = index(name, '\0');
90: *cp++ = ifnet.if_unit + '0';
91: if ((ifnet.if_flags&IFF_UP) == 0)
92: *cp++ = '*';
93: *cp = '\0';
94: ifaddraddr = (off_t)ifnet.if_addrlist;
95: }
96: printf("%-5.5s %-5d ", name, ifnet.if_mtu);
97: if (ifaddraddr == 0) {
98: printf("%-10.10s ", "none");
99: printf("%-12.12s ", "none");
100: } else {
101: klseek(kmem, (off_t)ifaddraddr, 0);
102: read(kmem, &ifaddr, sizeof ifaddr);
103: ifaddraddr = (off_t)ifaddr.ifa.ifa_next;
104: switch (ifaddr.ifa.ifa_addr.sa_family) {
105: case AF_UNSPEC:
106: printf("%-10.10s ", "none");
107: printf("%-12.12s ", "none");
108: break;
109: case AF_INET:
110: sin = (struct sockaddr_in *)&ifaddr.in.ia_addr;
111: #ifdef notdef
112: /* can't use inet_makeaddr because kernel
113: * keeps nets unshifted.
114: */
115: in = inet_makeaddr(ifaddr.in.ia_subnet,
116: INADDR_ANY);
117: printf("%-10.10s ", netname(in));
118: #else
119: printf("%-10.10s ",
120: netname(htonl(ifaddr.in.ia_subnet),
121: ifaddr.in.ia_subnetmask));
122: #endif
123: printf("%-12.12s ", routename(sin->sin_addr));
124: break;
125: case AF_NS:
126: {
127: struct sockaddr_ns *sns =
128: (struct sockaddr_ns *)&ifaddr.in.ia_addr;
129: long net;
130: char host[8];
131: *(union ns_net *) &net = sns->sns_addr.x_net;
132: sprintf(host, "%lxH", ntohl(net));
133: upHex(host);
134: printf("ns:%-8s ", host);
135:
136: printf("%-12s ",ns_phost(sns));
137: }
138: break;
139: default:
140: printf("af%2d: ", ifaddr.ifa.ifa_addr.sa_family);
141: for (cp = (char *)&ifaddr.ifa.ifa_addr +
142: sizeof(struct sockaddr) - 1;
143: cp >= ifaddr.ifa.ifa_addr.sa_data; --cp)
144: if (*cp != 0)
145: break;
146: n = cp - (char *)ifaddr.ifa.ifa_addr.sa_data + 1;
147: cp = (char *)ifaddr.ifa.ifa_addr.sa_data;
148: if (n <= 6)
149: while (--n)
150: printf("%02d.", *cp++ & 0xff);
151: else
152: while (--n)
153: printf("%02d", *cp++ & 0xff);
154: printf("%02d ", *cp & 0xff);
155: break;
156: }
157: }
158: printf("%-7D %-5D %-7D %-5D %-6D",
159: ifnet.if_ipackets, ifnet.if_ierrors,
160: ifnet.if_opackets, ifnet.if_oerrors,
161: ifnet.if_collisions);
162: if (tflag)
163: printf(" %-6d", ifnet.if_timer);
164: putchar('\n');
165: }
166: }
167:
168: #ifndef CRASH
169: #define MAXIF 10
170: struct iftot {
171: char ift_name[16]; /* interface name */
172: int ift_ip; /* input packets */
173: int ift_ie; /* input errors */
174: int ift_op; /* output packets */
175: int ift_oe; /* output errors */
176: int ift_co; /* collisions */
177: } iftot[MAXIF];
178:
179: /*
180: * Print a running summary of interface statistics.
181: * Repeat display every interval seconds, showing
182: * statistics collected over that interval. First
183: * line printed at top of screen is always cumulative.
184: */
185: sidewaysintpr(interval, off)
186: int interval;
187: off_t off;
188: {
189: struct ifnet ifnet;
190: off_t firstifnet;
191: register struct iftot *ip, *total;
192: register int line;
193: struct iftot *lastif, *sum, *interesting;
194: int maxtraffic;
195:
196: klseek(kmem, (off_t)off, 0);
197: #if pdp11
198: { unsigned x;
199: read(kmem, &x, sizeof (x));
200: firstifnet = (long)x;
201: }
202: #else
203: read(kmem, &firstifnet, sizeof (off_t));
204: #endif
205: lastif = iftot;
206: sum = iftot + MAXIF - 1;
207: total = sum - 1;
208: interesting = iftot;
209: for (off = firstifnet, ip = iftot; off;) {
210: char *cp;
211:
212: klseek(kmem, (off_t)off, 0);
213: read(kmem, &ifnet, sizeof ifnet);
214: klseek(kmem, (off_t)ifnet.if_name, 0);
215: ip->ift_name[0] = '(';
216: read(kmem, ip->ift_name + 1, 15);
217: if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
218: unit == ifnet.if_unit)
219: interesting = ip;
220: ip->ift_name[15] = '\0';
221: cp = index(ip->ift_name, '\0');
222: sprintf(cp, "%d)", ifnet.if_unit);
223: ip++;
224: if (ip >= iftot + MAXIF - 2)
225: break;
226: off = (off_t) ifnet.if_next;
227: }
228: lastif = ip;
229: banner:
230: printf(" input %-6.6s output ", interesting->ift_name);
231: if (lastif - iftot > 0)
232: printf(" input (Total) output ");
233: for (ip = iftot; ip < iftot + MAXIF; ip++) {
234: ip->ift_ip = 0;
235: ip->ift_ie = 0;
236: ip->ift_op = 0;
237: ip->ift_oe = 0;
238: ip->ift_co = 0;
239: }
240: putchar('\n');
241: printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
242: "packets", "errs", "packets", "errs", "colls");
243: if (lastif - iftot > 0)
244: printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
245: "packets", "errs", "packets", "errs", "colls");
246: putchar('\n');
247: fflush(stdout);
248: line = 0;
249: loop:
250: sum->ift_ip = 0;
251: sum->ift_ie = 0;
252: sum->ift_op = 0;
253: sum->ift_oe = 0;
254: sum->ift_co = 0;
255: for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
256: klseek(kmem, (off_t)off, 0);
257: read(kmem, &ifnet, sizeof ifnet);
258: if (ip == interesting)
259: printf("%-7d %-5d %-7d %-5d %-5d ",
260: ifnet.if_ipackets - ip->ift_ip,
261: ifnet.if_ierrors - ip->ift_ie,
262: ifnet.if_opackets - ip->ift_op,
263: ifnet.if_oerrors - ip->ift_oe,
264: ifnet.if_collisions - ip->ift_co);
265: ip->ift_ip = ifnet.if_ipackets;
266: ip->ift_ie = ifnet.if_ierrors;
267: ip->ift_op = ifnet.if_opackets;
268: ip->ift_oe = ifnet.if_oerrors;
269: ip->ift_co = ifnet.if_collisions;
270: sum->ift_ip += ip->ift_ip;
271: sum->ift_ie += ip->ift_ie;
272: sum->ift_op += ip->ift_op;
273: sum->ift_oe += ip->ift_oe;
274: sum->ift_co += ip->ift_co;
275: off = (off_t) ifnet.if_next;
276: }
277: if (lastif - iftot > 0)
278: printf("%-7d %-5d %-7d %-5d %-5d\n",
279: sum->ift_ip - total->ift_ip,
280: sum->ift_ie - total->ift_ie,
281: sum->ift_op - total->ift_op,
282: sum->ift_oe - total->ift_oe,
283: sum->ift_co - total->ift_co);
284: *total = *sum;
285: fflush(stdout);
286: line++;
287: if (interval)
288: sleep(interval);
289: if (line == 21)
290: goto banner;
291: goto loop;
292: /*NOTREACHED*/
293: }
294: #endif !CRASH