1*3f117ed9Srenato /* $OpenBSD: parser.c,v 1.12 2016/05/23 19:06:03 renato Exp $ */
2ab0c2486Smichele
3ab0c2486Smichele /*
4ab0c2486Smichele * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
5ab0c2486Smichele * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6ab0c2486Smichele * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7ab0c2486Smichele *
8ab0c2486Smichele * Permission to use, copy, modify, and distribute this software for any
9ab0c2486Smichele * purpose with or without fee is hereby granted, provided that the above
10ab0c2486Smichele * copyright notice and this permission notice appear in all copies.
11ab0c2486Smichele *
12ab0c2486Smichele * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13ab0c2486Smichele * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14ab0c2486Smichele * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15ab0c2486Smichele * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16ab0c2486Smichele * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17ab0c2486Smichele * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18ab0c2486Smichele * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19ab0c2486Smichele */
20ab0c2486Smichele
21ab0c2486Smichele #include <sys/types.h>
22ab0c2486Smichele #include <sys/socket.h>
23ab0c2486Smichele #include <netinet/in.h>
24ab0c2486Smichele #include <arpa/inet.h>
25ab0c2486Smichele #include <err.h>
26ab0c2486Smichele #include <errno.h>
27ab0c2486Smichele #include <limits.h>
28ab0c2486Smichele #include <stdio.h>
29ab0c2486Smichele #include <stdlib.h>
30ab0c2486Smichele #include <string.h>
316fb6d103Srenato #include <netdb.h>
32ab0c2486Smichele
33ab0c2486Smichele #include "ldpd.h"
34ab0c2486Smichele
35ab0c2486Smichele #include "parser.h"
36ab0c2486Smichele
37ab0c2486Smichele enum token_type {
38ab0c2486Smichele NOTOKEN,
39ab0c2486Smichele ENDTOKEN,
40ab0c2486Smichele KEYWORD,
416fb6d103Srenato FAMILY,
42ab0c2486Smichele ADDRESS,
43ab0c2486Smichele FLAG,
44ab0c2486Smichele IFNAME
45ab0c2486Smichele };
46ab0c2486Smichele
47ab0c2486Smichele struct token {
48ab0c2486Smichele enum token_type type;
49ab0c2486Smichele const char *keyword;
50ab0c2486Smichele int value;
51ab0c2486Smichele const struct token *next;
52ab0c2486Smichele };
53ab0c2486Smichele
54ab0c2486Smichele static const struct token t_main[];
5592dd8dc8Sclaudio static const struct token t_fib[];
56ab0c2486Smichele static const struct token t_show[];
57ab0c2486Smichele static const struct token t_show_iface[];
586fb6d103Srenato static const struct token t_show_iface_af[];
59d50436bbSclaudio static const struct token t_show_disc[];
606fb6d103Srenato static const struct token t_show_disc_af[];
61ab0c2486Smichele static const struct token t_show_nbr[];
626fb6d103Srenato static const struct token t_show_nbr_af[];
63ab0c2486Smichele static const struct token t_show_lib[];
646fb6d103Srenato static const struct token t_show_lib_af[];
6592dd8dc8Sclaudio static const struct token t_show_fib[];
666fb6d103Srenato static const struct token t_show_fib_af[];
67a94c7fd0Srenato static const struct token t_show_l2vpn[];
68*3f117ed9Srenato static const struct token t_clear[];
69*3f117ed9Srenato static const struct token t_clear_nbr[];
70c3319070Sclaudio static const struct token t_log[];
71ab0c2486Smichele
72ab0c2486Smichele static const struct token t_main[] = {
73fe91619eSrenato {KEYWORD, "reload", RELOAD, NULL},
7492dd8dc8Sclaudio {KEYWORD, "fib", FIB, t_fib},
75ab0c2486Smichele {KEYWORD, "show", SHOW, t_show},
76*3f117ed9Srenato {KEYWORD, "clear", CLEAR_NBR, t_clear},
77c3319070Sclaudio {KEYWORD, "log", NONE, t_log},
78ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
79ab0c2486Smichele };
80ab0c2486Smichele
8192dd8dc8Sclaudio static const struct token t_fib[] = {
8292dd8dc8Sclaudio { KEYWORD, "couple", FIB_COUPLE, NULL},
8392dd8dc8Sclaudio { KEYWORD, "decouple", FIB_DECOUPLE, NULL},
84ab0c2486Smichele { ENDTOKEN, "", NONE, NULL}
85ab0c2486Smichele };
86ab0c2486Smichele
87ab0c2486Smichele static const struct token t_show[] = {
88ab0c2486Smichele {NOTOKEN, "", NONE, NULL},
89ab0c2486Smichele {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface},
90d50436bbSclaudio {KEYWORD, "discovery", SHOW_DISC, t_show_disc},
91ab0c2486Smichele {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr},
92ab0c2486Smichele {KEYWORD, "lib", SHOW_LIB, t_show_lib},
9392dd8dc8Sclaudio {KEYWORD, "fib", SHOW_FIB, t_show_fib},
94a94c7fd0Srenato {KEYWORD, "l2vpn", NONE, t_show_l2vpn},
95ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
96ab0c2486Smichele };
97ab0c2486Smichele
98ab0c2486Smichele static const struct token t_show_iface[] = {
99ab0c2486Smichele {NOTOKEN, "", NONE, NULL},
1006fb6d103Srenato {KEYWORD, "family", NONE, t_show_iface_af},
1016fb6d103Srenato {ENDTOKEN, "", NONE, NULL}
1026fb6d103Srenato };
1036fb6d103Srenato
1046fb6d103Srenato static const struct token t_show_iface_af[] = {
1056fb6d103Srenato {FAMILY, "", NONE, t_show_iface},
106ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
107ab0c2486Smichele };
108ab0c2486Smichele
109d50436bbSclaudio static const struct token t_show_disc[] = {
110d50436bbSclaudio {NOTOKEN, "", NONE, NULL},
1116fb6d103Srenato {KEYWORD, "family", NONE, t_show_disc_af},
1126fb6d103Srenato {ENDTOKEN, "", NONE, NULL}
1136fb6d103Srenato };
1146fb6d103Srenato
1156fb6d103Srenato static const struct token t_show_disc_af[] = {
1166fb6d103Srenato {FAMILY, "", NONE, t_show_disc},
117d50436bbSclaudio {ENDTOKEN, "", NONE, NULL}
118d50436bbSclaudio };
119d50436bbSclaudio
120ab0c2486Smichele static const struct token t_show_nbr[] = {
121ab0c2486Smichele {NOTOKEN, "", NONE, NULL},
1226fb6d103Srenato {KEYWORD, "family", NONE, t_show_nbr_af},
1236fb6d103Srenato {ENDTOKEN, "", NONE, NULL}
1246fb6d103Srenato };
1256fb6d103Srenato
1266fb6d103Srenato static const struct token t_show_nbr_af[] = {
1276fb6d103Srenato {FAMILY, "", NONE, t_show_nbr},
128ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
129ab0c2486Smichele };
130ab0c2486Smichele
131ab0c2486Smichele static const struct token t_show_lib[] = {
132ab0c2486Smichele {NOTOKEN, "", NONE, NULL},
1336fb6d103Srenato {KEYWORD, "family", NONE, t_show_lib_af},
134ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
135ab0c2486Smichele };
136ab0c2486Smichele
1376fb6d103Srenato static const struct token t_show_lib_af[] = {
1386fb6d103Srenato {FAMILY, "", NONE, t_show_lib},
139c3319070Sclaudio {ENDTOKEN, "", NONE, NULL}
140c3319070Sclaudio };
141c3319070Sclaudio
14292dd8dc8Sclaudio static const struct token t_show_fib[] = {
143ab0c2486Smichele {NOTOKEN, "", NONE, NULL},
1446fb6d103Srenato {KEYWORD, "family", NONE, t_show_fib_af},
14592dd8dc8Sclaudio {KEYWORD, "interface", SHOW_FIB_IFACE, t_show_iface},
14692dd8dc8Sclaudio {FLAG, "connected", F_CONNECTED, t_show_fib},
14792dd8dc8Sclaudio {FLAG, "static", F_STATIC, t_show_fib},
148ab0c2486Smichele {ADDRESS, "", NONE, NULL},
149ab0c2486Smichele {ENDTOKEN, "", NONE, NULL}
150ab0c2486Smichele };
151ab0c2486Smichele
1526fb6d103Srenato static const struct token t_show_fib_af[] = {
1536fb6d103Srenato {FAMILY, "", NONE, t_show_fib},
1546fb6d103Srenato {ENDTOKEN, "", NONE, NULL}
1556fb6d103Srenato };
1566fb6d103Srenato
157a94c7fd0Srenato static const struct token t_show_l2vpn[] = {
158a94c7fd0Srenato {KEYWORD, "bindings", SHOW_L2VPN_BINDING, NULL},
159a94c7fd0Srenato {KEYWORD, "pseudowires", SHOW_L2VPN_PW, NULL},
160a94c7fd0Srenato {ENDTOKEN, "", NONE, NULL}
161a94c7fd0Srenato };
162a94c7fd0Srenato
163*3f117ed9Srenato static const struct token t_clear[] = {
164*3f117ed9Srenato {KEYWORD, "neighbors", CLEAR_NBR, t_clear_nbr},
165*3f117ed9Srenato {ENDTOKEN, "", NONE, NULL}
166*3f117ed9Srenato };
167*3f117ed9Srenato
168*3f117ed9Srenato static const struct token t_clear_nbr[] = {
169*3f117ed9Srenato {NOTOKEN, "", NONE, NULL},
170*3f117ed9Srenato {ADDRESS, "", NONE, NULL},
171*3f117ed9Srenato {ENDTOKEN, "", NONE, NULL}
172*3f117ed9Srenato };
173*3f117ed9Srenato
1746fb6d103Srenato static const struct token t_log[] = {
1756fb6d103Srenato {KEYWORD, "verbose", LOG_VERBOSE, NULL},
1766fb6d103Srenato {KEYWORD, "brief", LOG_BRIEF, NULL},
1776fb6d103Srenato {ENDTOKEN, "", NONE, NULL}
1786fb6d103Srenato };
1796fb6d103Srenato
1806be5843eStedu static const struct token *match_token(const char *, const struct token *,
1816be5843eStedu struct parse_result *);
1826be5843eStedu static void show_valid_args(const struct token *);
183ab0c2486Smichele
184ab0c2486Smichele struct parse_result *
parse(int argc,char * argv[])185ab0c2486Smichele parse(int argc, char *argv[])
186ab0c2486Smichele {
1876be5843eStedu static struct parse_result res;
188ab0c2486Smichele const struct token *table = t_main;
189ab0c2486Smichele const struct token *match;
190ab0c2486Smichele
19124d93a00Srenato memset(&res, 0, sizeof(res));
192ab0c2486Smichele
193ab0c2486Smichele while (argc >= 0) {
1946be5843eStedu if ((match = match_token(argv[0], table, &res)) == NULL) {
195ab0c2486Smichele fprintf(stderr, "valid commands/args:\n");
196ab0c2486Smichele show_valid_args(table);
197ab0c2486Smichele return (NULL);
198ab0c2486Smichele }
199ab0c2486Smichele
200ab0c2486Smichele argc--;
201ab0c2486Smichele argv++;
202ab0c2486Smichele
203ab0c2486Smichele if (match->type == NOTOKEN || match->next == NULL)
204ab0c2486Smichele break;
205ab0c2486Smichele
206ab0c2486Smichele table = match->next;
207ab0c2486Smichele }
208ab0c2486Smichele
209ab0c2486Smichele if (argc > 0) {
210ab0c2486Smichele fprintf(stderr, "superfluous argument: %s\n", argv[0]);
211ab0c2486Smichele return (NULL);
212ab0c2486Smichele }
213ab0c2486Smichele
214ab0c2486Smichele return (&res);
215ab0c2486Smichele }
216ab0c2486Smichele
2176be5843eStedu static const struct token *
match_token(const char * word,const struct token * table,struct parse_result * res)2186be5843eStedu match_token(const char *word, const struct token *table,
2196be5843eStedu struct parse_result *res)
220ab0c2486Smichele {
2216c1e7e28Srenato uint i, match;
222ab0c2486Smichele const struct token *t = NULL;
223ab0c2486Smichele
224ab0c2486Smichele match = 0;
225ab0c2486Smichele
226ab0c2486Smichele for (i = 0; table[i].type != ENDTOKEN; i++) {
227ab0c2486Smichele switch (table[i].type) {
228ab0c2486Smichele case NOTOKEN:
229ab0c2486Smichele if (word == NULL || strlen(word) == 0) {
230ab0c2486Smichele match++;
231ab0c2486Smichele t = &table[i];
232ab0c2486Smichele }
233ab0c2486Smichele break;
234ab0c2486Smichele case KEYWORD:
235ab0c2486Smichele if (word != NULL && strncmp(word, table[i].keyword,
236ab0c2486Smichele strlen(word)) == 0) {
237ab0c2486Smichele match++;
238ab0c2486Smichele t = &table[i];
239ab0c2486Smichele if (t->value)
2406be5843eStedu res->action = t->value;
241ab0c2486Smichele }
242ab0c2486Smichele break;
243ab0c2486Smichele case FLAG:
244ab0c2486Smichele if (word != NULL && strncmp(word, table[i].keyword,
245ab0c2486Smichele strlen(word)) == 0) {
246ab0c2486Smichele match++;
247ab0c2486Smichele t = &table[i];
2486be5843eStedu res->flags |= t->value;
249ab0c2486Smichele }
250ab0c2486Smichele break;
2516fb6d103Srenato case FAMILY:
2526fb6d103Srenato if (word == NULL)
2536fb6d103Srenato break;
2546fb6d103Srenato if (!strcmp(word, "inet") ||
2556fb6d103Srenato !strcasecmp(word, "IPv4")) {
256ab0c2486Smichele match++;
257ab0c2486Smichele t = &table[i];
2586fb6d103Srenato res->family = AF_INET;
2596fb6d103Srenato }
2606fb6d103Srenato if (!strcmp(word, "inet6") ||
2616fb6d103Srenato !strcasecmp(word, "IPv6")) {
2626fb6d103Srenato match++;
2636fb6d103Srenato t = &table[i];
2646fb6d103Srenato res->family = AF_INET6;
265ab0c2486Smichele }
266ab0c2486Smichele break;
2676fb6d103Srenato case ADDRESS:
2686fb6d103Srenato if (parse_addr(word, &res->family, &res->addr)) {
269ab0c2486Smichele match++;
270ab0c2486Smichele t = &table[i];
271ab0c2486Smichele if (t->value)
2726be5843eStedu res->action = t->value;
273ab0c2486Smichele }
274ab0c2486Smichele break;
275ab0c2486Smichele case IFNAME:
276ab0c2486Smichele if (!match && word != NULL && strlen(word) > 0) {
2776be5843eStedu if (strlcpy(res->ifname, word,
2786be5843eStedu sizeof(res->ifname)) >=
2796be5843eStedu sizeof(res->ifname))
280ab0c2486Smichele err(1, "interface name too long");
281ab0c2486Smichele match++;
282ab0c2486Smichele t = &table[i];
283ab0c2486Smichele if (t->value)
2846be5843eStedu res->action = t->value;
285ab0c2486Smichele }
286ab0c2486Smichele break;
287ab0c2486Smichele
288ab0c2486Smichele case ENDTOKEN:
289ab0c2486Smichele break;
290ab0c2486Smichele }
291ab0c2486Smichele }
292ab0c2486Smichele
293ab0c2486Smichele if (match != 1) {
294ab0c2486Smichele if (word == NULL)
295ab0c2486Smichele fprintf(stderr, "missing argument:\n");
296ab0c2486Smichele else if (match > 1)
297ab0c2486Smichele fprintf(stderr, "ambiguous argument: %s\n", word);
298ab0c2486Smichele else if (match < 1)
299ab0c2486Smichele fprintf(stderr, "unknown argument: %s\n", word);
300ab0c2486Smichele return (NULL);
301ab0c2486Smichele }
302ab0c2486Smichele
303ab0c2486Smichele return (t);
304ab0c2486Smichele }
305ab0c2486Smichele
3066be5843eStedu static void
show_valid_args(const struct token * table)3074b80bbd0Sjsg show_valid_args(const struct token *table)
308ab0c2486Smichele {
309ab0c2486Smichele int i;
310ab0c2486Smichele
311ab0c2486Smichele for (i = 0; table[i].type != ENDTOKEN; i++) {
312ab0c2486Smichele switch (table[i].type) {
313ab0c2486Smichele case NOTOKEN:
314ab0c2486Smichele fprintf(stderr, " <cr>\n");
315ab0c2486Smichele break;
316ab0c2486Smichele case KEYWORD:
317ab0c2486Smichele case FLAG:
318ab0c2486Smichele fprintf(stderr, " %s\n", table[i].keyword);
319ab0c2486Smichele break;
3206fb6d103Srenato case FAMILY:
3216fb6d103Srenato fprintf(stderr, " [ inet | inet6 | IPv4 | IPv6 ]\n");
3226fb6d103Srenato break;
323ab0c2486Smichele case ADDRESS:
324ab0c2486Smichele fprintf(stderr, " <address>\n");
325ab0c2486Smichele break;
326ab0c2486Smichele case IFNAME:
327ab0c2486Smichele fprintf(stderr, " <interface>\n");
328ab0c2486Smichele case ENDTOKEN:
329ab0c2486Smichele break;
330ab0c2486Smichele }
331ab0c2486Smichele }
332ab0c2486Smichele }
333ab0c2486Smichele
334ab0c2486Smichele int
parse_addr(const char * word,int * family,union ldpd_addr * addr)3356fb6d103Srenato parse_addr(const char *word, int *family, union ldpd_addr *addr)
336ab0c2486Smichele {
337ab0c2486Smichele struct in_addr ina;
3386fb6d103Srenato struct addrinfo hints, *r;
3396fb6d103Srenato struct sockaddr_in6 *sa_in6;
340ab0c2486Smichele
341ab0c2486Smichele if (word == NULL)
342ab0c2486Smichele return (0);
343ab0c2486Smichele
3446fb6d103Srenato memset(addr, 0, sizeof(*addr));
34524d93a00Srenato memset(&ina, 0, sizeof(ina));
346ab0c2486Smichele
3476fb6d103Srenato if (inet_net_pton(AF_INET, word, &ina, sizeof(ina)) != -1) {
3486fb6d103Srenato *family = AF_INET;
3496fb6d103Srenato addr->v4.s_addr = ina.s_addr;
3506fb6d103Srenato return (1);
3516fb6d103Srenato }
3526fb6d103Srenato
3536fb6d103Srenato memset(&hints, 0, sizeof(hints));
3546fb6d103Srenato hints.ai_family = AF_INET6;
3556fb6d103Srenato hints.ai_socktype = SOCK_DGRAM; /*dummy*/
3566fb6d103Srenato hints.ai_flags = AI_NUMERICHOST;
3576fb6d103Srenato if (getaddrinfo(word, "0", &hints, &r) == 0) {
3586fb6d103Srenato sa_in6 = (struct sockaddr_in6 *)r->ai_addr;
3596fb6d103Srenato *family = AF_INET6;
3606fb6d103Srenato addr->v6 = sa_in6->sin6_addr;
3616fb6d103Srenato freeaddrinfo(r);
362ab0c2486Smichele return (1);
363ab0c2486Smichele }
364ab0c2486Smichele
365ab0c2486Smichele return (0);
366ab0c2486Smichele }
367