blob: 457c5b38dabbe0803d733bce97878de2b5580cdd [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",
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 */
50int 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 Tarreau4e6336f2012-04-27 16:32:26 +0200100 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200101 else if (*beg < '0' || *beg > '9') {
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200102 beg++;
103 arg->data.sint = read_uint(&beg, in);
104 if (beg < in)
105 goto parse_err;
106 if (*word == '-')
Willy Tarreau2ac57182012-04-19 15:24:50 +0200107 arg->data.sint = -arg->data.sint;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200108 else if (*word != '+') // invalid first character
Willy Tarreau2ac57182012-04-19 15:24:50 +0200109 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 Tarreau4e6336f2012-04-27 16:32:26 +0200118 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200119
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200120 arg->data.uint = read_uint(&beg, in);
121 if (beg < in)
122 goto parse_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200123 break;
124
125 case ARGT_FE:
126 case ARGT_BE:
127 case ARGT_TAB:
128 case ARGT_SRV:
129 case ARGT_USR:
130 case ARGT_STR:
131 /* all types that must be resolved are stored as strings
132 * during the parsing. The caller must at one point resolve
133 * them and free the string.
134 */
135 arg->data.str.str = word;
136 arg->data.str.len = in - beg;
137 arg->data.str.size = arg->data.str.len + 1;
138 word = NULL;
139 break;
140
141 case ARGT_IPV4:
142 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200143 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200144
145 if (inet_pton(AF_INET, word, &arg->data.ipv4) <= 0)
146 goto parse_err;
147 break;
148
149 case ARGT_MSK4:
150 if (in == beg) // empty mask
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200151 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200152
153 if (!str2mask(word, &arg->data.ipv4))
154 goto parse_err;
155
156 arg->type = ARGT_IPV4;
157 break;
158
159 case ARGT_IPV6:
160 if (in == beg) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200161 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200162
163 if (inet_pton(AF_INET6, word, &arg->data.ipv6) <= 0)
164 goto parse_err;
165 break;
166
167 case ARGT_MSK6: /* not yet implemented */
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200168 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200169
170 case ARGT_TIME:
171 if (in == beg) // empty time
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200172 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200173
174 ptr_err = parse_time_err(word, &arg->data.uint, TIME_UNIT_MS);
175 if (ptr_err)
176 goto parse_err;
177
178 arg->type = ARGT_UINT;
179 break;
180
181 case ARGT_SIZE:
182 if (in == beg) // empty size
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200183 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200184
185 ptr_err = parse_size_err(word, &arg->data.uint);
186 if (ptr_err)
187 goto parse_err;
188
189 arg->type = ARGT_UINT;
190 break;
191
192 /* FIXME: other types need to be implemented here */
193 default:
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200194 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200195 }
196
197 pos++;
198 arg++;
199
200 /* don't go back to parsing if we reached end */
201 if (!len || pos >= nbarg)
202 break;
203
204 /* skip comma */
205 in++; len--;
206 }
207
208 end_parse:
209 free(word); word = NULL;
210
211 if (pos < min_arg) {
212 /* not enough arguments */
213 if (err_msg)
214 memprintf(err_msg,
215 "Missing arguments (got %d/%d), type '%s' expected",
216 pos, min_arg, arg_type_names[(mask >> (pos * 4)) & 15]);
217 goto err;
218 }
219
220 if (len) {
221 /* too many arguments, starting at <in> */
222 if (err_msg) {
223 /* the caller is responsible for freeing this message */
224 word = my_strndup(in, len);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200225 memprintf(err_msg, "end of arguments expected at position %d, but got '%s'",
226 pos + 1, word);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200227 free(word); word = NULL;
228 }
229 goto err;
230 }
231
232 /* note that pos might be < nbarg and this is not an error, it's up to the
233 * caller to decide what to do with optional args.
234 */
235 *argp = arg_list;
236
237 if (err_arg)
238 *err_arg = pos;
239 if (err_ptr)
240 *err_ptr = in;
241 return pos;
242
Willy Tarreau2ac57182012-04-19 15:24:50 +0200243 err:
244 free(word);
245 free(arg_list);
246 if (err_arg)
247 *err_arg = pos;
248 if (err_ptr)
249 *err_ptr = in;
250 return -1;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200251
252 empty_err:
253 if (err_msg) {
254 memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
255 arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
256 }
257 goto err;
258
259 parse_err:
260 if (err_msg) {
261 memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
262 word, arg_type_names[(mask >> (pos * 4)) & 15], pos + 1);
263 }
264 goto err;
265
266 not_impl:
267 if (err_msg) {
268 memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
269 arg_type_names[(mask >> (pos * 4)) & 15]);
270 }
271 goto err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200272}