put together a PcapTrace and test it
This commit is contained in:
1 parent
66cf44ec19
commit
8d1be72fb6
6 files changed
+160
-3
No files matched your search
@@ -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
|
||||
@@ -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 */
|
||||
Reference in new issue
Block a user