xref: /openbsd-src/usr.sbin/ripctl/parser.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: parser.c,v 1.2 2007/01/26 10:04:56 claudio 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 
63 static const struct token t_main[] = {
64 /*	{KEYWORD,	"reload",	RELOAD,		NULL}, */
65 	{KEYWORD,	"fib",		FIB,		t_fib},
66 	{KEYWORD,	"show",		SHOW,		t_show},
67 	{ENDTOKEN,	"",		NONE,		NULL}
68 };
69 
70 static const struct token t_fib[] = {
71 	{ KEYWORD,	"couple",	FIB_COUPLE,	NULL},
72 	{ KEYWORD,	"decouple",	FIB_DECOUPLE,	NULL},
73 	{ ENDTOKEN,	"",		NONE,		NULL}
74 };
75 
76 static const struct token t_show[] = {
77 	{NOTOKEN,	"",		NONE,		NULL},
78 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
79 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
80 	{KEYWORD,	"rib",		SHOW_RIB,	t_show_rib},
81 	{KEYWORD,	"fib",		SHOW_FIB,	t_show_fib},
82 	{ENDTOKEN,	"",		NONE,		NULL}
83 };
84 
85 static const struct token t_show_iface[] = {
86 	{NOTOKEN,	"",		NONE,			NULL},
87 	{ENDTOKEN,	"",		NONE,			NULL}
88 };
89 
90 static const struct token t_show_nbr[] = {
91 	{NOTOKEN,	"",		NONE,		NULL},
92 	{ENDTOKEN,	"",		NONE,		NULL}
93 };
94 
95 static const struct token t_show_rib[] = {
96 	{NOTOKEN,	"",		NONE,		NULL},
97 	{ENDTOKEN,	"",		NONE,		NULL}
98 };
99 
100 static const struct token t_show_fib[] = {
101 	{NOTOKEN,	"",		NONE,			NULL},
102 	{KEYWORD,	"interface",	SHOW_FIB_IFACE,		t_show_iface},
103 	{FLAG,		"connected",	F_CONNECTED,		t_show_fib},
104 	{FLAG,		"static",	F_STATIC,		t_show_fib},
105 	{FLAG,		"rip",		F_RIPD_INSERTED,	t_show_fib},
106 	{ADDRESS,	"",		NONE,			NULL},
107 	{ENDTOKEN,	"",		NONE,			NULL}
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(prefixlen2mask(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 
298 /* XXX local copy from kroute.c, should go to shared file */
299 in_addr_t
300 prefixlen2mask(u_int8_t prefixlen)
301 {
302 	if (prefixlen == 0)
303 		return (0);
304 
305 	return (0xffffffff << (32 - prefixlen));
306 }
307