blob: 3f2560729397cceb1d4efb441923a9e7280377c0 [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>
23
24#include <ebsttree.h>
25
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010026char *pat_match_names[PAT_MATCH_NUM] = {
27 [PAT_MATCH_FOUND] = "found",
28 [PAT_MATCH_BOOL] = "bool",
29 [PAT_MATCH_INT] = "int",
30 [PAT_MATCH_IP] = "ip",
31 [PAT_MATCH_BIN] = "bin",
32 [PAT_MATCH_LEN] = "len",
33 [PAT_MATCH_STR] = "str",
34 [PAT_MATCH_BEG] = "beg",
35 [PAT_MATCH_SUB] = "sub",
36 [PAT_MATCH_DIR] = "dir",
37 [PAT_MATCH_DOM] = "dom",
38 [PAT_MATCH_END] = "end",
39 [PAT_MATCH_REG] = "reg",
Thierry FOURNIERed66c292013-11-28 11:05:19 +010040};
41
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010042int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, struct sample_storage *, int *, char **) = {
43 [PAT_MATCH_FOUND] = pat_parse_nothing,
44 [PAT_MATCH_BOOL] = pat_parse_nothing,
45 [PAT_MATCH_INT] = pat_parse_int,
46 [PAT_MATCH_IP] = pat_parse_ip,
47 [PAT_MATCH_BIN] = pat_parse_bin,
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010048 [PAT_MATCH_LEN] = pat_parse_len,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010049 [PAT_MATCH_STR] = pat_parse_str,
50 [PAT_MATCH_BEG] = pat_parse_str,
51 [PAT_MATCH_SUB] = pat_parse_str,
52 [PAT_MATCH_DIR] = pat_parse_str,
53 [PAT_MATCH_DOM] = pat_parse_str,
54 [PAT_MATCH_END] = pat_parse_str,
55 [PAT_MATCH_REG] = pat_parse_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010056};
57
Willy Tarreau0cba6072013-11-28 22:21:02 +010058enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010059 [PAT_MATCH_FOUND] = NULL,
60 [PAT_MATCH_BOOL] = pat_match_nothing,
61 [PAT_MATCH_INT] = pat_match_int,
62 [PAT_MATCH_IP] = pat_match_ip,
63 [PAT_MATCH_BIN] = pat_match_bin,
64 [PAT_MATCH_LEN] = pat_match_len,
65 [PAT_MATCH_STR] = pat_match_str,
66 [PAT_MATCH_BEG] = pat_match_beg,
67 [PAT_MATCH_SUB] = pat_match_sub,
68 [PAT_MATCH_DIR] = pat_match_dir,
69 [PAT_MATCH_DOM] = pat_match_dom,
70 [PAT_MATCH_END] = pat_match_end,
71 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010072};
73
74/*
75 * These functions are exported and may be used by any other component.
76 */
77
78/* ignore the current line */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010079int pat_parse_nothing(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +010080{
81 return 1;
82}
83
84/* always return false */
Willy Tarreau0cba6072013-11-28 22:21:02 +010085enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +010086{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010087 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +010088}
89
90
91/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +010092enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +010093{
94 int icase;
95
96 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010097 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +010098
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010099 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100100 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
101 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100102 return PAT_MATCH;
103 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100104}
105
106/* NB: For two binaries buf to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100107enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100108{
109 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100110 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111
112 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100113 return PAT_MATCH;
114 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100115}
116
117/* Lookup a string in the expression's pattern tree. The node is returned if it
118 * exists, otherwise NULL.
119 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100120static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100121{
122 /* data are stored in a tree */
123 struct ebmb_node *node;
124 char prev;
125
126 /* we may have to force a trailing zero on the test pattern */
127 prev = smp->data.str.str[smp->data.str.len];
128 if (prev)
129 smp->data.str.str[smp->data.str.len] = '\0';
130 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
131 if (prev)
132 smp->data.str.str[smp->data.str.len] = prev;
133 return node;
134}
135
136/* Executes a regex. It temporarily changes the data to add a trailing zero,
137 * and restores the previous character when leaving.
138 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100139enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100140{
141 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100142 return PAT_MATCH;
143 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144}
145
146/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100147enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100148{
149 int icase;
150
151 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100152 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100153
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100154 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100155 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
156 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100157 return PAT_NOMATCH;
158 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159}
160
161/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100162enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100163{
164 int icase;
165
166 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100167 return PAT_NOMATCH;
168 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100169 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
170 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100171 return PAT_NOMATCH;
172 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173}
174
175/* Checks that the pattern is included inside the tested string.
176 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
177 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100178enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100179{
180 int icase;
181 char *end;
182 char *c;
183
184 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100185 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100186
187 end = smp->data.str.str + smp->data.str.len - pattern->len;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100188 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100189 if (icase) {
190 for (c = smp->data.str.str; c <= end; c++) {
191 if (tolower(*c) != tolower(*pattern->ptr.str))
192 continue;
193 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100194 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100195 }
196 } else {
197 for (c = smp->data.str.str; c <= end; c++) {
198 if (*c != *pattern->ptr.str)
199 continue;
200 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100201 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100202 }
203 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100204 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100205}
206
207/* Background: Fast way to find a zero byte in a word
208 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
209 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
210 *
211 * To look for 4 different byte values, xor the word with those bytes and
212 * then check for zero bytes:
213 *
214 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
215 * where <delimiter> is the 4 byte values to look for (as an uint)
216 * and <c> is the character that is being tested
217 */
218static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
219{
220 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
221 return (mask - 0x01010101) & ~mask & 0x80808080U;
222}
223
224static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
225{
226 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
227}
228
229/* This one is used by other real functions. It checks that the pattern is
230 * included inside the tested string, but enclosed between the specified
231 * delimiters or at the beginning or end of the string. The delimiters are
232 * provided as an unsigned int made by make_4delim() and match up to 4 different
233 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
234 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100235static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100236{
237 int may_match, icase;
238 char *c, *end;
239 char *ps;
240 int pl;
241
242 pl = pattern->len;
243 ps = pattern->ptr.str;
244
245 while (pl > 0 && is_delimiter(*ps, delimiters)) {
246 pl--;
247 ps++;
248 }
249
250 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
251 pl--;
252
253 if (pl > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100254 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100255
256 may_match = 1;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100257 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100258 end = smp->data.str.str + smp->data.str.len - pl;
259 for (c = smp->data.str.str; c <= end; c++) {
260 if (is_delimiter(*c, delimiters)) {
261 may_match = 1;
262 continue;
263 }
264
265 if (!may_match)
266 continue;
267
268 if (icase) {
269 if ((tolower(*c) == tolower(*ps)) &&
270 (strncasecmp(ps, c, pl) == 0) &&
271 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100272 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273 } else {
274 if ((*c == *ps) &&
275 (strncmp(ps, c, pl) == 0) &&
276 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100277 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100278 }
279 may_match = 0;
280 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100281 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100282}
283
284/* Checks that the pattern is included inside the tested string, but enclosed
285 * between the delimiters '?' or '/' or at the beginning or end of the string.
286 * Delimiters at the beginning or end of the pattern are ignored.
287 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100288enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100289{
290 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
291}
292
293/* Checks that the pattern is included inside the tested string, but enclosed
294 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
295 * the string. Delimiters at the beginning or end of the pattern are ignored.
296 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100297enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100298{
299 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
300}
301
302/* Checks that the integer in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100303enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100304{
305 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
306 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100307 return PAT_MATCH;
308 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100309}
310
311/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100312enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100313{
314 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
315 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100316 return PAT_MATCH;
317 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100318}
319
Willy Tarreau0cba6072013-11-28 22:21:02 +0100320enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100321{
322 unsigned int v4; /* in network byte order */
323 struct in6_addr *v6;
324 int bits, pos;
325 struct in6_addr tmp6;
326
327 if (pattern->type == SMP_T_IPV4) {
328 if (smp->type == SMP_T_IPV4) {
329 v4 = smp->data.ipv4.s_addr;
330 }
331 else if (smp->type == SMP_T_IPV6) {
332 /* v4 match on a V6 sample. We want to check at least for
333 * the following forms :
334 * - ::ffff:ip:v4 (ipv4 mapped)
335 * - ::0000:ip:v4 (old ipv4 mapped)
336 * - 2002:ip:v4:: (6to4)
337 */
338 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
339 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
340 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
341 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
342 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
343 }
344 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
345 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
346 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
347 }
348 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100349 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100350 }
351 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100352 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100353
354 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100355 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100356 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100357 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100358 }
359 else if (pattern->type == SMP_T_IPV6) {
360 if (smp->type == SMP_T_IPV4) {
361 /* Convert the IPv4 sample address to IPv4 with the
362 * mapping method using the ::ffff: prefix.
363 */
364 memset(&tmp6, 0, 10);
365 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
366 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
367 v6 = &tmp6;
368 }
369 else if (smp->type == SMP_T_IPV6) {
370 v6 = &smp->data.ipv6;
371 }
372 else {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100373 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100374 }
375
376 bits = pattern->val.ipv6.mask;
377 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
378 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
379 if (bits < 32)
380 v4 &= htonl((~0U) << (32-bits));
381 if (v4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100382 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100383 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100384 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100385 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100386 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100387}
388
389/* Lookup an IPv4 address in the expression's pattern tree using the longest
390 * match method. The node is returned if it exists, otherwise NULL.
391 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100392static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100393{
394 struct in_addr *s;
395
396 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100397 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100398
399 s = &smp->data.ipv4;
400 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
401}
402
403/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100404int pat_parse_str(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100405{
406 int len;
407
408 len = strlen(*text);
409 pattern->type = SMP_T_CSTR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100410 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100412 if (pattern->flags & PAT_F_TREE_OK) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100413 /* we're allowed to put the data in a tree whose root is pointed
414 * to by val.tree.
415 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100416 struct pat_idx_elt *node;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100417
418 node = calloc(1, sizeof(*node) + len + 1);
419 if (!node) {
420 memprintf(err, "out of memory while loading string pattern");
421 return 0;
422 }
423 node->smp = smp;
424 memcpy(node->node.key, *text, len + 1);
425 if (ebst_insert(pattern->val.tree, &node->node) != &node->node)
426 free(node); /* was a duplicate */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100427 pattern->flags |= PAT_F_TREE; /* this pattern now contains a tree */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100428 return 1;
429 }
430
431 pattern->ptr.str = strdup(*text);
432 pattern->smp = smp;
433 if (!pattern->ptr.str) {
434 memprintf(err, "out of memory while loading string pattern");
435 return 0;
436 }
437 pattern->len = len;
438 return 1;
439}
440
441/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100442int pat_parse_bin(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100443{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100444 pattern->type = SMP_T_CBIN;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100445 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100446 pattern->smp = smp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100447
Willy Tarreau126d4062013-12-03 17:50:47 +0100448 return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100449}
450
451/* Parse and concatenate all further strings into one. */
452int
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100453pat_parse_strcat(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100454{
455
456 int len = 0, i;
457 char *s;
458
459 for (i = 0; *text[i]; i++)
460 len += strlen(text[i])+1;
461
462 pattern->type = SMP_T_CSTR;
463 pattern->ptr.str = s = calloc(1, len);
464 pattern->smp = smp;
465 if (!pattern->ptr.str) {
466 memprintf(err, "out of memory while loading pattern");
467 return 0;
468 }
469
470 for (i = 0; *text[i]; i++)
471 s += sprintf(s, i?" %s":"%s", text[i]);
472
473 pattern->len = len;
474
475 return i;
476}
477
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100478/* Free data allocated by pat_parse_reg */
479static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100480{
481 regex_free(ptr);
482}
483
484/* Parse a regex. It is allocated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100485int pat_parse_reg(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100486{
487 regex *preg;
488
489 preg = calloc(1, sizeof(*preg));
490
491 if (!preg) {
492 memprintf(err, "out of memory while loading pattern");
493 return 0;
494 }
495
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100496 if (!regex_comp(*text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100497 free(preg);
498 return 0;
499 }
500
501 pattern->ptr.reg = preg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100502 pattern->freeptrbuf = &pat_free_reg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100503 pattern->smp = smp;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100504 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100505 return 1;
506}
507
508/* Parse a range of positive integers delimited by either ':' or '-'. If only
509 * one integer is read, it is set as both min and max. An operator may be
510 * specified as the prefix, among this list of 5 :
511 *
512 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
513 *
514 * The default operator is "eq". It supports range matching. Ranges are
515 * rejected for other operators. The operator may be changed at any time.
516 * The operator is stored in the 'opaque' argument.
517 *
518 * If err is non-NULL, an error message will be returned there on errors and
519 * the caller will have to free it.
520 *
521 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100522int pat_parse_int(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100523{
524 signed long long i;
525 unsigned int j, last, skip = 0;
526 const char *ptr = *text;
527
528 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100529 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100530 pattern->smp = smp;
531 while (!isdigit((unsigned char)*ptr)) {
532 switch (get_std_op(ptr)) {
533 case STD_OP_EQ: *opaque = 0; break;
534 case STD_OP_GT: *opaque = 1; break;
535 case STD_OP_GE: *opaque = 2; break;
536 case STD_OP_LT: *opaque = 3; break;
537 case STD_OP_LE: *opaque = 4; break;
538 default:
539 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
540 return 0;
541 }
542
543 skip++;
544 ptr = text[skip];
545 }
546
547 last = i = 0;
548 while (1) {
549 j = *ptr++;
550 if ((j == '-' || j == ':') && !last) {
551 last++;
552 pattern->val.range.min = i;
553 i = 0;
554 continue;
555 }
556 j -= '0';
557 if (j > 9)
558 // also catches the terminating zero
559 break;
560 i *= 10;
561 i += j;
562 }
563
564 if (last && *opaque >= 1 && *opaque <= 4) {
565 /* having a range with a min or a max is absurd */
566 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
567 return 0;
568 }
569
570 if (!last)
571 pattern->val.range.min = i;
572 pattern->val.range.max = i;
573
574 switch (*opaque) {
575 case 0: /* eq */
576 pattern->val.range.min_set = 1;
577 pattern->val.range.max_set = 1;
578 break;
579 case 1: /* gt */
580 pattern->val.range.min++; /* gt = ge + 1 */
581 case 2: /* ge */
582 pattern->val.range.min_set = 1;
583 pattern->val.range.max_set = 0;
584 break;
585 case 3: /* lt */
586 pattern->val.range.max--; /* lt = le - 1 */
587 case 4: /* le */
588 pattern->val.range.min_set = 0;
589 pattern->val.range.max_set = 1;
590 break;
591 }
592 return skip + 1;
593}
594
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100595int pat_parse_len(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
596{
597 int ret;
598
599 ret = pat_parse_int(text, pattern, smp, opaque, err);
600 pattern->expect_type = SMP_T_CSTR;
601 return ret;
602}
603
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100604/* Parse a range of positive 2-component versions delimited by either ':' or
605 * '-'. The version consists in a major and a minor, both of which must be
606 * smaller than 65536, because internally they will be represented as a 32-bit
607 * integer.
608 * If only one version is read, it is set as both min and max. Just like for
609 * pure integers, an operator may be specified as the prefix, among this list
610 * of 5 :
611 *
612 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
613 *
614 * The default operator is "eq". It supports range matching. Ranges are
615 * rejected for other operators. The operator may be changed at any time.
616 * The operator is stored in the 'opaque' argument. This allows constructs
617 * such as the following one :
618 *
619 * acl obsolete_ssl ssl_req_proto lt 3
620 * acl unsupported_ssl ssl_req_proto gt 3.1
621 * acl valid_ssl ssl_req_proto 3.0-3.1
622 *
623 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100624int pat_parse_dotted_ver(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100625{
626 signed long long i;
627 unsigned int j, last, skip = 0;
628 const char *ptr = *text;
629
630
631 while (!isdigit((unsigned char)*ptr)) {
632 switch (get_std_op(ptr)) {
633 case STD_OP_EQ: *opaque = 0; break;
634 case STD_OP_GT: *opaque = 1; break;
635 case STD_OP_GE: *opaque = 2; break;
636 case STD_OP_LT: *opaque = 3; break;
637 case STD_OP_LE: *opaque = 4; break;
638 default:
639 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
640 return 0;
641 }
642
643 skip++;
644 ptr = text[skip];
645 }
646
647 last = i = 0;
648 while (1) {
649 j = *ptr++;
650 if (j == '.') {
651 /* minor part */
652 if (i >= 65536)
653 return 0;
654 i <<= 16;
655 continue;
656 }
657 if ((j == '-' || j == ':') && !last) {
658 last++;
659 if (i < 65536)
660 i <<= 16;
661 pattern->val.range.min = i;
662 i = 0;
663 continue;
664 }
665 j -= '0';
666 if (j > 9)
667 // also catches the terminating zero
668 break;
669 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
670 i += j;
671 }
672
673 /* if we only got a major version, let's shift it now */
674 if (i < 65536)
675 i <<= 16;
676
677 if (last && *opaque >= 1 && *opaque <= 4) {
678 /* having a range with a min or a max is absurd */
679 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
680 return 0;
681 }
682
683 pattern->smp = smp;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100684 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100685
686 if (!last)
687 pattern->val.range.min = i;
688 pattern->val.range.max = i;
689
690 switch (*opaque) {
691 case 0: /* eq */
692 pattern->val.range.min_set = 1;
693 pattern->val.range.max_set = 1;
694 break;
695 case 1: /* gt */
696 pattern->val.range.min++; /* gt = ge + 1 */
697 case 2: /* ge */
698 pattern->val.range.min_set = 1;
699 pattern->val.range.max_set = 0;
700 break;
701 case 3: /* lt */
702 pattern->val.range.max--; /* lt = le - 1 */
703 case 4: /* le */
704 pattern->val.range.min_set = 0;
705 pattern->val.range.max_set = 1;
706 break;
707 }
708 return skip + 1;
709}
710
711/* Parse an IP address and an optional mask in the form addr[/mask].
712 * The addr may either be an IPv4 address or a hostname. The mask
713 * may either be a dotted mask or a number of bits. Returns 1 if OK,
714 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
715 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100716int pat_parse_ip(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100717{
718 struct eb_root *tree = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100719 if (pattern->flags & PAT_F_TREE_OK)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100720 tree = pattern->val.tree;
721
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100722 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100723 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
724 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100725 struct pat_idx_elt *node;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100726 /* check if the mask is contiguous so that we can insert the
727 * network into the tree. A continuous mask has only ones on
728 * the left. This means that this mask + its lower bit added
729 * once again is null.
730 */
731 pattern->type = SMP_T_IPV4;
732 if (mask + (mask & -mask) == 0 && tree) {
733 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
734 /* FIXME: insert <addr>/<mask> into the tree here */
735 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
736 if (!node) {
737 memprintf(err, "out of memory while loading IPv4 pattern");
738 return 0;
739 }
740 node->smp = smp;
741 memcpy(node->node.key, &pattern->val.ipv4.addr, 4); /* network byte order */
742 node->node.node.pfx = mask;
743 if (ebmb_insert_prefix(tree, &node->node, 4) != &node->node)
744 free(node); /* was a duplicate */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100745 pattern->flags |= PAT_F_TREE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100746 return 1;
747 }
748 return 1;
749 }
750 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
751 /* no tree support right now */
752 pattern->type = SMP_T_IPV6;
753 return 1;
754 }
755 else {
756 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
757 return 0;
758 }
759}
760
761/* NB: does nothing if <pat> is NULL */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100762void pattern_free(struct pattern *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100763{
764 if (!pat)
765 return;
766
767 if (pat->ptr.ptr) {
768 if (pat->freeptrbuf)
769 pat->freeptrbuf(pat->ptr.ptr);
770
771 free(pat->ptr.ptr);
772 }
773
774 free(pat);
775}
776
777void free_pattern_list(struct list *head)
778{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100779 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100780 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100781 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100782}
783
784void free_pattern_tree(struct eb_root *root)
785{
786 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100787 struct pat_idx_elt *elt;
788
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100789 node = eb_first(root);
790 while (node) {
791 next = eb_next(node);
792 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100793 elt = container_of(node, struct pat_idx_elt, node);
794 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100795 node = next;
796 }
797}
798
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100799void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100800{
801 free_pattern_list(&expr->patterns);
802 free_pattern_tree(&expr->pattern_tree);
803 LIST_INIT(&expr->patterns);
804}
805
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100806void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100807{
808 LIST_INIT(&expr->patterns);
809 expr->pattern_tree = EB_ROOT_UNIQUE;
810}
811
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100812/* return 1 if the process is ok
813 * return -1 if the parser fail. The err message is filled.
814 * return -2 if out of memory
815 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100816int pattern_register(struct pattern_expr *expr, char *text,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100817 struct sample_storage *smp,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100818 struct pattern **pattern,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100819 int patflags, char **err)
820{
821 const char *args[2];
822 int opaque = 0;
823
824 args[0] = text;
825 args[1] = "";
826
827 /* we keep the previous pattern along iterations as long as it's not used */
828 if (!*pattern)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100829 *pattern = (struct pattern *)malloc(sizeof(**pattern));
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100830 if (!*pattern)
831 return -1;
832
833 memset(*pattern, 0, sizeof(**pattern));
834 (*pattern)->flags = patflags;
835
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100836 if (!((*pattern)->flags & PAT_F_IGNORE_CASE) &&
837 (expr->match == pat_match_str || expr->match == pat_match_ip)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100838 /* we pre-set the data pointer to the tree's head so that functions
839 * which are able to insert in a tree know where to do that.
840 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100841 (*pattern)->flags |= PAT_F_TREE_OK;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100842 (*pattern)->val.tree = &expr->pattern_tree;
843 }
844
845 (*pattern)->type = SMP_TYPES; /* unspecified type by default */
846 if (!expr->parse(args, *pattern, smp, &opaque, err))
847 return -1;
848
849 /* if the parser did not feed the tree, let's chain the pattern to the list */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100850 if (!((*pattern)->flags & PAT_F_TREE)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100851 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
852 *pattern = NULL; /* get a new one */
853 }
854
855 return 1;
856}
857
858/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
859 * be returned there on errors and the caller will have to free it.
860 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100861int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100862 const char *filename, int patflags,
863 char **err)
864{
865 FILE *file;
866 char *c;
867 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100868 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100869 int ret = 0;
870 int line = 0;
871 int code;
872
873 file = fopen(filename, "r");
874 if (!file) {
875 memprintf(err, "failed to open pattern file <%s>", filename);
876 return 0;
877 }
878
879 /* now parse all patterns. The file may contain only one pattern per
880 * line. If the line contains spaces, they will be part of the pattern.
881 * The pattern stops at the first CR, LF or EOF encountered.
882 */
883 pattern = NULL;
884 while (fgets(trash.str, trash.size, file) != NULL) {
885 line++;
886 c = trash.str;
887
888 /* ignore lines beginning with a dash */
889 if (*c == '#')
890 continue;
891
892 /* strip leading spaces and tabs */
893 while (*c == ' ' || *c == '\t')
894 c++;
895
896
897 arg = c;
898 while (*c && *c != '\n' && *c != '\r')
899 c++;
900 *c = 0;
901
902 /* empty lines are ignored too */
903 if (c == arg)
904 continue;
905
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100906 code = pattern_register(expr, arg, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100907 if (code == -2) {
908 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
909 goto out_close;
910 }
911 else if (code < 0) {
912 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
913 goto out_free_pattern;
914 }
915 }
916
917 ret = 1; /* success */
918
919 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100920 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100921 out_close:
922 fclose(file);
923 return ret;
924}
925
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100926/* This function matches a sample <smp> against a set of patterns presented in
927 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
928 * with the pointer associated with the matching pattern. This function returns
929 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100930 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100931enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
932 struct sample_storage **sample)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100933{
Willy Tarreau0cba6072013-11-28 22:21:02 +0100934 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100935 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100936 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100937 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100938
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100939 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100940 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100941 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100942 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100943 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100944 }
945 else if (!expr->match) {
946 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100947 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100948 }
949 else {
950 if (!eb_is_empty(&expr->pattern_tree)) {
951 /* a tree is present, let's check what type it is */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100952 if (expr->match == pat_match_str)
953 node = pat_lookup_str(smp, expr);
954 else if (expr->match == pat_match_ip)
955 node = pat_lookup_ip(smp, expr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100956 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100957 pat_res |= PAT_MATCH;
958 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100959 if (sample)
960 *sample = elt->smp;
961 }
962 }
963
964 /* call the match() function for all tests on this value */
965 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100966 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100967 break;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100968 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100969 if (sample)
970 *sample = pattern->smp;
971 }
972 }
973
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100974 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100975}
976