Home | History | Annotate | Line # | Download | only in netstat
route.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983, 1988 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)route.c	5.20 (Berkeley) 11/29/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include <sys/param.h>
     39  1.1  cgd #include <sys/socket.h>
     40  1.1  cgd #include <sys/mbuf.h>
     41  1.1  cgd 
     42  1.1  cgd #include <net/if.h>
     43  1.1  cgd #define  KERNEL
     44  1.1  cgd #include <net/route.h>
     45  1.1  cgd #undef KERNEL
     46  1.1  cgd #include <netinet/in.h>
     47  1.1  cgd 
     48  1.1  cgd #ifdef NS
     49  1.1  cgd #include <netns/ns.h>
     50  1.1  cgd #endif
     51  1.1  cgd 
     52  1.1  cgd #include <netdb.h>
     53  1.1  cgd #include <sys/kinfo.h>
     54  1.1  cgd 
     55  1.1  cgd #include <stdio.h>
     56  1.1  cgd #include <string.h>
     57  1.1  cgd 
     58  1.1  cgd extern	int nflag, aflag, Aflag, af;
     59  1.1  cgd int do_rtent;
     60  1.1  cgd extern	char *routename(), *netname(), *plural();
     61  1.1  cgd #ifdef NS
     62  1.1  cgd exter	char *ns_print();
     63  1.1  cgd #endif
     64  1.1  cgd extern	char *malloc();
     65  1.1  cgd #define kget(p, d) \
     66  1.1  cgd 	(kvm_read((off_t)(p), (char *)&(d), sizeof (d)))
     67  1.1  cgd 
     68  1.1  cgd /*
     69  1.1  cgd  * Definitions for showing gateway flags.
     70  1.1  cgd  */
     71  1.1  cgd struct bits {
     72  1.1  cgd 	short	b_mask;
     73  1.1  cgd 	char	b_val;
     74  1.1  cgd } bits[] = {
     75  1.1  cgd 	{ RTF_UP,	'U' },
     76  1.1  cgd 	{ RTF_GATEWAY,	'G' },
     77  1.1  cgd 	{ RTF_HOST,	'H' },
     78  1.1  cgd 	{ RTF_DYNAMIC,	'D' },
     79  1.1  cgd 	{ RTF_MODIFIED,	'M' },
     80  1.1  cgd 	{ RTF_CLONING,	'C' },
     81  1.1  cgd 	{ RTF_XRESOLVE,	'X' },
     82  1.1  cgd 	{ RTF_LLINFO,	'L' },
     83  1.1  cgd 	{ RTF_REJECT,	'R' },
     84  1.1  cgd 	{ 0 }
     85  1.1  cgd };
     86  1.1  cgd 
     87  1.1  cgd /*
     88  1.1  cgd  * Print routing tables.
     89  1.1  cgd  */
     90  1.1  cgd routepr(hostaddr, netaddr, hashsizeaddr, treeaddr)
     91  1.1  cgd 	off_t hostaddr, netaddr, hashsizeaddr, treeaddr;
     92  1.1  cgd {
     93  1.1  cgd 	struct mbuf mb;
     94  1.1  cgd 	register struct ortentry *rt;
     95  1.1  cgd 	register struct mbuf *m;
     96  1.1  cgd 	char name[16], *flags;
     97  1.1  cgd 	struct mbuf **routehash;
     98  1.1  cgd 	int hashsize;
     99  1.1  cgd 	int i, doinghost = 1;
    100  1.1  cgd 
    101  1.1  cgd 	printf("Routing tables\n");
    102  1.1  cgd 	if (Aflag)
    103  1.1  cgd 		printf("%-8.8s ","Address");
    104  1.1  cgd 	printf("%-16.16s %-18.18s %-6.6s  %6.6s%8.8s  %s\n",
    105  1.1  cgd 		"Destination", "Gateway",
    106  1.1  cgd 		"Flags", "Refs", "Use", "Interface");
    107  1.1  cgd 	if (treeaddr)
    108  1.1  cgd 		return treestuff(treeaddr);
    109  1.1  cgd 	if (hostaddr == 0) {
    110  1.1  cgd 		printf("rthost: symbol not in namelist\n");
    111  1.1  cgd 		return;
    112  1.1  cgd 	}
    113  1.1  cgd 	if (netaddr == 0) {
    114  1.1  cgd 		printf("rtnet: symbol not in namelist\n");
    115  1.1  cgd 		return;
    116  1.1  cgd 	}
    117  1.1  cgd 	if (hashsizeaddr == 0) {
    118  1.1  cgd 		printf("rthashsize: symbol not in namelist\n");
    119  1.1  cgd 		return;
    120  1.1  cgd 	}
    121  1.1  cgd 	kget(hashsizeaddr, hashsize);
    122  1.1  cgd 	routehash = (struct mbuf **)malloc( hashsize*sizeof (struct mbuf *) );
    123  1.1  cgd 	kvm_read(hostaddr, (char *)routehash, hashsize*sizeof (struct mbuf *));
    124  1.1  cgd again:
    125  1.1  cgd 	for (i = 0; i < hashsize; i++) {
    126  1.1  cgd 		if (routehash[i] == 0)
    127  1.1  cgd 			continue;
    128  1.1  cgd 		m = routehash[i];
    129  1.1  cgd 		while (m) {
    130  1.1  cgd 			kget(m, mb);
    131  1.1  cgd 			if (Aflag)
    132  1.1  cgd 				printf("%8.8x ", m);
    133  1.1  cgd 			p_ortentry((struct ortentry *)(mb.m_dat));
    134  1.1  cgd 			m = mb.m_next;
    135  1.1  cgd 		}
    136  1.1  cgd 	}
    137  1.1  cgd 	if (doinghost) {
    138  1.1  cgd 		kvm_read(netaddr, (char *)routehash,
    139  1.1  cgd 			hashsize*sizeof (struct mbuf *));
    140  1.1  cgd 		doinghost = 0;
    141  1.1  cgd 		goto again;
    142  1.1  cgd 	}
    143  1.1  cgd 	free((char *)routehash);
    144  1.1  cgd 	return;
    145  1.1  cgd }
    146  1.1  cgd 
    147  1.1  cgd static union {
    148  1.1  cgd 	struct	sockaddr u_sa;
    149  1.1  cgd 	u_short	u_data[128];
    150  1.1  cgd } pt_u;
    151  1.1  cgd int do_rtent = 0;
    152  1.1  cgd struct rtentry rtentry;
    153  1.1  cgd struct radix_node rnode;
    154  1.1  cgd struct radix_mask rmask;
    155  1.1  cgd 
    156  1.1  cgd int NewTree = 0;
    157  1.1  cgd treestuff(rtree)
    158  1.1  cgd off_t rtree;
    159  1.1  cgd {
    160  1.1  cgd 	struct radix_node_head *rnh, head;
    161  1.1  cgd 
    162  1.1  cgd 	if (Aflag == 0 && NewTree)
    163  1.1  cgd 		return(ntreestuff());
    164  1.1  cgd 	for (kget(rtree, rnh); rnh; rnh = head.rnh_next) {
    165  1.1  cgd 		kget(rnh, head);
    166  1.1  cgd 		if (head.rnh_af == 0) {
    167  1.1  cgd 			if (Aflag || af == AF_UNSPEC) {
    168  1.1  cgd 				printf("Netmasks:\n");
    169  1.1  cgd 				p_tree(head.rnh_treetop);
    170  1.1  cgd 			}
    171  1.1  cgd 		} else if (af == AF_UNSPEC || af == head.rnh_af) {
    172  1.1  cgd 			printf("\nRoute Tree for Protocol Family %d:\n",
    173  1.1  cgd 								head.rnh_af);
    174  1.1  cgd 			do_rtent = 1;
    175  1.1  cgd 			p_tree(head.rnh_treetop);
    176  1.1  cgd 		}
    177  1.1  cgd 	}
    178  1.1  cgd }
    179  1.1  cgd 
    180  1.1  cgd struct sockaddr *
    181  1.1  cgd kgetsa(dst)
    182  1.1  cgd register struct sockaddr *dst;
    183  1.1  cgd {
    184  1.1  cgd 	kget(dst, pt_u.u_sa);
    185  1.1  cgd 	if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) {
    186  1.1  cgd 		kvm_read((off_t)dst, pt_u.u_data, pt_u.u_sa.sa_len);
    187  1.1  cgd 	}
    188  1.1  cgd 	return (&pt_u.u_sa);
    189  1.1  cgd }
    190  1.1  cgd 
    191  1.1  cgd p_tree(rn)
    192  1.1  cgd struct radix_node *rn;
    193  1.1  cgd {
    194  1.1  cgd 
    195  1.1  cgd again:
    196  1.1  cgd 	kget(rn, rnode);
    197  1.1  cgd 	if (rnode.rn_b < 0) {
    198  1.1  cgd 		if (Aflag)
    199  1.1  cgd 			printf("%-8.8x ", rn);
    200  1.1  cgd 		if (rnode.rn_flags & RNF_ROOT)
    201  1.1  cgd 			printf("(root node)%s",
    202  1.1  cgd 				    rnode.rn_dupedkey ? " =>\n" : "\n");
    203  1.1  cgd 		else if (do_rtent) {
    204  1.1  cgd 			kget(rn, rtentry);
    205  1.1  cgd 			p_rtentry(&rtentry);
    206  1.1  cgd 			if (Aflag)
    207  1.1  cgd 				p_rtnode();
    208  1.1  cgd 		} else {
    209  1.1  cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
    210  1.1  cgd 				    0, 44);
    211  1.1  cgd 			putchar('\n');
    212  1.1  cgd 		}
    213  1.1  cgd 		if (rn = rnode.rn_dupedkey)
    214  1.1  cgd 			goto again;
    215  1.1  cgd 	} else {
    216  1.1  cgd 		if (Aflag && do_rtent) {
    217  1.1  cgd 			printf("%-8.8x ", rn);
    218  1.1  cgd 			p_rtnode();
    219  1.1  cgd 		}
    220  1.1  cgd 		rn = rnode.rn_r;
    221  1.1  cgd 		p_tree(rnode.rn_l);
    222  1.1  cgd 		p_tree(rn);
    223  1.1  cgd 	}
    224  1.1  cgd }
    225  1.1  cgd char nbuf[20];
    226  1.1  cgd 
    227  1.1  cgd p_rtnode()
    228  1.1  cgd {
    229  1.1  cgd 
    230  1.1  cgd 	struct radix_mask *rm = rnode.rn_mklist;
    231  1.1  cgd 	if (rnode.rn_b < 0) {
    232  1.1  cgd 		if (rnode.rn_mask) {
    233  1.1  cgd 			printf("\t  mask ");
    234  1.1  cgd 			p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
    235  1.1  cgd 				    0, -1);
    236  1.1  cgd 		} else if (rm == 0)
    237  1.1  cgd 			return;
    238  1.1  cgd 	} else {
    239  1.1  cgd 		sprintf(nbuf, "(%d)", rnode.rn_b);
    240  1.1  cgd 		printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r);
    241  1.1  cgd 	}
    242  1.1  cgd 	while (rm) {
    243  1.1  cgd 		kget(rm, rmask);
    244  1.1  cgd 		sprintf(nbuf, " %d refs, ", rmask.rm_refs);
    245  1.1  cgd 		printf(" mk = %8.8x {(%d),%s",
    246  1.1  cgd 			rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
    247  1.1  cgd 		p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1);
    248  1.1  cgd 		putchar('}');
    249  1.1  cgd 		if (rm = rmask.rm_mklist)
    250  1.1  cgd 			printf(" ->");
    251  1.1  cgd 	}
    252  1.1  cgd 	putchar('\n');
    253  1.1  cgd }
    254  1.1  cgd 
    255  1.1  cgd ntreestuff()
    256  1.1  cgd {
    257  1.1  cgd 	int needed;
    258  1.1  cgd 	char *buf, *next, *lim;
    259  1.1  cgd 	register struct rt_msghdr *rtm;
    260  1.1  cgd 
    261  1.1  cgd 	if ((needed = getkerninfo(KINFO_RT_DUMP, 0, 0, 0)) < 0)
    262  1.1  cgd 		{ perror("route-getkerninfo-estimate"); exit(1);}
    263  1.1  cgd 	if ((buf = malloc(needed)) == 0)
    264  1.1  cgd 		{ printf("out of space\n"); exit(1);}
    265  1.1  cgd 	if (getkerninfo(KINFO_RT_DUMP, buf, &needed, 0) < 0)
    266  1.1  cgd 		{ perror("actual retrieval of routing table"); exit(1);}
    267  1.1  cgd 	lim  = buf + needed;
    268  1.1  cgd 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    269  1.1  cgd 		rtm = (struct rt_msghdr *)next;
    270  1.1  cgd 		np_rtentry(rtm);
    271  1.1  cgd 	}
    272  1.1  cgd }
    273  1.1  cgd 
    274  1.1  cgd np_rtentry(rtm)
    275  1.1  cgd register struct rt_msghdr *rtm;
    276  1.1  cgd {
    277  1.1  cgd 	register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    278  1.1  cgd 	static int masks_done, old_af, banner_printed;
    279  1.1  cgd 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    280  1.1  cgd 
    281  1.1  cgd #ifdef notdef
    282  1.1  cgd 	/* for the moment, netmasks are skipped over */
    283  1.1  cgd 	if (!banner_printed) {
    284  1.1  cgd 		printf("Netmasks:\n");
    285  1.1  cgd 		banner_printed = 1;
    286  1.1  cgd 	}
    287  1.1  cgd 	if (masks_done == 0) {
    288  1.1  cgd 		if (rtm->rtm_addrs != RTA_DST ) {
    289  1.1  cgd 			masks_done = 1;
    290  1.1  cgd 			af = sa->sa_family;
    291  1.1  cgd 		}
    292  1.1  cgd 	} else
    293  1.1  cgd #endif
    294  1.1  cgd 		af = sa->sa_family;
    295  1.1  cgd 	if (af != old_af) {
    296  1.1  cgd 		printf("\nRoute Tree for Protocol Family %d:\n", af);
    297  1.1  cgd 		old_af = af;
    298  1.1  cgd 	}
    299  1.1  cgd 	if (rtm->rtm_addrs == RTA_DST)
    300  1.1  cgd 		p_sockaddr(sa, 0, 36);
    301  1.1  cgd 	else {
    302  1.1  cgd 		p_sockaddr(sa, rtm->rtm_flags, 16);
    303  1.1  cgd 		if (sa->sa_len == 0)
    304  1.1  cgd 			sa->sa_len = sizeof(long);
    305  1.1  cgd 		sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
    306  1.1  cgd 		p_sockaddr(sa, 0, 18);
    307  1.1  cgd 	}
    308  1.1  cgd 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
    309  1.1  cgd 	putchar('\n');
    310  1.1  cgd }
    311  1.1  cgd 
    312  1.1  cgd p_sockaddr(sa, flags, width)
    313  1.1  cgd struct sockaddr *sa;
    314  1.1  cgd int flags, width;
    315  1.1  cgd {
    316  1.1  cgd 	char format[20], workbuf[128], *cp, *cplim;
    317  1.1  cgd 	register char *cpout;
    318  1.1  cgd 
    319  1.1  cgd 	switch(sa->sa_family) {
    320  1.1  cgd 	case AF_INET:
    321  1.1  cgd 	    {
    322  1.1  cgd 		register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    323  1.1  cgd 
    324  1.1  cgd 		cp = (sin->sin_addr.s_addr == 0) ? "default" :
    325  1.1  cgd 		      ((flags & RTF_HOST) ?
    326  1.1  cgd 			routename(sin->sin_addr) : netname(sin->sin_addr, 0L));
    327  1.1  cgd 	    }
    328  1.1  cgd 		break;
    329  1.1  cgd 
    330  1.1  cgd #ifdef NS
    331  1.1  cgd 	case AF_NS:
    332  1.1  cgd 		cp = ns_print((struct sockaddr_ns *)sa);
    333  1.1  cgd 		break;
    334  1.1  cgd #endif
    335  1.1  cgd 
    336  1.1  cgd 	default:
    337  1.1  cgd 	    {
    338  1.1  cgd 		register u_short *s = ((u_short *)sa->sa_data), *slim;
    339  1.1  cgd 
    340  1.1  cgd 		slim = (u_short *) sa + ((sa->sa_len + sizeof(u_short) - 1) /
    341  1.1  cgd 		    sizeof(u_short));
    342  1.1  cgd 		cp = workbuf;
    343  1.1  cgd 		cplim = cp + sizeof(workbuf) - 6;
    344  1.1  cgd 		cp += sprintf(cp, "(%d)", sa->sa_family);
    345  1.1  cgd 		while (s < slim && cp < cplim)
    346  1.1  cgd 			cp += sprintf(cp, " %x", *s++);
    347  1.1  cgd 		cp = workbuf;
    348  1.1  cgd 	    }
    349  1.1  cgd 	}
    350  1.1  cgd 	if (width < 0 )
    351  1.1  cgd 		printf("%s ", cp);
    352  1.1  cgd 	else {
    353  1.1  cgd 		if (nflag)
    354  1.1  cgd 			printf("%-*s ", width, cp);
    355  1.1  cgd 		else
    356  1.1  cgd 			printf("%-*.*s ", width, width, cp);
    357  1.1  cgd 	}
    358  1.1  cgd }
    359  1.1  cgd 
    360  1.1  cgd p_flags(f, format)
    361  1.1  cgd register int f;
    362  1.1  cgd char *format;
    363  1.1  cgd {
    364  1.1  cgd 	char name[33], *flags;
    365  1.1  cgd 	register struct bits *p = bits;
    366  1.1  cgd 	for (flags = name; p->b_mask; p++)
    367  1.1  cgd 		if (p->b_mask & f)
    368  1.1  cgd 			*flags++ = p->b_val;
    369  1.1  cgd 	*flags = '\0';
    370  1.1  cgd 	printf(format, name);
    371  1.1  cgd }
    372  1.1  cgd 
    373  1.1  cgd p_rtentry(rt)
    374  1.1  cgd register struct rtentry *rt;
    375  1.1  cgd {
    376  1.1  cgd 	char name[16];
    377  1.1  cgd 	register struct sockaddr *sa;
    378  1.1  cgd 	struct ifnet ifnet;
    379  1.1  cgd 
    380  1.1  cgd 	p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, 16);
    381  1.1  cgd 	p_sockaddr(kgetsa(rt->rt_gateway), 0, 18);
    382  1.1  cgd 	p_flags(rt->rt_flags, "%-6.6s ");
    383  1.1  cgd 	printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
    384  1.1  cgd 	if (rt->rt_ifp == 0) {
    385  1.1  cgd 		putchar('\n');
    386  1.1  cgd 		return;
    387  1.1  cgd 	}
    388  1.1  cgd 	kget(rt->rt_ifp, ifnet);
    389  1.1  cgd 	kvm_read((off_t)ifnet.if_name, name, 16);
    390  1.1  cgd 	printf(" %.15s%d%s", name, ifnet.if_unit,
    391  1.1  cgd 		rt->rt_nodes[0].rn_dupedkey ? " =>\n" : "\n");
    392  1.1  cgd }
    393  1.1  cgd 
    394  1.1  cgd p_ortentry(rt)
    395  1.1  cgd register struct ortentry *rt;
    396  1.1  cgd {
    397  1.1  cgd 	char name[16], *flags;
    398  1.1  cgd 	register struct bits *p;
    399  1.1  cgd 	register struct sockaddr_in *sin;
    400  1.1  cgd 	struct ifnet ifnet;
    401  1.1  cgd 
    402  1.1  cgd 	p_sockaddr(&rt->rt_dst, rt->rt_flags, 16);
    403  1.1  cgd 	p_sockaddr(&rt->rt_gateway, 0, 18);
    404  1.1  cgd 	p_flags(rt->rt_flags, "%-6.6s ");
    405  1.1  cgd 	printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
    406  1.1  cgd 	if (rt->rt_ifp == 0) {
    407  1.1  cgd 		putchar('\n');
    408  1.1  cgd 		return;
    409  1.1  cgd 	}
    410  1.1  cgd 	kget(rt->rt_ifp, ifnet);
    411  1.1  cgd 	kvm_read((off_t)ifnet.if_name, name, 16);
    412  1.1  cgd 	printf(" %.15s%d\n", name, ifnet.if_unit);
    413  1.1  cgd }
    414  1.1  cgd 
    415  1.1  cgd char *
    416  1.1  cgd routename(in)
    417  1.1  cgd 	struct in_addr in;
    418  1.1  cgd {
    419  1.1  cgd 	register char *cp;
    420  1.1  cgd 	static char line[MAXHOSTNAMELEN + 1];
    421  1.1  cgd 	struct hostent *hp;
    422  1.1  cgd 	static char domain[MAXHOSTNAMELEN + 1];
    423  1.1  cgd 	static int first = 1;
    424  1.1  cgd 	char *index();
    425  1.1  cgd 
    426  1.1  cgd 	if (first) {
    427  1.1  cgd 		first = 0;
    428  1.1  cgd 		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
    429  1.1  cgd 		    (cp = index(domain, '.')))
    430  1.1  cgd 			(void) strcpy(domain, cp + 1);
    431  1.1  cgd 		else
    432  1.1  cgd 			domain[0] = 0;
    433  1.1  cgd 	}
    434  1.1  cgd 	cp = 0;
    435  1.1  cgd 	if (!nflag) {
    436  1.1  cgd 		hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
    437  1.1  cgd 			AF_INET);
    438  1.1  cgd 		if (hp) {
    439  1.1  cgd 			if ((cp = index(hp->h_name, '.')) &&
    440  1.1  cgd 			    !strcmp(cp + 1, domain))
    441  1.1  cgd 				*cp = 0;
    442  1.1  cgd 			cp = hp->h_name;
    443  1.1  cgd 		}
    444  1.1  cgd 	}
    445  1.1  cgd 	if (cp)
    446  1.1  cgd 		strncpy(line, cp, sizeof(line) - 1);
    447  1.1  cgd 	else {
    448  1.1  cgd #define C(x)	((x) & 0xff)
    449  1.1  cgd 		in.s_addr = ntohl(in.s_addr);
    450  1.1  cgd 		sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
    451  1.1  cgd 			C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
    452  1.1  cgd 	}
    453  1.1  cgd 	return (line);
    454  1.1  cgd }
    455  1.1  cgd 
    456  1.1  cgd /*
    457  1.1  cgd  * Return the name of the network whose address is given.
    458  1.1  cgd  * The address is assumed to be that of a net or subnet, not a host.
    459  1.1  cgd  */
    460  1.1  cgd char *
    461  1.1  cgd netname(in, mask)
    462  1.1  cgd 	struct in_addr in;
    463  1.1  cgd 	u_long mask;
    464  1.1  cgd {
    465  1.1  cgd 	char *cp = 0;
    466  1.1  cgd 	static char line[MAXHOSTNAMELEN + 1];
    467  1.1  cgd 	struct netent *np = 0;
    468  1.1  cgd 	u_long net;
    469  1.1  cgd 	register i;
    470  1.1  cgd 	int subnetshift;
    471  1.1  cgd 
    472  1.1  cgd 	i = ntohl(in.s_addr);
    473  1.1  cgd 	if (!nflag && i) {
    474  1.1  cgd 		if (mask == 0) {
    475  1.1  cgd 			if (IN_CLASSA(i)) {
    476  1.1  cgd 				mask = IN_CLASSA_NET;
    477  1.1  cgd 				subnetshift = 8;
    478  1.1  cgd 			} else if (IN_CLASSB(i)) {
    479  1.1  cgd 				mask = IN_CLASSB_NET;
    480  1.1  cgd 				subnetshift = 8;
    481  1.1  cgd 			} else {
    482  1.1  cgd 				mask = IN_CLASSC_NET;
    483  1.1  cgd 				subnetshift = 4;
    484  1.1  cgd 			}
    485  1.1  cgd 			/*
    486  1.1  cgd 			 * If there are more bits than the standard mask
    487  1.1  cgd 			 * would suggest, subnets must be in use.
    488  1.1  cgd 			 * Guess at the subnet mask, assuming reasonable
    489  1.1  cgd 			 * width subnet fields.
    490  1.1  cgd 			 */
    491  1.1  cgd 			while (i &~ mask)
    492  1.1  cgd 				mask = (long)mask >> subnetshift;
    493  1.1  cgd 		}
    494  1.1  cgd 		net = i & mask;
    495  1.1  cgd 		while ((mask & 1) == 0)
    496  1.1  cgd 			mask >>= 1, net >>= 1;
    497  1.1  cgd 		np = getnetbyaddr(net, AF_INET);
    498  1.1  cgd 		if (np)
    499  1.1  cgd 			cp = np->n_name;
    500  1.1  cgd 	}
    501  1.1  cgd 	if (cp)
    502  1.1  cgd 		strncpy(line, cp, sizeof(line) - 1);
    503  1.1  cgd 	else if ((i & 0xffffff) == 0)
    504  1.1  cgd 		sprintf(line, "%u", C(i >> 24));
    505  1.1  cgd 	else if ((i & 0xffff) == 0)
    506  1.1  cgd 		sprintf(line, "%u.%u", C(i >> 24) , C(i >> 16));
    507  1.1  cgd 	else if ((i & 0xff) == 0)
    508  1.1  cgd 		sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8));
    509  1.1  cgd 	else
    510  1.1  cgd 		sprintf(line, "%u.%u.%u.%u", C(i >> 24),
    511  1.1  cgd 			C(i >> 16), C(i >> 8), C(i));
    512  1.1  cgd 	return (line);
    513  1.1  cgd }
    514  1.1  cgd 
    515  1.1  cgd /*
    516  1.1  cgd  * Print routing statistics
    517  1.1  cgd  */
    518  1.1  cgd rt_stats(off)
    519  1.1  cgd 	off_t off;
    520  1.1  cgd {
    521  1.1  cgd 	struct rtstat rtstat;
    522  1.1  cgd 
    523  1.1  cgd 	if (off == 0) {
    524  1.1  cgd 		printf("rtstat: symbol not in namelist\n");
    525  1.1  cgd 		return;
    526  1.1  cgd 	}
    527  1.1  cgd 	kvm_read(off, (char *)&rtstat, sizeof (rtstat));
    528  1.1  cgd 	printf("routing:\n");
    529  1.1  cgd 	printf("\t%u bad routing redirect%s\n",
    530  1.1  cgd 		rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
    531  1.1  cgd 	printf("\t%u dynamically created route%s\n",
    532  1.1  cgd 		rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
    533  1.1  cgd 	printf("\t%u new gateway%s due to redirects\n",
    534  1.1  cgd 		rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
    535  1.1  cgd 	printf("\t%u destination%s found unreachable\n",
    536  1.1  cgd 		rtstat.rts_unreach, plural(rtstat.rts_unreach));
    537  1.1  cgd 	printf("\t%u use%s of a wildcard route\n",
    538  1.1  cgd 		rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
    539  1.1  cgd }
    540  1.1  cgd #ifdef NS
    541  1.1  cgd short ns_nullh[] = {0,0,0};
    542  1.1  cgd short ns_bh[] = {-1,-1,-1};
    543  1.1  cgd 
    544  1.1  cgd char *
    545  1.1  cgd ns_print(sns)
    546  1.1  cgd struct sockaddr_ns *sns;
    547  1.1  cgd {
    548  1.1  cgd 	struct ns_addr work;
    549  1.1  cgd 	union { union ns_net net_e; u_long long_e; } net;
    550  1.1  cgd 	u_short port;
    551  1.1  cgd 	static char mybuf[50], cport[10], chost[25];
    552  1.1  cgd 	char *host = "";
    553  1.1  cgd 	register char *p; register u_char *q;
    554  1.1  cgd 
    555  1.1  cgd 	work = sns->sns_addr;
    556  1.1  cgd 	port = ntohs(work.x_port);
    557  1.1  cgd 	work.x_port = 0;
    558  1.1  cgd 	net.net_e  = work.x_net;
    559  1.1  cgd 	if (ns_nullhost(work) && net.long_e == 0) {
    560  1.1  cgd 		if (port ) {
    561  1.1  cgd 			sprintf(mybuf, "*.%xH", port);
    562  1.1  cgd 			upHex(mybuf);
    563  1.1  cgd 		} else
    564  1.1  cgd 			sprintf(mybuf, "*.*");
    565  1.1  cgd 		return (mybuf);
    566  1.1  cgd 	}
    567  1.1  cgd 
    568  1.1  cgd 	if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
    569  1.1  cgd 		host = "any";
    570  1.1  cgd 	} else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
    571  1.1  cgd 		host = "*";
    572  1.1  cgd 	} else {
    573  1.1  cgd 		q = work.x_host.c_host;
    574  1.1  cgd 		sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
    575  1.1  cgd 			q[0], q[1], q[2], q[3], q[4], q[5]);
    576  1.1  cgd 		for (p = chost; *p == '0' && p < chost + 12; p++);
    577  1.1  cgd 		host = p;
    578  1.1  cgd 	}
    579  1.1  cgd 	if (port)
    580  1.1  cgd 		sprintf(cport, ".%xH", htons(port));
    581  1.1  cgd 	else
    582  1.1  cgd 		*cport = 0;
    583  1.1  cgd 
    584  1.1  cgd 	sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
    585  1.1  cgd 	upHex(mybuf);
    586  1.1  cgd 	return(mybuf);
    587  1.1  cgd }
    588  1.1  cgd 
    589  1.1  cgd char *
    590  1.1  cgd ns_phost(sns)
    591  1.1  cgd struct sockaddr_ns *sns;
    592  1.1  cgd {
    593  1.1  cgd 	struct sockaddr_ns work;
    594  1.1  cgd 	static union ns_net ns_zeronet;
    595  1.1  cgd 	char *p;
    596  1.1  cgd 
    597  1.1  cgd 	work = *sns;
    598  1.1  cgd 	work.sns_addr.x_port = 0;
    599  1.1  cgd 	work.sns_addr.x_net = ns_zeronet;
    600  1.1  cgd 
    601  1.1  cgd 	p = ns_print(&work);
    602  1.1  cgd 	if (strncmp("0H.", p, 3) == 0) p += 3;
    603  1.1  cgd 	return(p);
    604  1.1  cgd }
    605  1.1  cgd upHex(p0)
    606  1.1  cgd char *p0;
    607  1.1  cgd {
    608  1.1  cgd 	register char *p = p0;
    609  1.1  cgd 	for (; *p; p++) switch (*p) {
    610  1.1  cgd 
    611  1.1  cgd 	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
    612  1.1  cgd 		*p += ('A' - 'a');
    613  1.1  cgd 	}
    614  1.1  cgd }
    615  1.1  cgd #endif /* NS */
    616