14
#include <arpa/inet.h>
15
#include <types/stick_table.h>
16
#include <common/config.h>
17
#include <common/standard.h>
19
#include <types/global.h>
20
#include <types/pattern.h>
22
#include <proto/log.h>
17
23
#include <proto/pattern.h>
18
#include <common/standard.h>
20
/* static structure used on pattern_process if <p> is NULL*/
21
static struct pattern spattern;
23
/* trash chunk used for pattern conversions */
24
static struct chunk trash_chunk;
26
/* trash buffers used or pattern conversions */
27
static char pattern_trash_buf1[BUFSIZE];
28
static char pattern_trash_buf2[BUFSIZE];
30
/* pattern_trash_buf point on used buffer*/
31
static char *pattern_trash_buf = pattern_trash_buf1;
33
/* static structure used to returns builded table key from a pattern*/
34
static struct stktable_key stable_key;
36
/* list head of all known pattern fetch keywords */
37
static struct pattern_fetch_kw_list pattern_fetches = {
38
.list = LIST_HEAD_INIT(pattern_fetches.list)
41
/* list head of all known pattern format conversion keywords */
42
static struct pattern_conv_kw_list pattern_convs = {
43
.list = LIST_HEAD_INIT(pattern_convs.list)
47
* Registers the pattern fetch keyword list <kwl> as a list of valid keywords for next
50
void pattern_register_fetches(struct pattern_fetch_kw_list *pfkl)
52
LIST_ADDQ(&pattern_fetches.list, &pfkl->list);
56
* Registers the pattern format coverstion keyword list <pckl> as a list of valid keywords for next
59
void pattern_register_convs(struct pattern_conv_kw_list *pckl)
61
LIST_ADDQ(&pattern_convs.list, &pckl->list);
65
* Returns the pointer on pattern fetch keyword structure identified by
66
* string of <len> in buffer <kw>.
69
struct pattern_fetch *find_pattern_fetch(const char *kw, int len)
72
struct pattern_fetch_kw_list *kwl;
74
list_for_each_entry(kwl, &pattern_fetches.list, list) {
75
for (index = 0; kwl->kw[index].kw != NULL; index++) {
76
if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
77
kwl->kw[index].kw[len] == '\0')
78
return &kwl->kw[index];
85
* Returns the pointer on pattern format conversion keyword structure identified by
86
* string of <len> in buffer <kw>.
89
struct pattern_conv *find_pattern_conv(const char *kw, int len)
92
struct pattern_conv_kw_list *kwl;
94
list_for_each_entry(kwl, &pattern_convs.list, list) {
95
for (index = 0; kwl->kw[index].kw != NULL; index++) {
96
if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
97
kwl->kw[index].kw[len] == '\0')
98
return &kwl->kw[index];
106
* Returns a static trash struct chunk to use in pattern casts or format conversions
107
* Swiths the 2 available trash buffers to protect data during convert
109
static struct chunk *get_trash_chunk(void)
111
if (pattern_trash_buf == pattern_trash_buf1)
112
pattern_trash_buf = pattern_trash_buf2;
114
pattern_trash_buf = pattern_trash_buf1;
116
trash_chunk.str = pattern_trash_buf;
118
trash_chunk.size = BUFSIZE;
124
* Used to set pattern data from a struct chunk, could be the trash struct chunk
126
static void pattern_data_setstring(union pattern_data *data, struct chunk *c)
128
data->str.str = c->str;
129
data->str.len = c->len;
130
data->str.size = c->size;
133
/******************************************************************/
134
/* Pattern casts functions */
135
/******************************************************************/
137
static int c_ip2int(union pattern_data *data)
139
data->integer = ntohl(data->ip.s_addr);
143
static int c_ip2str(union pattern_data *data)
145
struct chunk *trash = get_trash_chunk();
147
if (!inet_ntop(AF_INET, (void *)&data->ip, trash->str, trash->size))
150
trash->len = strlen(trash->str);
151
pattern_data_setstring(data, trash);
156
static int c_int2ip(union pattern_data *data)
158
data->ip.s_addr = htonl(data->integer);
162
/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
163
* or the number of chars read in case of success.
165
static int buf2ip(const char *buf, size_t len, struct in_addr *dst)
168
int saw_digit, octets, ch;
170
const char *cp = buf;
176
for (addr = buf; addr - buf < len; addr++) {
177
unsigned char digit = (ch = *addr) - '0';
179
if (digit > 9 && ch != '.')
183
u_int new = *tp * 10 + digit;
195
} else if (ch == '.' && saw_digit) {
208
memcpy(&dst->s_addr, tmp, 4);
212
static int c_str2ip(union pattern_data *data)
214
if (!buf2ip(data->str.str, data->str.len, &data->ip))
219
static int c_int2str(union pattern_data *data)
221
struct chunk *trash = get_trash_chunk();
224
pos = ultoa_r(data->integer, trash->str, trash->size);
230
trash->len = strlen(pos);
232
pattern_data_setstring(data, trash);
237
static int c_donothing(union pattern_data *data)
242
static int c_str2int(union pattern_data *data)
247
for (i = 0; i < data->str.len; i++) {
248
uint32_t val = data->str.str[i] - '0';
253
ret = ret * 10 + val;
260
/*****************************************************************/
261
/* Pattern casts matrix: */
262
/* pattern_casts[from type][to type] */
263
/* NULL pointer used for impossible pattern casts */
264
/*****************************************************************/
266
typedef int (*pattern_cast)(union pattern_data *data);
267
static pattern_cast pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = { { c_donothing, c_ip2int, c_ip2str },
268
{ c_int2ip, c_donothing, c_int2str },
269
{ c_str2ip, c_str2int, c_donothing } };
272
/*****************************************************************/
273
/* typed pattern to typed table key functions */
274
/*****************************************************************/
276
static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
278
return (void *)&pdata->integer;
281
static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
283
return (void *)&pdata->ip.s_addr;
286
static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
288
kdata->integer = ntohl(pdata->ip.s_addr);
289
return (void *)&kdata->integer;
292
static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
294
kdata->ip.s_addr = htonl(pdata->integer);
295
return (void *)&kdata->ip.s_addr;
298
static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
300
*len = pdata->str.len;
301
return (void *)pdata->str.str;
304
static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
306
if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
309
*len = strlen((const char *)kdata->buf);
310
return (void *)kdata->buf;
313
static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
317
key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf));
321
*len = strlen((const char *)key);
325
static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
327
if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
330
return (void *)&kdata->ip.s_addr;
334
static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
339
for (i = 0; i < pdata->str.len; i++) {
340
uint32_t val = pdata->str.str[i] - '0';
345
kdata->integer = kdata->integer * 10 + val;
347
return (void *)&kdata->integer;
350
/*****************************************************************/
351
/* typed pattern to typed table key matrix: */
352
/* pattern_keys[from pattern type][to table key type] */
353
/* NULL pointer used for impossible pattern casts */
354
/*****************************************************************/
356
typedef void *(*pattern_key)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
357
static pattern_key pattern_keys[PATTERN_TYPES][STKTABLE_TYPES] = { { k_ip2ip, k_ip2int, k_ip2str },
358
{ k_int2ip, k_int2int, k_int2str },
359
{ k_str2ip, k_str2int, k_str2str } };
361
* Parse a pattern expression configuration:
362
* fetch keyword followed by format conversion keywords.
363
* Returns a pointer on allocated pattern expression structure.
365
struct pattern_expr *pattern_parse_expr(char **str, int *idx)
369
struct pattern_expr *expr;
370
struct pattern_fetch *fetch;
371
struct pattern_conv *conv;
372
unsigned long prev_type;
377
end = str[*idx] + strlen(str[*idx]);
378
endw = strchr(str[*idx], '(');
382
else if ((end-1)[0] != ')')
385
fetch = find_pattern_fetch(str[*idx], endw - str[*idx]);
389
if (fetch->out_type >= PATTERN_TYPES)
392
prev_type = fetch->out_type;
393
expr = calloc(1, sizeof(struct pattern_expr));
395
LIST_INIT(&(expr->conv_exprs));
399
expr->arg_len = end - endw - 2;
400
expr->arg = my_strndup(endw + 1, expr->arg_len);
403
for (*idx += 1; *(str[*idx]); (*idx)++) {
404
struct pattern_conv_expr *conv_expr;
406
end = str[*idx] + strlen(str[*idx]);
407
endw = strchr(str[*idx], '(');
411
else if ((end-1)[0] != ')')
414
conv = find_pattern_conv(str[*idx], endw - str[*idx]);
418
if (conv->in_type >= PATTERN_TYPES ||
419
conv->out_type >= PATTERN_TYPES)
422
/* If impossible type conversion */
423
if (!pattern_casts[prev_type][conv->in_type])
426
prev_type = conv->out_type;
427
conv_expr = calloc(1, sizeof(struct pattern_conv_expr));
429
LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
430
conv_expr->conv = conv;
433
int i = end - endw - 2;
434
char *p = my_strndup(endw + 1, i);
436
if (conv->parse_args) {
437
i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i);
442
conv_expr->arg_i = i;
443
conv_expr->arg_p = p;
24
#include <proto/sample.h>
28
char *pat_match_names[PAT_MATCH_NUM] = {
29
[PAT_MATCH_FOUND] = "found",
30
[PAT_MATCH_BOOL] = "bool",
31
[PAT_MATCH_INT] = "int",
32
[PAT_MATCH_IP] = "ip",
33
[PAT_MATCH_BIN] = "bin",
34
[PAT_MATCH_LEN] = "len",
35
[PAT_MATCH_STR] = "str",
36
[PAT_MATCH_BEG] = "beg",
37
[PAT_MATCH_SUB] = "sub",
38
[PAT_MATCH_DIR] = "dir",
39
[PAT_MATCH_DOM] = "dom",
40
[PAT_MATCH_END] = "end",
41
[PAT_MATCH_REG] = "reg",
44
int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, int, char **) = {
45
[PAT_MATCH_FOUND] = pat_parse_nothing,
46
[PAT_MATCH_BOOL] = pat_parse_nothing,
47
[PAT_MATCH_INT] = pat_parse_int,
48
[PAT_MATCH_IP] = pat_parse_ip,
49
[PAT_MATCH_BIN] = pat_parse_bin,
50
[PAT_MATCH_LEN] = pat_parse_int,
51
[PAT_MATCH_STR] = pat_parse_str,
52
[PAT_MATCH_BEG] = pat_parse_str,
53
[PAT_MATCH_SUB] = pat_parse_str,
54
[PAT_MATCH_DIR] = pat_parse_str,
55
[PAT_MATCH_DOM] = pat_parse_str,
56
[PAT_MATCH_END] = pat_parse_str,
57
[PAT_MATCH_REG] = pat_parse_reg,
60
int (*pat_index_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *, char **) = {
61
[PAT_MATCH_FOUND] = pat_idx_list_val,
62
[PAT_MATCH_BOOL] = pat_idx_list_val,
63
[PAT_MATCH_INT] = pat_idx_list_val,
64
[PAT_MATCH_IP] = pat_idx_tree_ip,
65
[PAT_MATCH_BIN] = pat_idx_list_ptr,
66
[PAT_MATCH_LEN] = pat_idx_list_val,
67
[PAT_MATCH_STR] = pat_idx_tree_str,
68
[PAT_MATCH_BEG] = pat_idx_tree_pfx,
69
[PAT_MATCH_SUB] = pat_idx_list_str,
70
[PAT_MATCH_DIR] = pat_idx_list_str,
71
[PAT_MATCH_DOM] = pat_idx_list_str,
72
[PAT_MATCH_END] = pat_idx_list_str,
73
[PAT_MATCH_REG] = pat_idx_list_reg,
76
void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pat_ref_elt *) = {
77
[PAT_MATCH_FOUND] = pat_del_list_val,
78
[PAT_MATCH_BOOL] = pat_del_list_val,
79
[PAT_MATCH_INT] = pat_del_list_val,
80
[PAT_MATCH_IP] = pat_del_tree_ip,
81
[PAT_MATCH_BIN] = pat_del_list_ptr,
82
[PAT_MATCH_LEN] = pat_del_list_val,
83
[PAT_MATCH_STR] = pat_del_tree_str,
84
[PAT_MATCH_BEG] = pat_del_tree_str,
85
[PAT_MATCH_SUB] = pat_del_list_ptr,
86
[PAT_MATCH_DIR] = pat_del_list_ptr,
87
[PAT_MATCH_DOM] = pat_del_list_ptr,
88
[PAT_MATCH_END] = pat_del_list_ptr,
89
[PAT_MATCH_REG] = pat_del_list_reg,
92
void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = {
93
[PAT_MATCH_FOUND] = pat_prune_val,
94
[PAT_MATCH_BOOL] = pat_prune_val,
95
[PAT_MATCH_INT] = pat_prune_val,
96
[PAT_MATCH_IP] = pat_prune_val,
97
[PAT_MATCH_BIN] = pat_prune_ptr,
98
[PAT_MATCH_LEN] = pat_prune_val,
99
[PAT_MATCH_STR] = pat_prune_ptr,
100
[PAT_MATCH_BEG] = pat_prune_ptr,
101
[PAT_MATCH_SUB] = pat_prune_ptr,
102
[PAT_MATCH_DIR] = pat_prune_ptr,
103
[PAT_MATCH_DOM] = pat_prune_ptr,
104
[PAT_MATCH_END] = pat_prune_ptr,
105
[PAT_MATCH_REG] = pat_prune_reg,
108
struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = {
109
[PAT_MATCH_FOUND] = NULL,
110
[PAT_MATCH_BOOL] = pat_match_nothing,
111
[PAT_MATCH_INT] = pat_match_int,
112
[PAT_MATCH_IP] = pat_match_ip,
113
[PAT_MATCH_BIN] = pat_match_bin,
114
[PAT_MATCH_LEN] = pat_match_len,
115
[PAT_MATCH_STR] = pat_match_str,
116
[PAT_MATCH_BEG] = pat_match_beg,
117
[PAT_MATCH_SUB] = pat_match_sub,
118
[PAT_MATCH_DIR] = pat_match_dir,
119
[PAT_MATCH_DOM] = pat_match_dom,
120
[PAT_MATCH_END] = pat_match_end,
121
[PAT_MATCH_REG] = pat_match_reg,
124
/* Just used for checking configuration compatibility */
125
int pat_match_types[PAT_MATCH_NUM] = {
126
[PAT_MATCH_FOUND] = SMP_T_UINT,
127
[PAT_MATCH_BOOL] = SMP_T_UINT,
128
[PAT_MATCH_INT] = SMP_T_UINT,
129
[PAT_MATCH_IP] = SMP_T_ADDR,
130
[PAT_MATCH_BIN] = SMP_T_BIN,
131
[PAT_MATCH_LEN] = SMP_T_STR,
132
[PAT_MATCH_STR] = SMP_T_STR,
133
[PAT_MATCH_BEG] = SMP_T_STR,
134
[PAT_MATCH_SUB] = SMP_T_STR,
135
[PAT_MATCH_DIR] = SMP_T_STR,
136
[PAT_MATCH_DOM] = SMP_T_STR,
137
[PAT_MATCH_END] = SMP_T_STR,
138
[PAT_MATCH_REG] = SMP_T_STR,
141
/* this struct is used to return information */
142
static struct pattern static_pattern;
144
/* This is the root of the list of all pattern_ref avalaibles. */
145
struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
149
* The following functions are not exported and are used by internals process
150
* of pattern matching
154
/* Background: Fast way to find a zero byte in a word
155
* http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
156
* hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL;
158
* To look for 4 different byte values, xor the word with those bytes and
159
* then check for zero bytes:
161
* v = (((unsigned char)c * 0x1010101U) ^ delimiter)
162
* where <delimiter> is the 4 byte values to look for (as an uint)
163
* and <c> is the character that is being tested
165
static inline unsigned int is_delimiter(unsigned char c, unsigned int mask)
167
mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */
168
return (mask - 0x01010101) & ~mask & 0x80808080U;
171
static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4)
173
return d1 << 24 | d2 << 16 | d3 << 8 | d4;
179
* These functions are exported and may be used by any other component.
181
* The following functions are used for parsing pattern matching
182
* input value. The <text> contain the string to be parsed. <pattern>
183
* must be a preallocated pattern. The pat_parse_* functions fill this
184
* structure with the parsed value. <usage> can be PAT_U_COMPILE or
185
* PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated
186
* for filling the pattern. If the value PAT_U_LOOKUP is set, the parser
187
* use "trash" or return pointers to the input strings. In both cases,
188
* the caller must use the value PAT_U_LOOKUP with caution. <err> is
189
* filled with an error message built with memprintf() function.
191
* In succes case, the pat_parse_* function return 1. If the function
192
* fail, it returns 0 and <err> is filled.
196
/* ignore the current line */
197
int pat_parse_nothing(const char *text, struct pattern *pattern, int mflags, char **err)
202
/* Parse a string. It is allocated and duplicated. */
203
int pat_parse_str(const char *text, struct pattern *pattern, int mflags, char **err)
205
pattern->type = SMP_T_STR;
206
pattern->ptr.str = (char *)text;
207
pattern->len = strlen(text);
211
/* Parse a binary written in hexa. It is allocated. */
212
int pat_parse_bin(const char *text, struct pattern *pattern, int mflags, char **err)
216
pattern->type = SMP_T_BIN;
217
trash = get_trash_chunk();
218
pattern->len = trash->size;
219
pattern->ptr.str = trash->str;
220
return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err);
223
/* Parse a regex. It is allocated. */
224
int pat_parse_reg(const char *text, struct pattern *pattern, int mflags, char **err)
228
trash = get_trash_chunk();
229
if (trash->size < sizeof(*pattern->ptr.reg)) {
230
memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
231
(int)sizeof(*pattern->ptr.reg), trash->size);
235
pattern->ptr.str = (char *)text;
240
/* Parse a range of positive integers delimited by either ':' or '-'. If only
241
* one integer is read, it is set as both min and max. An operator may be
242
* specified as the prefix, among this list of 5 :
244
* 0:eq, 1:gt, 2:ge, 3:lt, 4:le
246
* The default operator is "eq". It supports range matching. Ranges are
247
* rejected for other operators. The operator may be changed at any time.
248
* The operator is stored in the 'opaque' argument.
250
* If err is non-NULL, an error message will be returned there on errors and
251
* the caller will have to free it. The function returns zero on error, and
252
* non-zero on success.
255
int pat_parse_int(const char *text, struct pattern *pattern, int mflags, char **err)
257
const char *ptr = text;
259
pattern->type = SMP_T_UINT;
261
/* Empty string is not valid */
263
goto not_valid_range;
265
/* Search ':' or '-' separator. */
266
while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
269
/* If separator not found. */
271
if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) {
272
memprintf(err, "'%s' is not a number", text);
275
pattern->val.range.max = pattern->val.range.min;
276
pattern->val.range.min_set = 1;
277
pattern->val.range.max_set = 1;
281
/* If the separator is the first character. */
282
if (ptr == text && *(ptr + 1) != '\0') {
283
if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
284
goto not_valid_range;
286
pattern->val.range.min_set = 0;
287
pattern->val.range.max_set = 1;
291
/* If separator is the last character. */
292
if (*(ptr + 1) == '\0') {
293
if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
294
goto not_valid_range;
296
pattern->val.range.min_set = 1;
297
pattern->val.range.max_set = 0;
301
/* Else, parse two numbers. */
302
if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0)
303
goto not_valid_range;
305
if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0)
306
goto not_valid_range;
308
if (pattern->val.range.min > pattern->val.range.max)
309
goto not_valid_range;
311
pattern->val.range.min_set = 1;
312
pattern->val.range.max_set = 1;
316
memprintf(err, "'%s' is not a valid number range", text);
320
/* Parse a range of positive 2-component versions delimited by either ':' or
321
* '-'. The version consists in a major and a minor, both of which must be
322
* smaller than 65536, because internally they will be represented as a 32-bit
324
* If only one version is read, it is set as both min and max. Just like for
325
* pure integers, an operator may be specified as the prefix, among this list
328
* 0:eq, 1:gt, 2:ge, 3:lt, 4:le
330
* The default operator is "eq". It supports range matching. Ranges are
331
* rejected for other operators. The operator may be changed at any time.
332
* The operator is stored in the 'opaque' argument. This allows constructs
333
* such as the following one :
335
* acl obsolete_ssl ssl_req_proto lt 3
336
* acl unsupported_ssl ssl_req_proto gt 3.1
337
* acl valid_ssl ssl_req_proto 3.0-3.1
340
int pat_parse_dotted_ver(const char *text, struct pattern *pattern, int mflags, char **err)
342
const char *ptr = text;
344
pattern->type = SMP_T_UINT;
346
/* Search ':' or '-' separator. */
347
while (*ptr != '\0' && *ptr != ':' && *ptr != '-')
350
/* If separator not found. */
351
if (*ptr == '\0' && ptr > text) {
352
if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
353
memprintf(err, "'%s' is not a dotted number", text);
356
pattern->val.range.max = pattern->val.range.min;
357
pattern->val.range.min_set = 1;
358
pattern->val.range.max_set = 1;
362
/* If the separator is the first character. */
363
if (ptr == text && *(ptr+1) != '\0') {
364
if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
365
memprintf(err, "'%s' is not a valid dotted number range", text);
368
pattern->val.range.min_set = 0;
369
pattern->val.range.max_set = 1;
373
/* If separator is the last character. */
374
if (ptr == &text[strlen(text)-1]) {
375
if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
376
memprintf(err, "'%s' is not a valid dotted number range", text);
379
pattern->val.range.min_set = 1;
380
pattern->val.range.max_set = 0;
384
/* Else, parse two numbers. */
385
if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) {
386
memprintf(err, "'%s' is not a valid dotted number range", text);
389
if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) {
390
memprintf(err, "'%s' is not a valid dotted number range", text);
393
if (pattern->val.range.min > pattern->val.range.max) {
394
memprintf(err, "'%s' is not a valid dotted number range", text);
397
pattern->val.range.min_set = 1;
398
pattern->val.range.max_set = 1;
402
/* Parse an IP address and an optional mask in the form addr[/mask].
403
* The addr may either be an IPv4 address or a hostname. The mask
404
* may either be a dotted mask or a number of bits. Returns 1 if OK,
405
* otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6).
407
int pat_parse_ip(const char *text, struct pattern *pattern, int mflags, char **err)
409
if (str2net(text, !(mflags & PAT_MF_NO_DNS) && (global.mode & MODE_STARTING),
410
&pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) {
411
pattern->type = SMP_T_IPV4;
414
else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) {
415
pattern->type = SMP_T_IPV6;
419
memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text);
426
* These functions are exported and may be used by any other component.
428
* This fucntion just take a sample <smp> and check if this sample match
429
* with the pattern <pattern>. This fucntion return just PAT_MATCH or
434
/* always return false */
435
struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill)
437
if (smp->data.uint) {
439
static_pattern.smp = NULL;
440
static_pattern.ref = NULL;
441
static_pattern.type = 0;
442
static_pattern.ptr.str = NULL;
444
return &static_pattern;
451
/* NB: For two strings to be identical, it is required that their lengths match */
452
struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill)
455
struct ebmb_node *node;
457
struct pattern_tree *elt;
458
struct pattern_list *lst;
459
struct pattern *pattern;
461
/* Lookup a string in the expression's pattern tree. */
462
if (!eb_is_empty(&expr->pattern_tree)) {
463
/* we may have to force a trailing zero on the test pattern */
464
prev = smp->data.str.str[smp->data.str.len];
466
smp->data.str.str[smp->data.str.len] = '\0';
467
node = ebst_lookup(&expr->pattern_tree, smp->data.str.str);
469
smp->data.str.str[smp->data.str.len] = prev;
473
elt = ebmb_entry(node, struct pattern_tree, node);
474
static_pattern.smp = elt->smp;
475
static_pattern.ref = elt->ref;
476
static_pattern.sflags = PAT_SF_TREE;
477
static_pattern.type = SMP_T_STR;
478
static_pattern.ptr.str = (char *)elt->node.key;
480
return &static_pattern;
484
/* look in the list */
485
list_for_each_entry(lst, &expr->patterns, list) {
488
if (pattern->len != smp->data.str.len)
491
icase = expr->mflags & PAT_MF_IGNORE_CASE;
492
if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) ||
493
(!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0))
500
/* NB: For two binaries buf to be identical, it is required that their lengths match */
501
struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill)
503
struct pattern_list *lst;
504
struct pattern *pattern;
506
/* Look in the list. */
507
list_for_each_entry(lst, &expr->patterns, list) {
510
if (pattern->len != smp->data.str.len)
513
if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
520
/* Executes a regex. It temporarily changes the data to add a trailing zero,
521
* and restores the previous character when leaving.
523
struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill)
525
struct pattern_list *lst;
526
struct pattern *pattern;
528
/* look in the list */
529
list_for_each_entry(lst, &expr->patterns, list) {
532
if (regex_exec2(pattern->ptr.reg, smp->data.str.str, smp->data.str.len))
538
/* Checks that the pattern matches the beginning of the tested string. */
539
struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill)
542
struct ebmb_node *node;
544
struct pattern_tree *elt;
545
struct pattern_list *lst;
546
struct pattern *pattern;
548
/* Lookup a string in the expression's pattern tree. */
549
if (!eb_is_empty(&expr->pattern_tree)) {
550
/* we may have to force a trailing zero on the test pattern */
551
prev = smp->data.str.str[smp->data.str.len];
553
smp->data.str.str[smp->data.str.len] = '\0';
554
node = ebmb_lookup_longest(&expr->pattern_tree, smp->data.str.str);
556
smp->data.str.str[smp->data.str.len] = prev;
560
elt = ebmb_entry(node, struct pattern_tree, node);
561
static_pattern.smp = elt->smp;
562
static_pattern.ref = elt->ref;
563
static_pattern.sflags = PAT_SF_TREE;
564
static_pattern.type = SMP_T_STR;
565
static_pattern.ptr.str = (char *)elt->node.key;
567
return &static_pattern;
571
/* look in the list */
572
list_for_each_entry(lst, &expr->patterns, list) {
575
if (pattern->len > smp->data.str.len)
578
icase = expr->mflags & PAT_MF_IGNORE_CASE;
579
if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) ||
580
(!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0))
588
/* Checks that the pattern matches the end of the tested string. */
589
struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill)
592
struct pattern_list *lst;
593
struct pattern *pattern;
595
list_for_each_entry(lst, &expr->patterns, list) {
598
if (pattern->len > smp->data.str.len)
601
icase = expr->mflags & PAT_MF_IGNORE_CASE;
602
if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) ||
603
(!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0))
611
/* Checks that the pattern is included inside the tested string.
612
* NB: Suboptimal, should be rewritten using a Boyer-Moore method.
614
struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill)
619
struct pattern_list *lst;
620
struct pattern *pattern;
622
list_for_each_entry(lst, &expr->patterns, list) {
625
if (pattern->len > smp->data.str.len)
628
end = smp->data.str.str + smp->data.str.len - pattern->len;
629
icase = expr->mflags & PAT_MF_IGNORE_CASE;
631
for (c = smp->data.str.str; c <= end; c++) {
632
if (tolower(*c) != tolower(*pattern->ptr.str))
634
if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0)
638
for (c = smp->data.str.str; c <= end; c++) {
639
if (*c != *pattern->ptr.str)
641
if (strncmp(pattern->ptr.str, c, pattern->len) == 0)
649
/* This one is used by other real functions. It checks that the pattern is
650
* included inside the tested string, but enclosed between the specified
651
* delimiters or at the beginning or end of the string. The delimiters are
652
* provided as an unsigned int made by make_4delim() and match up to 4 different
653
* delimiters. Delimiters are stripped at the beginning and end of the pattern.
655
static int match_word(struct sample *smp, struct pattern *pattern, int mflags, unsigned int delimiters)
657
int may_match, icase;
663
ps = pattern->ptr.str;
665
while (pl > 0 && is_delimiter(*ps, delimiters)) {
670
while (pl > 0 && is_delimiter(ps[pl - 1], delimiters))
673
if (pl > smp->data.str.len)
677
icase = mflags & PAT_MF_IGNORE_CASE;
678
end = smp->data.str.str + smp->data.str.len - pl;
679
for (c = smp->data.str.str; c <= end; c++) {
680
if (is_delimiter(*c, delimiters)) {
689
if ((tolower(*c) == tolower(*ps)) &&
690
(strncasecmp(ps, c, pl) == 0) &&
691
(c == end || is_delimiter(c[pl], delimiters)))
695
(strncmp(ps, c, pl) == 0) &&
696
(c == end || is_delimiter(c[pl], delimiters)))
704
/* Checks that the pattern is included inside the tested string, but enclosed
705
* between the delimiters '?' or '/' or at the beginning or end of the string.
706
* Delimiters at the beginning or end of the pattern are ignored.
708
struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill)
710
struct pattern_list *lst;
711
struct pattern *pattern;
713
list_for_each_entry(lst, &expr->patterns, list) {
715
if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '?', '?')))
721
/* Checks that the pattern is included inside the tested string, but enclosed
722
* between the delmiters '/', '?', '.' or ":" or at the beginning or end of
723
* the string. Delimiters at the beginning or end of the pattern are ignored.
725
struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill)
727
struct pattern_list *lst;
728
struct pattern *pattern;
730
list_for_each_entry(lst, &expr->patterns, list) {
732
if (match_word(smp, pattern, expr->mflags, make_4delim('/', '?', '.', ':')))
738
/* Checks that the integer in <test> is included between min and max */
739
struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill)
741
struct pattern_list *lst;
742
struct pattern *pattern;
744
list_for_each_entry(lst, &expr->patterns, list) {
746
if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) &&
747
(!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max))
753
/* Checks that the length of the pattern in <test> is included between min and max */
754
struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill)
756
struct pattern_list *lst;
757
struct pattern *pattern;
759
list_for_each_entry(lst, &expr->patterns, list) {
761
if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) &&
762
(!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max))
768
struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill)
770
unsigned int v4; /* in network byte order */
771
struct in6_addr tmp6;
773
struct ebmb_node *node;
774
struct pattern_tree *elt;
775
struct pattern_list *lst;
776
struct pattern *pattern;
778
/* The input sample is IPv4. Try to match in the trees. */
779
if (smp->type == SMP_T_IPV4) {
780
/* Lookup an IPv4 address in the expression's pattern tree using
781
* the longest match method.
784
node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr);
787
elt = ebmb_entry(node, struct pattern_tree, node);
788
static_pattern.smp = elt->smp;
789
static_pattern.ref = elt->ref;
790
static_pattern.sflags = PAT_SF_TREE;
791
static_pattern.type = SMP_T_IPV4;
792
memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
793
if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
796
return &static_pattern;
799
/* The IPv4 sample dont match the IPv4 tree. Convert the IPv4
800
* sample address to IPv6 with the mapping method using the ::ffff:
801
* prefix, and try to lookup in the IPv6 tree.
803
memset(&tmp6, 0, 10);
804
*(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff);
805
*(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr;
806
node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6);
809
elt = ebmb_entry(node, struct pattern_tree, node);
810
static_pattern.smp = elt->smp;
811
static_pattern.ref = elt->ref;
812
static_pattern.sflags = PAT_SF_TREE;
813
static_pattern.type = SMP_T_IPV6;
814
memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
815
static_pattern.val.ipv6.mask = elt->node.node.pfx;
817
return &static_pattern;
821
/* The input sample is IPv6. Try to match in the trees. */
822
if (smp->type == SMP_T_IPV6) {
823
/* Lookup an IPv6 address in the expression's pattern tree using
824
* the longest match method.
826
node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6);
829
elt = ebmb_entry(node, struct pattern_tree, node);
830
static_pattern.smp = elt->smp;
831
static_pattern.ref = elt->ref;
832
static_pattern.sflags = PAT_SF_TREE;
833
static_pattern.type = SMP_T_IPV6;
834
memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16);
835
static_pattern.val.ipv6.mask = elt->node.node.pfx;
837
return &static_pattern;
840
/* Try to convert 6 to 4 when the start of the ipv6 address match the
842
* - ::ffff:ip:v4 (ipv4 mapped)
843
* - ::0000:ip:v4 (old ipv4 mapped)
844
* - 2002:ip:v4:: (6to4)
846
if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
847
*(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
848
(*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
849
*(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) ||
850
*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
851
if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0)
852
v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
854
v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
855
ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
857
/* Lookup an IPv4 address in the expression's pattern tree using the longest
860
node = ebmb_lookup_longest(&expr->pattern_tree, &v4);
863
elt = ebmb_entry(node, struct pattern_tree, node);
864
static_pattern.smp = elt->smp;
865
static_pattern.ref = elt->ref;
866
static_pattern.sflags = PAT_SF_TREE;
867
static_pattern.type = SMP_T_IPV4;
868
memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4);
869
if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask))
872
return &static_pattern;
877
/* Lookup in the list. the list contain only IPv4 patterns */
878
list_for_each_entry(lst, &expr->patterns, list) {
881
/* The input sample is IPv4, use it as is. */
882
if (smp->type == SMP_T_IPV4) {
883
v4 = smp->data.ipv4.s_addr;
885
else if (smp->type == SMP_T_IPV6) {
886
/* v4 match on a V6 sample. We want to check at least for
887
* the following forms :
888
* - ::ffff:ip:v4 (ipv4 mapped)
889
* - ::0000:ip:v4 (old ipv4 mapped)
890
* - 2002:ip:v4:: (6to4)
892
if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 &&
893
*(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 &&
894
(*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 ||
895
*(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) {
896
v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12];
898
else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) {
899
v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) +
900
ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4]));
906
/* Check if the input sample match the current pattern. */
907
if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0)
913
void free_pattern_tree(struct eb_root *root)
915
struct eb_node *node, *next;
916
struct pattern_tree *elt;
918
node = eb_first(root);
920
next = eb_next(node);
922
elt = container_of(node, struct pattern_tree, node);
929
void pat_prune_val(struct pattern_expr *expr)
931
struct pattern_list *pat, *tmp;
933
list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
938
free_pattern_tree(&expr->pattern_tree);
939
free_pattern_tree(&expr->pattern_tree_2);
940
LIST_INIT(&expr->patterns);
943
void pat_prune_ptr(struct pattern_expr *expr)
945
struct pattern_list *pat, *tmp;
947
list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
948
free(pat->pat.ptr.ptr);
953
free_pattern_tree(&expr->pattern_tree);
954
free_pattern_tree(&expr->pattern_tree_2);
955
LIST_INIT(&expr->patterns);
958
void pat_prune_reg(struct pattern_expr *expr)
960
struct pattern_list *pat, *tmp;
962
list_for_each_entry_safe(pat, tmp, &expr->patterns, list) {
963
regex_free(pat->pat.ptr.ptr);
968
free_pattern_tree(&expr->pattern_tree);
969
free_pattern_tree(&expr->pattern_tree_2);
970
LIST_INIT(&expr->patterns);
975
* The following functions are used for the pattern indexation
979
int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err)
981
struct pattern_list *patl;
983
/* allocate pattern */
984
patl = calloc(1, sizeof(*patl));
986
memprintf(err, "out of memory while indexing pattern");
990
/* duplicate pattern */
991
memcpy(&patl->pat, pat, sizeof(*pat));
993
/* chain pattern in the expression */
994
LIST_ADDQ(&expr->patterns, &patl->list);
1000
int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err)
1002
struct pattern_list *patl;
1004
/* allocate pattern */
1005
patl = calloc(1, sizeof(*patl));
1009
/* duplicate pattern */
1010
memcpy(&patl->pat, pat, sizeof(*pat));
1011
patl->pat.ptr.ptr = malloc(patl->pat.len);
1012
if (!patl->pat.ptr.ptr) {
1014
memprintf(err, "out of memory while indexing pattern");
1017
memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1019
/* chain pattern in the expression */
1020
LIST_ADDQ(&expr->patterns, &patl->list);
1026
int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1028
struct pattern_list *patl;
1030
/* allocate pattern */
1031
patl = calloc(1, sizeof(*patl));
1033
memprintf(err, "out of memory while indexing pattern");
1037
/* duplicate pattern */
1038
memcpy(&patl->pat, pat, sizeof(*pat));
1039
patl->pat.ptr.str = malloc(patl->pat.len + 1);
1040
if (!patl->pat.ptr.str) {
1042
memprintf(err, "out of memory while indexing pattern");
1045
memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len);
1046
patl->pat.ptr.str[patl->pat.len] = '\0';
1048
/* chain pattern in the expression */
1049
LIST_ADDQ(&expr->patterns, &patl->list);
1055
int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err)
1057
struct pattern_list *patl;
1059
/* allocate pattern */
1060
patl = calloc(1, sizeof(*patl));
1062
memprintf(err, "out of memory while indexing pattern");
1066
/* duplicate pattern */
1067
memcpy(&patl->pat, pat, sizeof(*pat));
1069
/* allocate regex */
1070
patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg));
1071
if (!patl->pat.ptr.reg) {
1073
memprintf(err, "out of memory while indexing pattern");
1078
if (!regex_comp(pat->ptr.str, patl->pat.ptr.reg, !(expr->mflags & PAT_MF_IGNORE_CASE), 0, err)) {
1079
free(patl->pat.ptr.reg);
1084
/* chain pattern in the expression */
1085
LIST_ADDQ(&expr->patterns, &patl->list);
1091
int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err)
1094
struct pattern_tree *node;
1096
/* Only IPv4 can be indexed */
1097
if (pat->type == SMP_T_IPV4) {
1098
/* in IPv4 case, check if the mask is contiguous so that we can
1099
* insert the network into the tree. A continuous mask has only
1100
* ones on the left. This means that this mask + its lower bit
1101
* added once again is null.
1103
mask = ntohl(pat->val.ipv4.mask.s_addr);
1104
if (mask + (mask & -mask) == 0) {
1105
mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */
1107
/* node memory allocation */
1108
node = calloc(1, sizeof(*node) + 4);
1110
memprintf(err, "out of memory while loading pattern");
1114
/* copy the pointer to sample associated to this node */
1115
node->smp = pat->smp;
1116
node->ref = pat->ref;
1118
/* FIXME: insert <addr>/<mask> into the tree here */
1119
memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */
1120
node->node.node.pfx = mask;
1122
/* Insert the entry. */
1123
ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4);
1129
/* If the mask is not contiguous, just add the pattern to the list */
1130
return pat_idx_list_val(expr, pat, err);
1133
else if (pat->type == SMP_T_IPV6) {
1134
/* IPv6 also can be indexed */
1135
node = calloc(1, sizeof(*node) + 16);
1137
memprintf(err, "out of memory while loading pattern");
1141
/* copy the pointer to sample associated to this node */
1142
node->smp = pat->smp;
1143
node->ref = pat->ref;
1145
/* FIXME: insert <addr>/<mask> into the tree here */
1146
memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */
1147
node->node.node.pfx = pat->val.ipv6.mask;
1149
/* Insert the entry. */
1150
ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16);
1159
int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
1162
struct pattern_tree *node;
1164
/* Only string can be indexed */
1165
if (pat->type != SMP_T_STR) {
1166
memprintf(err, "internal error: string expected, but the type is '%s'",
1167
smp_to_type[pat->type]);
1171
/* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1172
if (expr->mflags & PAT_MF_IGNORE_CASE)
1173
return pat_idx_list_str(expr, pat, err);
1175
/* Process the key len */
1176
len = strlen(pat->ptr.str) + 1;
1178
/* node memory allocation */
1179
node = calloc(1, sizeof(*node) + len);
1181
memprintf(err, "out of memory while loading pattern");
1185
/* copy the pointer to sample associated to this node */
1186
node->smp = pat->smp;
1187
node->ref = pat->ref;
1189
/* copy the string */
1190
memcpy(node->node.key, pat->ptr.str, len);
1192
/* index the new node */
1193
ebst_insert(&expr->pattern_tree, &node->node);
1199
int pat_idx_tree_pfx(struct pattern_expr *expr, struct pattern *pat, char **err)
1202
struct pattern_tree *node;
1204
/* Only string can be indexed */
1205
if (pat->type != SMP_T_STR) {
1206
memprintf(err, "internal error: string expected, but the type is '%s'",
1207
smp_to_type[pat->type]);
1211
/* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
1212
if (expr->mflags & PAT_MF_IGNORE_CASE)
1213
return pat_idx_list_str(expr, pat, err);
1215
/* Process the key len */
1216
len = strlen(pat->ptr.str);
1218
/* node memory allocation */
1219
node = calloc(1, sizeof(*node) + len + 1);
1221
memprintf(err, "out of memory while loading pattern");
1225
/* copy the pointer to sample associated to this node */
1226
node->smp = pat->smp;
1227
node->ref = pat->ref;
1229
/* copy the string and the trailing zero */
1230
memcpy(node->node.key, pat->ptr.str, len + 1);
1231
node->node.node.pfx = len * 8;
1233
/* index the new node */
1234
ebmb_insert_prefix(&expr->pattern_tree, &node->node, len);
1240
void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref)
1242
struct pattern_list *pat;
1243
struct pattern_list *safe;
1245
list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1246
/* Check equality. */
1247
if (pat->pat.ref != ref)
1250
/* Delete and free entry. */
1251
LIST_DEL(&pat->list);
1257
void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref)
1259
struct ebmb_node *node, *next_node;
1260
struct pattern_tree *elt;
1262
/* browse each node of the tree for IPv4 addresses. */
1263
for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1265
node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1266
/* Extract container of the tree node. */
1267
elt = container_of(node, struct pattern_tree, node);
1269
/* Check equality. */
1270
if (elt->ref != ref)
1273
/* Delete and free entry. */
1279
/* Browse each node of the list for IPv4 addresses. */
1280
pat_del_list_val(expr, ref);
1282
/* browse each node of the tree for IPv6 addresses. */
1283
for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL;
1285
node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1286
/* Extract container of the tree node. */
1287
elt = container_of(node, struct pattern_tree, node);
1289
/* Check equality. */
1290
if (elt->ref != ref)
1293
/* Delete and free entry. */
1300
void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref)
1302
struct pattern_list *pat;
1303
struct pattern_list *safe;
1305
list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1306
/* Check equality. */
1307
if (pat->pat.ref != ref)
1310
/* Delete and free entry. */
1311
LIST_DEL(&pat->list);
1312
free(pat->pat.ptr.ptr);
1318
void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref)
1320
struct ebmb_node *node, *next_node;
1321
struct pattern_tree *elt;
1323
/* browse each node of the tree. */
1324
for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL;
1326
node = next_node, next_node = node ? ebmb_next(node) : NULL) {
1327
/* Extract container of the tree node. */
1328
elt = container_of(node, struct pattern_tree, node);
1330
/* Check equality. */
1331
if (elt->ref != ref)
1334
/* Delete and free entry. */
1341
void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref)
1343
struct pattern_list *pat;
1344
struct pattern_list *safe;
1346
list_for_each_entry_safe(pat, safe, &expr->patterns, list) {
1347
/* Check equality. */
1348
if (pat->pat.ref != ref)
1351
/* Delete and free entry. */
1352
LIST_DEL(&pat->list);
1353
regex_free(pat->pat.ptr.ptr);
1359
void pattern_init_expr(struct pattern_expr *expr)
1361
LIST_INIT(&expr->patterns);
1362
expr->pattern_tree = EB_ROOT;
1363
expr->pattern_tree_2 = EB_ROOT;
1366
void pattern_init_head(struct pattern_head *head)
1368
LIST_INIT(&head->head);
1371
/* The following functions are relative to the management of the reference
1372
* lists. These lists are used to store the original pattern and associated
1373
* value as string form.
1375
* This is used with modifiable ACL and MAPS
1377
* The pattern reference are stored with two identifiers: the unique_id and
1380
* The reference identify a file. Each file with the same name point to the
1381
* same reference. We can register many times one file. If the file is modified,
1382
* all his dependencies are also modified. The reference can be used with map or
1385
* The unique_id identify inline acl. The unique id is unique for each acl.
1386
* You cannot force the same id in the configuration file, because this repoort
1389
* A particular case appears if the filename is a number. In this case, the
1390
* unique_id is set with the number represented by the filename and the
1391
* reference is also set. This method prevent double unique_id.
1395
/* This function lookup for reference. If the reference is found, they return
1396
* pointer to the struct pat_ref, else return NULL.
1398
struct pat_ref *pat_ref_lookup(const char *reference)
1400
struct pat_ref *ref;
1402
list_for_each_entry(ref, &pattern_reference, list)
1403
if (ref->reference && strcmp(reference, ref->reference) == 0)
1408
/* This function lookup for unique id. If the reference is found, they return
1409
* pointer to the struct pat_ref, else return NULL.
1411
struct pat_ref *pat_ref_lookupid(int unique_id)
1413
struct pat_ref *ref;
1415
list_for_each_entry(ref, &pattern_reference, list)
1416
if (ref->unique_id == unique_id)
1421
/* This function remove all pattern matching the pointer <refelt> from
1422
* the the reference and from each expr member of the reference. This
1423
* function returns 1 if the deletion is done and return 0 is the entry
1426
int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt)
1428
struct pattern_expr *expr;
1429
struct pat_ref_elt *elt, *safe;
1431
/* delete pattern from reference */
1432
list_for_each_entry_safe(elt, safe, &ref->head, list) {
1433
if (elt == refelt) {
1434
LIST_DEL(&elt->list);
1439
list_for_each_entry(expr, &ref->pat, list)
1440
pattern_delete(expr, elt);
1448
/* This function remove all pattern match <key> from the the reference
1449
* and from each expr member of the reference. This fucntion returns 1
1450
* if the deletion is done and return 0 is the entry is not found.
1452
int pat_ref_delete(struct pat_ref *ref, const char *key)
1454
struct pattern_expr *expr;
1455
struct pat_ref_elt *elt, *safe;
1458
/* delete pattern from reference */
1459
list_for_each_entry_safe(elt, safe, &ref->head, list) {
1460
if (strcmp(key, elt->pattern) == 0) {
1461
list_for_each_entry(expr, &ref->pat, list)
1462
pattern_delete(expr, elt);
1464
LIST_DEL(&elt->list);
1479
* find and return an element <elt> matching <key> in a reference <ref>
1480
* return NULL if not found
1482
struct pat_ref_elt *pat_ref_find_elt(struct pat_ref *ref, const char *key)
1484
struct pat_ref_elt *elt;
1486
list_for_each_entry(elt, &ref->head, list) {
1487
if (strcmp(key, elt->pattern) == 0)
1495
/* This function modify the sample of the first pattern that match the <key>. */
1496
static inline int pat_ref_set_elt(struct pat_ref *ref, struct pat_ref_elt *elt,
1497
const char *value, char **err)
1499
struct pattern_expr *expr;
1500
struct sample_storage **smp;
1502
struct sample_storage test;
1504
/* Try all needed converters. */
1505
list_for_each_entry(expr, &ref->pat, list) {
1506
if (!expr->pat_head->parse_smp)
1509
if (!expr->pat_head->parse_smp(value, &test)) {
1510
memprintf(err, "unable to parse '%s'", value);
1515
/* Modify pattern from reference. */
1516
sample = strdup(value);
1518
memprintf(err, "out of memory error");
1522
elt->sample = sample;
1524
/* Load sample in each reference. All the conversion are tested
1525
* below, normally these calls dosn't fail.
1527
list_for_each_entry(expr, &ref->pat, list) {
1528
if (!expr->pat_head->parse_smp)
1531
smp = pattern_find_smp(expr, elt);
1532
if (smp && *smp && !expr->pat_head->parse_smp(sample, *smp))
1539
/* This function modify the sample of the first pattern that match the <key>. */
1540
int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err)
1542
struct pat_ref_elt *elt;
1544
/* Look for pattern in the reference. */
1545
list_for_each_entry(elt, &ref->head, list) {
1546
if (elt == refelt) {
1547
if (!pat_ref_set_elt(ref, elt, value, err))
1553
memprintf(err, "key or pattern not found");
1557
/* This function modify the sample of the first pattern that match the <key>. */
1558
int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
1560
struct pat_ref_elt *elt;
1572
/* Look for pattern in the reference. */
1573
list_for_each_entry(elt, &ref->head, list) {
1574
if (strcmp(key, elt->pattern) == 0) {
1575
if (!pat_ref_set_elt(ref, elt, value, merr)) {
1579
memprintf(err, "%s, %s", *err, *merr);
1589
memprintf(err, "entry not found");
1595
/* This function create new reference. <ref> is the reference name.
1596
* <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1597
* be unique. The user must check the reference with "pat_ref_lookup()"
1598
* before calling this function. If the fucntion fail, it return NULL,
1599
* else return new struct pat_ref.
1601
struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags)
1603
struct pat_ref *ref;
1605
ref = malloc(sizeof(*ref));
1610
ref->display = strdup(display);
1611
if (!ref->display) {
1617
ref->display = NULL;
1619
ref->reference = strdup(reference);
1620
if (!ref->reference) {
1627
ref->unique_id = -1;
1629
LIST_INIT(&ref->head);
1630
LIST_INIT(&ref->pat);
1632
LIST_ADDQ(&pattern_reference, &ref->list);
1637
/* This function create new reference. <unique_id> is the unique id. If
1638
* the value of <unique_id> is -1, the unique id is calculated later.
1639
* <flags> are PAT_REF_*. /!\ The reference is not checked, and must
1640
* be unique. The user must check the reference with "pat_ref_lookup()"
1641
* or pat_ref_lookupid before calling this function. If the function
1642
* fail, it return NULL, else return new struct pat_ref.
1644
struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags)
1646
struct pat_ref *ref;
1648
ref = malloc(sizeof(*ref));
1653
ref->display = strdup(display);
1654
if (!ref->display) {
1660
ref->display = NULL;
1662
ref->reference = NULL;
1664
ref->unique_id = unique_id;
1665
LIST_INIT(&ref->head);
1666
LIST_INIT(&ref->pat);
1668
LIST_ADDQ(&pattern_reference, &ref->list);
1673
/* This function adds entry to <ref>. It can failed with memory error.
1674
* If the function fails, it returns 0.
1676
int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line)
1678
struct pat_ref_elt *elt;
1680
elt = malloc(sizeof(*elt));
1686
elt->pattern = strdup(pattern);
1687
if (!elt->pattern) {
1693
elt->sample = strdup(sample);
1703
LIST_ADDQ(&ref->head, &elt->list);
1708
/* This function create sample found in <elt>, parse the pattern also
1709
* found in <elt> and insert it in <expr>. The function copy <patflags>
1710
* in <expr>. If the function fails, it returns0 and <err> is filled.
1711
* In succes case, the function returns 1.
1714
int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr,
1715
int patflags, char **err)
1717
struct sample_storage *smp;
1718
struct pattern pattern;
1721
if (elt->sample && expr->pat_head->parse_smp) {
1723
smp = malloc(sizeof(*smp));
1728
if (!expr->pat_head->parse_smp(elt->sample, smp)) {
1729
memprintf(err, "unable to parse '%s'", elt->sample);
1738
/* initialise pattern */
1739
memset(&pattern, 0, sizeof(pattern));
1744
if (!expr->pat_head->parse(elt->pattern, &pattern, expr->mflags, err)) {
1750
if (!expr->pat_head->index(expr, &pattern, err)) {
1758
/* This function adds entry to <ref>. It can failed with memory error. The new
1759
* entry is added at all the pattern_expr registered in this reference. The
1760
* function stop on the first error encountered. It returns 0 and err is
1761
* filled. If an error is encountered, the complete add operation is cancelled.
1762
* If the insertion is a success the function returns 1.
1764
int pat_ref_add(struct pat_ref *ref,
1765
const char *pattern, const char *sample,
1768
struct pat_ref_elt *elt;
1769
struct pattern_expr *expr;
1771
elt = malloc(sizeof(*elt));
1773
memprintf(err, "out of memory error");
1779
elt->pattern = strdup(pattern);
1780
if (!elt->pattern) {
1782
memprintf(err, "out of memory error");
1787
elt->sample = strdup(sample);
1791
memprintf(err, "out of memory error");
1798
LIST_ADDQ(&ref->head, &elt->list);
1800
list_for_each_entry(expr, &ref->pat, list) {
1801
if (!pat_ref_push(elt, expr, 0, err)) {
1802
/* If the insertion fails, try to delete all the added entries. */
1803
pat_ref_delete_by_id(ref, elt);
1810
/* This function prune <ref>, replace all reference by the references
1811
* of <replace>, and reindex all the news values.
1813
* The pattern are loaded in best effort and the errors are ignored,
1814
* but writed in the logs.
1816
void pat_ref_reload(struct pat_ref *ref, struct pat_ref *replace)
1818
struct pattern_expr *expr;
1819
struct pat_ref_elt *elt;
1824
LIST_ADD(&replace->head, &ref->head);
1825
LIST_DEL(&replace->head);
1827
list_for_each_entry(elt, &ref->head, list) {
1828
list_for_each_entry(expr, &ref->pat, list) {
1829
if (!pat_ref_push(elt, expr, 0, &err)) {
1830
send_log(NULL, LOG_NOTICE, "%s", err);
1838
/* This function prune all entries of <ref>. This function
1839
* prune the associated pattern_expr.
1841
void pat_ref_prune(struct pat_ref *ref)
1843
struct pat_ref_elt *elt, *safe;
1844
struct pattern_expr *expr;
1846
list_for_each_entry_safe(elt, safe, &ref->head, list) {
1847
LIST_DEL(&elt->list);
1853
list_for_each_entry(expr, &ref->pat, list)
1854
expr->pat_head->prune(expr);
1857
/* This function lookup for existing reference <ref> in pattern_head <head>. */
1858
struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref)
1860
struct pattern_expr_list *expr;
1862
list_for_each_entry(expr, &head->head, list)
1863
if (expr->expr->ref == ref)
1868
/* This function create new pattern_expr associated to the reference <ref>.
1869
* <ref> can be NULL. If an error is occured, the function returns NULL and
1870
* <err> is filled. Otherwise, the function returns new pattern_expr linked
1871
* with <head> and <ref>.
1873
struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err)
1875
struct pattern_expr *expr;
1876
struct pattern_expr_list *list;
1878
/* Memory and initialization of the chain element. */
1879
list = malloc(sizeof(*list));
1881
memprintf(err, "out of memory");
1885
/* Look for existing similar expr. No that only the index, parse and
1886
* parse_smp function must be identical for having similar pattern.
1887
* The other function depends of theses first.
1890
list_for_each_entry(expr, &ref->pat, list)
1891
if (expr->pat_head->index == head->index &&
1892
expr->pat_head->parse == head->parse &&
1893
expr->pat_head->parse_smp == head->parse_smp)
1895
if (&expr->list == &ref->pat)
1901
/* If no similar expr was found, we create new expr. */
1903
/* Get a lot of memory for the expr struct. */
1904
expr = malloc(sizeof(*expr));
1906
memprintf(err, "out of memory");
1910
/* Initialize this new expr. */
1911
pattern_init_expr(expr);
1913
/* This new pattern expression reference one of his heads. */
1914
expr->pat_head = head;
1916
/* Link with ref, or to self to facilitate LIST_DEL() */
1918
LIST_ADDQ(&ref->pat, &expr->list);
1920
LIST_INIT(&expr->list);
1924
/* We must free this pattern if it is no more used. */
1928
/* If the pattern used already exists, it is already linked
1929
* with ref and we must not free it.
1934
/* The new list element reference the pattern_expr. */
1937
/* Link the list element with the pattern_head. */
1938
LIST_ADDQ(&head->head, &list->list);
450
/* TODO: prune_pattern_expr(expr); */
455
* Process a fetch + format conversion of defined by the pattern expression <expr>
456
* on request or response considering the <dir> parameter.
457
* Returns a pointer on a typed pattern structure containing the result or NULL if
458
* pattern is not found or when format conversion failed.
459
* If <p> is not null, function returns results in structure pointed by <p>.
460
* If <p> is null, functions returns a pointer on a static pattern structure.
462
struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7, int dir,
463
struct pattern_expr *expr, struct pattern *p)
465
struct pattern_conv_expr *conv_expr;
470
if (!expr->fetch->process(px, l4, l7, dir, expr->arg, expr->arg_len, &p->data))
473
p->type = expr->fetch->out_type;
475
list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
476
if (!pattern_casts[p->type][conv_expr->conv->in_type](&p->data))
479
p->type = conv_expr->conv->in_type;
480
if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
483
p->type = conv_expr->conv->out_type;
489
* Process a fetch + format conversion of defined by the pattern expression <expr>
490
* on request or response considering the <dir> parameter.
491
* Returns a pointer on a static tablekey structure of type <table_type> of
492
* the converted result.
494
struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4, void *l7, int dir,
495
struct pattern_expr *expr, unsigned long table_type)
497
struct pattern *ptrn;
499
ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
503
stable_key.key_len = (size_t)-1;
504
stable_key.key = pattern_keys[ptrn->type][table_type](&ptrn->data, &stable_key.data, &stable_key.key_len);
513
* Returns 1 if pattern expression <expr> result cannot be converted to table key of
514
* type <table_type> .
516
* Used in configuration check
518
int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
521
if (table_type >= STKTABLE_TYPES)
524
if (LIST_ISEMPTY(&expr->conv_exprs)) {
525
if (!pattern_keys[expr->fetch->out_type][table_type])
528
struct pattern_conv_expr *conv_expr;
529
conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
531
if (!pattern_keys[conv_expr->conv->out_type][table_type])
537
/* Converts an argument string to an IPv4 mask stored in network byte order in
538
* arg_i. Returns non-zero in case of success, 0 on error.
540
static int pattern_conv_arg_to_ipmask(const char *arg_str, void **arg_p, int *arg_i)
544
if (!str2mask(arg_str, &mask))
547
*arg_i = mask.s_addr;
551
/*****************************************************************/
552
/* Pattern format convert functions */
553
/*****************************************************************/
555
static int pattern_conv_str2lower(const void *arg_p, int arg_i, union pattern_data *data)
559
for (i = 0; i < data->str.len; i++) {
560
if ((data->str.str[i] >= 'A') && (data->str.str[i] <= 'Z'))
561
data->str.str[i] += 'a' - 'A';
566
static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_data *data)
570
for (i = 0; i < data->str.len; i++) {
571
if ((data->str.str[i] >= 'a') && (data->str.str[i] <= 'z'))
572
data->str.str[i] += 'A' - 'a';
577
/* takes the netmask in arg_i */
578
static int pattern_conv_ipmask(const void *arg_p, int arg_i, union pattern_data *data)
580
data->ip.s_addr &= arg_i;
584
/* Note: must not be declared <const> as its list will be overwritten */
585
static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
586
{ "upper", pattern_conv_str2upper, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
587
{ "lower", pattern_conv_str2lower, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
588
{ "ipmask", pattern_conv_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP, pattern_conv_arg_to_ipmask },
589
{ NULL, NULL, 0, 0 },
592
__attribute__((constructor))
593
static void __pattern_init(void)
595
/* register pattern format convert keywords */
596
pattern_register_convs(&pattern_conv_kws);
1942
/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
1943
* be returned there on errors and the caller will have to free it.
1945
* The file contains one key + value per line. Lines which start with '#' are
1946
* ignored, just like empty lines. Leading tabs/spaces are stripped. The key is
1947
* then the first "word" (series of non-space/tabs characters), and the value is
1948
* what follows this series of space/tab till the end of the line excluding
1949
* trailing spaces/tabs.
1953
* # this is a comment and is ignored
1954
* 62.212.114.60 1wt.eu \n
1955
* <-><-----------><---><----><---->
1956
* | | | | `--- trailing spaces ignored
1957
* | | | `-------- value
1958
* | | `--------------- middle spaces ignored
1959
* | `------------------------ key
1960
* `-------------------------------- leading spaces ignored
1962
* Return non-zero in case of succes, otherwise 0.
1964
int pat_ref_read_from_file_smp(struct pat_ref *ref, const char *filename, char **err)
1975
file = fopen(filename, "r");
1977
memprintf(err, "failed to open pattern file <%s>", filename);
1981
/* now parse all patterns. The file may contain only one pattern
1982
* followed by one value per line. The start spaces, separator spaces
1983
* and and spaces are stripped. Each can contain comment started by '#'
1985
while (fgets(trash.str, trash.size, file) != NULL) {
1989
/* ignore lines beginning with a dash */
1993
/* strip leading spaces and tabs */
1994
while (*c == ' ' || *c == '\t')
1997
/* empty lines are ignored too */
1998
if (*c == '\0' || *c == '\r' || *c == '\n')
2001
/* look for the end of the key */
2003
while (*c && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
2008
/* strip middle spaces and tabs */
2009
while (*c == ' ' || *c == '\t')
2012
/* look for the end of the value, it is the end of the line */
2014
while (*c && *c != '\n' && *c != '\r')
2018
/* trim possibly trailing spaces and tabs */
2019
while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t'))
2022
/* set final \0 and check entries */
2027
if (!pat_ref_append(ref, key_beg, value_beg, line)) {
2028
memprintf(err, "out of memory");
2041
/* Reads patterns from a file. If <err_msg> is non-NULL, an error message will
2042
* be returned there on errors and the caller will have to free it.
2044
int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err)
2052
file = fopen(filename, "r");
2054
memprintf(err, "failed to open pattern file <%s>", filename);
2058
/* now parse all patterns. The file may contain only one pattern per
2059
* line. If the line contains spaces, they will be part of the pattern.
2060
* The pattern stops at the first CR, LF or EOF encountered.
2062
while (fgets(trash.str, trash.size, file) != NULL) {
2066
/* ignore lines beginning with a dash */
2070
/* strip leading spaces and tabs */
2071
while (*c == ' ' || *c == '\t')
2076
while (*c && *c != '\n' && *c != '\r')
2080
/* empty lines are ignored too */
2084
if (!pat_ref_append(ref, arg, NULL, line)) {
2085
memprintf(err, "out of memory when loading patterns from file <%s>", filename);
2090
ret = 1; /* success */
2097
int pattern_read_from_file(struct pattern_head *head, unsigned int refflags,
2098
const char *filename, int patflags, int load_smp,
2099
char **err, const char *file, int line)
2101
struct pat_ref *ref;
2102
struct pattern_expr *expr;
2103
struct pat_ref_elt *elt;
2105
/* Lookup for the existing reference. */
2106
ref = pat_ref_lookup(filename);
2108
/* If the reference doesn't exists, create it and load associated file. */
2110
chunk_printf(&trash,
2111
"pattern loaded from file '%s' used by %s at file '%s' line %d",
2112
filename, refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2114
ref = pat_ref_new(filename, trash.str, refflags);
2116
memprintf(err, "out of memory");
2121
ref->flags |= PAT_REF_SMP;
2122
if (!pat_ref_read_from_file_smp(ref, filename, err))
2126
if (!pat_ref_read_from_file(ref, filename, err))
2131
/* The reference already exists, check the map compatibility. */
2133
/* If the load require samples and the flag PAT_REF_SMP is not set,
2134
* the reference doesn't contain sample, and cannot be used.
2137
if (!(ref->flags & PAT_REF_SMP)) {
2138
memprintf(err, "The file \"%s\" is already used as one column file "
2139
"and cannot be used by as two column file.",
2145
/* The load doesn't require samples. If the flag PAT_REF_SMP is
2146
* set, the reference contains a sample, and cannot be used.
2148
if (ref->flags & PAT_REF_SMP) {
2149
memprintf(err, "The file \"%s\" is already used as two column file "
2150
"and cannot be used by as one column file.",
2156
/* Extends display */
2157
chunk_printf(&trash, "%s", ref->display);
2158
chunk_appendf(&trash, ", by %s at file '%s' line %d",
2159
refflags & PAT_REF_MAP ? "map" : "acl", file, line);
2161
ref->display = strdup(trash.str);
2162
if (!ref->display) {
2163
memprintf(err, "out of memory");
2168
ref->flags |= refflags;
2171
/* Now, we can loading patterns from the reference. */
2173
/* Lookup for existing reference in the head. If the reference
2174
* doesn't exists, create it.
2176
expr = pattern_lookup_expr(head, ref);
2177
if (!expr || (expr->mflags != patflags)) {
2178
expr = pattern_new_expr(head, ref, err);
2181
expr->mflags = patflags;
2184
/* Load reference content in the pattern expression. */
2185
list_for_each_entry(elt, &ref->head, list) {
2186
if (!pat_ref_push(elt, expr, patflags, err)) {
2188
memprintf(err, "%s at line %d of file '%s'",
2189
*err, elt->line, filename);
2197
/* This function executes a pattern match on a sample. It applies pattern <expr>
2198
* to sample <smp>. The function returns NULL if the sample dont match. It returns
2199
* non-null if the sample match. If <fill> is true and the sample match, the
2200
* function returns the matched pattern. In many cases, this pattern can be a
2203
struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill)
2205
struct pattern_expr_list *list;
2206
struct pattern *pat;
2210
static_pattern.smp = NULL;
2211
static_pattern.ref = NULL;
2212
static_pattern.sflags = 0;
2213
static_pattern.type = SMP_T_UINT;
2214
static_pattern.val.i = 1;
2216
return &static_pattern;
2219
/* convert input to string */
2220
if (!sample_convert(smp, head->expect_type))
2223
list_for_each_entry(list, &head->head, list) {
2224
pat = head->match(smp, list->expr, fill);
2231
/* This function prune the pattern expression. */
2232
void pattern_prune(struct pattern_head *head)
2234
struct pattern_expr_list *list, *safe;
2236
list_for_each_entry_safe(list, safe, &head->head, list) {
2237
LIST_DEL(&list->list);
2238
if (list->do_free) {
2239
LIST_DEL(&list->expr->list);
2240
head->prune(list->expr);
2247
/* This function lookup for a pattern matching the <key> and return a
2248
* pointer to a pointer of the sample stoarge. If the <key> dont match,
2249
* the function returns NULL. If the key cannot be parsed, the function
2252
struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *ref)
2254
struct ebmb_node *node;
2255
struct pattern_tree *elt;
2256
struct pattern_list *pat;
2258
for (node = ebmb_first(&expr->pattern_tree);
2260
node = ebmb_next(node)) {
2261
elt = container_of(node, struct pattern_tree, node);
2262
if (elt->ref == ref)
2266
for (node = ebmb_first(&expr->pattern_tree_2);
2268
node = ebmb_next(node)) {
2269
elt = container_of(node, struct pattern_tree, node);
2270
if (elt->ref == ref)
2274
list_for_each_entry(pat, &expr->patterns, list)
2275
if (pat->pat.ref == ref)
2276
return &pat->pat.smp;
2281
/* This function search all the pattern matching the <key> and delete it.
2282
* If the parsing of the input key fails, the function returns 0 and the
2283
* <err> is filled, else return 1;
2285
int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref)
2287
expr->pat_head->delete(expr, ref);
2291
/* This function finalize the configuration parsing. Its set all the
2294
void pattern_finalize_config(void)
2297
struct pat_ref *ref, *ref2, *ref3;
2298
struct list pr = LIST_HEAD_INIT(pr);
2300
list_for_each_entry(ref, &pattern_reference, list) {
2301
if (ref->unique_id == -1) {
2302
/* Look for the first free id. */
2304
list_for_each_entry(ref2, &pattern_reference, list) {
2305
if (ref2->unique_id == i) {
2310
if (&ref2->list == &pattern_reference)
2314
/* Uses the unique id and increment it for the next entry. */
2320
/* This sort the reference list by id. */
2321
list_for_each_entry_safe(ref, ref2, &pattern_reference, list) {
2322
LIST_DEL(&ref->list);
2323
list_for_each_entry(ref3, &pr, list) {
2324
if (ref->unique_id < ref3->unique_id) {
2325
LIST_ADDQ(&ref3->list, &ref->list);
2329
if (&ref3->list == &pr)
2330
LIST_ADDQ(&pr, &ref->list);
2334
LIST_ADD(&pr, &pattern_reference);