list.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2024 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. static void
  43. add_to_list(tcpr_list_t *list_ptr, const char *first, const char *second)
  44. {
  45. list_ptr->min = strtoull(first, NULL, 0);
  46. if (second != NULL) {
  47. if (second[0] == '\0') {
  48. list_ptr->max = 0;
  49. } else {
  50. list_ptr->max = strtoull(second, NULL, 0);
  51. }
  52. } else {
  53. list_ptr->max = list_ptr->min;
  54. }
  55. }
  56. /**
  57. * Processes a string (ourstr) containing the list in human readable
  58. * format and places the data in **list and finally returns 1 for
  59. * success, 0 for fail.
  60. */
  61. int
  62. parse_list(tcpr_list_t **listdata, char *ourstr)
  63. {
  64. tcpr_list_t *listcur, *list_ptr;
  65. char *this = NULL;
  66. char *first, *second;
  67. int rcode;
  68. regex_t preg;
  69. char regex[] = "^[0-9]+(-([0-9]+|\\s*))?$";
  70. char *token = NULL;
  71. u_int i;
  72. /* compile the regex first */
  73. if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
  74. char ebuf[EBUF_SIZE];
  75. regerror(rcode, &preg, ebuf, sizeof(ebuf));
  76. errx(-1, "Unable to compile regex (%s): %s", regex, ebuf);
  77. }
  78. /* first iteration */
  79. this = strtok_r(ourstr, ",", &token);
  80. first = this;
  81. second = NULL;
  82. /* regex test */
  83. if (this == NULL || regexec(&preg, this, 0, NULL, 0) != 0) {
  84. warnx("Unable to parse: %s", this);
  85. regfree(&preg);
  86. return 0;
  87. }
  88. *listdata = new_list();
  89. list_ptr = *listdata;
  90. listcur = list_ptr;
  91. for (i = 0; i < strlen(this); i++) {
  92. if (this[i] == '-') {
  93. this[i] = '\0';
  94. second = &this[i + 1];
  95. }
  96. }
  97. add_to_list(list_ptr, first, second);
  98. while (1) {
  99. this = strtok_r(NULL, ",", &token);
  100. if (this == NULL)
  101. break;
  102. first = this;
  103. second = NULL;
  104. /* regex test */
  105. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  106. warnx("Unable to parse: %s", this);
  107. regfree(&preg);
  108. return 0;
  109. }
  110. listcur->next = new_list();
  111. listcur = listcur->next;
  112. for (i = 0; i < strlen(this); i++) {
  113. if (this[i] == '-') {
  114. this[i] = '\0';
  115. second = &this[i + 1];
  116. }
  117. }
  118. add_to_list(listcur, first, second);
  119. }
  120. regfree(&preg);
  121. return 1;
  122. }
  123. /**
  124. * Checks to see if the given integer exists in the LIST.
  125. * Return 1 if in the list, otherwise 0
  126. */
  127. tcpr_dir_t
  128. check_list(tcpr_list_t *list, COUNTER value)
  129. {
  130. tcpr_list_t *current;
  131. for (current = list; current; current = current->next) {
  132. if (current->min != 0 && current->max != 0) {
  133. if ((value >= current->min) && (value <= current->max))
  134. return 1;
  135. } else if (current->min == 0) {
  136. if (value <= current->max)
  137. return 1;
  138. } else if (current->max == 0) {
  139. if (value >= current->min)
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }
  145. /**
  146. * Free's all the memory associated with the given LIST
  147. */
  148. void
  149. free_list(tcpr_list_t *list)
  150. {
  151. if (list == NULL)
  152. return;
  153. /* recursively go down the list */
  154. if (list->next != NULL)
  155. free_list(list->next);
  156. safe_free(list);
  157. }