blob: eac123dac7f4d0183abf7e1f52757db333749215 [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
22#include <proto/pattern.h>
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010023#include <proto/sample.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010024
25#include <ebsttree.h>
26
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010027char *pat_match_names[PAT_MATCH_NUM] = {
28 [PAT_MATCH_FOUND] = "found",
29 [PAT_MATCH_BOOL] = "bool",
30 [PAT_MATCH_INT] = "int",
31 [PAT_MATCH_IP] = "ip",
32 [PAT_MATCH_BIN] = "bin",
33 [PAT_MATCH_LEN] = "len",
34 [PAT_MATCH_STR] = "str",
35 [PAT_MATCH_BEG] = "beg",
36 [PAT_MATCH_SUB] = "sub",
37 [PAT_MATCH_DIR] = "dir",
38 [PAT_MATCH_DOM] = "dom",
39 [PAT_MATCH_END] = "end",
40 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010041};
42
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044 [PAT_MATCH_FOUND] = pat_parse_nothing,
45 [PAT_MATCH_BOOL] = pat_parse_nothing,
46 [PAT_MATCH_INT] = pat_parse_int,
47 [PAT_MATCH_IP] = pat_parse_ip,
48 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIER5d344082014-01-27 14:19:53 +010049 [PAT_MATCH_LEN] = pat_parse_int,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010050 [PAT_MATCH_STR] = pat_parse_str,
51 [PAT_MATCH_BEG] = pat_parse_str,
52 [PAT_MATCH_SUB] = pat_parse_str,
53 [PAT_MATCH_DIR] = pat_parse_str,
54 [PAT_MATCH_DOM] = pat_parse_str,
55 [PAT_MATCH_END] = pat_parse_str,
56 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057};
58
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010059int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
60 [PAT_MATCH_FOUND] = pat_idx_list_val,
61 [PAT_MATCH_BOOL] = pat_idx_list_val,
62 [PAT_MATCH_INT] = pat_idx_list_val,
63 [PAT_MATCH_IP] = pat_idx_tree_ip,
64 [PAT_MATCH_BIN] = pat_idx_list_ptr,
65 [PAT_MATCH_LEN] = pat_idx_list_val,
66 [PAT_MATCH_STR] = pat_idx_tree_str,
67 [PAT_MATCH_BEG] = pat_idx_list_str,
68 [PAT_MATCH_SUB] = pat_idx_list_str,
69 [PAT_MATCH_DIR] = pat_idx_list_str,
70 [PAT_MATCH_DOM] = pat_idx_list_str,
71 [PAT_MATCH_END] = pat_idx_list_str,
72 [PAT_MATCH_REG] = pat_idx_list_reg,
73};
74
Thierry FOURNIERb1136502014-01-15 11:38:49 +010075void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *) = {
76 [PAT_MATCH_FOUND] = pat_del_list_val,
77 [PAT_MATCH_BOOL] = pat_del_list_val,
78 [PAT_MATCH_INT] = pat_del_list_val,
79 [PAT_MATCH_IP] = pat_del_tree_ip,
80 [PAT_MATCH_BIN] = pat_del_list_ptr,
81 [PAT_MATCH_LEN] = pat_del_list_val,
82 [PAT_MATCH_STR] = pat_del_tree_str,
83 [PAT_MATCH_BEG] = pat_del_list_str,
84 [PAT_MATCH_SUB] = pat_del_list_str,
85 [PAT_MATCH_DIR] = pat_del_list_str,
86 [PAT_MATCH_DOM] = pat_del_list_str,
87 [PAT_MATCH_END] = pat_del_list_str,
88 [PAT_MATCH_REG] = pat_del_list_reg,
89};
90
Thierry FOURNIER55d0b102014-01-15 11:25:26 +010091struct sample_storage **(*pat_find_smp_fcts[PAT_MATCH_NUM])(struct pattern_expr *,
92 struct pattern *) = {
93 [PAT_MATCH_FOUND] = pat_find_smp_list_val,
94 [PAT_MATCH_BOOL] = pat_find_smp_list_val,
95 [PAT_MATCH_INT] = pat_find_smp_list_val,
96 [PAT_MATCH_IP] = pat_find_smp_tree_ip,
97 [PAT_MATCH_BIN] = pat_find_smp_list_ptr,
98 [PAT_MATCH_LEN] = pat_find_smp_list_val,
99 [PAT_MATCH_STR] = pat_find_smp_tree_str,
100 [PAT_MATCH_BEG] = pat_find_smp_list_str,
101 [PAT_MATCH_SUB] = pat_find_smp_list_str,
102 [PAT_MATCH_DIR] = pat_find_smp_list_str,
103 [PAT_MATCH_DOM] = pat_find_smp_list_str,
104 [PAT_MATCH_END] = pat_find_smp_list_str,
105 [PAT_MATCH_REG] = pat_find_smp_list_reg,
106};
107
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100108void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
109 [PAT_MATCH_FOUND] = pat_prune_val,
110 [PAT_MATCH_BOOL] = pat_prune_val,
111 [PAT_MATCH_INT] = pat_prune_val,
112 [PAT_MATCH_IP] = pat_prune_val,
113 [PAT_MATCH_BIN] = pat_prune_ptr,
114 [PAT_MATCH_LEN] = pat_prune_val,
115 [PAT_MATCH_STR] = pat_prune_ptr,
116 [PAT_MATCH_BEG] = pat_prune_ptr,
117 [PAT_MATCH_SUB] = pat_prune_ptr,
118 [PAT_MATCH_DIR] = pat_prune_ptr,
119 [PAT_MATCH_DOM] = pat_prune_ptr,
120 [PAT_MATCH_END] = pat_prune_ptr,
121 [PAT_MATCH_REG] = pat_prune_reg,
122};
123
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100124struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100125 [PAT_MATCH_FOUND] = NULL,
126 [PAT_MATCH_BOOL] = pat_match_nothing,
127 [PAT_MATCH_INT] = pat_match_int,
128 [PAT_MATCH_IP] = pat_match_ip,
129 [PAT_MATCH_BIN] = pat_match_bin,
130 [PAT_MATCH_LEN] = pat_match_len,
131 [PAT_MATCH_STR] = pat_match_str,
132 [PAT_MATCH_BEG] = pat_match_beg,
133 [PAT_MATCH_SUB] = pat_match_sub,
134 [PAT_MATCH_DIR] = pat_match_dir,
135 [PAT_MATCH_DOM] = pat_match_dom,
136 [PAT_MATCH_END] = pat_match_end,
137 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138};
139
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100140/* Just used for checking configuration compatibility */
141int pat_match_types[PAT_MATCH_NUM] = {
142 [PAT_MATCH_FOUND] = SMP_T_UINT,
143 [PAT_MATCH_BOOL] = SMP_T_UINT,
144 [PAT_MATCH_INT] = SMP_T_UINT,
145 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100146 [PAT_MATCH_BIN] = SMP_T_BIN,
147 [PAT_MATCH_LEN] = SMP_T_STR,
148 [PAT_MATCH_STR] = SMP_T_STR,
149 [PAT_MATCH_BEG] = SMP_T_STR,
150 [PAT_MATCH_SUB] = SMP_T_STR,
151 [PAT_MATCH_DIR] = SMP_T_STR,
152 [PAT_MATCH_DOM] = SMP_T_STR,
153 [PAT_MATCH_END] = SMP_T_STR,
154 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100155};
156
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100157/* this struct is used to return information */
158static struct pattern static_pattern;
159
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100160/* This is the root of the list of all pattern_ref avalaibles. */
161struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
162
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100163/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100164 *
165 * The following functions are not exported and are used by internals process
166 * of pattern matching
167 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168 */
169
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100170/* Background: Fast way to find a zero byte in a word
171 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
172 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
173 *
174 * To look for 4 different byte values, xor the word with those bytes and
175 * then check for zero bytes:
176 *
177 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
178 * where <delimiter> is the 4 byte values to look for (as an uint)
179 * and <c> is the character that is being tested
180 */
181static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
182{
183 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
184 return (mask - 0x01010101) & ~mask & 0x80808080U;
185}
186
187static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
188{
189 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
190}
191
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100192
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100193/*
194 *
195 * These functions are exported and may be used by any other component.
196 *
197 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100198 * input value. The <text> contain the string to be parsed. <pattern>
199 * must be a preallocated pattern. The pat_parse_* functions fill this
200 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
201 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
202 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
203 * use "trash" or return pointers to the input strings. In both cases,
204 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
205 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100206 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100207 * In succes case, the pat_parse_* function return 1. If the function
208 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100209 *
210 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100211
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100212/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100213int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100214{
215 return 1;
216}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100217
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100218/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100219int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100220{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100221 pattern->type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100222 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100223 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100224 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100225}
226
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100227/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100228int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100229{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100230 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100232 pattern->type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100233 trash = get_trash_chunk();
234 pattern->len = trash->size;
235 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100236 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100237}
238
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100239/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100240int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100241{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100242 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100243
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100244 trash = get_trash_chunk();
245 if (trash->size < sizeof(*pattern->ptr.reg)) {
246 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
247 (int)sizeof(*pattern->ptr.reg), trash->size);
248 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100249 }
250
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100251 pattern->ptr.reg = (struct my_regex *)trash->str;
252 pattern->ptr.reg->regstr = (char *)text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100253
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254 return 1;
255}
256
257/* Parse a range of positive integers delimited by either ':' or '-'. If only
258 * one integer is read, it is set as both min and max. An operator may be
259 * specified as the prefix, among this list of 5 :
260 *
261 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
262 *
263 * The default operator is "eq". It supports range matching. Ranges are
264 * rejected for other operators. The operator may be changed at any time.
265 * The operator is stored in the 'opaque' argument.
266 *
267 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100268 * the caller will have to free it. The function returns zero on error, and
269 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100270 *
271 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100272int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100274 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100275
276 pattern->type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100277
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100278 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100279 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100280 goto not_valid_range;
281
282 /* Search ':' or '-' separator. */
283 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
284 ptr++;
285
286 /* If separator not found. */
287 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100288 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
289 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100290 return 0;
291 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100292 pattern->val.range.max = pattern->val.range.min;
293 pattern->val.range.min_set = 1;
294 pattern->val.range.max_set = 1;
295 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 }
297
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100298 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100299 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100300 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
301 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100302
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100303 pattern->val.range.min_set = 0;
304 pattern->val.range.max_set = 1;
305 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100306 }
307
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100308 /* If separator is the last character. */
309 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100310 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100311 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100312
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100313 pattern->val.range.min_set = 1;
314 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100315 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100317
318 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100319 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100320 goto not_valid_range;
321
322 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
323 goto not_valid_range;
324
325 if (pattern->val.range.min > pattern->val.range.max)
326 goto not_valid_range;
327
328 pattern->val.range.min_set = 1;
329 pattern->val.range.max_set = 1;
330 return 1;
331
332 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100333 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100334 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100335}
336
337/* Parse a range of positive 2-component versions delimited by either ':' or
338 * '-'. The version consists in a major and a minor, both of which must be
339 * smaller than 65536, because internally they will be represented as a 32-bit
340 * integer.
341 * If only one version is read, it is set as both min and max. Just like for
342 * pure integers, an operator may be specified as the prefix, among this list
343 * of 5 :
344 *
345 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
346 *
347 * The default operator is "eq". It supports range matching. Ranges are
348 * rejected for other operators. The operator may be changed at any time.
349 * The operator is stored in the 'opaque' argument. This allows constructs
350 * such as the following one :
351 *
352 * acl obsolete_ssl ssl_req_proto lt 3
353 * acl unsupported_ssl ssl_req_proto gt 3.1
354 * acl valid_ssl ssl_req_proto 3.0-3.1
355 *
356 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100357int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100359 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100360
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100361 pattern->type = SMP_T_UINT;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100362
363 /* Search ':' or '-' separator. */
364 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
365 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100366
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100368 if (*ptr == '\0' && ptr > text) {
369 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
370 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371 return 0;
372 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 pattern->val.range.max = pattern->val.range.min;
374 pattern->val.range.min_set = 1;
375 pattern->val.range.max_set = 1;
376 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100377 }
378
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100379 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100380 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100382 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100384 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100385 pattern->val.range.min_set = 0;
386 pattern->val.range.max_set = 1;
387 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100388 }
389
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100390 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100391 if (ptr == &text[strlen(text)-1]) {
392 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
393 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100394 return 0;
395 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100396 pattern->val.range.min_set = 1;
397 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100398 return 1;
399 }
400
401 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100402 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
403 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100404 return 0;
405 }
406 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100407 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100408 return 0;
409 }
410 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100411 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100412 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100414 pattern->val.range.min_set = 1;
415 pattern->val.range.max_set = 1;
416 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100417}
418
419/* Parse an IP address and an optional mask in the form addr[/mask].
420 * The addr may either be an IPv4 address or a hostname. The mask
421 * may either be a dotted mask or a number of bits. Returns 1 if OK,
422 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
423 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100424int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100425{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100426 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100427 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100428 return 1;
429 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100430 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100431 pattern->type = SMP_T_IPV6;
432 return 1;
433 }
434 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100435 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100436 return 0;
437 }
438}
439
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100440/*
441 *
442 * These functions are exported and may be used by any other component.
443 *
444 * This fucntion just take a sample <smp> and check if this sample match
445 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
446 * PAT_NOMATCH.
447 *
448 */
449
450/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100451struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100452{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100453 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100454}
455
456
457/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100458struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100459{
460 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100461 struct ebmb_node *node;
462 char prev;
463 struct pattern_tree *elt;
464 struct pattern_list *lst;
465 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100466
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100467 /* Lookup a string in the expression's pattern tree. */
468 if (!eb_is_empty(&expr->pattern_tree)) {
469 /* we may have to force a trailing zero on the test pattern */
470 prev = smp->data.str.str[smp->data.str.len];
471 if (prev)
472 smp->data.str.str[smp->data.str.len] = '\0';
473 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
474 if (prev)
475 smp->data.str.str[smp->data.str.len] = prev;
476
477 if (node) {
478 if (fill) {
479 elt = ebmb_entry(node, struct pattern_tree, node);
480 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100481 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100482 static_pattern.flags = PAT_F_TREE;
483 static_pattern.type = SMP_T_STR;
484 static_pattern.ptr.str = (char *)elt->node.key;
485 }
486 return &static_pattern;
487 }
488 }
489
490 /* look in the list */
491 list_for_each_entry(lst, &expr->patterns, list) {
492 pattern = &lst->pat;
493
494 if (pattern->len != smp->data.str.len)
495 continue;
496
497 icase = pattern->flags & PAT_F_IGNORE_CASE;
498 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
499 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
500 return pattern;
501 }
502
503 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100504}
505
506/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100507struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100508{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100509 struct pattern_list *lst;
510 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100511
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100512 /* Look in the list. */
513 list_for_each_entry(lst, &expr->patterns, list) {
514 pattern = &lst->pat;
515
516 if (pattern->len != smp->data.str.len)
517 continue;
518
519 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
520 return pattern;
521 }
522
523 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524}
525
526/* Executes a regex. It temporarily changes the data to add a trailing zero,
527 * and restores the previous character when leaving.
528 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100529struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100530{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100531 struct pattern_list *lst;
532 struct pattern *pattern;
533
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100534 /* look in the list */
535 list_for_each_entry(lst, &expr->patterns, list) {
536 pattern = &lst->pat;
537
538 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
539 return pattern;
540 }
541 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100542}
543
544/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100545struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100546{
547 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100548 struct pattern_list *lst;
549 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100550
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100551 list_for_each_entry(lst, &expr->patterns, list) {
552 pattern = &lst->pat;
553
554 if (pattern->len > smp->data.str.len)
555 continue;
556
557 icase = pattern->flags & PAT_F_IGNORE_CASE;
558 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
559 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
560 continue;
561
562 return pattern;
563 }
564 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100565}
566
567/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100568struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100569{
570 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100571 struct pattern_list *lst;
572 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100573
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100574 list_for_each_entry(lst, &expr->patterns, list) {
575 pattern = &lst->pat;
576
577 if (pattern->len > smp->data.str.len)
578 continue;
579
580 icase = pattern->flags & PAT_F_IGNORE_CASE;
581 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
582 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
583 continue;
584
585 return pattern;
586 }
587 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100588}
589
590/* Checks that the pattern is included inside the tested string.
591 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
592 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100593struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100594{
595 int icase;
596 char *end;
597 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100598 struct pattern_list *lst;
599 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100600
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100601 list_for_each_entry(lst, &expr->patterns, list) {
602 pattern = &lst->pat;
603
604 if (pattern->len > smp->data.str.len)
605 continue;
606
607 end = smp->data.str.str + smp->data.str.len - pattern->len;
608 icase = pattern->flags & PAT_F_IGNORE_CASE;
609 if (icase) {
610 for (c = smp->data.str.str; c <= end; c++) {
611 if (tolower(*c) != tolower(*pattern->ptr.str))
612 continue;
613 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
614 return pattern;
615 }
616 } else {
617 for (c = smp->data.str.str; c <= end; c++) {
618 if (*c != *pattern->ptr.str)
619 continue;
620 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
621 return pattern;
622 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100623 }
624 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100625 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100626}
627
628/* This one is used by other real functions. It checks that the pattern is
629 * included inside the tested string, but enclosed between the specified
630 * delimiters or at the beginning or end of the string. The delimiters are
631 * provided as an unsigned int made by make_4delim() and match up to 4 different
632 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
633 */
634static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
635{
636 int may_match, icase;
637 char *c, *end;
638 char *ps;
639 int pl;
640
641 pl = pattern->len;
642 ps = pattern->ptr.str;
643
644 while (pl > 0 && is_delimiter(*ps, delimiters)) {
645 pl--;
646 ps++;
647 }
648
649 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
650 pl--;
651
652 if (pl > smp->data.str.len)
653 return PAT_NOMATCH;
654
655 may_match = 1;
656 icase = pattern->flags & PAT_F_IGNORE_CASE;
657 end = smp->data.str.str + smp->data.str.len - pl;
658 for (c = smp->data.str.str; c <= end; c++) {
659 if (is_delimiter(*c, delimiters)) {
660 may_match = 1;
661 continue;
662 }
663
664 if (!may_match)
665 continue;
666
667 if (icase) {
668 if ((tolower(*c) == tolower(*ps)) &&
669 (strncasecmp(ps, c, pl) == 0) &&
670 (c == end || is_delimiter(c[pl], delimiters)))
671 return PAT_MATCH;
672 } else {
673 if ((*c == *ps) &&
674 (strncmp(ps, c, pl) == 0) &&
675 (c == end || is_delimiter(c[pl], delimiters)))
676 return PAT_MATCH;
677 }
678 may_match = 0;
679 }
680 return PAT_NOMATCH;
681}
682
683/* Checks that the pattern is included inside the tested string, but enclosed
684 * between the delimiters '?' or '/' or at the beginning or end of the string.
685 * Delimiters at the beginning or end of the pattern are ignored.
686 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100687struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100688{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100689 struct pattern_list *lst;
690 struct pattern *pattern;
691
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100692 list_for_each_entry(lst, &expr->patterns, list) {
693 pattern = &lst->pat;
694 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
695 return pattern;
696 }
697 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100698}
699
700/* Checks that the pattern is included inside the tested string, but enclosed
701 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
702 * the string. Delimiters at the beginning or end of the pattern are ignored.
703 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100705{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100706 struct pattern_list *lst;
707 struct pattern *pattern;
708
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100709 list_for_each_entry(lst, &expr->patterns, list) {
710 pattern = &lst->pat;
711 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
712 return pattern;
713 }
714 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100715}
716
717/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100718struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100719{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720 struct pattern_list *lst;
721 struct pattern *pattern;
722
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100723 list_for_each_entry(lst, &expr->patterns, list) {
724 pattern = &lst->pat;
725 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
726 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
727 return pattern;
728 }
729 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100730}
731
732/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100733struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100734{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100735 struct pattern_list *lst;
736 struct pattern *pattern;
737
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738 list_for_each_entry(lst, &expr->patterns, list) {
739 pattern = &lst->pat;
740 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
741 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
742 return pattern;
743 }
744 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100745}
746
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100747struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100748{
749 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100750 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100751 struct in_addr *s;
752 struct ebmb_node *node;
753 struct pattern_tree *elt;
754 struct pattern_list *lst;
755 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100756
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100757 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100758 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100759 /* Lookup an IPv4 address in the expression's pattern tree using
760 * the longest match method.
761 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100762 s = &smp->data.ipv4;
763 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
764 if (node) {
765 if (fill) {
766 elt = ebmb_entry(node, struct pattern_tree, node);
767 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100768 static_pattern.ref = elt->ref;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 static_pattern.flags = PAT_F_TREE;
770 static_pattern.type = SMP_T_IPV4;
771 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
772 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
773 return NULL;
774 }
775 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100776 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100777
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100778 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
779 * sample address to IPv6 with the mapping method using the ::ffff:
780 * prefix, and try to lookup in the IPv6 tree.
781 */
782 memset(&tmp6, 0, 10);
783 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
784 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
785 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
786 if (node) {
787 if (fill) {
788 elt = ebmb_entry(node, struct pattern_tree, node);
789 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100790 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100791 static_pattern.flags = PAT_F_TREE;
792 static_pattern.type = SMP_T_IPV6;
793 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
794 static_pattern.val.ipv6.mask = elt->node.node.pfx;
795 }
796 return &static_pattern;
797 }
798 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100799
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100800 /* The input sample is IPv6. Try to match in the trees. */
801 if (smp->type == SMP_T_IPV6) {
802 /* Lookup an IPv6 address in the expression's pattern tree using
803 * the longest match method.
804 */
805 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
806 if (node) {
807 if (fill) {
808 elt = ebmb_entry(node, struct pattern_tree, node);
809 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100810 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100811 static_pattern.flags = PAT_F_TREE;
812 static_pattern.type = SMP_T_IPV6;
813 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
814 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100815 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100816 return &static_pattern;
817 }
818
819 /* Try to convert 6 to 4 when the start of the ipv6 address match the
820 * following forms :
821 * - ::ffff:ip:v4 (ipv4 mapped)
822 * - ::0000:ip:v4 (old ipv4 mapped)
823 * - 2002:ip:v4:: (6to4)
824 */
825 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
826 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
827 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
828 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
829 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
830 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
831 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
832 else
833 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
834 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
835
836 /* Lookup an IPv4 address in the expression's pattern tree using the longest
837 * match method.
838 */
839 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
840 if (node) {
841 if (fill) {
842 elt = ebmb_entry(node, struct pattern_tree, node);
843 static_pattern.smp = elt->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +0100844 static_pattern.ref = elt->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100845 static_pattern.flags = PAT_F_TREE;
846 static_pattern.type = SMP_T_IPV4;
847 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
848 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
849 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100850 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100851 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100852 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100853 }
854 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100855
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100856 /* Lookup in the list. the list contain only IPv4 patterns */
857 list_for_each_entry(lst, &expr->patterns, list) {
858 pattern = &lst->pat;
859
860 /* The input sample is IPv4, use it as is. */
861 if (smp->type == SMP_T_IPV4) {
862 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100863 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100864 else if (smp->type == SMP_T_IPV6) {
865 /* v4 match on a V6 sample. We want to check at least for
866 * the following forms :
867 * - ::ffff:ip:v4 (ipv4 mapped)
868 * - ::0000:ip:v4 (old ipv4 mapped)
869 * - 2002:ip:v4:: (6to4)
870 */
871 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
872 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
873 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
874 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
875 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100876 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100877 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
878 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
879 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100880 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100881 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100882 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100883 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100884
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100885 /* Check if the input sample match the current pattern. */
886 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100887 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100888 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100889 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100890}
891
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100892void free_pattern_tree(struct eb_root *root)
893{
894 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100895 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100896
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100897 node = eb_first(root);
898 while (node) {
899 next = eb_next(node);
900 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100901 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100902 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100903 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100904 node = next;
905 }
906}
907
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100908void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100909{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100910 struct pattern_list *pat, *tmp;
911
912 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
913 free(pat->pat.smp);
914 free(pat);
915 }
916
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100917 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100918 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100919 LIST_INIT(&expr->patterns);
920}
921
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100922void pat_prune_ptr(struct pattern_expr *expr)
923{
924 struct pattern_list *pat, *tmp;
925
926 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
927 free(pat->pat.ptr.ptr);
928 free(pat->pat.smp);
929 free(pat);
930 }
931
932 free_pattern_tree(&expr->pattern_tree);
933 free_pattern_tree(&expr->pattern_tree_2);
934 LIST_INIT(&expr->patterns);
935}
936
937void pat_prune_reg(struct pattern_expr *expr)
938{
939 struct pattern_list *pat, *tmp;
940
941 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
942 regex_free(pat->pat.ptr.ptr);
943 free(pat->pat.smp);
944 free(pat);
945 }
946
947 free_pattern_tree(&expr->pattern_tree);
948 free_pattern_tree(&expr->pattern_tree_2);
949 LIST_INIT(&expr->patterns);
950}
951
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100952/*
953 *
954 * The following functions are used for the pattern indexation
955 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100956 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100957
958int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100959{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100960 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100961
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100962 /* allocate pattern */
963 patl = calloc(1, sizeof(*patl));
964 if (!patl) {
965 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100966 return 0;
967 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100968
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100969 /* duplicate pattern */
970 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100971
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100972 /* chain pattern in the expression */
973 LIST_ADDQ(&expr->patterns, &patl->list);
974
975 /* that's ok */
976 return 1;
977}
978
979int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
980{
981 struct pattern_list *patl;
982
983 /* allocate pattern */
984 patl = calloc(1, sizeof(*patl));
985 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100986 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100987
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100988 /* duplicate pattern */
989 memcpy(&patl->pat, pat, sizeof(*pat));
990 patl->pat.ptr.ptr = malloc(patl->pat.len);
991 if (!patl->pat.ptr.ptr) {
992 free(patl);
993 memprintf(err, "out of memory while indexing pattern");
994 return 0;
995 }
996 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100997
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100998 /* chain pattern in the expression */
999 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001000
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001001 /* that's ok */
1002 return 1;
1003}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001004
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001005int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1006{
1007 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001008
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001009 /* allocate pattern */
1010 patl = calloc(1, sizeof(*patl));
1011 if (!patl) {
1012 memprintf(err, "out of memory while indexing pattern");
1013 return 0;
1014 }
1015
1016 /* duplicate pattern */
1017 memcpy(&patl->pat, pat, sizeof(*pat));
1018 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1019 if (!patl->pat.ptr.str) {
1020 free(patl);
1021 memprintf(err, "out of memory while indexing pattern");
1022 return 0;
1023 }
1024 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1025 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001026
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001027 /* chain pattern in the expression */
1028 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001029
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001030 /* that's ok */
1031 return 1;
1032}
1033
1034int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1035{
1036 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001037
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001038 /* allocate pattern */
1039 patl = calloc(1, sizeof(*patl));
1040 if (!patl) {
1041 memprintf(err, "out of memory while indexing pattern");
1042 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001043 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001044
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001045 /* duplicate pattern */
1046 memcpy(&patl->pat, pat, sizeof(*pat));
1047
1048 /* allocate regex */
1049 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1050 if (!patl->pat.ptr.reg) {
1051 free(patl);
1052 memprintf(err, "out of memory while indexing pattern");
1053 return 0;
1054 }
1055
1056 /* compile regex */
1057 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1058 free(patl);
1059 free(patl->pat.ptr.reg);
1060 return 0;
1061 }
1062
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001063 /* chain pattern in the expression */
1064 LIST_ADDQ(&expr->patterns, &patl->list);
1065
1066 /* that's ok */
1067 return 1;
1068}
1069
1070int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1071{
1072 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001073 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001074
1075 /* Only IPv4 can be indexed */
1076 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001077 /* in IPv4 case, check if the mask is contiguous so that we can
1078 * insert the network into the tree. A continuous mask has only
1079 * ones on the left. This means that this mask + its lower bit
1080 * added once again is null.
1081 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001082 mask = ntohl(pat->val.ipv4.mask.s_addr);
1083 if (mask + (mask & -mask) == 0) {
1084 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001085
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001086 /* node memory allocation */
1087 node = calloc(1, sizeof(*node) + 4);
1088 if (!node) {
1089 memprintf(err, "out of memory while loading pattern");
1090 return 0;
1091 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001092
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001093 /* copy the pointer to sample associated to this node */
1094 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001095 node->ref = pat->ref;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001096
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001097 /* FIXME: insert <addr>/<mask> into the tree here */
1098 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1099 node->node.node.pfx = mask;
1100 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1101 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001102
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001103 /* that's ok */
1104 return 1;
1105 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001106 else {
1107 /* If the mask is not contiguous, just add the pattern to the list */
1108 return pat_idx_list_val(expr, pat, err);
1109 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001110 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001111 else if (pat->type == SMP_T_IPV6) {
1112 /* IPv6 also can be indexed */
1113 node = calloc(1, sizeof(*node) + 16);
1114 if (!node) {
1115 memprintf(err, "out of memory while loading pattern");
1116 return 0;
1117 }
1118
1119 /* copy the pointer to sample associated to this node */
1120 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001121 node->ref = pat->ref;
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001122
1123 /* FIXME: insert <addr>/<mask> into the tree here */
1124 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1125 node->node.node.pfx = pat->val.ipv6.mask;
1126 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1127 free(node); /* was a duplicate */
1128
1129 /* that's ok */
1130 return 1;
1131 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001132
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001133 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001134}
1135
1136int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1137{
1138 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001139 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001140
1141 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001142 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001143 memprintf(err, "internal error: string expected, but the type is '%s'",
1144 smp_to_type[pat->type]);
1145 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001146 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001147
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001148 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1149 if (pat->flags & PAT_F_IGNORE_CASE)
1150 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001151
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001152 /* Process the key len */
1153 len = strlen(pat->ptr.str) + 1;
1154
1155 /* node memory allocation */
1156 node = calloc(1, sizeof(*node) + len);
1157 if (!node) {
1158 memprintf(err, "out of memory while loading pattern");
1159 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001160 }
1161
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001162 /* copy the pointer to sample associated to this node */
1163 node->smp = pat->smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001164 node->ref = pat->ref;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001165
1166 /* copy the string */
1167 memcpy(node->node.key, pat->ptr.str, len);
1168
1169 /* index the new node */
1170 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1171 free(node); /* was a duplicate */
1172
1173 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001174 return 1;
1175}
1176
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01001177struct sample_storage **pat_find_smp_list_val(struct pattern_expr *expr, struct pattern *pattern)
1178{
1179 struct pattern_list *pat;
1180 struct pattern_list *safe;
1181
1182 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1183
1184 /* Check equality. */
1185 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1186 continue;
1187 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1188 continue;
1189 if (pattern->val.range.min_set &&
1190 pattern->val.range.min != pat->pat.val.range.min)
1191 continue;
1192 if (pattern->val.range.max_set &&
1193 pattern->val.range.max != pat->pat.val.range.max)
1194 continue;
1195
1196 /* Return the pointer on the sample pointer. */
1197 return &pat->pat.smp;
1198 }
1199
1200 return NULL;
1201}
1202
1203struct sample_storage **pat_find_smp_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1204{
1205 struct ebmb_node *node, *next_node;
1206 struct pattern_tree *elt;
1207 struct pattern_list *pat;
1208 struct pattern_list *safe;
1209 unsigned int mask;
1210
1211 /* browse each node of the tree for IPv4 addresses. */
1212 if (pattern->type == SMP_T_IPV4) {
1213 /* Convert mask. If the mask is contiguous, browse each node
1214 * of the tree for IPv4 addresses.
1215 */
1216 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1217 if (mask + (mask & -mask) == 0) {
1218 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1219
1220 for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node);
1221 node;
1222 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1223 /* Extract container of the tree node. */
1224 elt = container_of(node, struct pattern_tree, node);
1225
1226 /* Check equality. */
1227 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1228 continue;
1229
1230 /* Return the pointer on the sample pointer. */
1231 return &elt->smp;
1232 }
1233 }
1234 else {
1235 /* Browse each node of the list for IPv4 addresses. */
1236 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1237 /* Check equality. */
1238 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1239 sizeof(pat->pat.val.ipv4.addr)) != 0)
1240 continue;
1241 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1242 sizeof(pat->pat.val.ipv4.addr)) != 0)
1243 continue;
1244
1245 /* Return the pointer on the sample pointer. */
1246 return &pat->pat.smp;
1247 }
1248 }
1249 }
1250 else if (pattern->type == SMP_T_IPV6) {
1251 /* browse each node of the tree for IPv4 addresses. */
1252 for (node = ebmb_first(&expr->pattern_tree_2), next_node = ebmb_next(node);
1253 node;
1254 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1255 /* Extract container of the tree node. */
1256 elt = container_of(node, struct pattern_tree, node);
1257
1258 /* Check equality. */
1259 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1260 continue;
1261
1262 /* Return the pointer on the sample pointer. */
1263 return &elt->smp;
1264 }
1265 }
1266
1267 return NULL;
1268}
1269
1270struct sample_storage **pat_find_smp_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1271{
1272 struct pattern_list *pat;
1273 struct pattern_list *safe;
1274
1275 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1276 /* Check equality. */
1277 if (pattern->len != pat->pat.len)
1278 continue;
1279 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1280 continue;
1281
1282 /* Return the pointer on the sample pointer. */
1283 return &pat->pat.smp;
1284 }
1285
1286 return NULL;
1287}
1288
1289struct sample_storage **pat_find_smp_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1290{
1291 struct ebmb_node *node, *next_node;
1292 struct pattern_tree *elt;
1293
1294 /* browse each node of the tree. */
1295 for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node);
1296 node;
1297 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1298 /* Extract container of the tree node. */
1299 elt = container_of(node, struct pattern_tree, node);
1300
1301 /* Check equality. */
1302 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1303 continue;
1304
1305 /* Return the pointer on the sample pointer. */
1306 return &elt->smp;
1307 }
1308
1309 return NULL;
1310}
1311
1312struct sample_storage **pat_find_smp_list_str(struct pattern_expr *expr, struct pattern *pattern)
1313{
1314 struct pattern_list *pat;
1315 struct pattern_list *safe;
1316
1317 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1318 /* Check equality. */
1319 if (pattern->len != pat->pat.len)
1320 continue;
1321 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1322 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1323 continue;
1324 }
1325 else {
1326 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1327 continue;
1328 }
1329
1330 /* Return the pointer on the sample pointer. */
1331 return &pat->pat.smp;
1332 }
1333
1334 return NULL;
1335}
1336
1337struct sample_storage **pat_find_smp_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1338{
1339 struct pattern_list *pat;
1340 struct pattern_list *safe;
1341
1342 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1343 /* Check equality. */
1344 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1345 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1346 continue;
1347 }
1348 else {
1349 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1350 continue;
1351 }
1352
1353 /* Return the pointer on the sample pointer. */
1354 return &pat->pat.smp;
1355 }
1356
1357 return NULL;
1358}
1359
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001360void pat_del_list_val(struct pattern_expr *expr, struct pattern *pattern)
1361{
1362 struct pattern_list *pat;
1363 struct pattern_list *safe;
1364
1365 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1366 /* Check equality. */
1367 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1368 continue;
1369 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1370 continue;
1371 if (pattern->val.range.min_set &&
1372 pattern->val.range.min != pat->pat.val.range.min)
1373 continue;
1374 if (pattern->val.range.max_set &&
1375 pattern->val.range.max != pat->pat.val.range.max)
1376 continue;
1377
1378 /* Delete and free entry. */
1379 LIST_DEL(&pat->list);
1380 free(pat->pat.smp);
1381 free(pat);
1382 }
1383}
1384
1385void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1386{
1387 struct ebmb_node *node, *next_node;
1388 struct pattern_tree *elt;
1389 struct pattern_list *pat;
1390 struct pattern_list *safe;
1391 unsigned int mask;
1392
1393 /* browse each node of the tree for IPv4 addresses. */
1394 if (pattern->type == SMP_T_IPV4) {
1395 /* Convert mask. If the mask is contiguous, browse each node
1396 * of the tree for IPv4 addresses.
1397 */
1398 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1399 if (mask + (mask & -mask) == 0) {
1400 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1401
1402 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1403 node;
1404 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1405 /* Extract container of the tree node. */
1406 elt = container_of(node, struct pattern_tree, node);
1407
1408 /* Check equality. */
1409 if (memcmp(&pattern->val.ipv4.addr, elt->node.key,
1410 sizeof(pattern->val.ipv4.addr)) != 0)
1411 continue;
1412 if (elt->node.node.pfx != mask)
1413 continue;
1414
1415 /* Delete and free entry. */
1416 ebmb_delete(node);
1417 free(elt->smp);
1418 free(elt);
1419 }
1420 }
1421 else {
1422 /* Browse each node of the list for IPv4 addresses. */
1423 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1424 /* Check equality, addr then mask */
1425 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1426 sizeof(pat->pat.val.ipv4.addr)) != 0)
1427 continue;
1428
1429 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1430 sizeof(pat->pat.val.ipv4.addr)) != 0)
1431 continue;
1432
1433 /* Delete and free entry. */
1434 LIST_DEL(&pat->list);
1435 free(pat->pat.smp);
1436 free(pat);
1437 }
1438 }
1439 }
1440 else if (pattern->type == SMP_T_IPV6) {
1441 /* browse each node of the tree for IPv6 addresses. */
1442 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1443 node;
1444 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1445 /* Extract container of the tree node. */
1446 elt = container_of(node, struct pattern_tree, node);
1447
1448 /* Check equality. */
1449 if (memcmp(&pattern->val.ipv6.addr, elt->node.key,
1450 sizeof(pattern->val.ipv6.addr)) != 0)
1451 continue;
1452 if (elt->node.node.pfx != pattern->val.ipv6.mask)
1453 continue;
1454
1455 /* Delete and free entry. */
1456 ebmb_delete(node);
1457 free(elt->smp);
1458 free(elt);
1459 }
1460 }
1461}
1462
1463void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1464{
1465 struct pattern_list *pat;
1466 struct pattern_list *safe;
1467
1468 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1469 /* Check equality. */
1470 if (pattern->len != pat->pat.len)
1471 continue;
1472 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1473 continue;
1474
1475 /* Delete and free entry. */
1476 LIST_DEL(&pat->list);
1477 free(pat->pat.ptr.ptr);
1478 free(pat->pat.smp);
1479 free(pat);
1480 }
1481}
1482
1483void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1484{
1485 struct ebmb_node *node, *next_node;
1486 struct pattern_tree *elt;
1487
1488 /* browse each node of the tree. */
1489 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1490 node;
1491 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1492 /* Extract container of the tree node. */
1493 elt = container_of(node, struct pattern_tree, node);
1494
1495 /* Check equality. */
1496 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1497 continue;
1498
1499 /* Delete and free entry. */
1500 ebmb_delete(node);
1501 free(elt->smp);
1502 free(elt);
1503 }
1504}
1505
1506void pat_del_list_str(struct pattern_expr *expr, struct pattern *pattern)
1507{
1508 struct pattern_list *pat;
1509 struct pattern_list *safe;
1510
1511 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1512 /* Check equality. */
1513 if (pattern->len != pat->pat.len)
1514 continue;
1515 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1516 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1517 continue;
1518 }
1519 else {
1520 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1521 continue;
1522 }
1523
1524 /* Delete and free entry. */
1525 LIST_DEL(&pat->list);
1526 free(pat->pat.ptr.str);
1527 free(pat->pat.smp);
1528 free(pat);
1529 }
1530}
1531
1532void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1533{
1534 struct pattern_list *pat;
1535 struct pattern_list *safe;
1536
1537 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1538 /* Check equality. */
1539 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1540 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1541 continue;
1542 }
1543 else {
1544 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1545 continue;
1546 }
1547
1548 /* Delete and free entry. */
1549 LIST_DEL(&pat->list);
1550 regex_free(pat->pat.ptr.ptr);
1551 free(pat->pat.smp);
1552 free(pat);
1553 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001554}
1555
1556void pattern_init_expr(struct pattern_expr *expr)
1557{
1558 LIST_INIT(&expr->patterns);
1559 expr->pattern_tree = EB_ROOT_UNIQUE;
1560 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
1561}
1562
1563void pattern_init_head(struct pattern_head *head)
1564{
1565 LIST_INIT(&head->head);
1566}
1567
1568/* The following functions are relative to the management of the reference
1569 * lists. These lists are used to store the original pattern and associated
1570 * value as string form.
1571 *
1572 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001573 *
1574 * The pattern reference are stored with two identifiers: the unique_id and
1575 * the reference.
1576 *
1577 * The reference identify a file. Each file with the same name point to the
1578 * same reference. We can register many times one file. If the file is modified,
1579 * all his dependencies are also modified. The reference can be used with map or
1580 * acl.
1581 *
1582 * The unique_id identify inline acl. The unique id is unique for each acl.
1583 * You cannot force the same id in the configuration file, because this repoort
1584 * an error.
1585 *
1586 * A particular case appears if the filename is a number. In this case, the
1587 * unique_id is set with the number represented by the filename and the
1588 * reference is also set. This method prevent double unique_id.
1589 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001590 */
1591
1592/* This function lookup for reference. If the reference is found, they return
1593 * pointer to the struct pat_ref, else return NULL.
1594 */
1595struct pat_ref *pat_ref_lookup(const char *reference)
1596{
1597 struct pat_ref *ref;
1598
1599 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001600 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601 return ref;
1602 return NULL;
1603}
1604
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001605/* This function lookup for unique id. If the reference is found, they return
1606 * pointer to the struct pat_ref, else return NULL.
1607 */
1608struct pat_ref *pat_ref_lookupid(int unique_id)
1609{
1610 struct pat_ref *ref;
1611
1612 list_for_each_entry(ref, &pattern_reference, list)
1613 if (ref->unique_id == unique_id)
1614 return ref;
1615 return NULL;
1616}
1617
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001618/* This function remove all pattern match <key> from the the reference
1619 * and from each expr member of the reference. This fucntion returns 1
1620 * if the deletion is done and return 0 is the entry is not found.
1621 */
1622int pat_ref_delete(struct pat_ref *ref, const char *key)
1623{
1624 struct pattern_expr *expr;
1625 struct pat_ref_elt *elt, *safe;
1626 int found = 0;
1627
1628 /* delete pattern from reference */
1629 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1630 if (strcmp(key, elt->pattern) == 0) {
1631 LIST_DEL(&elt->list);
1632 free(elt->sample);
1633 free(elt->pattern);
1634 free(elt);
1635 found = 1;
1636 }
1637 }
1638
1639 if (!found)
1640 return 0;
1641
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001642 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001643 pattern_delete(key, expr, NULL);
1644
1645 return 1;
1646}
1647
1648/* This function modify the sample of the first pattern that match the <key>. */
1649int pat_ref_set(struct pat_ref *ref, const char *key, const char *value)
1650{
1651 struct pattern_expr *expr;
1652 struct pat_ref_elt *elt;
1653 struct sample_storage **smp;
1654 char *sample;
1655 int found = 0;
1656
1657 /* modify pattern from reference */
1658 list_for_each_entry(elt, &ref->head, list) {
1659 if (strcmp(key, elt->pattern) == 0) {
1660 sample = strdup(value);
1661 if (!sample)
1662 return 0;
1663 free(elt->sample);
1664 elt->sample = sample;
1665 found = 1;
1666 break;
1667 }
1668 }
1669
1670 if (!found)
1671 return 0;
1672
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001673 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001674 smp = pattern_find_smp(key, expr, NULL);
1675 if (smp && expr->pat_head->parse_smp)
1676 if (!expr->pat_head->parse_smp(value, *smp))
1677 *smp = NULL;
1678 }
1679
1680 return 1;
1681}
1682
1683/* This function create new reference. <ref> is the reference name.
1684 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1685 * be unique. The user must check the reference with "pat_ref_lookup()"
1686 * before calling this function. If the fucntion fail, it return NULL,
1687 * else return new struct pat_ref.
1688 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001689struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001690{
1691 struct pat_ref *ref;
1692
1693 ref = malloc(sizeof(*ref));
1694 if (!ref)
1695 return NULL;
1696
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001697 if (display) {
1698 ref->display = strdup(display);
1699 if (!ref->display) {
1700 free(ref);
1701 return NULL;
1702 }
1703 }
1704 else
1705 ref->display = NULL;
1706
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001707 ref->reference = strdup(reference);
1708 if (!ref->reference) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001709 free(ref->display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001710 free(ref);
1711 return NULL;
1712 }
1713
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001714 ref->flags = flags;
1715 ref->unique_id = -1;
1716
1717 LIST_INIT(&ref->head);
1718 LIST_INIT(&ref->pat);
1719
1720 LIST_ADDQ(&pattern_reference, &ref->list);
1721
1722 return ref;
1723}
1724
1725/* This function create new reference. <unique_id> is the unique id. If
1726 * the value of <unique_id> is -1, the unique id is calculated later.
1727 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1728 * be unique. The user must check the reference with "pat_ref_lookup()"
1729 * or pat_ref_lookupid before calling this function. If the function
1730 * fail, it return NULL, else return new struct pat_ref.
1731 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001732struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001733{
1734 struct pat_ref *ref;
1735
1736 ref = malloc(sizeof(*ref));
1737 if (!ref)
1738 return NULL;
1739
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01001740 if (display) {
1741 ref->display = strdup(display);
1742 if (!ref->display) {
1743 free(ref);
1744 return NULL;
1745 }
1746 }
1747 else
1748 ref->display = NULL;
1749
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001750 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001751 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001752 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001753 LIST_INIT(&ref->head);
1754 LIST_INIT(&ref->pat);
1755
1756 LIST_ADDQ(&pattern_reference, &ref->list);
1757
1758 return ref;
1759}
1760
1761/* This function adds entry to <ref>. It can failed with memory error.
1762 * If the function fails, it returns 0.
1763 */
1764int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1765{
1766 struct pat_ref_elt *elt;
1767
1768 elt = malloc(sizeof(*elt));
1769 if (!elt)
1770 return 0;
1771
1772 elt->line = line;
1773
1774 elt->pattern = strdup(pattern);
1775 if (!elt->pattern) {
1776 free(elt);
1777 return 0;
1778 }
1779
1780 if (sample) {
1781 elt->sample = strdup(sample);
1782 if (!elt->sample) {
1783 free(elt->pattern);
1784 free(elt);
1785 return 0;
1786 }
1787 }
1788 else
1789 elt->sample = NULL;
1790
1791 LIST_ADDQ(&ref->head, &elt->list);
1792
1793 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001794}
1795
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001796/* This function create sample found in <elt>, parse the pattern also
1797 * found in <elt> and insert it in <expr>. The function copy <patflags>
1798 * in <expr>. If the function fails, it returns0 and <err> is filled.
1799 * In succes case, the function returns 1.
1800 */
1801static inline
1802int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1803 int patflags, char **err)
1804{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001805 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001806 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001807
1808 /* Create sample */
1809 if (elt->sample && expr->pat_head->parse_smp) {
1810 /* New sample. */
1811 smp = malloc(sizeof(*smp));
1812 if (!smp)
1813 return 0;
1814
1815 /* Parse value. */
1816 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1817 memprintf(err, "unable to parse '%s'", elt->sample);
1818 free(smp);
1819 return 0;
1820 }
1821
1822 }
1823 else
1824 smp = NULL;
1825
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001826 /* initialise pattern */
1827 memset(&pattern, 0, sizeof(pattern));
1828 pattern.flags = patflags;
1829 pattern.smp = smp;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01001830 pattern.ref = elt;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001831
1832 /* parse pattern */
1833 if (!expr->pat_head->parse(elt->pattern, &pattern, err)) {
1834 free(smp);
1835 return 0;
1836 }
1837
1838 /* index pattern */
1839 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001840 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001841 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001842 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001843
1844 return 1;
1845}
1846
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001847/* This function adds entry to <ref>. It can failed with memory error.
1848 * The new entry is added at all the pattern_expr registered in this
1849 * reference. The function stop on the first error encountered. It
1850 * returns 0 and err is filled.
1851 *
1852 * If an error is encountered, The complete add operation is cancelled.
1853 */
1854int pat_ref_add(struct pat_ref *ref,
1855 const char *pattern, const char *sample,
1856 char **err)
1857{
1858 struct pat_ref_elt *elt;
1859 struct pattern_expr *expr;
1860
1861 elt = malloc(sizeof(*elt));
1862 if (!elt) {
1863 memprintf(err, "out of memory error");
1864 return 0;
1865 }
1866
1867 elt->line = -1;
1868
1869 elt->pattern = strdup(pattern);
1870 if (!elt->pattern) {
1871 free(elt);
1872 memprintf(err, "out of memory error");
1873 return 0;
1874 }
1875
1876 if (sample) {
1877 elt->sample = strdup(sample);
1878 if (!elt->sample) {
1879 free(elt->pattern);
1880 free(elt);
1881 memprintf(err, "out of memory error");
1882 return 0;
1883 }
1884 }
1885 else
1886 elt->sample = NULL;
1887
1888 LIST_ADDQ(&ref->head, &elt->list);
1889
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001890 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001891 if (!pat_ref_push(elt, expr, 0, err)) {
1892 /* Try to delete all the added entries. */
1893 pat_ref_delete(ref, pattern);
1894 return 0;
1895 }
1896 }
1897
1898 return 1;
1899}
1900
1901/* This function prune all entries of <ref>. This function
1902 * prune the associated pattern_expr.
1903 */
1904void pat_ref_prune(struct pat_ref *ref)
1905{
1906 struct pat_ref_elt *elt, *safe;
1907 struct pattern_expr *expr;
1908
1909 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1910 LIST_DEL(&elt->list);
1911 free(elt->pattern);
1912 free(elt->sample);
1913 free(elt);
1914 }
1915
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001916 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001917 expr->pat_head->prune(expr);
1918}
1919
1920/* This function browse <ref> and try to index each entries in the <expr>.
1921 * If the flag <soe> (stop on error) is set, this function stop on the first
1922 * error, <err> is filled and return 0. If is not set, the function try to
1923 * load each entries and 1 is always returned.
1924 */
1925int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr,
1926 int patflags, int soe, char **err)
1927{
1928 struct pat_ref_elt *elt;
1929
1930 list_for_each_entry(elt, &ref->head, list) {
1931 if (soe && !pat_ref_push(elt, expr, patflags, err)) {
1932 if (elt->line > 0)
1933 memprintf(err, "%s at line %d of file '%s'",
1934 *err, elt->line, ref->reference);
1935 return 0;
1936 }
1937 }
1938 return 1;
1939}
1940
1941/* This function lookup for existing reference <ref> in pattern_head <head>. */
1942struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1943{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001944 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001945
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001946 list_for_each_entry(expr, &head->head, list)
1947 if (expr->expr->ref == ref)
1948 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001949 return NULL;
1950}
1951
1952/* This function create new pattern_expr associated to the reference <ref>.
1953 * <ref> can be NULL. If an error is occured, the function returns NULL and
1954 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1955 * with <head> and <ref>.
1956 */
1957struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1958{
1959 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001960 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001961
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001962 /* Memory and initialization of the chain element. */
1963 list = malloc(sizeof(*list));
1964 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001965 memprintf(err, "out of memory");
1966 return NULL;
1967 }
1968
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001969 /* Look for existing similar expr. No that only the index, parse and
1970 * parse_smp function must be identical for having similar pattern.
1971 * The other function depends of theses first.
1972 */
1973 if (ref) {
1974 list_for_each_entry(expr, &ref->pat, list)
1975 if (expr->pat_head->index == head->index &&
1976 expr->pat_head->parse == head->parse &&
1977 expr->pat_head->parse_smp == head->parse_smp)
1978 break;
1979 if (&expr->list == &ref->pat)
1980 expr = NULL;
1981 }
1982 else
1983 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001984
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001985 /* If no similar expr was found, we create new expr. */
1986 if (!expr) {
1987 /* Get a lot of memory for the expr struct. */
1988 expr = malloc(sizeof(*expr));
1989 if (!expr) {
1990 memprintf(err, "out of memory");
1991 return NULL;
1992 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001993
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001994 /* Initialize this new expr. */
1995 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001996
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001997 /* This new pattern expression reference one of his heads. */
1998 expr->pat_head = head;
1999
2000 /* Link with ref, or to self to facilitate LIST_DEL() */
2001 if (ref)
2002 LIST_ADDQ(&ref->pat, &expr->list);
2003 else
2004 LIST_INIT(&expr->list);
2005
2006 expr->ref = ref;
2007
2008 /* We must free this pattern if it is no more used. */
2009 list->do_free = 1;
2010 }
2011 else {
2012 /* If the pattern used already exists, it is already linked
2013 * with ref and we must not free it.
2014 */
2015 list->do_free = 0;
2016 }
2017
2018 /* The new list element reference the pattern_expr. */
2019 list->expr = expr;
2020
2021 /* Link the list element with the pattern_head. */
2022 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002023 return expr;
2024}
2025
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002026/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2027 * be returned there on errors and the caller will have to free it.
2028 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002029int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002030{
2031 FILE *file;
2032 char *c;
2033 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002034 int ret = 0;
2035 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002036
2037 file = fopen(filename, "r");
2038 if (!file) {
2039 memprintf(err, "failed to open pattern file <%s>", filename);
2040 return 0;
2041 }
2042
2043 /* now parse all patterns. The file may contain only one pattern per
2044 * line. If the line contains spaces, they will be part of the pattern.
2045 * The pattern stops at the first CR, LF or EOF encountered.
2046 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002047 while (fgets(trash.str, trash.size, file) != NULL) {
2048 line++;
2049 c = trash.str;
2050
2051 /* ignore lines beginning with a dash */
2052 if (*c == '#')
2053 continue;
2054
2055 /* strip leading spaces and tabs */
2056 while (*c == ' ' || *c == '\t')
2057 c++;
2058
2059
2060 arg = c;
2061 while (*c && *c != '\n' && *c != '\r')
2062 c++;
2063 *c = 0;
2064
2065 /* empty lines are ignored too */
2066 if (c == arg)
2067 continue;
2068
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002069 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002070 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2071 goto out_close;
2072 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002073 }
2074
2075 ret = 1; /* success */
2076
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002077 out_close:
2078 fclose(file);
2079 return ret;
2080}
2081
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002082int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
2083 const char *filename, int patflags,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002084 char **err, const char *display)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002085{
2086 struct pat_ref *ref;
2087 struct pattern_expr *expr;
2088
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002089 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002090 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002091
2092 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002093 if (!ref) {
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01002094 ref = pat_ref_new(filename, display, refflags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002095 if (!ref) {
2096 memprintf(err, "out of memory");
2097 return 0;
2098 }
2099
2100 if (!pat_ref_read_from_file(ref, filename, err))
2101 return 0;
2102 }
2103
2104 /* Now, we can loading patterns from the reference. */
2105
2106 /* Lookup for existing reference in the head. If the reference
2107 * doesn't exists, create it.
2108 */
2109 expr = pattern_lookup_expr(head, ref);
2110 if (!expr) {
2111 expr = pattern_new_expr(head, ref, err);
2112 if (!expr)
2113 return 0;
2114 }
2115
2116 /* Load reference content in expression. */
2117 if (!pat_ref_load(ref, expr, patflags, 1, err))
2118 return 0;
2119
2120 return 1;
2121}
2122
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002123/* This function executes a pattern match on a sample. It applies pattern <expr>
2124 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2125 * non-null if the sample match. If <fill> is true and the sample match, the
2126 * function returns the matched pattern. In many cases, this pattern can be a
2127 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002128 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002129struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002130{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002131 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002132 struct pattern *pat;
2133
2134 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002135 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002136 static_pattern.smp = NULL;
Thierry FOURNIER6bb53ff2014-01-28 15:54:36 +01002137 static_pattern.ref = NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002138 static_pattern.flags = 0;
2139 static_pattern.type = SMP_T_UINT;
2140 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002141 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002142 return &static_pattern;
2143 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002145 /* convert input to string */
2146 if (!sample_convert(smp, head->expect_type))
2147 return NULL;
2148
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002149 list_for_each_entry(list, &head->head, list) {
2150 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (pat)
2152 return pat;
2153 }
2154 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002155}
2156
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002157/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002158void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002159{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002160 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002162 list_for_each_entry_safe(list, safe, &head->head, list) {
2163 LIST_DEL(&list->list);
2164 if (list->do_free) {
2165 LIST_DEL(&list->expr->list);
2166 head->prune(list->expr);
2167 free(list->expr);
2168 }
2169 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002170 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002171}
2172
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002173/* This function lookup for a pattern matching the <key> and return a
2174 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2175 * the function returns NULL. If the key cannot be parsed, the function
2176 * fill <err>.
2177 */
2178struct sample_storage **pattern_find_smp(const char *key, struct pattern_expr *expr, char **err)
2179{
2180 struct pattern pattern;
2181
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182 if (!expr->pat_head->parse(key, &pattern, err))
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002183 return NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002184 return expr->pat_head->find_smp(expr, &pattern);
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002185}
2186
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002187/* This function search all the pattern matching the <key> and delete it.
2188 * If the parsing of the input key fails, the function returns 0 and the
2189 * <err> is filled, else return 1;
2190 */
2191int pattern_delete(const char *key, struct pattern_expr *expr, char **err)
2192{
2193 struct pattern pattern;
2194
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002195 if (!expr->pat_head->parse(key, &pattern, err))
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002196 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002197 expr->pat_head->delete(expr, &pattern);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002198 return 1;
2199}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002200
2201/* This function finalize the configuration parsing. Its set all the
2202 * automatic ids
2203 */
2204void pattern_finalize_config(void)
2205{
2206 int i = 0;
2207 struct pat_ref *ref, *ref2, *ref3;
2208 struct list pr = LIST_HEAD_INIT(pr);
2209
2210 list_for_each_entry(ref, &pattern_reference, list) {
2211 if (ref->unique_id == -1) {
2212 /* Look for the first free id. */
2213 while (1) {
2214 list_for_each_entry(ref2, &pattern_reference, list) {
2215 if (ref2->unique_id == i) {
2216 i++;
2217 break;
2218 }
2219 }
2220 if (&ref2->list == &pattern_reference);
2221 break;
2222 }
2223
2224 /* Uses the unique id and increment it for the next entry. */
2225 ref->unique_id = i;
2226 i++;
2227 }
2228 }
2229
2230 /* This sort the reference list by id. */
2231 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2232 LIST_DEL(&ref->list);
2233 list_for_each_entry(ref3, &pr, list) {
2234 if (ref->unique_id < ref3->unique_id) {
2235 LIST_ADDQ(&ref3->list, &ref->list);
2236 break;
2237 }
2238 }
2239 if (&ref3->list == &pr)
2240 LIST_ADDQ(&pr, &ref->list);
2241 }
2242
2243 /* swap root */
2244 LIST_ADD(&pr, &pattern_reference);
2245 LIST_DEL(&pr);
2246}