blob: eea3f4fbc225361fb8cd78f09f7a66476e6c1543 [file] [log] [blame]
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +01001/*
2 * MAP management functions.
3 *
4 * Copyright 2000-2013 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 <limits.h>
14#include <stdio.h>
15
16#include <common/standard.h>
17
18#include <types/global.h>
19#include <types/map.h>
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010020#include <types/pattern.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010021
22#include <proto/arg.h>
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010023#include <proto/map.h>
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010024#include <proto/pattern.h>
25#include <proto/sample.h>
26
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010027/* Parse an IPv4 address and store it into the sample.
28 * The output type is IPV4.
29 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010030int map_parse_ip(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010031{
32 if (!buf2ip(text, strlen(text), &smp->data.ipv4))
33 return 0;
34 smp->type = SMP_T_IPV4;
35 return 1;
36}
37
38/* Parse an IPv6 address and store it into the sample.
39 * The output type is IPV6.
40 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010041int map_parse_ip6(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010042{
43 if (!buf2ip6(text, strlen(text), &smp->data.ipv6))
44 return 0;
45 smp->type = SMP_T_IPV6;
46 return 1;
47}
48
49/* Parse a string and store a pointer to it into the sample. The original
50 * string must be left in memory because we return a direct memory reference.
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010051 * The output type is SMP_T_STR. There is no risk that the data will be
52 * overwritten because sample_conv_map() makes a const sample with this
53 * output.
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010054 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010055int map_parse_str(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010056{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010057 smp->data.str.str = (char *)text;
58 smp->data.str.len = strlen(text);
59 smp->data.str.size = smp->data.str.len + 1;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010060 smp->type = SMP_T_STR;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010061 return 1;
62}
63
64/* Parse an integer and convert it to a sample. The output type is SINT if the
65 * number is negative, or UINT if it is positive or null. The function returns
66 * zero (error) if the number is too large.
67 */
Thierry FOURNIERb0c0a0f2013-12-10 15:05:34 +010068int map_parse_int(const char *text, struct sample_storage *smp)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010069{
70 long long int value;
71 char *error;
72
73 /* parse interger and convert it. Return the value in 64 format. */
74 value = strtoll(text, &error, 10);
75 if (*error != '\0')
76 return 0;
77
78 /* check sign iand limits */
79 if (value < 0) {
80 if (value < INT_MIN)
81 return 0;
82 smp->type = SMP_T_SINT;
83 smp->data.sint = value;
84 }
85 else {
86 if (value > UINT_MAX)
87 return 0;
88 smp->type = SMP_T_UINT;
89 smp->data.uint = value;
90 }
91
92 return 1;
93}
94
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010095/* This crete and initialize map descriptor.
96 * Return NULL if out of memory error
97 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010098static struct map_descriptor *map_create_descriptor(struct sample_conv *conv)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +010099{
100 struct map_descriptor *desc;
101
102 desc = calloc(1, sizeof(*desc));
103 if (!desc)
104 return NULL;
105
106 desc->conv = conv;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100107
108 return desc;
109}
110
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100111/* This function load the map file according with data type declared into
112 * the "struct sample_conv".
113 *
114 * This function choose the indexation type (ebtree or list) according with
115 * the type of match needed.
116 */
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200117int sample_load_map(struct arg *arg, struct sample_conv *conv,
118 const char *file, int line, char **err)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100119{
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100120 struct map_descriptor *desc;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100121
122 /* create new map descriptor */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100123 desc = map_create_descriptor(conv);
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100124 if (!desc) {
125 memprintf(err, "out of memory");
126 return 0;
127 }
128
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100129 /* Initialize pattern */
130 pattern_init_head(&desc->pat);
131
132 /* This is original pattern, must free */
133 desc->do_free = 1;
134
135 /* Set the match method. */
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100136 desc->pat.match = pat_match_fcts[(long)conv->private];
137 desc->pat.parse = pat_parse_fcts[(long)conv->private];
138 desc->pat.index = pat_index_fcts[(long)conv->private];
139 desc->pat.delete = pat_delete_fcts[(long)conv->private];
140 desc->pat.prune = pat_prune_fcts[(long)conv->private];
141 desc->pat.expect_type = pat_match_types[(long)conv->private];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100142
143 /* Set the output parse method. */
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100144 switch (desc->conv->out_type) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100145 case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break;
146 case SMP_T_UINT: desc->pat.parse_smp = map_parse_int; break;
147 case SMP_T_IPV4: desc->pat.parse_smp = map_parse_ip; break;
148 case SMP_T_IPV6: desc->pat.parse_smp = map_parse_ip6; break;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100149 default:
150 memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.",
151 conv->out_type);
152 return 0;
153 }
154
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100155 /* Load map. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200156 if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.str, PAT_MF_NO_DNS,
Thierry FOURNIER94580c92014-02-11 14:36:45 +0100157 1, err, file, line))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100158 return 0;
Thierry FOURNIER0ffe78c2013-12-05 14:40:25 +0100159
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100160 /* replace the first argument by this definition */
161 arg[0].type = ARGT_MAP;
162 arg[0].data.map = desc;
163
164 return 1;
165}
166
Willy Tarreau87b09662015-04-03 00:22:06 +0200167static int sample_conv_map(struct stream *stream, const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +0100168 struct sample *smp, void *private)
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100169{
170 struct map_descriptor *desc;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100171 struct pattern *pat;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100172
173 /* get config */
174 desc = arg_p[0].data.map;
175
176 /* Execute the match function. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100177 pat = pattern_exec_match(&desc->pat, smp, 1);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100178
179 /* Match case. */
180 if (pat) {
181 /* Copy sample. */
182 if (pat->smp) {
183 smp->type = pat->smp->type;
184 smp->flags |= SMP_F_CONST;
185 memcpy(&smp->data, &pat->smp->data, sizeof(smp->data));
186 return 1;
187 }
188
189 /* Return just int sample containing 1. */
190 smp->type = SMP_T_UINT;
191 smp->data.uint= 1;
192 return 1;
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100193 }
194
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100195 /* If no default value avalaible, the converter fails. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100196 if (arg_p[1].type == ARGT_STOP)
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100197 return 0;
198
199 /* Return the default value. */
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100200 switch (desc->conv->out_type) {
201
202 case SMP_T_STR:
203 smp->type = SMP_T_STR;
204 smp->flags |= SMP_F_CONST;
205 smp->data.str = arg_p[1].data.str;
206 break;
207
208 case SMP_T_UINT:
209 smp->type = SMP_T_UINT;
210 smp->data.uint = arg_p[1].data.uint;
211 break;
212
213 case SMP_T_IPV4:
214 smp->type = SMP_T_IPV4;
215 smp->data.ipv4 = arg_p[1].data.ipv4;
216 break;
217
218 case SMP_T_IPV6:
219 smp->type = SMP_T_IPV6;
220 smp->data.ipv6 = arg_p[1].data.ipv6;
221 break;
222 }
223
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100224 return 1;
225}
226
227/* Note: must not be declared <const> as its list will be overwritten
228 *
229 * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function
230 * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the
231 * file can be parsed.
232 *
233 * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function
234 * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file.
235 *
236 * The map_* keywords only emit strings.
237 *
238 * The output type is only used during the configuration parsing. It is used for detecting
239 * compatibility problems.
240 *
241 * The arguments are: <file>[,<default value>]
242 */
243static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER1edc9712014-12-15 16:18:39 +0100244 { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
245 { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_STR },
246 { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_BEG },
247 { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_SUB },
248 { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DIR },
249 { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_DOM },
250 { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_END },
251 { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, (void *)PAT_MATCH_REG },
252 { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, (void *)PAT_MATCH_INT },
253 { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100254
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100255 { "map_str_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_STR },
256 { "map_beg_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_BEG },
257 { "map_sub_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_SUB },
258 { "map_dir_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DIR },
259 { "map_dom_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_DOM },
260 { "map_end_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_END },
261 { "map_reg_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_STR, SMP_T_UINT, (void *)PAT_MATCH_REG },
262 { "map_int_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_UINT, SMP_T_UINT, (void *)PAT_MATCH_INT },
263 { "map_ip_int", sample_conv_map, ARG2(1,STR,UINT), sample_load_map, SMP_T_ADDR, SMP_T_UINT, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100264
Thierry FOURNIER933e5de2015-03-13 00:10:16 +0100265 { "map_str_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_STR },
266 { "map_beg_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_BEG },
267 { "map_sub_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_SUB },
268 { "map_dir_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DIR },
269 { "map_dom_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_DOM },
270 { "map_end_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_END },
271 { "map_reg_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_STR, SMP_T_IPV4, (void *)PAT_MATCH_REG },
272 { "map_int_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_UINT, SMP_T_IPV4, (void *)PAT_MATCH_INT },
273 { "map_ip_ip", sample_conv_map, ARG2(1,STR,IPV4), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, (void *)PAT_MATCH_IP },
Thierry FOURNIERd5f624d2013-11-26 11:52:33 +0100274
275 { /* END */ },
276}};
277
278__attribute__((constructor))
279static void __map_init(void)
280{
281 /* register format conversion keywords */
282 sample_register_convs(&sample_conv_kws);
283}