diff -cr ns-3.24.1.ORIG/src/network/utils/pcap-file.h ns-3.24.1.PATCH/src/network/utils/pcap-file.h *** ns-3.24.1.ORIG/src/network/utils/pcap-file.h 2015-09-23 14:28:42.000000000 -0500 --- ns-3.24.1.PATCH/src/network/utils/pcap-file.h 2016-01-26 11:13:11.144990712 -0600 *************** *** 116,122 **** void Init (uint32_t dataLinkType, uint32_t snapLen = SNAPLEN_DEFAULT, int32_t timeZoneCorrection = ZONE_DEFAULT, ! bool swapMode = false); /** * \brief Write next packet to file --- 116,123 ---- void Init (uint32_t dataLinkType, uint32_t snapLen = SNAPLEN_DEFAULT, int32_t timeZoneCorrection = ZONE_DEFAULT, ! bool swapMode = false, ! bool nanosecMode = false); /** * \brief Write next packet to file *************** *** 192,197 **** --- 193,206 ---- bool GetSwapMode (void); /** + * \brief Get the nanosecond mode of the file. + * + * IsNanoSecMode returns true if the packet timestamps in the PCAP + * file have nanosecond resolution. + */ + bool IsNanoSecMode (void); + + /** * \brief Returns the magic number of the pcap file as defined by the magic_number * field in the pcap global header. * *************** *** 357,362 **** --- 366,372 ---- std::fstream m_file; //!< file stream PcapFileHeader m_fileHeader; //!< file header bool m_swapMode; //!< swap mode + bool m_nanosecMode; }; } // namespace ns3 diff -cr ns-3.24.1.ORIG/src/network/utils/pcap-file.cc ns-3.24.1.PATCH/src/network/utils/pcap-file.cc *** ns-3.24.1.ORIG/src/network/utils/pcap-file.cc 2015-09-23 14:28:42.000000000 -0500 --- ns-3.24.1.PATCH/src/network/utils/pcap-file.cc 2016-01-26 11:23:05.480866445 -0600 *************** *** 41,58 **** const uint32_t MAGIC = 0xa1b2c3d4; /**< Magic number identifying standard pcap file format */ const uint32_t SWAPPED_MAGIC = 0xd4c3b2a1; /**< Looks this way if byte swapping is required */ ! const uint32_t NS_MAGIC = 0xa1b23cd4; /**< Magic number identifying nanosec resolution pcap file format */ ! const uint32_t NS_SWAPPED_MAGIC = 0xd43cb2a1; /**< Looks this way if byte swapping is required */ const uint16_t VERSION_MAJOR = 2; /**< Major version of supported pcap file format */ const uint16_t VERSION_MINOR = 4; /**< Minor version of supported pcap file format */ PcapFile::PcapFile () : m_file (), ! m_swapMode (false) { NS_LOG_FUNCTION (this); ! FatalImpl::RegisterStream (&m_file); } PcapFile::~PcapFile () --- 41,59 ---- const uint32_t MAGIC = 0xa1b2c3d4; /**< Magic number identifying standard pcap file format */ const uint32_t SWAPPED_MAGIC = 0xd4c3b2a1; /**< Looks this way if byte swapping is required */ ! const uint32_t NS_MAGIC = 0xa1b23c4d; /**< Magic number identifying nanosec resolution pcap file format */ ! const uint32_t NS_SWAPPED_MAGIC = 0x4d3cb2a1; /**< Looks this way if byte swapping is required */ const uint16_t VERSION_MAJOR = 2; /**< Major version of supported pcap file format */ const uint16_t VERSION_MINOR = 4; /**< Minor version of supported pcap file format */ PcapFile::PcapFile () : m_file (), ! m_swapMode (false), ! m_nanosecMode (false) { NS_LOG_FUNCTION (this); ! FatalImpl::RegisterStream (&m_file); // [CWEBB: Is this OK for input streams?] } PcapFile::~PcapFile () *************** *** 146,151 **** --- 147,159 ---- return m_swapMode; } + bool + PcapFile::IsNanoSecMode (void) + { + NS_LOG_FUNCTION (this); + return m_nanosecMode; + } + uint8_t PcapFile::Swap (uint8_t val) { *************** *** 285,290 **** --- 293,304 ---- } // + // Timestamps can either be microsecond or nanosecond + // + m_nanosecMode = ((m_fileHeader.m_magicNumber == NS_MAGIC) || + (m_fileHeader.m_magicNumber == NS_SWAPPED_MAGIC)) ? true : false; + + // // We only deal with one version of the pcap file format. // if (m_fileHeader.m_versionMajor != VERSION_MAJOR || m_fileHeader.m_versionMinor != VERSION_MINOR) *************** *** 318,323 **** --- 332,338 ---- // mode |= std::ios::binary; + m_filename=filename; m_file.open (filename.c_str (), mode); if (mode & std::ios::in) { *************** *** 327,339 **** } void ! PcapFile::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t timeZoneCorrection, bool swapMode) { NS_LOG_FUNCTION (this << dataLinkType << snapLen << timeZoneCorrection << swapMode); // ! // Initialize the in-memory file header. // - m_fileHeader.m_magicNumber = MAGIC; m_fileHeader.m_versionMajor = VERSION_MAJOR; m_fileHeader.m_versionMinor = VERSION_MINOR; m_fileHeader.m_zone = timeZoneCorrection; --- 342,367 ---- } void ! PcapFile::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t timeZoneCorrection, bool swapMode, bool nanosecMode) { NS_LOG_FUNCTION (this << dataLinkType << snapLen << timeZoneCorrection << swapMode); + + // + // Initialize the magic number and nanosecond mode flag + // + m_nanosecMode = nanosecMode; + if (nanosecMode) + { + m_fileHeader.m_magicNumber = NS_MAGIC; + } + else + { + m_fileHeader.m_magicNumber = MAGIC; + } + // ! // Initialize remainder of the in-memory file header. // m_fileHeader.m_versionMajor = VERSION_MAJOR; m_fileHeader.m_versionMinor = VERSION_MINOR; m_fileHeader.m_zone = timeZoneCorrection; diff -cr ns-3.24.1.ORIG/src/network/utils/pcap-file-wrapper.cc ns-3.24.1.PATCH/src/network/utils/pcap-file-wrapper.cc *** ns-3.24.1.ORIG/src/network/utils/pcap-file-wrapper.cc 2015-09-23 14:28:42.000000000 -0500 --- ns-3.24.1.PATCH/src/network/utils/pcap-file-wrapper.cc 2016-01-26 11:22:13.650843615 -0600 *************** *** 17,22 **** --- 17,23 ---- */ #include "ns3/log.h" + #include "ns3/boolean.h" #include "ns3/uinteger.h" #include "ns3/buffer.h" #include "ns3/header.h" *************** *** 40,45 **** --- 41,51 ---- UintegerValue (PcapFile::SNAPLEN_DEFAULT), MakeUintegerAccessor (&PcapFileWrapper::m_snapLen), MakeUintegerChecker (0, PcapFile::SNAPLEN_DEFAULT)) + .AddAttribute ("NanosecMode", + "Whether packet timestamps in the PCAP file are nanoseconds or microseconds(default).", + BooleanValue (false), + MakeBooleanAccessor (&PcapFileWrapper::m_nanosecMode), + MakeBooleanChecker()) ; return tid; } *************** *** 53,68 **** PcapFileWrapper::~PcapFileWrapper () { NS_LOG_FUNCTION (this); ! Close (); } - bool PcapFileWrapper::Fail (void) const { NS_LOG_FUNCTION (this); return m_file.Fail (); } bool PcapFileWrapper::Eof (void) const { --- 59,74 ---- PcapFileWrapper::~PcapFileWrapper () { NS_LOG_FUNCTION (this); ! Close (); // [CWEBB: Since PcapFileWrapper isa ns3::Object, should there be a DoDispose method? Related to Bug #2164?] } bool PcapFileWrapper::Fail (void) const { NS_LOG_FUNCTION (this); return m_file.Fail (); } + bool PcapFileWrapper::Eof (void) const { *************** *** 101,111 **** NS_LOG_FUNCTION (this << dataLinkType << snapLen << tzCorrection); if (snapLen != std::numeric_limits::max ()) { ! m_file.Init (dataLinkType, snapLen, tzCorrection); } else { ! m_file.Init (dataLinkType, m_snapLen, tzCorrection); } } --- 107,117 ---- NS_LOG_FUNCTION (this << dataLinkType << snapLen << tzCorrection); if (snapLen != std::numeric_limits::max ()) { ! m_file.Init (dataLinkType, snapLen, tzCorrection, false, m_nanosecMode); } else { ! m_file.Init (dataLinkType, m_snapLen, tzCorrection, false, m_nanosecMode); } } *************** *** 113,145 **** PcapFileWrapper::Write (Time t, Ptr p) { NS_LOG_FUNCTION (this << t << p); ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; ! ! m_file.Write (s, us, p); } void PcapFileWrapper::Write (Time t, const Header &header, Ptr p) { NS_LOG_FUNCTION (this << t << &header << p); ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; ! ! m_file.Write (s, us, header, p); } void PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length) { NS_LOG_FUNCTION (this << t << &buffer << length); ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; - m_file.Write (s, us, buffer, length); } uint32_t --- 119,210 ---- PcapFileWrapper::Write (Time t, Ptr p) { NS_LOG_FUNCTION (this << t << p); ! if (m_file.IsNanoSecMode()) ! { ! uint64_t current = t.GetNanoSeconds (); ! uint64_t s = current / 1000000000; ! uint64_t ns = current % 1000000000; ! m_file.Write (s, ns, p); ! } ! else ! { ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; ! m_file.Write (s, us, p); ! } } void PcapFileWrapper::Write (Time t, const Header &header, Ptr p) { NS_LOG_FUNCTION (this << t << &header << p); ! if (m_file.IsNanoSecMode()) ! { ! uint64_t current = t.GetNanoSeconds (); ! uint64_t s = current / 1000000000; ! uint64_t ns = current % 1000000000; ! m_file.Write (s, ns, header, p); ! } ! else ! { ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; ! m_file.Write (s, us, header, p); ! } } void PcapFileWrapper::Write (Time t, uint8_t const *buffer, uint32_t length) { NS_LOG_FUNCTION (this << t << &buffer << length); ! if (m_file.IsNanoSecMode()) ! { ! uint64_t current = t.GetNanoSeconds (); ! uint64_t s = current / 1000000000; ! uint64_t ns = current % 1000000000; ! m_file.Write (s, ns, buffer, length); ! } ! else ! { ! uint64_t current = t.GetMicroSeconds (); ! uint64_t s = current / 1000000; ! uint64_t us = current % 1000000; ! m_file.Write (s, us, buffer, length); ! } ! } ! ! Ptr ! PcapFileWrapper::Read (Time &t) ! { ! uint32_t tsSec; ! uint32_t tsUsec; ! uint32_t inclLen; ! uint32_t origLen; ! uint32_t readLen; ! ! uint32_t maxBytes=65536; ! uint8_t datbuf[maxBytes]; ! ! m_file.Read (datbuf,maxBytes,tsSec,tsUsec,inclLen,origLen,readLen); ! ! if (m_file.Fail()) ! { ! return 0; ! } ! ! if (m_file.IsNanoSecMode()) ! { ! t = NanoSeconds(tsSec*1000000000ULL+tsUsec); ! } ! else ! { ! t = MicroSeconds(tsSec*1000000ULL+tsUsec); ! } ! ! return Create (datbuf,origLen); } uint32_t diff -cr ns-3.24.1.ORIG/src/network/utils/pcap-file-wrapper.h ns-3.24.1.PATCH/src/network/utils/pcap-file-wrapper.h *** ns-3.24.1.ORIG/src/network/utils/pcap-file-wrapper.h 2015-09-23 14:28:42.000000000 -0500 --- ns-3.24.1.PATCH/src/network/utils/pcap-file-wrapper.h 2016-01-26 11:18:27.345852328 -0600 *************** *** 143,148 **** --- 143,156 ---- void Write (Time t, uint8_t const *buffer, uint32_t length); /** + * \brief Read the next packet from the file. Returns a pointer to ns3::Packet + * + * \param t Reference to packet timestamp as ns3::Time. + * + */ + Ptr Read (Time &t); + + /** * \brief Returns the magic number of the pcap file as defined by the magic_number * field in the pcap global header. * *************** *** 215,220 **** --- 223,229 ---- private: PcapFile m_file; //!< Pcap file uint32_t m_snapLen; //!< max length of saved packets + bool m_nanosecMode;//!< Timestamps in nanosecond mode }; } // namespace ns3