blob: b417e9ffc6bc4e96b8a25a8fe253e2e08c429ed9 [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 pattern->type = SMP_T_IPV6;
402 return 1;
403 }
404 else {
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100405 memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100406 return 0;
407 }
408}
409
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100410/*
411 *
412 * These functions are exported and may be used by any other component.
413 *
414 * This fucntion just take a sample <smp> and check if this sample match
415 * with the pattern <pattern>. This fucntion return just PAT_MATCH or
416 * PAT_NOMATCH.
417 *
418 */
419
420/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100421struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100422{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100423 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100424}
425
426
427/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100428struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100429{
430 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100431 struct ebmb_node *node;
432 char prev;
433 struct pattern_tree *elt;
434 struct pattern_list *lst;
435 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100436
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100437 /* convert input to string */
438 if (!sample_convert(smp, SMP_T_STR))
439 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100440
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100441 /* Lookup a string in the expression's pattern tree. */
442 if (!eb_is_empty(&expr->pattern_tree)) {
443 /* we may have to force a trailing zero on the test pattern */
444 prev = smp->data.str.str[smp->data.str.len];
445 if (prev)
446 smp->data.str.str[smp->data.str.len] = '\0';
447 node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
448 if (prev)
449 smp->data.str.str[smp->data.str.len] = prev;
450
451 if (node) {
452 if (fill) {
453 elt = ebmb_entry(node, struct pattern_tree, node);
454 static_pattern.smp = elt->smp;
455 static_pattern.flags = PAT_F_TREE;
456 static_pattern.type = SMP_T_STR;
457 static_pattern.ptr.str = (char *)elt->node.key;
458 }
459 return &static_pattern;
460 }
461 }
462
463 /* look in the list */
464 list_for_each_entry(lst, &expr->patterns, list) {
465 pattern = &lst->pat;
466
467 if (pattern->len != smp->data.str.len)
468 continue;
469
470 icase = pattern->flags & PAT_F_IGNORE_CASE;
471 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
472 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
473 return pattern;
474 }
475
476 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100477}
478
479/* NB: For two binaries buf to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100480struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100481{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100482 struct pattern_list *lst;
483 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100484
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100485 /* Convert input to binary. */
486 if (!sample_convert(smp, SMP_T_BIN))
487 return NULL;
488
489 /* Look in the list. */
490 list_for_each_entry(lst, &expr->patterns, list) {
491 pattern = &lst->pat;
492
493 if (pattern->len != smp->data.str.len)
494 continue;
495
496 if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
497 return pattern;
498 }
499
500 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100501}
502
503/* Executes a regex. It temporarily changes the data to add a trailing zero,
504 * and restores the previous character when leaving.
505 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100506struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100507{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100508 struct pattern_list *lst;
509 struct pattern *pattern;
510
511 /* convert input to string */
512 if (!sample_convert(smp, SMP_T_STR))
513 return NULL;
514
515 /* look in the list */
516 list_for_each_entry(lst, &expr->patterns, list) {
517 pattern = &lst->pat;
518
519 if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0)
520 return pattern;
521 }
522 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100523}
524
525/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100526struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100527{
528 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100529 struct pattern_list *lst;
530 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100531
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100532 /* convert input to string */
533 if (!sample_convert(smp, SMP_T_STR))
534 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100535
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100536 list_for_each_entry(lst, &expr->patterns, list) {
537 pattern = &lst->pat;
538
539 if (pattern->len > smp->data.str.len)
540 continue;
541
542 icase = pattern->flags & PAT_F_IGNORE_CASE;
543 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
544 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
545 continue;
546
547 return pattern;
548 }
549 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100550}
551
552/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100553struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100554{
555 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100556 struct pattern_list *lst;
557 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100558
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100559 /* convert input to string */
560 if (!sample_convert(smp, SMP_T_STR))
561 return NULL;
562
563 list_for_each_entry(lst, &expr->patterns, list) {
564 pattern = &lst->pat;
565
566 if (pattern->len > smp->data.str.len)
567 continue;
568
569 icase = pattern->flags & PAT_F_IGNORE_CASE;
570 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
571 (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
572 continue;
573
574 return pattern;
575 }
576 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100577}
578
579/* Checks that the pattern is included inside the tested string.
580 * NB: Suboptimal, should be rewritten using a Boyer-Moore method.
581 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100582struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100583{
584 int icase;
585 char *end;
586 char *c;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100587 struct pattern_list *lst;
588 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100589
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100590 /* convert input to string */
591 if (!sample_convert(smp, SMP_T_STR))
592 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100593
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100594 list_for_each_entry(lst, &expr->patterns, list) {
595 pattern = &lst->pat;
596
597 if (pattern->len > smp->data.str.len)
598 continue;
599
600 end = smp->data.str.str + smp->data.str.len - pattern->len;
601 icase = pattern->flags & PAT_F_IGNORE_CASE;
602 if (icase) {
603 for (c = smp->data.str.str; c <= end; c++) {
604 if (tolower(*c) != tolower(*pattern->ptr.str))
605 continue;
606 if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
607 return pattern;
608 }
609 } else {
610 for (c = smp->data.str.str; c <= end; c++) {
611 if (*c != *pattern->ptr.str)
612 continue;
613 if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
614 return pattern;
615 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100616 }
617 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100618 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100619}
620
621/* This one is used by other real functions. It checks that the pattern is
622 * included inside the tested string, but enclosed between the specified
623 * delimiters or at the beginning or end of the string. The delimiters are
624 * provided as an unsigned int made by make_4delim() and match up to 4 different
625 * delimiters. Delimiters are stripped at the beginning and end of the pattern.
626 */
627static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters)
628{
629 int may_match, icase;
630 char *c, *end;
631 char *ps;
632 int pl;
633
634 pl = pattern->len;
635 ps = pattern->ptr.str;
636
637 while (pl > 0 && is_delimiter(*ps, delimiters)) {
638 pl--;
639 ps++;
640 }
641
642 while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
643 pl--;
644
645 if (pl > smp->data.str.len)
646 return PAT_NOMATCH;
647
648 may_match = 1;
649 icase = pattern->flags & PAT_F_IGNORE_CASE;
650 end = smp->data.str.str + smp->data.str.len - pl;
651 for (c = smp->data.str.str; c <= end; c++) {
652 if (is_delimiter(*c, delimiters)) {
653 may_match = 1;
654 continue;
655 }
656
657 if (!may_match)
658 continue;
659
660 if (icase) {
661 if ((tolower(*c) == tolower(*ps)) &&
662 (strncasecmp(ps, c, pl) == 0) &&
663 (c == end || is_delimiter(c[pl], delimiters)))
664 return PAT_MATCH;
665 } else {
666 if ((*c == *ps) &&
667 (strncmp(ps, c, pl) == 0) &&
668 (c == end || is_delimiter(c[pl], delimiters)))
669 return PAT_MATCH;
670 }
671 may_match = 0;
672 }
673 return PAT_NOMATCH;
674}
675
676/* Checks that the pattern is included inside the tested string, but enclosed
677 * between the delimiters '?' or '/' or at the beginning or end of the string.
678 * Delimiters at the beginning or end of the pattern are ignored.
679 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100680struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100681{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100682 struct pattern_list *lst;
683 struct pattern *pattern;
684
685 /* convert input to string */
686 if (!sample_convert(smp, SMP_T_STR))
687 return NULL;
688
689 list_for_each_entry(lst, &expr->patterns, list) {
690 pattern = &lst->pat;
691 if (match_word(smp, pattern, make_4delim('/', '?', '?', '?')))
692 return pattern;
693 }
694 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100695}
696
697/* Checks that the pattern is included inside the tested string, but enclosed
698 * between the delmiters '/', '?', '.' or ":" or at the beginning or end of
699 * the string. Delimiters at the beginning or end of the pattern are ignored.
700 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100701struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100702{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100703 struct pattern_list *lst;
704 struct pattern *pattern;
705
706 /* convert input to string */
707 if (!sample_convert(smp, SMP_T_STR))
708 return NULL;
709
710 list_for_each_entry(lst, &expr->patterns, list) {
711 pattern = &lst->pat;
712 if (match_word(smp, pattern, make_4delim('/', '?', '.', ':')))
713 return pattern;
714 }
715 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100716}
717
718/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100719struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100720{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100721 struct pattern_list *lst;
722 struct pattern *pattern;
723
724 /* convert input to integer */
725 if (!sample_convert(smp, SMP_T_UINT))
726 return NULL;
727
728 list_for_each_entry(lst, &expr->patterns, list) {
729 pattern = &lst->pat;
730 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
731 (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
732 return pattern;
733 }
734 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100735}
736
737/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100738struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100739{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100740 struct pattern_list *lst;
741 struct pattern *pattern;
742
743 /* convert input to string */
744 if (!sample_convert(smp, SMP_T_STR))
745 return NULL;
746
747 list_for_each_entry(lst, &expr->patterns, list) {
748 pattern = &lst->pat;
749 if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
750 (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
751 return pattern;
752 }
753 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100754}
755
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100756struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100757{
758 unsigned int v4; /* in network byte order */
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100759 struct in6_addr tmp6;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100760 struct in_addr *s;
761 struct ebmb_node *node;
762 struct pattern_tree *elt;
763 struct pattern_list *lst;
764 struct pattern *pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100765
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100766 /* convert input to addr */
767 if (!sample_convert(smp, SMP_T_ADDR))
768 return NULL;
769
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100770 /* The input sample is IPv4. Try to match in the trees. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100771 if (smp->type == SMP_T_IPV4) {
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100772 /* Lookup an IPv4 address in the expression's pattern tree using
773 * the longest match method.
774 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100775 s = &smp->data.ipv4;
776 node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
777 if (node) {
778 if (fill) {
779 elt = ebmb_entry(node, struct pattern_tree, node);
780 static_pattern.smp = elt->smp;
781 static_pattern.flags = PAT_F_TREE;
782 static_pattern.type = SMP_T_IPV4;
783 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
784 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
785 return NULL;
786 }
787 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100788 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100789
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100790 /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
791 * sample address to IPv6 with the mapping method using the ::ffff:
792 * prefix, and try to lookup in the IPv6 tree.
793 */
794 memset(&tmp6, 0, 10);
795 *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
796 *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
797 node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
798 if (node) {
799 if (fill) {
800 elt = ebmb_entry(node, struct pattern_tree, node);
801 static_pattern.smp = elt->smp;
802 static_pattern.flags = PAT_F_TREE;
803 static_pattern.type = SMP_T_IPV6;
804 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
805 static_pattern.val.ipv6.mask = elt->node.node.pfx;
806 }
807 return &static_pattern;
808 }
809 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100810
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100811 /* The input sample is IPv6. Try to match in the trees. */
812 if (smp->type == SMP_T_IPV6) {
813 /* Lookup an IPv6 address in the expression's pattern tree using
814 * the longest match method.
815 */
816 node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
817 if (node) {
818 if (fill) {
819 elt = ebmb_entry(node, struct pattern_tree, node);
820 static_pattern.smp = elt->smp;
821 static_pattern.flags = PAT_F_TREE;
822 static_pattern.type = SMP_T_IPV6;
823 memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
824 static_pattern.val.ipv6.mask = elt->node.node.pfx;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100825 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100826 return &static_pattern;
827 }
828
829 /* Try to convert 6 to 4 when the start of the ipv6 address match the
830 * following forms :
831 * - ::ffff:ip:v4 (ipv4 mapped)
832 * - ::0000:ip:v4 (old ipv4 mapped)
833 * - 2002:ip:v4:: (6to4)
834 */
835 if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
836 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
837 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
838 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
839 *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
840 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
841 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
842 else
843 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
844 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
845
846 /* Lookup an IPv4 address in the expression's pattern tree using the longest
847 * match method.
848 */
849 node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
850 if (node) {
851 if (fill) {
852 elt = ebmb_entry(node, struct pattern_tree, node);
853 static_pattern.smp = elt->smp;
854 static_pattern.flags = PAT_F_TREE;
855 static_pattern.type = SMP_T_IPV4;
856 memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
857 if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
858 return NULL;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100859 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100860 return &static_pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100861 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100862 }
863 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100864
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100865 /* Lookup in the list. the list contain only IPv4 patterns */
866 list_for_each_entry(lst, &expr->patterns, list) {
867 pattern = &lst->pat;
868
869 /* The input sample is IPv4, use it as is. */
870 if (smp->type == SMP_T_IPV4) {
871 v4 = smp->data.ipv4.s_addr;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100872 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100873 else if (smp->type == SMP_T_IPV6) {
874 /* v4 match on a V6 sample. We want to check at least for
875 * the following forms :
876 * - ::ffff:ip:v4 (ipv4 mapped)
877 * - ::0000:ip:v4 (old ipv4 mapped)
878 * - 2002:ip:v4:: (6to4)
879 */
880 if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
881 *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
882 (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
883 *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
884 v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100885 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100886 else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
887 v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
888 ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100889 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100890 else
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100891 continue;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100892 }
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100893
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100894 /* Check if the input sample match the current pattern. */
895 if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100896 return pattern;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100897 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100898 return NULL;
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100899}
900
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100901/* NB: does nothing if <pat> is NULL */
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100902void pattern_free(struct pattern_list *pat)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100903{
904 if (!pat)
905 return;
906
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100907 if (pat->pat.ptr.ptr) {
908 if (pat->pat.freeptrbuf)
909 pat->pat.freeptrbuf(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100910
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100911 free(pat->pat.ptr.ptr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100912 }
913
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100914 free(pat->pat.smp);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100915 free(pat);
916}
917
918void free_pattern_list(struct list *head)
919{
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100920 struct pattern_list *pat, *tmp;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100921 list_for_each_entry_safe(pat, tmp, head, list)
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100922 pattern_free(pat);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100923}
924
925void free_pattern_tree(struct eb_root *root)
926{
927 struct eb_node *node, *next;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100928 struct pattern_tree *elt;
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100929
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100930 node = eb_first(root);
931 while (node) {
932 next = eb_next(node);
933 eb_delete(node);
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +0100934 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +0100935 free(elt->smp);
Thierry FOURNIER3ce88c72013-12-09 11:29:46 +0100936 free(elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100937 node = next;
938 }
939}
940
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100941void pattern_prune_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100942{
943 free_pattern_list(&expr->patterns);
944 free_pattern_tree(&expr->pattern_tree);
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100945 free_pattern_tree(&expr->pattern_tree_2);
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100946 LIST_INIT(&expr->patterns);
947}
948
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100949void pattern_init_expr(struct pattern_expr *expr)
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100950{
951 LIST_INIT(&expr->patterns);
952 expr->pattern_tree = EB_ROOT_UNIQUE;
Thierry FOURNIER33a74332013-12-19 23:54:54 +0100953 expr->pattern_tree_2 = EB_ROOT_UNIQUE;
Thierry FOURNIERd163e1c2013-11-28 11:41:23 +0100954}
955
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100956/*
957 *
958 * The following functions are used for the pattern indexation
959 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100960 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100961
962int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100963{
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100964 struct pattern_list *patl;
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100965
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100966 /* allocate pattern */
967 patl = calloc(1, sizeof(*patl));
968 if (!patl) {
969 memprintf(err, "out of memory while indexing pattern");
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100970 return 0;
971 }
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100972
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100973 /* duplicate pattern */
974 memcpy(&patl->pat, pat, sizeof(*pat));
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100975
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100976 /* chain pattern in the expression */
977 LIST_ADDQ(&expr->patterns, &patl->list);
978
979 /* that's ok */
980 return 1;
981}
982
983int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
984{
985 struct pattern_list *patl;
986
987 /* allocate pattern */
988 patl = calloc(1, sizeof(*patl));
989 if (!patl)
Thierry FOURNIER972028f2014-01-23 17:53:31 +0100990 return 0;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +0100991
Thierry FOURNIERb9b08462013-12-13 15:12:32 +0100992 /* duplicate pattern */
993 memcpy(&patl->pat, pat, sizeof(*pat));
994 patl->pat.ptr.ptr = malloc(patl->pat.len);
995 if (!patl->pat.ptr.ptr) {
996 free(patl);
997 memprintf(err, "out of memory while indexing pattern");
998 return 0;
999 }
1000 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001001
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001002 /* chain pattern in the expression */
1003 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001004
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001005 /* that's ok */
1006 return 1;
1007}
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001008
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001009int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1010{
1011 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001012
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001013 /* allocate pattern */
1014 patl = calloc(1, sizeof(*patl));
1015 if (!patl) {
1016 memprintf(err, "out of memory while indexing pattern");
1017 return 0;
1018 }
1019
1020 /* duplicate pattern */
1021 memcpy(&patl->pat, pat, sizeof(*pat));
1022 patl->pat.ptr.str = malloc(patl->pat.len + 1);
1023 if (!patl->pat.ptr.str) {
1024 free(patl);
1025 memprintf(err, "out of memory while indexing pattern");
1026 return 0;
1027 }
1028 memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1029 patl->pat.ptr.str[patl->pat.len] = '\0';
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001030
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001031 /* chain pattern in the expression */
1032 LIST_ADDQ(&expr->patterns, &patl->list);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001033
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001034 /* that's ok */
1035 return 1;
1036}
1037
1038int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1039{
1040 struct pattern_list *patl;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001041
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001042 /* allocate pattern */
1043 patl = calloc(1, sizeof(*patl));
1044 if (!patl) {
1045 memprintf(err, "out of memory while indexing pattern");
1046 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001047 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001048
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001049 /* duplicate pattern */
1050 memcpy(&patl->pat, pat, sizeof(*pat));
1051
1052 /* allocate regex */
1053 patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1054 if (!patl->pat.ptr.reg) {
1055 free(patl);
1056 memprintf(err, "out of memory while indexing pattern");
1057 return 0;
1058 }
1059
1060 /* compile regex */
1061 if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) {
1062 free(patl);
1063 free(patl->pat.ptr.reg);
1064 return 0;
1065 }
1066
1067 /* free pattern method */
1068 patl->pat.freeptrbuf = &pat_free_reg;
1069
1070 /* chain pattern in the expression */
1071 LIST_ADDQ(&expr->patterns, &patl->list);
1072
1073 /* that's ok */
1074 return 1;
1075}
1076
1077int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1078{
1079 unsigned int mask;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001080 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001081
1082 /* Only IPv4 can be indexed */
1083 if (pat->type == SMP_T_IPV4) {
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001084 /* in IPv4 case, check if the mask is contiguous so that we can
1085 * insert the network into the tree. A continuous mask has only
1086 * ones on the left. This means that this mask + its lower bit
1087 * added once again is null.
1088 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001089 mask = ntohl(pat->val.ipv4.mask.s_addr);
1090 if (mask + (mask & -mask) == 0) {
1091 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001092
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001093 /* node memory allocation */
1094 node = calloc(1, sizeof(*node) + 4);
1095 if (!node) {
1096 memprintf(err, "out of memory while loading pattern");
1097 return 0;
1098 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001099
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001100 /* copy the pointer to sample associated to this node */
1101 node->smp = pat->smp;
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001102
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001103 /* FIXME: insert <addr>/<mask> into the tree here */
1104 memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1105 node->node.node.pfx = mask;
1106 if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node)
1107 free(node); /* was a duplicate */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001108
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001109 /* that's ok */
1110 return 1;
1111 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001112 else {
1113 /* If the mask is not contiguous, just add the pattern to the list */
1114 return pat_idx_list_val(expr, pat, err);
1115 }
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001116 }
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001117 else if (pat->type == SMP_T_IPV6) {
1118 /* IPv6 also can be indexed */
1119 node = calloc(1, sizeof(*node) + 16);
1120 if (!node) {
1121 memprintf(err, "out of memory while loading pattern");
1122 return 0;
1123 }
1124
1125 /* copy the pointer to sample associated to this node */
1126 node->smp = pat->smp;
1127
1128 /* FIXME: insert <addr>/<mask> into the tree here */
1129 memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1130 node->node.node.pfx = pat->val.ipv6.mask;
1131 if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node)
1132 free(node); /* was a duplicate */
1133
1134 /* that's ok */
1135 return 1;
1136 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001137
Thierry FOURNIER33a74332013-12-19 23:54:54 +01001138 return 0;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001139}
1140
1141int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1142{
1143 int len;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001144 struct pattern_tree *node;
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001145
1146 /* Only string can be indexed */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001147 if (pat->type != SMP_T_STR) {
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001148 memprintf(err, "internal error: string expected, but the type is '%s'",
1149 smp_to_type[pat->type]);
1150 return 0;
Thierry FOURNIER972028f2014-01-23 17:53:31 +01001151 }
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001152
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001153 /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1154 if (pat->flags & PAT_F_IGNORE_CASE)
1155 return pat_idx_list_str(expr, pat, err);
Thierry FOURNIER7148ce62013-12-06 19:06:43 +01001156
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001157 /* Process the key len */
1158 len = strlen(pat->ptr.str) + 1;
1159
1160 /* node memory allocation */
1161 node = calloc(1, sizeof(*node) + len);
1162 if (!node) {
1163 memprintf(err, "out of memory while loading pattern");
1164 return 0;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001165 }
1166
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001167 /* copy the pointer to sample associated to this node */
1168 node->smp = pat->smp;
1169
1170 /* copy the string */
1171 memcpy(node->node.key, pat->ptr.str, len);
1172
1173 /* index the new node */
1174 if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node)
1175 free(node); /* was a duplicate */
1176
1177 /* that's ok */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001178 return 1;
1179}
1180
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001181/* return 1 if the process is ok
1182 * return -1 if the parser fail. The err message is filled.
1183 * return -2 if out of memory
1184 */
1185int pattern_register(struct pattern_expr *expr, const char *arg,
1186 struct sample_storage *smp,
1187 int patflags, char **err)
1188{
1189 int ret;
1190 struct pattern pattern;
1191
1192 /* initialise pattern */
1193 memset(&pattern, 0, sizeof(pattern));
1194 pattern.flags = patflags;
1195 pattern.smp = smp;
1196
1197 /* parse pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001198 ret = expr->parse(arg, &pattern, err);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001199 if (!ret)
1200 return 0;
1201
1202 /* index pattern */
1203 if (!expr->index(expr, &pattern, err))
1204 return 0;
1205
1206 return 1;
1207}
1208
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001209/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1210 * be returned there on errors and the caller will have to free it.
1211 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01001212int pattern_read_from_file(struct pattern_expr *expr,
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001213 const char *filename, int patflags,
1214 char **err)
1215{
1216 FILE *file;
1217 char *c;
1218 char *arg;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001219 int ret = 0;
1220 int line = 0;
1221 int code;
1222
1223 file = fopen(filename, "r");
1224 if (!file) {
1225 memprintf(err, "failed to open pattern file <%s>", filename);
1226 return 0;
1227 }
1228
1229 /* now parse all patterns. The file may contain only one pattern per
1230 * line. If the line contains spaces, they will be part of the pattern.
1231 * The pattern stops at the first CR, LF or EOF encountered.
1232 */
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001233 while (fgets(trash.str, trash.size, file) != NULL) {
1234 line++;
1235 c = trash.str;
1236
1237 /* ignore lines beginning with a dash */
1238 if (*c == '#')
1239 continue;
1240
1241 /* strip leading spaces and tabs */
1242 while (*c == ' ' || *c == '\t')
1243 c++;
1244
1245
1246 arg = c;
1247 while (*c && *c != '\n' && *c != '\r')
1248 c++;
1249 *c = 0;
1250
1251 /* empty lines are ignored too */
1252 if (c == arg)
1253 continue;
1254
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001255 code = pattern_register(expr, arg, NULL, patflags, err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001256 if (code == -2) {
1257 memprintf(err, "out of memory when loading patterns from file <%s>", filename);
1258 goto out_close;
1259 }
1260 else if (code < 0) {
1261 memprintf(err, "%s when loading patterns from file <%s>", *err, filename);
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001262 goto out_close;
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001263 }
1264 }
1265
1266 ret = 1; /* success */
1267
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001268 out_close:
1269 fclose(file);
1270 return ret;
1271}
1272
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001273/* This function executes a pattern match on a sample. It applies pattern <expr>
1274 * to sample <smp>. The function returns NULL if the sample dont match. It returns
1275 * non-null if the sample match. If <fill> is true and the sample match, the
1276 * function returns the matched pattern. In many cases, this pattern can be a
1277 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001278 */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001279struct pattern *pattern_exec_match(struct pattern_expr *expr, struct sample *smp, int fill)
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001280{
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001281 if (!expr->match) {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001282 if (fill) {
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001283 static_pattern.smp = NULL;
1284 static_pattern.flags = 0;
1285 static_pattern.type = SMP_T_UINT;
1286 static_pattern.val.i = 1;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001287 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01001288 return &static_pattern;
1289 }
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01001290 return expr->match(smp, expr, fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001291}
1292
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001293/* This function browse the pattern expr <expr> to lookup the key <key>. On
1294 * error it returns 0. On success, it returns 1 and fills either <pat_elt>
1295 * or <idx_elt> with the respectively matched pointers, and the other one with
1296 * NULL. Pointers are not set if they're passed as NULL.
1297 */
1298int pattern_lookup(const char *key, struct pattern_expr *expr,
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001299 struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001300{
1301 struct pattern pattern;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001302 struct pattern_list *pat;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001303 struct ebmb_node *node;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001304 struct pattern_tree *elt;
Willy Tarreau668ae532013-12-15 16:42:26 +01001305 unsigned int mask = 0;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001306
1307 /* no real pattern */
1308 if (!expr->match || expr->match == pat_match_nothing)
1309 return 0;
1310
1311 /* build lookup pattern */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01001312 if (!expr->parse(key, &pattern, NULL))
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001313 return 0;
1314
1315 pat = NULL;
1316 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001317
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001318 /* Try to look up the tree first. IPv6 is not indexed */
1319 if (!eb_is_empty(&expr->pattern_tree) && pattern.type != SMP_T_IPV6) {
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001320 /* Check the pattern type */
1321 if (pattern.type != SMP_T_STR &&
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001322 pattern.type != SMP_T_IPV4) {
1323 memprintf(err, "Unexpected pattern type.");
1324 return 0;
1325 }
1326
1327 /* Convert mask. If the mask is not contiguous, ignore the lookup
1328 * in the tree, and browse the list.
1329 */
1330 if (expr->match == pat_match_ip) {
1331 mask = ntohl(pattern.val.ipv4.mask.s_addr);
1332 if (mask + (mask & -mask) != 0)
1333 goto browse_list;
1334 mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1335 }
1336
1337 /* browse each node of the tree, and check string */
1338 if (expr->match == pat_match_str) {
1339 for (node = ebmb_first(&expr->pattern_tree);
1340 node;
1341 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001342 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001343 if (strcmp(pattern.ptr.str, (char *)elt->node.key) == 0)
1344 goto found;
1345 }
1346 }
1347 else if (expr->match == pat_match_ip) {
1348 for (node = ebmb_first(&expr->pattern_tree);
1349 node;
1350 node = ebmb_next(node)) {
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001351 elt = container_of(node, struct pattern_tree, node);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001352 if (elt->node.node.pfx == mask &&
1353 memcmp(&pattern.val.ipv4.addr.s_addr, elt->node.key, 4) == 0)
1354 goto found;
1355 }
1356 }
1357 }
1358
1359browse_list:
Thierry FOURNIERc64de3f2013-12-10 15:08:39 +01001360 elt = NULL;
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001361 if (expr->parse == pat_parse_int ||
1362 expr->parse == pat_parse_len) {
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 (pattern.val.range.min_set != pat->pat.val.range.min_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001367 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001368 if (pattern.val.range.max_set != pat->pat.val.range.max_set)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001369 continue;
1370 if (pattern.val.range.min_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001371 pattern.val.range.min != pat->pat.val.range.min)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001372 continue;
1373 if (pattern.val.range.max_set &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001374 pattern.val.range.max != pat->pat.val.range.max)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001375 continue;
1376 goto found;
1377 }
1378 }
1379 else if (expr->parse == pat_parse_ip) {
1380 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001381 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001382 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001383 if (pattern.type != pat->pat.type)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001384 continue;
1385 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001386 memcmp(&pattern.val.ipv4.addr, &pat->pat.val.ipv4.addr, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001387 continue;
1388 if (pattern.type == SMP_T_IPV4 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001389 memcmp(&pattern.val.ipv4.mask, &pat->pat.val.ipv4.mask, sizeof(pat->pat.val.ipv4.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001390 continue;
1391 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001392 memcmp(&pattern.val.ipv6.addr, &pat->pat.val.ipv6.addr, sizeof(pat->pat.val.ipv6.addr)) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001393 continue;
1394 if (pattern.type == SMP_T_IPV6 &&
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001395 pattern.val.ipv6.mask != pat->pat.val.ipv6.mask)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001396 continue;
1397 goto found;
1398 }
1399 }
1400 else if (expr->parse == pat_parse_str) {
1401 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001402 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001403 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001404 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001405 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001406 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1407 if (strncasecmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001408 continue;
1409 }
1410 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001411 if (strncmp(pattern.ptr.str, pat->pat.ptr.str, pat->pat.len) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001412 continue;
1413 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001414 goto found;
1415 }
1416 }
1417 else if (expr->parse == pat_parse_bin) {
1418 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001419 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001420 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001421 if (pattern.len != pat->pat.len)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001422 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001423 if (memcmp(pattern.ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001424 continue;
1425 goto found;
1426 }
1427 }
1428 else if (expr->parse == pat_parse_reg) {
1429 list_for_each_entry(pat, &expr->patterns, list) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001430 if (pat->pat.flags & PAT_F_TREE)
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001431 continue;
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001432 if (pat->pat.flags & PAT_F_IGNORE_CASE) {
1433 if (strcasecmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001434 continue;
1435 }
1436 else {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001437 if (strcmp(pattern.ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0)
Thierry FOURNIER35249cb2014-01-14 13:38:40 +01001438 continue;
1439 }
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +01001440 goto found;
1441 }
1442 }
1443
1444 /* if we get there, we didn't find the pattern */
1445 return 0;
1446found:
1447 if (idx_elt)
1448 *idx_elt = elt;
1449
1450 if (pat_elt)
1451 *pat_elt = pat;
1452
1453 return 1;
1454}