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