Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / netstat / route.c
index f37ba7e..e9c41e8 100644 (file)
@@ -1,8 +1,28 @@
+/*
+ * 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.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)route.c    4.9 84/10/31";
-#endif
+static char sccsid[] = "@(#)route.c    5.14 (Berkeley) %G%";
+#endif /* not lint */
 
 
-#include <sys/types.h>
+#include <stdio.h>
+#include <strings.h>
+
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/mbuf.h>
 
 #include <sys/socket.h>
 #include <sys/mbuf.h>
 
@@ -10,11 +30,14 @@ static char sccsid[] = "@(#)route.c 4.9 84/10/31";
 #include <net/route.h>
 #include <netinet/in.h>
 
 #include <net/route.h>
 #include <netinet/in.h>
 
+#include <netns/ns.h>
+
 #include <netdb.h>
 
 extern int kmem;
 extern int nflag;
 #include <netdb.h>
 
 extern int kmem;
 extern int nflag;
-extern char *routename();
+extern char *routename(), *netname(), *ns_print(), *plural();
+extern char *malloc();
 
 /*
  * Definitions for showing gateway flags.
 
 /*
  * Definitions for showing gateway flags.
@@ -26,6 +49,8 @@ struct bits {
        { RTF_UP,       'U' },
        { RTF_GATEWAY,  'G' },
        { RTF_HOST,     'H' },
        { RTF_UP,       'U' },
        { RTF_GATEWAY,  'G' },
        { RTF_HOST,     'H' },
+       { RTF_DYNAMIC,  'D' },
+       { RTF_MODIFIED, 'M' },
        { 0 }
 };
 
        { 0 }
 };
 
@@ -58,14 +83,14 @@ routepr(hostaddr, netaddr, hashsizeaddr)
                return;
        }
        klseek(kmem, hashsizeaddr, 0);
                return;
        }
        klseek(kmem, hashsizeaddr, 0);
-       read(kmem, &hashsize, sizeof (hashsize));
+       read(kmem, (char *)&hashsize, sizeof (hashsize));
        routehash = (struct mbuf **)malloc( hashsize*sizeof (struct mbuf *) );
        klseek(kmem, hostaddr, 0);
        routehash = (struct mbuf **)malloc( hashsize*sizeof (struct mbuf *) );
        klseek(kmem, hostaddr, 0);
-       read(kmem, routehash, hashsize*sizeof (struct mbuf *));
+       read(kmem, (char *)routehash, hashsize*sizeof (struct mbuf *));
        printf("Routing tables\n");
        printf("Routing tables\n");
-       printf("%-15.15s %-15.15s %-8.8s %-6.6s %-10.10s %s\n",
+       printf("%-16.16s %-18.18s %-6.6s  %6.6s%8.8s  %s\n",
                "Destination", "Gateway",
                "Destination", "Gateway",
-               "Flags", "Refcnt", "Use", "Interface");
+               "Flags", "Refs", "Use", "Interface");
 again:
        for (i = 0; i < hashsize; i++) {
                if (routehash[i] == 0)
 again:
        for (i = 0; i < hashsize; i++) {
                if (routehash[i] == 0)
@@ -74,89 +99,172 @@ again:
                while (m) {
                        struct sockaddr_in *sin;
 
                while (m) {
                        struct sockaddr_in *sin;
 
-                       klseek(kmem, m, 0);
-                       read(kmem, &mb, sizeof (mb));
+                       klseek(kmem, (off_t)m, 0);
+                       read(kmem, (char *)&mb, sizeof (mb));
                        rt = mtod(&mb, struct rtentry *);
                        rt = mtod(&mb, struct rtentry *);
-                       sin = (struct sockaddr_in *)&rt->rt_dst;
-                       printf("%-15.15s ",
-                           sin->sin_addr.s_addr ?
-                               routename(sin->sin_addr) : "default");
-                       sin = (struct sockaddr_in *)&rt->rt_gateway;
-                       printf("%-15.15s ", routename(sin->sin_addr));
+                       if ((unsigned)rt < (unsigned)&mb ||
+                           (unsigned)rt >= (unsigned)(&mb + 1)) {
+                               printf("???\n");
+                               return;
+                       }
+
+                       switch(rt->rt_dst.sa_family) {
+                       case AF_INET:
+                               sin = (struct sockaddr_in *)&rt->rt_dst;
+                               printf("%-16.16s ",
+                                   (sin->sin_addr.s_addr == 0) ? "default" :
+                                   (rt->rt_flags & RTF_HOST) ?
+                                   routename(sin->sin_addr) :
+                                       netname(sin->sin_addr, 0L));
+                               sin = (struct sockaddr_in *)&rt->rt_gateway;
+                               printf("%-18.18s ", routename(sin->sin_addr));
+                               break;
+                       case AF_NS:
+                               printf("%-16s ",
+                                   ns_print((struct sockaddr_ns *)&rt->rt_dst));
+                               printf("%-18s ",
+                                   ns_print((struct sockaddr_ns *)&rt->rt_gateway));
+                               break;
+                       default:
+                               {
+                               u_short *s = (u_short *)rt->rt_dst.sa_data;
+                               printf("(%d)%x %x %x %x %x %x %x ",
+                                   rt->rt_dst.sa_family,
+                                   s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
+                               s = (u_short *)rt->rt_gateway.sa_data;
+                               printf("(%d)%x %x %x %x %x %x %x ",
+                                   rt->rt_gateway.sa_family,
+                                   s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
+                               }
+                       }
                        for (flags = name, p = bits; p->b_mask; p++)
                                if (p->b_mask & rt->rt_flags)
                                        *flags++ = p->b_val;
                        *flags = '\0';
                        for (flags = name, p = bits; p->b_mask; p++)
                                if (p->b_mask & rt->rt_flags)
                                        *flags++ = p->b_val;
                        *flags = '\0';
-                       printf("%-8.8s %-6d %-10d ", name,
+                       printf("%-6.6s %6d %8d ", name,
                                rt->rt_refcnt, rt->rt_use);
                        if (rt->rt_ifp == 0) {
                                putchar('\n');
                                m = mb.m_next;
                                continue;
                        }
                                rt->rt_refcnt, rt->rt_use);
                        if (rt->rt_ifp == 0) {
                                putchar('\n');
                                m = mb.m_next;
                                continue;
                        }
-                       klseek(kmem, rt->rt_ifp, 0);
-                       read(kmem, &ifnet, sizeof (ifnet));
-                       klseek(kmem, (int)ifnet.if_name, 0);
+                       klseek(kmem, (off_t)rt->rt_ifp, 0);
+                       read(kmem, (char *)&ifnet, sizeof (ifnet));
+                       klseek(kmem, (off_t)ifnet.if_name, 0);
                        read(kmem, name, 16);
                        read(kmem, name, 16);
-                       printf("%s%d\n", name, ifnet.if_unit);
+                       printf(" %.15s%d\n", name, ifnet.if_unit);
                        m = mb.m_next;
                }
        }
        if (doinghost) {
                klseek(kmem, netaddr, 0);
                        m = mb.m_next;
                }
        }
        if (doinghost) {
                klseek(kmem, netaddr, 0);
-               read(kmem, routehash, hashsize*sizeof (struct mbuf *));
+               read(kmem, (char *)routehash, hashsize*sizeof (struct mbuf *));
                doinghost = 0;
                goto again;
        }
                doinghost = 0;
                goto again;
        }
-       free(routehash);
+       free((char *)routehash);
 }
 
 char *
 routename(in)
        struct in_addr in;
 {
 }
 
 char *
 routename(in)
        struct in_addr in;
 {
-       char *cp = 0;
-       static char line[50];
+       register char *cp;
+       static char line[MAXHOSTNAMELEN + 1];
        struct hostent *hp;
        struct hostent *hp;
-       struct netent *np;
-       int lna, net, subnet;
+       static char domain[MAXHOSTNAMELEN + 1];
+       static int first = 1;
+       char *index();
 
 
-       net = inet_netof(in);
-       subnet = inet_subnetof(in);
-       lna = inet_lnaof(in);
+       if (first) {
+               first = 0;
+               if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
+                   (cp = index(domain, '.')))
+                       (void) strcpy(domain, cp + 1);
+               else
+                       domain[0] = 0;
+       }
+       cp = 0;
        if (!nflag) {
        if (!nflag) {
-               if (lna == INADDR_ANY) {
-                       np = getnetbyaddr(net, AF_INET);
-                       if (np)
-                               cp = np->n_name;
-               } else if ((subnet != net) && ((lna & 0xff) == 0) &&
-                   (np = getnetbyaddr(subnet, AF_INET))) {
-                       struct in_addr subnaddr, inet_makeaddr();
-                       subnaddr = inet_makeaddr(subnet, INADDR_ANY);
-                       if (bcmp(&in, &subnaddr, sizeof(in)) == 0)
-                               cp = np->n_name;
-                       else
-                               goto host;
-               } else {
-host:
-                       hp = gethostbyaddr(&in, sizeof (struct in_addr),
-                               AF_INET);
-                       if (hp)
-                               cp = hp->h_name;
+               hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
+                       AF_INET);
+               if (hp) {
+                       if ((cp = index(hp->h_name, '.')) &&
+                           !strcmp(cp + 1, domain))
+                               *cp = 0;
+                       cp = hp->h_name;
                }
        }
        if (cp)
                }
        }
        if (cp)
-               strcpy(line, cp);
+               strncpy(line, cp, sizeof(line) - 1);
        else {
        else {
-               u_char *ucp = (u_char *)&in;
-               if (lna == INADDR_ANY)
-                       sprintf(line, "%u.%u.%u", ucp[0], ucp[1], ucp[2]);
-               else
-                       sprintf(line, "%u.%u.%u.%u", ucp[0], ucp[1],
-                               ucp[2], ucp[3]);
+#define C(x)   ((x) & 0xff)
+               in.s_addr = ntohl(in.s_addr);
+               sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
+                       C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
        }
        return (line);
 }
        }
        return (line);
 }
+
+/*
+ * Return the name of the network whose address is given.
+ * The address is assumed to be that of a net or subnet, not a host.
+ */
+char *
+netname(in, mask)
+       struct in_addr in;
+       u_long mask;
+{
+       char *cp = 0;
+       static char line[MAXHOSTNAMELEN + 1];
+       struct netent *np = 0;
+       u_long net;
+       register i;
+       int subnetshift;
+
+       i = ntohl(in.s_addr);
+       if (!nflag && i) {
+               if (mask == 0) {
+                       if (IN_CLASSA(i)) {
+                               mask = IN_CLASSA_NET;
+                               subnetshift = 8;
+                       } else if (IN_CLASSB(i)) {
+                               mask = IN_CLASSB_NET;
+                               subnetshift = 8;
+                       } else {
+                               mask = IN_CLASSC_NET;
+                               subnetshift = 4;
+                       }
+                       /*
+                        * If there are more bits than the standard mask
+                        * would suggest, subnets must be in use.
+                        * Guess at the subnet mask, assuming reasonable
+                        * width subnet fields.
+                        */
+                       while (i &~ mask)
+                               mask = (long)mask >> subnetshift;
+               }
+               net = i & mask;
+               while ((mask & 1) == 0)
+                       mask >>= 1, net >>= 1;
+               np = getnetbyaddr(net, AF_INET);
+               if (np)
+                       cp = np->n_name;
+       }       
+       if (cp)
+               strncpy(line, cp, sizeof(line) - 1);
+       else if ((i & 0xffffff) == 0)
+               sprintf(line, "%u", C(i >> 24));
+       else if ((i & 0xffff) == 0)
+               sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16));
+       else if ((i & 0xff) == 0)
+               sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
+       else
+               sprintf(line, "%u.%u.%u.%u", C(i >> 24),
+                       C(i >> 16), C(i >> 8), C(i));
+       return (line);
+}
+
 /*
  * Print routing statistics
  */
 /*
  * Print routing statistics
  */
@@ -172,42 +280,88 @@ rt_stats(off)
        klseek(kmem, off, 0);
        read(kmem, (char *)&rtstat, sizeof (rtstat));
        printf("routing:\n");
        klseek(kmem, off, 0);
        read(kmem, (char *)&rtstat, sizeof (rtstat));
        printf("routing:\n");
-       printf("\t%d bad routing redirect%s\n",
+       printf("\t%u bad routing redirect%s\n",
                rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
                rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
-       printf("\t%d dynamically created route%s\n",
+       printf("\t%u dynamically created route%s\n",
                rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
                rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
-       printf("\t%d new gateway%s due to redirects\n",
+       printf("\t%u new gateway%s due to redirects\n",
                rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
                rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
-       printf("\t%d destination%s found unreachable\n",
+       printf("\t%u destination%s found unreachable\n",
                rtstat.rts_unreach, plural(rtstat.rts_unreach));
                rtstat.rts_unreach, plural(rtstat.rts_unreach));
-       printf("\t%d use%s of a wildcard route\n",
+       printf("\t%u use%s of a wildcard route\n",
                rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
 }
                rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
 }
+short ns_nullh[] = {0,0,0};
+short ns_bh[] = {-1,-1,-1};
 
 
-/*
- * Return the possible subnetwork number from an internet address.
- * If the address is of the form of a subnet address (most significant
- * bit of the host part is set), believe the subnet exists.
- * Otherwise, return the network number.
- * SHOULD FIND OUT WHETHER THIS IS A LOCAL NETWORK BEFORE LOOKING
- * INSIDE OF THE HOST PART.  We can only believe this if we have other
- * information (e.g., we can find a name for this number).
- */
-inet_subnetof(in)
-       struct in_addr in;
+char *
+ns_print(sns)
+struct sockaddr_ns *sns;
 {
 {
-       register u_long i = ntohl(in.s_addr);
+       struct ns_addr work;
+       union { union ns_net net_e; u_long long_e; } net;
+       u_short port;
+       static char mybuf[50], cport[10], chost[25];
+       char *host = "";
+       register char *p; register u_char *q;
 
 
-       if (IN_CLASSA(i)) {
-               if (IN_SUBNETA(i))
-                       return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
-       } else if (IN_CLASSB(i)) {
-               if (IN_SUBNETB(i))
-                       return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
-               else
-                       return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
-       } else
-               return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
+       work = sns->sns_addr;
+       port = ntohs(work.x_port);
+       work.x_port = 0;
+       net.net_e  = work.x_net;
+       if (ns_nullhost(work) && net.long_e == 0) {
+               if (port ) {
+                       sprintf(mybuf, "*.%xH", port);
+                       upHex(mybuf);
+               } else
+                       sprintf(mybuf, "*.*");
+               return (mybuf);
+       }
+
+       if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { 
+               host = "any";
+       } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
+               host = "*";
+       } else {
+               q = work.x_host.c_host;
+               sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
+                       q[0], q[1], q[2], q[3], q[4], q[5]);
+               for (p = chost; *p == '0' && p < chost + 12; p++);
+               host = p;
+       }
+       if (port)
+               sprintf(cport, ".%xH", htons(port));
+       else
+               *cport = 0;
+
+       sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
+       upHex(mybuf);
+       return(mybuf);
+}
+
+char *
+ns_phost(sns)
+struct sockaddr_ns *sns;
+{
+       struct sockaddr_ns work;
+       static union ns_net ns_zeronet;
+       char *p;
+       
+       work = *sns;
+       work.sns_addr.x_port = 0;
+       work.sns_addr.x_net = ns_zeronet;
+
+       p = ns_print(&work);
+       if (strncmp("0H.", p, 3) == 0) p += 3;
+       return(p);
+}
+upHex(p0)
+char *p0;
+{
+       register char *p = p0;
+       for (; *p; p++) switch (*p) {
+
+       case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
+               *p += ('A' - 'a');
+       }
 }
 }