]>
git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_regex.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AS regular expression routine
3 * Copyright (C) 1999 Kunihiro Ishiguro
15 #include "bgp_aspath.h"
16 #include "bgp_regex.h"
18 /* Character `_' has special mean. It represents [,{}() ] and the
19 beginning of the line(^) and the end of the line ($).
23 regex_t
*bgp_regcomp(const char *regstr
)
25 /* Convert _ character to generic regular expression. */
30 char magic_regexp
[] = "(^|[,{}() ]|$)";
35 for (i
= 0; i
< len
; i
++)
39 magic_str
= XMALLOC(MTYPE_TMP
, len
+ (14 * magic
) + 1);
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
);
47 magic_str
[j
++] = regstr
[i
];
51 regex
= XMALLOC(MTYPE_BGP_REGEXP
, sizeof(regex_t
));
53 ret
= regcomp(regex
, magic_str
, REG_EXTENDED
| REG_NOSUB
);
55 XFREE(MTYPE_TMP
, magic_str
);
58 XFREE(MTYPE_BGP_REGEXP
, regex
);
65 int bgp_regexec(regex_t
*regex
, struct aspath
*aspath
)
67 return regexec(regex
, aspath
->str
, 0, NULL
, 0);
70 void bgp_regex_free(regex_t
*regex
)
73 XFREE(MTYPE_BGP_REGEXP
, regex
);