bgp_regex.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* AS regular expression routine
  2. Copyright (C) 1999 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "log.h"
  18. #include "command.h"
  19. #include "memory.h"
  20. #include "filter.h"
  21. #include "bgpd.h"
  22. #include "bgp_aspath.h"
  23. #include "bgp_regex.h"
  24. /* Character `_' has special mean. It represents [,{}() ] and the
  25. beginning of the line(^) and the end of the line ($).
  26. (^|[,{}() ]|$) */
  27. regex_t *
  28. bgp_regcomp (const char *regstr)
  29. {
  30. /* Convert _ character to generic regular expression. */
  31. int i, j;
  32. int len;
  33. int magic = 0;
  34. char *magic_str;
  35. char magic_regexp[] = "(^|[,{}() ]|$)";
  36. int ret;
  37. regex_t *regex;
  38. len = strlen (regstr);
  39. for (i = 0; i < len; i++)
  40. if (regstr[i] == '_')
  41. magic++;
  42. magic_str = XMALLOC (MTYPE_TMP, len + (14 * magic) + 1);
  43. for (i = 0, j = 0; i < len; i++)
  44. {
  45. if (regstr[i] == '_')
  46. {
  47. memcpy (magic_str + j, magic_regexp, strlen (magic_regexp));
  48. j += strlen (magic_regexp);
  49. }
  50. else
  51. magic_str[j++] = regstr[i];
  52. }
  53. magic_str[j] = '\0';
  54. regex = XMALLOC (MTYPE_BGP_REGEXP, sizeof (regex_t));
  55. ret = regcomp (regex, magic_str, REG_EXTENDED|REG_NOSUB);
  56. XFREE (MTYPE_TMP, magic_str);
  57. if (ret != 0)
  58. {
  59. XFREE (MTYPE_BGP_REGEXP, regex);
  60. return NULL;
  61. }
  62. return regex;
  63. }
  64. int
  65. bgp_regexec (regex_t *regex, struct aspath *aspath)
  66. {
  67. return regexec (regex, aspath->str, 0, NULL, 0);
  68. }
  69. void
  70. bgp_regex_free (regex_t *regex)
  71. {
  72. regfree (regex);
  73. XFREE (MTYPE_BGP_REGEXP, regex);
  74. }