src/internet-node/pcap-trace.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 03 Mar 2008 21:00:27 +0100
changeset 2560 627fef42e280
parent 2536 08b25d309a04
child 2594 a8f89acd17a1
permissions -rw-r--r--
avoid optimized build warning.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
/* -*-	Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
/*
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
 * Copyright (c) 2007 INRIA
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     4
 * All rights reserved.
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
 *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
 *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
 * GNU General Public License for more details.
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
 *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
 *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
 */
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
#include "pcap-trace.h"
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
#include <sstream>
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    25
#include "ns3/config.h"
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
#include "ns3/callback.h"
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
#include "ns3/pcap-writer.h"
524
082ffdd8fbd7 move code around
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 485
diff changeset
    28
#include "ns3/node-list.h"
729
b5e744285e92 rename i-node to node
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 728
diff changeset
    29
#include "ns3/node.h"
1866
e7dbcc4df546 do not use Packet objects directly. Use Ptr<Packet> instead
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1384
diff changeset
    30
#include "ns3/packet.h"
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    31
#include "ns3/log.h"
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    32
733
fb1882e7d785 rename ipv4.h to ipv4-l3-protocol.h
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 729
diff changeset
    33
#include "ipv4-l3-protocol.h"
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    35
NS_LOG_COMPONENT_DEFINE ("PcapTrace");
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
namespace ns3 {
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    40
PcapTrace::PcapTrace (std::string filename)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    41
  : m_filename (filename)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    42
{}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    43
PcapTrace::~PcapTrace ()
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    44
{
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    45
  for (std::vector<Trace>::iterator i = m_traces.begin ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
       i != m_traces.end (); i++)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    47
    {
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    48
      delete i->writer;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    49
    }
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
void 
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    53
PcapTrace::TraceAllIp (void)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
{
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    55
  Config::ConnectWithContext ("/NodeList/*/$Ipv4L3Protocol/Tx",
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    56
                              MakeCallback (&PcapTrace::LogTxIp, this));
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    57
  Config::ConnectWithContext ("/NodeList/*/$Ipv4L3Protocol/Rx",
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    58
                              MakeCallback (&PcapTrace::LogRxIp, this));
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    61
PcapWriter *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    62
PcapTrace::GetStream (uint32_t nodeId, uint32_t interfaceId)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
{
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
  for (std::vector<Trace>::iterator i = m_traces.begin ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
       i != m_traces.end (); i++)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    66
    {
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
      if (i->nodeId == nodeId &&
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
	  i->interfaceId == interfaceId)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
	{
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
	  return i->writer;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
	}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
    }
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
  PcapTrace::Trace trace;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
  trace.nodeId = nodeId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
  trace.interfaceId = interfaceId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    76
  trace.writer = new PcapWriter ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    77
  std::ostringstream oss;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
  oss << m_filename << "-" << nodeId << "-" << interfaceId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    79
  std::string filename = oss.str ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
  trace.writer->Open (filename);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    81
  trace.writer->WriteIpHeader ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    82
  m_traces.push_back (trace);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    83
  return trace.writer;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    84
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    85
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    86
uint32_t
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    87
PcapTrace::GetNodeIndex (std::string context) const
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    88
{
2560
627fef42e280 avoid optimized build warning.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2536
diff changeset
    89
  std::string::size_type pos;
627fef42e280 avoid optimized build warning.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2536
diff changeset
    90
  pos = context.find ("/NodeList/");
2536
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    91
  NS_ASSERT (pos == 0);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    92
  std::string::size_type afterNodeIndex = context.find ("/", 11);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    93
  NS_ASSERT (afterNodeIndex != std::string::npos);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    94
  std::string index = context.substr (10, afterNodeIndex - 10);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    95
  NS_LOG_DEBUG ("index="<<index);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    96
  std::istringstream iss;
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    97
  iss.str (index);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    98
  uint32_t nodeIndex;
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
    99
  iss >> nodeIndex;
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   100
  return nodeIndex;
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   101
}  
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   102
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   103
void 
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   104
PcapTrace::LogTxIp (std::string context, Ptr<const Packet> p, uint32_t interfaceIndex)
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   105
{
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   106
  PcapWriter *writer = GetStream (GetNodeIndex (context), interfaceIndex);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   107
  writer->WritePacket (p);
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   108
}
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   109
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   110
void 
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   111
PcapTrace::LogRxIp (std::string context, Ptr<const Packet> p, uint32_t interfaceIndex)
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   112
{
08b25d309a04 port pcap trace to Config.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1866
diff changeset
   113
  PcapWriter *writer = GetStream (GetNodeIndex (context), interfaceIndex);
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   114
  writer->WritePacket (p);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   115
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   116
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   117
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   118
}//namespace ns3