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