diff -r 6e2d19edddae examples/tcp-large-transfer.cc --- a/examples/tcp-large-transfer.cc Mon Mar 31 15:39:50 2008 -0700 +++ b/examples/tcp-large-transfer.cc Tue Apr 01 22:26:12 2008 -0400 @@ -182,9 +182,9 @@ int main (int argc, char *argv[]) std::ofstream ascii; ascii.open ("tcp-large-transfer.tr"); - PointToPointHelper::EnablePcap ("tcp-large-transfer"); PointToPointHelper::EnableAscii (ascii); + internet.EnablePcap ("tcp-large-transfer"); Simulator::StopAt (Seconds(1000)); Simulator::Run (); diff -r 6e2d19edddae src/helper/internet-stack-helper.cc --- a/src/helper/internet-stack-helper.cc Mon Mar 31 15:39:50 2008 -0700 +++ b/src/helper/internet-stack-helper.cc Tue Apr 01 22:26:12 2008 -0400 @@ -20,8 +20,11 @@ #include "internet-stack-helper.h" #include "ns3/internet-stack.h" #include "ns3/packet-socket-factory.h" +#include "ns3/config.h" namespace ns3 { + +std::vector InternetStackHelper::m_traces; void InternetStackHelper::Build (NodeContainer c) @@ -35,5 +38,68 @@ InternetStackHelper::Build (NodeContaine } } +void +InternetStackHelper::EnablePcap (std::string filename) +{ + m_filename = filename; + Config::Connect ("/NodeList/*/$ns3::Ipv4L3Protocol/Tx", + MakeCallback (&InternetStackHelper::LogTxIp, this)); + Config::Connect ("/NodeList/*/$ns3::Ipv4L3Protocol/Rx", + MakeCallback (&InternetStackHelper::LogRxIp, this)); +} + +uint32_t +InternetStackHelper::GetNodeIndex (std::string context) const +{ + std::string::size_type pos; + pos = context.find ("/NodeList/"); + NS_ASSERT (pos == 0); + std::string::size_type afterNodeIndex = context.find ("/", 11); + NS_ASSERT (afterNodeIndex != std::string::npos); + std::string index = context.substr (10, afterNodeIndex - 10); + std::istringstream iss; + iss.str (index); + uint32_t nodeIndex; + iss >> nodeIndex; + return nodeIndex; +} + +void +InternetStackHelper::LogTxIp (std::string context, Ptr packet, uint32_t interfaceIndex) +{ + Ptr writer = GetStream (GetNodeIndex (context), interfaceIndex); + writer->WritePacket (packet); +} + +void +InternetStackHelper::LogRxIp (std::string context, Ptr packet, uint32_t interfaceIndex) +{ + Ptr writer = GetStream (GetNodeIndex (context), interfaceIndex); + writer->WritePacket (packet); +} + +Ptr +InternetStackHelper::GetStream (uint32_t nodeId, uint32_t interfaceId) +{ + for (std::vector::iterator i = m_traces.begin (); + i != m_traces.end (); i++) + { + if (i->nodeId == nodeId && + i->interfaceId == interfaceId) + { + return i->writer; + } + } + InternetStackHelper::Trace trace; + trace.nodeId = nodeId; + trace.interfaceId = interfaceId; + trace.writer = Create (); + std::ostringstream oss; + oss << m_filename << ".pcap-" << nodeId << "-" << interfaceId; + trace.writer->Open (oss.str ()); + trace.writer->WriteIpHeader (); + m_traces.push_back (trace); + return trace.writer; +} } // namespace ns3 diff -r 6e2d19edddae src/helper/internet-stack-helper.h --- a/src/helper/internet-stack-helper.h Mon Mar 31 15:39:50 2008 -0700 +++ b/src/helper/internet-stack-helper.h Tue Apr 01 22:26:12 2008 -0400 @@ -21,6 +21,9 @@ #define INTERNET_STACK_HELPER_H #include "node-container.h" +#include "net-device-container.h" +#include "ns3/pcap-writer.h" +#include "ns3/packet.h" namespace ns3 { @@ -37,6 +40,27 @@ public: * of the ns3::Ipv4, ns3::Udp, and, ns3::Tcp classes. */ void Build (NodeContainer c); + + /** + * \param filename filename prefix to use for pcap files. + * + * Enable pcap output on each device which is of the + * ns3::PointToPointNetDevice type + */ + void EnablePcap (std::string filename); + +private: + void LogRxIp (std::string context, Ptr packet, uint32_t deviceId); + void LogTxIp (std::string context, Ptr packet, uint32_t deviceId); + Ptr GetStream (uint32_t nodeId, uint32_t interfaceId); + struct Trace { + uint32_t nodeId; + uint32_t interfaceId; + Ptr writer; + }; + std::string m_filename; + uint32_t GetNodeIndex (std::string context) const; + static std::vector m_traces; }; } // namespace ns3