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