2013-11-28 10:05:19 +00:00
|
|
|
/*
|
|
|
|
* include/proto/pattern.h
|
|
|
|
* This file provides structures and types for pattern matching.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROTO_PATTERN_H
|
|
|
|
#define _PROTO_PATTERN_H
|
|
|
|
|
2013-11-28 21:24:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-28 21:21:02 +00:00
|
|
|
#include <common/config.h>
|
|
|
|
#include <common/standard.h>
|
|
|
|
#include <types/pattern.h>
|
|
|
|
|
2013-12-06 18:06:43 +00:00
|
|
|
/* parse the <args> with <expr> compliant parser. <pattern> is a context for
|
2013-11-28 10:05:19 +00:00
|
|
|
* the current parsed acl. It must initialized at NULL:
|
|
|
|
*
|
2013-11-28 17:22:00 +00:00
|
|
|
* struct pattern *pattern = NULL
|
|
|
|
* pattern_register(..., &pattern, ...);
|
2013-11-28 10:05:19 +00:00
|
|
|
*
|
2013-11-28 17:22:00 +00:00
|
|
|
* patflag are a lot of 'PAT_F_*' flags pattern compatible. see
|
2013-11-28 10:05:19 +00:00
|
|
|
* <types/acl.h>.
|
|
|
|
*
|
2013-12-06 18:06:43 +00:00
|
|
|
* The function returns 1 if the processing is ok, return 0
|
|
|
|
* if the parser fails, with <err> message filled.
|
2013-11-28 10:05:19 +00:00
|
|
|
*/
|
2013-12-13 14:12:32 +00:00
|
|
|
int pattern_register(struct pattern_expr *expr, const char *arg, struct sample_storage *smp, int patflags, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-11-28 21:24:25 +00:00
|
|
|
/* return the PAT_MATCH_* index for match name "name", or < 0 if not found */
|
|
|
|
static inline int pat_find_match_name(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PAT_MATCH_NUM; i++)
|
|
|
|
if (strcmp(name, pat_match_names[i]) == 0)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/* This function executes a pattern match on a sample. It applies pattern <expr>
|
|
|
|
* to sample <smp>. If <sample> is not NULL, a pointer to an optional sample
|
|
|
|
* associated to the matching patterned will be put there. The function returns
|
2013-11-28 17:22:00 +00:00
|
|
|
* PAT_MATCH or PAT_NOMATCH.
|
2013-11-28 10:05:19 +00:00
|
|
|
*/
|
2013-12-13 15:09:50 +00:00
|
|
|
enum pat_match_res pattern_exec_match(struct pattern_expr *expr, struct sample *smp, struct sample_storage **sample, struct pattern **pat, struct pattern_tree **elt);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-12-13 14:12:32 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following function gets "pattern", duplicate it and index it in "expr"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int pat_idx_list_val(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_ptr(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_str(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_list_reg(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_tree_ip(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err);
|
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* The following functions are general purpose pattern matching functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* ignore the current line */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_nothing(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse an integer. It is put both in min and max. */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_int(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-12-06 15:56:40 +00:00
|
|
|
/* Parse len like an integer, but specify expected string type */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_len(const char *text, struct pattern *pattern, char **err);
|
2013-12-06 15:56:40 +00:00
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/* Parse an version. It is put both in min and max. */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_dotted_ver(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a range of integers delimited by either ':' or '-'. If only one
|
|
|
|
* integer is read, it is set as both min and max.
|
|
|
|
*/
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_range(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a string. It is allocated and duplicated. */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_str(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a hexa binary definition. It is allocated and duplicated. */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_bin(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse a regex. It is allocated. */
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_reg(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Parse an IP address and an optional mask in the form addr[/mask].
|
|
|
|
* The addr may either be an IPv4 address or a hostname. The mask
|
|
|
|
* may either be a dotted mask or a number of bits. Returns 1 if OK,
|
|
|
|
* otherwise 0.
|
|
|
|
*/
|
2013-12-13 14:36:59 +00:00
|
|
|
int pat_parse_ip(const char *text, struct pattern *pattern, char **err);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2014-01-21 10:25:41 +00:00
|
|
|
/* NB: For two strings to be identical, it is required that their lengths match */
|
|
|
|
enum pat_match_res pat_match_str(struct sample *smp, struct pattern *pattern);
|
|
|
|
|
|
|
|
/* NB: For two binary buffers to be identical, it is required that their lengths match */
|
|
|
|
enum pat_match_res pat_match_bin(struct sample *smp, struct pattern *pattern);
|
|
|
|
|
|
|
|
/* Checks that the length of the pattern in <test> is included between min and max */
|
|
|
|
enum pat_match_res pat_match_len(struct sample *smp, struct pattern *pattern);
|
|
|
|
|
|
|
|
/* Checks that the integer in <test> is included between min and max */
|
|
|
|
enum pat_match_res pat_match_int(struct sample *smp, struct pattern *pattern);
|
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
/* always return false */
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_nothing(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern matches the end of the tested string. */
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_end(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern matches the beginning of the tested string. */
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_beg(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string. */
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_sub(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string, but enclosed
|
|
|
|
* between slashes or at the beginning or end of the string. Slashes at the
|
|
|
|
* beginning or end of the pattern are ignored.
|
|
|
|
*/
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_dir(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Checks that the pattern is included inside the tested string, but enclosed
|
|
|
|
* between dots or at the beginning or end of the string. Dots at the beginning
|
|
|
|
* or end of the pattern are ignored.
|
|
|
|
*/
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_dom(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Check that the IPv4 address in <test> matches the IP/mask in pattern */
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_ip(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
/* Executes a regex. It temporarily changes the data to add a trailing zero,
|
|
|
|
* and restores the previous character when leaving.
|
|
|
|
*/
|
2013-11-28 21:21:02 +00:00
|
|
|
enum pat_match_res pat_match_reg(struct sample *smp, struct pattern *pattern);
|
2013-11-28 10:05:19 +00:00
|
|
|
|
2013-11-28 17:22:00 +00:00
|
|
|
int pattern_read_from_file(struct pattern_expr *expr, const char *filename, int patflags, char **err);
|
2013-12-13 11:12:18 +00:00
|
|
|
void pattern_free(struct pattern_list *pat);
|
2013-11-28 17:22:00 +00:00
|
|
|
void pattern_prune_expr(struct pattern_expr *expr);
|
|
|
|
void pattern_init_expr(struct pattern_expr *expr);
|
2013-12-13 15:09:50 +00:00
|
|
|
int pattern_lookup(const char *args, struct pattern_expr *expr, struct pattern_list **pat_elt, struct pattern_tree **idx_elt, char **err);
|
2013-12-10 14:08:01 +00:00
|
|
|
|
2013-11-28 10:05:19 +00:00
|
|
|
|
|
|
|
#endif
|