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;
159 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
166 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) | ((val << 24) & 0xff000000);
200 m_file.seekp (0, std::ios::beg);
244 m_file.seekg (0, std::ios::beg);
271 m_file.setstate (std::ios::failbit);
291 m_file.setstate (std::ios::failbit);
300 m_file.setstate (std::ios::failbit);
318 mode |= std::ios::binary;
320 m_file.open (filename.c_str (), mode);
321 if (mode & std::ios::in)
329 PcapFile::Init (uint32_t dataLinkType, uint32_t snapLen, int32_t timeZoneCorrection,
bool swapMode)
331 NS_LOG_FUNCTION (
this << dataLinkType << snapLen << timeZoneCorrection << swapMode);
363 bool bigEndian = u.b[3];
389 Swap (&header, &header);
408 m_file.write ((
const char *)data, inclLen);
424 uint32_t totalSize = headerSize + p->
GetSize ();
430 uint32_t toCopy = std::min (headerSize, inclLen);
438 uint8_t *
const data,
446 NS_LOG_FUNCTION (
this << &data <<maxBytes << tsSec << tsUsec << inclLen << origLen << readLen);
455 m_file.read ((
char *)&header.m_tsSec,
sizeof(header.m_tsSec));
456 m_file.read ((
char *)&header.m_tsUsec,
sizeof(header.m_tsUsec));
457 m_file.read ((
char *)&header.m_inclLen,
sizeof(header.m_inclLen));
458 m_file.read ((
char *)&header.m_origLen,
sizeof(header.m_origLen));
467 Swap (&header, &header);
470 tsSec = header.m_tsSec;
471 tsUsec = header.m_tsUsec;
472 inclLen = header.m_inclLen;
473 origLen = header.m_origLen;
482 readLen = maxBytes < header.m_inclLen ? maxBytes : header.m_inclLen;
483 m_file.read ((
char *)data, readLen);
489 if (readLen < header.m_inclLen)
491 m_file.seekg (header.m_inclLen - readLen, std::ios::cur);
497 uint32_t & sec, uint32_t & usec,
502 pcap1.
Open (f1, std::ios::in);
503 pcap2.
Open (f2, std::ios::in);
504 bool bad = pcap1.
Fail () || pcap2.
Fail ();
510 uint8_t *data1 =
new uint8_t [snapLen] ();
511 uint8_t *data2 =
new uint8_t [snapLen] ();
514 uint32_t tsUsec1 = 0;
515 uint32_t tsUsec2 = 0;
516 uint32_t inclLen1 = 0;
517 uint32_t inclLen2 = 0;
518 uint32_t origLen1 = 0;
519 uint32_t origLen2 = 0;
520 uint32_t readLen1 = 0;
521 uint32_t readLen2 = 0;
524 while (!pcap1.
Eof () && !pcap2.
Eof ())
526 pcap1.
Read (data1, snapLen, tsSec1, tsUsec1, inclLen1, origLen1, readLen1);
527 pcap2.
Read (data2, snapLen, tsSec2, tsUsec2, inclLen2, origLen2, readLen2);
529 bool same = pcap1.
Fail () == pcap2.
Fail ();
540 if (tsSec1 != tsSec2 || tsUsec1 != tsUsec2)
546 if (readLen1 != readLen2)
552 if (std::memcmp (data1, data2, readLen1) != 0)
561 bad = pcap1.
Fail () || pcap2.
Fail ();
562 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
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.
Buffer::Iterator Begin(void) const
PcapFileHeader m_fileHeader
void Clear(void)
Clear all state bits of the underlying iostream.
void ReadAndVerifyFileHeader(void)
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)
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
Initialize the pcap file associated with this object.
uint32_t GetSigFigs(void)
void Close(void)
Close the underlying file.
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)
Create a new pcap file or open an existing pcap file.
const uint16_t VERSION_MINOR
Minor version of supported pcap file format.
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
const uint32_t NS_MAGIC
Magic number identifying nanosec resolution pcap file format.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
const uint32_t MAGIC
Magic number identifying standard pcap file format.
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.
uint32_t GetSnapLen(void)
int32_t GetTimeZoneOffset(void)