/*
                                  NETWOX
                             Network toolbox
                Copyright(c) 1999-2005 Laurent Constantin
                                  -----

  Main server    : http://www.laurentconstantin.com/
  Backup servers : http://go.to/laurentconstantin/
                   http://laurentconstantin.est-la.com/
                   http://laurentconstantin.free.fr/
                   http://membres.lycos.fr/lauconstantin/
  [my current email address is on the web servers]

                                  -----
  This file is part of Netwox.

  Netwox is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  version 2 as published by the Free Software Foundation.

  Netwox is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details (http://www.gnu.org/).

------------------------------------------------------------------------
*/

/*-------------------------------------------------------------*/
#include "../netwox.h"

/*-------------------------------------------------------------*/
netwib_conststring t000042_description[] = {
  NETWOX_DESC_spoof_sample,
  "Packets can be fragmented before been sent.",
  NETWOX_DESC_spoofip,
  NETWOX_DESC_toolpriv_spoof,
  NULL
};
netwox_toolarg t000042_args[] = {
  NETWOX_TOOLARG_OPTA_SPOOFIP('a', NULL, NULL, NULL),
  NETWOX_TOOLARG_OPT_IP_SRC('s', "ip4-src", "IP4 src", NULL),
  NETWOX_TOOLARG_OPT_IP_DST('d', "ip4-dst", "IP4 dst", NULL),
  NETWOX_TOOLARG_OPT_PORT_DST('S', "tcp-src", "TCP port src", NULL),
  NETWOX_TOOLARG_OPT_PORT_DST('D', "tcp-dst", "TCP port dst", NULL),
  NETWOX_TOOLARG_OPT_UINT32('n', "sample", "number of sample (1 to n)", "1"),
  NETWOX_TOOLARG_OPT_UINT32('f', "fragsize", "fragment size (0=nofrag)", NULL),
  NETWOX_TOOLARG_OPT_BOOL('x', "display", "display", NULL),
  NETWOX_TOOLARG_END
};
netwox_tooltreenodetype t000042_nodes[] = {
  NETWOX_TOOLTREENODETYPE_SPOOF_IP,
  NETWOX_TOOLTREENODETYPE_SPOOF_TCP,
  NETWOX_TOOLTREENODETYPE_END
};
netwox_tool_info t000042_info = {
  "Spoof of packet samples : fragment",
  t000042_description,
  "ip4, send",
  t000042_args,
  t000042_nodes,
};

/*-------------------------------------------------------------*/
netwib_err t000042_core(int argc, char *argv[])
{
  netwox_arg *parg;
  netwib_buf pkt;
  netwib_io *pio;
  netwib_spoof_ip_inittype inittype;
  netwib_uint32 fragsize;
  netwox_sample_info sampleinfo;
  netwox_sample sample;
  netwib_bool display, isset;

  /* obtain parameters */
  netwib_er(netwox_arg_init(argc, argv, &t000042_info, &parg));

  netwib_er(netwox_arg_spoofip(parg, 'a', &inittype));
  netwib_er(netwox_arg_uint32(parg, 'f', &fragsize));
  netwib_er(netwox_arg_bool(parg, 'x', &display));
  netwib_er(netwox_spooffrag_init(inittype, fragsize, 0, display, &pio));

  netwib_er(netwox_sample_info_initdefault(&sampleinfo));
  netwib_er(netwox_arg_ip(parg, 's', &sampleinfo.ipsrc));
  netwib_er(netwox_arg_ip(parg, 'd', &sampleinfo.ipdst));
  netwib_er(netwox_arg_isset(parg, 'S', &isset));
  if (isset) {
    netwib_er(netwox_arg_port(parg, 'S', &sampleinfo.portsrc));
  }
  netwib_er(netwox_arg_isset(parg, 'D', &isset));
  if (isset) {
    netwib_er(netwox_arg_port(parg, 'D', &sampleinfo.portdst));
  }

  netwib_er(netwox_arg_uint32(parg, 'n', (netwib_uint32*)&sample));

  /* construct packet */
  netwib_er(netwib_buf_init_mallocdefault(&pkt));
  netwib_er(netwox_sample_pkt(sample, &sampleinfo, &pkt));

  /* send */
  netwib_er(netwib_io_write(pio, &pkt));

  /* close */
  netwib_er(netwib_buf_close(&pkt));
  netwib_er(netwib_io_close(&pio));
  netwib_er(netwox_arg_close(&parg));

  return(NETWIB_ERR_OK);
}
