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 | pattern->type = SMP_T_IPV6; |
| 402 | return 1; |
| 403 | } |
| 404 | else { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 405 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | } |
| 409 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 410 | /* |
| 411 | * |
| 412 | * These functions are exported and may be used by any other component. |
| 413 | * |
| 414 | * This fucntion just take a sample <smp> and check if this sample match |
| 415 | * with the pattern <pattern>. This fucntion return just PAT_MATCH or |
| 416 | * PAT_NOMATCH. |
| 417 | * |
| 418 | */ |
| 419 | |
| 420 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 421 | 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] | 422 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 423 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | /* 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] | 428 | 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] | 429 | { |
| 430 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 431 | struct ebmb_node *node; |
| 432 | char prev; |
| 433 | struct pattern_tree *elt; |
| 434 | struct pattern_list *lst; |
| 435 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 436 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 437 | /* convert input to string */ |
| 438 | if (!sample_convert(smp, SMP_T_STR)) |
| 439 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 440 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 441 | /* Lookup a string in the expression's pattern tree. */ |
| 442 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 443 | /* we may have to force a trailing zero on the test pattern */ |
| 444 | prev = smp->data.str.str[smp->data.str.len]; |
| 445 | if (prev) |
| 446 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 447 | node = ebst_lookup(&expr->pattern_tree, smp->data.str.str); |
| 448 | if (prev) |
| 449 | smp->data.str.str[smp->data.str.len] = prev; |
| 450 | |
| 451 | if (node) { |
| 452 | if (fill) { |
| 453 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 454 | static_pattern.smp = elt->smp; |
| 455 | static_pattern.flags = PAT_F_TREE; |
| 456 | static_pattern.type = SMP_T_STR; |
| 457 | static_pattern.ptr.str = (char *)elt->node.key; |
| 458 | } |
| 459 | return &static_pattern; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /* look in the list */ |
| 464 | list_for_each_entry(lst, &expr->patterns, list) { |
| 465 | pattern = &lst->pat; |
| 466 | |
| 467 | if (pattern->len != smp->data.str.len) |
| 468 | continue; |
| 469 | |
| 470 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 471 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) || |
| 472 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)) |
| 473 | return pattern; |
| 474 | } |
| 475 | |
| 476 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* 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] | 480 | 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] | 481 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 482 | struct pattern_list *lst; |
| 483 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 484 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 485 | /* Convert input to binary. */ |
| 486 | if (!sample_convert(smp, SMP_T_BIN)) |
| 487 | return NULL; |
| 488 | |
| 489 | /* Look in the list. */ |
| 490 | list_for_each_entry(lst, &expr->patterns, list) { |
| 491 | pattern = &lst->pat; |
| 492 | |
| 493 | if (pattern->len != smp->data.str.len) |
| 494 | continue; |
| 495 | |
| 496 | if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) |
| 497 | return pattern; |
| 498 | } |
| 499 | |
| 500 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 504 | * and restores the previous character when leaving. |
| 505 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 506 | 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] | 507 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 508 | struct pattern_list *lst; |
| 509 | struct pattern *pattern; |
| 510 | |
| 511 | /* convert input to string */ |
| 512 | if (!sample_convert(smp, SMP_T_STR)) |
| 513 | return NULL; |
| 514 | |
| 515 | /* look in the list */ |
| 516 | list_for_each_entry(lst, &expr->patterns, list) { |
| 517 | pattern = &lst->pat; |
| 518 | |
| 519 | if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0) |
| 520 | return pattern; |
| 521 | } |
| 522 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 526 | 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] | 527 | { |
| 528 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 529 | struct pattern_list *lst; |
| 530 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 531 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 532 | /* convert input to string */ |
| 533 | if (!sample_convert(smp, SMP_T_STR)) |
| 534 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 535 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 536 | list_for_each_entry(lst, &expr->patterns, list) { |
| 537 | pattern = &lst->pat; |
| 538 | |
| 539 | if (pattern->len > smp->data.str.len) |
| 540 | continue; |
| 541 | |
| 542 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 543 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) || |
| 544 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0)) |
| 545 | continue; |
| 546 | |
| 547 | return pattern; |
| 548 | } |
| 549 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 553 | 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] | 554 | { |
| 555 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 556 | struct pattern_list *lst; |
| 557 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 558 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 559 | /* convert input to string */ |
| 560 | if (!sample_convert(smp, SMP_T_STR)) |
| 561 | return NULL; |
| 562 | |
| 563 | list_for_each_entry(lst, &expr->patterns, list) { |
| 564 | pattern = &lst->pat; |
| 565 | |
| 566 | if (pattern->len > smp->data.str.len) |
| 567 | continue; |
| 568 | |
| 569 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 570 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) || |
| 571 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0)) |
| 572 | continue; |
| 573 | |
| 574 | return pattern; |
| 575 | } |
| 576 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* Checks that the pattern is included inside the tested string. |
| 580 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 581 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 582 | 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] | 583 | { |
| 584 | int icase; |
| 585 | char *end; |
| 586 | char *c; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 587 | struct pattern_list *lst; |
| 588 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 589 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 590 | /* convert input to string */ |
| 591 | if (!sample_convert(smp, SMP_T_STR)) |
| 592 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 593 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 594 | list_for_each_entry(lst, &expr->patterns, list) { |
| 595 | pattern = &lst->pat; |
| 596 | |
| 597 | if (pattern->len > smp->data.str.len) |
| 598 | continue; |
| 599 | |
| 600 | end = smp->data.str.str + smp->data.str.len - pattern->len; |
| 601 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 602 | if (icase) { |
| 603 | for (c = smp->data.str.str; c <= end; c++) { |
| 604 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 605 | continue; |
| 606 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
| 607 | return pattern; |
| 608 | } |
| 609 | } else { |
| 610 | for (c = smp->data.str.str; c <= end; c++) { |
| 611 | if (*c != *pattern->ptr.str) |
| 612 | continue; |
| 613 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
| 614 | return pattern; |
| 615 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 616 | } |
| 617 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 618 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* This one is used by other real functions. It checks that the pattern is |
| 622 | * included inside the tested string, but enclosed between the specified |
| 623 | * delimiters or at the beginning or end of the string. The delimiters are |
| 624 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 625 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 626 | */ |
| 627 | static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters) |
| 628 | { |
| 629 | int may_match, icase; |
| 630 | char *c, *end; |
| 631 | char *ps; |
| 632 | int pl; |
| 633 | |
| 634 | pl = pattern->len; |
| 635 | ps = pattern->ptr.str; |
| 636 | |
| 637 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 638 | pl--; |
| 639 | ps++; |
| 640 | } |
| 641 | |
| 642 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 643 | pl--; |
| 644 | |
| 645 | if (pl > smp->data.str.len) |
| 646 | return PAT_NOMATCH; |
| 647 | |
| 648 | may_match = 1; |
| 649 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 650 | end = smp->data.str.str + smp->data.str.len - pl; |
| 651 | for (c = smp->data.str.str; c <= end; c++) { |
| 652 | if (is_delimiter(*c, delimiters)) { |
| 653 | may_match = 1; |
| 654 | continue; |
| 655 | } |
| 656 | |
| 657 | if (!may_match) |
| 658 | continue; |
| 659 | |
| 660 | if (icase) { |
| 661 | if ((tolower(*c) == tolower(*ps)) && |
| 662 | (strncasecmp(ps, c, pl) == 0) && |
| 663 | (c == end || is_delimiter(c[pl], delimiters))) |
| 664 | return PAT_MATCH; |
| 665 | } else { |
| 666 | if ((*c == *ps) && |
| 667 | (strncmp(ps, c, pl) == 0) && |
| 668 | (c == end || is_delimiter(c[pl], delimiters))) |
| 669 | return PAT_MATCH; |
| 670 | } |
| 671 | may_match = 0; |
| 672 | } |
| 673 | return PAT_NOMATCH; |
| 674 | } |
| 675 | |
| 676 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 677 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 678 | * Delimiters at the beginning or end of the pattern are ignored. |
| 679 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 680 | 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] | 681 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 682 | struct pattern_list *lst; |
| 683 | struct pattern *pattern; |
| 684 | |
| 685 | /* convert input to string */ |
| 686 | if (!sample_convert(smp, SMP_T_STR)) |
| 687 | return NULL; |
| 688 | |
| 689 | list_for_each_entry(lst, &expr->patterns, list) { |
| 690 | pattern = &lst->pat; |
| 691 | if (match_word(smp, pattern, make_4delim('/', '?', '?', '?'))) |
| 692 | return pattern; |
| 693 | } |
| 694 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 698 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 699 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 700 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 701 | 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] | 702 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 703 | struct pattern_list *lst; |
| 704 | struct pattern *pattern; |
| 705 | |
| 706 | /* convert input to string */ |
| 707 | if (!sample_convert(smp, SMP_T_STR)) |
| 708 | return NULL; |
| 709 | |
| 710 | list_for_each_entry(lst, &expr->patterns, list) { |
| 711 | pattern = &lst->pat; |
| 712 | if (match_word(smp, pattern, make_4delim('/', '?', '.', ':'))) |
| 713 | return pattern; |
| 714 | } |
| 715 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 719 | 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] | 720 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 721 | struct pattern_list *lst; |
| 722 | struct pattern *pattern; |
| 723 | |
| 724 | /* convert input to integer */ |
| 725 | if (!sample_convert(smp, SMP_T_UINT)) |
| 726 | return NULL; |
| 727 | |
| 728 | list_for_each_entry(lst, &expr->patterns, list) { |
| 729 | pattern = &lst->pat; |
| 730 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) && |
| 731 | (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max)) |
| 732 | return pattern; |
| 733 | } |
| 734 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* 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] | 738 | 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] | 739 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 740 | struct pattern_list *lst; |
| 741 | struct pattern *pattern; |
| 742 | |
| 743 | /* convert input to string */ |
| 744 | if (!sample_convert(smp, SMP_T_STR)) |
| 745 | return NULL; |
| 746 | |
| 747 | list_for_each_entry(lst, &expr->patterns, list) { |
| 748 | pattern = &lst->pat; |
| 749 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) && |
| 750 | (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max)) |
| 751 | return pattern; |
| 752 | } |
| 753 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 756 | 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] | 757 | { |
| 758 | unsigned int v4; /* in network byte order */ |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 759 | struct in6_addr tmp6; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 760 | struct in_addr *s; |
| 761 | struct ebmb_node *node; |
| 762 | struct pattern_tree *elt; |
| 763 | struct pattern_list *lst; |
| 764 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 765 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 766 | /* convert input to addr */ |
| 767 | if (!sample_convert(smp, SMP_T_ADDR)) |
| 768 | return NULL; |
| 769 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 770 | /* The input sample is IPv4. Try to match in the trees. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 771 | if (smp->type == SMP_T_IPV4) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 772 | /* Lookup an IPv4 address in the expression's pattern tree using |
| 773 | * the longest match method. |
| 774 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 775 | s = &smp->data.ipv4; |
| 776 | node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
| 777 | if (node) { |
| 778 | if (fill) { |
| 779 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 780 | static_pattern.smp = elt->smp; |
| 781 | static_pattern.flags = PAT_F_TREE; |
| 782 | static_pattern.type = SMP_T_IPV4; |
| 783 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 784 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 785 | return NULL; |
| 786 | } |
| 787 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 788 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 789 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 790 | /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4 |
| 791 | * sample address to IPv6 with the mapping method using the ::ffff: |
| 792 | * prefix, and try to lookup in the IPv6 tree. |
| 793 | */ |
| 794 | memset(&tmp6, 0, 10); |
| 795 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
| 796 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr; |
| 797 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6); |
| 798 | if (node) { |
| 799 | if (fill) { |
| 800 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 801 | static_pattern.smp = elt->smp; |
| 802 | static_pattern.flags = PAT_F_TREE; |
| 803 | static_pattern.type = SMP_T_IPV6; |
| 804 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 805 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
| 806 | } |
| 807 | return &static_pattern; |
| 808 | } |
| 809 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 810 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 811 | /* The input sample is IPv6. Try to match in the trees. */ |
| 812 | if (smp->type == SMP_T_IPV6) { |
| 813 | /* Lookup an IPv6 address in the expression's pattern tree using |
| 814 | * the longest match method. |
| 815 | */ |
| 816 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6); |
| 817 | if (node) { |
| 818 | if (fill) { |
| 819 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 820 | static_pattern.smp = elt->smp; |
| 821 | static_pattern.flags = PAT_F_TREE; |
| 822 | static_pattern.type = SMP_T_IPV6; |
| 823 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 824 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 825 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 826 | return &static_pattern; |
| 827 | } |
| 828 | |
| 829 | /* Try to convert 6 to 4 when the start of the ipv6 address match the |
| 830 | * following forms : |
| 831 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 832 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 833 | * - 2002:ip:v4:: (6to4) |
| 834 | */ |
| 835 | if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 836 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 837 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 838 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) || |
| 839 | *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 840 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0) |
| 841 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
| 842 | else |
| 843 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 844 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
| 845 | |
| 846 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 847 | * match method. |
| 848 | */ |
| 849 | node = ebmb_lookup_longest(&expr->pattern_tree, &v4); |
| 850 | if (node) { |
| 851 | if (fill) { |
| 852 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 853 | static_pattern.smp = elt->smp; |
| 854 | static_pattern.flags = PAT_F_TREE; |
| 855 | static_pattern.type = SMP_T_IPV4; |
| 856 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 857 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 858 | return NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 859 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 860 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 861 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 862 | } |
| 863 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 864 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 865 | /* Lookup in the list. the list contain only IPv4 patterns */ |
| 866 | list_for_each_entry(lst, &expr->patterns, list) { |
| 867 | pattern = &lst->pat; |
| 868 | |
| 869 | /* The input sample is IPv4, use it as is. */ |
| 870 | if (smp->type == SMP_T_IPV4) { |
| 871 | v4 = smp->data.ipv4.s_addr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 872 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 873 | else if (smp->type == SMP_T_IPV6) { |
| 874 | /* v4 match on a V6 sample. We want to check at least for |
| 875 | * the following forms : |
| 876 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 877 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 878 | * - 2002:ip:v4:: (6to4) |
| 879 | */ |
| 880 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 881 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 882 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 883 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 884 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 885 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 886 | else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 887 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 888 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 889 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 890 | else |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 891 | continue; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 892 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 893 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 894 | /* Check if the input sample match the current pattern. */ |
| 895 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 896 | return pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 897 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 898 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 901 | /* NB: does nothing if <pat> is NULL */ |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 902 | void pattern_free(struct pattern_list *pat) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 903 | { |
| 904 | if (!pat) |
| 905 | return; |
| 906 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 907 | if (pat->pat.ptr.ptr) { |
| 908 | if (pat->pat.freeptrbuf) |
| 909 | pat->pat.freeptrbuf(pat->pat.ptr.ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 910 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 911 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 912 | } |
| 913 | |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 914 | free(pat->pat.smp); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 915 | free(pat); |
| 916 | } |
| 917 | |
| 918 | void free_pattern_list(struct list *head) |
| 919 | { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 920 | struct pattern_list *pat, *tmp; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 921 | list_for_each_entry_safe(pat, tmp, head, list) |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 922 | pattern_free(pat); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | void free_pattern_tree(struct eb_root *root) |
| 926 | { |
| 927 | struct eb_node *node, *next; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 928 | struct pattern_tree *elt; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 929 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 930 | node = eb_first(root); |
| 931 | while (node) { |
| 932 | next = eb_next(node); |
| 933 | eb_delete(node); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 934 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 935 | free(elt->smp); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 936 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 937 | node = next; |
| 938 | } |
| 939 | } |
| 940 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 941 | void pattern_prune_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 942 | { |
| 943 | free_pattern_list(&expr->patterns); |
| 944 | free_pattern_tree(&expr->pattern_tree); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 945 | free_pattern_tree(&expr->pattern_tree_2); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 946 | LIST_INIT(&expr->patterns); |
| 947 | } |
| 948 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 949 | void pattern_init_expr(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 950 | { |
| 951 | LIST_INIT(&expr->patterns); |
| 952 | expr->pattern_tree = EB_ROOT_UNIQUE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 953 | expr->pattern_tree_2 = EB_ROOT_UNIQUE; |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 956 | /* |
| 957 | * |
| 958 | * The following functions are used for the pattern indexation |
| 959 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 960 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 961 | |
| 962 | 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] | 963 | { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 964 | struct pattern_list *patl; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 965 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 966 | /* allocate pattern */ |
| 967 | patl = calloc(1, sizeof(*patl)); |
| 968 | if (!patl) { |
| 969 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 970 | return 0; |
| 971 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 972 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 973 | /* duplicate pattern */ |
| 974 | memcpy(&patl->pat, pat, sizeof(*pat)); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 975 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 976 | /* chain pattern in the expression */ |
| 977 | LIST_ADDQ(&expr->patterns, &patl->list); |
| 978 | |
| 979 | /* that's ok */ |
| 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 984 | { |
| 985 | struct pattern_list *patl; |
| 986 | |
| 987 | /* allocate pattern */ |
| 988 | patl = calloc(1, sizeof(*patl)); |
| 989 | if (!patl) |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 990 | return 0; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 991 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 992 | /* duplicate pattern */ |
| 993 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 994 | patl->pat.ptr.ptr = malloc(patl->pat.len); |
| 995 | if (!patl->pat.ptr.ptr) { |
| 996 | free(patl); |
| 997 | memprintf(err, "out of memory while indexing pattern"); |
| 998 | return 0; |
| 999 | } |
| 1000 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1001 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1002 | /* chain pattern in the expression */ |
| 1003 | LIST_ADDQ(&expr->patterns, &patl->list); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1004 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1005 | /* that's ok */ |
| 1006 | return 1; |
| 1007 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1008 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1009 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1010 | { |
| 1011 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1012 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1013 | /* allocate pattern */ |
| 1014 | patl = calloc(1, sizeof(*patl)); |
| 1015 | if (!patl) { |
| 1016 | memprintf(err, "out of memory while indexing pattern"); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* duplicate pattern */ |
| 1021 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1022 | patl->pat.ptr.str = malloc(patl->pat.len + 1); |
| 1023 | if (!patl->pat.ptr.str) { |
| 1024 | free(patl); |
| 1025 | memprintf(err, "out of memory while indexing pattern"); |
| 1026 | return 0; |
| 1027 | } |
| 1028 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
| 1029 | patl->pat.ptr.str[patl->pat.len] = '\0'; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1030 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1031 | /* chain pattern in the expression */ |
| 1032 | LIST_ADDQ(&expr->patterns, &patl->list); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1033 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1034 | /* that's ok */ |
| 1035 | return 1; |
| 1036 | } |
| 1037 | |
| 1038 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1039 | { |
| 1040 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1042 | /* allocate pattern */ |
| 1043 | patl = calloc(1, sizeof(*patl)); |
| 1044 | if (!patl) { |
| 1045 | memprintf(err, "out of memory while indexing pattern"); |
| 1046 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1047 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1048 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1049 | /* duplicate pattern */ |
| 1050 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1051 | |
| 1052 | /* allocate regex */ |
| 1053 | patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg)); |
| 1054 | if (!patl->pat.ptr.reg) { |
| 1055 | free(patl); |
| 1056 | memprintf(err, "out of memory while indexing pattern"); |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* compile regex */ |
| 1061 | if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) { |
| 1062 | free(patl); |
| 1063 | free(patl->pat.ptr.reg); |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /* free pattern method */ |
| 1068 | patl->pat.freeptrbuf = &pat_free_reg; |
| 1069 | |
| 1070 | /* chain pattern in the expression */ |
| 1071 | LIST_ADDQ(&expr->patterns, &patl->list); |
| 1072 | |
| 1073 | /* that's ok */ |
| 1074 | return 1; |
| 1075 | } |
| 1076 | |
| 1077 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1078 | { |
| 1079 | unsigned int mask; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1080 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1081 | |
| 1082 | /* Only IPv4 can be indexed */ |
| 1083 | if (pat->type == SMP_T_IPV4) { |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1084 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 1085 | * insert the network into the tree. A continuous mask has only |
| 1086 | * ones on the left. This means that this mask + its lower bit |
| 1087 | * added once again is null. |
| 1088 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1089 | mask = ntohl(pat->val.ipv4.mask.s_addr); |
| 1090 | if (mask + (mask & -mask) == 0) { |
| 1091 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1092 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1093 | /* node memory allocation */ |
| 1094 | node = calloc(1, sizeof(*node) + 4); |
| 1095 | if (!node) { |
| 1096 | memprintf(err, "out of memory while loading pattern"); |
| 1097 | return 0; |
| 1098 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1099 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1100 | /* copy the pointer to sample associated to this node */ |
| 1101 | node->smp = pat->smp; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1102 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1103 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1104 | memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */ |
| 1105 | node->node.node.pfx = mask; |
| 1106 | if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node) |
| 1107 | free(node); /* was a duplicate */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1108 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1109 | /* that's ok */ |
| 1110 | return 1; |
| 1111 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 1112 | else { |
| 1113 | /* If the mask is not contiguous, just add the pattern to the list */ |
| 1114 | return pat_idx_list_val(expr, pat, err); |
| 1115 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1116 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 1117 | else if (pat->type == SMP_T_IPV6) { |
| 1118 | /* IPv6 also can be indexed */ |
| 1119 | node = calloc(1, sizeof(*node) + 16); |
| 1120 | if (!node) { |
| 1121 | memprintf(err, "out of memory while loading pattern"); |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | /* copy the pointer to sample associated to this node */ |
| 1126 | node->smp = pat->smp; |
| 1127 | |
| 1128 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1129 | memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */ |
| 1130 | node->node.node.pfx = pat->val.ipv6.mask; |
| 1131 | if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node) |
| 1132 | free(node); /* was a duplicate */ |
| 1133 | |
| 1134 | /* that's ok */ |
| 1135 | return 1; |
| 1136 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1137 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame^] | 1138 | return 0; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1142 | { |
| 1143 | int len; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1144 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1145 | |
| 1146 | /* Only string can be indexed */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1147 | if (pat->type != SMP_T_STR) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1148 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1149 | smp_to_type[pat->type]); |
| 1150 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1151 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1152 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1153 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1154 | if (pat->flags & PAT_F_IGNORE_CASE) |
| 1155 | return pat_idx_list_str(expr, pat, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1156 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1157 | /* Process the key len */ |
| 1158 | len = strlen(pat->ptr.str) + 1; |
| 1159 | |
| 1160 | /* node memory allocation */ |
| 1161 | node = calloc(1, sizeof(*node) + len); |
| 1162 | if (!node) { |
| 1163 | memprintf(err, "out of memory while loading pattern"); |
| 1164 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1167 | /* copy the pointer to sample associated to this node */ |
| 1168 | node->smp = pat->smp; |
| 1169 | |
| 1170 | /* copy the string */ |
| 1171 | memcpy(node->node.key, pat->ptr.str, len); |
| 1172 | |
| 1173 | /* index the new node */ |
| 1174 | if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node) |
| 1175 | free(node); /* was a duplicate */ |
| 1176 | |
| 1177 | /* that's ok */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1178 | return 1; |
| 1179 | } |
| 1180 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1181 | /* return 1 if the process is ok |
| 1182 | * return -1 if the parser fail. The err message is filled. |
| 1183 | * return -2 if out of memory |
| 1184 | */ |
| 1185 | int pattern_register(struct pattern_expr *expr, const char *arg, |
| 1186 | struct sample_storage *smp, |
| 1187 | int patflags, char **err) |
| 1188 | { |
| 1189 | int ret; |
| 1190 | struct pattern pattern; |
| 1191 | |
| 1192 | /* initialise pattern */ |
| 1193 | memset(&pattern, 0, sizeof(pattern)); |
| 1194 | pattern.flags = patflags; |
| 1195 | pattern.smp = smp; |
| 1196 | |
| 1197 | /* parse pattern */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 1198 | ret = expr->parse(arg, &pattern, err); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1199 | if (!ret) |
| 1200 | return 0; |
| 1201 | |
| 1202 | /* index pattern */ |
| 1203 | if (!expr->index(expr, &pattern, err)) |
| 1204 | return 0; |
| 1205 | |
| 1206 | return 1; |
| 1207 | } |
| 1208 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1209 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 1210 | * be returned there on errors and the caller will have to free it. |
| 1211 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 1212 | int pattern_read_from_file(struct pattern_expr *expr, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1213 | const char *filename, int patflags, |
| 1214 | char **err) |
| 1215 | { |
| 1216 | FILE *file; |
| 1217 | char *c; |
| 1218 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1219 | int ret = 0; |
| 1220 | int line = 0; |
| 1221 | int code; |
| 1222 | |
| 1223 | file = fopen(filename, "r"); |
| 1224 | if (!file) { |
| 1225 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* now parse all patterns. The file may contain only one pattern per |
| 1230 | * line. If the line contains spaces, they will be part of the pattern. |
| 1231 | * The pattern stops at the first CR, LF or EOF encountered. |
| 1232 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1233 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 1234 | line++; |
| 1235 | c = trash.str; |
| 1236 | |
| 1237 | /* ignore lines beginning with a dash */ |
| 1238 | if (*c == '#') |
| 1239 | continue; |
| 1240 | |
| 1241 | /* strip leading spaces and tabs */ |
| 1242 | while (*c == ' ' || *c == '\t') |
| 1243 | c++; |
| 1244 | |
| 1245 | |
| 1246 | arg = c; |
| 1247 | while (*c && *c != '\n' && *c != '\r') |
| 1248 | c++; |
| 1249 | *c = 0; |
| 1250 | |
| 1251 | /* empty lines are ignored too */ |
| 1252 | if (c == arg) |
| 1253 | continue; |
| 1254 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1255 | code = pattern_register(expr, arg, NULL, patflags, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1256 | if (code == -2) { |
| 1257 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 1258 | goto out_close; |
| 1259 | } |
| 1260 | else if (code < 0) { |
| 1261 | memprintf(err, "%s when loading patterns from file <%s>", *err, filename); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1262 | goto out_close; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | ret = 1; /* success */ |
| 1267 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1268 | out_close: |
| 1269 | fclose(file); |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 1273 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 1274 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 1275 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 1276 | * function returns the matched pattern. In many cases, this pattern can be a |
| 1277 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1278 | */ |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 1279 | 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] | 1280 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1281 | if (!expr->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 1282 | if (fill) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1283 | static_pattern.smp = NULL; |
| 1284 | static_pattern.flags = 0; |
| 1285 | static_pattern.type = SMP_T_UINT; |
| 1286 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 1287 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 1288 | return &static_pattern; |
| 1289 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1290 | return expr->match(smp, expr, fill); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1293 | /* This function browse the pattern expr <expr> to lookup the key <key>. On |
| 1294 | * error it returns 0. On success, it returns 1 and fills either <pat_elt> |
| 1295 | * or <idx_elt> with the respectively matched pointers, and the other one with |
| 1296 | * NULL. Pointers are not set if they're passed as NULL. |
| 1297 | */ |
| 1298 | int pattern_lookup(const char *key, struct pattern_expr *expr, |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1299 | struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1300 | { |
| 1301 | struct pattern pattern; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1302 | struct pattern_list *pat; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1303 | struct ebmb_node *node; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1304 | struct pattern_tree *elt; |
Willy Tarreau | 668ae53 | 2013-12-15 16:42:26 +0100 | [diff] [blame] | 1305 | unsigned int mask = 0; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1306 | |
| 1307 | /* no real pattern */ |
| 1308 | if (!expr->match || expr->match == pat_match_nothing) |
| 1309 | return 0; |
| 1310 | |
| 1311 | /* build lookup pattern */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 1312 | if (!expr->parse(key, &pattern, NULL)) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1313 | return 0; |
| 1314 | |
| 1315 | pat = NULL; |
| 1316 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1317 | |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1318 | /* Try to look up the tree first. IPv6 is not indexed */ |
| 1319 | if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) { |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1320 | /* Check the pattern type */ |
| 1321 | if (pattern.type != SMP_T_STR && |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1322 | pattern.type != SMP_T_IPV4) { |
| 1323 | memprintf(err, "Unexpected pattern type."); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* Convert mask. If the mask is not contiguous, ignore the lookup |
| 1328 | * in the tree, and browse the list. |
| 1329 | */ |
| 1330 | if (expr->match == pat_match_ip) { |
| 1331 | mask = ntohl(pattern.val.ipv4.mask.s_addr); |
| 1332 | if (mask + (mask & -mask) != 0) |
| 1333 | goto browse_list; |
| 1334 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1335 | } |
| 1336 | |
| 1337 | /* browse each node of the tree, and check string */ |
| 1338 | if (expr->match == pat_match_str) { |
| 1339 | for (node = ebmb_first(&expr->pattern_tree); |
| 1340 | node; |
| 1341 | node = ebmb_next(node)) { |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1342 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1343 | if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0) |
| 1344 | goto found; |
| 1345 | } |
| 1346 | } |
| 1347 | else if (expr->match == pat_match_ip) { |
| 1348 | for (node = ebmb_first(&expr->pattern_tree); |
| 1349 | node; |
| 1350 | node = ebmb_next(node)) { |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1351 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1352 | if (elt->node.node.pfx == mask && |
| 1353 | memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0) |
| 1354 | goto found; |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | browse_list: |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 1360 | elt = NULL; |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1361 | if (expr->parse == pat_parse_int || |
| 1362 | expr->parse == pat_parse_len) { |
| 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 (pattern.val.range.min_set != pat->pat.val.range.min_set) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1367 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1368 | if (pattern.val.range.max_set != pat->pat.val.range.max_set) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1369 | continue; |
| 1370 | if (pattern.val.range.min_set && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1371 | pattern.val.range.min != pat->pat.val.range.min) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1372 | continue; |
| 1373 | if (pattern.val.range.max_set && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1374 | pattern.val.range.max != pat->pat.val.range.max) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1375 | continue; |
| 1376 | goto found; |
| 1377 | } |
| 1378 | } |
| 1379 | else if (expr->parse == pat_parse_ip) { |
| 1380 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1381 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1382 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1383 | if (pattern.type != pat->pat.type) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1384 | continue; |
| 1385 | if (pattern.type == SMP_T_IPV4 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1386 | 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] | 1387 | continue; |
| 1388 | if (pattern.type == SMP_T_IPV4 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1389 | 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] | 1390 | continue; |
| 1391 | if (pattern.type == SMP_T_IPV6 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1392 | 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] | 1393 | continue; |
| 1394 | if (pattern.type == SMP_T_IPV6 && |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1395 | pattern.val.ipv6.mask != pat->pat.val.ipv6.mask) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1396 | continue; |
| 1397 | goto found; |
| 1398 | } |
| 1399 | } |
| 1400 | else if (expr->parse == pat_parse_str) { |
| 1401 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1402 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1403 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1404 | if (pattern.len != pat->pat.len) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1405 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1406 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1407 | 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] | 1408 | continue; |
| 1409 | } |
| 1410 | else { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1411 | 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] | 1412 | continue; |
| 1413 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1414 | goto found; |
| 1415 | } |
| 1416 | } |
| 1417 | else if (expr->parse == pat_parse_bin) { |
| 1418 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1419 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1420 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1421 | if (pattern.len != pat->pat.len) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1422 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1423 | 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] | 1424 | continue; |
| 1425 | goto found; |
| 1426 | } |
| 1427 | } |
| 1428 | else if (expr->parse == pat_parse_reg) { |
| 1429 | list_for_each_entry(pat, &expr->patterns, list) { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1430 | if (pat->pat.flags & PAT_F_TREE) |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1431 | continue; |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1432 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1433 | if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1434 | continue; |
| 1435 | } |
| 1436 | else { |
Thierry FOURNIER | 3ead5b9 | 2013-12-13 12:12:18 +0100 | [diff] [blame] | 1437 | if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
Thierry FOURNIER | 35249cb | 2014-01-14 13:38:40 +0100 | [diff] [blame] | 1438 | continue; |
| 1439 | } |
Thierry FOURNIER | 01cdcd4 | 2013-12-10 15:08:01 +0100 | [diff] [blame] | 1440 | goto found; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | /* if we get there, we didn't find the pattern */ |
| 1445 | return 0; |
| 1446 | found: |
| 1447 | if (idx_elt) |
| 1448 | *idx_elt = elt; |
| 1449 | |
| 1450 | if (pat_elt) |
| 1451 | *pat_elt = pat; |
| 1452 | |
| 1453 | return 1; |
| 1454 | } |