1 /* $OpenBSD: parser.c,v 1.19 2010/09/04 21:31:04 tedu 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 const struct token *match_token(const char *, const struct token *, 142 struct parse_result *); 143 static void show_valid_args(const struct token *); 144 145 struct parse_result * 146 parse(int argc, char *argv[]) 147 { 148 static struct parse_result res; 149 const struct token *table = t_main; 150 const struct token *match; 151 152 bzero(&res, sizeof(res)); 153 154 while (argc >= 0) { 155 if ((match = match_token(argv[0], table, &res)) == NULL) { 156 fprintf(stderr, "valid commands/args:\n"); 157 show_valid_args(table); 158 return (NULL); 159 } 160 161 argc--; 162 argv++; 163 164 if (match->type == NOTOKEN || match->next == NULL) 165 break; 166 167 table = match->next; 168 } 169 170 if (argc > 0) { 171 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 172 return (NULL); 173 } 174 175 return (&res); 176 } 177 178 static const struct token * 179 match_token(const char *word, const struct token *table, 180 struct parse_result *res) 181 { 182 u_int i, match; 183 const struct token *t = NULL; 184 185 match = 0; 186 187 for (i = 0; table[i].type != ENDTOKEN; i++) { 188 switch (table[i].type) { 189 case NOTOKEN: 190 if (word == NULL || strlen(word) == 0) { 191 match++; 192 t = &table[i]; 193 } 194 break; 195 case KEYWORD: 196 if (word != NULL && strncmp(word, table[i].keyword, 197 strlen(word)) == 0) { 198 match++; 199 t = &table[i]; 200 if (t->value) 201 res->action = t->value; 202 } 203 break; 204 case FLAG: 205 if (word != NULL && strncmp(word, table[i].keyword, 206 strlen(word)) == 0) { 207 match++; 208 t = &table[i]; 209 res->flags |= t->value; 210 } 211 break; 212 case ADDRESS: 213 if (parse_addr(word, &res->addr)) { 214 match++; 215 t = &table[i]; 216 if (t->value) 217 res->action = t->value; 218 } 219 break; 220 case PREFIX: 221 if (parse_prefix(word, &res->addr, &res->prefixlen)) { 222 match++; 223 t = &table[i]; 224 if (t->value) 225 res->action = t->value; 226 } 227 break; 228 case IFNAME: 229 if (!match && word != NULL && strlen(word) > 0) { 230 if (strlcpy(res->ifname, word, 231 sizeof(res->ifname)) >= 232 sizeof(res->ifname)) 233 err(1, "interface name too long"); 234 match++; 235 t = &table[i]; 236 if (t->value) 237 res->action = t->value; 238 } 239 break; 240 241 case ENDTOKEN: 242 break; 243 } 244 } 245 246 if (match != 1) { 247 if (word == NULL) 248 fprintf(stderr, "missing argument:\n"); 249 else if (match > 1) 250 fprintf(stderr, "ambiguous argument: %s\n", word); 251 else if (match < 1) 252 fprintf(stderr, "unknown argument: %s\n", word); 253 return (NULL); 254 } 255 256 return (t); 257 } 258 259 static void 260 show_valid_args(const struct token *table) 261 { 262 int i; 263 264 for (i = 0; table[i].type != ENDTOKEN; i++) { 265 switch (table[i].type) { 266 case NOTOKEN: 267 fprintf(stderr, " <cr>\n"); 268 break; 269 case KEYWORD: 270 case FLAG: 271 fprintf(stderr, " %s\n", table[i].keyword); 272 break; 273 case ADDRESS: 274 fprintf(stderr, " <address>\n"); 275 break; 276 case PREFIX: 277 fprintf(stderr, " <address>[/<len>]\n"); 278 break; 279 case IFNAME: 280 fprintf(stderr, " <interface>\n"); 281 break; 282 case ENDTOKEN: 283 break; 284 } 285 } 286 } 287 288 int 289 parse_addr(const char *word, struct in_addr *addr) 290 { 291 struct in_addr ina; 292 293 if (word == NULL) 294 return (0); 295 296 bzero(addr, sizeof(struct in_addr)); 297 bzero(&ina, sizeof(ina)); 298 299 if (inet_pton(AF_INET, word, &ina)) { 300 addr->s_addr = ina.s_addr; 301 return (1); 302 } 303 304 return (0); 305 } 306 307 int 308 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen) 309 { 310 struct in_addr ina; 311 int bits = 32; 312 313 if (word == NULL) 314 return (0); 315 316 bzero(addr, sizeof(struct in_addr)); 317 bzero(&ina, sizeof(ina)); 318 319 if (strrchr(word, '/') != NULL) { 320 if ((bits = inet_net_pton(AF_INET, word, 321 &ina, sizeof(ina))) == -1) 322 return (0); 323 addr->s_addr = ina.s_addr & htonl(prefixlen2mask(bits)); 324 *prefixlen = bits; 325 return (1); 326 } 327 *prefixlen = 32; 328 return (parse_addr(word, addr)); 329 } 330 331 /* XXX local copy from kroute.c, should go to shared file */ 332 in_addr_t 333 prefixlen2mask(u_int8_t prefixlen) 334 { 335 if (prefixlen == 0) 336 return (0); 337 338 return (0xffffffff << (32 - prefixlen)); 339 } 340