show.c revision 1.46 1 1.46 christos /* $NetBSD: show.c,v 1.46 2014/11/06 21:29:32 christos Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1983, 1988, 1993
5 1.1 gwr * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 gwr * may be used to endorse or promote products derived from this software
17 1.1 gwr * without specific prior written permission.
18 1.1 gwr *
19 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 gwr * SUCH DAMAGE.
30 1.1 gwr */
31 1.1 gwr
32 1.4 lukem #include <sys/cdefs.h>
33 1.1 gwr #ifndef lint
34 1.1 gwr #if 0
35 1.1 gwr static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
36 1.1 gwr #else
37 1.46 christos __RCSID("$NetBSD: show.c,v 1.46 2014/11/06 21:29:32 christos Exp $");
38 1.1 gwr #endif
39 1.1 gwr #endif /* not lint */
40 1.1 gwr
41 1.1 gwr #include <sys/param.h>
42 1.1 gwr #include <sys/protosw.h>
43 1.1 gwr #include <sys/socket.h>
44 1.1 gwr #include <sys/mbuf.h>
45 1.1 gwr
46 1.41 kefren #include <arpa/inet.h>
47 1.1 gwr #include <net/if.h>
48 1.1 gwr #include <net/if_dl.h>
49 1.1 gwr #include <net/if_types.h>
50 1.1 gwr #include <net/route.h>
51 1.1 gwr #include <netinet/in.h>
52 1.41 kefren #include <netmpls/mpls.h>
53 1.1 gwr
54 1.1 gwr #include <sys/sysctl.h>
55 1.1 gwr
56 1.1 gwr #include <netdb.h>
57 1.36 dyoung #include <stdbool.h>
58 1.1 gwr #include <stdio.h>
59 1.1 gwr #include <stdlib.h>
60 1.1 gwr #include <string.h>
61 1.1 gwr #include <unistd.h>
62 1.5 christos #include <err.h>
63 1.1 gwr
64 1.26 ginsbach #include "keywords.h"
65 1.46 christos #include "rtutil.h"
66 1.2 christos #include "extern.h"
67 1.42 pooka #include "prog_ops.h"
68 1.1 gwr
69 1.33 dyoung void
70 1.38 dyoung parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp,
71 1.36 dyoung const char **afnamep, bool nolink)
72 1.33 dyoung {
73 1.33 dyoung const char *afname = "unspec";
74 1.33 dyoung int af, flags;
75 1.33 dyoung
76 1.33 dyoung flags = 0;
77 1.33 dyoung af = AF_UNSPEC;
78 1.33 dyoung for (; argc >= 2; argc--) {
79 1.33 dyoung if (*argv[argc - 1] != '-')
80 1.33 dyoung goto bad;
81 1.33 dyoung switch (keyword(argv[argc - 1] + 1)) {
82 1.33 dyoung case K_HOST:
83 1.33 dyoung flags |= RTF_HOST;
84 1.33 dyoung break;
85 1.33 dyoung case K_LLINFO:
86 1.33 dyoung flags |= RTF_LLINFO;
87 1.33 dyoung break;
88 1.33 dyoung case K_INET:
89 1.33 dyoung af = AF_INET;
90 1.33 dyoung afname = argv[argc - 1] + 1;
91 1.33 dyoung break;
92 1.33 dyoung #ifdef INET6
93 1.33 dyoung case K_INET6:
94 1.33 dyoung af = AF_INET6;
95 1.33 dyoung afname = argv[argc - 1] + 1;
96 1.33 dyoung break;
97 1.33 dyoung #endif
98 1.33 dyoung #ifndef SMALL
99 1.33 dyoung case K_ATALK:
100 1.33 dyoung af = AF_APPLETALK;
101 1.33 dyoung afname = argv[argc - 1] + 1;
102 1.33 dyoung break;
103 1.41 kefren case K_MPLS:
104 1.41 kefren af = AF_MPLS;
105 1.41 kefren afname = argv[argc - 1] + 1;
106 1.41 kefren break;
107 1.33 dyoung #endif /* SMALL */
108 1.33 dyoung case K_LINK:
109 1.33 dyoung if (nolink)
110 1.33 dyoung goto bad;
111 1.33 dyoung af = AF_LINK;
112 1.33 dyoung afname = argv[argc - 1] + 1;
113 1.33 dyoung break;
114 1.33 dyoung default:
115 1.33 dyoung goto bad;
116 1.33 dyoung }
117 1.33 dyoung }
118 1.33 dyoung switch (argc) {
119 1.33 dyoung case 1:
120 1.33 dyoung case 0:
121 1.33 dyoung break;
122 1.33 dyoung default:
123 1.33 dyoung bad:
124 1.33 dyoung usage(argv[argc - 1]);
125 1.33 dyoung }
126 1.33 dyoung if (afnamep != NULL)
127 1.33 dyoung *afnamep = afname;
128 1.33 dyoung *afp = af;
129 1.33 dyoung *flagsp = flags;
130 1.33 dyoung }
131 1.33 dyoung
132 1.1 gwr /*
133 1.1 gwr * Print routing tables.
134 1.1 gwr */
135 1.1 gwr void
136 1.46 christos show(int argc, char *const *argv, int flags)
137 1.1 gwr {
138 1.46 christos int af, rflags;
139 1.46 christos static int interesting = RTF_UP | RTF_GATEWAY | RTF_HOST |
140 1.34 pooka RTF_REJECT | RTF_LLINFO;
141 1.1 gwr
142 1.46 christos parse_show_opts(argc, argv, &af, &rflags, NULL, true);
143 1.46 christos p_rttables(af, flags, rflags, interesting);
144 1.1 gwr }
145