]>
git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
43be8509ff53b043ea92e837504bf7011c68f87d
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
38 static const char rcsid
[] =
39 "$Id: if.c,v 1.1.1.2 2000/01/11 01:48:51 wsanchez Exp $";
42 #include <sys/types.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 #include <net/ethernet.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
56 #include <netipx/ipx.h>
57 #include <netipx/ipx_if.h>
62 #include <netns/ns_if.h>
65 #include <netiso/iso.h>
66 #include <netiso/iso_var.h>
68 #include <arpa/inet.h>
80 static void sidewaysintpr
__P((u_int
, u_long
));
81 static void catchalarm
__P((int));
84 * Print a description of the network interfaces.
87 intpr(interval
, ifnetaddr
)
92 struct ifnethead ifnethead
;
97 struct ipx_ifaddr ipx
;
103 struct iso_ifaddr iso
;
110 char name
[32], tname
[16];
112 if (ifnetaddr
== 0) {
113 printf("ifnet: symbol not defined\n");
117 sidewaysintpr((unsigned)interval
, ifnetaddr
);
120 if (kread(ifnetaddr
, (char *)&ifnethead
, sizeof ifnethead
))
122 ifnetaddr
= (u_long
)ifnethead
.tqh_first
;
123 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
))
126 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
127 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
129 printf(" %10.10s","Ibytes");
130 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
132 printf(" %10.10s","Obytes");
133 printf(" %5s", "Coll");
135 printf(" %s", "Time");
137 printf(" %s", "Drop");
140 while (ifnetaddr
|| ifaddraddr
) {
141 struct sockaddr_in
*sin
;
145 if (ifaddraddr
== 0) {
146 ifnetfound
= ifnetaddr
;
147 if (kread(ifnetaddr
, (char *)&ifnet
, sizeof ifnet
) ||
148 kread((u_long
)ifnet
.if_name
, tname
, 16))
151 ifnetaddr
= (u_long
)ifnet
.if_link
.tqe_next
;
152 snprintf(name
, 32, "%s%d", tname
, ifnet
.if_unit
);
153 if (interface
!= 0 && (strcmp(name
, interface
) != 0))
155 cp
= index(name
, '\0');
156 if ((ifnet
.if_flags
&IFF_UP
) == 0)
159 ifaddraddr
= (u_long
)ifnet
.if_addrhead
.tqh_first
;
161 printf("%-5.5s %-5lu ", name
, ifnet
.if_mtu
);
162 ifaddrfound
= ifaddraddr
;
163 if (ifaddraddr
== 0) {
164 printf("%-13.13s ", "none");
165 printf("%-15.15s ", "none");
167 if (kread(ifaddraddr
, (char *)&ifaddr
, sizeof ifaddr
)) {
171 #define CP(x) ((char *)(x))
172 cp
= (CP(ifaddr
.ifa
.ifa_addr
) - CP(ifaddraddr
)) +
173 CP(&ifaddr
); sa
= (struct sockaddr
*)cp
;
174 switch (sa
->sa_family
) {
176 printf("%-13.13s ", "none");
177 printf("%-15.15s ", "none");
180 sin
= (struct sockaddr_in
*)sa
;
182 /* can't use inet_makeaddr because kernel
183 * keeps nets unshifted.
185 in
= inet_makeaddr(ifaddr
.in
.ia_subnet
,
187 printf("%-13.13s ", netname(in
.s_addr
,
188 ifaddr
.in
.ia_subnetmask
));
191 netname(htonl(ifaddr
.in
.ia_subnet
),
192 ifaddr
.in
.ia_subnetmask
));
195 routename(sin
->sin_addr
.s_addr
));
201 struct sockaddr_ipx
*sipx
=
202 (struct sockaddr_ipx
*)sa
;
206 *(union ipx_net
*) &net
= sipx
->sipx_addr
.x_net
;
207 sprintf(netnum
, "%lx", (u_long
)ntohl(net
));
208 printf("ipx:%-8s ", netnum
);
209 /* printf("ipx:%-8s ", netname(net, 0L)); */
211 ipx_phost((struct sockaddr
*)sipx
));
217 printf("atalk:%-12.12s ",atalk_print(sa
,0x10) );
218 printf("%-9.9s ",atalk_print(sa
,0x0b) );
224 struct sockaddr_ns
*sns
=
225 (struct sockaddr_ns
*)sa
;
229 *(union ns_net
*) &net
= sns
->sns_addr
.x_net
;
230 sprintf(netnum
, "%lxH", ntohl(net
));
232 printf("ns:%-8s ", netnum
);
234 ns_phost((struct sockaddr
*)sns
));
240 struct sockaddr_dl
*sdl
=
241 (struct sockaddr_dl
*)sa
;
242 cp
= (char *)LLADDR(sdl
);
245 m
= printf("%-11.11s ", "<Link>");
248 m
= printf("(%d)", sa
->sa_family
);
249 for (cp
= sa
->sa_len
+ (char *)sa
;
250 --cp
> sa
->sa_data
&& (*cp
== 0);) {}
251 n
= cp
- sa
->sa_data
+ 1;
255 m
+= printf("%02x%c", *cp
++ & 0xff,
262 ifaddraddr
= (u_long
)ifaddr
.ifa
.ifa_link
.tqe_next
;
265 ifnet
.if_ipackets
, ifnet
.if_ierrors
);
267 printf("%10lu ", ifnet
.if_ibytes
);
269 ifnet
.if_opackets
, ifnet
.if_oerrors
);
271 printf("%10lu ", ifnet
.if_obytes
);
272 printf("%5lu", ifnet
.if_collisions
);
274 printf(" %3d", ifnet
.if_timer
);
276 printf(" %3d", ifnet
.if_snd
.ifq_drops
);
278 if (aflag
&& ifaddrfound
) {
280 * Print family's multicast addresses
283 struct ifmultiaddr ifma
;
286 struct sockaddr_in in
;
287 struct sockaddr_dl dl
;
291 for(multiaddr
= (u_long
)ifnet
.if_multiaddrs
.lh_first
;
293 multiaddr
= (u_long
)ifma
.ifma_link
.le_next
) {
294 if (kread(multiaddr
, (char *)&ifma
,
297 if (kread((u_long
)ifma
.ifma_addr
, (char *)&msa
,
300 if (msa
.sa
.sa_family
!= sa
->sa_family
)
304 switch (msa
.sa
.sa_family
) {
306 fmt
= routename(msa
.in
.sin_addr
.s_addr
);
310 switch (ifnet
.if_type
) {
314 (struct ether_addr
*)
321 printf("%23s %s\n", "", fmt
);
329 char ift_name
[16]; /* interface name */
330 u_int ift_ip
; /* input packets */
331 u_int ift_ie
; /* input errors */
332 u_int ift_op
; /* output packets */
333 u_int ift_oe
; /* output errors */
334 u_int ift_co
; /* collisions */
335 u_int ift_dr
; /* drops */
336 u_int ift_ib
; /* input bytes */
337 u_int ift_ob
; /* output bytes */
340 u_char signalled
; /* set if alarm goes off "early" */
343 * Print a running summary of interface statistics.
344 * Repeat display every interval seconds, showing statistics
345 * collected over that interval. Assumes that interval is non-zero.
346 * First line printed at top of screen is always cumulative.
347 * XXX - should be rewritten to use ifmib(4).
350 sidewaysintpr(interval
, off
)
356 struct ifnethead ifnethead
;
357 register struct iftot
*ip
, *total
;
359 struct iftot
*lastif
, *sum
, *interesting
;
361 u_long interesting_off
;
363 if (kread(off
, (char *)&ifnethead
, sizeof ifnethead
))
365 firstifnet
= (u_long
)ifnethead
.tqh_first
;
368 sum
= iftot
+ MAXIF
- 1;
372 for (off
= firstifnet
, ip
= iftot
; off
;) {
373 char name
[16], tname
[16];
375 if (kread(off
, (char *)&ifnet
, sizeof ifnet
))
377 if (kread((u_long
)ifnet
.if_name
, tname
, 16))
380 snprintf(name
, 16, "%s%d", tname
, ifnet
.if_unit
);
381 if (interface
&& strcmp(name
, interface
) == 0) {
383 interesting_off
= off
;
385 snprintf(ip
->ift_name
, 16, "(%s)", name
);;
387 if (ip
>= iftot
+ MAXIF
- 2)
389 off
= (u_long
) ifnet
.if_link
.tqe_next
;
393 (void)signal(SIGALRM
, catchalarm
);
395 (void)alarm(interval
);
396 for (ip
= iftot
; ip
< iftot
+ MAXIF
; ip
++) {
408 printf("%17s %14s %16s", "input",
409 interesting
? interesting
->ift_name
: "(Total)", "output");
411 printf("%10s %5s %10s %10s %5s %10s %5s",
412 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
414 printf(" %5.5s", "drops");
419 if (interesting
!= NULL
) {
421 if (kread(interesting_off
, (char *)&ifnet
, sizeof ifnet
)) {
426 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
427 ifnet
.if_ipackets
- ip
->ift_ip
,
428 ifnet
.if_ierrors
- ip
->ift_ie
,
429 ifnet
.if_ibytes
- ip
->ift_ib
,
430 ifnet
.if_opackets
- ip
->ift_op
,
431 ifnet
.if_oerrors
- ip
->ift_oe
,
432 ifnet
.if_obytes
- ip
->ift_ob
,
433 ifnet
.if_collisions
- ip
->ift_co
);
435 printf(" %5u", ifnet
.if_snd
.ifq_drops
- ip
->ift_dr
);
437 ip
->ift_ip
= ifnet
.if_ipackets
;
438 ip
->ift_ie
= ifnet
.if_ierrors
;
439 ip
->ift_ib
= ifnet
.if_ibytes
;
440 ip
->ift_op
= ifnet
.if_opackets
;
441 ip
->ift_oe
= ifnet
.if_oerrors
;
442 ip
->ift_ob
= ifnet
.if_obytes
;
443 ip
->ift_co
= ifnet
.if_collisions
;
444 ip
->ift_dr
= ifnet
.if_snd
.ifq_drops
;
454 for (off
= firstifnet
, ip
= iftot
; off
&& ip
< lastif
; ip
++) {
455 if (kread(off
, (char *)&ifnet
, sizeof ifnet
)) {
459 sum
->ift_ip
+= ifnet
.if_ipackets
;
460 sum
->ift_ie
+= ifnet
.if_ierrors
;
461 sum
->ift_ib
+= ifnet
.if_ibytes
;
462 sum
->ift_op
+= ifnet
.if_opackets
;
463 sum
->ift_oe
+= ifnet
.if_oerrors
;
464 sum
->ift_ob
+= ifnet
.if_obytes
;
465 sum
->ift_co
+= ifnet
.if_collisions
;
466 sum
->ift_dr
+= ifnet
.if_snd
.ifq_drops
;
467 off
= (u_long
) ifnet
.if_link
.tqe_next
;
470 printf("%10u %5u %10u %10u %5u %10u %5u",
471 sum
->ift_ip
- total
->ift_ip
,
472 sum
->ift_ie
- total
->ift_ie
,
473 sum
->ift_ib
- total
->ift_ib
,
474 sum
->ift_op
- total
->ift_op
,
475 sum
->ift_oe
- total
->ift_oe
,
476 sum
->ift_ob
- total
->ift_ob
,
477 sum
->ift_co
- total
->ift_co
);
479 printf(" %5u", sum
->ift_dr
- total
->ift_dr
);
486 oldmask
= sigblock(sigmask(SIGALRM
));
492 (void)alarm(interval
);
503 * Called if an interval expires before sidewaysintpr has completed a loop.
504 * Sets a flag to not wait for the alarm.