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