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