list.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  4. *
  5. * The Tcpreplay Suite of tools is free software: you can redistribute it
  6. * and/or modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or with the authors permission any later version.
  9. *
  10. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * A generic method to parse a list of integers which are
  20. * delimited by commas and dashes to indicate individual
  21. * numbers and ranges
  22. * Provides both a way to process the list and determine
  23. * if an integer exists in the list.
  24. */
  25. #include "defines.h"
  26. #include "config.h"
  27. #include "common.h"
  28. #include <regex.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <sys/types.h>
  32. /**
  33. * Creates a new tcpr_list entry. Malloc's memory.
  34. */
  35. tcpr_list_t *
  36. new_list()
  37. {
  38. tcpr_list_t *newlist;
  39. newlist = (tcpr_list_t *)safe_malloc(sizeof(tcpr_list_t));
  40. return (newlist);
  41. }
  42. /**
  43. * Processes a string (ourstr) containing the list in human readable
  44. * format and places the data in **list and finally returns 1 for
  45. * success, 0 for fail.
  46. */
  47. int
  48. parse_list(tcpr_list_t **listdata, char *ourstr)
  49. {
  50. tcpr_list_t *listcur, *list_ptr;
  51. char *this = NULL;
  52. char *first, *second;
  53. int rcode;
  54. regex_t preg;
  55. char regex[] = "^[0-9]+(-[0-9]+)?$";
  56. char *token = NULL;
  57. u_int i;
  58. /* compile the regex first */
  59. if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
  60. char ebuf[EBUF_SIZE];
  61. regerror(rcode, &preg, ebuf, sizeof(ebuf));
  62. errx(-1, "Unable to compile regex (%s): %s", regex, ebuf);
  63. }
  64. /* first iteration */
  65. this = strtok_r(ourstr, ",", &token);
  66. first = this;
  67. second = NULL;
  68. /* regex test */
  69. if (this == NULL || regexec(&preg, this, 0, NULL, 0) != 0) {
  70. warnx("Unable to parse: %s", this);
  71. regfree(&preg);
  72. return 0;
  73. }
  74. *listdata = new_list();
  75. list_ptr = *listdata;
  76. listcur = list_ptr;
  77. for (i = 0; i < strlen(this); i++) {
  78. if (this[i] == '-') {
  79. this[i] = '\0';
  80. second = &this[i + 1];
  81. }
  82. }
  83. list_ptr->min = strtoull(first, NULL, 0);
  84. if (second != NULL) {
  85. list_ptr->max = strtoull(second, NULL, 0);
  86. } else {
  87. list_ptr->max = list_ptr->min;
  88. }
  89. while (1) {
  90. this = strtok_r(NULL, ",", &token);
  91. if (this == NULL)
  92. break;
  93. first = this;
  94. second = NULL;
  95. /* regex test */
  96. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  97. warnx("Unable to parse: %s", this);
  98. regfree(&preg);
  99. return 0;
  100. }
  101. listcur->next = new_list();
  102. listcur = listcur->next;
  103. for (i = 0; i < strlen(this); i++) {
  104. if (this[i] == '-') {
  105. this[i] = '\0';
  106. second = &this[i + 1];
  107. }
  108. }
  109. listcur->min = strtoull(first, NULL, 0);
  110. if (second != NULL) {
  111. listcur->max = strtoull(second, NULL, 0);
  112. } else {
  113. listcur->max = listcur->min;
  114. }
  115. }
  116. regfree(&preg);
  117. return 1;
  118. }
  119. /**
  120. * Checks to see if the given integer exists in the LIST.
  121. * Return 1 if in the list, otherwise 0
  122. */
  123. tcpr_dir_t
  124. check_list(tcpr_list_t *list, COUNTER value)
  125. {
  126. tcpr_list_t *current;
  127. current = list;
  128. do {
  129. if ((current->min != 0) && (current->max != 0)) {
  130. if ((value >= current->min) && (value <= current->max))
  131. return 1;
  132. } else if (current->min == 0) {
  133. if (value <= current->max)
  134. return 1;
  135. } else if (current->max == 0) {
  136. if (value >= current->min)
  137. return 1;
  138. }
  139. if (current->next != NULL)
  140. current = current->next;
  141. else
  142. current = NULL;
  143. } while (current != NULL);
  144. return 0;
  145. }
  146. /**
  147. * Free's all the memory associated with the given LIST
  148. */
  149. void
  150. free_list(tcpr_list_t *list)
  151. {
  152. /* recursively go down the list */
  153. if (list->next != NULL)
  154. free_list(list->next);
  155. safe_free(list);
  156. }