list.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  3. * Copyright (c) 2013-2018 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 "config.h"
  26. #include "defines.h"
  27. #include "common.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <regex.h>
  32. #include <errno.h>
  33. /**
  34. * Creates a new tcpr_list entry. Malloc's memory.
  35. */
  36. tcpr_list_t *
  37. new_list()
  38. {
  39. tcpr_list_t *newlist;
  40. newlist = (tcpr_list_t *)safe_malloc(sizeof(tcpr_list_t));
  41. return (newlist);
  42. }
  43. /**
  44. * Processes a string (ourstr) containing the list in human readable
  45. * format and places the data in **list and finally returns 1 for
  46. * success, 0 for fail.
  47. */
  48. int
  49. parse_list(tcpr_list_t ** listdata, char *ourstr)
  50. {
  51. tcpr_list_t *listcur, *list_ptr;
  52. char *this = NULL;
  53. char *first, *second;
  54. int rcode;
  55. regex_t preg;
  56. char regex[] = "^[0-9]+(-[0-9]+)?$";
  57. char *token = NULL;
  58. u_int i;
  59. /* compile the regex first */
  60. if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
  61. char ebuf[EBUF_SIZE];
  62. regerror(rcode, &preg, ebuf, sizeof(ebuf));
  63. errx(-1, "Unable to compile regex (%s): %s", regex, ebuf);
  64. }
  65. /* first iteration */
  66. this = strtok_r(ourstr, ",", &token);
  67. first = this;
  68. second = NULL;
  69. /* regex test */
  70. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  71. warnx("Unable to parse: %s", this);
  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. }
  87. else {
  88. list_ptr->max = list_ptr->min;
  89. }
  90. while (1) {
  91. this = strtok_r(NULL, ",", &token);
  92. if (this == NULL)
  93. break;
  94. first = this;
  95. second = NULL;
  96. /* regex test */
  97. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  98. warnx("Unable to parse: %s", this);
  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. }
  113. else {
  114. listcur->max = listcur->min;
  115. }
  116. }
  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. }