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; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1584 | struct bref *bref, *back; |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1585 | |
| 1586 | /* delete pattern from reference */ |
| 1587 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1588 | if (elt == refelt) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1589 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 1590 | /* |
| 1591 | * we have to unlink all watchers. We must not relink them if |
| 1592 | * this elt was the last one in the list. |
| 1593 | */ |
| 1594 | LIST_DEL(&bref->users); |
| 1595 | LIST_INIT(&bref->users); |
| 1596 | if (elt->list.n != &ref->head) |
| 1597 | LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users); |
| 1598 | bref->ref = elt->list.n; |
| 1599 | } |
peter cai | aede6dd | 2015-10-07 00:07:43 -0700 | [diff] [blame] | 1600 | list_for_each_entry(expr, &ref->pat, list) |
| 1601 | pattern_delete(expr, elt); |
| 1602 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1603 | LIST_DEL(&elt->list); |
| 1604 | free(elt->sample); |
| 1605 | free(elt->pattern); |
| 1606 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1607 | return 1; |
| 1608 | } |
| 1609 | } |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1613 | /* This function remove all pattern match <key> from the the reference |
| 1614 | * and from each expr member of the reference. This fucntion returns 1 |
| 1615 | * if the deletion is done and return 0 is the entry is not found. |
| 1616 | */ |
| 1617 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1618 | { |
| 1619 | struct pattern_expr *expr; |
| 1620 | struct pat_ref_elt *elt, *safe; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1621 | struct bref *bref, *back; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1622 | int found = 0; |
| 1623 | |
| 1624 | /* delete pattern from reference */ |
| 1625 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1626 | if (strcmp(key, elt->pattern) == 0) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1627 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 1628 | /* |
| 1629 | * we have to unlink all watchers. We must not relink them if |
| 1630 | * this elt was the last one in the list. |
| 1631 | */ |
| 1632 | LIST_DEL(&bref->users); |
| 1633 | LIST_INIT(&bref->users); |
| 1634 | if (elt->list.n != &ref->head) |
| 1635 | LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users); |
| 1636 | bref->ref = elt->list.n; |
| 1637 | } |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1638 | list_for_each_entry(expr, &ref->pat, list) |
| 1639 | pattern_delete(expr, elt); |
| 1640 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1641 | LIST_DEL(&elt->list); |
| 1642 | free(elt->sample); |
| 1643 | free(elt->pattern); |
| 1644 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1645 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1646 | found = 1; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | if (!found) |
| 1651 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1652 | return 1; |
| 1653 | } |
| 1654 | |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 1655 | /* |
| 1656 | * find and return an element <elt> matching <key> in a reference <ref> |
| 1657 | * return NULL if not found |
| 1658 | */ |
| 1659 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key) |
| 1660 | { |
| 1661 | struct pat_ref_elt *elt; |
| 1662 | |
| 1663 | list_for_each_entry(elt, &ref->head, list) { |
| 1664 | if (strcmp(key, elt->pattern) == 0) |
| 1665 | return elt; |
| 1666 | } |
| 1667 | |
| 1668 | return NULL; |
| 1669 | } |
| 1670 | |
| 1671 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1672 | /* This function modify the sample of the first pattern that match the <key>. */ |
| 1673 | 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] | 1674 | const char *value, char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1675 | { |
| 1676 | struct pattern_expr *expr; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1677 | struct sample_data **data; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1678 | char *sample; |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 1679 | struct sample_data test; |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1680 | |
| 1681 | /* Try all needed converters. */ |
| 1682 | list_for_each_entry(expr, &ref->pat, list) { |
| 1683 | if (!expr->pat_head->parse_smp) |
| 1684 | continue; |
| 1685 | |
| 1686 | if (!expr->pat_head->parse_smp(value, &test)) { |
| 1687 | memprintf(err, "unable to parse '%s'", value); |
| 1688 | return 0; |
| 1689 | } |
| 1690 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1692 | /* Modify pattern from reference. */ |
| 1693 | sample = strdup(value); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1694 | if (!sample) { |
| 1695 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1696 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1697 | } |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1698 | free(elt->sample); |
| 1699 | elt->sample = sample; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1700 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1701 | /* Load sample in each reference. All the conversion are tested |
| 1702 | * below, normally these calls dosn't fail. |
| 1703 | */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1704 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1705 | if (!expr->pat_head->parse_smp) |
| 1706 | continue; |
| 1707 | |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1708 | data = pattern_find_smp(expr, elt); |
| 1709 | if (data && *data && !expr->pat_head->parse_smp(sample, *data)) |
| 1710 | *data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1711 | } |
| 1712 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1713 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1716 | /* 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] | 1717 | 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] | 1718 | { |
| 1719 | struct pat_ref_elt *elt; |
| 1720 | |
| 1721 | /* Look for pattern in the reference. */ |
| 1722 | list_for_each_entry(elt, &ref->head, list) { |
| 1723 | if (elt == refelt) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1724 | if (!pat_ref_set_elt(ref, elt, value, err)) |
| 1725 | return 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1726 | return 1; |
| 1727 | } |
| 1728 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1729 | |
| 1730 | memprintf(err, "key or pattern not found"); |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | /* 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] | 1735 | 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] | 1736 | { |
| 1737 | struct pat_ref_elt *elt; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1738 | int found = 0; |
| 1739 | char *_merr; |
| 1740 | char **merr; |
| 1741 | |
| 1742 | if (err) { |
| 1743 | merr = &_merr; |
| 1744 | *merr = NULL; |
| 1745 | } |
| 1746 | else |
| 1747 | merr = NULL; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* Look for pattern in the reference. */ |
| 1750 | list_for_each_entry(elt, &ref->head, list) { |
| 1751 | if (strcmp(key, elt->pattern) == 0) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1752 | if (!pat_ref_set_elt(ref, elt, value, merr)) { |
| 1753 | if (!found) |
| 1754 | *err = *merr; |
| 1755 | else { |
| 1756 | memprintf(err, "%s, %s", *err, *merr); |
| 1757 | free(*merr); |
| 1758 | *merr = NULL; |
| 1759 | } |
| 1760 | } |
| 1761 | found = 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1762 | } |
| 1763 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1764 | |
| 1765 | if (!found) { |
| 1766 | memprintf(err, "entry not found"); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | return 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1772 | /* This function create new reference. <ref> is the reference name. |
| 1773 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1774 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1775 | * before calling this function. If the fucntion fail, it return NULL, |
| 1776 | * else return new struct pat_ref. |
| 1777 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1778 | 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] | 1779 | { |
| 1780 | struct pat_ref *ref; |
| 1781 | |
| 1782 | ref = malloc(sizeof(*ref)); |
| 1783 | if (!ref) |
| 1784 | return NULL; |
| 1785 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1786 | if (display) { |
| 1787 | ref->display = strdup(display); |
| 1788 | if (!ref->display) { |
| 1789 | free(ref); |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | } |
| 1793 | else |
| 1794 | ref->display = NULL; |
| 1795 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1796 | ref->reference = strdup(reference); |
| 1797 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1798 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1799 | free(ref); |
| 1800 | return NULL; |
| 1801 | } |
| 1802 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1803 | ref->flags = flags; |
| 1804 | ref->unique_id = -1; |
| 1805 | |
| 1806 | LIST_INIT(&ref->head); |
| 1807 | LIST_INIT(&ref->pat); |
| 1808 | |
| 1809 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1810 | |
| 1811 | return ref; |
| 1812 | } |
| 1813 | |
| 1814 | /* This function create new reference. <unique_id> is the unique id. If |
| 1815 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1816 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1817 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1818 | * or pat_ref_lookupid before calling this function. If the function |
| 1819 | * fail, it return NULL, else return new struct pat_ref. |
| 1820 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1821 | 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] | 1822 | { |
| 1823 | struct pat_ref *ref; |
| 1824 | |
| 1825 | ref = malloc(sizeof(*ref)); |
| 1826 | if (!ref) |
| 1827 | return NULL; |
| 1828 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1829 | if (display) { |
| 1830 | ref->display = strdup(display); |
| 1831 | if (!ref->display) { |
| 1832 | free(ref); |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | } |
| 1836 | else |
| 1837 | ref->display = NULL; |
| 1838 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1839 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1840 | ref->flags = flags; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1841 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1842 | LIST_INIT(&ref->head); |
| 1843 | LIST_INIT(&ref->pat); |
| 1844 | |
| 1845 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1846 | |
| 1847 | return ref; |
| 1848 | } |
| 1849 | |
| 1850 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1851 | * If the function fails, it returns 0. |
| 1852 | */ |
| 1853 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line) |
| 1854 | { |
| 1855 | struct pat_ref_elt *elt; |
| 1856 | |
| 1857 | elt = malloc(sizeof(*elt)); |
| 1858 | if (!elt) |
| 1859 | return 0; |
| 1860 | |
| 1861 | elt->line = line; |
| 1862 | |
| 1863 | elt->pattern = strdup(pattern); |
| 1864 | if (!elt->pattern) { |
| 1865 | free(elt); |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | if (sample) { |
| 1870 | elt->sample = strdup(sample); |
| 1871 | if (!elt->sample) { |
| 1872 | free(elt->pattern); |
| 1873 | free(elt); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | } |
| 1877 | else |
| 1878 | elt->sample = NULL; |
| 1879 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1880 | LIST_INIT(&elt->back_refs); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1881 | LIST_ADDQ(&ref->head, &elt->list); |
| 1882 | |
| 1883 | return 1; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1884 | } |
| 1885 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1886 | /* This function create sample found in <elt>, parse the pattern also |
| 1887 | * found in <elt> and insert it in <expr>. The function copy <patflags> |
| 1888 | * in <expr>. If the function fails, it returns0 and <err> is filled. |
| 1889 | * In succes case, the function returns 1. |
| 1890 | */ |
| 1891 | static inline |
| 1892 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1893 | int patflags, char **err) |
| 1894 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1895 | struct sample_data *data; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1896 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1897 | |
| 1898 | /* Create sample */ |
| 1899 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1900 | /* New sample. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1901 | data = malloc(sizeof(*data)); |
| 1902 | if (!data) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1903 | return 0; |
| 1904 | |
| 1905 | /* Parse value. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1906 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1907 | memprintf(err, "unable to parse '%s'", elt->sample); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1908 | free(data); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | } |
| 1913 | else |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1914 | data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1915 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1916 | /* initialise pattern */ |
| 1917 | memset(&pattern, 0, sizeof(pattern)); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1918 | pattern.data = data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1919 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1920 | |
| 1921 | /* parse pattern */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1922 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1923 | free(data); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1924 | return 0; |
| 1925 | } |
| 1926 | |
| 1927 | /* index pattern */ |
| 1928 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1929 | free(data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1930 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1931 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1932 | |
| 1933 | return 1; |
| 1934 | } |
| 1935 | |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1936 | /* This function adds entry to <ref>. It can failed with memory error. The new |
| 1937 | * entry is added at all the pattern_expr registered in this reference. The |
| 1938 | * function stop on the first error encountered. It returns 0 and err is |
| 1939 | * filled. If an error is encountered, the complete add operation is cancelled. |
| 1940 | * If the insertion is a success the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1941 | */ |
| 1942 | int pat_ref_add(struct pat_ref *ref, |
| 1943 | const char *pattern, const char *sample, |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1944 | char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1945 | { |
| 1946 | struct pat_ref_elt *elt; |
| 1947 | struct pattern_expr *expr; |
| 1948 | |
| 1949 | elt = malloc(sizeof(*elt)); |
| 1950 | if (!elt) { |
| 1951 | memprintf(err, "out of memory error"); |
| 1952 | return 0; |
| 1953 | } |
| 1954 | |
| 1955 | elt->line = -1; |
| 1956 | |
| 1957 | elt->pattern = strdup(pattern); |
| 1958 | if (!elt->pattern) { |
| 1959 | free(elt); |
| 1960 | memprintf(err, "out of memory error"); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | if (sample) { |
| 1965 | elt->sample = strdup(sample); |
| 1966 | if (!elt->sample) { |
| 1967 | free(elt->pattern); |
| 1968 | free(elt); |
| 1969 | memprintf(err, "out of memory error"); |
| 1970 | return 0; |
| 1971 | } |
| 1972 | } |
| 1973 | else |
| 1974 | elt->sample = NULL; |
| 1975 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1976 | LIST_INIT(&elt->back_refs); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1977 | LIST_ADDQ(&ref->head, &elt->list); |
| 1978 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1979 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1980 | if (!pat_ref_push(elt, expr, 0, err)) { |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1981 | /* If the insertion fails, try to delete all the added entries. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1982 | pat_ref_delete_by_id(ref, elt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1983 | return 0; |
| 1984 | } |
| 1985 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1986 | return 1; |
| 1987 | } |
| 1988 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 1989 | /* This function prune <ref>, replace all reference by the references |
| 1990 | * of <replace>, and reindex all the news values. |
| 1991 | * |
| 1992 | * The pattern are loaded in best effort and the errors are ignored, |
| 1993 | * but writed in the logs. |
| 1994 | */ |
| 1995 | void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace) |
| 1996 | { |
| 1997 | struct pattern_expr *expr; |
| 1998 | struct pat_ref_elt *elt; |
| 1999 | char *err = NULL; |
| 2000 | |
| 2001 | pat_ref_prune(ref); |
| 2002 | |
| 2003 | LIST_ADD(&replace->head, &ref->head); |
| 2004 | LIST_DEL(&replace->head); |
| 2005 | |
| 2006 | list_for_each_entry(elt, &ref->head, list) { |
| 2007 | list_for_each_entry(expr, &ref->pat, list) { |
| 2008 | if (!pat_ref_push(elt, expr, 0, &err)) { |
| 2009 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 2010 | free(err); |
| 2011 | err = NULL; |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2017 | /* This function prune all entries of <ref>. This function |
| 2018 | * prune the associated pattern_expr. |
| 2019 | */ |
| 2020 | void pat_ref_prune(struct pat_ref *ref) |
| 2021 | { |
| 2022 | struct pat_ref_elt *elt, *safe; |
| 2023 | struct pattern_expr *expr; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2024 | struct bref *bref, *back; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2025 | |
| 2026 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2027 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 2028 | /* |
| 2029 | * we have to unlink all watchers. We must not relink them if |
| 2030 | * this elt was the last one in the list. |
| 2031 | */ |
| 2032 | LIST_DEL(&bref->users); |
| 2033 | LIST_INIT(&bref->users); |
| 2034 | if (elt->list.n != &ref->head) |
| 2035 | LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users); |
| 2036 | bref->ref = elt->list.n; |
| 2037 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2038 | LIST_DEL(&elt->list); |
| 2039 | free(elt->pattern); |
| 2040 | free(elt->sample); |
| 2041 | free(elt); |
| 2042 | } |
| 2043 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2044 | list_for_each_entry(expr, &ref->pat, list) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2045 | expr->pat_head->prune(expr); |
| 2046 | } |
| 2047 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2048 | /* This function lookup for existing reference <ref> in pattern_head <head>. */ |
| 2049 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 2050 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2051 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2052 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2053 | list_for_each_entry(expr, &head->head, list) |
| 2054 | if (expr->expr->ref == ref) |
| 2055 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2056 | return NULL; |
| 2057 | } |
| 2058 | |
| 2059 | /* This function create new pattern_expr associated to the reference <ref>. |
| 2060 | * <ref> can be NULL. If an error is occured, the function returns NULL and |
| 2061 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 2062 | * with <head> and <ref>. |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2063 | * |
| 2064 | * The returned value can be a alredy filled pattern list, in this case the |
| 2065 | * flag <reuse> is set. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2066 | */ |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2067 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2068 | int patflags, char **err, int *reuse) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2069 | { |
| 2070 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2071 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2072 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2073 | if (reuse) |
| 2074 | *reuse = 0; |
| 2075 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2076 | /* Memory and initialization of the chain element. */ |
| 2077 | list = malloc(sizeof(*list)); |
| 2078 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2079 | memprintf(err, "out of memory"); |
| 2080 | return NULL; |
| 2081 | } |
| 2082 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2083 | /* Look for existing similar expr. No that only the index, parse and |
| 2084 | * parse_smp function must be identical for having similar pattern. |
| 2085 | * The other function depends of theses first. |
| 2086 | */ |
| 2087 | if (ref) { |
| 2088 | list_for_each_entry(expr, &ref->pat, list) |
| 2089 | if (expr->pat_head->index == head->index && |
| 2090 | expr->pat_head->parse == head->parse && |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2091 | expr->pat_head->parse_smp == head->parse_smp && |
| 2092 | expr->mflags == patflags) |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2093 | break; |
| 2094 | if (&expr->list == &ref->pat) |
| 2095 | expr = NULL; |
| 2096 | } |
| 2097 | else |
| 2098 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2099 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2100 | /* If no similar expr was found, we create new expr. */ |
| 2101 | if (!expr) { |
| 2102 | /* Get a lot of memory for the expr struct. */ |
| 2103 | expr = malloc(sizeof(*expr)); |
| 2104 | if (!expr) { |
Andreas Seltenreich | e6e22e8 | 2016-03-03 20:20:23 +0100 | [diff] [blame] | 2105 | free(list); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2106 | memprintf(err, "out of memory"); |
| 2107 | return NULL; |
| 2108 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2109 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2110 | /* Initialize this new expr. */ |
| 2111 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2112 | |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2113 | /* Copy the pattern matching and indexing flags. */ |
| 2114 | expr->mflags = patflags; |
| 2115 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2116 | /* This new pattern expression reference one of his heads. */ |
| 2117 | expr->pat_head = head; |
| 2118 | |
| 2119 | /* Link with ref, or to self to facilitate LIST_DEL() */ |
| 2120 | if (ref) |
| 2121 | LIST_ADDQ(&ref->pat, &expr->list); |
| 2122 | else |
| 2123 | LIST_INIT(&expr->list); |
| 2124 | |
| 2125 | expr->ref = ref; |
| 2126 | |
| 2127 | /* We must free this pattern if it is no more used. */ |
| 2128 | list->do_free = 1; |
| 2129 | } |
| 2130 | else { |
| 2131 | /* If the pattern used already exists, it is already linked |
| 2132 | * with ref and we must not free it. |
| 2133 | */ |
| 2134 | list->do_free = 0; |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2135 | if (reuse) |
| 2136 | *reuse = 1; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | /* The new list element reference the pattern_expr. */ |
| 2140 | list->expr = expr; |
| 2141 | |
| 2142 | /* Link the list element with the pattern_head. */ |
| 2143 | LIST_ADDQ(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2144 | return expr; |
| 2145 | } |
| 2146 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2147 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2148 | * 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] | 2149 | * |
| 2150 | * The file contains one key + value per line. Lines which start with '#' are |
| 2151 | * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is |
| 2152 | * then the first "word" (series of non-space/tabs characters), and the value is |
| 2153 | * what follows this series of space/tab till the end of the line excluding |
| 2154 | * trailing spaces/tabs. |
| 2155 | * |
| 2156 | * Example : |
| 2157 | * |
| 2158 | * # this is a comment and is ignored |
| 2159 | * 62.212.114.60 1wt.eu \n |
| 2160 | * <-><-----------><---><----><----> |
| 2161 | * | | | | `--- trailing spaces ignored |
| 2162 | * | | | `-------- value |
| 2163 | * | | `--------------- middle spaces ignored |
| 2164 | * | `------------------------ key |
| 2165 | * `-------------------------------- leading spaces ignored |
| 2166 | * |
| 2167 | * Return non-zero in case of succes, otherwise 0. |
| 2168 | */ |
| 2169 | int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err) |
| 2170 | { |
| 2171 | FILE *file; |
| 2172 | char *c; |
| 2173 | int ret = 0; |
| 2174 | int line = 0; |
| 2175 | char *key_beg; |
| 2176 | char *key_end; |
| 2177 | char *value_beg; |
| 2178 | char *value_end; |
| 2179 | |
| 2180 | file = fopen(filename, "r"); |
| 2181 | if (!file) { |
| 2182 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | /* now parse all patterns. The file may contain only one pattern |
| 2187 | * followed by one value per line. The start spaces, separator spaces |
| 2188 | * and and spaces are stripped. Each can contain comment started by '#' |
| 2189 | */ |
| 2190 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2191 | line++; |
| 2192 | c = trash.str; |
| 2193 | |
| 2194 | /* ignore lines beginning with a dash */ |
| 2195 | if (*c == '#') |
| 2196 | continue; |
| 2197 | |
| 2198 | /* strip leading spaces and tabs */ |
| 2199 | while (*c == ' ' || *c == '\t') |
| 2200 | c++; |
| 2201 | |
| 2202 | /* empty lines are ignored too */ |
| 2203 | if (*c == '\0' || *c == '\r' || *c == '\n') |
| 2204 | continue; |
| 2205 | |
| 2206 | /* look for the end of the key */ |
| 2207 | key_beg = c; |
| 2208 | while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2209 | c++; |
| 2210 | |
| 2211 | key_end = c; |
| 2212 | |
| 2213 | /* strip middle spaces and tabs */ |
| 2214 | while (*c == ' ' || *c == '\t') |
| 2215 | c++; |
| 2216 | |
| 2217 | /* look for the end of the value, it is the end of the line */ |
| 2218 | value_beg = c; |
| 2219 | while (*c && *c != '\n' && *c != '\r') |
| 2220 | c++; |
| 2221 | value_end = c; |
| 2222 | |
| 2223 | /* trim possibly trailing spaces and tabs */ |
| 2224 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2225 | value_end--; |
| 2226 | |
| 2227 | /* set final \0 and check entries */ |
| 2228 | *key_end = '\0'; |
| 2229 | *value_end = '\0'; |
| 2230 | |
| 2231 | /* insert values */ |
| 2232 | if (!pat_ref_append(ref, key_beg, value_beg, line)) { |
| 2233 | memprintf(err, "out of memory"); |
| 2234 | goto out_close; |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | /* succes */ |
| 2239 | ret = 1; |
| 2240 | |
| 2241 | out_close: |
| 2242 | fclose(file); |
| 2243 | return ret; |
| 2244 | } |
| 2245 | |
| 2246 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2247 | * 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] | 2248 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2249 | 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] | 2250 | { |
| 2251 | FILE *file; |
| 2252 | char *c; |
| 2253 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2254 | int ret = 0; |
| 2255 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2256 | |
| 2257 | file = fopen(filename, "r"); |
| 2258 | if (!file) { |
| 2259 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | /* now parse all patterns. The file may contain only one pattern per |
| 2264 | * line. If the line contains spaces, they will be part of the pattern. |
| 2265 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2266 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2267 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2268 | line++; |
| 2269 | c = trash.str; |
| 2270 | |
| 2271 | /* ignore lines beginning with a dash */ |
| 2272 | if (*c == '#') |
| 2273 | continue; |
| 2274 | |
| 2275 | /* strip leading spaces and tabs */ |
| 2276 | while (*c == ' ' || *c == '\t') |
| 2277 | c++; |
| 2278 | |
| 2279 | |
| 2280 | arg = c; |
| 2281 | while (*c && *c != '\n' && *c != '\r') |
| 2282 | c++; |
| 2283 | *c = 0; |
| 2284 | |
| 2285 | /* empty lines are ignored too */ |
| 2286 | if (c == arg) |
| 2287 | continue; |
| 2288 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2289 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2290 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2291 | goto out_close; |
| 2292 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | ret = 1; /* success */ |
| 2296 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2297 | out_close: |
| 2298 | fclose(file); |
| 2299 | return ret; |
| 2300 | } |
| 2301 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2302 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2303 | const char *filename, int patflags, int load_smp, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2304 | char **err, const char *file, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2305 | { |
| 2306 | struct pat_ref *ref; |
| 2307 | struct pattern_expr *expr; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2308 | struct pat_ref_elt *elt; |
Willy Tarreau | 4deaf39 | 2014-11-26 13:17:03 +0100 | [diff] [blame] | 2309 | int reuse = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2310 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2311 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2312 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2313 | |
| 2314 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2315 | if (!ref) { |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2316 | chunk_printf(&trash, |
| 2317 | "pattern loaded from file '%s' used by %s at file '%s' line %d", |
| 2318 | filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2319 | |
| 2320 | ref = pat_ref_new(filename, trash.str, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2321 | if (!ref) { |
| 2322 | memprintf(err, "out of memory"); |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2326 | if (load_smp) { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2327 | ref->flags |= PAT_REF_SMP; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2328 | if (!pat_ref_read_from_file_smp(ref, filename, err)) |
| 2329 | return 0; |
| 2330 | } |
| 2331 | else { |
| 2332 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2333 | return 0; |
| 2334 | } |
| 2335 | } |
| 2336 | else { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2337 | /* The reference already exists, check the map compatibility. */ |
| 2338 | |
| 2339 | /* If the load require samples and the flag PAT_REF_SMP is not set, |
| 2340 | * the reference doesn't contain sample, and cannot be used. |
| 2341 | */ |
| 2342 | if (load_smp) { |
| 2343 | if (!(ref->flags & PAT_REF_SMP)) { |
| 2344 | memprintf(err, "The file \"%s\" is already used as one column file " |
| 2345 | "and cannot be used by as two column file.", |
| 2346 | filename); |
| 2347 | return 0; |
| 2348 | } |
| 2349 | } |
| 2350 | else { |
| 2351 | /* The load doesn't require samples. If the flag PAT_REF_SMP is |
| 2352 | * set, the reference contains a sample, and cannot be used. |
| 2353 | */ |
| 2354 | if (ref->flags & PAT_REF_SMP) { |
| 2355 | memprintf(err, "The file \"%s\" is already used as two column file " |
| 2356 | "and cannot be used by as one column file.", |
| 2357 | filename); |
| 2358 | return 0; |
| 2359 | } |
| 2360 | } |
| 2361 | |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2362 | /* Extends display */ |
| 2363 | chunk_printf(&trash, "%s", ref->display); |
| 2364 | chunk_appendf(&trash, ", by %s at file '%s' line %d", |
| 2365 | refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2366 | free(ref->display); |
| 2367 | ref->display = strdup(trash.str); |
| 2368 | if (!ref->display) { |
| 2369 | memprintf(err, "out of memory"); |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2373 | /* Merge flags. */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2374 | ref->flags |= refflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | /* Now, we can loading patterns from the reference. */ |
| 2378 | |
| 2379 | /* Lookup for existing reference in the head. If the reference |
| 2380 | * doesn't exists, create it. |
| 2381 | */ |
| 2382 | expr = pattern_lookup_expr(head, ref); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2383 | if (!expr || (expr->mflags != patflags)) { |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2384 | expr = pattern_new_expr(head, ref, patflags, err, &reuse); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2385 | if (!expr) |
| 2386 | return 0; |
| 2387 | } |
| 2388 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2389 | /* The returned expression may be not empty, because the function |
| 2390 | * "pattern_new_expr" lookup for similar pattern list and can |
| 2391 | * reuse a already filled pattern list. In this case, we can not |
| 2392 | * reload the patterns. |
| 2393 | */ |
| 2394 | if (reuse) |
| 2395 | return 1; |
| 2396 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2397 | /* Load reference content in the pattern expression. */ |
| 2398 | list_for_each_entry(elt, &ref->head, list) { |
| 2399 | if (!pat_ref_push(elt, expr, patflags, err)) { |
| 2400 | if (elt->line > 0) |
| 2401 | memprintf(err, "%s at line %d of file '%s'", |
| 2402 | *err, elt->line, filename); |
| 2403 | return 0; |
| 2404 | } |
| 2405 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2406 | |
| 2407 | return 1; |
| 2408 | } |
| 2409 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2410 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 2411 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 2412 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2413 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2414 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2415 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2416 | 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] | 2417 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2418 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2419 | struct pattern *pat; |
| 2420 | |
| 2421 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2422 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2423 | static_pattern.data = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2424 | static_pattern.ref = NULL; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2425 | static_pattern.sflags = 0; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2426 | static_pattern.type = SMP_T_SINT; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2427 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2428 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2429 | return &static_pattern; |
| 2430 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2431 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2432 | /* convert input to string */ |
| 2433 | if (!sample_convert(smp, head->expect_type)) |
| 2434 | return NULL; |
| 2435 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2436 | list_for_each_entry(list, &head->head, list) { |
| 2437 | pat = head->match(smp, list->expr, fill); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2438 | if (pat) |
| 2439 | return pat; |
| 2440 | } |
| 2441 | return NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2444 | /* This function prune the pattern expression. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2445 | void pattern_prune(struct pattern_head *head) |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2446 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2447 | struct pattern_expr_list *list, *safe; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2448 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2449 | list_for_each_entry_safe(list, safe, &head->head, list) { |
| 2450 | LIST_DEL(&list->list); |
| 2451 | if (list->do_free) { |
| 2452 | LIST_DEL(&list->expr->list); |
| 2453 | head->prune(list->expr); |
| 2454 | free(list->expr); |
| 2455 | } |
| 2456 | free(list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2457 | } |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2460 | /* This function lookup for a pattern matching the <key> and return a |
| 2461 | * pointer to a pointer of the sample stoarge. If the <key> dont match, |
| 2462 | * the function returns NULL. If the key cannot be parsed, the function |
| 2463 | * fill <err>. |
| 2464 | */ |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 2465 | 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] | 2466 | { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2467 | struct ebmb_node *node; |
| 2468 | struct pattern_tree *elt; |
| 2469 | struct pattern_list *pat; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2470 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2471 | for (node = ebmb_first(&expr->pattern_tree); |
| 2472 | node; |
| 2473 | node = ebmb_next(node)) { |
| 2474 | elt = container_of(node, struct pattern_tree, node); |
| 2475 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2476 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | for (node = ebmb_first(&expr->pattern_tree_2); |
| 2480 | node; |
| 2481 | node = ebmb_next(node)) { |
| 2482 | elt = container_of(node, struct pattern_tree, node); |
| 2483 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2484 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | list_for_each_entry(pat, &expr->patterns, list) |
| 2488 | if (pat->pat.ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2489 | return &pat->pat.data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2490 | |
| 2491 | return NULL; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2494 | /* This function search all the pattern matching the <key> and delete it. |
| 2495 | * If the parsing of the input key fails, the function returns 0 and the |
| 2496 | * <err> is filled, else return 1; |
| 2497 | */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2498 | int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2499 | { |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2500 | expr->pat_head->delete(expr, ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2501 | return 1; |
| 2502 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2503 | |
| 2504 | /* This function finalize the configuration parsing. Its set all the |
| 2505 | * automatic ids |
| 2506 | */ |
| 2507 | void pattern_finalize_config(void) |
| 2508 | { |
| 2509 | int i = 0; |
| 2510 | struct pat_ref *ref, *ref2, *ref3; |
| 2511 | struct list pr = LIST_HEAD_INIT(pr); |
| 2512 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 2513 | pat_lru_seed = random(); |
| 2514 | if (global.tune.pattern_cache) |
| 2515 | pat_lru_tree = lru64_new(global.tune.pattern_cache); |
| 2516 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2517 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2518 | if (ref->unique_id == -1) { |
| 2519 | /* Look for the first free id. */ |
| 2520 | while (1) { |
| 2521 | list_for_each_entry(ref2, &pattern_reference, list) { |
| 2522 | if (ref2->unique_id == i) { |
| 2523 | i++; |
| 2524 | break; |
| 2525 | } |
| 2526 | } |
Willy Tarreau | 3b78696 | 2014-04-26 12:37:25 +0200 | [diff] [blame] | 2527 | if (&ref2->list == &pattern_reference) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | /* Uses the unique id and increment it for the next entry. */ |
| 2532 | ref->unique_id = i; |
| 2533 | i++; |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | /* This sort the reference list by id. */ |
| 2538 | list_for_each_entry_safe(ref, ref2, &pattern_reference, list) { |
| 2539 | LIST_DEL(&ref->list); |
| 2540 | list_for_each_entry(ref3, &pr, list) { |
| 2541 | if (ref->unique_id < ref3->unique_id) { |
| 2542 | LIST_ADDQ(&ref3->list, &ref->list); |
| 2543 | break; |
| 2544 | } |
| 2545 | } |
| 2546 | if (&ref3->list == &pr) |
| 2547 | LIST_ADDQ(&pr, &ref->list); |
| 2548 | } |
| 2549 | |
| 2550 | /* swap root */ |
| 2551 | LIST_ADD(&pr, &pattern_reference); |
| 2552 | LIST_DEL(&pr); |
| 2553 | } |