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 | |
| 25 | /* parse the <text> with <expr> compliant parser. <pattern> is a context for |
| 26 | * the current parsed acl. It must initialized at NULL: |
| 27 | * |
| 28 | * struct acl_pattern *pattern = NULL |
| 29 | * acl_register_pattern(..., &pattern, ...); |
| 30 | * |
| 31 | * patflag are a combination of 'ACL_PAT_F_*' flags pattern compatible. see |
| 32 | * <types/acl.h>. |
| 33 | * |
| 34 | * The function returns 1 if the processing is ok, return -1 if the parser |
| 35 | * fails, with <err> message filled. It returns -2 in "out of memory" |
| 36 | * error case. |
| 37 | */ |
| 38 | int acl_register_pattern(struct acl_expr *expr, char *text, struct sample_storage *smp, struct acl_pattern **pattern, int patflags, char **err); |
| 39 | |
| 40 | /* This function executes a pattern match on a sample. It applies pattern <expr> |
| 41 | * to sample <smp>. If <sample> is not NULL, a pointer to an optional sample |
| 42 | * associated to the matching patterned will be put there. The function returns |
| 43 | * ACL_PAT_FAIL, ACL_PAT_MISS or ACL_PAT_PASS. |
| 44 | */ |
| 45 | inline int acl_exec_match(struct acl_expr *expr, struct sample *smp, struct sample_storage **sample); |
| 46 | |
| 47 | /* |
| 48 | * |
| 49 | * The following functions are general purpose pattern matching functions. |
| 50 | * |
| 51 | */ |
| 52 | |
| 53 | |
| 54 | /* ignore the current line */ |
| 55 | int acl_parse_nothing(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 56 | |
| 57 | /* NB: For two strings to be identical, it is required that their lengths match */ |
| 58 | int acl_match_str(struct sample *smp, struct acl_pattern *pattern); |
| 59 | |
| 60 | /* NB: For two binary buffers to be identical, it is required that their lengths match */ |
| 61 | int acl_match_bin(struct sample *smp, struct acl_pattern *pattern); |
| 62 | |
| 63 | /* Checks that the length of the pattern in <test> is included between min and max */ |
| 64 | int acl_match_len(struct sample *smp, struct acl_pattern *pattern); |
| 65 | |
| 66 | /* Checks that the integer in <test> is included between min and max */ |
| 67 | int acl_match_int(struct sample *smp, struct acl_pattern *pattern); |
| 68 | |
| 69 | /* Parse an integer. It is put both in min and max. */ |
| 70 | int acl_parse_int(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 71 | |
| 72 | /* Parse an version. It is put both in min and max. */ |
| 73 | int acl_parse_dotted_ver(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 74 | |
| 75 | /* Parse a range of integers delimited by either ':' or '-'. If only one |
| 76 | * integer is read, it is set as both min and max. |
| 77 | */ |
| 78 | int acl_parse_range(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 79 | |
| 80 | /* Parse a string. It is allocated and duplicated. */ |
| 81 | int acl_parse_str(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 82 | |
| 83 | /* Parse a hexa binary definition. It is allocated and duplicated. */ |
| 84 | int acl_parse_bin(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 85 | |
| 86 | /* Parse and concatenate strings into one. It is allocated and duplicated. */ |
| 87 | int acl_parse_strcat(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 88 | |
| 89 | /* Parse a regex. It is allocated. */ |
| 90 | int acl_parse_reg(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 91 | |
| 92 | /* Parse an IP address and an optional mask in the form addr[/mask]. |
| 93 | * The addr may either be an IPv4 address or a hostname. The mask |
| 94 | * may either be a dotted mask or a number of bits. Returns 1 if OK, |
| 95 | * otherwise 0. |
| 96 | */ |
| 97 | int acl_parse_ip(const char **text, struct acl_pattern *pattern, struct sample_storage *smp, int *opaque, char **err); |
| 98 | |
| 99 | /* always return false */ |
| 100 | int acl_match_nothing(struct sample *smp, struct acl_pattern *pattern); |
| 101 | |
| 102 | /* Checks that the pattern matches the end of the tested string. */ |
| 103 | int acl_match_end(struct sample *smp, struct acl_pattern *pattern); |
| 104 | |
| 105 | /* Checks that the pattern matches the beginning of the tested string. */ |
| 106 | int acl_match_beg(struct sample *smp, struct acl_pattern *pattern); |
| 107 | |
| 108 | /* Checks that the pattern is included inside the tested string. */ |
| 109 | int acl_match_sub(struct sample *smp, struct acl_pattern *pattern); |
| 110 | |
| 111 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 112 | * between slashes or at the beginning or end of the string. Slashes at the |
| 113 | * beginning or end of the pattern are ignored. |
| 114 | */ |
| 115 | int acl_match_dir(struct sample *smp, struct acl_pattern *pattern); |
| 116 | |
| 117 | /* Checks that the pattern is included inside the tested string, but enclosed |
| 118 | * between dots or at the beginning or end of the string. Dots at the beginning |
| 119 | * or end of the pattern are ignored. |
| 120 | */ |
| 121 | int acl_match_dom(struct sample *smp, struct acl_pattern *pattern); |
| 122 | |
| 123 | /* Check that the IPv4 address in <test> matches the IP/mask in pattern */ |
| 124 | int acl_match_ip(struct sample *smp, struct acl_pattern *pattern); |
| 125 | |
| 126 | /* Executes a regex. It temporarily changes the data to add a trailing zero, |
| 127 | * and restores the previous character when leaving. |
| 128 | */ |
| 129 | int acl_match_reg(struct sample *smp, struct acl_pattern *pattern); |
| 130 | |
| 131 | int acl_read_patterns_from_file(struct acl_expr *expr, const char *filename, int patflags, char **err); |
| 132 | void free_pattern(struct acl_pattern *pat); |
| 133 | void free_pattern_list(struct list *head); |
| 134 | void free_pattern_tree(struct eb_root *root); |
| 135 | |
| 136 | #endif |