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