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