blob: a8b56ca5e333221d1e98351c44fa9a13076883ad [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
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 FOURNIER46006bd2014-03-21 21:45:15 +010022#include <proto/log.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010023#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010024#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010025
26#include <ebsttree.h>
Willy Tarreauf3045d22015-04-29 16:24:50 +020027#include <import/lru.h>
28#include <import/xxhash.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010029
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010030char *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 Fournier8feaa662016-02-10 22:55:20 +010044 [PAT_MATCH_REGM] = "regm",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010045};
46
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020047int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010048 [PAT_MATCH_FOUND] = pat_parse_nothing,
49 [PAT_MATCH_BOOL] = pat_parse_nothing,
50 [PAT_MATCH_INT] = pat_parse_int,
51 [PAT_MATCH_IP] = pat_parse_ip,
52 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010053 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010054 [PAT_MATCH_STR] = pat_parse_str,
55 [PAT_MATCH_BEG] = pat_parse_str,
56 [PAT_MATCH_SUB] = pat_parse_str,
57 [PAT_MATCH_DIR] = pat_parse_str,
58 [PAT_MATCH_DOM] = pat_parse_str,
59 [PAT_MATCH_END] = pat_parse_str,
60 [PAT_MATCH_REG] = pat_parse_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010061 [PAT_MATCH_REGM] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062};
63
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010064int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
65 [PAT_MATCH_FOUND] = pat_idx_list_val,
66 [PAT_MATCH_BOOL] = pat_idx_list_val,
67 [PAT_MATCH_INT] = pat_idx_list_val,
68 [PAT_MATCH_IP] = pat_idx_tree_ip,
69 [PAT_MATCH_BIN] = pat_idx_list_ptr,
70 [PAT_MATCH_LEN] = pat_idx_list_val,
71 [PAT_MATCH_STR] = pat_idx_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020072 [PAT_MATCH_BEG] = pat_idx_tree_pfx,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010073 [PAT_MATCH_SUB] = pat_idx_list_str,
74 [PAT_MATCH_DIR] = pat_idx_list_str,
75 [PAT_MATCH_DOM] = pat_idx_list_str,
76 [PAT_MATCH_END] = pat_idx_list_str,
77 [PAT_MATCH_REG] = pat_idx_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010078 [PAT_MATCH_REGM] = pat_idx_list_regm,
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010079};
80
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010081void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
Thierry FOURNIERb1136502014-01-15 11:38:49 +010082 [PAT_MATCH_FOUND] = pat_del_list_val,
83 [PAT_MATCH_BOOL] = pat_del_list_val,
84 [PAT_MATCH_INT] = pat_del_list_val,
85 [PAT_MATCH_IP] = pat_del_tree_ip,
86 [PAT_MATCH_BIN] = pat_del_list_ptr,
87 [PAT_MATCH_LEN] = pat_del_list_val,
88 [PAT_MATCH_STR] = pat_del_tree_str,
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +020089 [PAT_MATCH_BEG] = pat_del_tree_str,
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010090 [PAT_MATCH_SUB] = pat_del_list_ptr,
91 [PAT_MATCH_DIR] = pat_del_list_ptr,
92 [PAT_MATCH_DOM] = pat_del_list_ptr,
93 [PAT_MATCH_END] = pat_del_list_ptr,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010094 [PAT_MATCH_REG] = pat_del_list_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +010095 [PAT_MATCH_REGM] = pat_del_list_reg,
Thierry FOURNIERb1136502014-01-15 11:38:49 +010096};
97
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010098void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
99 [PAT_MATCH_FOUND] = pat_prune_val,
100 [PAT_MATCH_BOOL] = pat_prune_val,
101 [PAT_MATCH_INT] = pat_prune_val,
102 [PAT_MATCH_IP] = pat_prune_val,
103 [PAT_MATCH_BIN] = pat_prune_ptr,
104 [PAT_MATCH_LEN] = pat_prune_val,
105 [PAT_MATCH_STR] = pat_prune_ptr,
106 [PAT_MATCH_BEG] = pat_prune_ptr,
107 [PAT_MATCH_SUB] = pat_prune_ptr,
108 [PAT_MATCH_DIR] = pat_prune_ptr,
109 [PAT_MATCH_DOM] = pat_prune_ptr,
110 [PAT_MATCH_END] = pat_prune_ptr,
111 [PAT_MATCH_REG] = pat_prune_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100112 [PAT_MATCH_REGM] = pat_prune_reg,
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100113};
114
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100115struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100116 [PAT_MATCH_FOUND] = NULL,
117 [PAT_MATCH_BOOL] = pat_match_nothing,
118 [PAT_MATCH_INT] = pat_match_int,
119 [PAT_MATCH_IP] = pat_match_ip,
120 [PAT_MATCH_BIN] = pat_match_bin,
121 [PAT_MATCH_LEN] = pat_match_len,
122 [PAT_MATCH_STR] = pat_match_str,
123 [PAT_MATCH_BEG] = pat_match_beg,
124 [PAT_MATCH_SUB] = pat_match_sub,
125 [PAT_MATCH_DIR] = pat_match_dir,
126 [PAT_MATCH_DOM] = pat_match_dom,
127 [PAT_MATCH_END] = pat_match_end,
128 [PAT_MATCH_REG] = pat_match_reg,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100129 [PAT_MATCH_REGM] = pat_match_regm,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130};
131
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100132/* Just used for checking configuration compatibility */
133int pat_match_types[PAT_MATCH_NUM] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200134 [PAT_MATCH_FOUND] = SMP_T_SINT,
135 [PAT_MATCH_BOOL] = SMP_T_SINT,
136 [PAT_MATCH_INT] = SMP_T_SINT,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100137 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100138 [PAT_MATCH_BIN] = SMP_T_BIN,
139 [PAT_MATCH_LEN] = SMP_T_STR,
140 [PAT_MATCH_STR] = SMP_T_STR,
141 [PAT_MATCH_BEG] = SMP_T_STR,
142 [PAT_MATCH_SUB] = SMP_T_STR,
143 [PAT_MATCH_DIR] = SMP_T_STR,
144 [PAT_MATCH_DOM] = SMP_T_STR,
145 [PAT_MATCH_END] = SMP_T_STR,
146 [PAT_MATCH_REG] = SMP_T_STR,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100147 [PAT_MATCH_REGM] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100148};
149
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100150/* this struct is used to return information */
151static struct pattern static_pattern;
152
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100153/* This is the root of the list of all pattern_ref avalaibles. */
154struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
155
Willy Tarreauf3045d22015-04-29 16:24:50 +0200156static struct lru64_head *pat_lru_tree;
157static unsigned long long pat_lru_seed;
158
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100160 *
161 * The following functions are not exported and are used by internals process
162 * of pattern matching
163 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100164 */
165
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166/* Background: Fast way to find a zero byte in a word
167 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
168 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
169 *
170 * To look for 4 different byte values, xor the word with those bytes and
171 * then check for zero bytes:
172 *
173 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
174 * where <delimiter> is the 4 byte values to look for (as an uint)
175 * and <c> is the character that is being tested
176 */
177static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
178{
179 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
180 return (mask - 0x01010101) & ~mask & 0x80808080U;
181}
182
183static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
184{
185 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
186}
187
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100188
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189/*
190 *
191 * These functions are exported and may be used by any other component.
192 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200193 * The following functions are used for parsing pattern matching input value.
194 * The <text> contain the string to be parsed. <pattern> must be a preallocated
195 * pattern. The pat_parse_* functions fill this structure with the parsed value.
196 * <err> is filled with an error message built with memprintf() function. It is
197 * allowed to use a trash as a temporary storage for the returned pattern, as
198 * the next call after these functions will be pat_idx_*.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100199 *
Willy Tarreau5def8ef2014-08-29 15:19:33 +0200200 * In success case, the pat_parse_* function returns 1. If the function
201 * fails, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100202 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100203
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100204/* ignore the current line */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200205int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206{
207 return 1;
208}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100209
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100210/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200211int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100213 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100214 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100215 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100216 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217}
218
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100219/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200220int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100221{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100222 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100224 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100225 trash = get_trash_chunk();
226 pattern->len = trash->size;
227 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100228 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229}
230
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231/* Parse a regex. It is allocated. */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200232int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233{
Thierry FOURNIER0b6d15f2014-01-29 19:35:16 +0100234 pattern->ptr.str = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100235 return 1;
236}
237
238/* Parse a range of positive integers delimited by either ':' or '-'. If only
239 * one integer is read, it is set as both min and max. An operator may be
240 * specified as the prefix, among this list of 5 :
241 *
242 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
243 *
244 * The default operator is "eq". It supports range matching. Ranges are
245 * rejected for other operators. The operator may be changed at any time.
246 * The operator is stored in the 'opaque' argument.
247 *
248 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100249 * the caller will have to free it. The function returns zero on error, and
250 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100251 *
252 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200253int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100255 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100256
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200257 pattern->type = SMP_T_SINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100258
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100259 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100260 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100261 goto not_valid_range;
262
263 /* Search ':' or '-' separator. */
264 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
265 ptr++;
266
267 /* If separator not found. */
268 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100269 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
270 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271 return 0;
272 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100273 pattern->val.range.max = pattern->val.range.min;
274 pattern->val.range.min_set = 1;
275 pattern->val.range.max_set = 1;
276 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100277 }
278
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100279 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100280 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100281 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
282 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100283
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100284 pattern->val.range.min_set = 0;
285 pattern->val.range.max_set = 1;
286 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100287 }
288
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100289 /* If separator is the last character. */
290 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100291 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100293
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294 pattern->val.range.min_set = 1;
295 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100296 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100297 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298
299 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100300 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100301 goto not_valid_range;
302
303 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
304 goto not_valid_range;
305
306 if (pattern->val.range.min > pattern->val.range.max)
307 goto not_valid_range;
308
309 pattern->val.range.min_set = 1;
310 pattern->val.range.max_set = 1;
311 return 1;
312
313 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100314 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100315 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316}
317
318/* Parse a range of positive 2-component versions delimited by either ':' or
319 * '-'. The version consists in a major and a minor, both of which must be
320 * smaller than 65536, because internally they will be represented as a 32-bit
321 * integer.
322 * If only one version is read, it is set as both min and max. Just like for
323 * pure integers, an operator may be specified as the prefix, among this list
324 * of 5 :
325 *
326 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
327 *
328 * The default operator is "eq". It supports range matching. Ranges are
329 * rejected for other operators. The operator may be changed at any time.
330 * The operator is stored in the 'opaque' argument. This allows constructs
331 * such as the following one :
332 *
333 * acl obsolete_ssl ssl_req_proto lt 3
334 * acl unsupported_ssl ssl_req_proto gt 3.1
335 * acl valid_ssl ssl_req_proto 3.0-3.1
336 *
337 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200338int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100339{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100340 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100341
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200342 pattern->type = SMP_T_SINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100343
344 /* Search ':' or '-' separator. */
345 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
346 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100347
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100348 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100349 if (*ptr == '\0' && ptr > text) {
350 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
351 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100352 return 0;
353 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->val.range.max = pattern->val.range.min;
355 pattern->val.range.min_set = 1;
356 pattern->val.range.max_set = 1;
357 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358 }
359
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100360 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100361 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100363 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100364 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100366 pattern->val.range.min_set = 0;
367 pattern->val.range.max_set = 1;
368 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100369 }
370
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100371 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100372 if (ptr == &text[strlen(text)-1]) {
373 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
374 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100375 return 0;
376 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100377 pattern->val.range.min_set = 1;
378 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 return 1;
380 }
381
382 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100383 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
384 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100385 return 0;
386 }
387 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100388 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100389 return 0;
390 }
391 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100392 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100393 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100395 pattern->val.range.min_set = 1;
396 pattern->val.range.max_set = 1;
397 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398}
399
400/* Parse an IP address and an optional mask in the form addr[/mask].
401 * The addr may either be an IPv4 address or a hostname. The mask
402 * may either be a dotted mask or a number of bits. Returns 1 if OK,
403 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
404 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200405int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406{
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200407 if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100408 &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100409 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100410 return 1;
411 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100412 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 pattern->type = SMP_T_IPV6;
414 return 1;
415 }
416 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100417 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100418 return 0;
419 }
420}
421
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422/*
423 *
424 * These functions are exported and may be used by any other component.
425 *
426 * This fucntion just take a sample <smp> and check if this sample match
427 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
428 * PAT_NOMATCH.
429 *
430 */
431
432/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100433struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100434{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200435 if (smp->data.u.sint) {
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100436 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200437 static_pattern.data = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100438 static_pattern.ref = NULL;
Thierry FOURNIERe5978bf2014-03-17 19:53:10 +0100439 static_pattern.type = 0;
440 static_pattern.ptr.str = NULL;
441 }
442 return &static_pattern;
443 }
444 else
445 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100446}
447
448
449/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100450struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100451{
452 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453 struct ebmb_node *node;
454 char prev;
455 struct pattern_tree *elt;
456 struct pattern_list *lst;
457 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200458 struct pattern *ret = NULL;
459 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100460
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 /* Lookup a string in the expression's pattern tree. */
462 if (!eb_is_empty(&expr->pattern_tree)) {
463 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200464 prev = smp->data.u.str.str[smp->data.u.str.len];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100465 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200466 smp->data.u.str.str[smp->data.u.str.len] = '\0';
467 node = ebst_lookup(&expr->pattern_tree, smp->data.u.str.str);
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100468 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200469 smp->data.u.str.str[smp->data.u.str.len] = prev;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100470
471 if (node) {
472 if (fill) {
473 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200474 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100475 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200476 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100477 static_pattern.type = SMP_T_STR;
478 static_pattern.ptr.str = (char *)elt->node.key;
479 }
480 return &static_pattern;
481 }
482 }
483
484 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200485 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200486 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200487
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200488 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200489 pat_lru_tree, expr, expr->revision);
490 if (lru && lru->domain)
491 return lru->data;
492 }
493
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100494 list_for_each_entry(lst, &expr->patterns, list) {
495 pattern = &lst->pat;
496
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200497 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100498 continue;
499
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200500 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200501 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) ||
502 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200503 ret = pattern;
504 break;
505 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506 }
507
Willy Tarreauf3045d22015-04-29 16:24:50 +0200508 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200509 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200510
511 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100512}
513
514/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100515struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100516{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100517 struct pattern_list *lst;
518 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200519 struct pattern *ret = NULL;
520 struct lru64 *lru = NULL;
521
522 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200523 unsigned long long seed = pat_lru_seed ^ (long)expr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200525 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200526 pat_lru_tree, expr, expr->revision);
527 if (lru && lru->domain)
528 return lru->data;
529 }
530
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 list_for_each_entry(lst, &expr->patterns, list) {
532 pattern = &lst->pat;
533
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200534 if (pattern->len != smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100535 continue;
536
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200537 if (memcmp(pattern->ptr.str, smp->data.u.str.str, smp->data.u.str.len) == 0) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200538 ret = pattern;
539 break;
540 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100541 }
542
Willy Tarreauf3045d22015-04-29 16:24:50 +0200543 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200544 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200545
546 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100547}
548
549/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry Fournier8feaa662016-02-10 22:55:20 +0100550 * and restores the previous character when leaving. This function fills
551 * a matching array.
552 */
553struct pattern *pat_match_regm(struct sample *smp, struct pattern_expr *expr, int fill)
554{
555 struct pattern_list *lst;
556 struct pattern *pattern;
557 struct pattern *ret = NULL;
558
559 list_for_each_entry(lst, &expr->patterns, list) {
560 pattern = &lst->pat;
561
562 if (regex_exec_match2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len,
563 MAX_MATCH, pmatch, 0)) {
564 ret = pattern;
565 smp->ctx.a[0] = pmatch;
566 break;
567 }
568 }
569
570 return ret;
571}
572
573/* Executes a regex. It temporarily changes the data to add a trailing zero,
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100574 * and restores the previous character when leaving.
575 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100576struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100577{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100578 struct pattern_list *lst;
579 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200580 struct pattern *ret = NULL;
581 struct lru64 *lru = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100582
Willy Tarreauf3045d22015-04-29 16:24:50 +0200583 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200584 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200585
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200586 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200587 pat_lru_tree, expr, expr->revision);
588 if (lru && lru->domain)
589 return lru->data;
590 }
591
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592 list_for_each_entry(lst, &expr->patterns, list) {
593 pattern = &lst->pat;
594
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200595 if (regex_exec2(pattern->ptr.reg, smp->data.u.str.str, smp->data.u.str.len)) {
Willy Tarreauf3045d22015-04-29 16:24:50 +0200596 ret = pattern;
597 break;
598 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100599 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200600
601 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200602 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200603
604 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100605}
606
607/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100608struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100609{
610 int icase;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200611 struct ebmb_node *node;
612 char prev;
613 struct pattern_tree *elt;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100614 struct pattern_list *lst;
615 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200616 struct pattern *ret = NULL;
617 struct lru64 *lru = NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100618
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200619 /* Lookup a string in the expression's pattern tree. */
620 if (!eb_is_empty(&expr->pattern_tree)) {
621 /* we may have to force a trailing zero on the test pattern */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200622 prev = smp->data.u.str.str[smp->data.u.str.len];
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200623 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200624 smp->data.u.str.str[smp->data.u.str.len] = '\0';
625 node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.u.str.str);
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200626 if (prev)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200627 smp->data.u.str.str[smp->data.u.str.len] = prev;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200628
629 if (node) {
630 if (fill) {
631 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200632 static_pattern.data = elt->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +0200633 static_pattern.ref = elt->ref;
634 static_pattern.sflags = PAT_SF_TREE;
635 static_pattern.type = SMP_T_STR;
636 static_pattern.ptr.str = (char *)elt->node.key;
637 }
638 return &static_pattern;
639 }
640 }
641
642 /* look in the list */
Willy Tarreauf3045d22015-04-29 16:24:50 +0200643 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200644 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200645
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200646 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200647 pat_lru_tree, expr, expr->revision);
648 if (lru && lru->domain)
649 return lru->data;
650 }
651
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100652 list_for_each_entry(lst, &expr->patterns, list) {
653 pattern = &lst->pat;
654
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200655 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100656 continue;
657
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200658 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200659 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0) ||
660 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100661 continue;
662
Willy Tarreauf3045d22015-04-29 16:24:50 +0200663 ret = pattern;
664 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100665 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200666
667 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200668 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200669
670 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100671}
672
673/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100674struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100675{
676 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100677 struct pattern_list *lst;
678 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200679 struct pattern *ret = NULL;
680 struct lru64 *lru = NULL;
681
682 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200683 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200684
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200685 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200686 pat_lru_tree, expr, expr->revision);
687 if (lru && lru->domain)
688 return lru->data;
689 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100690
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100691 list_for_each_entry(lst, &expr->patterns, list) {
692 pattern = &lst->pat;
693
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200694 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100695 continue;
696
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200697 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200698 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0) ||
699 (!icase && strncmp(pattern->ptr.str, smp->data.u.str.str + smp->data.u.str.len - pattern->len, pattern->len) != 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100700 continue;
701
Willy Tarreauf3045d22015-04-29 16:24:50 +0200702 ret = pattern;
703 break;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200705
706 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200707 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200708
709 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100710}
711
712/* Checks that the pattern is included inside the tested string.
713 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
714 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100715struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716{
717 int icase;
718 char *end;
719 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720 struct pattern_list *lst;
721 struct pattern *pattern;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200722 struct pattern *ret = NULL;
723 struct lru64 *lru = NULL;
724
725 if (pat_lru_tree) {
Willy Tarreauaee93142015-05-04 17:18:42 +0200726 unsigned long long seed = pat_lru_seed ^ (long)expr;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200727
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200728 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
Willy Tarreauf3045d22015-04-29 16:24:50 +0200729 pat_lru_tree, expr, expr->revision);
730 if (lru && lru->domain)
731 return lru->data;
732 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 list_for_each_entry(lst, &expr->patterns, list) {
735 pattern = &lst->pat;
736
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 if (pattern->len > smp->data.u.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738 continue;
739
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 end = smp->data.u.str.str + smp->data.u.str.len - pattern->len;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200741 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100742 if (icase) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200743 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100744 if (tolower(*c) != tolower(*pattern->ptr.str))
745 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200746 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) {
747 ret = pattern;
748 goto leave;
749 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 }
751 } else {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200752 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100753 if (*c != *pattern->ptr.str)
754 continue;
Willy Tarreauf3045d22015-04-29 16:24:50 +0200755 if (strncmp(pattern->ptr.str, c, pattern->len) == 0) {
756 ret = pattern;
757 goto leave;
758 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100759 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100760 }
761 }
Willy Tarreauf3045d22015-04-29 16:24:50 +0200762 leave:
763 if (lru)
Christopher Fauletf90ac552015-06-09 17:06:17 +0200764 lru64_commit(lru, ret, expr, expr->revision, NULL);
Willy Tarreauf3045d22015-04-29 16:24:50 +0200765
766 return ret;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100767}
768
769/* This one is used by other real functions. It checks that the pattern is
770 * included inside the tested string, but enclosed between the specified
771 * delimiters or at the beginning or end of the string. The delimiters are
772 * provided as an unsigned int made by make_4delim() and match up to 4 different
773 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
774 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200775static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100776{
777 int may_match, icase;
778 char *c, *end;
779 char *ps;
780 int pl;
781
782 pl = pattern->len;
783 ps = pattern->ptr.str;
784
785 while (pl > 0 && is_delimiter(*ps, delimiters)) {
786 pl--;
787 ps++;
788 }
789
790 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
791 pl--;
792
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200793 if (pl > smp->data.u.str.len)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100794 return PAT_NOMATCH;
795
796 may_match = 1;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200797 icase = mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 end = smp->data.u.str.str + smp->data.u.str.len - pl;
799 for (c = smp->data.u.str.str; c <= end; c++) {
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100800 if (is_delimiter(*c, delimiters)) {
801 may_match = 1;
802 continue;
803 }
804
805 if (!may_match)
806 continue;
807
808 if (icase) {
809 if ((tolower(*c) == tolower(*ps)) &&
810 (strncasecmp(ps, c, pl) == 0) &&
811 (c == end || is_delimiter(c[pl], delimiters)))
812 return PAT_MATCH;
813 } else {
814 if ((*c == *ps) &&
815 (strncmp(ps, c, pl) == 0) &&
816 (c == end || is_delimiter(c[pl], delimiters)))
817 return PAT_MATCH;
818 }
819 may_match = 0;
820 }
821 return PAT_NOMATCH;
822}
823
824/* Checks that the pattern is included inside the tested string, but enclosed
825 * between the delimiters '?' or '/' or at the beginning or end of the string.
826 * Delimiters at the beginning or end of the pattern are ignored.
827 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100828struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100829{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100830 struct pattern_list *lst;
831 struct pattern *pattern;
832
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100833 list_for_each_entry(lst, &expr->patterns, list) {
834 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200835 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100836 return pattern;
837 }
838 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100839}
840
841/* Checks that the pattern is included inside the tested string, but enclosed
842 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
843 * the string. Delimiters at the beginning or end of the pattern are ignored.
844 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100846{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100847 struct pattern_list *lst;
848 struct pattern *pattern;
849
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100850 list_for_each_entry(lst, &expr->patterns, list) {
851 pattern = &lst->pat;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200852 if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100853 return pattern;
854 }
855 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100856}
857
858/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100859struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100860{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100861 struct pattern_list *lst;
862 struct pattern *pattern;
863
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100864 list_for_each_entry(lst, &expr->patterns, list) {
865 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200866 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.sint) &&
867 (!pattern->val.range.max_set || smp->data.u.sint <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100868 return pattern;
869 }
870 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100871}
872
873/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100874struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100875{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 struct pattern_list *lst;
877 struct pattern *pattern;
878
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100879 list_for_each_entry(lst, &expr->patterns, list) {
880 pattern = &lst->pat;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200881 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.u.str.len) &&
882 (!pattern->val.range.max_set || smp->data.u.str.len <= pattern->val.range.max))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100883 return pattern;
884 }
885 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100886}
887
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100888struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100889{
890 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100891 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100892 struct in_addr *s;
893 struct ebmb_node *node;
894 struct pattern_tree *elt;
895 struct pattern_list *lst;
896 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100898 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200899 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100900 /* Lookup an IPv4 address in the expression's pattern tree using
901 * the longest match method.
902 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200903 s = &smp->data.u.ipv4;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100904 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
905 if (node) {
906 if (fill) {
907 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200908 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100909 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200910 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100911 static_pattern.type = SMP_T_IPV4;
912 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
913 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
914 return NULL;
915 }
916 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100917 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100918
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100919 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
920 * sample address to IPv6 with the mapping method using the ::ffff:
921 * prefix, and try to lookup in the IPv6 tree.
922 */
923 memset(&tmp6, 0, 10);
924 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200925 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.u.ipv4.s_addr;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100926 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
927 if (node) {
928 if (fill) {
929 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200930 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100931 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200932 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100933 static_pattern.type = SMP_T_IPV6;
934 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
935 static_pattern.val.ipv6.mask = elt->node.node.pfx;
936 }
937 return &static_pattern;
938 }
939 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100940
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100941 /* The input sample is IPv6. Try to match in the trees. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200942 if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100943 /* Lookup an IPv6 address in the expression's pattern tree using
944 * the longest match method.
945 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200946 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.u.ipv6);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100947 if (node) {
948 if (fill) {
949 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200950 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100951 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200952 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100953 static_pattern.type = SMP_T_IPV6;
954 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
955 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100956 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100957 return &static_pattern;
958 }
959
960 /* Try to convert 6 to 4 when the start of the ipv6 address match the
961 * following forms :
962 * - ::ffff:ip:v4 (ipv4 mapped)
963 * - ::0000:ip:v4 (old ipv4 mapped)
964 * - 2002:ip:v4:: (6to4)
965 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200966 if ((*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
967 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
968 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
969 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
970 *(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
971 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0)
972 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100973 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200974 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
975 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100976
977 /* Lookup an IPv4 address in the expression's pattern tree using the longest
978 * match method.
979 */
980 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
981 if (node) {
982 if (fill) {
983 elt = ebmb_entry(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +0200984 static_pattern.data = elt->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100985 static_pattern.ref = elt->ref;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +0200986 static_pattern.sflags = PAT_SF_TREE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100987 static_pattern.type = SMP_T_IPV4;
988 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
989 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
990 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100991 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100992 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100993 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100994 }
995 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100996
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100997 /* Lookup in the list. the list contain only IPv4 patterns */
998 list_for_each_entry(lst, &expr->patterns, list) {
999 pattern = &lst->pat;
1000
1001 /* The input sample is IPv4, use it as is. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001002 if (smp->data.type == SMP_T_IPV4) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001003 v4 = smp->data.u.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001004 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001005 else if (smp->data.type == SMP_T_IPV6) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001006 /* v4 match on a V6 sample. We want to check at least for
1007 * the following forms :
1008 * - ::ffff:ip:v4 (ipv4 mapped)
1009 * - ::0000:ip:v4 (old ipv4 mapped)
1010 * - 2002:ip:v4:: (6to4)
1011 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001012 if (*(uint32_t*)&smp->data.u.ipv6.s6_addr[0] == 0 &&
1013 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] == 0 &&
1014 (*(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == 0 ||
1015 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] == htonl(0xFFFF))) {
1016 v4 = *(uint32_t*)&smp->data.u.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001017 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001018 else if (*(uint16_t*)&smp->data.u.ipv6.s6_addr[0] == htons(0x2002)) {
1019 v4 = htonl((ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[2]) << 16) +
1020 ntohs(*(uint16_t*)&smp->data.u.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001021 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001022 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001023 continue;
Andreas Seltenreichf0653192016-03-03 20:08:35 +01001024 } else {
1025 /* impossible */
1026 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001027 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001028
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001029 /* Check if the input sample match the current pattern. */
1030 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001031 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001032 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001033 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +01001034}
1035
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001036void free_pattern_tree(struct eb_root *root)
1037{
1038 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001039 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001040
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001041 node = eb_first(root);
1042 while (node) {
1043 next = eb_next(node);
1044 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001045 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001046 free(elt->data);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +01001047 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001048 node = next;
1049 }
1050}
1051
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001052void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001053{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001054 struct pattern_list *pat, *tmp;
1055
1056 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001057 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001058 free(pat);
1059 }
1060
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001061 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001062 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +01001063 LIST_INIT(&expr->patterns);
1064}
1065
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001066void pat_prune_ptr(struct pattern_expr *expr)
1067{
1068 struct pattern_list *pat, *tmp;
1069
1070 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1071 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001072 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001073 free(pat);
1074 }
1075
1076 free_pattern_tree(&expr->pattern_tree);
1077 free_pattern_tree(&expr->pattern_tree_2);
1078 LIST_INIT(&expr->patterns);
1079}
1080
1081void pat_prune_reg(struct pattern_expr *expr)
1082{
1083 struct pattern_list *pat, *tmp;
1084
1085 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
1086 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001087 free(pat->pat.data);
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01001088 free(pat);
1089 }
1090
1091 free_pattern_tree(&expr->pattern_tree);
1092 free_pattern_tree(&expr->pattern_tree_2);
1093 LIST_INIT(&expr->patterns);
1094}
1095
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001096/*
1097 *
1098 * The following functions are used for the pattern indexation
1099 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001100 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101
1102int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001103{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001104 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001105
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001106 /* allocate pattern */
1107 patl = calloc(1, sizeof(*patl));
1108 if (!patl) {
1109 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001110 return 0;
1111 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001112
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001113 /* duplicate pattern */
1114 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001115
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116 /* chain pattern in the expression */
1117 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001118 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001119
1120 /* that's ok */
1121 return 1;
1122}
1123
1124int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1125{
1126 struct pattern_list *patl;
1127
1128 /* allocate pattern */
1129 patl = calloc(1, sizeof(*patl));
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001130 if (!patl) {
1131 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001132 return 0;
Thierry FOURNIER8aa83842015-02-06 17:50:55 +01001133 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001134
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001135 /* duplicate pattern */
1136 memcpy(&patl->pat, pat, sizeof(*pat));
1137 patl->pat.ptr.ptr = malloc(patl->pat.len);
1138 if (!patl->pat.ptr.ptr) {
1139 free(patl);
1140 memprintf(err, "out of memory while indexing pattern");
1141 return 0;
1142 }
1143 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* chain pattern in the expression */
1146 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001147 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001148
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001149 /* that's ok */
1150 return 1;
1151}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1154{
1155 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001156
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001157 /* allocate pattern */
1158 patl = calloc(1, sizeof(*patl));
1159 if (!patl) {
1160 memprintf(err, "out of memory while indexing pattern");
1161 return 0;
1162 }
1163
1164 /* duplicate pattern */
1165 memcpy(&patl->pat, pat, sizeof(*pat));
1166 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1167 if (!patl->pat.ptr.str) {
1168 free(patl);
1169 memprintf(err, "out of memory while indexing pattern");
1170 return 0;
1171 }
1172 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1173 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001174
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001175 /* chain pattern in the expression */
1176 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001177 expr->revision = rdtsc();
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001178
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001179 /* that's ok */
1180 return 1;
1181}
1182
Thierry Fournier8feaa662016-02-10 22:55:20 +01001183int pat_idx_list_reg_cap(struct pattern_expr *expr, struct pattern *pat, int cap, char **err)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001184{
1185 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001186
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001187 /* allocate pattern */
1188 patl = calloc(1, sizeof(*patl));
1189 if (!patl) {
1190 memprintf(err, "out of memory while indexing pattern");
1191 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001192 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001193
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001194 /* duplicate pattern */
1195 memcpy(&patl->pat, pat, sizeof(*pat));
1196
1197 /* allocate regex */
1198 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1199 if (!patl->pat.ptr.reg) {
1200 free(patl);
1201 memprintf(err, "out of memory while indexing pattern");
1202 return 0;
1203 }
1204
1205 /* compile regex */
Thierry Fournier8feaa662016-02-10 22:55:20 +01001206 if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg,
1207 !(expr->mflags & PAT_MF_IGNORE_CASE), cap, err)) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001208 free(patl->pat.ptr.reg);
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001209 free(patl);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001210 return 0;
1211 }
1212
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001213 /* chain pattern in the expression */
1214 LIST_ADDQ(&expr->patterns, &patl->list);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001215 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001216
1217 /* that's ok */
1218 return 1;
1219}
1220
Thierry Fournier8feaa662016-02-10 22:55:20 +01001221int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1222{
1223 return pat_idx_list_reg_cap(expr, pat, 0, err);
1224}
1225
1226int pat_idx_list_regm(struct pattern_expr *expr, struct pattern *pat, char **err)
1227{
1228 return pat_idx_list_reg_cap(expr, pat, 1, err);
1229}
1230
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001231int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1232{
1233 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001234 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001235
1236 /* Only IPv4 can be indexed */
1237 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001238 /* in IPv4 case, check if the mask is contiguous so that we can
1239 * insert the network into the tree. A continuous mask has only
1240 * ones on the left. This means that this mask + its lower bit
1241 * added once again is null.
1242 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001243 mask = ntohl(pat->val.ipv4.mask.s_addr);
1244 if (mask + (mask & -mask) == 0) {
1245 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001246
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001247 /* node memory allocation */
1248 node = calloc(1, sizeof(*node) + 4);
1249 if (!node) {
1250 memprintf(err, "out of memory while loading pattern");
1251 return 0;
1252 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001253
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001254 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001255 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001256 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001257
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001258 /* FIXME: insert <addr>/<mask> into the tree here */
1259 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1260 node->node.node.pfx = mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001261
1262 /* Insert the entry. */
1263 ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001264 expr->revision = rdtsc();
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001265
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001266 /* that's ok */
1267 return 1;
1268 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001269 else {
1270 /* If the mask is not contiguous, just add the pattern to the list */
1271 return pat_idx_list_val(expr, pat, err);
1272 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001273 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001274 else if (pat->type == SMP_T_IPV6) {
1275 /* IPv6 also can be indexed */
1276 node = calloc(1, sizeof(*node) + 16);
1277 if (!node) {
1278 memprintf(err, "out of memory while loading pattern");
1279 return 0;
1280 }
1281
1282 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001283 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001284 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001285
1286 /* FIXME: insert <addr>/<mask> into the tree here */
1287 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1288 node->node.node.pfx = pat->val.ipv6.mask;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001289
1290 /* Insert the entry. */
1291 ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001292 expr->revision = rdtsc();
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001293
1294 /* that's ok */
1295 return 1;
1296 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001297
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001298 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001299}
1300
1301int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1302{
1303 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001304 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001305
1306 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001307 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001308 memprintf(err, "internal error: string expected, but the type is '%s'",
1309 smp_to_type[pat->type]);
1310 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001311 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001312
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001313 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001314 if (expr->mflags & PAT_MF_IGNORE_CASE)
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001315 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001316
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001317 /* Process the key len */
1318 len = strlen(pat->ptr.str) + 1;
1319
1320 /* node memory allocation */
1321 node = calloc(1, sizeof(*node) + len);
1322 if (!node) {
1323 memprintf(err, "out of memory while loading pattern");
1324 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001325 }
1326
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001327 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001328 node->data = pat->data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001329 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001330
1331 /* copy the string */
1332 memcpy(node->node.key, pat->ptr.str, len);
1333
1334 /* index the new node */
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001335 ebst_insert(&expr->pattern_tree, &node->node);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001336 expr->revision = rdtsc();
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001337
1338 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001339 return 1;
1340}
1341
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001342int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1343{
1344 int len;
1345 struct pattern_tree *node;
1346
1347 /* Only string can be indexed */
1348 if (pat->type != SMP_T_STR) {
1349 memprintf(err, "internal error: string expected, but the type is '%s'",
1350 smp_to_type[pat->type]);
1351 return 0;
1352 }
1353
1354 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1355 if (expr->mflags & PAT_MF_IGNORE_CASE)
1356 return pat_idx_list_str(expr, pat, err);
1357
1358 /* Process the key len */
1359 len = strlen(pat->ptr.str);
1360
1361 /* node memory allocation */
1362 node = calloc(1, sizeof(*node) + len + 1);
1363 if (!node) {
1364 memprintf(err, "out of memory while loading pattern");
1365 return 0;
1366 }
1367
1368 /* copy the pointer to sample associated to this node */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001369 node->data = pat->data;
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001370 node->ref = pat->ref;
1371
1372 /* copy the string and the trailing zero */
1373 memcpy(node->node.key, pat->ptr.str, len + 1);
1374 node->node.node.pfx = len * 8;
1375
1376 /* index the new node */
1377 ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001378 expr->revision = rdtsc();
Willy Tarreaub1dd9bf2014-05-10 08:53:48 +02001379
1380 /* that's ok */
1381 return 1;
1382}
1383
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001384void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001385{
1386 struct pattern_list *pat;
1387 struct pattern_list *safe;
1388
1389 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1390 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001391 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001392 continue;
1393
1394 /* Delete and free entry. */
1395 LIST_DEL(&pat->list);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001396 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001397 free(pat);
1398 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001399 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001400}
1401
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001402void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001403{
1404 struct ebmb_node *node, *next_node;
1405 struct pattern_tree *elt;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001406
1407 /* browse each node of the tree for IPv4 addresses. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001408 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1409 node;
1410 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1411 /* Extract container of the tree node. */
1412 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001413
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001414 /* Check equality. */
1415 if (elt->ref != ref)
1416 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001417
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001418 /* Delete and free entry. */
1419 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001420 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001421 free(elt);
1422 }
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001423
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001424 /* Browse each node of the list for IPv4 addresses. */
1425 pat_del_list_val(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001426
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001427 /* browse each node of the tree for IPv6 addresses. */
1428 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1429 node;
1430 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1431 /* Extract container of the tree node. */
1432 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001433
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001434 /* Check equality. */
1435 if (elt->ref != ref)
1436 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001437
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001438 /* Delete and free entry. */
1439 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001440 free(elt->data);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001441 free(elt);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001442 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001443 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001444}
1445
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001446void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001447{
1448 struct pattern_list *pat;
1449 struct pattern_list *safe;
1450
1451 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1452 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001453 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001454 continue;
1455
1456 /* Delete and free entry. */
1457 LIST_DEL(&pat->list);
1458 free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001459 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001460 free(pat);
1461 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001462 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001463}
1464
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001465void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001466{
1467 struct ebmb_node *node, *next_node;
1468 struct pattern_tree *elt;
1469
Thierry FOURNIER73bc2852015-02-06 17:53:54 +01001470 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1471 if (expr->mflags & PAT_MF_IGNORE_CASE)
1472 return pat_del_list_ptr(expr, ref);
1473
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001474 /* browse each node of the tree. */
1475 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1476 node;
1477 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1478 /* Extract container of the tree node. */
1479 elt = container_of(node, struct pattern_tree, node);
1480
1481 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001482 if (elt->ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001483 continue;
1484
1485 /* Delete and free entry. */
1486 ebmb_delete(node);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001487 free(elt->data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001488 free(elt);
1489 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001490 expr->revision = rdtsc();
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001491}
1492
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001493void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001494{
1495 struct pattern_list *pat;
1496 struct pattern_list *safe;
1497
1498 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1499 /* Check equality. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001500 if (pat->pat.ref != ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001501 continue;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001502
1503 /* Delete and free entry. */
1504 LIST_DEL(&pat->list);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001505 regex_free(pat->pat.ptr.ptr);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001506 free(pat->pat.data);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001507 free(pat);
1508 }
Willy Tarreau72f073b2015-04-29 17:53:47 +02001509 expr->revision = rdtsc();
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001510}
1511
1512void pattern_init_expr(struct pattern_expr *expr)
1513{
1514 LIST_INIT(&expr->patterns);
Willy Tarreau72f073b2015-04-29 17:53:47 +02001515 expr->revision = 0;
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001516 expr->pattern_tree = EB_ROOT;
1517 expr->pattern_tree_2 = EB_ROOT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001518}
1519
1520void pattern_init_head(struct pattern_head *head)
1521{
1522 LIST_INIT(&head->head);
1523}
1524
1525/* The following functions are relative to the management of the reference
1526 * lists. These lists are used to store the original pattern and associated
1527 * value as string form.
1528 *
1529 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001530 *
1531 * The pattern reference are stored with two identifiers: the unique_id and
1532 * the reference.
1533 *
1534 * The reference identify a file. Each file with the same name point to the
1535 * same reference. We can register many times one file. If the file is modified,
1536 * all his dependencies are also modified. The reference can be used with map or
1537 * acl.
1538 *
1539 * The unique_id identify inline acl. The unique id is unique for each acl.
1540 * You cannot force the same id in the configuration file, because this repoort
1541 * an error.
1542 *
1543 * A particular case appears if the filename is a number. In this case, the
1544 * unique_id is set with the number represented by the filename and the
1545 * reference is also set. This method prevent double unique_id.
1546 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001547 */
1548
1549/* This function lookup for reference. If the reference is found, they return
1550 * pointer to the struct pat_ref, else return NULL.
1551 */
1552struct pat_ref *pat_ref_lookup(const char *reference)
1553{
1554 struct pat_ref *ref;
1555
1556 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001557 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001558 return ref;
1559 return NULL;
1560}
1561
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001562/* This function lookup for unique id. If the reference is found, they return
1563 * pointer to the struct pat_ref, else return NULL.
1564 */
1565struct pat_ref *pat_ref_lookupid(int unique_id)
1566{
1567 struct pat_ref *ref;
1568
1569 list_for_each_entry(ref, &pattern_reference, list)
1570 if (ref->unique_id == unique_id)
1571 return ref;
1572 return NULL;
1573}
1574
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001575/* This function remove all pattern matching the pointer <refelt> from
1576 * the the reference and from each expr member of the reference. This
1577 * function returns 1 if the deletion is done and return 0 is the entry
1578 * is not found.
1579 */
1580int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1581{
1582 struct pattern_expr *expr;
1583 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001584 struct bref *bref, *back;
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001585
1586 /* delete pattern from reference */
1587 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1588 if (elt == refelt) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001589 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1590 /*
1591 * we have to unlink all watchers. We must not relink them if
1592 * this elt was the last one in the list.
1593 */
1594 LIST_DEL(&bref->users);
1595 LIST_INIT(&bref->users);
1596 if (elt->list.n != &ref->head)
1597 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1598 bref->ref = elt->list.n;
1599 }
peter caiaede6dd2015-10-07 00:07:43 -07001600 list_for_each_entry(expr, &ref->pat, list)
1601 pattern_delete(expr, elt);
1602
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001603 LIST_DEL(&elt->list);
1604 free(elt->sample);
1605 free(elt->pattern);
1606 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001607 return 1;
1608 }
1609 }
1610 return 0;
1611}
1612
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001613/* This function remove all pattern match <key> from the the reference
1614 * and from each expr member of the reference. This fucntion returns 1
1615 * if the deletion is done and return 0 is the entry is not found.
1616 */
1617int pat_ref_delete(struct pat_ref *ref, const char *key)
1618{
1619 struct pattern_expr *expr;
1620 struct pat_ref_elt *elt, *safe;
Emeric Brun8d85aa42017-06-29 15:40:33 +02001621 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622 int found = 0;
1623
1624 /* delete pattern from reference */
1625 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1626 if (strcmp(key, elt->pattern) == 0) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02001627 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
1628 /*
1629 * we have to unlink all watchers. We must not relink them if
1630 * this elt was the last one in the list.
1631 */
1632 LIST_DEL(&bref->users);
1633 LIST_INIT(&bref->users);
1634 if (elt->list.n != &ref->head)
1635 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
1636 bref->ref = elt->list.n;
1637 }
Dirkjan Bussink07fcaaa2014-04-28 22:57:16 +00001638 list_for_each_entry(expr, &ref->pat, list)
1639 pattern_delete(expr, elt);
1640
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001641 LIST_DEL(&elt->list);
1642 free(elt->sample);
1643 free(elt->pattern);
1644 free(elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001645
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001646 found = 1;
1647 }
1648 }
1649
1650 if (!found)
1651 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001652 return 1;
1653}
1654
Baptiste Assmann953f74d2014-04-25 16:57:03 +02001655/*
1656 * find and return an element <elt> matching <key> in a reference <ref>
1657 * return NULL if not found
1658 */
1659struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1660{
1661 struct pat_ref_elt *elt;
1662
1663 list_for_each_entry(elt, &ref->head, list) {
1664 if (strcmp(key, elt->pattern) == 0)
1665 return elt;
1666 }
1667
1668 return NULL;
1669}
1670
1671
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001672 /* This function modify the sample of the first pattern that match the <key>. */
1673static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001674 const char *value, char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675{
1676 struct pattern_expr *expr;
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001677 struct sample_data **data;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001678 char *sample;
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02001679 struct sample_data test;
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001680
1681 /* Try all needed converters. */
1682 list_for_each_entry(expr, &ref->pat, list) {
1683 if (!expr->pat_head->parse_smp)
1684 continue;
1685
1686 if (!expr->pat_head->parse_smp(value, &test)) {
1687 memprintf(err, "unable to parse '%s'", value);
1688 return 0;
1689 }
1690 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001691
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001692 /* Modify pattern from reference. */
1693 sample = strdup(value);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001694 if (!sample) {
1695 memprintf(err, "out of memory error");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001696 return 0;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001697 }
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001698 free(elt->sample);
1699 elt->sample = sample;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001700
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001701 /* Load sample in each reference. All the conversion are tested
1702 * below, normally these calls dosn't fail.
1703 */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001704 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001705 if (!expr->pat_head->parse_smp)
1706 continue;
1707
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001708 data = pattern_find_smp(expr, elt);
1709 if (data && *data && !expr->pat_head->parse_smp(sample, *data))
1710 *data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001711 }
1712
Thierry FOURNIER149e0fe2014-01-29 19:35:06 +01001713 return 1;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001714}
1715
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001716/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001717int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001718{
1719 struct pat_ref_elt *elt;
1720
1721 /* Look for pattern in the reference. */
1722 list_for_each_entry(elt, &ref->head, list) {
1723 if (elt == refelt) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001724 if (!pat_ref_set_elt(ref, elt, value, err))
1725 return 0;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001726 return 1;
1727 }
1728 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001729
1730 memprintf(err, "key or pattern not found");
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001731 return 0;
1732}
1733
1734/* This function modify the sample of the first pattern that match the <key>. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001735int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001736{
1737 struct pat_ref_elt *elt;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001738 int found = 0;
1739 char *_merr;
1740 char **merr;
1741
1742 if (err) {
1743 merr = &_merr;
1744 *merr = NULL;
1745 }
1746 else
1747 merr = NULL;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001748
1749 /* Look for pattern in the reference. */
1750 list_for_each_entry(elt, &ref->head, list) {
1751 if (strcmp(key, elt->pattern) == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001752 if (!pat_ref_set_elt(ref, elt, value, merr)) {
1753 if (!found)
1754 *err = *merr;
1755 else {
1756 memprintf(err, "%s, %s", *err, *merr);
1757 free(*merr);
1758 *merr = NULL;
1759 }
1760 }
1761 found = 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001762 }
1763 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001764
1765 if (!found) {
1766 memprintf(err, "entry not found");
1767 return 0;
1768 }
1769 return 1;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01001770}
1771
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772/* This function create new reference. <ref> is the reference name.
1773 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1774 * be unique. The user must check the reference with "pat_ref_lookup()"
1775 * before calling this function. If the fucntion fail, it return NULL,
1776 * else return new struct pat_ref.
1777 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001778struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001779{
1780 struct pat_ref *ref;
1781
1782 ref = malloc(sizeof(*ref));
1783 if (!ref)
1784 return NULL;
1785
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001786 if (display) {
1787 ref->display = strdup(display);
1788 if (!ref->display) {
1789 free(ref);
1790 return NULL;
1791 }
1792 }
1793 else
1794 ref->display = NULL;
1795
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001796 ref->reference = strdup(reference);
1797 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001798 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001799 free(ref);
1800 return NULL;
1801 }
1802
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001803 ref->flags = flags;
1804 ref->unique_id = -1;
1805
1806 LIST_INIT(&ref->head);
1807 LIST_INIT(&ref->pat);
1808
1809 LIST_ADDQ(&pattern_reference, &ref->list);
1810
1811 return ref;
1812}
1813
1814/* This function create new reference. <unique_id> is the unique id. If
1815 * the value of <unique_id> is -1, the unique id is calculated later.
1816 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1817 * be unique. The user must check the reference with "pat_ref_lookup()"
1818 * or pat_ref_lookupid before calling this function. If the function
1819 * fail, it return NULL, else return new struct pat_ref.
1820 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001821struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001822{
1823 struct pat_ref *ref;
1824
1825 ref = malloc(sizeof(*ref));
1826 if (!ref)
1827 return NULL;
1828
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001829 if (display) {
1830 ref->display = strdup(display);
1831 if (!ref->display) {
1832 free(ref);
1833 return NULL;
1834 }
1835 }
1836 else
1837 ref->display = NULL;
1838
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001839 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001840 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001841 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001842 LIST_INIT(&ref->head);
1843 LIST_INIT(&ref->pat);
1844
1845 LIST_ADDQ(&pattern_reference, &ref->list);
1846
1847 return ref;
1848}
1849
1850/* This function adds entry to <ref>. It can failed with memory error.
1851 * If the function fails, it returns 0.
1852 */
1853int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1854{
1855 struct pat_ref_elt *elt;
1856
1857 elt = malloc(sizeof(*elt));
1858 if (!elt)
1859 return 0;
1860
1861 elt->line = line;
1862
1863 elt->pattern = strdup(pattern);
1864 if (!elt->pattern) {
1865 free(elt);
1866 return 0;
1867 }
1868
1869 if (sample) {
1870 elt->sample = strdup(sample);
1871 if (!elt->sample) {
1872 free(elt->pattern);
1873 free(elt);
1874 return 0;
1875 }
1876 }
1877 else
1878 elt->sample = NULL;
1879
Emeric Brun8d85aa42017-06-29 15:40:33 +02001880 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001881 LIST_ADDQ(&ref->head, &elt->list);
1882
1883 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001884}
1885
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001886/* This function create sample found in <elt>, parse the pattern also
1887 * found in <elt> and insert it in <expr>. The function copy <patflags>
1888 * in <expr>. If the function fails, it returns0 and <err> is filled.
1889 * In succes case, the function returns 1.
1890 */
1891static inline
1892int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1893 int patflags, char **err)
1894{
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001895 struct sample_data *data;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001896 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001897
1898 /* Create sample */
1899 if (elt->sample && expr->pat_head->parse_smp) {
1900 /* New sample. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001901 data = malloc(sizeof(*data));
1902 if (!data)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903 return 0;
1904
1905 /* Parse value. */
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001906 if (!expr->pat_head->parse_smp(elt->sample, data)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907 memprintf(err, "unable to parse '%s'", elt->sample);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001908 free(data);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001909 return 0;
1910 }
1911
1912 }
1913 else
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001914 data = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001915
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001916 /* initialise pattern */
1917 memset(&pattern, 0, sizeof(pattern));
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001918 pattern.data = data;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001919 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001920
1921 /* parse pattern */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001922 if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001923 free(data);
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001924 return 0;
1925 }
1926
1927 /* index pattern */
1928 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001929 free(data);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001930 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001931 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001932
1933 return 1;
1934}
1935
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001936/* This function adds entry to <ref>. It can failed with memory error. The new
1937 * entry is added at all the pattern_expr registered in this reference. The
1938 * function stop on the first error encountered. It returns 0 and err is
1939 * filled. If an error is encountered, the complete add operation is cancelled.
1940 * If the insertion is a success the function returns 1.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001941 */
1942int pat_ref_add(struct pat_ref *ref,
1943 const char *pattern, const char *sample,
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001944 char **err)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001945{
1946 struct pat_ref_elt *elt;
1947 struct pattern_expr *expr;
1948
1949 elt = malloc(sizeof(*elt));
1950 if (!elt) {
1951 memprintf(err, "out of memory error");
1952 return 0;
1953 }
1954
1955 elt->line = -1;
1956
1957 elt->pattern = strdup(pattern);
1958 if (!elt->pattern) {
1959 free(elt);
1960 memprintf(err, "out of memory error");
1961 return 0;
1962 }
1963
1964 if (sample) {
1965 elt->sample = strdup(sample);
1966 if (!elt->sample) {
1967 free(elt->pattern);
1968 free(elt);
1969 memprintf(err, "out of memory error");
1970 return 0;
1971 }
1972 }
1973 else
1974 elt->sample = NULL;
1975
Emeric Brun8d85aa42017-06-29 15:40:33 +02001976 LIST_INIT(&elt->back_refs);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001977 LIST_ADDQ(&ref->head, &elt->list);
1978
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001979 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02001980 if (!pat_ref_push(elt, expr, 0, err)) {
Thierry FOURNIER31db4ae2014-01-30 00:27:15 +01001981 /* If the insertion fails, try to delete all the added entries. */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01001982 pat_ref_delete_by_id(ref, elt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001983 return 0;
1984 }
1985 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001986 return 1;
1987}
1988
Thierry FOURNIER46006bd2014-03-21 21:45:15 +01001989/* This function prune <ref>, replace all reference by the references
1990 * of <replace>, and reindex all the news values.
1991 *
1992 * The pattern are loaded in best effort and the errors are ignored,
1993 * but writed in the logs.
1994 */
1995void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1996{
1997 struct pattern_expr *expr;
1998 struct pat_ref_elt *elt;
1999 char *err = NULL;
2000
2001 pat_ref_prune(ref);
2002
2003 LIST_ADD(&replace->head, &ref->head);
2004 LIST_DEL(&replace->head);
2005
2006 list_for_each_entry(elt, &ref->head, list) {
2007 list_for_each_entry(expr, &ref->pat, list) {
2008 if (!pat_ref_push(elt, expr, 0, &err)) {
2009 send_log(NULL, LOG_NOTICE, "%s", err);
2010 free(err);
2011 err = NULL;
2012 }
2013 }
2014 }
2015}
2016
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002017/* This function prune all entries of <ref>. This function
2018 * prune the associated pattern_expr.
2019 */
2020void pat_ref_prune(struct pat_ref *ref)
2021{
2022 struct pat_ref_elt *elt, *safe;
2023 struct pattern_expr *expr;
Emeric Brun8d85aa42017-06-29 15:40:33 +02002024 struct bref *bref, *back;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002025
2026 list_for_each_entry_safe(elt, safe, &ref->head, list) {
Emeric Brun8d85aa42017-06-29 15:40:33 +02002027 list_for_each_entry_safe(bref, back, &elt->back_refs, users) {
2028 /*
2029 * we have to unlink all watchers. We must not relink them if
2030 * this elt was the last one in the list.
2031 */
2032 LIST_DEL(&bref->users);
2033 LIST_INIT(&bref->users);
2034 if (elt->list.n != &ref->head)
2035 LIST_ADDQ(&LIST_ELEM(elt->list.n, struct stream *, list)->back_refs, &bref->users);
2036 bref->ref = elt->list.n;
2037 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002038 LIST_DEL(&elt->list);
2039 free(elt->pattern);
2040 free(elt->sample);
2041 free(elt);
2042 }
2043
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002044 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 expr->pat_head->prune(expr);
2046}
2047
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002048/* This function lookup for existing reference <ref> in pattern_head <head>. */
2049struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
2050{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002051 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002052
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002053 list_for_each_entry(expr, &head->head, list)
2054 if (expr->expr->ref == ref)
2055 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002056 return NULL;
2057}
2058
2059/* This function create new pattern_expr associated to the reference <ref>.
2060 * <ref> can be NULL. If an error is occured, the function returns NULL and
2061 * <err> is filled. Otherwise, the function returns new pattern_expr linked
2062 * with <head> and <ref>.
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002063 *
2064 * The returned value can be a alredy filled pattern list, in this case the
2065 * flag <reuse> is set.
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002066 */
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002067struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref,
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002068 int patflags, char **err, int *reuse)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002069{
2070 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002071 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002072
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002073 if (reuse)
2074 *reuse = 0;
2075
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002076 /* Memory and initialization of the chain element. */
2077 list = malloc(sizeof(*list));
2078 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002079 memprintf(err, "out of memory");
2080 return NULL;
2081 }
2082
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002083 /* Look for existing similar expr. No that only the index, parse and
2084 * parse_smp function must be identical for having similar pattern.
2085 * The other function depends of theses first.
2086 */
2087 if (ref) {
2088 list_for_each_entry(expr, &ref->pat, list)
2089 if (expr->pat_head->index == head->index &&
2090 expr->pat_head->parse == head->parse &&
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002091 expr->pat_head->parse_smp == head->parse_smp &&
2092 expr->mflags == patflags)
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002093 break;
2094 if (&expr->list == &ref->pat)
2095 expr = NULL;
2096 }
2097 else
2098 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002099
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002100 /* If no similar expr was found, we create new expr. */
2101 if (!expr) {
2102 /* Get a lot of memory for the expr struct. */
2103 expr = malloc(sizeof(*expr));
2104 if (!expr) {
Andreas Seltenreiche6e22e82016-03-03 20:20:23 +01002105 free(list);
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002106 memprintf(err, "out of memory");
2107 return NULL;
2108 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002109
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002110 /* Initialize this new expr. */
2111 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002112
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002113 /* Copy the pattern matching and indexing flags. */
2114 expr->mflags = patflags;
2115
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002116 /* This new pattern expression reference one of his heads. */
2117 expr->pat_head = head;
2118
2119 /* Link with ref, or to self to facilitate LIST_DEL() */
2120 if (ref)
2121 LIST_ADDQ(&ref->pat, &expr->list);
2122 else
2123 LIST_INIT(&expr->list);
2124
2125 expr->ref = ref;
2126
2127 /* We must free this pattern if it is no more used. */
2128 list->do_free = 1;
2129 }
2130 else {
2131 /* If the pattern used already exists, it is already linked
2132 * with ref and we must not free it.
2133 */
2134 list->do_free = 0;
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002135 if (reuse)
2136 *reuse = 1;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002137 }
2138
2139 /* The new list element reference the pattern_expr. */
2140 list->expr = expr;
2141
2142 /* Link the list element with the pattern_head. */
2143 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144 return expr;
2145}
2146
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002147/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2148 * be returned there on errors and the caller will have to free it.
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002149 *
2150 * The file contains one key + value per line. Lines which start with '#' are
2151 * ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
2152 * then the first "word" (series of non-space/tabs characters), and the value is
2153 * what follows this series of space/tab till the end of the line excluding
2154 * trailing spaces/tabs.
2155 *
2156 * Example :
2157 *
2158 * # this is a comment and is ignored
2159 * 62.212.114.60 1wt.eu \n
2160 * <-><-----------><---><----><---->
2161 * | | | | `--- trailing spaces ignored
2162 * | | | `-------- value
2163 * | | `--------------- middle spaces ignored
2164 * | `------------------------ key
2165 * `-------------------------------- leading spaces ignored
2166 *
2167 * Return non-zero in case of succes, otherwise 0.
2168 */
2169int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
2170{
2171 FILE *file;
2172 char *c;
2173 int ret = 0;
2174 int line = 0;
2175 char *key_beg;
2176 char *key_end;
2177 char *value_beg;
2178 char *value_end;
2179
2180 file = fopen(filename, "r");
2181 if (!file) {
2182 memprintf(err, "failed to open pattern file <%s>", filename);
2183 return 0;
2184 }
2185
2186 /* now parse all patterns. The file may contain only one pattern
2187 * followed by one value per line. The start spaces, separator spaces
2188 * and and spaces are stripped. Each can contain comment started by '#'
2189 */
2190 while (fgets(trash.str, trash.size, file) != NULL) {
2191 line++;
2192 c = trash.str;
2193
2194 /* ignore lines beginning with a dash */
2195 if (*c == '#')
2196 continue;
2197
2198 /* strip leading spaces and tabs */
2199 while (*c == ' ' || *c == '\t')
2200 c++;
2201
2202 /* empty lines are ignored too */
2203 if (*c == '\0' || *c == '\r' || *c == '\n')
2204 continue;
2205
2206 /* look for the end of the key */
2207 key_beg = c;
2208 while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2209 c++;
2210
2211 key_end = c;
2212
2213 /* strip middle spaces and tabs */
2214 while (*c == ' ' || *c == '\t')
2215 c++;
2216
2217 /* look for the end of the value, it is the end of the line */
2218 value_beg = c;
2219 while (*c && *c != '\n' && *c != '\r')
2220 c++;
2221 value_end = c;
2222
2223 /* trim possibly trailing spaces and tabs */
2224 while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2225 value_end--;
2226
2227 /* set final \0 and check entries */
2228 *key_end = '\0';
2229 *value_end = '\0';
2230
2231 /* insert values */
2232 if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2233 memprintf(err, "out of memory");
2234 goto out_close;
2235 }
2236 }
2237
2238 /* succes */
2239 ret = 1;
2240
2241 out_close:
2242 fclose(file);
2243 return ret;
2244}
2245
2246/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2247 * be returned there on errors and the caller will have to free it.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002248 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002249int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002250{
2251 FILE *file;
2252 char *c;
2253 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002254 int ret = 0;
2255 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002256
2257 file = fopen(filename, "r");
2258 if (!file) {
2259 memprintf(err, "failed to open pattern file <%s>", filename);
2260 return 0;
2261 }
2262
2263 /* now parse all patterns. The file may contain only one pattern per
2264 * line. If the line contains spaces, they will be part of the pattern.
2265 * The pattern stops at the first CR, LF or EOF encountered.
2266 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002267 while (fgets(trash.str, trash.size, file) != NULL) {
2268 line++;
2269 c = trash.str;
2270
2271 /* ignore lines beginning with a dash */
2272 if (*c == '#')
2273 continue;
2274
2275 /* strip leading spaces and tabs */
2276 while (*c == ' ' || *c == '\t')
2277 c++;
2278
2279
2280 arg = c;
2281 while (*c && *c != '\n' && *c != '\r')
2282 c++;
2283 *c = 0;
2284
2285 /* empty lines are ignored too */
2286 if (c == arg)
2287 continue;
2288
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002289 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002290 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2291 goto out_close;
2292 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002293 }
2294
2295 ret = 1; /* success */
2296
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002297 out_close:
2298 fclose(file);
2299 return ret;
2300}
2301
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002302int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002303 const char *filename, int patflags, int load_smp,
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002304 char **err, const char *file, int line)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002305{
2306 struct pat_ref *ref;
2307 struct pattern_expr *expr;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002308 struct pat_ref_elt *elt;
Willy Tarreau4deaf392014-11-26 13:17:03 +01002309 int reuse = 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002310
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002311 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002312 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002313
2314 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002315 if (!ref) {
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002316 chunk_printf(&trash,
2317 "pattern loaded from file '%s' used by %s at file '%s' line %d",
2318 filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2319
2320 ref = pat_ref_new(filename, trash.str, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002321 if (!ref) {
2322 memprintf(err, "out of memory");
2323 return 0;
2324 }
2325
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002326 if (load_smp) {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002327 ref->flags |= PAT_REF_SMP;
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002328 if (!pat_ref_read_from_file_smp(ref, filename, err))
2329 return 0;
2330 }
2331 else {
2332 if (!pat_ref_read_from_file(ref, filename, err))
2333 return 0;
2334 }
2335 }
2336 else {
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002337 /* The reference already exists, check the map compatibility. */
2338
2339 /* If the load require samples and the flag PAT_REF_SMP is not set,
2340 * the reference doesn't contain sample, and cannot be used.
2341 */
2342 if (load_smp) {
2343 if (!(ref->flags & PAT_REF_SMP)) {
2344 memprintf(err, "The file \"%s\" is already used as one column file "
2345 "and cannot be used by as two column file.",
2346 filename);
2347 return 0;
2348 }
2349 }
2350 else {
2351 /* The load doesn't require samples. If the flag PAT_REF_SMP is
2352 * set, the reference contains a sample, and cannot be used.
2353 */
2354 if (ref->flags & PAT_REF_SMP) {
2355 memprintf(err, "The file \"%s\" is already used as two column file "
2356 "and cannot be used by as one column file.",
2357 filename);
2358 return 0;
2359 }
2360 }
2361
Thierry FOURNIER94580c92014-02-11 14:36:45 +01002362 /* Extends display */
2363 chunk_printf(&trash, "%s", ref->display);
2364 chunk_appendf(&trash, ", by %s at file '%s' line %d",
2365 refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2366 free(ref->display);
2367 ref->display = strdup(trash.str);
2368 if (!ref->display) {
2369 memprintf(err, "out of memory");
2370 return 0;
2371 }
2372
Thierry FOURNIERc0bd9102014-01-29 12:32:58 +01002373 /* Merge flags. */
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002374 ref->flags |= refflags;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002375 }
2376
2377 /* Now, we can loading patterns from the reference. */
2378
2379 /* Lookup for existing reference in the head. If the reference
2380 * doesn't exists, create it.
2381 */
2382 expr = pattern_lookup_expr(head, ref);
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002383 if (!expr || (expr->mflags != patflags)) {
Emeric Brun7d27f3c2017-07-03 17:54:23 +02002384 expr = pattern_new_expr(head, ref, patflags, err, &reuse);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002385 if (!expr)
2386 return 0;
2387 }
2388
Thierry FOURNIER315ec422014-11-24 11:14:42 +01002389 /* The returned expression may be not empty, because the function
2390 * "pattern_new_expr" lookup for similar pattern list and can
2391 * reuse a already filled pattern list. In this case, we can not
2392 * reload the patterns.
2393 */
2394 if (reuse)
2395 return 1;
2396
Thierry FOURNIER39bef452014-01-29 13:29:45 +01002397 /* Load reference content in the pattern expression. */
2398 list_for_each_entry(elt, &ref->head, list) {
2399 if (!pat_ref_push(elt, expr, patflags, err)) {
2400 if (elt->line > 0)
2401 memprintf(err, "%s at line %d of file '%s'",
2402 *err, elt->line, filename);
2403 return 0;
2404 }
2405 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002406
2407 return 1;
2408}
2409
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002410/* This function executes a pattern match on a sample. It applies pattern <expr>
2411 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2412 * non-null if the sample match. If <fill> is true and the sample match, the
2413 * function returns the matched pattern. In many cases, this pattern can be a
2414 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002415 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002416struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002417{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002418 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002419 struct pattern *pat;
2420
2421 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002422 if (fill) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002423 static_pattern.data = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002424 static_pattern.ref = NULL;
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002425 static_pattern.sflags = 0;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002426 static_pattern.type = SMP_T_SINT;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002427 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002428 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002429 return &static_pattern;
2430 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002431
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002432 /* convert input to string */
2433 if (!sample_convert(smp, head->expect_type))
2434 return NULL;
2435
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002436 list_for_each_entry(list, &head->head, list) {
2437 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002438 if (pat)
2439 return pat;
2440 }
2441 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002442}
2443
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002444/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002445void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002446{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002447 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002448
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002449 list_for_each_entry_safe(list, safe, &head->head, list) {
2450 LIST_DEL(&list->list);
2451 if (list->do_free) {
2452 LIST_DEL(&list->expr->list);
2453 head->prune(list->expr);
2454 free(list->expr);
2455 }
2456 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002457 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002458}
2459
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002460/* This function lookup for a pattern matching the <key> and return a
2461 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2462 * the function returns NULL. If the key cannot be parsed, the function
2463 * fill <err>.
2464 */
Thierry FOURNIER12ba0c22015-08-14 00:02:11 +02002465struct sample_data **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002466{
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002467 struct ebmb_node *node;
2468 struct pattern_tree *elt;
2469 struct pattern_list *pat;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002470
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002471 for (node = ebmb_first(&expr->pattern_tree);
2472 node;
2473 node = ebmb_next(node)) {
2474 elt = container_of(node, struct pattern_tree, node);
2475 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002476 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002477 }
2478
2479 for (node = ebmb_first(&expr->pattern_tree_2);
2480 node;
2481 node = ebmb_next(node)) {
2482 elt = container_of(node, struct pattern_tree, node);
2483 if (elt->ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002484 return &elt->data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002485 }
2486
2487 list_for_each_entry(pat, &expr->patterns, list)
2488 if (pat->pat.ref == ref)
Thierry FOURNIER503bb092015-08-19 08:35:43 +02002489 return &pat->pat.data;
Thierry FOURNIERe369ca22014-01-29 16:24:55 +01002490
2491 return NULL;
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002492}
2493
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002494/* This function search all the pattern matching the <key> and delete it.
2495 * If the parsing of the input key fails, the function returns 0 and the
2496 * <err> is filled, else return 1;
2497 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002498int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002499{
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +01002500 expr->pat_head->delete(expr, ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002501 return 1;
2502}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002503
2504/* This function finalize the configuration parsing. Its set all the
2505 * automatic ids
2506 */
2507void pattern_finalize_config(void)
2508{
2509 int i = 0;
2510 struct pat_ref *ref, *ref2, *ref3;
2511 struct list pr = LIST_HEAD_INIT(pr);
2512
Willy Tarreauf3045d22015-04-29 16:24:50 +02002513 pat_lru_seed = random();
2514 if (global.tune.pattern_cache)
2515 pat_lru_tree = lru64_new(global.tune.pattern_cache);
2516
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002517 list_for_each_entry(ref, &pattern_reference, list) {
2518 if (ref->unique_id == -1) {
2519 /* Look for the first free id. */
2520 while (1) {
2521 list_for_each_entry(ref2, &pattern_reference, list) {
2522 if (ref2->unique_id == i) {
2523 i++;
2524 break;
2525 }
2526 }
Willy Tarreau3b786962014-04-26 12:37:25 +02002527 if (&ref2->list == &pattern_reference)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002528 break;
2529 }
2530
2531 /* Uses the unique id and increment it for the next entry. */
2532 ref->unique_id = i;
2533 i++;
2534 }
2535 }
2536
2537 /* This sort the reference list by id. */
2538 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2539 LIST_DEL(&ref->list);
2540 list_for_each_entry(ref3, &pr, list) {
2541 if (ref->unique_id < ref3->unique_id) {
2542 LIST_ADDQ(&ref3->list, &ref->list);
2543 break;
2544 }
2545 }
2546 if (&ref3->list == &pr)
2547 LIST_ADDQ(&pr, &ref->list);
2548 }
2549
2550 /* swap root */
2551 LIST_ADD(&pr, &pattern_reference);
2552 LIST_DEL(&pr);
2553}