optimization for normal masks requires keeping ptr to associated leaf,
[unix-history] / usr / src / usr.bin / netstat / route.c
index 84b4d32..13aec77 100644 (file)
@@ -1,12 +1,12 @@
 /*
 /*
- * Copyright (c) 1983, 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
  *
  * %sccs.include.redist.c%
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)route.c    5.24 (Berkeley) %G%";
+static char sccsid[] = "@(#)route.c    8.4 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -24,7 +24,7 @@ static char sccsid[] = "@(#)route.c   5.24 (Berkeley) %G%";
 
 #include <netns/ns.h>
 
 
 #include <netns/ns.h>
 
-#include <sys/kinfo.h>
+#include <sys/sysctl.h>
 
 #include <netdb.h>
 #include <stdio.h>
 
 #include <netdb.h>
 #include <stdio.h>
@@ -33,7 +33,7 @@ static char sccsid[] = "@(#)route.c   5.24 (Berkeley) %G%";
 #include <unistd.h>
 #include "netstat.h"
 
 #include <unistd.h>
 #include "netstat.h"
 
-#define kget(p, d) (kread((off_t)(p), (char *)&(d), sizeof (d)))
+#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
 
 /*
  * Definitions for showing gateway flags.
 
 /*
  * Definitions for showing gateway flags.
@@ -85,7 +85,7 @@ static void p_rtentry __P((struct rtentry *));
  */
 void
 routepr(rtree)
  */
 void
 routepr(rtree)
-       off_t rtree;
+       u_long rtree;
 {
        struct radix_node_head *rnh, head;
        int i;
 {
        struct radix_node_head *rnh, head;
        int i;
@@ -178,7 +178,7 @@ kgetsa(dst)
 
        kget(dst, pt_u.u_sa);
        if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
 
        kget(dst, pt_u.u_sa);
        if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
-               kread((off_t)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
+               kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
        return (&pt_u.u_sa);
 }
 
        return (&pt_u.u_sa);
 }
 
@@ -253,16 +253,23 @@ p_rtnode()
 static void
 ntreestuff()
 {
 static void
 ntreestuff()
 {
-       int needed;
+       size_t needed;
+       int mib[6];
        char *buf, *next, *lim;
        register struct rt_msghdr *rtm;
 
        char *buf, *next, *lim;
        register struct rt_msghdr *rtm;
 
-       if ((needed = getkerninfo(KINFO_RT_DUMP, 0, 0, 0)) < 0)
-               { perror("route-getkerninfo-estimate"); exit(1);}
+        mib[0] = CTL_NET;
+        mib[1] = PF_ROUTE;
+        mib[2] = 0;
+        mib[3] = 0;
+        mib[4] = NET_RT_DUMP;
+        mib[5] = 0;
+        if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+               { perror("route-sysctl-estimate"); exit(1);}
        if ((buf = malloc(needed)) == 0)
                { printf("out of space\n"); exit(1);}
        if ((buf = malloc(needed)) == 0)
                { printf("out of space\n"); exit(1);}
-       if (getkerninfo(KINFO_RT_DUMP, buf, &needed, 0) < 0)
-               { perror("getkerninfo of routing table"); exit(1);}
+        if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
+               { perror("sysctl of routing table"); exit(1);}
        lim  = buf + needed;
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                rtm = (struct rt_msghdr *)next;
        lim  = buf + needed;
        for (next = buf; next < lim; next += rtm->rtm_msglen) {
                rtm = (struct rt_msghdr *)next;
@@ -367,14 +374,16 @@ p_sockaddr(sa, flags, width)
 
        default:
            {
 
        default:
            {
-               register u_short *s = ((u_short *)sa->sa_data), *slim;
+               register u_char *s = (u_char *)sa->sa_data, *slim;
 
 
-               slim = (u_short *) sa + ((sa->sa_len + sizeof(u_short) - 1) /
-                   sizeof(u_short));
+               slim =  sa->sa_len + (u_char *) sa;
                cplim = cp + sizeof(workbuf) - 6;
                cp += sprintf(cp, "(%d)", sa->sa_family);
                cplim = cp + sizeof(workbuf) - 6;
                cp += sprintf(cp, "(%d)", sa->sa_family);
-               while (s < slim && cp < cplim)
-                       cp += sprintf(cp, " %x", *s++);
+               while (s < slim && cp < cplim) {
+                       cp += sprintf(cp, " %02x", *s++);
+                       if (s < slim)
+                           cp += sprintf(cp, "%02x", *s++);
+               }
                cp = workbuf;
            }
        }
                cp = workbuf;
            }
        }
@@ -411,13 +420,13 @@ p_rtentry(rt)
        static char name[16];
 
        p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
        static char name[16];
 
        p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
-       p_sockaddr(kgetsa(rt->rt_gateway), 0, WID_GW);
+       p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW);
        p_flags(rt->rt_flags, "%-6.6s ");
        printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
        if (rt->rt_ifp) {
                if (rt->rt_ifp != lastif) {
                        kget(rt->rt_ifp, ifnet);
        p_flags(rt->rt_flags, "%-6.6s ");
        printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
        if (rt->rt_ifp) {
                if (rt->rt_ifp != lastif) {
                        kget(rt->rt_ifp, ifnet);
-                       kread((off_t)ifnet.if_name, name, 16);
+                       kread((u_long)ifnet.if_name, name, 16);
                        lastif = rt->rt_ifp;
                }
                printf(" %.15s%d%s", name, ifnet.if_unit,
                        lastif = rt->rt_ifp;
                }
                printf(" %.15s%d%s", name, ifnet.if_unit,
@@ -529,7 +538,7 @@ netname(in, mask)
  */
 void
 rt_stats(off)
  */
 void
 rt_stats(off)
-       off_t off;
+       u_long off;
 {
        struct rtstat rtstat;
 
 {
        struct rtstat rtstat;