list.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* $Id: list.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * A generic method to parse a list of integers which are
  33. * delimited by commas and dashes to indicate individual
  34. * numbers and ranges
  35. * Provides both a way to process the list and determine
  36. * if an integer exists in the list.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <regex.h>
  42. #include <errno.h>
  43. #include "config.h"
  44. #include "tcpreplay.h"
  45. #include "err.h"
  46. #include "cidr.h"
  47. #include "list.h"
  48. #define EBUF_SIZE 256
  49. LIST *
  50. new_list()
  51. {
  52. LIST *newlist;
  53. newlist = (LIST *) malloc(sizeof(LIST));
  54. if (newlist == NULL)
  55. err(1, "unable to malloc memory for new_list()");
  56. memset(newlist, 0, sizeof(LIST));
  57. return (newlist);
  58. }
  59. /*
  60. * Processes a string (ourstr) containing the list in human readable
  61. * format and places the data in **list and finally returns 1 for
  62. * success, 0 for fail.
  63. */
  64. int
  65. parse_list(LIST ** listdata, char *ourstr)
  66. {
  67. LIST *listcur, *list_ptr;
  68. char *this = NULL;
  69. char *first, *second;
  70. int i, rcode;
  71. regex_t preg;
  72. char ebuf[EBUF_SIZE];
  73. char regex[] = "^[0-9]+(-[0-9]+)?$";
  74. char *token = NULL;
  75. /* compile the regex first */
  76. if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
  77. regerror(rcode, &preg, ebuf, sizeof(ebuf));
  78. errx(1, "Unable to compile regex (%s): %s", regex, ebuf);
  79. }
  80. /* first iteration */
  81. this = strtok_r(ourstr, ",", &token);
  82. first = this;
  83. second = NULL;
  84. /* regex test */
  85. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  86. warnx("Unable to parse: %s", this);
  87. return 0;
  88. }
  89. *listdata = new_list();
  90. list_ptr = *listdata;
  91. listcur = list_ptr;
  92. for (i = 0; i < strlen(this); i++) {
  93. if (this[i] == '-') {
  94. this[i] = '\0';
  95. second = &this[i + 1];
  96. }
  97. }
  98. list_ptr->min = strtoull(first, NULL, 0);
  99. if (second != NULL) {
  100. list_ptr->max = strtoull(second, NULL, 0);
  101. }
  102. else {
  103. list_ptr->max = list_ptr->min;
  104. }
  105. while (1) {
  106. this = strtok_r(NULL, ",", &token);
  107. if (this == NULL)
  108. break;
  109. first = this;
  110. second = NULL;
  111. /* regex test */
  112. if (regexec(&preg, this, 0, NULL, 0) != 0) {
  113. warnx("Unable to parse: %s", this);
  114. return 0;
  115. }
  116. listcur->next = new_list();
  117. listcur = listcur->next;
  118. for (i = 0; i < strlen(this); i++) {
  119. if (this[i] == '-') {
  120. this[i] = '\0';
  121. second = &this[i + 1];
  122. }
  123. }
  124. listcur->min = strtoull(first, NULL, 0);
  125. if (second != NULL) {
  126. listcur->max = strtoull(second, NULL, 0);
  127. }
  128. else {
  129. listcur->max = listcur->min;
  130. }
  131. }
  132. return 1;
  133. }
  134. /*
  135. * Checks to see if the given integer exists in the LIST.
  136. * Returns 1 for true, 0 for false
  137. */
  138. int
  139. check_list(LIST * list, u_int64_t value)
  140. {
  141. LIST *current;
  142. current = list;
  143. do {
  144. if ((current->min != 0) && (current->max != 0)) {
  145. if ((value >= current->min) && (value <= current->max))
  146. return 1;
  147. }
  148. else if (current->min == 0) {
  149. if (value <= current->max)
  150. return 1;
  151. }
  152. else if (current->max == 0) {
  153. if (value >= current->min)
  154. return 1;
  155. }
  156. if (current->next != NULL) {
  157. current = current->next;
  158. }
  159. else {
  160. current = NULL;
  161. }
  162. } while (current != NULL);
  163. return 0;
  164. }
  165. /*
  166. * Free's all the memory associated with the given LIST
  167. */
  168. void
  169. free_list(LIST * list)
  170. {
  171. /* recursively go down the list */
  172. if (list->next != NULL)
  173. free_list(list->next);
  174. free(list);
  175. }