1 /* $OpenBSD: parser.c,v 1.6 2005/03/15 22:09:43 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_show[]; 54 static const struct token t_show_iface[]; 55 static const struct token t_show_db[]; 56 static const struct token t_show_area[]; 57 static const struct token t_show_nbr[]; 58 static const struct token t_show_rib[]; 59 static const struct token t_show_fib[]; 60 61 static const struct token t_main[] = { 62 /* {KEYWORD, "reload", RELOAD, NULL}, */ 63 {KEYWORD, "show", SHOW, t_show}, 64 {ENDTOKEN, "", NONE, NULL} 65 }; 66 67 static const struct token t_show[] = { 68 {NOTOKEN, "", NONE, NULL}, 69 {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, 70 {KEYWORD, "database", SHOW_DB, t_show_db}, 71 {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, 72 {KEYWORD, "rib", SHOW_RIB, t_show_rib}, 73 {KEYWORD, "fib", SHOW_FIB, t_show_fib}, 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 {IFNAME, "", NONE, NULL}, 81 {ENDTOKEN, "", NONE, NULL} 82 }; 83 84 static const struct token t_show_db[] = { 85 {NOTOKEN, "", NONE, NULL}, 86 {KEYWORD, "area", SHOW_DBBYAREA, t_show_area}, 87 /* {KEYWORD, "router", NONE, NULL}, 88 {KEYWORD, "network", NONE, NULL}, */ 89 {ENDTOKEN, "", NONE, NULL} 90 }; 91 92 static const struct token t_show_area[] = { 93 {ADDRESS, "", NONE, NULL}, 94 {ENDTOKEN, "", NONE, NULL} 95 }; 96 97 static const struct token t_show_nbr[] = { 98 {NOTOKEN, "", NONE, NULL}, 99 {KEYWORD, "detail", SHOW_NBR_DTAIL, NULL}, 100 {ENDTOKEN, "", NONE, NULL} 101 }; 102 103 static const struct token t_show_rib[] = { 104 {NOTOKEN, "", NONE, NULL}, 105 {KEYWORD, "detail", SHOW_RIB_DTAIL, NULL}, 106 {ENDTOKEN, "", NONE, NULL} 107 }; 108 109 static const struct token t_show_fib[] = { 110 {NOTOKEN, "", NONE, NULL}, 111 {FLAG, "connected", F_CONNECTED, t_show_fib}, 112 {FLAG, "static", F_STATIC, t_show_fib}, 113 {FLAG, "ospf", F_OSPFD_INSERTED, t_show_fib}, 114 {ADDRESS, "", NONE, NULL}, 115 {ENDTOKEN, "", NONE, NULL} 116 }; 117 118 static struct parse_result res; 119 120 struct parse_result * 121 parse(int argc, char *argv[]) 122 { 123 const struct token *table = t_main; 124 const struct token *match; 125 126 bzero(&res, sizeof(res)); 127 128 while (argc > 0) { 129 if ((match = match_token(argv[0], table)) == NULL) { 130 fprintf(stderr, "valid commands/args:\n"); 131 show_valid_args(table); 132 return (NULL); 133 } 134 135 argc--; 136 argv++; 137 138 if (match->type == NOTOKEN || match->next == NULL) 139 break; 140 141 table = match->next; 142 } 143 144 if (argc > 0) { 145 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 146 return (NULL); 147 } 148 149 return (&res); 150 } 151 152 const struct token * 153 match_token(const char *word, const struct token table[]) 154 { 155 u_int i, match; 156 const struct token *t = NULL; 157 158 match = 0; 159 160 for (i = 0; table[i].type != ENDTOKEN; i++) { 161 switch (table[i].type) { 162 case NOTOKEN: 163 if (word == NULL || strlen(word) == 0) { 164 match++; 165 t = &table[i]; 166 } 167 break; 168 case KEYWORD: 169 if (word != NULL && strncmp(word, table[i].keyword, 170 strlen(word)) == 0) { 171 match++; 172 t = &table[i]; 173 if (t->value) 174 res.action = t->value; 175 } 176 break; 177 case FLAG: 178 if (word != NULL && strncmp(word, table[i].keyword, 179 strlen(word)) == 0) { 180 match++; 181 t = &table[i]; 182 res.flags |= t->value; 183 } 184 break; 185 case ADDRESS: 186 if (parse_addr(word, &res.addr)) { 187 match++; 188 t = &table[i]; 189 if (t->value) 190 res.action = t->value; 191 } 192 break; 193 case PREFIX: 194 if (parse_prefix(word, &res.addr, &res.prefixlen)) { 195 match++; 196 t = &table[i]; 197 if (t->value) 198 res.action = t->value; 199 } 200 break; 201 case IFNAME: 202 if (!match && word != NULL && strlen(word) > 0) { 203 if (strlcpy(res.ifname, word, 204 sizeof(res.ifname)) >= 205 sizeof(res.ifname)) 206 err(1, "interface name too long"); 207 match++; 208 t = &table[i]; 209 } 210 break; 211 212 case ENDTOKEN: 213 break; 214 } 215 } 216 217 if (match != 1) { 218 if (match > 1) 219 fprintf(stderr, "ambiguous argument: %s\n", word); 220 if (match < 1) 221 fprintf(stderr, "unknown argument: %s\n", word); 222 return (NULL); 223 } 224 225 return (t); 226 } 227 228 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