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