xref: /openbsd-src/usr.sbin/dvmrpctl/parser.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: parser.c,v 1.4 2009/11/13 20:09:54 jsg Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005, 2006 Esben Norby <norby@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "dvmrpd.h"
32 #include "parser.h"
33 
34 enum token_type {
35 	NOTOKEN,
36 	ENDTOKEN,
37 	KEYWORD,
38 	ADDRESS,
39 	FLAG,
40 	PREFIX,
41 	IFNAME
42 };
43 
44 struct token {
45 	enum token_type		 type;
46 	const char		*keyword;
47 	int			 value;
48 	const struct token	*next;
49 };
50 
51 static const struct token t_main[];
52 static const struct token t_show[];
53 static const struct token t_show_iface[];
54 static const struct token t_show_nbr[];
55 static const struct token t_show_mfc[];
56 static const struct token t_show_rib[];
57 static const struct token t_show_fib[];
58 static const struct token t_log[];
59 
60 static const struct token t_main[] = {
61 /*	{KEYWORD,	"reload",	RELOAD,		NULL}, */
62 	{KEYWORD,	"show",		SHOW,		t_show},
63 	{KEYWORD,	"log",		NONE,		t_log},
64 	{ENDTOKEN,	"",		NONE,		NULL}
65 };
66 
67 static const struct token t_show[] = {
68 	{NOTOKEN,	"",		NONE,		NULL},
69 	{KEYWORD,	"igmp",		SHOW_IGMP,	NULL},
70 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
71 	{KEYWORD,	"mfc",		SHOW_MFC,	t_show_mfc},
72 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
73 	{KEYWORD,	"rib",		SHOW_RIB,	t_show_rib},
74 	{KEYWORD,	"summary",	SHOW_SUM,	NULL},
75 	{ENDTOKEN,	"",		NONE,		NULL}
76 };
77 
78 static const struct token t_show_iface[] = {
79 	{NOTOKEN,	"",		NONE,		NULL},
80 	{KEYWORD,	"detail",	SHOW_IFACE_DTAIL,NULL},
81 	{IFNAME,	"",		SHOW_IFACE_DTAIL,NULL},
82 	{ENDTOKEN,	"",		NONE,		NULL}
83 };
84 
85 static const struct token t_show_mfc[] = {
86 	{NOTOKEN,	"",		NONE,		NULL},
87 	{KEYWORD,	"detail",	SHOW_MFC_DTAIL,	NULL},
88 	{ENDTOKEN,	"",		NONE,		NULL}
89 };
90 
91 static const struct token t_show_nbr[] = {
92 	{NOTOKEN,	"",		NONE,		NULL},
93 	{KEYWORD,	"detail",	SHOW_NBR_DTAIL,	NULL},
94 	{ENDTOKEN,	"",		NONE,		NULL}
95 };
96 
97 static const struct token t_show_rib[] = {
98 	{NOTOKEN,	"",		NONE,		NULL},
99 	{KEYWORD,	"detail",	SHOW_RIB_DTAIL,	NULL},
100 	{ENDTOKEN,	"",		NONE,		NULL}
101 };
102 
103 static const struct token t_log[] = {
104 	{KEYWORD,	"verbose",	LOG_VERBOSE,	NULL},
105 	{KEYWORD,	"brief",	LOG_BRIEF,	NULL},
106 	{ENDTOKEN,	"",		NONE,		NULL}
107 };
108 
109 
110 static struct parse_result	res;
111 
112 struct parse_result *
113 parse(int argc, char *argv[])
114 {
115 	const struct token	*table = t_main;
116 	const struct token	*match;
117 
118 	bzero(&res, sizeof(res));
119 
120 	while (argc >= 0) {
121 		if ((match = match_token(argv[0], table)) == NULL) {
122 			fprintf(stderr, "valid commands/args:\n");
123 			show_valid_args(table);
124 			return (NULL);
125 		}
126 
127 		argc--;
128 		argv++;
129 
130 		if (match->type == NOTOKEN || match->next == NULL)
131 			break;
132 
133 		table = match->next;
134 	}
135 
136 	if (argc > 0) {
137 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
138 		return (NULL);
139 	}
140 
141 	return (&res);
142 }
143 
144 const struct token *
145 match_token(const char *word, const struct token *table)
146 {
147 	u_int			 i, match;
148 	const struct token	*t = NULL;
149 
150 	match = 0;
151 
152 	for (i = 0; table[i].type != ENDTOKEN; i++) {
153 		switch (table[i].type) {
154 		case NOTOKEN:
155 			if (word == NULL || strlen(word) == 0) {
156 				match++;
157 				t = &table[i];
158 			}
159 			break;
160 		case KEYWORD:
161 			if (word != NULL && strncmp(word, table[i].keyword,
162 			    strlen(word)) == 0) {
163 				match++;
164 				t = &table[i];
165 				if (t->value)
166 					res.action = t->value;
167 			}
168 			break;
169 		case FLAG:
170 			if (word != NULL && strncmp(word, table[i].keyword,
171 			    strlen(word)) == 0) {
172 				match++;
173 				t = &table[i];
174 				res.flags |= t->value;
175 			}
176 			break;
177 		case ADDRESS:
178 			if (parse_addr(word, &res.addr)) {
179 				match++;
180 				t = &table[i];
181 				if (t->value)
182 					res.action = t->value;
183 			}
184 			break;
185 		case PREFIX:
186 			if (parse_prefix(word, &res.addr, &res.prefixlen)) {
187 				match++;
188 				t = &table[i];
189 				if (t->value)
190 					res.action = t->value;
191 			}
192 			break;
193 		case IFNAME:
194 			if (!match && word != NULL && strlen(word) > 0) {
195 				if (strlcpy(res.ifname, word,
196 				    sizeof(res.ifname)) >=
197 				    sizeof(res.ifname))
198 					err(1, "interface name too long");
199 				match++;
200 				t = &table[i];
201 				if (t->value)
202 					res.action = t->value;
203 			}
204 			break;
205 
206 		case ENDTOKEN:
207 			break;
208 		}
209 	}
210 
211 	if (match != 1) {
212 		if (word == NULL)
213 			fprintf(stderr, "missing argument:\n");
214 		else if (match > 1)
215 			fprintf(stderr, "ambiguous argument: %s\n", word);
216 		else if (match < 1)
217 			fprintf(stderr, "unknown argument: %s\n", word);
218 		return (NULL);
219 	}
220 
221 	return (t);
222 }
223 
224 void
225 show_valid_args(const struct token *table)
226 {
227 	int	i;
228 
229 	for (i = 0; table[i].type != ENDTOKEN; i++) {
230 		switch (table[i].type) {
231 		case NOTOKEN:
232 			fprintf(stderr, "  <cr>\n");
233 			break;
234 		case KEYWORD:
235 		case FLAG:
236 			fprintf(stderr, "  %s\n", table[i].keyword);
237 			break;
238 		case ADDRESS:
239 			fprintf(stderr, "  <address>\n");
240 			break;
241 		case PREFIX:
242 			fprintf(stderr, "  <address>[/<len>]\n");
243 			break;
244 		case IFNAME:
245 			fprintf(stderr, "  <interface>\n");
246 		case ENDTOKEN:
247 			break;
248 		}
249 	}
250 }
251 
252 int
253 parse_addr(const char *word, struct in_addr *addr)
254 {
255 	struct in_addr	ina;
256 
257 	if (word == NULL)
258 		return (0);
259 
260 	bzero(addr, sizeof(struct in_addr));
261 	bzero(&ina, sizeof(ina));
262 
263 	if (inet_pton(AF_INET, word, &ina)) {
264 		addr->s_addr = ina.s_addr;
265 		return (1);
266 	}
267 
268 	return (0);
269 }
270 
271 int
272 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen)
273 {
274 	struct in_addr	 ina;
275 	int		 bits = 32;
276 
277 	if (word == NULL)
278 		return (0);
279 
280 	bzero(addr, sizeof(struct in_addr));
281 	bzero(&ina, sizeof(ina));
282 
283 	if (strrchr(word, '/') != NULL) {
284 		if ((bits = inet_net_pton(AF_INET, word,
285 		    &ina, sizeof(ina))) == -1)
286 			return (0);
287 		addr->s_addr = ina.s_addr & htonl(0xffffffff << (32 - bits));
288 		*prefixlen = bits;
289 		return (1);
290 	} else {
291 		*prefixlen = 32;
292 		return (parse_addr(word, addr));
293 	}
294 
295 	return (0);
296 }
297