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

1.56    ! claudio     1: /*     $OpenBSD: if.c,v 1.55 2007/12/19 01:47:00 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.
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:
1.56    ! claudio    33: #include <sys/param.h>
1.1       deraadt    34: #include <sys/types.h>
                     35: #include <sys/protosw.h>
                     36: #include <sys/socket.h>
1.56    ! claudio    37: #include <sys/sysctl.h>
1.1       deraadt    38:
                     39: #include <net/if.h>
                     40: #include <net/if_dl.h>
1.5       deraadt    41: #include <net/if_types.h>
1.56    ! claudio    42: #include <net/route.h>
1.1       deraadt    43: #include <netinet/in.h>
                     44: #include <netinet/in_var.h>
1.11      millert    45: #include <netinet/if_ether.h>
1.1       deraadt    46: #include <arpa/inet.h>
                     47:
1.13      millert    48: #include <limits.h>
1.1       deraadt    49: #include <signal.h>
                     50: #include <stdio.h>
1.36      david      51: #include <stdlib.h>
1.1       deraadt    52: #include <string.h>
                     53: #include <unistd.h>
                     54:
                     55: #include "netstat.h"
                     56:
1.56    ! claudio    57: static void print_addr(struct sockaddr *, struct sockaddr **, struct if_data *);
        !            58: static void sidewaysintpr(u_int);
1.29      millert    59: static void catchalarm(int);
1.56    ! claudio    60: static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
        !            61: static void fetchifs(void);
1.1       deraadt    62:
                     63: /*
                     64:  * Print a description of the network interfaces.
1.3       deraadt    65:  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
                     66:  * which is a TAILQ_HEAD.
1.1       deraadt    67:  */
                     68: void
1.56    ! claudio    69: intpr(int interval)
1.1       deraadt    70: {
1.56    ! claudio    71:        struct if_msghdr ifm;
        !            72:        int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
        !            73:        char name[IFNAMSIZ + 1];        /* + 1 for the '*' */
        !            74:        char *buf, *next, *lim, *cp;
        !            75:        struct rt_msghdr *rtm;
        !            76:        struct ifa_msghdr *ifam;
        !            77:        struct if_data *ifd;
        !            78:        struct sockaddr *sa, *rti_info[RTAX_MAX];
        !            79:        struct sockaddr_dl *sdl;
        !            80:        u_int64_t total = 0;
        !            81:        size_t len;
1.1       deraadt    82:
                     83:        if (interval) {
1.56    ! claudio    84:                sidewaysintpr((unsigned)interval);
1.1       deraadt    85:                return;
                     86:        }
1.3       deraadt    87:
1.56    ! claudio    88:        if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
        !            89:                err(1, "sysctl");
        !            90:        if ((buf = malloc(len)) == NULL)
        !            91:                err(1, NULL);
        !            92:        if (sysctl(mib, 6, buf, &len, NULL, 0) == -1)
        !            93:                err(1, "sysctl");
1.3       deraadt    94:
1.22      camield    95:        printf("%-7.7s %-5.5s %-11.11s %-17.17s ",
1.33      deraadt    96:            "Name", "Mtu", "Network", "Address");
1.22      camield    97:        if (bflag)
                     98:                printf("%10.10s %10.10s", "Ibytes", "Obytes");
                     99:        else
                    100:                printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
                    101:                    "Ipkts", "Ierrs", "Opkts", "Oerrs", "Colls");
1.1       deraadt   102:        if (tflag)
                    103:                printf(" %s", "Time");
                    104:        if (dflag)
                    105:                printf(" %s", "Drop");
                    106:        putchar('\n');
1.56    ! claudio   107:
        !           108:        lim = buf + len;
        !           109:        for (next = buf; next < lim; next += rtm->rtm_msglen) {
        !           110:                rtm = (struct rt_msghdr *)next;
        !           111:                if (rtm->rtm_version != RTM_VERSION)
        !           112:                        continue;
        !           113:                switch (rtm->rtm_type) {
        !           114:                case RTM_IFINFO:
        !           115:                        total = 0;
        !           116:                        bcopy(next, &ifm, sizeof ifm);
        !           117:                        ifd = &ifm.ifm_data;
        !           118:
        !           119:                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
        !           120:                        get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
        !           121:
        !           122:                        sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
        !           123:                        if (sdl == NULL || sdl->sdl_family != AF_LINK)
        !           124:                                continue;
        !           125:                        bzero(name, sizeof(name));
        !           126:                        if (sdl->sdl_nlen >= IFNAMSIZ)
        !           127:                                memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
        !           128:                        else if (sdl->sdl_nlen > 0)
        !           129:                                memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
        !           130:
1.3       deraadt   131:                        if (interface != 0 && strcmp(name, interface) != 0)
1.1       deraadt   132:                                continue;
1.56    ! claudio   133:
        !           134:                        /* mark inactive interfaces with a '*' */
1.10      millert   135:                        cp = strchr(name, '\0');
1.56    ! claudio   136:                        if ((ifm.ifm_flags & IFF_UP) == 0)
1.1       deraadt   137:                                *cp++ = '*';
                    138:                        *cp = '\0';
1.25      brian     139:
1.56    ! claudio   140:                        if (qflag) {
        !           141:                                total = ifd->ifi_ibytes + ifd->ifi_obytes +
        !           142:                                    ifd->ifi_ipackets + ifd->ifi_ierrors +
        !           143:                                    ifd->ifi_opackets + ifd->ifi_oerrors +
        !           144:                                    ifd->ifi_collisions;
        !           145:                                if (tflag)
        !           146:                                        total += 0; // XXX ifnet.if_timer;
        !           147:                                if (dflag)
        !           148:                                        total += 0; // XXX ifnet.if_snd.ifq_drops;
        !           149:                                if (total == 0)
        !           150:                                        continue;
        !           151:                        }
        !           152:
        !           153:                        printf("%-7s %-5ld ", name, ifd->ifi_mtu);
        !           154:                        print_addr(rti_info[RTAX_IFP], rti_info, ifd);
        !           155:                        break;
        !           156:                case RTM_NEWADDR:
        !           157:                        if (qflag && total == 0)
1.25      brian     158:                                continue;
1.56    ! claudio   159:                        ifam = (struct ifa_msghdr *)next;
        !           160:                        if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
        !           161:                            RTA_BRD)) == 0)
        !           162:                                break;
        !           163:
        !           164:                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
        !           165:                        get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
        !           166:
        !           167:                        printf("%-7s %-5ld ", name, ifd->ifi_mtu);
        !           168:                        print_addr(rti_info[RTAX_IFA], rti_info, ifd);
        !           169:                        break;
1.25      brian     170:                }
1.56    ! claudio   171:        }
        !           172: }
1.25      brian     173:
1.56    ! claudio   174: static void
        !           175: print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd)
        !           176: {
        !           177:        struct sockaddr_dl *sdl;
        !           178:        struct sockaddr_in *sin;
        !           179:        struct sockaddr_in6 *sin6;
        !           180:        char *cp;
        !           181:        int m, n;
        !           182:
        !           183:        switch (sa->sa_family) {
        !           184:        case AF_UNSPEC:
        !           185:                printf("%-11.11s ", "none");
        !           186:                printf("%-17.17s ", "none");
        !           187:                break;
        !           188:        case AF_INET:
        !           189:                sin = (struct sockaddr_in *)sa;
        !           190:                cp = netname4(sin->sin_addr.s_addr,
        !           191:                    ((struct sockaddr_in *)rtinfo[RTAX_NETMASK])->sin_addr.s_addr);
        !           192:                if (vflag)
        !           193:                        n = strlen(cp) < 11 ? 11 : strlen(cp);
        !           194:                else
        !           195:                        n = 11;
        !           196:                printf("%-*.*s ", n, n, cp);
        !           197:                cp = routename4(sin->sin_addr.s_addr);
        !           198:                if (vflag)
        !           199:                        n = strlen(cp) < 17 ? 17 : strlen(cp);
        !           200:                else
        !           201:                        n = 17;
        !           202:                printf("%-*.*s ", n, n, cp);
        !           203:
        !           204: #if 0
        !           205:                if (aflag) {
        !           206:                        u_long multiaddr;
        !           207:                        struct in_multi inm;
        !           208:
        !           209:                        multiaddr = (u_long)LIST_FIRST(&ifaddr.in.ia_multiaddrs);
        !           210:                        while (multiaddr != 0) {
        !           211:                                kread(multiaddr, &inm, sizeof inm);
        !           212:                                printf("\n%25s %-17.17s ", "",
        !           213:                                    routename4(inm.inm_addr.s_addr));
        !           214:                                multiaddr = (u_long)LIST_NEXT(&inm, inm_list);
1.1       deraadt   215:                        }
1.56    ! claudio   216:                }
        !           217: #endif
        !           218:                break;
        !           219:        case AF_INET6:
        !           220:                sin6 = (struct sockaddr_in6 *)sa;
        !           221: #ifdef __KAME__
        !           222:                if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
        !           223:                        sin6->sin6_scope_id =
        !           224:                            ntohs(*(u_int16_t *)
        !           225:                            &sin6->sin6_addr.s6_addr[2]);
        !           226:                        sin6->sin6_addr.s6_addr[2] = 0;
        !           227:                        sin6->sin6_addr.s6_addr[3] = 0;
        !           228:                }
1.1       deraadt   229: #endif
1.56    ! claudio   230:                cp = netname6(sin6,
        !           231:                    (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK]);
        !           232:                if (vflag)
        !           233:                        n = strlen(cp) < 11 ? 11 : strlen(cp);
        !           234:                else
        !           235:                        n = 11;
        !           236:                printf("%-*.*s ", n, n, cp);
        !           237:                cp = routename6(sin6);
        !           238:                if (vflag)
        !           239:                        n = strlen(cp) < 17 ? 17 : strlen(cp);
        !           240:                else
        !           241:                        n = 17;
        !           242:                printf("%-*.*s ", n, n, cp);
        !           243: #if 0
        !           244:                if (aflag) {
        !           245:                        u_long multiaddr;
        !           246:                        struct in6_multi inm;
        !           247:                        struct sockaddr_in6 m6;
        !           248:
        !           249:                        multiaddr = (u_long)LIST_FIRST(&ifaddr.in6.ia6_multiaddrs);
        !           250:                        while (multiaddr != 0) {
        !           251:                                kread(multiaddr, &inm, sizeof inm);
        !           252:                                memset(&m6, 0, sizeof(m6));
        !           253:                                m6.sin6_len = sizeof(struct sockaddr_in6);
        !           254:                                m6.sin6_family = AF_INET6;
        !           255:                                m6.sin6_addr = inm.in6m_addr;
1.34      itojun    256: #ifdef __KAME__
1.56    ! claudio   257:                                if (IN6_IS_ADDR_MC_LINKLOCAL(&m6.sin6_addr) ||
        !           258:                                    IN6_IS_ADDR_MC_INTFACELOCAL(&m6.sin6_addr)) {
        !           259:                                        m6.sin6_scope_id =
1.30      deraadt   260:                                            ntohs(*(u_int16_t *)
1.56    ! claudio   261:                                            &m6.sin6_addr.s6_addr[2]);
        !           262:                                        m6.sin6_addr.s6_addr[2] = 0;
        !           263:                                        m6.sin6_addr.s6_addr[3] = 0;
1.20      itojun    264:                                }
                    265: #endif
1.56    ! claudio   266:                                cp = routename6(&m6);
1.20      itojun    267:                                if (vflag)
                    268:                                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    269:                                else
                    270:                                        n = 17;
1.56    ! claudio   271:                                printf("\n%25s %-*.*s ", "",
        !           272:                                    n, n, cp);
        !           273:                                multiaddr = (u_long)LIST_NEXT(&inm, in6m_entry);
        !           274:                        }
        !           275:                }
1.31      itojun    276: #endif
1.56    ! claudio   277:                break;
        !           278:        case AF_APPLETALK:
        !           279:                printf("atlk:%-12s",atalk_print(sa,0x10) );
        !           280:                printf("%-12s ",atalk_print(sa,0x0b) );
        !           281:                break;
        !           282:        case AF_LINK:
        !           283:                sdl = (struct sockaddr_dl *)sa;
        !           284:                m = printf("%-11.11s ", "<Link>");
        !           285:                if (sdl->sdl_type == IFT_ETHER ||
        !           286:                    sdl->sdl_type == IFT_CARP ||
        !           287:                    sdl->sdl_type == IFT_FDDI ||
        !           288:                    sdl->sdl_type == IFT_ISO88025)
        !           289:                        printf("%-17.17s ",
        !           290:                            ether_ntoa((struct ether_addr *)LLADDR(sdl)));
        !           291:                else {
        !           292:                        cp = (char *)LLADDR(sdl);
        !           293:                        n = sdl->sdl_alen;
        !           294:                        goto hexprint;
1.1       deraadt   295:                }
1.56    ! claudio   296:                break;
        !           297:        default:
        !           298:                m = printf("(%d)", sa->sa_family);
        !           299:                for (cp = sa->sa_len + (char *)sa;
        !           300:                        --cp > sa->sa_data && (*cp == 0);) {}
        !           301:                n = cp - sa->sa_data + 1;
        !           302:                cp = sa->sa_data;
        !           303: hexprint:
        !           304:                while (--n >= 0)
        !           305:                        m += printf("%x%c", *cp++ & 0xff,
        !           306:                                    n > 0 ? '.' : ' ');
        !           307:                m = 30 - m;
        !           308:                while (m-- > 0)
        !           309:                        putchar(' ');
        !           310:                break;
1.1       deraadt   311:        }
1.56    ! claudio   312:        if (bflag)
        !           313:                printf("%10llu %10llu",
        !           314:                    ifd->ifi_ibytes, ifd->ifi_obytes);
        !           315:        else
        !           316:                printf("%8llu %5llu %8llu %5llu %5llu",
        !           317:                    ifd->ifi_ipackets, ifd->ifi_ierrors,
        !           318:                    ifd->ifi_opackets, ifd->ifi_oerrors,
        !           319:                    ifd->ifi_collisions);
        !           320:        if (tflag)
        !           321:                printf(" %4d", 0 /* XXX ifnet.if_timer */);
        !           322:        if (dflag)
        !           323:                printf(" %4d", 0 /* XXX ifnet.if_snd.ifq_drops */);
        !           324:        putchar('\n');
1.1       deraadt   325: }
                    326:
                    327: struct iftot {
1.3       deraadt   328:        char    ift_name[IFNAMSIZ];     /* interface name */
1.53      mk        329:        u_int64_t ift_ip;               /* input packets */
                    330:        u_int64_t ift_ib;               /* input bytes */
                    331:        u_int64_t ift_ie;               /* input errors */
                    332:        u_int64_t ift_op;               /* output packets */
                    333:        u_int64_t ift_ob;               /* output bytes */
                    334:        u_int64_t ift_oe;               /* output errors */
                    335:        u_int64_t ift_co;               /* collisions */
                    336:        u_int64_t ift_dr;               /* drops */
1.56    ! claudio   337: } ip_cur, ip_old, sum_cur, sum_old;
1.1       deraadt   338:
1.26      millert   339: volatile sig_atomic_t signalled;       /* set if alarm goes off "early" */
1.1       deraadt   340:
                    341: /*
                    342:  * Print a running summary of interface statistics.
                    343:  * Repeat display every interval seconds, showing statistics
                    344:  * collected over that interval.  Assumes that interval is non-zero.
                    345:  * First line printed at top of screen is always cumulative.
                    346:  */
                    347: static void
1.56    ! claudio   348: sidewaysintpr(unsigned int interval)
1.1       deraadt   349: {
                    350:        struct ifnet ifnet;
1.56    ! claudio   351:        sigset_t emptyset;
1.27      mpech     352:        int line;
1.3       deraadt   353:
1.56    ! claudio   354:        fetchifs();
        !           355:        if (ip_cur.ift_name[0] == '\0') {
1.4       deraadt   356:                fprintf(stderr, "%s: %s: unknown interface\n",
                    357:                    __progname, interface);
                    358:                exit(1);
1.1       deraadt   359:        }
                    360:
                    361:        (void)signal(SIGALRM, catchalarm);
1.56    ! claudio   362:        signalled = 0;
1.1       deraadt   363:        (void)alarm(interval);
                    364: banner:
1.22      camield   365:        if (bflag)
                    366:                printf("%7.7s in %8.8s %6.6s out %5.5s",
1.56    ! claudio   367:                    ip_cur.ift_name, " ",
        !           368:                    ip_cur.ift_name, " ");
1.22      camield   369:        else
                    370:                printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
1.56    ! claudio   371:                    ip_cur.ift_name, " ",
        !           372:                    ip_cur.ift_name, " ", " ");
        !           373:        if (dflag)
        !           374:                printf(" %5.5s", " ");
        !           375:
        !           376:        if (bflag)
        !           377:                printf("  %7.7s in %8.8s %6.6s out %5.5s",
        !           378:                    "total", " ", "total", " ");
        !           379:        else
        !           380:                printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
        !           381:                    "total", " ", "total", " ", " ");
1.22      camield   382:        if (dflag)
                    383:                printf(" %5.5s", " ");
1.1       deraadt   384:        putchar('\n');
1.22      camield   385:        if (bflag)
                    386:                printf("%10.10s %8.8s %10.10s %5.5s",
                    387:                    "bytes", " ", "bytes", " ");
                    388:        else
                    389:                printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
                    390:                    "packets", "errs", "packets", "errs", "colls");
1.1       deraadt   391:        if (dflag)
                    392:                printf(" %5.5s", "drops");
1.56    ! claudio   393:
        !           394:        if (bflag)
        !           395:                printf("  %10.10s %8.8s %10.10s %5.5s",
        !           396:                    "bytes", " ", "bytes", " ");
        !           397:        else
        !           398:                printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
        !           399:                    "packets", "errs", "packets", "errs", "colls");
        !           400:        if (dflag)
        !           401:                printf(" %5.5s", "drops");
1.1       deraadt   402:        putchar('\n');
                    403:        fflush(stdout);
                    404:        line = 0;
1.56    ! claudio   405:        bzero(&ip_old, sizeof(ip_old));
        !           406:        bzero(&sum_old, sizeof(sum_old));
1.1       deraadt   407: loop:
1.56    ! claudio   408:        bzero(&sum_cur, sizeof(sum_cur));
        !           409:
        !           410:        fetchifs();
        !           411:
        !           412:        if (bflag)
        !           413:                printf("%10llu %8.8s %10llu %5.5s",
        !           414:                    ip_cur.ift_ib - ip_old.ift_ib, " ",
        !           415:                    ip_cur.ift_ob - ip_old.ift_ob, " ");
        !           416:        else
        !           417:                printf("%8llu %5llu %8llu %5llu %5llu",
        !           418:                    ip_cur.ift_ip - ip_old.ift_ip,
        !           419:                    ip_cur.ift_ie - ip_old.ift_ie,
        !           420:                    ip_cur.ift_op - ip_old.ift_op,
        !           421:                    ip_cur.ift_oe - ip_old.ift_oe,
        !           422:                    ip_cur.ift_co - ip_old.ift_co);
        !           423:        if (dflag)
        !           424:                printf(" %5llu",
        !           425:                    /* XXX ifnet.if_snd.ifq_drops - ip->ift_dr); */
        !           426:                    0);
        !           427:
        !           428:        ip_old = ip_cur;
        !           429:
        !           430:        if (bflag)
        !           431:                printf("  %10llu %8.8s %10llu %5.5s",
        !           432:                    sum_cur.ift_ib - sum_old.ift_ib, " ",
        !           433:                    sum_cur.ift_ob - sum_old.ift_ob, " ");
        !           434:        else
        !           435:                printf("  %8llu %5llu %8llu %5llu %5llu",
        !           436:                    sum_cur.ift_ip - sum_old.ift_ip,
        !           437:                    sum_cur.ift_ie - sum_old.ift_ie,
        !           438:                    sum_cur.ift_op - sum_old.ift_op,
        !           439:                    sum_cur.ift_oe - sum_old.ift_oe,
        !           440:                    sum_cur.ift_co - sum_old.ift_co);
        !           441:        if (dflag)
        !           442:                printf(" %5llu", sum_cur.ift_dr - sum_old.ift_dr);
        !           443:
        !           444:        sum_old = sum_cur;
        !           445:
1.1       deraadt   446:        putchar('\n');
                    447:        fflush(stdout);
                    448:        line++;
1.26      millert   449:        sigemptyset(&emptyset);
                    450:        if (!signalled)
                    451:                sigsuspend(&emptyset);
1.56    ! claudio   452:        signalled = 0;
1.1       deraadt   453:        (void)alarm(interval);
                    454:        if (line == 21)
                    455:                goto banner;
                    456:        goto loop;
                    457:        /*NOTREACHED*/
                    458: }
                    459:
                    460: /*
                    461:  * Called if an interval expires before sidewaysintpr has completed a loop.
                    462:  * Sets a flag to not wait for the alarm.
                    463:  */
1.46      deraadt   464: /* ARGSUSED */
1.1       deraadt   465: static void
1.33      deraadt   466: catchalarm(int signo)
1.1       deraadt   467: {
1.56    ! claudio   468:        signalled = 1;
        !           469: }
        !           470:
        !           471: static void
        !           472: get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
        !           473: {
        !           474:        int i;
        !           475:
        !           476:        for (i = 0; i < RTAX_MAX; i++) {
        !           477:                if (addrs & (1 << i)) {
        !           478:                        rti_info[i] = sa;
        !           479:                        sa = (struct sockaddr *)((char *)(sa) +
        !           480:                            roundup(sa->sa_len, sizeof(long)));
        !           481:                } else
        !           482:                        rti_info[i] = NULL;
        !           483:        }
        !           484: }
        !           485:
        !           486: static void
        !           487: fetchifs(void)
        !           488: {
        !           489:        struct if_msghdr ifm;
        !           490:        int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
        !           491:        struct rt_msghdr *rtm;
        !           492:        struct if_data *ifd;
        !           493:        struct sockaddr *sa, *rti_info[RTAX_MAX];
        !           494:        struct sockaddr_dl *sdl;
        !           495:        char *buf, *next, *lim;
        !           496:        char name[IFNAMSIZ];
        !           497:        size_t len;
        !           498:
        !           499:        if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
        !           500:                err(1, "sysctl");
        !           501:        if ((buf = malloc(len)) == NULL)
        !           502:                err(1, NULL);
        !           503:        if (sysctl(mib, 6, buf, &len, NULL, 0) == -1)
        !           504:                err(1, "sysctl");
        !           505:
        !           506:        lim = buf + len;
        !           507:        for (next = buf; next < lim; next += rtm->rtm_msglen) {
        !           508:                rtm = (struct rt_msghdr *)next;
        !           509:                if (rtm->rtm_version != RTM_VERSION)
        !           510:                        continue;
        !           511:                switch (rtm->rtm_type) {
        !           512:                case RTM_IFINFO:
        !           513:                        bcopy(next, &ifm, sizeof ifm);
        !           514:                        ifd = &ifm.ifm_data;
        !           515:
        !           516:                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
        !           517:                        get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
        !           518:
        !           519:                        sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
        !           520:                        if (sdl == NULL || sdl->sdl_family != AF_LINK)
        !           521:                                continue;
        !           522:                        bzero(name, sizeof(name));
        !           523:                        if (sdl->sdl_nlen >= IFNAMSIZ)
        !           524:                                memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
        !           525:                        else if (sdl->sdl_nlen > 0)
        !           526:                                memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
        !           527:
        !           528:                        if (interface != 0 && !strcmp(name, interface)) {
        !           529:                                strlcpy(ip_cur.ift_name, name,
        !           530:                                    sizeof(ip_cur.ift_name));
        !           531:                                ip_cur.ift_ip = ifd->ifi_ipackets;
        !           532:                                ip_cur.ift_ib = ifd->ifi_ibytes;
        !           533:                                ip_cur.ift_ie = ifd->ifi_ierrors;
        !           534:                                ip_cur.ift_op = ifd->ifi_opackets;
        !           535:                                ip_cur.ift_ob = ifd->ifi_obytes;
        !           536:                                ip_cur.ift_oe = ifd->ifi_oerrors;
        !           537:                                ip_cur.ift_co = ifd->ifi_collisions;
        !           538:                                ip_cur.ift_dr = 0;
        !           539:                                    /* XXX ifnet.if_snd.ifq_drops */
        !           540:                        }
        !           541:
        !           542:                        sum_cur.ift_ip += ifd->ifi_ipackets;
        !           543:                        sum_cur.ift_ib += ifd->ifi_ibytes;
        !           544:                        sum_cur.ift_ie += ifd->ifi_ierrors;
        !           545:                        sum_cur.ift_op += ifd->ifi_opackets;
        !           546:                        sum_cur.ift_ob += ifd->ifi_obytes;
        !           547:                        sum_cur.ift_oe += ifd->ifi_oerrors;
        !           548:                        sum_cur.ift_co += ifd->ifi_collisions;
        !           549:                        sum_cur.ift_dr += 0; /* XXX ifnet.if_snd.ifq_drops */
        !           550:                        break;
        !           551:                }
        !           552:        }
        !           553:        if (interface == NULL) {
        !           554:                strlcpy(ip_cur.ift_name, name,
        !           555:                    sizeof(ip_cur.ift_name));
        !           556:                ip_cur.ift_ip = ifd->ifi_ipackets;
        !           557:                ip_cur.ift_ib = ifd->ifi_ibytes;
        !           558:                ip_cur.ift_ie = ifd->ifi_ierrors;
        !           559:                ip_cur.ift_op = ifd->ifi_opackets;
        !           560:                ip_cur.ift_ob = ifd->ifi_obytes;
        !           561:                ip_cur.ift_oe = ifd->ifi_oerrors;
        !           562:                ip_cur.ift_co = ifd->ifi_collisions;
        !           563:                ip_cur.ift_dr = 0;
        !           564:                    /* XXX ifnet.if_snd.ifq_drops */
        !           565:        }
1.47      claudio   566: }