xref: /openbsd/usr.sbin/ripctl/parser.c (revision 17df1aa7)
1 /*	$OpenBSD: parser.c,v 1.4 2009/11/13 20:09:54 jsg 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 struct parse_result	res;
119 
120 struct parse_result *
121 parse(int argc, char *argv[])
122 {
123 	const struct token	*table = t_main;
124 	const struct token	*match;
125 
126 	bzero(&res, sizeof(res));
127 
128 	while (argc >= 0) {
129 		if ((match = match_token(argv[0], table)) == NULL) {
130 			fprintf(stderr, "valid commands/args:\n");
131 			show_valid_args(table);
132 			return (NULL);
133 		}
134 
135 		argc--;
136 		argv++;
137 
138 		if (match->type == NOTOKEN || match->next == NULL)
139 			break;
140 
141 		table = match->next;
142 	}
143 
144 	if (argc > 0) {
145 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
146 		return (NULL);
147 	}
148 
149 	return (&res);
150 }
151 
152 const struct token *
153 match_token(const char *word, const struct token *table)
154 {
155 	u_int			 i, match;
156 	const struct token	*t = NULL;
157 
158 	match = 0;
159 
160 	for (i = 0; table[i].type != ENDTOKEN; i++) {
161 		switch (table[i].type) {
162 		case NOTOKEN:
163 			if (word == NULL || strlen(word) == 0) {
164 				match++;
165 				t = &table[i];
166 			}
167 			break;
168 		case KEYWORD:
169 			if (word != NULL && strncmp(word, table[i].keyword,
170 			    strlen(word)) == 0) {
171 				match++;
172 				t = &table[i];
173 				if (t->value)
174 					res.action = t->value;
175 			}
176 			break;
177 		case FLAG:
178 			if (word != NULL && strncmp(word, table[i].keyword,
179 			    strlen(word)) == 0) {
180 				match++;
181 				t = &table[i];
182 				res.flags |= t->value;
183 			}
184 			break;
185 		case ADDRESS:
186 			if (parse_addr(word, &res.addr)) {
187 				match++;
188 				t = &table[i];
189 				if (t->value)
190 					res.action = t->value;
191 			}
192 			break;
193 		case PREFIX:
194 			if (parse_prefix(word, &res.addr, &res.prefixlen)) {
195 				match++;
196 				t = &table[i];
197 				if (t->value)
198 					res.action = t->value;
199 			}
200 			break;
201 		case IFNAME:
202 			if (!match && word != NULL && strlen(word) > 0) {
203 				if (strlcpy(res.ifname, word,
204 				    sizeof(res.ifname)) >=
205 				    sizeof(res.ifname))
206 					err(1, "interface name too long");
207 				match++;
208 				t = &table[i];
209 				if (t->value)
210 					res.action = t->value;
211 			}
212 			break;
213 
214 		case ENDTOKEN:
215 			break;
216 		}
217 	}
218 
219 	if (match != 1) {
220 		if (word == NULL)
221 			fprintf(stderr, "missing argument:\n");
222 		else if (match > 1)
223 			fprintf(stderr, "ambiguous argument: %s\n", word);
224 		else if (match < 1)
225 			fprintf(stderr, "unknown argument: %s\n", word);
226 		return (NULL);
227 	}
228 
229 	return (t);
230 }
231 
232 void
233 show_valid_args(const struct token *table)
234 {
235 	int	i;
236 
237 	for (i = 0; table[i].type != ENDTOKEN; i++) {
238 		switch (table[i].type) {
239 		case NOTOKEN:
240 			fprintf(stderr, "  <cr>\n");
241 			break;
242 		case KEYWORD:
243 		case FLAG:
244 			fprintf(stderr, "  %s\n", table[i].keyword);
245 			break;
246 		case ADDRESS:
247 			fprintf(stderr, "  <address>\n");
248 			break;
249 		case PREFIX:
250 			fprintf(stderr, "  <address>[/<len>]\n");
251 			break;
252 		case IFNAME:
253 			fprintf(stderr, "  <interface>\n");
254 		case ENDTOKEN:
255 			break;
256 		}
257 	}
258 }
259 
260 int
261 parse_addr(const char *word, struct in_addr *addr)
262 {
263 	struct in_addr	ina;
264 
265 	if (word == NULL)
266 		return (0);
267 
268 	bzero(addr, sizeof(struct in_addr));
269 	bzero(&ina, sizeof(ina));
270 
271 	if (inet_pton(AF_INET, word, &ina)) {
272 		addr->s_addr = ina.s_addr;
273 		return (1);
274 	}
275 
276 	return (0);
277 }
278 
279 int
280 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen)
281 {
282 	struct in_addr	 ina;
283 	int		 bits = 32;
284 
285 	if (word == NULL)
286 		return (0);
287 
288 	bzero(addr, sizeof(struct in_addr));
289 	bzero(&ina, sizeof(ina));
290 
291 	if (strrchr(word, '/') != NULL) {
292 		if ((bits = inet_net_pton(AF_INET, word,
293 		    &ina, sizeof(ina))) == -1)
294 			return (0);
295 		addr->s_addr = ina.s_addr & htonl(prefixlen2mask(bits));
296 		*prefixlen = bits;
297 		return (1);
298 	} else {
299 		*prefixlen = 32;
300 		return (parse_addr(word, addr));
301 	}
302 
303 	return (0);
304 }
305 
306 /* XXX local copy from kroute.c, should go to shared file */
307 in_addr_t
308 prefixlen2mask(u_int8_t prefixlen)
309 {
310 	if (prefixlen == 0)
311 		return (0);
312 
313 	return (0xffffffff << (32 - prefixlen));
314 }
315