A class representing a pcap file. More...
#include "pcap-file.h"
Classes | |
struct | PcapFileHeader |
Pcap file header. More... | |
struct | PcapRecordHeader |
Pcap record header. More... | |
Public Member Functions | |
PcapFile () | |
~PcapFile () | |
void | Clear () |
Clear all state bits of the underlying iostream. | |
void | Close () |
Close the underlying file. | |
bool | Eof () const |
bool | Fail () const |
uint32_t | GetDataLinkType () |
Returns the data link type field of the pcap file as defined by the network field in the pcap global header. | |
uint32_t | GetMagic () |
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global header. | |
uint32_t | GetSigFigs () |
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap global header. | |
uint32_t | GetSnapLen () |
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the pcap global header. | |
bool | GetSwapMode () |
Get the swap mode of the file. | |
int32_t | GetTimeZoneOffset () |
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global header. | |
uint16_t | GetVersionMajor () |
Returns the major version of the pcap file as defined by the version_major field in the pcap global header. | |
uint16_t | GetVersionMinor () |
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global 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. | |
bool | IsNanoSecMode () |
Get the nanosecond mode of the file. | |
void | Open (const std::string &filename, std::ios::openmode mode) |
Create a new pcap file or open an existing 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 | Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr< const Packet > p) |
Write next packet to file. | |
void | Write (uint32_t tsSec, uint32_t tsUsec, const uint8_t *const data, uint32_t totalLen) |
Write next packet to file. | |
void | Write (uint32_t tsSec, uint32_t tsUsec, Ptr< const Packet > p) |
Write next packet to file. | |
Static Public Member Functions | |
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. | |
Static Public Attributes | |
static const uint32_t | SNAPLEN_DEFAULT |
Default value for maximum octets to save per packet. | |
static const int32_t | ZONE_DEFAULT = 0 |
Time zone offset for current location. | |
Private Member Functions | |
void | ReadAndVerifyFileHeader () |
Read and verify a Pcap file header. | |
void | Swap (PcapFileHeader *from, PcapFileHeader *to) |
Swap the byte order of a Pcap file header. | |
void | Swap (PcapRecordHeader *from, PcapRecordHeader *to) |
Swap the byte order of a Pcap record header. | |
uint16_t | Swap (uint16_t val) |
Swap a value byte order. | |
uint32_t | Swap (uint32_t val) |
Swap a value byte order. | |
uint8_t | Swap (uint8_t val) |
Swap a value byte order. | |
void | WriteFileHeader () |
Write a Pcap file header. | |
uint32_t | WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen) |
Write a Pcap packet header. | |
Private Attributes | |
std::fstream | m_file |
file stream | |
PcapFileHeader | m_fileHeader |
file header | |
std::string | m_filename |
file name | |
bool | m_nanosecMode |
nanosecond timestamp mode | |
bool | m_swapMode |
swap mode | |
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 31 of file pcap-file.h.
ns3::PcapFile::PcapFile | ( | ) |
Definition at line 43 of file pcap-file.cc.
References m_file, NS_LOG_FUNCTION, and ns3::FatalImpl::RegisterStream().
ns3::PcapFile::~PcapFile | ( | ) |
Definition at line 52 of file pcap-file.cc.
References Close(), m_file, NS_LOG_FUNCTION, and ns3::FatalImpl::UnregisterStream().
void ns3::PcapFile::Clear | ( | ) |
Clear all state bits of the underlying iostream.
Definition at line 74 of file pcap-file.cc.
References m_file, and NS_LOG_FUNCTION.
Referenced by ns3::PcapFileWrapper::Clear(), and ReadModeCreateTestCase::DoRun().
void ns3::PcapFile::Close | ( | ) |
Close the underlying file.
Definition at line 81 of file pcap-file.cc.
References m_file, and NS_LOG_FUNCTION.
Referenced by ~PcapFile(), ns3::PcapFileWrapper::Close(), DiffTestCase::DoRun(), FileHeaderTestCase::DoRun(), ReadFileTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), WriteModeCreateTestCase::DoRun(), Ns3TcpLossTestCase::DoTeardown(), and Ns3TcpStateTestCase::DoTeardown().
|
static |
Compare two PCAP files packet-by-packet.
f1 | First PCAP file name |
f2 | Second PCAP file name |
sec | [out] Time stamp of first different packet, seconds. Undefined if files don't differ. |
usec | [out] Time stamp of first different packet, microseconds. Undefined if files don't differ. |
packets | [out] Number of first different packet. Total number of parsed packets if files don't differ. |
snapLen | Snap length (if used) |
Definition at line 526 of file pcap-file.cc.
References Eof(), Fail(), NS_LOG_FUNCTION, Open(), and Read().
Referenced by DiffTestCase::DoRun().
bool ns3::PcapFile::Eof | ( | ) | const |
Definition at line 67 of file pcap-file.cc.
References m_file, and NS_LOG_FUNCTION.
Referenced by Diff(), ReadFileTestCase::DoRun(), and ns3::PcapFileWrapper::Eof().
bool ns3::PcapFile::Fail | ( | ) | const |
Definition at line 60 of file pcap-file.cc.
References m_file, and NS_LOG_FUNCTION.
Referenced by Diff(), DiffTestCase::DoRun(), FileHeaderTestCase::DoRun(), ReadFileTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), WriteModeCreateTestCase::DoRun(), ns3::PcapFileWrapper::Fail(), and ns3::PcapFileWrapper::Read().
uint32_t ns3::PcapFile::GetDataLinkType | ( | ) |
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
Definition at line 130 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_type, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), and ns3::PcapFileWrapper::GetDataLinkType().
uint32_t ns3::PcapFile::GetMagic | ( | ) |
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
Definition at line 88 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_magicNumber, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetMagic().
uint32_t ns3::PcapFile::GetSigFigs | ( | ) |
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
Definition at line 116 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_sigFigs, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetSigFigs().
uint32_t ns3::PcapFile::GetSnapLen | ( | ) |
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
Definition at line 123 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_snapLen, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetSnapLen().
bool ns3::PcapFile::GetSwapMode | ( | ) |
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.
Definition at line 137 of file pcap-file.cc.
References m_swapMode, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun().
int32_t ns3::PcapFile::GetTimeZoneOffset | ( | ) |
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
Definition at line 109 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_zone, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetTimeZoneOffset().
uint16_t ns3::PcapFile::GetVersionMajor | ( | ) |
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
Definition at line 95 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_versionMajor, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetVersionMajor().
uint16_t ns3::PcapFile::GetVersionMinor | ( | ) |
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
Definition at line 102 of file pcap-file.cc.
References m_fileHeader, ns3::PcapFile::PcapFileHeader::m_versionMinor, and NS_LOG_FUNCTION.
Referenced by FileHeaderTestCase::DoRun(), and ns3::PcapFileWrapper::GetVersionMinor().
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.
dataLinkType | A 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. |
snapLen | An optional maximum size for packets written to the file. Defaults to 65535. If packets exceed this length they are truncated. |
timeZoneCorrection | An 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). |
swapMode | Flag indicating a difference in endianness of the writing system. Defaults to false. |
nanosecMode | Flag indicating the time resolution of the writing system. Default to false. |
Definition at line 340 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 DiffTestCase::DoRun(), FileHeaderTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), WriteModeCreateTestCase::DoRun(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), and ns3::PcapFileWrapper::Init().
bool ns3::PcapFile::IsNanoSecMode | ( | ) |
Get the nanosecond mode of the file.
Definition at line 144 of file pcap-file.cc.
References m_nanosecMode, and NS_LOG_FUNCTION.
Referenced by ns3::PcapFileWrapper::Read(), ns3::PcapFileWrapper::Write(), ns3::PcapFileWrapper::Write(), and ns3::PcapFileWrapper::Write().
void ns3::PcapFile::Open | ( | const std::string & | 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).
filename | String containing the name of the file. |
mode | the access mode for the file. |
Definition at line 320 of file pcap-file.cc.
References m_file, m_filename, NS_ASSERT, NS_LOG_FUNCTION, and ReadAndVerifyFileHeader().
Referenced by Diff(), DiffTestCase::DoRun(), FileHeaderTestCase::DoRun(), ReadFileTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), WriteModeCreateTestCase::DoRun(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpStateTestCase::DoSetup(), and ns3::PcapFileWrapper::Open().
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.
data | [out] Data buffer |
maxBytes | Allocated 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 468 of file pcap-file.cc.
References data, m_file, ns3::PcapFile::PcapRecordHeader::m_inclLen, ns3::PcapFile::PcapRecordHeader::m_origLen, m_swapMode, ns3::PcapFile::PcapRecordHeader::m_tsSec, ns3::PcapFile::PcapRecordHeader::m_tsUsec, NS_ASSERT, NS_LOG_FUNCTION, and Swap().
Referenced by Diff(), ReadFileTestCase::DoRun(), RecordHeaderTestCase::DoRun(), Ns3TcpLossTestCase::Ipv4L3Tx(), Ns3TcpStateTestCase::Ipv4L3Tx(), and ns3::PcapFileWrapper::Read().
|
private |
Read and verify a Pcap file header.
Definition at line 241 of file pcap-file.cc.
References m_file, 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::NS_SWAPPED_MAGIC, Swap(), ns3::SWAPPED_MAGIC, ns3::VERSION_MAJOR, and ns3::VERSION_MINOR.
Referenced by Open().
|
private |
Swap the byte order of a Pcap file header.
from | original file header |
to | swapped file header |
Definition at line 173 of file pcap-file.cc.
References ns3::PcapFile::PcapFileHeader::m_magicNumber, ns3::PcapFile::PcapFileHeader::m_sigFigs, ns3::PcapFile::PcapFileHeader::m_snapLen, ns3::PcapFile::PcapFileHeader::m_type, ns3::PcapFile::PcapFileHeader::m_versionMajor, ns3::PcapFile::PcapFileHeader::m_versionMinor, ns3::PcapFile::PcapFileHeader::m_zone, NS_LOG_FUNCTION, and Swap().
|
private |
Swap the byte order of a Pcap record header.
from | original record header |
to | swapped record header |
Definition at line 186 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().
|
private |
Swap a value byte order.
val | the value |
Definition at line 158 of file pcap-file.cc.
References NS_LOG_FUNCTION.
Swap a value byte order.
val | the value |
Definition at line 165 of file pcap-file.cc.
References NS_LOG_FUNCTION.
|
private |
Swap a value byte order.
val | the value |
Definition at line 151 of file pcap-file.cc.
References NS_LOG_FUNCTION.
Referenced by Read(), ReadAndVerifyFileHeader(), Swap(), Swap(), WriteFileHeader(), and WritePacketHeader().
void ns3::PcapFile::Write | ( | uint32_t | tsSec, |
uint32_t | tsUsec, | ||
const Header & | header, | ||
Ptr< const Packet > | p ) |
Write next packet to file.
tsSec | Packet timestamp, seconds |
tsUsec | Packet timestamp, microseconds |
header | Header to write, in front of packet |
p | Packet to write |
Definition at line 451 of file pcap-file.cc.
References ns3::Buffer::AddAtStart(), ns3::Buffer::Begin(), ns3::Buffer::CopyData(), ns3::Header::GetSerializedSize(), m_file, NS_LOG_FUNCTION, ns3::Header::Serialize(), and WritePacketHeader().
void ns3::PcapFile::Write | ( | uint32_t | tsSec, |
uint32_t | tsUsec, | ||
const uint8_t *const | data, | ||
uint32_t | totalLen ) |
Write next packet to file.
tsSec | Packet timestamp, seconds |
tsUsec | Packet timestamp, microseconds |
data | Data buffer |
totalLen | Total packet length |
Definition at line 433 of file pcap-file.cc.
References data, m_file, NS_BUILD_DEBUG, NS_LOG_FUNCTION, and WritePacketHeader().
Referenced by DiffTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), WriteModeCreateTestCase::DoRun(), Ns3TcpLossTestCase::Ipv4L3Tx(), Ns3TcpStateTestCase::Ipv4L3Tx(), ns3::PcapFileWrapper::Write(), ns3::PcapFileWrapper::Write(), and ns3::PcapFileWrapper::Write().
Write next packet to file.
Definition at line 442 of file pcap-file.cc.
References m_file, NS_BUILD_DEBUG, NS_LOG_FUNCTION, and WritePacketHeader().
|
private |
Write a Pcap file header.
Definition at line 196 of file pcap-file.cc.
References m_file, m_fileHeader, ns3::PcapFile::PcapFileHeader::m_magicNumber, 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, NS_LOG_FUNCTION, and Swap().
Referenced by Init().
|
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
tsSec | Time stamp (seconds part) |
tsUsec | Time stamp (microseconds part) |
totalLen | total packet length |
Definition at line 402 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(), Write(), and Write().
|
private |
file stream
Definition at line 366 of file pcap-file.h.
Referenced by PcapFile(), ~PcapFile(), Clear(), Close(), Eof(), Fail(), Open(), Read(), ReadAndVerifyFileHeader(), Write(), Write(), Write(), WriteFileHeader(), and WritePacketHeader().
|
private |
file header
Definition at line 367 of file pcap-file.h.
Referenced by GetDataLinkType(), GetMagic(), GetSigFigs(), GetSnapLen(), GetTimeZoneOffset(), GetVersionMajor(), GetVersionMinor(), Init(), ReadAndVerifyFileHeader(), WriteFileHeader(), and WritePacketHeader().
|
private |
|
private |
nanosecond timestamp mode
Definition at line 369 of file pcap-file.h.
Referenced by Init(), IsNanoSecMode(), and ReadAndVerifyFileHeader().
|
private |
swap mode
Definition at line 368 of file pcap-file.h.
Referenced by GetSwapMode(), Init(), Read(), ReadAndVerifyFileHeader(), WriteFileHeader(), and WritePacketHeader().
|
static |
Default value for maximum octets to save per packet.
Definition at line 35 of file pcap-file.h.
Referenced by ns3::PcapFileWrapper::GetTypeId().
|
static |
Time zone offset for current location.
Definition at line 34 of file pcap-file.h.