blob: d6ae8d9f7f5caa123ef70cf185d74a1730220ff2 [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 FOURNIERcc0e0b32013-12-06 16:56:40 +010049 [PAT_MATCH_LEN] = pat_parse_len,
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 FOURNIER5338eea2013-12-16 14:22:13 +010075struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010076 [PAT_MATCH_FOUND] = NULL,
77 [PAT_MATCH_BOOL] = pat_match_nothing,
78 [PAT_MATCH_INT] = pat_match_int,
79 [PAT_MATCH_IP] = pat_match_ip,
80 [PAT_MATCH_BIN] = pat_match_bin,
81 [PAT_MATCH_LEN] = pat_match_len,
82 [PAT_MATCH_STR] = pat_match_str,
83 [PAT_MATCH_BEG] = pat_match_beg,
84 [PAT_MATCH_SUB] = pat_match_sub,
85 [PAT_MATCH_DIR] = pat_match_dir,
86 [PAT_MATCH_DOM] = pat_match_dom,
87 [PAT_MATCH_END] = pat_match_end,
88 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010089};
90
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010091/* Just used for checking configuration compatibility */
92int pat_match_types[PAT_MATCH_NUM] = {
93 [PAT_MATCH_FOUND] = SMP_T_UINT,
94 [PAT_MATCH_BOOL] = SMP_T_UINT,
95 [PAT_MATCH_INT] = SMP_T_UINT,
96 [PAT_MATCH_IP] = SMP_T_ADDR,
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010097 [PAT_MATCH_BIN] = SMP_T_BIN,
98 [PAT_MATCH_LEN] = SMP_T_STR,
99 [PAT_MATCH_STR] = SMP_T_STR,
100 [PAT_MATCH_BEG] = SMP_T_STR,
101 [PAT_MATCH_SUB] = SMP_T_STR,
102 [PAT_MATCH_DIR] = SMP_T_STR,
103 [PAT_MATCH_DOM] = SMP_T_STR,
104 [PAT_MATCH_END] = SMP_T_STR,
105 [PAT_MATCH_REG] = SMP_T_STR,
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100106};
107
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +0100108/* this struct is used to return information */
109static struct pattern static_pattern;
110
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111/*
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100112 *
113 * The following functions are not exported and are used by internals process
114 * of pattern matching
115 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116 */
117
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100118/* Free data allocated by pat_parse_reg */
119static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100120{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100121 regex_free(ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100122}
123
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124/* Background: Fast way to find a zero byte in a word
125 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
126 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
127 *
128 * To look for 4 different byte values, xor the word with those bytes and
129 * then check for zero bytes:
130 *
131 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
132 * where <delimiter> is the 4 byte values to look for (as an uint)
133 * and <c> is the character that is being tested
134 */
135static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
136{
137 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
138 return (mask - 0x01010101) & ~mask & 0x80808080U;
139}
140
141static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
142{
143 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
144}
145
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100146
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100147/*
148 *
149 * These functions are exported and may be used by any other component.
150 *
151 * The following functions are used for parsing pattern matching
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100152 * input value. The <text> contain the string to be parsed. <pattern>
153 * must be a preallocated pattern. The pat_parse_* functions fill this
154 * structure with the parsed value. <usage> can be PAT_U_COMPILE or
155 * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
156 * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
157 * use "trash" or return pointers to the input strings. In both cases,
158 * the caller must use the value PAT_U_LOOKUP with caution. <err> is
159 * filled with an error message built with memprintf() function.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100160 *
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100161 * In succes case, the pat_parse_* function return 1. If the function
162 * fail, it returns 0 and <err> is filled.
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100163 *
164 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100165
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100166/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100167int pat_parse_nothing(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100168{
169 return 1;
170}
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100171
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100172/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100173int pat_parse_str(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100174{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100175 pattern->type = SMP_T_STR;
176 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100177 pattern->ptr.str = (char *)text;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100178 pattern->len = strlen(text);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100179 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100180}
181
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100182/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100183int pat_parse_bin(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100184{
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100185 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100186
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100187 pattern->type = SMP_T_BIN;
188 pattern->expect_type = SMP_T_BIN;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100189 trash = get_trash_chunk();
190 pattern->len = trash->size;
191 pattern->ptr.str = trash->str;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100192 return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100193}
194
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100195/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100196int pat_parse_reg(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197{
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +0100198 struct chunk *trash;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100199
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100200 trash = get_trash_chunk();
201 if (trash->size < sizeof(*pattern->ptr.reg)) {
202 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
203 (int)sizeof(*pattern->ptr.reg), trash->size);
204 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205 }
206
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100207 pattern->ptr.reg = (struct my_regex *)trash->str;
208 pattern->ptr.reg->regstr = (char *)text;
209 pattern->freeptrbuf = NULL;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100210
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100211 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100212 return 1;
213}
214
215/* Parse a range of positive integers delimited by either ':' or '-'. If only
216 * one integer is read, it is set as both min and max. An operator may be
217 * specified as the prefix, among this list of 5 :
218 *
219 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
220 *
221 * The default operator is "eq". It supports range matching. Ranges are
222 * rejected for other operators. The operator may be changed at any time.
223 * The operator is stored in the 'opaque' argument.
224 *
225 * If err is non-NULL, an error message will be returned there on errors and
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100226 * the caller will have to free it. The function returns zero on error, and
227 * non-zero on success.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100228 *
229 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100230int pat_parse_int(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100231{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100232 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100233
234 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100235 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100236
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100237 /* Empty string is not valid */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100238 if (!*text)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100239 goto not_valid_range;
240
241 /* Search ':' or '-' separator. */
242 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
243 ptr++;
244
245 /* If separator not found. */
246 if (!*ptr) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100247 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
248 memprintf(err, "'%s' is not a number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100249 return 0;
250 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100251 pattern->val.range.max = pattern->val.range.min;
252 pattern->val.range.min_set = 1;
253 pattern->val.range.max_set = 1;
254 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100255 }
256
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100257 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100258 if (ptr == text && *(ptr + 1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100259 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
260 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100261
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100262 pattern->val.range.min_set = 0;
263 pattern->val.range.max_set = 1;
264 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100265 }
266
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100267 /* If separator is the last character. */
268 if (*(ptr + 1) == '\0') {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100269 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100270 goto not_valid_range;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100271
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100272 pattern->val.range.min_set = 1;
273 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100274 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100275 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100276
277 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100278 if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100279 goto not_valid_range;
280
281 if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
282 goto not_valid_range;
283
284 if (pattern->val.range.min > pattern->val.range.max)
285 goto not_valid_range;
286
287 pattern->val.range.min_set = 1;
288 pattern->val.range.max_set = 1;
289 return 1;
290
291 not_valid_range:
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100292 memprintf(err, "'%s' is not a valid number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100293 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100294}
295
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100296int pat_parse_len(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100297{
298 int ret;
299
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100300 ret = pat_parse_int(text, pattern, err);
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100301 pattern->expect_type = SMP_T_STR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100302 return ret;
303}
304
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100305/* Parse a range of positive 2-component versions delimited by either ':' or
306 * '-'. The version consists in a major and a minor, both of which must be
307 * smaller than 65536, because internally they will be represented as a 32-bit
308 * integer.
309 * If only one version is read, it is set as both min and max. Just like for
310 * pure integers, an operator may be specified as the prefix, among this list
311 * of 5 :
312 *
313 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
314 *
315 * The default operator is "eq". It supports range matching. Ranges are
316 * rejected for other operators. The operator may be changed at any time.
317 * The operator is stored in the 'opaque' argument. This allows constructs
318 * such as the following one :
319 *
320 * acl obsolete_ssl ssl_req_proto lt 3
321 * acl unsupported_ssl ssl_req_proto gt 3.1
322 * acl valid_ssl ssl_req_proto 3.0-3.1
323 *
324 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100325int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100326{
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100327 const char *ptr = text;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100328
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100329 pattern->type = SMP_T_UINT;
330 pattern->expect_type = SMP_T_UINT;
331
332 /* Search ':' or '-' separator. */
333 while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
334 ptr++;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100335
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100336 /* If separator not found. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100337 if (*ptr == '\0' && ptr > text) {
338 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
339 memprintf(err, "'%s' is not a dotted number", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100340 return 0;
341 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100342 pattern->val.range.max = pattern->val.range.min;
343 pattern->val.range.min_set = 1;
344 pattern->val.range.max_set = 1;
345 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100346 }
347
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100348 /* If the separator is the first character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100349 if (ptr == text && *(ptr+1) != '\0') {
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100350 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100351 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100352 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100354 pattern->val.range.min_set = 0;
355 pattern->val.range.max_set = 1;
356 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100357 }
358
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100359 /* If separator is the last character. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100360 if (ptr == &text[strlen(text)-1]) {
361 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
362 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100363 return 0;
364 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100365 pattern->val.range.min_set = 1;
366 pattern->val.range.max_set = 0;
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100367 return 1;
368 }
369
370 /* Else, parse two numbers. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100371 if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
372 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100373 return 0;
374 }
375 if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100376 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100377 return 0;
378 }
379 if (pattern->val.range.min > pattern->val.range.max) {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100380 memprintf(err, "'%s' is not a valid dotted number range", text);
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100381 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100382 }
Thierry FOURNIER511e9472014-01-23 17:40:34 +0100383 pattern->val.range.min_set = 1;
384 pattern->val.range.max_set = 1;
385 return 1;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100386}
387
388/* Parse an IP address and an optional mask in the form addr[/mask].
389 * The addr may either be an IPv4 address or a hostname. The mask
390 * may either be a dotted mask or a number of bits. Returns 1 if OK,
391 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
392 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100393int pat_parse_ip(const char *text, struct pattern *pattern, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100394{
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100395 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100396 if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100397 pattern->type = SMP_T_IPV4;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398 return 1;
399 }
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100400 else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100401 /* no tree support right now */
402 pattern->type = SMP_T_IPV6;
403 return 1;
404 }
405 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100406 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100407 return 0;
408 }
409}
410
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100411/*
412 *
413 * These functions are exported and may be used by any other component.
414 *
415 * This fucntion just take a sample <smp> and check if this sample match
416 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
417 * PAT_NOMATCH.
418 *
419 */
420
421/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100422struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100423{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100424 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100425}
426
427
428/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100429struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100430{
431 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100432 struct ebmb_node *node;
433 char prev;
434 struct pattern_tree *elt;
435 struct pattern_list *lst;
436 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100437
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100438 /* convert input to string */
439 if (!sample_convert(smp, SMP_T_STR))
440 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100441
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100442 /* Lookup a string in the expression's pattern tree. */
443 if (!eb_is_empty(&expr->pattern_tree)) {
444 /* we may have to force a trailing zero on the test pattern */
445 prev = smp->data.str.str[smp->data.str.len];
446 if (prev)
447 smp->data.str.str[smp->data.str.len] = '\0';
448 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
449 if (prev)
450 smp->data.str.str[smp->data.str.len] = prev;
451
452 if (node) {
453 if (fill) {
454 elt = ebmb_entry(node, struct pattern_tree, node);
455 static_pattern.smp = elt->smp;
456 static_pattern.flags = PAT_F_TREE;
457 static_pattern.type = SMP_T_STR;
458 static_pattern.ptr.str = (char *)elt->node.key;
459 }
460 return &static_pattern;
461 }
462 }
463
464 /* look in the list */
465 list_for_each_entry(lst, &expr->patterns, list) {
466 pattern = &lst->pat;
467
468 if (pattern->len != smp->data.str.len)
469 continue;
470
471 icase = pattern->flags & PAT_F_IGNORE_CASE;
472 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
473 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
474 return pattern;
475 }
476
477 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100478}
479
480/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100481struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100482{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100483 struct pattern_list *lst;
484 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100485
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100486 /* Convert input to binary. */
487 if (!sample_convert(smp, SMP_T_BIN))
488 return NULL;
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 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
498 return pattern;
499 }
500
501 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100502}
503
504/* Executes a regex. It temporarily changes the data to add a trailing zero,
505 * and restores the previous character when leaving.
506 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100507struct pattern *pat_match_reg(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;
511
512 /* convert input to string */
513 if (!sample_convert(smp, SMP_T_STR))
514 return NULL;
515
516 /* look in the list */
517 list_for_each_entry(lst, &expr->patterns, list) {
518 pattern = &lst->pat;
519
520 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
521 return pattern;
522 }
523 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100524}
525
526/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100527struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100528{
529 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100530 struct pattern_list *lst;
531 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100532
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100533 /* convert input to string */
534 if (!sample_convert(smp, SMP_T_STR))
535 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100536
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100537 list_for_each_entry(lst, &expr->patterns, list) {
538 pattern = &lst->pat;
539
540 if (pattern->len > smp->data.str.len)
541 continue;
542
543 icase = pattern->flags & PAT_F_IGNORE_CASE;
544 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
545 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
546 continue;
547
548 return pattern;
549 }
550 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100551}
552
553/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100554struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100555{
556 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100557 struct pattern_list *lst;
558 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100559
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100560 /* convert input to string */
561 if (!sample_convert(smp, SMP_T_STR))
562 return NULL;
563
564 list_for_each_entry(lst, &expr->patterns, list) {
565 pattern = &lst->pat;
566
567 if (pattern->len > smp->data.str.len)
568 continue;
569
570 icase = pattern->flags & PAT_F_IGNORE_CASE;
571 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
572 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
573 continue;
574
575 return pattern;
576 }
577 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100578}
579
580/* Checks that the pattern is included inside the tested string.
581 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
582 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100583struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100584{
585 int icase;
586 char *end;
587 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100588 struct pattern_list *lst;
589 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100590
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100591 /* convert input to string */
592 if (!sample_convert(smp, SMP_T_STR))
593 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100594
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100595 list_for_each_entry(lst, &expr->patterns, list) {
596 pattern = &lst->pat;
597
598 if (pattern->len > smp->data.str.len)
599 continue;
600
601 end = smp->data.str.str + smp->data.str.len - pattern->len;
602 icase = pattern->flags & PAT_F_IGNORE_CASE;
603 if (icase) {
604 for (c = smp->data.str.str; c <= end; c++) {
605 if (tolower(*c) != tolower(*pattern->ptr.str))
606 continue;
607 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
608 return pattern;
609 }
610 } else {
611 for (c = smp->data.str.str; c <= end; c++) {
612 if (*c != *pattern->ptr.str)
613 continue;
614 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
615 return pattern;
616 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100617 }
618 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100619 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100620}
621
622/* This one is used by other real functions. It checks that the pattern is
623 * included inside the tested string, but enclosed between the specified
624 * delimiters or at the beginning or end of the string. The delimiters are
625 * provided as an unsigned int made by make_4delim() and match up to 4 different
626 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
627 */
628static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
629{
630 int may_match, icase;
631 char *c, *end;
632 char *ps;
633 int pl;
634
635 pl = pattern->len;
636 ps = pattern->ptr.str;
637
638 while (pl > 0 && is_delimiter(*ps, delimiters)) {
639 pl--;
640 ps++;
641 }
642
643 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
644 pl--;
645
646 if (pl > smp->data.str.len)
647 return PAT_NOMATCH;
648
649 may_match = 1;
650 icase = pattern->flags & PAT_F_IGNORE_CASE;
651 end = smp->data.str.str + smp->data.str.len - pl;
652 for (c = smp->data.str.str; c <= end; c++) {
653 if (is_delimiter(*c, delimiters)) {
654 may_match = 1;
655 continue;
656 }
657
658 if (!may_match)
659 continue;
660
661 if (icase) {
662 if ((tolower(*c) == tolower(*ps)) &&
663 (strncasecmp(ps, c, pl) == 0) &&
664 (c == end || is_delimiter(c[pl], delimiters)))
665 return PAT_MATCH;
666 } else {
667 if ((*c == *ps) &&
668 (strncmp(ps, c, pl) == 0) &&
669 (c == end || is_delimiter(c[pl], delimiters)))
670 return PAT_MATCH;
671 }
672 may_match = 0;
673 }
674 return PAT_NOMATCH;
675}
676
677/* Checks that the pattern is included inside the tested string, but enclosed
678 * between the delimiters '?' or '/' or at the beginning or end of the string.
679 * Delimiters at the beginning or end of the pattern are ignored.
680 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100681struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100682{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100683 struct pattern_list *lst;
684 struct pattern *pattern;
685
686 /* convert input to string */
687 if (!sample_convert(smp, SMP_T_STR))
688 return NULL;
689
690 list_for_each_entry(lst, &expr->patterns, list) {
691 pattern = &lst->pat;
692 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
693 return pattern;
694 }
695 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100696}
697
698/* Checks that the pattern is included inside the tested string, but enclosed
699 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
700 * the string. Delimiters at the beginning or end of the pattern are ignored.
701 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100702struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100703{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100704 struct pattern_list *lst;
705 struct pattern *pattern;
706
707 /* convert input to string */
708 if (!sample_convert(smp, SMP_T_STR))
709 return NULL;
710
711 list_for_each_entry(lst, &expr->patterns, list) {
712 pattern = &lst->pat;
713 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
714 return pattern;
715 }
716 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100717}
718
719/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100720struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100721{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100722 struct pattern_list *lst;
723 struct pattern *pattern;
724
725 /* convert input to integer */
726 if (!sample_convert(smp, SMP_T_UINT))
727 return NULL;
728
729 list_for_each_entry(lst, &expr->patterns, list) {
730 pattern = &lst->pat;
731 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
732 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
733 return pattern;
734 }
735 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100736}
737
738/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100739struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100740{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100741 struct pattern_list *lst;
742 struct pattern *pattern;
743
744 /* convert input to string */
745 if (!sample_convert(smp, SMP_T_STR))
746 return NULL;
747
748 list_for_each_entry(lst, &expr->patterns, list) {
749 pattern = &lst->pat;
750 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
751 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
752 return pattern;
753 }
754 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100755}
756
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100757struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100758{
759 unsigned int v4; /* in network byte order */
760 struct in6_addr *v6;
761 int bits, pos;
762 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100763 struct in_addr *s;
764 struct ebmb_node *node;
765 struct pattern_tree *elt;
766 struct pattern_list *lst;
767 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100768
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100769 /* convert input to addr */
770 if (!sample_convert(smp, SMP_T_ADDR))
771 return NULL;
772
773 /* Lookup an IPv4 address in the expression's pattern tree using the longest
774 * match method.
775 */
776 if (smp->type == SMP_T_IPV4) {
777 s = &smp->data.ipv4;
778 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
779 if (node) {
780 if (fill) {
781 elt = ebmb_entry(node, struct pattern_tree, node);
782 static_pattern.smp = elt->smp;
783 static_pattern.flags = PAT_F_TREE;
784 static_pattern.type = SMP_T_IPV4;
785 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
786 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
787 return NULL;
788 }
789 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100790 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100791 }
792
793 /* Lookup in the list */
794 list_for_each_entry(lst, &expr->patterns, list) {
795 pattern = &lst->pat;
796
797 if (pattern->type == SMP_T_IPV4) {
798 if (smp->type == SMP_T_IPV4) {
799 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100800 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100801 else if (smp->type == SMP_T_IPV6) {
802 /* v4 match on a V6 sample. We want to check at least for
803 * the following forms :
804 * - ::ffff:ip:v4 (ipv4 mapped)
805 * - ::0000:ip:v4 (old ipv4 mapped)
806 * - 2002:ip:v4:: (6to4)
807 */
808 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
809 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
810 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
811 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
812 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
813 }
814 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
815 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
816 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
817 }
818 else
819 continue;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100820 }
821 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100822 continue;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100823
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100824 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
825 return pattern;
826 else
827 continue;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100828 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100829 else if (pattern->type == SMP_T_IPV6) {
830 if (smp->type == SMP_T_IPV4) {
831 /* Convert the IPv4 sample address to IPv4 with the
832 * mapping method using the ::ffff: prefix.
833 */
834 memset(&tmp6, 0, 10);
835 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
836 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
837 v6 = &tmp6;
838 }
839 else if (smp->type == SMP_T_IPV6) {
840 v6 = &smp->data.ipv6;
841 }
842 else {
843 continue;
844 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100845
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100846 bits = pattern->val.ipv6.mask;
847 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
848 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
849 if (bits < 32)
850 v4 &= htonl((~0U) << (32-bits));
851 if (v4)
852 continue;
853 }
854 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100855 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100856 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100857 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100858}
859
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100860/* NB: does nothing if <pat> is NULL */
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100861void pattern_free(struct pattern_list *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100862{
863 if (!pat)
864 return;
865
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100866 if (pat->pat.ptr.ptr) {
867 if (pat->pat.freeptrbuf)
868 pat->pat.freeptrbuf(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100869
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100870 free(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100871 }
872
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100873 free(pat->pat.smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100874 free(pat);
875}
876
877void free_pattern_list(struct list *head)
878{
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100879 struct pattern_list *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100880 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100881 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100882}
883
884void free_pattern_tree(struct eb_root *root)
885{
886 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100887 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100888
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100889 node = eb_first(root);
890 while (node) {
891 next = eb_next(node);
892 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100893 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100894 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100895 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100896 node = next;
897 }
898}
899
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100900void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100901{
902 free_pattern_list(&expr->patterns);
903 free_pattern_tree(&expr->pattern_tree);
904 LIST_INIT(&expr->patterns);
905}
906
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100907void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100908{
909 LIST_INIT(&expr->patterns);
910 expr->pattern_tree = EB_ROOT_UNIQUE;
911}
912
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100913/*
914 *
915 * The following functions are used for the pattern indexation
916 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100917 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100918
919int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100920{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100921 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100922
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100923 /* allocate pattern */
924 patl = calloc(1, sizeof(*patl));
925 if (!patl) {
926 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100927 return 0;
928 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100929
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100930 /* duplicate pattern */
931 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100932
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100933 /* chain pattern in the expression */
934 LIST_ADDQ(&expr->patterns, &patl->list);
935
936 /* that's ok */
937 return 1;
938}
939
940int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
941{
942 struct pattern_list *patl;
943
944 /* allocate pattern */
945 patl = calloc(1, sizeof(*patl));
946 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100947 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100948
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100949 /* duplicate pattern */
950 memcpy(&patl->pat, pat, sizeof(*pat));
951 patl->pat.ptr.ptr = malloc(patl->pat.len);
952 if (!patl->pat.ptr.ptr) {
953 free(patl);
954 memprintf(err, "out of memory while indexing pattern");
955 return 0;
956 }
957 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100958
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100959 /* chain pattern in the expression */
960 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100961
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100962 /* that's ok */
963 return 1;
964}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100965
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100966int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
967{
968 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100969
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100970 /* allocate pattern */
971 patl = calloc(1, sizeof(*patl));
972 if (!patl) {
973 memprintf(err, "out of memory while indexing pattern");
974 return 0;
975 }
976
977 /* duplicate pattern */
978 memcpy(&patl->pat, pat, sizeof(*pat));
979 patl->pat.ptr.str = malloc(patl->pat.len + 1);
980 if (!patl->pat.ptr.str) {
981 free(patl);
982 memprintf(err, "out of memory while indexing pattern");
983 return 0;
984 }
985 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
986 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100987
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100988 /* chain pattern in the expression */
989 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100990
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100991 /* that's ok */
992 return 1;
993}
994
995int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
996{
997 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100998
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100999 /* allocate pattern */
1000 patl = calloc(1, sizeof(*patl));
1001 if (!patl) {
1002 memprintf(err, "out of memory while indexing pattern");
1003 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001004 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001005
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001006 /* duplicate pattern */
1007 memcpy(&patl->pat, pat, sizeof(*pat));
1008
1009 /* allocate regex */
1010 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1011 if (!patl->pat.ptr.reg) {
1012 free(patl);
1013 memprintf(err, "out of memory while indexing pattern");
1014 return 0;
1015 }
1016
1017 /* compile regex */
1018 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1019 free(patl);
1020 free(patl->pat.ptr.reg);
1021 return 0;
1022 }
1023
1024 /* free pattern method */
1025 patl->pat.freeptrbuf = &pat_free_reg;
1026
1027 /* chain pattern in the expression */
1028 LIST_ADDQ(&expr->patterns, &patl->list);
1029
1030 /* that's ok */
1031 return 1;
1032}
1033
1034int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1035{
1036 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001037 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001038
1039 /* Only IPv4 can be indexed */
1040 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001041 /* in IPv4 case, check if the mask is contiguous so that we can
1042 * insert the network into the tree. A continuous mask has only
1043 * ones on the left. This means that this mask + its lower bit
1044 * added once again is null.
1045 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001046 mask = ntohl(pat->val.ipv4.mask.s_addr);
1047 if (mask + (mask & -mask) == 0) {
1048 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001049
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001050 /* node memory allocation */
1051 node = calloc(1, sizeof(*node) + 4);
1052 if (!node) {
1053 memprintf(err, "out of memory while loading pattern");
1054 return 0;
1055 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001056
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001057 /* copy the pointer to sample associated to this node */
1058 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001059
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001060 /* FIXME: insert <addr>/<mask> into the tree here */
1061 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1062 node->node.node.pfx = mask;
1063 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1064 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001065
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001066 /* that's ok */
1067 return 1;
1068 }
1069 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001070
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001071 /* If the value cannot be indexed, just add it to the list */
1072 return pat_idx_list_val(expr, pat, err);
1073}
1074
1075int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1076{
1077 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001078 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001079
1080 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001081 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001082 memprintf(err, "internal error: string expected, but the type is '%s'",
1083 smp_to_type[pat->type]);
1084 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001085 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001086
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001087 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1088 if (pat->flags & PAT_F_IGNORE_CASE)
1089 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001090
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001091 /* Process the key len */
1092 len = strlen(pat->ptr.str) + 1;
1093
1094 /* node memory allocation */
1095 node = calloc(1, sizeof(*node) + len);
1096 if (!node) {
1097 memprintf(err, "out of memory while loading pattern");
1098 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001099 }
1100
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001101 /* copy the pointer to sample associated to this node */
1102 node->smp = pat->smp;
1103
1104 /* copy the string */
1105 memcpy(node->node.key, pat->ptr.str, len);
1106
1107 /* index the new node */
1108 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1109 free(node); /* was a duplicate */
1110
1111 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001112 return 1;
1113}
1114
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001115/* return 1 if the process is ok
1116 * return -1 if the parser fail. The err message is filled.
1117 * return -2 if out of memory
1118 */
1119int pattern_register(struct pattern_expr *expr, const char *arg,
1120 struct sample_storage *smp,
1121 int patflags, char **err)
1122{
1123 int ret;
1124 struct pattern pattern;
1125
1126 /* initialise pattern */
1127 memset(&pattern, 0, sizeof(pattern));
1128 pattern.flags = patflags;
1129 pattern.smp = smp;
1130
1131 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001132 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001133 if (!ret)
1134 return 0;
1135
1136 /* index pattern */
1137 if (!expr->index(expr, &pattern, err))
1138 return 0;
1139
1140 return 1;
1141}
1142
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001143/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1144 * be returned there on errors and the caller will have to free it.
1145 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001146int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001147 const char *filename, int patflags,
1148 char **err)
1149{
1150 FILE *file;
1151 char *c;
1152 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001153 int ret = 0;
1154 int line = 0;
1155 int code;
1156
1157 file = fopen(filename, "r");
1158 if (!file) {
1159 memprintf(err, "failed to open pattern file <%s>", filename);
1160 return 0;
1161 }
1162
1163 /* now parse all patterns. The file may contain only one pattern per
1164 * line. If the line contains spaces, they will be part of the pattern.
1165 * The pattern stops at the first CR, LF or EOF encountered.
1166 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001167 while (fgets(trash.str, trash.size, file) != NULL) {
1168 line++;
1169 c = trash.str;
1170
1171 /* ignore lines beginning with a dash */
1172 if (*c == '#')
1173 continue;
1174
1175 /* strip leading spaces and tabs */
1176 while (*c == ' ' || *c == '\t')
1177 c++;
1178
1179
1180 arg = c;
1181 while (*c && *c != '\n' && *c != '\r')
1182 c++;
1183 *c = 0;
1184
1185 /* empty lines are ignored too */
1186 if (c == arg)
1187 continue;
1188
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001189 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001190 if (code == -2) {
1191 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1192 goto out_close;
1193 }
1194 else if (code < 0) {
1195 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001196 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001197 }
1198 }
1199
1200 ret = 1; /* success */
1201
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001202 out_close:
1203 fclose(file);
1204 return ret;
1205}
1206
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001207/* This function executes a pattern match on a sample. It applies pattern <expr>
1208 * to sample <smp>. The function returns NULL if the sample dont match. It returns
1209 * non-null if the sample match. If <fill> is true and the sample match, the
1210 * function returns the matched pattern. In many cases, this pattern can be a
1211 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001212 */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001213struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001214{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001215 if (!expr->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001216 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001217 static_pattern.smp = NULL;
1218 static_pattern.flags = 0;
1219 static_pattern.type = SMP_T_UINT;
1220 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001221 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001222 return &static_pattern;
1223 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001224 return expr->match(smp, expr, fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001225}
1226
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001227/* This function browse the pattern expr <expr> to lookup the key <key>. On
1228 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1229 * or <idx_elt> with the respectively matched pointers, and the other one with
1230 * NULL. Pointers are not set if they're passed as NULL.
1231 */
1232int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001233 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001234{
1235 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001236 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001237 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001238 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001239 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001240
1241 /* no real pattern */
1242 if (!expr->match || expr->match == pat_match_nothing)
1243 return 0;
1244
1245 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001246 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001247 return 0;
1248
1249 pat = NULL;
1250 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001251
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001252 /* Try to look up the tree first. IPv6 is not indexed */
1253 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001254 /* Check the pattern type */
1255 if (pattern.type != SMP_T_STR &&
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001256 pattern.type != SMP_T_IPV4) {
1257 memprintf(err, "Unexpected pattern type.");
1258 return 0;
1259 }
1260
1261 /* Convert mask. If the mask is not contiguous, ignore the lookup
1262 * in the tree, and browse the list.
1263 */
1264 if (expr->match == pat_match_ip) {
1265 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1266 if (mask + (mask & -mask) != 0)
1267 goto browse_list;
1268 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1269 }
1270
1271 /* browse each node of the tree, and check string */
1272 if (expr->match == pat_match_str) {
1273 for (node = ebmb_first(&expr->pattern_tree);
1274 node;
1275 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001276 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001277 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1278 goto found;
1279 }
1280 }
1281 else if (expr->match == pat_match_ip) {
1282 for (node = ebmb_first(&expr->pattern_tree);
1283 node;
1284 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001285 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001286 if (elt->node.node.pfx == mask &&
1287 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1288 goto found;
1289 }
1290 }
1291 }
1292
1293browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001294 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001295 if (expr->parse == pat_parse_int ||
1296 expr->parse == pat_parse_len) {
1297 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001298 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001299 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001300 if (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001301 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001302 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001303 continue;
1304 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001305 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001306 continue;
1307 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001308 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001309 continue;
1310 goto found;
1311 }
1312 }
1313 else if (expr->parse == pat_parse_ip) {
1314 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001315 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001316 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001317 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001318 continue;
1319 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001320 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001321 continue;
1322 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001323 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001324 continue;
1325 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001326 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001327 continue;
1328 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001329 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001330 continue;
1331 goto found;
1332 }
1333 }
1334 else if (expr->parse == pat_parse_str) {
1335 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001336 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001337 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001338 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001339 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001340 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1341 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001342 continue;
1343 }
1344 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001345 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001346 continue;
1347 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001348 goto found;
1349 }
1350 }
1351 else if (expr->parse == pat_parse_bin) {
1352 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001353 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001354 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001355 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001356 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001357 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001358 continue;
1359 goto found;
1360 }
1361 }
1362 else if (expr->parse == pat_parse_reg) {
1363 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001364 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001365 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001366 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1367 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001368 continue;
1369 }
1370 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001371 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001372 continue;
1373 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001374 goto found;
1375 }
1376 }
1377
1378 /* if we get there, we didn't find the pattern */
1379 return 0;
1380found:
1381 if (idx_elt)
1382 *idx_elt = elt;
1383
1384 if (pat_elt)
1385 *pat_elt = pat;
1386
1387 return 1;
1388}