23 #include "ns3/assert.h" 24 #include "ns3/packet.h" 25 #include "ns3/fatal-error.h" 26 #include "ns3/fatal-impl.h" 27 #include "ns3/header.h" 28 #include "ns3/buffer.h" 31 #include "ns3/build-profile.h" 41 const uint32_t
MAGIC = 0xa1b2c3d4;
168 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
175 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000);
209 m_file.seekp (0, std::ios::beg);
253 m_file.seekg (0, std::ios::beg);
280 m_file.setstate (std::ios::failbit);
306 m_file.setstate (std::ios::failbit);
315 m_file.setstate (std::ios::failbit);
333 mode |= std::ios::binary;
336 m_file.open (filename.c_str (), mode);
337 if (mode & std::ios::in)
345 PcapFile::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t timeZoneCorrection,
bool swapMode,
bool nanosecMode)
347 NS_LOG_FUNCTION (
this << dataLinkType << snapLen << timeZoneCorrection << swapMode);
392 bool bigEndian = u.b[3];
418 Swap (&header, &header);
456 uint32_t totalSize = headerSize + p->
GetSize ();
462 uint32_t toCopy =
std::min (headerSize, inclLen);
470 uint8_t *
const data,
478 NS_LOG_FUNCTION (
this << &
data <<maxBytes << tsSec << tsUsec << inclLen << origLen << readLen);
487 m_file.read ((
char *)&header.m_tsSec,
sizeof(header.m_tsSec));
488 m_file.read ((
char *)&header.m_tsUsec,
sizeof(header.m_tsUsec));
489 m_file.read ((
char *)&header.m_inclLen,
sizeof(header.m_inclLen));
490 m_file.read ((
char *)&header.m_origLen,
sizeof(header.m_origLen));
499 Swap (&header, &header);
502 tsSec = header.m_tsSec;
503 tsUsec = header.m_tsUsec;
504 inclLen = header.m_inclLen;
505 origLen = header.m_origLen;
514 readLen = maxBytes < header.m_inclLen ? maxBytes : header.m_inclLen;
521 if (readLen < header.m_inclLen)
523 m_file.seekg (header.m_inclLen - readLen, std::ios::cur);
529 uint32_t & sec, uint32_t & usec, uint32_t & packets,
534 pcap1.
Open (f1, std::ios::in);
535 pcap2.
Open (f2, std::ios::in);
536 bool bad = pcap1.
Fail () || pcap2.
Fail ();
542 uint8_t *data1 =
new uint8_t [snapLen] ();
543 uint8_t *data2 =
new uint8_t [snapLen] ();
546 uint32_t tsUsec1 = 0;
547 uint32_t tsUsec2 = 0;
548 uint32_t inclLen1 = 0;
549 uint32_t inclLen2 = 0;
550 uint32_t origLen1 = 0;
551 uint32_t origLen2 = 0;
552 uint32_t readLen1 = 0;
553 uint32_t readLen2 = 0;
556 while (!pcap1.
Eof () && !pcap2.
Eof ())
558 pcap1.
Read (data1, snapLen, tsSec1, tsUsec1, inclLen1, origLen1, readLen1);
559 pcap2.
Read (data2, snapLen, tsSec2, tsUsec2, inclLen2, origLen2, readLen2);
561 bool same = pcap1.
Fail () == pcap2.
Fail ();
574 if (tsSec1 != tsSec2 || tsUsec1 != tsUsec2)
580 if (readLen1 != readLen2)
586 if (std::memcmp (data1, data2, readLen1) != 0)
595 bad = pcap1.
Fail () || pcap2.
Fail ();
596 bool eof = pcap1.
Eof () && pcap2.
Eof ();
void AddAtStart(uint32_t start)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool m_nanosecMode
nanosecond timestamp mode
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
static bool Diff(std::string const &f1, std::string const &f2, uint32_t &sec, uint32_t &usec, uint32_t &packets, uint32_t snapLen=SNAPLEN_DEFAULT)
Compare two PCAP files packet-by-packet.
uint8_t Swap(uint8_t val)
Swap a value byte order.
void WriteFileHeader(void)
Write a Pcap file header.
uint16_t GetVersionMinor(void)
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
automatically resized byte buffer
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false, bool nanosecMode=false)
Initialize the pcap file associated with this object.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
const uint16_t VERSION_MAJOR
Major version of supported pcap file format.
const uint32_t SWAPPED_MAGIC
Looks this way if byte swapping is required.
bool GetSwapMode(void)
Get the swap mode of the file.
A class representing a pcap file.
void Read(uint8_t *const data, uint32_t maxBytes, uint32_t &tsSec, uint32_t &tsUsec, uint32_t &inclLen, uint32_t &origLen, uint32_t &readLen)
Read next packet from file.
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
PcapFileHeader m_fileHeader
file header
uint32_t GetMagic(void)
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Clear(void)
Clear all state bits of the underlying iostream.
void ReadAndVerifyFileHeader(void)
Read and verify a Pcap file header.
const uint32_t NS_SWAPPED_MAGIC
Looks this way if byte swapping is required.
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
Write a Pcap packet header.
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
void Close(void)
Close the underlying file.
uint16_t GetVersionMajor(void)
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
void Open(std::string const &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
const uint16_t VERSION_MINOR
Minor version of supported pcap file format.
std::fstream m_file
file stream
const uint32_t NS_MAGIC
Magic number identifying nanosec resolution pcap file format.
#define NS_BUILD_DEBUG(code)
Execute a code snippet in debug builds.
const uint32_t MAGIC
Magic number identifying standard pcap file format.
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.
bool IsNanoSecMode(void)
Get the nanosecond mode of the file.
Buffer::Iterator Begin(void) const
uint32_t GetSnapLen(void)
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
int32_t GetTimeZoneOffset(void)
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
std::string m_filename
file name