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