blob: f23ee0243a5ec028b8c94cc8256c95fddea71c9b [file] [log] [blame]
Willy Tarreau2ac57182012-04-19 15:24:50 +02001/*
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
20static 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",
Willy Tarreau141987c2015-01-19 18:58:20 +010036 [ARGT_MAP] = "map",
Willy Tarreau2ac57182012-04-19 15:24:50 +020037 /* Unassigned types must never happen. Better crash during parsing if they do. */
38};
39
Willy Tarreau2e845be2012-10-19 19:49:09 +020040/* This dummy arg list may be used by default when no arg is found, it helps
41 * parsers by removing pointer checks.
42 */
43struct arg empty_arg_list[8] = { };
44
Willy Tarreaua4312fa2013-04-02 16:34:32 +020045/* This function clones a struct arg_list template into a new one which is
46 * returned.
47 */
48struct arg_list *arg_list_clone(const struct arg_list *orig)
49{
50 struct arg_list *new;
51
52 if ((new = calloc(1, sizeof(*new))) != NULL) {
53 /* ->list will be set by the caller when inserting the element.
54 * ->arg and ->arg_pos will be set by the caller.
55 */
56 new->ctx = orig->ctx;
57 new->kw = orig->kw;
58 new->conv = orig->conv;
59 new->file = orig->file;
60 new->line = orig->line;
61 }
62 return new;
63}
64
65/* This function clones a struct <arg_list> template into a new one which is
66 * set to point to arg <arg> at pos <pos>, and which is returned if the caller
67 * wants to apply further changes.
68 */
69struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos)
70{
71 struct arg_list *new;
72
73 new = arg_list_clone(orig);
74 new->arg = arg;
75 new->arg_pos = pos;
76 LIST_ADDQ(&orig->list, &new->list);
77 return new;
78}
79
Willy Tarreau2ac57182012-04-19 15:24:50 +020080/* This function builds an argument list from a config line. It returns the
81 * number of arguments found, or <0 in case of any error. Everything needed
82 * it automatically allocated. A pointer to an error message might be returned
83 * in err_msg if not NULL, in which case it would be allocated and the caller
84 * will have to check it and free it. The output arg list is returned in argp
85 * which must be valid. The returned array is always terminated by an arg of
86 * type ARGT_STOP (0), unless the mask indicates that no argument is supported.
Willy Tarreaua4312fa2013-04-02 16:34:32 +020087 * Unresolved arguments are appended to arg list <al>, which also serves as a
88 * template to create new entries. The mask is composed of a number of
89 * mandatory arguments in its lower 4 bits, and a concatenation of each
90 * argument type in each subsequent 4-bit block. If <err_msg> is not NULL, it
91 * must point to a freeable or NULL pointer.
Willy Tarreau2ac57182012-04-19 15:24:50 +020092 */
93int make_arg_list(const char *in, int len, unsigned int mask, struct arg **argp,
Willy Tarreaua4312fa2013-04-02 16:34:32 +020094 char **err_msg, const char **err_ptr, int *err_arg,
95 struct arg_list *al)
Willy Tarreau2ac57182012-04-19 15:24:50 +020096{
97 int nbarg;
98 int pos;
Willy Tarreaua4312fa2013-04-02 16:34:32 +020099 struct arg *arg;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200100 const char *beg;
101 char *word = NULL;
102 const char *ptr_err = NULL;
103 int min_arg;
104
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200105 *argp = NULL;
106
Willy Tarreau2ac57182012-04-19 15:24:50 +0200107 min_arg = mask & 15;
108 mask >>= 4;
109
110 pos = 0;
111 /* find between 0 and 8 the max number of args supported by the mask */
112 for (nbarg = 0; nbarg < 8 && ((mask >> (nbarg * 4)) & 0xF); nbarg++);
113
114 if (!nbarg)
115 goto end_parse;
116
117 /* Note: an empty input string contains an empty argument if this argument
118 * is marked mandatory. Otherwise we can ignore it.
119 */
120 if (!len && !min_arg)
121 goto end_parse;
122
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200123 arg = *argp = calloc(nbarg + 1, sizeof(*arg));
Willy Tarreau2ac57182012-04-19 15:24:50 +0200124
125 /* Note: empty arguments after a comma always exist. */
126 while (pos < nbarg) {
127 beg = in;
128 while (len && *in != ',') {
129 in++;
130 len--;
131 }
132
133 /* we have a new argument between <beg> and <in> (not included).
134 * For ease of handling, we copy it into a zero-terminated word.
135 * By default, the output argument will be the same type of the
136 * expected one.
137 */
138 free(word);
139 word = my_strndup(beg, in - beg);
140
141 arg->type = (mask >> (pos * 4)) & 15;
142
143 switch (arg->type) {
144 case ARGT_SINT:
145 if (in == beg) // empty number
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200146 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200147 else if (*beg < '0' || *beg > '9') {
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200148 beg++;
149 arg->data.sint = read_uint(&beg, in);
150 if (beg < in)
151 goto parse_err;
152 if (*word == '-')
Willy Tarreau2ac57182012-04-19 15:24:50 +0200153 arg->data.sint = -arg->data.sint;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200154 else if (*word != '+') // invalid first character
Willy Tarreau2ac57182012-04-19 15:24:50 +0200155 goto parse_err;
156 break;
157 }
158
159 arg->type = ARGT_UINT;
160 /* fall through ARGT_UINT if no sign is present */
161
162 case ARGT_UINT:
163 if (in == beg) // empty number
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200164 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200165
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200166 arg->data.uint = read_uint(&beg, in);
167 if (beg < in)
168 goto parse_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200169 break;
170
171 case ARGT_FE:
172 case ARGT_BE:
173 case ARGT_TAB:
174 case ARGT_SRV:
175 case ARGT_USR:
Willy Tarreau496aa012012-06-01 10:38:29 +0200176 /* These argument types need to be stored as strings during
177 * parsing then resolved later.
178 */
179 arg->unresolved = 1;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200180 arg_list_add(al, arg, pos);
181
Willy Tarreau496aa012012-06-01 10:38:29 +0200182 /* fall through */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200183 case ARGT_STR:
184 /* all types that must be resolved are stored as strings
185 * during the parsing. The caller must at one point resolve
186 * them and free the string.
187 */
188 arg->data.str.str = word;
189 arg->data.str.len = in - beg;
190 arg->data.str.size = arg->data.str.len + 1;
191 word = NULL;
192 break;
193
194 case ARGT_IPV4:
195 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200196 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200197
198 if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0)
199 goto parse_err;
200 break;
201
202 case ARGT_MSK4:
203 if (in == beg) // empty mask
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200204 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200205
206 if (!str2mask(word, &arg->data.ipv4))
207 goto parse_err;
208
209 arg->type = ARGT_IPV4;
210 break;
211
212 case ARGT_IPV6:
213 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200214 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200215
216 if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0)
217 goto parse_err;
218 break;
219
220 case ARGT_MSK6: /* not yet implemented */
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200221 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200222
223 case ARGT_TIME:
224 if (in == beg) // empty time
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200225 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200226
227 ptr_err = parse_time_err(word, &arg->data.uint, TIME_UNIT_MS);
228 if (ptr_err)
229 goto parse_err;
230
231 arg->type = ARGT_UINT;
232 break;
233
234 case ARGT_SIZE:
235 if (in == beg) // empty size
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200236 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200237
238 ptr_err = parse_size_err(word, &arg->data.uint);
239 if (ptr_err)
240 goto parse_err;
241
242 arg->type = ARGT_UINT;
243 break;
244
245 /* FIXME: other types need to be implemented here */
246 default:
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200247 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200248 }
249
250 pos++;
251 arg++;
252
253 /* don't go back to parsing if we reached end */
254 if (!len || pos >= nbarg)
255 break;
256
257 /* skip comma */
258 in++; len--;
259 }
260
261 end_parse:
262 free(word); word = NULL;
263
264 if (pos < min_arg) {
265 /* not enough arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200266 memprintf(err_msg,
Willy Tarreaub111d422013-12-13 00:38:47 +0100267 "missing arguments (got %d/%d), type '%s' expected",
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200268 pos, min_arg, arg_type_names[(mask >> (pos * 4)) & 15]);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200269 goto err;
270 }
271
272 if (len) {
273 /* too many arguments, starting at <in> */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200274 /* the caller is responsible for freeing this message */
275 word = my_strndup(in, len);
Willy Tarreaub111d422013-12-13 00:38:47 +0100276 if (nbarg)
277 memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
278 pos + 1, word);
279 else
280 memprintf(err_msg, "no argument supported, but got '%s'", word);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200281 free(word); word = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200282 goto err;
283 }
284
285 /* note that pos might be < nbarg and this is not an error, it's up to the
286 * caller to decide what to do with optional args.
287 */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200288 if (err_arg)
289 *err_arg = pos;
290 if (err_ptr)
291 *err_ptr = in;
292 return pos;
293
Willy Tarreau2ac57182012-04-19 15:24:50 +0200294 err:
295 free(word);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200296 free(*argp);
Willy Tarreau681e49d2013-12-06 15:30:05 +0100297 *argp = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200298 if (err_arg)
299 *err_arg = pos;
300 if (err_ptr)
301 *err_ptr = in;
302 return -1;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200303
304 empty_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200305 memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
306 arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200307 goto err;
308
309 parse_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200310 memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
311 word, arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200312 goto err;
313
314 not_impl:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200315 memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
316 arg_type_names[(mask >> (pos * 4)) & 15]);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200317 goto err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200318}