parse_args.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* $Id: parse_args.c 1873 2007-07-10 03:51:46Z 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. /* --srcipmap */
  77. if (HAVE_OPT(SRCIPMAP)) {
  78. tcpedit->rewrite_ip ++;
  79. if (! parse_cidr_map(&tcpedit->srcipmap, OPT_ARG(SRCIPMAP))) {
  80. tcpedit_seterr(tcpedit,
  81. "Unable to parse --srcipmap=%s", OPT_ARG(SRCIPMAP));
  82. return -1;
  83. }
  84. }
  85. /* --dstipmap */
  86. if (HAVE_OPT(DSTIPMAP)) {
  87. tcpedit->rewrite_ip ++;
  88. if (! parse_cidr_map(&tcpedit->dstipmap, OPT_ARG(DSTIPMAP))) {
  89. tcpedit_seterr(tcpedit,
  90. "Unable to parse --dstipmap=%s", OPT_ARG(DSTIPMAP));
  91. return -1;
  92. }
  93. }
  94. /*
  95. * If we have one and only one -N, then use the same map data
  96. * for both interfaces/files
  97. */
  98. if ((tcpedit->cidrmap1 != NULL) && (tcpedit->cidrmap2 == NULL))
  99. tcpedit->cidrmap2 = tcpedit->cidrmap1;
  100. /* --fixcsum */
  101. if (HAVE_OPT(FIXCSUM))
  102. tcpedit->fixcsum = 1;
  103. #ifdef ENABLE_EFCS
  104. /* --efcs */
  105. if (HAVE_OPT(EFCS))
  106. tcpedit->efcs = 1;
  107. #endif
  108. /* --mtu */
  109. if (HAVE_OPT(MTU))
  110. tcpedit->mtu = OPT_VALUE_MTU;
  111. /* --skipbroadcast */
  112. if (HAVE_OPT(SKIPBROADCAST))
  113. tcpedit->skip_broadcast = 1;
  114. /* --fixlen */
  115. if (HAVE_OPT(FIXLEN)) {
  116. if (strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
  117. tcpedit->fixlen = TCPEDIT_FIXLEN_PAD;
  118. } else if (strcmp(OPT_ARG(FIXLEN), "trunc") == 0) {
  119. tcpedit->fixlen = TCPEDIT_FIXLEN_TRUNC;
  120. } else if (strcmp(OPT_ARG(FIXLEN), "del") == 0) {
  121. tcpedit->fixlen = TCPEDIT_FIXLEN_DEL;
  122. } else {
  123. tcpedit_seterr(tcpedit, "Invalid --fixlen=%s", OPT_ARG(FIXLEN));
  124. return -1;
  125. }
  126. }
  127. /* TCP/UDP port rewriting */
  128. if (HAVE_OPT(PORTMAP)) {
  129. if (! parse_portmap(&tcpedit->portmap, OPT_ARG(PORTMAP))) {
  130. tcpedit_seterr(tcpedit,
  131. "Unable to parse --portmap=%s", OPT_ARG(PORTMAP));
  132. return -1;
  133. }
  134. }
  135. /*
  136. * IP address rewriting processing. Call srandom() then add up
  137. * 5 calls to random() as our mixer for randomizing. This should
  138. * work better since most people aren't going to write out values
  139. * close to 32bit integers.
  140. */
  141. if (HAVE_OPT(SEED)) {
  142. tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
  143. srandom(OPT_VALUE_SEED);
  144. tcpedit->seed = random() + random() + random() + random() + random();
  145. }
  146. if (HAVE_OPT(ENDPOINTS)) {
  147. tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
  148. if (! parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2,
  149. OPT_ARG(ENDPOINTS))) {
  150. tcpedit_seterr(tcpedit,
  151. "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
  152. return -1;
  153. }
  154. }
  155. /*
  156. * figure out the max packet len
  157. if (tcpedit->l2.enabled) {
  158. // custom l2 header
  159. dbg(1, "Using custom L2 header to calculate max frame size\n");
  160. tcpedit->maxpacket = tcpedit->mtu + tcpedit->l2.len;
  161. }
  162. else if (tcpedit->l2.dlt == DLT_EN10MB || tcpedit->l2.dlt == DLT_VLAN) {
  163. // ethernet
  164. dbg(1, "Using Ethernet to calculate max frame size\n");
  165. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  166. } else {
  167. // uh, wtf is this now? we'll just assume ethernet and hope things work
  168. tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
  169. tcpedit_seterr(tcpedit,
  170. "Unsupported DLT type: %s. We'll just treat it as ethernet.\n"
  171. "You may need to increase the MTU (-t <size>) if you get errors\n",
  172. pcap_datalink_val_to_name(tcpedit->l2.dlt));
  173. rcode = 1;
  174. }
  175. */
  176. return rcode;
  177. }