]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_regex.c
Merge pull request #12795 from pguibert6WIND/vpnv6_nexthop_encoding
[mirror_frr.git] / bgpd / bgp_regex.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AS regular expression routine
3 * Copyright (C) 1999 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include "log.h"
9 #include "command.h"
10 #include "memory.h"
11 #include "queue.h"
12 #include "filter.h"
13
14 #include "bgpd.h"
15 #include "bgp_aspath.h"
16 #include "bgp_regex.h"
17
18 /* Character `_' has special mean. It represents [,{}() ] and the
19 beginning of the line(^) and the end of the line ($).
20
21 (^|[,{}() ]|$) */
22
23 regex_t *bgp_regcomp(const char *regstr)
24 {
25 /* Convert _ character to generic regular expression. */
26 int i, j;
27 int len;
28 int magic = 0;
29 char *magic_str;
30 char magic_regexp[] = "(^|[,{}() ]|$)";
31 int ret;
32 regex_t *regex;
33
34 len = strlen(regstr);
35 for (i = 0; i < len; i++)
36 if (regstr[i] == '_')
37 magic++;
38
39 magic_str = XMALLOC(MTYPE_TMP, len + (14 * magic) + 1);
40
41 for (i = 0, j = 0; i < len; i++) {
42 if (regstr[i] == '_') {
43 memcpy(magic_str + j, magic_regexp,
44 strlen(magic_regexp));
45 j += strlen(magic_regexp);
46 } else
47 magic_str[j++] = regstr[i];
48 }
49 magic_str[j] = '\0';
50
51 regex = XMALLOC(MTYPE_BGP_REGEXP, sizeof(regex_t));
52
53 ret = regcomp(regex, magic_str, REG_EXTENDED | REG_NOSUB);
54
55 XFREE(MTYPE_TMP, magic_str);
56
57 if (ret != 0) {
58 XFREE(MTYPE_BGP_REGEXP, regex);
59 return NULL;
60 }
61
62 return regex;
63 }
64
65 int bgp_regexec(regex_t *regex, struct aspath *aspath)
66 {
67 return regexec(regex, aspath->str, 0, NULL, 0);
68 }
69
70 void bgp_regex_free(regex_t *regex)
71 {
72 regfree(regex);
73 XFREE(MTYPE_BGP_REGEXP, regex);
74 }