Home | History | Annotate | Line # | Download | only in route
show.c revision 1.2
      1 /*	$NetBSD: show.c,v 1.2 1997/04/03 02:35:52 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     39 #else
     40 static char *rcsid = "$NetBSD: show.c,v 1.2 1997/04/03 02:35:52 christos Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/protosw.h>
     46 #include <sys/socket.h>
     47 #include <sys/mbuf.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_types.h>
     52 #include <net/route.h>
     53 #include <netinet/in.h>
     54 #include <netns/ns.h>
     55 
     56 #include <sys/sysctl.h>
     57 
     58 #include <netdb.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 
     64 #include "extern.h"
     65 
     66 /*
     67  * Definitions for showing gateway flags.
     68  */
     69 struct bits {
     70 	short	b_mask;
     71 	char	b_val;
     72 };
     73 static const struct bits bits[] = {
     74 	{ RTF_UP,	'U' },
     75 	{ RTF_GATEWAY,	'G' },
     76 	{ RTF_HOST,	'H' },
     77 	{ RTF_REJECT,	'R' },
     78 	{ RTF_DYNAMIC,	'D' },
     79 	{ RTF_MODIFIED,	'M' },
     80 	{ RTF_DONE,	'd' }, /* Completed -- for routing messages only */
     81 	{ RTF_MASK,	'm' }, /* Mask Present -- for routing messages only */
     82 	{ RTF_CLONING,	'C' },
     83 	{ RTF_XRESOLVE,	'X' },
     84 	{ RTF_LLINFO,	'L' },
     85 	{ RTF_STATIC,	'S' },
     86 	{ RTF_PROTO1,	'1' },
     87 	{ RTF_PROTO2,	'2' },
     88 	{ 0 }
     89 };
     90 
     91 static void pr_rthdr __P((void));
     92 static void p_rtentry __P((struct rt_msghdr *));
     93 static void pr_family __P((int));
     94 static void p_sockaddr __P((struct sockaddr *, int, int ));
     95 static void p_flags __P((int, char *));
     96 
     97 /*
     98  * Print routing tables.
     99  */
    100 void
    101 show(argc, argv)
    102 	int argc;
    103 	char **argv;
    104 {
    105 	size_t needed;
    106 	int mib[6];
    107 	char *buf, *next, *lim;
    108 	register struct rt_msghdr *rtm;
    109 
    110 	mib[0] = CTL_NET;
    111 	mib[1] = PF_ROUTE;
    112 	mib[2] = 0;
    113 	mib[3] = 0;
    114 	mib[4] = NET_RT_DUMP;
    115 	mib[5] = 0;
    116 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)	{
    117 		perror("route-sysctl-estimate");
    118 		exit(1);
    119 	}
    120 	if ((buf = malloc(needed)) == 0) {
    121 		printf("out of space\n");
    122 		exit(1);
    123 	}
    124 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    125 		perror("sysctl of routing table");
    126 		exit(1);
    127 	}
    128 	lim  = buf + needed;
    129 
    130 	printf("Routing tables\n");
    131 
    132 	/* for (i = 0; i <= AF_MAX; i++) ??? */
    133 	{
    134 		for (next = buf; next < lim; next += rtm->rtm_msglen) {
    135 			rtm = (struct rt_msghdr *)next;
    136 			p_rtentry(rtm);
    137 		}
    138 	}
    139 }
    140 
    141 
    142 /* column widths; each followed by one space */
    143 #define	WID_DST		16	/* width of destination column */
    144 #define	WID_GW		18	/* width of gateway column */
    145 
    146 /*
    147  * Print header for routing table columns.
    148  */
    149 static void
    150 pr_rthdr()
    151 {
    152 
    153 	printf("%-*.*s %-*.*s %-6.6s\n",
    154 		WID_DST, WID_DST, "Destination",
    155 		WID_GW, WID_GW, "Gateway",
    156 		"Flags");
    157 }
    158 
    159 
    160 /*
    161  * Print a routing table entry.
    162  */
    163 static void
    164 p_rtentry(rtm)
    165 	register struct rt_msghdr *rtm;
    166 {
    167 	register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
    168 #ifdef notdef
    169 	static int masks_done, banner_printed;
    170 #endif
    171 	static int old_af;
    172 	int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
    173 
    174 #ifdef notdef
    175 	/* for the moment, netmasks are skipped over */
    176 	if (!banner_printed) {
    177 		printf("Netmasks:\n");
    178 		banner_printed = 1;
    179 	}
    180 	if (masks_done == 0) {
    181 		if (rtm->rtm_addrs != RTA_DST ) {
    182 			masks_done = 1;
    183 			af = sa->sa_family;
    184 		}
    185 	} else
    186 #endif
    187 		af = sa->sa_family;
    188 	if (old_af != af) {
    189 		old_af = af;
    190 		pr_family(af);
    191 		pr_rthdr();
    192 	}
    193 	if (rtm->rtm_addrs == RTA_DST)
    194 		p_sockaddr(sa, 0, 36);
    195 	else {
    196 		p_sockaddr(sa, rtm->rtm_flags, 16);
    197 		if (sa->sa_len == 0)
    198 			sa->sa_len = sizeof(long);
    199 		sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
    200 		p_sockaddr(sa, 0, 18);
    201 	}
    202 	p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
    203 	putchar('\n');
    204 }
    205 
    206 
    207 /*
    208  * Print address family header before a section of the routing table.
    209  */
    210 static void
    211 pr_family(af)
    212 	int af;
    213 {
    214 	char *afname;
    215 
    216 	switch (af) {
    217 	case AF_INET:
    218 		afname = "Internet";
    219 		break;
    220 #ifndef SMALL
    221 	case AF_NS:
    222 		afname = "XNS";
    223 		break;
    224 	case AF_ISO:
    225 		afname = "ISO";
    226 		break;
    227 	case AF_CCITT:
    228 		afname = "X.25";
    229 		break;
    230 #endif /* SMALL */
    231 	default:
    232 		afname = NULL;
    233 		break;
    234 	}
    235 	if (afname)
    236 		printf("\n%s:\n", afname);
    237 	else
    238 		printf("\nProtocol Family %d:\n", af);
    239 }
    240 
    241 
    242 static void
    243 p_sockaddr(sa, flags, width)
    244 	struct sockaddr *sa;
    245 	int flags, width;
    246 {
    247 	char workbuf[128], *cplim;
    248 	register char *cp = workbuf;
    249 
    250 	switch(sa->sa_family) {
    251 
    252 	case AF_LINK:
    253 	    {
    254 		register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
    255 
    256 		if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
    257 		    sdl->sdl_slen == 0)
    258 			(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
    259 		else switch (sdl->sdl_type) {
    260 		case IFT_ETHER:
    261 		    {
    262 			register int i;
    263 			register 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 				cp += sprintf(cp, "%s%x", cplim, *lla);
    269 				cplim = ":";
    270 			}
    271 			cp = workbuf;
    272 			break;
    273 		    }
    274 		default:
    275 			cp = link_ntoa(sdl);
    276 			break;
    277 		}
    278 		break;
    279 	    }
    280 
    281 	case AF_INET:
    282 	    {
    283 		register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
    284 
    285 		cp = (sin->sin_addr.s_addr == 0) ? "default" :
    286 			((flags & RTF_HOST) ?
    287 			routename(sa) :	netname(sa));
    288 		break;
    289 	    }
    290 
    291 #ifndef SMALL
    292 	case AF_NS:
    293 		cp = ns_print((struct sockaddr_ns *)sa);
    294 		break;
    295 #endif /* SMALL */
    296 
    297 	default:
    298 	    {
    299 		register u_char *s = (u_char *)sa->sa_data, *slim;
    300 
    301 		slim =  sa->sa_len + (u_char *) sa;
    302 		cplim = cp + sizeof(workbuf) - 6;
    303 		cp += sprintf(cp, "(%d)", sa->sa_family);
    304 		while (s < slim && cp < cplim) {
    305 			cp += sprintf(cp, " %02x", *s++);
    306 			if (s < slim)
    307 			    cp += sprintf(cp, "%02x", *s++);
    308 		}
    309 		cp = workbuf;
    310 	    }
    311 	}
    312 	if (width < 0 )
    313 		printf("%s ", cp);
    314 	else {
    315 		if (nflag)
    316 			printf("%-*s ", width, cp);
    317 		else
    318 			printf("%-*.*s ", width, width, cp);
    319 	}
    320 }
    321 
    322 static void
    323 p_flags(f, format)
    324 	register int f;
    325 	char *format;
    326 {
    327 	char name[33], *flags;
    328 	register const struct bits *p = bits;
    329 
    330 	for (flags = name; p->b_mask; p++)
    331 		if (p->b_mask & f)
    332 			*flags++ = p->b_val;
    333 	*flags = '\0';
    334 	printf(format, name);
    335 }
    336 
    337