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 | |
| 149 | static int c_int2ip(union pattern_data *data) |
| 150 | { |
| 151 | data->ip.s_addr = htonl(data->integer); |
| 152 | return 1; |
| 153 | } |
| 154 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 155 | static int c_str2ip(union pattern_data *data) |
| 156 | { |
| 157 | if (!buf2ip(data->str.str, data->str.len, &data->ip)) |
| 158 | return 0; |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | static int c_int2str(union pattern_data *data) |
| 163 | { |
| 164 | struct chunk *trash = get_trash_chunk(); |
| 165 | char *pos; |
| 166 | |
| 167 | pos = ultoa_r(data->integer, trash->str, trash->size); |
| 168 | |
| 169 | if (!pos) |
| 170 | return 0; |
| 171 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 172 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 173 | trash->str = pos; |
| 174 | trash->len = strlen(pos); |
| 175 | |
| 176 | pattern_data_setstring(data, trash); |
| 177 | |
| 178 | return 1; |
| 179 | } |
| 180 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 181 | static int c_datadup(union pattern_data *data) |
| 182 | { |
| 183 | struct chunk *trash = get_trash_chunk(); |
| 184 | |
| 185 | trash->len = data->str.len < trash->size ? data->str.len : trash->size; |
| 186 | memcpy(trash->str, data->str.str, trash->len); |
| 187 | |
| 188 | pattern_data_setstring(data, trash); |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 194 | static int c_donothing(union pattern_data *data) |
| 195 | { |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | static int c_str2int(union pattern_data *data) |
| 200 | { |
| 201 | int i; |
| 202 | uint32_t ret = 0; |
| 203 | |
| 204 | for (i = 0; i < data->str.len; i++) { |
| 205 | uint32_t val = data->str.str[i] - '0'; |
| 206 | |
| 207 | if (val > 9) |
| 208 | break; |
| 209 | |
| 210 | ret = ret * 10 + val; |
| 211 | } |
| 212 | |
| 213 | data->integer = ret; |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | /*****************************************************************/ |
| 218 | /* Pattern casts matrix: */ |
| 219 | /* pattern_casts[from type][to type] */ |
| 220 | /* NULL pointer used for impossible pattern casts */ |
| 221 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 222 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 223 | typedef int (*pattern_cast_fct)(union pattern_data *data); |
| 224 | static pattern_cast_fct pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 225 | /* to: IP INTEGER STRING DATA CONSTSTRING CONSTDATA */ |
| 226 | /* from: IP */ { c_donothing, c_ip2int, c_ip2str, NULL, c_ip2str, NULL }, |
| 227 | /* INTEGER */ { c_int2ip, c_donothing, c_int2str, NULL, c_int2str, NULL }, |
| 228 | /* STRING */ { c_str2ip, c_str2int, c_donothing, c_donothing, c_donothing, c_donothing }, |
| 229 | /* DATA */ { NULL, NULL, NULL, c_donothing, NULL, c_donothing }, |
| 230 | /* CONSTSTRING */ { c_str2ip, c_str2int, c_datadup, c_datadup, c_donothing, c_donothing }, |
| 231 | /* CONSTDATA */ { NULL, NULL, NULL, c_datadup, NULL, NULL }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 232 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 233 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 234 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Parse a pattern expression configuration: |
| 237 | * fetch keyword followed by format conversion keywords. |
| 238 | * Returns a pointer on allocated pattern expression structure. |
| 239 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 240 | 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] | 241 | { |
| 242 | const char *endw; |
| 243 | const char *end; |
| 244 | struct pattern_expr *expr; |
| 245 | struct pattern_fetch *fetch; |
| 246 | struct pattern_conv *conv; |
| 247 | unsigned long prev_type; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 248 | char *p; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 249 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 250 | snprintf(err, err_size, "memory error."); |
| 251 | if (!str[*idx]) { |
| 252 | |
| 253 | snprintf(err, err_size, "missing fetch method."); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 254 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 255 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 256 | |
| 257 | end = str[*idx] + strlen(str[*idx]); |
| 258 | endw = strchr(str[*idx], '('); |
| 259 | |
| 260 | if (!endw) |
| 261 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 262 | else if ((end-1)[0] != ')') { |
| 263 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 264 | if (p) { |
| 265 | snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p); |
| 266 | free(p); |
| 267 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 268 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 269 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 270 | |
| 271 | fetch = find_pattern_fetch(str[*idx], endw - str[*idx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 272 | if (!fetch) { |
| 273 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 274 | if (p) { |
| 275 | snprintf(err, err_size, "unknown fetch method '%s'.", p); |
| 276 | free(p); |
| 277 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 278 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 279 | } |
| 280 | if (fetch->out_type >= PATTERN_TYPES) { |
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 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 283 | if (p) { |
| 284 | snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p); |
| 285 | free(p); |
| 286 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 287 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 288 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 289 | |
| 290 | prev_type = fetch->out_type; |
| 291 | expr = calloc(1, sizeof(struct pattern_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 292 | if (!expr) |
| 293 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 294 | |
| 295 | LIST_INIT(&(expr->conv_exprs)); |
| 296 | expr->fetch = fetch; |
| 297 | |
| 298 | if (end != endw) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 299 | int i = end - endw - 2; |
| 300 | |
| 301 | if (!fetch->parse_args) { |
| 302 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 303 | if (p) { |
| 304 | snprintf(err, err_size, "fetch method '%s' does not support any args.", p); |
| 305 | free(p); |
| 306 | } |
| 307 | goto out_error; |
| 308 | } |
| 309 | p = my_strndup(endw + 1, i); |
| 310 | if (!p) |
| 311 | goto out_error; |
| 312 | i = fetch->parse_args(p, &expr->arg_p, &expr->arg_i); |
| 313 | free(p); |
| 314 | if (!i) { |
| 315 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 316 | if (p) { |
| 317 | snprintf(err, err_size, "invalid args in fetch method '%s'.", p); |
| 318 | free(p); |
| 319 | } |
| 320 | goto out_error; |
| 321 | } |
| 322 | } |
| 323 | else if (fetch->parse_args) { |
| 324 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 325 | if (p) { |
| 326 | snprintf(err, err_size, "missing args for fetch method '%s'.", p); |
| 327 | free(p); |
| 328 | } |
| 329 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | for (*idx += 1; *(str[*idx]); (*idx)++) { |
| 333 | struct pattern_conv_expr *conv_expr; |
| 334 | |
| 335 | end = str[*idx] + strlen(str[*idx]); |
| 336 | endw = strchr(str[*idx], '('); |
| 337 | |
| 338 | if (!endw) |
| 339 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 340 | else if ((end-1)[0] != ')') { |
| 341 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 342 | if (p) { |
| 343 | snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p); |
| 344 | free(p); |
| 345 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 346 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 347 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 348 | |
| 349 | conv = find_pattern_conv(str[*idx], endw - str[*idx]); |
| 350 | if (!conv) |
| 351 | break; |
| 352 | |
| 353 | if (conv->in_type >= PATTERN_TYPES || |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 354 | conv->out_type >= PATTERN_TYPES) { |
| 355 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 356 | if (p) { |
| 357 | snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p); |
| 358 | free(p); |
| 359 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 360 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 361 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 362 | |
| 363 | /* If impossible type conversion */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 364 | if (!pattern_casts[prev_type][conv->in_type]) { |
| 365 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 366 | if (p) { |
| 367 | snprintf(err, err_size, "conv method '%s' cannot be applied.", p); |
| 368 | free(p); |
| 369 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 370 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 371 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 372 | |
| 373 | prev_type = conv->out_type; |
| 374 | conv_expr = calloc(1, sizeof(struct pattern_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 375 | if (!conv_expr) |
| 376 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 377 | |
| 378 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 379 | conv_expr->conv = conv; |
| 380 | |
| 381 | if (end != endw) { |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 382 | int i = end - endw - 2; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 383 | |
| 384 | if (!conv->parse_args) { |
| 385 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 386 | |
| 387 | if (p) { |
| 388 | snprintf(err, err_size, "conv method '%s' does not support any args.", p); |
| 389 | free(p); |
| 390 | } |
| 391 | goto out_error; |
| 392 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 393 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 394 | p = my_strndup(endw + 1, i); |
| 395 | if (!p) |
| 396 | goto out_error; |
| 397 | i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i); |
| 398 | free(p); |
| 399 | if (!i) { |
| 400 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 401 | if (p) { |
| 402 | snprintf(err, err_size, "invalid args in conv method '%s'.", p); |
| 403 | free(p); |
| 404 | } |
| 405 | goto out_error; |
| 406 | } |
| 407 | } |
| 408 | else if (conv->parse_args) { |
| 409 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 410 | if (p) { |
| 411 | snprintf(err, err_size, "missing args for conv method '%s'.", p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 412 | free(p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 413 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 414 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 415 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 416 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 417 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 418 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 419 | return expr; |
| 420 | |
| 421 | out_error: |
| 422 | /* TODO: prune_pattern_expr(expr); */ |
| 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Process a fetch + format conversion of defined by the pattern expression <expr> |
| 428 | * on request or response considering the <dir> parameter. |
| 429 | * Returns a pointer on a typed pattern structure containing the result or NULL if |
| 430 | * pattern is not found or when format conversion failed. |
| 431 | * If <p> is not null, function returns results in structure pointed by <p>. |
| 432 | * If <p> is null, functions returns a pointer on a static pattern structure. |
| 433 | */ |
| 434 | struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir, |
| 435 | struct pattern_expr *expr, struct pattern *p) |
| 436 | { |
| 437 | struct pattern_conv_expr *conv_expr; |
| 438 | |
| 439 | if (p == NULL) |
| 440 | p = &spattern; |
| 441 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 442 | 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] | 443 | return NULL; |
| 444 | |
| 445 | p->type = expr->fetch->out_type; |
| 446 | |
| 447 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
| 448 | if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data)) |
| 449 | return NULL; |
| 450 | |
| 451 | p->type = conv_expr->conv->in_type; |
Willy Tarreau | 1a51b63 | 2010-01-26 17:17:56 +0100 | [diff] [blame] | 452 | 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] | 453 | return NULL; |
| 454 | |
| 455 | p->type = conv_expr->conv->out_type; |
| 456 | } |
| 457 | return p; |
| 458 | } |
| 459 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 460 | /* Converts an argument string mask to a pattern_arg type IP. |
| 461 | * Returns non-zero in case of success, 0 on error. |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 462 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 463 | 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] | 464 | { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 465 | *arg_i = 1; |
| 466 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 467 | (*arg_p)->type = PATTERN_ARG_TYPE_IP; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 468 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 469 | if (!str2mask(arg_str, &(*arg_p)->data.ip)) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 470 | return 0; |
| 471 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 472 | return 1; |
| 473 | } |
| 474 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 475 | |
| 476 | /* Converts an argument string to a pattern_arg type STRING. |
| 477 | * Returns non-zero in case of success, 0 on error. |
| 478 | */ |
| 479 | int pattern_arg_str(const char *arg_str, struct pattern_arg **arg_p, int *arg_i) |
| 480 | { |
| 481 | *arg_i = 1; |
| 482 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 483 | (*arg_p)->type = PATTERN_ARG_TYPE_STRING; |
| 484 | (*arg_p)->data.str.str = strdup(arg_str); |
| 485 | (*arg_p)->data.str.len = strlen(arg_str); |
| 486 | |
| 487 | |
| 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 492 | /*****************************************************************/ |
| 493 | /* Pattern format convert functions */ |
| 494 | /*****************************************************************/ |
| 495 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 496 | 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] | 497 | { |
| 498 | int i; |
| 499 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 500 | if (!data->str.size) |
| 501 | return 0; |
| 502 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 503 | for (i = 0; i < data->str.len; i++) { |
| 504 | if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z')) |
| 505 | data->str.str[i] += 'a' - 'A'; |
| 506 | } |
| 507 | return 1; |
| 508 | } |
| 509 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 510 | 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] | 511 | { |
| 512 | int i; |
| 513 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 514 | if (!data->str.size) |
| 515 | return 0; |
| 516 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 517 | for (i = 0; i < data->str.len; i++) { |
| 518 | if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z')) |
| 519 | data->str.str[i] += 'A' - 'a'; |
| 520 | } |
| 521 | return 1; |
| 522 | } |
| 523 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 524 | /* takes the netmask in arg_i */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 525 | 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] | 526 | { |
| 527 | data->ip.s_addr &= arg_i; |
| 528 | return 1; |
| 529 | } |
| 530 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 531 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 532 | static struct pattern_conv_kw_list pattern_conv_kws = {{ },{ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 533 | { "upper", pattern_conv_str2upper, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING }, |
| 534 | { "lower", pattern_conv_str2lower, NULL, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING }, |
| 535 | { "ipmask", pattern_conv_ipmask, pattern_arg_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP }, |
| 536 | { NULL, NULL, NULL, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 537 | }}; |
| 538 | |
| 539 | __attribute__((constructor)) |
| 540 | static void __pattern_init(void) |
| 541 | { |
| 542 | /* register pattern format convert keywords */ |
| 543 | pattern_register_convs(&pattern_conv_kws); |
| 544 | } |