parse_args.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "parse_args.h"
  20. #include "config.h"
  21. #include "portmap.h"
  22. #include "tcpedit.h"
  23. #include "tcpedit_stub.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. /**
  27. * returns 0 for success w/o errors
  28. * returns 1 for success w/ warnings
  29. * returns -1 for error
  30. */
  31. int
  32. tcpedit_post_args(tcpedit_t *tcpedit)
  33. {
  34. int rcode;
  35. int i;
  36. uint32_t seed = 1, rand_num;
  37. assert(tcpedit);
  38. /* --pnat */
  39. if (HAVE_OPT(PNAT)) {
  40. int ct = STACKCT_OPT(PNAT);
  41. char **list = (char **)STACKLST_OPT(PNAT);
  42. int first = 1;
  43. tcpedit->rewrite_ip = true;
  44. do {
  45. char *p = *list++;
  46. if (first) {
  47. if (!parse_cidr_map(&tcpedit->cidrmap1, p)) {
  48. tcpedit_seterr(tcpedit, "Unable to parse first --pnat=%s", p);
  49. return -1;
  50. }
  51. } else {
  52. if (!parse_cidr_map(&tcpedit->cidrmap2, p)) {
  53. tcpedit_seterr(tcpedit, "Unable to parse second --pnat=%s", p);
  54. return -1;
  55. }
  56. }
  57. first = 0;
  58. } while (--ct > 0);
  59. }
  60. /* --srcipmap */
  61. if (HAVE_OPT(SRCIPMAP)) {
  62. tcpedit->rewrite_ip = true;
  63. if (!parse_cidr_map(&tcpedit->srcipmap, OPT_ARG(SRCIPMAP))) {
  64. tcpedit_seterr(tcpedit, "Unable to parse --srcipmap=%s", OPT_ARG(SRCIPMAP));
  65. return -1;
  66. }
  67. }
  68. /* --dstipmap */
  69. if (HAVE_OPT(DSTIPMAP)) {
  70. tcpedit->rewrite_ip = true;
  71. if (!parse_cidr_map(&tcpedit->dstipmap, OPT_ARG(DSTIPMAP))) {
  72. tcpedit_seterr(tcpedit, "Unable to parse --dstipmap=%s", OPT_ARG(DSTIPMAP));
  73. return -1;
  74. }
  75. }
  76. /*
  77. * If we have one and only one -N, then use the same map data
  78. * for both interfaces/files
  79. */
  80. if ((tcpedit->cidrmap1 != NULL) && (tcpedit->cidrmap2 == NULL))
  81. tcpedit->cidrmap2 = tcpedit->cidrmap1;
  82. /* --fixcsum */
  83. if (HAVE_OPT(FIXCSUM))
  84. tcpedit->fixcsum = true;
  85. /* --fixhdrlen */
  86. if (HAVE_OPT(FIXHDRLEN))
  87. tcpedit->fixhdrlen = true;
  88. /* --efcs */
  89. if (HAVE_OPT(EFCS))
  90. tcpedit->efcs = true;
  91. /* --ttl */
  92. if (HAVE_OPT(TTL)) {
  93. long ttl;
  94. if (strchr(OPT_ARG(TTL), '+')) {
  95. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_ADD;
  96. } else if (strchr(OPT_ARG(TTL), '-')) {
  97. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_SUB;
  98. } else {
  99. tcpedit->ttl_mode = TCPEDIT_TTL_MODE_SET;
  100. }
  101. ttl = strtol(OPT_ARG(TTL), (char **)NULL, 10);
  102. if (ttl < 0)
  103. ttl *= -1; /* convert to positive value */
  104. if (ttl > 255) {
  105. tcpedit_seterr(tcpedit, "Invalid --ttl value (must be 0-255): %ld", ttl);
  106. return -1;
  107. }
  108. tcpedit->ttl_value = (u_int8_t)ttl;
  109. }
  110. /* --tos */
  111. if (HAVE_OPT(TOS))
  112. tcpedit->tos = OPT_VALUE_TOS;
  113. /* --tclass */
  114. if (HAVE_OPT(TCLASS))
  115. tcpedit->tclass = OPT_VALUE_TCLASS;
  116. /* --flowlabel */
  117. if (HAVE_OPT(FLOWLABEL))
  118. tcpedit->flowlabel = OPT_VALUE_FLOWLABEL;
  119. /* --mtu */
  120. if (HAVE_OPT(MTU))
  121. tcpedit->mtu = OPT_VALUE_MTU;
  122. /* --mtu-trunc */
  123. if (HAVE_OPT(MTU_TRUNC))
  124. tcpedit->mtu_truncate = true;
  125. /* --skipbroadcast */
  126. if (HAVE_OPT(SKIPBROADCAST))
  127. tcpedit->skip_broadcast = true;
  128. /* --fixlen */
  129. if (HAVE_OPT(FIXLEN)) {
  130. if (strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
  131. tcpedit->fixlen = TCPEDIT_FIXLEN_PAD;
  132. } else if (strcmp(OPT_ARG(FIXLEN), "trunc") == 0) {
  133. tcpedit->fixlen = TCPEDIT_FIXLEN_TRUNC;
  134. } else if (strcmp(OPT_ARG(FIXLEN), "del") == 0) {
  135. tcpedit->fixlen = TCPEDIT_FIXLEN_DEL;
  136. } else {
  137. tcpedit_seterr(tcpedit, "Invalid --fixlen=%s", OPT_ARG(FIXLEN));
  138. return -1;
  139. }
  140. }
  141. /* --rewrite-sequence */
  142. if (HAVE_OPT(TCP_SEQUENCE)) {
  143. tcpedit->tcp_sequence_enable = 1;
  144. seed = OPT_VALUE_TCP_SEQUENCE;
  145. for (i = 0; i < 5; ++i)
  146. rand_num = tcpr_random(&seed);
  147. tcpedit->tcp_sequence_adjust = rand_num;
  148. }
  149. /* TCP/UDP port rewriting */
  150. if (HAVE_OPT(PORTMAP)) {
  151. int ct = STACKCT_OPT(PORTMAP);
  152. char **list = (char **)STACKLST_OPT(PORTMAP);
  153. int first = 1;
  154. tcpedit_portmap_t *portmap_head, *portmap;
  155. do {
  156. char *p = *list++;
  157. if (first) {
  158. if (!parse_portmap(&tcpedit->portmap, p)) {
  159. tcpedit_seterr(tcpedit, "Unable to parse --portmap=%s", p);
  160. return -1;
  161. }
  162. } else {
  163. if (!parse_portmap(&portmap, p)) {
  164. tcpedit_seterr(tcpedit, "Unable to parse --portmap=%s", p);
  165. return -1;
  166. }
  167. /* append to end of tcpedit->portmap linked list */
  168. portmap_head = tcpedit->portmap;
  169. while (portmap_head->next != NULL)
  170. portmap_head = portmap_head->next;
  171. portmap_head->next = portmap;
  172. }
  173. first = 0;
  174. } while (--ct > 0);
  175. }
  176. /*
  177. * IP address rewriting processing. Update seed by making
  178. * 5 calls to tcpr_random() as our mixer for randomizing. This should
  179. * work better since most people aren't going to write out values
  180. * close to 32bit integers.
  181. */
  182. if (HAVE_OPT(SEED)) {
  183. tcpedit->rewrite_ip = true;
  184. seed = OPT_VALUE_SEED;
  185. } else if (HAVE_OPT(FUZZ_SEED)) {
  186. /* --fuzz-seed */
  187. seed = OPT_VALUE_FUZZ_SEED;
  188. tcpedit->fuzz_factor = OPT_VALUE_FUZZ_FACTOR;
  189. }
  190. for (i = 0; i < 5; ++i)
  191. rand_num = tcpr_random(&seed);
  192. srandom(rand_num);
  193. if (HAVE_OPT(SEED)) {
  194. tcpedit->rewrite_ip = true;
  195. tcpedit->seed = seed;
  196. }
  197. if (HAVE_OPT(FUZZ_SEED)) {
  198. /* --fuzz-seed */
  199. tcpedit->fuzz_seed = seed;
  200. }
  201. if (HAVE_OPT(ENDPOINTS)) {
  202. tcpedit->rewrite_ip = true;
  203. if (!parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2, OPT_ARG(ENDPOINTS))) {
  204. tcpedit_seterr(tcpedit, "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
  205. return -1;
  206. }
  207. }
  208. /* parse the tcpedit dlt args */
  209. rcode = tcpedit_dlt_post_args(tcpedit);
  210. if (rcode < 0) {
  211. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  212. } else if (rcode == 1) {
  213. warnx("%s", tcpedit_geterr(tcpedit));
  214. }
  215. return 0;
  216. }