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