blob: 0850f2fe4a5840cf797d35e5df8ee7027b322ee6 [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;
481 static_pattern.flags = PAT_F_TREE;
482 static_pattern.type = SMP_T_STR;
483 static_pattern.ptr.str = (char *)elt->node.key;
484 }
485 return &static_pattern;
486 }
487 }
488
489 /* look in the list */
490 list_for_each_entry(lst, &expr->patterns, list) {
491 pattern = &lst->pat;
492
493 if (pattern->len != smp->data.str.len)
494 continue;
495
496 icase = pattern->flags & PAT_F_IGNORE_CASE;
497 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
498 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
499 return pattern;
500 }
501
502 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100503}
504
505/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100507{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100508 struct pattern_list *lst;
509 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100510
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100511 /* Look in the list. */
512 list_for_each_entry(lst, &expr->patterns, list) {
513 pattern = &lst->pat;
514
515 if (pattern->len != smp->data.str.len)
516 continue;
517
518 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
519 return pattern;
520 }
521
522 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100523}
524
525/* Executes a regex. It temporarily changes the data to add a trailing zero,
526 * and restores the previous character when leaving.
527 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100528struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100529{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100530 struct pattern_list *lst;
531 struct pattern *pattern;
532
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100533 /* look in the list */
534 list_for_each_entry(lst, &expr->patterns, list) {
535 pattern = &lst->pat;
536
537 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
538 return pattern;
539 }
540 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100541}
542
543/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100544struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100545{
546 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100547 struct pattern_list *lst;
548 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100549
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100550 list_for_each_entry(lst, &expr->patterns, list) {
551 pattern = &lst->pat;
552
553 if (pattern->len > smp->data.str.len)
554 continue;
555
556 icase = pattern->flags & PAT_F_IGNORE_CASE;
557 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
558 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
559 continue;
560
561 return pattern;
562 }
563 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100564}
565
566/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100567struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100568{
569 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100570 struct pattern_list *lst;
571 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100572
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100573 list_for_each_entry(lst, &expr->patterns, list) {
574 pattern = &lst->pat;
575
576 if (pattern->len > smp->data.str.len)
577 continue;
578
579 icase = pattern->flags & PAT_F_IGNORE_CASE;
580 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
581 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
582 continue;
583
584 return pattern;
585 }
586 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100587}
588
589/* Checks that the pattern is included inside the tested string.
590 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
591 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100592struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593{
594 int icase;
595 char *end;
596 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100597 struct pattern_list *lst;
598 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100599
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100600 list_for_each_entry(lst, &expr->patterns, list) {
601 pattern = &lst->pat;
602
603 if (pattern->len > smp->data.str.len)
604 continue;
605
606 end = smp->data.str.str + smp->data.str.len - pattern->len;
607 icase = pattern->flags & PAT_F_IGNORE_CASE;
608 if (icase) {
609 for (c = smp->data.str.str; c <= end; c++) {
610 if (tolower(*c) != tolower(*pattern->ptr.str))
611 continue;
612 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
613 return pattern;
614 }
615 } else {
616 for (c = smp->data.str.str; c <= end; c++) {
617 if (*c != *pattern->ptr.str)
618 continue;
619 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
620 return pattern;
621 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100622 }
623 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100624 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100625}
626
627/* This one is used by other real functions. It checks that the pattern is
628 * included inside the tested string, but enclosed between the specified
629 * delimiters or at the beginning or end of the string. The delimiters are
630 * provided as an unsigned int made by make_4delim() and match up to 4 different
631 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
632 */
633static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
634{
635 int may_match, icase;
636 char *c, *end;
637 char *ps;
638 int pl;
639
640 pl = pattern->len;
641 ps = pattern->ptr.str;
642
643 while (pl > 0 && is_delimiter(*ps, delimiters)) {
644 pl--;
645 ps++;
646 }
647
648 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
649 pl--;
650
651 if (pl > smp->data.str.len)
652 return PAT_NOMATCH;
653
654 may_match = 1;
655 icase = pattern->flags & PAT_F_IGNORE_CASE;
656 end = smp->data.str.str + smp->data.str.len - pl;
657 for (c = smp->data.str.str; c <= end; c++) {
658 if (is_delimiter(*c, delimiters)) {
659 may_match = 1;
660 continue;
661 }
662
663 if (!may_match)
664 continue;
665
666 if (icase) {
667 if ((tolower(*c) == tolower(*ps)) &&
668 (strncasecmp(ps, c, pl) == 0) &&
669 (c == end || is_delimiter(c[pl], delimiters)))
670 return PAT_MATCH;
671 } else {
672 if ((*c == *ps) &&
673 (strncmp(ps, c, pl) == 0) &&
674 (c == end || is_delimiter(c[pl], delimiters)))
675 return PAT_MATCH;
676 }
677 may_match = 0;
678 }
679 return PAT_NOMATCH;
680}
681
682/* Checks that the pattern is included inside the tested string, but enclosed
683 * between the delimiters '?' or '/' or at the beginning or end of the string.
684 * Delimiters at the beginning or end of the pattern are ignored.
685 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100686struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100687{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100688 struct pattern_list *lst;
689 struct pattern *pattern;
690
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100691 list_for_each_entry(lst, &expr->patterns, list) {
692 pattern = &lst->pat;
693 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
694 return pattern;
695 }
696 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100697}
698
699/* Checks that the pattern is included inside the tested string, but enclosed
700 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
701 * the string. Delimiters at the beginning or end of the pattern are ignored.
702 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100704{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100705 struct pattern_list *lst;
706 struct pattern *pattern;
707
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100708 list_for_each_entry(lst, &expr->patterns, list) {
709 pattern = &lst->pat;
710 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
711 return pattern;
712 }
713 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100714}
715
716/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100717struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100718{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719 struct pattern_list *lst;
720 struct pattern *pattern;
721
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100722 list_for_each_entry(lst, &expr->patterns, list) {
723 pattern = &lst->pat;
724 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
725 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
726 return pattern;
727 }
728 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100729}
730
731/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100732struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100733{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100734 struct pattern_list *lst;
735 struct pattern *pattern;
736
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100737 list_for_each_entry(lst, &expr->patterns, list) {
738 pattern = &lst->pat;
739 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
740 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
741 return pattern;
742 }
743 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100744}
745
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100746struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100747{
748 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100749 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100750 struct in_addr *s;
751 struct ebmb_node *node;
752 struct pattern_tree *elt;
753 struct pattern_list *lst;
754 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100756 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100757 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100758 /* Lookup an IPv4 address in the expression's pattern tree using
759 * the longest match method.
760 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100761 s = &smp->data.ipv4;
762 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
763 if (node) {
764 if (fill) {
765 elt = ebmb_entry(node, struct pattern_tree, node);
766 static_pattern.smp = elt->smp;
767 static_pattern.flags = PAT_F_TREE;
768 static_pattern.type = SMP_T_IPV4;
769 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
770 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
771 return NULL;
772 }
773 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100774 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100775
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100776 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
777 * sample address to IPv6 with the mapping method using the ::ffff:
778 * prefix, and try to lookup in the IPv6 tree.
779 */
780 memset(&tmp6, 0, 10);
781 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
782 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
783 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
784 if (node) {
785 if (fill) {
786 elt = ebmb_entry(node, struct pattern_tree, node);
787 static_pattern.smp = elt->smp;
788 static_pattern.flags = PAT_F_TREE;
789 static_pattern.type = SMP_T_IPV6;
790 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
791 static_pattern.val.ipv6.mask = elt->node.node.pfx;
792 }
793 return &static_pattern;
794 }
795 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100796
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100797 /* The input sample is IPv6. Try to match in the trees. */
798 if (smp->type == SMP_T_IPV6) {
799 /* Lookup an IPv6 address in the expression's pattern tree using
800 * the longest match method.
801 */
802 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
803 if (node) {
804 if (fill) {
805 elt = ebmb_entry(node, struct pattern_tree, node);
806 static_pattern.smp = elt->smp;
807 static_pattern.flags = PAT_F_TREE;
808 static_pattern.type = SMP_T_IPV6;
809 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
810 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100811 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100812 return &static_pattern;
813 }
814
815 /* Try to convert 6 to 4 when the start of the ipv6 address match the
816 * following forms :
817 * - ::ffff:ip:v4 (ipv4 mapped)
818 * - ::0000:ip:v4 (old ipv4 mapped)
819 * - 2002:ip:v4:: (6to4)
820 */
821 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
822 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
823 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
824 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
825 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
826 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
827 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
828 else
829 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
830 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
831
832 /* Lookup an IPv4 address in the expression's pattern tree using the longest
833 * match method.
834 */
835 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
836 if (node) {
837 if (fill) {
838 elt = ebmb_entry(node, struct pattern_tree, node);
839 static_pattern.smp = elt->smp;
840 static_pattern.flags = PAT_F_TREE;
841 static_pattern.type = SMP_T_IPV4;
842 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
843 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
844 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100845 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100846 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100847 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100848 }
849 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100850
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100851 /* Lookup in the list. the list contain only IPv4 patterns */
852 list_for_each_entry(lst, &expr->patterns, list) {
853 pattern = &lst->pat;
854
855 /* The input sample is IPv4, use it as is. */
856 if (smp->type == SMP_T_IPV4) {
857 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100858 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100859 else if (smp->type == SMP_T_IPV6) {
860 /* v4 match on a V6 sample. We want to check at least for
861 * the following forms :
862 * - ::ffff:ip:v4 (ipv4 mapped)
863 * - ::0000:ip:v4 (old ipv4 mapped)
864 * - 2002:ip:v4:: (6to4)
865 */
866 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
867 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
868 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
869 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
870 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100871 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100872 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
873 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
874 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100875 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100876 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100877 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100878 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100879
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100880 /* Check if the input sample match the current pattern. */
881 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100882 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100883 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100884 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100885}
886
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100887void free_pattern_tree(struct eb_root *root)
888{
889 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100890 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100891
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100892 node = eb_first(root);
893 while (node) {
894 next = eb_next(node);
895 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100896 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100897 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100898 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100899 node = next;
900 }
901}
902
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100903void pat_prune_val(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100904{
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100905 struct pattern_list *pat, *tmp;
906
907 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
908 free(pat->pat.smp);
909 free(pat);
910 }
911
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100912 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100913 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100914 LIST_INIT(&expr->patterns);
915}
916
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +0100917void pat_prune_ptr(struct pattern_expr *expr)
918{
919 struct pattern_list *pat, *tmp;
920
921 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
922 free(pat->pat.ptr.ptr);
923 free(pat->pat.smp);
924 free(pat);
925 }
926
927 free_pattern_tree(&expr->pattern_tree);
928 free_pattern_tree(&expr->pattern_tree_2);
929 LIST_INIT(&expr->patterns);
930}
931
932void pat_prune_reg(struct pattern_expr *expr)
933{
934 struct pattern_list *pat, *tmp;
935
936 list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
937 regex_free(pat->pat.ptr.ptr);
938 free(pat->pat.smp);
939 free(pat);
940 }
941
942 free_pattern_tree(&expr->pattern_tree);
943 free_pattern_tree(&expr->pattern_tree_2);
944 LIST_INIT(&expr->patterns);
945}
946
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100947/*
948 *
949 * The following functions are used for the pattern indexation
950 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100951 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100952
953int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100954{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100955 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100956
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100957 /* allocate pattern */
958 patl = calloc(1, sizeof(*patl));
959 if (!patl) {
960 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100961 return 0;
962 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100963
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100964 /* duplicate pattern */
965 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100966
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100967 /* chain pattern in the expression */
968 LIST_ADDQ(&expr->patterns, &patl->list);
969
970 /* that's ok */
971 return 1;
972}
973
974int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
975{
976 struct pattern_list *patl;
977
978 /* allocate pattern */
979 patl = calloc(1, sizeof(*patl));
980 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100981 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100982
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100983 /* duplicate pattern */
984 memcpy(&patl->pat, pat, sizeof(*pat));
985 patl->pat.ptr.ptr = malloc(patl->pat.len);
986 if (!patl->pat.ptr.ptr) {
987 free(patl);
988 memprintf(err, "out of memory while indexing pattern");
989 return 0;
990 }
991 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100992
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100993 /* chain pattern in the expression */
994 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100995
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100996 /* that's ok */
997 return 1;
998}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100999
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001000int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1001{
1002 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001003
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001004 /* allocate pattern */
1005 patl = calloc(1, sizeof(*patl));
1006 if (!patl) {
1007 memprintf(err, "out of memory while indexing pattern");
1008 return 0;
1009 }
1010
1011 /* duplicate pattern */
1012 memcpy(&patl->pat, pat, sizeof(*pat));
1013 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1014 if (!patl->pat.ptr.str) {
1015 free(patl);
1016 memprintf(err, "out of memory while indexing pattern");
1017 return 0;
1018 }
1019 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1020 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001021
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001022 /* chain pattern in the expression */
1023 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001024
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001025 /* that's ok */
1026 return 1;
1027}
1028
1029int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1030{
1031 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001032
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001033 /* allocate pattern */
1034 patl = calloc(1, sizeof(*patl));
1035 if (!patl) {
1036 memprintf(err, "out of memory while indexing pattern");
1037 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001038 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001039
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001040 /* duplicate pattern */
1041 memcpy(&patl->pat, pat, sizeof(*pat));
1042
1043 /* allocate regex */
1044 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1045 if (!patl->pat.ptr.reg) {
1046 free(patl);
1047 memprintf(err, "out of memory while indexing pattern");
1048 return 0;
1049 }
1050
1051 /* compile regex */
1052 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1053 free(patl);
1054 free(patl->pat.ptr.reg);
1055 return 0;
1056 }
1057
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001058 /* chain pattern in the expression */
1059 LIST_ADDQ(&expr->patterns, &patl->list);
1060
1061 /* that's ok */
1062 return 1;
1063}
1064
1065int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1066{
1067 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001068 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001069
1070 /* Only IPv4 can be indexed */
1071 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001072 /* in IPv4 case, check if the mask is contiguous so that we can
1073 * insert the network into the tree. A continuous mask has only
1074 * ones on the left. This means that this mask + its lower bit
1075 * added once again is null.
1076 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001077 mask = ntohl(pat->val.ipv4.mask.s_addr);
1078 if (mask + (mask & -mask) == 0) {
1079 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001080
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001081 /* node memory allocation */
1082 node = calloc(1, sizeof(*node) + 4);
1083 if (!node) {
1084 memprintf(err, "out of memory while loading pattern");
1085 return 0;
1086 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001087
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001088 /* copy the pointer to sample associated to this node */
1089 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001090
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001091 /* FIXME: insert <addr>/<mask> into the tree here */
1092 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1093 node->node.node.pfx = mask;
1094 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1095 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001096
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001097 /* that's ok */
1098 return 1;
1099 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001100 else {
1101 /* If the mask is not contiguous, just add the pattern to the list */
1102 return pat_idx_list_val(expr, pat, err);
1103 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001104 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001105 else if (pat->type == SMP_T_IPV6) {
1106 /* IPv6 also can be indexed */
1107 node = calloc(1, sizeof(*node) + 16);
1108 if (!node) {
1109 memprintf(err, "out of memory while loading pattern");
1110 return 0;
1111 }
1112
1113 /* copy the pointer to sample associated to this node */
1114 node->smp = pat->smp;
1115
1116 /* FIXME: insert <addr>/<mask> into the tree here */
1117 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1118 node->node.node.pfx = pat->val.ipv6.mask;
1119 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1120 free(node); /* was a duplicate */
1121
1122 /* that's ok */
1123 return 1;
1124 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001125
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001126 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001127}
1128
1129int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1130{
1131 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001132 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001133
1134 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001135 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001136 memprintf(err, "internal error: string expected, but the type is '%s'",
1137 smp_to_type[pat->type]);
1138 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001139 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001140
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001141 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1142 if (pat->flags & PAT_F_IGNORE_CASE)
1143 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001144
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145 /* Process the key len */
1146 len = strlen(pat->ptr.str) + 1;
1147
1148 /* node memory allocation */
1149 node = calloc(1, sizeof(*node) + len);
1150 if (!node) {
1151 memprintf(err, "out of memory while loading pattern");
1152 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001153 }
1154
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001155 /* copy the pointer to sample associated to this node */
1156 node->smp = pat->smp;
1157
1158 /* copy the string */
1159 memcpy(node->node.key, pat->ptr.str, len);
1160
1161 /* index the new node */
1162 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1163 free(node); /* was a duplicate */
1164
1165 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001166 return 1;
1167}
1168
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01001169struct sample_storage **pat_find_smp_list_val(struct pattern_expr *expr, struct pattern *pattern)
1170{
1171 struct pattern_list *pat;
1172 struct pattern_list *safe;
1173
1174 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1175
1176 /* Check equality. */
1177 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1178 continue;
1179 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1180 continue;
1181 if (pattern->val.range.min_set &&
1182 pattern->val.range.min != pat->pat.val.range.min)
1183 continue;
1184 if (pattern->val.range.max_set &&
1185 pattern->val.range.max != pat->pat.val.range.max)
1186 continue;
1187
1188 /* Return the pointer on the sample pointer. */
1189 return &pat->pat.smp;
1190 }
1191
1192 return NULL;
1193}
1194
1195struct sample_storage **pat_find_smp_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1196{
1197 struct ebmb_node *node, *next_node;
1198 struct pattern_tree *elt;
1199 struct pattern_list *pat;
1200 struct pattern_list *safe;
1201 unsigned int mask;
1202
1203 /* browse each node of the tree for IPv4 addresses. */
1204 if (pattern->type == SMP_T_IPV4) {
1205 /* Convert mask. If the mask is contiguous, browse each node
1206 * of the tree for IPv4 addresses.
1207 */
1208 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1209 if (mask + (mask & -mask) == 0) {
1210 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1211
1212 for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node);
1213 node;
1214 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1215 /* Extract container of the tree node. */
1216 elt = container_of(node, struct pattern_tree, node);
1217
1218 /* Check equality. */
1219 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1220 continue;
1221
1222 /* Return the pointer on the sample pointer. */
1223 return &elt->smp;
1224 }
1225 }
1226 else {
1227 /* Browse each node of the list for IPv4 addresses. */
1228 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1229 /* Check equality. */
1230 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1231 sizeof(pat->pat.val.ipv4.addr)) != 0)
1232 continue;
1233 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1234 sizeof(pat->pat.val.ipv4.addr)) != 0)
1235 continue;
1236
1237 /* Return the pointer on the sample pointer. */
1238 return &pat->pat.smp;
1239 }
1240 }
1241 }
1242 else if (pattern->type == SMP_T_IPV6) {
1243 /* browse each node of the tree for IPv4 addresses. */
1244 for (node = ebmb_first(&expr->pattern_tree_2), next_node = ebmb_next(node);
1245 node;
1246 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1247 /* Extract container of the tree node. */
1248 elt = container_of(node, struct pattern_tree, node);
1249
1250 /* Check equality. */
1251 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1252 continue;
1253
1254 /* Return the pointer on the sample pointer. */
1255 return &elt->smp;
1256 }
1257 }
1258
1259 return NULL;
1260}
1261
1262struct sample_storage **pat_find_smp_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1263{
1264 struct pattern_list *pat;
1265 struct pattern_list *safe;
1266
1267 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1268 /* Check equality. */
1269 if (pattern->len != pat->pat.len)
1270 continue;
1271 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1272 continue;
1273
1274 /* Return the pointer on the sample pointer. */
1275 return &pat->pat.smp;
1276 }
1277
1278 return NULL;
1279}
1280
1281struct sample_storage **pat_find_smp_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1282{
1283 struct ebmb_node *node, *next_node;
1284 struct pattern_tree *elt;
1285
1286 /* browse each node of the tree. */
1287 for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node);
1288 node;
1289 node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) {
1290 /* Extract container of the tree node. */
1291 elt = container_of(node, struct pattern_tree, node);
1292
1293 /* Check equality. */
1294 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1295 continue;
1296
1297 /* Return the pointer on the sample pointer. */
1298 return &elt->smp;
1299 }
1300
1301 return NULL;
1302}
1303
1304struct sample_storage **pat_find_smp_list_str(struct pattern_expr *expr, struct pattern *pattern)
1305{
1306 struct pattern_list *pat;
1307 struct pattern_list *safe;
1308
1309 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1310 /* Check equality. */
1311 if (pattern->len != pat->pat.len)
1312 continue;
1313 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1314 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1315 continue;
1316 }
1317 else {
1318 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1319 continue;
1320 }
1321
1322 /* Return the pointer on the sample pointer. */
1323 return &pat->pat.smp;
1324 }
1325
1326 return NULL;
1327}
1328
1329struct sample_storage **pat_find_smp_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1330{
1331 struct pattern_list *pat;
1332 struct pattern_list *safe;
1333
1334 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1335 /* Check equality. */
1336 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1337 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1338 continue;
1339 }
1340 else {
1341 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1342 continue;
1343 }
1344
1345 /* Return the pointer on the sample pointer. */
1346 return &pat->pat.smp;
1347 }
1348
1349 return NULL;
1350}
1351
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001352void pat_del_list_val(struct pattern_expr *expr, struct pattern *pattern)
1353{
1354 struct pattern_list *pat;
1355 struct pattern_list *safe;
1356
1357 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1358 /* Check equality. */
1359 if (pattern->val.range.min_set != pat->pat.val.range.min_set)
1360 continue;
1361 if (pattern->val.range.max_set != pat->pat.val.range.max_set)
1362 continue;
1363 if (pattern->val.range.min_set &&
1364 pattern->val.range.min != pat->pat.val.range.min)
1365 continue;
1366 if (pattern->val.range.max_set &&
1367 pattern->val.range.max != pat->pat.val.range.max)
1368 continue;
1369
1370 /* Delete and free entry. */
1371 LIST_DEL(&pat->list);
1372 free(pat->pat.smp);
1373 free(pat);
1374 }
1375}
1376
1377void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pattern)
1378{
1379 struct ebmb_node *node, *next_node;
1380 struct pattern_tree *elt;
1381 struct pattern_list *pat;
1382 struct pattern_list *safe;
1383 unsigned int mask;
1384
1385 /* browse each node of the tree for IPv4 addresses. */
1386 if (pattern->type == SMP_T_IPV4) {
1387 /* Convert mask. If the mask is contiguous, browse each node
1388 * of the tree for IPv4 addresses.
1389 */
1390 mask = ntohl(pattern->val.ipv4.mask.s_addr);
1391 if (mask + (mask & -mask) == 0) {
1392 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1393
1394 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1395 node;
1396 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1397 /* Extract container of the tree node. */
1398 elt = container_of(node, struct pattern_tree, node);
1399
1400 /* Check equality. */
1401 if (memcmp(&pattern->val.ipv4.addr, elt->node.key,
1402 sizeof(pattern->val.ipv4.addr)) != 0)
1403 continue;
1404 if (elt->node.node.pfx != mask)
1405 continue;
1406
1407 /* Delete and free entry. */
1408 ebmb_delete(node);
1409 free(elt->smp);
1410 free(elt);
1411 }
1412 }
1413 else {
1414 /* Browse each node of the list for IPv4 addresses. */
1415 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1416 /* Check equality, addr then mask */
1417 if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr,
1418 sizeof(pat->pat.val.ipv4.addr)) != 0)
1419 continue;
1420
1421 if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask,
1422 sizeof(pat->pat.val.ipv4.addr)) != 0)
1423 continue;
1424
1425 /* Delete and free entry. */
1426 LIST_DEL(&pat->list);
1427 free(pat->pat.smp);
1428 free(pat);
1429 }
1430 }
1431 }
1432 else if (pattern->type == SMP_T_IPV6) {
1433 /* browse each node of the tree for IPv6 addresses. */
1434 for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1435 node;
1436 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1437 /* Extract container of the tree node. */
1438 elt = container_of(node, struct pattern_tree, node);
1439
1440 /* Check equality. */
1441 if (memcmp(&pattern->val.ipv6.addr, elt->node.key,
1442 sizeof(pattern->val.ipv6.addr)) != 0)
1443 continue;
1444 if (elt->node.node.pfx != pattern->val.ipv6.mask)
1445 continue;
1446
1447 /* Delete and free entry. */
1448 ebmb_delete(node);
1449 free(elt->smp);
1450 free(elt);
1451 }
1452 }
1453}
1454
1455void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pattern)
1456{
1457 struct pattern_list *pat;
1458 struct pattern_list *safe;
1459
1460 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1461 /* Check equality. */
1462 if (pattern->len != pat->pat.len)
1463 continue;
1464 if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
1465 continue;
1466
1467 /* Delete and free entry. */
1468 LIST_DEL(&pat->list);
1469 free(pat->pat.ptr.ptr);
1470 free(pat->pat.smp);
1471 free(pat);
1472 }
1473}
1474
1475void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pattern)
1476{
1477 struct ebmb_node *node, *next_node;
1478 struct pattern_tree *elt;
1479
1480 /* browse each node of the tree. */
1481 for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1482 node;
1483 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1484 /* Extract container of the tree node. */
1485 elt = container_of(node, struct pattern_tree, node);
1486
1487 /* Check equality. */
1488 if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0)
1489 continue;
1490
1491 /* Delete and free entry. */
1492 ebmb_delete(node);
1493 free(elt->smp);
1494 free(elt);
1495 }
1496}
1497
1498void pat_del_list_str(struct pattern_expr *expr, struct pattern *pattern)
1499{
1500 struct pattern_list *pat;
1501 struct pattern_list *safe;
1502
1503 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1504 /* Check equality. */
1505 if (pattern->len != pat->pat.len)
1506 continue;
1507 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1508 if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1509 continue;
1510 }
1511 else {
1512 if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
1513 continue;
1514 }
1515
1516 /* Delete and free entry. */
1517 LIST_DEL(&pat->list);
1518 free(pat->pat.ptr.str);
1519 free(pat->pat.smp);
1520 free(pat);
1521 }
1522}
1523
1524void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pattern)
1525{
1526 struct pattern_list *pat;
1527 struct pattern_list *safe;
1528
1529 list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1530 /* Check equality. */
1531 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1532 if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1533 continue;
1534 }
1535 else {
1536 if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
1537 continue;
1538 }
1539
1540 /* Delete and free entry. */
1541 LIST_DEL(&pat->list);
1542 regex_free(pat->pat.ptr.ptr);
1543 free(pat->pat.smp);
1544 free(pat);
1545 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001546}
1547
1548void pattern_init_expr(struct pattern_expr *expr)
1549{
1550 LIST_INIT(&expr->patterns);
1551 expr->pattern_tree = EB_ROOT_UNIQUE;
1552 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
1553}
1554
1555void pattern_init_head(struct pattern_head *head)
1556{
1557 LIST_INIT(&head->head);
1558}
1559
1560/* The following functions are relative to the management of the reference
1561 * lists. These lists are used to store the original pattern and associated
1562 * value as string form.
1563 *
1564 * This is used with modifiable ACL and MAPS
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001565 *
1566 * The pattern reference are stored with two identifiers: the unique_id and
1567 * the reference.
1568 *
1569 * The reference identify a file. Each file with the same name point to the
1570 * same reference. We can register many times one file. If the file is modified,
1571 * all his dependencies are also modified. The reference can be used with map or
1572 * acl.
1573 *
1574 * The unique_id identify inline acl. The unique id is unique for each acl.
1575 * You cannot force the same id in the configuration file, because this repoort
1576 * an error.
1577 *
1578 * A particular case appears if the filename is a number. In this case, the
1579 * unique_id is set with the number represented by the filename and the
1580 * reference is also set. This method prevent double unique_id.
1581 *
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001582 */
1583
1584/* This function lookup for reference. If the reference is found, they return
1585 * pointer to the struct pat_ref, else return NULL.
1586 */
1587struct pat_ref *pat_ref_lookup(const char *reference)
1588{
1589 struct pat_ref *ref;
1590
1591 list_for_each_entry(ref, &pattern_reference, list)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001592 if (ref->reference && strcmp(reference, ref->reference) == 0)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001593 return ref;
1594 return NULL;
1595}
1596
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001597/* This function lookup for unique id. If the reference is found, they return
1598 * pointer to the struct pat_ref, else return NULL.
1599 */
1600struct pat_ref *pat_ref_lookupid(int unique_id)
1601{
1602 struct pat_ref *ref;
1603
1604 list_for_each_entry(ref, &pattern_reference, list)
1605 if (ref->unique_id == unique_id)
1606 return ref;
1607 return NULL;
1608}
1609
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610/* This function remove all pattern match <key> from the the reference
1611 * and from each expr member of the reference. This fucntion returns 1
1612 * if the deletion is done and return 0 is the entry is not found.
1613 */
1614int pat_ref_delete(struct pat_ref *ref, const char *key)
1615{
1616 struct pattern_expr *expr;
1617 struct pat_ref_elt *elt, *safe;
1618 int found = 0;
1619
1620 /* delete pattern from reference */
1621 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1622 if (strcmp(key, elt->pattern) == 0) {
1623 LIST_DEL(&elt->list);
1624 free(elt->sample);
1625 free(elt->pattern);
1626 free(elt);
1627 found = 1;
1628 }
1629 }
1630
1631 if (!found)
1632 return 0;
1633
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001634 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001635 pattern_delete(key, expr, NULL);
1636
1637 return 1;
1638}
1639
1640/* This function modify the sample of the first pattern that match the <key>. */
1641int pat_ref_set(struct pat_ref *ref, const char *key, const char *value)
1642{
1643 struct pattern_expr *expr;
1644 struct pat_ref_elt *elt;
1645 struct sample_storage **smp;
1646 char *sample;
1647 int found = 0;
1648
1649 /* modify pattern from reference */
1650 list_for_each_entry(elt, &ref->head, list) {
1651 if (strcmp(key, elt->pattern) == 0) {
1652 sample = strdup(value);
1653 if (!sample)
1654 return 0;
1655 free(elt->sample);
1656 elt->sample = sample;
1657 found = 1;
1658 break;
1659 }
1660 }
1661
1662 if (!found)
1663 return 0;
1664
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001665 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001666 smp = pattern_find_smp(key, expr, NULL);
1667 if (smp && expr->pat_head->parse_smp)
1668 if (!expr->pat_head->parse_smp(value, *smp))
1669 *smp = NULL;
1670 }
1671
1672 return 1;
1673}
1674
1675/* This function create new reference. <ref> is the reference name.
1676 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1677 * be unique. The user must check the reference with "pat_ref_lookup()"
1678 * before calling this function. If the fucntion fail, it return NULL,
1679 * else return new struct pat_ref.
1680 */
1681struct pat_ref *pat_ref_new(const char *reference, unsigned int flags)
1682{
1683 struct pat_ref *ref;
1684
1685 ref = malloc(sizeof(*ref));
1686 if (!ref)
1687 return NULL;
1688
1689 ref->reference = strdup(reference);
1690 if (!ref->reference) {
1691 free(ref);
1692 return NULL;
1693 }
1694
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001695 ref->flags = flags;
1696 ref->unique_id = -1;
1697
1698 LIST_INIT(&ref->head);
1699 LIST_INIT(&ref->pat);
1700
1701 LIST_ADDQ(&pattern_reference, &ref->list);
1702
1703 return ref;
1704}
1705
1706/* This function create new reference. <unique_id> is the unique id. If
1707 * the value of <unique_id> is -1, the unique id is calculated later.
1708 * <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1709 * be unique. The user must check the reference with "pat_ref_lookup()"
1710 * or pat_ref_lookupid before calling this function. If the function
1711 * fail, it return NULL, else return new struct pat_ref.
1712 */
1713struct pat_ref *pat_ref_newid(int unique_id, unsigned int flags)
1714{
1715 struct pat_ref *ref;
1716
1717 ref = malloc(sizeof(*ref));
1718 if (!ref)
1719 return NULL;
1720
1721 ref->reference = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001722 ref->flags = flags;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001723 ref->unique_id = unique_id;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001724 LIST_INIT(&ref->head);
1725 LIST_INIT(&ref->pat);
1726
1727 LIST_ADDQ(&pattern_reference, &ref->list);
1728
1729 return ref;
1730}
1731
1732/* This function adds entry to <ref>. It can failed with memory error.
1733 * If the function fails, it returns 0.
1734 */
1735int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1736{
1737 struct pat_ref_elt *elt;
1738
1739 elt = malloc(sizeof(*elt));
1740 if (!elt)
1741 return 0;
1742
1743 elt->line = line;
1744
1745 elt->pattern = strdup(pattern);
1746 if (!elt->pattern) {
1747 free(elt);
1748 return 0;
1749 }
1750
1751 if (sample) {
1752 elt->sample = strdup(sample);
1753 if (!elt->sample) {
1754 free(elt->pattern);
1755 free(elt);
1756 return 0;
1757 }
1758 }
1759 else
1760 elt->sample = NULL;
1761
1762 LIST_ADDQ(&ref->head, &elt->list);
1763
1764 return 1;
Thierry FOURNIERb1136502014-01-15 11:38:49 +01001765}
1766
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001767/* This function create sample found in <elt>, parse the pattern also
1768 * found in <elt> and insert it in <expr>. The function copy <patflags>
1769 * in <expr>. If the function fails, it returns0 and <err> is filled.
1770 * In succes case, the function returns 1.
1771 */
1772static inline
1773int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1774 int patflags, char **err)
1775{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001776 struct sample_storage *smp;
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001777 struct pattern pattern;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001778
1779 /* Create sample */
1780 if (elt->sample && expr->pat_head->parse_smp) {
1781 /* New sample. */
1782 smp = malloc(sizeof(*smp));
1783 if (!smp)
1784 return 0;
1785
1786 /* Parse value. */
1787 if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1788 memprintf(err, "unable to parse '%s'", elt->sample);
1789 free(smp);
1790 return 0;
1791 }
1792
1793 }
1794 else
1795 smp = NULL;
1796
Thierry FOURNIERd25c8422014-01-28 15:34:35 +01001797 /* initialise pattern */
1798 memset(&pattern, 0, sizeof(pattern));
1799 pattern.flags = patflags;
1800 pattern.smp = smp;
1801
1802 /* parse pattern */
1803 if (!expr->pat_head->parse(elt->pattern, &pattern, err)) {
1804 free(smp);
1805 return 0;
1806 }
1807
1808 /* index pattern */
1809 if (!expr->pat_head->index(expr, &pattern, err)) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001810 free(smp);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001811 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001812 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001813
1814 return 1;
1815}
1816
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001817/* This function adds entry to <ref>. It can failed with memory error.
1818 * The new entry is added at all the pattern_expr registered in this
1819 * reference. The function stop on the first error encountered. It
1820 * returns 0 and err is filled.
1821 *
1822 * If an error is encountered, The complete add operation is cancelled.
1823 */
1824int pat_ref_add(struct pat_ref *ref,
1825 const char *pattern, const char *sample,
1826 char **err)
1827{
1828 struct pat_ref_elt *elt;
1829 struct pattern_expr *expr;
1830
1831 elt = malloc(sizeof(*elt));
1832 if (!elt) {
1833 memprintf(err, "out of memory error");
1834 return 0;
1835 }
1836
1837 elt->line = -1;
1838
1839 elt->pattern = strdup(pattern);
1840 if (!elt->pattern) {
1841 free(elt);
1842 memprintf(err, "out of memory error");
1843 return 0;
1844 }
1845
1846 if (sample) {
1847 elt->sample = strdup(sample);
1848 if (!elt->sample) {
1849 free(elt->pattern);
1850 free(elt);
1851 memprintf(err, "out of memory error");
1852 return 0;
1853 }
1854 }
1855 else
1856 elt->sample = NULL;
1857
1858 LIST_ADDQ(&ref->head, &elt->list);
1859
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001860 list_for_each_entry(expr, &ref->pat, list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001861 if (!pat_ref_push(elt, expr, 0, err)) {
1862 /* Try to delete all the added entries. */
1863 pat_ref_delete(ref, pattern);
1864 return 0;
1865 }
1866 }
1867
1868 return 1;
1869}
1870
1871/* This function prune all entries of <ref>. This function
1872 * prune the associated pattern_expr.
1873 */
1874void pat_ref_prune(struct pat_ref *ref)
1875{
1876 struct pat_ref_elt *elt, *safe;
1877 struct pattern_expr *expr;
1878
1879 list_for_each_entry_safe(elt, safe, &ref->head, list) {
1880 LIST_DEL(&elt->list);
1881 free(elt->pattern);
1882 free(elt->sample);
1883 free(elt);
1884 }
1885
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001886 list_for_each_entry(expr, &ref->pat, list)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001887 expr->pat_head->prune(expr);
1888}
1889
1890/* This function browse <ref> and try to index each entries in the <expr>.
1891 * If the flag <soe> (stop on error) is set, this function stop on the first
1892 * error, <err> is filled and return 0. If is not set, the function try to
1893 * load each entries and 1 is always returned.
1894 */
1895int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr,
1896 int patflags, int soe, char **err)
1897{
1898 struct pat_ref_elt *elt;
1899
1900 list_for_each_entry(elt, &ref->head, list) {
1901 if (soe && !pat_ref_push(elt, expr, patflags, err)) {
1902 if (elt->line > 0)
1903 memprintf(err, "%s at line %d of file '%s'",
1904 *err, elt->line, ref->reference);
1905 return 0;
1906 }
1907 }
1908 return 1;
1909}
1910
1911/* This function lookup for existing reference <ref> in pattern_head <head>. */
1912struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1913{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001914 struct pattern_expr_list *expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001915
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001916 list_for_each_entry(expr, &head->head, list)
1917 if (expr->expr->ref == ref)
1918 return expr->expr;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001919 return NULL;
1920}
1921
1922/* This function create new pattern_expr associated to the reference <ref>.
1923 * <ref> can be NULL. If an error is occured, the function returns NULL and
1924 * <err> is filled. Otherwise, the function returns new pattern_expr linked
1925 * with <head> and <ref>.
1926 */
1927struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1928{
1929 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001930 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001931
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001932 /* Memory and initialization of the chain element. */
1933 list = malloc(sizeof(*list));
1934 if (!list) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001935 memprintf(err, "out of memory");
1936 return NULL;
1937 }
1938
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001939 /* Look for existing similar expr. No that only the index, parse and
1940 * parse_smp function must be identical for having similar pattern.
1941 * The other function depends of theses first.
1942 */
1943 if (ref) {
1944 list_for_each_entry(expr, &ref->pat, list)
1945 if (expr->pat_head->index == head->index &&
1946 expr->pat_head->parse == head->parse &&
1947 expr->pat_head->parse_smp == head->parse_smp)
1948 break;
1949 if (&expr->list == &ref->pat)
1950 expr = NULL;
1951 }
1952 else
1953 expr = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001954
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001955 /* If no similar expr was found, we create new expr. */
1956 if (!expr) {
1957 /* Get a lot of memory for the expr struct. */
1958 expr = malloc(sizeof(*expr));
1959 if (!expr) {
1960 memprintf(err, "out of memory");
1961 return NULL;
1962 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001963
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001964 /* Initialize this new expr. */
1965 pattern_init_expr(expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001966
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001967 /* This new pattern expression reference one of his heads. */
1968 expr->pat_head = head;
1969
1970 /* Link with ref, or to self to facilitate LIST_DEL() */
1971 if (ref)
1972 LIST_ADDQ(&ref->pat, &expr->list);
1973 else
1974 LIST_INIT(&expr->list);
1975
1976 expr->ref = ref;
1977
1978 /* We must free this pattern if it is no more used. */
1979 list->do_free = 1;
1980 }
1981 else {
1982 /* If the pattern used already exists, it is already linked
1983 * with ref and we must not free it.
1984 */
1985 list->do_free = 0;
1986 }
1987
1988 /* The new list element reference the pattern_expr. */
1989 list->expr = expr;
1990
1991 /* Link the list element with the pattern_head. */
1992 LIST_ADDQ(&head->head, &list->list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001993 return expr;
1994}
1995
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001996/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1997 * be returned there on errors and the caller will have to free it.
1998 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001999int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002000{
2001 FILE *file;
2002 char *c;
2003 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002004 int ret = 0;
2005 int line = 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002006
2007 file = fopen(filename, "r");
2008 if (!file) {
2009 memprintf(err, "failed to open pattern file <%s>", filename);
2010 return 0;
2011 }
2012
2013 /* now parse all patterns. The file may contain only one pattern per
2014 * line. If the line contains spaces, they will be part of the pattern.
2015 * The pattern stops at the first CR, LF or EOF encountered.
2016 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002017 while (fgets(trash.str, trash.size, file) != NULL) {
2018 line++;
2019 c = trash.str;
2020
2021 /* ignore lines beginning with a dash */
2022 if (*c == '#')
2023 continue;
2024
2025 /* strip leading spaces and tabs */
2026 while (*c == ' ' || *c == '\t')
2027 c++;
2028
2029
2030 arg = c;
2031 while (*c && *c != '\n' && *c != '\r')
2032 c++;
2033 *c = 0;
2034
2035 /* empty lines are ignored too */
2036 if (c == arg)
2037 continue;
2038
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002039 if (!pat_ref_append(ref, arg, NULL, line)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002040 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2041 goto out_close;
2042 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002043 }
2044
2045 ret = 1; /* success */
2046
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002047 out_close:
2048 fclose(file);
2049 return ret;
2050}
2051
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002052int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
2053 const char *filename, int patflags,
2054 char **err)
2055{
2056 struct pat_ref *ref;
2057 struct pattern_expr *expr;
2058
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002059 /* Lookup for the existing reference. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 ref = pat_ref_lookup(filename);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002061
2062 /* If the reference doesn't exists, create it and load associated file. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002063 if (!ref) {
2064 ref = pat_ref_new(filename, refflags);
2065 if (!ref) {
2066 memprintf(err, "out of memory");
2067 return 0;
2068 }
2069
2070 if (!pat_ref_read_from_file(ref, filename, err))
2071 return 0;
2072 }
2073
2074 /* Now, we can loading patterns from the reference. */
2075
2076 /* Lookup for existing reference in the head. If the reference
2077 * doesn't exists, create it.
2078 */
2079 expr = pattern_lookup_expr(head, ref);
2080 if (!expr) {
2081 expr = pattern_new_expr(head, ref, err);
2082 if (!expr)
2083 return 0;
2084 }
2085
2086 /* Load reference content in expression. */
2087 if (!pat_ref_load(ref, expr, patflags, 1, err))
2088 return 0;
2089
2090 return 1;
2091}
2092
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002093/* This function executes a pattern match on a sample. It applies pattern <expr>
2094 * to sample <smp>. The function returns NULL if the sample dont match. It returns
2095 * non-null if the sample match. If <fill> is true and the sample match, the
2096 * function returns the matched pattern. In many cases, this pattern can be a
2097 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002098 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002099struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002100{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002101 struct pattern_expr_list *list;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002102 struct pattern *pat;
2103
2104 if (!head->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002105 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01002106 static_pattern.smp = NULL;
2107 static_pattern.flags = 0;
2108 static_pattern.type = SMP_T_UINT;
2109 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002110 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01002111 return &static_pattern;
2112 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002113
Thierry FOURNIER5d344082014-01-27 14:19:53 +01002114 /* convert input to string */
2115 if (!sample_convert(smp, head->expect_type))
2116 return NULL;
2117
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002118 list_for_each_entry(list, &head->head, list) {
2119 pat = head->match(smp, list->expr, fill);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002120 if (pat)
2121 return pat;
2122 }
2123 return NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01002124}
2125
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002126/* This function prune the pattern expression. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002127void pattern_prune(struct pattern_head *head)
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002128{
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002129 struct pattern_expr_list *list, *safe;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002130
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01002131 list_for_each_entry_safe(list, safe, &head->head, list) {
2132 LIST_DEL(&list->list);
2133 if (list->do_free) {
2134 LIST_DEL(&list->expr->list);
2135 head->prune(list->expr);
2136 free(list->expr);
2137 }
2138 free(list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002139 }
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +01002140}
2141
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002142/* This function lookup for a pattern matching the <key> and return a
2143 * pointer to a pointer of the sample stoarge. If the <key> dont match,
2144 * the function returns NULL. If the key cannot be parsed, the function
2145 * fill <err>.
2146 */
2147struct sample_storage **pattern_find_smp(const char *key, struct pattern_expr *expr, char **err)
2148{
2149 struct pattern pattern;
2150
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (!expr->pat_head->parse(key, &pattern, err))
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002152 return NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002153 return expr->pat_head->find_smp(expr, &pattern);
Thierry FOURNIER55d0b102014-01-15 11:25:26 +01002154}
2155
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002156/* This function search all the pattern matching the <key> and delete it.
2157 * If the parsing of the input key fails, the function returns 0 and the
2158 * <err> is filled, else return 1;
2159 */
2160int pattern_delete(const char *key, struct pattern_expr *expr, char **err)
2161{
2162 struct pattern pattern;
2163
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164 if (!expr->pat_head->parse(key, &pattern, err))
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002165 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002166 expr->pat_head->delete(expr, &pattern);
Thierry FOURNIERb1136502014-01-15 11:38:49 +01002167 return 1;
2168}
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002169
2170/* This function finalize the configuration parsing. Its set all the
2171 * automatic ids
2172 */
2173void pattern_finalize_config(void)
2174{
2175 int i = 0;
2176 struct pat_ref *ref, *ref2, *ref3;
2177 struct list pr = LIST_HEAD_INIT(pr);
2178
2179 list_for_each_entry(ref, &pattern_reference, list) {
2180 if (ref->unique_id == -1) {
2181 /* Look for the first free id. */
2182 while (1) {
2183 list_for_each_entry(ref2, &pattern_reference, list) {
2184 if (ref2->unique_id == i) {
2185 i++;
2186 break;
2187 }
2188 }
2189 if (&ref2->list == &pattern_reference);
2190 break;
2191 }
2192
2193 /* Uses the unique id and increment it for the next entry. */
2194 ref->unique_id = i;
2195 i++;
2196 }
2197 }
2198
2199 /* This sort the reference list by id. */
2200 list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2201 LIST_DEL(&ref->list);
2202 list_for_each_entry(ref3, &pr, list) {
2203 if (ref->unique_id < ref3->unique_id) {
2204 LIST_ADDQ(&ref3->list, &ref->list);
2205 break;
2206 }
2207 }
2208 if (&ref3->list == &pr)
2209 LIST_ADDQ(&pr, &ref->list);
2210 }
2211
2212 /* swap root */
2213 LIST_ADD(&pr, &pattern_reference);
2214 LIST_DEL(&pr);
2215}