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