put together a PcapTrace and test it
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 29 Mar 2007 23:24:10 +0200
changeset 456 5361882cc2f3
parent 455 2f16d26e361c
child 457 a468fac09924
put together a PcapTrace and test it
SConstruct
examples/simple-p2p.cc
src/common/pcap-writer.cc
src/common/pcap-writer.h
src/node/pcap-trace.cc
src/node/pcap-trace.h
--- a/SConstruct	Thu Mar 29 22:49:28 2007 +0200
+++ b/SConstruct	Thu Mar 29 23:24:10 2007 +0200
@@ -219,6 +219,7 @@
     'channel.cc',
     'node-list.cc',
     'ascii-trace.cc',
+    'pcap-trace.cc',
     ])
 node.add_headers ([
     'ipv4-header.h',
@@ -266,6 +267,7 @@
     'application-list.h',
     'onoff-application.h',
     'ascii-trace.h',
+    'pcap-trace.h',
     ])
 
 p2p = build.Ns3Module ('p2p', 'src/devices/p2p')
--- a/examples/simple-p2p.cc	Thu Mar 29 22:49:28 2007 +0200
+++ b/examples/simple-p2p.cc	Thu Mar 29 23:24:10 2007 +0200
@@ -49,6 +49,7 @@
 #include "ns3/data-rate.h"
 
 #include "ns3/ascii-trace.h"
+#include "ns3/pcap-trace.h"
 #include "ns3/internet-node.h"
 #include "ns3/p2p-channel.h"
 #include "ns3/p2p-net-device.h"
@@ -154,9 +155,14 @@
 
   // Configure tracing of all enqueue, dequeue, and NetDevice receive events
   // Trace output will be sent to the simple-p2p.tr file
+#if 0
   AsciiTrace trace ("simple-p2p.tr");
   trace.TraceAllQueues ();
   trace.TraceAllNetDeviceRx ();
+#else
+  PcapTrace trace ("simple-p2p.tr");
+  trace.TraceAllIp ();
+#endif
 
   Simulator::StopAt (Seconds(10.0));
 
--- a/src/common/pcap-writer.cc	Thu Mar 29 22:49:28 2007 +0200
+++ b/src/common/pcap-writer.cc	Thu Mar 29 23:24:10 2007 +0200
@@ -48,10 +48,10 @@
 }
 
 void
-PcapWriter::Open (char const *name)
+PcapWriter::Open (std::string const &name)
 {
   m_writer = new SystemFile ();
-  m_writer->Open (name);
+  m_writer->Open (name.c_str ());
 }
 
 void 
--- a/src/common/pcap-writer.h	Thu Mar 29 22:49:28 2007 +0200
+++ b/src/common/pcap-writer.h	Thu Mar 29 23:24:10 2007 +0200
@@ -46,7 +46,7 @@
    * This method creates the file if it does not exist. If it
    * exists, the file is emptied.
    */
-  void Open (char const *name);
+  void Open (std::string const &name);
 
   /**
    * Write a pcap header in the output file which specifies
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/node/pcap-trace.cc	Thu Mar 29 23:24:10 2007 +0200
@@ -0,0 +1,95 @@
+/* -*-	Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INRIA
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ */
+#include "pcap-trace.h"
+
+#include <sstream>
+
+#include "ns3/trace-root.h"
+#include "ns3/trace-context.h"
+#include "ns3/callback.h"
+#include "ns3/pcap-writer.h"
+
+#include "ipv4.h"
+#include "node-list.h"
+#include "node.h"
+
+
+namespace ns3 {
+
+
+PcapTrace::PcapTrace (std::string filename)
+  : m_filename (filename)
+{}
+PcapTrace::~PcapTrace ()
+{
+  for (std::vector<Trace>::iterator i = m_traces.begin ();
+       i != m_traces.end (); i++)
+    {
+      delete i->writer;
+    }
+}
+
+void 
+PcapTrace::TraceAllIp (void)
+{
+  TraceRoot::Connect ("/nodes/*/ipv4/interfaces/*/(tx|rx)",
+		      MakeCallback (&PcapTrace::LogIp, this));
+}
+
+PcapWriter *
+PcapTrace::GetStream (uint32_t nodeId, uint32_t interfaceId)
+{
+  for (std::vector<Trace>::iterator i = m_traces.begin ();
+       i != m_traces.end (); i++)
+    {
+      if (i->nodeId == nodeId &&
+	  i->interfaceId == interfaceId)
+	{
+	  return i->writer;
+	}
+    }
+  PcapTrace::Trace trace;
+  trace.nodeId = nodeId;
+  trace.interfaceId = interfaceId;
+  trace.writer = new PcapWriter ();
+  std::ostringstream oss;
+  oss << m_filename << "-" << nodeId << "-" << interfaceId;
+  std::string filename = oss.str ();
+  trace.writer->Open (filename);
+  trace.writer->WriteIpHeader ();
+  m_traces.push_back (trace);
+  return trace.writer;
+}
+
+void 
+PcapTrace::LogIp (TraceContext const &context, Packet &p)
+{
+  NodeList::NodeIndex nodeIndex;
+  context.Get (nodeIndex);
+  uint32_t nodeId = NodeList::GetNode (nodeIndex)->GetId ();
+  Ipv4::InterfaceIndex interfaceIndex;
+  context.Get (interfaceIndex);
+  PcapWriter *writer = GetStream (nodeId, interfaceIndex);
+  writer->WritePacket (p);
+}
+
+
+}//namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/node/pcap-trace.h	Thu Mar 29 23:24:10 2007 +0200
@@ -0,0 +1,54 @@
+/* -*-	Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INRIA
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ */
+#ifndef PCAP_TRACE_H
+#define PCAP_TRACE_H
+
+#include <string>
+#include <vector>
+
+namespace ns3 {
+
+class Packet;
+class TraceContext;
+class PcapWriter;
+
+class PcapTrace 
+{
+public:
+  PcapTrace (std::string filename);
+  ~PcapTrace ();
+
+  void TraceAllIp (void);
+private:
+  PcapWriter *GetStream (uint32_t nodeId, uint32_t interfaceId);
+  void LogIp (TraceContext const &context, Packet &p);
+  std::string m_filename;
+  struct Trace {
+    uint32_t nodeId;
+    uint32_t interfaceId;
+    PcapWriter *writer;
+  };
+  std::vector<Trace> m_traces;
+};
+
+}//namespace ns3
+
+#endif /* PCAP_TRACE_H */