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;
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);
410 m_file.write ((
const char *)data, inclLen);
428 uint32_t totalSize = headerSize + p->
GetSize ();
434 uint32_t toCopy = std::min (headerSize, inclLen);
442 uint8_t *
const data,
450 NS_LOG_FUNCTION (
this << &data <<maxBytes << tsSec << tsUsec << inclLen << origLen << readLen);
459 m_file.read ((
char *)&header.m_tsSec,
sizeof(header.m_tsSec));
460 m_file.read ((
char *)&header.m_tsUsec,
sizeof(header.m_tsUsec));
461 m_file.read ((
char *)&header.m_inclLen,
sizeof(header.m_inclLen));
462 m_file.read ((
char *)&header.m_origLen,
sizeof(header.m_origLen));
471 Swap (&header, &header);
474 tsSec = header.m_tsSec;
475 tsUsec = header.m_tsUsec;
476 inclLen = header.m_inclLen;
477 origLen = header.m_origLen;
486 readLen = maxBytes < header.m_inclLen ? maxBytes : header.m_inclLen;
487 m_file.read ((
char *)data, readLen);
493 if (readLen < header.m_inclLen)
495 m_file.seekg (header.m_inclLen - readLen, std::ios::cur);
501 uint32_t & sec, uint32_t & usec, uint32_t & packets,
506 pcap1.
Open (f1, std::ios::in);
507 pcap2.
Open (f2, std::ios::in);
508 bool bad = pcap1.
Fail () || pcap2.
Fail ();
514 uint8_t *data1 =
new uint8_t [snapLen] ();
515 uint8_t *data2 =
new uint8_t [snapLen] ();
518 uint32_t tsUsec1 = 0;
519 uint32_t tsUsec2 = 0;
520 uint32_t inclLen1 = 0;
521 uint32_t inclLen2 = 0;
522 uint32_t origLen1 = 0;
523 uint32_t origLen2 = 0;
524 uint32_t readLen1 = 0;
525 uint32_t readLen2 = 0;
528 while (!pcap1.
Eof () && !pcap2.
Eof ())
530 pcap1.
Read (data1, snapLen, tsSec1, tsUsec1, inclLen1, origLen1, readLen1);
531 pcap2.
Read (data2, snapLen, tsSec2, tsUsec2, inclLen2, origLen2, readLen2);
533 bool same = pcap1.
Fail () == pcap2.
Fail ();
546 if (tsSec1 != tsSec2 || tsUsec1 != tsUsec2)
552 if (readLen1 != readLen2)
558 if (std::memcmp (data1, data2, readLen1) != 0)
567 bad = pcap1.
Fail () || pcap2.
Fail ();
568 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 "...
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
#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.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
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
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.
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)
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.
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.
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.
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.
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...