parse_args.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* $Id: parse_args.c 1758 2007-03-22 06:10:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2006 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. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include "tcpedit-int.h"
  35. #include "tcpedit_stub.h"
  36. #include "parse_args.h"
  37. #include "portmap.h"
  38. #include <string.h>
  39. #include <stdlib.h>
  40. /*
  41. * returns 0 for sucess w/o errors
  42. * returns 1 for sucess w/ warnings
  43. * returns -1 for error
  44. */
  45. int
  46. tcpedit_post_args(tcpedit_t **tcpedit_ex) {
  47. tcpedit_t *tcpedit;
  48. int rcode = 0;
  49. assert(tcpedit_ex);
  50. tcpedit = *tcpedit_ex;
  51. assert(tcpedit);
  52. /* --pnat */
  53. if (HAVE_OPT(PNAT)) {
  54. int ct = STACKCT_OPT(PNAT);
  55. char **list = STACKLST_OPT(PNAT);
  56. int first = 1;
  57. tcpedit->rewrite_ip ++;
  58. do {
  59. char *p = *list++;
  60. if (first) {
  61. if (! parse_cidr_map(&tcpedit->cidrmap1, p)) {
  62. tcpedit_seterr(tcpedit,
  63. "Unable to parse first --pnat=%s", p);
  64. return -1;
  65. }
  66. } else {
  67. if (! parse_cidr_map(&tcpedit->cidrmap2, p)) {
  68. tcpedit_seterr(tcpedit,
  69. "Unable to parse second --pnat=%s", p);
  70. return -1;
  71. }
  72. }
  73. first = 0;
  74. } while (--ct > 0);
  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 = 1;
  85. #ifdef ENABLE_EFCS
  86. /* --efcs */
  87. if (HAVE_OPT(EFCS))
  88. tcpedit->efcs = 1;
  89. #endif
  90. /* --mtu */
  91. if (HAVE_OPT(MTU))
  92. tcpedit->mtu = OPT_VALUE_MTU;
  93. /* --skipbroadcast */
  94. if (HAVE_OPT(SKIPBROADCAST))
  95. tcpedit->skip_broadcast = 1;
  96. /* --fixlen */
  97. if (HAVE_OPT(FIXLEN)) {
  98. if (strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
  99. tcpedit->fixlen = TCPEDIT_FIXLEN_PAD;
  100. } else if (strcmp(OPT_ARG(FIXLEN), "trunc") == 0) {
  101. tcpedit->fixlen = TCPEDIT_FIXLEN_TRUNC;
  102. } else if (strcmp(OPT_ARG(FIXLEN), "del") == 0) {
  103. tcpedit->fixlen = TCPEDIT_FIXLEN_DEL;
  104. } else {
  105. tcpedit_seterr(tcpedit, "Invalid --fixlen=%s", OPT_ARG(FIXLEN));
  106. return -1;
  107. }
  108. }
  109. /* TCP/UDP port rewriting */
  110. if (HAVE_OPT(PORTMAP)) {
  111. if (! parse_portmap(&tcpedit->portmap, OPT_ARG(PORTMAP))) {
  112. tcpedit_seterr(tcpedit,
  113. "Unable to parse --portmap=%s", OPT_ARG(PORTMAP));
  114. return -1;
  115. }
  116. }
  117. /*
  118. * IP address rewriting processing. Call srandom() then add up
  119. * 5 calls to random() as our mixer for randomizing. This should
  120. * work better since most people aren't going to write out values
  121. * close to 32bit integers.
  122. */
  123. if (HAVE_OPT(SEED)) {
  124. tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
  125. srandom(OPT_VALUE_SEED);
  126. tcpedit->seed = random() + random() + random() + random() + random();
  127. }
  128. if (HAVE_OPT(ENDPOINTS)) {
  129. tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
  130. if (! parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2,
  131. OPT_ARG(ENDPOINTS))) {
  132. tcpedit_seterr(tcpedit,
  133. "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
  134. return -1;
  135. }
  136. }
  137. /*
  138. * figure out the max packet len
  139. if (tcpedit->l2.enabled) {
  140. // custom l2 header
  141. dbg(1, "Using custom L2 header to calculate max frame size\n");
  142. tcpedit->maxpacket = tcpedit->mtu + tcpedit->l2.len;
  143. }
  144. else if (tcpedit->l2.dlt == DLT_EN10MB || tcpedit->l2.dlt == DLT_VLAN) {
  145. // ethernet
  146. dbg(1, "Using Ethernet to calculate max frame size\n");
  147. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  148. } else {
  149. // uh, wtf is this now? we'll just assume ethernet and hope things work
  150. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  151. tcpedit_seterr(tcpedit,
  152. "Unsupported DLT type: %s. We'll just treat it as ethernet.\n"
  153. "You may need to increase the MTU (-t <size>) if you get errors\n",
  154. pcap_datalink_val_to_name(tcpedit->l2.dlt));
  155. rcode = 1;
  156. }
  157. */
  158. return rcode;
  159. }