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) { |
peter cai | aede6dd | 2015-10-07 00:07:43 -0700 | [diff] [blame] | 1543 | list_for_each_entry(expr, &ref->pat, list) |
| 1544 | pattern_delete(expr, elt); |
| 1545 | |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1546 | LIST_DEL(&elt->list); |
| 1547 | free(elt->sample); |
| 1548 | free(elt->pattern); |
| 1549 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1550 | return 1; |
| 1551 | } |
| 1552 | } |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1556 | /* This function remove all pattern match <key> from the the reference |
| 1557 | * and from each expr member of the reference. This fucntion returns 1 |
| 1558 | * if the deletion is done and return 0 is the entry is not found. |
| 1559 | */ |
| 1560 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1561 | { |
| 1562 | struct pattern_expr *expr; |
| 1563 | struct pat_ref_elt *elt, *safe; |
| 1564 | int found = 0; |
| 1565 | |
| 1566 | /* delete pattern from reference */ |
| 1567 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1568 | if (strcmp(key, elt->pattern) == 0) { |
Dirkjan Bussink | 07fcaaa | 2014-04-28 22:57:16 +0000 | [diff] [blame] | 1569 | list_for_each_entry(expr, &ref->pat, list) |
| 1570 | pattern_delete(expr, elt); |
| 1571 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1572 | LIST_DEL(&elt->list); |
| 1573 | free(elt->sample); |
| 1574 | free(elt->pattern); |
| 1575 | free(elt); |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1576 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1577 | found = 1; |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | if (!found) |
| 1582 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1583 | return 1; |
| 1584 | } |
| 1585 | |
Baptiste Assmann | 953f74d | 2014-04-25 16:57:03 +0200 | [diff] [blame] | 1586 | /* |
| 1587 | * find and return an element <elt> matching <key> in a reference <ref> |
| 1588 | * return NULL if not found |
| 1589 | */ |
| 1590 | struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key) |
| 1591 | { |
| 1592 | struct pat_ref_elt *elt; |
| 1593 | |
| 1594 | list_for_each_entry(elt, &ref->head, list) { |
| 1595 | if (strcmp(key, elt->pattern) == 0) |
| 1596 | return elt; |
| 1597 | } |
| 1598 | |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | |
| 1602 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1603 | /* This function modify the sample of the first pattern that match the <key>. */ |
| 1604 | 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] | 1605 | const char *value, char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1606 | { |
| 1607 | struct pattern_expr *expr; |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1608 | struct sample_data **data; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1609 | char *sample; |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 1610 | struct sample_data test; |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1611 | |
| 1612 | /* Try all needed converters. */ |
| 1613 | list_for_each_entry(expr, &ref->pat, list) { |
| 1614 | if (!expr->pat_head->parse_smp) |
| 1615 | continue; |
| 1616 | |
| 1617 | if (!expr->pat_head->parse_smp(value, &test)) { |
| 1618 | memprintf(err, "unable to parse '%s'", value); |
| 1619 | return 0; |
| 1620 | } |
| 1621 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1622 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1623 | /* Modify pattern from reference. */ |
| 1624 | sample = strdup(value); |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1625 | if (!sample) { |
| 1626 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1627 | return 0; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1628 | } |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1629 | free(elt->sample); |
| 1630 | elt->sample = sample; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1631 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1632 | /* Load sample in each reference. All the conversion are tested |
| 1633 | * below, normally these calls dosn't fail. |
| 1634 | */ |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1635 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1636 | if (!expr->pat_head->parse_smp) |
| 1637 | continue; |
| 1638 | |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1639 | data = pattern_find_smp(expr, elt); |
| 1640 | if (data && *data && !expr->pat_head->parse_smp(sample, *data)) |
| 1641 | *data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
Thierry FOURNIER | 149e0fe | 2014-01-29 19:35:06 +0100 | [diff] [blame] | 1644 | return 1; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1647 | /* 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] | 1648 | 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] | 1649 | { |
| 1650 | struct pat_ref_elt *elt; |
| 1651 | |
| 1652 | /* Look for pattern in the reference. */ |
| 1653 | list_for_each_entry(elt, &ref->head, list) { |
| 1654 | if (elt == refelt) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1655 | if (!pat_ref_set_elt(ref, elt, value, err)) |
| 1656 | return 0; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1657 | return 1; |
| 1658 | } |
| 1659 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1660 | |
| 1661 | memprintf(err, "key or pattern not found"); |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | /* 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] | 1666 | 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] | 1667 | { |
| 1668 | struct pat_ref_elt *elt; |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1669 | int found = 0; |
| 1670 | char *_merr; |
| 1671 | char **merr; |
| 1672 | |
| 1673 | if (err) { |
| 1674 | merr = &_merr; |
| 1675 | *merr = NULL; |
| 1676 | } |
| 1677 | else |
| 1678 | merr = NULL; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1679 | |
| 1680 | /* Look for pattern in the reference. */ |
| 1681 | list_for_each_entry(elt, &ref->head, list) { |
| 1682 | if (strcmp(key, elt->pattern) == 0) { |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1683 | if (!pat_ref_set_elt(ref, elt, value, merr)) { |
| 1684 | if (!found) |
| 1685 | *err = *merr; |
| 1686 | else { |
| 1687 | memprintf(err, "%s, %s", *err, *merr); |
| 1688 | free(*merr); |
| 1689 | *merr = NULL; |
| 1690 | } |
| 1691 | } |
| 1692 | found = 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1693 | } |
| 1694 | } |
Thierry FOURNIER | 364cfdf | 2014-01-29 19:08:49 +0100 | [diff] [blame] | 1695 | |
| 1696 | if (!found) { |
| 1697 | memprintf(err, "entry not found"); |
| 1698 | return 0; |
| 1699 | } |
| 1700 | return 1; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1703 | /* This function create new reference. <ref> is the reference name. |
| 1704 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1705 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1706 | * before calling this function. If the fucntion fail, it return NULL, |
| 1707 | * else return new struct pat_ref. |
| 1708 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1709 | 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] | 1710 | { |
| 1711 | struct pat_ref *ref; |
| 1712 | |
| 1713 | ref = malloc(sizeof(*ref)); |
| 1714 | if (!ref) |
| 1715 | return NULL; |
| 1716 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1717 | if (display) { |
| 1718 | ref->display = strdup(display); |
| 1719 | if (!ref->display) { |
| 1720 | free(ref); |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | } |
| 1724 | else |
| 1725 | ref->display = NULL; |
| 1726 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1727 | ref->reference = strdup(reference); |
| 1728 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1729 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1730 | free(ref); |
| 1731 | return NULL; |
| 1732 | } |
| 1733 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1734 | ref->flags = flags; |
| 1735 | ref->unique_id = -1; |
| 1736 | |
| 1737 | LIST_INIT(&ref->head); |
| 1738 | LIST_INIT(&ref->pat); |
| 1739 | |
| 1740 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1741 | |
| 1742 | return ref; |
| 1743 | } |
| 1744 | |
| 1745 | /* This function create new reference. <unique_id> is the unique id. If |
| 1746 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1747 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1748 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1749 | * or pat_ref_lookupid before calling this function. If the function |
| 1750 | * fail, it return NULL, else return new struct pat_ref. |
| 1751 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1752 | 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] | 1753 | { |
| 1754 | struct pat_ref *ref; |
| 1755 | |
| 1756 | ref = malloc(sizeof(*ref)); |
| 1757 | if (!ref) |
| 1758 | return NULL; |
| 1759 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1760 | if (display) { |
| 1761 | ref->display = strdup(display); |
| 1762 | if (!ref->display) { |
| 1763 | free(ref); |
| 1764 | return NULL; |
| 1765 | } |
| 1766 | } |
| 1767 | else |
| 1768 | ref->display = NULL; |
| 1769 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1770 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1771 | ref->flags = flags; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1772 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1773 | LIST_INIT(&ref->head); |
| 1774 | LIST_INIT(&ref->pat); |
| 1775 | |
| 1776 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1777 | |
| 1778 | return ref; |
| 1779 | } |
| 1780 | |
| 1781 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1782 | * If the function fails, it returns 0. |
| 1783 | */ |
| 1784 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line) |
| 1785 | { |
| 1786 | struct pat_ref_elt *elt; |
| 1787 | |
| 1788 | elt = malloc(sizeof(*elt)); |
| 1789 | if (!elt) |
| 1790 | return 0; |
| 1791 | |
| 1792 | elt->line = line; |
| 1793 | |
| 1794 | elt->pattern = strdup(pattern); |
| 1795 | if (!elt->pattern) { |
| 1796 | free(elt); |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | if (sample) { |
| 1801 | elt->sample = strdup(sample); |
| 1802 | if (!elt->sample) { |
| 1803 | free(elt->pattern); |
| 1804 | free(elt); |
| 1805 | return 0; |
| 1806 | } |
| 1807 | } |
| 1808 | else |
| 1809 | elt->sample = NULL; |
| 1810 | |
| 1811 | LIST_ADDQ(&ref->head, &elt->list); |
| 1812 | |
| 1813 | return 1; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1816 | /* This function create sample found in <elt>, parse the pattern also |
| 1817 | * found in <elt> and insert it in <expr>. The function copy <patflags> |
| 1818 | * in <expr>. If the function fails, it returns0 and <err> is filled. |
| 1819 | * In succes case, the function returns 1. |
| 1820 | */ |
| 1821 | static inline |
| 1822 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1823 | int patflags, char **err) |
| 1824 | { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1825 | struct sample_data *data; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1826 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1827 | |
| 1828 | /* Create sample */ |
| 1829 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1830 | /* New sample. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1831 | data = malloc(sizeof(*data)); |
| 1832 | if (!data) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1833 | return 0; |
| 1834 | |
| 1835 | /* Parse value. */ |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1836 | if (!expr->pat_head->parse_smp(elt->sample, data)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1837 | memprintf(err, "unable to parse '%s'", elt->sample); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1838 | free(data); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | } |
| 1843 | else |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1844 | data = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1845 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1846 | /* initialise pattern */ |
| 1847 | memset(&pattern, 0, sizeof(pattern)); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1848 | pattern.data = data; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1849 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* parse pattern */ |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1852 | if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1853 | free(data); |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1854 | return 0; |
| 1855 | } |
| 1856 | |
| 1857 | /* index pattern */ |
| 1858 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1859 | free(data); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1860 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1861 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1862 | |
| 1863 | return 1; |
| 1864 | } |
| 1865 | |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1866 | /* This function adds entry to <ref>. It can failed with memory error. The new |
| 1867 | * entry is added at all the pattern_expr registered in this reference. The |
| 1868 | * function stop on the first error encountered. It returns 0 and err is |
| 1869 | * filled. If an error is encountered, the complete add operation is cancelled. |
| 1870 | * If the insertion is a success the function returns 1. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1871 | */ |
| 1872 | int pat_ref_add(struct pat_ref *ref, |
| 1873 | const char *pattern, const char *sample, |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1874 | char **err) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1875 | { |
| 1876 | struct pat_ref_elt *elt; |
| 1877 | struct pattern_expr *expr; |
| 1878 | |
| 1879 | elt = malloc(sizeof(*elt)); |
| 1880 | if (!elt) { |
| 1881 | memprintf(err, "out of memory error"); |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | elt->line = -1; |
| 1886 | |
| 1887 | elt->pattern = strdup(pattern); |
| 1888 | if (!elt->pattern) { |
| 1889 | free(elt); |
| 1890 | memprintf(err, "out of memory error"); |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | if (sample) { |
| 1895 | elt->sample = strdup(sample); |
| 1896 | if (!elt->sample) { |
| 1897 | free(elt->pattern); |
| 1898 | free(elt); |
| 1899 | memprintf(err, "out of memory error"); |
| 1900 | return 0; |
| 1901 | } |
| 1902 | } |
| 1903 | else |
| 1904 | elt->sample = NULL; |
| 1905 | |
| 1906 | LIST_ADDQ(&ref->head, &elt->list); |
| 1907 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1908 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 1909 | if (!pat_ref_push(elt, expr, 0, err)) { |
Thierry FOURNIER | 31db4ae | 2014-01-30 00:27:15 +0100 | [diff] [blame] | 1910 | /* If the insertion fails, try to delete all the added entries. */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 1911 | pat_ref_delete_by_id(ref, elt); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1912 | return 0; |
| 1913 | } |
| 1914 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1915 | return 1; |
| 1916 | } |
| 1917 | |
Thierry FOURNIER | 46006bd | 2014-03-21 21:45:15 +0100 | [diff] [blame] | 1918 | /* This function prune <ref>, replace all reference by the references |
| 1919 | * of <replace>, and reindex all the news values. |
| 1920 | * |
| 1921 | * The pattern are loaded in best effort and the errors are ignored, |
| 1922 | * but writed in the logs. |
| 1923 | */ |
| 1924 | void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace) |
| 1925 | { |
| 1926 | struct pattern_expr *expr; |
| 1927 | struct pat_ref_elt *elt; |
| 1928 | char *err = NULL; |
| 1929 | |
| 1930 | pat_ref_prune(ref); |
| 1931 | |
| 1932 | LIST_ADD(&replace->head, &ref->head); |
| 1933 | LIST_DEL(&replace->head); |
| 1934 | |
| 1935 | list_for_each_entry(elt, &ref->head, list) { |
| 1936 | list_for_each_entry(expr, &ref->pat, list) { |
| 1937 | if (!pat_ref_push(elt, expr, 0, &err)) { |
| 1938 | send_log(NULL, LOG_NOTICE, "%s", err); |
| 1939 | free(err); |
| 1940 | err = NULL; |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1946 | /* This function prune all entries of <ref>. This function |
| 1947 | * prune the associated pattern_expr. |
| 1948 | */ |
| 1949 | void pat_ref_prune(struct pat_ref *ref) |
| 1950 | { |
| 1951 | struct pat_ref_elt *elt, *safe; |
| 1952 | struct pattern_expr *expr; |
| 1953 | |
| 1954 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1955 | LIST_DEL(&elt->list); |
| 1956 | free(elt->pattern); |
| 1957 | free(elt->sample); |
| 1958 | free(elt); |
| 1959 | } |
| 1960 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1961 | list_for_each_entry(expr, &ref->pat, list) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1962 | expr->pat_head->prune(expr); |
| 1963 | } |
| 1964 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1965 | /* This function lookup for existing reference <ref> in pattern_head <head>. */ |
| 1966 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 1967 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1968 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1969 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1970 | list_for_each_entry(expr, &head->head, list) |
| 1971 | if (expr->expr->ref == ref) |
| 1972 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1973 | return NULL; |
| 1974 | } |
| 1975 | |
| 1976 | /* This function create new pattern_expr associated to the reference <ref>. |
| 1977 | * <ref> can be NULL. If an error is occured, the function returns NULL and |
| 1978 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 1979 | * with <head> and <ref>. |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 1980 | * |
| 1981 | * The returned value can be a alredy filled pattern list, in this case the |
| 1982 | * flag <reuse> is set. |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1983 | */ |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 1984 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, |
| 1985 | char **err, int *reuse) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1986 | { |
| 1987 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1988 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1989 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 1990 | if (reuse) |
| 1991 | *reuse = 0; |
| 1992 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1993 | /* Memory and initialization of the chain element. */ |
| 1994 | list = malloc(sizeof(*list)); |
| 1995 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1996 | memprintf(err, "out of memory"); |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2000 | /* Look for existing similar expr. No that only the index, parse and |
| 2001 | * parse_smp function must be identical for having similar pattern. |
| 2002 | * The other function depends of theses first. |
| 2003 | */ |
| 2004 | if (ref) { |
| 2005 | list_for_each_entry(expr, &ref->pat, list) |
| 2006 | if (expr->pat_head->index == head->index && |
| 2007 | expr->pat_head->parse == head->parse && |
| 2008 | expr->pat_head->parse_smp == head->parse_smp) |
| 2009 | break; |
| 2010 | if (&expr->list == &ref->pat) |
| 2011 | expr = NULL; |
| 2012 | } |
| 2013 | else |
| 2014 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2015 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2016 | /* If no similar expr was found, we create new expr. */ |
| 2017 | if (!expr) { |
| 2018 | /* Get a lot of memory for the expr struct. */ |
| 2019 | expr = malloc(sizeof(*expr)); |
| 2020 | if (!expr) { |
| 2021 | memprintf(err, "out of memory"); |
| 2022 | return NULL; |
| 2023 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2024 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2025 | /* Initialize this new expr. */ |
| 2026 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2027 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2028 | /* This new pattern expression reference one of his heads. */ |
| 2029 | expr->pat_head = head; |
| 2030 | |
| 2031 | /* Link with ref, or to self to facilitate LIST_DEL() */ |
| 2032 | if (ref) |
| 2033 | LIST_ADDQ(&ref->pat, &expr->list); |
| 2034 | else |
| 2035 | LIST_INIT(&expr->list); |
| 2036 | |
| 2037 | expr->ref = ref; |
| 2038 | |
| 2039 | /* We must free this pattern if it is no more used. */ |
| 2040 | list->do_free = 1; |
| 2041 | } |
| 2042 | else { |
| 2043 | /* If the pattern used already exists, it is already linked |
| 2044 | * with ref and we must not free it. |
| 2045 | */ |
| 2046 | list->do_free = 0; |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2047 | if (reuse) |
| 2048 | *reuse = 1; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | /* The new list element reference the pattern_expr. */ |
| 2052 | list->expr = expr; |
| 2053 | |
| 2054 | /* Link the list element with the pattern_head. */ |
| 2055 | LIST_ADDQ(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2056 | return expr; |
| 2057 | } |
| 2058 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2059 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2060 | * 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] | 2061 | * |
| 2062 | * The file contains one key + value per line. Lines which start with '#' are |
| 2063 | * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is |
| 2064 | * then the first "word" (series of non-space/tabs characters), and the value is |
| 2065 | * what follows this series of space/tab till the end of the line excluding |
| 2066 | * trailing spaces/tabs. |
| 2067 | * |
| 2068 | * Example : |
| 2069 | * |
| 2070 | * # this is a comment and is ignored |
| 2071 | * 62.212.114.60 1wt.eu \n |
| 2072 | * <-><-----------><---><----><----> |
| 2073 | * | | | | `--- trailing spaces ignored |
| 2074 | * | | | `-------- value |
| 2075 | * | | `--------------- middle spaces ignored |
| 2076 | * | `------------------------ key |
| 2077 | * `-------------------------------- leading spaces ignored |
| 2078 | * |
| 2079 | * Return non-zero in case of succes, otherwise 0. |
| 2080 | */ |
| 2081 | int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err) |
| 2082 | { |
| 2083 | FILE *file; |
| 2084 | char *c; |
| 2085 | int ret = 0; |
| 2086 | int line = 0; |
| 2087 | char *key_beg; |
| 2088 | char *key_end; |
| 2089 | char *value_beg; |
| 2090 | char *value_end; |
| 2091 | |
| 2092 | file = fopen(filename, "r"); |
| 2093 | if (!file) { |
| 2094 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | /* now parse all patterns. The file may contain only one pattern |
| 2099 | * followed by one value per line. The start spaces, separator spaces |
| 2100 | * and and spaces are stripped. Each can contain comment started by '#' |
| 2101 | */ |
| 2102 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2103 | line++; |
| 2104 | c = trash.str; |
| 2105 | |
| 2106 | /* ignore lines beginning with a dash */ |
| 2107 | if (*c == '#') |
| 2108 | continue; |
| 2109 | |
| 2110 | /* strip leading spaces and tabs */ |
| 2111 | while (*c == ' ' || *c == '\t') |
| 2112 | c++; |
| 2113 | |
| 2114 | /* empty lines are ignored too */ |
| 2115 | if (*c == '\0' || *c == '\r' || *c == '\n') |
| 2116 | continue; |
| 2117 | |
| 2118 | /* look for the end of the key */ |
| 2119 | key_beg = c; |
| 2120 | while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2121 | c++; |
| 2122 | |
| 2123 | key_end = c; |
| 2124 | |
| 2125 | /* strip middle spaces and tabs */ |
| 2126 | while (*c == ' ' || *c == '\t') |
| 2127 | c++; |
| 2128 | |
| 2129 | /* look for the end of the value, it is the end of the line */ |
| 2130 | value_beg = c; |
| 2131 | while (*c && *c != '\n' && *c != '\r') |
| 2132 | c++; |
| 2133 | value_end = c; |
| 2134 | |
| 2135 | /* trim possibly trailing spaces and tabs */ |
| 2136 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2137 | value_end--; |
| 2138 | |
| 2139 | /* set final \0 and check entries */ |
| 2140 | *key_end = '\0'; |
| 2141 | *value_end = '\0'; |
| 2142 | |
| 2143 | /* insert values */ |
| 2144 | if (!pat_ref_append(ref, key_beg, value_beg, line)) { |
| 2145 | memprintf(err, "out of memory"); |
| 2146 | goto out_close; |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* succes */ |
| 2151 | ret = 1; |
| 2152 | |
| 2153 | out_close: |
| 2154 | fclose(file); |
| 2155 | return ret; |
| 2156 | } |
| 2157 | |
| 2158 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2159 | * 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] | 2160 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2161 | 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] | 2162 | { |
| 2163 | FILE *file; |
| 2164 | char *c; |
| 2165 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2166 | int ret = 0; |
| 2167 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2168 | |
| 2169 | file = fopen(filename, "r"); |
| 2170 | if (!file) { |
| 2171 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
| 2175 | /* now parse all patterns. The file may contain only one pattern per |
| 2176 | * line. If the line contains spaces, they will be part of the pattern. |
| 2177 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2178 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2179 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2180 | line++; |
| 2181 | c = trash.str; |
| 2182 | |
| 2183 | /* ignore lines beginning with a dash */ |
| 2184 | if (*c == '#') |
| 2185 | continue; |
| 2186 | |
| 2187 | /* strip leading spaces and tabs */ |
| 2188 | while (*c == ' ' || *c == '\t') |
| 2189 | c++; |
| 2190 | |
| 2191 | |
| 2192 | arg = c; |
| 2193 | while (*c && *c != '\n' && *c != '\r') |
| 2194 | c++; |
| 2195 | *c = 0; |
| 2196 | |
| 2197 | /* empty lines are ignored too */ |
| 2198 | if (c == arg) |
| 2199 | continue; |
| 2200 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2201 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2202 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2203 | goto out_close; |
| 2204 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | ret = 1; /* success */ |
| 2208 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2209 | out_close: |
| 2210 | fclose(file); |
| 2211 | return ret; |
| 2212 | } |
| 2213 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2214 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2215 | const char *filename, int patflags, int load_smp, |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2216 | char **err, const char *file, int line) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2217 | { |
| 2218 | struct pat_ref *ref; |
| 2219 | struct pattern_expr *expr; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2220 | struct pat_ref_elt *elt; |
Willy Tarreau | 4deaf39 | 2014-11-26 13:17:03 +0100 | [diff] [blame] | 2221 | int reuse = 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2222 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2223 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2224 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2225 | |
| 2226 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2227 | if (!ref) { |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2228 | chunk_printf(&trash, |
| 2229 | "pattern loaded from file '%s' used by %s at file '%s' line %d", |
| 2230 | filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2231 | |
| 2232 | ref = pat_ref_new(filename, trash.str, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2233 | if (!ref) { |
| 2234 | memprintf(err, "out of memory"); |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2238 | if (load_smp) { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2239 | ref->flags |= PAT_REF_SMP; |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2240 | if (!pat_ref_read_from_file_smp(ref, filename, err)) |
| 2241 | return 0; |
| 2242 | } |
| 2243 | else { |
| 2244 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2245 | return 0; |
| 2246 | } |
| 2247 | } |
| 2248 | else { |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2249 | /* The reference already exists, check the map compatibility. */ |
| 2250 | |
| 2251 | /* If the load require samples and the flag PAT_REF_SMP is not set, |
| 2252 | * the reference doesn't contain sample, and cannot be used. |
| 2253 | */ |
| 2254 | if (load_smp) { |
| 2255 | if (!(ref->flags & PAT_REF_SMP)) { |
| 2256 | memprintf(err, "The file \"%s\" is already used as one column file " |
| 2257 | "and cannot be used by as two column file.", |
| 2258 | filename); |
| 2259 | return 0; |
| 2260 | } |
| 2261 | } |
| 2262 | else { |
| 2263 | /* The load doesn't require samples. If the flag PAT_REF_SMP is |
| 2264 | * set, the reference contains a sample, and cannot be used. |
| 2265 | */ |
| 2266 | if (ref->flags & PAT_REF_SMP) { |
| 2267 | memprintf(err, "The file \"%s\" is already used as two column file " |
| 2268 | "and cannot be used by as one column file.", |
| 2269 | filename); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | } |
| 2273 | |
Thierry FOURNIER | 94580c9 | 2014-02-11 14:36:45 +0100 | [diff] [blame] | 2274 | /* Extends display */ |
| 2275 | chunk_printf(&trash, "%s", ref->display); |
| 2276 | chunk_appendf(&trash, ", by %s at file '%s' line %d", |
| 2277 | refflags & PAT_REF_MAP ? "map" : "acl", file, line); |
| 2278 | free(ref->display); |
| 2279 | ref->display = strdup(trash.str); |
| 2280 | if (!ref->display) { |
| 2281 | memprintf(err, "out of memory"); |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
Thierry FOURNIER | c0bd910 | 2014-01-29 12:32:58 +0100 | [diff] [blame] | 2285 | /* Merge flags. */ |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2286 | ref->flags |= refflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | /* Now, we can loading patterns from the reference. */ |
| 2290 | |
| 2291 | /* Lookup for existing reference in the head. If the reference |
| 2292 | * doesn't exists, create it. |
| 2293 | */ |
| 2294 | expr = pattern_lookup_expr(head, ref); |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2295 | if (!expr || (expr->mflags != patflags)) { |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2296 | expr = pattern_new_expr(head, ref, err, &reuse); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2297 | if (!expr) |
| 2298 | return 0; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2299 | expr->mflags = patflags; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2300 | } |
| 2301 | |
Thierry FOURNIER | 315ec42 | 2014-11-24 11:14:42 +0100 | [diff] [blame] | 2302 | /* The returned expression may be not empty, because the function |
| 2303 | * "pattern_new_expr" lookup for similar pattern list and can |
| 2304 | * reuse a already filled pattern list. In this case, we can not |
| 2305 | * reload the patterns. |
| 2306 | */ |
| 2307 | if (reuse) |
| 2308 | return 1; |
| 2309 | |
Thierry FOURNIER | 39bef45 | 2014-01-29 13:29:45 +0100 | [diff] [blame] | 2310 | /* Load reference content in the pattern expression. */ |
| 2311 | list_for_each_entry(elt, &ref->head, list) { |
| 2312 | if (!pat_ref_push(elt, expr, patflags, err)) { |
| 2313 | if (elt->line > 0) |
| 2314 | memprintf(err, "%s at line %d of file '%s'", |
| 2315 | *err, elt->line, filename); |
| 2316 | return 0; |
| 2317 | } |
| 2318 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2319 | |
| 2320 | return 1; |
| 2321 | } |
| 2322 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2323 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 2324 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 2325 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2326 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2327 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2328 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2329 | 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] | 2330 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2331 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2332 | struct pattern *pat; |
| 2333 | |
| 2334 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2335 | if (fill) { |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2336 | static_pattern.data = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2337 | static_pattern.ref = NULL; |
Thierry FOURNIER | e47e4e2 | 2014-04-28 11:18:57 +0200 | [diff] [blame] | 2338 | static_pattern.sflags = 0; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2339 | static_pattern.type = SMP_T_SINT; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2340 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2341 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2342 | return &static_pattern; |
| 2343 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2344 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2345 | /* convert input to string */ |
| 2346 | if (!sample_convert(smp, head->expect_type)) |
| 2347 | return NULL; |
| 2348 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2349 | list_for_each_entry(list, &head->head, list) { |
| 2350 | pat = head->match(smp, list->expr, fill); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2351 | if (pat) |
| 2352 | return pat; |
| 2353 | } |
| 2354 | return NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2355 | } |
| 2356 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2357 | /* This function prune the pattern expression. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2358 | void pattern_prune(struct pattern_head *head) |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2359 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2360 | struct pattern_expr_list *list, *safe; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2361 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2362 | list_for_each_entry_safe(list, safe, &head->head, list) { |
| 2363 | LIST_DEL(&list->list); |
| 2364 | if (list->do_free) { |
| 2365 | LIST_DEL(&list->expr->list); |
| 2366 | head->prune(list->expr); |
| 2367 | free(list->expr); |
| 2368 | } |
| 2369 | free(list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2370 | } |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2373 | /* This function lookup for a pattern matching the <key> and return a |
| 2374 | * pointer to a pointer of the sample stoarge. If the <key> dont match, |
| 2375 | * the function returns NULL. If the key cannot be parsed, the function |
| 2376 | * fill <err>. |
| 2377 | */ |
Thierry FOURNIER | 12ba0c2 | 2015-08-14 00:02:11 +0200 | [diff] [blame] | 2378 | 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] | 2379 | { |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2380 | struct ebmb_node *node; |
| 2381 | struct pattern_tree *elt; |
| 2382 | struct pattern_list *pat; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2383 | |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2384 | for (node = ebmb_first(&expr->pattern_tree); |
| 2385 | node; |
| 2386 | node = ebmb_next(node)) { |
| 2387 | elt = container_of(node, struct pattern_tree, node); |
| 2388 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2389 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | for (node = ebmb_first(&expr->pattern_tree_2); |
| 2393 | node; |
| 2394 | node = ebmb_next(node)) { |
| 2395 | elt = container_of(node, struct pattern_tree, node); |
| 2396 | if (elt->ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2397 | return &elt->data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | list_for_each_entry(pat, &expr->patterns, list) |
| 2401 | if (pat->pat.ref == ref) |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2402 | return &pat->pat.data; |
Thierry FOURNIER | e369ca2 | 2014-01-29 16:24:55 +0100 | [diff] [blame] | 2403 | |
| 2404 | return NULL; |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2405 | } |
| 2406 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2407 | /* This function search all the pattern matching the <key> and delete it. |
| 2408 | * If the parsing of the input key fails, the function returns 0 and the |
| 2409 | * <err> is filled, else return 1; |
| 2410 | */ |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2411 | int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2412 | { |
Thierry FOURNIER | 7acca4b | 2014-01-28 16:43:36 +0100 | [diff] [blame] | 2413 | expr->pat_head->delete(expr, ref); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2414 | return 1; |
| 2415 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2416 | |
| 2417 | /* This function finalize the configuration parsing. Its set all the |
| 2418 | * automatic ids |
| 2419 | */ |
| 2420 | void pattern_finalize_config(void) |
| 2421 | { |
| 2422 | int i = 0; |
| 2423 | struct pat_ref *ref, *ref2, *ref3; |
| 2424 | struct list pr = LIST_HEAD_INIT(pr); |
| 2425 | |
Willy Tarreau | f3045d2 | 2015-04-29 16:24:50 +0200 | [diff] [blame] | 2426 | pat_lru_seed = random(); |
| 2427 | if (global.tune.pattern_cache) |
| 2428 | pat_lru_tree = lru64_new(global.tune.pattern_cache); |
| 2429 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2430 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2431 | if (ref->unique_id == -1) { |
| 2432 | /* Look for the first free id. */ |
| 2433 | while (1) { |
| 2434 | list_for_each_entry(ref2, &pattern_reference, list) { |
| 2435 | if (ref2->unique_id == i) { |
| 2436 | i++; |
| 2437 | break; |
| 2438 | } |
| 2439 | } |
Willy Tarreau | 3b78696 | 2014-04-26 12:37:25 +0200 | [diff] [blame] | 2440 | if (&ref2->list == &pattern_reference) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2441 | break; |
| 2442 | } |
| 2443 | |
| 2444 | /* Uses the unique id and increment it for the next entry. */ |
| 2445 | ref->unique_id = i; |
| 2446 | i++; |
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | /* This sort the reference list by id. */ |
| 2451 | list_for_each_entry_safe(ref, ref2, &pattern_reference, list) { |
| 2452 | LIST_DEL(&ref->list); |
| 2453 | list_for_each_entry(ref3, &pr, list) { |
| 2454 | if (ref->unique_id < ref3->unique_id) { |
| 2455 | LIST_ADDQ(&ref3->list, &ref->list); |
| 2456 | break; |
| 2457 | } |
| 2458 | } |
| 2459 | if (&ref3->list == &pr) |
| 2460 | LIST_ADDQ(&pr, &ref->list); |
| 2461 | } |
| 2462 | |
| 2463 | /* swap root */ |
| 2464 | LIST_ADD(&pr, &pattern_reference); |
| 2465 | LIST_DEL(&pr); |
| 2466 | } |