[BACK]Return to if.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/if.c, Revision 1.43

1.43    ! jaredy      1: /*     $OpenBSD: if.c,v 1.42 2005/03/13 16:05:50 mpf Exp $     */
1.6       deraadt     2: /*     $NetBSD: if.c,v 1.16.4.2 1996/06/07 21:46:46 thorpej Exp $      */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.35      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
                     36: #else
1.43    ! jaredy     37: static char *rcsid = "$OpenBSD: if.c,v 1.42 2005/03/13 16:05:50 mpf Exp $";
1.1       deraadt    38: #endif
                     39: #endif /* not lint */
                     40:
                     41: #include <sys/types.h>
                     42: #include <sys/protosw.h>
                     43: #include <sys/socket.h>
                     44:
                     45: #include <net/if.h>
                     46: #include <net/if_dl.h>
1.5       deraadt    47: #include <net/if_types.h>
1.1       deraadt    48: #include <netinet/in.h>
                     49: #include <netinet/in_var.h>
1.11      millert    50: #include <netinet/if_ether.h>
1.1       deraadt    51: #include <netns/ns.h>
                     52: #include <netns/ns_if.h>
1.8       mickey     53: #include <netipx/ipx.h>
                     54: #include <netipx/ipx_if.h>
1.1       deraadt    55: #include <arpa/inet.h>
                     56:
1.13      millert    57: #include <limits.h>
1.1       deraadt    58: #include <signal.h>
                     59: #include <stdio.h>
1.36      david      60: #include <stdlib.h>
1.1       deraadt    61: #include <string.h>
                     62: #include <unistd.h>
                     63:
                     64: #include "netstat.h"
                     65:
                     66: #define        YES     1
                     67: #define        NO      0
                     68:
1.29      millert    69: static void sidewaysintpr(u_int, u_long);
                     70: static void catchalarm(int);
1.1       deraadt    71:
                     72: /*
                     73:  * Print a description of the network interfaces.
1.3       deraadt    74:  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
                     75:  * which is a TAILQ_HEAD.
1.1       deraadt    76:  */
                     77: void
1.33      deraadt    78: intpr(int interval, u_long ifnetaddr)
1.1       deraadt    79: {
                     80:        struct ifnet ifnet;
                     81:        union {
                     82:                struct ifaddr ifa;
                     83:                struct in_ifaddr in;
1.18      itojun     84: #ifdef INET6
                     85:                struct in6_ifaddr in6;
                     86: #endif
1.1       deraadt    87:                struct ns_ifaddr ns;
1.8       mickey     88:                struct ipx_ifaddr ipx;
1.1       deraadt    89:        } ifaddr;
1.33      deraadt    90:        u_long total, ifaddraddr;
1.1       deraadt    91:        struct sockaddr *sa;
1.3       deraadt    92:        struct ifnet_head ifhead;       /* TAILQ_HEAD */
                     93:        char name[IFNAMSIZ];
1.1       deraadt    94:
                     95:        if (ifnetaddr == 0) {
                     96:                printf("ifnet: symbol not defined\n");
                     97:                return;
                     98:        }
                     99:        if (interval) {
                    100:                sidewaysintpr((unsigned)interval, ifnetaddr);
                    101:                return;
                    102:        }
1.3       deraadt   103:
                    104:        /*
                    105:         * Find the pointer to the first ifnet structure.  Replace
                    106:         * the pointer to the TAILQ_HEAD with the actual pointer
                    107:         * to the first list element.
                    108:         */
1.43    ! jaredy    109:        if (kread(ifnetaddr, &ifhead, sizeof ifhead))
1.1       deraadt   110:                return;
1.3       deraadt   111:        ifnetaddr = (u_long)ifhead.tqh_first;
                    112:
1.22      camield   113:        printf("%-7.7s %-5.5s %-11.11s %-17.17s ",
1.33      deraadt   114:            "Name", "Mtu", "Network", "Address");
1.22      camield   115:        if (bflag)
                    116:                printf("%10.10s %10.10s", "Ibytes", "Obytes");
                    117:        else
                    118:                printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
                    119:                    "Ipkts", "Ierrs", "Opkts", "Oerrs", "Colls");
1.1       deraadt   120:        if (tflag)
                    121:                printf(" %s", "Time");
                    122:        if (dflag)
                    123:                printf(" %s", "Drop");
                    124:        putchar('\n');
                    125:        ifaddraddr = 0;
                    126:        while (ifnetaddr || ifaddraddr) {
                    127:                struct sockaddr_in *sin;
1.18      itojun    128: #ifdef INET6
                    129:                struct sockaddr_in6 *sin6;
                    130: #endif
1.27      mpech     131:                char *cp;
1.1       deraadt   132:                int n, m;
                    133:
                    134:                if (ifaddraddr == 0) {
1.43    ! jaredy    135:                        if (kread(ifnetaddr, &ifnet, sizeof ifnet))
1.1       deraadt   136:                                return;
1.3       deraadt   137:                        bcopy(ifnet.if_xname, name, IFNAMSIZ);
                    138:                        name[IFNAMSIZ - 1] = '\0';      /* sanity */
1.1       deraadt   139:                        ifnetaddr = (u_long)ifnet.if_list.tqe_next;
1.3       deraadt   140:                        if (interface != 0 && strcmp(name, interface) != 0)
1.1       deraadt   141:                                continue;
1.10      millert   142:                        cp = strchr(name, '\0');
1.1       deraadt   143:                        if ((ifnet.if_flags & IFF_UP) == 0)
                    144:                                *cp++ = '*';
                    145:                        *cp = '\0';
                    146:                        ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
                    147:                }
1.25      brian     148:
                    149:                if (qflag) {
                    150:                        total = ifnet.if_ibytes + ifnet.if_obytes +
                    151:                            ifnet.if_ipackets + ifnet.if_ierrors +
                    152:                            ifnet.if_opackets + ifnet.if_oerrors +
                    153:                            ifnet.if_collisions;
                    154:                        if (tflag)
                    155:                                total += ifnet.if_timer;
                    156:                        if (dflag)
                    157:                                total += ifnet.if_snd.ifq_drops;
                    158:                        if (total == 0) {
                    159:                                ifaddraddr = 0;
                    160:                                continue;
                    161:                        }
                    162:                }
                    163:
1.16      mickey    164:                printf("%-7.7s %-5ld ", name, ifnet.if_mtu);
1.1       deraadt   165:                if (ifaddraddr == 0) {
                    166:                        printf("%-11.11s ", "none");
1.37      deraadt   167:                        printf("%-17.17s ", "none");
1.1       deraadt   168:                } else {
1.43    ! jaredy    169:                        if (kread(ifaddraddr, &ifaddr, sizeof ifaddr)) {
1.1       deraadt   170:                                ifaddraddr = 0;
                    171:                                continue;
                    172:                        }
                    173: #define CP(x) ((char *)(x))
                    174:                        cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
                    175:                                CP(&ifaddr); sa = (struct sockaddr *)cp;
                    176:                        switch (sa->sa_family) {
                    177:                        case AF_UNSPEC:
                    178:                                printf("%-11.11s ", "none");
                    179:                                printf("%-17.17s ", "none");
                    180:                                break;
                    181:                        case AF_INET:
                    182:                                sin = (struct sockaddr_in *)sa;
                    183: #ifdef notdef
                    184:                                /* can't use inet_makeaddr because kernel
                    185:                                 * keeps nets unshifted.
                    186:                                 */
                    187:                                in = inet_makeaddr(ifaddr.in.ia_subnet,
1.30      deraadt   188:                                    INADDR_ANY);
1.20      itojun    189:                                cp = netname(in.s_addr,
1.30      deraadt   190:                                    ifaddr.in.ia_subnetmask);
1.1       deraadt   191: #else
1.20      itojun    192:                                cp = netname(ifaddr.in.ia_subnet,
                    193:                                    ifaddr.in.ia_subnetmask);
1.1       deraadt   194: #endif
1.20      itojun    195:                                if (vflag)
                    196:                                        n = strlen(cp) < 11 ? 11 : strlen(cp);
                    197:                                else
                    198:                                        n = 11;
                    199:                                printf("%-*.*s ", n, n, cp);
                    200:                                cp = routename(sin->sin_addr.s_addr);
                    201:                                if (vflag)
                    202:                                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    203:                                else
                    204:                                        n = 17;
                    205:                                printf("%-*.*s ", n, n, cp);
1.1       deraadt   206:
                    207:                                if (aflag) {
                    208:                                        u_long multiaddr;
                    209:                                        struct in_multi inm;
1.28      mickey    210:
1.1       deraadt   211:                                        multiaddr = (u_long)ifaddr.in.ia_multiaddrs.lh_first;
                    212:                                        while (multiaddr != 0) {
1.43    ! jaredy    213:                                                kread(multiaddr, &inm, sizeof inm);
1.19      itojun    214:                                                printf("\n%25s %-17.17s ", "",
1.1       deraadt   215:                                                    routename(inm.inm_addr.s_addr));
                    216:                                                multiaddr = (u_long)inm.inm_list.le_next;
                    217:                                        }
1.8       mickey    218:                                }
                    219:                                break;
1.18      itojun    220: #ifdef INET6
                    221:                        case AF_INET6:
                    222:                                sin6 = (struct sockaddr_in6 *)sa;
1.34      itojun    223: #ifdef __KAME__
1.20      itojun    224:                                if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
                    225:                                        sin6->sin6_scope_id =
1.30      deraadt   226:                                            ntohs(*(u_int16_t *)
1.38      deraadt   227:                                            &sin6->sin6_addr.s6_addr[2]);
1.20      itojun    228:                                        sin6->sin6_addr.s6_addr[2] = 0;
                    229:                                        sin6->sin6_addr.s6_addr[3] = 0;
                    230:                                }
                    231: #endif
                    232:                                cp = netname6(&ifaddr.in6.ia_addr,
1.30      deraadt   233:                                    &ifaddr.in6.ia_prefixmask.sin6_addr);
1.20      itojun    234:                                if (vflag)
                    235:                                        n = strlen(cp) < 11 ? 11 : strlen(cp);
                    236:                                else
                    237:                                        n = 11;
                    238:                                printf("%-*.*s ", n, n, cp);
                    239:                                cp = routename6(sin6);
                    240:                                if (vflag)
                    241:                                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    242:                                else
                    243:                                        n = 17;
                    244:                                printf("%-*.*s ", n, n, cp);
1.19      itojun    245:                                if (aflag) {
                    246:                                        u_long multiaddr;
                    247:                                        struct in6_multi inm;
1.31      itojun    248:                                        struct sockaddr_in6 m6;
1.28      mickey    249:
1.19      itojun    250:                                        multiaddr = (u_long)ifaddr.in6.ia6_multiaddrs.lh_first;
                    251:                                        while (multiaddr != 0) {
1.43    ! jaredy    252:                                                kread(multiaddr, &inm, sizeof inm);
1.31      itojun    253:                                                memset(&m6, 0, sizeof(m6));
                    254:                                                m6.sin6_len = sizeof(struct sockaddr_in6);
                    255:                                                m6.sin6_family = AF_INET6;
                    256:                                                m6.sin6_addr = inm.in6m_addr;
1.34      itojun    257: #ifdef __KAME__
1.32      itojun    258:                                                if (IN6_IS_ADDR_MC_LINKLOCAL(&m6.sin6_addr)) {
1.31      itojun    259:                                                        m6.sin6_scope_id =
                    260:                                                            ntohs(*(u_int16_t *)
1.38      deraadt   261:                                                            &m6.sin6_addr.s6_addr[2]);
1.31      itojun    262:                                                        m6.sin6_addr.s6_addr[2] = 0;
                    263:                                                        m6.sin6_addr.s6_addr[3] = 0;
                    264:                                                }
                    265: #endif
                    266:                                                cp = routename6(&m6);
1.19      itojun    267:                                                if (vflag)
1.31      itojun    268:                                                        n = strlen(cp) < 17 ? 17 : strlen(cp);
1.19      itojun    269:                                                else
                    270:                                                        n = 17;
                    271:                                                printf("\n%25s %-*.*s ", "",
1.31      itojun    272:                                                    n, n, cp);
1.19      itojun    273:                                                multiaddr = (u_long)inm.in6m_entry.le_next;
                    274:                                        }
                    275:                                }
1.18      itojun    276:                                break;
                    277: #endif
1.8       mickey    278:                        case AF_IPX:
                    279:                                {
                    280:                                struct sockaddr_ipx *sipx =
                    281:                                        (struct sockaddr_ipx *)sa;
                    282:                                u_long net;
                    283:                                char netnum[8];
                    284:
1.17      deraadt   285:                                *(union ipx_net *)&net = sipx->sipx_addr.ipx_net;
                    286:                                snprintf(netnum, sizeof netnum, "%xH",
                    287:                                    ntohl(net));
1.8       mickey    288:                                upHex(netnum);
                    289:                                printf("ipx:%-8s", netnum);
                    290:                                printf("%-17s ",
                    291:                                    ipx_phost((struct sockaddr *)sipx));
1.1       deraadt   292:                                }
1.15      denny     293:                                break;
                    294:                        case AF_APPLETALK:
                    295:                                printf("atlk:%-12s",atalk_print(sa,0x10) );
                    296:                                printf("%-12s ",atalk_print(sa,0x0b) );
1.1       deraadt   297:                                break;
                    298:                        case AF_NS:
                    299:                                {
                    300:                                struct sockaddr_ns *sns =
                    301:                                        (struct sockaddr_ns *)sa;
                    302:                                u_long net;
                    303:                                char netnum[8];
                    304:
1.17      deraadt   305:                                *(union ns_net *)&net = sns->sns_addr.x_net;
                    306:                                snprintf(netnum, sizeof netnum, "%xH",
                    307:                                    ntohl(net));
1.1       deraadt   308:                                upHex(netnum);
                    309:                                printf("ns:%-8s ", netnum);
                    310:                                printf("%-17s ",
                    311:                                    ns_phost((struct sockaddr *)sns));
                    312:                                }
                    313:                                break;
                    314:                        case AF_LINK:
                    315:                                {
                    316:                                struct sockaddr_dl *sdl =
                    317:                                        (struct sockaddr_dl *)sa;
1.5       deraadt   318:                                m = printf("%-11.11s ", "<Link>");
                    319:                                if (sdl->sdl_type == IFT_ETHER ||
1.42      mpf       320:                                    sdl->sdl_type == IFT_CARP ||
1.23      fgsch     321:                                    sdl->sdl_type == IFT_FDDI ||
                    322:                                    sdl->sdl_type == IFT_ISO88025)
1.5       deraadt   323:                                        printf("%-17.17s ",
1.11      millert   324:                                            ether_ntoa((struct ether_addr *)LLADDR(sdl)));
1.5       deraadt   325:                                else {
                    326:                                        cp = (char *)LLADDR(sdl);
                    327:                                        n = sdl->sdl_alen;
                    328:                                        goto hexprint;
                    329:                                }
1.1       deraadt   330:                                }
1.5       deraadt   331:                                break;
1.1       deraadt   332:                        default:
                    333:                                m = printf("(%d)", sa->sa_family);
                    334:                                for (cp = sa->sa_len + (char *)sa;
                    335:                                        --cp > sa->sa_data && (*cp == 0);) {}
                    336:                                n = cp - sa->sa_data + 1;
                    337:                                cp = sa->sa_data;
                    338:                        hexprint:
                    339:                                while (--n >= 0)
                    340:                                        m += printf("%x%c", *cp++ & 0xff,
                    341:                                                    n > 0 ? '.' : ' ');
                    342:                                m = 30 - m;
                    343:                                while (m-- > 0)
                    344:                                        putchar(' ');
                    345:                                break;
                    346:                        }
                    347:                        ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
                    348:                }
1.22      camield   349:                if (bflag)
                    350:                        printf("%10lu %10lu",
                    351:                            ifnet.if_ibytes, ifnet.if_obytes);
                    352:                else
                    353:                        printf("%8lu %5lu %8lu %5lu %5lu",
                    354:                            ifnet.if_ipackets, ifnet.if_ierrors,
                    355:                            ifnet.if_opackets, ifnet.if_oerrors,
                    356:                            ifnet.if_collisions);
1.1       deraadt   357:                if (tflag)
1.22      camield   358:                        printf(" %4d", ifnet.if_timer);
1.1       deraadt   359:                if (dflag)
1.22      camield   360:                        printf(" %4d", ifnet.if_snd.ifq_drops);
1.1       deraadt   361:                putchar('\n');
                    362:        }
                    363: }
                    364:
1.4       deraadt   365: #define        MAXIF   100
1.1       deraadt   366: struct iftot {
1.3       deraadt   367:        char    ift_name[IFNAMSIZ];     /* interface name */
1.40      dhartmei  368:        u_long  ift_ip;                 /* input packets */
                    369:        u_long  ift_ib;                 /* input bytes */
                    370:        u_long  ift_ie;                 /* input errors */
                    371:        u_long  ift_op;                 /* output packets */
                    372:        u_long  ift_ob;                 /* output bytes */
                    373:        u_long  ift_oe;                 /* output errors */
                    374:        u_long  ift_co;                 /* collisions */
                    375:        u_long  ift_dr;                 /* drops */
1.1       deraadt   376: } iftot[MAXIF];
                    377:
1.26      millert   378: volatile sig_atomic_t signalled;       /* set if alarm goes off "early" */
1.1       deraadt   379:
                    380: /*
                    381:  * Print a running summary of interface statistics.
                    382:  * Repeat display every interval seconds, showing statistics
                    383:  * collected over that interval.  Assumes that interval is non-zero.
                    384:  * First line printed at top of screen is always cumulative.
                    385:  */
                    386: static void
1.33      deraadt   387: sidewaysintpr(unsigned int interval, u_long off)
1.1       deraadt   388: {
                    389:        struct ifnet ifnet;
                    390:        u_long firstifnet;
1.27      mpech     391:        struct iftot *ip, *total;
                    392:        int line;
1.1       deraadt   393:        struct iftot *lastif, *sum, *interesting;
1.3       deraadt   394:        struct ifnet_head ifhead;       /* TAILQ_HEAD */
1.26      millert   395:        sigset_t emptyset;
1.1       deraadt   396:
1.3       deraadt   397:        /*
                    398:         * Find the pointer to the first ifnet structure.  Replace
                    399:         * the pointer to the TAILQ_HEAD with the actual pointer
                    400:         * to the first list element.
                    401:         */
1.43    ! jaredy    402:        if (kread(off, &ifhead, sizeof ifhead))
1.1       deraadt   403:                return;
1.3       deraadt   404:        firstifnet = (u_long)ifhead.tqh_first;
                    405:
1.1       deraadt   406:        lastif = iftot;
                    407:        sum = iftot + MAXIF - 1;
                    408:        total = sum - 1;
1.4       deraadt   409:        interesting = (interface == NULL) ? iftot : NULL;
1.1       deraadt   410:        for (off = firstifnet, ip = iftot; off;) {
1.43    ! jaredy    411:                if (kread(off, &ifnet, sizeof ifnet))
1.1       deraadt   412:                        break;
1.6       deraadt   413:                bzero(ip->ift_name, sizeof(ip->ift_name));
1.22      camield   414:                snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
1.6       deraadt   415:                if (interface && strcmp(ifnet.if_xname, interface) == 0)
1.1       deraadt   416:                        interesting = ip;
                    417:                ip++;
                    418:                if (ip >= iftot + MAXIF - 2)
                    419:                        break;
                    420:                off = (u_long)ifnet.if_list.tqe_next;
1.4       deraadt   421:        }
                    422:        if (interesting == NULL) {
                    423:                fprintf(stderr, "%s: %s: unknown interface\n",
                    424:                    __progname, interface);
                    425:                exit(1);
1.1       deraadt   426:        }
                    427:        lastif = ip;
                    428:
                    429:        (void)signal(SIGALRM, catchalarm);
                    430:        signalled = NO;
                    431:        (void)alarm(interval);
                    432: banner:
1.22      camield   433:        if (bflag)
                    434:                printf("%7.7s in %8.8s %6.6s out %5.5s",
                    435:                    interesting->ift_name, " ",
                    436:                    interesting->ift_name, " ");
                    437:        else
                    438:                printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
                    439:                    interesting->ift_name, " ",
                    440:                    interesting->ift_name, " ", " ");
                    441:        if (dflag)
                    442:                printf(" %5.5s", " ");
1.1       deraadt   443:        if (lastif - iftot > 0) {
1.22      camield   444:                if (bflag)
                    445:                        printf("  %7.7s in %8.8s %6.6s out %5.5s",
                    446:                            "total", " ", "total", " ");
                    447:                else
                    448:                        printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
                    449:                            "total", " ", "total", " ", " ");
1.1       deraadt   450:                if (dflag)
1.22      camield   451:                        printf(" %5.5s", " ");
1.1       deraadt   452:        }
                    453:        for (ip = iftot; ip < iftot + MAXIF; ip++) {
                    454:                ip->ift_ip = 0;
1.22      camield   455:                ip->ift_ib = 0;
1.1       deraadt   456:                ip->ift_ie = 0;
                    457:                ip->ift_op = 0;
1.22      camield   458:                ip->ift_ob = 0;
1.1       deraadt   459:                ip->ift_oe = 0;
                    460:                ip->ift_co = 0;
                    461:                ip->ift_dr = 0;
                    462:        }
                    463:        putchar('\n');
1.22      camield   464:        if (bflag)
                    465:                printf("%10.10s %8.8s %10.10s %5.5s",
                    466:                    "bytes", " ", "bytes", " ");
                    467:        else
                    468:                printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
                    469:                    "packets", "errs", "packets", "errs", "colls");
1.1       deraadt   470:        if (dflag)
                    471:                printf(" %5.5s", "drops");
1.22      camield   472:        if (lastif - iftot > 0) {
                    473:                if (bflag)
                    474:                        printf("  %10.10s %8.8s %10.10s %5.5s",
                    475:                            "bytes", " ", "bytes", " ");
                    476:                else
                    477:                        printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
                    478:                            "packets", "errs", "packets", "errs", "colls");
                    479:                if (dflag)
                    480:                        printf(" %5.5s", "drops");
                    481:        }
1.1       deraadt   482:        putchar('\n');
                    483:        fflush(stdout);
                    484:        line = 0;
                    485: loop:
                    486:        sum->ift_ip = 0;
1.22      camield   487:        sum->ift_ib = 0;
1.1       deraadt   488:        sum->ift_ie = 0;
                    489:        sum->ift_op = 0;
1.22      camield   490:        sum->ift_ob = 0;
1.1       deraadt   491:        sum->ift_oe = 0;
                    492:        sum->ift_co = 0;
                    493:        sum->ift_dr = 0;
                    494:        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
1.43    ! jaredy    495:                if (kread(off, &ifnet, sizeof ifnet)) {
1.1       deraadt   496:                        off = 0;
                    497:                        continue;
                    498:                }
                    499:                if (ip == interesting) {
1.22      camield   500:                        if (bflag)
                    501:                                printf("%10lu %8.8s %10lu %5.5s",
                    502:                                    ifnet.if_ibytes - ip->ift_ib, " ",
                    503:                                    ifnet.if_obytes - ip->ift_ob, " ");
                    504:                        else
                    505:                                printf("%8lu %5lu %8lu %5lu %5lu",
                    506:                                    ifnet.if_ipackets - ip->ift_ip,
                    507:                                    ifnet.if_ierrors - ip->ift_ie,
                    508:                                    ifnet.if_opackets - ip->ift_op,
                    509:                                    ifnet.if_oerrors - ip->ift_oe,
                    510:                                    ifnet.if_collisions - ip->ift_co);
1.1       deraadt   511:                        if (dflag)
1.41      jaredy    512:                                printf(" %5lu",
1.1       deraadt   513:                                    ifnet.if_snd.ifq_drops - ip->ift_dr);
                    514:                }
                    515:                ip->ift_ip = ifnet.if_ipackets;
1.22      camield   516:                ip->ift_ib = ifnet.if_ibytes;
1.1       deraadt   517:                ip->ift_ie = ifnet.if_ierrors;
                    518:                ip->ift_op = ifnet.if_opackets;
1.22      camield   519:                ip->ift_ob = ifnet.if_obytes;
1.1       deraadt   520:                ip->ift_oe = ifnet.if_oerrors;
                    521:                ip->ift_co = ifnet.if_collisions;
                    522:                ip->ift_dr = ifnet.if_snd.ifq_drops;
                    523:                sum->ift_ip += ip->ift_ip;
1.22      camield   524:                sum->ift_ib += ip->ift_ib;
1.1       deraadt   525:                sum->ift_ie += ip->ift_ie;
                    526:                sum->ift_op += ip->ift_op;
1.22      camield   527:                sum->ift_ob += ip->ift_ob;
1.1       deraadt   528:                sum->ift_oe += ip->ift_oe;
                    529:                sum->ift_co += ip->ift_co;
                    530:                sum->ift_dr += ip->ift_dr;
                    531:                off = (u_long)ifnet.if_list.tqe_next;
                    532:        }
                    533:        if (lastif - iftot > 0) {
1.22      camield   534:                if (bflag)
                    535:                        printf("  %10lu %8.8s %10lu %5.5s",
1.41      jaredy    536:                            sum->ift_ib - total->ift_ib, " ",
                    537:                            sum->ift_ob - total->ift_ob, " ");
1.22      camield   538:                else
                    539:                        printf("  %8lu %5lu %8lu %5lu %5lu",
1.41      jaredy    540:                            sum->ift_ip - total->ift_ip,
                    541:                            sum->ift_ie - total->ift_ie,
                    542:                            sum->ift_op - total->ift_op,
                    543:                            sum->ift_oe - total->ift_oe,
                    544:                            sum->ift_co - total->ift_co);
1.1       deraadt   545:                if (dflag)
1.41      jaredy    546:                        printf(" %5lu", sum->ift_dr - total->ift_dr);
1.1       deraadt   547:        }
                    548:        *total = *sum;
                    549:        putchar('\n');
                    550:        fflush(stdout);
                    551:        line++;
1.26      millert   552:        sigemptyset(&emptyset);
                    553:        if (!signalled)
                    554:                sigsuspend(&emptyset);
1.1       deraadt   555:        signalled = NO;
                    556:        (void)alarm(interval);
                    557:        if (line == 21)
                    558:                goto banner;
                    559:        goto loop;
                    560:        /*NOTREACHED*/
                    561: }
                    562:
                    563: /*
                    564:  * Called if an interval expires before sidewaysintpr has completed a loop.
                    565:  * Sets a flag to not wait for the alarm.
                    566:  */
                    567: static void
1.33      deraadt   568: catchalarm(int signo)
1.1       deraadt   569: {
                    570:        signalled = YES;
                    571: }