parse_args.c 7.4 KB

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