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