Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions used to parse typed argument lists |
| 3 | * |
| 4 | * Copyright 2012 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <arpa/inet.h> |
| 16 | |
| 17 | #include <common/standard.h> |
| 18 | #include <proto/arg.h> |
| 19 | |
| 20 | static const char *arg_type_names[ARGT_NBTYPES] = { |
| 21 | [ARGT_STOP] = "end of arguments", |
| 22 | [ARGT_UINT] = "unsigned integer", |
| 23 | [ARGT_SINT] = "signed integer", |
| 24 | [ARGT_STR] = "string", |
| 25 | [ARGT_IPV4] = "IPv4 address", |
| 26 | [ARGT_MSK4] = "IPv4 mask", |
| 27 | [ARGT_IPV6] = "IPv6 address", |
| 28 | [ARGT_MSK6] = "IPv6 mask", |
| 29 | [ARGT_TIME] = "delay", |
| 30 | [ARGT_SIZE] = "size", |
| 31 | [ARGT_FE] = "frontend", |
| 32 | [ARGT_BE] = "backend", |
| 33 | [ARGT_TAB] = "table", |
| 34 | [ARGT_SRV] = "server", |
| 35 | [ARGT_USR] = "user list", |
| 36 | /* Unassigned types must never happen. Better crash during parsing if they do. */ |
| 37 | }; |
| 38 | |
| 39 | /* This function builds an argument list from a config line. It returns the |
| 40 | * number of arguments found, or <0 in case of any error. Everything needed |
| 41 | * it automatically allocated. A pointer to an error message might be returned |
| 42 | * in err_msg if not NULL, in which case it would be allocated and the caller |
| 43 | * will have to check it and free it. The output arg list is returned in argp |
| 44 | * which must be valid. The returned array is always terminated by an arg of |
| 45 | * type ARGT_STOP (0), unless the mask indicates that no argument is supported. |
| 46 | * The mask is composed of a number of mandatory arguments in its lower 4 bits, |
| 47 | * and a concatenation of each argument type in each subsequent 4-bit block. If |
| 48 | * <err_msg> is not NULL, it must point to a freeable or NULL pointer. |
| 49 | */ |
| 50 | int make_arg_list(const char *in, int len, unsigned int mask, struct arg **argp, |
| 51 | char **err_msg, const char **err_ptr, int *err_arg) |
| 52 | { |
| 53 | int nbarg; |
| 54 | int pos; |
| 55 | struct arg *arg, *arg_list = NULL; |
| 56 | const char *beg; |
| 57 | char *word = NULL; |
| 58 | const char *ptr_err = NULL; |
| 59 | int min_arg; |
| 60 | |
| 61 | min_arg = mask & 15; |
| 62 | mask >>= 4; |
| 63 | |
| 64 | pos = 0; |
| 65 | /* find between 0 and 8 the max number of args supported by the mask */ |
| 66 | for (nbarg = 0; nbarg < 8 && ((mask >> (nbarg * 4)) & 0xF); nbarg++); |
| 67 | |
| 68 | if (!nbarg) |
| 69 | goto end_parse; |
| 70 | |
| 71 | /* Note: an empty input string contains an empty argument if this argument |
| 72 | * is marked mandatory. Otherwise we can ignore it. |
| 73 | */ |
| 74 | if (!len && !min_arg) |
| 75 | goto end_parse; |
| 76 | |
| 77 | arg = arg_list = calloc(nbarg + 1, sizeof(*arg)); |
| 78 | |
| 79 | /* Note: empty arguments after a comma always exist. */ |
| 80 | while (pos < nbarg) { |
| 81 | beg = in; |
| 82 | while (len && *in != ',') { |
| 83 | in++; |
| 84 | len--; |
| 85 | } |
| 86 | |
| 87 | /* we have a new argument between <beg> and <in> (not included). |
| 88 | * For ease of handling, we copy it into a zero-terminated word. |
| 89 | * By default, the output argument will be the same type of the |
| 90 | * expected one. |
| 91 | */ |
| 92 | free(word); |
| 93 | word = my_strndup(beg, in - beg); |
| 94 | |
| 95 | arg->type = (mask >> (pos * 4)) & 15; |
| 96 | |
| 97 | switch (arg->type) { |
| 98 | case ARGT_SINT: |
| 99 | if (in == beg) // empty number |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 100 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 101 | else if (*beg < '0' || *beg > '9') { |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 102 | beg++; |
| 103 | arg->data.sint = read_uint(&beg, in); |
| 104 | if (beg < in) |
| 105 | goto parse_err; |
| 106 | if (*word == '-') |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 107 | arg->data.sint = -arg->data.sint; |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 108 | else if (*word != '+') // invalid first character |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 109 | goto parse_err; |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | arg->type = ARGT_UINT; |
| 114 | /* fall through ARGT_UINT if no sign is present */ |
| 115 | |
| 116 | case ARGT_UINT: |
| 117 | if (in == beg) // empty number |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 118 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 119 | |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 120 | arg->data.uint = read_uint(&beg, in); |
| 121 | if (beg < in) |
| 122 | goto parse_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 123 | break; |
| 124 | |
| 125 | case ARGT_FE: |
| 126 | case ARGT_BE: |
| 127 | case ARGT_TAB: |
| 128 | case ARGT_SRV: |
| 129 | case ARGT_USR: |
Willy Tarreau | 496aa01 | 2012-06-01 10:38:29 +0200 | [diff] [blame] | 130 | /* These argument types need to be stored as strings during |
| 131 | * parsing then resolved later. |
| 132 | */ |
| 133 | arg->unresolved = 1; |
| 134 | /* fall through */ |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 135 | case ARGT_STR: |
| 136 | /* all types that must be resolved are stored as strings |
| 137 | * during the parsing. The caller must at one point resolve |
| 138 | * them and free the string. |
| 139 | */ |
| 140 | arg->data.str.str = word; |
| 141 | arg->data.str.len = in - beg; |
| 142 | arg->data.str.size = arg->data.str.len + 1; |
| 143 | word = NULL; |
| 144 | break; |
| 145 | |
| 146 | case ARGT_IPV4: |
| 147 | if (in == beg) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 148 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 149 | |
| 150 | if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0) |
| 151 | goto parse_err; |
| 152 | break; |
| 153 | |
| 154 | case ARGT_MSK4: |
| 155 | if (in == beg) // empty mask |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 156 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 157 | |
| 158 | if (!str2mask(word, &arg->data.ipv4)) |
| 159 | goto parse_err; |
| 160 | |
| 161 | arg->type = ARGT_IPV4; |
| 162 | break; |
| 163 | |
| 164 | case ARGT_IPV6: |
| 165 | if (in == beg) // empty address |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 166 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 167 | |
| 168 | if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0) |
| 169 | goto parse_err; |
| 170 | break; |
| 171 | |
| 172 | case ARGT_MSK6: /* not yet implemented */ |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 173 | goto not_impl; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 174 | |
| 175 | case ARGT_TIME: |
| 176 | if (in == beg) // empty time |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 177 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 178 | |
| 179 | ptr_err = parse_time_err(word, &arg->data.uint, TIME_UNIT_MS); |
| 180 | if (ptr_err) |
| 181 | goto parse_err; |
| 182 | |
| 183 | arg->type = ARGT_UINT; |
| 184 | break; |
| 185 | |
| 186 | case ARGT_SIZE: |
| 187 | if (in == beg) // empty size |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 188 | goto empty_err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 189 | |
| 190 | ptr_err = parse_size_err(word, &arg->data.uint); |
| 191 | if (ptr_err) |
| 192 | goto parse_err; |
| 193 | |
| 194 | arg->type = ARGT_UINT; |
| 195 | break; |
| 196 | |
| 197 | /* FIXME: other types need to be implemented here */ |
| 198 | default: |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 199 | goto not_impl; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | pos++; |
| 203 | arg++; |
| 204 | |
| 205 | /* don't go back to parsing if we reached end */ |
| 206 | if (!len || pos >= nbarg) |
| 207 | break; |
| 208 | |
| 209 | /* skip comma */ |
| 210 | in++; len--; |
| 211 | } |
| 212 | |
| 213 | end_parse: |
| 214 | free(word); word = NULL; |
| 215 | |
| 216 | if (pos < min_arg) { |
| 217 | /* not enough arguments */ |
| 218 | if (err_msg) |
| 219 | memprintf(err_msg, |
| 220 | "Missing arguments (got %d/%d), type '%s' expected", |
| 221 | pos, min_arg, arg_type_names[(mask >> (pos * 4)) & 15]); |
| 222 | goto err; |
| 223 | } |
| 224 | |
| 225 | if (len) { |
| 226 | /* too many arguments, starting at <in> */ |
| 227 | if (err_msg) { |
| 228 | /* the caller is responsible for freeing this message */ |
| 229 | word = my_strndup(in, len); |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 230 | memprintf(err_msg, "end of arguments expected at position %d, but got '%s'", |
| 231 | pos + 1, word); |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 232 | free(word); word = NULL; |
| 233 | } |
| 234 | goto err; |
| 235 | } |
| 236 | |
| 237 | /* note that pos might be < nbarg and this is not an error, it's up to the |
| 238 | * caller to decide what to do with optional args. |
| 239 | */ |
| 240 | *argp = arg_list; |
| 241 | |
| 242 | if (err_arg) |
| 243 | *err_arg = pos; |
| 244 | if (err_ptr) |
| 245 | *err_ptr = in; |
| 246 | return pos; |
| 247 | |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 248 | err: |
| 249 | free(word); |
| 250 | free(arg_list); |
| 251 | if (err_arg) |
| 252 | *err_arg = pos; |
| 253 | if (err_ptr) |
| 254 | *err_ptr = in; |
| 255 | return -1; |
Willy Tarreau | 4e6336f | 2012-04-27 16:32:26 +0200 | [diff] [blame] | 256 | |
| 257 | empty_err: |
| 258 | if (err_msg) { |
| 259 | memprintf(err_msg, "expected type '%s' at position %d, but got nothing", |
| 260 | arg_type_names[(mask >> (pos * 4)) & 15], pos + 1); |
| 261 | } |
| 262 | goto err; |
| 263 | |
| 264 | parse_err: |
| 265 | if (err_msg) { |
| 266 | memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d", |
| 267 | word, arg_type_names[(mask >> (pos * 4)) & 15], pos + 1); |
| 268 | } |
| 269 | goto err; |
| 270 | |
| 271 | not_impl: |
| 272 | if (err_msg) { |
| 273 | memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug", |
| 274 | arg_type_names[(mask >> (pos * 4)) & 15]); |
| 275 | } |
| 276 | goto err; |
Willy Tarreau | 2ac5718 | 2012-04-19 15:24:50 +0200 | [diff] [blame] | 277 | } |