123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /* $Id: parse_args.c 1873 2007-07-10 03:51:46Z aturner $ */
- /*
- * Copyright (c) 2006 Aaron Turner.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the names of the copyright owners nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
- * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "config.h"
- #include "defines.h"
- #include "common.h"
- #include "tcpedit-int.h"
- #include "tcpedit_stub.h"
- #include "parse_args.h"
- #include "portmap.h"
- #include <string.h>
- #include <stdlib.h>
- /*
- * returns 0 for sucess w/o errors
- * returns 1 for sucess w/ warnings
- * returns -1 for error
- */
- int
- tcpedit_post_args(tcpedit_t **tcpedit_ex) {
- tcpedit_t *tcpedit;
- int rcode = 0;
- assert(tcpedit_ex);
- tcpedit = *tcpedit_ex;
- assert(tcpedit);
- /* --pnat */
- if (HAVE_OPT(PNAT)) {
- int ct = STACKCT_OPT(PNAT);
- char **list = STACKLST_OPT(PNAT);
- int first = 1;
- tcpedit->rewrite_ip ++;
- do {
- char *p = *list++;
- if (first) {
- if (! parse_cidr_map(&tcpedit->cidrmap1, p)) {
- tcpedit_seterr(tcpedit,
- "Unable to parse first --pnat=%s", p);
- return -1;
- }
- } else {
- if (! parse_cidr_map(&tcpedit->cidrmap2, p)) {
- tcpedit_seterr(tcpedit,
- "Unable to parse second --pnat=%s", p);
- return -1;
- }
- }
-
- first = 0;
- } while (--ct > 0);
- }
-
- /* --srcipmap */
- if (HAVE_OPT(SRCIPMAP)) {
- tcpedit->rewrite_ip ++;
- if (! parse_cidr_map(&tcpedit->srcipmap, OPT_ARG(SRCIPMAP))) {
- tcpedit_seterr(tcpedit,
- "Unable to parse --srcipmap=%s", OPT_ARG(SRCIPMAP));
- return -1;
- }
- }
- /* --dstipmap */
- if (HAVE_OPT(DSTIPMAP)) {
- tcpedit->rewrite_ip ++;
- if (! parse_cidr_map(&tcpedit->dstipmap, OPT_ARG(DSTIPMAP))) {
- tcpedit_seterr(tcpedit,
- "Unable to parse --dstipmap=%s", OPT_ARG(DSTIPMAP));
- return -1;
- }
- }
- /*
- * If we have one and only one -N, then use the same map data
- * for both interfaces/files
- */
- if ((tcpedit->cidrmap1 != NULL) && (tcpedit->cidrmap2 == NULL))
- tcpedit->cidrmap2 = tcpedit->cidrmap1;
- /* --fixcsum */
- if (HAVE_OPT(FIXCSUM))
- tcpedit->fixcsum = 1;
- #ifdef ENABLE_EFCS
- /* --efcs */
- if (HAVE_OPT(EFCS))
- tcpedit->efcs = 1;
- #endif
- /* --mtu */
- if (HAVE_OPT(MTU))
- tcpedit->mtu = OPT_VALUE_MTU;
-
- /* --skipbroadcast */
- if (HAVE_OPT(SKIPBROADCAST))
- tcpedit->skip_broadcast = 1;
- /* --fixlen */
- if (HAVE_OPT(FIXLEN)) {
- if (strcmp(OPT_ARG(FIXLEN), "pad") == 0) {
- tcpedit->fixlen = TCPEDIT_FIXLEN_PAD;
- } else if (strcmp(OPT_ARG(FIXLEN), "trunc") == 0) {
- tcpedit->fixlen = TCPEDIT_FIXLEN_TRUNC;
- } else if (strcmp(OPT_ARG(FIXLEN), "del") == 0) {
- tcpedit->fixlen = TCPEDIT_FIXLEN_DEL;
- } else {
- tcpedit_seterr(tcpedit, "Invalid --fixlen=%s", OPT_ARG(FIXLEN));
- return -1;
- }
- }
-
- /* TCP/UDP port rewriting */
- if (HAVE_OPT(PORTMAP)) {
- if (! parse_portmap(&tcpedit->portmap, OPT_ARG(PORTMAP))) {
- tcpedit_seterr(tcpedit,
- "Unable to parse --portmap=%s", OPT_ARG(PORTMAP));
- return -1;
- }
- }
- /*
- * IP address rewriting processing. Call srandom() then add up
- * 5 calls to random() as our mixer for randomizing. This should
- * work better since most people aren't going to write out values
- * close to 32bit integers.
- */
- if (HAVE_OPT(SEED)) {
- tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
- srandom(OPT_VALUE_SEED);
- tcpedit->seed = random() + random() + random() + random() + random();
- }
- if (HAVE_OPT(ENDPOINTS)) {
- tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
- if (! parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2,
- OPT_ARG(ENDPOINTS))) {
- tcpedit_seterr(tcpedit,
- "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
- return -1;
- }
- }
- /*
- * figure out the max packet len
- if (tcpedit->l2.enabled) {
- // custom l2 header
- dbg(1, "Using custom L2 header to calculate max frame size\n");
- tcpedit->maxpacket = tcpedit->mtu + tcpedit->l2.len;
- }
- else if (tcpedit->l2.dlt == DLT_EN10MB || tcpedit->l2.dlt == DLT_VLAN) {
- // ethernet
- dbg(1, "Using Ethernet to calculate max frame size\n");
- tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
- } else {
- // uh, wtf is this now? we'll just assume ethernet and hope things work
- tcpedit->maxpacket = tcpedit->mtu + TCPR_ETH_H;
- tcpedit_seterr(tcpedit,
- "Unsupported DLT type: %s. We'll just treat it as ethernet.\n"
- "You may need to increase the MTU (-t <size>) if you get errors\n",
- pcap_datalink_val_to_name(tcpedit->l2.dlt));
- rcode = 1;
- }
- */
- return rcode;
- }
|