1 /* $OpenBSD: parser.c,v 1.18 2010/02/19 10:35:52 dlg 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 "ospfd.h" 32 33 #include "parser.h" 34 35 enum token_type { 36 NOTOKEN, 37 ENDTOKEN, 38 KEYWORD, 39 ADDRESS, 40 FLAG, 41 PREFIX, 42 IFNAME 43 }; 44 45 struct token { 46 enum token_type type; 47 const char *keyword; 48 int value; 49 const struct token *next; 50 }; 51 52 static const struct token t_main[]; 53 static const struct token t_fib[]; 54 static const struct token t_show[]; 55 static const struct token t_show_iface[]; 56 static const struct token t_show_db[]; 57 static const struct token t_show_area[]; 58 static const struct token t_show_nbr[]; 59 static const struct token t_show_rib[]; 60 static const struct token t_show_fib[]; 61 static const struct token t_log[]; 62 63 static const struct token t_main[] = { 64 {KEYWORD, "reload", RELOAD, NULL}, 65 {KEYWORD, "fib", FIB, t_fib}, 66 {KEYWORD, "show", SHOW, t_show}, 67 {KEYWORD, "log", NONE, t_log}, 68 {ENDTOKEN, "", NONE, NULL} 69 }; 70 71 static const struct token t_fib[] = { 72 { KEYWORD, "couple", FIB_COUPLE, NULL}, 73 { KEYWORD, "decouple", FIB_DECOUPLE, NULL}, 74 { KEYWORD, "reload", FIB_RELOAD, NULL}, 75 { ENDTOKEN, "", NONE, NULL} 76 }; 77 78 static const struct token t_show[] = { 79 {NOTOKEN, "", NONE, NULL}, 80 {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, 81 {KEYWORD, "database", SHOW_DB, t_show_db}, 82 {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, 83 {KEYWORD, "rib", SHOW_RIB, t_show_rib}, 84 {KEYWORD, "fib", SHOW_FIB, t_show_fib}, 85 {KEYWORD, "summary", SHOW_SUM, NULL}, 86 {ENDTOKEN, "", NONE, NULL} 87 }; 88 89 static const struct token t_show_iface[] = { 90 {NOTOKEN, "", NONE, NULL}, 91 {KEYWORD, "detail", SHOW_IFACE_DTAIL, NULL}, 92 {IFNAME, "", SHOW_IFACE_DTAIL, NULL}, 93 {ENDTOKEN, "", NONE, NULL} 94 }; 95 96 static const struct token t_show_db[] = { 97 {NOTOKEN, "", NONE, NULL}, 98 {KEYWORD, "area", SHOW_DBBYAREA, t_show_area}, 99 {KEYWORD, "asbr", SHOW_DBASBR, NULL}, 100 {KEYWORD, "external", SHOW_DBEXT, NULL}, 101 {KEYWORD, "network", SHOW_DBNET, NULL}, 102 {KEYWORD, "router", SHOW_DBRTR, NULL}, 103 {KEYWORD, "self-originated", SHOW_DBSELF, NULL}, 104 {KEYWORD, "summary", SHOW_DBSUM, NULL}, 105 {ENDTOKEN, "", NONE, NULL} 106 }; 107 108 static const struct token t_show_area[] = { 109 {ADDRESS, "", NONE, NULL}, 110 {ENDTOKEN, "", NONE, NULL} 111 }; 112 113 static const struct token t_show_nbr[] = { 114 {NOTOKEN, "", NONE, NULL}, 115 {KEYWORD, "detail", SHOW_NBR_DTAIL, NULL}, 116 {ENDTOKEN, "", NONE, NULL} 117 }; 118 119 static const struct token t_show_rib[] = { 120 {NOTOKEN, "", NONE, NULL}, 121 {KEYWORD, "detail", SHOW_RIB_DTAIL, NULL}, 122 {ENDTOKEN, "", NONE, NULL} 123 }; 124 125 static const struct token t_show_fib[] = { 126 {NOTOKEN, "", NONE, NULL}, 127 {KEYWORD, "interface", SHOW_FIB_IFACE, t_show_iface}, 128 {FLAG, "connected", F_CONNECTED, t_show_fib}, 129 {FLAG, "static", F_STATIC, t_show_fib}, 130 {FLAG, "ospf", F_OSPFD_INSERTED, t_show_fib}, 131 {ADDRESS, "", NONE, NULL}, 132 {ENDTOKEN, "", NONE, NULL} 133 }; 134 135 static const struct token t_log[] = { 136 {KEYWORD, "verbose", LOG_VERBOSE, NULL}, 137 {KEYWORD, "brief", LOG_BRIEF, NULL}, 138 {ENDTOKEN, "", NONE, NULL} 139 }; 140 141 static struct parse_result res; 142 143 struct parse_result * 144 parse(int argc, char *argv[]) 145 { 146 const struct token *table = t_main; 147 const struct token *match; 148 149 bzero(&res, sizeof(res)); 150 151 while (argc >= 0) { 152 if ((match = match_token(argv[0], table)) == NULL) { 153 fprintf(stderr, "valid commands/args:\n"); 154 show_valid_args(table); 155 return (NULL); 156 } 157 158 argc--; 159 argv++; 160 161 if (match->type == NOTOKEN || match->next == NULL) 162 break; 163 164 table = match->next; 165 } 166 167 if (argc > 0) { 168 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 169 return (NULL); 170 } 171 172 return (&res); 173 } 174 175 const struct token * 176 match_token(const char *word, const struct token *table) 177 { 178 u_int i, match; 179 const struct token *t = NULL; 180 181 match = 0; 182 183 for (i = 0; table[i].type != ENDTOKEN; i++) { 184 switch (table[i].type) { 185 case NOTOKEN: 186 if (word == NULL || strlen(word) == 0) { 187 match++; 188 t = &table[i]; 189 } 190 break; 191 case KEYWORD: 192 if (word != NULL && strncmp(word, table[i].keyword, 193 strlen(word)) == 0) { 194 match++; 195 t = &table[i]; 196 if (t->value) 197 res.action = t->value; 198 } 199 break; 200 case FLAG: 201 if (word != NULL && strncmp(word, table[i].keyword, 202 strlen(word)) == 0) { 203 match++; 204 t = &table[i]; 205 res.flags |= t->value; 206 } 207 break; 208 case ADDRESS: 209 if (parse_addr(word, &res.addr)) { 210 match++; 211 t = &table[i]; 212 if (t->value) 213 res.action = t->value; 214 } 215 break; 216 case PREFIX: 217 if (parse_prefix(word, &res.addr, &res.prefixlen)) { 218 match++; 219 t = &table[i]; 220 if (t->value) 221 res.action = t->value; 222 } 223 break; 224 case IFNAME: 225 if (!match && word != NULL && strlen(word) > 0) { 226 if (strlcpy(res.ifname, word, 227 sizeof(res.ifname)) >= 228 sizeof(res.ifname)) 229 err(1, "interface name too long"); 230 match++; 231 t = &table[i]; 232 if (t->value) 233 res.action = t->value; 234 } 235 break; 236 237 case ENDTOKEN: 238 break; 239 } 240 } 241 242 if (match != 1) { 243 if (word == NULL) 244 fprintf(stderr, "missing argument:\n"); 245 else if (match > 1) 246 fprintf(stderr, "ambiguous argument: %s\n", word); 247 else if (match < 1) 248 fprintf(stderr, "unknown argument: %s\n", word); 249 return (NULL); 250 } 251 252 return (t); 253 } 254 255 void 256 show_valid_args(const struct token *table) 257 { 258 int i; 259 260 for (i = 0; table[i].type != ENDTOKEN; i++) { 261 switch (table[i].type) { 262 case NOTOKEN: 263 fprintf(stderr, " <cr>\n"); 264 break; 265 case KEYWORD: 266 case FLAG: 267 fprintf(stderr, " %s\n", table[i].keyword); 268 break; 269 case ADDRESS: 270 fprintf(stderr, " <address>\n"); 271 break; 272 case PREFIX: 273 fprintf(stderr, " <address>[/<len>]\n"); 274 break; 275 case IFNAME: 276 fprintf(stderr, " <interface>\n"); 277 break; 278 case ENDTOKEN: 279 break; 280 } 281 } 282 } 283 284 int 285 parse_addr(const char *word, struct in_addr *addr) 286 { 287 struct in_addr ina; 288 289 if (word == NULL) 290 return (0); 291 292 bzero(addr, sizeof(struct in_addr)); 293 bzero(&ina, sizeof(ina)); 294 295 if (inet_pton(AF_INET, word, &ina)) { 296 addr->s_addr = ina.s_addr; 297 return (1); 298 } 299 300 return (0); 301 } 302 303 int 304 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen) 305 { 306 struct in_addr ina; 307 int bits = 32; 308 309 if (word == NULL) 310 return (0); 311 312 bzero(addr, sizeof(struct in_addr)); 313 bzero(&ina, sizeof(ina)); 314 315 if (strrchr(word, '/') != NULL) { 316 if ((bits = inet_net_pton(AF_INET, word, 317 &ina, sizeof(ina))) == -1) 318 return (0); 319 addr->s_addr = ina.s_addr & htonl(prefixlen2mask(bits)); 320 *prefixlen = bits; 321 return (1); 322 } 323 *prefixlen = 32; 324 return (parse_addr(word, addr)); 325 } 326 327 /* XXX local copy from kroute.c, should go to shared file */ 328 in_addr_t 329 prefixlen2mask(u_int8_t prefixlen) 330 { 331 if (prefixlen == 0) 332 return (0); 333 334 return (0xffffffff << (32 - prefixlen)); 335 } 336