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> |
Jerome Magnin | 556a94a | 2020-01-17 18:01:20 +0100 | [diff] [blame] | 15 | #include <errno.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/standard.h> |
| 19 | |
| 20 | #include <types/global.h> |
| 21 | #include <types/pattern.h> |
| 22 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 23 | #include <proto/log.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 24 | #include <proto/pattern.h> |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 25 | #include <proto/sample.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 26 | |
| 27 | #include <ebsttree.h> |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 28 | #include <import/lru.h> |
| 29 | #include <import/xxhash.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 30 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 31 | char *pat_match_names[PAT_MATCH_NUM] = { |
| 32 | [PAT_MATCH_FOUND] = "found", |
| 33 | [PAT_MATCH_BOOL] = "bool", |
| 34 | [PAT_MATCH_INT] = "int", |
| 35 | [PAT_MATCH_IP] = "ip", |
| 36 | [PAT_MATCH_BIN] = "bin", |
| 37 | [PAT_MATCH_LEN] = "len", |
| 38 | [PAT_MATCH_STR] = "str", |
| 39 | [PAT_MATCH_BEG] = "beg", |
| 40 | [PAT_MATCH_SUB] = "sub", |
| 41 | [PAT_MATCH_DIR] = "dir", |
| 42 | [PAT_MATCH_DOM] = "dom", |
| 43 | [PAT_MATCH_END] = "end", |
| 44 | [PAT_MATCH_REG] = "reg", |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 45 | [PAT_MATCH_REGM] = "regm", |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 48 | 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] | 49 | [PAT_MATCH_FOUND] = pat_parse_nothing, |
| 50 | [PAT_MATCH_BOOL] = pat_parse_nothing, |
| 51 | [PAT_MATCH_INT] = pat_parse_int, |
| 52 | [PAT_MATCH_IP] = pat_parse_ip, |
| 53 | [PAT_MATCH_BIN] = pat_parse_bin, |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 54 | [PAT_MATCH_LEN] = pat_parse_int, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 55 | [PAT_MATCH_STR] = pat_parse_str, |
| 56 | [PAT_MATCH_BEG] = pat_parse_str, |
| 57 | [PAT_MATCH_SUB] = pat_parse_str, |
| 58 | [PAT_MATCH_DIR] = pat_parse_str, |
| 59 | [PAT_MATCH_DOM] = pat_parse_str, |
| 60 | [PAT_MATCH_END] = pat_parse_str, |
| 61 | [PAT_MATCH_REG] = pat_parse_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 62 | [PAT_MATCH_REGM] = pat_parse_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 65 | int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = { |
| 66 | [PAT_MATCH_FOUND] = pat_idx_list_val, |
| 67 | [PAT_MATCH_BOOL] = pat_idx_list_val, |
| 68 | [PAT_MATCH_INT] = pat_idx_list_val, |
| 69 | [PAT_MATCH_IP] = pat_idx_tree_ip, |
| 70 | [PAT_MATCH_BIN] = pat_idx_list_ptr, |
| 71 | [PAT_MATCH_LEN] = pat_idx_list_val, |
| 72 | [PAT_MATCH_STR] = pat_idx_tree_str, |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 73 | [PAT_MATCH_BEG] = pat_idx_tree_pfx, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 74 | [PAT_MATCH_SUB] = pat_idx_list_str, |
| 75 | [PAT_MATCH_DIR] = pat_idx_list_str, |
| 76 | [PAT_MATCH_DOM] = pat_idx_list_str, |
| 77 | [PAT_MATCH_END] = pat_idx_list_str, |
| 78 | [PAT_MATCH_REG] = pat_idx_list_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 79 | [PAT_MATCH_REGM] = pat_idx_list_regm, |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 82 | 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] | 83 | [PAT_MATCH_FOUND] = pat_del_list_val, |
| 84 | [PAT_MATCH_BOOL] = pat_del_list_val, |
| 85 | [PAT_MATCH_INT] = pat_del_list_val, |
| 86 | [PAT_MATCH_IP] = pat_del_tree_ip, |
| 87 | [PAT_MATCH_BIN] = pat_del_list_ptr, |
| 88 | [PAT_MATCH_LEN] = pat_del_list_val, |
| 89 | [PAT_MATCH_STR] = pat_del_tree_str, |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 90 | [PAT_MATCH_BEG] = pat_del_tree_str, |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 91 | [PAT_MATCH_SUB] = pat_del_list_ptr, |
| 92 | [PAT_MATCH_DIR] = pat_del_list_ptr, |
| 93 | [PAT_MATCH_DOM] = pat_del_list_ptr, |
| 94 | [PAT_MATCH_END] = pat_del_list_ptr, |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 95 | [PAT_MATCH_REG] = pat_del_list_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 96 | [PAT_MATCH_REGM] = pat_del_list_reg, |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 99 | void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = { |
| 100 | [PAT_MATCH_FOUND] = pat_prune_val, |
| 101 | [PAT_MATCH_BOOL] = pat_prune_val, |
| 102 | [PAT_MATCH_INT] = pat_prune_val, |
| 103 | [PAT_MATCH_IP] = pat_prune_val, |
| 104 | [PAT_MATCH_BIN] = pat_prune_ptr, |
| 105 | [PAT_MATCH_LEN] = pat_prune_val, |
| 106 | [PAT_MATCH_STR] = pat_prune_ptr, |
| 107 | [PAT_MATCH_BEG] = pat_prune_ptr, |
| 108 | [PAT_MATCH_SUB] = pat_prune_ptr, |
| 109 | [PAT_MATCH_DIR] = pat_prune_ptr, |
| 110 | [PAT_MATCH_DOM] = pat_prune_ptr, |
| 111 | [PAT_MATCH_END] = pat_prune_ptr, |
| 112 | [PAT_MATCH_REG] = pat_prune_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 113 | [PAT_MATCH_REGM] = pat_prune_reg, |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 116 | 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] | 117 | [PAT_MATCH_FOUND] = NULL, |
| 118 | [PAT_MATCH_BOOL] = pat_match_nothing, |
| 119 | [PAT_MATCH_INT] = pat_match_int, |
| 120 | [PAT_MATCH_IP] = pat_match_ip, |
| 121 | [PAT_MATCH_BIN] = pat_match_bin, |
| 122 | [PAT_MATCH_LEN] = pat_match_len, |
| 123 | [PAT_MATCH_STR] = pat_match_str, |
| 124 | [PAT_MATCH_BEG] = pat_match_beg, |
| 125 | [PAT_MATCH_SUB] = pat_match_sub, |
| 126 | [PAT_MATCH_DIR] = pat_match_dir, |
| 127 | [PAT_MATCH_DOM] = pat_match_dom, |
| 128 | [PAT_MATCH_END] = pat_match_end, |
| 129 | [PAT_MATCH_REG] = pat_match_reg, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 130 | [PAT_MATCH_REGM] = pat_match_regm, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 133 | /* Just used for checking configuration compatibility */ |
| 134 | int pat_match_types[PAT_MATCH_NUM] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 135 | [PAT_MATCH_FOUND] = SMP_T_SINT, |
| 136 | [PAT_MATCH_BOOL] = SMP_T_SINT, |
| 137 | [PAT_MATCH_INT] = SMP_T_SINT, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 138 | [PAT_MATCH_IP] = SMP_T_ADDR, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 139 | [PAT_MATCH_BIN] = SMP_T_BIN, |
| 140 | [PAT_MATCH_LEN] = SMP_T_STR, |
| 141 | [PAT_MATCH_STR] = SMP_T_STR, |
| 142 | [PAT_MATCH_BEG] = SMP_T_STR, |
| 143 | [PAT_MATCH_SUB] = SMP_T_STR, |
| 144 | [PAT_MATCH_DIR] = SMP_T_STR, |
| 145 | [PAT_MATCH_DOM] = SMP_T_STR, |
| 146 | [PAT_MATCH_END] = SMP_T_STR, |
| 147 | [PAT_MATCH_REG] = SMP_T_STR, |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 148 | [PAT_MATCH_REGM] = SMP_T_STR, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 151 | /* this struct is used to return information */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 152 | static THREAD_LOCAL struct pattern static_pattern; |
| 153 | static THREAD_LOCAL struct sample_data static_sample_data; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 154 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 155 | /* This is the root of the list of all pattern_ref avalaibles. */ |
| 156 | struct list pattern_reference = LIST_HEAD_INIT(pattern_reference); |
| 157 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 158 | static THREAD_LOCAL struct lru64_head *pat_lru_tree; |
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; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 456 | struct pattern_tree *elt; |
| 457 | struct pattern_list *lst; |
| 458 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 459 | struct pattern *ret = NULL; |
| 460 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 461 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 462 | /* Lookup a string in the expression's pattern tree. */ |
| 463 | if (!eb_is_empty(&expr->pattern_tree)) { |
Christopher Faulet | 135899e | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 464 | char prev = 0; |
| 465 | |
| 466 | if (smp->data.u.str.data < smp->data.u.str.size) { |
| 467 | /* we may have to force a trailing zero on the test pattern and |
| 468 | * the buffer is large enough to accommodate it. |
| 469 | */ |
| 470 | prev = smp->data.u.str.area[smp->data.u.str.data]; |
| 471 | if (prev) |
| 472 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
| 473 | } |
| 474 | else { |
| 475 | /* Otherwise, the sample is duplicated. A trailing zero |
| 476 | * is automatically added to the string. |
| 477 | */ |
| 478 | if (!smp_dup(smp)) |
| 479 | return NULL; |
| 480 | } |
| 481 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 482 | node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.area); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 483 | if (prev) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 484 | smp->data.u.str.area[smp->data.u.str.data] = prev; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 485 | if (node) { |
| 486 | if (fill) { |
| 487 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 488 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 489 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 490 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 491 | static_pattern.type = SMP_T_STR; |
| 492 | static_pattern.ptr.str = (char *)elt->node.key; |
| 493 | } |
| 494 | return &static_pattern; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 499 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 500 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 502 | 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] | 503 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 504 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 505 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 506 | return ret; |
| 507 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 510 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 511 | list_for_each_entry(lst, &expr->patterns, list) { |
| 512 | pattern = &lst->pat; |
| 513 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 514 | if (pattern->len != smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 515 | continue; |
| 516 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 517 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 518 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, smp->data.u.str.data) == 0) || |
| 519 | (!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] | 520 | ret = pattern; |
| 521 | break; |
| 522 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 525 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 526 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 527 | |
| 528 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* 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] | 532 | 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] | 533 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 534 | struct pattern_list *lst; |
| 535 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 536 | struct pattern *ret = NULL; |
| 537 | struct lru64 *lru = NULL; |
| 538 | |
| 539 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 540 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 541 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 542 | 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] | 543 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 544 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 545 | ret = lru->data; |
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 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 562 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 563 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 564 | |
| 565 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* 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] | 569 | * and restores the previous character when leaving. This function fills |
| 570 | * a matching array. |
| 571 | */ |
| 572 | struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill) |
| 573 | { |
| 574 | struct pattern_list *lst; |
| 575 | struct pattern *pattern; |
| 576 | struct pattern *ret = NULL; |
| 577 | |
| 578 | list_for_each_entry(lst, &expr->patterns, list) { |
| 579 | pattern = &lst->pat; |
| 580 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 581 | 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] | 582 | MAX_MATCH, pmatch, 0)) { |
| 583 | ret = pattern; |
| 584 | smp->ctx.a[0] = pmatch; |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | /* 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] | 593 | * and restores the previous character when leaving. |
| 594 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 595 | 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] | 596 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 597 | struct pattern_list *lst; |
| 598 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 599 | struct pattern *ret = NULL; |
| 600 | struct lru64 *lru = NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 601 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 602 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 603 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 604 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 605 | 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] | 606 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 607 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 608 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 609 | return ret; |
| 610 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 613 | list_for_each_entry(lst, &expr->patterns, list) { |
| 614 | pattern = &lst->pat; |
| 615 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 616 | 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] | 617 | ret = pattern; |
| 618 | break; |
| 619 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 620 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 621 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 622 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 623 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 624 | |
| 625 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 629 | 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] | 630 | { |
| 631 | int icase; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 632 | struct ebmb_node *node; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 633 | struct pattern_tree *elt; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 634 | struct pattern_list *lst; |
| 635 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 636 | struct pattern *ret = NULL; |
| 637 | struct lru64 *lru = NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 638 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 639 | /* Lookup a string in the expression's pattern tree. */ |
| 640 | if (!eb_is_empty(&expr->pattern_tree)) { |
Christopher Faulet | 135899e | 2020-06-30 18:52:32 +0200 | [diff] [blame] | 641 | char prev = 0; |
| 642 | |
| 643 | if (smp->data.u.str.data < smp->data.u.str.size) { |
| 644 | /* we may have to force a trailing zero on the test pattern and |
| 645 | * the buffer is large enough to accommodate it. |
| 646 | */ |
| 647 | prev = smp->data.u.str.area[smp->data.u.str.data]; |
| 648 | if (prev) |
| 649 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
| 650 | } |
| 651 | else { |
| 652 | /* Otherwise, the sample is duplicated. A trailing zero |
| 653 | * is automatically added to the string. |
| 654 | */ |
| 655 | if (!smp_dup(smp)) |
| 656 | return NULL; |
| 657 | } |
| 658 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 659 | node = ebmb_lookup_longest(&expr->pattern_tree, |
| 660 | smp->data.u.str.area); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 661 | if (prev) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 662 | smp->data.u.str.area[smp->data.u.str.data] = prev; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 663 | |
| 664 | if (node) { |
| 665 | if (fill) { |
| 666 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 667 | static_pattern.data = elt->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 668 | static_pattern.ref = elt->ref; |
| 669 | static_pattern.sflags = PAT_SF_TREE; |
| 670 | static_pattern.type = SMP_T_STR; |
| 671 | static_pattern.ptr.str = (char *)elt->node.key; |
| 672 | } |
| 673 | return &static_pattern; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | /* look in the list */ |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 678 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 679 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 680 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 681 | 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] | 682 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 683 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 684 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 685 | return ret; |
| 686 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 689 | list_for_each_entry(lst, &expr->patterns, list) { |
| 690 | pattern = &lst->pat; |
| 691 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 693 | continue; |
| 694 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 695 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 696 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area, pattern->len) != 0) || |
| 697 | (!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] | 698 | continue; |
| 699 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 700 | ret = pattern; |
| 701 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 702 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 703 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 704 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 705 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 706 | |
| 707 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 711 | 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] | 712 | { |
| 713 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 714 | struct pattern_list *lst; |
| 715 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 716 | struct pattern *ret = NULL; |
| 717 | struct lru64 *lru = NULL; |
| 718 | |
| 719 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 720 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 721 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | 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] | 723 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 724 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 725 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 726 | return ret; |
| 727 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 728 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 729 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 730 | list_for_each_entry(lst, &expr->patterns, list) { |
| 731 | pattern = &lst->pat; |
| 732 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 734 | continue; |
| 735 | |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 736 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 737 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.area + smp->data.u.str.data - pattern->len, pattern->len) != 0) || |
| 738 | (!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] | 739 | continue; |
| 740 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 741 | ret = pattern; |
| 742 | break; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 743 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 744 | |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 745 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 746 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 747 | |
| 748 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* Checks that the pattern is included inside the tested string. |
| 752 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 753 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 754 | 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] | 755 | { |
| 756 | int icase; |
| 757 | char *end; |
| 758 | char *c; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 759 | struct pattern_list *lst; |
| 760 | struct pattern *pattern; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 761 | struct pattern *ret = NULL; |
| 762 | struct lru64 *lru = NULL; |
| 763 | |
| 764 | if (pat_lru_tree) { |
Willy Tarreau | aee9314 | 2015-05-04 17:18:42 +0200 | [diff] [blame] | 765 | unsigned long long seed = pat_lru_seed ^ (long)expr; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 766 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 767 | 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] | 768 | pat_lru_tree, expr, expr->revision); |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 769 | if (lru && lru->domain) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 770 | ret = lru->data; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 771 | return ret; |
| 772 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 773 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 774 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 775 | list_for_each_entry(lst, &expr->patterns, list) { |
| 776 | pattern = &lst->pat; |
| 777 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 778 | if (pattern->len > smp->data.u.str.data) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 779 | continue; |
| 780 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 781 | 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] | 782 | icase = expr->mflags & PAT_MF_IGNORE_CASE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 783 | if (icase) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 784 | for (c = smp->data.u.str.area; c <= end; c++) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 785 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 786 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 787 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 788 | ret = pattern; |
| 789 | goto leave; |
| 790 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 791 | } |
| 792 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 793 | for (c = smp->data.u.str.area; c <= end; c++) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 794 | if (*c != *pattern->ptr.str) |
| 795 | continue; |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 796 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) { |
| 797 | ret = pattern; |
| 798 | goto leave; |
| 799 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 800 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 801 | } |
| 802 | } |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 803 | leave: |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 804 | if (lru) |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 805 | lru64_commit(lru, ret, expr, expr->revision, NULL); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 806 | |
| 807 | return ret; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* This one is used by other real functions. It checks that the pattern is |
| 811 | * included inside the tested string, but enclosed between the specified |
| 812 | * delimiters or at the beginning or end of the string. The delimiters are |
| 813 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 814 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 815 | */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 816 | 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] | 817 | { |
| 818 | int may_match, icase; |
| 819 | char *c, *end; |
| 820 | char *ps; |
| 821 | int pl; |
| 822 | |
| 823 | pl = pattern->len; |
| 824 | ps = pattern->ptr.str; |
| 825 | |
| 826 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 827 | pl--; |
| 828 | ps++; |
| 829 | } |
| 830 | |
| 831 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 832 | pl--; |
| 833 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 834 | if (pl > smp->data.u.str.data) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 835 | return PAT_NOMATCH; |
| 836 | |
| 837 | may_match = 1; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 838 | icase = mflags & PAT_MF_IGNORE_CASE; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 839 | end = smp->data.u.str.area + smp->data.u.str.data - pl; |
| 840 | for (c = smp->data.u.str.area; c <= end; c++) { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 841 | if (is_delimiter(*c, delimiters)) { |
| 842 | may_match = 1; |
| 843 | continue; |
| 844 | } |
| 845 | |
| 846 | if (!may_match) |
| 847 | continue; |
| 848 | |
| 849 | if (icase) { |
| 850 | if ((tolower(*c) == tolower(*ps)) && |
| 851 | (strncasecmp(ps, c, pl) == 0) && |
| 852 | (c == end || is_delimiter(c[pl], delimiters))) |
| 853 | return PAT_MATCH; |
| 854 | } else { |
| 855 | if ((*c == *ps) && |
| 856 | (strncmp(ps, c, pl) == 0) && |
| 857 | (c == end || is_delimiter(c[pl], delimiters))) |
| 858 | return PAT_MATCH; |
| 859 | } |
| 860 | may_match = 0; |
| 861 | } |
| 862 | return PAT_NOMATCH; |
| 863 | } |
| 864 | |
| 865 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 866 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 867 | * Delimiters at the beginning or end of the pattern are ignored. |
| 868 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 869 | 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] | 870 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 871 | struct pattern_list *lst; |
| 872 | struct pattern *pattern; |
| 873 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 874 | list_for_each_entry(lst, &expr->patterns, list) { |
| 875 | pattern = &lst->pat; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 876 | if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?'))) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 877 | return pattern; |
| 878 | } |
| 879 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 883 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 884 | * the string. 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_dom(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 integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 900 | 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] | 901 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 902 | struct pattern_list *lst; |
| 903 | struct pattern *pattern; |
| 904 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 905 | list_for_each_entry(lst, &expr->patterns, list) { |
| 906 | pattern = &lst->pat; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 907 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) && |
| 908 | (!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] | 909 | return pattern; |
| 910 | } |
| 911 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /* 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] | 915 | 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] | 916 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 917 | struct pattern_list *lst; |
| 918 | struct pattern *pattern; |
| 919 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 920 | list_for_each_entry(lst, &expr->patterns, list) { |
| 921 | pattern = &lst->pat; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 922 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.data) && |
| 923 | (!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] | 924 | return pattern; |
| 925 | } |
| 926 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 929 | 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] | 930 | { |
| 931 | unsigned int v4; /* in network byte order */ |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 932 | struct in6_addr tmp6; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 933 | struct in_addr *s; |
| 934 | struct ebmb_node *node; |
| 935 | struct pattern_tree *elt; |
| 936 | struct pattern_list *lst; |
| 937 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 938 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 939 | /* The input sample is IPv4. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 940 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 941 | /* Lookup an IPv4 address in the expression's pattern tree using |
| 942 | * the longest match method. |
| 943 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 944 | s = &smp->data.u.ipv4; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 945 | node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
| 946 | if (node) { |
| 947 | if (fill) { |
| 948 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 949 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 950 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 951 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 952 | static_pattern.type = SMP_T_IPV4; |
Willy Tarreau | 33ccf1c | 2019-06-16 18:40:33 +0200 | [diff] [blame] | 953 | static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 954 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 955 | return NULL; |
| 956 | } |
| 957 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 958 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 959 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 960 | /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4 |
| 961 | * sample address to IPv6 with the mapping method using the ::ffff: |
| 962 | * prefix, and try to lookup in the IPv6 tree. |
| 963 | */ |
| 964 | memset(&tmp6, 0, 10); |
| 965 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 966 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 967 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6); |
| 968 | if (node) { |
| 969 | if (fill) { |
| 970 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 971 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 972 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 973 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 974 | static_pattern.type = SMP_T_IPV6; |
Willy Tarreau | 33ccf1c | 2019-06-16 18:40:33 +0200 | [diff] [blame] | 975 | static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 976 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
| 977 | } |
| 978 | return &static_pattern; |
| 979 | } |
| 980 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 981 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 982 | /* The input sample is IPv6. Try to match in the trees. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 983 | if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 984 | /* Lookup an IPv6 address in the expression's pattern tree using |
| 985 | * the longest match method. |
| 986 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 987 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 988 | if (node) { |
| 989 | if (fill) { |
| 990 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 991 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 992 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 993 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 994 | static_pattern.type = SMP_T_IPV6; |
Willy Tarreau | 33ccf1c | 2019-06-16 18:40:33 +0200 | [diff] [blame] | 995 | static_pattern.val.ipv6.addr = *(struct in6_addr *)elt->node.key; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 996 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 997 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 998 | return &static_pattern; |
| 999 | } |
| 1000 | |
| 1001 | /* Try to convert 6 to 4 when the start of the ipv6 address match the |
| 1002 | * following forms : |
| 1003 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 1004 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 1005 | * - 2002:ip:v4:: (6to4) |
| 1006 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1007 | if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 && |
| 1008 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 && |
| 1009 | (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 || |
| 1010 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) || |
| 1011 | *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) { |
| 1012 | if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0) |
| 1013 | v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12]; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1014 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1015 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) + |
| 1016 | ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4])); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1017 | |
| 1018 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 1019 | * match method. |
| 1020 | */ |
| 1021 | node = ebmb_lookup_longest(&expr->pattern_tree, &v4); |
| 1022 | if (node) { |
| 1023 | if (fill) { |
| 1024 | elt = ebmb_entry(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1025 | static_pattern.data = elt->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1026 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1027 | static_pattern.sflags = PAT_SF_TREE; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1028 | static_pattern.type = SMP_T_IPV4; |
Willy Tarreau | 33ccf1c | 2019-06-16 18:40:33 +0200 | [diff] [blame] | 1029 | static_pattern.val.ipv4.addr = *(struct in_addr *)elt->node.key; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1030 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 1031 | return NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1032 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1033 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1034 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1035 | } |
| 1036 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1037 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1038 | /* Lookup in the list. the list contain only IPv4 patterns */ |
| 1039 | list_for_each_entry(lst, &expr->patterns, list) { |
| 1040 | pattern = &lst->pat; |
| 1041 | |
| 1042 | /* The input sample is IPv4, use it as is. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1043 | if (smp->data.type == SMP_T_IPV4) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1044 | v4 = smp->data.u.ipv4.s_addr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1045 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1046 | else if (smp->data.type == SMP_T_IPV6) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1047 | /* v4 match on a V6 sample. We want to check at least for |
| 1048 | * the following forms : |
| 1049 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 1050 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 1051 | * - 2002:ip:v4:: (6to4) |
| 1052 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1053 | if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 && |
| 1054 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 && |
| 1055 | (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 || |
| 1056 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 1057 | v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1058 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1059 | else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) { |
| 1060 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) + |
| 1061 | ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4])); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1062 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1063 | else |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1064 | continue; |
Andreas Seltenreich | f065319 | 2016-03-03 20:08:35 +0100 | [diff] [blame] | 1065 | } else { |
| 1066 | /* impossible */ |
| 1067 | continue; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1068 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1069 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1070 | /* Check if the input sample match the current pattern. */ |
| 1071 | 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] | 1072 | return pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1073 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 1074 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1077 | void free_pattern_tree(struct eb_root *root) |
| 1078 | { |
| 1079 | struct eb_node *node, *next; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1080 | struct pattern_tree *elt; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1081 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1082 | node = eb_first(root); |
| 1083 | while (node) { |
| 1084 | next = eb_next(node); |
| 1085 | eb_delete(node); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1086 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1087 | free(elt->data); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 1088 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1089 | node = next; |
| 1090 | } |
| 1091 | } |
| 1092 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1093 | void pat_prune_val(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1094 | { |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1095 | struct pattern_list *pat, *tmp; |
| 1096 | |
| 1097 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1098 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1099 | free(pat); |
| 1100 | } |
| 1101 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1102 | free_pattern_tree(&expr->pattern_tree); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1103 | free_pattern_tree(&expr->pattern_tree_2); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 1104 | LIST_INIT(&expr->patterns); |
| 1105 | } |
| 1106 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1107 | void pat_prune_ptr(struct pattern_expr *expr) |
| 1108 | { |
| 1109 | struct pattern_list *pat, *tmp; |
| 1110 | |
| 1111 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 1112 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1113 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1114 | free(pat); |
| 1115 | } |
| 1116 | |
| 1117 | free_pattern_tree(&expr->pattern_tree); |
| 1118 | free_pattern_tree(&expr->pattern_tree_2); |
| 1119 | LIST_INIT(&expr->patterns); |
| 1120 | } |
| 1121 | |
| 1122 | void pat_prune_reg(struct pattern_expr *expr) |
| 1123 | { |
| 1124 | struct pattern_list *pat, *tmp; |
| 1125 | |
| 1126 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 1127 | regex_free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1128 | free(pat->pat.data); |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 1129 | free(pat); |
| 1130 | } |
| 1131 | |
| 1132 | free_pattern_tree(&expr->pattern_tree); |
| 1133 | free_pattern_tree(&expr->pattern_tree_2); |
| 1134 | LIST_INIT(&expr->patterns); |
| 1135 | } |
| 1136 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1137 | /* |
| 1138 | * |
| 1139 | * The following functions are used for the pattern indexation |
| 1140 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1141 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1142 | |
| 1143 | 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] | 1144 | { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1145 | struct pattern_list *patl; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1146 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1147 | /* allocate pattern */ |
| 1148 | patl = calloc(1, sizeof(*patl)); |
| 1149 | if (!patl) { |
| 1150 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1153 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1154 | /* duplicate pattern */ |
| 1155 | memcpy(&patl->pat, pat, sizeof(*pat)); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1156 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1157 | /* chain pattern in the expression */ |
| 1158 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1159 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* that's ok */ |
| 1162 | return 1; |
| 1163 | } |
| 1164 | |
| 1165 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1166 | { |
| 1167 | struct pattern_list *patl; |
| 1168 | |
| 1169 | /* allocate pattern */ |
| 1170 | patl = calloc(1, sizeof(*patl)); |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1171 | if (!patl) { |
| 1172 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1173 | return 0; |
Thierry FOURNIER | 8aa8384 | 2015-02-06 17:50:55 +0100 | [diff] [blame] | 1174 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1175 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1176 | /* duplicate pattern */ |
| 1177 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1178 | patl->pat.ptr.ptr = malloc(patl->pat.len); |
| 1179 | if (!patl->pat.ptr.ptr) { |
| 1180 | free(patl); |
| 1181 | memprintf(err, "out of memory while indexing pattern"); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1185 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1186 | /* chain pattern in the expression */ |
| 1187 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1188 | expr->revision = rdtsc(); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1189 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1190 | /* that's ok */ |
| 1191 | return 1; |
| 1192 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1193 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1194 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1195 | { |
| 1196 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1197 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1198 | /* allocate pattern */ |
| 1199 | patl = calloc(1, sizeof(*patl)); |
| 1200 | if (!patl) { |
| 1201 | memprintf(err, "out of memory while indexing pattern"); |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /* duplicate pattern */ |
| 1206 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1207 | patl->pat.ptr.str = malloc(patl->pat.len + 1); |
| 1208 | if (!patl->pat.ptr.str) { |
| 1209 | free(patl); |
| 1210 | memprintf(err, "out of memory while indexing pattern"); |
| 1211 | return 0; |
| 1212 | } |
| 1213 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
| 1214 | patl->pat.ptr.str[patl->pat.len] = '\0'; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1215 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1216 | /* chain pattern in the expression */ |
| 1217 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1218 | expr->revision = rdtsc(); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1219 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1220 | /* that's ok */ |
| 1221 | return 1; |
| 1222 | } |
| 1223 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1224 | 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] | 1225 | { |
| 1226 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1227 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1228 | /* allocate pattern */ |
| 1229 | patl = calloc(1, sizeof(*patl)); |
| 1230 | if (!patl) { |
| 1231 | memprintf(err, "out of memory while indexing pattern"); |
| 1232 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1233 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1234 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1235 | /* duplicate pattern */ |
| 1236 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1237 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1238 | /* compile regex */ |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1239 | if (!(patl->pat.ptr.reg = regex_comp(pat->ptr.str, !(expr->mflags & PAT_MF_IGNORE_CASE), |
| 1240 | cap, err))) { |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1241 | free(patl); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1242 | return 0; |
| 1243 | } |
| 1244 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1245 | /* chain pattern in the expression */ |
| 1246 | LIST_ADDQ(&expr->patterns, &patl->list); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1247 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* that's ok */ |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
Thierry Fournier | 8feaa66 | 2016-02-10 22:55:20 +0100 | [diff] [blame] | 1253 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1254 | { |
| 1255 | return pat_idx_list_reg_cap(expr, pat, 0, err); |
| 1256 | } |
| 1257 | |
| 1258 | int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1259 | { |
| 1260 | return pat_idx_list_reg_cap(expr, pat, 1, err); |
| 1261 | } |
| 1262 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1263 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1264 | { |
| 1265 | unsigned int mask; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1266 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1267 | |
| 1268 | /* Only IPv4 can be indexed */ |
| 1269 | if (pat->type == SMP_T_IPV4) { |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1270 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 1271 | * insert the network into the tree. A continuous mask has only |
| 1272 | * ones on the left. This means that this mask + its lower bit |
| 1273 | * added once again is null. |
| 1274 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1275 | mask = ntohl(pat->val.ipv4.mask.s_addr); |
| 1276 | if (mask + (mask & -mask) == 0) { |
| 1277 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1278 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1279 | /* node memory allocation */ |
| 1280 | node = calloc(1, sizeof(*node) + 4); |
| 1281 | if (!node) { |
| 1282 | memprintf(err, "out of memory while loading pattern"); |
| 1283 | return 0; |
| 1284 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1285 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1286 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1287 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1288 | node->ref = pat->ref; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1289 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1290 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1291 | memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */ |
| 1292 | node->node.node.pfx = mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Insert the entry. */ |
| 1295 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1296 | expr->revision = rdtsc(); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1297 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1298 | /* that's ok */ |
| 1299 | return 1; |
| 1300 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1301 | else { |
| 1302 | /* If the mask is not contiguous, just add the pattern to the list */ |
| 1303 | return pat_idx_list_val(expr, pat, err); |
| 1304 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1305 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1306 | else if (pat->type == SMP_T_IPV6) { |
| 1307 | /* IPv6 also can be indexed */ |
| 1308 | node = calloc(1, sizeof(*node) + 16); |
| 1309 | if (!node) { |
| 1310 | memprintf(err, "out of memory while loading pattern"); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1315 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1316 | node->ref = pat->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1317 | |
| 1318 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1319 | memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */ |
| 1320 | node->node.node.pfx = pat->val.ipv6.mask; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* Insert the entry. */ |
| 1323 | ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1324 | expr->revision = rdtsc(); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1325 | |
| 1326 | /* that's ok */ |
| 1327 | return 1; |
| 1328 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1329 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1330 | return 0; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1334 | { |
| 1335 | int len; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1336 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1337 | |
| 1338 | /* Only string can be indexed */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1339 | if (pat->type != SMP_T_STR) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1340 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1341 | smp_to_type[pat->type]); |
| 1342 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1343 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1344 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1345 | /* 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] | 1346 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1347 | return pat_idx_list_str(expr, pat, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1348 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1349 | /* Process the key len */ |
| 1350 | len = strlen(pat->ptr.str) + 1; |
| 1351 | |
| 1352 | /* node memory allocation */ |
| 1353 | node = calloc(1, sizeof(*node) + len); |
| 1354 | if (!node) { |
| 1355 | memprintf(err, "out of memory while loading pattern"); |
| 1356 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1359 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1360 | node->data = pat->data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1361 | node->ref = pat->ref; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1362 | |
| 1363 | /* copy the string */ |
| 1364 | memcpy(node->node.key, pat->ptr.str, len); |
| 1365 | |
| 1366 | /* index the new node */ |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1367 | ebst_insert(&expr->pattern_tree, &node->node); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1368 | expr->revision = rdtsc(); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1369 | |
| 1370 | /* that's ok */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1371 | return 1; |
| 1372 | } |
| 1373 | |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1374 | int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1375 | { |
| 1376 | int len; |
| 1377 | struct pattern_tree *node; |
| 1378 | |
| 1379 | /* Only string can be indexed */ |
| 1380 | if (pat->type != SMP_T_STR) { |
| 1381 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1382 | smp_to_type[pat->type]); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1387 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
| 1388 | return pat_idx_list_str(expr, pat, err); |
| 1389 | |
| 1390 | /* Process the key len */ |
| 1391 | len = strlen(pat->ptr.str); |
| 1392 | |
| 1393 | /* node memory allocation */ |
| 1394 | node = calloc(1, sizeof(*node) + len + 1); |
| 1395 | if (!node) { |
| 1396 | memprintf(err, "out of memory while loading pattern"); |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | /* copy the pointer to sample associated to this node */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1401 | node->data = pat->data; |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1402 | node->ref = pat->ref; |
| 1403 | |
| 1404 | /* copy the string and the trailing zero */ |
| 1405 | memcpy(node->node.key, pat->ptr.str, len + 1); |
| 1406 | node->node.node.pfx = len * 8; |
| 1407 | |
| 1408 | /* index the new node */ |
| 1409 | ebmb_insert_prefix(&expr->pattern_tree, &node->node, len); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1410 | expr->revision = rdtsc(); |
Willy Tarreau | b1dd9bf | 2014-05-10 08:53:48 +0200 | [diff] [blame] | 1411 | |
| 1412 | /* that's ok */ |
| 1413 | return 1; |
| 1414 | } |
| 1415 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1416 | 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] | 1417 | { |
| 1418 | struct pattern_list *pat; |
| 1419 | struct pattern_list *safe; |
| 1420 | |
| 1421 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1422 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1423 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1424 | continue; |
| 1425 | |
| 1426 | /* Delete and free entry. */ |
| 1427 | LIST_DEL(&pat->list); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1428 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1429 | free(pat); |
| 1430 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1431 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1432 | } |
| 1433 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1434 | 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] | 1435 | { |
| 1436 | struct ebmb_node *node, *next_node; |
| 1437 | struct pattern_tree *elt; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1438 | |
| 1439 | /* browse each node of the tree for IPv4 addresses. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1440 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1441 | node; |
| 1442 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1443 | /* Extract container of the tree node. */ |
| 1444 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1445 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1446 | /* Check equality. */ |
| 1447 | if (elt->ref != ref) |
| 1448 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1449 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1450 | /* Delete and free entry. */ |
| 1451 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1452 | free(elt->data); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1453 | free(elt); |
| 1454 | } |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1455 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1456 | /* Browse each node of the list for IPv4 addresses. */ |
| 1457 | pat_del_list_val(expr, ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1458 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1459 | /* browse each node of the tree for IPv6 addresses. */ |
| 1460 | for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL; |
| 1461 | node; |
| 1462 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1463 | /* Extract container of the tree node. */ |
| 1464 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1465 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1466 | /* Check equality. */ |
| 1467 | if (elt->ref != ref) |
| 1468 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1469 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1470 | /* Delete and free entry. */ |
| 1471 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1472 | free(elt->data); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1473 | free(elt); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1474 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1475 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1476 | } |
| 1477 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1478 | 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] | 1479 | { |
| 1480 | struct pattern_list *pat; |
| 1481 | struct pattern_list *safe; |
| 1482 | |
| 1483 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1484 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1485 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1486 | continue; |
| 1487 | |
| 1488 | /* Delete and free entry. */ |
| 1489 | LIST_DEL(&pat->list); |
| 1490 | free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1491 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1492 | free(pat); |
| 1493 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1494 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1495 | } |
| 1496 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1497 | 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] | 1498 | { |
| 1499 | struct ebmb_node *node, *next_node; |
| 1500 | struct pattern_tree *elt; |
| 1501 | |
Thierry FOURNIER | 73bc285 | 2015-02-06 17:53:54 +0100 | [diff] [blame] | 1502 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1503 | if (expr->mflags & PAT_MF_IGNORE_CASE) |
| 1504 | return pat_del_list_ptr(expr, ref); |
| 1505 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1506 | /* browse each node of the tree. */ |
| 1507 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1508 | node; |
| 1509 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1510 | /* Extract container of the tree node. */ |
| 1511 | elt = container_of(node, struct pattern_tree, node); |
| 1512 | |
| 1513 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1514 | if (elt->ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1515 | continue; |
| 1516 | |
| 1517 | /* Delete and free entry. */ |
| 1518 | ebmb_delete(node); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1519 | free(elt->data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1520 | free(elt); |
| 1521 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1522 | expr->revision = rdtsc(); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1525 | 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] | 1526 | { |
| 1527 | struct pattern_list *pat; |
| 1528 | struct pattern_list *safe; |
| 1529 | |
| 1530 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1531 | /* Check equality. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1532 | if (pat->pat.ref != ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1533 | continue; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1534 | |
| 1535 | /* Delete and free entry. */ |
| 1536 | LIST_DEL(&pat->list); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1537 | regex_free(pat->pat.ptr.ptr); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1538 | free(pat->pat.data); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1539 | free(pat); |
| 1540 | } |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1541 | expr->revision = rdtsc(); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | void pattern_init_expr(struct pattern_expr *expr) |
| 1545 | { |
| 1546 | LIST_INIT(&expr->patterns); |
Willy Tarreau | 72f073b | 2015-04-29 17:53:47 +0200 | [diff] [blame] | 1547 | expr->revision = 0; |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1548 | expr->pattern_tree = EB_ROOT; |
| 1549 | expr->pattern_tree_2 = EB_ROOT; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | void pattern_init_head(struct pattern_head *head) |
| 1553 | { |
| 1554 | LIST_INIT(&head->head); |
| 1555 | } |
| 1556 | |
| 1557 | /* The following functions are relative to the management of the reference |
| 1558 | * lists. These lists are used to store the original pattern and associated |
| 1559 | * value as string form. |
| 1560 | * |
| 1561 | * This is used with modifiable ACL and MAPS |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1562 | * |
| 1563 | * The pattern reference are stored with two identifiers: the unique_id and |
| 1564 | * the reference. |
| 1565 | * |
| 1566 | * The reference identify a file. Each file with the same name point to the |
| 1567 | * same reference. We can register many times one file. If the file is modified, |
| 1568 | * all his dependencies are also modified. The reference can be used with map or |
| 1569 | * acl. |
| 1570 | * |
| 1571 | * The unique_id identify inline acl. The unique id is unique for each acl. |
| 1572 | * You cannot force the same id in the configuration file, because this repoort |
| 1573 | * an error. |
| 1574 | * |
| 1575 | * A particular case appears if the filename is a number. In this case, the |
| 1576 | * unique_id is set with the number represented by the filename and the |
| 1577 | * reference is also set. This method prevent double unique_id. |
| 1578 | * |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1579 | */ |
| 1580 | |
| 1581 | /* This function lookup for reference. If the reference is found, they return |
| 1582 | * pointer to the struct pat_ref, else return NULL. |
| 1583 | */ |
| 1584 | struct pat_ref *pat_ref_lookup(const char *reference) |
| 1585 | { |
| 1586 | struct pat_ref *ref; |
| 1587 | |
| 1588 | list_for_each_entry(ref, &pattern_reference, list) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1589 | if (ref->reference && strcmp(reference, ref->reference) == 0) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1590 | return ref; |
| 1591 | return NULL; |
| 1592 | } |
| 1593 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1594 | /* This function lookup for unique id. If the reference is found, they return |
| 1595 | * pointer to the struct pat_ref, else return NULL. |
| 1596 | */ |
| 1597 | struct pat_ref *pat_ref_lookupid(int unique_id) |
| 1598 | { |
| 1599 | struct pat_ref *ref; |
| 1600 | |
| 1601 | list_for_each_entry(ref, &pattern_reference, list) |
| 1602 | if (ref->unique_id == unique_id) |
| 1603 | return ref; |
| 1604 | return NULL; |
| 1605 | } |
| 1606 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1607 | /* This function remove all pattern matching the pointer <refelt> from |
| 1608 | * the the reference and from each expr member of the reference. This |
| 1609 | * function returns 1 if the deletion is done and return 0 is the entry |
| 1610 | * is not found. |
| 1611 | */ |
| 1612 | int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt) |
| 1613 | { |
| 1614 | struct pattern_expr *expr; |
| 1615 | struct pat_ref_elt *elt, *safe; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1616 | struct bref *bref, *back; |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1617 | |
| 1618 | /* delete pattern from reference */ |
| 1619 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1620 | if (elt == refelt) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1621 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 1622 | /* |
| 1623 | * we have to unlink all watchers. We must not relink them if |
| 1624 | * this elt was the last one in the list. |
| 1625 | */ |
| 1626 | LIST_DEL(&bref->users); |
| 1627 | LIST_INIT(&bref->users); |
| 1628 | if (elt->list.n != &ref->head) |
Willy Tarreau | 49ee3b2 | 2019-04-30 11:43:43 +0200 | [diff] [blame] | 1629 | LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1630 | bref->ref = elt->list.n; |
| 1631 | } |
peter cai | aede6dd | 2015-10-07 00:07:43 -0700 | [diff] [blame] | 1632 | list_for_each_entry(expr, &ref->pat, list) |
| 1633 | pattern_delete(expr, elt); |
| 1634 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 1635 | /* pat_ref_elt is trashed once all expr |
| 1636 | are cleaned and there is no ref remaining */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1637 | LIST_DEL(&elt->list); |
| 1638 | free(elt->sample); |
| 1639 | free(elt->pattern); |
| 1640 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1641 | return 1; |
| 1642 | } |
| 1643 | } |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1647 | /* This function remove all pattern match <key> from the the reference |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 1648 | * and from each expr member of the reference. This function returns 1 |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1649 | * if the deletion is done and return 0 is the entry is not found. |
| 1650 | */ |
| 1651 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1652 | { |
| 1653 | struct pattern_expr *expr; |
| 1654 | struct pat_ref_elt *elt, *safe; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1655 | struct bref *bref, *back; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1656 | int found = 0; |
| 1657 | |
| 1658 | /* delete pattern from reference */ |
| 1659 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1660 | if (strcmp(key, elt->pattern) == 0) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1661 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 1662 | /* |
| 1663 | * we have to unlink all watchers. We must not relink them if |
| 1664 | * this elt was the last one in the list. |
| 1665 | */ |
| 1666 | LIST_DEL(&bref->users); |
| 1667 | LIST_INIT(&bref->users); |
| 1668 | if (elt->list.n != &ref->head) |
Willy Tarreau | 49ee3b2 | 2019-04-30 11:43:43 +0200 | [diff] [blame] | 1669 | LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1670 | bref->ref = elt->list.n; |
| 1671 | } |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1672 | list_for_each_entry(expr, &ref->pat, list) |
| 1673 | pattern_delete(expr, elt); |
| 1674 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 1675 | /* pat_ref_elt is trashed once all expr |
| 1676 | are cleaned and there is no ref remaining */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1677 | LIST_DEL(&elt->list); |
| 1678 | free(elt->sample); |
| 1679 | free(elt->pattern); |
| 1680 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1681 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1682 | found = 1; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (!found) |
| 1687 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1688 | return 1; |
| 1689 | } |
| 1690 | |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 1691 | /* |
| 1692 | * find and return an element <elt> matching <key> in a reference <ref> |
| 1693 | * return NULL if not found |
| 1694 | */ |
| 1695 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key) |
| 1696 | { |
| 1697 | struct pat_ref_elt *elt; |
| 1698 | |
| 1699 | list_for_each_entry(elt, &ref->head, list) { |
| 1700 | if (strcmp(key, elt->pattern) == 0) |
| 1701 | return elt; |
| 1702 | } |
| 1703 | |
| 1704 | return NULL; |
| 1705 | } |
| 1706 | |
| 1707 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1708 | /* This function modify the sample of the first pattern that match the <key>. */ |
| 1709 | 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] | 1710 | const char *value, char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1711 | { |
| 1712 | struct pattern_expr *expr; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1713 | struct sample_data **data; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1714 | char *sample; |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 1715 | struct sample_data test; |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1716 | |
| 1717 | /* Try all needed converters. */ |
| 1718 | list_for_each_entry(expr, &ref->pat, list) { |
| 1719 | if (!expr->pat_head->parse_smp) |
| 1720 | continue; |
| 1721 | |
| 1722 | if (!expr->pat_head->parse_smp(value, &test)) { |
| 1723 | memprintf(err, "unable to parse '%s'", value); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1727 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1728 | /* Modify pattern from reference. */ |
| 1729 | sample = strdup(value); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1730 | if (!sample) { |
| 1731 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1732 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1733 | } |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1734 | /* Load sample in each reference. All the conversion are tested |
| 1735 | * below, normally these calls dosn't fail. |
| 1736 | */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1737 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1738 | if (!expr->pat_head->parse_smp) |
| 1739 | continue; |
| 1740 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1741 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1742 | data = pattern_find_smp(expr, elt); |
| 1743 | if (data && *data && !expr->pat_head->parse_smp(sample, *data)) |
| 1744 | *data = NULL; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1745 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 1748 | /* free old sample only when all exprs are updated */ |
| 1749 | free(elt->sample); |
| 1750 | elt->sample = sample; |
| 1751 | |
| 1752 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1753 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1756 | /* 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] | 1757 | 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] | 1758 | { |
| 1759 | struct pat_ref_elt *elt; |
| 1760 | |
| 1761 | /* Look for pattern in the reference. */ |
| 1762 | list_for_each_entry(elt, &ref->head, list) { |
| 1763 | if (elt == refelt) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1764 | if (!pat_ref_set_elt(ref, elt, value, err)) |
| 1765 | return 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1766 | return 1; |
| 1767 | } |
| 1768 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1769 | |
| 1770 | memprintf(err, "key or pattern not found"); |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | /* 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] | 1775 | 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] | 1776 | { |
| 1777 | struct pat_ref_elt *elt; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1778 | int found = 0; |
| 1779 | char *_merr; |
| 1780 | char **merr; |
| 1781 | |
| 1782 | if (err) { |
| 1783 | merr = &_merr; |
| 1784 | *merr = NULL; |
| 1785 | } |
| 1786 | else |
| 1787 | merr = NULL; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1788 | |
| 1789 | /* Look for pattern in the reference. */ |
| 1790 | list_for_each_entry(elt, &ref->head, list) { |
| 1791 | if (strcmp(key, elt->pattern) == 0) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1792 | if (!pat_ref_set_elt(ref, elt, value, merr)) { |
William Lallemand | 579fb25 | 2018-06-11 10:53:46 +0200 | [diff] [blame] | 1793 | if (err && merr) { |
| 1794 | if (!found) { |
| 1795 | *err = *merr; |
| 1796 | } else { |
| 1797 | memprintf(err, "%s, %s", *err, *merr); |
| 1798 | free(*merr); |
| 1799 | *merr = NULL; |
| 1800 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | found = 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1804 | } |
| 1805 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1806 | |
| 1807 | if (!found) { |
| 1808 | memprintf(err, "entry not found"); |
| 1809 | return 0; |
| 1810 | } |
| 1811 | return 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1812 | } |
| 1813 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 1814 | /* This function creates a new reference. <ref> is the reference name. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1815 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1816 | * be unique. The user must check the reference with "pat_ref_lookup()" |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 1817 | * before calling this function. If the function fail, it return NULL, |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1818 | * else return new struct pat_ref. |
| 1819 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 1822 | struct pat_ref *ref; |
| 1823 | |
| 1824 | ref = malloc(sizeof(*ref)); |
| 1825 | if (!ref) |
| 1826 | return NULL; |
| 1827 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1828 | if (display) { |
| 1829 | ref->display = strdup(display); |
| 1830 | if (!ref->display) { |
| 1831 | free(ref); |
| 1832 | return NULL; |
| 1833 | } |
| 1834 | } |
| 1835 | else |
| 1836 | ref->display = NULL; |
| 1837 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1838 | ref->reference = strdup(reference); |
| 1839 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1840 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1841 | free(ref); |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1845 | ref->flags = flags; |
| 1846 | ref->unique_id = -1; |
| 1847 | |
| 1848 | LIST_INIT(&ref->head); |
| 1849 | LIST_INIT(&ref->pat); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1850 | HA_SPIN_INIT(&ref->lock); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1851 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1852 | |
| 1853 | return ref; |
| 1854 | } |
| 1855 | |
| 1856 | /* This function create new reference. <unique_id> is the unique id. If |
| 1857 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1858 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1859 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1860 | * or pat_ref_lookupid before calling this function. If the function |
| 1861 | * fail, it return NULL, else return new struct pat_ref. |
| 1862 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1863 | 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] | 1864 | { |
| 1865 | struct pat_ref *ref; |
| 1866 | |
| 1867 | ref = malloc(sizeof(*ref)); |
| 1868 | if (!ref) |
| 1869 | return NULL; |
| 1870 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1871 | if (display) { |
| 1872 | ref->display = strdup(display); |
| 1873 | if (!ref->display) { |
| 1874 | free(ref); |
| 1875 | return NULL; |
| 1876 | } |
| 1877 | } |
| 1878 | else |
| 1879 | ref->display = NULL; |
| 1880 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1881 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1882 | ref->flags = flags; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1883 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1884 | LIST_INIT(&ref->head); |
| 1885 | LIST_INIT(&ref->pat); |
Aurélien Nephtali | 564d15a | 2018-04-19 16:56:07 +0200 | [diff] [blame] | 1886 | HA_SPIN_INIT(&ref->lock); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1887 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1888 | |
| 1889 | return ref; |
| 1890 | } |
| 1891 | |
| 1892 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1893 | * If the function fails, it returns 0. |
| 1894 | */ |
| 1895 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line) |
| 1896 | { |
| 1897 | struct pat_ref_elt *elt; |
| 1898 | |
| 1899 | elt = malloc(sizeof(*elt)); |
| 1900 | if (!elt) |
| 1901 | return 0; |
| 1902 | |
| 1903 | elt->line = line; |
| 1904 | |
| 1905 | elt->pattern = strdup(pattern); |
| 1906 | if (!elt->pattern) { |
| 1907 | free(elt); |
| 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | if (sample) { |
| 1912 | elt->sample = strdup(sample); |
| 1913 | if (!elt->sample) { |
| 1914 | free(elt->pattern); |
| 1915 | free(elt); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | } |
| 1919 | else |
| 1920 | elt->sample = NULL; |
| 1921 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 1922 | LIST_INIT(&elt->back_refs); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1923 | LIST_ADDQ(&ref->head, &elt->list); |
| 1924 | |
| 1925 | return 1; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1926 | } |
| 1927 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1928 | /* This function create sample found in <elt>, parse the pattern also |
| 1929 | * found in <elt> and insert it in <expr>. The function copy <patflags> |
| 1930 | * in <expr>. If the function fails, it returns0 and <err> is filled. |
| 1931 | * In succes case, the function returns 1. |
| 1932 | */ |
| 1933 | static inline |
| 1934 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1935 | int patflags, char **err) |
| 1936 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1937 | struct sample_data *data; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1938 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1939 | |
| 1940 | /* Create sample */ |
| 1941 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1942 | /* New sample. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1943 | data = malloc(sizeof(*data)); |
| 1944 | if (!data) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1945 | return 0; |
| 1946 | |
| 1947 | /* Parse value. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1948 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1949 | memprintf(err, "unable to parse '%s'", elt->sample); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1950 | free(data); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1951 | return 0; |
| 1952 | } |
| 1953 | |
| 1954 | } |
| 1955 | else |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1956 | data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1957 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1958 | /* initialise pattern */ |
| 1959 | memset(&pattern, 0, sizeof(pattern)); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1960 | pattern.data = data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1961 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1962 | |
| 1963 | /* parse pattern */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1964 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1965 | free(data); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
| 1968 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1969 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1970 | /* index pattern */ |
| 1971 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1972 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1973 | free(data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1974 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1975 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1976 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1977 | |
| 1978 | return 1; |
| 1979 | } |
| 1980 | |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1981 | /* This function adds entry to <ref>. It can failed with memory error. The new |
| 1982 | * entry is added at all the pattern_expr registered in this reference. The |
| 1983 | * function stop on the first error encountered. It returns 0 and err is |
| 1984 | * filled. If an error is encountered, the complete add operation is cancelled. |
| 1985 | * If the insertion is a success the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1986 | */ |
| 1987 | int pat_ref_add(struct pat_ref *ref, |
| 1988 | const char *pattern, const char *sample, |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1989 | char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1990 | { |
| 1991 | struct pat_ref_elt *elt; |
| 1992 | struct pattern_expr *expr; |
| 1993 | |
| 1994 | elt = malloc(sizeof(*elt)); |
| 1995 | if (!elt) { |
| 1996 | memprintf(err, "out of memory error"); |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | elt->line = -1; |
| 2001 | |
| 2002 | elt->pattern = strdup(pattern); |
| 2003 | if (!elt->pattern) { |
| 2004 | free(elt); |
| 2005 | memprintf(err, "out of memory error"); |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | if (sample) { |
| 2010 | elt->sample = strdup(sample); |
| 2011 | if (!elt->sample) { |
| 2012 | free(elt->pattern); |
| 2013 | free(elt); |
| 2014 | memprintf(err, "out of memory error"); |
| 2015 | return 0; |
| 2016 | } |
| 2017 | } |
| 2018 | else |
| 2019 | elt->sample = NULL; |
| 2020 | |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2021 | LIST_INIT(&elt->back_refs); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2022 | LIST_ADDQ(&ref->head, &elt->list); |
| 2023 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2024 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2025 | if (!pat_ref_push(elt, expr, 0, err)) { |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 2026 | /* If the insertion fails, try to delete all the added entries. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2027 | pat_ref_delete_by_id(ref, elt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2028 | return 0; |
| 2029 | } |
| 2030 | } |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2031 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2032 | return 1; |
| 2033 | } |
| 2034 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2035 | /* This function prunes <ref>, replaces all references by the references |
| 2036 | * of <replace>, and reindexes all the news values. |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2037 | * |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2038 | * The patterns are loaded in best effort and the errors are ignored, |
| 2039 | * but written in the logs. |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2040 | */ |
| 2041 | void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace) |
| 2042 | { |
| 2043 | struct pattern_expr *expr; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2044 | struct pat_ref_elt *elt, *safe; |
| 2045 | struct bref *bref, *back; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2046 | struct pattern pattern; |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2047 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2048 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2049 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2050 | list_for_each_entry(expr, &ref->pat, list) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2051 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* all expr are locked, we can safely remove all pat_ref */ |
| 2055 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 2056 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 2057 | /* |
| 2058 | * we have to unlink all watchers. We must not relink them if |
| 2059 | * this elt was the last one in the list. |
| 2060 | */ |
| 2061 | LIST_DEL(&bref->users); |
| 2062 | LIST_INIT(&bref->users); |
| 2063 | if (elt->list.n != &ref->head) |
Willy Tarreau | 49ee3b2 | 2019-04-30 11:43:43 +0200 | [diff] [blame] | 2064 | LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2065 | bref->ref = elt->list.n; |
| 2066 | } |
| 2067 | LIST_DEL(&elt->list); |
| 2068 | free(elt->pattern); |
| 2069 | free(elt->sample); |
| 2070 | free(elt); |
| 2071 | } |
| 2072 | |
| 2073 | /* switch pat_ret_elt lists */ |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2074 | LIST_ADD(&replace->head, &ref->head); |
| 2075 | LIST_DEL(&replace->head); |
| 2076 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2077 | list_for_each_entry(expr, &ref->pat, list) { |
| 2078 | expr->pat_head->prune(expr); |
| 2079 | list_for_each_entry(elt, &ref->head, list) { |
Dragan Dosen | f147479 | 2018-09-18 20:18:09 +0200 | [diff] [blame] | 2080 | char *err = NULL; |
| 2081 | struct sample_data *data = NULL; |
| 2082 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2083 | /* Create sample */ |
| 2084 | if (elt->sample && expr->pat_head->parse_smp) { |
| 2085 | /* New sample. */ |
| 2086 | data = malloc(sizeof(*data)); |
| 2087 | if (!data) |
| 2088 | continue; |
| 2089 | |
| 2090 | /* Parse value. */ |
| 2091 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
| 2092 | memprintf(&err, "unable to parse '%s'", elt->sample); |
| 2093 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 2094 | free(err); |
| 2095 | free(data); |
| 2096 | continue; |
| 2097 | } |
| 2098 | |
| 2099 | } |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* initialise pattern */ |
| 2102 | memset(&pattern, 0, sizeof(pattern)); |
| 2103 | pattern.data = data; |
| 2104 | pattern.ref = elt; |
| 2105 | |
| 2106 | /* parse pattern */ |
| 2107 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, &err)) { |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2108 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 2109 | free(err); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2110 | free(data); |
| 2111 | continue; |
| 2112 | } |
| 2113 | |
| 2114 | /* index pattern */ |
| 2115 | if (!expr->pat_head->index(expr, &pattern, &err)) { |
| 2116 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 2117 | free(err); |
| 2118 | free(data); |
| 2119 | continue; |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2120 | } |
| 2121 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2122 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2123 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2124 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2127 | /* This function prune all entries of <ref>. This function |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2128 | * prunes the associated pattern_expr. It may return before the end of |
| 2129 | * the list is reached, returning 0, to yield. The caller must call it |
| 2130 | * again. Otherwise it returns 1 once done. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2131 | */ |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2132 | int pat_ref_prune(struct pat_ref *ref) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2133 | { |
| 2134 | struct pat_ref_elt *elt, *safe; |
| 2135 | struct pattern_expr *expr; |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2136 | struct bref *bref, *back; |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2137 | int loops = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2138 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2139 | list_for_each_entry(expr, &ref->pat, list) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2140 | HA_RWLOCK_WRLOCK(PATEXP_LOCK, &expr->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2141 | expr->pat_head->prune(expr); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2142 | HA_RWLOCK_WRUNLOCK(PATEXP_LOCK, &expr->lock); |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2143 | loops++; |
| 2144 | /* yield often, some lists may be huge, especially those |
| 2145 | * having to be freed through free_pattern_tree() |
| 2146 | */ |
| 2147 | if (loops > 10) |
| 2148 | return 0; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | /* we trash pat_ref_elt in a second time to ensure that data is |
| 2152 | free once there is no ref on it */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2153 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2154 | list_for_each_entry_safe(bref, back, &elt->back_refs, users) { |
| 2155 | /* |
| 2156 | * we have to unlink all watchers. We must not relink them if |
| 2157 | * this elt was the last one in the list. |
| 2158 | */ |
| 2159 | LIST_DEL(&bref->users); |
| 2160 | LIST_INIT(&bref->users); |
| 2161 | if (elt->list.n != &ref->head) |
Willy Tarreau | 49ee3b2 | 2019-04-30 11:43:43 +0200 | [diff] [blame] | 2162 | LIST_ADDQ(&LIST_ELEM(elt->list.n, typeof(elt), list)->back_refs, &bref->users); |
Emeric Brun | 8d85aa4 | 2017-06-29 15:40:33 +0200 | [diff] [blame] | 2163 | bref->ref = elt->list.n; |
| 2164 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2165 | LIST_DEL(&elt->list); |
| 2166 | free(elt->pattern); |
| 2167 | free(elt->sample); |
| 2168 | free(elt); |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2169 | loops++; |
| 2170 | if (loops > 100000) |
| 2171 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2172 | } |
Willy Tarreau | b88a37c | 2019-12-20 18:22:02 +0100 | [diff] [blame] | 2173 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2176 | /* This function lookup for existing reference <ref> in pattern_head <head>. */ |
| 2177 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 2178 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2179 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2180 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2181 | list_for_each_entry(expr, &head->head, list) |
| 2182 | if (expr->expr->ref == ref) |
| 2183 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2184 | return NULL; |
| 2185 | } |
| 2186 | |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2187 | /* This function creates new pattern_expr associated to the reference <ref>. |
| 2188 | * <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] | 2189 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 2190 | * with <head> and <ref>. |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2191 | * |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2192 | * 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] | 2193 | * flag <reuse> is set. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2194 | */ |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2195 | 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] | 2196 | int patflags, char **err, int *reuse) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2197 | { |
| 2198 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2199 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2200 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2201 | if (reuse) |
| 2202 | *reuse = 0; |
| 2203 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2204 | /* Memory and initialization of the chain element. */ |
| 2205 | list = malloc(sizeof(*list)); |
| 2206 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2207 | memprintf(err, "out of memory"); |
| 2208 | return NULL; |
| 2209 | } |
| 2210 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2211 | /* Look for existing similar expr. No that only the index, parse and |
| 2212 | * parse_smp function must be identical for having similar pattern. |
Joseph Herlant | 4189d67 | 2018-11-15 10:22:31 -0800 | [diff] [blame] | 2213 | * The other function depends of these first. |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2214 | */ |
| 2215 | if (ref) { |
| 2216 | list_for_each_entry(expr, &ref->pat, list) |
| 2217 | if (expr->pat_head->index == head->index && |
| 2218 | expr->pat_head->parse == head->parse && |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2219 | expr->pat_head->parse_smp == head->parse_smp && |
| 2220 | expr->mflags == patflags) |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2221 | break; |
| 2222 | if (&expr->list == &ref->pat) |
| 2223 | expr = NULL; |
| 2224 | } |
| 2225 | else |
| 2226 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2227 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2228 | /* If no similar expr was found, we create new expr. */ |
| 2229 | if (!expr) { |
| 2230 | /* Get a lot of memory for the expr struct. */ |
| 2231 | expr = malloc(sizeof(*expr)); |
| 2232 | if (!expr) { |
Andreas Seltenreich | e6e22e8 | 2016-03-03 20:20:23 +0100 | [diff] [blame] | 2233 | free(list); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2234 | memprintf(err, "out of memory"); |
| 2235 | return NULL; |
| 2236 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2237 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2238 | /* Initialize this new expr. */ |
| 2239 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2240 | |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2241 | /* Copy the pattern matching and indexing flags. */ |
| 2242 | expr->mflags = patflags; |
| 2243 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2244 | /* This new pattern expression reference one of his heads. */ |
| 2245 | expr->pat_head = head; |
| 2246 | |
| 2247 | /* Link with ref, or to self to facilitate LIST_DEL() */ |
| 2248 | if (ref) |
| 2249 | LIST_ADDQ(&ref->pat, &expr->list); |
| 2250 | else |
| 2251 | LIST_INIT(&expr->list); |
| 2252 | |
| 2253 | expr->ref = ref; |
| 2254 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2255 | HA_RWLOCK_INIT(&expr->lock); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2256 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2257 | /* We must free this pattern if it is no more used. */ |
| 2258 | list->do_free = 1; |
| 2259 | } |
| 2260 | else { |
| 2261 | /* If the pattern used already exists, it is already linked |
| 2262 | * with ref and we must not free it. |
| 2263 | */ |
| 2264 | list->do_free = 0; |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2265 | if (reuse) |
| 2266 | *reuse = 1; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* The new list element reference the pattern_expr. */ |
| 2270 | list->expr = expr; |
| 2271 | |
| 2272 | /* Link the list element with the pattern_head. */ |
| 2273 | LIST_ADDQ(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2274 | return expr; |
| 2275 | } |
| 2276 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2277 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2278 | * 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] | 2279 | * |
| 2280 | * The file contains one key + value per line. Lines which start with '#' are |
| 2281 | * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is |
| 2282 | * then the first "word" (series of non-space/tabs characters), and the value is |
| 2283 | * what follows this series of space/tab till the end of the line excluding |
| 2284 | * trailing spaces/tabs. |
| 2285 | * |
| 2286 | * Example : |
| 2287 | * |
| 2288 | * # this is a comment and is ignored |
| 2289 | * 62.212.114.60 1wt.eu \n |
| 2290 | * <-><-----------><---><----><----> |
| 2291 | * | | | | `--- trailing spaces ignored |
| 2292 | * | | | `-------- value |
| 2293 | * | | `--------------- middle spaces ignored |
| 2294 | * | `------------------------ key |
| 2295 | * `-------------------------------- leading spaces ignored |
| 2296 | * |
| 2297 | * Return non-zero in case of succes, otherwise 0. |
| 2298 | */ |
| 2299 | int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err) |
| 2300 | { |
| 2301 | FILE *file; |
| 2302 | char *c; |
| 2303 | int ret = 0; |
| 2304 | int line = 0; |
| 2305 | char *key_beg; |
| 2306 | char *key_end; |
| 2307 | char *value_beg; |
| 2308 | char *value_end; |
| 2309 | |
| 2310 | file = fopen(filename, "r"); |
| 2311 | if (!file) { |
| 2312 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | /* now parse all patterns. The file may contain only one pattern |
| 2317 | * followed by one value per line. The start spaces, separator spaces |
| 2318 | * and and spaces are stripped. Each can contain comment started by '#' |
| 2319 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2320 | while (fgets(trash.area, trash.size, file) != NULL) { |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2321 | line++; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2322 | c = trash.area; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2323 | |
| 2324 | /* ignore lines beginning with a dash */ |
| 2325 | if (*c == '#') |
| 2326 | continue; |
| 2327 | |
| 2328 | /* strip leading spaces and tabs */ |
| 2329 | while (*c == ' ' || *c == '\t') |
| 2330 | c++; |
| 2331 | |
| 2332 | /* empty lines are ignored too */ |
| 2333 | if (*c == '\0' || *c == '\r' || *c == '\n') |
| 2334 | continue; |
| 2335 | |
| 2336 | /* look for the end of the key */ |
| 2337 | key_beg = c; |
| 2338 | while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2339 | c++; |
| 2340 | |
| 2341 | key_end = c; |
| 2342 | |
| 2343 | /* strip middle spaces and tabs */ |
| 2344 | while (*c == ' ' || *c == '\t') |
| 2345 | c++; |
| 2346 | |
| 2347 | /* look for the end of the value, it is the end of the line */ |
| 2348 | value_beg = c; |
| 2349 | while (*c && *c != '\n' && *c != '\r') |
| 2350 | c++; |
| 2351 | value_end = c; |
| 2352 | |
| 2353 | /* trim possibly trailing spaces and tabs */ |
| 2354 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2355 | value_end--; |
| 2356 | |
| 2357 | /* set final \0 and check entries */ |
| 2358 | *key_end = '\0'; |
| 2359 | *value_end = '\0'; |
| 2360 | |
| 2361 | /* insert values */ |
| 2362 | if (!pat_ref_append(ref, key_beg, value_beg, line)) { |
| 2363 | memprintf(err, "out of memory"); |
| 2364 | goto out_close; |
| 2365 | } |
| 2366 | } |
| 2367 | |
Jerome Magnin | 8991ce1 | 2020-01-17 16:09:33 +0100 | [diff] [blame] | 2368 | if (ferror(file)) { |
| 2369 | memprintf(err, "error encountered while reading <%s> : %s", |
| 2370 | filename, strerror(errno)); |
| 2371 | goto out_close; |
| 2372 | } |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2373 | /* succes */ |
| 2374 | ret = 1; |
| 2375 | |
| 2376 | out_close: |
| 2377 | fclose(file); |
| 2378 | return ret; |
| 2379 | } |
| 2380 | |
| 2381 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2382 | * 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] | 2383 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2384 | 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] | 2385 | { |
| 2386 | FILE *file; |
| 2387 | char *c; |
| 2388 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2389 | int ret = 0; |
| 2390 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2391 | |
| 2392 | file = fopen(filename, "r"); |
| 2393 | if (!file) { |
| 2394 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
| 2398 | /* now parse all patterns. The file may contain only one pattern per |
| 2399 | * line. If the line contains spaces, they will be part of the pattern. |
| 2400 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2401 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2402 | while (fgets(trash.area, trash.size, file) != NULL) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2403 | line++; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2404 | c = trash.area; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2405 | |
| 2406 | /* ignore lines beginning with a dash */ |
| 2407 | if (*c == '#') |
| 2408 | continue; |
| 2409 | |
| 2410 | /* strip leading spaces and tabs */ |
| 2411 | while (*c == ' ' || *c == '\t') |
| 2412 | c++; |
| 2413 | |
| 2414 | |
| 2415 | arg = c; |
| 2416 | while (*c && *c != '\n' && *c != '\r') |
| 2417 | c++; |
| 2418 | *c = 0; |
| 2419 | |
| 2420 | /* empty lines are ignored too */ |
| 2421 | if (c == arg) |
| 2422 | continue; |
| 2423 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2424 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2425 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2426 | goto out_close; |
| 2427 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
Jerome Magnin | 8991ce1 | 2020-01-17 16:09:33 +0100 | [diff] [blame] | 2430 | if (ferror(file)) { |
| 2431 | memprintf(err, "error encountered while reading <%s> : %s", |
| 2432 | filename, strerror(errno)); |
| 2433 | goto out_close; |
| 2434 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2435 | ret = 1; /* success */ |
| 2436 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2437 | out_close: |
| 2438 | fclose(file); |
| 2439 | return ret; |
| 2440 | } |
| 2441 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2442 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2443 | const char *filename, int patflags, int load_smp, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2444 | char **err, const char *file, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2445 | { |
| 2446 | struct pat_ref *ref; |
| 2447 | struct pattern_expr *expr; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2448 | struct pat_ref_elt *elt; |
Willy Tarreau | 4deaf39 | 2014-11-26 13:17:03 +0100 | [diff] [blame] | 2449 | int reuse = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2450 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2451 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2452 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2453 | |
| 2454 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2455 | if (!ref) { |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2456 | chunk_printf(&trash, |
| 2457 | "pattern loaded from file '%s' used by %s at file '%s' line %d", |
| 2458 | filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2459 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2460 | ref = pat_ref_new(filename, trash.area, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2461 | if (!ref) { |
| 2462 | memprintf(err, "out of memory"); |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2466 | if (load_smp) { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2467 | ref->flags |= PAT_REF_SMP; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2468 | if (!pat_ref_read_from_file_smp(ref, filename, err)) |
| 2469 | return 0; |
| 2470 | } |
| 2471 | else { |
| 2472 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2473 | return 0; |
| 2474 | } |
| 2475 | } |
| 2476 | else { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2477 | /* The reference already exists, check the map compatibility. */ |
| 2478 | |
| 2479 | /* If the load require samples and the flag PAT_REF_SMP is not set, |
| 2480 | * the reference doesn't contain sample, and cannot be used. |
| 2481 | */ |
| 2482 | if (load_smp) { |
| 2483 | if (!(ref->flags & PAT_REF_SMP)) { |
| 2484 | memprintf(err, "The file \"%s\" is already used as one column file " |
| 2485 | "and cannot be used by as two column file.", |
| 2486 | filename); |
| 2487 | return 0; |
| 2488 | } |
| 2489 | } |
| 2490 | else { |
| 2491 | /* The load doesn't require samples. If the flag PAT_REF_SMP is |
| 2492 | * set, the reference contains a sample, and cannot be used. |
| 2493 | */ |
| 2494 | if (ref->flags & PAT_REF_SMP) { |
| 2495 | memprintf(err, "The file \"%s\" is already used as two column file " |
| 2496 | "and cannot be used by as one column file.", |
| 2497 | filename); |
| 2498 | return 0; |
| 2499 | } |
| 2500 | } |
| 2501 | |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2502 | /* Extends display */ |
| 2503 | chunk_printf(&trash, "%s", ref->display); |
| 2504 | chunk_appendf(&trash, ", by %s at file '%s' line %d", |
| 2505 | refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2506 | free(ref->display); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2507 | ref->display = strdup(trash.area); |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2508 | if (!ref->display) { |
| 2509 | memprintf(err, "out of memory"); |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2513 | /* Merge flags. */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2514 | ref->flags |= refflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* Now, we can loading patterns from the reference. */ |
| 2518 | |
| 2519 | /* Lookup for existing reference in the head. If the reference |
| 2520 | * doesn't exists, create it. |
| 2521 | */ |
| 2522 | expr = pattern_lookup_expr(head, ref); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2523 | if (!expr || (expr->mflags != patflags)) { |
Emeric Brun | 7d27f3c | 2017-07-03 17:54:23 +0200 | [diff] [blame] | 2524 | expr = pattern_new_expr(head, ref, patflags, err, &reuse); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2525 | if (!expr) |
| 2526 | return 0; |
| 2527 | } |
| 2528 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2529 | /* The returned expression may be not empty, because the function |
| 2530 | * "pattern_new_expr" lookup for similar pattern list and can |
| 2531 | * reuse a already filled pattern list. In this case, we can not |
| 2532 | * reload the patterns. |
| 2533 | */ |
| 2534 | if (reuse) |
| 2535 | return 1; |
| 2536 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2537 | /* Load reference content in the pattern expression. */ |
| 2538 | list_for_each_entry(elt, &ref->head, list) { |
| 2539 | if (!pat_ref_push(elt, expr, patflags, err)) { |
| 2540 | if (elt->line > 0) |
| 2541 | memprintf(err, "%s at line %d of file '%s'", |
| 2542 | *err, elt->line, filename); |
| 2543 | return 0; |
| 2544 | } |
| 2545 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2546 | |
| 2547 | return 1; |
| 2548 | } |
| 2549 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2550 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 2551 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 2552 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2553 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2554 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2555 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2556 | 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] | 2557 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2558 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2559 | struct pattern *pat; |
| 2560 | |
| 2561 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2562 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2563 | static_pattern.data = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2564 | static_pattern.ref = NULL; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2565 | static_pattern.sflags = 0; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2566 | static_pattern.type = SMP_T_SINT; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2567 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2568 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2569 | return &static_pattern; |
| 2570 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2571 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2572 | /* convert input to string */ |
| 2573 | if (!sample_convert(smp, head->expect_type)) |
| 2574 | return NULL; |
| 2575 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2576 | list_for_each_entry(list, &head->head, list) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2577 | HA_RWLOCK_RDLOCK(PATEXP_LOCK, &list->expr->lock); |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2578 | pat = head->match(smp, list->expr, fill); |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2579 | if (pat) { |
| 2580 | /* We duplicate the pattern cause it could be modified |
| 2581 | by another thread */ |
| 2582 | if (pat != &static_pattern) { |
| 2583 | memcpy(&static_pattern, pat, sizeof(struct pattern)); |
| 2584 | pat = &static_pattern; |
| 2585 | } |
| 2586 | |
| 2587 | /* We also duplicate the sample data for |
| 2588 | same reason */ |
| 2589 | if (pat->data && (pat->data != &static_sample_data)) { |
Christopher Faulet | 09fdf4b | 2017-11-09 16:14:16 +0100 | [diff] [blame] | 2590 | switch(pat->data->type) { |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2591 | case SMP_T_STR: |
| 2592 | static_sample_data.type = SMP_T_STR; |
| 2593 | static_sample_data.u.str = *get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2594 | static_sample_data.u.str.data = pat->data->u.str.data; |
| 2595 | if (static_sample_data.u.str.data >= static_sample_data.u.str.size) |
| 2596 | static_sample_data.u.str.data = static_sample_data.u.str.size - 1; |
| 2597 | memcpy(static_sample_data.u.str.area, |
Willy Tarreau | 45b2d9a | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2598 | pat->data->u.str.area, static_sample_data.u.str.data); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2599 | static_sample_data.u.str.area[static_sample_data.u.str.data] = 0; |
Willy Tarreau | 45b2d9a | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2600 | pat->data = &static_sample_data; |
| 2601 | break; |
| 2602 | |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2603 | case SMP_T_IPV4: |
| 2604 | case SMP_T_IPV6: |
| 2605 | case SMP_T_SINT: |
| 2606 | memcpy(&static_sample_data, pat->data, sizeof(struct sample_data)); |
Willy Tarreau | 45b2d9a | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2607 | pat->data = &static_sample_data; |
| 2608 | break; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2609 | default: |
Willy Tarreau | 45b2d9a | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2610 | /* unimplemented pattern type */ |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2611 | pat->data = NULL; |
Willy Tarreau | 45b2d9a | 2020-06-11 16:37:35 +0200 | [diff] [blame] | 2612 | break; |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 2613 | } |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 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 | |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2687 | /* This function compares two pat_ref** on unique_id */ |
| 2688 | static int cmp_pat_ref(const void *_a, const void *_b) |
| 2689 | { |
| 2690 | struct pat_ref * const *a = _a; |
| 2691 | struct pat_ref * const *b = _b; |
| 2692 | |
| 2693 | if ((*a)->unique_id < (*b)->unique_id) |
| 2694 | return -1; |
| 2695 | else if ((*a)->unique_id > (*b)->unique_id) |
| 2696 | return 1; |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | /* This function finalize the configuration parsing. It sets all the |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2701 | * automatic ids |
| 2702 | */ |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2703 | int pattern_finalize_config(void) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2704 | { |
Tim Duesterhus | d5a8b9d | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2705 | size_t len = 0; |
| 2706 | size_t unassigned_pos = 0; |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2707 | int next_unique_id = 0; |
Tim Duesterhus | d5a8b9d | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2708 | size_t i, j; |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2709 | struct pat_ref *ref, **arr; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2710 | struct list pr = LIST_HEAD_INIT(pr); |
| 2711 | |
Willy Tarreau | 861c4ef | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 2712 | pat_lru_seed = ha_random(); |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 2713 | |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2714 | /* Count pat_refs with user defined unique_id and totalt count */ |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2715 | list_for_each_entry(ref, &pattern_reference, list) { |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2716 | len++; |
| 2717 | if (ref->unique_id != -1) |
| 2718 | unassigned_pos++; |
| 2719 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2720 | |
Tim Duesterhus | d5a8b9d | 2020-03-17 21:08:24 +0100 | [diff] [blame] | 2721 | if (len == 0) { |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2725 | arr = calloc(len, sizeof(*arr)); |
| 2726 | if (arr == NULL) { |
| 2727 | ha_alert("Out of memory error.\n"); |
| 2728 | return ERR_ALERT | ERR_FATAL; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2731 | i = 0; |
| 2732 | j = unassigned_pos; |
| 2733 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2734 | if (ref->unique_id != -1) |
| 2735 | arr[i++] = ref; |
| 2736 | else |
| 2737 | arr[j++] = ref; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2738 | } |
| 2739 | |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2740 | /* Sort first segment of array with user-defined unique ids for |
| 2741 | * fast lookup when generating unique ids |
| 2742 | */ |
| 2743 | qsort(arr, unassigned_pos, sizeof(*arr), cmp_pat_ref); |
| 2744 | |
| 2745 | /* Assign unique ids to the rest of the elements */ |
| 2746 | for (i = unassigned_pos; i < len; i++) { |
| 2747 | do { |
| 2748 | arr[i]->unique_id = next_unique_id++; |
| 2749 | } while (bsearch(&arr[i], arr, unassigned_pos, sizeof(*arr), cmp_pat_ref)); |
| 2750 | } |
| 2751 | |
| 2752 | /* Sort complete array */ |
| 2753 | qsort(arr, len, sizeof(*arr), cmp_pat_ref); |
| 2754 | |
| 2755 | /* Convert back to linked list */ |
| 2756 | for (i = 0; i < len; i++) |
| 2757 | LIST_ADDQ(&pr, &arr[i]->list); |
| 2758 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2759 | /* swap root */ |
| 2760 | LIST_ADD(&pr, &pattern_reference); |
| 2761 | LIST_DEL(&pr); |
Carl Henrik Lunde | 4944c8c | 2020-02-27 16:45:50 +0100 | [diff] [blame] | 2762 | |
| 2763 | free(arr); |
| 2764 | return 0; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2765 | } |
Willy Tarreau | 7fdd81c | 2019-10-23 06:59:31 +0200 | [diff] [blame] | 2766 | |
| 2767 | static int pattern_per_thread_lru_alloc() |
| 2768 | { |
| 2769 | if (!global.tune.pattern_cache) |
| 2770 | return 1; |
| 2771 | pat_lru_tree = lru64_new(global.tune.pattern_cache); |
| 2772 | return !!pat_lru_tree; |
| 2773 | } |
| 2774 | |
| 2775 | static void pattern_per_thread_lru_free() |
| 2776 | { |
| 2777 | lru64_destroy(pat_lru_tree); |
| 2778 | } |
| 2779 | |
| 2780 | REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc); |
| 2781 | REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free); |