1 /* $OpenBSD: parser.c,v 1.2 2007/01/26 09:55:02 claudio 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 59 static const struct token t_main[] = { 60 /* {KEYWORD, "reload", RELOAD, NULL}, */ 61 {KEYWORD, "show", SHOW, t_show}, 62 {ENDTOKEN, "", NONE, NULL} 63 }; 64 65 static const struct token t_show[] = { 66 {NOTOKEN, "", NONE, NULL}, 67 {KEYWORD, "igmp", SHOW_IGMP, NULL}, 68 {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, 69 {KEYWORD, "mfc", SHOW_MFC, t_show_mfc}, 70 {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, 71 {KEYWORD, "rib", SHOW_RIB, t_show_rib}, 72 {KEYWORD, "summary", SHOW_SUM, NULL}, 73 {ENDTOKEN, "", NONE, NULL} 74 }; 75 76 static const struct token t_show_iface[] = { 77 {NOTOKEN, "", NONE, NULL}, 78 {KEYWORD, "detail", SHOW_IFACE_DTAIL,NULL}, 79 {IFNAME, "", SHOW_IFACE_DTAIL,NULL}, 80 {ENDTOKEN, "", NONE, NULL} 81 }; 82 83 static const struct token t_show_mfc[] = { 84 {NOTOKEN, "", NONE, NULL}, 85 {KEYWORD, "detail", SHOW_MFC_DTAIL, NULL}, 86 {ENDTOKEN, "", NONE, NULL} 87 }; 88 89 static const struct token t_show_nbr[] = { 90 {NOTOKEN, "", NONE, NULL}, 91 {KEYWORD, "detail", SHOW_NBR_DTAIL, NULL}, 92 {ENDTOKEN, "", NONE, NULL} 93 }; 94 95 static const struct token t_show_rib[] = { 96 {NOTOKEN, "", NONE, NULL}, 97 {KEYWORD, "detail", SHOW_RIB_DTAIL, NULL}, 98 {ENDTOKEN, "", NONE, NULL} 99 }; 100 101 static struct parse_result res; 102 103 struct parse_result * 104 parse(int argc, char *argv[]) 105 { 106 const struct token *table = t_main; 107 const struct token *match; 108 109 bzero(&res, sizeof(res)); 110 111 while (argc >= 0) { 112 if ((match = match_token(argv[0], table)) == NULL) { 113 fprintf(stderr, "valid commands/args:\n"); 114 show_valid_args(table); 115 return (NULL); 116 } 117 118 argc--; 119 argv++; 120 121 if (match->type == NOTOKEN || match->next == NULL) 122 break; 123 124 table = match->next; 125 } 126 127 if (argc > 0) { 128 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 129 return (NULL); 130 } 131 132 return (&res); 133 } 134 135 const struct token * 136 match_token(const char *word, const struct token table[]) 137 { 138 u_int i, match; 139 const struct token *t = NULL; 140 141 match = 0; 142 143 for (i = 0; table[i].type != ENDTOKEN; i++) { 144 switch (table[i].type) { 145 case NOTOKEN: 146 if (word == NULL || strlen(word) == 0) { 147 match++; 148 t = &table[i]; 149 } 150 break; 151 case KEYWORD: 152 if (word != NULL && strncmp(word, table[i].keyword, 153 strlen(word)) == 0) { 154 match++; 155 t = &table[i]; 156 if (t->value) 157 res.action = t->value; 158 } 159 break; 160 case FLAG: 161 if (word != NULL && strncmp(word, table[i].keyword, 162 strlen(word)) == 0) { 163 match++; 164 t = &table[i]; 165 res.flags |= t->value; 166 } 167 break; 168 case ADDRESS: 169 if (parse_addr(word, &res.addr)) { 170 match++; 171 t = &table[i]; 172 if (t->value) 173 res.action = t->value; 174 } 175 break; 176 case PREFIX: 177 if (parse_prefix(word, &res.addr, &res.prefixlen)) { 178 match++; 179 t = &table[i]; 180 if (t->value) 181 res.action = t->value; 182 } 183 break; 184 case IFNAME: 185 if (!match && word != NULL && strlen(word) > 0) { 186 if (strlcpy(res.ifname, word, 187 sizeof(res.ifname)) >= 188 sizeof(res.ifname)) 189 err(1, "interface name too long"); 190 match++; 191 t = &table[i]; 192 if (t->value) 193 res.action = t->value; 194 } 195 break; 196 197 case ENDTOKEN: 198 break; 199 } 200 } 201 202 if (match != 1) { 203 if (word == NULL) 204 fprintf(stderr, "missing argument:\n"); 205 else if (match > 1) 206 fprintf(stderr, "ambiguous argument: %s\n", word); 207 else 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