Home | History | Annotate | Line # | Download | only in netstat
route.c revision 1.39
      1  1.39    itojun /*	$NetBSD: route.c,v 1.39 1999/09/03 04:26:31 itojun Exp $	*/
      2  1.14   thorpej 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1983, 1988, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.23     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.14   thorpej #if 0
     39  1.14   thorpej static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     40  1.14   thorpej #else
     41  1.39    itojun __RCSID("$NetBSD: route.c,v 1.39 1999/09/03 04:26:31 itojun Exp $");
     42  1.14   thorpej #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46  1.10   mycroft #include <sys/protosw.h>
     47   1.1       cgd #include <sys/socket.h>
     48   1.1       cgd #include <sys/mbuf.h>
     49  1.30       mrg #include <sys/un.h>
     50   1.1       cgd 
     51   1.1       cgd #include <net/if.h>
     52  1.10   mycroft #include <net/if_dl.h>
     53  1.10   mycroft #include <net/if_types.h>
     54  1.11       jtc #define _KERNEL
     55   1.1       cgd #include <net/route.h>
     56  1.11       jtc #undef _KERNEL
     57   1.1       cgd #include <netinet/in.h>
     58  1.21  christos #include <netatalk/at.h>
     59  1.30       mrg #include <netiso/iso.h>
     60   1.1       cgd 
     61   1.1       cgd #include <netns/ns.h>
     62   1.1       cgd 
     63  1.10   mycroft #include <sys/sysctl.h>
     64   1.4      paul 
     65  1.38    itojun #include <arpa/inet.h>
     66  1.38    itojun 
     67  1.29       mrg #include <err.h>
     68   1.1       cgd #include <netdb.h>
     69   1.1       cgd #include <stdio.h>
     70  1.10   mycroft #include <stdlib.h>
     71   1.1       cgd #include <string.h>
     72  1.10   mycroft #include <unistd.h>
     73  1.29       mrg 
     74  1.10   mycroft #include "netstat.h"
     75   1.1       cgd 
     76  1.10   mycroft #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
     77   1.1       cgd 
     78  1.39    itojun /* alignment constraint for routing socket */
     79  1.39    itojun #define ROUNDUP(a) \
     80  1.39    itojun 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     81  1.39    itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     82  1.39    itojun 
     83   1.1       cgd /*
     84   1.1       cgd  * Definitions for showing gateway flags.
     85   1.1       cgd  */
     86   1.1       cgd struct bits {
     87   1.1       cgd 	short	b_mask;
     88   1.1       cgd 	char	b_val;
     89   1.1       cgd } bits[] = {
     90   1.1       cgd 	{ RTF_UP,	'U' },
     91   1.1       cgd 	{ RTF_GATEWAY,	'G' },
     92   1.1       cgd 	{ RTF_HOST,	'H' },
     93  1.10   mycroft 	{ RTF_REJECT,	'R' },
     94   1.1       cgd 	{ RTF_DYNAMIC,	'D' },
     95   1.1       cgd 	{ RTF_MODIFIED,	'M' },
     96  1.10   mycroft 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     97  1.10   mycroft 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     98   1.1       cgd 	{ RTF_CLONING,	'C' },
     99   1.1       cgd 	{ RTF_XRESOLVE,	'X' },
    100   1.1       cgd 	{ RTF_LLINFO,	'L' },
    101  1.10   mycroft 	{ RTF_STATIC,	'S' },
    102  1.10   mycroft 	{ RTF_PROTO1,	'1' },
    103  1.10   mycroft 	{ RTF_PROTO2,	'2' },
    104   1.1       cgd 	{ 0 }
    105   1.1       cgd };
    106   1.1       cgd 
    107  1.30       mrg /*
    108  1.30       mrg  * XXX we put all of the sockaddr types in here to force the alignment
    109  1.30       mrg  * to be correct.
    110  1.30       mrg  */
    111  1.37    chopps static union sockaddr_union {
    112  1.10   mycroft 	struct	sockaddr u_sa;
    113  1.30       mrg 	struct	sockaddr_in u_in;
    114  1.30       mrg 	struct	sockaddr_un u_un;
    115  1.30       mrg 	struct	sockaddr_iso u_iso;
    116  1.30       mrg 	struct	sockaddr_at u_at;
    117  1.30       mrg 	struct	sockaddr_dl u_dl;
    118  1.30       mrg 	struct	sockaddr_ns u_ns;
    119  1.10   mycroft 	u_short	u_data[128];
    120  1.38    itojun 	int u_dummy;		/* force word-alignment */
    121  1.10   mycroft } pt_u;
    122  1.10   mycroft 
    123  1.10   mycroft int	do_rtent = 0;
    124  1.10   mycroft struct	rtentry rtentry;
    125  1.10   mycroft struct	radix_node rnode;
    126  1.10   mycroft struct	radix_mask rmask;
    127  1.10   mycroft 
    128  1.10   mycroft int	NewTree = 0;
    129  1.10   mycroft 
    130  1.10   mycroft static struct sockaddr *kgetsa __P((struct sockaddr *));
    131  1.10   mycroft static void p_tree __P((struct radix_node *));
    132  1.21  christos static void p_rtnode __P((void));
    133  1.21  christos static void ntreestuff __P((void));
    134  1.10   mycroft static void np_rtentry __P((struct rt_msghdr *));
    135  1.16    mellon static void p_sockaddr __P((const struct sockaddr *,
    136  1.16    mellon 			    const struct sockaddr *, int, int));
    137  1.10   mycroft static void p_flags __P((int, char *));
    138  1.10   mycroft static void p_rtentry __P((struct rtentry *));
    139  1.21  christos static void ntreestuff __P((void));
    140  1.21  christos static u_long forgemask __P((u_long));
    141  1.29       mrg static void domask __P((char *, size_t, u_long, u_long));
    142  1.38    itojun #ifdef INET6
    143  1.38    itojun char *netname6 __P((struct in6_addr *, struct in6_addr *));
    144  1.38    itojun static char ntop_buf[INET6_ADDRSTRLEN];
    145  1.38    itojun #endif
    146   1.3       cgd 
    147   1.3       cgd /*
    148   1.1       cgd  * Print routing tables.
    149   1.1       cgd  */
    150  1.10   mycroft void
    151  1.10   mycroft routepr(rtree)
    152  1.10   mycroft 	u_long rtree;
    153   1.1       cgd {
    154  1.10   mycroft 	struct radix_node_head *rnh, head;
    155  1.10   mycroft 	int i;
    156   1.1       cgd 
    157   1.1       cgd 	printf("Routing tables\n");
    158  1.10   mycroft 
    159  1.10   mycroft 	if (Aflag == 0 && NewTree)
    160  1.10   mycroft 		ntreestuff();
    161  1.10   mycroft 	else {
    162  1.10   mycroft 		if (rtree == 0) {
    163  1.10   mycroft 			printf("rt_tables: symbol not in namelist\n");
    164  1.10   mycroft 			return;
    165  1.10   mycroft 		}
    166  1.10   mycroft 
    167  1.10   mycroft 		kget(rtree, rt_tables);
    168  1.10   mycroft 		for (i = 0; i <= AF_MAX; i++) {
    169  1.10   mycroft 			if ((rnh = rt_tables[i]) == 0)
    170  1.10   mycroft 				continue;
    171  1.10   mycroft 			kget(rnh, head);
    172  1.10   mycroft 			if (i == AF_UNSPEC) {
    173  1.10   mycroft 				if (Aflag && af == 0) {
    174  1.10   mycroft 					printf("Netmasks:\n");
    175  1.10   mycroft 					p_tree(head.rnh_treetop);
    176  1.10   mycroft 				}
    177  1.10   mycroft 			} else if (af == AF_UNSPEC || af == i) {
    178  1.10   mycroft 				pr_family(i);
    179  1.10   mycroft 				do_rtent = 1;
    180  1.10   mycroft 				pr_rthdr();
    181  1.10   mycroft 				p_tree(head.rnh_treetop);
    182  1.10   mycroft 			}
    183   1.1       cgd 		}
    184   1.1       cgd 	}
    185   1.1       cgd }
    186   1.1       cgd 
    187  1.10   mycroft /*
    188  1.10   mycroft  * Print address family header before a section of the routing table.
    189  1.10   mycroft  */
    190  1.10   mycroft void
    191  1.10   mycroft pr_family(af)
    192  1.10   mycroft 	int af;
    193   1.4      paul {
    194  1.10   mycroft 	char *afname;
    195   1.4      paul 
    196  1.10   mycroft 	switch (af) {
    197   1.4      paul 	case AF_INET:
    198  1.10   mycroft 		afname = "Internet";
    199  1.10   mycroft 		break;
    200  1.38    itojun #ifdef INET6
    201  1.38    itojun 	case AF_INET6:
    202  1.38    itojun 		afname = "Internet6";
    203  1.38    itojun 		break;
    204  1.38    itojun #endif
    205   1.4      paul 	case AF_NS:
    206  1.10   mycroft 		afname = "XNS";
    207  1.10   mycroft 		break;
    208   1.4      paul 	case AF_ISO:
    209  1.10   mycroft 		afname = "ISO";
    210   1.4      paul 		break;
    211  1.21  christos 	case AF_APPLETALK:
    212  1.21  christos 		afname = "AppleTalk";
    213  1.21  christos 		break;
    214  1.10   mycroft 	case AF_CCITT:
    215  1.10   mycroft 		afname = "X.25";
    216   1.4      paul 		break;
    217   1.4      paul 	default:
    218  1.10   mycroft 		afname = NULL;
    219  1.10   mycroft 		break;
    220   1.4      paul 	}
    221  1.10   mycroft 	if (afname)
    222  1.10   mycroft 		printf("\n%s:\n", afname);
    223  1.10   mycroft 	else
    224  1.10   mycroft 		printf("\nProtocol Family %d:\n", af);
    225   1.4      paul }
    226   1.4      paul 
    227  1.10   mycroft /* column widths; each followed by one space */
    228  1.38    itojun #ifndef INET6
    229  1.22  christos #define	WID_DST		18	/* width of destination column */
    230  1.10   mycroft #define	WID_GW		18	/* width of gateway column */
    231  1.38    itojun #else
    232  1.38    itojun #define	WID_DST	(nflag ? 28 : 18)	/* width of destination column */
    233  1.38    itojun #define	WID_GW	(nflag ? 26 : 18)	/* width of gateway column */
    234  1.38    itojun #endif /* INET6 */
    235   1.4      paul 
    236  1.10   mycroft /*
    237  1.10   mycroft  * Print header for routing table columns.
    238  1.10   mycroft  */
    239  1.10   mycroft void
    240  1.10   mycroft pr_rthdr()
    241   1.1       cgd {
    242   1.1       cgd 
    243  1.10   mycroft 	if (Aflag)
    244  1.10   mycroft 		printf("%-8.8s ","Address");
    245  1.13   thorpej 	printf("%-*.*s %-*.*s %-6.6s  %6.6s%8.8s %6.6s  %s\n",
    246  1.10   mycroft 		WID_DST, WID_DST, "Destination",
    247  1.10   mycroft 		WID_GW, WID_GW, "Gateway",
    248  1.13   thorpej 		"Flags", "Refs", "Use", "Mtu", "Interface");
    249   1.1       cgd }
    250   1.1       cgd 
    251  1.10   mycroft static struct sockaddr *
    252   1.1       cgd kgetsa(dst)
    253  1.23     lukem 	struct sockaddr *dst;
    254   1.1       cgd {
    255  1.10   mycroft 
    256   1.1       cgd 	kget(dst, pt_u.u_sa);
    257  1.10   mycroft 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
    258  1.10   mycroft 		kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
    259   1.1       cgd 	return (&pt_u.u_sa);
    260   1.1       cgd }
    261   1.1       cgd 
    262  1.10   mycroft static void
    263   1.1       cgd p_tree(rn)
    264  1.10   mycroft 	struct radix_node *rn;
    265   1.1       cgd {
    266   1.1       cgd 
    267   1.1       cgd again:
    268   1.1       cgd 	kget(rn, rnode);
    269   1.1       cgd 	if (rnode.rn_b < 0) {
    270   1.1       cgd 		if (Aflag)
    271  1.21  christos 			printf("%-8.8lx ", (u_long) rn);
    272  1.10   mycroft 		if (rnode.rn_flags & RNF_ROOT) {
    273  1.10   mycroft 			if (Aflag)
    274  1.10   mycroft 				printf("(root node)%s",
    275   1.1       cgd 				    rnode.rn_dupedkey ? " =>\n" : "\n");
    276  1.10   mycroft 		} else if (do_rtent) {
    277   1.1       cgd 			kget(rn, rtentry);
    278   1.1       cgd 			p_rtentry(&rtentry);
    279   1.1       cgd 			if (Aflag)
    280   1.1       cgd 				p_rtnode();
    281   1.1       cgd 		} else {
    282   1.1       cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
    283  1.16    mellon 			    NULL, 0, 44);
    284   1.1       cgd 			putchar('\n');
    285   1.1       cgd 		}
    286  1.21  christos 		if ((rn = rnode.rn_dupedkey) != NULL)
    287   1.1       cgd 			goto again;
    288   1.1       cgd 	} else {
    289   1.1       cgd 		if (Aflag && do_rtent) {
    290  1.21  christos 			printf("%-8.8lx ", (u_long) rn);
    291   1.1       cgd 			p_rtnode();
    292   1.1       cgd 		}
    293   1.1       cgd 		rn = rnode.rn_r;
    294   1.1       cgd 		p_tree(rnode.rn_l);
    295   1.1       cgd 		p_tree(rn);
    296   1.1       cgd 	}
    297   1.1       cgd }
    298   1.1       cgd 
    299  1.10   mycroft static void
    300   1.1       cgd p_rtnode()
    301   1.1       cgd {
    302  1.10   mycroft 	struct radix_mask *rm = rnode.rn_mklist;
    303  1.29       mrg 	char	nbuf[20];
    304   1.1       cgd 
    305   1.1       cgd 	if (rnode.rn_b < 0) {
    306   1.1       cgd 		if (rnode.rn_mask) {
    307   1.1       cgd 			printf("\t  mask ");
    308   1.1       cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
    309  1.16    mellon 				    NULL, 0, -1);
    310   1.1       cgd 		} else if (rm == 0)
    311   1.1       cgd 			return;
    312   1.1       cgd 	} else {
    313  1.29       mrg 		(void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
    314  1.21  christos 		printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
    315  1.21  christos 		    (u_long) rnode.rn_r);
    316   1.1       cgd 	}
    317   1.1       cgd 	while (rm) {
    318   1.1       cgd 		kget(rm, rmask);
    319  1.29       mrg 		(void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
    320  1.21  christos 		printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
    321  1.21  christos 		    -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
    322  1.16    mellon 		if (rmask.rm_flags & RNF_NORMAL) {
    323  1.16    mellon 			struct radix_node rnode_aux;
    324  1.16    mellon 			printf(" <normal>, ");
    325  1.16    mellon 			kget(rmask.rm_leaf, rnode_aux);
    326  1.16    mellon 			p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
    327  1.16    mellon 				    NULL, 0, -1);
    328  1.16    mellon 		} else
    329  1.29       mrg 			p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
    330  1.29       mrg 			    NULL, 0, -1);
    331   1.1       cgd 		putchar('}');
    332  1.21  christos 		if ((rm = rmask.rm_mklist) != NULL)
    333   1.1       cgd 			printf(" ->");
    334   1.1       cgd 	}
    335   1.1       cgd 	putchar('\n');
    336   1.1       cgd }
    337   1.1       cgd 
    338  1.10   mycroft static void
    339   1.1       cgd ntreestuff()
    340   1.1       cgd {
    341  1.10   mycroft 	size_t needed;
    342  1.10   mycroft 	int mib[6];
    343   1.1       cgd 	char *buf, *next, *lim;
    344  1.23     lukem 	struct rt_msghdr *rtm;
    345   1.1       cgd 
    346  1.29       mrg 	mib[0] = CTL_NET;
    347  1.29       mrg 	mib[1] = PF_ROUTE;
    348  1.29       mrg 	mib[2] = 0;
    349  1.29       mrg 	mib[3] = 0;
    350  1.29       mrg 	mib[4] = NET_RT_DUMP;
    351  1.29       mrg 	mib[5] = 0;
    352  1.29       mrg 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    353  1.29       mrg 		err(1, "route sysctl estimate");
    354   1.1       cgd 	if ((buf = malloc(needed)) == 0)
    355  1.29       mrg 		errx(1, "out of space");
    356  1.29       mrg 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    357  1.29       mrg 		err(1, "sysctl of routing table");
    358   1.1       cgd 	lim  = buf + needed;
    359   1.1       cgd 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    360   1.1       cgd 		rtm = (struct rt_msghdr *)next;
    361   1.1       cgd 		np_rtentry(rtm);
    362   1.1       cgd 	}
    363   1.1       cgd }
    364   1.1       cgd 
    365  1.10   mycroft static void
    366   1.1       cgd np_rtentry(rtm)
    367  1.23     lukem 	struct rt_msghdr *rtm;
    368   1.1       cgd {
    369  1.23     lukem 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    370  1.10   mycroft #ifdef notdef
    371  1.10   mycroft 	static int masks_done, banner_printed;
    372  1.10   mycroft #endif
    373  1.10   mycroft 	static int old_af;
    374   1.1       cgd 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    375   1.1       cgd 
    376   1.1       cgd #ifdef notdef
    377   1.1       cgd 	/* for the moment, netmasks are skipped over */
    378   1.1       cgd 	if (!banner_printed) {
    379   1.1       cgd 		printf("Netmasks:\n");
    380   1.1       cgd 		banner_printed = 1;
    381   1.1       cgd 	}
    382   1.1       cgd 	if (masks_done == 0) {
    383   1.1       cgd 		if (rtm->rtm_addrs != RTA_DST ) {
    384   1.1       cgd 			masks_done = 1;
    385   1.1       cgd 			af = sa->sa_family;
    386   1.1       cgd 		}
    387   1.1       cgd 	} else
    388   1.1       cgd #endif
    389   1.1       cgd 		af = sa->sa_family;
    390   1.1       cgd 	if (af != old_af) {
    391  1.10   mycroft 		pr_family(af);
    392   1.1       cgd 		old_af = af;
    393   1.1       cgd 	}
    394   1.1       cgd 	if (rtm->rtm_addrs == RTA_DST)
    395  1.16    mellon 		p_sockaddr(sa, NULL, 0, 36);
    396   1.1       cgd 	else {
    397  1.16    mellon 		p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
    398  1.39    itojun 		sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
    399  1.16    mellon 		p_sockaddr(sa, NULL, 0, 18);
    400   1.1       cgd 	}
    401   1.1       cgd 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
    402   1.1       cgd 	putchar('\n');
    403   1.1       cgd }
    404   1.1       cgd 
    405  1.10   mycroft static void
    406  1.16    mellon p_sockaddr(sa, mask, flags, width)
    407  1.16    mellon 	const struct sockaddr *sa, *mask;
    408  1.10   mycroft 	int flags, width;
    409   1.1       cgd {
    410  1.10   mycroft 	char workbuf[128], *cplim;
    411  1.23     lukem 	char *cp = workbuf;
    412   1.1       cgd 
    413   1.1       cgd 	switch(sa->sa_family) {
    414   1.1       cgd 	case AF_INET:
    415   1.1       cgd 	    {
    416  1.23     lukem 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    417   1.1       cgd 
    418  1.16    mellon 		if (sin->sin_addr.s_addr == INADDR_ANY)
    419  1.16    mellon 			cp = "default";
    420  1.16    mellon 		else if (flags & RTF_HOST)
    421  1.16    mellon 			cp = routename(sin->sin_addr.s_addr);
    422  1.16    mellon 		else if (mask)
    423  1.16    mellon 			cp = netname(sin->sin_addr.s_addr,
    424  1.19   mycroft 			    ((struct sockaddr_in *)mask)->sin_addr.s_addr);
    425  1.16    mellon 		else
    426  1.16    mellon 			cp = netname(sin->sin_addr.s_addr, INADDR_ANY);
    427  1.10   mycroft 		break;
    428   1.1       cgd 	    }
    429   1.1       cgd 
    430  1.38    itojun #ifdef INET6
    431  1.38    itojun 	case AF_INET6:
    432  1.38    itojun 	    {
    433  1.38    itojun 		struct in6_addr *in6 = &((struct sockaddr_in6 *)sa)->sin6_addr;
    434  1.38    itojun 
    435  1.38    itojun 		if (flags & RTF_HOST)
    436  1.38    itojun 			cp = routename6((char *)in6);
    437  1.38    itojun 		else if (mask) {
    438  1.38    itojun 			cp = netname6(in6,
    439  1.38    itojun 				&((struct sockaddr_in6 *)mask)->sin6_addr);
    440  1.38    itojun 		} else
    441  1.38    itojun 			cp = (char *)inet_ntop(AF_INET6, in6, ntop_buf,
    442  1.38    itojun 						sizeof(ntop_buf));
    443  1.38    itojun 		break;
    444  1.38    itojun 	    }
    445  1.38    itojun #endif
    446  1.38    itojun 
    447  1.33       mrg #ifndef SMALL
    448  1.21  christos 	case AF_APPLETALK:
    449  1.21  christos 	case 0:
    450  1.21  christos 	    {
    451  1.21  christos 		if (!(flags & RTF_HOST) && mask)
    452  1.21  christos 			cp = atalk_print2(sa,mask,11);
    453  1.21  christos 		else
    454  1.21  christos 			cp = atalk_print(sa,11);
    455  1.21  christos 		break;
    456  1.21  christos 	    }
    457   1.1       cgd 	case AF_NS:
    458  1.18    mellon 		cp = ns_print((struct sockaddr *)sa);
    459   1.4      paul 		break;
    460  1.33       mrg #endif
    461   1.4      paul 
    462   1.4      paul 	case AF_LINK:
    463  1.10   mycroft 	    {
    464  1.23     lukem 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
    465  1.10   mycroft 
    466  1.10   mycroft 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
    467  1.10   mycroft 		    sdl->sdl_slen == 0)
    468  1.29       mrg 			(void)snprintf(workbuf, sizeof workbuf, "link#%d",
    469  1.29       mrg 			    sdl->sdl_index);
    470  1.10   mycroft 		else switch (sdl->sdl_type) {
    471  1.20   thorpej 		case IFT_FDDI:
    472  1.10   mycroft 		case IFT_ETHER:
    473  1.10   mycroft 		    {
    474  1.23     lukem 			int i;
    475  1.23     lukem 			u_char *lla = (u_char *)sdl->sdl_data +
    476  1.10   mycroft 			    sdl->sdl_nlen;
    477  1.10   mycroft 
    478  1.10   mycroft 			cplim = "";
    479  1.10   mycroft 			for (i = 0; i < sdl->sdl_alen; i++, lla++) {
    480  1.29       mrg 				/* XXX */
    481  1.20   thorpej 				cp += sprintf(cp, "%s%02x", cplim, *lla);
    482  1.10   mycroft 				cplim = ":";
    483  1.10   mycroft 			}
    484  1.10   mycroft 			cp = workbuf;
    485  1.10   mycroft 			break;
    486  1.10   mycroft 		    }
    487  1.10   mycroft 		default:
    488  1.10   mycroft 			cp = link_ntoa(sdl);
    489  1.10   mycroft 			break;
    490  1.10   mycroft 		}
    491   1.4      paul 		break;
    492  1.10   mycroft 	    }
    493   1.1       cgd 
    494   1.1       cgd 	default:
    495   1.1       cgd 	    {
    496  1.23     lukem 		u_char *s = (u_char *)sa->sa_data, *slim;
    497   1.1       cgd 
    498  1.10   mycroft 		slim =  sa->sa_len + (u_char *) sa;
    499   1.1       cgd 		cplim = cp + sizeof(workbuf) - 6;
    500   1.1       cgd 		cp += sprintf(cp, "(%d)", sa->sa_family);
    501   1.7       cgd 		while (s < slim && cp < cplim) {
    502   1.7       cgd 			cp += sprintf(cp, " %02x", *s++);
    503   1.7       cgd 			if (s < slim)
    504  1.10   mycroft 			    cp += sprintf(cp, "%02x", *s++);
    505   1.7       cgd 		}
    506   1.1       cgd 		cp = workbuf;
    507   1.1       cgd 	    }
    508   1.1       cgd 	}
    509   1.1       cgd 	if (width < 0 )
    510   1.1       cgd 		printf("%s ", cp);
    511   1.1       cgd 	else {
    512   1.1       cgd 		if (nflag)
    513   1.1       cgd 			printf("%-*s ", width, cp);
    514   1.1       cgd 		else
    515   1.1       cgd 			printf("%-*.*s ", width, width, cp);
    516   1.1       cgd 	}
    517   1.1       cgd }
    518   1.1       cgd 
    519  1.10   mycroft static void
    520   1.1       cgd p_flags(f, format)
    521  1.23     lukem 	int f;
    522   1.4      paul 	char *format;
    523   1.4      paul {
    524   1.4      paul 	char name[33], *flags;
    525  1.23     lukem 	struct bits *p = bits;
    526   1.4      paul 
    527  1.29       mrg 	for (flags = name; p->b_mask && flags - name < sizeof(name); p++)
    528   1.4      paul 		if (p->b_mask & f)
    529   1.4      paul 			*flags++ = p->b_val;
    530   1.4      paul 	*flags = '\0';
    531   1.4      paul 	printf(format, name);
    532   1.4      paul }
    533   1.4      paul 
    534  1.37    chopps static struct sockaddr *sockcopy __P((struct sockaddr *,
    535  1.37    chopps     union sockaddr_union *));
    536  1.35    chopps 
    537  1.35    chopps /*
    538  1.35    chopps  * copy a sockaddr into an allocated region, allocate at least sockaddr
    539  1.35    chopps  * bytes and zero unused
    540  1.35    chopps  */
    541  1.35    chopps static struct sockaddr *
    542  1.37    chopps sockcopy(sp, dp)
    543  1.35    chopps 	struct sockaddr *sp;
    544  1.37    chopps 	union sockaddr_union *dp;
    545  1.35    chopps {
    546  1.37    chopps 	int len;
    547  1.35    chopps 
    548  1.37    chopps 	if (sp == 0 || sp->sa_len == 0)
    549  1.37    chopps 		(void)memset(dp, 0, sizeof (*sp));
    550  1.37    chopps 	else {
    551  1.37    chopps 		len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
    552  1.37    chopps 		(void)memcpy(dp, sp, len);
    553  1.37    chopps 	}
    554  1.37    chopps 	return ((struct sockaddr *)dp);
    555  1.35    chopps }
    556  1.35    chopps 
    557  1.10   mycroft static void
    558   1.4      paul p_rtentry(rt)
    559  1.23     lukem 	struct rtentry *rt;
    560   1.4      paul {
    561  1.10   mycroft 	static struct ifnet ifnet, *lastif;
    562  1.37    chopps 	union sockaddr_union addr_un, mask_un;
    563  1.35    chopps 	struct sockaddr *addr, *mask;
    564   1.4      paul 
    565  1.38    itojun 	memset(&addr_un, 0, sizeof(addr_un));
    566  1.38    itojun 	memset(&mask_un, 0, sizeof(mask_un));
    567  1.37    chopps 	addr = sockcopy(kgetsa(rt_key(rt)), &addr_un);
    568  1.35    chopps 	if (rt_mask(rt))
    569  1.37    chopps 		mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
    570  1.16    mellon 	else
    571  1.38    itojun 		mask = sockcopy(NULL, &mask_un);
    572  1.35    chopps 	p_sockaddr(addr, mask, rt->rt_flags, WID_DST);
    573  1.16    mellon 	p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, WID_GW);
    574   1.4      paul 	p_flags(rt->rt_flags, "%-6.6s ");
    575  1.27       kml 	printf("%6d %8lu ", rt->rt_refcnt, rt->rt_use);
    576  1.13   thorpej 	if (rt->rt_rmx.rmx_mtu)
    577  1.27       kml 		printf("%6lu", rt->rt_rmx.rmx_mtu);
    578  1.13   thorpej 	else
    579  1.26       kml 		printf("%6s", "-");
    580  1.26       kml 	putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
    581  1.10   mycroft 	if (rt->rt_ifp) {
    582  1.10   mycroft 		if (rt->rt_ifp != lastif) {
    583  1.10   mycroft 			kget(rt->rt_ifp, ifnet);
    584  1.10   mycroft 			lastif = rt->rt_ifp;
    585  1.10   mycroft 		}
    586  1.15   thorpej 		printf(" %.16s%s", ifnet.if_xname,
    587  1.10   mycroft 			rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
    588   1.1       cgd 	}
    589  1.10   mycroft 	putchar('\n');
    590  1.34       kml  	if (vflag) {
    591  1.34       kml  		printf("\texpire   %10lu%c  recvpipe %10ld%c  "
    592  1.34       kml 		       "sendpipe %10ld%c\n",
    593  1.34       kml  			rt->rt_rmx.rmx_expire,
    594  1.34       kml  			(rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ',
    595  1.34       kml  			rt->rt_rmx.rmx_recvpipe,
    596  1.34       kml  			(rt->rt_rmx.rmx_locks & RTV_RPIPE) ? 'L' : ' ',
    597  1.34       kml  			rt->rt_rmx.rmx_sendpipe,
    598  1.34       kml  			(rt->rt_rmx.rmx_locks & RTV_SPIPE) ? 'L' : ' ');
    599  1.34       kml  		printf("\tssthresh %10lu%c  rtt      %10ld%c  "
    600  1.34       kml 		       "rttvar   %10ld%c\n",
    601  1.34       kml  			rt->rt_rmx.rmx_ssthresh,
    602  1.34       kml  			(rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
    603  1.34       kml  			rt->rt_rmx.rmx_rtt,
    604  1.34       kml  			(rt->rt_rmx.rmx_locks & RTV_RTT) ? 'L' : ' ',
    605  1.34       kml  			rt->rt_rmx.rmx_rttvar,
    606  1.34       kml 			(rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
    607  1.34       kml  	}
    608  1.34       kml 
    609   1.1       cgd }
    610   1.1       cgd 
    611   1.1       cgd char *
    612   1.1       cgd routename(in)
    613  1.12   mycroft 	u_int32_t in;
    614   1.1       cgd {
    615  1.23     lukem 	char *cp;
    616   1.1       cgd 	static char line[MAXHOSTNAMELEN + 1];
    617   1.1       cgd 	struct hostent *hp;
    618   1.1       cgd 	static char domain[MAXHOSTNAMELEN + 1];
    619   1.1       cgd 	static int first = 1;
    620   1.1       cgd 
    621   1.1       cgd 	if (first) {
    622   1.1       cgd 		first = 0;
    623  1.28       mrg 		if (gethostname(domain, MAXHOSTNAMELEN) == 0) {
    624  1.28       mrg 			domain[sizeof(domain) - 1] = '\0';
    625  1.28       mrg 			if ((cp = strchr(domain, '.')))
    626  1.28       mrg 				(void)strcpy(domain, cp + 1);
    627  1.28       mrg 			else
    628  1.28       mrg 				domain[0] = 0;
    629  1.28       mrg 		} else
    630   1.1       cgd 			domain[0] = 0;
    631   1.1       cgd 	}
    632   1.1       cgd 	cp = 0;
    633   1.1       cgd 	if (!nflag) {
    634   1.1       cgd 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
    635   1.1       cgd 			AF_INET);
    636   1.1       cgd 		if (hp) {
    637  1.23     lukem 			if ((cp = strchr(hp->h_name, '.')) &&
    638   1.1       cgd 			    !strcmp(cp + 1, domain))
    639   1.1       cgd 				*cp = 0;
    640   1.1       cgd 			cp = hp->h_name;
    641   1.1       cgd 		}
    642   1.1       cgd 	}
    643  1.29       mrg 	if (cp) {
    644   1.1       cgd 		strncpy(line, cp, sizeof(line) - 1);
    645  1.29       mrg 		line[sizeof(line) - 1] = '\0';
    646  1.29       mrg 	} else {
    647   1.1       cgd #define C(x)	((x) & 0xff)
    648  1.10   mycroft 		in = ntohl(in);
    649  1.29       mrg 		snprintf(line, sizeof line, "%u.%u.%u.%u",
    650  1.10   mycroft 		    C(in >> 24), C(in >> 16), C(in >> 8), C(in));
    651   1.1       cgd 	}
    652   1.1       cgd 	return (line);
    653   1.1       cgd }
    654   1.1       cgd 
    655  1.16    mellon static u_long
    656  1.16    mellon forgemask(a)
    657  1.16    mellon 	u_long a;
    658  1.16    mellon {
    659  1.16    mellon 	u_long m;
    660  1.16    mellon 
    661  1.16    mellon 	if (IN_CLASSA(a))
    662  1.16    mellon 		m = IN_CLASSA_NET;
    663  1.16    mellon 	else if (IN_CLASSB(a))
    664  1.16    mellon 		m = IN_CLASSB_NET;
    665  1.16    mellon 	else
    666  1.16    mellon 		m = IN_CLASSC_NET;
    667  1.16    mellon 	return (m);
    668  1.16    mellon }
    669  1.16    mellon 
    670  1.16    mellon static void
    671  1.29       mrg domask(dst, dlen, addr, mask)
    672  1.16    mellon 	char *dst;
    673  1.29       mrg 	size_t dlen;
    674  1.16    mellon 	u_long addr, mask;
    675  1.16    mellon {
    676  1.23     lukem 	int b, i;
    677  1.16    mellon 
    678  1.16    mellon 	if (!mask || (forgemask(addr) == mask)) {
    679  1.16    mellon 		*dst = '\0';
    680  1.16    mellon 		return;
    681  1.16    mellon 	}
    682  1.16    mellon 	i = 0;
    683  1.16    mellon 	for (b = 0; b < 32; b++)
    684  1.16    mellon 		if (mask & (1 << b)) {
    685  1.23     lukem 			int bb;
    686  1.16    mellon 
    687  1.16    mellon 			i = b;
    688  1.16    mellon 			for (bb = b+1; bb < 32; bb++)
    689  1.16    mellon 				if (!(mask & (1 << bb))) {
    690  1.16    mellon 					i = -1;	/* noncontig */
    691  1.16    mellon 					break;
    692  1.16    mellon 				}
    693  1.16    mellon 			break;
    694  1.16    mellon 		}
    695  1.16    mellon 	if (i == -1)
    696  1.29       mrg 		(void)snprintf(dst, dlen, "&0x%lx", mask);
    697  1.16    mellon 	else
    698  1.29       mrg 		(void)snprintf(dst, dlen, "/%d", 32-i);
    699  1.16    mellon }
    700  1.16    mellon 
    701   1.1       cgd /*
    702   1.1       cgd  * Return the name of the network whose address is given.
    703   1.1       cgd  * The address is assumed to be that of a net or subnet, not a host.
    704   1.1       cgd  */
    705   1.1       cgd char *
    706   1.1       cgd netname(in, mask)
    707  1.12   mycroft 	u_int32_t in, mask;
    708   1.1       cgd {
    709   1.1       cgd 	char *cp = 0;
    710  1.16    mellon 	static char line[MAXHOSTNAMELEN + 4];
    711   1.1       cgd 	struct netent *np = 0;
    712  1.16    mellon 	u_int32_t net, omask;
    713  1.23     lukem 	u_int32_t i;
    714   1.1       cgd 	int subnetshift;
    715   1.1       cgd 
    716  1.16    mellon 	i = ntohl(in);
    717  1.19   mycroft 	omask = mask = ntohl(mask);
    718  1.16    mellon 	if (!nflag && i != INADDR_ANY) {
    719  1.12   mycroft 		if (mask == INADDR_ANY) {
    720  1.16    mellon 			switch (mask = forgemask(i)) {
    721  1.16    mellon 			case IN_CLASSA_NET:
    722   1.1       cgd 				subnetshift = 8;
    723  1.16    mellon 				break;
    724  1.16    mellon 			case IN_CLASSB_NET:
    725   1.1       cgd 				subnetshift = 8;
    726  1.16    mellon 				break;
    727  1.16    mellon 			case IN_CLASSC_NET:
    728   1.1       cgd 				subnetshift = 4;
    729  1.16    mellon 				break;
    730  1.16    mellon 			default:
    731  1.16    mellon 				abort();
    732   1.1       cgd 			}
    733   1.1       cgd 			/*
    734   1.1       cgd 			 * If there are more bits than the standard mask
    735   1.1       cgd 			 * would suggest, subnets must be in use.
    736   1.1       cgd 			 * Guess at the subnet mask, assuming reasonable
    737   1.1       cgd 			 * width subnet fields.
    738   1.1       cgd 			 */
    739  1.16    mellon 			while (i &~ mask)
    740   1.1       cgd 				mask = (long)mask >> subnetshift;
    741   1.1       cgd 		}
    742  1.16    mellon 		net = i & mask;
    743  1.32        pk 		/*
    744  1.32        pk 		 * Note: shift the hosts bits out in octet units, since
    745  1.32        pk 		 * not all versions of getnetbyaddr() do this for us (e.g.
    746  1.32        pk 		 * the current `etc/networks' parser).
    747  1.32        pk 		 */
    748  1.32        pk 		while ((mask & 0xff) == 0)
    749  1.32        pk 			mask >>= 8, net >>= 8;
    750   1.1       cgd 		np = getnetbyaddr(net, AF_INET);
    751   1.1       cgd 		if (np)
    752   1.1       cgd 			cp = np->n_name;
    753  1.10   mycroft 	}
    754   1.1       cgd 	if (cp)
    755   1.1       cgd 		strncpy(line, cp, sizeof(line) - 1);
    756  1.16    mellon 	else if ((i & 0xffffff) == 0)
    757  1.29       mrg 		(void)snprintf(line, sizeof line, "%u", C(i >> 24));
    758  1.16    mellon 	else if ((i & 0xffff) == 0)
    759  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u", C(i >> 24)
    760  1.29       mrg 		    , C(i >> 16));
    761  1.16    mellon 	else if ((i & 0xff) == 0)
    762  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u", C(i >> 24),
    763  1.29       mrg 		    C(i >> 16), C(i >> 8));
    764   1.1       cgd 	else
    765  1.29       mrg 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u", C(i >> 24),
    766  1.16    mellon 			C(i >> 16), C(i >> 8), C(i));
    767  1.29       mrg 	domask(line + strlen(line), sizeof(line) - strlen(line), i, omask);
    768   1.1       cgd 	return (line);
    769   1.1       cgd }
    770  1.38    itojun 
    771  1.38    itojun #ifdef INET6
    772  1.38    itojun char *
    773  1.38    itojun netname6(in6, mask)
    774  1.38    itojun 	struct in6_addr *in6;
    775  1.38    itojun 	struct in6_addr *mask;
    776  1.38    itojun {
    777  1.38    itojun 	static char line[MAXHOSTNAMELEN + 1];
    778  1.38    itojun 	struct in6_addr net6;
    779  1.38    itojun 	u_char *p;
    780  1.38    itojun 	u_char *lim;
    781  1.38    itojun 	int masklen, final = 0, illegal = 0;
    782  1.39    itojun 	int i;
    783  1.38    itojun 
    784  1.38    itojun 	net6 = *in6;
    785  1.39    itojun 	for (i = 0; i < sizeof(net6); i++)
    786  1.39    itojun 		net6.s6_addr[i] &= mask->s6_addr[i];
    787  1.38    itojun 
    788  1.38    itojun 	masklen = 0;
    789  1.38    itojun 	lim = (u_char *)mask + 16;
    790  1.38    itojun 	for (p = (u_char *)mask; p < lim; p++) {
    791  1.38    itojun 		if (final && *p) {
    792  1.38    itojun 			illegal++;
    793  1.38    itojun 			continue;
    794  1.38    itojun 		}
    795  1.38    itojun 
    796  1.38    itojun 		switch (*p & 0xff) {
    797  1.38    itojun 		case 0xff:
    798  1.38    itojun 			masklen += 8;
    799  1.38    itojun 			break;
    800  1.38    itojun 		case 0xfe:
    801  1.38    itojun 			masklen += 7;
    802  1.38    itojun 			final++;
    803  1.38    itojun 			break;
    804  1.38    itojun 		case 0xfc:
    805  1.38    itojun 			masklen += 6;
    806  1.38    itojun 			final++;
    807  1.38    itojun 			break;
    808  1.38    itojun 		case 0xf8:
    809  1.38    itojun 			masklen += 5;
    810  1.38    itojun 			final++;
    811  1.38    itojun 			break;
    812  1.38    itojun 		case 0xf0:
    813  1.38    itojun 			masklen += 4;
    814  1.38    itojun 			final++;
    815  1.38    itojun 			break;
    816  1.38    itojun 		case 0xe0:
    817  1.38    itojun 			masklen += 3;
    818  1.38    itojun 			final++;
    819  1.38    itojun 			break;
    820  1.38    itojun 		case 0xc0:
    821  1.38    itojun 			masklen += 2;
    822  1.38    itojun 			final++;
    823  1.38    itojun 			break;
    824  1.38    itojun 		case 0x80:
    825  1.38    itojun 			masklen += 1;
    826  1.38    itojun 			final++;
    827  1.38    itojun 			break;
    828  1.38    itojun 		case 0x00:
    829  1.38    itojun 			final++;
    830  1.38    itojun 			break;
    831  1.38    itojun 		default:
    832  1.38    itojun 			final++;
    833  1.38    itojun 			illegal++;
    834  1.38    itojun 			break;
    835  1.38    itojun 		}
    836  1.38    itojun 	}
    837  1.38    itojun 
    838  1.38    itojun 	if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(in6))
    839  1.38    itojun 		return("default");
    840  1.38    itojun 
    841  1.38    itojun 	if (illegal)
    842  1.38    itojun 		fprintf(stderr, "illegal prefixlen\n");
    843  1.38    itojun 	sprintf(line, "%s/%d", inet_ntop(AF_INET6, &net6, ntop_buf,
    844  1.38    itojun 					sizeof(ntop_buf)),
    845  1.38    itojun 				masklen);
    846  1.38    itojun 	return line;
    847  1.38    itojun }
    848  1.38    itojun 
    849  1.38    itojun char *
    850  1.38    itojun routename6(in6)
    851  1.38    itojun 	char *in6;
    852  1.38    itojun {
    853  1.38    itojun 	static char line[MAXHOSTNAMELEN + 1];
    854  1.38    itojun 	sprintf(line, "%s", inet_ntop(AF_INET6, in6, ntop_buf,
    855  1.38    itojun 				sizeof(ntop_buf)));
    856  1.38    itojun 	return line;
    857  1.38    itojun }
    858  1.38    itojun #endif /*INET6*/
    859   1.1       cgd 
    860   1.1       cgd /*
    861   1.1       cgd  * Print routing statistics
    862   1.1       cgd  */
    863  1.10   mycroft void
    864   1.1       cgd rt_stats(off)
    865   1.9       cgd 	u_long off;
    866   1.1       cgd {
    867   1.1       cgd 	struct rtstat rtstat;
    868   1.1       cgd 
    869   1.1       cgd 	if (off == 0) {
    870   1.1       cgd 		printf("rtstat: symbol not in namelist\n");
    871   1.1       cgd 		return;
    872   1.1       cgd 	}
    873  1.10   mycroft 	kread(off, (char *)&rtstat, sizeof (rtstat));
    874   1.1       cgd 	printf("routing:\n");
    875   1.1       cgd 	printf("\t%u bad routing redirect%s\n",
    876   1.1       cgd 		rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
    877   1.1       cgd 	printf("\t%u dynamically created route%s\n",
    878   1.1       cgd 		rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
    879   1.1       cgd 	printf("\t%u new gateway%s due to redirects\n",
    880   1.1       cgd 		rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
    881   1.1       cgd 	printf("\t%u destination%s found unreachable\n",
    882   1.1       cgd 		rtstat.rts_unreach, plural(rtstat.rts_unreach));
    883   1.1       cgd 	printf("\t%u use%s of a wildcard route\n",
    884   1.1       cgd 		rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
    885   1.1       cgd }
    886   1.1       cgd short ns_nullh[] = {0,0,0};
    887   1.1       cgd short ns_bh[] = {-1,-1,-1};
    888   1.1       cgd 
    889   1.1       cgd char *
    890  1.10   mycroft ns_print(sa)
    891  1.17    mellon 	struct sockaddr *sa;
    892   1.1       cgd {
    893  1.23     lukem 	struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
    894   1.1       cgd 	struct ns_addr work;
    895  1.29       mrg 	union {
    896  1.29       mrg 		union	ns_net net_e;
    897  1.29       mrg 		u_long	long_e;
    898  1.29       mrg 	} net;
    899   1.1       cgd 	u_short port;
    900   1.1       cgd 	static char mybuf[50], cport[10], chost[25];
    901   1.1       cgd 	char *host = "";
    902  1.23     lukem 	char *p;
    903  1.23     lukem 	u_char *q;
    904   1.1       cgd 
    905   1.1       cgd 	work = sns->sns_addr;
    906   1.1       cgd 	port = ntohs(work.x_port);
    907   1.1       cgd 	work.x_port = 0;
    908   1.1       cgd 	net.net_e  = work.x_net;
    909   1.1       cgd 	if (ns_nullhost(work) && net.long_e == 0) {
    910   1.1       cgd 		if (port ) {
    911  1.29       mrg 			(void)snprintf(mybuf, sizeof mybuf, "*.%xH", port);
    912   1.1       cgd 			upHex(mybuf);
    913   1.1       cgd 		} else
    914  1.29       mrg 			(void)snprintf(mybuf, sizeof mybuf, "*.*");
    915   1.1       cgd 		return (mybuf);
    916   1.1       cgd 	}
    917   1.1       cgd 
    918  1.23     lukem 	if (memcmp(ns_bh, work.x_host.c_host, 6) == 0) {
    919   1.1       cgd 		host = "any";
    920  1.23     lukem 	} else if (memcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
    921   1.1       cgd 		host = "*";
    922   1.1       cgd 	} else {
    923   1.1       cgd 		q = work.x_host.c_host;
    924  1.29       mrg 		(void)snprintf(chost, sizeof chost, "%02x%02x%02x%02x%02x%02xH",
    925   1.1       cgd 			q[0], q[1], q[2], q[3], q[4], q[5]);
    926  1.10   mycroft 		for (p = chost; *p == '0' && p < chost + 12; p++)
    927  1.10   mycroft 			continue;
    928   1.1       cgd 		host = p;
    929   1.1       cgd 	}
    930   1.1       cgd 	if (port)
    931  1.29       mrg 		(void)snprintf(cport, sizeof cport, ".%xH", htons(port));
    932   1.1       cgd 	else
    933   1.1       cgd 		*cport = 0;
    934   1.1       cgd 
    935  1.29       mrg 	(void)snprintf(mybuf, sizeof mybuf, "%xH.%s%s", (int)ntohl(net.long_e),
    936  1.29       mrg 	    host, cport);
    937   1.1       cgd 	upHex(mybuf);
    938  1.29       mrg 	return (mybuf);
    939   1.1       cgd }
    940   1.1       cgd 
    941   1.1       cgd char *
    942  1.10   mycroft ns_phost(sa)
    943  1.10   mycroft 	struct sockaddr *sa;
    944   1.1       cgd {
    945  1.23     lukem 	struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
    946   1.1       cgd 	struct sockaddr_ns work;
    947   1.1       cgd 	static union ns_net ns_zeronet;
    948   1.1       cgd 	char *p;
    949  1.10   mycroft 
    950   1.1       cgd 	work = *sns;
    951   1.1       cgd 	work.sns_addr.x_port = 0;
    952   1.1       cgd 	work.sns_addr.x_net = ns_zeronet;
    953   1.1       cgd 
    954  1.10   mycroft 	p = ns_print((struct sockaddr *)&work);
    955  1.29       mrg 	if (strncmp("0H.", p, 3) == 0)
    956  1.29       mrg 		p += 3;
    957  1.29       mrg 	return (p);
    958   1.1       cgd }
    959  1.10   mycroft 
    960  1.10   mycroft void
    961   1.1       cgd upHex(p0)
    962  1.10   mycroft 	char *p0;
    963   1.1       cgd {
    964  1.23     lukem 	char *p = p0;
    965   1.1       cgd 
    966  1.29       mrg 	for (; *p; p++)
    967  1.29       mrg 		switch (*p) {
    968  1.29       mrg 		case 'a':
    969  1.29       mrg 		case 'b':
    970  1.29       mrg 		case 'c':
    971  1.29       mrg 		case 'd':
    972  1.29       mrg 		case 'e':
    973  1.29       mrg 		case 'f':
    974  1.29       mrg 			*p += ('A' - 'a');
    975  1.29       mrg 		}
    976   1.1       cgd }
    977