xref: /openbsd-src/usr.sbin/ospfctl/parser.c (revision 3735523097595f2ae9c7910298932db42ce27cd7)
1 /*	$OpenBSD: parser.c,v 1.4 2005/03/12 11:03:05 norby 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 "parser.h"
32 
33 enum token_type {
34 	NOTOKEN,
35 	ENDTOKEN,
36 	KEYWORD,
37 	ADDRESS,
38 	FLAG,
39 	PREFIX,
40 	IFNAME
41 };
42 
43 struct token {
44 	enum token_type		 type;
45 	const char		*keyword;
46 	int			 value;
47 	const struct token	*next;
48 };
49 
50 static const struct token t_main[];
51 static const struct token t_show[];
52 static const struct token t_show_iface[];
53 static const struct token t_show_db[];
54 static const struct token t_show_area[];
55 static const struct token t_show_nbr[];
56 static const struct token t_show_rib[];
57 
58 static const struct token t_main[] = {
59 /*	{KEYWORD,	"reload",	RELOAD,		NULL}, */
60 	{KEYWORD,	"show",		SHOW,		t_show},
61 	{ENDTOKEN,	"",		NONE,		NULL}
62 };
63 
64 static const struct token t_show[] = {
65 	{NOTOKEN,	"",		NONE,		NULL},
66 	{KEYWORD,	"interfaces",	SHOW_IFACE,	t_show_iface},
67 	{KEYWORD,	"database",	SHOW_DB,	t_show_db},
68 	{KEYWORD,	"neighbor",	SHOW_NBR,	t_show_nbr},
69 /*	{KEYWORD,	"summary",	SHOW_SUMMARY,	NULL}, */
70 	{KEYWORD,	"rib",		SHOW_RIB,	t_show_rib},
71 	{ENDTOKEN,	"",		NONE,		NULL}
72 };
73 
74 static const struct token t_show_iface[] = {
75 	{NOTOKEN,	"",		NONE,		NULL},
76 	{IFNAME,	"",		NONE,		NULL},
77 	{ENDTOKEN,	"",		NONE,		NULL}
78 };
79 
80 static const struct token t_show_db[] = {
81 	{NOTOKEN,	"",		NONE,		NULL},
82 	{KEYWORD,	"area",		SHOW_DBBYAREA,	t_show_area},
83 /*	{KEYWORD,	"router",	NONE,		NULL},
84 	{KEYWORD,	"network",	NONE,		NULL}, */
85 	{ENDTOKEN,	"",		NONE,		NULL}
86 };
87 
88 static const struct token t_show_area[] = {
89 	{ADDRESS,	"",		NONE,		NULL},
90 	{ENDTOKEN,	"",		NONE,		NULL}
91 };
92 
93 static const struct token t_show_nbr[] = {
94 	{NOTOKEN,	"",		NONE,		NULL},
95 	{KEYWORD,	"detail",	SHOW_NBR_DTAIL,	NULL},
96 	{ENDTOKEN,	"",		NONE,		NULL}
97 };
98 
99 static const struct token t_show_rib[] = {
100 	{NOTOKEN,	"",		NONE,		NULL},
101 	{KEYWORD,	"detail",	SHOW_RIB_DTAIL,	NULL},
102 	{ENDTOKEN,	"",		NONE,		NULL}
103 };
104 
105 static struct parse_result	res;
106 
107 struct parse_result *
108 parse(int argc, char *argv[])
109 {
110 	const struct token	*table = t_main;
111 	const struct token	*match;
112 
113 	bzero(&res, sizeof(res));
114 
115 	while (argc > 0) {
116 		if ((match = match_token(argv[0], table)) == NULL) {
117 			fprintf(stderr, "valid commands/args:\n");
118 			show_valid_args(table);
119 			return (NULL);
120 		}
121 
122 		argc--;
123 		argv++;
124 
125 		if (match->type == NOTOKEN || match->next == NULL)
126 			break;
127 
128 		table = match->next;
129 	}
130 
131 	if (argc > 0) {
132 		fprintf(stderr, "superfluous argument: %s\n", argv[0]);
133 		return (NULL);
134 	}
135 
136 	return (&res);
137 }
138 
139 const struct token *
140 match_token(const char *word, const struct token table[])
141 {
142 	u_int			 i, match;
143 	const struct token	*t = NULL;
144 
145 	match = 0;
146 
147 	for (i = 0; table[i].type != ENDTOKEN; i++) {
148 		switch (table[i].type) {
149 		case NOTOKEN:
150 			if (word == NULL || strlen(word) == 0) {
151 				match++;
152 				t = &table[i];
153 			}
154 			break;
155 		case KEYWORD:
156 			if (word != NULL && strncmp(word, table[i].keyword,
157 			    strlen(word)) == 0) {
158 				match++;
159 				t = &table[i];
160 				if (t->value)
161 					res.action = t->value;
162 			}
163 			break;
164 		case FLAG:
165 			if (word != NULL && strncmp(word, table[i].keyword,
166 			    strlen(word)) == 0) {
167 				match++;
168 				t = &table[i];
169 				res.flags |= t->value;
170 			}
171 			break;
172 		case ADDRESS:
173 			if (parse_addr(word, &res.addr)) {
174 				match++;
175 				t = &table[i];
176 				if (t->value)
177 					res.action = t->value;
178 			}
179 			break;
180 		case PREFIX:
181 			if (parse_prefix(word, &res.addr, &res.prefixlen)) {
182 				match++;
183 				t = &table[i];
184 				if (t->value)
185 					res.action = t->value;
186 			}
187 			break;
188 		case IFNAME:
189 			if (!match && word != NULL && strlen(word) > 0) {
190 				if (strlcpy(res.ifname, word,
191 				    sizeof(res.ifname)) >=
192 				    sizeof(res.ifname))
193 					err(1, "interface name too long");
194 				match++;
195 				t = &table[i];
196 			}
197 			break;
198 
199 		case ENDTOKEN:
200 			break;
201 		}
202 	}
203 
204 	if (match != 1) {
205 		if (match > 1)
206 			fprintf(stderr, "ambiguous argument: %s\n", word);
207 		if (match < 1)
208 			fprintf(stderr, "unknown argument: %s\n", word);
209 		return (NULL);
210 	}
211 
212 	return (t);
213 }
214 
215 void
216 show_valid_args(const struct token table[])
217 {
218 	int	i;
219 
220 	for (i = 0; table[i].type != ENDTOKEN; i++) {
221 		switch (table[i].type) {
222 		case NOTOKEN:
223 			fprintf(stderr, "  <cr>\n");
224 			break;
225 		case KEYWORD:
226 		case FLAG:
227 			fprintf(stderr, "  %s\n", table[i].keyword);
228 			break;
229 		case ADDRESS:
230 			fprintf(stderr, "  <address>\n");
231 			break;
232 		case PREFIX:
233 			fprintf(stderr, "  <address>[/<len>]\n");
234 			break;
235 		case IFNAME:
236 			fprintf(stderr, "  <interface>\n");
237 		case ENDTOKEN:
238 			break;
239 		}
240 	}
241 }
242 
243 int
244 parse_addr(const char *word, struct in_addr *addr)
245 {
246 	struct in_addr	ina;
247 
248 	if (word == NULL)
249 		return (0);
250 
251 	bzero(addr, sizeof(struct in_addr));
252 	bzero(&ina, sizeof(ina));
253 
254 	if (inet_pton(AF_INET, word, &ina)) {
255 		addr->s_addr = ina.s_addr;
256 		return (1);
257 	}
258 
259 	return (0);
260 }
261 
262 int
263 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen)
264 {
265 	struct in_addr	 ina;
266 	int		 bits = 32;
267 
268 	if (word == NULL)
269 		return (0);
270 
271 	bzero(addr, sizeof(struct in_addr));
272 	bzero(&ina, sizeof(ina));
273 
274 	if (strrchr(word, '/') != NULL) {
275 		if ((bits = inet_net_pton(AF_INET, word,
276 		    &ina, sizeof(ina))) == -1)
277 			return (0);
278 		addr->s_addr = ina.s_addr & htonl(0xffffffff << (32 - bits));
279 		*prefixlen = bits;
280 		return (1);
281 	} else {
282 		*prefixlen = 32;
283 		return (parse_addr(word, addr));
284 	}
285 
286 	return (0);
287 }
288