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