blob: b9947b5dde91a70de7a6379202a144e51cbd35dd [file] [log] [blame]
Thierry FOURNIERed66c292013-11-28 11:05:19 +01001/*
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 Tarreau6f8fe312013-11-28 22:24:25 +010025#include <string.h>
26
Willy Tarreau0cba6072013-11-28 22:21:02 +010027#include <common/config.h>
28#include <common/standard.h>
29#include <types/pattern.h>
30
Thierry FOURNIER7148ce62013-12-06 19:06:43 +010031/* parse the <args> with <expr> compliant parser. <pattern> is a context for
Thierry FOURNIERed66c292013-11-28 11:05:19 +010032 * the current parsed acl. It must initialized at NULL:
33 *
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010034 * struct pattern *pattern = NULL
35 * pattern_register(..., &pattern, ...);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010036 *
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010037 * patflag are a lot of 'PAT_F_*' flags pattern compatible. see
Thierry FOURNIERed66c292013-11-28 11:05:19 +010038 * <types/acl.h>.
39 *
Thierry FOURNIER7148ce62013-12-06 19:06:43 +010040 * The function returns 1 if the processing is ok, return 0
41 * if the parser fails, with <err> message filled.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010042 */
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010043int pattern_register(struct pattern_expr *expr, const char *arg, struct sample_storage *smp, int patflags, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010044
Willy Tarreau6f8fe312013-11-28 22:24:25 +010045/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
46static inline int pat_find_match_name(const char *name)
47{
48 int i;
49
50 for (i = 0; i < PAT_MATCH_NUM; i++)
51 if (strcmp(name, pat_match_names[i]) == 0)
52 return i;
53 return -1;
54}
55
Thierry FOURNIERed66c292013-11-28 11:05:19 +010056/* This function executes a pattern match on a sample. It applies pattern <expr>
57 * to sample <smp>. If <sample> is not NULL, a pointer to an optional sample
58 * associated to the matching patterned will be put there. The function returns
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010059 * PAT_MATCH or PAT_NOMATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010060 */
Thierry FOURNIER76090642013-12-10 15:03:38 +010061enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, struct sample_storage **sample, struct pattern **pat, struct pat_idx_elt **elt);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062
63/*
64 *
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010065 * The following function gets "pattern", duplicate it and index it in "expr"
66 *
67 */
68int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
69int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
70int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
71int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
72int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
73int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
74
75/*
76 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +010077 * The following functions are general purpose pattern matching functions.
78 *
79 */
80
81
82/* ignore the current line */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010083int pat_parse_nothing(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010084
Thierry FOURNIERed66c292013-11-28 11:05:19 +010085/* Parse an integer. It is put both in min and max. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010086int pat_parse_int(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010087
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010088/* Parse len like an integer, but specify expected string type */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010089int pat_parse_len(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERcc0e0b32013-12-06 16:56:40 +010090
Thierry FOURNIERed66c292013-11-28 11:05:19 +010091/* Parse an version. It is put both in min and max. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010092int pat_parse_dotted_ver(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010093
94/* Parse a range of integers delimited by either ':' or '-'. If only one
95 * integer is read, it is set as both min and max.
96 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010097int pat_parse_range(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010098
99/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100100int pat_parse_str(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100101
102/* Parse a hexa binary definition. It is allocated and duplicated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100103int pat_parse_bin(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100104
105/* Parse and concatenate strings into one. It is allocated and duplicated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100106int pat_parse_strcat(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100107
108/* Parse a regex. It is allocated. */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100109int pat_parse_reg(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100110
111/* Parse an IP address and an optional mask in the form addr[/mask].
112 * The addr may either be an IPv4 address or a hostname. The mask
113 * may either be a dotted mask or a number of bits. Returns 1 if OK,
114 * otherwise 0.
115 */
Thierry FOURNIER580c32c2014-01-24 10:58:12 +0100116int pat_parse_ip(const char *text, struct pattern *pattern, enum pat_usage usage, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100117
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100118/* NB: For two strings to be identical, it is required that their lengths match */
119enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern);
120
121/* NB: For two binary buffers to be identical, it is required that their lengths match */
122enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern);
123
124/* Checks that the length of the pattern in <test> is included between min and max */
125enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern);
126
127/* Checks that the integer in <test> is included between min and max */
128enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern);
129
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100130/* always return false */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100131enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100132
133/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100134enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100135
136/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100137enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138
139/* Checks that the pattern is included inside the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100140enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100141
142/* Checks that the pattern is included inside the tested string, but enclosed
143 * between slashes or at the beginning or end of the string. Slashes at the
144 * beginning or end of the pattern are ignored.
145 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100146enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100147
148/* Checks that the pattern is included inside the tested string, but enclosed
149 * between dots or at the beginning or end of the string. Dots at the beginning
150 * or end of the pattern are ignored.
151 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100152enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100153
154/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100155enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100156
157/* Executes a regex. It temporarily changes the data to add a trailing zero,
158 * and restores the previous character when leaving.
159 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100160enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100161
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100162int pattern_read_from_file(struct pattern_expr *expr, const char *filename, int patflags, char **err);
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100163void pattern_free(struct pattern_list *pat);
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100164void pattern_prune_expr(struct pattern_expr *expr);
165void pattern_init_expr(struct pattern_expr *expr);
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +0100166int pattern_lookup(const char *args, struct pattern_expr *expr, struct pattern_list **pat_elt, struct pat_idx_elt **idx_elt, char **err);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +0100167
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100168
169#endif