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

1.59    ! claudio     1: /*     $OpenBSD: if.c,v 1.58 2008/12/24 16:53:22 dhill 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.57      claudio   159:                        if (interface != 0 && strcmp(name, interface) != 0)
                    160:                                continue;
                    161:
1.56      claudio   162:                        ifam = (struct ifa_msghdr *)next;
                    163:                        if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
                    164:                            RTA_BRD)) == 0)
                    165:                                break;
                    166:
                    167:                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
                    168:                        get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
                    169:
                    170:                        printf("%-7s %-5ld ", name, ifd->ifi_mtu);
                    171:                        print_addr(rti_info[RTAX_IFA], rti_info, ifd);
                    172:                        break;
1.25      brian     173:                }
1.56      claudio   174:        }
1.58      dhill     175:        free(buf);
1.56      claudio   176: }
1.25      brian     177:
1.56      claudio   178: static void
                    179: print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd)
                    180: {
                    181:        struct sockaddr_dl *sdl;
                    182:        struct sockaddr_in *sin;
                    183:        struct sockaddr_in6 *sin6;
                    184:        char *cp;
                    185:        int m, n;
                    186:
                    187:        switch (sa->sa_family) {
                    188:        case AF_UNSPEC:
                    189:                printf("%-11.11s ", "none");
                    190:                printf("%-17.17s ", "none");
                    191:                break;
                    192:        case AF_INET:
                    193:                sin = (struct sockaddr_in *)sa;
                    194:                cp = netname4(sin->sin_addr.s_addr,
                    195:                    ((struct sockaddr_in *)rtinfo[RTAX_NETMASK])->sin_addr.s_addr);
                    196:                if (vflag)
                    197:                        n = strlen(cp) < 11 ? 11 : strlen(cp);
                    198:                else
                    199:                        n = 11;
                    200:                printf("%-*.*s ", n, n, cp);
                    201:                cp = routename4(sin->sin_addr.s_addr);
                    202:                if (vflag)
                    203:                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    204:                else
                    205:                        n = 17;
                    206:                printf("%-*.*s ", n, n, cp);
                    207:
                    208: #if 0
                    209:                if (aflag) {
                    210:                        u_long multiaddr;
                    211:                        struct in_multi inm;
                    212:
                    213:                        multiaddr = (u_long)LIST_FIRST(&ifaddr.in.ia_multiaddrs);
                    214:                        while (multiaddr != 0) {
                    215:                                kread(multiaddr, &inm, sizeof inm);
                    216:                                printf("\n%25s %-17.17s ", "",
                    217:                                    routename4(inm.inm_addr.s_addr));
                    218:                                multiaddr = (u_long)LIST_NEXT(&inm, inm_list);
1.1       deraadt   219:                        }
1.56      claudio   220:                }
                    221: #endif
                    222:                break;
                    223:        case AF_INET6:
                    224:                sin6 = (struct sockaddr_in6 *)sa;
                    225: #ifdef __KAME__
                    226:                if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
                    227:                        sin6->sin6_scope_id =
                    228:                            ntohs(*(u_int16_t *)
                    229:                            &sin6->sin6_addr.s6_addr[2]);
                    230:                        sin6->sin6_addr.s6_addr[2] = 0;
                    231:                        sin6->sin6_addr.s6_addr[3] = 0;
                    232:                }
1.1       deraadt   233: #endif
1.56      claudio   234:                cp = netname6(sin6,
                    235:                    (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK]);
                    236:                if (vflag)
                    237:                        n = strlen(cp) < 11 ? 11 : strlen(cp);
                    238:                else
                    239:                        n = 11;
                    240:                printf("%-*.*s ", n, n, cp);
                    241:                cp = routename6(sin6);
                    242:                if (vflag)
                    243:                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    244:                else
                    245:                        n = 17;
                    246:                printf("%-*.*s ", n, n, cp);
                    247: #if 0
                    248:                if (aflag) {
                    249:                        u_long multiaddr;
                    250:                        struct in6_multi inm;
                    251:                        struct sockaddr_in6 m6;
                    252:
                    253:                        multiaddr = (u_long)LIST_FIRST(&ifaddr.in6.ia6_multiaddrs);
                    254:                        while (multiaddr != 0) {
                    255:                                kread(multiaddr, &inm, sizeof inm);
                    256:                                memset(&m6, 0, sizeof(m6));
                    257:                                m6.sin6_len = sizeof(struct sockaddr_in6);
                    258:                                m6.sin6_family = AF_INET6;
                    259:                                m6.sin6_addr = inm.in6m_addr;
1.34      itojun    260: #ifdef __KAME__
1.56      claudio   261:                                if (IN6_IS_ADDR_MC_LINKLOCAL(&m6.sin6_addr) ||
                    262:                                    IN6_IS_ADDR_MC_INTFACELOCAL(&m6.sin6_addr)) {
                    263:                                        m6.sin6_scope_id =
1.30      deraadt   264:                                            ntohs(*(u_int16_t *)
1.56      claudio   265:                                            &m6.sin6_addr.s6_addr[2]);
                    266:                                        m6.sin6_addr.s6_addr[2] = 0;
                    267:                                        m6.sin6_addr.s6_addr[3] = 0;
1.20      itojun    268:                                }
                    269: #endif
1.56      claudio   270:                                cp = routename6(&m6);
1.20      itojun    271:                                if (vflag)
                    272:                                        n = strlen(cp) < 17 ? 17 : strlen(cp);
                    273:                                else
                    274:                                        n = 17;
1.56      claudio   275:                                printf("\n%25s %-*.*s ", "",
                    276:                                    n, n, cp);
                    277:                                multiaddr = (u_long)LIST_NEXT(&inm, in6m_entry);
                    278:                        }
                    279:                }
1.31      itojun    280: #endif
1.56      claudio   281:                break;
                    282:        case AF_APPLETALK:
                    283:                printf("atlk:%-12s",atalk_print(sa,0x10) );
                    284:                printf("%-12s ",atalk_print(sa,0x0b) );
                    285:                break;
                    286:        case AF_LINK:
                    287:                sdl = (struct sockaddr_dl *)sa;
                    288:                m = printf("%-11.11s ", "<Link>");
                    289:                if (sdl->sdl_type == IFT_ETHER ||
                    290:                    sdl->sdl_type == IFT_CARP ||
                    291:                    sdl->sdl_type == IFT_FDDI ||
                    292:                    sdl->sdl_type == IFT_ISO88025)
                    293:                        printf("%-17.17s ",
                    294:                            ether_ntoa((struct ether_addr *)LLADDR(sdl)));
                    295:                else {
                    296:                        cp = (char *)LLADDR(sdl);
                    297:                        n = sdl->sdl_alen;
                    298:                        goto hexprint;
1.1       deraadt   299:                }
1.56      claudio   300:                break;
                    301:        default:
                    302:                m = printf("(%d)", sa->sa_family);
                    303:                for (cp = sa->sa_len + (char *)sa;
                    304:                        --cp > sa->sa_data && (*cp == 0);) {}
                    305:                n = cp - sa->sa_data + 1;
                    306:                cp = sa->sa_data;
                    307: hexprint:
                    308:                while (--n >= 0)
                    309:                        m += printf("%x%c", *cp++ & 0xff,
                    310:                                    n > 0 ? '.' : ' ');
                    311:                m = 30 - m;
                    312:                while (m-- > 0)
                    313:                        putchar(' ');
                    314:                break;
1.1       deraadt   315:        }
1.56      claudio   316:        if (bflag)
                    317:                printf("%10llu %10llu",
                    318:                    ifd->ifi_ibytes, ifd->ifi_obytes);
                    319:        else
                    320:                printf("%8llu %5llu %8llu %5llu %5llu",
                    321:                    ifd->ifi_ipackets, ifd->ifi_ierrors,
                    322:                    ifd->ifi_opackets, ifd->ifi_oerrors,
                    323:                    ifd->ifi_collisions);
                    324:        if (tflag)
                    325:                printf(" %4d", 0 /* XXX ifnet.if_timer */);
                    326:        if (dflag)
                    327:                printf(" %4d", 0 /* XXX ifnet.if_snd.ifq_drops */);
                    328:        putchar('\n');
1.1       deraadt   329: }
                    330:
                    331: struct iftot {
1.3       deraadt   332:        char    ift_name[IFNAMSIZ];     /* interface name */
1.53      mk        333:        u_int64_t ift_ip;               /* input packets */
                    334:        u_int64_t ift_ib;               /* input bytes */
                    335:        u_int64_t ift_ie;               /* input errors */
                    336:        u_int64_t ift_op;               /* output packets */
                    337:        u_int64_t ift_ob;               /* output bytes */
                    338:        u_int64_t ift_oe;               /* output errors */
                    339:        u_int64_t ift_co;               /* collisions */
                    340:        u_int64_t ift_dr;               /* drops */
1.56      claudio   341: } ip_cur, ip_old, sum_cur, sum_old;
1.1       deraadt   342:
1.26      millert   343: volatile sig_atomic_t signalled;       /* set if alarm goes off "early" */
1.1       deraadt   344:
                    345: /*
                    346:  * Print a running summary of interface statistics.
                    347:  * Repeat display every interval seconds, showing statistics
                    348:  * collected over that interval.  Assumes that interval is non-zero.
                    349:  * First line printed at top of screen is always cumulative.
                    350:  */
                    351: static void
1.56      claudio   352: sidewaysintpr(unsigned int interval)
1.1       deraadt   353: {
                    354:        struct ifnet ifnet;
1.56      claudio   355:        sigset_t emptyset;
1.27      mpech     356:        int line;
1.3       deraadt   357:
1.56      claudio   358:        fetchifs();
                    359:        if (ip_cur.ift_name[0] == '\0') {
1.4       deraadt   360:                fprintf(stderr, "%s: %s: unknown interface\n",
                    361:                    __progname, interface);
                    362:                exit(1);
1.1       deraadt   363:        }
                    364:
                    365:        (void)signal(SIGALRM, catchalarm);
1.56      claudio   366:        signalled = 0;
1.1       deraadt   367:        (void)alarm(interval);
                    368: banner:
1.22      camield   369:        if (bflag)
                    370:                printf("%7.7s in %8.8s %6.6s out %5.5s",
1.56      claudio   371:                    ip_cur.ift_name, " ",
                    372:                    ip_cur.ift_name, " ");
1.22      camield   373:        else
                    374:                printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
1.56      claudio   375:                    ip_cur.ift_name, " ",
                    376:                    ip_cur.ift_name, " ", " ");
                    377:        if (dflag)
                    378:                printf(" %5.5s", " ");
                    379:
                    380:        if (bflag)
                    381:                printf("  %7.7s in %8.8s %6.6s out %5.5s",
                    382:                    "total", " ", "total", " ");
                    383:        else
                    384:                printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
                    385:                    "total", " ", "total", " ", " ");
1.22      camield   386:        if (dflag)
                    387:                printf(" %5.5s", " ");
1.1       deraadt   388:        putchar('\n');
1.22      camield   389:        if (bflag)
                    390:                printf("%10.10s %8.8s %10.10s %5.5s",
                    391:                    "bytes", " ", "bytes", " ");
                    392:        else
                    393:                printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
                    394:                    "packets", "errs", "packets", "errs", "colls");
1.1       deraadt   395:        if (dflag)
                    396:                printf(" %5.5s", "drops");
1.56      claudio   397:
                    398:        if (bflag)
                    399:                printf("  %10.10s %8.8s %10.10s %5.5s",
                    400:                    "bytes", " ", "bytes", " ");
                    401:        else
                    402:                printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
                    403:                    "packets", "errs", "packets", "errs", "colls");
                    404:        if (dflag)
                    405:                printf(" %5.5s", "drops");
1.1       deraadt   406:        putchar('\n');
                    407:        fflush(stdout);
                    408:        line = 0;
1.56      claudio   409:        bzero(&ip_old, sizeof(ip_old));
                    410:        bzero(&sum_old, sizeof(sum_old));
1.1       deraadt   411: loop:
1.56      claudio   412:        bzero(&sum_cur, sizeof(sum_cur));
                    413:
                    414:        fetchifs();
                    415:
                    416:        if (bflag)
                    417:                printf("%10llu %8.8s %10llu %5.5s",
                    418:                    ip_cur.ift_ib - ip_old.ift_ib, " ",
                    419:                    ip_cur.ift_ob - ip_old.ift_ob, " ");
                    420:        else
                    421:                printf("%8llu %5llu %8llu %5llu %5llu",
                    422:                    ip_cur.ift_ip - ip_old.ift_ip,
                    423:                    ip_cur.ift_ie - ip_old.ift_ie,
                    424:                    ip_cur.ift_op - ip_old.ift_op,
                    425:                    ip_cur.ift_oe - ip_old.ift_oe,
                    426:                    ip_cur.ift_co - ip_old.ift_co);
                    427:        if (dflag)
                    428:                printf(" %5llu",
                    429:                    /* XXX ifnet.if_snd.ifq_drops - ip->ift_dr); */
                    430:                    0);
                    431:
                    432:        ip_old = ip_cur;
                    433:
                    434:        if (bflag)
                    435:                printf("  %10llu %8.8s %10llu %5.5s",
                    436:                    sum_cur.ift_ib - sum_old.ift_ib, " ",
                    437:                    sum_cur.ift_ob - sum_old.ift_ob, " ");
                    438:        else
                    439:                printf("  %8llu %5llu %8llu %5llu %5llu",
                    440:                    sum_cur.ift_ip - sum_old.ift_ip,
                    441:                    sum_cur.ift_ie - sum_old.ift_ie,
                    442:                    sum_cur.ift_op - sum_old.ift_op,
                    443:                    sum_cur.ift_oe - sum_old.ift_oe,
                    444:                    sum_cur.ift_co - sum_old.ift_co);
                    445:        if (dflag)
                    446:                printf(" %5llu", sum_cur.ift_dr - sum_old.ift_dr);
                    447:
                    448:        sum_old = sum_cur;
                    449:
1.1       deraadt   450:        putchar('\n');
                    451:        fflush(stdout);
                    452:        line++;
1.26      millert   453:        sigemptyset(&emptyset);
                    454:        if (!signalled)
                    455:                sigsuspend(&emptyset);
1.56      claudio   456:        signalled = 0;
1.1       deraadt   457:        (void)alarm(interval);
                    458:        if (line == 21)
                    459:                goto banner;
                    460:        goto loop;
                    461:        /*NOTREACHED*/
                    462: }
                    463:
                    464: /*
                    465:  * Called if an interval expires before sidewaysintpr has completed a loop.
                    466:  * Sets a flag to not wait for the alarm.
                    467:  */
1.46      deraadt   468: /* ARGSUSED */
1.1       deraadt   469: static void
1.33      deraadt   470: catchalarm(int signo)
1.1       deraadt   471: {
1.56      claudio   472:        signalled = 1;
                    473: }
                    474:
                    475: static void
                    476: get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
                    477: {
                    478:        int i;
                    479:
                    480:        for (i = 0; i < RTAX_MAX; i++) {
                    481:                if (addrs & (1 << i)) {
                    482:                        rti_info[i] = sa;
                    483:                        sa = (struct sockaddr *)((char *)(sa) +
                    484:                            roundup(sa->sa_len, sizeof(long)));
                    485:                } else
                    486:                        rti_info[i] = NULL;
                    487:        }
                    488: }
                    489:
                    490: static void
                    491: fetchifs(void)
                    492: {
                    493:        struct if_msghdr ifm;
                    494:        int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
                    495:        struct rt_msghdr *rtm;
                    496:        struct if_data *ifd;
                    497:        struct sockaddr *sa, *rti_info[RTAX_MAX];
                    498:        struct sockaddr_dl *sdl;
                    499:        char *buf, *next, *lim;
                    500:        char name[IFNAMSIZ];
                    501:        size_t len;
                    502:
                    503:        if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
                    504:                err(1, "sysctl");
                    505:        if ((buf = malloc(len)) == NULL)
                    506:                err(1, NULL);
                    507:        if (sysctl(mib, 6, buf, &len, NULL, 0) == -1)
                    508:                err(1, "sysctl");
                    509:
                    510:        lim = buf + len;
                    511:        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                    512:                rtm = (struct rt_msghdr *)next;
                    513:                if (rtm->rtm_version != RTM_VERSION)
                    514:                        continue;
                    515:                switch (rtm->rtm_type) {
                    516:                case RTM_IFINFO:
                    517:                        bcopy(next, &ifm, sizeof ifm);
                    518:                        ifd = &ifm.ifm_data;
                    519:
                    520:                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
                    521:                        get_rtaddrs(ifm.ifm_addrs, sa, rti_info);
                    522:
                    523:                        sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
                    524:                        if (sdl == NULL || sdl->sdl_family != AF_LINK)
                    525:                                continue;
                    526:                        bzero(name, sizeof(name));
                    527:                        if (sdl->sdl_nlen >= IFNAMSIZ)
                    528:                                memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
                    529:                        else if (sdl->sdl_nlen > 0)
                    530:                                memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
                    531:
1.59    ! claudio   532:                        if (interface != NULL && !strcmp(name, interface)) {
1.56      claudio   533:                                strlcpy(ip_cur.ift_name, name,
                    534:                                    sizeof(ip_cur.ift_name));
                    535:                                ip_cur.ift_ip = ifd->ifi_ipackets;
                    536:                                ip_cur.ift_ib = ifd->ifi_ibytes;
                    537:                                ip_cur.ift_ie = ifd->ifi_ierrors;
                    538:                                ip_cur.ift_op = ifd->ifi_opackets;
                    539:                                ip_cur.ift_ob = ifd->ifi_obytes;
                    540:                                ip_cur.ift_oe = ifd->ifi_oerrors;
                    541:                                ip_cur.ift_co = ifd->ifi_collisions;
                    542:                                ip_cur.ift_dr = 0;
                    543:                                    /* XXX ifnet.if_snd.ifq_drops */
                    544:                        }
                    545:
                    546:                        sum_cur.ift_ip += ifd->ifi_ipackets;
                    547:                        sum_cur.ift_ib += ifd->ifi_ibytes;
                    548:                        sum_cur.ift_ie += ifd->ifi_ierrors;
                    549:                        sum_cur.ift_op += ifd->ifi_opackets;
                    550:                        sum_cur.ift_ob += ifd->ifi_obytes;
                    551:                        sum_cur.ift_oe += ifd->ifi_oerrors;
                    552:                        sum_cur.ift_co += ifd->ifi_collisions;
                    553:                        sum_cur.ift_dr += 0; /* XXX ifnet.if_snd.ifq_drops */
                    554:                        break;
                    555:                }
                    556:        }
                    557:        if (interface == NULL) {
                    558:                strlcpy(ip_cur.ift_name, name,
                    559:                    sizeof(ip_cur.ift_name));
                    560:                ip_cur.ift_ip = ifd->ifi_ipackets;
                    561:                ip_cur.ift_ib = ifd->ifi_ibytes;
                    562:                ip_cur.ift_ie = ifd->ifi_ierrors;
                    563:                ip_cur.ift_op = ifd->ifi_opackets;
                    564:                ip_cur.ift_ob = ifd->ifi_obytes;
                    565:                ip_cur.ift_oe = ifd->ifi_oerrors;
                    566:                ip_cur.ift_co = ifd->ifi_collisions;
                    567:                ip_cur.ift_dr = 0;
                    568:                    /* XXX ifnet.if_snd.ifq_drops */
                    569:        }
1.58      dhill     570:        free(buf);
1.47      claudio   571: }