show.c revision 1.7 1 /* $NetBSD: show.c,v 1.7 1998/07/28 19:22:56 mycroft 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.7 1998/07/28 19:22:56 mycroft 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 case AF_APPLETALK:
229 afname = "AppleTalk";
230 break;
231 #endif /* SMALL */
232 default:
233 afname = NULL;
234 break;
235 }
236 if (afname)
237 printf("\n%s:\n", afname);
238 else
239 printf("\nProtocol Family %d:\n", af);
240 }
241
242
243 static void
244 p_sockaddr(sa, flags, width)
245 struct sockaddr *sa;
246 int flags, width;
247 {
248 char workbuf[128], *cplim;
249 char *cp = workbuf;
250 int cplen = 0, len;
251
252 switch(sa->sa_family) {
253
254 case AF_LINK:
255 {
256 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
257
258 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
259 sdl->sdl_slen == 0)
260 (void)snprintf(workbuf, sizeof workbuf, "link#%d",
261 sdl->sdl_index);
262 else switch (sdl->sdl_type) {
263 case IFT_ETHER:
264 {
265 int i;
266 u_char *lla = (u_char *)sdl->sdl_data +
267 sdl->sdl_nlen;
268
269 cplim = "";
270 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
271 len = snprintf(cp, sizeof(workbuf) - cplen,
272 "%s%x", cplim, *lla);
273 cp += len;
274 cplen += len;
275 cplim = ":";
276 }
277 cp = workbuf;
278 break;
279 }
280 default:
281 cp = link_ntoa(sdl);
282 break;
283 }
284 break;
285 }
286
287 case AF_INET:
288 {
289 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
290
291 cp = (sin->sin_addr.s_addr == 0) ? "default" :
292 ((flags & RTF_HOST) ?
293 routename(sa) : netname(sa));
294 break;
295 }
296
297 #ifndef SMALL
298 case AF_NS:
299 cp = ns_print((struct sockaddr_ns *)sa);
300 break;
301 #endif /* SMALL */
302
303 default:
304 {
305 u_char *s = (u_char *)sa->sa_data, *slim;
306
307 slim = sa->sa_len + (u_char *) sa;
308 cplim = cp + sizeof(workbuf) - 6;
309 cp += snprintf(cp, cplim - cp, "(%d)", sa->sa_family);
310 while (s < slim && cp < cplim) {
311 cp += snprintf(cp, cplim - cp, " %02x", *s++);
312 if (s < slim)
313 cp += snprintf(cp, cplim - cp, "%02x", *s++);
314 }
315 cp = workbuf;
316 }
317 }
318 if (width < 0 )
319 printf("%s ", cp);
320 else {
321 if (nflag)
322 printf("%-*s ", width, cp);
323 else
324 printf("%-*.*s ", width, width, cp);
325 }
326 }
327
328 static void
329 p_flags(f, format)
330 int f;
331 char *format;
332 {
333 char name[33], *flags;
334 const struct bits *p = bits;
335
336 for (flags = name; p->b_mask; p++)
337 if (p->b_mask & f)
338 *flags++ = p->b_val;
339 *flags = '\0';
340 printf(format, name);
341 }
342
343