]> git.saurik.com Git - apple/network_cmds.git/blame - netstat.tproj/if.c
network_cmds-329.2.1.tar.gz
[apple/network_cmds.git] / netstat.tproj / if.c
CommitLineData
9c859447
A
1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
b7080c8e
A
28/*
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61#ifndef lint
62/*
63static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
64*/
65static const char rcsid[] =
b8dff150 66 "$Id: if.c,v 1.7 2006/01/16 04:53:59 lindak Exp $";
b7080c8e
A
67#endif /* not lint */
68
69#include <sys/types.h>
b7080c8e 70#include <sys/socket.h>
7ba0088d 71#include <sys/sysctl.h>
b7080c8e
A
72#include <sys/time.h>
73
74#include <net/if.h>
75#include <net/if_var.h>
76#include <net/if_dl.h>
77#include <net/if_types.h>
2b484d24 78#include <net/if_mib.h>
b7080c8e 79#include <net/ethernet.h>
2b484d24
A
80#include <net/route.h>
81
b7080c8e
A
82#include <netinet/in.h>
83#include <netinet/in_var.h>
84
b7080c8e
A
85#include <arpa/inet.h>
86
87#include <signal.h>
88#include <stdio.h>
89#include <string.h>
90#include <unistd.h>
2b484d24
A
91#include <stdlib.h>
92#include <err.h>
b7080c8e
A
93
94#include "netstat.h"
95
96#define YES 1
97#define NO 0
98
2b484d24
A
99#define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
100
101#define NEXT_SA(p) (struct sockaddr *) \
9c859447
A
102 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
103 sizeof(uint32_t)))
2b484d24
A
104
105static void sidewaysintpr ();
7ba0088d
A
106static void catchalarm (int);
107
108#ifdef INET6
2b484d24 109char *netname6 (struct sockaddr_in6 *, struct sockaddr *);
7ba0088d 110static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
7ba0088d
A
111#endif
112
7ba0088d
A
113/*
114 * Display a formatted value, or a '-' in the same space.
115 */
116static void
2b484d24 117show_stat(const char *fmt, int width, u_int64_t value, short showvalue)
7ba0088d
A
118{
119 char newfmt[32];
120
121 /* Construct the format string */
122 if (showvalue) {
123 sprintf(newfmt, "%%%d%s", width, fmt);
124 printf(newfmt, value);
125 } else {
126 sprintf(newfmt, "%%%ds", width);
127 printf(newfmt, "-");
128 }
129}
130
2b484d24
A
131size_t
132get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
133{
134 int i;
135 size_t len = 0;
136
137 for (i = 0; i < RTAX_MAX; i++) {
138 if (addrs & (1 << i)) {
139 rti_info[i] = sa;
140 if (sa->sa_len < sizeof(struct sockaddr))
141 len += sizeof(struct sockaddr);
142 else
143 len += sa->sa_len;
144 sa = NEXT_SA(sa);
145 } else {
146 rti_info[i] = NULL;
147 }
148 }
149 return len;
150}
7ba0088d 151
2b484d24
A
152static void
153multipr(int family, char *buf, char *lim)
154{
155 char *next;
156
157 for (next = buf; next < lim; ) {
158 struct ifma_msghdr2 *ifmam = (struct ifma_msghdr2 *)next;
159 struct sockaddr *rti_info[RTAX_MAX];
160 struct sockaddr *sa;
161 const char *fmt = 0;
162
163 next += ifmam->ifmam_msglen;
164 if (ifmam->ifmam_type == RTM_IFINFO2)
165 break;
166 else if (ifmam->ifmam_type != RTM_NEWMADDR2)
167 continue;
168 get_rti_info(ifmam->ifmam_addrs, (struct sockaddr*)(ifmam + 1), rti_info);
169 sa = rti_info[RTAX_IFA];
170
171 if (sa->sa_family != family)
172 continue;
173 switch (sa->sa_family) {
174 case AF_INET: {
175 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
176
177 fmt = routename(sin->sin_addr.s_addr);
178 break;
179 }
180 #ifdef INET6
181 case AF_INET6: {
b8dff150
A
182 struct sockaddr_in6 sin6;
183
184 memcpy(&sin6, sa, sizeof(struct sockaddr_in6));
185
186 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
187 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
9c859447 188 sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
b8dff150
A
189 sin6.sin6_addr.s6_addr[2] = 0;
190 sin6.sin6_addr.s6_addr[3] = 0;
191 }
2b484d24
A
192
193 printf("%23s %-19.19s(refs: %d)\n", "",
9c859447
A
194 inet_ntop(AF_INET6, &sin6.sin6_addr,
195 ntop_buf, sizeof(ntop_buf)),
2b484d24
A
196 ifmam->ifmam_refcount);
197 break;
198 }
199 #endif /* INET6 */
200 case AF_LINK: {
201 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
202
203 switch (sdl->sdl_type) {
204 case IFT_ETHER:
205 case IFT_FDDI:
9c859447 206 fmt = ether_ntoa((struct ether_addr *)
2b484d24
A
207 LLADDR(sdl));
208 break;
209 }
210 break;
211 }
212 }
213 if (fmt)
214 printf("%23s %s\n", "", fmt);
215 }
216}
b7080c8e
A
217
218/*
219 * Print a description of the network interfaces.
220 */
221void
2b484d24 222intpr(void (*pfunc)(char *))
b7080c8e 223{
2b484d24
A
224 u_int64_t opackets = 0;
225 u_int64_t ipackets = 0;
226 u_int64_t obytes = 0;
227 u_int64_t ibytes = 0;
228 u_int64_t oerrors = 0;
229 u_int64_t ierrors = 0;
230 u_int64_t collisions = 0;
9c859447 231 uint32_t mtu = 0;
2b484d24
A
232 short timer = 0;
233 int drops = 0;
7ba0088d 234 struct sockaddr *sa = NULL;
2b484d24 235 char name[32];
7ba0088d
A
236 short network_layer;
237 short link_layer;
2b484d24
A
238 int mib[6];
239 char *buf = NULL, *lim, *next;
240 size_t len;
241 struct if_msghdr *ifm;
242 struct sockaddr *rti_info[RTAX_MAX];
243 unsigned int ifindex = 0;
244
b7080c8e 245 if (interval) {
2b484d24 246 sidewaysintpr();
b7080c8e
A
247 return;
248 }
2b484d24
A
249
250 if (interface != 0)
251 ifindex = if_nametoindex(interface);
252
253 mib[0] = CTL_NET; // networking subsystem
254 mib[1] = PF_ROUTE; // type of information
255 mib[2] = 0; // protocol (IPPROTO_xxx)
256 mib[3] = 0; // address family
257 mib[4] = NET_RT_IFLIST2; // operation
258 mib[5] = 0;
259 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
b7080c8e 260 return;
2b484d24
A
261 if ((buf = malloc(len)) == NULL) {
262 printf("malloc failed\n");
263 exit(1);
264 }
265 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
266 if (buf)
267 free(buf);
b7080c8e 268 return;
2b484d24 269 }
7ba0088d
A
270 if (!pfunc) {
271 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
272 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
273 if (bflag)
274 printf(" %10.10s","Ibytes");
275 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
276 if (bflag)
277 printf(" %10.10s","Obytes");
278 printf(" %5s", "Coll");
279 if (tflag)
280 printf(" %s", "Time");
281 if (dflag)
282 printf(" %s", "Drop");
283 putchar('\n');
284 }
2b484d24
A
285 lim = buf + len;
286 for (next = buf; next < lim; ) {
287 char *cp;
b7080c8e 288 int n, m;
2b484d24 289
7ba0088d
A
290 network_layer = 0;
291 link_layer = 0;
2b484d24
A
292 ifm = (struct if_msghdr *)next;
293 next += ifm->ifm_msglen;
294
295 if (ifm->ifm_type == RTM_IFINFO2) {
296 struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
297 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1);
7ba0088d 298
2b484d24
A
299 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
300 name[sdl->sdl_nlen] = 0;
301 if (interface != 0 && if2m->ifm_index != ifindex)
b7080c8e
A
302 continue;
303 cp = index(name, '\0');
7ba0088d
A
304
305 if (pfunc) {
306 (*pfunc)(name);
307 continue;
308 }
309
2b484d24 310 if ((if2m->ifm_flags & IFF_UP) == 0)
b7080c8e
A
311 *cp++ = '*';
312 *cp = '\0';
2b484d24
A
313
314 /*
315 * Get the interface stats. These may get
316 * overriden below on a per-interface basis.
317 */
318 opackets = if2m->ifm_data.ifi_opackets;
319 ipackets = if2m->ifm_data.ifi_ipackets;
320 obytes = if2m->ifm_data.ifi_obytes;
321 ibytes = if2m->ifm_data.ifi_ibytes;
322 oerrors =if2m->ifm_data.ifi_oerrors;
323 ierrors = if2m->ifm_data.ifi_ierrors;
324 collisions = if2m->ifm_data.ifi_collisions;
325 timer = if2m->ifm_timer;
326 drops = if2m->ifm_snd_drops;
327 mtu = if2m->ifm_data.ifi_mtu;
328
329 get_rti_info(if2m->ifm_addrs, (struct sockaddr*)(if2m + 1), rti_info);
330 sa = rti_info[RTAX_IFP];
331 } else if (ifm->ifm_type == RTM_NEWADDR) {
332 struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
333
334 if (interface != 0 && ifam->ifam_index != ifindex)
335 continue;
336 get_rti_info(ifam->ifam_addrs, (struct sockaddr*)(ifam + 1), rti_info);
337 sa = rti_info[RTAX_IFA];
9c859447 338 } else {
2b484d24 339 continue;
9c859447
A
340 }
341 printf("%-5.5s %-5u ", name, mtu);
2b484d24
A
342
343 if (sa == 0) {
b7080c8e
A
344 printf("%-13.13s ", "none");
345 printf("%-15.15s ", "none");
346 } else {
b7080c8e
A
347 switch (sa->sa_family) {
348 case AF_UNSPEC:
349 printf("%-13.13s ", "none");
350 printf("%-15.15s ", "none");
351 break;
9c859447 352
2b484d24
A
353 case AF_INET: {
354 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
355 struct sockaddr_in mask;
356
357 mask.sin_addr.s_addr = 0;
9c859447
A
358 memcpy(&mask,
359 rti_info[RTAX_NETMASK],
360 ((struct sockaddr_in *)rti_info[RTAX_NETMASK])->sin_len);
2b484d24 361
9c859447
A
362 printf("%-13.13s ",
363 netname(sin->sin_addr.s_addr & mask.sin_addr.s_addr,
3a228055 364 ntohl(mask.sin_addr.s_addr)));
2b484d24 365
b7080c8e
A
366 printf("%-15.15s ",
367 routename(sin->sin_addr.s_addr));
7ba0088d
A
368
369 network_layer = 1;
b7080c8e 370 break;
2b484d24 371 }
7ba0088d 372#ifdef INET6
2b484d24
A
373 case AF_INET6: {
374 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
375 struct sockaddr *mask = (struct sockaddr *)rti_info[RTAX_NETMASK];
376
7ba0088d 377 printf("%-11.11s ",
2b484d24
A
378 netname6(sin6,
379 mask));
7ba0088d
A
380 printf("%-17.17s ",
381 (char *)inet_ntop(AF_INET6,
382 &sin6->sin6_addr,
383 ntop_buf, sizeof(ntop_buf)));
b7080c8e 384
7ba0088d
A
385 network_layer = 1;
386 break;
2b484d24 387 }
7ba0088d 388#endif /*INET6*/
9c859447 389 case AF_LINK: {
b7080c8e
A
390 struct sockaddr_dl *sdl =
391 (struct sockaddr_dl *)sa;
7ba0088d
A
392 char linknum[10];
393 cp = (char *)LLADDR(sdl);
394 n = sdl->sdl_alen;
395 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
396 m = printf("%-11.11s ", linknum);
b7080c8e 397 goto hexprint;
9c859447
A
398 }
399
b7080c8e
A
400 default:
401 m = printf("(%d)", sa->sa_family);
402 for (cp = sa->sa_len + (char *)sa;
403 --cp > sa->sa_data && (*cp == 0);) {}
404 n = cp - sa->sa_data + 1;
405 cp = sa->sa_data;
406 hexprint:
407 while (--n >= 0)
408 m += printf("%02x%c", *cp++ & 0xff,
7ba0088d 409 n > 0 ? ':' : ' ');
b7080c8e
A
410 m = 30 - m;
411 while (m-- > 0)
412 putchar(' ');
7ba0088d
A
413
414 link_layer = 1;
b7080c8e
A
415 break;
416 }
7ba0088d 417 }
7ba0088d 418
2b484d24 419 show_stat("llu", 8, ipackets, link_layer|network_layer);
7ba0088d 420 printf(" ");
2b484d24 421 show_stat("llu", 5, ierrors, link_layer);
7ba0088d
A
422 printf(" ");
423 if (bflag) {
2b484d24 424 show_stat("llu", 10, ibytes, link_layer|network_layer);
7ba0088d
A
425 printf(" ");
426 }
2b484d24 427 show_stat("llu", 8, opackets, link_layer|network_layer);
7ba0088d 428 printf(" ");
2b484d24 429 show_stat("llu", 5, oerrors, link_layer);
7ba0088d
A
430 printf(" ");
431 if (bflag) {
2b484d24 432 show_stat("llu", 10, obytes, link_layer|network_layer);
7ba0088d
A
433 printf(" ");
434 }
2b484d24 435 show_stat("llu", 5, collisions, link_layer);
7ba0088d
A
436 if (tflag) {
437 printf(" ");
2b484d24 438 show_stat("ll", 3, timer, link_layer);
7ba0088d
A
439 }
440 if (dflag) {
441 printf(" ");
2b484d24 442 show_stat("ll", 3, drops, link_layer);
b7080c8e 443 }
b7080c8e 444 putchar('\n');
2b484d24
A
445
446 if (aflag)
447 multipr(sa->sa_family, next, lim);
b7080c8e
A
448 }
449}
450
b7080c8e 451struct iftot {
7ba0088d 452 SLIST_ENTRY(iftot) chain;
2b484d24
A
453 char ift_name[16]; /* interface name */
454 u_int64_t ift_ip; /* input packets */
455 u_int64_t ift_ie; /* input errors */
456 u_int64_t ift_op; /* output packets */
457 u_int64_t ift_oe; /* output errors */
458 u_int64_t ift_co; /* collisions */
459 u_int64_t ift_dr; /* drops */
460 u_int64_t ift_ib; /* input bytes */
461 u_int64_t ift_ob; /* output bytes */
7f5b2e89
A
462 u_int64_t ift_obgp; /* output bg packets */
463 u_int64_t ift_obgb; /* output bg bytes */
7ba0088d 464};
b7080c8e
A
465
466u_char signalled; /* set if alarm goes off "early" */
467
468/*
469 * Print a running summary of interface statistics.
470 * Repeat display every interval seconds, showing statistics
471 * collected over that interval. Assumes that interval is non-zero.
472 * First line printed at top of screen is always cumulative.
473 * XXX - should be rewritten to use ifmib(4).
474 */
475static void
2b484d24 476sidewaysintpr()
b7080c8e 477{
2b484d24 478 struct iftot *total, *sum, *interesting;
b7080c8e 479 register int line;
b8dff150 480 int first;
2b484d24
A
481 int name[6];
482 size_t len;
483 unsigned int ifcount, i;
484 struct ifmibdata *ifmdall = 0;
485 int interesting_row;
b8dff150
A
486 sigset_t sigset, oldsigset;
487 struct itimerval timer_interval;
488
2b484d24
A
489
490 /* Common OID prefix */
491 name[0] = CTL_NET;
492 name[1] = PF_LINK;
493 name[2] = NETLINK_GENERIC;
494
495 len = sizeof(int);
496 name[3] = IFMIB_SYSTEM;
497 name[4] = IFMIB_IFCOUNT;
498 if (sysctl(name, 5, &ifcount, &len, 0, 0) == 1)
499 err(1, "sysctl IFMIB_IFCOUNT");
500
501 len = ifcount * sizeof(struct ifmibdata);
502 ifmdall = malloc(len);
503 if (ifmdall == 0)
504 err(1, "malloc failed");
505 name[3] = IFMIB_IFALLDATA;
506 name[4] = 0;
507 name[5] = IFDATA_GENERAL;
508 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
509 err(1, "sysctl IFMIB_IFALLDATA");
510
b7080c8e 511 interesting = NULL;
2b484d24
A
512 interesting_row = 0;
513 for (i = 0; i < ifcount; i++) {
514 struct ifmibdata *ifmd = ifmdall + i;
515
516 if (interface && strcmp(ifmd->ifmd_name, interface) == 0) {
517 if ((interesting = calloc(ifcount, sizeof(struct iftot))) == NULL)
518 err(1, "malloc failed");
519 interesting_row = i + 1;
520 snprintf(interesting->ift_name, 16, "(%s)", ifmd->ifmd_name);;
7ba0088d 521 }
7ba0088d 522 }
2b484d24
A
523 if ((total = calloc(1, sizeof(struct iftot))) == NULL)
524 err(1, "malloc failed");
525
526 if ((sum = calloc(1, sizeof(struct iftot))) == NULL)
527 err(1, "malloc failed");
7ba0088d 528
b8dff150
A
529 /* create a timer that fires repeatedly every interval seconds */
530 timer_interval.it_value.tv_sec = interval;
531 timer_interval.it_value.tv_usec = 0;
532 timer_interval.it_interval.tv_sec = interval;
533 timer_interval.it_interval.tv_usec = 0;
b7080c8e
A
534 (void)signal(SIGALRM, catchalarm);
535 signalled = NO;
b8dff150 536 (void)setitimer(ITIMER_REAL, &timer_interval, NULL);
b7080c8e
A
537 first = 1;
538banner:
539 printf("%17s %14s %16s", "input",
540 interesting ? interesting->ift_name : "(Total)", "output");
541 putchar('\n');
542 printf("%10s %5s %10s %10s %5s %10s %5s",
543 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
544 if (dflag)
545 printf(" %5.5s", "drops");
7f5b2e89
A
546 if (prioflag)
547 printf(" %10s %10s", "obgpkts", "obgbytes");
b7080c8e
A
548 putchar('\n');
549 fflush(stdout);
550 line = 0;
551loop:
552 if (interesting != NULL) {
2b484d24
A
553 struct ifmibdata ifmd;
554
555 len = sizeof(struct ifmibdata);
556 name[3] = IFMIB_IFDATA;
557 name[4] = interesting_row;
558 name[5] = IFDATA_GENERAL;
559 if (sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1)
560 err(1, "sysctl IFDATA_GENERAL %d", interesting_row);
561
b7080c8e 562 if (!first) {
2b484d24
A
563 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
564 ifmd.ifmd_data.ifi_ipackets - interesting->ift_ip,
565 ifmd.ifmd_data.ifi_ierrors - interesting->ift_ie,
566 ifmd.ifmd_data.ifi_ibytes - interesting->ift_ib,
567 ifmd.ifmd_data.ifi_opackets - interesting->ift_op,
568 ifmd.ifmd_data.ifi_oerrors - interesting->ift_oe,
569 ifmd.ifmd_data.ifi_obytes - interesting->ift_ob,
570 ifmd.ifmd_data.ifi_collisions - interesting->ift_co);
b7080c8e 571 if (dflag)
2b484d24 572 printf(" %5llu", ifmd.ifmd_snd_drops - interesting->ift_dr);
7f5b2e89
A
573 if (prioflag)
574 printf(" %10llu %10llu",
575 ifmd.ifmd_filler[0] - interesting->ift_obgp,
576 ifmd.ifmd_filler[1] - interesting->ift_obgb);
b7080c8e 577 }
2b484d24
A
578 interesting->ift_ip = ifmd.ifmd_data.ifi_ipackets;
579 interesting->ift_ie = ifmd.ifmd_data.ifi_ierrors;
580 interesting->ift_ib = ifmd.ifmd_data.ifi_ibytes;
581 interesting->ift_op = ifmd.ifmd_data.ifi_opackets;
582 interesting->ift_oe = ifmd.ifmd_data.ifi_oerrors;
583 interesting->ift_ob = ifmd.ifmd_data.ifi_obytes;
584 interesting->ift_co = ifmd.ifmd_data.ifi_collisions;
585 interesting->ift_dr = ifmd.ifmd_snd_drops;
7f5b2e89
A
586 /* private counters */
587 interesting->ift_obgp = ifmd.ifmd_filler[0];
588 interesting->ift_obgb = ifmd.ifmd_filler[1];
b7080c8e 589 } else {
2b484d24
A
590 unsigned int latest_ifcount;
591
592 len = sizeof(int);
593 name[3] = IFMIB_SYSTEM;
594 name[4] = IFMIB_IFCOUNT;
595 if (sysctl(name, 5, &latest_ifcount, &len, 0, 0) == 1)
596 err(1, "sysctl IFMIB_IFCOUNT");
597 if (latest_ifcount > ifcount) {
598 ifcount = latest_ifcount;
599 len = ifcount * sizeof(struct ifmibdata);
600 free(ifmdall);
601 ifmdall = malloc(len);
602 if (ifmdall == 0)
603 err(1, "malloc failed");
604 } else if (latest_ifcount > ifcount) {
605 ifcount = latest_ifcount;
606 len = ifcount * sizeof(struct ifmibdata);
607 }
608 len = ifcount * sizeof(struct ifmibdata);
609 name[3] = IFMIB_IFALLDATA;
610 name[4] = 0;
611 name[5] = IFDATA_GENERAL;
612 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
613 err(1, "sysctl IFMIB_IFALLDATA");
614
b7080c8e
A
615 sum->ift_ip = 0;
616 sum->ift_ie = 0;
617 sum->ift_ib = 0;
618 sum->ift_op = 0;
619 sum->ift_oe = 0;
620 sum->ift_ob = 0;
621 sum->ift_co = 0;
622 sum->ift_dr = 0;
7f5b2e89
A
623 sum->ift_obgp = 0;
624 sum->ift_obgb = 0;
2b484d24
A
625 for (i = 0; i < ifcount; i++) {
626 struct ifmibdata *ifmd = ifmdall + i;
627
628 sum->ift_ip += ifmd->ifmd_data.ifi_ipackets;
629 sum->ift_ie += ifmd->ifmd_data.ifi_ierrors;
630 sum->ift_ib += ifmd->ifmd_data.ifi_ibytes;
631 sum->ift_op += ifmd->ifmd_data.ifi_opackets;
632 sum->ift_oe += ifmd->ifmd_data.ifi_oerrors;
633 sum->ift_ob += ifmd->ifmd_data.ifi_obytes;
634 sum->ift_co += ifmd->ifmd_data.ifi_collisions;
635 sum->ift_dr += ifmd->ifmd_snd_drops;
7f5b2e89
A
636 /* private counters */
637 sum->ift_obgp += ifmd->ifmd_filler[0];
638 sum->ift_obgb += ifmd->ifmd_filler[1];
b7080c8e
A
639 }
640 if (!first) {
2b484d24 641 printf("%10llu %5llu %10llu %10llu %5llu %10llu %5llu",
b7080c8e
A
642 sum->ift_ip - total->ift_ip,
643 sum->ift_ie - total->ift_ie,
644 sum->ift_ib - total->ift_ib,
645 sum->ift_op - total->ift_op,
646 sum->ift_oe - total->ift_oe,
647 sum->ift_ob - total->ift_ob,
648 sum->ift_co - total->ift_co);
649 if (dflag)
2b484d24 650 printf(" %5llu", sum->ift_dr - total->ift_dr);
7f5b2e89
A
651 if (prioflag)
652 printf(" %10llu %10llu",
653 sum->ift_obgp - total->ift_obgp,
654 sum->ift_obgb - total->ift_obgb);
b7080c8e
A
655 }
656 *total = *sum;
657 }
658 if (!first)
659 putchar('\n');
660 fflush(stdout);
b8dff150
A
661 sigemptyset(&sigset);
662 sigaddset(&sigset, SIGALRM);
663 (void)sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
664 if (!signalled) {
665 sigemptyset(&sigset);
666 sigsuspend(&sigset);
b7080c8e 667 }
b8dff150
A
668 (void)sigprocmask(SIG_SETMASK, &oldsigset, NULL);
669
b7080c8e 670 signalled = NO;
b7080c8e
A
671 line++;
672 first = 0;
673 if (line == 21)
674 goto banner;
675 else
676 goto loop;
677 /*NOTREACHED*/
678}
679
b8dff150 680void
9c859447 681intervalpr(void (*pr)(uint32_t, char *, int), uint32_t off, char *name , int af)
b8dff150
A
682{
683 struct itimerval timer_interval;
684 sigset_t sigset, oldsigset;
685
686 /* create a timer that fires repeatedly every interval seconds */
687 timer_interval.it_value.tv_sec = interval;
688 timer_interval.it_value.tv_usec = 0;
689 timer_interval.it_interval.tv_sec = interval;
690 timer_interval.it_interval.tv_usec = 0;
691 (void) signal(SIGALRM, catchalarm);
692 signalled = NO;
693 (void) setitimer(ITIMER_REAL, &timer_interval, NULL);
694
695 for (;;) {
696 pr(off, name, af);
697
698 fflush(stdout);
699 sigemptyset(&sigset);
700 sigaddset(&sigset, SIGALRM);
701 (void) sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
702 if (!signalled) {
703 sigemptyset(&sigset);
704 sigsuspend(&sigset);
705 }
706 (void) sigprocmask(SIG_SETMASK, &oldsigset, NULL);
707 signalled = NO;
708 }
709}
710
b7080c8e
A
711/*
712 * Called if an interval expires before sidewaysintpr has completed a loop.
713 * Sets a flag to not wait for the alarm.
714 */
715static void
7ba0088d 716catchalarm(int signo )
b7080c8e
A
717{
718 signalled = YES;
719}