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 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 22 | #include <proto/log.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 23 | #include <proto/pattern.h> |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 24 | #include <proto/sample.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 25 | |
| 26 | #include <ebsttree.h> |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 27 | #include <import/lru.h> |
| 28 | #include <import/xxhash.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 29 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 30 | char *pat_match_names[PAT_MATCH_NUM] = { |
| 31 | [PAT_MATCH_FOUND] = "found", |
| 32 | [PAT_MATCH_BOOL] = "bool", |
| 33 | [PAT_MATCH_INT] = "int", |
| 34 | [PAT_MATCH_IP] = "ip", |
| 35 | [PAT_MATCH_BIN] = "bin", |
| 36 | [PAT_MATCH_LEN] = "len", |
| 37 | [PAT_MATCH_STR] = "str", |
| 38 | [PAT_MATCH_BEG] = "beg", |
| 39 | [PAT_MATCH_SUB] = "sub", |
| 40 | [PAT_MATCH_DIR] = "dir", |
| 41 | [PAT_MATCH_DOM] = "dom", |
| 42 | [PAT_MATCH_END] = "end", |
| 43 | [PAT_MATCH_REG] = "reg", |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 44 | [PAT_MATCH_REGM] = "regm", |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 47 | int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 48 | [PAT_MATCH_FOUND] = pat_parse_nothing, |
| 49 | [PAT_MATCH_BOOL] = pat_parse_nothing, |
| 50 | [PAT_MATCH_INT] = pat_parse_int, |
| 51 | [PAT_MATCH_IP] = pat_parse_ip, |
| 52 | [PAT_MATCH_BIN] = pat_parse_bin, |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 53 | [PAT_MATCH_LEN] = pat_parse_int, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 54 | [PAT_MATCH_STR] = pat_parse_str, |
| 55 | [PAT_MATCH_BEG] = pat_parse_str, |
| 56 | [PAT_MATCH_SUB] = pat_parse_str, |
| 57 | [PAT_MATCH_DIR] = pat_parse_str, |
| 58 | [PAT_MATCH_DOM] = pat_parse_str, |
| 59 | [PAT_MATCH_END] = pat_parse_str, |
| 60 | [PAT_MATCH_REG] = pat_parse_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 61 | [PAT_MATCH_REGM] = pat_parse_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 64 | int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = { |
| 65 | [PAT_MATCH_FOUND] = pat_idx_list_val, |
| 66 | [PAT_MATCH_BOOL] = pat_idx_list_val, |
| 67 | [PAT_MATCH_INT] = pat_idx_list_val, |
| 68 | [PAT_MATCH_IP] = pat_idx_tree_ip, |
| 69 | [PAT_MATCH_BIN] = pat_idx_list_ptr, |
| 70 | [PAT_MATCH_LEN] = pat_idx_list_val, |
| 71 | [PAT_MATCH_STR] = pat_idx_tree_str, |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 72 | [PAT_MATCH_BEG] = pat_idx_tree_pfx, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 73 | [PAT_MATCH_SUB] = pat_idx_list_str, |
| 74 | [PAT_MATCH_DIR] = pat_idx_list_str, |
| 75 | [PAT_MATCH_DOM] = pat_idx_list_str, |
| 76 | [PAT_MATCH_END] = pat_idx_list_str, |
| 77 | [PAT_MATCH_REG] = pat_idx_list_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 78 | [PAT_MATCH_REGM] = pat_idx_list_regm, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 81 | void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = { |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 82 | [PAT_MATCH_FOUND] = pat_del_list_val, |
| 83 | [PAT_MATCH_BOOL] = pat_del_list_val, |
| 84 | [PAT_MATCH_INT] = pat_del_list_val, |
| 85 | [PAT_MATCH_IP] = pat_del_tree_ip, |
| 86 | [PAT_MATCH_BIN] = pat_del_list_ptr, |
| 87 | [PAT_MATCH_LEN] = pat_del_list_val, |
| 88 | [PAT_MATCH_STR] = pat_del_tree_str, |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 89 | [PAT_MATCH_BEG] = pat_del_tree_str, |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 90 | [PAT_MATCH_SUB] = pat_del_list_ptr, |
| 91 | [PAT_MATCH_DIR] = pat_del_list_ptr, |
| 92 | [PAT_MATCH_DOM] = pat_del_list_ptr, |
| 93 | [PAT_MATCH_END] = pat_del_list_ptr, |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 94 | [PAT_MATCH_REG] = pat_del_list_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 95 | [PAT_MATCH_REGM] = pat_del_list_reg, |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 98 | void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = { |
| 99 | [PAT_MATCH_FOUND] = pat_prune_val, |
| 100 | [PAT_MATCH_BOOL] = pat_prune_val, |
| 101 | [PAT_MATCH_INT] = pat_prune_val, |
| 102 | [PAT_MATCH_IP] = pat_prune_val, |
| 103 | [PAT_MATCH_BIN] = pat_prune_ptr, |
| 104 | [PAT_MATCH_LEN] = pat_prune_val, |
| 105 | [PAT_MATCH_STR] = pat_prune_ptr, |
| 106 | [PAT_MATCH_BEG] = pat_prune_ptr, |
| 107 | [PAT_MATCH_SUB] = pat_prune_ptr, |
| 108 | [PAT_MATCH_DIR] = pat_prune_ptr, |
| 109 | [PAT_MATCH_DOM] = pat_prune_ptr, |
| 110 | [PAT_MATCH_END] = pat_prune_ptr, |
| 111 | [PAT_MATCH_REG] = pat_prune_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 112 | [PAT_MATCH_REGM] = pat_prune_reg, |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 115 | 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] | 116 | [PAT_MATCH_FOUND] = NULL, |
| 117 | [PAT_MATCH_BOOL] = pat_match_nothing, |
| 118 | [PAT_MATCH_INT] = pat_match_int, |
| 119 | [PAT_MATCH_IP] = pat_match_ip, |
| 120 | [PAT_MATCH_BIN] = pat_match_bin, |
| 121 | [PAT_MATCH_LEN] = pat_match_len, |
| 122 | [PAT_MATCH_STR] = pat_match_str, |
| 123 | [PAT_MATCH_BEG] = pat_match_beg, |
| 124 | [PAT_MATCH_SUB] = pat_match_sub, |
| 125 | [PAT_MATCH_DIR] = pat_match_dir, |
| 126 | [PAT_MATCH_DOM] = pat_match_dom, |
| 127 | [PAT_MATCH_END] = pat_match_end, |
| 128 | [PAT_MATCH_REG] = pat_match_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 129 | [PAT_MATCH_REGM] = pat_match_regm, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 132 | /* Just used for checking configuration compatibility */ |
| 133 | int pat_match_types[PAT_MATCH_NUM] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 134 | [PAT_MATCH_FOUND] = SMP_T_SINT, |
| 135 | [PAT_MATCH_BOOL] = SMP_T_SINT, |
| 136 | [PAT_MATCH_INT] = SMP_T_SINT, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 137 | [PAT_MATCH_IP] = SMP_T_ADDR, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 138 | [PAT_MATCH_BIN] = SMP_T_BIN, |
| 139 | [PAT_MATCH_LEN] = SMP_T_STR, |
| 140 | [PAT_MATCH_STR] = SMP_T_STR, |
| 141 | [PAT_MATCH_BEG] = SMP_T_STR, |
| 142 | [PAT_MATCH_SUB] = SMP_T_STR, |
| 143 | [PAT_MATCH_DIR] = SMP_T_STR, |
| 144 | [PAT_MATCH_DOM] = SMP_T_STR, |
| 145 | [PAT_MATCH_END] = SMP_T_STR, |
| 146 | [PAT_MATCH_REG] = SMP_T_STR, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 147 | [PAT_MATCH_REGM] = SMP_T_STR, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 150 | /* this struct is used to return information */ |
| 151 | static struct pattern static_pattern; |
| 152 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 153 | /* This is the root of the list of all pattern_ref avalaibles. */ |
| 154 | struct list pattern_reference = LIST_HEAD_INIT(pattern_reference); |
| 155 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 156 | static struct lru64_head *pat_lru_tree; |
| 157 | static unsigned long long pat_lru_seed; |
| 158 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 159 | /* |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 160 | * |
| 161 | * The following functions are not exported and are used by internals process |
| 162 | * of pattern matching |
| 163 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 164 | */ |
| 165 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 166 | /* Background: Fast way to find a zero byte in a word |
| 167 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 168 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 169 | * |
| 170 | * To look for 4 different byte values, xor the word with those bytes and |
| 171 | * then check for zero bytes: |
| 172 | * |
| 173 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 174 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 175 | * and <c> is the character that is being tested |
| 176 | */ |
| 177 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 178 | { |
| 179 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 180 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 181 | } |
| 182 | |
| 183 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 184 | { |
| 185 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 186 | } |
| 187 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 188 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 189 | /* |
| 190 | * |
| 191 | * These functions are exported and may be used by any other component. |
| 192 | * |
Willy Tarreau | 5def8ef | 2014-08-29 15:19:33 +0200 | [diff] [blame] | 193 | * The following functions are used for parsing pattern matching input value. |
| 194 | * The <text> contain the string to be parsed. <pattern> must be a preallocated |
| 195 | * pattern. The pat_parse_* functions fill this structure with the parsed value. |
| 196 | * <err> is filled with an error message built with memprintf() function. It is |
| 197 | * allowed to use a trash as a temporary storage for the returned pattern, as |
| 198 | * the next call after these functions will be pat_idx_*. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 199 | * |
Willy Tarreau | 5def8ef | 2014-08-29 15:19:33 +0200 | [diff] [blame] | 200 | * In success case, the pat_parse_* function returns 1. If the function |
| 201 | * fails, it returns 0 and <err> is filled. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 202 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 204 | /* ignore the current line */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 205 | int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 206 | { |
| 207 | return 1; |
| 208 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 209 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 210 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 211 | int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 212 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 213 | pattern->type = SMP_T_STR; |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 214 | pattern->ptr.str = (char *)text; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 215 | pattern->len = strlen(text); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 216 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 219 | /* Parse a binary written in hexa. It is allocated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 220 | int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 221 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 222 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 223 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 224 | pattern->type = SMP_T_BIN; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 225 | trash = get_trash_chunk(); |
| 226 | pattern->len = trash->size; |
| 227 | pattern->ptr.str = trash->str; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 228 | return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 231 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 232 | int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 233 | { |
Thierry FOURNIER | 0b6d15f | 2014-01-29 19:35:16 +0100 | [diff] [blame] | 234 | pattern->ptr.str = (char *)text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 239 | * one integer is read, it is set as both min and max. An operator may be |
| 240 | * specified as the prefix, among this list of 5 : |
| 241 | * |
| 242 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 243 | * |
| 244 | * The default operator is "eq". It supports range matching. Ranges are |
| 245 | * rejected for other operators. The operator may be changed at any time. |
| 246 | * The operator is stored in the 'opaque' argument. |
| 247 | * |
| 248 | * 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] | 249 | * the caller will have to free it. The function returns zero on error, and |
| 250 | * non-zero on success. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 251 | * |
| 252 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 253 | int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 254 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 255 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 256 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 257 | pattern->type = SMP_T_SINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 258 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 259 | /* Empty string is not valid */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 260 | if (!*text) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 261 | goto not_valid_range; |
| 262 | |
| 263 | /* Search ':' or '-' separator. */ |
| 264 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 265 | ptr++; |
| 266 | |
| 267 | /* If separator not found. */ |
| 268 | if (!*ptr) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 269 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) { |
| 270 | memprintf(err, "'%s' is not a number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 271 | return 0; |
| 272 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 273 | pattern->val.range.max = pattern->val.range.min; |
| 274 | pattern->val.range.min_set = 1; |
| 275 | pattern->val.range.max_set = 1; |
| 276 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 279 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 280 | if (ptr == text && *(ptr + 1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 281 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 282 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 283 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 284 | pattern->val.range.min_set = 0; |
| 285 | pattern->val.range.max_set = 1; |
| 286 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 289 | /* If separator is the last character. */ |
| 290 | if (*(ptr + 1) == '\0') { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 291 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 292 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 294 | pattern->val.range.min_set = 1; |
| 295 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 296 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 297 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 298 | |
| 299 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 300 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 301 | goto not_valid_range; |
| 302 | |
| 303 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 304 | goto not_valid_range; |
| 305 | |
| 306 | if (pattern->val.range.min > pattern->val.range.max) |
| 307 | goto not_valid_range; |
| 308 | |
| 309 | pattern->val.range.min_set = 1; |
| 310 | pattern->val.range.max_set = 1; |
| 311 | return 1; |
| 312 | |
| 313 | not_valid_range: |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 314 | memprintf(err, "'%s' is not a valid number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 315 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 319 | * '-'. The version consists in a major and a minor, both of which must be |
| 320 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 321 | * integer. |
| 322 | * If only one version is read, it is set as both min and max. Just like for |
| 323 | * pure integers, an operator may be specified as the prefix, among this list |
| 324 | * of 5 : |
| 325 | * |
| 326 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 327 | * |
| 328 | * The default operator is "eq". It supports range matching. Ranges are |
| 329 | * rejected for other operators. The operator may be changed at any time. |
| 330 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 331 | * such as the following one : |
| 332 | * |
| 333 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 334 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 335 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 336 | * |
| 337 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 338 | int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 339 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 340 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 341 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 342 | pattern->type = SMP_T_SINT; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 343 | |
| 344 | /* Search ':' or '-' separator. */ |
| 345 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 346 | ptr++; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 347 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 348 | /* If separator not found. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 349 | if (*ptr == '\0' && ptr > text) { |
| 350 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 351 | memprintf(err, "'%s' is not a dotted number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 352 | return 0; |
| 353 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 354 | pattern->val.range.max = pattern->val.range.min; |
| 355 | pattern->val.range.min_set = 1; |
| 356 | pattern->val.range.max_set = 1; |
| 357 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 360 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 361 | if (ptr == text && *(ptr+1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 362 | 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] | 363 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 364 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 365 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 366 | pattern->val.range.min_set = 0; |
| 367 | pattern->val.range.max_set = 1; |
| 368 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 371 | /* If separator is the last character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 372 | if (ptr == &text[strlen(text)-1]) { |
| 373 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 374 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 375 | return 0; |
| 376 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 377 | pattern->val.range.min_set = 1; |
| 378 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 383 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 384 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | 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] | 388 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | if (pattern->val.range.min > pattern->val.range.max) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 392 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 393 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 394 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 395 | pattern->val.range.min_set = 1; |
| 396 | pattern->val.range.max_set = 1; |
| 397 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 401 | * The addr may either be an IPv4 address or a hostname. The mask |
| 402 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 403 | * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6). |
| 404 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 405 | int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 406 | { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 407 | if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING), |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 408 | &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 409 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 410 | return 1; |
| 411 | } |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 412 | else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 413 | pattern->type = SMP_T_IPV6; |
| 414 | return 1; |
| 415 | } |
| 416 | else { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 417 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | } |
| 421 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 422 | /* |
| 423 | * |
| 424 | * These functions are exported and may be used by any other component. |
| 425 | * |
| 426 | * This fucntion just take a sample <smp> and check if this sample match |
| 427 | * with the pattern <pattern>. This fucntion return just PAT_MATCH or |
| 428 | * PAT_NOMATCH. |
| 429 | * |
| 430 | */ |
| 431 | |
| 432 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 433 | 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] | 434 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 435 | if (smp->data.u.sint) { |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 436 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 437 | static_pattern.data = NULL; |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 438 | static_pattern.ref = NULL; |
Thierry FOURNIER | e5978bf | 2014-03-17 19:53:10 +0100 | [diff] [blame] | 439 | static_pattern.type = 0; |
| 440 | static_pattern.ptr.str = NULL; |
| 441 | } |
| 442 | return &static_pattern; |
| 443 | } |
| 444 | else |
| 445 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | |
| 449 | /* 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] | 450 | 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] | 451 | { |
| 452 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 453 | struct ebmb_node *node; |
| 454 | char prev; |
| 455 | struct pattern_tree *elt; |
| 456 | struct pattern_list *lst; |
| 457 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 458 | struct pattern *ret = NULL; |
| 459 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 460 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 461 | /* Lookup a string in the expression's pattern tree. */ |
| 462 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 463 | /* we may have to force a trailing zero on the test pattern */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 464 | prev = smp->data.u.str.str[smp->data.u.str.len]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 465 | if (prev) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 466 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 467 | node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 468 | if (prev) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 469 | smp->data.u.str.str[smp->data.u.str.len] = prev; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 470 | |
| 471 | if (node) { |
| 472 | if (fill) { |
| 473 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 474 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 475 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 476 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 477 | static_pattern.type = SMP_T_STR; |
| 478 | static_pattern.ptr.str = (char *)elt->node.key; |
| 479 | } |
| 480 | return &static_pattern; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 485 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 486 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 487 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 488 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 489 | pat_lru_tree, expr, expr->revision); |
| 490 | if (lru && lru->domain) |
| 491 | return lru->data; |
| 492 | } |
| 493 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 494 | list_for_each_entry(lst, &expr->patterns, list) { |
| 495 | pattern = &lst->pat; |
| 496 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 497 | if (pattern->len != smp->data.u.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 498 | continue; |
| 499 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 500 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 501 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) || |
| 502 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 503 | ret = pattern; |
| 504 | break; |
| 505 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 508 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 509 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 510 | |
| 511 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* 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] | 515 | 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] | 516 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 517 | struct pattern_list *lst; |
| 518 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 519 | struct pattern *ret = NULL; |
| 520 | struct lru64 *lru = NULL; |
| 521 | |
| 522 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 523 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 524 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 525 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 526 | pat_lru_tree, expr, expr->revision); |
| 527 | if (lru && lru->domain) |
| 528 | return lru->data; |
| 529 | } |
| 530 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 531 | list_for_each_entry(lst, &expr->patterns, list) { |
| 532 | pattern = &lst->pat; |
| 533 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 534 | if (pattern->len != smp->data.u.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 535 | continue; |
| 536 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 537 | if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 538 | ret = pattern; |
| 539 | break; |
| 540 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 543 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 544 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 545 | |
| 546 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 550 | * and restores the previous character when leaving. This function fills |
| 551 | * a matching array. |
| 552 | */ |
| 553 | struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill) |
| 554 | { |
| 555 | struct pattern_list *lst; |
| 556 | struct pattern *pattern; |
| 557 | struct pattern *ret = NULL; |
| 558 | |
| 559 | list_for_each_entry(lst, &expr->patterns, list) { |
| 560 | pattern = &lst->pat; |
| 561 | |
| 562 | if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len, |
| 563 | MAX_MATCH, pmatch, 0)) { |
| 564 | ret = pattern; |
| 565 | smp->ctx.a[0] = pmatch; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 574 | * and restores the previous character when leaving. |
| 575 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 576 | 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] | 577 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 578 | struct pattern_list *lst; |
| 579 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 580 | struct pattern *ret = NULL; |
| 581 | struct lru64 *lru = NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 582 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 583 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 584 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 585 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 586 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 587 | pat_lru_tree, expr, expr->revision); |
| 588 | if (lru && lru->domain) |
| 589 | return lru->data; |
| 590 | } |
| 591 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 592 | list_for_each_entry(lst, &expr->patterns, list) { |
| 593 | pattern = &lst->pat; |
| 594 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 595 | if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) { |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 596 | ret = pattern; |
| 597 | break; |
| 598 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 599 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 600 | |
| 601 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 602 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 603 | |
| 604 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 608 | 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] | 609 | { |
| 610 | int icase; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 611 | struct ebmb_node *node; |
| 612 | char prev; |
| 613 | struct pattern_tree *elt; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 614 | struct pattern_list *lst; |
| 615 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 616 | struct pattern *ret = NULL; |
| 617 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 618 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 619 | /* Lookup a string in the expression's pattern tree. */ |
| 620 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 621 | /* we may have to force a trailing zero on the test pattern */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 622 | prev = smp->data.u.str.str[smp->data.u.str.len]; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 623 | if (prev) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 624 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 625 | node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 626 | if (prev) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 627 | smp->data.u.str.str[smp->data.u.str.len] = prev; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 628 | |
| 629 | if (node) { |
| 630 | if (fill) { |
| 631 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 632 | static_pattern.data = elt->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 633 | static_pattern.ref = elt->ref; |
| 634 | static_pattern.sflags = PAT_SF_TREE; |
| 635 | static_pattern.type = SMP_T_STR; |
| 636 | static_pattern.ptr.str = (char *)elt->node.key; |
| 637 | } |
| 638 | return &static_pattern; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 643 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 644 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 645 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 646 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 647 | pat_lru_tree, expr, expr->revision); |
| 648 | if (lru && lru->domain) |
| 649 | return lru->data; |
| 650 | } |
| 651 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 652 | list_for_each_entry(lst, &expr->patterns, list) { |
| 653 | pattern = &lst->pat; |
| 654 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 655 | if (pattern->len > smp->data.u.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 656 | continue; |
| 657 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 658 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 659 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) || |
| 660 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 661 | continue; |
| 662 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 663 | ret = pattern; |
| 664 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 665 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 666 | |
| 667 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 668 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 669 | |
| 670 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 674 | 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] | 675 | { |
| 676 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 677 | struct pattern_list *lst; |
| 678 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 679 | struct pattern *ret = NULL; |
| 680 | struct lru64 *lru = NULL; |
| 681 | |
| 682 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 683 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 684 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 685 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 686 | pat_lru_tree, expr, expr->revision); |
| 687 | if (lru && lru->domain) |
| 688 | return lru->data; |
| 689 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 690 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 691 | list_for_each_entry(lst, &expr->patterns, list) { |
| 692 | pattern = &lst->pat; |
| 693 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 694 | if (pattern->len > smp->data.u.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 695 | continue; |
| 696 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 697 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 698 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) || |
| 699 | (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 700 | continue; |
| 701 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 702 | ret = pattern; |
| 703 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 704 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 705 | |
| 706 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 707 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 708 | |
| 709 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* Checks that the pattern is included inside the tested string. |
| 713 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 714 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 715 | 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] | 716 | { |
| 717 | int icase; |
| 718 | char *end; |
| 719 | char *c; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 720 | struct pattern_list *lst; |
| 721 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 722 | struct pattern *ret = NULL; |
| 723 | struct lru64 *lru = NULL; |
| 724 | |
| 725 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 726 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 727 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 728 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 729 | pat_lru_tree, expr, expr->revision); |
| 730 | if (lru && lru->domain) |
| 731 | return lru->data; |
| 732 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 733 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 734 | list_for_each_entry(lst, &expr->patterns, list) { |
| 735 | pattern = &lst->pat; |
| 736 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 737 | if (pattern->len > smp->data.u.str.len) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 738 | continue; |
| 739 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 740 | end = smp->data.u.str.str + smp->data.u.str.len - pattern->len; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 741 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 742 | if (icase) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 743 | for (c = smp->data.u.str.str; c <= end; c++) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 744 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 745 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 746 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 747 | ret = pattern; |
| 748 | goto leave; |
| 749 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 750 | } |
| 751 | } else { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 752 | for (c = smp->data.u.str.str; c <= end; c++) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 753 | if (*c != *pattern->ptr.str) |
| 754 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 755 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 756 | ret = pattern; |
| 757 | goto leave; |
| 758 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 759 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 762 | leave: |
| 763 | if (lru) |
Christopher Faulet | f90ac55 | 2015-06-09 17:06:17 +0200 | [diff] [blame] | 764 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 765 | |
| 766 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* This one is used by other real functions. It checks that the pattern is |
| 770 | * included inside the tested string, but enclosed between the specified |
| 771 | * delimiters or at the beginning or end of the string. The delimiters are |
| 772 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 773 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 774 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 775 | static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 776 | { |
| 777 | int may_match, icase; |
| 778 | char *c, *end; |
| 779 | char *ps; |
| 780 | int pl; |
| 781 | |
| 782 | pl = pattern->len; |
| 783 | ps = pattern->ptr.str; |
| 784 | |
| 785 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 786 | pl--; |
| 787 | ps++; |
| 788 | } |
| 789 | |
| 790 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 791 | pl--; |
| 792 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 793 | if (pl > smp->data.u.str.len) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 794 | return PAT_NOMATCH; |
| 795 | |
| 796 | may_match = 1; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 797 | icase = mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 798 | end = smp->data.u.str.str + smp->data.u.str.len - pl; |
| 799 | for (c = smp->data.u.str.str; c <= end; c++) { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 800 | if (is_delimiter(*c, delimiters)) { |
| 801 | may_match = 1; |
| 802 | continue; |
| 803 | } |
| 804 | |
| 805 | if (!may_match) |
| 806 | continue; |
| 807 | |
| 808 | if (icase) { |
| 809 | if ((tolower(*c) == tolower(*ps)) && |
| 810 | (strncasecmp(ps, c, pl) == 0) && |
| 811 | (c == end || is_delimiter(c[pl], delimiters))) |
| 812 | return PAT_MATCH; |
| 813 | } else { |
| 814 | if ((*c == *ps) && |
| 815 | (strncmp(ps, c, pl) == 0) && |
| 816 | (c == end || is_delimiter(c[pl], delimiters))) |
| 817 | return PAT_MATCH; |
| 818 | } |
| 819 | may_match = 0; |
| 820 | } |
| 821 | return PAT_NOMATCH; |
| 822 | } |
| 823 | |
| 824 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 825 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 826 | * Delimiters at the beginning or end of the pattern are ignored. |
| 827 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 828 | 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] | 829 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 830 | struct pattern_list *lst; |
| 831 | struct pattern *pattern; |
| 832 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 833 | list_for_each_entry(lst, &expr->patterns, list) { |
| 834 | pattern = &lst->pat; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 835 | if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?'))) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 836 | return pattern; |
| 837 | } |
| 838 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 842 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 843 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 844 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 845 | 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] | 846 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 847 | struct pattern_list *lst; |
| 848 | struct pattern *pattern; |
| 849 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 850 | list_for_each_entry(lst, &expr->patterns, list) { |
| 851 | pattern = &lst->pat; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 852 | if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':'))) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 853 | return pattern; |
| 854 | } |
| 855 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 859 | 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] | 860 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 861 | struct pattern_list *lst; |
| 862 | struct pattern *pattern; |
| 863 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 864 | list_for_each_entry(lst, &expr->patterns, list) { |
| 865 | pattern = &lst->pat; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 866 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) && |
| 867 | (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 868 | return pattern; |
| 869 | } |
| 870 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* 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] | 874 | 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] | 875 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 876 | struct pattern_list *lst; |
| 877 | struct pattern *pattern; |
| 878 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 879 | list_for_each_entry(lst, &expr->patterns, list) { |
| 880 | pattern = &lst->pat; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 881 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) && |
| 882 | (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max)) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 883 | return pattern; |
| 884 | } |
| 885 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 886 | } |
| 887 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 888 | 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] | 889 | { |
| 890 | unsigned int v4; /* in network byte order */ |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 891 | struct in6_addr tmp6; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 892 | struct in_addr *s; |
| 893 | struct ebmb_node *node; |
| 894 | struct pattern_tree *elt; |
| 895 | struct pattern_list *lst; |
| 896 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 897 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 898 | /* The input sample is IPv4. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 899 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 900 | /* Lookup an IPv4 address in the expression's pattern tree using |
| 901 | * the longest match method. |
| 902 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 903 | s = &smp->data.u.ipv4; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 904 | node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
| 905 | if (node) { |
| 906 | if (fill) { |
| 907 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 908 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 909 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 910 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 911 | static_pattern.type = SMP_T_IPV4; |
| 912 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 913 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 914 | return NULL; |
| 915 | } |
| 916 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 917 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 918 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 919 | /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4 |
| 920 | * sample address to IPv6 with the mapping method using the ::ffff: |
| 921 | * prefix, and try to lookup in the IPv6 tree. |
| 922 | */ |
| 923 | memset(&tmp6, 0, 10); |
| 924 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 925 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 926 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6); |
| 927 | if (node) { |
| 928 | if (fill) { |
| 929 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 930 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 931 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 932 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 933 | static_pattern.type = SMP_T_IPV6; |
| 934 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 935 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
| 936 | } |
| 937 | return &static_pattern; |
| 938 | } |
| 939 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 940 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 941 | /* The input sample is IPv6. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 942 | if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 943 | /* Lookup an IPv6 address in the expression's pattern tree using |
| 944 | * the longest match method. |
| 945 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 946 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 947 | if (node) { |
| 948 | if (fill) { |
| 949 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 950 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 951 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 952 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 953 | static_pattern.type = SMP_T_IPV6; |
| 954 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 955 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 956 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 957 | return &static_pattern; |
| 958 | } |
| 959 | |
| 960 | /* Try to convert 6 to 4 when the start of the ipv6 address match the |
| 961 | * following forms : |
| 962 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 963 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 964 | * - 2002:ip:v4:: (6to4) |
| 965 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 966 | if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 && |
| 967 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 && |
| 968 | (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 || |
| 969 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) || |
| 970 | *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) { |
| 971 | if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0) |
| 972 | v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12]; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 973 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 974 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) + |
| 975 | ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4])); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 976 | |
| 977 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 978 | * match method. |
| 979 | */ |
| 980 | node = ebmb_lookup_longest(&expr->pattern_tree, &v4); |
| 981 | if (node) { |
| 982 | if (fill) { |
| 983 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 984 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 985 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 986 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 987 | static_pattern.type = SMP_T_IPV4; |
| 988 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 989 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 990 | return NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 991 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 992 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 993 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 994 | } |
| 995 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 996 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 997 | /* Lookup in the list. the list contain only IPv4 patterns */ |
| 998 | list_for_each_entry(lst, &expr->patterns, list) { |
| 999 | pattern = &lst->pat; |
| 1000 | |
| 1001 | /* The input sample is IPv4, use it as is. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1002 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1003 | v4 = smp->data.u.ipv4.s_addr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1004 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1005 | else if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1006 | /* v4 match on a V6 sample. We want to check at least for |
| 1007 | * the following forms : |
| 1008 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 1009 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 1010 | * - 2002:ip:v4:: (6to4) |
| 1011 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1012 | if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 && |
| 1013 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 && |
| 1014 | (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 || |
| 1015 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 1016 | v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1017 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1018 | else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) { |
| 1019 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) + |
| 1020 | ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4])); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1021 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1022 | else |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1023 | continue; |
Andreas Seltenreich | f065319 | 2016-03-03 20:08:35 +0100 | [diff] [blame] | 1024 | } else { |
| 1025 | /* impossible */ |
| 1026 | continue; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1027 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1028 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1029 | /* Check if the input sample match the current pattern. */ |
| 1030 | 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] | 1031 | return pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1032 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1033 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1036 | void free_pattern_tree(struct eb_root *root) |
| 1037 | { |
| 1038 | struct eb_node *node, *next; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1039 | struct pattern_tree *elt; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1040 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1041 | node = eb_first(root); |
| 1042 | while (node) { |
| 1043 | next = eb_next(node); |
| 1044 | eb_delete(node); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1045 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1046 | free(elt->data); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1047 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1048 | node = next; |
| 1049 | } |
| 1050 | } |
| 1051 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1052 | void pat_prune_val(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1053 | { |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1054 | struct pattern_list *pat, *tmp; |
| 1055 | |
| 1056 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1057 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1058 | free(pat); |
| 1059 | } |
| 1060 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1061 | free_pattern_tree(&expr->pattern_tree); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1062 | free_pattern_tree(&expr->pattern_tree_2); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1063 | LIST_INIT(&expr->patterns); |
| 1064 | } |
| 1065 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1066 | void pat_prune_ptr(struct pattern_expr *expr) |
| 1067 | { |
| 1068 | struct pattern_list *pat, *tmp; |
| 1069 | |
| 1070 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 1071 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1072 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1073 | free(pat); |
| 1074 | } |
| 1075 | |
| 1076 | free_pattern_tree(&expr->pattern_tree); |
| 1077 | free_pattern_tree(&expr->pattern_tree_2); |
| 1078 | LIST_INIT(&expr->patterns); |
| 1079 | } |
| 1080 | |
| 1081 | void pat_prune_reg(struct pattern_expr *expr) |
| 1082 | { |
| 1083 | struct pattern_list *pat, *tmp; |
| 1084 | |
| 1085 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 1086 | regex_free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1087 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1088 | free(pat); |
| 1089 | } |
| 1090 | |
| 1091 | free_pattern_tree(&expr->pattern_tree); |
| 1092 | free_pattern_tree(&expr->pattern_tree_2); |
| 1093 | LIST_INIT(&expr->patterns); |
| 1094 | } |
| 1095 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1096 | /* |
| 1097 | * |
| 1098 | * The following functions are used for the pattern indexation |
| 1099 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1100 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1101 | |
| 1102 | 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] | 1103 | { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1104 | struct pattern_list *patl; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1105 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1106 | /* allocate pattern */ |
| 1107 | patl = calloc(1, sizeof(*patl)); |
| 1108 | if (!patl) { |
| 1109 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1112 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1113 | /* duplicate pattern */ |
| 1114 | memcpy(&patl->pat, pat, sizeof(*pat)); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1115 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1116 | /* chain pattern in the expression */ |
| 1117 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1118 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1119 | |
| 1120 | /* that's ok */ |
| 1121 | return 1; |
| 1122 | } |
| 1123 | |
| 1124 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1125 | { |
| 1126 | struct pattern_list *patl; |
| 1127 | |
| 1128 | /* allocate pattern */ |
| 1129 | patl = calloc(1, sizeof(*patl)); |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1130 | if (!patl) { |
| 1131 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1132 | return 0; |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1133 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1134 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1135 | /* duplicate pattern */ |
| 1136 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1137 | patl->pat.ptr.ptr = malloc(patl->pat.len); |
| 1138 | if (!patl->pat.ptr.ptr) { |
| 1139 | free(patl); |
| 1140 | memprintf(err, "out of memory while indexing pattern"); |
| 1141 | return 0; |
| 1142 | } |
| 1143 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1144 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1145 | /* chain pattern in the expression */ |
| 1146 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1147 | expr->revision = rdtsc(); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1148 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1149 | /* that's ok */ |
| 1150 | return 1; |
| 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 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1154 | { |
| 1155 | struct pattern_list *patl; |
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 | /* allocate pattern */ |
| 1158 | patl = calloc(1, sizeof(*patl)); |
| 1159 | if (!patl) { |
| 1160 | memprintf(err, "out of memory while indexing pattern"); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /* duplicate pattern */ |
| 1165 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1166 | patl->pat.ptr.str = malloc(patl->pat.len + 1); |
| 1167 | if (!patl->pat.ptr.str) { |
| 1168 | free(patl); |
| 1169 | memprintf(err, "out of memory while indexing pattern"); |
| 1170 | return 0; |
| 1171 | } |
| 1172 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
| 1173 | patl->pat.ptr.str[patl->pat.len] = '\0'; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1174 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1175 | /* chain pattern in the expression */ |
| 1176 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1177 | expr->revision = rdtsc(); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1178 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1179 | /* that's ok */ |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1183 | int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err) |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1184 | { |
| 1185 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1186 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1187 | /* allocate pattern */ |
| 1188 | patl = calloc(1, sizeof(*patl)); |
| 1189 | if (!patl) { |
| 1190 | memprintf(err, "out of memory while indexing pattern"); |
| 1191 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1192 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1193 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1194 | /* duplicate pattern */ |
| 1195 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1196 | |
| 1197 | /* allocate regex */ |
| 1198 | patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg)); |
| 1199 | if (!patl->pat.ptr.reg) { |
| 1200 | free(patl); |
| 1201 | memprintf(err, "out of memory while indexing pattern"); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /* compile regex */ |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1206 | if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg, |
| 1207 | !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1208 | free(patl->pat.ptr.reg); |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1209 | free(patl); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1210 | return 0; |
| 1211 | } |
| 1212 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1213 | /* chain pattern in the expression */ |
| 1214 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1215 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1216 | |
| 1217 | /* that's ok */ |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1221 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1222 | { |
| 1223 | return pat_idx_list_reg_cap(expr, pat, 0, err); |
| 1224 | } |
| 1225 | |
| 1226 | int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1227 | { |
| 1228 | return pat_idx_list_reg_cap(expr, pat, 1, err); |
| 1229 | } |
| 1230 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1231 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1232 | { |
| 1233 | unsigned int mask; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1234 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1235 | |
| 1236 | /* Only IPv4 can be indexed */ |
| 1237 | if (pat->type == SMP_T_IPV4) { |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1238 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 1239 | * insert the network into the tree. A continuous mask has only |
| 1240 | * ones on the left. This means that this mask + its lower bit |
| 1241 | * added once again is null. |
| 1242 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1243 | mask = ntohl(pat->val.ipv4.mask.s_addr); |
| 1244 | if (mask + (mask & -mask) == 0) { |
| 1245 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1246 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1247 | /* node memory allocation */ |
| 1248 | node = calloc(1, sizeof(*node) + 4); |
| 1249 | if (!node) { |
| 1250 | memprintf(err, "out of memory while loading pattern"); |
| 1251 | return 0; |
| 1252 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1253 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1254 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1255 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1256 | node->ref = pat->ref; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1257 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1258 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1259 | memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */ |
| 1260 | node->node.node.pfx = mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1261 | |
| 1262 | /* Insert the entry. */ |
| 1263 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1264 | expr->revision = rdtsc(); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1265 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1266 | /* that's ok */ |
| 1267 | return 1; |
| 1268 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1269 | else { |
| 1270 | /* If the mask is not contiguous, just add the pattern to the list */ |
| 1271 | return pat_idx_list_val(expr, pat, err); |
| 1272 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1273 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1274 | else if (pat->type == SMP_T_IPV6) { |
| 1275 | /* IPv6 also can be indexed */ |
| 1276 | node = calloc(1, sizeof(*node) + 16); |
| 1277 | if (!node) { |
| 1278 | memprintf(err, "out of memory while loading pattern"); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1283 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1284 | node->ref = pat->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1285 | |
| 1286 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1287 | memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */ |
| 1288 | node->node.node.pfx = pat->val.ipv6.mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1289 | |
| 1290 | /* Insert the entry. */ |
| 1291 | ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1292 | expr->revision = rdtsc(); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* that's ok */ |
| 1295 | return 1; |
| 1296 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1297 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1298 | return 0; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1302 | { |
| 1303 | int len; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1304 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1305 | |
| 1306 | /* Only string can be indexed */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1307 | if (pat->type != SMP_T_STR) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1308 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1309 | smp_to_type[pat->type]); |
| 1310 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1311 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1312 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1313 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1314 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1315 | return pat_idx_list_str(expr, pat, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1316 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1317 | /* Process the key len */ |
| 1318 | len = strlen(pat->ptr.str) + 1; |
| 1319 | |
| 1320 | /* node memory allocation */ |
| 1321 | node = calloc(1, sizeof(*node) + len); |
| 1322 | if (!node) { |
| 1323 | memprintf(err, "out of memory while loading pattern"); |
| 1324 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1327 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1328 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1329 | node->ref = pat->ref; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1330 | |
| 1331 | /* copy the string */ |
| 1332 | memcpy(node->node.key, pat->ptr.str, len); |
| 1333 | |
| 1334 | /* index the new node */ |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1335 | ebst_insert(&expr->pattern_tree, &node->node); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1336 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1337 | |
| 1338 | /* that's ok */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1339 | return 1; |
| 1340 | } |
| 1341 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1342 | int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1343 | { |
| 1344 | int len; |
| 1345 | struct pattern_tree *node; |
| 1346 | |
| 1347 | /* Only string can be indexed */ |
| 1348 | if (pat->type != SMP_T_STR) { |
| 1349 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1350 | smp_to_type[pat->type]); |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1355 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
| 1356 | return pat_idx_list_str(expr, pat, err); |
| 1357 | |
| 1358 | /* Process the key len */ |
| 1359 | len = strlen(pat->ptr.str); |
| 1360 | |
| 1361 | /* node memory allocation */ |
| 1362 | node = calloc(1, sizeof(*node) + len + 1); |
| 1363 | if (!node) { |
| 1364 | memprintf(err, "out of memory while loading pattern"); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1369 | node->data = pat->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1370 | node->ref = pat->ref; |
| 1371 | |
| 1372 | /* copy the string and the trailing zero */ |
| 1373 | memcpy(node->node.key, pat->ptr.str, len + 1); |
| 1374 | node->node.node.pfx = len * 8; |
| 1375 | |
| 1376 | /* index the new node */ |
| 1377 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, len); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1378 | expr->revision = rdtsc(); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1379 | |
| 1380 | /* that's ok */ |
| 1381 | return 1; |
| 1382 | } |
| 1383 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1384 | void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1385 | { |
| 1386 | struct pattern_list *pat; |
| 1387 | struct pattern_list *safe; |
| 1388 | |
| 1389 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1390 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1391 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1392 | continue; |
| 1393 | |
| 1394 | /* Delete and free entry. */ |
| 1395 | LIST_DEL(&pat->list); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1396 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1397 | free(pat); |
| 1398 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1399 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1402 | void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1403 | { |
| 1404 | struct ebmb_node *node, *next_node; |
| 1405 | struct pattern_tree *elt; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1406 | |
| 1407 | /* browse each node of the tree for IPv4 addresses. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1408 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1409 | node; |
| 1410 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1411 | /* Extract container of the tree node. */ |
| 1412 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1413 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1414 | /* Check equality. */ |
| 1415 | if (elt->ref != ref) |
| 1416 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1417 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1418 | /* Delete and free entry. */ |
| 1419 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1420 | free(elt->data); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1421 | free(elt); |
| 1422 | } |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1423 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1424 | /* Browse each node of the list for IPv4 addresses. */ |
| 1425 | pat_del_list_val(expr, ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1426 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1427 | /* browse each node of the tree for IPv6 addresses. */ |
| 1428 | for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL; |
| 1429 | node; |
| 1430 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1431 | /* Extract container of the tree node. */ |
| 1432 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1433 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1434 | /* Check equality. */ |
| 1435 | if (elt->ref != ref) |
| 1436 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1437 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1438 | /* Delete and free entry. */ |
| 1439 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1440 | free(elt->data); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1441 | free(elt); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1442 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1443 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1446 | void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1447 | { |
| 1448 | struct pattern_list *pat; |
| 1449 | struct pattern_list *safe; |
| 1450 | |
| 1451 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1452 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1453 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1454 | continue; |
| 1455 | |
| 1456 | /* Delete and free entry. */ |
| 1457 | LIST_DEL(&pat->list); |
| 1458 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1459 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1460 | free(pat); |
| 1461 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1462 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1465 | void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1466 | { |
| 1467 | struct ebmb_node *node, *next_node; |
| 1468 | struct pattern_tree *elt; |
| 1469 | |
Thierry FOURNIER | 73bc285 | 2015-02-06 17:53:54 +0100 | [diff] [blame] | 1470 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1471 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
| 1472 | return pat_del_list_ptr(expr, ref); |
| 1473 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1474 | /* browse each node of the tree. */ |
| 1475 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1476 | node; |
| 1477 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1478 | /* Extract container of the tree node. */ |
| 1479 | elt = container_of(node, struct pattern_tree, node); |
| 1480 | |
| 1481 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1482 | if (elt->ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1483 | continue; |
| 1484 | |
| 1485 | /* Delete and free entry. */ |
| 1486 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1487 | free(elt->data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1488 | free(elt); |
| 1489 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1490 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1491 | } |
| 1492 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1493 | void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1494 | { |
| 1495 | struct pattern_list *pat; |
| 1496 | struct pattern_list *safe; |
| 1497 | |
| 1498 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1499 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1500 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1501 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1502 | |
| 1503 | /* Delete and free entry. */ |
| 1504 | LIST_DEL(&pat->list); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1505 | regex_free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1506 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1507 | free(pat); |
| 1508 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1509 | expr->revision = rdtsc(); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | void pattern_init_expr(struct pattern_expr *expr) |
| 1513 | { |
| 1514 | LIST_INIT(&expr->patterns); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1515 | expr->revision = 0; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1516 | expr->pattern_tree = EB_ROOT; |
| 1517 | expr->pattern_tree_2 = EB_ROOT; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | void pattern_init_head(struct pattern_head *head) |
| 1521 | { |
| 1522 | LIST_INIT(&head->head); |
| 1523 | } |
| 1524 | |
| 1525 | /* The following functions are relative to the management of the reference |
| 1526 | * lists. These lists are used to store the original pattern and associated |
| 1527 | * value as string form. |
| 1528 | * |
| 1529 | * This is used with modifiable ACL and MAPS |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1530 | * |
| 1531 | * The pattern reference are stored with two identifiers: the unique_id and |
| 1532 | * the reference. |
| 1533 | * |
| 1534 | * The reference identify a file. Each file with the same name point to the |
| 1535 | * same reference. We can register many times one file. If the file is modified, |
| 1536 | * all his dependencies are also modified. The reference can be used with map or |
| 1537 | * acl. |
| 1538 | * |
| 1539 | * The unique_id identify inline acl. The unique id is unique for each acl. |
| 1540 | * You cannot force the same id in the configuration file, because this repoort |
| 1541 | * an error. |
| 1542 | * |
| 1543 | * A particular case appears if the filename is a number. In this case, the |
| 1544 | * unique_id is set with the number represented by the filename and the |
| 1545 | * reference is also set. This method prevent double unique_id. |
| 1546 | * |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1547 | */ |
| 1548 | |
| 1549 | /* This function lookup for reference. If the reference is found, they return |
| 1550 | * pointer to the struct pat_ref, else return NULL. |
| 1551 | */ |
| 1552 | struct pat_ref *pat_ref_lookup(const char *reference) |
| 1553 | { |
| 1554 | struct pat_ref *ref; |
| 1555 | |
| 1556 | list_for_each_entry(ref, &pattern_reference, list) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1557 | if (ref->reference && strcmp(reference, ref->reference) == 0) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1558 | return ref; |
| 1559 | return NULL; |
| 1560 | } |
| 1561 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1562 | /* This function lookup for unique id. If the reference is found, they return |
| 1563 | * pointer to the struct pat_ref, else return NULL. |
| 1564 | */ |
| 1565 | struct pat_ref *pat_ref_lookupid(int unique_id) |
| 1566 | { |
| 1567 | struct pat_ref *ref; |
| 1568 | |
| 1569 | list_for_each_entry(ref, &pattern_reference, list) |
| 1570 | if (ref->unique_id == unique_id) |
| 1571 | return ref; |
| 1572 | return NULL; |
| 1573 | } |
| 1574 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1575 | /* This function remove all pattern matching the pointer <refelt> from |
| 1576 | * the the reference and from each expr member of the reference. This |
| 1577 | * function returns 1 if the deletion is done and return 0 is the entry |
| 1578 | * is not found. |
| 1579 | */ |
| 1580 | int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt) |
| 1581 | { |
| 1582 | struct pattern_expr *expr; |
| 1583 | struct pat_ref_elt *elt, *safe; |
| 1584 | |
| 1585 | /* delete pattern from reference */ |
| 1586 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1587 | if (elt == refelt) { |
peter cai | aede6dd | 2015-10-07 00:07:43 -0700 | [diff] [blame] | 1588 | list_for_each_entry(expr, &ref->pat, list) |
| 1589 | pattern_delete(expr, elt); |
| 1590 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1591 | LIST_DEL(&elt->list); |
| 1592 | free(elt->sample); |
| 1593 | free(elt->pattern); |
| 1594 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1595 | return 1; |
| 1596 | } |
| 1597 | } |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1601 | /* This function remove all pattern match <key> from the the reference |
| 1602 | * and from each expr member of the reference. This fucntion returns 1 |
| 1603 | * if the deletion is done and return 0 is the entry is not found. |
| 1604 | */ |
| 1605 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1606 | { |
| 1607 | struct pattern_expr *expr; |
| 1608 | struct pat_ref_elt *elt, *safe; |
| 1609 | int found = 0; |
| 1610 | |
| 1611 | /* delete pattern from reference */ |
| 1612 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1613 | if (strcmp(key, elt->pattern) == 0) { |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1614 | list_for_each_entry(expr, &ref->pat, list) |
| 1615 | pattern_delete(expr, elt); |
| 1616 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1617 | LIST_DEL(&elt->list); |
| 1618 | free(elt->sample); |
| 1619 | free(elt->pattern); |
| 1620 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1621 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1622 | found = 1; |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | if (!found) |
| 1627 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
| 1630 | |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 1631 | /* |
| 1632 | * find and return an element <elt> matching <key> in a reference <ref> |
| 1633 | * return NULL if not found |
| 1634 | */ |
| 1635 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key) |
| 1636 | { |
| 1637 | struct pat_ref_elt *elt; |
| 1638 | |
| 1639 | list_for_each_entry(elt, &ref->head, list) { |
| 1640 | if (strcmp(key, elt->pattern) == 0) |
| 1641 | return elt; |
| 1642 | } |
| 1643 | |
| 1644 | return NULL; |
| 1645 | } |
| 1646 | |
| 1647 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1648 | /* This function modify the sample of the first pattern that match the <key>. */ |
| 1649 | static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt, |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1650 | const char *value, char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1651 | { |
| 1652 | struct pattern_expr *expr; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1653 | struct sample_data **data; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1654 | char *sample; |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 1655 | struct sample_data test; |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1656 | |
| 1657 | /* Try all needed converters. */ |
| 1658 | list_for_each_entry(expr, &ref->pat, list) { |
| 1659 | if (!expr->pat_head->parse_smp) |
| 1660 | continue; |
| 1661 | |
| 1662 | if (!expr->pat_head->parse_smp(value, &test)) { |
| 1663 | memprintf(err, "unable to parse '%s'", value); |
| 1664 | return 0; |
| 1665 | } |
| 1666 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1667 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1668 | /* Modify pattern from reference. */ |
| 1669 | sample = strdup(value); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1670 | if (!sample) { |
| 1671 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1672 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1673 | } |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1674 | free(elt->sample); |
| 1675 | elt->sample = sample; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1676 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1677 | /* Load sample in each reference. All the conversion are tested |
| 1678 | * below, normally these calls dosn't fail. |
| 1679 | */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1680 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1681 | if (!expr->pat_head->parse_smp) |
| 1682 | continue; |
| 1683 | |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1684 | data = pattern_find_smp(expr, elt); |
| 1685 | if (data && *data && !expr->pat_head->parse_smp(sample, *data)) |
| 1686 | *data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1689 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1692 | /* This function modify the sample of the first pattern that match the <key>. */ |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1693 | int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err) |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1694 | { |
| 1695 | struct pat_ref_elt *elt; |
| 1696 | |
| 1697 | /* Look for pattern in the reference. */ |
| 1698 | list_for_each_entry(elt, &ref->head, list) { |
| 1699 | if (elt == refelt) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1700 | if (!pat_ref_set_elt(ref, elt, value, err)) |
| 1701 | return 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1702 | return 1; |
| 1703 | } |
| 1704 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1705 | |
| 1706 | memprintf(err, "key or pattern not found"); |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /* This function modify the sample of the first pattern that match the <key>. */ |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1711 | int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err) |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1712 | { |
| 1713 | struct pat_ref_elt *elt; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1714 | int found = 0; |
| 1715 | char *_merr; |
| 1716 | char **merr; |
| 1717 | |
| 1718 | if (err) { |
| 1719 | merr = &_merr; |
| 1720 | *merr = NULL; |
| 1721 | } |
| 1722 | else |
| 1723 | merr = NULL; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1724 | |
| 1725 | /* Look for pattern in the reference. */ |
| 1726 | list_for_each_entry(elt, &ref->head, list) { |
| 1727 | if (strcmp(key, elt->pattern) == 0) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1728 | if (!pat_ref_set_elt(ref, elt, value, merr)) { |
| 1729 | if (!found) |
| 1730 | *err = *merr; |
| 1731 | else { |
| 1732 | memprintf(err, "%s, %s", *err, *merr); |
| 1733 | free(*merr); |
| 1734 | *merr = NULL; |
| 1735 | } |
| 1736 | } |
| 1737 | found = 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1738 | } |
| 1739 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1740 | |
| 1741 | if (!found) { |
| 1742 | memprintf(err, "entry not found"); |
| 1743 | return 0; |
| 1744 | } |
| 1745 | return 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1748 | /* This function create new reference. <ref> is the reference name. |
| 1749 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1750 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1751 | * before calling this function. If the fucntion fail, it return NULL, |
| 1752 | * else return new struct pat_ref. |
| 1753 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1754 | struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1755 | { |
| 1756 | struct pat_ref *ref; |
| 1757 | |
| 1758 | ref = malloc(sizeof(*ref)); |
| 1759 | if (!ref) |
| 1760 | return NULL; |
| 1761 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1762 | if (display) { |
| 1763 | ref->display = strdup(display); |
| 1764 | if (!ref->display) { |
| 1765 | free(ref); |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | } |
| 1769 | else |
| 1770 | ref->display = NULL; |
| 1771 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1772 | ref->reference = strdup(reference); |
| 1773 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1774 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1775 | free(ref); |
| 1776 | return NULL; |
| 1777 | } |
| 1778 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1779 | ref->flags = flags; |
| 1780 | ref->unique_id = -1; |
| 1781 | |
| 1782 | LIST_INIT(&ref->head); |
| 1783 | LIST_INIT(&ref->pat); |
| 1784 | |
| 1785 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1786 | |
| 1787 | return ref; |
| 1788 | } |
| 1789 | |
| 1790 | /* This function create new reference. <unique_id> is the unique id. If |
| 1791 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1792 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1793 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1794 | * or pat_ref_lookupid before calling this function. If the function |
| 1795 | * fail, it return NULL, else return new struct pat_ref. |
| 1796 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1797 | struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1798 | { |
| 1799 | struct pat_ref *ref; |
| 1800 | |
| 1801 | ref = malloc(sizeof(*ref)); |
| 1802 | if (!ref) |
| 1803 | return NULL; |
| 1804 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1805 | if (display) { |
| 1806 | ref->display = strdup(display); |
| 1807 | if (!ref->display) { |
| 1808 | free(ref); |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | } |
| 1812 | else |
| 1813 | ref->display = NULL; |
| 1814 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1815 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1816 | ref->flags = flags; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1817 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1818 | LIST_INIT(&ref->head); |
| 1819 | LIST_INIT(&ref->pat); |
| 1820 | |
| 1821 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1822 | |
| 1823 | return ref; |
| 1824 | } |
| 1825 | |
| 1826 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1827 | * If the function fails, it returns 0. |
| 1828 | */ |
| 1829 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line) |
| 1830 | { |
| 1831 | struct pat_ref_elt *elt; |
| 1832 | |
| 1833 | elt = malloc(sizeof(*elt)); |
| 1834 | if (!elt) |
| 1835 | return 0; |
| 1836 | |
| 1837 | elt->line = line; |
| 1838 | |
| 1839 | elt->pattern = strdup(pattern); |
| 1840 | if (!elt->pattern) { |
| 1841 | free(elt); |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | if (sample) { |
| 1846 | elt->sample = strdup(sample); |
| 1847 | if (!elt->sample) { |
| 1848 | free(elt->pattern); |
| 1849 | free(elt); |
| 1850 | return 0; |
| 1851 | } |
| 1852 | } |
| 1853 | else |
| 1854 | elt->sample = NULL; |
| 1855 | |
| 1856 | LIST_ADDQ(&ref->head, &elt->list); |
| 1857 | |
| 1858 | return 1; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1861 | /* This function create sample found in <elt>, parse the pattern also |
| 1862 | * found in <elt> and insert it in <expr>. The function copy <patflags> |
| 1863 | * in <expr>. If the function fails, it returns0 and <err> is filled. |
| 1864 | * In succes case, the function returns 1. |
| 1865 | */ |
| 1866 | static inline |
| 1867 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1868 | int patflags, char **err) |
| 1869 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1870 | struct sample_data *data; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1871 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* Create sample */ |
| 1874 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1875 | /* New sample. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1876 | data = malloc(sizeof(*data)); |
| 1877 | if (!data) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1878 | return 0; |
| 1879 | |
| 1880 | /* Parse value. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1881 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1882 | memprintf(err, "unable to parse '%s'", elt->sample); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1883 | free(data); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1884 | return 0; |
| 1885 | } |
| 1886 | |
| 1887 | } |
| 1888 | else |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1889 | data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1890 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1891 | /* initialise pattern */ |
| 1892 | memset(&pattern, 0, sizeof(pattern)); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1893 | pattern.data = data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1894 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1895 | |
| 1896 | /* parse pattern */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1897 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1898 | free(data); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
| 1901 | |
| 1902 | /* index pattern */ |
| 1903 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1904 | free(data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1905 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1906 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1907 | |
| 1908 | return 1; |
| 1909 | } |
| 1910 | |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1911 | /* This function adds entry to <ref>. It can failed with memory error. The new |
| 1912 | * entry is added at all the pattern_expr registered in this reference. The |
| 1913 | * function stop on the first error encountered. It returns 0 and err is |
| 1914 | * filled. If an error is encountered, the complete add operation is cancelled. |
| 1915 | * If the insertion is a success the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1916 | */ |
| 1917 | int pat_ref_add(struct pat_ref *ref, |
| 1918 | const char *pattern, const char *sample, |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1919 | char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1920 | { |
| 1921 | struct pat_ref_elt *elt; |
| 1922 | struct pattern_expr *expr; |
| 1923 | |
| 1924 | elt = malloc(sizeof(*elt)); |
| 1925 | if (!elt) { |
| 1926 | memprintf(err, "out of memory error"); |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | elt->line = -1; |
| 1931 | |
| 1932 | elt->pattern = strdup(pattern); |
| 1933 | if (!elt->pattern) { |
| 1934 | free(elt); |
| 1935 | memprintf(err, "out of memory error"); |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | if (sample) { |
| 1940 | elt->sample = strdup(sample); |
| 1941 | if (!elt->sample) { |
| 1942 | free(elt->pattern); |
| 1943 | free(elt); |
| 1944 | memprintf(err, "out of memory error"); |
| 1945 | return 0; |
| 1946 | } |
| 1947 | } |
| 1948 | else |
| 1949 | elt->sample = NULL; |
| 1950 | |
| 1951 | LIST_ADDQ(&ref->head, &elt->list); |
| 1952 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1953 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1954 | if (!pat_ref_push(elt, expr, 0, err)) { |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1955 | /* If the insertion fails, try to delete all the added entries. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1956 | pat_ref_delete_by_id(ref, elt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1957 | return 0; |
| 1958 | } |
| 1959 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1960 | return 1; |
| 1961 | } |
| 1962 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 1963 | /* This function prune <ref>, replace all reference by the references |
| 1964 | * of <replace>, and reindex all the news values. |
| 1965 | * |
| 1966 | * The pattern are loaded in best effort and the errors are ignored, |
| 1967 | * but writed in the logs. |
| 1968 | */ |
| 1969 | void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace) |
| 1970 | { |
| 1971 | struct pattern_expr *expr; |
| 1972 | struct pat_ref_elt *elt; |
| 1973 | char *err = NULL; |
| 1974 | |
| 1975 | pat_ref_prune(ref); |
| 1976 | |
| 1977 | LIST_ADD(&replace->head, &ref->head); |
| 1978 | LIST_DEL(&replace->head); |
| 1979 | |
| 1980 | list_for_each_entry(elt, &ref->head, list) { |
| 1981 | list_for_each_entry(expr, &ref->pat, list) { |
| 1982 | if (!pat_ref_push(elt, expr, 0, &err)) { |
| 1983 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 1984 | free(err); |
| 1985 | err = NULL; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1991 | /* This function prune all entries of <ref>. This function |
| 1992 | * prune the associated pattern_expr. |
| 1993 | */ |
| 1994 | void pat_ref_prune(struct pat_ref *ref) |
| 1995 | { |
| 1996 | struct pat_ref_elt *elt, *safe; |
| 1997 | struct pattern_expr *expr; |
| 1998 | |
| 1999 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 2000 | LIST_DEL(&elt->list); |
| 2001 | free(elt->pattern); |
| 2002 | free(elt->sample); |
| 2003 | free(elt); |
| 2004 | } |
| 2005 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2006 | list_for_each_entry(expr, &ref->pat, list) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2007 | expr->pat_head->prune(expr); |
| 2008 | } |
| 2009 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2010 | /* This function lookup for existing reference <ref> in pattern_head <head>. */ |
| 2011 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 2012 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2013 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2014 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2015 | list_for_each_entry(expr, &head->head, list) |
| 2016 | if (expr->expr->ref == ref) |
| 2017 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2018 | return NULL; |
| 2019 | } |
| 2020 | |
| 2021 | /* This function create new pattern_expr associated to the reference <ref>. |
| 2022 | * <ref> can be NULL. If an error is occured, the function returns NULL and |
| 2023 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 2024 | * with <head> and <ref>. |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2025 | * |
| 2026 | * The returned value can be a alredy filled pattern list, in this case the |
| 2027 | * flag <reuse> is set. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2028 | */ |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2029 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, |
| 2030 | char **err, int *reuse) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2031 | { |
| 2032 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2033 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2034 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2035 | if (reuse) |
| 2036 | *reuse = 0; |
| 2037 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2038 | /* Memory and initialization of the chain element. */ |
| 2039 | list = malloc(sizeof(*list)); |
| 2040 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2041 | memprintf(err, "out of memory"); |
| 2042 | return NULL; |
| 2043 | } |
| 2044 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2045 | /* Look for existing similar expr. No that only the index, parse and |
| 2046 | * parse_smp function must be identical for having similar pattern. |
| 2047 | * The other function depends of theses first. |
| 2048 | */ |
| 2049 | if (ref) { |
| 2050 | list_for_each_entry(expr, &ref->pat, list) |
| 2051 | if (expr->pat_head->index == head->index && |
| 2052 | expr->pat_head->parse == head->parse && |
| 2053 | expr->pat_head->parse_smp == head->parse_smp) |
| 2054 | break; |
| 2055 | if (&expr->list == &ref->pat) |
| 2056 | expr = NULL; |
| 2057 | } |
| 2058 | else |
| 2059 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2060 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2061 | /* If no similar expr was found, we create new expr. */ |
| 2062 | if (!expr) { |
| 2063 | /* Get a lot of memory for the expr struct. */ |
| 2064 | expr = malloc(sizeof(*expr)); |
| 2065 | if (!expr) { |
Andreas Seltenreich | e6e22e8 | 2016-03-03 20:20:23 +0100 | [diff] [blame] | 2066 | free(list); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2067 | memprintf(err, "out of memory"); |
| 2068 | return NULL; |
| 2069 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2070 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2071 | /* Initialize this new expr. */ |
| 2072 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2073 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2074 | /* This new pattern expression reference one of his heads. */ |
| 2075 | expr->pat_head = head; |
| 2076 | |
| 2077 | /* Link with ref, or to self to facilitate LIST_DEL() */ |
| 2078 | if (ref) |
| 2079 | LIST_ADDQ(&ref->pat, &expr->list); |
| 2080 | else |
| 2081 | LIST_INIT(&expr->list); |
| 2082 | |
| 2083 | expr->ref = ref; |
| 2084 | |
| 2085 | /* We must free this pattern if it is no more used. */ |
| 2086 | list->do_free = 1; |
| 2087 | } |
| 2088 | else { |
| 2089 | /* If the pattern used already exists, it is already linked |
| 2090 | * with ref and we must not free it. |
| 2091 | */ |
| 2092 | list->do_free = 0; |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2093 | if (reuse) |
| 2094 | *reuse = 1; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /* The new list element reference the pattern_expr. */ |
| 2098 | list->expr = expr; |
| 2099 | |
| 2100 | /* Link the list element with the pattern_head. */ |
| 2101 | LIST_ADDQ(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2102 | return expr; |
| 2103 | } |
| 2104 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2105 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2106 | * be returned there on errors and the caller will have to free it. |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2107 | * |
| 2108 | * The file contains one key + value per line. Lines which start with '#' are |
| 2109 | * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is |
| 2110 | * then the first "word" (series of non-space/tabs characters), and the value is |
| 2111 | * what follows this series of space/tab till the end of the line excluding |
| 2112 | * trailing spaces/tabs. |
| 2113 | * |
| 2114 | * Example : |
| 2115 | * |
| 2116 | * # this is a comment and is ignored |
| 2117 | * 62.212.114.60 1wt.eu \n |
| 2118 | * <-><-----------><---><----><----> |
| 2119 | * | | | | `--- trailing spaces ignored |
| 2120 | * | | | `-------- value |
| 2121 | * | | `--------------- middle spaces ignored |
| 2122 | * | `------------------------ key |
| 2123 | * `-------------------------------- leading spaces ignored |
| 2124 | * |
| 2125 | * Return non-zero in case of succes, otherwise 0. |
| 2126 | */ |
| 2127 | int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err) |
| 2128 | { |
| 2129 | FILE *file; |
| 2130 | char *c; |
| 2131 | int ret = 0; |
| 2132 | int line = 0; |
| 2133 | char *key_beg; |
| 2134 | char *key_end; |
| 2135 | char *value_beg; |
| 2136 | char *value_end; |
| 2137 | |
| 2138 | file = fopen(filename, "r"); |
| 2139 | if (!file) { |
| 2140 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | /* now parse all patterns. The file may contain only one pattern |
| 2145 | * followed by one value per line. The start spaces, separator spaces |
| 2146 | * and and spaces are stripped. Each can contain comment started by '#' |
| 2147 | */ |
| 2148 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2149 | line++; |
| 2150 | c = trash.str; |
| 2151 | |
| 2152 | /* ignore lines beginning with a dash */ |
| 2153 | if (*c == '#') |
| 2154 | continue; |
| 2155 | |
| 2156 | /* strip leading spaces and tabs */ |
| 2157 | while (*c == ' ' || *c == '\t') |
| 2158 | c++; |
| 2159 | |
| 2160 | /* empty lines are ignored too */ |
| 2161 | if (*c == '\0' || *c == '\r' || *c == '\n') |
| 2162 | continue; |
| 2163 | |
| 2164 | /* look for the end of the key */ |
| 2165 | key_beg = c; |
| 2166 | while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2167 | c++; |
| 2168 | |
| 2169 | key_end = c; |
| 2170 | |
| 2171 | /* strip middle spaces and tabs */ |
| 2172 | while (*c == ' ' || *c == '\t') |
| 2173 | c++; |
| 2174 | |
| 2175 | /* look for the end of the value, it is the end of the line */ |
| 2176 | value_beg = c; |
| 2177 | while (*c && *c != '\n' && *c != '\r') |
| 2178 | c++; |
| 2179 | value_end = c; |
| 2180 | |
| 2181 | /* trim possibly trailing spaces and tabs */ |
| 2182 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2183 | value_end--; |
| 2184 | |
| 2185 | /* set final \0 and check entries */ |
| 2186 | *key_end = '\0'; |
| 2187 | *value_end = '\0'; |
| 2188 | |
| 2189 | /* insert values */ |
| 2190 | if (!pat_ref_append(ref, key_beg, value_beg, line)) { |
| 2191 | memprintf(err, "out of memory"); |
| 2192 | goto out_close; |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | /* succes */ |
| 2197 | ret = 1; |
| 2198 | |
| 2199 | out_close: |
| 2200 | fclose(file); |
| 2201 | return ret; |
| 2202 | } |
| 2203 | |
| 2204 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2205 | * be returned there on errors and the caller will have to free it. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2206 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2207 | int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2208 | { |
| 2209 | FILE *file; |
| 2210 | char *c; |
| 2211 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2212 | int ret = 0; |
| 2213 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2214 | |
| 2215 | file = fopen(filename, "r"); |
| 2216 | if (!file) { |
| 2217 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | /* now parse all patterns. The file may contain only one pattern per |
| 2222 | * line. If the line contains spaces, they will be part of the pattern. |
| 2223 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2224 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2225 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2226 | line++; |
| 2227 | c = trash.str; |
| 2228 | |
| 2229 | /* ignore lines beginning with a dash */ |
| 2230 | if (*c == '#') |
| 2231 | continue; |
| 2232 | |
| 2233 | /* strip leading spaces and tabs */ |
| 2234 | while (*c == ' ' || *c == '\t') |
| 2235 | c++; |
| 2236 | |
| 2237 | |
| 2238 | arg = c; |
| 2239 | while (*c && *c != '\n' && *c != '\r') |
| 2240 | c++; |
| 2241 | *c = 0; |
| 2242 | |
| 2243 | /* empty lines are ignored too */ |
| 2244 | if (c == arg) |
| 2245 | continue; |
| 2246 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2247 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2248 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2249 | goto out_close; |
| 2250 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | ret = 1; /* success */ |
| 2254 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2255 | out_close: |
| 2256 | fclose(file); |
| 2257 | return ret; |
| 2258 | } |
| 2259 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2260 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2261 | const char *filename, int patflags, int load_smp, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2262 | char **err, const char *file, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2263 | { |
| 2264 | struct pat_ref *ref; |
| 2265 | struct pattern_expr *expr; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2266 | struct pat_ref_elt *elt; |
Willy Tarreau | 4deaf39 | 2014-11-26 13:17:03 +0100 | [diff] [blame] | 2267 | int reuse = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2268 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2269 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2270 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2271 | |
| 2272 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2273 | if (!ref) { |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2274 | chunk_printf(&trash, |
| 2275 | "pattern loaded from file '%s' used by %s at file '%s' line %d", |
| 2276 | filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2277 | |
| 2278 | ref = pat_ref_new(filename, trash.str, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2279 | if (!ref) { |
| 2280 | memprintf(err, "out of memory"); |
| 2281 | return 0; |
| 2282 | } |
| 2283 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2284 | if (load_smp) { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2285 | ref->flags |= PAT_REF_SMP; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2286 | if (!pat_ref_read_from_file_smp(ref, filename, err)) |
| 2287 | return 0; |
| 2288 | } |
| 2289 | else { |
| 2290 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2291 | return 0; |
| 2292 | } |
| 2293 | } |
| 2294 | else { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2295 | /* The reference already exists, check the map compatibility. */ |
| 2296 | |
| 2297 | /* If the load require samples and the flag PAT_REF_SMP is not set, |
| 2298 | * the reference doesn't contain sample, and cannot be used. |
| 2299 | */ |
| 2300 | if (load_smp) { |
| 2301 | if (!(ref->flags & PAT_REF_SMP)) { |
| 2302 | memprintf(err, "The file \"%s\" is already used as one column file " |
| 2303 | "and cannot be used by as two column file.", |
| 2304 | filename); |
| 2305 | return 0; |
| 2306 | } |
| 2307 | } |
| 2308 | else { |
| 2309 | /* The load doesn't require samples. If the flag PAT_REF_SMP is |
| 2310 | * set, the reference contains a sample, and cannot be used. |
| 2311 | */ |
| 2312 | if (ref->flags & PAT_REF_SMP) { |
| 2313 | memprintf(err, "The file \"%s\" is already used as two column file " |
| 2314 | "and cannot be used by as one column file.", |
| 2315 | filename); |
| 2316 | return 0; |
| 2317 | } |
| 2318 | } |
| 2319 | |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2320 | /* Extends display */ |
| 2321 | chunk_printf(&trash, "%s", ref->display); |
| 2322 | chunk_appendf(&trash, ", by %s at file '%s' line %d", |
| 2323 | refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2324 | free(ref->display); |
| 2325 | ref->display = strdup(trash.str); |
| 2326 | if (!ref->display) { |
| 2327 | memprintf(err, "out of memory"); |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2331 | /* Merge flags. */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2332 | ref->flags |= refflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* Now, we can loading patterns from the reference. */ |
| 2336 | |
| 2337 | /* Lookup for existing reference in the head. If the reference |
| 2338 | * doesn't exists, create it. |
| 2339 | */ |
| 2340 | expr = pattern_lookup_expr(head, ref); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2341 | if (!expr || (expr->mflags != patflags)) { |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2342 | expr = pattern_new_expr(head, ref, err, &reuse); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2343 | if (!expr) |
| 2344 | return 0; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2345 | expr->mflags = patflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2348 | /* The returned expression may be not empty, because the function |
| 2349 | * "pattern_new_expr" lookup for similar pattern list and can |
| 2350 | * reuse a already filled pattern list. In this case, we can not |
| 2351 | * reload the patterns. |
| 2352 | */ |
| 2353 | if (reuse) |
| 2354 | return 1; |
| 2355 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2356 | /* Load reference content in the pattern expression. */ |
| 2357 | list_for_each_entry(elt, &ref->head, list) { |
| 2358 | if (!pat_ref_push(elt, expr, patflags, err)) { |
| 2359 | if (elt->line > 0) |
| 2360 | memprintf(err, "%s at line %d of file '%s'", |
| 2361 | *err, elt->line, filename); |
| 2362 | return 0; |
| 2363 | } |
| 2364 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2365 | |
| 2366 | return 1; |
| 2367 | } |
| 2368 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2369 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 2370 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 2371 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2372 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2373 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2374 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2375 | struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2376 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2377 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2378 | struct pattern *pat; |
| 2379 | |
| 2380 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2381 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2382 | static_pattern.data = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2383 | static_pattern.ref = NULL; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2384 | static_pattern.sflags = 0; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2385 | static_pattern.type = SMP_T_SINT; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2386 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2387 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2388 | return &static_pattern; |
| 2389 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2390 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2391 | /* convert input to string */ |
| 2392 | if (!sample_convert(smp, head->expect_type)) |
| 2393 | return NULL; |
| 2394 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2395 | list_for_each_entry(list, &head->head, list) { |
| 2396 | pat = head->match(smp, list->expr, fill); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2397 | if (pat) |
| 2398 | return pat; |
| 2399 | } |
| 2400 | return NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2401 | } |
| 2402 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2403 | /* This function prune the pattern expression. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2404 | void pattern_prune(struct pattern_head *head) |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2405 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2406 | struct pattern_expr_list *list, *safe; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2407 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2408 | list_for_each_entry_safe(list, safe, &head->head, list) { |
| 2409 | LIST_DEL(&list->list); |
| 2410 | if (list->do_free) { |
| 2411 | LIST_DEL(&list->expr->list); |
| 2412 | head->prune(list->expr); |
| 2413 | free(list->expr); |
| 2414 | } |
| 2415 | free(list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2416 | } |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2419 | /* This function lookup for a pattern matching the <key> and return a |
| 2420 | * pointer to a pointer of the sample stoarge. If the <key> dont match, |
| 2421 | * the function returns NULL. If the key cannot be parsed, the function |
| 2422 | * fill <err>. |
| 2423 | */ |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 2424 | struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2425 | { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2426 | struct ebmb_node *node; |
| 2427 | struct pattern_tree *elt; |
| 2428 | struct pattern_list *pat; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2429 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2430 | for (node = ebmb_first(&expr->pattern_tree); |
| 2431 | node; |
| 2432 | node = ebmb_next(node)) { |
| 2433 | elt = container_of(node, struct pattern_tree, node); |
| 2434 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2435 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | for (node = ebmb_first(&expr->pattern_tree_2); |
| 2439 | node; |
| 2440 | node = ebmb_next(node)) { |
| 2441 | elt = container_of(node, struct pattern_tree, node); |
| 2442 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2443 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | list_for_each_entry(pat, &expr->patterns, list) |
| 2447 | if (pat->pat.ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2448 | return &pat->pat.data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2449 | |
| 2450 | return NULL; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2451 | } |
| 2452 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2453 | /* This function search all the pattern matching the <key> and delete it. |
| 2454 | * If the parsing of the input key fails, the function returns 0 and the |
| 2455 | * <err> is filled, else return 1; |
| 2456 | */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2457 | int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2458 | { |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2459 | expr->pat_head->delete(expr, ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2460 | return 1; |
| 2461 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2462 | |
| 2463 | /* This function finalize the configuration parsing. Its set all the |
| 2464 | * automatic ids |
| 2465 | */ |
| 2466 | void pattern_finalize_config(void) |
| 2467 | { |
| 2468 | int i = 0; |
| 2469 | struct pat_ref *ref, *ref2, *ref3; |
| 2470 | struct list pr = LIST_HEAD_INIT(pr); |
| 2471 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 2472 | pat_lru_seed = random(); |
| 2473 | if (global.tune.pattern_cache) |
| 2474 | pat_lru_tree = lru64_new(global.tune.pattern_cache); |
| 2475 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2476 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2477 | if (ref->unique_id == -1) { |
| 2478 | /* Look for the first free id. */ |
| 2479 | while (1) { |
| 2480 | list_for_each_entry(ref2, &pattern_reference, list) { |
| 2481 | if (ref2->unique_id == i) { |
| 2482 | i++; |
| 2483 | break; |
| 2484 | } |
| 2485 | } |
Willy Tarreau | 3b78696 | 2014-04-26 12:37:25 +0200 | [diff] [blame] | 2486 | if (&ref2->list == &pattern_reference) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2487 | break; |
| 2488 | } |
| 2489 | |
| 2490 | /* Uses the unique id and increment it for the next entry. */ |
| 2491 | ref->unique_id = i; |
| 2492 | i++; |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | /* This sort the reference list by id. */ |
| 2497 | list_for_each_entry_safe(ref, ref2, &pattern_reference, list) { |
| 2498 | LIST_DEL(&ref->list); |
| 2499 | list_for_each_entry(ref3, &pr, list) { |
| 2500 | if (ref->unique_id < ref3->unique_id) { |
| 2501 | LIST_ADDQ(&ref3->list, &ref->list); |
| 2502 | break; |
| 2503 | } |
| 2504 | } |
| 2505 | if (&ref3->list == &pr) |
| 2506 | LIST_ADDQ(&pr, &ref->list); |
| 2507 | } |
| 2508 | |
| 2509 | /* swap root */ |
| 2510 | LIST_ADD(&pr, &pattern_reference); |
| 2511 | LIST_DEL(&pr); |
| 2512 | } |