[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.34

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