]> git.saurik.com Git - apple/network_cmds.git/blobdiff - netstat.tproj/route.c
network_cmds-596.100.2.tar.gz
[apple/network_cmds.git] / netstat.tproj / route.c
index e94d0d62bd4683d0d043cffbfd687ccfc5979682..6f727ad2fd4d49ca08f80b19e31ff348a070dcae 100644 (file)
@@ -1,25 +1,29 @@
 /*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ *
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ *
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
- * Reserved.  This file contains Original Code and/or Modifications of
- * Original Code as defined in and that are subject to the Apple Public
- * Source License Version 1.0 (the 'License').  You may not use this file
- * except in compliance with the License.  Please obtain a copy of the
- * License at http://www.apple.com/publicsource and read it before using
- * this file.
- * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License."
- * 
- * @APPLE_LICENSE_HEADER_END@
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * Copyright (c) 1983, 1988, 1993
  * SUCH DAMAGE.
  */
 
-#ifndef lint
-#if 0
-static char sccsid[] = "From: @(#)route.c      8.6 (Berkeley) 4/28/95";
-#endif
-static const char rcsid[] =
-       "$Id: route.c,v 1.7 2004/10/14 22:24:09 lindak Exp $";
-#endif /* not lint */
-
+#include <stdint.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/time.h>
@@ -74,22 +71,10 @@ static const char rcsid[] =
 #include <net/radix.h>
 
 #include <netinet/in.h>
-#ifndef __APPLE__
-#include <netipx/ipx.h>
-#include <netatalk/at.h>
-#include <netgraph/ng_socket.h>
-#endif
-
-#ifdef NS
-#include <netns/ns.h>
-#endif
 
 #include <sys/sysctl.h>
 
 #include <arpa/inet.h>
-#ifndef __APPLE__
-#include <libutil.h>
-#endif
 #include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -99,20 +84,16 @@ static const char rcsid[] =
 #include <time.h>
 #include "netstat.h"
 
-#if 0
-#define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
-#endif
-
 /* alignment constraint for routing socket */
 #define ROUNDUP(a) \
-       ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
+       ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
 
 /*
  * Definitions for showing gateway flags.
  */
 struct bits {
-       u_long  b_mask;
+       uint32_t        b_mask;
        char    b_val;
 } bits[] = {
        { RTF_UP,       'U' },
@@ -121,6 +102,7 @@ struct bits {
        { RTF_REJECT,   'R' },
        { RTF_DYNAMIC,  'D' },
        { RTF_MODIFIED, 'M' },
+       { RTF_MULTICAST,'m' },
        { RTF_DONE,     'd' }, /* Completed -- for routing messages only */
        { RTF_CLONING,  'C' },
        { RTF_XRESOLVE, 'X' },
@@ -133,79 +115,24 @@ struct bits {
        { RTF_PROTO3,   '3' },
        { RTF_BLACKHOLE,'B' },
        { RTF_BROADCAST,'b' },
+       { RTF_IFSCOPE,  'I' },
+       { RTF_IFREF,    'i' },
+       { RTF_PROXY,    'Y' },
+       { RTF_ROUTER,   'r' },
        { 0 }
 };
 
 typedef union {
-       long    dummy;          /* Helps align structure. */
+       uint32_t dummy;         /* Helps align structure. */
        struct  sockaddr u_sa;
        u_short u_data[128];
 } sa_u;
 
-#if 0
-static sa_u pt_u;
-#endif
-
-int    do_rtent = 0;
-struct rtentry rtentry;
-struct radix_node rnode;
-struct radix_mask rmask;
-struct radix_node_head *rt_tables[AF_MAX+1];
-
-#if 0
-static struct sockaddr *kgetsa __P((struct sockaddr *));
-static void p_tree __P((struct radix_node *));
-static void p_rtnode __P((void));
-static void p_rtentry __P((struct rtentry *));
-#endif
-static void ntreestuff __P((void));
 static void np_rtentry __P((struct rt_msghdr2 *));
 static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int));
 static void p_flags __P((int, char *));
-static u_long forgemask __P((u_long));
-static void domask __P((char *, u_long, u_long));
-
-/*
- * Print routing tables.
- */
-void
-routepr(u_long rtree)
-{
-#if 0
-       struct radix_node_head *rnh, head;
-       int i;
-#endif
-
-       printf("Routing tables\n");
-
-       if (dflag == 0) 
-               ntreestuff();
-       else {
-               if (rtree == 0) {
-                       printf("rt_tables: symbol not in namelist\n");
-                       return;
-               }
-#if 0
-               kget(rtree, rt_tables);
-               for (i = 0; i <= AF_MAX; i++) {
-                       if ((rnh = rt_tables[i]) == 0)
-                               continue;
-                       kget(rnh, head);
-                       if (i == AF_UNSPEC) {
-                               if (Aflag && af == 0) {
-                                       printf("Netmasks:\n");
-                                       p_tree(head.rnh_treetop);
-                               }
-                       } else if (af == AF_UNSPEC || af == i) {
-                               pr_family(i);
-                               do_rtent = 1;
-                               pr_rthdr(i);
-                               p_tree(head.rnh_treetop);
-                       }
-               }
-#endif
-       }
-}
+static uint32_t forgemask __P((uint32_t));
+static void domask __P((char *, uint32_t, uint32_t));
 
 /*
  * Print address family header before a section of the routing table.
@@ -227,25 +154,6 @@ pr_family(int af)
        case AF_IPX:
                afname = "IPX";
                break;
-#ifdef NS
-       case AF_NS:
-               afname = "XNS";
-               break;
-#endif
-#ifndef __APPLE__
-       case AF_ISO:
-               afname = "ISO";
-               break;
-       case AF_APPLETALK:
-               afname = "AppleTalk";
-               break;
-       case AF_CCITT:
-               afname = "X.25";
-               break;
-       case AF_NETGRAPH:
-               afname = "Netgraph";
-               break;
-#endif
        default:
                afname = NULL;
                break;
@@ -260,13 +168,16 @@ pr_family(int af)
 #ifndef INET6
 #define        WID_DST(af)     18      /* width of destination column */
 #define        WID_GW(af)      18      /* width of gateway column */
-#define        WID_IF(af)      6       /* width of netif column */
+#define        WID_RT_IFA(af)  18      /* width of source column */
+#define        WID_IF(af)      7       /* width of netif column */
 #else
 #define        WID_DST(af) \
        ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39: 18)) : 18)
 #define        WID_GW(af) \
        ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 31 : 18)) : 18)
-#define        WID_IF(af)      ((af) == AF_INET6 ? 8 : 6)
+#define        WID_RT_IFA(af) \
+       ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 39 : 18)) : 18)
+#define        WID_IF(af)      ((af) == AF_INET6 ? 8 : 7)
 #endif /*INET6*/
 
 /*
@@ -275,123 +186,52 @@ pr_family(int af)
 void
 pr_rthdr(int af)
 {
-
-       if (Aflag)
-               printf("%-8.8s ","Address");
-       if (af == AF_INET || lflag)
-               if (lflag)
-                       printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n",
+       if (lflag) {
+               if (lflag > 2)
+                       printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
+                               "%10s %10s %8s %8s %8s\n",
                                WID_DST(af), WID_DST(af), "Destination",
                                WID_GW(af), WID_GW(af), "Gateway",
+                               WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
                                "Flags", "Refs", "Use", "Mtu",
-                               WID_IF(af), WID_IF(af), "Netif", "Expire");
+                               WID_IF(af), WID_IF(af), "Netif", "Expire",
+                               "rtt(ms)", "rttvar(ms)", "recvpipe", "sendpipe", "ssthresh");
+               else if (lflag > 1)
+                       printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s "
+                               "%10s %10s\n",
+                               WID_DST(af), WID_DST(af), "Destination",
+                               WID_GW(af), WID_GW(af), "Gateway",
+                               WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
+                               "Flags", "Refs", "Use", "Mtu",
+                               WID_IF(af), WID_IF(af), "Netif", "Expire",
+                               "rtt(ms)", "rttvar(ms)");
                else
-                       printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n",
+                       printf("%-*.*s %-*.*s %-*.*s %-10.10s %6.6s %8.8s %6.6s %*.*s %6s\n",
                                WID_DST(af), WID_DST(af), "Destination",
                                WID_GW(af), WID_GW(af), "Gateway",
-                               "Flags", "Refs", "Use",
+                               WID_RT_IFA(af), WID_RT_IFA(af), "RT_IFA",
+                               "Flags", "Refs", "Use", "Mtu",
                                WID_IF(af), WID_IF(af), "Netif", "Expire");
-       else
-               printf("%-*.*s %-*.*s %-6.6s  %8.8s %6s\n",
+    }  else {
+               printf("%-*.*s %-*.*s %-10.10s %*.*s %6s\n",
                        WID_DST(af), WID_DST(af), "Destination",
                        WID_GW(af), WID_GW(af), "Gateway",
-                       "Flags", "Netif", "Expire");
-}
-
-#if 0
-static struct sockaddr *
-kgetsa(struct sockaddr *dst)
-{
-
-       kget(dst, pt_u.u_sa);
-       if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
-               kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
-       return (&pt_u.u_sa);
-}
-
-static void
-p_tree(struct radix_node *rn)
-{
-
-again:
-       kget(rn, rnode);
-       if (rnode.rn_bit < 0) {
-               if (Aflag)
-                       printf("%-8.8lx ", (u_long)rn);
-               if (rnode.rn_flags & RNF_ROOT) {
-                       if (Aflag)
-                               printf("(root node)%s",
-                                   rnode.rn_dupedkey ? " =>\n" : "\n");
-               } else if (do_rtent) {
-                       kget(rn, rtentry);
-                       p_rtentry(&rtentry);
-                       if (Aflag)
-                               p_rtnode();
-               } else {
-                       p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
-                                  NULL, 0, 44);
-                       putchar('\n');
-               }
-               if ((rn = rnode.rn_dupedkey))
-                       goto again;
-       } else {
-               if (Aflag && do_rtent) {
-                       printf("%-8.8lx ", (u_long)rn);
-                       p_rtnode();
-               }
-               rn = rnode.rn_right;
-               p_tree(rnode.rn_left);
-               p_tree(rn);
+                       "Flags", WID_IF(af), WID_IF(af), "Netif", "Expire");
        }
 }
 
-char   nbuf[20];
-
-static void
-p_rtnode(void)
-{
-       struct radix_mask *rm = rnode.rn_mklist;
-
-       if (rnode.rn_bit < 0) {
-               if (rnode.rn_mask) {
-                       printf("\t  mask ");
-                       p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
-                                  NULL, 0, -1);
-               } else if (rm == 0)
-                       return;
-       } else {
-               sprintf(nbuf, "(%d)", rnode.rn_bit);
-               printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right);
-       }
-       while (rm) {
-               kget(rm, rmask);
-               sprintf(nbuf, " %d refs, ", rmask.rm_refs);
-               printf(" mk = %8.8lx {(%d),%s",
-                       (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " ");
-               if (rmask.rm_flags & RNF_NORMAL) {
-                       struct radix_node rnode_aux;
-                       printf(" <normal>, ");
-                       kget(rmask.rm_leaf, rnode_aux);
-                       p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
-                                   NULL, 0, -1);
-               } else
-                   p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
-                               NULL, 0, -1);
-               putchar('}');
-               if ((rm = rmask.rm_mklist))
-                       printf(" ->");
-       }
-       putchar('\n');
-}
-#endif
-
-static void
-ntreestuff(void)
+/*
+ * Print routing tables.
+ */
+void
+routepr(void)
 {
        size_t needed;
        int mib[6];
        char *buf, *next, *lim;
-       register struct rt_msghdr2 *rtm;
+       struct rt_msghdr2 *rtm;
+
+       printf("Routing tables\n");
 
        mib[0] = CTL_NET;
        mib[1] = PF_ROUTE;
@@ -425,11 +265,11 @@ get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
                 if (addrs & (1 << i)) {
                         rti_info[i] = sa;
                                                sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
-                }
-                else
+               } else {
                         rti_info[i] = NULL;
         }
 }
+}
 
 static void
 np_rtentry(struct rt_msghdr2 *rtm)
@@ -445,10 +285,14 @@ np_rtentry(struct rt_msghdr2 *rtm)
        /*
         * Don't print protocol-cloned routes unless -a.
         */
-       if ((rtm->rtm_flags & RTF_WASCLONED) && (rtm->rtm_parentflags & RTF_PRCLONING) && !aflag) {
+       if ((rtm->rtm_flags & RTF_WASCLONED) &&
+           (rtm->rtm_parentflags & RTF_PRCLONING) &&
+           !aflag) {
                        return;
        }
 
+       if (lflag > 1 && zflag != 0 && rtm->rtm_rmx.rmx_rtt == 0 && rtm->rtm_rmx.rmx_rttvar == 0)
+               return;
        fam = sa->sa_family;
        if (af != AF_UNSPEC && af != fam)
                return;
@@ -469,18 +313,22 @@ np_rtentry(struct rt_msghdr2 *rtm)
 
        p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
            WID_GW(addr.u_sa.sa_family));
+
+       if (lflag && (rtm->rtm_addrs & RTA_IFA)) {
+               p_sockaddr(rti_info[RTAX_IFA], NULL, RTF_HOST,
+                   WID_RT_IFA(addr.u_sa.sa_family));
+       }
        
-       p_flags(rtm->rtm_flags, "%-6.6s ");
+       p_flags(rtm->rtm_flags, "%-10.10s ");
 
-       if (addr.u_sa.sa_family == AF_INET || lflag) {
-               printf("%6u %8d ", rtm->rtm_refcnt, rtm->rtm_use);
-               if (lflag) {
-                       if (rtm->rtm_rmx.rmx_mtu != 0)
-                               printf("%6lu ", rtm->rtm_rmx.rmx_mtu);
-                       else
-                               printf("%6s ", "");
-               }
+       if (lflag) {
+               printf("%6u %8u ", rtm->rtm_refcnt, (unsigned int)rtm->rtm_use);
+               if (rtm->rtm_rmx.rmx_mtu != 0)
+                       printf("%6u ", rtm->rtm_rmx.rmx_mtu);
+               else
+                       printf("%6s ", "");
        }
+
        if (rtm->rtm_index != lastindex) {
                if_indextoname(rtm->rtm_index, ifname);
                lastindex = rtm->rtm_index;
@@ -494,6 +342,36 @@ np_rtentry(struct rt_msghdr2 *rtm)
                if ((expire_time =
                        rtm->rtm_rmx.rmx_expire - time((time_t *)0)) > 0)
                        printf(" %6d", (int)expire_time);
+               else
+                       printf(" %6s", "!");
+       } else {
+               printf(" %6s", "");
+       }
+       if (lflag > 1) {
+               if (rtm->rtm_rmx.rmx_rtt != 0)
+                       printf(" %6u.%03u", rtm->rtm_rmx.rmx_rtt / 1000,
+                              rtm->rtm_rmx.rmx_rtt % 1000);
+               else
+                       printf(" %10s", "");
+               if (rtm->rtm_rmx.rmx_rttvar != 0)
+                       printf(" %6u.%03u", rtm->rtm_rmx.rmx_rttvar / 1000,
+                              rtm->rtm_rmx.rmx_rttvar % 1000);
+               else
+                       printf(" %10s", "");
+               if (lflag > 2) {
+                       if (rtm->rtm_rmx.rmx_recvpipe != 0)
+                               printf(" %8u", rtm->rtm_rmx.rmx_recvpipe);
+                       else
+                               printf(" %8s", "");
+                       if (rtm->rtm_rmx.rmx_sendpipe != 0)
+                               printf(" %8u", rtm->rtm_rmx.rmx_sendpipe);
+                       else
+                               printf(" %8s", "");
+                       if (rtm->rtm_rmx.rmx_ssthresh != 0)
+                               printf(" %8u", rtm->rtm_rmx.rmx_ssthresh);
+                       else
+                               printf(" %8s", "");
+               }
        }
        putchar('\n');
 }
@@ -502,32 +380,29 @@ static void
 p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
 {
        char workbuf[128], *cplim;
-       register char *cp = workbuf;
+       char *cp = workbuf;
 
        switch(sa->sa_family) {
-       case AF_INET:
-           {
-               register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
+       case AF_INET: {
+               struct sockaddr_in *sin = (struct sockaddr_in *)sa;
 
                if ((sin->sin_addr.s_addr == INADDR_ANY) &&
                        mask &&
-                       (ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) == 0L ||
-                        mask->sa_len == 0))
+                   (ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) == 0L || mask->sa_len == 0))
                                cp = "default" ;
                else if (flags & RTF_HOST)
                        cp = routename(sin->sin_addr.s_addr);
                else if (mask)
                        cp = netname(sin->sin_addr.s_addr,
-                                    ntohl(((struct sockaddr_in *)mask)
-                                          ->sin_addr.s_addr));
+                           ntohl(((struct sockaddr_in *)mask)->
+                           sin_addr.s_addr));
                else
                        cp = netname(sin->sin_addr.s_addr, 0L);
                break;
            }
 
 #ifdef INET6
-       case AF_INET6:
-           {
+       case AF_INET6: {
                struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
                struct in6_addr *in6 = &sa6->sin6_addr;
 
@@ -536,6 +411,7 @@ p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
                 * sin6_scope_id field of SA should be set in the future.
                 */
                if (IN6_IS_ADDR_LINKLOCAL(in6) ||
+                   IN6_IS_ADDR_MC_NODELOCAL(in6) ||
                    IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
                    /* XXX: override is ok? */
                    sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
@@ -546,61 +422,29 @@ p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
                    cp = routename6(sa6);
                else if (mask)
                    cp = netname6(sa6, mask);
-               else {
+               else
                    cp = netname6(sa6, NULL);
-               }
                break;
            }
 #endif /*INET6*/
-#ifndef __APPLE__
-       case AF_IPX:
-           {
-               struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
-               if (ipx_nullnet(satoipx_addr(work)))
-                       cp = "default";
-               else
-                       cp = ipx_print(sa);
-               break;
-           }
-       case AF_APPLETALK:
-           {
-               if (!(flags & RTF_HOST) && mask)
-                       cp = atalk_print2(sa,mask,9);
-               else
-                       cp = atalk_print(sa,11);
-               break;
-           }
-       case AF_NETGRAPH:
-           {
-               printf("%s", ((struct sockaddr_ng *)sa)->sg_data);
-               break;
-           }
-#endif
-#ifdef NS
-       case AF_NS:
-               cp = ns_print(sa);
-               break;
-#endif
 
-       case AF_LINK:
-           {
-               register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
+       case AF_LINK: {
+               struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
 
                if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
-                   sdl->sdl_slen == 0)
-                       (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
-               else
+                   sdl->sdl_slen == 0) {
+                       (void) snprintf(workbuf, sizeof(workbuf), "link#%d", sdl->sdl_index);
+               } else {
                        switch (sdl->sdl_type) {
 
-                       case IFT_ETHER:
-                           {
-                               register int i;
-                               register u_char *lla = (u_char *)sdl->sdl_data +
+                       case IFT_ETHER: {
+                               int i;
+                               u_char *lla = (u_char *)sdl->sdl_data +
                                    sdl->sdl_nlen;
 
                                cplim = "";
                                for (i = 0; i < sdl->sdl_alen; i++, lla++) {
-                                       cp += sprintf(cp, "%s%x", cplim, *lla);
+                                       cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%s%x", cplim, *lla);
                                        cplim = ":";
                                }
                                cp = workbuf;
@@ -611,27 +455,27 @@ p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width)
                                cp = link_ntoa(sdl);
                                break;
                        }
+               }
                break;
            }
 
-       default:
-           {
-               register u_char *s = (u_char *)sa->sa_data, *slim;
+       default: {
+               u_char *s = (u_char *)sa->sa_data, *slim;
 
                slim =  sa->sa_len + (u_char *) sa;
                cplim = cp + sizeof(workbuf) - 6;
-               cp += sprintf(cp, "(%d)", sa->sa_family);
+               cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "(%d)", sa->sa_family);
                while (s < slim && cp < cplim) {
-                       cp += sprintf(cp, " %02x", *s++);
+                       cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), " %02x", *s++);
                        if (s < slim)
-                           cp += sprintf(cp, "%02x", *s++);
+                           cp += snprintf(cp, sizeof(workbuf) - (cp - workbuf), "%02x", *s++);
                }
                cp = workbuf;
            }
        }
-       if (width < 0 )
+       if (width < 0 ) {
                printf("%s ", cp);
-       else {
+       else {
                if (nflag)
                        printf("%-*s ", width, cp);
                else
@@ -643,7 +487,7 @@ static void
 p_flags(int f, char *format)
 {
        char name[33], *flags;
-       register struct bits *p = bits;
+       struct bits *p = bits;
 
        for (flags = name; p->b_mask; p++)
                if (p->b_mask & f)
@@ -652,74 +496,10 @@ p_flags(int f, char *format)
        printf(format, name);
 }
 
-#if 0
-static void
-p_rtentry(struct rtentry *rt)
-{
-       static struct ifnet ifnet, *lastif;
-       struct rtentry parent;
-       static char name[16];
-       static char prettyname[9];
-       struct sockaddr *sa;
-       sa_u addr, mask;
-
-       /*
-        * Don't print protocol-cloned routes unless -a.
-        */
-       if (rt->rt_flags & RTF_WASCLONED && !aflag) {
-               kget(rt->rt_parent, parent);
-               if (parent.rt_flags & RTF_PRCLONING)
-                       return;
-       }
-
-       bzero(&addr, sizeof(addr));
-       if ((sa = kgetsa(rt_key(rt))))
-               bcopy(sa, &addr, sa->sa_len);
-       bzero(&mask, sizeof(mask));
-       if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt))))
-               bcopy(sa, &mask, sa->sa_len);
-       p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags,
-           WID_DST(addr.u_sa.sa_family));
-       p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST,
-           WID_GW(addr.u_sa.sa_family));
-       p_flags(rt->rt_flags, "%-6.6s ");
-       if (addr.u_sa.sa_family == AF_INET || lflag) {
-               printf("%6u %8ld ", rt->rt_refcnt, rt->rt_use);
-               if (lflag) {
-                       if (rt->rt_rmx.rmx_mtu != 0)
-                               printf("%6lu ", rt->rt_rmx.rmx_mtu);
-                       else
-                               printf("%6s ", "");
-               }
-       }
-       if (rt->rt_ifp) {
-               if (rt->rt_ifp != lastif) {
-                       kget(rt->rt_ifp, ifnet);
-                       kread((u_long)ifnet.if_name, name, 16);
-                       lastif = rt->rt_ifp;
-                       snprintf(prettyname, sizeof prettyname,
-                                "%s%d", name, ifnet.if_unit);
-               }
-               printf("%*.*s", WID_IF(addr.u_sa.sa_family),
-                   WID_IF(addr.u_sa.sa_family), prettyname);
-               if (rt->rt_rmx.rmx_expire) {
-                       time_t expire_time;
-
-                       if ((expire_time =
-                           rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0)
-                               printf(" %6d", (int)expire_time);
-               }
-               if (rt->rt_nodes[0].rn_dupedkey)
-                       printf(" =>");
-       }
-       putchar('\n');
-}
-#endif
-
 char *
-routename(u_long in)
+routename(uint32_t in)
 {
-       register char *cp;
+       char *cp;
        static char line[MAXHOSTNAMELEN];
        struct hostent *hp;
 
@@ -733,21 +513,20 @@ routename(u_long in)
                }
        }
        if (cp) {
-               strncpy(line, cp, sizeof(line) - 1);
-               line[sizeof(line) - 1] = '\0';
+               strlcpy(line, cp, sizeof(line));
        } else {
 #define C(x)   ((x) & 0xff)
                in = ntohl(in);
-               sprintf(line, "%lu.%lu.%lu.%lu",
+               snprintf(line, sizeof(line), "%u.%u.%u.%u",
                    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
        }
        return (line);
 }
 
-static u_long
-forgemask(u_long a)
+static uint32_t
+forgemask(uint32_t a)
 {
-       u_long m;
+       uint32_t m;
 
        if (IN_CLASSA(a))
                m = IN_CLASSA_NET;
@@ -759,9 +538,9 @@ forgemask(u_long a)
 }
 
 static void
-domask(char *dst, u_long addr, u_long mask)
+domask(char *dst, uint32_t addr, uint32_t mask)
 {
-       register int b, i;
+       int b, i;
 
        if (!mask || (forgemask(addr) == mask)) {
                *dst = '\0';
@@ -770,7 +549,7 @@ domask(char *dst, u_long addr, u_long mask)
        i = 0;
        for (b = 0; b < 32; b++)
                if (mask & (1 << b)) {
-                       register int bb;
+                       int bb;
 
                        i = b;
                        for (bb = b+1; bb < 32; bb++)
@@ -781,9 +560,9 @@ domask(char *dst, u_long addr, u_long mask)
                        break;
                }
        if (i == -1)
-               sprintf(dst, "&0x%lx", mask);
+               snprintf(dst, sizeof(dst), "&0x%x", mask);
        else
-               sprintf(dst, "/%d", 32-i);
+               snprintf(dst, sizeof(dst), "/%d", 32-i);
 }
 
 /*
@@ -791,13 +570,13 @@ domask(char *dst, u_long addr, u_long mask)
  * The address is assumed to be that of a net or subnet, not a host.
  */
 char *
-netname(u_long in, u_long mask)
+netname(uint32_t in, uint32_t mask)
 {
        char *cp = 0;
        static char line[MAXHOSTNAMELEN];
        struct netent *np = 0;
-       u_long net, omask, dmask;
-       register u_long i;
+       uint32_t net, omask, dmask;
+       uint32_t i;
 
        i = ntohl(in);
        dmask = forgemask(i);
@@ -812,31 +591,31 @@ netname(u_long in, u_long mask)
                }
        }
        if (cp)
-               strncpy(line, cp, sizeof(line) - 1);
+               strlcpy(line, cp, sizeof(line));
        else {
                switch (dmask) {
                case IN_CLASSA_NET:
                        if ((i & IN_CLASSA_HOST) == 0) {
-                               sprintf(line, "%lu", C(i >> 24));
+                               snprintf(line, sizeof(line), "%u", C(i >> 24));
                                break;
                        }
                        /* FALLTHROUGH */
                case IN_CLASSB_NET:
                        if ((i & IN_CLASSB_HOST) == 0) {
-                               sprintf(line, "%lu.%lu",
+                               snprintf(line, sizeof(line), "%u.%u",
                                        C(i >> 24), C(i >> 16));
                                break;
                        }
                        /* FALLTHROUGH */
                case IN_CLASSC_NET:
                        if ((i & IN_CLASSC_HOST) == 0) {
-                               sprintf(line, "%lu.%lu.%lu",
+                               snprintf(line, sizeof(line), "%u.%u.%u",
                                        C(i >> 24), C(i >> 16), C(i >> 8));
                                break;
                        }
                        /* FALLTHROUGH */
                default:
-                       sprintf(line, "%lu.%lu.%lu.%lu",
+                       snprintf(line, sizeof(line), "%u.%u.%u.%u",
                                C(i >> 24), C(i >> 16), C(i >> 8), C(i));
                        break;
                }
@@ -893,10 +672,9 @@ netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam)
                }
                if (illegal)
                        fprintf(stderr, "illegal prefixlen\n");
-       }
-       else
+       } else {
                masklen = 128;
-
+       }
        if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
                return("default");
 
@@ -906,7 +684,7 @@ netname6(struct sockaddr_in6 *sa6, struct sockaddr *sam)
                    NULL, 0, flag);
 
        if (nflag)
-               sprintf(&line[strlen(line)], "/%d", masklen);
+               snprintf(&line[strlen(line)], sizeof(line) - strlen(line), "/%d", masklen);
 
        return line;
 }
@@ -973,175 +751,19 @@ rt_stats(void)
        p(rts_newgateway, "\t%u new gateway%s due to redirects\n");
        p(rts_unreach, "\t%u destination%s found unreachable\n");
        p(rts_wildcard, "\t%u use%s of a wildcard route\n");
+       p(rts_badrtgwroute, "\t%u lookup%s returned indirect "
+           "routes pointing to indirect gateway route\n");
 #undef p
 
        if (rttrash || sflag <= 1)
                printf("\t%u route%s not in table but not freed\n",
                    rttrash, plural(rttrash));
 }
-#ifndef __APPLE__
-char *
-ipx_print(struct sockaddr *sa)
-{
-       u_short port;
-       struct servent *sp = 0;
-       char *net = "", *host = "";
-       register char *p;
-       register u_char *q;
-       struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr;
-       static char mybuf[50];
-       char cport[10], chost[15], cnet[15];
-
-       port = ntohs(work.x_port);
-
-       if (ipx_nullnet(work) && ipx_nullhost(work)) {
-
-               if (port) {
-                       if (sp)
-                               sprintf(mybuf, "*.%s", sp->s_name);
-                       else
-                               sprintf(mybuf, "*.%x", port);
-               } else
-                       sprintf(mybuf, "*.*");
-
-               return (mybuf);
-       }
-
-       if (ipx_wildnet(work))
-               net = "any";
-       else if (ipx_nullnet(work))
-               net = "*";
-       else {
-               q = work.x_net.c_net;
-               sprintf(cnet, "%02x%02x%02x%02x",
-                       q[0], q[1], q[2], q[3]);
-               for (p = cnet; *p == '0' && p < cnet + 8; p++)
-                       continue;
-               net = p;
-       }
-
-       if (ipx_wildhost(work))
-               host = "any";
-       else if (ipx_nullhost(work))
-               host = "*";
-       else {
-               q = work.x_host.c_host;
-               sprintf(chost, "%02x%02x%02x%02x%02x%02x",
-                       q[0], q[1], q[2], q[3], q[4], q[5]);
-               for (p = chost; *p == '0' && p < chost + 12; p++)
-                       continue;
-               host = p;
-       }
-
-       if (port) {
-               if (strcmp(host, "*") == 0)
-                       host = "";
-               if (sp) 
-                       snprintf(cport, sizeof(cport),
-                               "%s%s", *host ? "." : "", sp->s_name);
-               else    
-                       snprintf(cport, sizeof(cport),
-                               "%s%x", *host ? "." : "", port);
-       } else
-               *cport = 0;
-
-       snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport);
-       return(mybuf);
-}
-
-char *
-ipx_phost(struct sockaddr *sa)
-{
-       register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa;
-       struct sockaddr_ipx work;
-       static union ipx_net ipx_zeronet;
-       char *p;
-       struct ipx_addr in;
-
-       work = *sipx;
-       in = work.sipx_addr;
-
-       work.sipx_addr.x_port = 0;
-       work.sipx_addr.x_net = ipx_zeronet;
-       p = ipx_print((struct sockaddr *)&work);
-       if (strncmp("*.", p, 2) == 0) p += 2;
-
-       return(p);
-}
-#endif
-#ifdef NS
-short ns_nullh[] = {0,0,0};
-short ns_bh[] = {-1,-1,-1};
-
-char *
-ns_print(struct sockaddr *sa)
-{
-       register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
-       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;
-
-       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++)
-                       continue;
-               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(struct sockaddr *sa)
-{
-       register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
-       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((struct sockaddr *)&work);
-       if (strncmp("0H.", p, 3) == 0)
-               p += 3;
-       return(p);
-}
-#endif
 
 void
 upHex(char *p0)
 {
-       register char *p = p0;
+       char *p = p0;
 
        for (; *p; p++)
                switch (*p) {