Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Pattern 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 <ctype.h> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | #include <common/config.h> |
| 17 | #include <common/standard.h> |
| 18 | |
| 19 | #include <types/global.h> |
| 20 | #include <types/pattern.h> |
| 21 | |
| 22 | #include <proto/pattern.h> |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 23 | #include <proto/sample.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 24 | |
| 25 | #include <ebsttree.h> |
| 26 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 27 | char *pat_match_names[PAT_MATCH_NUM] = { |
| 28 | [PAT_MATCH_FOUND] = "found", |
| 29 | [PAT_MATCH_BOOL] = "bool", |
| 30 | [PAT_MATCH_INT] = "int", |
| 31 | [PAT_MATCH_IP] = "ip", |
| 32 | [PAT_MATCH_BIN] = "bin", |
| 33 | [PAT_MATCH_LEN] = "len", |
| 34 | [PAT_MATCH_STR] = "str", |
| 35 | [PAT_MATCH_BEG] = "beg", |
| 36 | [PAT_MATCH_SUB] = "sub", |
| 37 | [PAT_MATCH_DIR] = "dir", |
| 38 | [PAT_MATCH_DOM] = "dom", |
| 39 | [PAT_MATCH_END] = "end", |
| 40 | [PAT_MATCH_REG] = "reg", |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 43 | int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, enum pat_usage, int *, char **) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 44 | [PAT_MATCH_FOUND] = pat_parse_nothing, |
| 45 | [PAT_MATCH_BOOL] = pat_parse_nothing, |
| 46 | [PAT_MATCH_INT] = pat_parse_int, |
| 47 | [PAT_MATCH_IP] = pat_parse_ip, |
| 48 | [PAT_MATCH_BIN] = pat_parse_bin, |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 49 | [PAT_MATCH_LEN] = pat_parse_len, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 50 | [PAT_MATCH_STR] = pat_parse_str, |
| 51 | [PAT_MATCH_BEG] = pat_parse_str, |
| 52 | [PAT_MATCH_SUB] = pat_parse_str, |
| 53 | [PAT_MATCH_DIR] = pat_parse_str, |
| 54 | [PAT_MATCH_DOM] = pat_parse_str, |
| 55 | [PAT_MATCH_END] = pat_parse_str, |
| 56 | [PAT_MATCH_REG] = pat_parse_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 59 | enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 60 | [PAT_MATCH_FOUND] = NULL, |
| 61 | [PAT_MATCH_BOOL] = pat_match_nothing, |
| 62 | [PAT_MATCH_INT] = pat_match_int, |
| 63 | [PAT_MATCH_IP] = pat_match_ip, |
| 64 | [PAT_MATCH_BIN] = pat_match_bin, |
| 65 | [PAT_MATCH_LEN] = pat_match_len, |
| 66 | [PAT_MATCH_STR] = pat_match_str, |
| 67 | [PAT_MATCH_BEG] = pat_match_beg, |
| 68 | [PAT_MATCH_SUB] = pat_match_sub, |
| 69 | [PAT_MATCH_DIR] = pat_match_dir, |
| 70 | [PAT_MATCH_DOM] = pat_match_dom, |
| 71 | [PAT_MATCH_END] = pat_match_end, |
| 72 | [PAT_MATCH_REG] = pat_match_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 75 | /* Just used for checking configuration compatibility */ |
| 76 | int pat_match_types[PAT_MATCH_NUM] = { |
| 77 | [PAT_MATCH_FOUND] = SMP_T_UINT, |
| 78 | [PAT_MATCH_BOOL] = SMP_T_UINT, |
| 79 | [PAT_MATCH_INT] = SMP_T_UINT, |
| 80 | [PAT_MATCH_IP] = SMP_T_ADDR, |
| 81 | [PAT_MATCH_BIN] = SMP_T_CBIN, |
| 82 | [PAT_MATCH_LEN] = SMP_T_CSTR, |
| 83 | [PAT_MATCH_STR] = SMP_T_CSTR, |
| 84 | [PAT_MATCH_BEG] = SMP_T_CSTR, |
| 85 | [PAT_MATCH_SUB] = SMP_T_CSTR, |
| 86 | [PAT_MATCH_DIR] = SMP_T_CSTR, |
| 87 | [PAT_MATCH_DOM] = SMP_T_CSTR, |
| 88 | [PAT_MATCH_END] = SMP_T_CSTR, |
| 89 | [PAT_MATCH_REG] = SMP_T_CSTR, |
| 90 | }; |
| 91 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 92 | /* |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 93 | * |
| 94 | * The following functions are not exported and are used by internals process |
| 95 | * of pattern matching |
| 96 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 97 | */ |
| 98 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 99 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 100 | * match method. The node is returned if it exists, otherwise NULL. |
| 101 | */ |
| 102 | static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 103 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 104 | struct in_addr *s; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 105 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 106 | if (smp->type != SMP_T_IPV4) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 107 | return PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 108 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 109 | s = &smp->data.ipv4; |
| 110 | return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 113 | /* Free data allocated by pat_parse_reg */ |
| 114 | static void pat_free_reg(void *ptr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 115 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 116 | regex_free(ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* Lookup a string in the expression's pattern tree. The node is returned if it |
| 120 | * exists, otherwise NULL. |
| 121 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 122 | static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 123 | { |
| 124 | /* data are stored in a tree */ |
| 125 | struct ebmb_node *node; |
| 126 | char prev; |
| 127 | |
| 128 | /* we may have to force a trailing zero on the test pattern */ |
| 129 | prev = smp->data.str.str[smp->data.str.len]; |
| 130 | if (prev) |
| 131 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 132 | node = ebst_lookup(&expr->pattern_tree, smp->data.str.str); |
| 133 | if (prev) |
| 134 | smp->data.str.str[smp->data.str.len] = prev; |
| 135 | return node; |
| 136 | } |
| 137 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 138 | /* Background: Fast way to find a zero byte in a word |
| 139 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 140 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 141 | * |
| 142 | * To look for 4 different byte values, xor the word with those bytes and |
| 143 | * then check for zero bytes: |
| 144 | * |
| 145 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 146 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 147 | * and <c> is the character that is being tested |
| 148 | */ |
| 149 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 150 | { |
| 151 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 152 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 153 | } |
| 154 | |
| 155 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 156 | { |
| 157 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 158 | } |
| 159 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 160 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 161 | /* |
| 162 | * |
| 163 | * These functions are exported and may be used by any other component. |
| 164 | * |
| 165 | * The following functions are used for parsing pattern matching |
| 166 | * input value. The <text> contain a list of word. The last entry |
| 167 | * must be one NULL character. the <text> contain the string to be |
| 168 | * parsed. <pattern> must be a preallocated pattern. The pat_parse_* |
| 169 | * functions fill this structure with the parsed value. <usage> can |
| 170 | * be PAT_U_COMPILE or PAT_U_LOOKUP. If the value PAT_U_COMPILE is |
| 171 | * used memory is allocated for filling the pattern. If the value |
| 172 | * PAT_U_LOOKUP is set, the parser use "trash" or return pointers |
| 173 | * to the input strings. In both cases, the caller must use the |
| 174 | * value PAT_U_LOOKUP with caution. <opaque> is used to pass value |
| 175 | * between two calls to the parser. the interger must ben initilized |
| 176 | * to 0 (see note below). <err> is filled with an error message built |
| 177 | * with memprintf() function. |
| 178 | * |
| 179 | * In succes case, the pat_parse_* function return the number of |
| 180 | * <text> eated. If the function fail, it returns 0 and <err> is |
| 181 | * filled. |
| 182 | * |
| 183 | * NOTE: <opaque>iIt is used with integer range parser. The following |
| 184 | * configuration line is processed with this method: |
| 185 | * |
| 186 | * acl ... -m int eq 10 20 |
| 187 | * |
| 188 | * The first call to the parser eat 2 elements: "eq" and "10". The |
| 189 | * pattern is filled with "eq 10" content. The <opaque> contain |
| 190 | * coded value value that represent "eq". |
| 191 | * |
| 192 | * The second call to the parser just eat 1 element: "20". The opaque |
| 193 | * contain the value of the operator. The parser returns pattern filled |
| 194 | * with "eq 20". |
| 195 | * |
| 196 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 198 | /* ignore the current line */ |
| 199 | int pat_parse_nothing(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
| 200 | { |
| 201 | return 1; |
| 202 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 204 | /* Parse a string. It is allocated and duplicated. */ |
| 205 | int pat_parse_str(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
| 206 | { |
| 207 | pattern->type = SMP_T_CSTR; |
| 208 | pattern->expect_type = SMP_T_CSTR; |
| 209 | if (usage == PAT_U_COMPILE) { |
| 210 | pattern->ptr.str = strdup(*text); |
| 211 | if (!pattern->ptr.str) { |
| 212 | memprintf(err, "out of memory while loading string pattern"); |
| 213 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 214 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 215 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 216 | else |
| 217 | pattern->ptr.str = (char *)*text; |
| 218 | pattern->len = strlen(*text); |
| 219 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 222 | /* Parse a binary written in hexa. It is allocated. */ |
| 223 | int pat_parse_bin(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 224 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 225 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 226 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 227 | pattern->type = SMP_T_CBIN; |
| 228 | pattern->expect_type = SMP_T_CBIN; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 229 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 230 | if (usage == PAT_U_COMPILE) |
| 231 | /* If the parse_binary fails, it returns 0. In succes case, it returns |
| 232 | * the length of the arsed binary content. The functions pat_parse_* |
| 233 | * must return 0 if fail and the number of elements eated from **text |
| 234 | * if not fail. In succes case, this function eat always 1 elements. |
| 235 | * The double operator "!" converts the range "1-n" to "1". |
| 236 | */ |
| 237 | return !!parse_binary(*text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 238 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 239 | trash = get_trash_chunk(); |
| 240 | pattern->len = trash->size; |
| 241 | pattern->ptr.str = trash->str; |
| 242 | return !!parse_binary(*text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 245 | /* Parse and concatenate all further strings into one. */ |
| 246 | int |
| 247 | pat_parse_strcat(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 248 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 249 | int len = 0, i; |
| 250 | char *s; |
| 251 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 252 | |
| 253 | for (i = 0; *text[i]; i++) |
| 254 | len += strlen(text[i])+1; |
| 255 | |
| 256 | pattern->type = SMP_T_CSTR; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 257 | if (usage == PAT_U_COMPILE) { |
| 258 | pattern->ptr.str = calloc(1, len); |
| 259 | if (!pattern->ptr.str) { |
| 260 | memprintf(err, "out of memory while loading pattern"); |
| 261 | return 0; |
| 262 | } |
| 263 | } |
| 264 | else { |
| 265 | trash = get_trash_chunk(); |
| 266 | if (trash->size < len) { |
| 267 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 268 | len, trash->size); |
| 269 | return 0; |
| 270 | } |
| 271 | pattern->ptr.str = trash->str; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 274 | s = pattern->ptr.str; |
| 275 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 276 | for (i = 0; *text[i]; i++) |
| 277 | s += sprintf(s, i?" %s":"%s", text[i]); |
| 278 | |
| 279 | pattern->len = len; |
| 280 | |
| 281 | return i; |
| 282 | } |
| 283 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 284 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 285 | int pat_parse_reg(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 286 | { |
Thierry FOURNIER | 799c042 | 2013-12-06 20:36:20 +0100 | [diff] [blame] | 287 | struct my_regex *preg; |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 288 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 289 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 290 | if (usage == PAT_U_COMPILE) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 291 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 292 | preg = calloc(1, sizeof(*preg)); |
| 293 | if (!preg) { |
| 294 | memprintf(err, "out of memory while loading pattern"); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | if (!regex_comp(*text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) { |
| 299 | free(preg); |
| 300 | return 0; |
| 301 | } |
| 302 | pattern->freeptrbuf = &pat_free_reg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 303 | } |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 304 | else { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 305 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 306 | trash = get_trash_chunk(); |
| 307 | if (trash->size < sizeof(*preg)) { |
| 308 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 309 | (int)sizeof(*preg), trash->size); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | preg = (struct my_regex *)trash->str; |
| 314 | preg->regstr = (char *)*text; |
| 315 | pattern->freeptrbuf = NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | pattern->ptr.reg = preg; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 319 | pattern->expect_type = SMP_T_CSTR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 324 | * one integer is read, it is set as both min and max. An operator may be |
| 325 | * specified as the prefix, among this list of 5 : |
| 326 | * |
| 327 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 328 | * |
| 329 | * The default operator is "eq". It supports range matching. Ranges are |
| 330 | * rejected for other operators. The operator may be changed at any time. |
| 331 | * The operator is stored in the 'opaque' argument. |
| 332 | * |
| 333 | * If err is non-NULL, an error message will be returned there on errors and |
| 334 | * the caller will have to free it. |
| 335 | * |
| 336 | */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 337 | int pat_parse_int(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 338 | { |
| 339 | signed long long i; |
| 340 | unsigned int j, last, skip = 0; |
| 341 | const char *ptr = *text; |
| 342 | |
| 343 | pattern->type = SMP_T_UINT; |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 344 | pattern->expect_type = SMP_T_UINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 345 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 346 | while (!isdigit((unsigned char)*ptr)) { |
| 347 | switch (get_std_op(ptr)) { |
| 348 | case STD_OP_EQ: *opaque = 0; break; |
| 349 | case STD_OP_GT: *opaque = 1; break; |
| 350 | case STD_OP_GE: *opaque = 2; break; |
| 351 | case STD_OP_LT: *opaque = 3; break; |
| 352 | case STD_OP_LE: *opaque = 4; break; |
| 353 | default: |
| 354 | memprintf(err, "'%s' is neither a number nor a supported operator", ptr); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | skip++; |
| 359 | ptr = text[skip]; |
| 360 | } |
| 361 | |
| 362 | last = i = 0; |
| 363 | while (1) { |
| 364 | j = *ptr++; |
| 365 | if ((j == '-' || j == ':') && !last) { |
| 366 | last++; |
| 367 | pattern->val.range.min = i; |
| 368 | i = 0; |
| 369 | continue; |
| 370 | } |
| 371 | j -= '0'; |
| 372 | if (j > 9) |
| 373 | // also catches the terminating zero |
| 374 | break; |
| 375 | i *= 10; |
| 376 | i += j; |
| 377 | } |
| 378 | |
| 379 | if (last && *opaque >= 1 && *opaque <= 4) { |
| 380 | /* having a range with a min or a max is absurd */ |
| 381 | memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | if (!last) |
| 386 | pattern->val.range.min = i; |
| 387 | pattern->val.range.max = i; |
| 388 | |
| 389 | switch (*opaque) { |
| 390 | case 0: /* eq */ |
| 391 | pattern->val.range.min_set = 1; |
| 392 | pattern->val.range.max_set = 1; |
| 393 | break; |
| 394 | case 1: /* gt */ |
| 395 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 396 | case 2: /* ge */ |
| 397 | pattern->val.range.min_set = 1; |
| 398 | pattern->val.range.max_set = 0; |
| 399 | break; |
| 400 | case 3: /* lt */ |
| 401 | pattern->val.range.max--; /* lt = le - 1 */ |
| 402 | case 4: /* le */ |
| 403 | pattern->val.range.min_set = 0; |
| 404 | pattern->val.range.max_set = 1; |
| 405 | break; |
| 406 | } |
| 407 | return skip + 1; |
| 408 | } |
| 409 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 410 | int pat_parse_len(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 411 | { |
| 412 | int ret; |
| 413 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 414 | ret = pat_parse_int(text, pattern, usage, opaque, err); |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 415 | pattern->expect_type = SMP_T_CSTR; |
| 416 | return ret; |
| 417 | } |
| 418 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 419 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 420 | * '-'. The version consists in a major and a minor, both of which must be |
| 421 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 422 | * integer. |
| 423 | * If only one version is read, it is set as both min and max. Just like for |
| 424 | * pure integers, an operator may be specified as the prefix, among this list |
| 425 | * of 5 : |
| 426 | * |
| 427 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 428 | * |
| 429 | * The default operator is "eq". It supports range matching. Ranges are |
| 430 | * rejected for other operators. The operator may be changed at any time. |
| 431 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 432 | * such as the following one : |
| 433 | * |
| 434 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 435 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 436 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 437 | * |
| 438 | */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 439 | int pat_parse_dotted_ver(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 440 | { |
| 441 | signed long long i; |
| 442 | unsigned int j, last, skip = 0; |
| 443 | const char *ptr = *text; |
| 444 | |
| 445 | |
| 446 | while (!isdigit((unsigned char)*ptr)) { |
| 447 | switch (get_std_op(ptr)) { |
| 448 | case STD_OP_EQ: *opaque = 0; break; |
| 449 | case STD_OP_GT: *opaque = 1; break; |
| 450 | case STD_OP_GE: *opaque = 2; break; |
| 451 | case STD_OP_LT: *opaque = 3; break; |
| 452 | case STD_OP_LE: *opaque = 4; break; |
| 453 | default: |
| 454 | memprintf(err, "'%s' is neither a number nor a supported operator", ptr); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | skip++; |
| 459 | ptr = text[skip]; |
| 460 | } |
| 461 | |
| 462 | last = i = 0; |
| 463 | while (1) { |
| 464 | j = *ptr++; |
| 465 | if (j == '.') { |
| 466 | /* minor part */ |
| 467 | if (i >= 65536) |
| 468 | return 0; |
| 469 | i <<= 16; |
| 470 | continue; |
| 471 | } |
| 472 | if ((j == '-' || j == ':') && !last) { |
| 473 | last++; |
| 474 | if (i < 65536) |
| 475 | i <<= 16; |
| 476 | pattern->val.range.min = i; |
| 477 | i = 0; |
| 478 | continue; |
| 479 | } |
| 480 | j -= '0'; |
| 481 | if (j > 9) |
| 482 | // also catches the terminating zero |
| 483 | break; |
| 484 | i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10; |
| 485 | i += j; |
| 486 | } |
| 487 | |
| 488 | /* if we only got a major version, let's shift it now */ |
| 489 | if (i < 65536) |
| 490 | i <<= 16; |
| 491 | |
| 492 | if (last && *opaque >= 1 && *opaque <= 4) { |
| 493 | /* having a range with a min or a max is absurd */ |
| 494 | memprintf(err, "version range '%s' specified with a comparison operator", text[skip]); |
| 495 | return 0; |
| 496 | } |
| 497 | |
Thierry FOURNIER | 59ad9d6 | 2014-01-27 16:04:43 +0100 | [diff] [blame] | 498 | pattern->expect_type = SMP_T_UINT; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 499 | |
| 500 | if (!last) |
| 501 | pattern->val.range.min = i; |
| 502 | pattern->val.range.max = i; |
| 503 | |
| 504 | switch (*opaque) { |
| 505 | case 0: /* eq */ |
| 506 | pattern->val.range.min_set = 1; |
| 507 | pattern->val.range.max_set = 1; |
| 508 | break; |
| 509 | case 1: /* gt */ |
| 510 | pattern->val.range.min++; /* gt = ge + 1 */ |
| 511 | case 2: /* ge */ |
| 512 | pattern->val.range.min_set = 1; |
| 513 | pattern->val.range.max_set = 0; |
| 514 | break; |
| 515 | case 3: /* lt */ |
| 516 | pattern->val.range.max--; /* lt = le - 1 */ |
| 517 | case 4: /* le */ |
| 518 | pattern->val.range.min_set = 0; |
| 519 | pattern->val.range.max_set = 1; |
| 520 | break; |
| 521 | } |
| 522 | return skip + 1; |
| 523 | } |
| 524 | |
| 525 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 526 | * The addr may either be an IPv4 address or a hostname. The mask |
| 527 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 528 | * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6). |
| 529 | */ |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 530 | int pat_parse_ip(const char **text, struct pattern *pattern, enum pat_usage usage, int *opaque, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 531 | { |
Thierry FOURNIER | cc0e0b3 | 2013-12-06 16:56:40 +0100 | [diff] [blame] | 532 | pattern->expect_type = SMP_T_ADDR; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 533 | if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 534 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) { |
| 538 | /* no tree support right now */ |
| 539 | pattern->type = SMP_T_IPV6; |
| 540 | return 1; |
| 541 | } |
| 542 | else { |
| 543 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text); |
| 544 | return 0; |
| 545 | } |
| 546 | } |
| 547 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 548 | /* |
| 549 | * |
| 550 | * These functions are exported and may be used by any other component. |
| 551 | * |
| 552 | * This fucntion just take a sample <smp> and check if this sample match |
| 553 | * with the pattern <pattern>. This fucntion return just PAT_MATCH or |
| 554 | * PAT_NOMATCH. |
| 555 | * |
| 556 | */ |
| 557 | |
| 558 | /* always return false */ |
| 559 | enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern) |
| 560 | { |
| 561 | return PAT_NOMATCH; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 566 | enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern) |
| 567 | { |
| 568 | int icase; |
| 569 | |
| 570 | if (pattern->len != smp->data.str.len) |
| 571 | return PAT_NOMATCH; |
| 572 | |
| 573 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 574 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) || |
| 575 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)) |
| 576 | return PAT_MATCH; |
| 577 | return PAT_NOMATCH; |
| 578 | } |
| 579 | |
| 580 | /* NB: For two binaries buf to be identical, it is required that their lengths match */ |
| 581 | enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern) |
| 582 | { |
| 583 | if (pattern->len != smp->data.str.len) |
| 584 | return PAT_NOMATCH; |
| 585 | |
| 586 | if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) |
| 587 | return PAT_MATCH; |
| 588 | return PAT_NOMATCH; |
| 589 | } |
| 590 | |
| 591 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 592 | * and restores the previous character when leaving. |
| 593 | */ |
| 594 | enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern) |
| 595 | { |
| 596 | if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0) |
| 597 | return PAT_MATCH; |
| 598 | return PAT_NOMATCH; |
| 599 | } |
| 600 | |
| 601 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 602 | enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern) |
| 603 | { |
| 604 | int icase; |
| 605 | |
| 606 | if (pattern->len > smp->data.str.len) |
| 607 | return PAT_NOMATCH; |
| 608 | |
| 609 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 610 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) || |
| 611 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0)) |
| 612 | return PAT_NOMATCH; |
| 613 | return PAT_MATCH; |
| 614 | } |
| 615 | |
| 616 | /* Checks that the pattern matches the end of the tested string. */ |
| 617 | enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern) |
| 618 | { |
| 619 | int icase; |
| 620 | |
| 621 | if (pattern->len > smp->data.str.len) |
| 622 | return PAT_NOMATCH; |
| 623 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 624 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) || |
| 625 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0)) |
| 626 | return PAT_NOMATCH; |
| 627 | return PAT_MATCH; |
| 628 | } |
| 629 | |
| 630 | /* Checks that the pattern is included inside the tested string. |
| 631 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 632 | */ |
| 633 | enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern) |
| 634 | { |
| 635 | int icase; |
| 636 | char *end; |
| 637 | char *c; |
| 638 | |
| 639 | if (pattern->len > smp->data.str.len) |
| 640 | return PAT_NOMATCH; |
| 641 | |
| 642 | end = smp->data.str.str + smp->data.str.len - pattern->len; |
| 643 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 644 | if (icase) { |
| 645 | for (c = smp->data.str.str; c <= end; c++) { |
| 646 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 647 | continue; |
| 648 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
| 649 | return PAT_MATCH; |
| 650 | } |
| 651 | } else { |
| 652 | for (c = smp->data.str.str; c <= end; c++) { |
| 653 | if (*c != *pattern->ptr.str) |
| 654 | continue; |
| 655 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
| 656 | return PAT_MATCH; |
| 657 | } |
| 658 | } |
| 659 | return PAT_NOMATCH; |
| 660 | } |
| 661 | |
| 662 | /* This one is used by other real functions. It checks that the pattern is |
| 663 | * included inside the tested string, but enclosed between the specified |
| 664 | * delimiters or at the beginning or end of the string. The delimiters are |
| 665 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 666 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 667 | */ |
| 668 | static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters) |
| 669 | { |
| 670 | int may_match, icase; |
| 671 | char *c, *end; |
| 672 | char *ps; |
| 673 | int pl; |
| 674 | |
| 675 | pl = pattern->len; |
| 676 | ps = pattern->ptr.str; |
| 677 | |
| 678 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 679 | pl--; |
| 680 | ps++; |
| 681 | } |
| 682 | |
| 683 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 684 | pl--; |
| 685 | |
| 686 | if (pl > smp->data.str.len) |
| 687 | return PAT_NOMATCH; |
| 688 | |
| 689 | may_match = 1; |
| 690 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 691 | end = smp->data.str.str + smp->data.str.len - pl; |
| 692 | for (c = smp->data.str.str; c <= end; c++) { |
| 693 | if (is_delimiter(*c, delimiters)) { |
| 694 | may_match = 1; |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | if (!may_match) |
| 699 | continue; |
| 700 | |
| 701 | if (icase) { |
| 702 | if ((tolower(*c) == tolower(*ps)) && |
| 703 | (strncasecmp(ps, c, pl) == 0) && |
| 704 | (c == end || is_delimiter(c[pl], delimiters))) |
| 705 | return PAT_MATCH; |
| 706 | } else { |
| 707 | if ((*c == *ps) && |
| 708 | (strncmp(ps, c, pl) == 0) && |
| 709 | (c == end || is_delimiter(c[pl], delimiters))) |
| 710 | return PAT_MATCH; |
| 711 | } |
| 712 | may_match = 0; |
| 713 | } |
| 714 | return PAT_NOMATCH; |
| 715 | } |
| 716 | |
| 717 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 718 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 719 | * Delimiters at the beginning or end of the pattern are ignored. |
| 720 | */ |
| 721 | enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern) |
| 722 | { |
| 723 | return match_word(smp, pattern, make_4delim('/', '?', '?', '?')); |
| 724 | } |
| 725 | |
| 726 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 727 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 728 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 729 | */ |
| 730 | enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern) |
| 731 | { |
| 732 | return match_word(smp, pattern, make_4delim('/', '?', '.', ':')); |
| 733 | } |
| 734 | |
| 735 | /* Checks that the integer in <test> is included between min and max */ |
| 736 | enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern) |
| 737 | { |
| 738 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) && |
| 739 | (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max)) |
| 740 | return PAT_MATCH; |
| 741 | return PAT_NOMATCH; |
| 742 | } |
| 743 | |
| 744 | /* Checks that the length of the pattern in <test> is included between min and max */ |
| 745 | enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern) |
| 746 | { |
| 747 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) && |
| 748 | (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max)) |
| 749 | return PAT_MATCH; |
| 750 | return PAT_NOMATCH; |
| 751 | } |
| 752 | |
| 753 | enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern) |
| 754 | { |
| 755 | unsigned int v4; /* in network byte order */ |
| 756 | struct in6_addr *v6; |
| 757 | int bits, pos; |
| 758 | struct in6_addr tmp6; |
| 759 | |
| 760 | if (pattern->type == SMP_T_IPV4) { |
| 761 | if (smp->type == SMP_T_IPV4) { |
| 762 | v4 = smp->data.ipv4.s_addr; |
| 763 | } |
| 764 | else if (smp->type == SMP_T_IPV6) { |
| 765 | /* v4 match on a V6 sample. We want to check at least for |
| 766 | * the following forms : |
| 767 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 768 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 769 | * - 2002:ip:v4:: (6to4) |
| 770 | */ |
| 771 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 772 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 773 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 774 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 775 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
| 776 | } |
| 777 | else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 778 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 779 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
| 780 | } |
| 781 | else |
| 782 | return PAT_NOMATCH; |
| 783 | } |
| 784 | else |
| 785 | return PAT_NOMATCH; |
| 786 | |
| 787 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
| 788 | return PAT_MATCH; |
| 789 | else |
| 790 | return PAT_NOMATCH; |
| 791 | } |
| 792 | else if (pattern->type == SMP_T_IPV6) { |
| 793 | if (smp->type == SMP_T_IPV4) { |
| 794 | /* Convert the IPv4 sample address to IPv4 with the |
| 795 | * mapping method using the ::ffff: prefix. |
| 796 | */ |
| 797 | memset(&tmp6, 0, 10); |
| 798 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
| 799 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr; |
| 800 | v6 = &tmp6; |
| 801 | } |
| 802 | else if (smp->type == SMP_T_IPV6) { |
| 803 | v6 = &smp->data.ipv6; |
| 804 | } |
| 805 | else { |
| 806 | return PAT_NOMATCH; |
| 807 | } |
| 808 | |
| 809 | bits = pattern->val.ipv6.mask; |
| 810 | for (pos = 0; bits > 0; pos += 4, bits -= 32) { |
| 811 | v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos]; |
| 812 | if (bits < 32) |
| 813 | v4 &= htonl((~0U) << (32-bits)); |
| 814 | if (v4) |
| 815 | return PAT_NOMATCH; |
| 816 | } |
| 817 | return PAT_MATCH; |
| 818 | } |
| 819 | return PAT_NOMATCH; |
| 820 | } |
| 821 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 822 | /* NB: does nothing if <pat> is NULL */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 823 | void pattern_free(struct pattern *pat) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 824 | { |
| 825 | if (!pat) |
| 826 | return; |
| 827 | |
| 828 | if (pat->ptr.ptr) { |
| 829 | if (pat->freeptrbuf) |
| 830 | pat->freeptrbuf(pat->ptr.ptr); |
| 831 | |
| 832 | free(pat->ptr.ptr); |
| 833 | } |
| 834 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 835 | free(pat->smp); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 836 | free(pat); |
| 837 | } |
| 838 | |
| 839 | void free_pattern_list(struct list *head) |
| 840 | { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 841 | struct pattern *pat, *tmp; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 842 | list_for_each_entry_safe(pat, tmp, head, list) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 843 | pattern_free(pat); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void free_pattern_tree(struct eb_root *root) |
| 847 | { |
| 848 | struct eb_node *node, *next; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 849 | struct pat_idx_elt *elt; |
| 850 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 851 | node = eb_first(root); |
| 852 | while (node) { |
| 853 | next = eb_next(node); |
| 854 | eb_delete(node); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 855 | elt = container_of(node, struct pat_idx_elt, node); |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 856 | free(elt->smp); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 857 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 858 | node = next; |
| 859 | } |
| 860 | } |
| 861 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 862 | void pattern_prune_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 863 | { |
| 864 | free_pattern_list(&expr->patterns); |
| 865 | free_pattern_tree(&expr->pattern_tree); |
| 866 | LIST_INIT(&expr->patterns); |
| 867 | } |
| 868 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 869 | void pattern_init_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 870 | { |
| 871 | LIST_INIT(&expr->patterns); |
| 872 | expr->pattern_tree = EB_ROOT_UNIQUE; |
| 873 | } |
| 874 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 875 | /* return 1 if the process is ok |
| 876 | * return -1 if the parser fail. The err message is filled. |
| 877 | * return -2 if out of memory |
| 878 | */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 879 | int pattern_register(struct pattern_expr *expr, const char **args, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 880 | struct sample_storage *smp, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 881 | struct pattern **pattern, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 882 | int patflags, char **err) |
| 883 | { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 884 | int opaque = 0; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 885 | unsigned int mask = 0; |
| 886 | struct pat_idx_elt *node; |
| 887 | int len; |
| 888 | int ret; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 889 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 890 | /* eat args */ |
| 891 | while (**args) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 892 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 893 | /* we keep the previous pattern along iterations as long as it's not used */ |
| 894 | if (!*pattern) |
| 895 | *pattern = (struct pattern *)malloc(sizeof(**pattern)); |
| 896 | if (!*pattern) { |
| 897 | memprintf(err, "out of memory while loading pattern"); |
| 898 | return 0; |
| 899 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 900 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 901 | memset(*pattern, 0, sizeof(**pattern)); |
| 902 | (*pattern)->flags = patflags; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 903 | |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 904 | ret = expr->parse(args, *pattern, PAT_U_COMPILE, &opaque, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 905 | if (!ret) |
| 906 | return 0; |
| 907 | |
| 908 | /* each parser return the number of args eated */ |
| 909 | args += ret; |
| 910 | |
| 911 | /* |
| 912 | * |
| 913 | * SMP_T_CSTR tree indexation |
| 914 | * |
| 915 | * The match "pat_match_str()" can use tree. |
| 916 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 917 | */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 918 | if (expr->match == pat_match_str) { |
| 919 | |
| 920 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 921 | if ((*pattern)->flags & PAT_F_IGNORE_CASE) |
| 922 | goto just_chain_the_pattern; |
| 923 | |
| 924 | /* Process the key len */ |
| 925 | len = strlen((*pattern)->ptr.str) + 1; |
| 926 | |
| 927 | /* node memory allocation */ |
| 928 | node = calloc(1, sizeof(*node) + len); |
| 929 | if (!node) { |
| 930 | memprintf(err, "out of memory while loading pattern"); |
| 931 | return 0; |
| 932 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 933 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 934 | /* copy the pointer to sample associated to this node */ |
| 935 | node->smp = smp; |
| 936 | |
| 937 | /* copy the string */ |
| 938 | memcpy(node->node.key, (*pattern)->ptr.str, len); |
| 939 | |
| 940 | /* the "map_parser_str()" function always duplicate string information */ |
| 941 | free((*pattern)->ptr.str); |
Willy Tarreau | 6762a30 | 2013-12-16 10:40:28 +0100 | [diff] [blame] | 942 | (*pattern)->ptr.str = NULL; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 943 | |
| 944 | /* we pre-set the data pointer to the tree's head so that functions |
| 945 | * which are able to insert in a tree know where to do that. |
| 946 | * |
| 947 | * because "val" is an "union", the previous data are crushed. |
| 948 | */ |
| 949 | (*pattern)->flags |= PAT_F_TREE; |
| 950 | (*pattern)->val.tree = &expr->pattern_tree; |
| 951 | |
| 952 | /* index the new node */ |
| 953 | if (ebst_insert((*pattern)->val.tree, &node->node) != &node->node) |
| 954 | free(node); /* was a duplicate */ |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * |
| 959 | * SMP_T_IPV4 tree indexation |
| 960 | * |
| 961 | * The match "pat_match_ip()" can use tree. |
| 962 | * |
| 963 | */ |
| 964 | else if (expr->match == pat_match_ip) { |
| 965 | |
| 966 | /* Only IPv4 can be indexed */ |
| 967 | if ((*pattern)->type != SMP_T_IPV4) |
| 968 | goto just_chain_the_pattern; |
| 969 | |
| 970 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 971 | * insert the network into the tree. A continuous mask has only |
| 972 | * ones on the left. This means that this mask + its lower bit |
| 973 | * added once again is null. |
| 974 | */ |
| 975 | mask = ntohl((*pattern)->val.ipv4.mask.s_addr); |
| 976 | if (mask + (mask & -mask) != 0) |
| 977 | goto just_chain_the_pattern; |
| 978 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 979 | |
| 980 | /* node memory allocation */ |
| 981 | node = calloc(1, sizeof(*node) + 4); |
| 982 | if (!node) { |
| 983 | memprintf(err, "out of memory while loading pattern"); |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | /* copy the pointer to sample associated to this node */ |
| 988 | node->smp = smp; |
| 989 | |
| 990 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 991 | memcpy(node->node.key, &(*pattern)->val.ipv4.addr, 4); /* network byte order */ |
| 992 | |
| 993 | /* we pre-set the data pointer to the tree's head so that functions |
| 994 | * which are able to insert in a tree know where to do that. |
| 995 | * |
| 996 | * because "val" is an "union", the previous data are crushed. |
| 997 | */ |
| 998 | (*pattern)->flags |= PAT_F_TREE; |
| 999 | (*pattern)->val.tree = &expr->pattern_tree; |
| 1000 | |
| 1001 | /* Index the new node |
| 1002 | * FIXME: insert <addr>/<mask> into the tree here |
| 1003 | */ |
| 1004 | node->node.node.pfx = mask; |
| 1005 | if (ebmb_insert_prefix((*pattern)->val.tree, &node->node, 4) != &node->node) |
| 1006 | free(node); /* was a duplicate */ |
| 1007 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1008 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1009 | /* |
| 1010 | * |
| 1011 | * if the parser did not feed the tree, let's chain the pattern to the list |
| 1012 | * |
| 1013 | */ |
| 1014 | else { |
| 1015 | |
| 1016 | just_chain_the_pattern: |
| 1017 | |
| 1018 | LIST_ADDQ(&expr->patterns, &(*pattern)->list); |
| 1019 | |
| 1020 | /* copy the pointer to sample associated to this node */ |
| 1021 | (*pattern)->smp = smp; |
| 1022 | |
| 1023 | /* get a new one */ |
| 1024 | *pattern = NULL; |
| 1025 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 1032 | * be returned there on errors and the caller will have to free it. |
| 1033 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1034 | int pattern_read_from_file(struct pattern_expr *expr, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1035 | const char *filename, int patflags, |
| 1036 | char **err) |
| 1037 | { |
| 1038 | FILE *file; |
| 1039 | char *c; |
| 1040 | char *arg; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1041 | struct pattern *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1042 | int ret = 0; |
| 1043 | int line = 0; |
| 1044 | int code; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1045 | const char *args[2]; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1046 | |
| 1047 | file = fopen(filename, "r"); |
| 1048 | if (!file) { |
| 1049 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | /* now parse all patterns. The file may contain only one pattern per |
| 1054 | * line. If the line contains spaces, they will be part of the pattern. |
| 1055 | * The pattern stops at the first CR, LF or EOF encountered. |
| 1056 | */ |
| 1057 | pattern = NULL; |
| 1058 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 1059 | line++; |
| 1060 | c = trash.str; |
| 1061 | |
| 1062 | /* ignore lines beginning with a dash */ |
| 1063 | if (*c == '#') |
| 1064 | continue; |
| 1065 | |
| 1066 | /* strip leading spaces and tabs */ |
| 1067 | while (*c == ' ' || *c == '\t') |
| 1068 | c++; |
| 1069 | |
| 1070 | |
| 1071 | arg = c; |
| 1072 | while (*c && *c != '\n' && *c != '\r') |
| 1073 | c++; |
| 1074 | *c = 0; |
| 1075 | |
| 1076 | /* empty lines are ignored too */ |
| 1077 | if (c == arg) |
| 1078 | continue; |
| 1079 | |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1080 | args[0] = arg; |
| 1081 | args[1] = ""; |
| 1082 | |
| 1083 | code = pattern_register(expr, args, NULL, &pattern, patflags, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1084 | if (code == -2) { |
| 1085 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 1086 | goto out_close; |
| 1087 | } |
| 1088 | else if (code < 0) { |
| 1089 | memprintf(err, "%s when loading patterns from file <%s>", *err, filename); |
| 1090 | goto out_free_pattern; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | ret = 1; /* success */ |
| 1095 | |
| 1096 | out_free_pattern: |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1097 | pattern_free(pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1098 | out_close: |
| 1099 | fclose(file); |
| 1100 | return ret; |
| 1101 | } |
| 1102 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1103 | /* This function matches a sample <smp> against a set of patterns presented in |
| 1104 | * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed |
| 1105 | * with the pointer associated with the matching pattern. This function returns |
| 1106 | * PAT_NOMATCH or PAT_MATCH. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1107 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1108 | enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1109 | struct sample_storage **sample, |
| 1110 | struct pattern **pat, struct pat_idx_elt **idx_elt) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1111 | { |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame] | 1112 | enum pat_match_res pat_res = PAT_NOMATCH; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1113 | struct pattern *pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1114 | struct ebmb_node *node = NULL; |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1115 | struct pat_idx_elt *elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1116 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1117 | if (expr->match == pat_match_nothing) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1118 | if (smp->data.uint) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1119 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1120 | else |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1121 | pat_res |= PAT_NOMATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1122 | } |
| 1123 | else if (!expr->match) { |
| 1124 | /* just check for existence */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1125 | pat_res |= PAT_MATCH; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1126 | } |
| 1127 | else { |
| 1128 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 1129 | /* a tree is present, let's check what type it is */ |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 1130 | if (expr->match == pat_match_str) { |
| 1131 | if (sample_convert(smp, SMP_T_STR)) |
| 1132 | node = pat_lookup_str(smp, expr); |
| 1133 | } |
| 1134 | else if (expr->match == pat_match_ip) { |
| 1135 | if (sample_convert(smp, SMP_T_IPV4)) |
| 1136 | node = pat_lookup_ip(smp, expr); |
| 1137 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1138 | if (node) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1139 | pat_res |= PAT_MATCH; |
| 1140 | elt = ebmb_entry(node, struct pat_idx_elt, node); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1141 | if (sample) |
| 1142 | *sample = elt->smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1143 | if (idx_elt) |
| 1144 | *idx_elt = elt; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* call the match() function for all tests on this value */ |
| 1149 | list_for_each_entry(pattern, &expr->patterns, list) { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1150 | if (pat_res == PAT_MATCH) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1151 | break; |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 1152 | if (sample_convert(smp, pattern->expect_type)) |
| 1153 | pat_res |= expr->match(smp, pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1154 | if (sample) |
| 1155 | *sample = pattern->smp; |
Thierry FOURNIER | 7609064 | 2013-12-10 15:03:38 +0100 | [diff] [blame] | 1156 | if (pat) |
| 1157 | *pat = pattern; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1161 | return pat_res; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1164 | /* This function browse the pattern expr <expr> to lookup the key <key>. On |
| 1165 | * error it returns 0. On success, it returns 1 and fills either <pat_elt> |
| 1166 | * or <idx_elt> with the respectively matched pointers, and the other one with |
| 1167 | * NULL. Pointers are not set if they're passed as NULL. |
| 1168 | */ |
| 1169 | int pattern_lookup(const char *key, struct pattern_expr *expr, |
| 1170 | struct pattern **pat_elt, struct pat_idx_elt **idx_elt, char **err) |
| 1171 | { |
| 1172 | struct pattern pattern; |
| 1173 | struct pattern *pat; |
| 1174 | struct ebmb_node *node; |
| 1175 | struct pat_idx_elt *elt; |
| 1176 | const char *args[2]; |
| 1177 | int opaque = 0; |
Willy Tarreau | 668ae53 | 2013-12-15 16:42:26 +0100 | [diff] [blame] | 1178 | unsigned int mask = 0; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1179 | |
| 1180 | /* no real pattern */ |
| 1181 | if (!expr->match || expr->match == pat_match_nothing) |
| 1182 | return 0; |
| 1183 | |
| 1184 | /* build lookup pattern */ |
| 1185 | args[0] = key; |
| 1186 | args[1] = ""; |
| 1187 | if (!expr->parse(args, &pattern, PAT_U_LOOKUP, &opaque, NULL)) |
| 1188 | return 0; |
| 1189 | |
| 1190 | pat = NULL; |
| 1191 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1192 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1193 | /* Try to look up the tree first. IPv6 is not indexed */ |
| 1194 | if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) { |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1195 | /* Check the pattern type */ |
| 1196 | if (pattern.type != SMP_T_STR && |
| 1197 | pattern.type != SMP_T_CSTR && |
| 1198 | pattern.type != SMP_T_IPV4) { |
| 1199 | memprintf(err, "Unexpected pattern type."); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* Convert mask. If the mask is not contiguous, ignore the lookup |
| 1204 | * in the tree, and browse the list. |
| 1205 | */ |
| 1206 | if (expr->match == pat_match_ip) { |
| 1207 | mask = ntohl(pattern.val.ipv4.mask.s_addr); |
| 1208 | if (mask + (mask & -mask) != 0) |
| 1209 | goto browse_list; |
| 1210 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1211 | } |
| 1212 | |
| 1213 | /* browse each node of the tree, and check string */ |
| 1214 | if (expr->match == pat_match_str) { |
| 1215 | for (node = ebmb_first(&expr->pattern_tree); |
| 1216 | node; |
| 1217 | node = ebmb_next(node)) { |
| 1218 | elt = container_of(node, struct pat_idx_elt, node); |
| 1219 | if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0) |
| 1220 | goto found; |
| 1221 | } |
| 1222 | } |
| 1223 | else if (expr->match == pat_match_ip) { |
| 1224 | for (node = ebmb_first(&expr->pattern_tree); |
| 1225 | node; |
| 1226 | node = ebmb_next(node)) { |
| 1227 | elt = container_of(node, struct pat_idx_elt, node); |
| 1228 | if (elt->node.node.pfx == mask && |
| 1229 | memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0) |
| 1230 | goto found; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | browse_list: |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1236 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1237 | if (expr->parse == pat_parse_int || |
| 1238 | expr->parse == pat_parse_len) { |
| 1239 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1240 | if (pat->flags & PAT_F_TREE) |
| 1241 | continue; |
| 1242 | if (pattern.val.range.min_set != pat->val.range.min_set) |
| 1243 | continue; |
| 1244 | if (pattern.val.range.max_set != pat->val.range.max_set) |
| 1245 | continue; |
| 1246 | if (pattern.val.range.min_set && |
| 1247 | pattern.val.range.min != pat->val.range.min) |
| 1248 | continue; |
| 1249 | if (pattern.val.range.max_set && |
| 1250 | pattern.val.range.max != pat->val.range.max) |
| 1251 | continue; |
| 1252 | goto found; |
| 1253 | } |
| 1254 | } |
| 1255 | else if (expr->parse == pat_parse_ip) { |
| 1256 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1257 | if (pat->flags & PAT_F_TREE) |
| 1258 | continue; |
| 1259 | if (pattern.type != pat->type) |
| 1260 | continue; |
| 1261 | if (pattern.type == SMP_T_IPV4 && |
| 1262 | memcmp(&pattern.val.ipv4.addr, &pat->val.ipv4.addr, sizeof(pat->val.ipv4.addr)) != 0) |
| 1263 | continue; |
| 1264 | if (pattern.type == SMP_T_IPV4 && |
| 1265 | memcmp(&pattern.val.ipv4.mask, &pat->val.ipv4.mask, sizeof(pat->val.ipv4.addr)) != 0) |
| 1266 | continue; |
| 1267 | if (pattern.type == SMP_T_IPV6 && |
| 1268 | memcmp(&pattern.val.ipv6.addr, &pat->val.ipv6.addr, sizeof(pat->val.ipv6.addr)) != 0) |
| 1269 | continue; |
| 1270 | if (pattern.type == SMP_T_IPV6 && |
| 1271 | pattern.val.ipv6.mask != pat->val.ipv6.mask) |
| 1272 | continue; |
| 1273 | goto found; |
| 1274 | } |
| 1275 | } |
| 1276 | else if (expr->parse == pat_parse_str) { |
| 1277 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1278 | if (pat->flags & PAT_F_TREE) |
| 1279 | continue; |
| 1280 | if (pattern.len != pat->len) |
| 1281 | continue; |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1282 | if (pat->flags & PAT_F_IGNORE_CASE) { |
| 1283 | if (strncasecmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0) |
| 1284 | continue; |
| 1285 | } |
| 1286 | else { |
| 1287 | if (strncmp(pattern.ptr.str, pat->ptr.str, pat->len) != 0) |
| 1288 | continue; |
| 1289 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1290 | goto found; |
| 1291 | } |
| 1292 | } |
| 1293 | else if (expr->parse == pat_parse_bin) { |
| 1294 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1295 | if (pat->flags & PAT_F_TREE) |
| 1296 | continue; |
| 1297 | if (pattern.len != pat->len) |
| 1298 | continue; |
| 1299 | if (memcmp(pattern.ptr.ptr, pat->ptr.ptr, pat->len) != 0) |
| 1300 | continue; |
| 1301 | goto found; |
| 1302 | } |
| 1303 | } |
| 1304 | else if (expr->parse == pat_parse_reg) { |
| 1305 | list_for_each_entry(pat, &expr->patterns, list) { |
| 1306 | if (pat->flags & PAT_F_TREE) |
| 1307 | continue; |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1308 | if (pat->flags & PAT_F_IGNORE_CASE) { |
| 1309 | if (strcasecmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0) |
| 1310 | continue; |
| 1311 | } |
| 1312 | else { |
| 1313 | if (strcmp(pattern.ptr.reg->regstr, pat->ptr.reg->regstr) != 0) |
| 1314 | continue; |
| 1315 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1316 | goto found; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /* if we get there, we didn't find the pattern */ |
| 1321 | return 0; |
| 1322 | found: |
| 1323 | if (idx_elt) |
| 1324 | *idx_elt = elt; |
| 1325 | |
| 1326 | if (pat_elt) |
| 1327 | *pat_elt = pat; |
| 1328 | |
| 1329 | return 1; |
| 1330 | } |