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"
40 const uint32_t
MAGIC = 0xa1b2c3d4;
160 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
167 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000);
201 m_file.seekp (0, std::ios::beg);
245 m_file.seekg (0, std::ios::beg);
272 m_file.setstate (std::ios::failbit);
292 m_file.setstate (std::ios::failbit);
301 m_file.setstate (std::ios::failbit);
319 mode |= std::ios::binary;
321 m_file.open (filename.c_str (), mode);
322 if (mode & std::ios::in)
330 PcapFile::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t timeZoneCorrection,
bool swapMode)
332 NS_LOG_FUNCTION (
this << dataLinkType << snapLen << timeZoneCorrection << swapMode);
364 bool bigEndian = u.b[3];
390 Swap (&header, &header);
409 m_file.write ((
const char *)data, inclLen);
425 uint32_t totalSize = headerSize + p->
GetSize ();
431 uint32_t toCopy = std::min (headerSize, inclLen);
439 uint8_t *
const data,
447 NS_LOG_FUNCTION (
this << &data <<maxBytes << tsSec << tsUsec << inclLen << origLen << readLen);
456 m_file.read ((
char *)&header.m_tsSec,
sizeof(header.m_tsSec));
457 m_file.read ((
char *)&header.m_tsUsec,
sizeof(header.m_tsUsec));
458 m_file.read ((
char *)&header.m_inclLen,
sizeof(header.m_inclLen));
459 m_file.read ((
char *)&header.m_origLen,
sizeof(header.m_origLen));
468 Swap (&header, &header);
471 tsSec = header.m_tsSec;
472 tsUsec = header.m_tsUsec;
473 inclLen = header.m_inclLen;
474 origLen = header.m_origLen;
483 readLen = maxBytes < header.m_inclLen ? maxBytes : header.m_inclLen;
484 m_file.read ((
char *)data, readLen);
490 if (readLen < header.m_inclLen)
492 m_file.seekg (header.m_inclLen - readLen, std::ios::cur);
498 uint32_t & sec, uint32_t & usec,
503 pcap1.
Open (f1, std::ios::in);
504 pcap2.
Open (f2, std::ios::in);
505 bool bad = pcap1.
Fail () || pcap2.
Fail ();
511 uint8_t *data1 =
new uint8_t [snapLen] ();
512 uint8_t *data2 =
new uint8_t [snapLen] ();
515 uint32_t tsUsec1 = 0;
516 uint32_t tsUsec2 = 0;
517 uint32_t inclLen1 = 0;
518 uint32_t inclLen2 = 0;
519 uint32_t origLen1 = 0;
520 uint32_t origLen2 = 0;
521 uint32_t readLen1 = 0;
522 uint32_t readLen2 = 0;
525 while (!pcap1.
Eof () && !pcap2.
Eof ())
527 pcap1.
Read (data1, snapLen, tsSec1, tsUsec1, inclLen1, origLen1, readLen1);
528 pcap2.
Read (data2, snapLen, tsSec2, tsUsec2, inclLen2, origLen2, readLen2);
530 bool same = pcap1.
Fail () == pcap2.
Fail ();
541 if (tsSec1 != tsSec2 || tsUsec1 != tsUsec2)
547 if (readLen1 != readLen2)
553 if (std::memcmp (data1, data2, readLen1) != 0)
562 bad = pcap1.
Fail () || pcap2.
Fail ();
563 bool eof = pcap1.
Eof () && pcap2.
Eof ();
#define NS_LOG_FUNCTION(parameters)
NS_LOG_COMPONENT_DEFINE("PcapFile")
uint8_t Swap(uint8_t val)
void WriteFileHeader(void)
uint16_t GetVersionMinor(void)
automatically resized byte buffer
#define NS_ASSERT(condition)
uint32_t GetSize(void) const
const uint16_t VERSION_MAJOR
const uint32_t SWAPPED_MAGIC
bool GetSwapMode(void)
Get the swap mode of the 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
Buffer::Iterator Begin(void) const
PcapFileHeader m_fileHeader
void ReadAndVerifyFileHeader(void)
const uint32_t NS_SWAPPED_MAGIC
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
uint32_t GetSigFigs(void)
uint16_t GetVersionMajor(void)
static bool Diff(std::string const &f1, std::string const &f2, uint32_t &sec, uint32_t &usec, uint32_t snapLen=SNAPLEN_DEFAULT)
Compare two PCAP files packet-by-packet.
void Open(std::string const &filename, std::ios::openmode mode)
const uint16_t VERSION_MINOR
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
bool AddAtStart(uint32_t start)
uint32_t GetDataLinkType(void)
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.
const int32_t SIGFIGS_DEFAULT
uint32_t GetSnapLen(void)
int32_t GetTimeZoneOffset(void)