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

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