src/internet-node/pcap-trace.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 30 Jul 2007 13:29:36 +0200
changeset 1171 335886fe4ddd
parent 733 fb1882e7d785
child 1229 e4cfe4094c10
permissions -rw-r--r--
InetAddress -> InetSocketAddress
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
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    25
#include "ns3/trace-root.h"
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
#include "ns3/trace-context.h"
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
#include "ns3/callback.h"
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
#include "ns3/pcap-writer.h"
524
082ffdd8fbd7 move code around
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 485
diff changeset
    29
#include "ns3/node-list.h"
729
b5e744285e92 rename i-node to node
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 728
diff changeset
    30
#include "ns3/node.h"
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    31
733
fb1882e7d785 rename ipv4.h to ipv4-l3-protocol.h
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 729
diff changeset
    32
#include "ipv4-l3-protocol.h"
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    34
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    35
namespace ns3 {
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
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    38
PcapTrace::PcapTrace (std::string filename)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    39
  : m_filename (filename)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    40
{}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    41
PcapTrace::~PcapTrace ()
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
  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
    44
       i != m_traces.end (); i++)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    45
    {
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
      delete i->writer;
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
}
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
void 
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    51
PcapTrace::TraceAllIp (void)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
{
457
a468fac09924 add context information to tx and rx trace callbacks
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 456
diff changeset
    53
  TraceRoot::Connect ("/nodes/*/ipv4/(tx|rx)",
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
		      MakeCallback (&PcapTrace::LogIp, this));
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    55
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    56
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
PcapWriter *
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
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
    59
{
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
  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
    61
       i != m_traces.end (); i++)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    62
    {
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    63
      if (i->nodeId == nodeId &&
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    64
	  i->interfaceId == interfaceId)
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
	{
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    66
	  return i->writer;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
	}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    68
    }
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
  PcapTrace::Trace trace;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
  trace.nodeId = nodeId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
  trace.interfaceId = interfaceId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    72
  trace.writer = new PcapWriter ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
  std::ostringstream oss;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    74
  oss << m_filename << "-" << nodeId << "-" << interfaceId;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
  std::string filename = oss.str ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    76
  trace.writer->Open (filename);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    77
  trace.writer->WriteIpHeader ();
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    78
  m_traces.push_back (trace);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    79
  return trace.writer;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    81
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    82
void 
457
a468fac09924 add context information to tx and rx trace callbacks
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 456
diff changeset
    83
PcapTrace::LogIp (TraceContext const &context, Packet const &p, uint32_t interfaceIndex)
456
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
  NodeList::NodeIndex nodeIndex;
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    86
  context.Get (nodeIndex);
728
95c426b1cb60 rename INode to Node
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
    87
  uint32_t nodeId = NodeList::GetNode (nodeIndex)->GetId ();
456
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    88
  PcapWriter *writer = GetStream (nodeId, interfaceIndex);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    89
  writer->WritePacket (p);
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    90
}
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    91
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    92
5361882cc2f3 put together a PcapTrace and test it
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    93
}//namespace ns3