1*6bae335dSjsg /* $OpenBSD: parser.c,v 1.2 2023/04/19 12:58:16 jsg Exp $ */
2dfcae16eSflorian
3dfcae16eSflorian /*
4dfcae16eSflorian * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5dfcae16eSflorian * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6dfcae16eSflorian *
7dfcae16eSflorian * Permission to use, copy, modify, and distribute this software for any
8dfcae16eSflorian * purpose with or without fee is hereby granted, provided that the above
9dfcae16eSflorian * copyright notice and this permission notice appear in all copies.
10dfcae16eSflorian *
11dfcae16eSflorian * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12dfcae16eSflorian * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13dfcae16eSflorian * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14dfcae16eSflorian * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15dfcae16eSflorian * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16dfcae16eSflorian * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17dfcae16eSflorian * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18dfcae16eSflorian */
19dfcae16eSflorian
20dfcae16eSflorian #include <sys/types.h>
21dfcae16eSflorian #include <sys/queue.h>
22dfcae16eSflorian #include <sys/socket.h>
23dfcae16eSflorian #include <netinet/in.h>
24dfcae16eSflorian #include <arpa/inet.h>
25dfcae16eSflorian
26dfcae16eSflorian #include <net/if.h>
27dfcae16eSflorian #include <netinet/if_ether.h>
28dfcae16eSflorian
29dfcae16eSflorian #include <err.h>
30dfcae16eSflorian #include <errno.h>
31dfcae16eSflorian #include <event.h>
32dfcae16eSflorian #include <imsg.h>
33dfcae16eSflorian #include <limits.h>
34dfcae16eSflorian #include <stdio.h>
35dfcae16eSflorian #include <stdlib.h>
36dfcae16eSflorian #include <string.h>
37dfcae16eSflorian
38dfcae16eSflorian #include "slaacd.h"
39dfcae16eSflorian #include "parser.h"
40dfcae16eSflorian
41dfcae16eSflorian enum token_type {
42dfcae16eSflorian NOTOKEN,
43dfcae16eSflorian ENDTOKEN,
44dfcae16eSflorian INTERFACENAME,
45dfcae16eSflorian KEYWORD
46dfcae16eSflorian };
47dfcae16eSflorian
48dfcae16eSflorian struct token {
49dfcae16eSflorian enum token_type type;
50dfcae16eSflorian const char *keyword;
51dfcae16eSflorian int value;
52dfcae16eSflorian const struct token *next;
53dfcae16eSflorian };
54dfcae16eSflorian
55dfcae16eSflorian static const struct token t_main[];
56dfcae16eSflorian static const struct token t_log[];
57dfcae16eSflorian static const struct token t_show[];
58dfcae16eSflorian static const struct token t_show_interface[];
59dfcae16eSflorian static const struct token t_send[];
60dfcae16eSflorian static const struct token t_send_sol[];
61dfcae16eSflorian
62dfcae16eSflorian static const struct token t_main[] = {
63dfcae16eSflorian {KEYWORD, "show", SHOW, t_show},
64dfcae16eSflorian {KEYWORD, "log", NONE, t_log},
65dfcae16eSflorian {KEYWORD, "send", NONE, t_send},
66dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
67dfcae16eSflorian };
68dfcae16eSflorian
69dfcae16eSflorian static const struct token t_log[] = {
70dfcae16eSflorian {KEYWORD, "verbose", LOG_VERBOSE, NULL},
71dfcae16eSflorian {KEYWORD, "brief", LOG_BRIEF, NULL},
72dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
73dfcae16eSflorian };
74dfcae16eSflorian
75dfcae16eSflorian static const struct token t_show[] = {
76dfcae16eSflorian {KEYWORD, "interface", SHOW_INTERFACE, t_show_interface},
77dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
78dfcae16eSflorian };
79dfcae16eSflorian
80dfcae16eSflorian static const struct token t_send[] = {
81dfcae16eSflorian {KEYWORD, "solicitation", SEND_SOLICITATION, t_send_sol},
82dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
83dfcae16eSflorian };
84dfcae16eSflorian static const struct token t_send_sol[] = {
85dfcae16eSflorian {INTERFACENAME, "", SEND_SOLICITATION, NULL},
86dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
87dfcae16eSflorian };
88dfcae16eSflorian
89dfcae16eSflorian static const struct token t_show_interface[] = {
90dfcae16eSflorian {NOTOKEN, "", NONE, NULL},
91dfcae16eSflorian {INTERFACENAME, "", SHOW_INTERFACE, NULL},
92dfcae16eSflorian {ENDTOKEN, "", NONE, NULL}
93dfcae16eSflorian };
94dfcae16eSflorian
95dfcae16eSflorian static const struct token *match_token(const char *, const struct token *,
96dfcae16eSflorian struct parse_result *);
97dfcae16eSflorian static void show_valid_args(const struct token *);
98dfcae16eSflorian
99dfcae16eSflorian struct parse_result *
parse(int argc,char * argv[])100dfcae16eSflorian parse(int argc, char *argv[])
101dfcae16eSflorian {
102dfcae16eSflorian static struct parse_result res;
103dfcae16eSflorian const struct token *table = t_main;
104dfcae16eSflorian const struct token *match;
105dfcae16eSflorian
106dfcae16eSflorian memset(&res, 0, sizeof(res));
107dfcae16eSflorian
108dfcae16eSflorian while (argc >= 0) {
109dfcae16eSflorian if ((match = match_token(argv[0], table, &res)) == NULL) {
110dfcae16eSflorian fprintf(stderr, "valid commands/args:\n");
111dfcae16eSflorian show_valid_args(table);
112dfcae16eSflorian return (NULL);
113dfcae16eSflorian }
114dfcae16eSflorian
115dfcae16eSflorian argc--;
116dfcae16eSflorian argv++;
117dfcae16eSflorian
118dfcae16eSflorian if (match->type == NOTOKEN || match->next == NULL)
119dfcae16eSflorian break;
120dfcae16eSflorian
121dfcae16eSflorian table = match->next;
122dfcae16eSflorian }
123dfcae16eSflorian
124dfcae16eSflorian if (argc > 0) {
125dfcae16eSflorian fprintf(stderr, "superfluous argument: %s\n", argv[0]);
126dfcae16eSflorian return (NULL);
127dfcae16eSflorian }
128dfcae16eSflorian
129dfcae16eSflorian return (&res);
130dfcae16eSflorian }
131dfcae16eSflorian
132dfcae16eSflorian static const struct token *
match_token(const char * word,const struct token * table,struct parse_result * res)133dfcae16eSflorian match_token(const char *word, const struct token *table,
134dfcae16eSflorian struct parse_result *res)
135dfcae16eSflorian {
136dfcae16eSflorian u_int i, match;
137dfcae16eSflorian const struct token *t = NULL;
138dfcae16eSflorian
139dfcae16eSflorian match = 0;
140dfcae16eSflorian
141dfcae16eSflorian for (i = 0; table[i].type != ENDTOKEN; i++) {
142dfcae16eSflorian switch (table[i].type) {
143dfcae16eSflorian case NOTOKEN:
144dfcae16eSflorian if (word == NULL || strlen(word) == 0) {
145dfcae16eSflorian match++;
146dfcae16eSflorian t = &table[i];
147dfcae16eSflorian }
148dfcae16eSflorian break;
149dfcae16eSflorian case INTERFACENAME:
150dfcae16eSflorian if (!match && word != NULL && strlen(word) > 0) {
151dfcae16eSflorian if ((res->if_index = if_nametoindex(word)) == 0)
152dfcae16eSflorian errx(1, "unknown interface");
153dfcae16eSflorian match++;
154dfcae16eSflorian t = &table[i];
155dfcae16eSflorian if (t->value)
156dfcae16eSflorian res->action = t->value;
157dfcae16eSflorian }
158dfcae16eSflorian break;
159dfcae16eSflorian case KEYWORD:
160dfcae16eSflorian if (word != NULL && strncmp(word, table[i].keyword,
161dfcae16eSflorian strlen(word)) == 0) {
162dfcae16eSflorian match++;
163dfcae16eSflorian t = &table[i];
164dfcae16eSflorian if (t->value)
165dfcae16eSflorian res->action = t->value;
166dfcae16eSflorian }
167dfcae16eSflorian break;
168dfcae16eSflorian case ENDTOKEN:
169dfcae16eSflorian break;
170dfcae16eSflorian }
171dfcae16eSflorian }
172dfcae16eSflorian
173dfcae16eSflorian if (match != 1) {
174dfcae16eSflorian if (word == NULL)
175dfcae16eSflorian fprintf(stderr, "missing argument:\n");
176dfcae16eSflorian else if (match > 1)
177dfcae16eSflorian fprintf(stderr, "ambiguous argument: %s\n", word);
178dfcae16eSflorian else if (match < 1)
179dfcae16eSflorian fprintf(stderr, "unknown argument: %s\n", word);
180dfcae16eSflorian return (NULL);
181dfcae16eSflorian }
182dfcae16eSflorian
183dfcae16eSflorian return (t);
184dfcae16eSflorian }
185dfcae16eSflorian
186dfcae16eSflorian static void
show_valid_args(const struct token * table)187dfcae16eSflorian show_valid_args(const struct token *table)
188dfcae16eSflorian {
189dfcae16eSflorian int i;
190dfcae16eSflorian
191dfcae16eSflorian for (i = 0; table[i].type != ENDTOKEN; i++) {
192dfcae16eSflorian switch (table[i].type) {
193dfcae16eSflorian case NOTOKEN:
194dfcae16eSflorian fprintf(stderr, " <cr>\n");
195dfcae16eSflorian break;
196dfcae16eSflorian case INTERFACENAME:
197dfcae16eSflorian fprintf(stderr, " <interface>\n");
198dfcae16eSflorian break;
199dfcae16eSflorian case KEYWORD:
200dfcae16eSflorian fprintf(stderr, " %s\n", table[i].keyword);
201dfcae16eSflorian break;
202dfcae16eSflorian case ENDTOKEN:
203dfcae16eSflorian break;
204dfcae16eSflorian }
205dfcae16eSflorian }
206dfcae16eSflorian }
207