BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / netstat / if.c
index e03a86d..a3dcef9 100644 (file)
@@ -1,20 +1,37 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)if.c       5.4 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)if.c       5.14 (Berkeley) 6/18/90";
+#endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <net/if.h>
 
 #include <sys/types.h>
 #include <sys/socket.h>
 
 #include <net/if.h>
+#include <net/if_dl.h>
 #include <netinet/in.h>
 #include <netinet/in_var.h>
 #include <netns/ns.h>
 #include <netinet/in.h>
 #include <netinet/in_var.h>
 #include <netns/ns.h>
+#include <netns/ns_if.h>
+#include <netiso/iso.h>
+#include <netiso/iso_var.h>
 
 #include <stdio.h>
 #include <signal.h>
 
 #include <stdio.h>
 #include <signal.h>
@@ -22,8 +39,8 @@ static char sccsid[] = "@(#)if.c      5.4 (Berkeley) %G%";
 #define        YES     1
 #define        NO      0
 
 #define        YES     1
 #define        NO      0
 
-extern int kmem;
 extern int tflag;
 extern int tflag;
+extern int dflag;
 extern int nflag;
 extern char *interface;
 extern int unit;
 extern int nflag;
 extern char *interface;
 extern int unit;
@@ -40,8 +57,11 @@ intpr(interval, ifnetaddr)
        union {
                struct ifaddr ifa;
                struct in_ifaddr in;
        union {
                struct ifaddr ifa;
                struct in_ifaddr in;
+               struct ns_ifaddr ns;
+               struct iso_ifaddr iso;
        } ifaddr;
        off_t ifaddraddr;
        } ifaddr;
        off_t ifaddraddr;
+       struct sockaddr *sa;
        char name[16];
 
        if (ifnetaddr == 0) {
        char name[16];
 
        if (ifnetaddr == 0) {
@@ -52,28 +72,27 @@ intpr(interval, ifnetaddr)
                sidewaysintpr((unsigned)interval, ifnetaddr);
                return;
        }
                sidewaysintpr((unsigned)interval, ifnetaddr);
                return;
        }
-       klseek(kmem, ifnetaddr, 0);
-       read(kmem, (char *)&ifnetaddr, sizeof ifnetaddr);
-       printf("%-5.5s %-5.5s %-10.10s  %-12.12s %-7.7s %-5.5s %-7.7s %-5.5s",
+       kvm_read(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr);
+       printf("%-5.5s %-5.5s %-11.11s %-15.15s %8.8s %5.5s %8.8s %5.5s",
                "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
                "Opkts", "Oerrs");
                "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
                "Opkts", "Oerrs");
-       printf(" %-6.6s", "Collis");
+       printf(" %5s", "Coll");
        if (tflag)
        if (tflag)
-               printf(" %-6.6s", "Timer");
+               printf(" %s", "Time");
+       if (dflag)
+               printf(" %s", "Drop");
        putchar('\n');
        ifaddraddr = 0;
        while (ifnetaddr || ifaddraddr) {
                struct sockaddr_in *sin;
                register char *cp;
        putchar('\n');
        ifaddraddr = 0;
        while (ifnetaddr || ifaddraddr) {
                struct sockaddr_in *sin;
                register char *cp;
-               int n;
+               int n, m;
                char *index();
                struct in_addr inet_makeaddr();
 
                if (ifaddraddr == 0) {
                char *index();
                struct in_addr inet_makeaddr();
 
                if (ifaddraddr == 0) {
-                       klseek(kmem, ifnetaddr, 0);
-                       read(kmem, (char *)&ifnet, sizeof ifnet);
-                       klseek(kmem, (off_t)ifnet.if_name, 0);
-                       read(kmem, name, 16);
+                       kvm_read(ifnetaddr, (char *)&ifnet, sizeof ifnet);
+                       kvm_read((off_t)ifnet.if_name, name, 16);
                        name[15] = '\0';
                        ifnetaddr = (off_t) ifnet.if_next;
                        if (interface != 0 &&
                        name[15] = '\0';
                        ifnetaddr = (off_t) ifnet.if_next;
                        if (interface != 0 &&
@@ -88,73 +107,83 @@ intpr(interval, ifnetaddr)
                }
                printf("%-5.5s %-5d ", name, ifnet.if_mtu);
                if (ifaddraddr == 0) {
                }
                printf("%-5.5s %-5d ", name, ifnet.if_mtu);
                if (ifaddraddr == 0) {
-                       printf("%-10.10s  ", "none");
-                       printf("%-12.12s ", "none");
+                       printf("%-11.11s ", "none");
+                       printf("%-15.15s ", "none");
                } else {
                } else {
-                       klseek(kmem, ifaddraddr, 0);
-                       read(kmem, (char *)&ifaddr, sizeof ifaddr);
-                       ifaddraddr = (off_t)ifaddr.ifa.ifa_next;
-                       switch (ifaddr.ifa.ifa_addr.sa_family) {
+                       kvm_read(ifaddraddr, (char *)&ifaddr, sizeof ifaddr);
+#define CP(x) ((char *)(x))
+                       cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
+                               CP(&ifaddr); sa = (struct sockaddr *)cp;
+                       switch (sa->sa_family) {
                        case AF_UNSPEC:
                        case AF_UNSPEC:
-                               printf("%-10.10s  ", "none");
-                               printf("%-12.12s ", "none");
+                               printf("%-11.11s ", "none");
+                               printf("%-15.15s ", "none");
                                break;
                        case AF_INET:
                                break;
                        case AF_INET:
-                               sin = (struct sockaddr_in *)&ifaddr.in.ia_addr;
+                               sin = (struct sockaddr_in *)sa;
 #ifdef notdef
                                /* can't use inet_makeaddr because kernel
                                 * keeps nets unshifted.
                                 */
                                in = inet_makeaddr(ifaddr.in.ia_subnet,
                                        INADDR_ANY);
 #ifdef notdef
                                /* can't use inet_makeaddr because kernel
                                 * keeps nets unshifted.
                                 */
                                in = inet_makeaddr(ifaddr.in.ia_subnet,
                                        INADDR_ANY);
-                               printf("%-10.10s  ", netname(in));
+                               printf("%-11.11s ", netname(in));
 #else
 #else
-                               printf("%-10.10s  ",
+                               printf("%-11.11s ",
                                        netname(htonl(ifaddr.in.ia_subnet),
                                                ifaddr.in.ia_subnetmask));
 #endif
                                        netname(htonl(ifaddr.in.ia_subnet),
                                                ifaddr.in.ia_subnetmask));
 #endif
-                               printf("%-12.12s ", routename(sin->sin_addr));
+                               printf("%-15.15s ", routename(sin->sin_addr));
                                break;
                        case AF_NS:
                                {
                                struct sockaddr_ns *sns =
                                break;
                        case AF_NS:
                                {
                                struct sockaddr_ns *sns =
-                               (struct sockaddr_ns *)&ifaddr.in.ia_addr;
+                                       (struct sockaddr_ns *)sa;
                                u_long net;
                                u_long net;
-                               char host[8];
+                               char netnum[8];
                                char *ns_phost();
 
                                *(union ns_net *) &net = sns->sns_addr.x_net;
                                char *ns_phost();
 
                                *(union ns_net *) &net = sns->sns_addr.x_net;
-                               sprintf(host, "%lxH", ntohl(net));
-                               upHex(host);
-                               printf("ns:%-8s ", host);
-                               printf("%-12s ", ns_phost(sns));
+               sprintf(netnum, "%lxH", ntohl(net));
+                               upHex(netnum);
+                               printf("ns:%-8s ", netnum);
+                               printf("%-15s ", ns_phost(sns));
                                }
                                break;
                                }
                                break;
+                       case AF_LINK:
+                               {
+                               struct sockaddr_dl *sdl =
+                                       (struct sockaddr_dl *)sa;
+                                   cp = (char *)LLADDR(sdl);
+                                   n = sdl->sdl_alen;
+                               }
+                               m = printf("<Link>");
+                               goto hexprint;
                        default:
                        default:
-                               printf("af%2d: ", ifaddr.ifa.ifa_addr.sa_family);
-                               for (cp = (char *)&ifaddr.ifa.ifa_addr +
-                                   sizeof(struct sockaddr) - 1;
-                                   cp >= ifaddr.ifa.ifa_addr.sa_data; --cp)
-                                       if (*cp != 0)
-                                               break;
-                               n = cp - (char *)ifaddr.ifa.ifa_addr.sa_data + 1;
-                               cp = (char *)ifaddr.ifa.ifa_addr.sa_data;
-                               if (n <= 6)
-                                       while (--n)
-                                               printf("%02d.", *cp++ & 0xff);
-                               else
-                                       while (--n)
-                                               printf("%02d", *cp++ & 0xff);
-                               printf("%02d ", *cp & 0xff);
+                               m = printf("(%d)", sa->sa_family);
+                               for (cp = sa->sa_len + (char *)sa;
+                                       --cp > sa->sa_data && (*cp == 0);) {}
+                               n = cp - sa->sa_data + 1;
+                               cp = sa->sa_data;
+                       hexprint:
+                               while (--n >= 0)
+                                       m += printf("%x%c", *cp++ & 0xff,
+                                                   n > 0 ? '.' : ' ');
+                               m = 28 - m;
+                               while (m-- > 0)
+                                       putchar(' ');
                                break;
                        }
                                break;
                        }
+                       ifaddraddr = (off_t)ifaddr.ifa.ifa_next;
                }
                }
-               printf("%-7d %-5d %-7d %-5d %-6d",
+               printf("%8d %5d %8d %5d %5d",
                    ifnet.if_ipackets, ifnet.if_ierrors,
                    ifnet.if_opackets, ifnet.if_oerrors,
                    ifnet.if_collisions);
                if (tflag)
                    ifnet.if_ipackets, ifnet.if_ierrors,
                    ifnet.if_opackets, ifnet.if_oerrors,
                    ifnet.if_collisions);
                if (tflag)
-                       printf(" %-6d", ifnet.if_timer);
+                       printf(" %3d", ifnet.if_timer);
+               if (dflag)
+                       printf(" %3d", ifnet.if_snd.ifq_drops);
                putchar('\n');
        }
 }
                putchar('\n');
        }
 }
@@ -167,6 +196,7 @@ struct      iftot {
        int     ift_op;                 /* output packets */
        int     ift_oe;                 /* output errors */
        int     ift_co;                 /* collisions */
        int     ift_op;                 /* output packets */
        int     ift_oe;                 /* output errors */
        int     ift_co;                 /* collisions */
+       int     ift_dr;                 /* drops */
 } iftot[MAXIF];
 
 u_char signalled;                      /* set if alarm goes off "early" */
 } iftot[MAXIF];
 
 u_char signalled;                      /* set if alarm goes off "early" */
@@ -189,8 +219,7 @@ sidewaysintpr(interval, off)
        int oldmask;
        int catchalarm();
 
        int oldmask;
        int catchalarm();
 
-       klseek(kmem, off, 0);
-       read(kmem, (char *)&firstifnet, sizeof (off_t));
+       kvm_read(off, (char *)&firstifnet, sizeof (off_t));
        lastif = iftot;
        sum = iftot + MAXIF - 1;
        total = sum - 1;
        lastif = iftot;
        sum = iftot + MAXIF - 1;
        total = sum - 1;
@@ -198,11 +227,9 @@ sidewaysintpr(interval, off)
        for (off = firstifnet, ip = iftot; off;) {
                char *cp;
 
        for (off = firstifnet, ip = iftot; off;) {
                char *cp;
 
-               klseek(kmem, off, 0);
-               read(kmem, (char *)&ifnet, sizeof ifnet);
-               klseek(kmem, (off_t)ifnet.if_name, 0);
+               kvm_read(off, (char *)&ifnet, sizeof ifnet);
                ip->ift_name[0] = '(';
                ip->ift_name[0] = '(';
-               read(kmem, ip->ift_name + 1, 15);
+               kvm_read((off_t)ifnet.if_name, ip->ift_name + 1, 15);
                if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
                    unit == ifnet.if_unit)
                        interesting = ip;
                if (interface && strcmp(ip->ift_name + 1, interface) == 0 &&
                    unit == ifnet.if_unit)
                        interesting = ip;
@@ -220,22 +247,30 @@ sidewaysintpr(interval, off)
        signalled = NO;
        (void)alarm(interval);
 banner:
        signalled = NO;
        (void)alarm(interval);
 banner:
-       printf("    input   %-6.6s    output       ", interesting->ift_name);
-       if (lastif - iftot > 0)
-               printf("   input  (Total)    output       ");
+       printf("   input    %-6.6s    output       ", interesting->ift_name);
+       if (lastif - iftot > 0) {
+               if (dflag)
+                       printf("      ");
+               printf("     input   (Total)    output");
+       }
        for (ip = iftot; ip < iftot + MAXIF; ip++) {
                ip->ift_ip = 0;
                ip->ift_ie = 0;
                ip->ift_op = 0;
                ip->ift_oe = 0;
                ip->ift_co = 0;
        for (ip = iftot; ip < iftot + MAXIF; ip++) {
                ip->ift_ip = 0;
                ip->ift_ie = 0;
                ip->ift_op = 0;
                ip->ift_oe = 0;
                ip->ift_co = 0;
+               ip->ift_dr = 0;
        }
        putchar('\n');
        }
        putchar('\n');
-       printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
+       printf("%8.8s %5.5s %8.8s %5.5s %5.5s ",
                "packets", "errs", "packets", "errs", "colls");
                "packets", "errs", "packets", "errs", "colls");
+       if (dflag)
+               printf("%5.5s ", "drops");
        if (lastif - iftot > 0)
        if (lastif - iftot > 0)
-               printf("%-7.7s %-5.5s %-7.7s %-5.5s %-5.5s ",
+               printf(" %8.8s %5.5s %8.8s %5.5s %5.5s",
                        "packets", "errs", "packets", "errs", "colls");
                        "packets", "errs", "packets", "errs", "colls");
+       if (dflag)
+               printf(" %5.5s", "drops");
        putchar('\n');
        fflush(stdout);
        line = 0;
        putchar('\n');
        fflush(stdout);
        line = 0;
@@ -245,36 +280,46 @@ loop:
        sum->ift_op = 0;
        sum->ift_oe = 0;
        sum->ift_co = 0;
        sum->ift_op = 0;
        sum->ift_oe = 0;
        sum->ift_co = 0;
+       sum->ift_dr = 0;
        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
-               klseek(kmem, off, 0);
-               read(kmem, (char *)&ifnet, sizeof ifnet);
-               if (ip == interesting)
-                       printf("%-7d %-5d %-7d %-5d %-5d ",
+               kvm_read(off, (char *)&ifnet, sizeof ifnet);
+               if (ip == interesting) {
+                       printf("%8d %5d %8d %5d %5d",
                                ifnet.if_ipackets - ip->ift_ip,
                                ifnet.if_ierrors - ip->ift_ie,
                                ifnet.if_opackets - ip->ift_op,
                                ifnet.if_oerrors - ip->ift_oe,
                                ifnet.if_collisions - ip->ift_co);
                                ifnet.if_ipackets - ip->ift_ip,
                                ifnet.if_ierrors - ip->ift_ie,
                                ifnet.if_opackets - ip->ift_op,
                                ifnet.if_oerrors - ip->ift_oe,
                                ifnet.if_collisions - ip->ift_co);
+                       if (dflag)
+                               printf(" %5d",
+                                   ifnet.if_snd.ifq_drops - ip->ift_dr);
+               }
                ip->ift_ip = ifnet.if_ipackets;
                ip->ift_ie = ifnet.if_ierrors;
                ip->ift_op = ifnet.if_opackets;
                ip->ift_oe = ifnet.if_oerrors;
                ip->ift_co = ifnet.if_collisions;
                ip->ift_ip = ifnet.if_ipackets;
                ip->ift_ie = ifnet.if_ierrors;
                ip->ift_op = ifnet.if_opackets;
                ip->ift_oe = ifnet.if_oerrors;
                ip->ift_co = ifnet.if_collisions;
+               ip->ift_dr = ifnet.if_snd.ifq_drops;
                sum->ift_ip += ip->ift_ip;
                sum->ift_ie += ip->ift_ie;
                sum->ift_op += ip->ift_op;
                sum->ift_oe += ip->ift_oe;
                sum->ift_co += ip->ift_co;
                sum->ift_ip += ip->ift_ip;
                sum->ift_ie += ip->ift_ie;
                sum->ift_op += ip->ift_op;
                sum->ift_oe += ip->ift_oe;
                sum->ift_co += ip->ift_co;
+               sum->ift_dr += ip->ift_dr;
                off = (off_t) ifnet.if_next;
        }
                off = (off_t) ifnet.if_next;
        }
-       if (lastif - iftot > 0)
-               printf("%-7d %-5d %-7d %-5d %-5d\n",
+       if (lastif - iftot > 0) {
+               printf("  %8d %5d %8d %5d %5d",
                        sum->ift_ip - total->ift_ip,
                        sum->ift_ie - total->ift_ie,
                        sum->ift_op - total->ift_op,
                        sum->ift_oe - total->ift_oe,
                        sum->ift_co - total->ift_co);
                        sum->ift_ip - total->ift_ip,
                        sum->ift_ie - total->ift_ie,
                        sum->ift_op - total->ift_op,
                        sum->ift_oe - total->ift_oe,
                        sum->ift_co - total->ift_co);
+               if (dflag)
+                       printf(" %5d", sum->ift_dr - total->ift_dr);
+       }
        *total = *sum;
        *total = *sum;
+       putchar('\n');
        fflush(stdout);
        line++;
        oldmask = sigblock(sigmask(SIGALRM));
        fflush(stdout);
        line++;
        oldmask = sigblock(sigmask(SIGALRM));