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