<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "/usr/share/xml/docbook/schema/dtd/4.2/docbookx.dtd" []>

<refentry id="nexp">

  <refentryinfo>
    <date>29 November 2006</date>
  </refentryinfo>

  <refmeta>
    <refentrytitle>nexp</refentrytitle>
    <manvolnum>1</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>nexp</refname>
    <refpurpose>
      A framework for crafting network packets and processing responses
    </refpurpose>
  </refnamediv>

  <refsynopsisdiv>
    <cmdsynopsis>
      <command>nexp</command>
      <arg choice="opt"><option>-V</option></arg>
      <arg choice="opt"><option>-v</option></arg>
      <arg choice="opt"><option>-t</option></arg>
      <arg choice="opt"><option>-c</option> <replaceable class="option">cmd</replaceable></arg>
      <arg choice="opt"><option>-s</option> <replaceable class="option">seed</replaceable></arg>
      <arg choice="opt"><option>-h</option></arg>
      <arg choice="opt"><replaceable class="option">cmdfile</replaceable></arg>
      <arg choice="opt"><replaceable class="option">args</replaceable></arg>
    </cmdsynopsis>
  </refsynopsisdiv>

  <refsect1>
    <title>INTRODUCTION</title>

    <para>
      <command>Network Expect</command> (<command>nexp</command>) is a
      framework that allows to easily build tools that can interact
      with network traffic. Following a script, traffic can be
      injected into the network, and decisions can be taken, and acted
      upon, based on received network traffic. An interpreted language
      provides branching and high-level control structures to direct
      the interaction with the network.
    </para>

    <para>
      <command>Network Expect</command> was heavily influenced and
      inspired on the Expect program written by Don Libes, which
      allows to "talk" to interactive programs in a scripted
      fashion. Because of this, you will find a lot of similarities
      between commands in <command>Network Expect</command> and
      commands in Don Libes' Expect. If you are a regular Expect user,
      it should not be very difficult to start writing
      <command>Network Expect</command> scripts because the basics are
      the same.
    </para>

    <para>
      In Don Libes' Expect, scripts can send data to a process just as
      if a user were interactively typing commands. Then, the script
      would read the responses sent by the application and take
      decisions accordingly. In <command>Network Expect</command>'s
      case, a script could send traffic to a network device and then
      take decisions based on the received network traffic. The type
      of things that <command>Network Expect</command> can do are
      usually very low level network operations, which usually require
      writing a custom application in a language like C.
    </para>

    <para>
      <command>Network Expect</command>'s philosophy is based on the
      observation that network applications always operate on an
      action-reaction principle in which something is sent to an
      application running on a remote host and a response is then
      expected.
    </para>

    <para>
      Some of the things that <command>Network Expect</command> can do
      include:
    </para>

    <itemizedlist>

      <listitem>
	<para>
	  Generate arbitrary network traffic and inject it into a
	  network at layer 2 or layer 3.
	</para>

	<para>
	  A wide range of protocols is supported, including IP version
	  6 as well as protocol options like IPv4, IPv6 and TCP
	  options, something that other tools may not offer. For
	  example, <command>Network Expect</command> supports TCP MD5
	  signatures (RFC 2385).
	</para>

	<para>
	  This <command>Network Expect</command> functionality is very
	  similar to the functionality provided by several packet
	  crafting and forging open source tools like Nemesis, Packit,
	  hping, Scapy, and others.
	</para>

      </listitem>

      <listitem>
	<para>
	  Listen for network traffic and take decisions based on the
	  type of traffic received.
	</para>
      </listitem>

      <listitem>
	<para>
	  Open a sniffer trace in PCAP format and replay it after
	  changing some values in the original packet capture.
	</para>
      </listitem>

      <listitem>
	<para>
	  Emulate network protocols to see how they interact with
	  other speakers of that protocol. For example, emulating a
	  TCP server to investigate approaches to randomization of TCP
	  Initial Sequence Numbers (ISN) can be easily done in
	  <command>Network Expect</command>.
	</para>
      </listitem>

    </itemizedlist>

  </refsect1>

  <refsect1>
    <title>USAGE</title>

    <para>
      <command>Network Expect</command> reads cmdfile for a list of
      commands to execute. <command>Network Expect</command> may also
      be invoked implicitly on systems which support the #!  notation
      by marking the script executable, and making the first line in
      your script:
    </para>

    <programlisting>
#!/usr/bin/nexp -f</programlisting>

    <para>
      Of course, the path must accurately describe where
      <command>Network Expect</command> lives; /usr/bin is just an
      example.
    </para>

    <para>
      The <option>-c</option> flag prefaces a command to be executed
      before any in the script.  The command should be quoted to
      prevent being broken up by the shell.  This option may be used
      multiple times.  Multiple commands may be executed with a single
      <option>-c</option> by separating them with semicolons.
      Commands are executed in the order they appear.
    </para>

    <para>
      <option>-V</option> causes <command>Network Expect</command> to
       print its version number and exit.
    </para>

    <para>
      The <option>-s</option> flag allows to specify a random seed
      that will cause predicatibility of pseudo-random numbers
      generated by <command>Network Expect</command> during execution
      of a script. In cases where <command>Network Expect</command> is
      used as a protocol fuzzer, this option is useful to be able to
      re-generate a specific test case.
    </para>

    <para>
      <option>-v</option> increases the verbosity level. Some commands
       display additional information when the verbosity level is
       higher.
    </para>

    <para>
      The <option>-t</option> flag changes the display format used by
      commands that display dates or generate strings that represent
      dates.
    </para>

    <para>
      Optional arguments are constructed into a list and stored in the
      variable named argv.  argc is initialized to the length of argv.
    </para>

    <para>
      argv0 is defined to be the name of the script (or binary if no
      script is used).  For example, the following prints out the name
      of the script and the first three arguments:
    </para>

    <programlisting>
puts "$argv0 [lrange $argv 0 2]"</programlisting>

  </refsect1>

  <refsect1>
    <title>NETWORK LISTENERS AND SPEAKERS</title>

    <para>
      An integral part of <command>Network Expect</command> is the
      concept of network <emphasis>listeners</emphasis> and network
      <emphasis>speakers</emphasis>, which are the <command>Network
      Expect</command> equivalent to spawned processes in Don Libes'
      <command>Expect</command> world. In <command>Expect</command>,
      the <command>send</command> command sends data to a spawned
      process, and the <command>expect</command> command waits for a
      specific pattern in the data received from a spawned process.
    </para>

    <para>
      In <command>Network Expect</command>, the command to send data
      to the network, called <command>send_network</command>, uses a
      <emphasis>speaker</emphasis> that specifies how the traffic will
      be injected. <command>Network Expect</command> speakers can
      specify IPv4 or IPv6 sockets, in which case packets will be
      injected at layer 3 and will be routed by the operating system
      kernel. <command>Network Expect</command> speakers can also
      specify a physical interface, in which case the packet will be
      injected at layer 2. A network speaker can also be a PCAP file
      (also known as a "savefile"), in which case packets will be written
      to this file instead of injected to the network. Other network
      speaker types include TCP and UDP sockets, in which case the payload
      generated by the <command>send_network</command> is transported
      by TCP segments or UDP datagrams built by the operating system
      kernel.
    </para>

    <para>
      <command>Network Expect</command> listeners, on the other hand,
      specify where packets will be read from. Listeners can be
      associated with either a physical interface, or with a PCAP
      file. In either case an optional PCAP filter and/or Wireshark
      display filter can be associated with the listener to limit the
      type of packets the listener will return. When reading from a PCAP
      file the inter-packet delay between packets can be kept, or packets
      can be read at full speed.
    </para>

    <para>
      Both listeners and speakers are created with the
      <command>Network Expect</command> command
      <command>spawn_network</command>, just as in Don Libes'
      <command>Expect</command> spawned processes are created with
      the <command>spawn</command> command.
    </para>

    <para>
      The way to specify network listeners and speakers in commands
      that require them is the same regardless of the command. Network
      listeners are always specified using the <emphasis
      remap="I">-i</emphasis> switch (think
      <emphasis>i</emphasis>nput) followed by the name of the
      listener, and network speakers are specified using the <emphasis
      rempa="I">-o</emphasis> switch (think
      <emphasis>o</emphasis>utput) followed by the name of the
      speaker.
    </para>

    <para>
      Network isteners and speakers are created using the
      <command>spawn_network</command> command. Just as in Don Libes'
      <command>Expect</command> one cannot choose the spawn ID
      returned by the <command>spawn</command> command, in
      <command>Network Expect</command> it is not possible to choose
      the name of a network listener or speaker. However, one can
      assign the network listener or speaker name to a variable and
      use that variable whenever a network listener or speaker needs
      to be specified.
    </para>

    <para>
      What follows are a few examples of creation of network listeners
      and speakers:
    </para>

    <programlisting>
spawn_network -i eth1 icmp and src host 172.16.1.1</programlisting>

    <para>
      This command creates a network listener on interface eth1 and
      assigns the filter "icmp and src host 172.16.1.1", i.e. "listen
      only for ICMP messages coming from host 172.16.1.1". Since the
      <emphasis remap="I">-o</emphasis> switch has not been specified
      this command will not create a network speaker.
    </para>

    <programlisting>
spawn_network -o eth0 -r /tmp/packets.pcap tcp and host 172.16.1.1</programlisting>

    <para>
      This command creates a network speaker on interface eth0 and a
      network listener that will read from the PCAP file
      "/tmp/packets.pcap" TCP segments to or from the host 172.16.1.1.
    </para>

    <programlisting>
spawn_network -nolistener -6</programlisting>

    <para>
      This will create a network speaker for injecting IPv6 packets at
      layer 3. Since the <emphasis remap="I">-nolistener</emphasis>
      switch has been specified, this command only creates a network
      speaker and no listener.
    </para>

    <programlisting>
spawn_network -nolistener -w /tmp/mypackets.pcap</programlisting>

    <para>
      This only creates a speaker that will write packets to the PCAP
      file "/tmp/mypackets.pcap". Note that when writing packets to a
      PCAP file, injection implicitley takes place at layer 2, and
      using an Ethernet header. This must be taken into consideration
      when specifying the packet to send using the
      <command>send_network</command> command.
    </para>

    <para>
      <command>Network Expect</command> listeners and speakers play a
      very important role in the operation of <command>Network
      Expect</command> so becoming confortable with them is key to
      understanding <command>Network Expect</command>'s philosophy.
    </para>

  </refsect1>

  <refsect1>
    <title>NUMERIC SPECIFICATIONS</title>

    <para>When defining packets, <command>Network Expect</command>
      allows to specify values for most fields in protocol headers
      using a syntax that gives great flexibility. This syntax allows
      to make the value of a field <emphasis>change</emphasis> with
      each packet that is generated. The syntax is better presented with
      an example. Suppose that we have an hypothetical protocol field
      called <emphasis>port</emphasis> that is used to specify a numeric
      value at packet generation time. The following numeric specifications
      can be used to specify the actual value
    </para>

    <itemizedlist>

      <listitem>
	<para>
	  <emphasis>port = 'telnet'</emphasis> (fixed): the generated value
	  will always be 23, telnet's port number.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23</emphasis> (fixed): the generated value will
	  always be 23.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23++</emphasis> (increment): the generated value
	  will be 23 initially, and will be incremented by one with
	  each successive packet.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23--</emphasis> (increment): the generated value will
	  be 23 initially, and will be decremented by one with each
	  successive packet.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23+=5</emphasis> (decrement): the generated
	  value will be 23 initially, and will be incremented by 5
	  with each successive packet.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23-=5</emphasis> (decrement): the generated
	  value will be 23 initially, and will be decremented by 5
	  with each successive packet.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 23..25</emphasis> (range): the generated value
	  will start with 23, will be incremented by one until it
	  reaches 25, and then will go back to 23.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 25..23</emphasis> (range): the generated value
	  will start with 25, will be decremented by one until it
	  reaches 23, and then will go back to 25.
	</para>
      </listitem>

      <listitem>
	<para>
	  <emphasis>port = 'random'</emphasis>: the generated value will be
	  a random number in each successive packet.
	</para>
      </listitem>

    </itemizedlist>

  </refsect1>

  <refsect1>
    <title>COMMANDS</title>

    <para>
      <command>Network Expect</command> uses Tcl (Tool Command
      Language).  Tcl provides control flow (e.g., if, for, break),
      expression evaluation and several other features such as
      recursion, procedure definition, etc.  Commands used here but
      not defined (e.g., set, if, exec) are Tcl commands (see
      tcl<manvolnum>3</manvolnum>). <command>Network Expect</command>
      introduces additional commands, described below.  Unless
      otherwise specified, commands return the empty string.
    </para>

    <para>
      Commands are listed alphabetically so that they can be quickly
      located.  However, new users may find it easier to start by
      reading the descriptions of <command>spawn_network</command>,
      <command>send_network</command>,
      <command>expect_network</command>, and
      <command>send_expect</command>, in that order.
    </para>

    <variablelist>

       <!--
       *****************************************************************
       *****************************************************************
       **                            barray                           **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>barray</command> <emphasis>new | length | delete |
	  examine | dump | slice | cmp | string
	  &lt;args&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    The <command>barray</command> command is used to perform
	    management of variables of type <varname>barray</varname>
	    (byte array.)
	  </para>

	  <para>
	    <command>barray new &lt;payload spec&gt;</command> will
	    create and return a new <varname>barray</varname>
	    variable.
	  </para>

	  <para>
	    <command>barray length &lt;barray variable&gt;</command>
	    will return the number of bytes that the barray variable
	    uses.
	  </para>

	  <para>
	    <command>barray delete &lt;barray variable&gt;</command>
	    will delete a <varname>barray</varname> variable.
	  </para>

	  <para>
	    <command>barray examine &lt;barray variable&gt;
	    [/&lt;FMT&gt;] [&lt;offset&gt;]</command> allows to
	    inspect the contents of the specified
	    <varname>barray</varname> variable, starting at the
	    optional <option>offset</option>, and using the format
	    optionally specified with
	    <option>/FMT</option>. <option>/FMT</option> can include
	    an optional count followed by the display format, '&lt;'
	    or '>' to specify little endian or big endian, and the
	    size of each element to display. Display formats are
	    strings ('s'), octal ('o'), hexadecimal ('x'), signed
	    decimal ('d'), and unsigned decimal ('u'). Sizes are byte
	    ('b'), half-word ('h'), and word ('w').
	  </para>

	  <para>
	    <command>barray dump &lt;barray variable&gt;</command>
	    produces a hexadecimal dump of the specified
	    <varname>barray</varname> variable.
	  </para>

	  <para>
	    <command>barray slice &lt;barray variable&gt; &lt;slice
	    spec&gt;</command> returns a slice of the specified
	    <varname>barray</varname> variable. <option>slice
	    spec</option> must be in the format &lt;[start]:[end]&gt;
	    where <option>start</option> and <option>end</option> are
	    offset into the <varname>barray</varname> variable. If
	    <option>start</option> is ommited the start offset is 0,
	    and if <option>end</option> is ommited the end offset is
	    the end of the <varname>barray</varname> variable.
	  </para>

	  <para>
	    <command>barray cmp &lt;barray variable 1&gt; &lt;barray
	    variable 2&gt;</command> compares two
	    <varname>barray</varname> variables and returns an integer
	    less than, equal to, or greater than zero if the first
	    <option>n</option> bytes of <varname>barray variable
	    1</varname> are found, respectively, to be less than, to
	    match, or be greater than the first <option>n</option>
	    bytes of <varname>barray variable
	    2</varname>. <option>n</option> is calculated to be the
	    minimum of the lengths of both <varname>barray</varname>
	    variables.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                        close_network                        **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>close_network</command>
	  <emphasis>&lt;listener/speaker name&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    This command closes the network listener and/or speaker
	    referenced by <emphasis>&lt;listener/speaker
	    name&gt;</emphasis>. Closing a network listener is not a
	    very important thing to do since the operation just
	    releases system resources used by the listener. However,
	    closing a network speaker is a very important, especially
	    when the speaker is associated with a PCAP file since
	    closing the speaker closes the associated PCAP file.
	  </para>
	</listitem>

      </varlistentry>
      
       <!--
       *****************************************************************
       *****************************************************************
       **                      expect_network                         **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>expect_network</command> <emphasis>[-timeout
	  &lt;timeout&gt;]</emphasis> <emphasis>[-i &lt;listener
	  ID&gt;] {condition1} {body1}</emphasis> ... <emphasis>[-i
	  &lt;listener ID&gt;] {conditionN} {bodyN}</emphasis>
	</term>

	<listitem>
	  <para>
	    The <command>expect_network</command> command waits for
	    network traffic from one or more network listeners
	    (specified with the <option>-i</option> option) until a
	    condition evaluates to true, until a specified time period
	    has passed, or until an end-of-file is seen (when the
	    listener is associated with a PCAP file.)
	  </para>

	  <para>
	    Conditions from the most recent
	    <command>expect_network_before</command> command are
	    implicitly used before any other conditions. Conditions
	    from the most recent
	    <command>expect_network_after</command> command are
	    implicitly used after any other conditions.
	  </para>

	  <para>
	    Conditions are regular Tcl expressions, and bodies are
	    sets of Tcl commands. If the final body is empty, it may
	    be omitted.
	  </para>

	  <para>
	    If the arguments to the entire
	    <command>expect_network</command> statement require more
	    than one line, all the arguments may be "braced" into one
	    so as to avoid terminating each line with a backslash.  In
	    this one case, the usual Tcl substitutions will occur
	    despite the braces.
	  </para>

	  <para>
	    If a condition is the keyword <varname>eof</varname>, the
	    corresponding body is executed upon end-of-file (this is
	    only meaningful when a listener is reading from a PCAP
	    file, not on a live capture from an interface.)  If a
	    condition is the keyword <varname>timeout</varname>, the
	    corresponding body is executed upon timeout.  If no
	    <varname>timeout</varname> keyword is used, an implicit
	    null action is executed upon timeout.  The default timeout
	    period is 0.5 seconds but may be set, for example to 30,
	    by the command "set timeout 30".  An infinite timeout may
	    be designated by the value -1. <!-- If a condition is the
	    keyword <varname>default</para>, the corresponding body is
	    executed upon either timeout or end-of-file. -->
	  </para>

	  <para>
	    If a condition is true, then the corresponding body is
	    executed.  <command>expect_network</command> returns the
	    result of the body (or the empty string if no condition
	    was true.)  In the event that multiple conditions match,
	    the one appearing first is used to select a body.
	  </para>

	  <para>
	    Each time a new packet arrives, it is decoded and the
	    conditions are evaluated in the order they are listed.
	    When a packet is decoded, the values of the different
	    fields in the packet are made available to Tcl scripts via
	    Tcl variables. Scripts can use these values in an
	    condition for a <command>expect_network</command> command.
	  </para>

	  <para>
	    In the following example, a listener for ARP requests on
	    interface eth0 is created and then an
	    <command>expect_network</command> command is used to wait
	    for actual ARP requests and respond to them with an ARP
	    reply:
	  </para>

	  <programlisting>
# Spawn a listener for ARP requests
spawn_network -i eth0 host 192.168.1.1 and {arp[6:2]} == 1

expect_network {1} {
    # Received an ARP request, send ARP reply
    send_network -o eth0 \
        ether(src = $mymac, dst = $arp(sha) )/ \
        arp-reply(tha = $arp(sha), tip = $arp(sip), \
                  sha = $mymac, sip = 192.168.1.1)
    nexp_continue
}
	  </programlisting>

<!--
	  <para>
	    The spawn id associated with  the  matching  output  (or  eof  or
	    full_buffer) is stored in expect_out(spawn_id).
	  </para>
-->

	  <para>
	    The <option>-timeout</option> flag causes the current
	    <command>expect_network</command> command to use the
	    following value as a timeout instead of using the value of
	    the <varname>timeout</varname> variable.
	  </para>

	  <para>
	    Actions such as <command>break</command> and
	    <command>continue</command> cause control structures
	    (i.e., <command>for</command>, <command>proc</command>) to
	    behave in the usual way.  The command
	    <command>nexp_continue</command> allows
	    <command>expect_network</command> itself to continue
	    executing rather than returning as it normally would.
	  </para>

	  <para>
	    This is useful for avoiding explicit loops or repeated
	    <command>expect_network</command> statements.  The
	    following example is part of a fragment to respond to ICMP
	    echo and ARP requests.  The
	    <command>nexp_continue</command> avoids having to write a
	    second <command>expect_network</command> statement (to
	    look for the requests again.)
	  </para>

	  <programlisting>
# Spawn a listener for ARP requests
spawn_network -i $interface host $myip and {arp[6:2]} == 1
set arpl $listener_id

# Spawn a listener for ICMP messages sent to us
spawn_network -i $interface icmp and dst host $myip
set icmpl $listener_id

expect_network -i $arpl {1} {
    # Received an ARP request, send ARP reply
    send_network -o $interface \
        ether(src = $mymac, dst = $arp(sha) )/ \
        arp-reply(tha = $arp(sha), tip = $arp(sip), \
                  sha = $mymac, sip = $myip)
    nexp_continue
} -i $icmpl {$icmp(type) == 8} {
    # Received ICMP echo request, send echo reply
    send_network -o ip \
        ip(src = $myip, dst = $ip(src) )/ \
        icmp-echoreply(id = $icmp(id), seq = $icmp(seq) )/ \
	raw($raw)
    nexp_continue
}
	  </programlisting>

	</listitem>

      </varlistentry>
      
       <!--
       *****************************************************************
       *****************************************************************
       **                    expect_network_after                     **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>expect_network_after</command>
	  <emphasis>[expect_args]</emphasis>
	</term>

	<listitem>
	  <para>
	    works identically to the
	    <command>expect_network_before</command> except that if
	    conditions from both <command>expect_network</command> and
	    <command>expect_network_after</command> evaluate to true,
	    the <command>expect_network</command> conditions is used.
	    See the <command>expect_network_before</command> command
	    for more information.
	  </para>
	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                 expect_network_background                   **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>expect_network_background</command>
	  <emphasis>[expect_args]</emphasis>
	</term>

	<listitem>
	  <para>
	    takes the same arguments as
	    <command>expect_network</command>, however it returns
	    immediately.  Conditions are evaluated whenever a new
	    packet arrives.  The expression <varname>timeout</varname>
	    and <varname>default</varname> are meaningless to
	    <command>expect_network_background</command> and are
	    silently discarded.  Otherwise, the
	    <command>expect_network_background</command> command uses
	    <command>expect_network_before</command> and
	    <command>expect_network_after</command> conditions just
	    like <command>expect_network</command> does.
	  </para>

	  <para>
	    Please note that if a condition of the
	    <command>expect_network_background</command> command
	    evaluates to true, the command will not continue to listen
	    for traffic unless the body includes a
	    <command>nexp_continue</command> command that forces
	    <command>expect_network_background</command> to continue
	    executing.
	  </para>
	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                   expect_network_before                     **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>expect_network_before</command>
	  <emphasis>[expect_args]</emphasis>
	</term>

	<listitem>
	  <para>
	    takes the same arguments as
	    <command>expect_network</command>, however it returns
	    immediately.  Condition-action pairs from the most recent
	    <command>expect_network_before</command> with the same
	    network listener ID are implicitly added to any following
	    <command>expect_network</command> commands. If a condition
	    evaluates to true, it is treated as if it had been
	    specified in the <command>expect_network</command> command
	    itself, and the associated body is executed in the context
	    of the <command>expect_network</command> command.  If
	    conditions from both
	    <command>expect_network_before</command> and
	    <command>expect_network</command> can evaluate to true,
	    the <command>expect_network_before</command> condition is
	    used.
	  </para>

	  <para>
	    Unless overridden by a <option>-i</option> flag,
	    <command>expect_network_before</command> conditions are
	    evaluated against the network listener ID defined at the
	    time that the <command>expect_network_before</command>
	    command was executed (not when its condition evaluated to
	    true.)
	  </para>

	  <para>
	    The <option>-info</option> flag causes
	    <command>expect_network_before</command> to return the
	    current specifications of what conditions will be
	    evaluated.  By default, it reports on the current network
	    listener.  An optional network listener ID specification
	    may be given for information on that network listener.
	  </para>

	  <para>
	    Instead of a network listener specification, the flag
	    <option>-all</option> will cause <option>-info</option> to
	    report on all network listeners.
	  </para>

	  <para>
	    The output of the <option>-info</option> flag can be
	    reused as the argument to
	    <command>expect_network_before</command>.
	  </para>

	</listitem>

      </varlistentry>

      <!--
       *****************************************************************
       *****************************************************************
       **                          iflist                             **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>iflist</command>
	  <emphasis>[&lt;-refresh&gt;]</emphasis>
	</term>

	<listitem>

	  <para>
	    <command>iflist</command> returns a list that contains the
	    name of all interfaces in the system. This list is built
	    when <command>Network Expect</command> starts. If the
	    optional argument <option>-refresh</option> is specified
	    then the list is refreshed before returning it.
	  </para>

	</listitem>

      </varlistentry>

      <!--
       *****************************************************************
       *****************************************************************
       **                        nexp_continue                        **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>nexp_continue</command>
	</term>

	<listitem>

	  <para>
	    The command <command>nexp_continue</command> allows expect
	    itself to continue executing rather than returning as it
	    normally would.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                        nexp_version                         **
       *****************************************************************
       *****************************************************************
      -->
     <varlistentry>
	<term>
	  <command>nexp_version</command> <emphasis>[[-exit]
	  &lt;version&gt;]</emphasis>
	</term>
    
	<listitem>
	  <para>
             is useful for assuring that the script is compatible with
             the current version of <command>Network Expect</command>.
	  </para>

	  <para>
             With no  arguments,  the  current version of Expect is returned.
             This version may then be encoded in your script.  If you actually
             know  that you are not using features of recent versions, you can
             specify an earlier version.
	  </para>

	  <para>
	    Versions consist of two numbers separated by dots.  First
	    is the major number.  Scripts written for versions of
	    <command>Network Expect</command> with a different major
	    number will almost certainly not work.
	    <command>nexp_version</command> returns an error if the
	    major numbers do not match.
	  </para>

	  <para>
	    Second is the minor number.  Scripts written for a version
	    with a greater minor number than the current version may
	    depend upon some new feature and might not run.
	    <command>nexp_version</command> returns an error if the
	    major numbers match, but the script minor number is
	    greater than that of the running <command>Network
	    Expect</command>.
	  </para>

	  <para>
             With the -exit flag, Expect prints an error and exits if
             the version is out of date.
	  </para>
	</listitem>

      </varlistentry>

      <!--
       *****************************************************************
       *****************************************************************
       **                          outif                              **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>outif</command> <emphasis>&lt;target&gt;</emphasis>
	</term>

	<listitem>

	  <para>
	    The <command>outif</command> command returns the outgoing
	    interface that would be used to reach
	    <option>target</option>. <option>target</option> can be an
	    IP address or host name.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                            packet                           **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>packet</command> <emphasis>decode | hash | data | dump
	  | ts | tdelta &lt;args&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    The <command>packet</command> command allows to manage
	    variables of type <varname>packet</varname>.
	  </para>

	  <para>
	    <command>packet decode &lt;packet variable&gt;</command>
	    will decode the specified  <varname>packet</varname>
	    variable, just as if the packet was received during the
	    execution of a <command>expect_network</command> command.
	  </para>

	  <para>
	    <command>packet hash &lt;packet variable&gt;</command>
	    calculates a hash of the specified
	    <varname>packet</varname> variable.
	  </para>

	  <para>
	    <command>packet data &lt;packet variable&gt;</command>
	    returns a <varname>barray</varname> variable that contains
	    all of the packet's data.
	  </para>

	  <para>
	    <command>packet dump &lt;packet variable&gt;</command>
	    displays an hexadecimal dump (on standard output) of the
	    <varname>packet</varname>'s data.
	  </para>

	  <para>
	    <command>packet ts &lt;packet variable&gt;</command> will
	    return a <varname>timeval</varname> variable that
	    corresponds to the timestamp associated with the specified
	    <varname>packet</varname> variable.
	  </para>

	  <para>
	    <command>packet tdelta &lt;packet variable 1&gt;
	    &lt;packet variable 2&gt;</command> calculates the time
	    delta between the <varname>timestamp</varname> associated
	    with &lt;packet variable 1&gt; and the
	    <varname>timestamp</varname> associated with &lt;packet
	    variable 2&gt;.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                             pdu                             **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>pdu</command> <emphasis>&lt;new | delete | dup |
	  append | count | build | list&gt; &lt;args&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    The <command>pdu</command> command is used to manage
	    variables of type <varname>pdu</varname> (Protocol Data
	    Unit.)
	  </para>

	  <para>
	    <command>pdu new &lt;PDU definition&gt;</command> will
	    create and return a new <varname>pdu</varname>
	    variable. The PDU definition is the same as used in other
	    <command>Network Expect</command> commands like
	    <command>send_network</command> and
	    <command>send_expect</command>.
	  </para>

	  <para>
	    <command>pdu delete &lt;pdu variable&gt;</command> will
	    delete a <varname>pdu</varname> variable.
	  </para>

	  <para>
	    <command>pdu dup &lt;pdu variable&gt;</command> will
	    duplicate a <varname>pdu</varname> variable.
	  </para>

	  <para>
	    <command>pdu append &lt;pdu variable 1&gt; &lt;pdu
	    variable 2&gt;</command> will append the
	    <varname>pdu</varname> variable <varname>pdu variable
	    2</varname> to the <varname>pdu</varname> variable
	    <varname>pdu variable 1</varname>. The result is
	    <varname>pdu variable 1</varname>.
	  </para>

	  <para>
	    <command>pdu count &lt;pdu variable&gt;</command> returns
	    the number of packets that would be generated if that PDU
	    were to be sent using the <command>send_network</command>
	    command, for example. The number of packets depends on the
	    numeric specifications used in the definition of the PDU.
	  </para>

	  <para>
	    <command>pdu build &lt;pdu variable&gt;</command> builds
	    the specified <varname>pdu</varname> variable and returns
	    a variable of type <varname>packet</varname>.
	  </para>

	  <para>
	    <command>pdu list</command> displays the list of PDUs that
	    <command>Network Expect</command> knows about, i.e. the
	    PDUs that can be created via certain <command>Network
	    Expect</command> commands like
	    <command>send_network</command>,
	    <command>send_expect</command>, and <command>pdu
	    new</command>.
	  </para>

	</listitem>

      </varlistentry>


       <!--
       *****************************************************************
       *****************************************************************
       **                           random                            **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>random</command> <emphasis>[x:y | number x:y | ip
	  [a.b.c.d/nn | a.b.c.d:e.f.g.h] | mac]</emphasis>
	</term>

	<listitem>

	  <para>
	    The <command>random</command> command can be used to
	    obtain a variety of random objects. Current supported
	    objects are numbers, IPv4 addresses, and MAC addresses. It
	    is possible to obtain a random number or IP address from
	    within a range. In the case of numbers the range is
	    specified using the notation x:y. In the case of IP
	    addresses the range can be specified using the IP
	    address/netmask bits notation, or using the
	    a.b.c.d:e.f.g.h notation. If no object is specified then a
	    random number is returned.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                        send_expect                          **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>send_expect</command> <emphasis>[-i &lt;listener
	  ID&gt;] [-o &lt;speaker ID&gt;] [-timeout &lt;timeout&gt;]
	  [-n &lt;number of tries&gt;] &lt;PDU definition&gt;</emphasis>
	</term>
    
	<listitem>
	  <para>
	    This command sends a number of packets to a target or list
	    of targets and then waits for responses. After responses
	    are received, the command matches sent packets with
	    received packets. This command was inspired by
	    the <command>sr()</command> family of commands in
	    <command>Scapy</command>, the packet manipulation tool by
	    Philippe Biondi.
	  </para>

	  <para>
	    The <option>-i</option> flag specifies what listener to
	    use to read responses. It is possible to specify multiple
	    listeners by using this option multiple times.
	  </para>

	  <para>
	    The <option>-o</option> flag specifies what speaker to use
	    when injecting the stimulus.
	  </para>

	  <para>
	    <option>-timeout</option> specifies how long to wait,
	    after all packets have been sent, for answers to the
	    injected stimulus. The timeout is specified in seconds and
	    the default is 1 second.
	  </para>

	  <para>
	    <option>-n</option> specifies how many times to retry
	    sending the stimulus if not all sent packets have a
	    corresponding received answer.
	  </para>

	  <para>
	    The definition of the PDU to send is the same as it used
	    in the <command>send_network</command> command.
	  </para>

	  <para>
	    The <command>send_expect</command> command creates three
	    lists: the list of sent packets and the corresponding list
	    of matching answers, and the list of unanswered packets. A
	    script can then use these lists, decode packets, and
	    present some useful information. For example, the
	    following code snippet sends ICMP echo requests to all hosts
	    in a network a displays which hosts replied:
	  </para>

	  <programlisting>
set network 192.168.1.1-192.168.1.254

# Spawn a listener. We don't really have to specify a filter,
# like in "spawn_network {icmp[icmptype] == icmp-echoreply}"
# because the send_expect command will intelligently match
# injected stimulus (ICMP echo requests) with received
# answers (ICMP echo replies). A filter means less work for
# the send_expect command, but other than that it adds nothing.
spawn_network

send_expect -n 2 -4 -D $network -icmp-echo random:random \
    -payload "12345678901234567890" -delay .001

puts "\n[llength $_(received)] hosts sent echo-replies back:\n"

foreach r $_(received) s $_(sent) {
    packet decode r
    puts [format "$pdu(1,tot_len) bytes from $ip(src): ttl=$ip(ttl) time=%.3f ms" [expr [packet tdelta r s]*1000] ]
}
	  </programlisting>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                        send_network                         **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>send_network</command> <emphasis>[options] &lt;PDU
	  definition&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    This command allows the creation custom TCP/IP packets
	    based on packet definitions provided by the user through
	    command-line arguments passed to the program, through a
	    command file specified in the command-line, or through a
	    mix of these two methods. <command>send_network</command>
	    refers to instances of protocols in the TCP/IP protocol
	    suite as <emphasis>protocol data units</emphasis>, or PDUs
	    for short. A protocol data unit always has a header (think
	    of the IP version 4 header, or the TCP header), may or may
	    not have options (think of the TCP options, or IP version
	    6 extension headers), and may or may not have a
	    payload. Due to the extensive use of encapsulation in the
	    TCP/IP protocol suite, the payload of a PDU can be another
	    payload. For example, an Ethernet frame can have as its
	    payload an IP version 4 packet, which in itself is another
	    PDU. Then this IP version 4 packet can have as its payload
	    a TCP segment, which is another PDU, and so on.
	  </para>

	  <para>
	    When defining PDUs that <command>send_network</command>
	    will create you start by creating the definitions for PDUs
	    that are closer to the physical layer, and then move up
	    the protocol stack until you reach, in some cases, and if
	    that is what you want, the application layer. To implement
	    this approach through a command-line interface (CLI) you
	    start by entering the lower-level PDUs closer to the
	    beginning of the command-line, or, if you are reading your
	    packet definitions from a file, by entering definitions
	    for lower-level PDUs at the beginning of the file. After
	    you have defined a certain PDU you cannot define another
	    PDU that is lower than the previous in the protocol
	    stack. For example, you cannot define a TCP segment and
	    then create an Ethernet frame as the segment's payload,
	    just because that does not make sense (defining a TCP
	    segment and then a BGP message does make sense, for
	    example.) For this reason, order does matter in the
	    command-line (or in the file, if defining PDUs in a file)
	    when creating the PDUs.
	  </para>
	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                             sleep                           **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>sleep</command> <emphasis>&lt;seconds&gt;</emphasis>
	</term>

	<listitem>

	  <para>
             causes the script to sleep for the given number of
             seconds. <option>seconds</option> may be a decimal
             number.  Interrupts are processed while <command>Network
             Expect</command> sleeps.
	  </para>

	</listitem>

      </varlistentry>
      
       <!--
       *****************************************************************
       *****************************************************************
       **                        spawn_network                        **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>spawn_network</command>
	  <emphasis>[-nolistener]</emphasis>
	  <emphasis>[-fullspeed]</emphasis>
	  <emphasis>[-info]</emphasis> <emphasis>[-i &lt;input
	  interface&gt;]</emphasis> <emphasis>[-o &lt;output
	  interface&gt;]</emphasis> <emphasis>[-r &lt;input PCAP
	  file&gt;]</emphasis> <emphasis>[-p]</emphasis> <emphasis>[-s
	  &lt;snaplen&gt;]</emphasis> <emphasis>[-w &lt;output PCAP
	  file&gt;]</emphasis> <emphasis>[-hexdump]</emphasis>
	  <emphasis>[-stdout]</emphasis> <emphasis>[-4]</emphasis>
	  <emphasis>[-6]</emphasis> <emphasis>[&lt;PCAP
	  filter&gt;]</emphasis>
	</term>
    
	<listitem>
	  <para>
	    The <command>spawn_network</command> command is used to
	    find information about existing listeners and speakers or
	    to create network speakers and network listeners.
	  </para>

	  <para>
	    To find information about existing network listeners and
	    speakers the <command>spawn_network</command> command
	    needs to be invoked with only the <option>-info</option>
	    option.
	  </para>

	  <para>
	    Listeners are created by using the options
	    <option>-i</option> or
	    <option>-r</option>. <option>-i</option> specifies an
	    interface to listen for traffic on, and
	    <option>-r</option> specifies the use of a PCAP file for
	    reading instead of reading live traffic from an
	    interface. In both cases, an optional <option>PCAP
	    filter</option> can be specified to limit the type of
	    traffic that will be read. If <option>-i</option> is not
	    used then <command>Network Expect</command> will try to
	    find a suitable interface. <option>-fullspeed</option>
	    causes reading from a PCAP file at full speed, without
	    preserving the inter-packet delay present in the
	    savefile. If this option is not specified then the
	    inter-packet delay present in the savefile will not be
	    preserved.
	  </para>

	  <para>
	    Speakers are created using one of <option>-o</option>,
	    <option>-w</option>, <option>hexdump</option>,
	    <option>stdout</option>, <option>-4</option>, or
	    <option>-6</option>. <option>-o</option> specifies the use
	    of an interface for injecting traffic into the
	    network. This implies the use of layer 2 injection in
	    which the Ethernet header is specified by the user.
	    <option>-w</option> specifies that all traffic will be
	    sent to the PCAP file <option>PCAP file</option>. This
	    option requires the use of layer 2 injection, i.e. the
	    Ethernet header must be
	    included. <option>-hexdump</option> specifies that all
	    packets be sent to standard output in hexadecimal format.
	    <option>-stdout</option> causes all packets to be sent to
	    standard output in raw format. The <option>-4</option> and
	    <option>-6</option> options specify the creation of layer
	    3 speakers to inject IPv4 and IPv6 traffic
	    respectively. In this case all routing decisions are
	    performed by the kernel.
	  </para>

	  <para>
	    The <option>-p</option> and <option>-s</option> options
	    are only meaningful for listener
	    creation. <option>-p</option> specifies that the interface
	    be not put in promiscuous mode when creating a listener
	    that will listen on an interface. <option>-s</option>
	    specifies the snapshot length of captured packets. These
	    two options are equivalent to the
	    tcpdump<manvolnum>8</manvolnum> options of the same names.
	  </para>

	  <para>
	    Note that if no options to create a speaker are specified,
	    i.e.  <option>-o</option>, <option>-w</option>,
	    <option>hexdump</option>, <option>stdout</option>,
	    <option>-4</option>, or <option>-6</option>, the default
	    action is to only create a listener. If only a speaker is
	    desired one of the options to create a speaker must be
	    specified and the <option>-nolistner</option> option must
	    be used.
	  </para>

	  <para>
	    As an example, the follow command creates a listener on
	    interface eth0 for ARP requests for the MAC address of
	    host 192.168.1.1:
	  </para>

	  <programlisting>
spawn_network -i eth0 host 192.168.1.1 and {arp[6:2]} == 1
	  </programlisting>

	  <para>
	    Note that curly braces are necessary around "arp[6:2]"
	    because brackets have special meaning in Tcl.
	  </para>

	</listitem>


      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                            system                           **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>system</command> <emphasis>&lt;system command&gt;
	  [&lt;args&gt;]</emphasis>
	</term>

	<listitem>

	  <para>
	    The <command>system</command> command allows to run
	    arbitrary system commands from within a <command>Network
	    Expect</command> script.
	  </para>

	  <para>
	    Please note that this command does no input validation at
	    all, which means that it is very insecure. For example
	    "system {ls;/bin/sh}" will give you a shell. If nexp is
	    run as roon then the shell will be a root shell.
	  </para>

	  <para>
	    The <command>exec</command> command in the standard Tcl
	    distribution is a much better alternative to the
	    <command>Network Expect</command>
	    <command>system</command> command. Please see the Tcl
	    documentation for the <command>exec</command> command for
	    additional information.
	  </para>

	</listitem>

      </varlistentry>

       <!--
       *****************************************************************
       *****************************************************************
       **                            timeval                          **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>timeval</command> <emphasis>new | delta
	  &lt;args&gt;</emphasis>
	</term>

	<listitem>
	  <para>
	    The <command>timeval</command> command allows to manage
	    variables of type <varname>timeval</varname>.
	  </para>

	  <para>
	    <command>timeval new;</command> returns a
	    <varname>timeval</varname> variable that corresponds to
	    the current system time.
	  </para>

	  <para>
	    <command>timeval tdelta &lt;timeval variable 1&gt;
	    &lt;timeval variable 2&gt;</command> calculates the time
	    delta between the <varname>timeval variable 2</varname> and
	    <varname>timeval variable 1</varname>.
	  </para>

	</listitem>

      </varlistentry>


       <!--
       *****************************************************************
       *****************************************************************
       **                           txdelta                           **
       *****************************************************************
       *****************************************************************
      -->
      <varlistentry>
	<term>
	  <command>txdelta</command> <emphasis>&lt;speaker ID&gt;</emphasis>
	</term>

	<listitem>

	  <para>
	    Every time a packet is sent, the time the packet was sent
	    is saved to the network speaker that was used to send
	    that packet. The <command>txdelta</command> (short for
	    "transmission delta") command returns the number of
	    seconds that have elapsed since the last packet that was
	    sent via the specified <option>speaker ID</option>.
	  </para>

	  <para>
	    The following example provides the foundation for a very
	    simple ping program, and shows how the
	    <command>txdelta</command> command can be used:
	  </para>

	  <programlisting>
for {set id 0; set seq 0} {1} {incr seq} {
    send_network -4 -D $target -icmp-echo $id:$seq -payload "12345678901234567890"

    expect_network -timeout 1 {$icmp(type) == 0 &amp;&amp; $icmp(id) == $id} {
        puts [format "$pdu(2,tot_len) bytes from $ip(src): icmp_seq=$seq ttl=$ip(ttl) time=%.3f ms" [expr [txdelta ip]*1000 ] ]
        sleep [expr 1.0 - [txdelta ip] ]
    }
}
	  </programlisting>

	</listitem>

      </varlistentry>
      

    </variablelist>

  </refsect1>

  <refsect1>
    <title>SPECIAL VARIABLES</title>

    <para>
      Special variables.
    </para>

  </refsect1>

<!--

  <refsect1>
    <title>OPTIONS</title>

    <para><command>ttt</command> has a big number of options due to
      the amount of protocols it supports. Furthermore, because of the
      complexity of some protocols, some options take complex
      parameters that allow to describe how to create a specific part
      or field in a protocol's header. In the discussion that follows,
      options have been categorized in the following categories:
      General, Ethernet, ARP, Generic Routing Encapsulation, IPv4,
      IPv6, ICMP, IGMP, TCP, UDP, and BGP. As <command>ttt</command>
      grows more protocols will be added and therefore new categories
      will be added here.
    </para>

    <para>
      Some commands have a short version, which is always one dash
      ("-") followed by a single letter. All commands have a long
      version, which is also prefixed by single. Long options that
      have an argument can be separated by one or more spaces or by an
      equal sign ("="). It is recommended that the equal sign is used
      instead of spaces because when reading commands from a file
      using the <emphasis>-r</emphasis> switch options are required to
      use the equal sign.
    </para>

    <refsect2>
      <title>General Options</title>

      <variablelist>

	<varlistentry>
	  <term>-h|-help</term>
	  <listitem>
	    <para>
	      Print usage summary and exit.
	    </para>
	  </listitem> 
	</varlistentry>

	<varlistentry>
	  <term>-v|-verbose</term>
	  <listitem>
	    <para>
	      Increase verbosity level. This will cause
	      certain operations to provide more feedback to the user.
	    </para>
	  </listitem> 
	</varlistentry>
		
	<varlistentry>
	  <term>-r|cmd-file=cmdfile</term>
	  <listitem>
	    <para>
	      Read commands from file
	      <filename>cmdfile</filename>. This option can appear any
	      number of times. When it is found, processing of the
	      command-line (or current command file) is suspended,
	      processing of the new command file starts, and as soon
	      as the file has been processed, execution of the
	      original command-line resumes where it left off. The
	      <emphasis>-r</emphasis> can be omitted. If it is omitted
	      note that the file will be processed
	      <emphasis>after</emphasis> all command-line options have
	      been processed, not in the order in which the file name
	      appears in the command line.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-list-pdu=PDUID|all</term>
	  <listitem>
	    <para>
	      List information about the PDU with ID
	      <emphasis>PDUID</emphasis> or about all PDUs if the
	      <emphasis>all</emphasis> keyword is used instead of a
	      specific PDU ID.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-c|-count=NUM</term>
	  <listitem>
	    <para>
	      This specifies the number of packets to send. Please
	      note that if you do not use a numeric specification that
	      changes a field with every packet (like an increment),
	      all packets that will be sent will be identical.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-d|-delay=NUM</term>
	  <listitem>
	    <para>
	      Sets the delay between packets transmissions to
	      <emphasis>NUM</emphasis> microseconds. The minimum delay
	      that is allowed is 1 microsecond and the default value
	      is 100,000 microseconds (0.1 seconds or 10 packets per
	      second.)
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-rate=NUM</term>
	  <listitem>
	    <para>
	      This is another way of specifying the transmission delay
	      between packets. <emphasis>NUM</emphasis> is interpreted
	      as the number of packets per second to send. Internally,
	      this number is converted to an equivalent delay in
	      microseconds.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-flood=NUM</term>
	  <listitem>
	    <para>
	      Number of packets of the <emphasis>same</emphasis> type
	      that will be injected to the network. The difference
	      between -count and -flood is that in the former case a
	      new packet is <emphasis>built</emphasis> between sends,
	      and if a numeric specification is used, every packet
	      sent will be different. In the latter case, the same
	      packet is sent <emphasis>NUM</emphasis> times. Since no
	      packet is built, nothing changes, and packet injection
	      happens as fast as the kernel and the machine allow.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-n|-dry-run</term>
	  <listitem>
	    <para>
	      Do not inject any packets into the wire. Instead, print
	      out the structure of the protocol data units that has
	      been specified through command-line arguments or through
	      parameters in a command file. When the
	      <emphasis>-v</emphasis> is used in conjunction with this
	      switch, a lot of information about the packet that would
	      be sent is printed out. This is a great debugging tool
	      to make sure the packet you have created looks like what
	      you want.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-snaplen NUM</term>
	  <listitem>
	    <para>
	      Specify the number of bytes of data from each packet
	      that are saved to the PCAP file. This is identical to
	      <command>tcpdump</command>'s <emphasis>-s</emphasis>
	      parameter. Only meaningful if using
	      <emphasis>-pcapdump</emphasis>.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-nocksum</term>
	  <listitem>
	    <para>
	      For those protocols that have a checksum field in the
	      header, this specifies that no checksum must be
	      calculated.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-P|-payload INFILE</term>
	  <listitem>
	    <para>
	      Specifies that a PDU payload must be read from the file
	      <emphasis>INFILE</emphasis>. This option must be towards
	      the end of the command-line, after all PDUs have been
	      defined and no more PDUs are desired. This option
	      creates what is called a "raw" PDU. A raw PDU cannot be
	      followed by another PDU, must always be the last. If
	      <emphasis>INFILE</emphasis> is a dash ('-') then the
	      payload is read from standard input.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>Ethernet Options</title>

      <variablelist>

	<varlistentry>
	  <term>-ether</term>
	  <listitem>
	    <para>
	      Creates an Ethernet frame. Ethernet source and
	      destination MAC addresses must be specified with the
	      <emphasis>-ether-src</emphasis> and
	      <emphasis>-ether-dst</emphasis> options.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ether-src MACADDR</term>
	  <listitem>
	    <para>
	      Sets the Ethernet source MAC address to
	      <emphasis>MACADDR</emphasis>.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ether-dst MACADDR</term>
	  <listitem>
	    <para>
	      Sets the Ethernet destination MAC address to
	      <emphasis>MACADDR</emphasis>.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ether-proto NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the type of payload that the Ethernet frame is
	      carrying. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above. You should only use
	      this option if you are not specifying a higher layer
	      protocol like ARP, IPv4 or IPv6, and want to inject a
	      raw Ethernet payload.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>ARP Options</title>

      <variablelist>

	<varlistentry>
	  <term>-arp-request</term>
	  <listitem>
	    <para>
	      Creates an ARP request packet.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-arp-reply</term>
	  <listitem>
	    <para>
	      Creates an ARP reply packet.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-arp-sha MACADDR</term>
	  <listitem>
	    <para>
	      Specifies the sender's hardware address for the selected
	      ARP operation.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-arp-sip ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the sender's IP address for the selected
	      ARP operation.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-arp-tha MACADDR</term>
	  <listitem>
	    <para>
	      Specifies the target's hardware address for the selected
	      ARP operation.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-arp-tip ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the target's IP address for the selected ARP
	      operation.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>Generic Routing Encapsulation (GRE) Options</title>

      <variablelist>

	<varlistentry>
	  <term>-gre</term>
	  <listitem>
	    <para>
	      Creates a GRE encapsulation header.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-gre-protocol NUM</term>
	  <listitem>
	    <para>
	      Specifies the GRE protocol being encapsulated.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-gre-cksum</term>
	  <listitem>
	    <para>
	      Enables addition of a checksum to the GRE header.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>IPv4 Options</title>

      <variablelist>

	<varlistentry>
	  <term>-4|-ipv4</term>
	  <listitem>
	    <para>
	      Create IP version 4 packets.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-S|-src ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the source IP address.  IP addresses can be
	      specified in numeric format (separated by dots) or by
	      fully-qualified domain name (FQDN), in which case the
	      name is resolved to its IP address.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-D|-dst ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the destination IP address.  IP addresses can
	      be specified in numeric format (separated by dots) or by
	      fully-qualified domain name (FQDN), in which case the
	      name is resolved to its IP address.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-I|-id NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the ID field in the IPv4
	      header. The default is 64 if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-T|-ttl NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Time-to-live field in the
	      IPv4 header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-t|-tos NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Type Of Service field in the
	      IPv4 header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-p|-protocol NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Protocol field in the IPv4
	      header. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.  You should only use
	      this option if you are not specifying a higher layer
	      protocol like UDP, TCP or ICMP, and want to inject a raw
	      IPv4 payload.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ipopt-eol</term>
	  <listitem>
	    <para>
	      Specifies that an IP end-of-option-list option be added
	      to the IPv4 header. Note that when specifying IPv4
	      options it is up to the user to make sure that the
	      correct padding is specified so the entire option
	      section is correctly
	      aligned. <emphasis>-ipopt-nop</emphasis> should be used
	      to pad between options and
	      <emphasis>-ipopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 791 to know about
	      padding requirements. You <emphasis>need</emphasis> to
	      make sure that the IP header has enough space for this
	      option. Remember that the IP header only has space for
	      40 bytes of options.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ipopt-nop</term>
	  <listitem>
	    <para>
	      Specifies that an IP no-operation option be added to the
	      IPv4 header. Note that when specifying IPv4 options it
	      is up to the user to make sure that the correct padding
	      is specified so the entire option section is correctly
	      aligned. <emphasis>-ipopt-nop</emphasis> should be used
	      to pad between options and
	      <emphasis>-ipopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 791 to know about
	      padding requirements. You <emphasis>need</emphasis> to
	      make sure that the IP header has enough space for this
	      option. Remember that the IP header only has space for
	      40 bytes of options.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ipopt-rr ROUTESPEC</term>
	  <listitem>
	    <para>
	      Specifies that an IP record-route option be added to the
	      IPv4 header. <emphasis>ROUTESPEC</emphasis> is a route
	      specification, and it is also used for the
	      <emphasis>-ipopt-lsrr</emphasis> and
	      <emphasis>-ipopt-ssrr</emphasis> options. It has the
	      following syntax:
	    </para>

	    <para>
	      length,pointer[,IP address 1,IP address 2, ... ,IP
	      address n]
	    </para>

	    <para><emphasis>length</emphasis> is the total space that
	      should be reserved for this option. It is not a numeric
	      specification, just a
	      number. <emphasis>pointer</emphasis>, another number and
	      <emphasis>not</emphasis> a numeric specification,
	      represents the value of the PTR field in the
	      option. After specifying <emphasis>length</emphasis> and
	      <emphasis>pointer</emphasis> a list of IP addresses can
	      optionally follow. IP addresses can be specified in
	      numeric format (separated by dots) or by fully-qualified
	      domain name (FQDN), in which case the name is resolved
	      to its IP address. Note that fields are separated by
	      commas and that no space between commas is allowed. You
	      <emphasis>need</emphasis> to make sure that the IP
	      header has enough space for this option. Remember that
	      the IP header only has space for 40 bytes of options.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ipopt-lsrr ROUTESPEC</term>
	  <listitem>
	    <para>
	      Specifies that an IP loose-source, record-route option
	      be added to the IPv4
	      header. <emphasis>ROUTESPEC</emphasis> is a route
	      specification, and its format is explained above in the
	      description of the <emphasis>-ipopt-rr</emphasis>
	      option. You <emphasis>need</emphasis> to make sure that
	      the IP header has enough space for this option. Remember
	      that the IP header only has space for 40 bytes of
	      options.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ipopt-ssrr ROUTESPEC</term>
	  <listitem>
	    <para>
	      Specifies that an IP strict-source, record-route option
	      be added to the IPv4
	      header. <emphasis>ROUTESPEC</emphasis> is a route
	      specification, and its format is explained above in the
	      description of the <emphasis>-ipopt-rr</emphasis>
	      option. You <emphasis>need</emphasis> to make sure that
	      the IP header has enough space for this option. Remember
	      that the IP header only has space for 40 bytes of
	      options.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>IPv6 Options</title>

      <variablelist>

	<varlistentry>
	  <term>-6|-ipv6</term>
	  <listitem>
	    <para>
	      Create IP version 6 packets.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-S|-src ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the source IP address.  IP addresses can be
	      specified in numeric format (separated by dots) or by
	      fully-qualified domain name (FQDN), in which case the
	      name is resolved to its IP address.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-D|-dst ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the destination IP address.  IP addresses can
	      be specified in numeric format (separated by dots) or by
	      fully-qualified domain name (FQDN), in which case the
	      name is resolved to its IP address.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-next-header NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Next Header field in the IPv6
	      header (<emphasis>or</emphasis> in the Next Header field
	      of the currently last <emphasis>extension
	      header</emphasis>, if the packet has extension headers.)
	      <emphasis>NUMSPEC</emphasis> is a numeric specification
	      as explained above.  You should only use this option if
	      you are not specifying a higher layer protocol like UDP,
	      TCP or ICMP, and want to inject a raw IPv6 payload.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-no-next-header</term>
	  <listitem>
	    <para>
	      Sets the value of the Next Header field in the IPv6
	      header to 59 (<emphasis>or</emphasis> in the Next Header
	      field of the currently last <emphasis>extension
	      header</emphasis>, if the packet has extension
	      headers.). RFC 2460 states that a Next Header field with
	      a value of 59 indicates that there is nothing following
	      the header (or extension header.)
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-T|-hlim NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Hop Limit field in the IPv6
	      header. The default is 64 if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ip6hbh TLVSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a Hop-by-Hop Options extension header be
	      added to the IPv6 header.  <emphasis>TLVSPEC</emphasis>
	      is a specification of type-length-value options (RFC
	      2640 presents the format of type-length-value options,
	      you must familiarize your self with them if you intend
	      to use this extension header.) The
	      <emphasis>TLVSPEC</emphasis> specification is also used
	      by the Destination Options extension header
	      (-ip6destopts).  The syntax of the
	      <emphasis>TLVSPEC</emphasis> specification is as
	      follows:
	    </para>

	    <para>
	      padn:n|pad1|type:(random:n|filename), ...
	    </para>

	    <para><emphasis>padn</emphasis> specifies that a PADN
	      option, with a length of <emphasis>n</emphasis> octets,
	      be inserted in the options
	      area. <emphasis>pad1</emphasis> inserts a PAD1
	      option. <emphasis>type</emphasis> is a number that
	      represents the option type. After specifying the option
	      type, the total length <emphasis>n</emphasis> must
	      follow, and then either <emphasis>random</emphasis>, if
	      the option data is to be random, or the name of the file
	      if the option data is to be read from a file. Note that
	      fields are separated by commas and that no space between
	      commas is allowed. You can specify any combination you
	      want, e.g. any combination of padn, pad1, or raw option
	      type. However, you need to make sure that the total
	      length of the option is a multiple of 8 octets, just as
	      the RFC says. The tool will allow you to shoot yourself
	      in the foot, so remember that with great power comes
	      great responsibilities; you need to count and insert
	      enough padding!
	    </para>

	    <para>
	      An example of a TLVSPEC would be:
	    </para>

	    <para>
	      -ip6hbh 3:random:5,4:random:padn:9
	    </para>

	    <para>
	      In this example, the resulting extension header will
	      have: one option of type 3, with a total length of 5
	      bytes (which include the option type and option length
	      octets, which leaves space for 3 bytes of data) and
	      random data, then followed by 9 bytes of padding. The
	      total space used by this extension header will be: 1
	      octet for the Next Header field, plus one octet for the
	      length of the entire extension header (in multiple of 8,
	      without including the first 8 octets), plus 5 octets for
	      the type 3 option, plus 9 octets for the type 4 option,
	      for a total of 16 octets (therefore, the length field of
	      the extension header will be 1.)
	    </para>

	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ip6destopts TLVSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a Destination Options extension header be
	      added to the IPv6 header. <emphasis>TLVSPEC</emphasis>
	      is the same type-length-value options specification
	      discussed above in the <emphasis>-ip6hbh</emphasis>
	      command-line option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ip6frag FRAGSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a Fragment extension header be added to
	      the IPv6 header.  <emphasis>FRAGSPEC</emphasis> is a
	      fragment specification.  The syntax of the
	      <emphasis>FRAGSPEC</emphasis> specification is as
	      follows:
	    </para>

	    <para>
	      offset,more|last,ID
	    </para>

	    <para><emphasis>offset</emphasis> is a number that
	      specifies the fragment offset. This corresponds to the
	      Fragment Offset field in the fragment extension header
	      (RFC 2640 is your friend once again.) Following the
	      offset, the word "more" or "last" must be used. "last"
	      will reset the <emphasis>M</emphasis> (More Fragments)
	      flag in the fragment extension header, indicating that
	      this is the last fragment of a packet. "more" will set
	      this flag indicating that there are more fragments on
	      the way. Finally, <emphasis>ID</emphasis> is the 32-bit
	      identification field in the fragment extension
	      header. This number <emphasis>is</emphasis> a numeric
	      specification, as explained above.  An example of a
	      FRAGSPEC would be:
	    </para>

	    <para>
	      -ip6frag 100,more,10+100
	    </para>

	    <para>
	      In this example, the resulting extension header will
	      specify a packet with an <emphasis>offset</emphasis>
	      equal to 100, with the <emphasis>M</emphasis> flag set,
	      and with an <emphasis>identification</emphasis> field
	      that starts at 10 and increments by 100 in every packet.
	    </para>

	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-ip6rthdr RTHDRSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a routing extension header be added to
	      the IPv6 header.  <emphasis>RTHDRSPEC</emphasis> is a
	      routing header specification.  The syntax of the
	      <emphasis>RTHDRSPEC</emphasis> specification is as
	      follows:
	    </para>

	    <para>
	      type,segsleft[,IP address 1,IP address 2, ... ,IP address n]
	    </para>

	    <para><emphasis>type</emphasis> is a <emphasis>numeric
	      specification</emphasis> that specifies the routing type
	      (see RFC 2640 section
	      4.4). <emphasis>segsleft</emphasis> is another
	      <emphasis>numeric specification</emphasis> that
	      represents the number of route segments remaining. Only
	      these first two parameters are mandatory, and they can
	      optionally be followed by a list of IP addresses
	      (numeric or FQDN) separated by commas (no spaces between
	      fields, please.)  An example of a RTHDRSPEC would be:
	    </para>

	    <para>
	      -ip6rthdr 0,5,::1,::2,::3
	    </para>

	    <para>
	      In this example, the routing type is 0, there are 5
	      segments left, and three IP addresses (::1, ::2 and ::3)
	      that will be placed in the extension header.
	    </para>

	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>ICMP Options</title>

      <variablelist>

	<varlistentry>
	  <term>-icmp-raw TYPE:CODE</term>
	  <listitem>
	    <para>
	      Selects ICMP as IP's upper layer protocol using
	      <emphasis>TYPE</emphasis> and <emphasis>CODE</emphasis>
	      as the ICMP type and code respectively.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-icmp-echo ID:SEQ</term>
	  <listitem>
	    <para>
	      Specifies an ICMP echo request
	      message. <emphasis>ID</emphasis> is the
	      <emphasis>identifier</emphasis> in the ICMP echo
	      header. <emphasis>SEQ</emphasis> is the sequence
	      number. Both <emphasis>ID</emphasis> and
	      <emphasis>SEQ</emphasis> are numeric specifications as
	      explained above. Note that if you want to add data to
	      the ICMP echo request packet you need to do so using the
	      <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-echoreply ID:SEQ</term>
	  <listitem>
	    <para>
	      Specifies an ICMP echo reply
	      message. <emphasis>ID</emphasis> is the
	      <emphasis>identifier</emphasis> in the ICMP echo
	      header. <emphasis>SEQ</emphasis> is the sequence
	      number. Both <emphasis>ID</emphasis> and
	      <emphasis>SEQ</emphasis> are numeric specifications as
	      explained above. Note that if you want to add data to
	      the ICMP echo reply packet you need to do so using the
	      <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-icmp-unreachable net|host|protocol|port|fragneeded|srfailed</term>
	  <listitem>
	    <para>
	      Specifies an ICMP unreachable message. The ICMP
	      unreachable can be <emphasis>net</emphasis> for an
	      unreachable network, <emphasis>host</emphasis> for an
	      unreachable host, <emphasis>protocol</emphasis> for an
	      unreachable protocol, <emphasis>port</emphasis> for an
	      unreachable port, <emphasis>fragneeded</emphasis> for
	      packets that need fragmentation but have the Don't
	      Fragment (DF) bit set, <emphasis>srfailed</emphasis> for
	      packet that can't be delivered due to a source route
	      failure. The data section of the ICMP message (IP header
	      + 64 bits of original IP payload) must be specified
	      through the <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-icmp-timeexceed intransit|fragment</term>
	  <listitem>
	    <para>
	      Specifies an ICMP time exceeded message. Use
	      <emphasis>intransit</emphasis> to set the code for time
	      to live exceeded in in transit, and
	      <emphasis>fragment</emphasis> to set the code for
	      fragment re-assembly time exceeded.  The data section of
	      the ICMP message (IP header + 64 bits of original IP
	      payload) must be specified through the
	      <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-icmp-parmproblem NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies an ICMP parameter problem
	      message. <emphasis>NUMSPEC</emphasis> is the pointer
	      parameter in the ICMP header that specifies the place in
	      the header where the problem is.  The data section of
	      the ICMP message (IP header + 64 bits of original IP
	      payload) must be specified through the
	      <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-icmp-srcquench</term>
	  <listitem>
	    <para>
	      Specifies an ICMP source quench message.  The data
	      section of the ICMP message (IP header + 64 bits of
	      original IP payload) must be specified through the
	      <emphasis>-P</emphasis> option.
	    </para>
	  </listitem>
	</varlistentry>
	<varlistentry>
	  <term>-icmp-redirect ADDRESS:(net|host|tos_net|tos_host|NUMSPEC)</term>
	  <listitem>
	    <para>
	      Specifies an ICMP redirect
	      message. <emphasis>ADDRESS</emphasis> is the IP address
	      of the new gateway. It can be specified in numeric
	      dotted format or as a FQDN. Following the address of the
	      new gateway the redirection type must be specified. This
	      can be <emphasis>net</emphasis> for redirection of
	      datagrams for the network, <emphasis>host</emphasis> for
	      redirection of datagrams for the host,
	      <emphasis>tos_net</emphasis> for redirection of
	      datagrams for the Type of Service and network,
	      <emphasis>tos_host</emphasis> for redirection of
	      datagrams for the Type of Service and host, or just a
	      numeric specification. The data section of the ICMP
	      message (IP header + 64 bits of original IP payload)
	      must be specified through the <emphasis>-P</emphasis>
	      option.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>IGMP Options</title>

      <variablelist>

	<varlistentry>
	  <term>-igmp</term>
	  <listitem>
	    <para>
	      Selects IGMP as IP's upper layer protocol.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-igmp-type NUMSPEC</term>
	  <listitem>
	    <para>
	      Sets the type of the IGMP message to
	      <emphasis>NUMSPEC</emphasis>, which is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-igmp-resptime NUMSPEC</term>
	  <listitem>
	    <para>
	      Sets the maximum allowed time before sending a
	      responding report to <emphasis>NUMSPEC</emphasis>, which
	      is a numeric specification as explained above. See RFC
	      2236 for details.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-igmp-group ADDRESS</term>
	  <listitem>
	    <para>
	      Sets the multicast group address to be used in
	      Membership Report or Leave Group messages to
	      <emphasis>ADDRESS</emphasis>.  It can be specified in
	      numeric dotted format or as a FQDN.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>TCP Options</title>

      <variablelist>

	<varlistentry>
	  <term>-tcp</term>
	  <listitem>
	    <para>
	      Selects TCP as IP's upper layer protocol.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-x|-sport NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the TCP's source port number.  The default is
	      zero if not specified. <emphasis>NUMSPEC</emphasis> is a
	      numeric specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-y|-dport NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the TCP's destination port number.  The
	      default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-f|-tcpflags FLAGS</term>
	  <listitem>
	    <para>
	      Specifies the TCP flags to set in the TCP
	      segment. <emphasis>FLAGS</emphasis> is a combination of
	      the following letters (one after the other, no spaces,
	      please): <emphasis>S</emphasis> for
	      <emphasis>SYN</emphasis>, <emphasis>A</emphasis> for
	      <emphasis>ACK</emphasis>, <emphasis>R</emphasis> for
	      <emphasis>RST</emphasis>, <emphasis>P</emphasis> for
	      <emphasis>PSH</emphasis>,<emphasis>F</emphasis> for
	      <emphasis>FIN</emphasis>, and <emphasis>U</emphasis> for
	      <emphasis>URG</emphasis>. The long options
	      <emphasis>-syn</emphasis>, <emphasis>-ack</emphasis>,
	      <emphasis>-rst</emphasis>, <emphasis>-psh</emphasis>,
	      <emphasis>-fin</emphasis>, and
	      <emphasis>-urg</emphasis> can also be
	      used. <emphasis>-f</emphasis> (or
	      <emphasis>-tcpflags</emphasis>) can be specified
	      multiple times, or just once with all the wanted flags,
	      like in <emphasis>-f SRF</emphasis>.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-w|-window NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Window field in the TCP
	      header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-s|-sequence NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Sequence Number field in the
	      TCP header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-a|-acknowledgement NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Acknowledgement Number field
	      in the TCP header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-u|-urgent NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies the value of the Urgent Pointer field in the
	      TCP header. The default is zero if not
	      specified. <emphasis>NUMSPEC</emphasis> is a numeric
	      specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-5|-md5 SECRET</term>
	  <listitem>
	    <para>
	      Specifies that a TCP MD5 signature option (RFC 2385) be
	      added to the TCP header. <emphasis>SECRET</emphasis> is
	      the password to use.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-mss NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a Maximum Segment Size option be added to
	      the TCP header. <emphasis>NUMSPEC</emphasis> is a
	      numeric specification as explained above. Note that when
	      specifying TCP options it is up to the user to make sure
	      that the correct padding is specified so the entire
	      option section is correctly
	      aligned. <emphasis>-tcpopt-nop</emphasis> should be
	      used to pad between options and
	      <emphasis>-tcpopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 793 to know about
	      padding and alignment requirements.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-wscale NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies that a Window Scale option be added to
	      the TCP header. <emphasis>NUMSPEC</emphasis> is a
	      numeric specification as explained above. Note that when
	      specifying TCP options it is up to the user to make sure
	      that the correct padding is specified so the entire
	      option section is correctly
	      aligned. <emphasis>-tcpopt-nop</emphasis> should be
	      used to pad between options and
	      <emphasis>-tcpopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 793 to know about
	      padding and alignment requirements.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-tcpopt-eol</term>
	  <listitem>
	    <para>
	      Specifies that an TCP end-of-options-list option be
	      added to the TCP header. Note that when specifying TCP
	      options it is up to the user to make sure that the
	      correct padding is specified so the entire option
	      section is correctly
	      aligned. <emphasis>-tcpopt-nop</emphasis> should be
	      used to pad between options and
	      <emphasis>-tcpopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 793 to know about
	      padding requirements.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-tcpopt-nop</term>
	  <listitem>
	    <para>
	      Specifies that an TCP no-operation option be added to
	      the TCP header. Note that when specifying TCP options it
	      is up to the user to make sure that the correct padding
	      is specified so the entire option section is correctly
	      aligned. <emphasis>-tcpopt-nop</emphasis> should be
	      used to pad between options and
	      <emphasis>-tcpopt-eol</emphasis> should be used to pad
	      at the end of the option list. See RFC 793 to know about
	      padding requirements.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

    <refsect2>
      <title>BGP Options</title>

      <variablelist>

	<varlistentry>
	  <term>-bgp-update</term>
	  <listitem>
	    <para>
	      Selects a BGP UPDATE message as TCP's payload.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-bgp-as NUMSPEC</term>
	  <listitem>
	    <para>
	      Specifies <emphasis>NUMSPEC</emphasis> as the autonomous
	      system number for the BGP UPDATE message. The default is
	      0 if not specified. <emphasis>NUMSPEC</emphasis> is a
	      numeric specification as explained above.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-bgp-nexthop ADDRESS</term>
	  <listitem>
	    <para>
	      Specifies the address of the next-hop for the BGP UPDATE
	      message. <emphasis>ADDRESS</emphasis> can be a numeric
	      IP address or a FQDN.
	    </para>
	  </listitem>
	</varlistentry>

	<varlistentry>
	  <term>-bgp-destnet NETADDR</term>
	  <listitem>
	    <para>
	      Specifies the network address/network prefix of the
	      destination network advertised in the BGP UPDATE
	      message, for example: 172.16.0/16.
	    </para>
	  </listitem>
	</varlistentry>

      </variablelist>

    </refsect2>

  </refsect1>

-->

  <refsect1>
    <title>EXAMPLES</title>

    <para>
      The <filename class="directory">examples</filename> directory in
      the <command>Network Expect</command> distribution contains
      plenty of examples that should provide a very good idea of how
      to use <command>Network Expect</command>. A few selected
      examples have been selected for this manual page.
    </para>

    <orderedlist>

      <listitem>
	<para>
	  The following code snippet performs a TCP three-way
	  handshake. Everything is done by hand, which allows to play
	  with TCP initial sequence numbers (ISNs), window sizes, etc.
	  The code is a bit more complex that necessary because an
	  effort is made to handle error coditions (timeout, remote host
	  not listening on the destination port, strange combination of
	  TCP flags received in response to our initial SYN.)
	</para>

	<programlisting>
# Some useful constants
set SYN 0x02
set RST 0x04
set ACK 0x10

set retries 3
set isn [random]
set myip 192.168.1.2
set target 192.168.1.1
set sport [random 20000:65535]
set dport 21
set window 4096

# Spawn a listener for TCP segments coming from the FTP server to us
spawn_network -i $interface "tcp and src host $target and dst host $myip and src port $dport and dst port $sport"


# Send TCP SYN
send_network ip(src = $myip, dst = $target)/ \
             tcp(src = $sport, dst = $dport, window = $window, \
                 syn, seq = $isn, ack-seq = 0)

# Wait for response from the server
expect_network {$tcp(flags) == ($SYN | $ACK)} {
    # Got a SYN+ACK so we need to send the final segment of the
    # 3-way HS
    send_network ip(dst = $myip, dst = $target)/ \
                 tcp(dst = $tcp(dstport), dst = $tcp(srcport), \
                     window = $window, ack, seq = $tcp(ack), \
                     ack-seq = [expr $tcp(seq) + 1])
} {$tcp(flags) &amp; $RST} {
    puts "Connection refused"
    exit 1
} {1} {
    # Any other weird combination of TCP flags we respond to
    # with a RST
    send_network ip(src = $myip, dst = $target)/ \
                 tcp(src = $tcp(dstport), dst = $tcp(srcport), \
                     rst)
    exit 1
} timeout {
    # Our SYN got lost in transit or it was filtered - perform
    # exponential backoff and retransmit the SYN...
    if {$retries > 0} {
        incr retries -1
        set timeout [expr $timeout*2]
        puts "SYN timeout, increasing timeout to $timeout"
        send_network ip(src = $myip, dst =  $target)/ \
                     tcp(src = $sport, dst = $dport, \
                         window = $window, syn, seq = $isn, \
                         ack-seq = 0)
        nexp_continue
    } else {
        puts "Connection timed out"
        exit 1
    }
}

# TCP connection has been established. Now do something...
	</programlisting>

      </listitem>

      <listitem>
	<para>
	  This illustrates how to create a phantom host on the network
	  that can respond to ICMP echo requests, and therefore, to
	  ARP requests as well.
	</para>

	<programlisting>
set interface eth0
set myip 192.168.1.1
set mymac [random mac]

# Spawn a listener for ARP requests
spawn_network -i $interface host $myip and {arp[6:2]} == 1
set arpl $listener_id

# Spawn a listener for ICMP messages sent to us
spawn_network -i $interface icmp and dst host $myip
set icmpl $listener_id

expect_network_background -i $arpl {1} {
    # Received an ARP request, send ARP reply
    send_network -o $interface \
        ether(src = $mymac, dst = $arp(sha) )/ \
        arp-reply(tha = $arp(sha), tip = $arp(sip), \
                  sha = $mymac, sip = $myip)
    nexp_continue
} -i $icmpl {$icmp(type) == 8} {
    # Received ICMP echo request, send echo reply
    send_network -o ip \
        ip(src = $myip, dst = $ip(src) )/ \
        icmp-echoreply(id =  $icmp(id), seq = $icmp(seq) )/ \
        raw($raw)
    nexp_continue
}
	</programlisting>

      </listitem>

      <listitem>
	<para>
	  This example how to create a very simple traceroute program
	  that uses TCP probes to port 80 of the target host. It uses
	  the <command>send_expect</command> command.
	</para>

	<programlisting>
set target "www.example.com"
set ttlrange "1:30"
set interface [outif $target]

# Spawn a listener. We don't really have to specify a filter because the
# send_expect command will intelligently match injected stimulus with
# received answers.
spawn_network -i $interface

send_expect -tries 2  -delay 0.001 \
    ip(id = random, dst = $target, ttl = $ttlrange)/ \
    tcp(src = random, dst = 80, syn)

foreach r $_(received) s $_(sent) {
    packet decode r
    set source $ip(src)
    set pdu_type $pdu(1,type)

    packet decode s

    puts [format "$ip(ttl) $source %.3f ms $pdu_type" [expr [packet tdelta r s]*1000] ]
}
	</programlisting>

      </listitem>

      <listitem>
	<para>
	  This shows how to perform an ARP scan using regular
	  <command>send_network</command> and
	  <command>expect_network</command> commands:
	</para>

	<programlisting>
set interface eth0
set network "$iface($interface,ip)/$iface($interface,netmask)"

set arprequest [pdu new -o $interface
    ether(dst = BROADCAST)/ \
    arp-request(tha = BROADCAST, tip = $network, \
                sha = $iface($interface,hw_addr), \
                sip = $iface($interface,ip) ) ]

# Spawn a listener for ARP replies
spawn_network -i $interface {arp[6:2]} == 2

for {set i 0} {$i &lt; [pdu count arprequest]} {incr i} {
    # Send ARP request
    send_network -count 1 arprequest

    # Read ARP reply
    expect_network -timeout .05 {1} {
        puts "$arp(sip) is at $arp(sha)"
    }
}
	</programlisting>

      </listitem>

      <listitem>
	<para>
	  This example shows how to do an ARP scan but in a more
	  efficient manner using the <command>send_expect</command>
	  command:
	</para>

	<programlisting>
set interface eth0
set network "$iface($interface,ip)/$iface($interface,netmask)"

# Spawn a listener for ARP replies
spawn_network -i $interface {arp[6:2]} == 2

send_expect -o $interface -delay 0.001 -tries 2 \
    ether(dst = BROADCAST)/ \
    arp-request(tha = BROADCAST, tip = $network, \
	        sha = $iface($interface,hw_addr),
                sip = $iface($interface,ip) )

puts "\nFound [llength $_(received)] hosts alive:\n"

foreach r $_(received) {
    packet decode r
    puts "$arp(sip) is at $arp(sha)"
}
	</programlisting>

      </listitem>

    </orderedlist>

  </refsect1>

  <refsect1>
    <title>BUGS</title>

    <para>
      <command>Network Expect</command> does not run very well under
      the Solaris operating system because in that operating system
      select() does not seem to work well with packet capture file
      descriptors (select() returns when there is no data ready to be
      read.)
    </para>

    <para>
      <command>Network Expect</command> does not work at all in
      Microsoft Windows because select() does not work at all with
      packet capture file descriptors (pcap_get_selectable_fd() does
      not exist under Microsoft Windows.)
    </para>

    <para>
      Error-checking is almost non-existant.
    </para>

    <para>
      These isn't input validation for the numeric specifications.
    </para>

    <para>
      The parser of PDU definitions (and therefore the number of
      tokens that the parser handles) has become a big, wild
      beast. It's very easy to add new tokens and PDU definitions are
      elegant and pretty, but the parser is huge. Guess can't have
      everything.
    </para>

  </refsect1>

  <refsect1>
    <title>VERSION</title>

    <para>This man page is correct for version 1.0 of <command>Network
      Expect</command>.
    </para>
  </refsect1>

  <refsect1>
    <title>SEE ALSO</title>
    
    <para>
      nexp-numspec<manvolnum>1</manvolnum>,
      nexp-payload<manvolnum>1</manvolnum>,
      nexp-ether<manvolnum>5</manvolnum>,
      nexp-gre<manvolnum>5</manvolnum>,
      nexp-ip<manvolnum>5</manvolnum>,
      nexp-mpls<manvolnum>5</manvolnum>,
      expect<manvolnum>1</manvolnum>
    </para>
  </refsect1>

  <refsect1>
    <title>AUTHOR</title>
	
    <para>
      <command>Network Expect</command> was written by Eloy Paris
      &lt;peloy@netexpect.org&gt;. However, <command>Network
      Expect</command> borrows ideas from lots of Open Source tools
      like Nemesis, Packit, hping, Expect, and Scapy. The
      <command>Network Expect</command> author is indebted to the
      authors of these tools for their contribution.
    </para>
	
    <para>
      This man page was written by Eloy Paris although it borrows
      heavily from Expect's manual page.
    </para>
  </refsect1>

</refentry>
