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