not every value return in an nlist structure was being
[unix-history] / usr / src / usr.bin / netstat / if.c
index 7469343..0828ad3 100644 (file)
@@ -2,59 +2,55 @@
  * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * 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 the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)if.c       5.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)if.c       5.18 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
+#include <sys/protosw.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
-
 #include <net/if.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 <arpa/inet.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <string.h>
 #include <signal.h>
 #include <signal.h>
+#include <unistd.h>
+#include "netstat.h"
 
 #define        YES     1
 #define        NO      0
 
 
 #define        YES     1
 #define        NO      0
 
-extern int kmem;
-extern int tflag;
-extern int dflag;
-extern int nflag;
-extern char *interface;
-extern int unit;
-extern char *routename(), *netname(), *ns_phost();
-extern char *index();
+static void sidewaysintpr __P((unsigned, u_long));
+static void catchalarm __P(());
 
 /*
  * Print a description of the network interfaces.
  */
 
 /*
  * Print a description of the network interfaces.
  */
+void
 intpr(interval, ifnetaddr)
        int interval;
 intpr(interval, ifnetaddr)
        int interval;
-       off_t ifnetaddr;
+       u_long ifnetaddr;
 {
        struct ifnet ifnet;
        union {
                struct ifaddr ifa;
                struct in_ifaddr in;
 {
        struct ifnet ifnet;
        union {
                struct ifaddr ifa;
                struct in_ifaddr in;
+               struct ns_ifaddr ns;
+               struct iso_ifaddr iso;
        } ifaddr;
        } ifaddr;
-       off_t ifaddraddr;
+       u_long ifaddraddr;
+       struct sockaddr *sa;
        char name[16];
 
        if (ifnetaddr == 0) {
        char name[16];
 
        if (ifnetaddr == 0) {
@@ -65,8 +61,8 @@ intpr(interval, ifnetaddr)
                sidewaysintpr((unsigned)interval, ifnetaddr);
                return;
        }
                sidewaysintpr((unsigned)interval, ifnetaddr);
                return;
        }
-       klseek(kmem, ifnetaddr, 0);
-       read(kmem, (char *)&ifnetaddr, sizeof ifnetaddr);
+       if (kread(ifnetaddr, (char *)&ifnetaddr, sizeof ifnetaddr))
+               return;
        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");
        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");
@@ -80,15 +76,14 @@ intpr(interval, ifnetaddr)
        while (ifnetaddr || ifaddraddr) {
                struct sockaddr_in *sin;
                register char *cp;
        while (ifnetaddr || ifaddraddr) {
                struct sockaddr_in *sin;
                register char *cp;
-               int n;
+               int n, m;
 
                if (ifaddraddr == 0) {
 
                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);
+                       if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet) ||
+                           kread((u_long)ifnet.if_name, name, 16))
+                               return;
                        name[15] = '\0';
                        name[15] = '\0';
-                       ifnetaddr = (off_t) ifnet.if_next;
+                       ifnetaddr = (u_long) ifnet.if_next;
                        if (interface != 0 &&
                            (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
                                continue;
                        if (interface != 0 &&
                            (strcmp(name, interface) != 0 || unit != ifnet.if_unit))
                                continue;
@@ -97,71 +92,83 @@ intpr(interval, ifnetaddr)
                        if ((ifnet.if_flags&IFF_UP) == 0)
                                *cp++ = '*';
                        *cp = '\0';
                        if ((ifnet.if_flags&IFF_UP) == 0)
                                *cp++ = '*';
                        *cp = '\0';
-                       ifaddraddr = (off_t)ifnet.if_addrlist;
+                       ifaddraddr = (u_long)ifnet.if_addrlist;
                }
                printf("%-5.5s %-5d ", name, ifnet.if_mtu);
                if (ifaddraddr == 0) {
                        printf("%-11.11s ", "none");
                        printf("%-15.15s ", "none");
                } else {
                }
                printf("%-5.5s %-5d ", name, ifnet.if_mtu);
                if (ifaddraddr == 0) {
                        printf("%-11.11s ", "none");
                        printf("%-15.15s ", "none");
                } 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) {
+                       if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
+                               ifaddraddr = 0;
+                               continue;
+                       }
+#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:
                                printf("%-11.11s ", "none");
                                printf("%-15.15s ", "none");
                                break;
                        case AF_INET:
                        case AF_UNSPEC:
                                printf("%-11.11s ", "none");
                                printf("%-15.15s ", "none");
                                break;
                        case AF_INET:
-                               sin = (struct sockaddr_in *)&ifaddr.in.ia_addr;
+                               sin = (struct sockaddr_in *)sa;
 #ifdef notdef
 #ifdef notdef
-extern struct in_addr inet_makeaddr();
                                /* can't use inet_makeaddr because kernel
                                 * keeps nets unshifted.
                                 */
                                in = inet_makeaddr(ifaddr.in.ia_subnet,
                                        INADDR_ANY);
                                /* can't use inet_makeaddr because kernel
                                 * keeps nets unshifted.
                                 */
                                in = inet_makeaddr(ifaddr.in.ia_subnet,
                                        INADDR_ANY);
-                               printf("%-11.11s ", netname(in));
+                               printf("%-11.11s ", netname(in.s_addr,
+                                   ifaddr.in.ia_subnetmask));
 #else
                                printf("%-11.11s ",
 #else
                                printf("%-11.11s ",
-                                       netname(htonl(ifaddr.in.ia_subnet),
-                                               ifaddr.in.ia_subnetmask));
+                                   netname(htonl(ifaddr.in.ia_subnet),
+                                   ifaddr.in.ia_subnetmask));
 #endif
 #endif
-                               printf("%-15.15s ", routename(sin->sin_addr));
+                               printf("%-15.15s ",
+                                   routename(sin->sin_addr.s_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;
                                char netnum[8];
                                u_long net;
                                char netnum[8];
-                               char *ns_phost();
 
                                *(union ns_net *) &net = sns->sns_addr.x_net;
 
                                *(union ns_net *) &net = sns->sns_addr.x_net;
-                               sprintf(netnum, "%lxH", ntohl(net));
+               sprintf(netnum, "%lxH", ntohl(net));
                                upHex(netnum);
                                printf("ns:%-8s ", netnum);
                                upHex(netnum);
                                printf("ns:%-8s ", netnum);
-                               printf("%-15s ", ns_phost(sns));
+                               printf("%-15s ",
+                                   ns_phost((struct sockaddr *)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 <= 7)
-                                       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 = (u_long)ifaddr.ifa.ifa_next;
                }
                printf("%8d %5d %8d %5d %5d",
                    ifnet.if_ipackets, ifnet.if_ierrors,
                }
                printf("%8d %5d %8d %5d %5d",
                    ifnet.if_ipackets, ifnet.if_ierrors,
@@ -194,20 +201,20 @@ u_char    signalled;                      /* set if alarm goes off "early" */
  * collected over that interval.  Assumes that interval is non-zero.
  * First line printed at top of screen is always cumulative.
  */
  * collected over that interval.  Assumes that interval is non-zero.
  * First line printed at top of screen is always cumulative.
  */
+static void
 sidewaysintpr(interval, off)
        unsigned interval;
 sidewaysintpr(interval, off)
        unsigned interval;
-       off_t off;
+       u_long off;
 {
        struct ifnet ifnet;
 {
        struct ifnet ifnet;
-       off_t firstifnet;
+       u_long firstifnet;
        register struct iftot *ip, *total;
        register int line;
        struct iftot *lastif, *sum, *interesting;
        int oldmask;
        register struct iftot *ip, *total;
        register int line;
        struct iftot *lastif, *sum, *interesting;
        int oldmask;
-       int catchalarm();
 
 
-       klseek(kmem, off, 0);
-       read(kmem, (char *)&firstifnet, sizeof (off_t));
+       if (kread(off, (char *)&firstifnet, sizeof (u_long)))
+               return;
        lastif = iftot;
        sum = iftot + MAXIF - 1;
        total = sum - 1;
        lastif = iftot;
        sum = iftot + MAXIF - 1;
        total = sum - 1;
@@ -215,11 +222,11 @@ 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);
+               if (kread(off, (char *)&ifnet, sizeof ifnet))
+                       break;
                ip->ift_name[0] = '(';
                ip->ift_name[0] = '(';
-               read(kmem, ip->ift_name + 1, 15);
+               if (kread((u_long)ifnet.if_name, ip->ift_name + 1, 15))
+                       break;
                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;
@@ -229,7 +236,7 @@ sidewaysintpr(interval, off)
                ip++;
                if (ip >= iftot + MAXIF - 2)
                        break;
                ip++;
                if (ip >= iftot + MAXIF - 2)
                        break;
-               off = (off_t) ifnet.if_next;
+               off = (u_long) ifnet.if_next;
        }
        lastif = ip;
 
        }
        lastif = ip;
 
@@ -272,8 +279,10 @@ loop:
        sum->ift_co = 0;
        sum->ift_dr = 0;
        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
        sum->ift_co = 0;
        sum->ift_dr = 0;
        for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
-               klseek(kmem, off, 0);
-               read(kmem, (char *)&ifnet, sizeof ifnet);
+               if (kread(off, (char *)&ifnet, sizeof ifnet)) {
+                       off = 0;
+                       continue;
+               }
                if (ip == interesting) {
                        printf("%8d %5d %8d %5d %5d",
                                ifnet.if_ipackets - ip->ift_ip,
                if (ip == interesting) {
                        printf("%8d %5d %8d %5d %5d",
                                ifnet.if_ipackets - ip->ift_ip,
@@ -297,7 +306,7 @@ loop:
                sum->ift_oe += ip->ift_oe;
                sum->ift_co += ip->ift_co;
                sum->ift_dr += ip->ift_dr;
                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 = (u_long) ifnet.if_next;
        }
        if (lastif - iftot > 0) {
                printf("  %8d %5d %8d %5d %5d",
        }
        if (lastif - iftot > 0) {
                printf("  %8d %5d %8d %5d %5d",
@@ -330,6 +339,7 @@ loop:
  * Called if an interval expires before sidewaysintpr has completed a loop.
  * Sets a flag to not wait for the alarm.
  */
  * Called if an interval expires before sidewaysintpr has completed a loop.
  * Sets a flag to not wait for the alarm.
  */
+static void
 catchalarm()
 {
        signalled = YES;
 catchalarm()
 {
        signalled = YES;