blob: 29773f8b8c9115d894e3d4da3d5f8cad43dcf348 [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 FOURNIERed66c292013-11-28 11:05:19 +010031/* parse the <text> with <expr> compliant parser. <pattern> is a context for
32 * 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 *
40 * The function returns 1 if the processing is ok, return -1 if the parser
41 * fails, with <err> message filled. It returns -2 in "out of memory"
42 * error case.
43 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010044int pattern_register(struct pattern_expr *expr, char *text, struct sample_storage *smp, struct pattern **pattern, int patflags, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010045
Willy Tarreau6f8fe312013-11-28 22:24:25 +010046/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
47static inline int pat_find_match_name(const char *name)
48{
49 int i;
50
51 for (i = 0; i < PAT_MATCH_NUM; i++)
52 if (strcmp(name, pat_match_names[i]) == 0)
53 return i;
54 return -1;
55}
56
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057/* This function executes a pattern match on a sample. It applies pattern <expr>
58 * to sample <smp>. If <sample> is not NULL, a pointer to an optional sample
59 * associated to the matching patterned will be put there. The function returns
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010060 * PAT_MATCH or PAT_NOMATCH.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010061 */
Willy Tarreau0cba6072013-11-28 22:21:02 +010062enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, struct sample_storage **sample);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063
64/*
65 *
66 * The following functions are general purpose pattern matching functions.
67 *
68 */
69
70
71/* ignore the current line */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010072int pat_parse_nothing(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010073
74/* NB: For two strings to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +010075enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010076
77/* NB: For two binary buffers to be identical, it is required that their lengths match */
Willy Tarreau0cba6072013-11-28 22:21:02 +010078enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010079
80/* Checks that the length of the pattern in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +010081enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010082
83/* Checks that the integer in <test> is included between min and max */
Willy Tarreau0cba6072013-11-28 22:21:02 +010084enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010085
86/* Parse an integer. It is put both in min and max. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010087int pat_parse_int(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010088
89/* Parse an version. It is put both in min and max. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010090int pat_parse_dotted_ver(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010091
92/* Parse a range of integers delimited by either ':' or '-'. If only one
93 * integer is read, it is set as both min and max.
94 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010095int pat_parse_range(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010096
97/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010098int pat_parse_str(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010099
100/* Parse a hexa binary definition. It is allocated and duplicated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100101int pat_parse_bin(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100102
103/* Parse and concatenate strings into one. It is allocated and duplicated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100104int pat_parse_strcat(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100105
106/* Parse a regex. It is allocated. */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100107int pat_parse_reg(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100108
109/* Parse an IP address and an optional mask in the form addr[/mask].
110 * The addr may either be an IPv4 address or a hostname. The mask
111 * may either be a dotted mask or a number of bits. Returns 1 if OK,
112 * otherwise 0.
113 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100114int pat_parse_ip(const char **text, struct pattern *pattern, struct sample_storage *smp, int *opaque, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100115
116/* always return false */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100117enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100118
119/* Checks that the pattern matches the end of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100120enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100121
122/* Checks that the pattern matches the beginning of the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100123enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100124
125/* Checks that the pattern is included inside the tested string. */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100126enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100127
128/* Checks that the pattern is included inside the tested string, but enclosed
129 * between slashes or at the beginning or end of the string. Slashes at the
130 * beginning or end of the pattern are ignored.
131 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100132enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100133
134/* Checks that the pattern is included inside the tested string, but enclosed
135 * between dots or at the beginning or end of the string. Dots at the beginning
136 * or end of the pattern are ignored.
137 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100138enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100139
140/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100141enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100142
143/* Executes a regex. It temporarily changes the data to add a trailing zero,
144 * and restores the previous character when leaving.
145 */
Willy Tarreau0cba6072013-11-28 22:21:02 +0100146enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100147
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100148int pattern_read_from_file(struct pattern_expr *expr, const char *filename, int patflags, char **err);
149void pattern_free(struct pattern *pat);
150void pattern_prune_expr(struct pattern_expr *expr);
151void pattern_init_expr(struct pattern_expr *expr);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100152
153#endif