blob: e9269e4ebbef1e3be1bb80b7cef1a3009dc6fbef [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 FOURNIER1e00d382014-02-11 11:31:40 +010031void pattern_finalize_config(void);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010032
Willy Tarreau6f8fe312013-11-28 22:24:25 +010033/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
34static inline int pat_find_match_name(const char *name)
35{
36 int i;
37
38 for (i = 0; i < PAT_MATCH_NUM; i++)
39 if (strcmp(name, pat_match_names[i]) == 0)
40 return i;
41 return -1;
42}
43
Thierry FOURNIERed66c292013-11-28 11:05:19 +010044/* This function executes a pattern match on a sample. It applies pattern <expr>
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +010045 * to sample <smp>. The function returns NULL if the sample dont match. It returns
46 * non-null if the sample match. If <fill> is true and the sample match, the
47 * function returns the matched pattern. In many cases, this pattern can be a
48 * static buffer.
Thierry FOURNIERed66c292013-11-28 11:05:19 +010049 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010050struct pattern *pattern_exec_match(struct pattern_head *head, struct sample *smp, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010051
52/*
53 *
Thierry FOURNIERb9b08462013-12-13 15:12:32 +010054 * The following function gets "pattern", duplicate it and index it in "expr"
55 *
56 */
57int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
58int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
59int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
60int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
61int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
62int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
63
64/*
65 *
Thierry FOURNIERb1136502014-01-15 11:38:49 +010066 * The following functions search pattern <pattern> into the pattern
67 * expression <expr>. If the pattern is found, delete it. This function
68 * never fails.
69 *
70 */
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +010071void pat_del_list_val(struct pattern_expr *expr, struct pat_ref_elt *ref);
72void pat_del_tree_ip(struct pattern_expr *expr, struct pat_ref_elt *ref);
73void pat_del_list_ptr(struct pattern_expr *expr, struct pat_ref_elt *ref);
74void pat_del_tree_str(struct pattern_expr *expr, struct pat_ref_elt *ref);
75void pat_del_list_reg(struct pattern_expr *expr, struct pat_ref_elt *ref);
Thierry FOURNIERb1136502014-01-15 11:38:49 +010076
77/*
78 *
Thierry FOURNIER6f7203d2014-01-14 16:24:51 +010079 * The following functions clean all entries of a pattern expression and
80 * reset the tree and list root.
81 *
82 */
83void pat_prune_val(struct pattern_expr *expr);
84void pat_prune_ptr(struct pattern_expr *expr);
85void pat_prune_reg(struct pattern_expr *expr);
86
87/*
88 *
Thierry FOURNIERed66c292013-11-28 11:05:19 +010089 * The following functions are general purpose pattern matching functions.
90 *
91 */
92
93
94/* ignore the current line */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010095int pat_parse_nothing(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010096
Thierry FOURNIERed66c292013-11-28 11:05:19 +010097/* Parse an integer. It is put both in min and max. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +010098int pat_parse_int(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +010099
100/* Parse an version. It is put both in min and max. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100101int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100102
103/* Parse a range of integers delimited by either ':' or '-'. If only one
104 * integer is read, it is set as both min and max.
105 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100106int pat_parse_range(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100107
108/* Parse a string. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100109int pat_parse_str(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100110
111/* Parse a hexa binary definition. It is allocated and duplicated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100112int pat_parse_bin(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100113
114/* Parse a regex. It is allocated. */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100115int pat_parse_reg(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100116
117/* Parse an IP address and an optional mask in the form addr[/mask].
118 * The addr may either be an IPv4 address or a hostname. The mask
119 * may either be a dotted mask or a number of bits. Returns 1 if OK,
120 * otherwise 0.
121 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +0100122int pat_parse_ip(const char *text, struct pattern *pattern, char **err);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100123
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100124/* NB: For two strings to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100125struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100126
127/* NB: For two binary buffers to be identical, it is required that their lengths match */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100128struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100129
130/* Checks that the length of the pattern in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100131struct pattern *pat_match_len(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100132
133/* Checks that the integer in <test> is included between min and max */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100134struct pattern *pat_match_int(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERe7ba2362014-01-21 11:25:41 +0100135
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100136/* always return false */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100137struct pattern *pat_match_nothing(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100138
139/* Checks that the pattern matches the end of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100140struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100141
142/* Checks that the pattern matches the beginning of the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100143struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100144
145/* Checks that the pattern is included inside the tested string. */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100146struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100147
148/* Checks that the pattern is included inside the tested string, but enclosed
149 * between slashes or at the beginning or end of the string. Slashes at the
150 * beginning or end of the pattern are ignored.
151 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100152struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100153
154/* Checks that the pattern is included inside the tested string, but enclosed
155 * between dots or at the beginning or end of the string. Dots at the beginning
156 * or end of the pattern are ignored.
157 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100158struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100159
160/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100161struct pattern *pat_match_ip(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100162
163/* Executes a regex. It temporarily changes the data to add a trailing zero,
164 * and restores the previous character when leaving.
165 */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +0100166struct pattern *pat_match_reg(struct sample *smp, struct pattern_expr *expr, int fill);
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100167
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100168/*
169 * pattern_ref manipulation.
170 */
171struct pat_ref *pat_ref_lookup(const char *reference);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100172struct pat_ref *pat_ref_lookupid(int unique_id);
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +0100173struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned int flags);
174struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int flags);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100175int pat_ref_append(struct pat_ref *ref, char *pattern, char *sample, int line);
176int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +0100177int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
178int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100179int pat_ref_delete(struct pat_ref *ref, const char *key);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +0100180int pat_ref_delete_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100181void pat_ref_prune(struct pat_ref *ref);
182int pat_ref_load(struct pat_ref *ref, struct pattern_expr *expr, int patflags, int soe, char **err);
183
184/*
185 * pattern_head manipulation.
186 */
187void pattern_init_head(struct pattern_head *head);
188void pattern_prune(struct pattern_head *head);
Thierry FOURNIER39bef452014-01-29 13:29:45 +0100189int pattern_read_from_file(struct pattern_head *head, unsigned int refflags, const char *filename, int patflags, int load_smp, char **err, const char *display);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100190
191/*
192 * pattern_expr manipulation.
193 */
Thierry FOURNIERa65b3432013-11-28 18:22:00 +0100194void pattern_init_expr(struct pattern_expr *expr);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100195struct pattern_expr *pattern_lookup_expr(struct pattern_head *head, struct pat_ref *ref);
196struct pattern_expr *pattern_new_expr(struct pattern_head *head, struct pat_ref *ref, char **err);
Thierry FOURNIERe369ca22014-01-29 16:24:55 +0100197struct sample_storage **pattern_find_smp(struct pattern_expr *expr, struct pat_ref_elt *elt);
Thierry FOURNIER7acca4b2014-01-28 16:43:36 +0100198int pattern_delete(struct pattern_expr *expr, struct pat_ref_elt *ref);
Thierry FOURNIER01cdcd42013-12-10 15:08:01 +0100199
Thierry FOURNIERed66c292013-11-28 11:05:19 +0100200
201#endif