Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 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 FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 23 | #include <proto/sample.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 24 | |
| 25 | #include <ebsttree.h> |
| 26 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 27 | char *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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 43 | int (*pat_parse_fcts[PAT_MATCH_NUM])(const char *, struct pattern *, char **) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 44 | [PAT_MATCH_FOUND] = pat_parse_nothing, |
| 45 | [PAT_MATCH_BOOL] = pat_parse_nothing, |
| 46 | [PAT_MATCH_INT] = pat_parse_int, |
| 47 | [PAT_MATCH_IP] = pat_parse_ip, |
| 48 | [PAT_MATCH_BIN] = pat_parse_bin, |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 49 | [PAT_MATCH_LEN] = pat_parse_int, |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 50 | [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 FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 59 | int (*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 FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 75 | void (*pat_delete_fcts[PAT_MATCH_NUM])(struct pattern_expr *, struct pattern *) = { |
| 76 | [PAT_MATCH_FOUND] = pat_del_list_val, |
| 77 | [PAT_MATCH_BOOL] = pat_del_list_val, |
| 78 | [PAT_MATCH_INT] = pat_del_list_val, |
| 79 | [PAT_MATCH_IP] = pat_del_tree_ip, |
| 80 | [PAT_MATCH_BIN] = pat_del_list_ptr, |
| 81 | [PAT_MATCH_LEN] = pat_del_list_val, |
| 82 | [PAT_MATCH_STR] = pat_del_tree_str, |
| 83 | [PAT_MATCH_BEG] = pat_del_list_str, |
| 84 | [PAT_MATCH_SUB] = pat_del_list_str, |
| 85 | [PAT_MATCH_DIR] = pat_del_list_str, |
| 86 | [PAT_MATCH_DOM] = pat_del_list_str, |
| 87 | [PAT_MATCH_END] = pat_del_list_str, |
| 88 | [PAT_MATCH_REG] = pat_del_list_reg, |
| 89 | }; |
| 90 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 91 | struct sample_storage **(*pat_find_smp_fcts[PAT_MATCH_NUM])(struct pattern_expr *, |
| 92 | struct pattern *) = { |
| 93 | [PAT_MATCH_FOUND] = pat_find_smp_list_val, |
| 94 | [PAT_MATCH_BOOL] = pat_find_smp_list_val, |
| 95 | [PAT_MATCH_INT] = pat_find_smp_list_val, |
| 96 | [PAT_MATCH_IP] = pat_find_smp_tree_ip, |
| 97 | [PAT_MATCH_BIN] = pat_find_smp_list_ptr, |
| 98 | [PAT_MATCH_LEN] = pat_find_smp_list_val, |
| 99 | [PAT_MATCH_STR] = pat_find_smp_tree_str, |
| 100 | [PAT_MATCH_BEG] = pat_find_smp_list_str, |
| 101 | [PAT_MATCH_SUB] = pat_find_smp_list_str, |
| 102 | [PAT_MATCH_DIR] = pat_find_smp_list_str, |
| 103 | [PAT_MATCH_DOM] = pat_find_smp_list_str, |
| 104 | [PAT_MATCH_END] = pat_find_smp_list_str, |
| 105 | [PAT_MATCH_REG] = pat_find_smp_list_reg, |
| 106 | }; |
| 107 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 108 | void (*pat_prune_fcts[PAT_MATCH_NUM])(struct pattern_expr *) = { |
| 109 | [PAT_MATCH_FOUND] = pat_prune_val, |
| 110 | [PAT_MATCH_BOOL] = pat_prune_val, |
| 111 | [PAT_MATCH_INT] = pat_prune_val, |
| 112 | [PAT_MATCH_IP] = pat_prune_val, |
| 113 | [PAT_MATCH_BIN] = pat_prune_ptr, |
| 114 | [PAT_MATCH_LEN] = pat_prune_val, |
| 115 | [PAT_MATCH_STR] = pat_prune_ptr, |
| 116 | [PAT_MATCH_BEG] = pat_prune_ptr, |
| 117 | [PAT_MATCH_SUB] = pat_prune_ptr, |
| 118 | [PAT_MATCH_DIR] = pat_prune_ptr, |
| 119 | [PAT_MATCH_DOM] = pat_prune_ptr, |
| 120 | [PAT_MATCH_END] = pat_prune_ptr, |
| 121 | [PAT_MATCH_REG] = pat_prune_reg, |
| 122 | }; |
| 123 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 124 | struct pattern *(*pat_match_fcts[PAT_MATCH_NUM])(struct sample *, struct pattern_expr *, int) = { |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 125 | [PAT_MATCH_FOUND] = NULL, |
| 126 | [PAT_MATCH_BOOL] = pat_match_nothing, |
| 127 | [PAT_MATCH_INT] = pat_match_int, |
| 128 | [PAT_MATCH_IP] = pat_match_ip, |
| 129 | [PAT_MATCH_BIN] = pat_match_bin, |
| 130 | [PAT_MATCH_LEN] = pat_match_len, |
| 131 | [PAT_MATCH_STR] = pat_match_str, |
| 132 | [PAT_MATCH_BEG] = pat_match_beg, |
| 133 | [PAT_MATCH_SUB] = pat_match_sub, |
| 134 | [PAT_MATCH_DIR] = pat_match_dir, |
| 135 | [PAT_MATCH_DOM] = pat_match_dom, |
| 136 | [PAT_MATCH_END] = pat_match_end, |
| 137 | [PAT_MATCH_REG] = pat_match_reg, |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 140 | /* Just used for checking configuration compatibility */ |
| 141 | int pat_match_types[PAT_MATCH_NUM] = { |
| 142 | [PAT_MATCH_FOUND] = SMP_T_UINT, |
| 143 | [PAT_MATCH_BOOL] = SMP_T_UINT, |
| 144 | [PAT_MATCH_INT] = SMP_T_UINT, |
| 145 | [PAT_MATCH_IP] = SMP_T_ADDR, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 146 | [PAT_MATCH_BIN] = SMP_T_BIN, |
| 147 | [PAT_MATCH_LEN] = SMP_T_STR, |
| 148 | [PAT_MATCH_STR] = SMP_T_STR, |
| 149 | [PAT_MATCH_BEG] = SMP_T_STR, |
| 150 | [PAT_MATCH_SUB] = SMP_T_STR, |
| 151 | [PAT_MATCH_DIR] = SMP_T_STR, |
| 152 | [PAT_MATCH_DOM] = SMP_T_STR, |
| 153 | [PAT_MATCH_END] = SMP_T_STR, |
| 154 | [PAT_MATCH_REG] = SMP_T_STR, |
Thierry FOURNIER | e3ded59 | 2013-12-06 15:36:54 +0100 | [diff] [blame] | 155 | }; |
| 156 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 157 | /* this struct is used to return information */ |
| 158 | static struct pattern static_pattern; |
| 159 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 160 | /* This is the root of the list of all pattern_ref avalaibles. */ |
| 161 | struct list pattern_reference = LIST_HEAD_INIT(pattern_reference); |
| 162 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 163 | /* |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 164 | * |
| 165 | * The following functions are not exported and are used by internals process |
| 166 | * of pattern matching |
| 167 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 168 | */ |
| 169 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 170 | /* Background: Fast way to find a zero byte in a word |
| 171 | * http://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord |
| 172 | * hasZeroByte = (v - 0x01010101UL) & ~v & 0x80808080UL; |
| 173 | * |
| 174 | * To look for 4 different byte values, xor the word with those bytes and |
| 175 | * then check for zero bytes: |
| 176 | * |
| 177 | * v = (((unsigned char)c * 0x1010101U) ^ delimiter) |
| 178 | * where <delimiter> is the 4 byte values to look for (as an uint) |
| 179 | * and <c> is the character that is being tested |
| 180 | */ |
| 181 | static inline unsigned int is_delimiter(unsigned char c, unsigned int mask) |
| 182 | { |
| 183 | mask ^= (c * 0x01010101); /* propagate the char to all 4 bytes */ |
| 184 | return (mask - 0x01010101) & ~mask & 0x80808080U; |
| 185 | } |
| 186 | |
| 187 | static inline unsigned int make_4delim(unsigned char d1, unsigned char d2, unsigned char d3, unsigned char d4) |
| 188 | { |
| 189 | return d1 << 24 | d2 << 16 | d3 << 8 | d4; |
| 190 | } |
| 191 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 192 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 193 | /* |
| 194 | * |
| 195 | * These functions are exported and may be used by any other component. |
| 196 | * |
| 197 | * The following functions are used for parsing pattern matching |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 198 | * input value. The <text> contain the string to be parsed. <pattern> |
| 199 | * must be a preallocated pattern. The pat_parse_* functions fill this |
| 200 | * structure with the parsed value. <usage> can be PAT_U_COMPILE or |
| 201 | * PAT_U_LOOKUP. If the value PAT_U_COMPILE is used memory is allocated |
| 202 | * for filling the pattern. If the value PAT_U_LOOKUP is set, the parser |
| 203 | * use "trash" or return pointers to the input strings. In both cases, |
| 204 | * the caller must use the value PAT_U_LOOKUP with caution. <err> is |
| 205 | * filled with an error message built with memprintf() function. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 206 | * |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 207 | * In succes case, the pat_parse_* function return 1. If the function |
| 208 | * fail, it returns 0 and <err> is filled. |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 209 | * |
| 210 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 211 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 212 | /* ignore the current line */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 213 | int pat_parse_nothing(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 214 | { |
| 215 | return 1; |
| 216 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 217 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 218 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 219 | int pat_parse_str(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 220 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 221 | pattern->type = SMP_T_STR; |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 222 | pattern->ptr.str = (char *)text; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 223 | pattern->len = strlen(text); |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 224 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 227 | /* Parse a binary written in hexa. It is allocated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 228 | int pat_parse_bin(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 229 | { |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 230 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 231 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 232 | pattern->type = SMP_T_BIN; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 233 | trash = get_trash_chunk(); |
| 234 | pattern->len = trash->size; |
| 235 | pattern->ptr.str = trash->str; |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 236 | return !!parse_binary(text, &pattern->ptr.str, &pattern->len, err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 239 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 240 | int pat_parse_reg(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 241 | { |
Thierry FOURNIER | 0b2fe4a | 2013-12-06 20:33:50 +0100 | [diff] [blame] | 242 | struct chunk *trash; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 243 | |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 244 | trash = get_trash_chunk(); |
| 245 | if (trash->size < sizeof(*pattern->ptr.reg)) { |
| 246 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 247 | (int)sizeof(*pattern->ptr.reg), trash->size); |
| 248 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 251 | pattern->ptr.reg = (struct my_regex *)trash->str; |
| 252 | pattern->ptr.reg->regstr = (char *)text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 253 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | /* Parse a range of positive integers delimited by either ':' or '-'. If only |
| 258 | * one integer is read, it is set as both min and max. An operator may be |
| 259 | * specified as the prefix, among this list of 5 : |
| 260 | * |
| 261 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 262 | * |
| 263 | * The default operator is "eq". It supports range matching. Ranges are |
| 264 | * rejected for other operators. The operator may be changed at any time. |
| 265 | * The operator is stored in the 'opaque' argument. |
| 266 | * |
| 267 | * If err is non-NULL, an error message will be returned there on errors and |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 268 | * the caller will have to free it. The function returns zero on error, and |
| 269 | * non-zero on success. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 270 | * |
| 271 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 272 | int pat_parse_int(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 273 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 274 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 275 | |
| 276 | pattern->type = SMP_T_UINT; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 277 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 278 | /* Empty string is not valid */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 279 | if (!*text) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 280 | goto not_valid_range; |
| 281 | |
| 282 | /* Search ':' or '-' separator. */ |
| 283 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 284 | ptr++; |
| 285 | |
| 286 | /* If separator not found. */ |
| 287 | if (!*ptr) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 288 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) { |
| 289 | memprintf(err, "'%s' is not a number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 292 | pattern->val.range.max = pattern->val.range.min; |
| 293 | pattern->val.range.min_set = 1; |
| 294 | pattern->val.range.max_set = 1; |
| 295 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 298 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 299 | if (ptr == text && *(ptr + 1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 300 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 301 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 302 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 303 | pattern->val.range.min_set = 0; |
| 304 | pattern->val.range.max_set = 1; |
| 305 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 308 | /* If separator is the last character. */ |
| 309 | if (*(ptr + 1) == '\0') { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 310 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 311 | goto not_valid_range; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 312 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 313 | pattern->val.range.min_set = 1; |
| 314 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 315 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 316 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 317 | |
| 318 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 319 | if (strl2llrc(text, ptr - text, &pattern->val.range.min) != 0) |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 320 | goto not_valid_range; |
| 321 | |
| 322 | if (strl2llrc(ptr + 1, strlen(ptr + 1), &pattern->val.range.max) != 0) |
| 323 | goto not_valid_range; |
| 324 | |
| 325 | if (pattern->val.range.min > pattern->val.range.max) |
| 326 | goto not_valid_range; |
| 327 | |
| 328 | pattern->val.range.min_set = 1; |
| 329 | pattern->val.range.max_set = 1; |
| 330 | return 1; |
| 331 | |
| 332 | not_valid_range: |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 333 | memprintf(err, "'%s' is not a valid number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 334 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /* Parse a range of positive 2-component versions delimited by either ':' or |
| 338 | * '-'. The version consists in a major and a minor, both of which must be |
| 339 | * smaller than 65536, because internally they will be represented as a 32-bit |
| 340 | * integer. |
| 341 | * If only one version is read, it is set as both min and max. Just like for |
| 342 | * pure integers, an operator may be specified as the prefix, among this list |
| 343 | * of 5 : |
| 344 | * |
| 345 | * 0:eq, 1:gt, 2:ge, 3:lt, 4:le |
| 346 | * |
| 347 | * The default operator is "eq". It supports range matching. Ranges are |
| 348 | * rejected for other operators. The operator may be changed at any time. |
| 349 | * The operator is stored in the 'opaque' argument. This allows constructs |
| 350 | * such as the following one : |
| 351 | * |
| 352 | * acl obsolete_ssl ssl_req_proto lt 3 |
| 353 | * acl unsupported_ssl ssl_req_proto gt 3.1 |
| 354 | * acl valid_ssl ssl_req_proto 3.0-3.1 |
| 355 | * |
| 356 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 357 | int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 358 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 359 | const char *ptr = text; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 360 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 361 | pattern->type = SMP_T_UINT; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 362 | |
| 363 | /* Search ':' or '-' separator. */ |
| 364 | while (*ptr != '\0' && *ptr != ':' && *ptr != '-') |
| 365 | ptr++; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 366 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 367 | /* If separator not found. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 368 | if (*ptr == '\0' && ptr > text) { |
| 369 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 370 | memprintf(err, "'%s' is not a dotted number", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 371 | return 0; |
| 372 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 373 | pattern->val.range.max = pattern->val.range.min; |
| 374 | pattern->val.range.min_set = 1; |
| 375 | pattern->val.range.max_set = 1; |
| 376 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 379 | /* If the separator is the first character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 380 | if (ptr == text && *(ptr+1) != '\0') { |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 381 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 382 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 383 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 384 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 385 | pattern->val.range.min_set = 0; |
| 386 | pattern->val.range.max_set = 1; |
| 387 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 390 | /* If separator is the last character. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 391 | if (ptr == &text[strlen(text)-1]) { |
| 392 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 393 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 394 | return 0; |
| 395 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 396 | pattern->val.range.min_set = 1; |
| 397 | pattern->val.range.max_set = 0; |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | /* Else, parse two numbers. */ |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 402 | if (strl2llrc_dotted(text, ptr-text, &pattern->val.range.min) != 0) { |
| 403 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | if (strl2llrc_dotted(ptr+1, strlen(ptr+1), &pattern->val.range.max) != 0) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 407 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | if (pattern->val.range.min > pattern->val.range.max) { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 411 | memprintf(err, "'%s' is not a valid dotted number range", text); |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 412 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 413 | } |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 414 | pattern->val.range.min_set = 1; |
| 415 | pattern->val.range.max_set = 1; |
| 416 | return 1; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 420 | * The addr may either be an IPv4 address or a hostname. The mask |
| 421 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 422 | * otherwise 0. NOTE: IP address patterns are typed (IPV4/IPV6). |
| 423 | */ |
Thierry FOURNIER | edc15c3 | 2013-12-13 15:36:59 +0100 | [diff] [blame] | 424 | int pat_parse_ip(const char *text, struct pattern *pattern, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 425 | { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 426 | if (str2net(text, &pattern->val.ipv4.addr, &pattern->val.ipv4.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 427 | pattern->type = SMP_T_IPV4; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 428 | return 1; |
| 429 | } |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 430 | else if (str62net(text, &pattern->val.ipv6.addr, &pattern->val.ipv6.mask)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 431 | pattern->type = SMP_T_IPV6; |
| 432 | return 1; |
| 433 | } |
| 434 | else { |
Thierry FOURNIER | 580c32c | 2014-01-24 10:58:12 +0100 | [diff] [blame] | 435 | memprintf(err, "'%s' is not a valid IPv4 or IPv6 address", text); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | } |
| 439 | |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 440 | /* |
| 441 | * |
| 442 | * These functions are exported and may be used by any other component. |
| 443 | * |
| 444 | * This fucntion just take a sample <smp> and check if this sample match |
| 445 | * with the pattern <pattern>. This fucntion return just PAT_MATCH or |
| 446 | * PAT_NOMATCH. |
| 447 | * |
| 448 | */ |
| 449 | |
| 450 | /* always return false */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 451 | struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 452 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 453 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
| 457 | /* NB: For two strings to be identical, it is required that their lengths match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 458 | struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 459 | { |
| 460 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 461 | struct ebmb_node *node; |
| 462 | char prev; |
| 463 | struct pattern_tree *elt; |
| 464 | struct pattern_list *lst; |
| 465 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 466 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 467 | /* Lookup a string in the expression's pattern tree. */ |
| 468 | if (!eb_is_empty(&expr->pattern_tree)) { |
| 469 | /* we may have to force a trailing zero on the test pattern */ |
| 470 | prev = smp->data.str.str[smp->data.str.len]; |
| 471 | if (prev) |
| 472 | smp->data.str.str[smp->data.str.len] = '\0'; |
| 473 | node = ebst_lookup(&expr->pattern_tree, smp->data.str.str); |
| 474 | if (prev) |
| 475 | smp->data.str.str[smp->data.str.len] = prev; |
| 476 | |
| 477 | if (node) { |
| 478 | if (fill) { |
| 479 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 480 | static_pattern.smp = elt->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 481 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 482 | static_pattern.flags = PAT_F_TREE; |
| 483 | static_pattern.type = SMP_T_STR; |
| 484 | static_pattern.ptr.str = (char *)elt->node.key; |
| 485 | } |
| 486 | return &static_pattern; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* look in the list */ |
| 491 | list_for_each_entry(lst, &expr->patterns, list) { |
| 492 | pattern = &lst->pat; |
| 493 | |
| 494 | if (pattern->len != smp->data.str.len) |
| 495 | continue; |
| 496 | |
| 497 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 498 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) || |
| 499 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)) |
| 500 | return pattern; |
| 501 | } |
| 502 | |
| 503 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* NB: For two binaries buf to be identical, it is required that their lengths match */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 507 | struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 508 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 509 | struct pattern_list *lst; |
| 510 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 511 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 512 | /* Look in the list. */ |
| 513 | list_for_each_entry(lst, &expr->patterns, list) { |
| 514 | pattern = &lst->pat; |
| 515 | |
| 516 | if (pattern->len != smp->data.str.len) |
| 517 | continue; |
| 518 | |
| 519 | if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0) |
| 520 | return pattern; |
| 521 | } |
| 522 | |
| 523 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 527 | * and restores the previous character when leaving. |
| 528 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 529 | struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 530 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 531 | struct pattern_list *lst; |
| 532 | struct pattern *pattern; |
| 533 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 534 | /* look in the list */ |
| 535 | list_for_each_entry(lst, &expr->patterns, list) { |
| 536 | pattern = &lst->pat; |
| 537 | |
| 538 | if (regex_exec(pattern->ptr.reg, smp->data.str.str, smp->data.str.len) == 0) |
| 539 | return pattern; |
| 540 | } |
| 541 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Checks that the pattern matches the beginning of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 545 | struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 546 | { |
| 547 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 548 | struct pattern_list *lst; |
| 549 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 550 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 551 | list_for_each_entry(lst, &expr->patterns, list) { |
| 552 | pattern = &lst->pat; |
| 553 | |
| 554 | if (pattern->len > smp->data.str.len) |
| 555 | continue; |
| 556 | |
| 557 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 558 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0) || |
| 559 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str, pattern->len) != 0)) |
| 560 | continue; |
| 561 | |
| 562 | return pattern; |
| 563 | } |
| 564 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* Checks that the pattern matches the end of the tested string. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 568 | struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 569 | { |
| 570 | int icase; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 571 | struct pattern_list *lst; |
| 572 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 573 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 574 | list_for_each_entry(lst, &expr->patterns, list) { |
| 575 | pattern = &lst->pat; |
| 576 | |
| 577 | if (pattern->len > smp->data.str.len) |
| 578 | continue; |
| 579 | |
| 580 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 581 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0) || |
| 582 | (!icase && strncmp(pattern->ptr.str, smp->data.str.str + smp->data.str.len - pattern->len, pattern->len) != 0)) |
| 583 | continue; |
| 584 | |
| 585 | return pattern; |
| 586 | } |
| 587 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* Checks that the pattern is included inside the tested string. |
| 591 | * NB: Suboptimal, should be rewritten using a Boyer-Moore method. |
| 592 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 593 | struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 594 | { |
| 595 | int icase; |
| 596 | char *end; |
| 597 | char *c; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 598 | struct pattern_list *lst; |
| 599 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 600 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 601 | list_for_each_entry(lst, &expr->patterns, list) { |
| 602 | pattern = &lst->pat; |
| 603 | |
| 604 | if (pattern->len > smp->data.str.len) |
| 605 | continue; |
| 606 | |
| 607 | end = smp->data.str.str + smp->data.str.len - pattern->len; |
| 608 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 609 | if (icase) { |
| 610 | for (c = smp->data.str.str; c <= end; c++) { |
| 611 | if (tolower(*c) != tolower(*pattern->ptr.str)) |
| 612 | continue; |
| 613 | if (strncasecmp(pattern->ptr.str, c, pattern->len) == 0) |
| 614 | return pattern; |
| 615 | } |
| 616 | } else { |
| 617 | for (c = smp->data.str.str; c <= end; c++) { |
| 618 | if (*c != *pattern->ptr.str) |
| 619 | continue; |
| 620 | if (strncmp(pattern->ptr.str, c, pattern->len) == 0) |
| 621 | return pattern; |
| 622 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 623 | } |
| 624 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 625 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* This one is used by other real functions. It checks that the pattern is |
| 629 | * included inside the tested string, but enclosed between the specified |
| 630 | * delimiters or at the beginning or end of the string. The delimiters are |
| 631 | * provided as an unsigned int made by make_4delim() and match up to 4 different |
| 632 | * delimiters. Delimiters are stripped at the beginning and end of the pattern. |
| 633 | */ |
| 634 | static int match_word(struct sample *smp, struct pattern *pattern, unsigned int delimiters) |
| 635 | { |
| 636 | int may_match, icase; |
| 637 | char *c, *end; |
| 638 | char *ps; |
| 639 | int pl; |
| 640 | |
| 641 | pl = pattern->len; |
| 642 | ps = pattern->ptr.str; |
| 643 | |
| 644 | while (pl > 0 && is_delimiter(*ps, delimiters)) { |
| 645 | pl--; |
| 646 | ps++; |
| 647 | } |
| 648 | |
| 649 | while (pl > 0 && is_delimiter(ps[pl - 1], delimiters)) |
| 650 | pl--; |
| 651 | |
| 652 | if (pl > smp->data.str.len) |
| 653 | return PAT_NOMATCH; |
| 654 | |
| 655 | may_match = 1; |
| 656 | icase = pattern->flags & PAT_F_IGNORE_CASE; |
| 657 | end = smp->data.str.str + smp->data.str.len - pl; |
| 658 | for (c = smp->data.str.str; c <= end; c++) { |
| 659 | if (is_delimiter(*c, delimiters)) { |
| 660 | may_match = 1; |
| 661 | continue; |
| 662 | } |
| 663 | |
| 664 | if (!may_match) |
| 665 | continue; |
| 666 | |
| 667 | if (icase) { |
| 668 | if ((tolower(*c) == tolower(*ps)) && |
| 669 | (strncasecmp(ps, c, pl) == 0) && |
| 670 | (c == end || is_delimiter(c[pl], delimiters))) |
| 671 | return PAT_MATCH; |
| 672 | } else { |
| 673 | if ((*c == *ps) && |
| 674 | (strncmp(ps, c, pl) == 0) && |
| 675 | (c == end || is_delimiter(c[pl], delimiters))) |
| 676 | return PAT_MATCH; |
| 677 | } |
| 678 | may_match = 0; |
| 679 | } |
| 680 | return PAT_NOMATCH; |
| 681 | } |
| 682 | |
| 683 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 684 | * between the delimiters '?' or '/' or at the beginning or end of the string. |
| 685 | * Delimiters at the beginning or end of the pattern are ignored. |
| 686 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 687 | struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 688 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 689 | struct pattern_list *lst; |
| 690 | struct pattern *pattern; |
| 691 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 692 | list_for_each_entry(lst, &expr->patterns, list) { |
| 693 | pattern = &lst->pat; |
| 694 | if (match_word(smp, pattern, make_4delim('/', '?', '?', '?'))) |
| 695 | return pattern; |
| 696 | } |
| 697 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 701 | * between the delmiters '/', '?', '.' or ":" or at the beginning or end of |
| 702 | * the string. Delimiters at the beginning or end of the pattern are ignored. |
| 703 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 704 | struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 705 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 706 | struct pattern_list *lst; |
| 707 | struct pattern *pattern; |
| 708 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 709 | list_for_each_entry(lst, &expr->patterns, list) { |
| 710 | pattern = &lst->pat; |
| 711 | if (match_word(smp, pattern, make_4delim('/', '?', '.', ':'))) |
| 712 | return pattern; |
| 713 | } |
| 714 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* Checks that the integer in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 718 | struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 719 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 720 | struct pattern_list *lst; |
| 721 | struct pattern *pattern; |
| 722 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 723 | list_for_each_entry(lst, &expr->patterns, list) { |
| 724 | pattern = &lst->pat; |
| 725 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.uint) && |
| 726 | (!pattern->val.range.max_set || smp->data.uint <= pattern->val.range.max)) |
| 727 | return pattern; |
| 728 | } |
| 729 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 733 | struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 734 | { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 735 | struct pattern_list *lst; |
| 736 | struct pattern *pattern; |
| 737 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 738 | list_for_each_entry(lst, &expr->patterns, list) { |
| 739 | pattern = &lst->pat; |
| 740 | if ((!pattern->val.range.min_set || pattern->val.range.min <= smp->data.str.len) && |
| 741 | (!pattern->val.range.max_set || smp->data.str.len <= pattern->val.range.max)) |
| 742 | return pattern; |
| 743 | } |
| 744 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 747 | struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill) |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 748 | { |
| 749 | unsigned int v4; /* in network byte order */ |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 750 | struct in6_addr tmp6; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 751 | struct in_addr *s; |
| 752 | struct ebmb_node *node; |
| 753 | struct pattern_tree *elt; |
| 754 | struct pattern_list *lst; |
| 755 | struct pattern *pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 756 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 757 | /* The input sample is IPv4. Try to match in the trees. */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 758 | if (smp->type == SMP_T_IPV4) { |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 759 | /* Lookup an IPv4 address in the expression's pattern tree using |
| 760 | * the longest match method. |
| 761 | */ |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 762 | s = &smp->data.ipv4; |
| 763 | node = ebmb_lookup_longest(&expr->pattern_tree, &s->s_addr); |
| 764 | if (node) { |
| 765 | if (fill) { |
| 766 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 767 | static_pattern.smp = elt->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 768 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 769 | static_pattern.flags = PAT_F_TREE; |
| 770 | static_pattern.type = SMP_T_IPV4; |
| 771 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 772 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 773 | return NULL; |
| 774 | } |
| 775 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 776 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 777 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 778 | /* The IPv4 sample dont match the IPv4 tree. Convert the IPv4 |
| 779 | * sample address to IPv6 with the mapping method using the ::ffff: |
| 780 | * prefix, and try to lookup in the IPv6 tree. |
| 781 | */ |
| 782 | memset(&tmp6, 0, 10); |
| 783 | *(uint16_t*)&tmp6.s6_addr[10] = htons(0xffff); |
| 784 | *(uint32_t*)&tmp6.s6_addr[12] = smp->data.ipv4.s_addr; |
| 785 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &tmp6); |
| 786 | if (node) { |
| 787 | if (fill) { |
| 788 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 789 | static_pattern.smp = elt->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 790 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 791 | static_pattern.flags = PAT_F_TREE; |
| 792 | static_pattern.type = SMP_T_IPV6; |
| 793 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 794 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
| 795 | } |
| 796 | return &static_pattern; |
| 797 | } |
| 798 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 799 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 800 | /* The input sample is IPv6. Try to match in the trees. */ |
| 801 | if (smp->type == SMP_T_IPV6) { |
| 802 | /* Lookup an IPv6 address in the expression's pattern tree using |
| 803 | * the longest match method. |
| 804 | */ |
| 805 | node = ebmb_lookup_longest(&expr->pattern_tree_2, &smp->data.ipv6); |
| 806 | if (node) { |
| 807 | if (fill) { |
| 808 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 809 | static_pattern.smp = elt->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 810 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 811 | static_pattern.flags = PAT_F_TREE; |
| 812 | static_pattern.type = SMP_T_IPV6; |
| 813 | memcpy(&static_pattern.val.ipv6.addr, elt->node.key, 16); |
| 814 | static_pattern.val.ipv6.mask = elt->node.node.pfx; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 815 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 816 | return &static_pattern; |
| 817 | } |
| 818 | |
| 819 | /* Try to convert 6 to 4 when the start of the ipv6 address match the |
| 820 | * following forms : |
| 821 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 822 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 823 | * - 2002:ip:v4:: (6to4) |
| 824 | */ |
| 825 | if ((*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 826 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 827 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 828 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) || |
| 829 | *(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 830 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0) |
| 831 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
| 832 | else |
| 833 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 834 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
| 835 | |
| 836 | /* Lookup an IPv4 address in the expression's pattern tree using the longest |
| 837 | * match method. |
| 838 | */ |
| 839 | node = ebmb_lookup_longest(&expr->pattern_tree, &v4); |
| 840 | if (node) { |
| 841 | if (fill) { |
| 842 | elt = ebmb_entry(node, struct pattern_tree, node); |
| 843 | static_pattern.smp = elt->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 844 | static_pattern.ref = elt->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 845 | static_pattern.flags = PAT_F_TREE; |
| 846 | static_pattern.type = SMP_T_IPV4; |
| 847 | memcpy(&static_pattern.val.ipv4.addr.s_addr, elt->node.key, 4); |
| 848 | if (!cidr2dotted(elt->node.node.pfx, &static_pattern.val.ipv4.mask)) |
| 849 | return NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 850 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 851 | return &static_pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 852 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 853 | } |
| 854 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 855 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 856 | /* Lookup in the list. the list contain only IPv4 patterns */ |
| 857 | list_for_each_entry(lst, &expr->patterns, list) { |
| 858 | pattern = &lst->pat; |
| 859 | |
| 860 | /* The input sample is IPv4, use it as is. */ |
| 861 | if (smp->type == SMP_T_IPV4) { |
| 862 | v4 = smp->data.ipv4.s_addr; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 863 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 864 | else if (smp->type == SMP_T_IPV6) { |
| 865 | /* v4 match on a V6 sample. We want to check at least for |
| 866 | * the following forms : |
| 867 | * - ::ffff:ip:v4 (ipv4 mapped) |
| 868 | * - ::0000:ip:v4 (old ipv4 mapped) |
| 869 | * - 2002:ip:v4:: (6to4) |
| 870 | */ |
| 871 | if (*(uint32_t*)&smp->data.ipv6.s6_addr[0] == 0 && |
| 872 | *(uint32_t*)&smp->data.ipv6.s6_addr[4] == 0 && |
| 873 | (*(uint32_t*)&smp->data.ipv6.s6_addr[8] == 0 || |
| 874 | *(uint32_t*)&smp->data.ipv6.s6_addr[8] == htonl(0xFFFF))) { |
| 875 | v4 = *(uint32_t*)&smp->data.ipv6.s6_addr[12]; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 876 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 877 | else if (*(uint16_t*)&smp->data.ipv6.s6_addr[0] == htons(0x2002)) { |
| 878 | v4 = htonl((ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[2]) << 16) + |
| 879 | ntohs(*(uint16_t*)&smp->data.ipv6.s6_addr[4])); |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 880 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 881 | else |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 882 | continue; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 883 | } |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 884 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 885 | /* Check if the input sample match the current pattern. */ |
| 886 | if (((v4 ^ pattern->val.ipv4.addr.s_addr) & pattern->val.ipv4.mask.s_addr) == 0) |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 887 | return pattern; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 888 | } |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 889 | return NULL; |
Thierry FOURNIER | e7ba236 | 2014-01-21 11:25:41 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 892 | void free_pattern_tree(struct eb_root *root) |
| 893 | { |
| 894 | struct eb_node *node, *next; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 895 | struct pattern_tree *elt; |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 896 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 897 | node = eb_first(root); |
| 898 | while (node) { |
| 899 | next = eb_next(node); |
| 900 | eb_delete(node); |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 901 | elt = container_of(node, struct pattern_tree, node); |
Thierry FOURNIER | c64de3f | 2013-12-10 15:08:39 +0100 | [diff] [blame] | 902 | free(elt->smp); |
Thierry FOURNIER | 3ce88c7 | 2013-12-09 11:29:46 +0100 | [diff] [blame] | 903 | free(elt); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 904 | node = next; |
| 905 | } |
| 906 | } |
| 907 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 908 | void pat_prune_val(struct pattern_expr *expr) |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 909 | { |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 910 | struct pattern_list *pat, *tmp; |
| 911 | |
| 912 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 913 | free(pat->pat.smp); |
| 914 | free(pat); |
| 915 | } |
| 916 | |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 917 | free_pattern_tree(&expr->pattern_tree); |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 918 | free_pattern_tree(&expr->pattern_tree_2); |
Thierry FOURNIER | d163e1c | 2013-11-28 11:41:23 +0100 | [diff] [blame] | 919 | LIST_INIT(&expr->patterns); |
| 920 | } |
| 921 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 922 | void pat_prune_ptr(struct pattern_expr *expr) |
| 923 | { |
| 924 | struct pattern_list *pat, *tmp; |
| 925 | |
| 926 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 927 | free(pat->pat.ptr.ptr); |
| 928 | free(pat->pat.smp); |
| 929 | free(pat); |
| 930 | } |
| 931 | |
| 932 | free_pattern_tree(&expr->pattern_tree); |
| 933 | free_pattern_tree(&expr->pattern_tree_2); |
| 934 | LIST_INIT(&expr->patterns); |
| 935 | } |
| 936 | |
| 937 | void pat_prune_reg(struct pattern_expr *expr) |
| 938 | { |
| 939 | struct pattern_list *pat, *tmp; |
| 940 | |
| 941 | list_for_each_entry_safe(pat, tmp, &expr->patterns, list) { |
| 942 | regex_free(pat->pat.ptr.ptr); |
| 943 | free(pat->pat.smp); |
| 944 | free(pat); |
| 945 | } |
| 946 | |
| 947 | free_pattern_tree(&expr->pattern_tree); |
| 948 | free_pattern_tree(&expr->pattern_tree_2); |
| 949 | LIST_INIT(&expr->patterns); |
| 950 | } |
| 951 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 952 | /* |
| 953 | * |
| 954 | * The following functions are used for the pattern indexation |
| 955 | * |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 956 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 957 | |
| 958 | int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 959 | { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 960 | struct pattern_list *patl; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 961 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 962 | /* allocate pattern */ |
| 963 | patl = calloc(1, sizeof(*patl)); |
| 964 | if (!patl) { |
| 965 | memprintf(err, "out of memory while indexing pattern"); |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 966 | return 0; |
| 967 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 968 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 969 | /* duplicate pattern */ |
| 970 | memcpy(&patl->pat, pat, sizeof(*pat)); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 971 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 972 | /* chain pattern in the expression */ |
| 973 | LIST_ADDQ(&expr->patterns, &patl->list); |
| 974 | |
| 975 | /* that's ok */ |
| 976 | return 1; |
| 977 | } |
| 978 | |
| 979 | int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 980 | { |
| 981 | struct pattern_list *patl; |
| 982 | |
| 983 | /* allocate pattern */ |
| 984 | patl = calloc(1, sizeof(*patl)); |
| 985 | if (!patl) |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 986 | return 0; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 987 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 988 | /* duplicate pattern */ |
| 989 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 990 | patl->pat.ptr.ptr = malloc(patl->pat.len); |
| 991 | if (!patl->pat.ptr.ptr) { |
| 992 | free(patl); |
| 993 | memprintf(err, "out of memory while indexing pattern"); |
| 994 | return 0; |
| 995 | } |
| 996 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 997 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 998 | /* chain pattern in the expression */ |
| 999 | LIST_ADDQ(&expr->patterns, &patl->list); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1000 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1001 | /* that's ok */ |
| 1002 | return 1; |
| 1003 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1004 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1005 | int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1006 | { |
| 1007 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1008 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1009 | /* allocate pattern */ |
| 1010 | patl = calloc(1, sizeof(*patl)); |
| 1011 | if (!patl) { |
| 1012 | memprintf(err, "out of memory while indexing pattern"); |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | /* duplicate pattern */ |
| 1017 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1018 | patl->pat.ptr.str = malloc(patl->pat.len + 1); |
| 1019 | if (!patl->pat.ptr.str) { |
| 1020 | free(patl); |
| 1021 | memprintf(err, "out of memory while indexing pattern"); |
| 1022 | return 0; |
| 1023 | } |
| 1024 | memcpy(patl->pat.ptr.ptr, pat->ptr.ptr, pat->len); |
| 1025 | patl->pat.ptr.str[patl->pat.len] = '\0'; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1026 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1027 | /* chain pattern in the expression */ |
| 1028 | LIST_ADDQ(&expr->patterns, &patl->list); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1029 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1030 | /* that's ok */ |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1035 | { |
| 1036 | struct pattern_list *patl; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1037 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1038 | /* allocate pattern */ |
| 1039 | patl = calloc(1, sizeof(*patl)); |
| 1040 | if (!patl) { |
| 1041 | memprintf(err, "out of memory while indexing pattern"); |
| 1042 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1043 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1044 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1045 | /* duplicate pattern */ |
| 1046 | memcpy(&patl->pat, pat, sizeof(*pat)); |
| 1047 | |
| 1048 | /* allocate regex */ |
| 1049 | patl->pat.ptr.reg = calloc(1, sizeof(*patl->pat.ptr.reg)); |
| 1050 | if (!patl->pat.ptr.reg) { |
| 1051 | free(patl); |
| 1052 | memprintf(err, "out of memory while indexing pattern"); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | /* compile regex */ |
| 1057 | if (!regex_comp(pat->ptr.reg->regstr, patl->pat.ptr.reg, !(patl->pat.flags & PAT_F_IGNORE_CASE), 0, err)) { |
| 1058 | free(patl); |
| 1059 | free(patl->pat.ptr.reg); |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1063 | /* chain pattern in the expression */ |
| 1064 | LIST_ADDQ(&expr->patterns, &patl->list); |
| 1065 | |
| 1066 | /* that's ok */ |
| 1067 | return 1; |
| 1068 | } |
| 1069 | |
| 1070 | int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1071 | { |
| 1072 | unsigned int mask; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1073 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1074 | |
| 1075 | /* Only IPv4 can be indexed */ |
| 1076 | if (pat->type == SMP_T_IPV4) { |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1077 | /* in IPv4 case, check if the mask is contiguous so that we can |
| 1078 | * insert the network into the tree. A continuous mask has only |
| 1079 | * ones on the left. This means that this mask + its lower bit |
| 1080 | * added once again is null. |
| 1081 | */ |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1082 | mask = ntohl(pat->val.ipv4.mask.s_addr); |
| 1083 | if (mask + (mask & -mask) == 0) { |
| 1084 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1085 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1086 | /* node memory allocation */ |
| 1087 | node = calloc(1, sizeof(*node) + 4); |
| 1088 | if (!node) { |
| 1089 | memprintf(err, "out of memory while loading pattern"); |
| 1090 | return 0; |
| 1091 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1092 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1093 | /* copy the pointer to sample associated to this node */ |
| 1094 | node->smp = pat->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1095 | node->ref = pat->ref; |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1096 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1097 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1098 | memcpy(node->node.key, &pat->val.ipv4.addr, 4); /* network byte order */ |
| 1099 | node->node.node.pfx = mask; |
| 1100 | if (ebmb_insert_prefix(&expr->pattern_tree, &node->node, 4) != &node->node) |
| 1101 | free(node); /* was a duplicate */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1102 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1103 | /* that's ok */ |
| 1104 | return 1; |
| 1105 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1106 | else { |
| 1107 | /* If the mask is not contiguous, just add the pattern to the list */ |
| 1108 | return pat_idx_list_val(expr, pat, err); |
| 1109 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1110 | } |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1111 | else if (pat->type == SMP_T_IPV6) { |
| 1112 | /* IPv6 also can be indexed */ |
| 1113 | node = calloc(1, sizeof(*node) + 16); |
| 1114 | if (!node) { |
| 1115 | memprintf(err, "out of memory while loading pattern"); |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | /* copy the pointer to sample associated to this node */ |
| 1120 | node->smp = pat->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1121 | node->ref = pat->ref; |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1122 | |
| 1123 | /* FIXME: insert <addr>/<mask> into the tree here */ |
| 1124 | memcpy(node->node.key, &pat->val.ipv6.addr, 16); /* network byte order */ |
| 1125 | node->node.node.pfx = pat->val.ipv6.mask; |
| 1126 | if (ebmb_insert_prefix(&expr->pattern_tree_2, &node->node, 16) != &node->node) |
| 1127 | free(node); /* was a duplicate */ |
| 1128 | |
| 1129 | /* that's ok */ |
| 1130 | return 1; |
| 1131 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1132 | |
Thierry FOURNIER | 33a7433 | 2013-12-19 23:54:54 +0100 | [diff] [blame] | 1133 | return 0; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err) |
| 1137 | { |
| 1138 | int len; |
Thierry FOURNIER | e1bcac5 | 2013-12-13 16:09:50 +0100 | [diff] [blame] | 1139 | struct pattern_tree *node; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1140 | |
| 1141 | /* Only string can be indexed */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1142 | if (pat->type != SMP_T_STR) { |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1143 | memprintf(err, "internal error: string expected, but the type is '%s'", |
| 1144 | smp_to_type[pat->type]); |
| 1145 | return 0; |
Thierry FOURNIER | 972028f | 2014-01-23 17:53:31 +0100 | [diff] [blame] | 1146 | } |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1147 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1148 | /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */ |
| 1149 | if (pat->flags & PAT_F_IGNORE_CASE) |
| 1150 | return pat_idx_list_str(expr, pat, err); |
Thierry FOURNIER | 7148ce6 | 2013-12-06 19:06:43 +0100 | [diff] [blame] | 1151 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1152 | /* Process the key len */ |
| 1153 | len = strlen(pat->ptr.str) + 1; |
| 1154 | |
| 1155 | /* node memory allocation */ |
| 1156 | node = calloc(1, sizeof(*node) + len); |
| 1157 | if (!node) { |
| 1158 | memprintf(err, "out of memory while loading pattern"); |
| 1159 | return 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1160 | } |
| 1161 | |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1162 | /* copy the pointer to sample associated to this node */ |
| 1163 | node->smp = pat->smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1164 | node->ref = pat->ref; |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1165 | |
| 1166 | /* copy the string */ |
| 1167 | memcpy(node->node.key, pat->ptr.str, len); |
| 1168 | |
| 1169 | /* index the new node */ |
| 1170 | if (ebst_insert(&expr->pattern_tree, &node->node) != &node->node) |
| 1171 | free(node); /* was a duplicate */ |
| 1172 | |
| 1173 | /* that's ok */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1174 | return 1; |
| 1175 | } |
| 1176 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 1177 | struct sample_storage **pat_find_smp_list_val(struct pattern_expr *expr, struct pattern *pattern) |
| 1178 | { |
| 1179 | struct pattern_list *pat; |
| 1180 | struct pattern_list *safe; |
| 1181 | |
| 1182 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1183 | |
| 1184 | /* Check equality. */ |
| 1185 | if (pattern->val.range.min_set != pat->pat.val.range.min_set) |
| 1186 | continue; |
| 1187 | if (pattern->val.range.max_set != pat->pat.val.range.max_set) |
| 1188 | continue; |
| 1189 | if (pattern->val.range.min_set && |
| 1190 | pattern->val.range.min != pat->pat.val.range.min) |
| 1191 | continue; |
| 1192 | if (pattern->val.range.max_set && |
| 1193 | pattern->val.range.max != pat->pat.val.range.max) |
| 1194 | continue; |
| 1195 | |
| 1196 | /* Return the pointer on the sample pointer. */ |
| 1197 | return &pat->pat.smp; |
| 1198 | } |
| 1199 | |
| 1200 | return NULL; |
| 1201 | } |
| 1202 | |
| 1203 | struct sample_storage **pat_find_smp_tree_ip(struct pattern_expr *expr, struct pattern *pattern) |
| 1204 | { |
| 1205 | struct ebmb_node *node, *next_node; |
| 1206 | struct pattern_tree *elt; |
| 1207 | struct pattern_list *pat; |
| 1208 | struct pattern_list *safe; |
| 1209 | unsigned int mask; |
| 1210 | |
| 1211 | /* browse each node of the tree for IPv4 addresses. */ |
| 1212 | if (pattern->type == SMP_T_IPV4) { |
| 1213 | /* Convert mask. If the mask is contiguous, browse each node |
| 1214 | * of the tree for IPv4 addresses. |
| 1215 | */ |
| 1216 | mask = ntohl(pattern->val.ipv4.mask.s_addr); |
| 1217 | if (mask + (mask & -mask) == 0) { |
| 1218 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1219 | |
| 1220 | for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node); |
| 1221 | node; |
| 1222 | node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) { |
| 1223 | /* Extract container of the tree node. */ |
| 1224 | elt = container_of(node, struct pattern_tree, node); |
| 1225 | |
| 1226 | /* Check equality. */ |
| 1227 | if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0) |
| 1228 | continue; |
| 1229 | |
| 1230 | /* Return the pointer on the sample pointer. */ |
| 1231 | return &elt->smp; |
| 1232 | } |
| 1233 | } |
| 1234 | else { |
| 1235 | /* Browse each node of the list for IPv4 addresses. */ |
| 1236 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1237 | /* Check equality. */ |
| 1238 | if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr, |
| 1239 | sizeof(pat->pat.val.ipv4.addr)) != 0) |
| 1240 | continue; |
| 1241 | if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask, |
| 1242 | sizeof(pat->pat.val.ipv4.addr)) != 0) |
| 1243 | continue; |
| 1244 | |
| 1245 | /* Return the pointer on the sample pointer. */ |
| 1246 | return &pat->pat.smp; |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | else if (pattern->type == SMP_T_IPV6) { |
| 1251 | /* browse each node of the tree for IPv4 addresses. */ |
| 1252 | for (node = ebmb_first(&expr->pattern_tree_2), next_node = ebmb_next(node); |
| 1253 | node; |
| 1254 | node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) { |
| 1255 | /* Extract container of the tree node. */ |
| 1256 | elt = container_of(node, struct pattern_tree, node); |
| 1257 | |
| 1258 | /* Check equality. */ |
| 1259 | if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0) |
| 1260 | continue; |
| 1261 | |
| 1262 | /* Return the pointer on the sample pointer. */ |
| 1263 | return &elt->smp; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
| 1270 | struct sample_storage **pat_find_smp_list_ptr(struct pattern_expr *expr, struct pattern *pattern) |
| 1271 | { |
| 1272 | struct pattern_list *pat; |
| 1273 | struct pattern_list *safe; |
| 1274 | |
| 1275 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1276 | /* Check equality. */ |
| 1277 | if (pattern->len != pat->pat.len) |
| 1278 | continue; |
| 1279 | if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0) |
| 1280 | continue; |
| 1281 | |
| 1282 | /* Return the pointer on the sample pointer. */ |
| 1283 | return &pat->pat.smp; |
| 1284 | } |
| 1285 | |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | |
| 1289 | struct sample_storage **pat_find_smp_tree_str(struct pattern_expr *expr, struct pattern *pattern) |
| 1290 | { |
| 1291 | struct ebmb_node *node, *next_node; |
| 1292 | struct pattern_tree *elt; |
| 1293 | |
| 1294 | /* browse each node of the tree. */ |
| 1295 | for (node = ebmb_first(&expr->pattern_tree), next_node = ebmb_next(node); |
| 1296 | node; |
| 1297 | node = next_node, next_node = next_node ? ebmb_next(next_node) : NULL) { |
| 1298 | /* Extract container of the tree node. */ |
| 1299 | elt = container_of(node, struct pattern_tree, node); |
| 1300 | |
| 1301 | /* Check equality. */ |
| 1302 | if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0) |
| 1303 | continue; |
| 1304 | |
| 1305 | /* Return the pointer on the sample pointer. */ |
| 1306 | return &elt->smp; |
| 1307 | } |
| 1308 | |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
| 1312 | struct sample_storage **pat_find_smp_list_str(struct pattern_expr *expr, struct pattern *pattern) |
| 1313 | { |
| 1314 | struct pattern_list *pat; |
| 1315 | struct pattern_list *safe; |
| 1316 | |
| 1317 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1318 | /* Check equality. */ |
| 1319 | if (pattern->len != pat->pat.len) |
| 1320 | continue; |
| 1321 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1322 | if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
| 1323 | continue; |
| 1324 | } |
| 1325 | else { |
| 1326 | if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
| 1330 | /* Return the pointer on the sample pointer. */ |
| 1331 | return &pat->pat.smp; |
| 1332 | } |
| 1333 | |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | |
| 1337 | struct sample_storage **pat_find_smp_list_reg(struct pattern_expr *expr, struct pattern *pattern) |
| 1338 | { |
| 1339 | struct pattern_list *pat; |
| 1340 | struct pattern_list *safe; |
| 1341 | |
| 1342 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1343 | /* Check equality. */ |
| 1344 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1345 | if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
| 1346 | continue; |
| 1347 | } |
| 1348 | else { |
| 1349 | if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
| 1350 | continue; |
| 1351 | } |
| 1352 | |
| 1353 | /* Return the pointer on the sample pointer. */ |
| 1354 | return &pat->pat.smp; |
| 1355 | } |
| 1356 | |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1360 | void pat_del_list_val(struct pattern_expr *expr, struct pattern *pattern) |
| 1361 | { |
| 1362 | struct pattern_list *pat; |
| 1363 | struct pattern_list *safe; |
| 1364 | |
| 1365 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1366 | /* Check equality. */ |
| 1367 | if (pattern->val.range.min_set != pat->pat.val.range.min_set) |
| 1368 | continue; |
| 1369 | if (pattern->val.range.max_set != pat->pat.val.range.max_set) |
| 1370 | continue; |
| 1371 | if (pattern->val.range.min_set && |
| 1372 | pattern->val.range.min != pat->pat.val.range.min) |
| 1373 | continue; |
| 1374 | if (pattern->val.range.max_set && |
| 1375 | pattern->val.range.max != pat->pat.val.range.max) |
| 1376 | continue; |
| 1377 | |
| 1378 | /* Delete and free entry. */ |
| 1379 | LIST_DEL(&pat->list); |
| 1380 | free(pat->pat.smp); |
| 1381 | free(pat); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | void pat_del_tree_ip(struct pattern_expr *expr, struct pattern *pattern) |
| 1386 | { |
| 1387 | struct ebmb_node *node, *next_node; |
| 1388 | struct pattern_tree *elt; |
| 1389 | struct pattern_list *pat; |
| 1390 | struct pattern_list *safe; |
| 1391 | unsigned int mask; |
| 1392 | |
| 1393 | /* browse each node of the tree for IPv4 addresses. */ |
| 1394 | if (pattern->type == SMP_T_IPV4) { |
| 1395 | /* Convert mask. If the mask is contiguous, browse each node |
| 1396 | * of the tree for IPv4 addresses. |
| 1397 | */ |
| 1398 | mask = ntohl(pattern->val.ipv4.mask.s_addr); |
| 1399 | if (mask + (mask & -mask) == 0) { |
| 1400 | mask = mask ? 33 - flsnz(mask & -mask) : 0; /* equals cidr value */ |
| 1401 | |
| 1402 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1403 | node; |
| 1404 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1405 | /* Extract container of the tree node. */ |
| 1406 | elt = container_of(node, struct pattern_tree, node); |
| 1407 | |
| 1408 | /* Check equality. */ |
| 1409 | if (memcmp(&pattern->val.ipv4.addr, elt->node.key, |
| 1410 | sizeof(pattern->val.ipv4.addr)) != 0) |
| 1411 | continue; |
| 1412 | if (elt->node.node.pfx != mask) |
| 1413 | continue; |
| 1414 | |
| 1415 | /* Delete and free entry. */ |
| 1416 | ebmb_delete(node); |
| 1417 | free(elt->smp); |
| 1418 | free(elt); |
| 1419 | } |
| 1420 | } |
| 1421 | else { |
| 1422 | /* Browse each node of the list for IPv4 addresses. */ |
| 1423 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1424 | /* Check equality, addr then mask */ |
| 1425 | if (memcmp(&pattern->val.ipv4.addr, &pat->pat.val.ipv4.addr, |
| 1426 | sizeof(pat->pat.val.ipv4.addr)) != 0) |
| 1427 | continue; |
| 1428 | |
| 1429 | if (memcmp(&pattern->val.ipv4.mask, &pat->pat.val.ipv4.mask, |
| 1430 | sizeof(pat->pat.val.ipv4.addr)) != 0) |
| 1431 | continue; |
| 1432 | |
| 1433 | /* Delete and free entry. */ |
| 1434 | LIST_DEL(&pat->list); |
| 1435 | free(pat->pat.smp); |
| 1436 | free(pat); |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | else if (pattern->type == SMP_T_IPV6) { |
| 1441 | /* browse each node of the tree for IPv6 addresses. */ |
| 1442 | for (node = ebmb_first(&expr->pattern_tree_2), next_node = node ? ebmb_next(node) : NULL; |
| 1443 | node; |
| 1444 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1445 | /* Extract container of the tree node. */ |
| 1446 | elt = container_of(node, struct pattern_tree, node); |
| 1447 | |
| 1448 | /* Check equality. */ |
| 1449 | if (memcmp(&pattern->val.ipv6.addr, elt->node.key, |
| 1450 | sizeof(pattern->val.ipv6.addr)) != 0) |
| 1451 | continue; |
| 1452 | if (elt->node.node.pfx != pattern->val.ipv6.mask) |
| 1453 | continue; |
| 1454 | |
| 1455 | /* Delete and free entry. */ |
| 1456 | ebmb_delete(node); |
| 1457 | free(elt->smp); |
| 1458 | free(elt); |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | void pat_del_list_ptr(struct pattern_expr *expr, struct pattern *pattern) |
| 1464 | { |
| 1465 | struct pattern_list *pat; |
| 1466 | struct pattern_list *safe; |
| 1467 | |
| 1468 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1469 | /* Check equality. */ |
| 1470 | if (pattern->len != pat->pat.len) |
| 1471 | continue; |
| 1472 | if (memcmp(pattern->ptr.ptr, pat->pat.ptr.ptr, pat->pat.len) != 0) |
| 1473 | continue; |
| 1474 | |
| 1475 | /* Delete and free entry. */ |
| 1476 | LIST_DEL(&pat->list); |
| 1477 | free(pat->pat.ptr.ptr); |
| 1478 | free(pat->pat.smp); |
| 1479 | free(pat); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | void pat_del_tree_str(struct pattern_expr *expr, struct pattern *pattern) |
| 1484 | { |
| 1485 | struct ebmb_node *node, *next_node; |
| 1486 | struct pattern_tree *elt; |
| 1487 | |
| 1488 | /* browse each node of the tree. */ |
| 1489 | for (node = ebmb_first(&expr->pattern_tree), next_node = node ? ebmb_next(node) : NULL; |
| 1490 | node; |
| 1491 | node = next_node, next_node = node ? ebmb_next(node) : NULL) { |
| 1492 | /* Extract container of the tree node. */ |
| 1493 | elt = container_of(node, struct pattern_tree, node); |
| 1494 | |
| 1495 | /* Check equality. */ |
| 1496 | if (strcmp(pattern->ptr.str, (char *)elt->node.key) != 0) |
| 1497 | continue; |
| 1498 | |
| 1499 | /* Delete and free entry. */ |
| 1500 | ebmb_delete(node); |
| 1501 | free(elt->smp); |
| 1502 | free(elt); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | void pat_del_list_str(struct pattern_expr *expr, struct pattern *pattern) |
| 1507 | { |
| 1508 | struct pattern_list *pat; |
| 1509 | struct pattern_list *safe; |
| 1510 | |
| 1511 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1512 | /* Check equality. */ |
| 1513 | if (pattern->len != pat->pat.len) |
| 1514 | continue; |
| 1515 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1516 | if (strncasecmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
| 1517 | continue; |
| 1518 | } |
| 1519 | else { |
| 1520 | if (strncmp(pattern->ptr.str, pat->pat.ptr.str, pat->pat.len) != 0) |
| 1521 | continue; |
| 1522 | } |
| 1523 | |
| 1524 | /* Delete and free entry. */ |
| 1525 | LIST_DEL(&pat->list); |
| 1526 | free(pat->pat.ptr.str); |
| 1527 | free(pat->pat.smp); |
| 1528 | free(pat); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | void pat_del_list_reg(struct pattern_expr *expr, struct pattern *pattern) |
| 1533 | { |
| 1534 | struct pattern_list *pat; |
| 1535 | struct pattern_list *safe; |
| 1536 | |
| 1537 | list_for_each_entry_safe(pat, safe, &expr->patterns, list) { |
| 1538 | /* Check equality. */ |
| 1539 | if (pat->pat.flags & PAT_F_IGNORE_CASE) { |
| 1540 | if (strcasecmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
| 1541 | continue; |
| 1542 | } |
| 1543 | else { |
| 1544 | if (strcmp(pattern->ptr.reg->regstr, pat->pat.ptr.reg->regstr) != 0) |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
| 1548 | /* Delete and free entry. */ |
| 1549 | LIST_DEL(&pat->list); |
| 1550 | regex_free(pat->pat.ptr.ptr); |
| 1551 | free(pat->pat.smp); |
| 1552 | free(pat); |
| 1553 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | void pattern_init_expr(struct pattern_expr *expr) |
| 1557 | { |
| 1558 | LIST_INIT(&expr->patterns); |
| 1559 | expr->pattern_tree = EB_ROOT_UNIQUE; |
| 1560 | expr->pattern_tree_2 = EB_ROOT_UNIQUE; |
| 1561 | } |
| 1562 | |
| 1563 | void pattern_init_head(struct pattern_head *head) |
| 1564 | { |
| 1565 | LIST_INIT(&head->head); |
| 1566 | } |
| 1567 | |
| 1568 | /* The following functions are relative to the management of the reference |
| 1569 | * lists. These lists are used to store the original pattern and associated |
| 1570 | * value as string form. |
| 1571 | * |
| 1572 | * This is used with modifiable ACL and MAPS |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1573 | * |
| 1574 | * The pattern reference are stored with two identifiers: the unique_id and |
| 1575 | * the reference. |
| 1576 | * |
| 1577 | * The reference identify a file. Each file with the same name point to the |
| 1578 | * same reference. We can register many times one file. If the file is modified, |
| 1579 | * all his dependencies are also modified. The reference can be used with map or |
| 1580 | * acl. |
| 1581 | * |
| 1582 | * The unique_id identify inline acl. The unique id is unique for each acl. |
| 1583 | * You cannot force the same id in the configuration file, because this repoort |
| 1584 | * an error. |
| 1585 | * |
| 1586 | * A particular case appears if the filename is a number. In this case, the |
| 1587 | * unique_id is set with the number represented by the filename and the |
| 1588 | * reference is also set. This method prevent double unique_id. |
| 1589 | * |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1590 | */ |
| 1591 | |
| 1592 | /* This function lookup for reference. If the reference is found, they return |
| 1593 | * pointer to the struct pat_ref, else return NULL. |
| 1594 | */ |
| 1595 | struct pat_ref *pat_ref_lookup(const char *reference) |
| 1596 | { |
| 1597 | struct pat_ref *ref; |
| 1598 | |
| 1599 | list_for_each_entry(ref, &pattern_reference, list) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1600 | if (ref->reference && strcmp(reference, ref->reference) == 0) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1601 | return ref; |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1605 | /* This function lookup for unique id. If the reference is found, they return |
| 1606 | * pointer to the struct pat_ref, else return NULL. |
| 1607 | */ |
| 1608 | struct pat_ref *pat_ref_lookupid(int unique_id) |
| 1609 | { |
| 1610 | struct pat_ref *ref; |
| 1611 | |
| 1612 | list_for_each_entry(ref, &pattern_reference, list) |
| 1613 | if (ref->unique_id == unique_id) |
| 1614 | return ref; |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1618 | /* This function remove all pattern match <key> from the the reference |
| 1619 | * and from each expr member of the reference. This fucntion returns 1 |
| 1620 | * if the deletion is done and return 0 is the entry is not found. |
| 1621 | */ |
| 1622 | int pat_ref_delete(struct pat_ref *ref, const char *key) |
| 1623 | { |
| 1624 | struct pattern_expr *expr; |
| 1625 | struct pat_ref_elt *elt, *safe; |
| 1626 | int found = 0; |
| 1627 | |
| 1628 | /* delete pattern from reference */ |
| 1629 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1630 | if (strcmp(key, elt->pattern) == 0) { |
| 1631 | LIST_DEL(&elt->list); |
| 1632 | free(elt->sample); |
| 1633 | free(elt->pattern); |
| 1634 | free(elt); |
| 1635 | found = 1; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | if (!found) |
| 1640 | return 0; |
| 1641 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1642 | list_for_each_entry(expr, &ref->pat, list) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1643 | pattern_delete(key, expr, NULL); |
| 1644 | |
| 1645 | return 1; |
| 1646 | } |
| 1647 | |
| 1648 | /* This function modify the sample of the first pattern that match the <key>. */ |
| 1649 | int pat_ref_set(struct pat_ref *ref, const char *key, const char *value) |
| 1650 | { |
| 1651 | struct pattern_expr *expr; |
| 1652 | struct pat_ref_elt *elt; |
| 1653 | struct sample_storage **smp; |
| 1654 | char *sample; |
| 1655 | int found = 0; |
| 1656 | |
| 1657 | /* modify pattern from reference */ |
| 1658 | list_for_each_entry(elt, &ref->head, list) { |
| 1659 | if (strcmp(key, elt->pattern) == 0) { |
| 1660 | sample = strdup(value); |
| 1661 | if (!sample) |
| 1662 | return 0; |
| 1663 | free(elt->sample); |
| 1664 | elt->sample = sample; |
| 1665 | found = 1; |
| 1666 | break; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | if (!found) |
| 1671 | return 0; |
| 1672 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1673 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1674 | smp = pattern_find_smp(key, expr, NULL); |
| 1675 | if (smp && expr->pat_head->parse_smp) |
| 1676 | if (!expr->pat_head->parse_smp(value, *smp)) |
| 1677 | *smp = NULL; |
| 1678 | } |
| 1679 | |
| 1680 | return 1; |
| 1681 | } |
| 1682 | |
| 1683 | /* This function create new reference. <ref> is the reference name. |
| 1684 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1685 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1686 | * before calling this function. If the fucntion fail, it return NULL, |
| 1687 | * else return new struct pat_ref. |
| 1688 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1689 | struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1690 | { |
| 1691 | struct pat_ref *ref; |
| 1692 | |
| 1693 | ref = malloc(sizeof(*ref)); |
| 1694 | if (!ref) |
| 1695 | return NULL; |
| 1696 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1697 | if (display) { |
| 1698 | ref->display = strdup(display); |
| 1699 | if (!ref->display) { |
| 1700 | free(ref); |
| 1701 | return NULL; |
| 1702 | } |
| 1703 | } |
| 1704 | else |
| 1705 | ref->display = NULL; |
| 1706 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1707 | ref->reference = strdup(reference); |
| 1708 | if (!ref->reference) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1709 | free(ref->display); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1710 | free(ref); |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1714 | ref->flags = flags; |
| 1715 | ref->unique_id = -1; |
| 1716 | |
| 1717 | LIST_INIT(&ref->head); |
| 1718 | LIST_INIT(&ref->pat); |
| 1719 | |
| 1720 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1721 | |
| 1722 | return ref; |
| 1723 | } |
| 1724 | |
| 1725 | /* This function create new reference. <unique_id> is the unique id. If |
| 1726 | * the value of <unique_id> is -1, the unique id is calculated later. |
| 1727 | * <flags> are PAT_REF_*. /!\ The reference is not checked, and must |
| 1728 | * be unique. The user must check the reference with "pat_ref_lookup()" |
| 1729 | * or pat_ref_lookupid before calling this function. If the function |
| 1730 | * fail, it return NULL, else return new struct pat_ref. |
| 1731 | */ |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1732 | struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags) |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1733 | { |
| 1734 | struct pat_ref *ref; |
| 1735 | |
| 1736 | ref = malloc(sizeof(*ref)); |
| 1737 | if (!ref) |
| 1738 | return NULL; |
| 1739 | |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 1740 | if (display) { |
| 1741 | ref->display = strdup(display); |
| 1742 | if (!ref->display) { |
| 1743 | free(ref); |
| 1744 | return NULL; |
| 1745 | } |
| 1746 | } |
| 1747 | else |
| 1748 | ref->display = NULL; |
| 1749 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1750 | ref->reference = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1751 | ref->flags = flags; |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 1752 | ref->unique_id = unique_id; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1753 | LIST_INIT(&ref->head); |
| 1754 | LIST_INIT(&ref->pat); |
| 1755 | |
| 1756 | LIST_ADDQ(&pattern_reference, &ref->list); |
| 1757 | |
| 1758 | return ref; |
| 1759 | } |
| 1760 | |
| 1761 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1762 | * If the function fails, it returns 0. |
| 1763 | */ |
| 1764 | int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line) |
| 1765 | { |
| 1766 | struct pat_ref_elt *elt; |
| 1767 | |
| 1768 | elt = malloc(sizeof(*elt)); |
| 1769 | if (!elt) |
| 1770 | return 0; |
| 1771 | |
| 1772 | elt->line = line; |
| 1773 | |
| 1774 | elt->pattern = strdup(pattern); |
| 1775 | if (!elt->pattern) { |
| 1776 | free(elt); |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | if (sample) { |
| 1781 | elt->sample = strdup(sample); |
| 1782 | if (!elt->sample) { |
| 1783 | free(elt->pattern); |
| 1784 | free(elt); |
| 1785 | return 0; |
| 1786 | } |
| 1787 | } |
| 1788 | else |
| 1789 | elt->sample = NULL; |
| 1790 | |
| 1791 | LIST_ADDQ(&ref->head, &elt->list); |
| 1792 | |
| 1793 | return 1; |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 1794 | } |
| 1795 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1796 | /* This function create sample found in <elt>, parse the pattern also |
| 1797 | * found in <elt> and insert it in <expr>. The function copy <patflags> |
| 1798 | * in <expr>. If the function fails, it returns0 and <err> is filled. |
| 1799 | * In succes case, the function returns 1. |
| 1800 | */ |
| 1801 | static inline |
| 1802 | int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, |
| 1803 | int patflags, char **err) |
| 1804 | { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1805 | struct sample_storage *smp; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1806 | struct pattern pattern; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* Create sample */ |
| 1809 | if (elt->sample && expr->pat_head->parse_smp) { |
| 1810 | /* New sample. */ |
| 1811 | smp = malloc(sizeof(*smp)); |
| 1812 | if (!smp) |
| 1813 | return 0; |
| 1814 | |
| 1815 | /* Parse value. */ |
| 1816 | if (!expr->pat_head->parse_smp(elt->sample, smp)) { |
| 1817 | memprintf(err, "unable to parse '%s'", elt->sample); |
| 1818 | free(smp); |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | } |
| 1823 | else |
| 1824 | smp = NULL; |
| 1825 | |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1826 | /* initialise pattern */ |
| 1827 | memset(&pattern, 0, sizeof(pattern)); |
| 1828 | pattern.flags = patflags; |
| 1829 | pattern.smp = smp; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 1830 | pattern.ref = elt; |
Thierry FOURNIER | d25c842 | 2014-01-28 15:34:35 +0100 | [diff] [blame] | 1831 | |
| 1832 | /* parse pattern */ |
| 1833 | if (!expr->pat_head->parse(elt->pattern, &pattern, err)) { |
| 1834 | free(smp); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | /* index pattern */ |
| 1839 | if (!expr->pat_head->index(expr, &pattern, err)) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1840 | free(smp); |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1841 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1842 | } |
Thierry FOURNIER | b9b0846 | 2013-12-13 15:12:32 +0100 | [diff] [blame] | 1843 | |
| 1844 | return 1; |
| 1845 | } |
| 1846 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1847 | /* This function adds entry to <ref>. It can failed with memory error. |
| 1848 | * The new entry is added at all the pattern_expr registered in this |
| 1849 | * reference. The function stop on the first error encountered. It |
| 1850 | * returns 0 and err is filled. |
| 1851 | * |
| 1852 | * If an error is encountered, The complete add operation is cancelled. |
| 1853 | */ |
| 1854 | int pat_ref_add(struct pat_ref *ref, |
| 1855 | const char *pattern, const char *sample, |
| 1856 | char **err) |
| 1857 | { |
| 1858 | struct pat_ref_elt *elt; |
| 1859 | struct pattern_expr *expr; |
| 1860 | |
| 1861 | elt = malloc(sizeof(*elt)); |
| 1862 | if (!elt) { |
| 1863 | memprintf(err, "out of memory error"); |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| 1867 | elt->line = -1; |
| 1868 | |
| 1869 | elt->pattern = strdup(pattern); |
| 1870 | if (!elt->pattern) { |
| 1871 | free(elt); |
| 1872 | memprintf(err, "out of memory error"); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | if (sample) { |
| 1877 | elt->sample = strdup(sample); |
| 1878 | if (!elt->sample) { |
| 1879 | free(elt->pattern); |
| 1880 | free(elt); |
| 1881 | memprintf(err, "out of memory error"); |
| 1882 | return 0; |
| 1883 | } |
| 1884 | } |
| 1885 | else |
| 1886 | elt->sample = NULL; |
| 1887 | |
| 1888 | LIST_ADDQ(&ref->head, &elt->list); |
| 1889 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1890 | list_for_each_entry(expr, &ref->pat, list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1891 | if (!pat_ref_push(elt, expr, 0, err)) { |
| 1892 | /* Try to delete all the added entries. */ |
| 1893 | pat_ref_delete(ref, pattern); |
| 1894 | return 0; |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | return 1; |
| 1899 | } |
| 1900 | |
| 1901 | /* This function prune all entries of <ref>. This function |
| 1902 | * prune the associated pattern_expr. |
| 1903 | */ |
| 1904 | void pat_ref_prune(struct pat_ref *ref) |
| 1905 | { |
| 1906 | struct pat_ref_elt *elt, *safe; |
| 1907 | struct pattern_expr *expr; |
| 1908 | |
| 1909 | list_for_each_entry_safe(elt, safe, &ref->head, list) { |
| 1910 | LIST_DEL(&elt->list); |
| 1911 | free(elt->pattern); |
| 1912 | free(elt->sample); |
| 1913 | free(elt); |
| 1914 | } |
| 1915 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1916 | list_for_each_entry(expr, &ref->pat, list) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1917 | expr->pat_head->prune(expr); |
| 1918 | } |
| 1919 | |
| 1920 | /* This function browse <ref> and try to index each entries in the <expr>. |
| 1921 | * If the flag <soe> (stop on error) is set, this function stop on the first |
| 1922 | * error, <err> is filled and return 0. If is not set, the function try to |
| 1923 | * load each entries and 1 is always returned. |
| 1924 | */ |
| 1925 | int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, |
| 1926 | int patflags, int soe, char **err) |
| 1927 | { |
| 1928 | struct pat_ref_elt *elt; |
| 1929 | |
| 1930 | list_for_each_entry(elt, &ref->head, list) { |
| 1931 | if (soe && !pat_ref_push(elt, expr, patflags, err)) { |
| 1932 | if (elt->line > 0) |
| 1933 | memprintf(err, "%s at line %d of file '%s'", |
| 1934 | *err, elt->line, ref->reference); |
| 1935 | return 0; |
| 1936 | } |
| 1937 | } |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
| 1941 | /* This function lookup for existing reference <ref> in pattern_head <head>. */ |
| 1942 | struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref) |
| 1943 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1944 | struct pattern_expr_list *expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1945 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1946 | list_for_each_entry(expr, &head->head, list) |
| 1947 | if (expr->expr->ref == ref) |
| 1948 | return expr->expr; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1949 | return NULL; |
| 1950 | } |
| 1951 | |
| 1952 | /* This function create new pattern_expr associated to the reference <ref>. |
| 1953 | * <ref> can be NULL. If an error is occured, the function returns NULL and |
| 1954 | * <err> is filled. Otherwise, the function returns new pattern_expr linked |
| 1955 | * with <head> and <ref>. |
| 1956 | */ |
| 1957 | struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err) |
| 1958 | { |
| 1959 | struct pattern_expr *expr; |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1960 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1961 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1962 | /* Memory and initialization of the chain element. */ |
| 1963 | list = malloc(sizeof(*list)); |
| 1964 | if (!list) { |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1965 | memprintf(err, "out of memory"); |
| 1966 | return NULL; |
| 1967 | } |
| 1968 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1969 | /* Look for existing similar expr. No that only the index, parse and |
| 1970 | * parse_smp function must be identical for having similar pattern. |
| 1971 | * The other function depends of theses first. |
| 1972 | */ |
| 1973 | if (ref) { |
| 1974 | list_for_each_entry(expr, &ref->pat, list) |
| 1975 | if (expr->pat_head->index == head->index && |
| 1976 | expr->pat_head->parse == head->parse && |
| 1977 | expr->pat_head->parse_smp == head->parse_smp) |
| 1978 | break; |
| 1979 | if (&expr->list == &ref->pat) |
| 1980 | expr = NULL; |
| 1981 | } |
| 1982 | else |
| 1983 | expr = NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1984 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1985 | /* If no similar expr was found, we create new expr. */ |
| 1986 | if (!expr) { |
| 1987 | /* Get a lot of memory for the expr struct. */ |
| 1988 | expr = malloc(sizeof(*expr)); |
| 1989 | if (!expr) { |
| 1990 | memprintf(err, "out of memory"); |
| 1991 | return NULL; |
| 1992 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1993 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1994 | /* Initialize this new expr. */ |
| 1995 | pattern_init_expr(expr); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 1996 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 1997 | /* This new pattern expression reference one of his heads. */ |
| 1998 | expr->pat_head = head; |
| 1999 | |
| 2000 | /* Link with ref, or to self to facilitate LIST_DEL() */ |
| 2001 | if (ref) |
| 2002 | LIST_ADDQ(&ref->pat, &expr->list); |
| 2003 | else |
| 2004 | LIST_INIT(&expr->list); |
| 2005 | |
| 2006 | expr->ref = ref; |
| 2007 | |
| 2008 | /* We must free this pattern if it is no more used. */ |
| 2009 | list->do_free = 1; |
| 2010 | } |
| 2011 | else { |
| 2012 | /* If the pattern used already exists, it is already linked |
| 2013 | * with ref and we must not free it. |
| 2014 | */ |
| 2015 | list->do_free = 0; |
| 2016 | } |
| 2017 | |
| 2018 | /* The new list element reference the pattern_expr. */ |
| 2019 | list->expr = expr; |
| 2020 | |
| 2021 | /* Link the list element with the pattern_head. */ |
| 2022 | LIST_ADDQ(&head->head, &list->list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2023 | return expr; |
| 2024 | } |
| 2025 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2026 | /* Reads patterns from a file. If <err_msg> is non-NULL, an error message will |
| 2027 | * be returned there on errors and the caller will have to free it. |
| 2028 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2029 | int pat_ref_read_from_file(struct pat_ref *ref, const char *filename, char **err) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2030 | { |
| 2031 | FILE *file; |
| 2032 | char *c; |
| 2033 | char *arg; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2034 | int ret = 0; |
| 2035 | int line = 0; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2036 | |
| 2037 | file = fopen(filename, "r"); |
| 2038 | if (!file) { |
| 2039 | memprintf(err, "failed to open pattern file <%s>", filename); |
| 2040 | return 0; |
| 2041 | } |
| 2042 | |
| 2043 | /* now parse all patterns. The file may contain only one pattern per |
| 2044 | * line. If the line contains spaces, they will be part of the pattern. |
| 2045 | * The pattern stops at the first CR, LF or EOF encountered. |
| 2046 | */ |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2047 | while (fgets(trash.str, trash.size, file) != NULL) { |
| 2048 | line++; |
| 2049 | c = trash.str; |
| 2050 | |
| 2051 | /* ignore lines beginning with a dash */ |
| 2052 | if (*c == '#') |
| 2053 | continue; |
| 2054 | |
| 2055 | /* strip leading spaces and tabs */ |
| 2056 | while (*c == ' ' || *c == '\t') |
| 2057 | c++; |
| 2058 | |
| 2059 | |
| 2060 | arg = c; |
| 2061 | while (*c && *c != '\n' && *c != '\r') |
| 2062 | c++; |
| 2063 | *c = 0; |
| 2064 | |
| 2065 | /* empty lines are ignored too */ |
| 2066 | if (c == arg) |
| 2067 | continue; |
| 2068 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2069 | if (!pat_ref_append(ref, arg, NULL, line)) { |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2070 | memprintf(err, "out of memory when loading patterns from file <%s>", filename); |
| 2071 | goto out_close; |
| 2072 | } |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | ret = 1; /* success */ |
| 2076 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2077 | out_close: |
| 2078 | fclose(file); |
| 2079 | return ret; |
| 2080 | } |
| 2081 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2082 | int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, |
| 2083 | const char *filename, int patflags, |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 2084 | char **err, const char *display) |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2085 | { |
| 2086 | struct pat_ref *ref; |
| 2087 | struct pattern_expr *expr; |
| 2088 | |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2089 | /* Lookup for the existing reference. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2090 | ref = pat_ref_lookup(filename); |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2091 | |
| 2092 | /* If the reference doesn't exists, create it and load associated file. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2093 | if (!ref) { |
Thierry FOURNIER | 0d6ba51 | 2014-02-11 03:31:34 +0100 | [diff] [blame] | 2094 | ref = pat_ref_new(filename, display, refflags); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2095 | if (!ref) { |
| 2096 | memprintf(err, "out of memory"); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | if (!pat_ref_read_from_file(ref, filename, err)) |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | /* Now, we can loading patterns from the reference. */ |
| 2105 | |
| 2106 | /* Lookup for existing reference in the head. If the reference |
| 2107 | * doesn't exists, create it. |
| 2108 | */ |
| 2109 | expr = pattern_lookup_expr(head, ref); |
| 2110 | if (!expr) { |
| 2111 | expr = pattern_new_expr(head, ref, err); |
| 2112 | if (!expr) |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | /* Load reference content in expression. */ |
| 2117 | if (!pat_ref_load(ref, expr, patflags, 1, err)) |
| 2118 | return 0; |
| 2119 | |
| 2120 | return 1; |
| 2121 | } |
| 2122 | |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2123 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 2124 | * to sample <smp>. The function returns NULL if the sample dont match. It returns |
| 2125 | * non-null if the sample match. If <fill> is true and the sample match, the |
| 2126 | * function returns the matched pattern. In many cases, this pattern can be a |
| 2127 | * static buffer. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2128 | */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2129 | struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill) |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2130 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2131 | struct pattern_expr_list *list; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2132 | struct pattern *pat; |
| 2133 | |
| 2134 | if (!head->match) { |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2135 | if (fill) { |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2136 | static_pattern.smp = NULL; |
Thierry FOURNIER | 6bb53ff | 2014-01-28 15:54:36 +0100 | [diff] [blame] | 2137 | static_pattern.ref = NULL; |
Thierry FOURNIER | 5338eea | 2013-12-16 14:22:13 +0100 | [diff] [blame] | 2138 | static_pattern.flags = 0; |
| 2139 | static_pattern.type = SMP_T_UINT; |
| 2140 | static_pattern.val.i = 1; |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2141 | } |
Thierry FOURNIER | 1794fdf | 2014-01-17 15:25:13 +0100 | [diff] [blame] | 2142 | return &static_pattern; |
| 2143 | } |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2144 | |
Thierry FOURNIER | 5d34408 | 2014-01-27 14:19:53 +0100 | [diff] [blame] | 2145 | /* convert input to string */ |
| 2146 | if (!sample_convert(smp, head->expect_type)) |
| 2147 | return NULL; |
| 2148 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2149 | list_for_each_entry(list, &head->head, list) { |
| 2150 | pat = head->match(smp, list->expr, fill); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2151 | if (pat) |
| 2152 | return pat; |
| 2153 | } |
| 2154 | return NULL; |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2157 | /* This function prune the pattern expression. */ |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2158 | void pattern_prune(struct pattern_head *head) |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2159 | { |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2160 | struct pattern_expr_list *list, *safe; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2161 | |
Thierry FOURNIER | c5959fd | 2014-01-20 14:29:33 +0100 | [diff] [blame] | 2162 | list_for_each_entry_safe(list, safe, &head->head, list) { |
| 2163 | LIST_DEL(&list->list); |
| 2164 | if (list->do_free) { |
| 2165 | LIST_DEL(&list->expr->list); |
| 2166 | head->prune(list->expr); |
| 2167 | free(list->expr); |
| 2168 | } |
| 2169 | free(list); |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2170 | } |
Thierry FOURNIER | 6f7203d | 2014-01-14 16:24:51 +0100 | [diff] [blame] | 2171 | } |
| 2172 | |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2173 | /* This function lookup for a pattern matching the <key> and return a |
| 2174 | * pointer to a pointer of the sample stoarge. If the <key> dont match, |
| 2175 | * the function returns NULL. If the key cannot be parsed, the function |
| 2176 | * fill <err>. |
| 2177 | */ |
| 2178 | struct sample_storage **pattern_find_smp(const char *key, struct pattern_expr *expr, char **err) |
| 2179 | { |
| 2180 | struct pattern pattern; |
| 2181 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2182 | if (!expr->pat_head->parse(key, &pattern, err)) |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2183 | return NULL; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2184 | return expr->pat_head->find_smp(expr, &pattern); |
Thierry FOURNIER | 55d0b10 | 2014-01-15 11:25:26 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2187 | /* This function search all the pattern matching the <key> and delete it. |
| 2188 | * If the parsing of the input key fails, the function returns 0 and the |
| 2189 | * <err> is filled, else return 1; |
| 2190 | */ |
| 2191 | int pattern_delete(const char *key, struct pattern_expr *expr, char **err) |
| 2192 | { |
| 2193 | struct pattern pattern; |
| 2194 | |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2195 | if (!expr->pat_head->parse(key, &pattern, err)) |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2196 | return 0; |
Thierry FOURNIER | 1e00d38 | 2014-02-11 11:31:40 +0100 | [diff] [blame] | 2197 | expr->pat_head->delete(expr, &pattern); |
Thierry FOURNIER | b113650 | 2014-01-15 11:38:49 +0100 | [diff] [blame] | 2198 | return 1; |
| 2199 | } |
Thierry FOURNIER | af5a29d | 2014-03-11 14:29:22 +0100 | [diff] [blame] | 2200 | |
| 2201 | /* This function finalize the configuration parsing. Its set all the |
| 2202 | * automatic ids |
| 2203 | */ |
| 2204 | void pattern_finalize_config(void) |
| 2205 | { |
| 2206 | int i = 0; |
| 2207 | struct pat_ref *ref, *ref2, *ref3; |
| 2208 | struct list pr = LIST_HEAD_INIT(pr); |
| 2209 | |
| 2210 | list_for_each_entry(ref, &pattern_reference, list) { |
| 2211 | if (ref->unique_id == -1) { |
| 2212 | /* Look for the first free id. */ |
| 2213 | while (1) { |
| 2214 | list_for_each_entry(ref2, &pattern_reference, list) { |
| 2215 | if (ref2->unique_id == i) { |
| 2216 | i++; |
| 2217 | break; |
| 2218 | } |
| 2219 | } |
| 2220 | if (&ref2->list == &pattern_reference); |
| 2221 | break; |
| 2222 | } |
| 2223 | |
| 2224 | /* Uses the unique id and increment it for the next entry. */ |
| 2225 | ref->unique_id = i; |
| 2226 | i++; |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | /* This sort the reference list by id. */ |
| 2231 | list_for_each_entry_safe(ref, ref2, &pattern_reference, list) { |
| 2232 | LIST_DEL(&ref->list); |
| 2233 | list_for_each_entry(ref3, &pr, list) { |
| 2234 | if (ref->unique_id < ref3->unique_id) { |
| 2235 | LIST_ADDQ(&ref3->list, &ref->list); |
| 2236 | break; |
| 2237 | } |
| 2238 | } |
| 2239 | if (&ref3->list == &pr) |
| 2240 | LIST_ADDQ(&pr, &ref->list); |
| 2241 | } |
| 2242 | |
| 2243 | /* swap root */ |
| 2244 | LIST_ADD(&pr, &pattern_reference); |
| 2245 | LIST_DEL(&pr); |
| 2246 | } |