Annotation of 43BSDTahoe/ucb/netstat/if.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983, 1988 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that the above copyright notice and this paragraph are
        !             7:  * duplicated in all such forms and that any documentation,
        !             8:  * advertising materials, and other materials related to such
        !             9:  * distribution and use acknowledge that the software was developed
        !            10:  * by the University of California, Berkeley.  The name of the
        !            11:  * University may not be used to endorse or promote products derived
        !            12:  * from this software without specific prior written permission.
        !            13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            16:  */
        !            17: 
        !            18: #ifndef lint
        !            19: static char sccsid[] = "@(#)if.c       5.8 (Berkeley) 6/29/88";
        !            20: #endif /* not lint */
        !            21: 
        !            22: #include <sys/types.h>
        !            23: #include <sys/socket.h>
        !            24: 
        !            25: #include <net/if.h>
        !            26: #include <netinet/in.h>
        !            27: #include <netinet/in_var.h>
        !            28: #include <netns/ns.h>
        !            29: 
        !            30: #include <stdio.h>
        !            31: #include <signal.h>
        !            32: 
        !            33: #define        YES     1
        !            34: #define        NO      0
        !            35: 
        !            36: extern int kmem;
        !            37: extern int tflag;
        !            38: extern int dflag;
        !            39: extern int nflag;
        !            40: extern char *interface;
        !            41: extern int unit;
        !            42: extern char *routename(), *netname(), *ns_phost();
        !            43: 
        !            44: /*
        !            45:  * Print a description of the network interfaces.
        !            46:  */
        !            47: intpr(interval, ifnetaddr)
        !            48:        int interval;
        !            49:        off_t ifnetaddr;
        !            50: {
        !            51:        struct ifnet ifnet;
        !            52:        union {
        !            53:                struct ifaddr ifa;
        !            54:                struct in_ifaddr in;
        !            55:        } ifaddr;
        !            56:        off_t ifaddraddr;
        !            57:        char name[16];
        !            58: 
        !            59:        if (ifnetaddr == 0) {
        !            60:                printf("ifnet: symbol not defined\n");
        !            61:                return;
        !            62:        }
        !            63:        if (interval) {
        !            64:                sidewaysintpr((unsigned)interval, ifnetaddr);
        !            65:                return;
        !            66:        }
        !            67:        klseek(kmem, ifnetaddr, 0);
        !            68:        read(kmem, (char *)&ifnetaddr, sizeof ifnetaddr);
        !            69:        printf("%-5.5s %-5.5s %-11.11s %-15.15s %8.8s %5.5s %8.8s %5.5s",
        !            70:                "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
        !            71:                "Opkts", "Oerrs");
        !            72:        printf(" %5s", "Coll");
        !            73:        if (tflag)
        !            74:                printf(" %s", "Time");
        !            75:        if (dflag)
        !            76:                printf(" %s", "Drop");
        !            77:        putchar('\n');
        !            78:        ifaddraddr = 0;
        !            79:        while (ifnetaddr || ifaddraddr) {
        !            80:                struct sockaddr_in *sin;
        !            81:                register char *cp;
        !            82:                int n;
        !            83:                char *index();
        !            84:                struct in_addr inet_makeaddr();
        !            85: 
        !            86:                if (ifaddraddr == 0) {
        !            87:                        klseek(kmem, ifnetaddr, 0);
        !            88:                        read(kmem, (char *)&ifnet, sizeof ifnet);
        !            89:                        klseek(kmem, (off_t)ifnet.if_name, 0);
        !            90:                        read(kmem, name, 16);
        !            91:                        name[15] = '\0';
        !            92:                        ifnetaddr = (off_t) ifnet.if_next;
        !            93:                        if (interface != 0 &&
        !            94:                            (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
        !            95:                                continue;
        !            96:                        cp = index(name, '\0');
        !            97:                        *cp++ = ifnet.if_unit + '0';
        !            98:                        if ((ifnet.if_flags&IFF_UP) == 0)
        !            99:                                *cp++ = '*';
        !           100:                        *cp = '\0';
        !           101:                        ifaddraddr = (off_t)ifnet.if_addrlist;
        !           102:                }
        !           103:                printf("%-5.5s %-5d ", name, ifnet.if_mtu);
        !           104:                if (ifaddraddr == 0) {
        !           105:                        printf("%-11.11s ", "none");
        !           106:                        printf("%-15.15s ", "none");
        !           107:                } else {
        !           108:                        klseek(kmem, ifaddraddr, 0);
        !           109:                        read(kmem, (char *)&ifaddr, sizeof ifaddr);
        !           110:                        ifaddraddr = (off_t)ifaddr.ifa.ifa_next;
        !           111:                        switch (ifaddr.ifa.ifa_addr.sa_family) {
        !           112:                        case AF_UNSPEC:
        !           113:                                printf("%-11.11s ", "none");
        !           114:                                printf("%-15.15s ", "none");
        !           115:                                break;
        !           116:                        case AF_INET:
        !           117:                                sin = (struct sockaddr_in *)&ifaddr.in.ia_addr;
        !           118: #ifdef notdef
        !           119:                                /* can't use inet_makeaddr because kernel
        !           120:                                 * keeps nets unshifted.
        !           121:                                 */
        !           122:                                in = inet_makeaddr(ifaddr.in.ia_subnet,
        !           123:                                        INADDR_ANY);
        !           124:                                printf("%-11.11s ", netname(in));
        !           125: #else
        !           126:                                printf("%-11.11s ",
        !           127:                                        netname(htonl(ifaddr.in.ia_subnet),
        !           128:                                                ifaddr.in.ia_subnetmask));
        !           129: #endif
        !           130:                                printf("%-15.15s ", routename(sin->sin_addr));
        !           131:                                break;
        !           132:                        case AF_NS:
        !           133:                                {
        !           134:                                struct sockaddr_ns *sns =
        !           135:                                (struct sockaddr_ns *)&ifaddr.in.ia_addr;
        !           136:                                u_long net;
        !           137:                                char netnum[8];
        !           138:                                char *ns_phost();
        !           139: 
        !           140:                                *(union ns_net *) &net = sns->sns_addr.x_net;
        !           141:                                sprintf(netnum, "%lxH", ntohl(net));
        !           142:                                upHex(netnum);
        !           143:                                printf("ns:%-8s ", netnum);
        !           144:                                printf("%-15s ", ns_phost(sns));
        !           145:                                }
        !           146:                                break;
        !           147:                        default:
        !           148:                                printf("af%2d: ", ifaddr.ifa.ifa_addr.sa_family);
        !           149:                                for (cp = (char *)&ifaddr.ifa.ifa_addr +
        !           150:                                    sizeof(struct sockaddr) - 1;
        !           151:                                    cp >= ifaddr.ifa.ifa_addr.sa_data; --cp)
        !           152:                                        if (*cp != 0)
        !           153:                                                break;
        !           154:                                n = cp - (char *)ifaddr.ifa.ifa_addr.sa_data + 1;
        !           155:                                cp = (char *)ifaddr.ifa.ifa_addr.sa_data;
        !           156:                                if (n <= 7)
        !           157:                                        while (--n)
        !           158:                                                printf("%02d.", *cp++ & 0xff);
        !           159:                                else
        !           160:                                        while (--n)
        !           161:                                                printf("%02d", *cp++ & 0xff);
        !           162:                                printf("%02d ", *cp & 0xff);
        !           163:                                break;
        !           164:                        }
        !           165:                }
        !           166:                printf("%8d %5d %8d %5d %5d",
        !           167:                    ifnet.if_ipackets, ifnet.if_ierrors,
        !           168:                    ifnet.if_opackets, ifnet.if_oerrors,
        !           169:                    ifnet.if_collisions);
        !           170:                if (tflag)
        !           171:                        printf(" %3d", ifnet.if_timer);
        !           172:                if (dflag)
        !           173:                        printf(" %3d", ifnet.if_snd.ifq_drops);
        !           174:                putchar('\n');
        !           175:        }
        !           176: }
        !           177: 
        !           178: #define        MAXIF   10
        !           179: struct iftot {
        !           180:        char    ift_name[16];           /* interface name */
        !           181:        int     ift_ip;                 /* input packets */
        !           182:        int     ift_ie;                 /* input errors */
        !           183:        int     ift_op;                 /* output packets */
        !           184:        int     ift_oe;                 /* output errors */
        !           185:        int     ift_co;                 /* collisions */
        !           186:        int     ift_dr;                 /* drops */
        !           187: } iftot[MAXIF];
        !           188: 
        !           189: u_char signalled;                      /* set if alarm goes off "early" */
        !           190: 
        !           191: /*
        !           192:  * Print a running summary of interface statistics.
        !           193:  * Repeat display every interval seconds, showing statistics
        !           194:  * collected over that interval.  Assumes that interval is non-zero.
        !           195:  * First line printed at top of screen is always cumulative.
        !           196:  */
        !           197: sidewaysintpr(interval, off)
        !           198:        unsigned interval;
        !           199:        off_t off;
        !           200: {
        !           201:        struct ifnet ifnet;
        !           202:        off_t firstifnet;
        !           203:        register struct iftot *ip, *total;
        !           204:        register int line;
        !           205:        struct iftot *lastif, *sum, *interesting;
        !           206:        int oldmask;
        !           207:        int catchalarm();
        !           208: 
        !           209:        klseek(kmem, off, 0);
        !           210:        read(kmem, (char *)&firstifnet, sizeof (off_t));
        !           211:        lastif = iftot;
        !           212:        sum = iftot + MAXIF - 1;
        !           213:        total = sum - 1;
        !           214:        interesting = iftot;
        !           215:        for (off = firstifnet, ip = iftot; off;) {
        !           216:                char *cp;
        !           217: 
        !           218:                klseek(kmem, off, 0);
        !           219:                read(kmem, (char *)&ifnet, sizeof ifnet);
        !           220:                klseek(kmem, (off_t)ifnet.if_name, 0);
        !           221:                ip->ift_name[0] = '(';
        !           222:                read(kmem, ip->ift_name + 1, 15);
        !           223:                if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
        !           224:                    unit == ifnet.if_unit)
        !           225:                        interesting = ip;
        !           226:                ip->ift_name[15] = '\0';
        !           227:                cp = index(ip->ift_name, '\0');
        !           228:                sprintf(cp, "%d)", ifnet.if_unit);
        !           229:                ip++;
        !           230:                if (ip >= iftot + MAXIF - 2)
        !           231:                        break;
        !           232:                off = (off_t) ifnet.if_next;
        !           233:        }
        !           234:        lastif = ip;
        !           235: 
        !           236:        (void)signal(SIGALRM, catchalarm);
        !           237:        signalled = NO;
        !           238:        (void)alarm(interval);
        !           239: banner:
        !           240:        printf("   input    %-6.6s    output       ", interesting->ift_name);
        !           241:        if (lastif - iftot > 0) {
        !           242:                if (dflag)
        !           243:                        printf("      ");
        !           244:                printf("     input   (Total)    output");
        !           245:        }
        !           246:        for (ip = iftot; ip < iftot + MAXIF; ip++) {
        !           247:                ip->ift_ip = 0;
        !           248:                ip->ift_ie = 0;
        !           249:                ip->ift_op = 0;
        !           250:                ip->ift_oe = 0;
        !           251:                ip->ift_co = 0;
        !           252:                ip->ift_dr = 0;
        !           253:        }
        !           254:        putchar('\n');
        !           255:        printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
        !           256:                "packets", "errs", "packets", "errs", "colls");
        !           257:        if (dflag)
        !           258:                printf("%5.5s ", "drops");
        !           259:        if (lastif - iftot > 0)
        !           260:                printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
        !           261:                        "packets", "errs", "packets", "errs", "colls");
        !           262:        if (dflag)
        !           263:                printf(" %5.5s", "drops");
        !           264:        putchar('\n');
        !           265:        fflush(stdout);
        !           266:        line = 0;
        !           267: loop:
        !           268:        sum->ift_ip = 0;
        !           269:        sum->ift_ie = 0;
        !           270:        sum->ift_op = 0;
        !           271:        sum->ift_oe = 0;
        !           272:        sum->ift_co = 0;
        !           273:        sum->ift_dr = 0;
        !           274:        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
        !           275:                klseek(kmem, off, 0);
        !           276:                read(kmem, (char *)&ifnet, sizeof ifnet);
        !           277:                if (ip == interesting) {
        !           278:                        printf("%8d %5d %8d %5d %5d",
        !           279:                                ifnet.if_ipackets - ip->ift_ip,
        !           280:                                ifnet.if_ierrors - ip->ift_ie,
        !           281:                                ifnet.if_opackets - ip->ift_op,
        !           282:                                ifnet.if_oerrors - ip->ift_oe,
        !           283:                                ifnet.if_collisions - ip->ift_co);
        !           284:                        if (dflag)
        !           285:                                printf(" %5d",
        !           286:                                    ifnet.if_snd.ifq_drops - ip->ift_dr);
        !           287:                }
        !           288:                ip->ift_ip = ifnet.if_ipackets;
        !           289:                ip->ift_ie = ifnet.if_ierrors;
        !           290:                ip->ift_op = ifnet.if_opackets;
        !           291:                ip->ift_oe = ifnet.if_oerrors;
        !           292:                ip->ift_co = ifnet.if_collisions;
        !           293:                ip->ift_dr = ifnet.if_snd.ifq_drops;
        !           294:                sum->ift_ip += ip->ift_ip;
        !           295:                sum->ift_ie += ip->ift_ie;
        !           296:                sum->ift_op += ip->ift_op;
        !           297:                sum->ift_oe += ip->ift_oe;
        !           298:                sum->ift_co += ip->ift_co;
        !           299:                sum->ift_dr += ip->ift_dr;
        !           300:                off = (off_t) ifnet.if_next;
        !           301:        }
        !           302:        if (lastif - iftot > 0) {
        !           303:                printf("  %8d %5d %8d %5d %5d",
        !           304:                        sum->ift_ip - total->ift_ip,
        !           305:                        sum->ift_ie - total->ift_ie,
        !           306:                        sum->ift_op - total->ift_op,
        !           307:                        sum->ift_oe - total->ift_oe,
        !           308:                        sum->ift_co - total->ift_co);
        !           309:                if (dflag)
        !           310:                        printf(" %5d", sum->ift_dr - total->ift_dr);
        !           311:        }
        !           312:        *total = *sum;
        !           313:        putchar('\n');
        !           314:        fflush(stdout);
        !           315:        line++;
        !           316:        oldmask = sigblock(sigmask(SIGALRM));
        !           317:        if (! signalled) {
        !           318:                sigpause(0);
        !           319:        }
        !           320:        sigsetmask(oldmask);
        !           321:        signalled = NO;
        !           322:        (void)alarm(interval);
        !           323:        if (line == 21)
        !           324:                goto banner;
        !           325:        goto loop;
        !           326:        /*NOTREACHED*/
        !           327: }
        !           328: 
        !           329: /*
        !           330:  * Called if an interval expires before sidewaysintpr has completed a loop.
        !           331:  * Sets a flag to not wait for the alarm.
        !           332:  */
        !           333: catchalarm()
        !           334: {
        !           335:        signalled = YES;
        !           336: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.