Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/pattern.h |
| 3 | * This file provides structures and types for pattern matching. |
| 4 | * |
| 5 | * Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_PATTERN_H |
| 23 | #define _PROTO_PATTERN_H |
| 24 | |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 25 | #include <common/config.h> |
| 26 | #include <common/standard.h> |
| 27 | #include <types/pattern.h> |
| 28 | |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 29 | /* parse the <text> with <expr> compliant parser. <pattern> is a context for |
| 30 | * the current parsed acl. It must initialized at NULL: |
| 31 | * |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 32 | * struct pattern *pattern = NULL |
| 33 | * pattern_register(..., &pattern, ...); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 34 | * |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 35 | * patflag are a lot of 'PAT_F_*' flags pattern compatible. see |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 36 | * <types/acl.h>. |
| 37 | * |
| 38 | * The function returns 1 if the processing is ok, return -1 if the parser |
| 39 | * fails, with <err> message filled. It returns -2 in "out of memory" |
| 40 | * error case. |
| 41 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 42 | int pattern_register(struct pattern_expr *expr, char *text, struct sample_storage *smp, struct pattern **pattern, int patflags, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 43 | |
| 44 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 45 | * to sample <smp>. If <sample> is not NULL, a pointer to an optional sample |
| 46 | * associated to the matching patterned will be put there. The function returns |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 47 | * PAT_MATCH or PAT_NOMATCH. |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 48 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 49 | enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, struct sample_storage **sample); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * |
| 53 | * The following functions are general purpose pattern matching functions. |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | |
| 58 | /* ignore the current line */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 59 | int pat_parse_nothing(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 60 | |
| 61 | /* NB: For two strings to be identical, it is required that their lengths match */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 62 | enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 63 | |
| 64 | /* NB: For two binary buffers to be identical, it is required that their lengths match */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 65 | enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 66 | |
| 67 | /* Checks that the length of the pattern in <test> is included between min and max */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 68 | enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 69 | |
| 70 | /* Checks that the integer in <test> is included between min and max */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 71 | enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 72 | |
| 73 | /* Parse an integer. It is put both in min and max. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 74 | int pat_parse_int(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 75 | |
| 76 | /* Parse an version. It is put both in min and max. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 77 | int pat_parse_dotted_ver(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 78 | |
| 79 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 80 | * integer is read, it is set as both min and max. |
| 81 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 82 | int pat_parse_range(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 83 | |
| 84 | /* Parse a string. It is allocated and duplicated. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 85 | int pat_parse_str(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 86 | |
| 87 | /* Parse a hexa binary definition. It is allocated and duplicated. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 88 | int pat_parse_bin(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 89 | |
| 90 | /* Parse and concatenate strings into one. It is allocated and duplicated. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 91 | int pat_parse_strcat(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 92 | |
| 93 | /* Parse a regex. It is allocated. */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 94 | int pat_parse_reg(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 95 | |
| 96 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 97 | * The addr may either be an IPv4 address or a hostname. The mask |
| 98 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 99 | * otherwise 0. |
| 100 | */ |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 101 | int pat_parse_ip(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 102 | |
| 103 | /* always return false */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 104 | enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 105 | |
| 106 | /* Checks that the pattern matches the end of the tested string. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 107 | enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 108 | |
| 109 | /* Checks that the pattern matches the beginning of the tested string. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 110 | enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 111 | |
| 112 | /* Checks that the pattern is included inside the tested string. */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 113 | enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 114 | |
| 115 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 116 | * between slashes or at the beginning or end of the string. Slashes at the |
| 117 | * beginning or end of the pattern are ignored. |
| 118 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 119 | enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 120 | |
| 121 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 122 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 123 | * or end of the pattern are ignored. |
| 124 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 125 | enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 126 | |
| 127 | /* Check that the IPv4 address in <test> matches the IP/mask in pattern */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 128 | enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 129 | |
| 130 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 131 | * and restores the previous character when leaving. |
| 132 | */ |
Willy Tarreau | 0cba607 | 2013-11-28 22:21:02 +0100 | [diff] [blame^] | 133 | enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 134 | |
Thierry FOURNIER | a65b343 | 2013-11-28 18:22:00 +0100 | [diff] [blame] | 135 | int pattern_read_from_file(struct pattern_expr *expr, const char *filename, int patflags, char **err); |
| 136 | void pattern_free(struct pattern *pat); |
| 137 | void pattern_prune_expr(struct pattern_expr *expr); |
| 138 | void pattern_init_expr(struct pattern_expr *expr); |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 139 | |
| 140 | #endif |