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