blob: 7248c54b0235f89a53a318d8c93afc2dbaac3b89 [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
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010020const char *arg_type_names[ARGT_NBTYPES] = {
Willy Tarreau2ac57182012-04-19 15:24:50 +020021 [ARGT_STOP] = "end of arguments",
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020022 [ARGT_SINT] = "integer",
Willy Tarreau2ac57182012-04-19 15:24:50 +020023 [ARGT_STR] = "string",
24 [ARGT_IPV4] = "IPv4 address",
25 [ARGT_MSK4] = "IPv4 mask",
26 [ARGT_IPV6] = "IPv6 address",
27 [ARGT_MSK6] = "IPv6 mask",
28 [ARGT_TIME] = "delay",
29 [ARGT_SIZE] = "size",
30 [ARGT_FE] = "frontend",
31 [ARGT_BE] = "backend",
32 [ARGT_TAB] = "table",
33 [ARGT_SRV] = "server",
34 [ARGT_USR] = "user list",
Willy Tarreau53c250e2015-01-19 18:58:20 +010035 [ARGT_MAP] = "map",
Willy Tarreau46947782015-01-19 19:00:58 +010036 [ARGT_REG] = "regex",
Willy Tarreauf7ead612015-09-21 20:57:12 +020037 [ARGT_VAR] = "variable",
Willy Tarreau2ac57182012-04-19 15:24:50 +020038 /* Unassigned types must never happen. Better crash during parsing if they do. */
39};
40
Willy Tarreau2e845be2012-10-19 19:49:09 +020041/* This dummy arg list may be used by default when no arg is found, it helps
42 * parsers by removing pointer checks.
43 */
Willy Tarreau3d241e72015-01-19 18:44:07 +010044struct arg empty_arg_list[ARGM_NBARGS] = { };
Willy Tarreau2e845be2012-10-19 19:49:09 +020045
Willy Tarreaua4312fa2013-04-02 16:34:32 +020046/* This function clones a struct arg_list template into a new one which is
47 * returned.
48 */
49struct arg_list *arg_list_clone(const struct arg_list *orig)
50{
51 struct arg_list *new;
52
53 if ((new = calloc(1, sizeof(*new))) != NULL) {
54 /* ->list will be set by the caller when inserting the element.
55 * ->arg and ->arg_pos will be set by the caller.
56 */
57 new->ctx = orig->ctx;
58 new->kw = orig->kw;
59 new->conv = orig->conv;
60 new->file = orig->file;
61 new->line = orig->line;
62 }
63 return new;
64}
65
66/* This function clones a struct <arg_list> template into a new one which is
67 * set to point to arg <arg> at pos <pos>, and which is returned if the caller
68 * wants to apply further changes.
69 */
70struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos)
71{
72 struct arg_list *new;
73
74 new = arg_list_clone(orig);
75 new->arg = arg;
76 new->arg_pos = pos;
77 LIST_ADDQ(&orig->list, &new->list);
78 return new;
79}
80
Willy Tarreau2ac57182012-04-19 15:24:50 +020081/* This function builds an argument list from a config line. It returns the
82 * number of arguments found, or <0 in case of any error. Everything needed
83 * it automatically allocated. A pointer to an error message might be returned
84 * in err_msg if not NULL, in which case it would be allocated and the caller
85 * will have to check it and free it. The output arg list is returned in argp
86 * which must be valid. The returned array is always terminated by an arg of
87 * type ARGT_STOP (0), unless the mask indicates that no argument is supported.
Willy Tarreaua4312fa2013-04-02 16:34:32 +020088 * Unresolved arguments are appended to arg list <al>, which also serves as a
89 * template to create new entries. The mask is composed of a number of
Willy Tarreau3d241e72015-01-19 18:44:07 +010090 * mandatory arguments in its lower ARGM_BITS bits, and a concatenation of each
91 * argument type in each subsequent ARGT_BITS-bit sblock. If <err_msg> is not
92 * NULL, it must point to a freeable or NULL pointer.
Willy Tarreau2ac57182012-04-19 15:24:50 +020093 */
94int make_arg_list(const char *in, int len, unsigned int mask, struct arg **argp,
Willy Tarreaua4312fa2013-04-02 16:34:32 +020095 char **err_msg, const char **err_ptr, int *err_arg,
96 struct arg_list *al)
Willy Tarreau2ac57182012-04-19 15:24:50 +020097{
98 int nbarg;
99 int pos;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200100 struct arg *arg;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200101 const char *beg;
102 char *word = NULL;
103 const char *ptr_err = NULL;
104 int min_arg;
105
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200106 *argp = NULL;
107
Willy Tarreau3d241e72015-01-19 18:44:07 +0100108 min_arg = mask & ARGM_MASK;
109 mask >>= ARGM_BITS;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200110
111 pos = 0;
Willy Tarreau3d241e72015-01-19 18:44:07 +0100112 /* find between 0 and NBARGS the max number of args supported by the mask */
113 for (nbarg = 0; nbarg < ARGM_NBARGS && ((mask >> (nbarg * ARGT_BITS)) & ARGT_MASK); nbarg++);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200114
115 if (!nbarg)
116 goto end_parse;
117
118 /* Note: an empty input string contains an empty argument if this argument
119 * is marked mandatory. Otherwise we can ignore it.
120 */
121 if (!len && !min_arg)
122 goto end_parse;
123
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200124 arg = *argp = calloc(nbarg + 1, sizeof(*arg));
Willy Tarreau2ac57182012-04-19 15:24:50 +0200125
126 /* Note: empty arguments after a comma always exist. */
127 while (pos < nbarg) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200128 unsigned int uint;
129
Willy Tarreau2ac57182012-04-19 15:24:50 +0200130 beg = in;
131 while (len && *in != ',') {
132 in++;
133 len--;
134 }
135
136 /* we have a new argument between <beg> and <in> (not included).
137 * For ease of handling, we copy it into a zero-terminated word.
138 * By default, the output argument will be the same type of the
139 * expected one.
140 */
141 free(word);
142 word = my_strndup(beg, in - beg);
143
Willy Tarreau3d241e72015-01-19 18:44:07 +0100144 arg->type = (mask >> (pos * ARGT_BITS)) & ARGT_MASK;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200145
146 switch (arg->type) {
147 case ARGT_SINT:
148 if (in == beg) // empty number
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200149 goto empty_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200150 arg->data.sint = read_int64(&beg, in);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200151 if (beg < in)
152 goto parse_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200153 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200154 break;
155
156 case ARGT_FE:
157 case ARGT_BE:
158 case ARGT_TAB:
159 case ARGT_SRV:
160 case ARGT_USR:
Willy Tarreau46947782015-01-19 19:00:58 +0100161 case ARGT_REG:
Willy Tarreau496aa012012-06-01 10:38:29 +0200162 /* These argument types need to be stored as strings during
163 * parsing then resolved later.
164 */
165 arg->unresolved = 1;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200166 arg_list_add(al, arg, pos);
167
Willy Tarreau496aa012012-06-01 10:38:29 +0200168 /* fall through */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200169 case ARGT_STR:
170 /* all types that must be resolved are stored as strings
171 * during the parsing. The caller must at one point resolve
172 * them and free the string.
173 */
174 arg->data.str.str = word;
175 arg->data.str.len = in - beg;
176 arg->data.str.size = arg->data.str.len + 1;
177 word = NULL;
178 break;
179
180 case ARGT_IPV4:
181 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200182 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200183
184 if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0)
185 goto parse_err;
186 break;
187
188 case ARGT_MSK4:
189 if (in == beg) // empty mask
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200190 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200191
192 if (!str2mask(word, &arg->data.ipv4))
193 goto parse_err;
194
195 arg->type = ARGT_IPV4;
196 break;
197
198 case ARGT_IPV6:
199 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200200 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200201
202 if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0)
203 goto parse_err;
204 break;
205
206 case ARGT_MSK6: /* not yet implemented */
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200207 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200208
209 case ARGT_TIME:
210 if (in == beg) // empty time
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200211 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200212
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200213 ptr_err = parse_time_err(word, &uint, TIME_UNIT_MS);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200214 if (ptr_err)
215 goto parse_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200216 arg->data.sint = uint;
217 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200218 break;
219
220 case ARGT_SIZE:
221 if (in == beg) // empty size
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200222 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200223
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200224 ptr_err = parse_size_err(word, &uint);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200225 if (ptr_err)
226 goto parse_err;
227
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200228 arg->data.sint = uint;
229 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200230 break;
231
232 /* FIXME: other types need to be implemented here */
233 default:
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200234 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200235 }
236
237 pos++;
238 arg++;
239
240 /* don't go back to parsing if we reached end */
241 if (!len || pos >= nbarg)
242 break;
243
244 /* skip comma */
245 in++; len--;
246 }
247
248 end_parse:
249 free(word); word = NULL;
250
251 if (pos < min_arg) {
252 /* not enough arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200253 memprintf(err_msg,
Willy Tarreaub111d422013-12-13 00:38:47 +0100254 "missing arguments (got %d/%d), type '%s' expected",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100255 pos, min_arg, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200256 goto err;
257 }
258
259 if (len) {
260 /* too many arguments, starting at <in> */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200261 /* the caller is responsible for freeing this message */
262 word = my_strndup(in, len);
Willy Tarreaub111d422013-12-13 00:38:47 +0100263 if (nbarg)
264 memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
265 pos + 1, word);
266 else
267 memprintf(err_msg, "no argument supported, but got '%s'", word);
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200268 free(word); word = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200269 goto err;
270 }
271
272 /* note that pos might be < nbarg and this is not an error, it's up to the
273 * caller to decide what to do with optional args.
274 */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200275 if (err_arg)
276 *err_arg = pos;
277 if (err_ptr)
278 *err_ptr = in;
279 return pos;
280
Willy Tarreau2ac57182012-04-19 15:24:50 +0200281 err:
282 free(word);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200283 free(*argp);
Willy Tarreau681e49d2013-12-06 15:30:05 +0100284 *argp = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200285 if (err_arg)
286 *err_arg = pos;
287 if (err_ptr)
288 *err_ptr = in;
289 return -1;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200290
291 empty_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200292 memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100293 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200294 goto err;
295
296 parse_err:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200297 memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100298 word, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200299 goto err;
300
301 not_impl:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200302 memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100303 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200304 goto err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200305}