Home | History | Annotate | Line # | Download | only in route
show.c revision 1.5
      1 /*	$NetBSD: show.c,v 1.5 1997/11/16 17:03:12 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     40 #else
     41 __RCSID("$NetBSD: show.c,v 1.5 1997/11/16 17:03:12 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/mbuf.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_dl.h>
     52 #include <net/if_types.h>
     53 #include <net/route.h>
     54 #include <netinet/in.h>
     55 #include <netns/ns.h>
     56 
     57 #include <sys/sysctl.h>
     58 
     59 #include <netdb.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <unistd.h>
     64 #include <err.h>
     65 
     66 #include "extern.h"
     67 
     68 /*
     69  * Definitions for showing gateway flags.
     70  */
     71 struct bits {
     72 	short	b_mask;
     73 	char	b_val;
     74 };
     75 static const struct bits bits[] = {
     76 	{ RTF_UP,	'U' },
     77 	{ RTF_GATEWAY,	'G' },
     78 	{ RTF_HOST,	'H' },
     79 	{ RTF_REJECT,	'R' },
     80 	{ RTF_DYNAMIC,	'D' },
     81 	{ RTF_MODIFIED,	'M' },
     82 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     83 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     84 	{ RTF_CLONING,	'C' },
     85 	{ RTF_XRESOLVE,	'X' },
     86 	{ RTF_LLINFO,	'L' },
     87 	{ RTF_STATIC,	'S' },
     88 	{ RTF_PROTO1,	'1' },
     89 	{ RTF_PROTO2,	'2' },
     90 	{ 0 }
     91 };
     92 
     93 static void pr_rthdr __P((void));
     94 static void p_rtentry __P((struct rt_msghdr *));
     95 static void pr_family __P((int));
     96 static void p_sockaddr __P((struct sockaddr *, int, int ));
     97 static void p_flags __P((int, char *));
     98 
     99 /*
    100  * Print routing tables.
    101  */
    102 void
    103 show(argc, argv)
    104 	int argc;
    105 	char **argv;
    106 {
    107 	size_t needed;
    108 	int mib[6];
    109 	char *buf, *next, *lim;
    110 	struct rt_msghdr *rtm;
    111 
    112 	mib[0] = CTL_NET;
    113 	mib[1] = PF_ROUTE;
    114 	mib[2] = 0;
    115 	mib[3] = 0;
    116 	mib[4] = NET_RT_DUMP;
    117 	mib[5] = 0;
    118 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)	{
    119 		perror("route-sysctl-estimate");
    120 		exit(1);
    121 	}
    122 	if ((buf = malloc(needed)) == 0)
    123 		err(1, "%s", "");
    124 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    125 		err(1, "sysctl of routing table");
    126 	lim  = buf + needed;
    127 
    128 	printf("Routing tables\n");
    129 
    130 	/* for (i = 0; i <= AF_MAX; i++) ??? */
    131 	{
    132 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    133 			rtm = (struct rt_msghdr *)next;
    134 			p_rtentry(rtm);
    135 		}
    136 	}
    137 }
    138 
    139 
    140 /* column widths; each followed by one space */
    141 #define	WID_DST		16	/* width of destination column */
    142 #define	WID_GW		18	/* width of gateway column */
    143 
    144 /*
    145  * Print header for routing table columns.
    146  */
    147 static void
    148 pr_rthdr()
    149 {
    150 
    151 	printf("%-*.*s %-*.*s %-6.6s\n",
    152 		WID_DST, WID_DST, "Destination",
    153 		WID_GW, WID_GW, "Gateway",
    154 		"Flags");
    155 }
    156 
    157 
    158 /*
    159  * Print a routing table entry.
    160  */
    161 static void
    162 p_rtentry(rtm)
    163 	struct rt_msghdr *rtm;
    164 {
    165 	struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    166 #ifdef notdef
    167 	static int masks_done, banner_printed;
    168 #endif
    169 	static int old_af;
    170 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    171 
    172 #ifdef notdef
    173 	/* for the moment, netmasks are skipped over */
    174 	if (!banner_printed) {
    175 		printf("Netmasks:\n");
    176 		banner_printed = 1;
    177 	}
    178 	if (masks_done == 0) {
    179 		if (rtm->rtm_addrs != RTA_DST ) {
    180 			masks_done = 1;
    181 			af = sa->sa_family;
    182 		}
    183 	} else
    184 #endif
    185 		af = sa->sa_family;
    186 	if (old_af != af) {
    187 		old_af = af;
    188 		pr_family(af);
    189 		pr_rthdr();
    190 	}
    191 	if (rtm->rtm_addrs == RTA_DST)
    192 		p_sockaddr(sa, 0, 36);
    193 	else {
    194 		p_sockaddr(sa, rtm->rtm_flags, 16);
    195 		if (sa->sa_len == 0)
    196 			sa->sa_len = sizeof(long);
    197 		sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
    198 		p_sockaddr(sa, 0, 18);
    199 	}
    200 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
    201 	putchar('\n');
    202 }
    203 
    204 
    205 /*
    206  * Print address family header before a section of the routing table.
    207  */
    208 static void
    209 pr_family(af)
    210 	int af;
    211 {
    212 	char *afname;
    213 
    214 	switch (af) {
    215 	case AF_INET:
    216 		afname = "Internet";
    217 		break;
    218 #ifndef SMALL
    219 	case AF_NS:
    220 		afname = "XNS";
    221 		break;
    222 	case AF_ISO:
    223 		afname = "ISO";
    224 		break;
    225 	case AF_CCITT:
    226 		afname = "X.25";
    227 		break;
    228 #endif /* SMALL */
    229 	default:
    230 		afname = NULL;
    231 		break;
    232 	}
    233 	if (afname)
    234 		printf("\n%s:\n", afname);
    235 	else
    236 		printf("\nProtocol Family %d:\n", af);
    237 }
    238 
    239 
    240 static void
    241 p_sockaddr(sa, flags, width)
    242 	struct sockaddr *sa;
    243 	int flags, width;
    244 {
    245 	char workbuf[128], *cplim;
    246 	char *cp = workbuf;
    247 	int cplen = 0, len;
    248 
    249 	switch(sa->sa_family) {
    250 
    251 	case AF_LINK:
    252 	    {
    253 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
    254 
    255 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
    256 		    sdl->sdl_slen == 0)
    257 			(void)snprintf(workbuf, sizeof workbuf, "link#%d",
    258 			    sdl->sdl_index);
    259 		else switch (sdl->sdl_type) {
    260 		case IFT_ETHER:
    261 		    {
    262 			int i;
    263 			u_char *lla = (u_char *)sdl->sdl_data +
    264 			    sdl->sdl_nlen;
    265 
    266 			cplim = "";
    267 			for (i = 0; i < sdl->sdl_alen; i++, lla++) {
    268 				len = snprintf(cp, sizeof(workbuf) - cplen,
    269 				    "%s%x", cplim, *lla);
    270 				cp += len;
    271 				cplen += len;
    272 				cplim = ":";
    273 			}
    274 			cp = workbuf;
    275 			break;
    276 		    }
    277 		default:
    278 			cp = link_ntoa(sdl);
    279 			break;
    280 		}
    281 		break;
    282 	    }
    283 
    284 	case AF_INET:
    285 	    {
    286 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    287 
    288 		cp = (sin->sin_addr.s_addr == 0) ? "default" :
    289 			((flags & RTF_HOST) ?
    290 			routename(sa) :	netname(sa));
    291 		break;
    292 	    }
    293 
    294 #ifndef SMALL
    295 	case AF_NS:
    296 		cp = ns_print((struct sockaddr_ns *)sa);
    297 		break;
    298 #endif /* SMALL */
    299 
    300 	default:
    301 	    {
    302 		u_char *s = (u_char *)sa->sa_data, *slim;
    303 
    304 		slim = sa->sa_len + (u_char *) sa;
    305 		cplim = cp + sizeof(workbuf) - 6;
    306 		cp += snprintf(cp, sizeof workbuf, "(%d)", sa->sa_family);
    307 		while (s < slim && cp < cplim) {
    308 			cp += sprintf(cp, " %02x", *s++);	/* XXX sprintf is safe */
    309 			if (s < slim)
    310 			    cp += sprintf(cp, "%02x", *s++);	/* XXX sprintf is safe */
    311 		}
    312 		cp = workbuf;
    313 	    }
    314 	}
    315 	if (width < 0 )
    316 		printf("%s ", cp);
    317 	else {
    318 		if (nflag)
    319 			printf("%-*s ", width, cp);
    320 		else
    321 			printf("%-*.*s ", width, width, cp);
    322 	}
    323 }
    324 
    325 static void
    326 p_flags(f, format)
    327 	int f;
    328 	char *format;
    329 {
    330 	char name[33], *flags;
    331 	const struct bits *p = bits;
    332 
    333 	for (flags = name; p->b_mask; p++)
    334 		if (p->b_mask & f)
    335 			*flags++ = p->b_val;
    336 	*flags = '\0';
    337 	printf(format, name);
    338 }
    339 
    340