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