xref: /openbsd-src/usr.sbin/ldpctl/parser.c (revision fe91619e3f3cc520135ded95e076f0829fe21c8b)
1 /*	$OpenBSD: parser.c,v 1.7 2015/07/21 05:43:46 renato 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_disc[];
58 static const struct token t_show_db[];
59 static const struct token t_show_area[];
60 static const struct token t_show_nbr[];
61 static const struct token t_show_lib[];
62 static const struct token t_show_fib[];
63 static const struct token t_log[];
64 
65 static const struct token t_main[] = {
66 	{KEYWORD,	"reload",	RELOAD,		NULL},
67 	{KEYWORD,	"fib",		FIB,		t_fib},
68 	{KEYWORD,	"show",		SHOW,		t_show},
69 	{KEYWORD,	"log",		NONE,		t_log},
70 	{ENDTOKEN,	"",		NONE,		NULL}
71 };
72 
73 static const struct token t_fib[] = {
74 	{ KEYWORD,	"couple",	FIB_COUPLE,	NULL},
75 	{ KEYWORD,	"decouple",	FIB_DECOUPLE,	NULL},
76 	{ ENDTOKEN,	"",		NONE,		NULL}
77 };
78 
79 static const struct token t_show[] = {
80 	{NOTOKEN,	"",		NONE,		NULL},
81 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
82 	{KEYWORD,	"discovery",	SHOW_DISC,	t_show_disc},
83 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
84 	{KEYWORD,	"lib",		SHOW_LIB,	t_show_lib},
85 	{KEYWORD,	"fib",		SHOW_FIB,	t_show_fib},
86 	{ENDTOKEN,	"",		NONE,		NULL}
87 };
88 
89 static const struct token t_show_iface[] = {
90 	{NOTOKEN,	"",		NONE,			NULL},
91 	{ENDTOKEN,	"",		NONE,			NULL}
92 };
93 
94 static const struct token t_show_disc[] = {
95 	{NOTOKEN,	"",		NONE,			NULL},
96 	{ENDTOKEN,	"",		NONE,			NULL}
97 };
98 
99 static const struct token t_show_nbr[] = {
100 	{NOTOKEN,	"",		NONE,		NULL},
101 	{ENDTOKEN,	"",		NONE,		NULL}
102 };
103 
104 static const struct token t_show_lib[] = {
105 	{NOTOKEN,	"",		NONE,		NULL},
106 	{ENDTOKEN,	"",		NONE,		NULL}
107 };
108 
109 static const struct token t_log[] = {
110 	{KEYWORD,	"verbose",	LOG_VERBOSE,	NULL},
111 	{KEYWORD,	"brief",	LOG_BRIEF,	NULL},
112 	{ENDTOKEN,	"",		NONE,		NULL}
113 };
114 
115 static const struct token t_show_fib[] = {
116 	{NOTOKEN,	"",		NONE,			NULL},
117 	{KEYWORD,	"interface",	SHOW_FIB_IFACE,		t_show_iface},
118 	{FLAG,		"connected",	F_CONNECTED,		t_show_fib},
119 	{FLAG,		"static",	F_STATIC,		t_show_fib},
120 	{ADDRESS,	"",		NONE,			NULL},
121 	{ENDTOKEN,	"",		NONE,			NULL}
122 };
123 
124 static const struct token *match_token(const char *, const struct token *,
125     struct parse_result *);
126 static void show_valid_args(const struct token *);
127 
128 struct parse_result *
129 parse(int argc, char *argv[])
130 {
131 	static struct parse_result	res;
132 	const struct token	*table = t_main;
133 	const struct token	*match;
134 
135 	bzero(&res, sizeof(res));
136 
137 	while (argc >= 0) {
138 		if ((match = match_token(argv[0], table, &res)) == NULL) {
139 			fprintf(stderr, "valid commands/args:\n");
140 			show_valid_args(table);
141 			return (NULL);
142 		}
143 
144 		argc--;
145 		argv++;
146 
147 		if (match->type == NOTOKEN || match->next == NULL)
148 			break;
149 
150 		table = match->next;
151 	}
152 
153 	if (argc > 0) {
154 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
155 		return (NULL);
156 	}
157 
158 	return (&res);
159 }
160 
161 static const struct token *
162 match_token(const char *word, const struct token *table,
163     struct parse_result *res)
164 {
165 	u_int			 i, match;
166 	const struct token	*t = NULL;
167 
168 	match = 0;
169 
170 	for (i = 0; table[i].type != ENDTOKEN; i++) {
171 		switch (table[i].type) {
172 		case NOTOKEN:
173 			if (word == NULL || strlen(word) == 0) {
174 				match++;
175 				t = &table[i];
176 			}
177 			break;
178 		case KEYWORD:
179 			if (word != NULL && strncmp(word, table[i].keyword,
180 			    strlen(word)) == 0) {
181 				match++;
182 				t = &table[i];
183 				if (t->value)
184 					res->action = t->value;
185 			}
186 			break;
187 		case FLAG:
188 			if (word != NULL && strncmp(word, table[i].keyword,
189 			    strlen(word)) == 0) {
190 				match++;
191 				t = &table[i];
192 				res->flags |= t->value;
193 			}
194 			break;
195 		case ADDRESS:
196 			if (parse_addr(word, &res->addr)) {
197 				match++;
198 				t = &table[i];
199 				if (t->value)
200 					res->action = t->value;
201 			}
202 			break;
203 		case PREFIX:
204 			if (parse_prefix(word, &res->addr, &res->prefixlen)) {
205 				match++;
206 				t = &table[i];
207 				if (t->value)
208 					res->action = t->value;
209 			}
210 			break;
211 		case IFNAME:
212 			if (!match && word != NULL && strlen(word) > 0) {
213 				if (strlcpy(res->ifname, word,
214 				    sizeof(res->ifname)) >=
215 				    sizeof(res->ifname))
216 					err(1, "interface name too long");
217 				match++;
218 				t = &table[i];
219 				if (t->value)
220 					res->action = t->value;
221 			}
222 			break;
223 
224 		case ENDTOKEN:
225 			break;
226 		}
227 	}
228 
229 	if (match != 1) {
230 		if (word == NULL)
231 			fprintf(stderr, "missing argument:\n");
232 		else if (match > 1)
233 			fprintf(stderr, "ambiguous argument: %s\n", word);
234 		else if (match < 1)
235 			fprintf(stderr, "unknown argument: %s\n", word);
236 		return (NULL);
237 	}
238 
239 	return (t);
240 }
241 
242 static void
243 show_valid_args(const struct token *table)
244 {
245 	int	i;
246 
247 	for (i = 0; table[i].type != ENDTOKEN; i++) {
248 		switch (table[i].type) {
249 		case NOTOKEN:
250 			fprintf(stderr, "  <cr>\n");
251 			break;
252 		case KEYWORD:
253 		case FLAG:
254 			fprintf(stderr, "  %s\n", table[i].keyword);
255 			break;
256 		case ADDRESS:
257 			fprintf(stderr, "  <address>\n");
258 			break;
259 		case PREFIX:
260 			fprintf(stderr, "  <address>[/<len>]\n");
261 			break;
262 		case IFNAME:
263 			fprintf(stderr, "  <interface>\n");
264 		case ENDTOKEN:
265 			break;
266 		}
267 	}
268 }
269 
270 int
271 parse_addr(const char *word, struct in_addr *addr)
272 {
273 	struct in_addr	ina;
274 
275 	if (word == NULL)
276 		return (0);
277 
278 	bzero(addr, sizeof(struct in_addr));
279 	bzero(&ina, sizeof(ina));
280 
281 	if (inet_pton(AF_INET, word, &ina)) {
282 		addr->s_addr = ina.s_addr;
283 		return (1);
284 	}
285 
286 	return (0);
287 }
288 
289 int
290 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen)
291 {
292 	struct in_addr	 ina;
293 	int		 bits = 32;
294 
295 	if (word == NULL)
296 		return (0);
297 
298 	bzero(addr, sizeof(struct in_addr));
299 	bzero(&ina, sizeof(ina));
300 
301 	if (strrchr(word, '/') != NULL) {
302 		if ((bits = inet_net_pton(AF_INET, word,
303 		    &ina, sizeof(ina))) == -1)
304 			return (0);
305 		addr->s_addr = ina.s_addr & htonl(prefixlen2mask(bits));
306 		*prefixlen = bits;
307 		return (1);
308 	} else {
309 		*prefixlen = 32;
310 		return (parse_addr(word, addr));
311 	}
312 
313 	return (0);
314 }
315 
316 /* XXX local copy from kroute.c, should go to shared file */
317 in_addr_t
318 prefixlen2mask(u_int8_t prefixlen)
319 {
320 	if (prefixlen == 0)
321 		return (0);
322 
323 	return (0xffffffff << (32 - prefixlen));
324 }
325