Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 1 | /* |
| 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 FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 20 | #include <types/pattern.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 21 | |
| 22 | #include <proto/arg.h> |
Thierry FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 23 | #include <proto/map.h> |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 24 | #include <proto/pattern.h> |
| 25 | #include <proto/sample.h> |
| 26 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 27 | /* Parse an IPv4 address and store it into the sample. |
| 28 | * The output type is IPV4. |
| 29 | */ |
Thierry FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 30 | int map_parse_ip(const char *text, struct sample_storage *smp) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 31 | { |
| 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 FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 41 | int map_parse_ip6(const char *text, struct sample_storage *smp) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 42 | { |
| 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 FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 51 | * 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 FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 54 | */ |
Thierry FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 55 | int map_parse_str(const char *text, struct sample_storage *smp) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 56 | { |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 57 | smp->data.str.str = (char *)text; |
| 58 | smp->data.str.len = strlen(text); |
| 59 | smp->data.str.size = smp->data.str.len + 1; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 60 | smp->type = SMP_T_STR; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 61 | 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 FOURNIER | b0c0a0f | 2013-12-10 15:05:34 +0100 | [diff] [blame] | 68 | int map_parse_int(const char *text, struct sample_storage *smp) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 69 | { |
| 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 FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 95 | /* This crete and initialize map descriptor. |
| 96 | * Return NULL if out of memory error |
| 97 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 98 | static struct map_descriptor *map_create_descriptor(struct sample_conv *conv) |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 99 | { |
| 100 | struct map_descriptor *desc; |
| 101 | |
| 102 | desc = calloc(1, sizeof(*desc)); |
| 103 | if (!desc) |
| 104 | return NULL; |
| 105 | |
| 106 | desc->conv = conv; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 107 | |
| 108 | return desc; |
| 109 | } |
| 110 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 111 | /* 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 | */ |
| 117 | static int sample_load_map(struct arg *arg, struct sample_conv *conv, char **err) |
| 118 | { |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 119 | struct map_descriptor *desc; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 120 | |
| 121 | /* create new map descriptor */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 122 | desc = map_create_descriptor(conv); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 123 | if (!desc) { |
| 124 | memprintf(err, "out of memory"); |
| 125 | return 0; |
| 126 | } |
| 127 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 128 | /* Initialize pattern */ |
| 129 | pattern_init_head(&desc->pat); |
| 130 | |
| 131 | /* This is original pattern, must free */ |
| 132 | desc->do_free = 1; |
| 133 | |
| 134 | /* Set the match method. */ |
| 135 | desc->pat.match = pat_match_fcts[conv->private]; |
| 136 | desc->pat.parse = pat_parse_fcts[conv->private]; |
| 137 | desc->pat.index = pat_index_fcts[conv->private]; |
| 138 | desc->pat.delete = pat_delete_fcts[conv->private]; |
| 139 | desc->pat.prune = pat_prune_fcts[conv->private]; |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 140 | desc->pat.expect_type = pat_match_types[conv->private]; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 141 | |
| 142 | /* Set the output parse method. */ |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 143 | switch (desc->conv->out_type) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 144 | case SMP_T_STR: desc->pat.parse_smp = map_parse_str; break; |
| 145 | case SMP_T_UINT: desc->pat.parse_smp = map_parse_int; break; |
| 146 | case SMP_T_IPV4: desc->pat.parse_smp = map_parse_ip; break; |
| 147 | case SMP_T_IPV6: desc->pat.parse_smp = map_parse_ip6; break; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 148 | default: |
| 149 | memprintf(err, "map: internal haproxy error: no default parse case for the input type <%d>.", |
| 150 | conv->out_type); |
| 151 | return 0; |
| 152 | } |
| 153 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 154 | /* Build displayed message. */ |
| 155 | snprintf(trash.str, trash.size, "map(s) loaded from file '%s'", arg[0].data.str.str); |
| 156 | trash.str[trash.size - 1] = '\0'; |
Thierry FOURNIER | 0ffe78c | 2013-12-05 14:40:25 +0100 | [diff] [blame] | 157 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 158 | /* Load map. */ |
| 159 | if (!pattern_read_from_file(&desc->pat, PAT_REF_MAP, arg[0].data.str.str, 0, |
| 160 | 1, err, trash.str)) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 161 | return 0; |
Thierry FOURNIER | 0ffe78c | 2013-12-05 14:40:25 +0100 | [diff] [blame] | 162 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 163 | /* The second argument is the default value */ |
| 164 | if (arg[1].type == ARGT_STR) { |
| 165 | desc->default_value = strdup(arg[1].data.str.str); |
| 166 | if (!desc->default_value) { |
| 167 | memprintf(err, "out of memory"); |
| 168 | return 0; |
| 169 | } |
| 170 | desc->def = calloc(1, sizeof(*desc->def)); |
| 171 | if (!desc->def) { |
| 172 | memprintf(err, "out of memory"); |
| 173 | return 0; |
| 174 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 175 | if (!desc->pat.parse_smp(desc->default_value, desc->def)) { |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 176 | memprintf(err, "Cannot parse default value"); |
| 177 | return 0; |
| 178 | } |
| 179 | } |
| 180 | else |
| 181 | desc->def = NULL; |
| 182 | |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 183 | /* replace the first argument by this definition */ |
| 184 | arg[0].type = ARGT_MAP; |
| 185 | arg[0].data.map = desc; |
| 186 | |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | static int sample_conv_map(const struct arg *arg_p, struct sample *smp) |
| 191 | { |
| 192 | struct map_descriptor *desc; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 193 | struct pattern *pat; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 194 | |
| 195 | /* get config */ |
| 196 | desc = arg_p[0].data.map; |
| 197 | |
| 198 | /* Execute the match function. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 199 | pat = pattern_exec_match(&desc->pat, smp, 1); |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 200 | |
| 201 | /* Match case. */ |
| 202 | if (pat) { |
| 203 | /* Copy sample. */ |
| 204 | if (pat->smp) { |
| 205 | smp->type = pat->smp->type; |
| 206 | smp->flags |= SMP_F_CONST; |
| 207 | memcpy(&smp->data, &pat->smp->data, sizeof(smp->data)); |
| 208 | return 1; |
| 209 | } |
| 210 | |
| 211 | /* Return just int sample containing 1. */ |
| 212 | smp->type = SMP_T_UINT; |
| 213 | smp->data.uint= 1; |
| 214 | return 1; |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 217 | /* If no default value avalaible, the converter fails. */ |
| 218 | if (!desc->def) |
| 219 | return 0; |
| 220 | |
| 221 | /* Return the default value. */ |
| 222 | smp->type = desc->def->type; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 223 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 224 | memcpy(&smp->data, &desc->def->data, sizeof(smp->data)); |
Thierry FOURNIER | d5f624d | 2013-11-26 11:52:33 +0100 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* Note: must not be declared <const> as its list will be overwritten |
| 229 | * |
| 230 | * For the map_*_int keywords, the output is declared as SMP_T_UINT, but the converter function |
| 231 | * can provide SMP_T_UINT, SMP_T_SINT or SMP_T_BOOL depending on how the patterns found in the |
| 232 | * file can be parsed. |
| 233 | * |
| 234 | * For the map_*_ip keyword, the output is declared as SMP_T_IPV4, but the converter function |
| 235 | * can provide SMP_T_IPV4 or SMP_T_IPV6 depending on the patterns found in the file. |
| 236 | * |
| 237 | * The map_* keywords only emit strings. |
| 238 | * |
| 239 | * The output type is only used during the configuration parsing. It is used for detecting |
| 240 | * compatibility problems. |
| 241 | * |
| 242 | * The arguments are: <file>[,<default value>] |
| 243 | */ |
| 244 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 245 | { "map", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR }, |
| 246 | { "map_str", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_STR }, |
| 247 | { "map_beg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_BEG }, |
| 248 | { "map_sub", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_SUB }, |
| 249 | { "map_dir", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DIR }, |
| 250 | { "map_dom", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_DOM }, |
| 251 | { "map_end", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_END }, |
| 252 | { "map_reg", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_STR, PAT_MATCH_REG }, |
| 253 | { "map_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_STR, PAT_MATCH_INT }, |
| 254 | { "map_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_STR, PAT_MATCH_IP }, |
| 255 | |
| 256 | { "map_str_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_STR }, |
| 257 | { "map_beg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_BEG }, |
| 258 | { "map_sub_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_SUB }, |
| 259 | { "map_dir_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DIR }, |
| 260 | { "map_dom_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_DOM }, |
| 261 | { "map_end_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_END }, |
| 262 | { "map_reg_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_UINT, PAT_MATCH_REG }, |
| 263 | { "map_int_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_UINT, PAT_MATCH_INT }, |
| 264 | { "map_ip_int", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_UINT, PAT_MATCH_IP }, |
| 265 | |
| 266 | { "map_str_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_STR }, |
| 267 | { "map_beg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_BEG }, |
| 268 | { "map_sub_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_SUB }, |
| 269 | { "map_dir_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DIR }, |
| 270 | { "map_dom_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_DOM }, |
| 271 | { "map_end_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_END }, |
| 272 | { "map_reg_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_STR, SMP_T_IPV4, PAT_MATCH_REG }, |
| 273 | { "map_int_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_UINT, SMP_T_IPV4, PAT_MATCH_INT }, |
| 274 | { "map_ip_ip", sample_conv_map, ARG2(1,STR,STR), sample_load_map, SMP_T_ADDR, SMP_T_IPV4, PAT_MATCH_IP }, |
| 275 | |
| 276 | { /* END */ }, |
| 277 | }}; |
| 278 | |
| 279 | __attribute__((constructor)) |
| 280 | static void __map_init(void) |
| 281 | { |
| 282 | /* register format conversion keywords */ |
| 283 | sample_register_convs(&sample_conv_kws); |
| 284 | } |