22 #include "ns3/assert.h"
23 #include "ns3/buffer.h"
24 #include "ns3/build-profile.h"
25 #include "ns3/fatal-error.h"
26 #include "ns3/fatal-impl.h"
27 #include "ns3/header.h"
29 #include "ns3/packet.h"
44 const uint32_t
MAGIC = 0xa1b2c3d4;
172 return ((val >> 8) & 0x00ff) | ((val << 8) & 0xff00);
179 return ((val >> 24) & 0x000000ff) | ((val >> 8) & 0x0000ff00) | ((val << 8) & 0x00ff0000) |
180 ((val << 24) & 0xff000000);
214 m_file.seekp(0, std::ios::beg);
258 m_file.seekg(0, std::ios::beg);
285 m_file.setstate(std::ios::failbit);
312 m_file.setstate(std::ios::failbit);
321 m_file.setstate(std::ios::failbit);
339 mode |= std::ios::binary;
342 m_file.open(filename, mode);
343 if (mode & std::ios::in)
353 int32_t timeZoneCorrection,
357 NS_LOG_FUNCTION(
this << dataLinkType << snapLen << timeZoneCorrection << swapMode);
402 bool bigEndian = u.b[3];
428 Swap(&header, &header);
466 uint32_t totalSize = headerSize + p->
GetSize();
472 uint32_t toCopy =
std::min(headerSize, inclLen);
487 NS_LOG_FUNCTION(
this << &
data << maxBytes << tsSec << tsUsec << inclLen << origLen << readLen);
508 Swap(&header, &header);
538 const std::string& f2,
547 pcap1.
Open(f1, std::ios::in);
548 pcap2.
Open(f2, std::ios::in);
549 bool bad = pcap1.
Fail() || pcap2.
Fail();
555 auto data1 =
new uint8_t[snapLen]();
556 auto data2 =
new uint8_t[snapLen]();
559 uint32_t tsUsec1 = 0;
560 uint32_t tsUsec2 = 0;
561 uint32_t inclLen1 = 0;
562 uint32_t inclLen2 = 0;
563 uint32_t origLen1 = 0;
564 uint32_t origLen2 = 0;
565 uint32_t readLen1 = 0;
566 uint32_t readLen2 = 0;
569 while (!pcap1.
Eof() && !pcap2.
Eof())
571 pcap1.
Read(data1, snapLen, tsSec1, tsUsec1, inclLen1, origLen1, readLen1);
572 pcap2.
Read(data2, snapLen, tsSec2, tsUsec2, inclLen2, origLen2, readLen2);
574 bool same = pcap1.
Fail() == pcap2.
Fail();
587 if (tsSec1 != tsSec2 || tsUsec1 != tsUsec2)
593 if (readLen1 != readLen2)
599 if (std::memcmp(data1, data2, readLen1) != 0)
609 bool eof = pcap1.
Eof() && pcap2.
Eof();
automatically resized byte buffer
void CopyData(std::ostream *os, uint32_t size) const
Copy the specified amount of data from the buffer to the given output stream.
void AddAtStart(uint32_t start)
Buffer::Iterator Begin() const
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
A class representing a pcap file.
bool IsNanoSecMode()
Get the nanosecond mode of the file.
void Close()
Close the underlying file.
static bool Diff(const std::string &f1, const std::string &f2, uint32_t &sec, uint32_t &usec, uint32_t &packets, uint32_t snapLen=SNAPLEN_DEFAULT)
Compare two PCAP files packet-by-packet.
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
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.
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
std::string m_filename
file name
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
PcapFileHeader m_fileHeader
file header
void Clear()
Clear all state bits of the underlying iostream.
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, bool nanosecMode=false)
Initialize the pcap file associated with this object.
void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t *const data, uint32_t totalLen)
Write next packet to file.
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
bool GetSwapMode()
Get the swap mode of the file.
bool m_nanosecMode
nanosecond timestamp mode
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
std::fstream m_file
file stream
void ReadAndVerifyFileHeader()
Read and verify a Pcap file header.
void WriteFileHeader()
Write a Pcap file header.
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
uint8_t Swap(uint8_t val)
Swap a value byte order.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_BUILD_DEBUG(code)
Execute a code snippet in debug builds.
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
const uint32_t MAGIC
Magic number identifying standard pcap file format.
const uint32_t NS_SWAPPED_MAGIC
Looks this way if byte swapping is required.
const uint32_t SWAPPED_MAGIC
Looks this way if byte swapping is required.
const uint16_t VERSION_MINOR
Minor version of supported pcap file format.
const uint32_t NS_MAGIC
Magic number identifying nanosec resolution pcap file format.
const uint16_t VERSION_MAJOR
Major version of supported pcap file format.