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