A Discrete-Event Network Simulator
API
ns3::PcapFile Class Reference

A class representing a pcap file. More...

#include "pcap-file.h"

+ Collaboration diagram for ns3::PcapFile:

Classes

struct  PcapFileHeader
 Pcap file header. More...
 
struct  PcapRecordHeader
 Pcap record header. More...
 

Public Member Functions

 PcapFile ()
 
 ~PcapFile ()
 
void Clear (void)
 Clear all state bits of the underlying iostream. More...
 
void Close (void)
 Close the underlying file. More...
 
bool Eof (void) const
 
bool Fail (void) const
 
uint32_t GetDataLinkType (void)
 Returns the data link type field of the pcap file as defined by the network field in the pcap global header. More...
 
uint32_t GetMagic (void)
 Returns the magic number of the pcap file as defined by the magic_number field in the pcap global header. More...
 
uint32_t GetSigFigs (void)
 Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap global header. More...
 
uint32_t GetSnapLen (void)
 Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the pcap global header. More...
 
bool GetSwapMode (void)
 Get the swap mode of the file. More...
 
int32_t GetTimeZoneOffset (void)
 Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global header. More...
 
uint16_t GetVersionMajor (void)
 Returns the major version of the pcap file as defined by the version_major field in the pcap global header. More...
 
uint16_t GetVersionMinor (void)
 Returns the minor version of the pcap file as defined by the version_minor field in the pcap global header. More...
 
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. More...
 
bool IsNanoSecMode (void)
 Get the nanosecond mode of the file. More...
 
void Open (std::string const &filename, std::ios::openmode mode)
 Create a new pcap file or open an existing pcap file. More...
 
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. More...
 
void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
 Write next packet to file. More...
 
void Write (uint32_t tsSec, uint32_t tsUsec, Ptr< const Packet > p)
 Write next packet to file. More...
 
void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr< const Packet > p)
 Write next packet to file. More...
 

Static Public Member Functions

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. More...
 

Static Public Attributes

static const uint32_t SNAPLEN_DEFAULT = 65535
 Default value for maximum octets to save per packet. More...
 
static const int32_t ZONE_DEFAULT = 0
 Time zone offset for current location. More...
 

Private Member Functions

void ReadAndVerifyFileHeader (void)
 Read and verify a Pcap file header. More...
 
uint8_t Swap (uint8_t val)
 Swap a value byte order. More...
 
uint16_t Swap (uint16_t val)
 Swap a value byte order. More...
 
uint32_t Swap (uint32_t val)
 Swap a value byte order. More...
 
void Swap (PcapFileHeader *from, PcapFileHeader *to)
 Swap the byte order of a Pcap file header. More...
 
void Swap (PcapRecordHeader *from, PcapRecordHeader *to)
 Swap the byte order of a Pcap record header. More...
 
void WriteFileHeader (void)
 Write a Pcap file header. More...
 
uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
 Write a Pcap packet header. More...
 

Private Attributes

std::fstream m_file
 file stream More...
 
PcapFileHeader m_fileHeader
 file header More...
 
std::string m_filename
 file name More...
 
bool m_nanosecMode
 nanosecond timestamp mode More...
 
bool m_swapMode
 swap mode More...
 

Detailed Description

A class representing a pcap file.

A class representing a pcap file. This allows easy creation, writing and reading of files composed of stored packets; which may be viewed using standard tools.

Definition at line 42 of file pcap-file.h.

Constructor & Destructor Documentation

◆ PcapFile()

ns3::PcapFile::PcapFile ( )

Definition at line 50 of file pcap-file.cc.

References m_file, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().

+ Here is the call graph for this function:

◆ ~PcapFile()

ns3::PcapFile::~PcapFile ( )

Definition at line 59 of file pcap-file.cc.

References Close(), m_file, NS_LOG_FUNCTION, and ns3::FatalImpl::UnregisterStream().

+ Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void ns3::PcapFile::Clear ( void  )

Clear all state bits of the underlying iostream.

Definition at line 80 of file pcap-file.cc.

References m_file, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::Clear().

+ Here is the caller graph for this function:

◆ Close()

void ns3::PcapFile::Close ( void  )

Close the underlying file.

Definition at line 88 of file pcap-file.cc.

References m_file, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::Close(), Ns3TcpLossTestCase::DoTeardown(), Ns3TcpStateTestCase::DoTeardown(), Ns3TcpInteroperabilityTestCase::DoTeardown(), and ~PcapFile().

+ Here is the caller graph for this function:

◆ Diff()

bool ns3::PcapFile::Diff ( std::string const &  f1,
std::string const &  f2,
uint32_t &  sec,
uint32_t &  usec,
uint32_t &  packets,
uint32_t  snapLen = SNAPLEN_DEFAULT 
)
static

Compare two PCAP files packet-by-packet.

Returns
true if files are different, false otherwise
Parameters
f1First PCAP file name
f2Second PCAP file name
sec[out] Time stamp of first different packet, seconds. Undefined if files doesn't differ.
usec[out] Time stamp of first different packet, microseconds. Undefined if files doesn't differ.
packets[out] Number of first different packet. Total number of parsed packets if files doesn't differ.
snapLenSnap length (if used)

Definition at line 528 of file pcap-file.cc.

References Eof(), Fail(), NS_LOG_FUNCTION, Open(), and Read().

+ Here is the call graph for this function:

◆ Eof()

bool ns3::PcapFile::Eof ( void  ) const
Returns
true if the 'eof' bit is set in the underlying iostream, false otherwise.

Definition at line 74 of file pcap-file.cc.

References m_file, and NS_LOG_FUNCTION.

Referenced by Diff(), and ns3::PcapFileWrapper::Eof().

+ Here is the caller graph for this function:

◆ Fail()

bool ns3::PcapFile::Fail ( void  ) const
Returns
true if the 'fail' bit is set in the underlying iostream, false otherwise.

Definition at line 68 of file pcap-file.cc.

References m_file, and NS_LOG_FUNCTION.

Referenced by Diff(), ns3::PcapFileWrapper::Fail(), and ns3::PcapFileWrapper::Read().

+ Here is the caller graph for this function:

◆ GetDataLinkType()

uint32_t ns3::PcapFile::GetDataLinkType ( void  )

Returns the data link type field of the pcap file as defined by the network field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
data link type field

Definition at line 137 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_type, and NS_LOG_FUNCTION.

Referenced by Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), Ns3TcpInteroperabilityTestCase::DoSetup(), and ns3::PcapFileWrapper::GetDataLinkType().

+ Here is the caller graph for this function:

◆ GetMagic()

uint32_t ns3::PcapFile::GetMagic ( void  )

Returns the magic number of the pcap file as defined by the magic_number field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
magic number

Definition at line 95 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_magicNumber, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetMagic().

+ Here is the caller graph for this function:

◆ GetSigFigs()

uint32_t ns3::PcapFile::GetSigFigs ( void  )

Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
accuracy of timestamps

Definition at line 123 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_sigFigs, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetSigFigs().

+ Here is the caller graph for this function:

◆ GetSnapLen()

uint32_t ns3::PcapFile::GetSnapLen ( void  )

Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
max length of saved packets field

Definition at line 130 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_snapLen, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetSnapLen().

+ Here is the caller graph for this function:

◆ GetSwapMode()

bool ns3::PcapFile::GetSwapMode ( void  )

Get the swap mode of the file.

Pcap files use a magic number that is overloaded to identify both the format of the file itself and the byte ordering of the file. The magic number (and all data) is written into the file according to the native byte ordering of the writing system. If a reading application reads the magic number identically (for example 0xa1b2c3d4) then no byte swapping is required to correctly interpret the file data. If the reading application sees the magic number is byte swapped (for example 0xd4c3b2a1) then it knows that it needs to byteswap appropriate fields in the format.

GetSWapMode returns a value indicating whether or not the fields are being byteswapped. Used primarily for testing the class itself, but may be useful as a flag indicating a difference in endianness of the writing system.

Returns
swap mode of the file

Definition at line 144 of file pcap-file.cc.

References m_swapMode, and NS_LOG_FUNCTION.

◆ GetTimeZoneOffset()

int32_t ns3::PcapFile::GetTimeZoneOffset ( void  )

Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
time zone offset

Definition at line 116 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_zone, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetTimeZoneOffset().

+ Here is the caller graph for this function:

◆ GetVersionMajor()

uint16_t ns3::PcapFile::GetVersionMajor ( void  )

Returns the major version of the pcap file as defined by the version_major field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
major version

Definition at line 102 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_versionMajor, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetVersionMajor().

+ Here is the caller graph for this function:

◆ GetVersionMinor()

uint16_t ns3::PcapFile::GetVersionMinor ( void  )

Returns the minor version of the pcap file as defined by the version_minor field in the pcap global header.

See http://wiki.wireshark.org/Development/LibpcapFileFormat

Returns
minor version

Definition at line 109 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_versionMinor, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::GetVersionMinor().

+ Here is the caller graph for this function:

◆ Init()

void ns3::PcapFile::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.

This file must have been previously opened with write permissions.

Parameters
dataLinkTypeA data link type as defined in the pcap library. If you want to make resulting pcap files visible in existing tools, the data link type must match existing definitions, such as PCAP_ETHERNET, PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet data, such as naked TCP headers, you are at liberty to locally define your own data link types. According to the pcap-linktype man page, "well-known" pcap linktypes range from 0 to 263. If you use a large random number for your type, chances are small for a collision.
snapLenAn optional maximum size for packets written to the file. Defaults to 65535. If packets exceed this length they are truncated.
timeZoneCorrectionAn integer describing the offset of your local time zone from UTC/GMT. For example, Pacific Standard Time in the US is GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC).
swapModeFlag indicating a difference in endianness of the writing system. Defaults to false.
nanosecModeFlag indicating the time resolution of the writing system. Default to false.
Returns
false if the open succeeds, true otherwise.
Warning
Calling this method on an existing file will result in the loss any existing data.

Definition at line 345 of file pcap-file.cc.

References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_magicNumber, m_nanosecMode, ns3::PcapFile::PcapFileHeader::m_sigFigs, ns3::PcapFile::PcapFileHeader::m_snapLen, m_swapMode, ns3::PcapFile::PcapFileHeader::m_type, ns3::PcapFile::PcapFileHeader::m_versionMajor, ns3::PcapFile::PcapFileHeader::m_versionMinor, ns3::PcapFile::PcapFileHeader::m_zone, ns3::MAGIC, NS_LOG_FUNCTION, ns3::NS_MAGIC, ns3::VERSION_MAJOR, ns3::VERSION_MINOR, and WriteFileHeader().

Referenced by Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), Ns3TcpInteroperabilityTestCase::DoSetup(), and ns3::PcapFileWrapper::Init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsNanoSecMode()

bool ns3::PcapFile::IsNanoSecMode ( void  )

Get the nanosecond mode of the file.

Returns
true if the packet timestamps in the PCAP file have nanosecond resolution.

Definition at line 151 of file pcap-file.cc.

References m_nanosecMode, and NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::Read(), and ns3::PcapFileWrapper::Write().

+ Here is the caller graph for this function:

◆ Open()

void ns3::PcapFile::Open ( std::string const &  filename,
std::ios::openmode  mode 
)

Create a new pcap file or open an existing pcap file.

Semantics are similar to the stdc++ io stream classes, but differ in that positions in the file are based on packets not characters. For example if the file is opened for reading, the file position indicator (seek position) points to the beginning of the first packet in the file, not zero (which would point to the start of the pcap header).

Since a pcap file is always a binary file, the file type is automatically selected as a binary file (fstream::binary is automatically ored with the mode field).

Parameters
filenameString containing the name of the file.
modethe access mode for the file.

Definition at line 325 of file pcap-file.cc.

References m_file, m_filename, NS_ASSERT, NS_LOG_FUNCTION, and ReadAndVerifyFileHeader().

Referenced by Diff(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), Ns3TcpInteroperabilityTestCase::DoSetup(), and ns3::PcapFileWrapper::Open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Read()

void ns3::PcapFile::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.

Parameters
data[out] Data buffer
maxBytesAllocated data buffer size
tsSec[out] Packet timestamp, seconds
tsUsec[out] Packet timestamp, microseconds
inclLen[out] Included length
origLen[out] Original length
readLen[out] Number of bytes read

Definition at line 469 of file pcap-file.cc.

References data, m_file, m_swapMode, NS_ASSERT, NS_LOG_FUNCTION, and Swap().

Referenced by Diff(), Ns3TcpStateTestCase::Ipv4L3Tx(), Ns3TcpLossTestCase::Ipv4L3Tx(), Ns3TcpInteroperabilityTestCase::Ipv4L3Tx(), and ns3::PcapFileWrapper::Read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ReadAndVerifyFileHeader()

◆ Swap() [1/5]

uint8_t ns3::PcapFile::Swap ( uint8_t  val)
private

Swap a value byte order.

Parameters
valthe value
Returns
the value with byte order swapped

Definition at line 158 of file pcap-file.cc.

References NS_LOG_FUNCTION.

Referenced by Read(), ReadAndVerifyFileHeader(), Swap(), WriteFileHeader(), and WritePacketHeader().

+ Here is the caller graph for this function:

◆ Swap() [2/5]

uint16_t ns3::PcapFile::Swap ( uint16_t  val)
private

Swap a value byte order.

Parameters
valthe value
Returns
the value with byte order swapped

Definition at line 165 of file pcap-file.cc.

References NS_LOG_FUNCTION.

◆ Swap() [3/5]

uint32_t ns3::PcapFile::Swap ( uint32_t  val)
private

Swap a value byte order.

Parameters
valthe value
Returns
the value with byte order swapped

Definition at line 172 of file pcap-file.cc.

References NS_LOG_FUNCTION.

◆ Swap() [4/5]

void ns3::PcapFile::Swap ( PcapFileHeader from,
PcapFileHeader to 
)
private

◆ Swap() [5/5]

void ns3::PcapFile::Swap ( PcapRecordHeader from,
PcapRecordHeader to 
)
private

Swap the byte order of a Pcap record header.

Parameters
fromoriginal record header
toswapped record header

Definition at line 192 of file pcap-file.cc.

References ns3::PcapFile::PcapRecordHeader::m_inclLen, ns3::PcapFile::PcapRecordHeader::m_origLen, ns3::PcapFile::PcapRecordHeader::m_tsSec, ns3::PcapFile::PcapRecordHeader::m_tsUsec, NS_LOG_FUNCTION, and Swap().

+ Here is the call graph for this function:

◆ Write() [1/3]

void ns3::PcapFile::Write ( uint32_t  tsSec,
uint32_t  tsUsec,
uint8_t const *const  data,
uint32_t  totalLen 
)

Write next packet to file.

Parameters
tsSecPacket timestamp, seconds
tsUsecPacket timestamp, microseconds
dataData buffer
totalLenTotal packet length

Definition at line 434 of file pcap-file.cc.

References data, m_file, NS_BUILD_DEBUG, NS_LOG_FUNCTION, and WritePacketHeader().

Referenced by Ns3TcpLossTestCase::Ipv4L3Tx(), Ns3TcpStateTestCase::Ipv4L3Tx(), Ns3TcpInteroperabilityTestCase::Ipv4L3Tx(), and ns3::PcapFileWrapper::Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Write() [2/3]

void ns3::PcapFile::Write ( uint32_t  tsSec,
uint32_t  tsUsec,
Ptr< const Packet p 
)

Write next packet to file.

Parameters
tsSecPacket timestamp, seconds
tsUsecPacket timestamp, microseconds
pPacket to write

Definition at line 443 of file pcap-file.cc.

References ns3::Packet::CopyData(), ns3::Packet::GetSize(), m_file, NS_BUILD_DEBUG, NS_LOG_FUNCTION, and WritePacketHeader().

+ Here is the call graph for this function:

◆ Write() [3/3]

void ns3::PcapFile::Write ( uint32_t  tsSec,
uint32_t  tsUsec,
const Header header,
Ptr< const Packet p 
)

Write next packet to file.

Parameters
tsSecPacket timestamp, seconds
tsUsecPacket timestamp, microseconds
headerHeader to write, in front of packet
pPacket to write

Definition at line 452 of file pcap-file.cc.

References ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), ns3::Packet::CopyData(), ns3::Buffer::CopyData(), ns3::Header::GetSerializedSize(), ns3::Packet::GetSize(), m_file, min, NS_LOG_FUNCTION, ns3::Header::Serialize(), and WritePacketHeader().

+ Here is the call graph for this function:

◆ WriteFileHeader()

void ns3::PcapFile::WriteFileHeader ( void  )
private

◆ WritePacketHeader()

uint32_t ns3::PcapFile::WritePacketHeader ( uint32_t  tsSec,
uint32_t  tsUsec,
uint32_t  totalLen 
)
private

Write a Pcap packet header.

The pcap header has a fixed length of 24 bytes. The last 4 bytes represent the link-layer type

Parameters
tsSecTime stamp (seconds part)
tsUsecTime stamp (microseconds part)
totalLentotal packet length
Returns
the length of the packet to write in the Pcap file

Definition at line 403 of file pcap-file.cc.

References m_file, m_fileHeader, ns3::PcapFile::PcapRecordHeader::m_inclLen, ns3::PcapFile::PcapRecordHeader::m_origLen, ns3::PcapFile::PcapFileHeader::m_snapLen, m_swapMode, ns3::PcapFile::PcapRecordHeader::m_tsSec, ns3::PcapFile::PcapRecordHeader::m_tsUsec, NS_ASSERT, NS_BUILD_DEBUG, NS_LOG_FUNCTION, and Swap().

Referenced by Write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_file

std::fstream ns3::PcapFile::m_file
private

◆ m_fileHeader

◆ m_filename

std::string ns3::PcapFile::m_filename
private

file name

Definition at line 369 of file pcap-file.h.

Referenced by Open().

◆ m_nanosecMode

bool ns3::PcapFile::m_nanosecMode
private

nanosecond timestamp mode

Definition at line 373 of file pcap-file.h.

Referenced by Init(), IsNanoSecMode(), and ReadAndVerifyFileHeader().

◆ m_swapMode

bool ns3::PcapFile::m_swapMode
private

swap mode

Definition at line 372 of file pcap-file.h.

Referenced by GetSwapMode(), Init(), Read(), ReadAndVerifyFileHeader(), WriteFileHeader(), and WritePacketHeader().

◆ SNAPLEN_DEFAULT

const uint32_t ns3::PcapFile::SNAPLEN_DEFAULT = 65535
static

Default value for maximum octets to save per packet.

Definition at line 46 of file pcap-file.h.

Referenced by ns3::PcapFileWrapper::GetTypeId().

◆ ZONE_DEFAULT

const int32_t ns3::PcapFile::ZONE_DEFAULT = 0
static

Time zone offset for current location.

Definition at line 45 of file pcap-file.h.


The documentation for this class was generated from the following files: