blob: 084f73a86c5124391034e8a554fa44ff327b22d3 [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 FOURNIERa65b3432013-11-28 18:22:00 +010043int (*pat_parse_fcts[PAT_MATCH_NUM])(const char **, struct pattern *, struct sample_storage *, int *, char **) = {
44 [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
Willy Tarreau0cba6072013-11-28 22:21:02 +010059enum pat_match_res (*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern *) = {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010060 [PAT_MATCH_FOUND] = NULL,
61 [PAT_MATCH_BOOL] = pat_match_nothing,
62 [PAT_MATCH_INT] = pat_match_int,
63 [PAT_MATCH_IP] = pat_match_ip,
64 [PAT_MATCH_BIN] = pat_match_bin,
65 [PAT_MATCH_LEN] = pat_match_len,
66 [PAT_MATCH_STR] = pat_match_str,
67 [PAT_MATCH_BEG] = pat_match_beg,
68 [PAT_MATCH_SUB] = pat_match_sub,
69 [PAT_MATCH_DIR] = pat_match_dir,
70 [PAT_MATCH_DOM] = pat_match_dom,
71 [PAT_MATCH_END] = pat_match_end,
72 [PAT_MATCH_REG] = pat_match_reg,
Thierry FOURNIERed66c292013-11-28 11:05:19 +010073};
74
Thierry FOURNIERe3ded592013-12-06 15:36:54 +010075/* Just used for checking configuration compatibility */
76int pat_match_types[PAT_MATCH_NUM] = {
77 [PAT_MATCH_FOUND] = SMP_T_UINT,
78 [PAT_MATCH_BOOL] = SMP_T_UINT,
79 [PAT_MATCH_INT] = SMP_T_UINT,
80 [PAT_MATCH_IP] = SMP_T_ADDR,
81 [PAT_MATCH_BIN] = SMP_T_CBIN,
82 [PAT_MATCH_LEN] = SMP_T_CSTR,
83 [PAT_MATCH_STR] = SMP_T_CSTR,
84 [PAT_MATCH_BEG] = SMP_T_CSTR,
85 [PAT_MATCH_SUB] = SMP_T_CSTR,
86 [PAT_MATCH_DIR] = SMP_T_CSTR,
87 [PAT_MATCH_DOM] = SMP_T_CSTR,
88 [PAT_MATCH_END] = SMP_T_CSTR,
89 [PAT_MATCH_REG] = SMP_T_CSTR,
90};
91
Thierry FOURNIERed66c292013-11-28 11:05:19 +010092/*
93 * These functions are exported and may be used by any other component.
94 */
95
96/* ignore the current line */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010097int pat_parse_nothing(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +010098{
99 return 1;
100}
101
102/* always return false */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100103enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100104{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100105 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100106}
107
108
109/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100110enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100111{
112 int icase;
113
114 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100115 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100117 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100118 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
119 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100120 return PAT_MATCH;
121 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100122}
123
124/* NB: For two binaries buf to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100125enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100126{
127 if (pattern->len != smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100128 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100129
130 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100131 return PAT_MATCH;
132 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100133}
134
135/* Lookup a string in the expression's pattern tree. The node is returned if it
136 * exists, otherwise NULL.
137 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100138static void *pat_lookup_str(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100139{
140 /* data are stored in a tree */
141 struct ebmb_node *node;
142 char prev;
143
144 /* we may have to force a trailing zero on the test pattern */
145 prev = smp->data.str.str[smp->data.str.len];
146 if (prev)
147 smp->data.str.str[smp->data.str.len] = '\0';
148 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
149 if (prev)
150 smp->data.str.str[smp->data.str.len] = prev;
151 return node;
152}
153
154/* Executes a regex. It temporarily changes the data to add a trailing zero,
155 * and restores the previous character when leaving.
156 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100157enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100158{
159 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100160 return PAT_MATCH;
161 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162}
163
164/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100165enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100166{
167 int icase;
168
169 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100170 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100171
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100172 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100173 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
174 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100175 return PAT_NOMATCH;
176 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100177}
178
179/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100180enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100181{
182 int icase;
183
184 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100185 return PAT_NOMATCH;
186 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100187 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
188 (!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 +0100189 return PAT_NOMATCH;
190 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100191}
192
193/* Checks that the pattern is included inside the tested string.
194 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
195 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100196enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100197{
198 int icase;
199 char *end;
200 char *c;
201
202 if (pattern->len > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100203 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100204
205 end = smp->data.str.str + smp->data.str.len - pattern->len;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100206 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100207 if (icase) {
208 for (c = smp->data.str.str; c <= end; c++) {
209 if (tolower(*c) != tolower(*pattern->ptr.str))
210 continue;
211 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100212 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100213 }
214 } else {
215 for (c = smp->data.str.str; c <= end; c++) {
216 if (*c != *pattern->ptr.str)
217 continue;
218 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100219 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100220 }
221 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100222 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100223}
224
225/* Background: Fast way to find a zero byte in a word
226 * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
227 * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
228 *
229 * To look for 4 different byte values, xor the word with those bytes and
230 * then check for zero bytes:
231 *
232 * v = (((unsigned char)c * 0x1010101U) ^ delimiter)
233 * where <delimiter> is the 4 byte values to look for (as an uint)
234 * and <c> is the character that is being tested
235 */
236static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
237{
238 mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
239 return (mask - 0x01010101) & ~mask & 0x80808080U;
240}
241
242static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
243{
244 return d1 << 24 | d2 << 16 | d3 << 8 | d4;
245}
246
247/* This one is used by other real functions. It checks that the pattern is
248 * included inside the tested string, but enclosed between the specified
249 * delimiters or at the beginning or end of the string. The delimiters are
250 * provided as an unsigned int made by make_4delim() and match up to 4 different
251 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
252 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100253static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100254{
255 int may_match, icase;
256 char *c, *end;
257 char *ps;
258 int pl;
259
260 pl = pattern->len;
261 ps = pattern->ptr.str;
262
263 while (pl > 0 && is_delimiter(*ps, delimiters)) {
264 pl--;
265 ps++;
266 }
267
268 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
269 pl--;
270
271 if (pl > smp->data.str.len)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100272 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100273
274 may_match = 1;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100275 icase = pattern->flags & PAT_F_IGNORE_CASE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100276 end = smp->data.str.str + smp->data.str.len - pl;
277 for (c = smp->data.str.str; c <= end; c++) {
278 if (is_delimiter(*c, delimiters)) {
279 may_match = 1;
280 continue;
281 }
282
283 if (!may_match)
284 continue;
285
286 if (icase) {
287 if ((tolower(*c) == tolower(*ps)) &&
288 (strncasecmp(ps, c, pl) == 0) &&
289 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100290 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100291 } else {
292 if ((*c == *ps) &&
293 (strncmp(ps, c, pl) == 0) &&
294 (c == end || is_delimiter(c[pl], delimiters)))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100295 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100296 }
297 may_match = 0;
298 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100299 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100300}
301
302/* Checks that the pattern is included inside the tested string, but enclosed
303 * between the delimiters '?' or '/' or at the beginning or end of the string.
304 * Delimiters at the beginning or end of the pattern are ignored.
305 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100306enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100307{
308 return match_word(smp, pattern, make_4delim('/', '?', '?', '?'));
309}
310
311/* Checks that the pattern is included inside the tested string, but enclosed
312 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
313 * the string. Delimiters at the beginning or end of the pattern are ignored.
314 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100315enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100316{
317 return match_word(smp, pattern, make_4delim('/', '?', '.', ':'));
318}
319
320/* Checks that the integer in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100321enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100322{
323 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
324 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100325 return PAT_MATCH;
326 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100327}
328
329/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100330enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100331{
332 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
333 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100334 return PAT_MATCH;
335 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100336}
337
Willy Tarreau0cba6072013-11-28 22:21:02 +0100338enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100339{
340 unsigned int v4; /* in network byte order */
341 struct in6_addr *v6;
342 int bits, pos;
343 struct in6_addr tmp6;
344
345 if (pattern->type == SMP_T_IPV4) {
346 if (smp->type == SMP_T_IPV4) {
347 v4 = smp->data.ipv4.s_addr;
348 }
349 else if (smp->type == SMP_T_IPV6) {
350 /* v4 match on a V6 sample. We want to check at least for
351 * the following forms :
352 * - ::ffff:ip:v4 (ipv4 mapped)
353 * - ::0000:ip:v4 (old ipv4 mapped)
354 * - 2002:ip:v4:: (6to4)
355 */
356 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
357 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
358 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
359 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
360 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
361 }
362 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
363 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
364 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
365 }
366 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100367 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100368 }
369 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100370 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100371
372 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100373 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100374 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100375 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100376 }
377 else if (pattern->type == SMP_T_IPV6) {
378 if (smp->type == SMP_T_IPV4) {
379 /* Convert the IPv4 sample address to IPv4 with the
380 * mapping method using the ::ffff: prefix.
381 */
382 memset(&tmp6, 0, 10);
383 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
384 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
385 v6 = &tmp6;
386 }
387 else if (smp->type == SMP_T_IPV6) {
388 v6 = &smp->data.ipv6;
389 }
390 else {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100391 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100392 }
393
394 bits = pattern->val.ipv6.mask;
395 for (pos = 0; bits > 0; pos += 4, bits -= 32) {
396 v4 = *(uint32_t*)&v6->s6_addr[pos] ^ *(uint32_t*)&pattern->val.ipv6.addr.s6_addr[pos];
397 if (bits < 32)
398 v4 &= htonl((~0U) << (32-bits));
399 if (v4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100400 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100401 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100402 return PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100403 }
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100404 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100405}
406
407/* Lookup an IPv4 address in the expression's pattern tree using the longest
408 * match method. The node is returned if it exists, otherwise NULL.
409 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100410static void *pat_lookup_ip(struct sample *smp, struct pattern_expr *expr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100411{
412 struct in_addr *s;
413
414 if (smp->type != SMP_T_IPV4)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100415 return PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100416
417 s = &smp->data.ipv4;
418 return ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
419}
420
421/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100422int pat_parse_str(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100423{
424 int len;
425
426 len = strlen(*text);
427 pattern->type = SMP_T_CSTR;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100428 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100429
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100430 if (pattern->flags & PAT_F_TREE_OK) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100431 /* we're allowed to put the data in a tree whose root is pointed
432 * to by val.tree.
433 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100434 struct pat_idx_elt *node;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100435
436 node = calloc(1, sizeof(*node) + len + 1);
437 if (!node) {
438 memprintf(err, "out of memory while loading string pattern");
439 return 0;
440 }
441 node->smp = smp;
442 memcpy(node->node.key, *text, len + 1);
443 if (ebst_insert(pattern->val.tree, &node->node) != &node->node)
444 free(node); /* was a duplicate */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100445 pattern->flags |= PAT_F_TREE; /* this pattern now contains a tree */
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100446 return 1;
447 }
448
449 pattern->ptr.str = strdup(*text);
450 pattern->smp = smp;
451 if (!pattern->ptr.str) {
452 memprintf(err, "out of memory while loading string pattern");
453 return 0;
454 }
455 pattern->len = len;
456 return 1;
457}
458
459/* Parse a binary written in hexa. It is allocated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100460int pat_parse_bin(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100461{
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100462 pattern->type = SMP_T_CBIN;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100463 pattern->expect_type = SMP_T_CBIN;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100464 pattern->smp = smp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100465
Willy Tarreau126d4062013-12-03 17:50:47 +0100466 return parse_binary(*text, &pattern->ptr.str, &pattern->len, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100467}
468
469/* Parse and concatenate all further strings into one. */
470int
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100471pat_parse_strcat(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100472{
473
474 int len = 0, i;
475 char *s;
476
477 for (i = 0; *text[i]; i++)
478 len += strlen(text[i])+1;
479
480 pattern->type = SMP_T_CSTR;
481 pattern->ptr.str = s = calloc(1, len);
482 pattern->smp = smp;
483 if (!pattern->ptr.str) {
484 memprintf(err, "out of memory while loading pattern");
485 return 0;
486 }
487
488 for (i = 0; *text[i]; i++)
489 s += sprintf(s, i?" %s":"%s", text[i]);
490
491 pattern->len = len;
492
493 return i;
494}
495
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100496/* Free data allocated by pat_parse_reg */
497static void pat_free_reg(void *ptr)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100498{
499 regex_free(ptr);
500}
501
502/* Parse a regex. It is allocated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100503int pat_parse_reg(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100504{
505 regex *preg;
506
507 preg = calloc(1, sizeof(*preg));
508
509 if (!preg) {
510 memprintf(err, "out of memory while loading pattern");
511 return 0;
512 }
513
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100514 if (!regex_comp(*text, preg, !(pattern->flags & PAT_F_IGNORE_CASE), 0, err)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100515 free(preg);
516 return 0;
517 }
518
519 pattern->ptr.reg = preg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100520 pattern->freeptrbuf = &pat_free_reg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100521 pattern->smp = smp;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100522 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100523 return 1;
524}
525
526/* Parse a range of positive integers delimited by either ':' or '-'. If only
527 * one integer is read, it is set as both min and max. An operator may be
528 * specified as the prefix, among this list of 5 :
529 *
530 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
531 *
532 * The default operator is "eq". It supports range matching. Ranges are
533 * rejected for other operators. The operator may be changed at any time.
534 * The operator is stored in the 'opaque' argument.
535 *
536 * If err is non-NULL, an error message will be returned there on errors and
537 * the caller will have to free it.
538 *
539 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100540int pat_parse_int(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100541{
542 signed long long i;
543 unsigned int j, last, skip = 0;
544 const char *ptr = *text;
545
546 pattern->type = SMP_T_UINT;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100547 pattern->expect_type = SMP_T_UINT;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100548 pattern->smp = smp;
549 while (!isdigit((unsigned char)*ptr)) {
550 switch (get_std_op(ptr)) {
551 case STD_OP_EQ: *opaque = 0; break;
552 case STD_OP_GT: *opaque = 1; break;
553 case STD_OP_GE: *opaque = 2; break;
554 case STD_OP_LT: *opaque = 3; break;
555 case STD_OP_LE: *opaque = 4; break;
556 default:
557 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
558 return 0;
559 }
560
561 skip++;
562 ptr = text[skip];
563 }
564
565 last = i = 0;
566 while (1) {
567 j = *ptr++;
568 if ((j == '-' || j == ':') && !last) {
569 last++;
570 pattern->val.range.min = i;
571 i = 0;
572 continue;
573 }
574 j -= '0';
575 if (j > 9)
576 // also catches the terminating zero
577 break;
578 i *= 10;
579 i += j;
580 }
581
582 if (last && *opaque >= 1 && *opaque <= 4) {
583 /* having a range with a min or a max is absurd */
584 memprintf(err, "integer range '%s' specified with a comparison operator", text[skip]);
585 return 0;
586 }
587
588 if (!last)
589 pattern->val.range.min = i;
590 pattern->val.range.max = i;
591
592 switch (*opaque) {
593 case 0: /* eq */
594 pattern->val.range.min_set = 1;
595 pattern->val.range.max_set = 1;
596 break;
597 case 1: /* gt */
598 pattern->val.range.min++; /* gt = ge + 1 */
599 case 2: /* ge */
600 pattern->val.range.min_set = 1;
601 pattern->val.range.max_set = 0;
602 break;
603 case 3: /* lt */
604 pattern->val.range.max--; /* lt = le - 1 */
605 case 4: /* le */
606 pattern->val.range.min_set = 0;
607 pattern->val.range.max_set = 1;
608 break;
609 }
610 return skip + 1;
611}
612
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100613int pat_parse_len(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
614{
615 int ret;
616
617 ret = pat_parse_int(text, pattern, smp, opaque, err);
618 pattern->expect_type = SMP_T_CSTR;
619 return ret;
620}
621
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100622/* Parse a range of positive 2-component versions delimited by either ':' or
623 * '-'. The version consists in a major and a minor, both of which must be
624 * smaller than 65536, because internally they will be represented as a 32-bit
625 * integer.
626 * If only one version is read, it is set as both min and max. Just like for
627 * pure integers, an operator may be specified as the prefix, among this list
628 * of 5 :
629 *
630 * 0:eq, 1:gt, 2:ge, 3:lt, 4:le
631 *
632 * The default operator is "eq". It supports range matching. Ranges are
633 * rejected for other operators. The operator may be changed at any time.
634 * The operator is stored in the 'opaque' argument. This allows constructs
635 * such as the following one :
636 *
637 * acl obsolete_ssl ssl_req_proto lt 3
638 * acl unsupported_ssl ssl_req_proto gt 3.1
639 * acl valid_ssl ssl_req_proto 3.0-3.1
640 *
641 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100642int 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 +0100643{
644 signed long long i;
645 unsigned int j, last, skip = 0;
646 const char *ptr = *text;
647
648
649 while (!isdigit((unsigned char)*ptr)) {
650 switch (get_std_op(ptr)) {
651 case STD_OP_EQ: *opaque = 0; break;
652 case STD_OP_GT: *opaque = 1; break;
653 case STD_OP_GE: *opaque = 2; break;
654 case STD_OP_LT: *opaque = 3; break;
655 case STD_OP_LE: *opaque = 4; break;
656 default:
657 memprintf(err, "'%s' is neither a number nor a supported operator", ptr);
658 return 0;
659 }
660
661 skip++;
662 ptr = text[skip];
663 }
664
665 last = i = 0;
666 while (1) {
667 j = *ptr++;
668 if (j == '.') {
669 /* minor part */
670 if (i >= 65536)
671 return 0;
672 i <<= 16;
673 continue;
674 }
675 if ((j == '-' || j == ':') && !last) {
676 last++;
677 if (i < 65536)
678 i <<= 16;
679 pattern->val.range.min = i;
680 i = 0;
681 continue;
682 }
683 j -= '0';
684 if (j > 9)
685 // also catches the terminating zero
686 break;
687 i = (i & 0xFFFF0000) + (i & 0xFFFF) * 10;
688 i += j;
689 }
690
691 /* if we only got a major version, let's shift it now */
692 if (i < 65536)
693 i <<= 16;
694
695 if (last && *opaque >= 1 && *opaque <= 4) {
696 /* having a range with a min or a max is absurd */
697 memprintf(err, "version range '%s' specified with a comparison operator", text[skip]);
698 return 0;
699 }
700
701 pattern->smp = smp;
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100702 pattern->expect_type = SMP_T_CSTR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100703
704 if (!last)
705 pattern->val.range.min = i;
706 pattern->val.range.max = i;
707
708 switch (*opaque) {
709 case 0: /* eq */
710 pattern->val.range.min_set = 1;
711 pattern->val.range.max_set = 1;
712 break;
713 case 1: /* gt */
714 pattern->val.range.min++; /* gt = ge + 1 */
715 case 2: /* ge */
716 pattern->val.range.min_set = 1;
717 pattern->val.range.max_set = 0;
718 break;
719 case 3: /* lt */
720 pattern->val.range.max--; /* lt = le - 1 */
721 case 4: /* le */
722 pattern->val.range.min_set = 0;
723 pattern->val.range.max_set = 1;
724 break;
725 }
726 return skip + 1;
727}
728
729/* Parse an IP address and an optional mask in the form addr[/mask].
730 * The addr may either be an IPv4 address or a hostname. The mask
731 * may either be a dotted mask or a number of bits. Returns 1 if OK,
732 * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
733 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100734int pat_parse_ip(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100735{
736 struct eb_root *tree = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100737 if (pattern->flags & PAT_F_TREE_OK)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100738 tree = pattern->val.tree;
739
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +0100740 pattern->expect_type = SMP_T_ADDR;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100741 if (str2net(*text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
742 unsigned int mask = ntohl(pattern->val.ipv4.mask.s_addr);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100743 struct pat_idx_elt *node;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100744 /* check if the mask is contiguous so that we can insert the
745 * network into the tree. A continuous mask has only ones on
746 * the left. This means that this mask + its lower bit added
747 * once again is null.
748 */
749 pattern->type = SMP_T_IPV4;
750 if (mask + (mask & -mask) == 0 && tree) {
751 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
752 /* FIXME: insert <addr>/<mask> into the tree here */
753 node = calloc(1, sizeof(*node) + 4); /* reserve 4 bytes for IPv4 address */
754 if (!node) {
755 memprintf(err, "out of memory while loading IPv4 pattern");
756 return 0;
757 }
758 node->smp = smp;
759 memcpy(node->node.key, &pattern->val.ipv4.addr, 4); /* network byte order */
760 node->node.node.pfx = mask;
761 if (ebmb_insert_prefix(tree, &node->node, 4) != &node->node)
762 free(node); /* was a duplicate */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100763 pattern->flags |= PAT_F_TREE;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100764 return 1;
765 }
766 return 1;
767 }
768 else if (str62net(*text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
769 /* no tree support right now */
770 pattern->type = SMP_T_IPV6;
771 return 1;
772 }
773 else {
774 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", *text);
775 return 0;
776 }
777}
778
779/* NB: does nothing if <pat> is NULL */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100780void pattern_free(struct pattern *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100781{
782 if (!pat)
783 return;
784
785 if (pat->ptr.ptr) {
786 if (pat->freeptrbuf)
787 pat->freeptrbuf(pat->ptr.ptr);
788
789 free(pat->ptr.ptr);
790 }
791
792 free(pat);
793}
794
795void free_pattern_list(struct list *head)
796{
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100797 struct pattern *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100798 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100799 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100800}
801
802void free_pattern_tree(struct eb_root *root)
803{
804 struct eb_node *node, *next;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100805 struct pat_idx_elt *elt;
806
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100807 node = eb_first(root);
808 while (node) {
809 next = eb_next(node);
810 eb_delete(node);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100811 elt = container_of(node, struct pat_idx_elt, node);
812 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100813 node = next;
814 }
815}
816
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100817void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100818{
819 free_pattern_list(&expr->patterns);
820 free_pattern_tree(&expr->pattern_tree);
821 LIST_INIT(&expr->patterns);
822}
823
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100824void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100825{
826 LIST_INIT(&expr->patterns);
827 expr->pattern_tree = EB_ROOT_UNIQUE;
828}
829
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100830/* return 1 if the process is ok
831 * return -1 if the parser fail. The err message is filled.
832 * return -2 if out of memory
833 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100834int pattern_register(struct pattern_expr *expr, char *text,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100835 struct sample_storage *smp,
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100836 struct pattern **pattern,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100837 int patflags, char **err)
838{
839 const char *args[2];
840 int opaque = 0;
841
842 args[0] = text;
843 args[1] = "";
844
845 /* we keep the previous pattern along iterations as long as it's not used */
846 if (!*pattern)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100847 *pattern = (struct pattern *)malloc(sizeof(**pattern));
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100848 if (!*pattern)
849 return -1;
850
851 memset(*pattern, 0, sizeof(**pattern));
852 (*pattern)->flags = patflags;
853
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100854 if (!((*pattern)->flags & PAT_F_IGNORE_CASE) &&
855 (expr->match == pat_match_str || expr->match == pat_match_ip)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100856 /* we pre-set the data pointer to the tree's head so that functions
857 * which are able to insert in a tree know where to do that.
858 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100859 (*pattern)->flags |= PAT_F_TREE_OK;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100860 (*pattern)->val.tree = &expr->pattern_tree;
861 }
862
863 (*pattern)->type = SMP_TYPES; /* unspecified type by default */
864 if (!expr->parse(args, *pattern, smp, &opaque, err))
865 return -1;
866
867 /* if the parser did not feed the tree, let's chain the pattern to the list */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100868 if (!((*pattern)->flags & PAT_F_TREE)) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100869 LIST_ADDQ(&expr->patterns, &(*pattern)->list);
870 *pattern = NULL; /* get a new one */
871 }
872
873 return 1;
874}
875
876/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
877 * be returned there on errors and the caller will have to free it.
878 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100879int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100880 const char *filename, int patflags,
881 char **err)
882{
883 FILE *file;
884 char *c;
885 char *arg;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100886 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100887 int ret = 0;
888 int line = 0;
889 int code;
890
891 file = fopen(filename, "r");
892 if (!file) {
893 memprintf(err, "failed to open pattern file <%s>", filename);
894 return 0;
895 }
896
897 /* now parse all patterns. The file may contain only one pattern per
898 * line. If the line contains spaces, they will be part of the pattern.
899 * The pattern stops at the first CR, LF or EOF encountered.
900 */
901 pattern = NULL;
902 while (fgets(trash.str, trash.size, file) != NULL) {
903 line++;
904 c = trash.str;
905
906 /* ignore lines beginning with a dash */
907 if (*c == '#')
908 continue;
909
910 /* strip leading spaces and tabs */
911 while (*c == ' ' || *c == '\t')
912 c++;
913
914
915 arg = c;
916 while (*c && *c != '\n' && *c != '\r')
917 c++;
918 *c = 0;
919
920 /* empty lines are ignored too */
921 if (c == arg)
922 continue;
923
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100924 code = pattern_register(expr, arg, NULL, &pattern, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100925 if (code == -2) {
926 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
927 goto out_close;
928 }
929 else if (code < 0) {
930 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
931 goto out_free_pattern;
932 }
933 }
934
935 ret = 1; /* success */
936
937 out_free_pattern:
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100938 pattern_free(pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100939 out_close:
940 fclose(file);
941 return ret;
942}
943
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100944/* This function matches a sample <smp> against a set of patterns presented in
945 * pattern expression <expr>. Upon success, if <sample> is not NULL, it is fed
946 * with the pointer associated with the matching pattern. This function returns
947 * PAT_NOMATCH or PAT_MATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100948 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100949enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp,
950 struct sample_storage **sample)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100951{
Willy Tarreau0cba6072013-11-28 22:21:02 +0100952 enum pat_match_res pat_res = PAT_NOMATCH;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100953 struct pattern *pattern;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100954 struct ebmb_node *node = NULL;
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100955 struct pat_idx_elt *elt;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100956
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100957 if (expr->match == pat_match_nothing) {
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100958 if (smp->data.uint)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100959 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100960 else
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100961 pat_res |= PAT_NOMATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100962 }
963 else if (!expr->match) {
964 /* just check for existence */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100965 pat_res |= PAT_MATCH;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100966 }
967 else {
968 if (!eb_is_empty(&expr->pattern_tree)) {
969 /* a tree is present, let's check what type it is */
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100970 if (expr->match == pat_match_str) {
971 if (sample_convert(smp, SMP_T_STR))
972 node = pat_lookup_str(smp, expr);
973 }
974 else if (expr->match == pat_match_ip) {
975 if (sample_convert(smp, SMP_T_IPV4))
976 node = pat_lookup_ip(smp, expr);
977 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100978 if (node) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100979 pat_res |= PAT_MATCH;
980 elt = ebmb_entry(node, struct pat_idx_elt, node);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100981 if (sample)
982 *sample = elt->smp;
983 }
984 }
985
986 /* call the match() function for all tests on this value */
987 list_for_each_entry(pattern, &expr->patterns, list) {
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100988 if (pat_res == PAT_MATCH)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100989 break;
Thierry FOURNIERe3ded592013-12-06 15:36:54 +0100990 if (sample_convert(smp, pattern->expect_type))
991 pat_res |= expr->match(smp, pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100992 if (sample)
993 *sample = pattern->smp;
994 }
995 }
996
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100997 return pat_res;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100998}
999