Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Patterns management functions. |
| 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 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 <string.h> |
| 14 | #include <arpa/inet.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 15 | |
| 16 | #include <proto/pattern.h> |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 17 | #include <proto/buffers.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 18 | #include <common/standard.h> |
| 19 | |
| 20 | /* static structure used on pattern_process if <p> is NULL*/ |
| 21 | static struct pattern spattern; |
| 22 | |
| 23 | /* trash chunk used for pattern conversions */ |
| 24 | static struct chunk trash_chunk; |
| 25 | |
| 26 | /* trash buffers used or pattern conversions */ |
| 27 | static char pattern_trash_buf1[BUFSIZE]; |
| 28 | static char pattern_trash_buf2[BUFSIZE]; |
| 29 | |
| 30 | /* pattern_trash_buf point on used buffer*/ |
| 31 | static char *pattern_trash_buf = pattern_trash_buf1; |
| 32 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 33 | /* list head of all known pattern fetch keywords */ |
| 34 | static struct pattern_fetch_kw_list pattern_fetches = { |
| 35 | .list = LIST_HEAD_INIT(pattern_fetches.list) |
| 36 | }; |
| 37 | |
| 38 | /* list head of all known pattern format conversion keywords */ |
| 39 | static struct pattern_conv_kw_list pattern_convs = { |
| 40 | .list = LIST_HEAD_INIT(pattern_convs.list) |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Registers the pattern fetch keyword list <kwl> as a list of valid keywords for next |
| 45 | * parsing sessions. |
| 46 | */ |
| 47 | void pattern_register_fetches(struct pattern_fetch_kw_list *pfkl) |
| 48 | { |
| 49 | LIST_ADDQ(&pattern_fetches.list, &pfkl->list); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Registers the pattern format coverstion keyword list <pckl> as a list of valid keywords for next |
| 54 | * parsing sessions. |
| 55 | */ |
| 56 | void pattern_register_convs(struct pattern_conv_kw_list *pckl) |
| 57 | { |
| 58 | LIST_ADDQ(&pattern_convs.list, &pckl->list); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Returns the pointer on pattern fetch keyword structure identified by |
| 63 | * string of <len> in buffer <kw>. |
| 64 | * |
| 65 | */ |
| 66 | struct pattern_fetch *find_pattern_fetch(const char *kw, int len) |
| 67 | { |
| 68 | int index; |
| 69 | struct pattern_fetch_kw_list *kwl; |
| 70 | |
| 71 | list_for_each_entry(kwl, &pattern_fetches.list, list) { |
| 72 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 73 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 74 | kwl->kw[index].kw[len] == '\0') |
| 75 | return &kwl->kw[index]; |
| 76 | } |
| 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Returns the pointer on pattern format conversion keyword structure identified by |
| 83 | * string of <len> in buffer <kw>. |
| 84 | * |
| 85 | */ |
| 86 | struct pattern_conv *find_pattern_conv(const char *kw, int len) |
| 87 | { |
| 88 | int index; |
| 89 | struct pattern_conv_kw_list *kwl; |
| 90 | |
| 91 | list_for_each_entry(kwl, &pattern_convs.list, list) { |
| 92 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 93 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 94 | kwl->kw[index].kw[len] == '\0') |
| 95 | return &kwl->kw[index]; |
| 96 | } |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* |
| 103 | * Returns a static trash struct chunk to use in pattern casts or format conversions |
| 104 | * Swiths the 2 available trash buffers to protect data during convert |
| 105 | */ |
| 106 | static struct chunk *get_trash_chunk(void) |
| 107 | { |
| 108 | if (pattern_trash_buf == pattern_trash_buf1) |
| 109 | pattern_trash_buf = pattern_trash_buf2; |
| 110 | else |
| 111 | pattern_trash_buf = pattern_trash_buf1; |
| 112 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 113 | chunk_init(&trash_chunk, pattern_trash_buf, BUFSIZE); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 114 | |
| 115 | return &trash_chunk; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Used to set pattern data from a struct chunk, could be the trash struct chunk |
| 120 | */ |
| 121 | static void pattern_data_setstring(union pattern_data *data, struct chunk *c) |
| 122 | { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 123 | chunk_initlen(&data->str, c->str, c->size, c->len); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /******************************************************************/ |
| 127 | /* Pattern casts functions */ |
| 128 | /******************************************************************/ |
| 129 | |
| 130 | static int c_ip2int(union pattern_data *data) |
| 131 | { |
| 132 | data->integer = ntohl(data->ip.s_addr); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | static int c_ip2str(union pattern_data *data) |
| 137 | { |
| 138 | struct chunk *trash = get_trash_chunk(); |
| 139 | |
| 140 | if (!inet_ntop(AF_INET, (void *)&data->ip, trash->str, trash->size)) |
| 141 | return 0; |
| 142 | |
| 143 | trash->len = strlen(trash->str); |
| 144 | pattern_data_setstring(data, trash); |
| 145 | |
| 146 | return 1; |
| 147 | } |
| 148 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 149 | static int c_ip2ipv6(union pattern_data *data) |
| 150 | { |
| 151 | v4tov6(&data->ipv6, &data->ip); |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | static int c_ipv62str(union pattern_data *data) |
| 156 | { |
| 157 | struct chunk *trash = get_trash_chunk(); |
| 158 | |
| 159 | if (!inet_ntop(AF_INET6, (void *)&data->ipv6, trash->str, trash->size)) |
| 160 | return 0; |
| 161 | |
| 162 | trash->len = strlen(trash->str); |
| 163 | pattern_data_setstring(data, trash); |
| 164 | |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | static int c_ipv62ip(union pattern_data *data) |
| 170 | { |
| 171 | return v6tov4(&data->ip, &data->ipv6); |
| 172 | } |
| 173 | */ |
| 174 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 175 | static int c_int2ip(union pattern_data *data) |
| 176 | { |
| 177 | data->ip.s_addr = htonl(data->integer); |
| 178 | return 1; |
| 179 | } |
| 180 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 181 | static int c_str2ip(union pattern_data *data) |
| 182 | { |
| 183 | if (!buf2ip(data->str.str, data->str.len, &data->ip)) |
| 184 | return 0; |
| 185 | return 1; |
| 186 | } |
| 187 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 188 | static int c_str2ipv6(union pattern_data *data) |
| 189 | { |
| 190 | return inet_pton(AF_INET6, data->str.str, &data->ipv6); |
| 191 | } |
| 192 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 193 | static int c_int2str(union pattern_data *data) |
| 194 | { |
| 195 | struct chunk *trash = get_trash_chunk(); |
| 196 | char *pos; |
| 197 | |
| 198 | pos = ultoa_r(data->integer, trash->str, trash->size); |
| 199 | |
| 200 | if (!pos) |
| 201 | return 0; |
| 202 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 203 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 204 | trash->str = pos; |
| 205 | trash->len = strlen(pos); |
| 206 | |
| 207 | pattern_data_setstring(data, trash); |
| 208 | |
| 209 | return 1; |
| 210 | } |
| 211 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 212 | static int c_datadup(union pattern_data *data) |
| 213 | { |
| 214 | struct chunk *trash = get_trash_chunk(); |
| 215 | |
| 216 | trash->len = data->str.len < trash->size ? data->str.len : trash->size; |
| 217 | memcpy(trash->str, data->str.str, trash->len); |
| 218 | |
| 219 | pattern_data_setstring(data, trash); |
| 220 | |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 225 | static int c_donothing(union pattern_data *data) |
| 226 | { |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | static int c_str2int(union pattern_data *data) |
| 231 | { |
| 232 | int i; |
| 233 | uint32_t ret = 0; |
| 234 | |
| 235 | for (i = 0; i < data->str.len; i++) { |
| 236 | uint32_t val = data->str.str[i] - '0'; |
| 237 | |
| 238 | if (val > 9) |
| 239 | break; |
| 240 | |
| 241 | ret = ret * 10 + val; |
| 242 | } |
| 243 | |
| 244 | data->integer = ret; |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | /*****************************************************************/ |
| 249 | /* Pattern casts matrix: */ |
| 250 | /* pattern_casts[from type][to type] */ |
| 251 | /* NULL pointer used for impossible pattern casts */ |
| 252 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 253 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 254 | typedef int (*pattern_cast_fct)(union pattern_data *data); |
| 255 | static pattern_cast_fct pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = { |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 256 | /* to: IP IPV6 INTEGER STRING DATA CONSTSTRING CONSTDATA */ |
| 257 | /* from: IP */ { c_donothing, c_ip2ipv6, c_ip2int, c_ip2str, NULL, c_ip2str, NULL }, |
| 258 | /* IPV6 */ { NULL, c_donothing, NULL, c_ipv62str, NULL, c_ipv62str, NULL }, |
| 259 | /* INTEGER */ { c_int2ip, NULL, c_donothing, c_int2str, NULL, c_int2str, NULL }, |
| 260 | /* STRING */ { c_str2ip, c_str2ipv6, c_str2int, c_donothing, c_donothing, c_donothing, c_donothing }, |
| 261 | /* DATA */ { NULL, NULL, NULL, NULL, c_donothing, NULL, c_donothing }, |
| 262 | /* CONSTSTRING */ { c_str2ip, c_str2ipv6, c_str2int, c_datadup, c_datadup, c_donothing, c_donothing }, |
| 263 | /* CONSTDATA */ { NULL, NULL, NULL, NULL, c_datadup, NULL, c_donothing }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 264 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 265 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 266 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 267 | /* |
| 268 | * Parse a pattern expression configuration: |
| 269 | * fetch keyword followed by format conversion keywords. |
| 270 | * Returns a pointer on allocated pattern expression structure. |
| 271 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 272 | struct pattern_expr *pattern_parse_expr(char **str, int *idx, char *err, int err_size) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 273 | { |
| 274 | const char *endw; |
| 275 | const char *end; |
| 276 | struct pattern_expr *expr; |
| 277 | struct pattern_fetch *fetch; |
| 278 | struct pattern_conv *conv; |
| 279 | unsigned long prev_type; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 280 | char *p; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 281 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 282 | snprintf(err, err_size, "memory error."); |
| 283 | if (!str[*idx]) { |
| 284 | |
| 285 | snprintf(err, err_size, "missing fetch method."); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 286 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 287 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 288 | |
| 289 | end = str[*idx] + strlen(str[*idx]); |
| 290 | endw = strchr(str[*idx], '('); |
| 291 | |
| 292 | if (!endw) |
| 293 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 294 | else if ((end-1)[0] != ')') { |
| 295 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 296 | if (p) { |
| 297 | snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p); |
| 298 | free(p); |
| 299 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 300 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 301 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 302 | |
| 303 | fetch = find_pattern_fetch(str[*idx], endw - str[*idx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 304 | if (!fetch) { |
| 305 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 306 | if (p) { |
| 307 | snprintf(err, err_size, "unknown fetch method '%s'.", p); |
| 308 | free(p); |
| 309 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 310 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 311 | } |
| 312 | if (fetch->out_type >= PATTERN_TYPES) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 313 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 314 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 315 | if (p) { |
| 316 | snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p); |
| 317 | free(p); |
| 318 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 319 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 320 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 321 | |
| 322 | prev_type = fetch->out_type; |
| 323 | expr = calloc(1, sizeof(struct pattern_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 324 | if (!expr) |
| 325 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 326 | |
| 327 | LIST_INIT(&(expr->conv_exprs)); |
| 328 | expr->fetch = fetch; |
| 329 | |
| 330 | if (end != endw) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 331 | int i = end - endw - 2; |
| 332 | |
| 333 | if (!fetch->parse_args) { |
| 334 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 335 | if (p) { |
| 336 | snprintf(err, err_size, "fetch method '%s' does not support any args.", p); |
| 337 | free(p); |
| 338 | } |
| 339 | goto out_error; |
| 340 | } |
| 341 | p = my_strndup(endw + 1, i); |
| 342 | if (!p) |
| 343 | goto out_error; |
| 344 | i = fetch->parse_args(p, &expr->arg_p, &expr->arg_i); |
| 345 | free(p); |
| 346 | if (!i) { |
| 347 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 348 | if (p) { |
| 349 | snprintf(err, err_size, "invalid args in fetch method '%s'.", p); |
| 350 | free(p); |
| 351 | } |
| 352 | goto out_error; |
| 353 | } |
| 354 | } |
| 355 | else if (fetch->parse_args) { |
| 356 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 357 | if (p) { |
| 358 | snprintf(err, err_size, "missing args for fetch method '%s'.", p); |
| 359 | free(p); |
| 360 | } |
| 361 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | for (*idx += 1; *(str[*idx]); (*idx)++) { |
| 365 | struct pattern_conv_expr *conv_expr; |
| 366 | |
| 367 | end = str[*idx] + strlen(str[*idx]); |
| 368 | endw = strchr(str[*idx], '('); |
| 369 | |
| 370 | if (!endw) |
| 371 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 372 | else if ((end-1)[0] != ')') { |
| 373 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 374 | if (p) { |
| 375 | snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p); |
| 376 | free(p); |
| 377 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 378 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 379 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 380 | |
| 381 | conv = find_pattern_conv(str[*idx], endw - str[*idx]); |
| 382 | if (!conv) |
| 383 | break; |
| 384 | |
| 385 | if (conv->in_type >= PATTERN_TYPES || |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 386 | conv->out_type >= PATTERN_TYPES) { |
| 387 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 388 | if (p) { |
| 389 | snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p); |
| 390 | free(p); |
| 391 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 392 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 393 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 394 | |
| 395 | /* If impossible type conversion */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 396 | if (!pattern_casts[prev_type][conv->in_type]) { |
| 397 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 398 | if (p) { |
| 399 | snprintf(err, err_size, "conv method '%s' cannot be applied.", p); |
| 400 | free(p); |
| 401 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 402 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 403 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 404 | |
| 405 | prev_type = conv->out_type; |
| 406 | conv_expr = calloc(1, sizeof(struct pattern_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 407 | if (!conv_expr) |
| 408 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 409 | |
| 410 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 411 | conv_expr->conv = conv; |
| 412 | |
| 413 | if (end != endw) { |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 414 | int i = end - endw - 2; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 415 | |
| 416 | if (!conv->parse_args) { |
| 417 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 418 | |
| 419 | if (p) { |
| 420 | snprintf(err, err_size, "conv method '%s' does not support any args.", p); |
| 421 | free(p); |
| 422 | } |
| 423 | goto out_error; |
| 424 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 425 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 426 | p = my_strndup(endw + 1, i); |
| 427 | if (!p) |
| 428 | goto out_error; |
| 429 | i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i); |
| 430 | free(p); |
| 431 | if (!i) { |
| 432 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 433 | if (p) { |
| 434 | snprintf(err, err_size, "invalid args in conv method '%s'.", p); |
| 435 | free(p); |
| 436 | } |
| 437 | goto out_error; |
| 438 | } |
| 439 | } |
| 440 | else if (conv->parse_args) { |
| 441 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 442 | if (p) { |
| 443 | snprintf(err, err_size, "missing args for conv method '%s'.", p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 444 | free(p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 445 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 446 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 447 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 448 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 449 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 450 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 451 | return expr; |
| 452 | |
| 453 | out_error: |
| 454 | /* TODO: prune_pattern_expr(expr); */ |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Process a fetch + format conversion of defined by the pattern expression <expr> |
| 460 | * on request or response considering the <dir> parameter. |
| 461 | * Returns a pointer on a typed pattern structure containing the result or NULL if |
| 462 | * pattern is not found or when format conversion failed. |
| 463 | * If <p> is not null, function returns results in structure pointed by <p>. |
| 464 | * If <p> is null, functions returns a pointer on a static pattern structure. |
| 465 | */ |
| 466 | struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir, |
| 467 | struct pattern_expr *expr, struct pattern *p) |
| 468 | { |
| 469 | struct pattern_conv_expr *conv_expr; |
| 470 | |
| 471 | if (p == NULL) |
| 472 | p = &spattern; |
| 473 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 474 | if (!expr->fetch->process(px, l4, l7, dir, expr->arg_p, expr->arg_i, &p->data)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 475 | return NULL; |
| 476 | |
| 477 | p->type = expr->fetch->out_type; |
| 478 | |
| 479 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
| 480 | if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data)) |
| 481 | return NULL; |
| 482 | |
| 483 | p->type = conv_expr->conv->in_type; |
Willy Tarreau | 1a51b63 | 2010-01-26 17:17:56 +0100 | [diff] [blame] | 484 | if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 485 | return NULL; |
| 486 | |
| 487 | p->type = conv_expr->conv->out_type; |
| 488 | } |
| 489 | return p; |
| 490 | } |
| 491 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 492 | /* Converts an argument string mask to a pattern_arg type IP. |
| 493 | * Returns non-zero in case of success, 0 on error. |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 494 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 495 | int pattern_arg_ipmask(const char *arg_str, struct pattern_arg **arg_p, int *arg_i) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 496 | { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 497 | *arg_i = 1; |
| 498 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 499 | (*arg_p)->type = PATTERN_ARG_TYPE_IP; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 500 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 501 | if (!str2mask(arg_str, &(*arg_p)->data.ip)) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 502 | return 0; |
| 503 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 504 | return 1; |
| 505 | } |
| 506 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 507 | |
| 508 | /* Converts an argument string to a pattern_arg type STRING. |
| 509 | * Returns non-zero in case of success, 0 on error. |
| 510 | */ |
| 511 | int pattern_arg_str(const char *arg_str, struct pattern_arg **arg_p, int *arg_i) |
| 512 | { |
| 513 | *arg_i = 1; |
| 514 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 515 | (*arg_p)->type = PATTERN_ARG_TYPE_STRING; |
| 516 | (*arg_p)->data.str.str = strdup(arg_str); |
| 517 | (*arg_p)->data.str.len = strlen(arg_str); |
| 518 | |
| 519 | |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 524 | /*****************************************************************/ |
| 525 | /* Pattern format convert functions */ |
| 526 | /*****************************************************************/ |
| 527 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 528 | static int pattern_conv_str2lower(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 529 | { |
| 530 | int i; |
| 531 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 532 | if (!data->str.size) |
| 533 | return 0; |
| 534 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 535 | for (i = 0; i < data->str.len; i++) { |
| 536 | if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z')) |
| 537 | data->str.str[i] += 'a' - 'A'; |
| 538 | } |
| 539 | return 1; |
| 540 | } |
| 541 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 542 | static int pattern_conv_str2upper(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 543 | { |
| 544 | int i; |
| 545 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 546 | if (!data->str.size) |
| 547 | return 0; |
| 548 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 549 | for (i = 0; i < data->str.len; i++) { |
| 550 | if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z')) |
| 551 | data->str.str[i] += 'A' - 'a'; |
| 552 | } |
| 553 | return 1; |
| 554 | } |
| 555 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 556 | /* takes the netmask in arg_i */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 557 | static int pattern_conv_ipmask(const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 558 | { |
Simon Horman | d281eed | 2011-08-13 08:03:49 +0900 | [diff] [blame] | 559 | data->ip.s_addr &= arg_p->data.ip.s_addr; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 560 | return 1; |
| 561 | } |
| 562 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 563 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 564 | static struct pattern_conv_kw_list pattern_conv_kws = {{ },{ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 565 | { "upper", pattern_conv_str2upper, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING }, |
| 566 | { "lower", pattern_conv_str2lower, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING }, |
| 567 | { "ipmask", pattern_conv_ipmask, pattern_arg_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP }, |
| 568 | { NULL, NULL, NULL, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 569 | }}; |
| 570 | |
| 571 | __attribute__((constructor)) |
| 572 | static void __pattern_init(void) |
| 573 | { |
| 574 | /* register pattern format convert keywords */ |
| 575 | pattern_register_convs(&pattern_conv_kws); |
| 576 | } |