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