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 $
6 * Copyright (c) 1983, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/protosw.h>
36 #include <sys/socket.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
46 #include <sys/sysctl.h>
59 * Definitions for showing gateway flags.
65 static const struct bits bits[] = {
70 { RTF_BLACKHOLE, 'B' },
72 { RTF_MODIFIED, 'M' },
73 { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
75 { RTF_XRESOLVE, 'X' },
84 static void p_rtentry(struct rt_msghdr *);
85 static int p_sockaddr(struct sockaddr *, int, int);
86 static void p_flags(int, const char *);
87 static void pr_rthdr(void);
88 static void pr_family(int);
91 * Print routing tables.
94 show(int argc, char *argv[])
96 struct rt_msghdr *rtm;
97 char *buf = NULL, *next, *lim = NULL;
105 if (argc == 2 && **argv == '-') {
106 switch (keyword(*argv + 1)) {
135 mib[4] = NET_RT_DUMP;
143 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) {
144 perror("route-sysctl-estimate");
148 if ((buf = malloc(needed)) == NULL) {
149 printf("out of space\n");
152 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) {
153 perror("sysctl of routing table");
159 printf("Routing tables\n");
162 for (next = buf; next < lim; next += rtm->rtm_msglen) {
163 rtm = (struct rt_msghdr *)next;
164 sa = (struct sockaddr *)(rtm + 1);
165 if (af != 0 && sa->sa_family != af)
173 /* column widths; each followed by one space */
174 #define WID_DST (nflag ? 20 : 32) /* destination column width */
175 #define WID_GW (nflag ? 20 : 32) /* gateway column width */
178 * Print header for routing table columns.
183 printf("%-*.*s %-*.*s %-6.6s\n",
184 WID_DST, WID_DST, "Destination",
185 WID_GW, WID_GW, "Gateway",
190 * Print a routing table entry.
193 p_rtentry(struct rt_msghdr *rtm)
195 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
197 static int masks_done, banner_printed;
200 int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
204 /* for the moment, netmasks are skipped over */
205 if (!banner_printed) {
206 printf("Netmasks:\n");
209 if (masks_done == 0) {
210 if (rtm->rtm_addrs != RTA_DST) {
224 * Print the information. If wflag is set p_sockaddr() can return
225 * a wider width then specified and we try to fit the second
226 * address in any remaining space so the flags still lines up.
228 if (rtm->rtm_addrs == RTA_DST) {
229 p_sockaddr(sa, 0, WID_DST + WID_GW + 2);
231 width = p_sockaddr(sa, rtm->rtm_flags, WID_DST);
232 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) + (char *)sa);
233 p_sockaddr(sa, 0, WID_GW + WID_DST - width);
235 p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
240 * Print address family header before a section of the routing table.
253 afname = "Internet6";
267 printf("\n%s:\n", afname);
269 printf("\nProtocol Family %d:\n", af);
273 p_sockaddr(struct sockaddr *sa, int flags, int width)
278 int len = sizeof(workbuf);
281 switch(sa->sa_family) {
285 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
287 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
288 sdl->sdl_slen == 0) {
289 snprintf(workbuf, sizeof(workbuf),
290 "link#%d", sdl->sdl_index);
292 switch (sdl->sdl_type) {
298 u_char *lla = (u_char *)sdl->sdl_data +
302 for (i = 0; i < sdl->sdl_alen; i++, lla++) {
303 snprintf(cp, len, "%s%02x",
308 break; /* overflow */
324 struct sockaddr_in *in = (struct sockaddr_in *)sa;
326 if (in->sin_addr.s_addr == 0) {
327 /* cp points to workbuf */
328 strncpy(cp, "default", len);
330 cp = (flags & RTF_HOST) ? routename(sa) : netname(sa);
337 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
339 if (IN6_IS_ADDR_UNSPECIFIED(&in6->sin6_addr)) {
340 /* cp points to workbuf */
341 strncpy(cp, "default", len);
343 cp = ((flags & RTF_HOST) ? routename(sa)
346 /* make sure numeric address is not truncated */
347 if (strchr(cp, ':') != NULL &&
348 (width < 0 || strlen(cp) > (size_t)width))
356 u_char *s = (u_char *)sa->sa_data, *slim;
358 slim = sa->sa_len + (u_char *) sa;
359 cplim = cp + sizeof(workbuf) - 6;
360 snprintf(cp, len, "(%d)", sa->sa_family);
365 break; /* overflow */
367 while (s < slim && cp < cplim) {
368 snprintf(cp, len, " %02x", *s++);
372 break; /* overflow */
374 snprintf(cp, len, "%02x", *s++);
378 break; /* overflow */
385 count = printf("%s ", cp);
388 count = printf("%-*s ", width, cp);
390 count = printf("%-*.*s ", width, width, cp);
396 p_flags(int f, const char *format)
398 char name[33], *flags;
399 const struct bits *p = bits;
401 for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++) {
406 printf(format, name);