1 /* $OpenBSD: parser.c,v 1.1 2009/06/01 20:59:45 michele Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> 5 * Copyright (c) 2004 Esben Norby <norby@openbsd.org> 6 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/types.h> 22 #include <sys/socket.h> 23 #include <netinet/in.h> 24 #include <arpa/inet.h> 25 #include <err.h> 26 #include <errno.h> 27 #include <limits.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 32 #include "ldpd.h" 33 34 #include "parser.h" 35 36 enum token_type { 37 NOTOKEN, 38 ENDTOKEN, 39 KEYWORD, 40 ADDRESS, 41 FLAG, 42 PREFIX, 43 IFNAME 44 }; 45 46 struct token { 47 enum token_type type; 48 const char *keyword; 49 int value; 50 const struct token *next; 51 }; 52 53 static const struct token t_main[]; 54 static const struct token t_lfib[]; 55 static const struct token t_show[]; 56 static const struct token t_show_iface[]; 57 static const struct token t_show_db[]; 58 static const struct token t_show_area[]; 59 static const struct token t_show_nbr[]; 60 static const struct token t_show_lib[]; 61 static const struct token t_show_lfib[]; 62 63 static const struct token t_main[] = { 64 /* {KEYWORD, "reload", RELOAD, NULL}, */ 65 {KEYWORD, "lfib", LFIB, t_lfib}, 66 {KEYWORD, "show", SHOW, t_show}, 67 {ENDTOKEN, "", NONE, NULL} 68 }; 69 70 static const struct token t_lfib[] = { 71 { KEYWORD, "couple", LFIB_COUPLE, NULL}, 72 { KEYWORD, "decouple", LFIB_DECOUPLE, NULL}, 73 { ENDTOKEN, "", NONE, NULL} 74 }; 75 76 static const struct token t_show[] = { 77 {NOTOKEN, "", NONE, NULL}, 78 {KEYWORD, "interfaces", SHOW_IFACE, t_show_iface}, 79 {KEYWORD, "neighbor", SHOW_NBR, t_show_nbr}, 80 {KEYWORD, "lib", SHOW_LIB, t_show_lib}, 81 {KEYWORD, "lfib", SHOW_LFIB, t_show_lfib}, 82 {ENDTOKEN, "", NONE, NULL} 83 }; 84 85 static const struct token t_show_iface[] = { 86 {NOTOKEN, "", NONE, NULL}, 87 {ENDTOKEN, "", NONE, NULL} 88 }; 89 90 static const struct token t_show_nbr[] = { 91 {NOTOKEN, "", NONE, NULL}, 92 {ENDTOKEN, "", NONE, NULL} 93 }; 94 95 static const struct token t_show_lib[] = { 96 {NOTOKEN, "", NONE, NULL}, 97 {ENDTOKEN, "", NONE, NULL} 98 }; 99 100 static const struct token t_show_lfib[] = { 101 {NOTOKEN, "", NONE, NULL}, 102 {KEYWORD, "interface", SHOW_LFIB_IFACE, t_show_iface}, 103 {FLAG, "connected", F_CONNECTED, t_show_lfib}, 104 {FLAG, "static", F_STATIC, t_show_lfib}, 105 {ADDRESS, "", NONE, NULL}, 106 {ENDTOKEN, "", NONE, NULL} 107 }; 108 109 static struct parse_result res; 110 111 struct parse_result * 112 parse(int argc, char *argv[]) 113 { 114 const struct token *table = t_main; 115 const struct token *match; 116 117 bzero(&res, sizeof(res)); 118 119 while (argc >= 0) { 120 if ((match = match_token(argv[0], table)) == NULL) { 121 fprintf(stderr, "valid commands/args:\n"); 122 show_valid_args(table); 123 return (NULL); 124 } 125 126 argc--; 127 argv++; 128 129 if (match->type == NOTOKEN || match->next == NULL) 130 break; 131 132 table = match->next; 133 } 134 135 if (argc > 0) { 136 fprintf(stderr, "superfluous argument: %s\n", argv[0]); 137 return (NULL); 138 } 139 140 return (&res); 141 } 142 143 const struct token * 144 match_token(const char *word, const struct token table[]) 145 { 146 u_int i, match; 147 const struct token *t = NULL; 148 149 match = 0; 150 151 for (i = 0; table[i].type != ENDTOKEN; i++) { 152 switch (table[i].type) { 153 case NOTOKEN: 154 if (word == NULL || strlen(word) == 0) { 155 match++; 156 t = &table[i]; 157 } 158 break; 159 case KEYWORD: 160 if (word != NULL && strncmp(word, table[i].keyword, 161 strlen(word)) == 0) { 162 match++; 163 t = &table[i]; 164 if (t->value) 165 res.action = t->value; 166 } 167 break; 168 case FLAG: 169 if (word != NULL && strncmp(word, table[i].keyword, 170 strlen(word)) == 0) { 171 match++; 172 t = &table[i]; 173 res.flags |= t->value; 174 } 175 break; 176 case ADDRESS: 177 if (parse_addr(word, &res.addr)) { 178 match++; 179 t = &table[i]; 180 if (t->value) 181 res.action = t->value; 182 } 183 break; 184 case PREFIX: 185 if (parse_prefix(word, &res.addr, &res.prefixlen)) { 186 match++; 187 t = &table[i]; 188 if (t->value) 189 res.action = t->value; 190 } 191 break; 192 case IFNAME: 193 if (!match && word != NULL && strlen(word) > 0) { 194 if (strlcpy(res.ifname, word, 195 sizeof(res.ifname)) >= 196 sizeof(res.ifname)) 197 err(1, "interface name too long"); 198 match++; 199 t = &table[i]; 200 if (t->value) 201 res.action = t->value; 202 } 203 break; 204 205 case ENDTOKEN: 206 break; 207 } 208 } 209 210 if (match != 1) { 211 if (word == NULL) 212 fprintf(stderr, "missing argument:\n"); 213 else if (match > 1) 214 fprintf(stderr, "ambiguous argument: %s\n", word); 215 else if (match < 1) 216 fprintf(stderr, "unknown argument: %s\n", word); 217 return (NULL); 218 } 219 220 return (t); 221 } 222 223 void 224 show_valid_args(const struct token table[]) 225 { 226 int i; 227 228 for (i = 0; table[i].type != ENDTOKEN; i++) { 229 switch (table[i].type) { 230 case NOTOKEN: 231 fprintf(stderr, " <cr>\n"); 232 break; 233 case KEYWORD: 234 case FLAG: 235 fprintf(stderr, " %s\n", table[i].keyword); 236 break; 237 case ADDRESS: 238 fprintf(stderr, " <address>\n"); 239 break; 240 case PREFIX: 241 fprintf(stderr, " <address>[/<len>]\n"); 242 break; 243 case IFNAME: 244 fprintf(stderr, " <interface>\n"); 245 case ENDTOKEN: 246 break; 247 } 248 } 249 } 250 251 int 252 parse_addr(const char *word, struct in_addr *addr) 253 { 254 struct in_addr ina; 255 256 if (word == NULL) 257 return (0); 258 259 bzero(addr, sizeof(struct in_addr)); 260 bzero(&ina, sizeof(ina)); 261 262 if (inet_pton(AF_INET, word, &ina)) { 263 addr->s_addr = ina.s_addr; 264 return (1); 265 } 266 267 return (0); 268 } 269 270 int 271 parse_prefix(const char *word, struct in_addr *addr, u_int8_t *prefixlen) 272 { 273 struct in_addr ina; 274 int bits = 32; 275 276 if (word == NULL) 277 return (0); 278 279 bzero(addr, sizeof(struct in_addr)); 280 bzero(&ina, sizeof(ina)); 281 282 if (strrchr(word, '/') != NULL) { 283 if ((bits = inet_net_pton(AF_INET, word, 284 &ina, sizeof(ina))) == -1) 285 return (0); 286 addr->s_addr = ina.s_addr & htonl(prefixlen2mask(bits)); 287 *prefixlen = bits; 288 return (1); 289 } else { 290 *prefixlen = 32; 291 return (parse_addr(word, addr)); 292 } 293 294 return (0); 295 } 296 297 /* XXX local copy from kroute.c, should go to shared file */ 298 in_addr_t 299 prefixlen2mask(u_int8_t prefixlen) 300 { 301 if (prefixlen == 0) 302 return (0); 303 304 return (0xffffffff << (32 - prefixlen)); 305 } 306