2 * $OpenBSD: show.c,v 1.26 2003/08/26 08:33:12 itojun Exp $
3 * $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $
4 * $DragonFly: src/sbin/route/show.c,v 1.3 2004/03/23 18:25:51 dillon Exp $
7 * Copyright (c) 1983, 1988, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include <sys/sysctl.h>
61 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
62 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
65 * Definitions for showing gateway flags.
71 static const struct bits bits[] = {
76 { RTF_BLACKHOLE, 'B' },
78 { RTF_MODIFIED, 'M' },
79 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
81 { RTF_XRESOLVE, 'X' },
90 static void p_rtentry(struct rt_msghdr *);
91 static int p_sockaddr(struct sockaddr *, int, int);
92 static void p_flags(int, char *);
93 static void pr_rthdr(void);
94 static void pr_family(int);
98 void show(int argc, char *argv[]);
101 * Print routing tables.
104 show(int argc, char *argv[])
106 struct rt_msghdr *rtm;
107 char *buf = NULL, *next, *lim = NULL;
114 if (argc == 2 && **argv == '-')
115 switch (keyword(*argv + 1)) {
146 mib[4] = NET_RT_DUMP;
148 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
149 perror("route-sysctl-estimate");
153 if ((buf = malloc(needed)) == 0) {
154 printf("out of space\n");
157 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
158 perror("sysctl of routing table");
164 printf("Routing tables\n");
167 for (next = buf; next < lim; next += rtm->rtm_msglen) {
168 rtm = (struct rt_msghdr *)next;
169 sa = (struct sockaddr *)(rtm + 1);
170 if (af && sa->sa_family != af)
178 /* column widths; each followed by one space */
179 #define WID_DST (nflag ? 20 : 32) /* destination column width */
180 #define WID_GW (nflag ? 20 : 32) /* gateway column width */
183 * Print header for routing table columns.
188 printf("%-*.*s %-*.*s %-6.6s\n",
189 WID_DST, WID_DST, "Destination",
190 WID_GW, WID_GW, "Gateway",
195 * Print a routing table entry.
198 p_rtentry(struct rt_msghdr *rtm)
200 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
202 static int masks_done, banner_printed;
205 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
209 /* for the moment, netmasks are skipped over */
210 if (!banner_printed) {
211 printf("Netmasks:\n");
214 if (masks_done == 0) {
215 if (rtm->rtm_addrs != RTA_DST ) {
229 * Print the information. If wflag is set p_sockaddr() can return
230 * a wider width then specified and we try to fit the second
231 * address in any remaining space so the flags still lines up.
233 if (rtm->rtm_addrs == RTA_DST) {
234 p_sockaddr(sa, 0, WID_DST + WID_GW + 2);
236 width = p_sockaddr(sa, rtm->rtm_flags, WID_DST);
237 sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
238 p_sockaddr(sa, 0, WID_GW + WID_DST - width);
240 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
245 * Print address family header before a section of the routing table.
258 afname = "Internet6";
274 afname = "AppleTalk";
281 printf("\n%s:\n", afname);
283 printf("\nProtocol Family %d:\n", af);
287 p_sockaddr(struct sockaddr *sa, int flags, int width)
289 char workbuf[128], *cplim;
291 int len = sizeof(workbuf);
294 switch(sa->sa_family) {
298 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
300 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
302 snprintf(workbuf, sizeof(workbuf),
303 "link#%d", sdl->sdl_index);
305 switch (sdl->sdl_type) {
309 u_char *lla = (u_char *)sdl->sdl_data +
313 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
314 snprintf(cp, len, "%s%x", cplim, *lla);
318 break; /* overflow */
334 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
336 if (sin->sin_addr.s_addr == 0)
339 cp = (flags & RTF_HOST) ? routename(sa) : netname(sa);
346 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
348 cp = IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "default" :
349 ((flags & RTF_HOST) ? routename(sa) : netname(sa));
350 /* make sure numeric address is not truncated */
351 if (strchr(cp, ':') != NULL && strlen(cp) > width)
358 cp = ns_print((struct sockaddr_ns *)sa);
363 u_char *s = (u_char *)sa->sa_data, *slim;
365 slim = sa->sa_len + (u_char *) sa;
366 cplim = cp + sizeof(workbuf) - 6;
367 snprintf(cp, len, "(%d)", sa->sa_family);
372 break; /* overflow */
374 while (s < slim && cp < cplim) {
375 snprintf(cp, len, " %02x", *s++);
379 break; /* overflow */
381 snprintf(cp, len, "%02x", *s++);
385 break; /* overflow */
392 count = printf("%s ", cp);
395 count = printf("%-*s ", width, cp);
397 count = printf("%-*.*s ", width, width, cp);
403 p_flags(int f, char *format)
405 char name[33], *flags;
406 const struct bits *p = bits;
408 for (flags = name; p->b_mask && flags < &name[sizeof(name-2)]; p++) {
413 printf(format, name);