]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
718e3744 | 2 | /* AS regular expression routine |
896014f4 | 3 | * Copyright (C) 1999 Kunihiro Ishiguro |
896014f4 | 4 | */ |
718e3744 | 5 | |
6 | #include <zebra.h> | |
7 | ||
8 | #include "log.h" | |
9 | #include "command.h" | |
10 | #include "memory.h" | |
3f9c7369 | 11 | #include "queue.h" |
039f3a34 | 12 | #include "filter.h" |
718e3744 | 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 | |
d62a17ae | 19 | beginning of the line(^) and the end of the line ($). |
718e3744 | 20 | |
21 | (^|[,{}() ]|$) */ | |
22 | ||
d62a17ae | 23 | regex_t *bgp_regcomp(const char *regstr) |
718e3744 | 24 | { |
d62a17ae | 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]; | |
718e3744 | 48 | } |
d62a17ae | 49 | magic_str[j] = '\0'; |
718e3744 | 50 | |
d62a17ae | 51 | regex = XMALLOC(MTYPE_BGP_REGEXP, sizeof(regex_t)); |
718e3744 | 52 | |
d62a17ae | 53 | ret = regcomp(regex, magic_str, REG_EXTENDED | REG_NOSUB); |
718e3744 | 54 | |
d62a17ae | 55 | XFREE(MTYPE_TMP, magic_str); |
718e3744 | 56 | |
d62a17ae | 57 | if (ret != 0) { |
58 | XFREE(MTYPE_BGP_REGEXP, regex); | |
59 | return NULL; | |
60 | } | |
718e3744 | 61 | |
d62a17ae | 62 | return regex; |
718e3744 | 63 | } |
64 | ||
d62a17ae | 65 | int bgp_regexec(regex_t *regex, struct aspath *aspath) |
718e3744 | 66 | { |
d62a17ae | 67 | return regexec(regex, aspath->str, 0, NULL, 0); |
718e3744 | 68 | } |
69 | ||
d62a17ae | 70 | void bgp_regex_free(regex_t *regex) |
718e3744 | 71 | { |
d62a17ae | 72 | regfree(regex); |
73 | XFREE(MTYPE_BGP_REGEXP, regex); | |
718e3744 | 74 | } |