A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ns3::PcapFile Class Reference

Classes

struct  PcapFileHeader
 
struct  PcapRecordHeader
 

Public Member Functions

void Clear (void)
 
void Close (void)
 
bool Eof (void) const
 
bool Fail (void) const
 
uint32_t GetDataLinkType (void)
 
uint32_t GetMagic (void)
 
uint32_t GetSigFigs (void)
 
uint32_t GetSnapLen (void)
 
bool GetSwapMode (void)
 Get the swap mode of the file. More...
 
int32_t GetTimeZoneOffset (void)
 
uint16_t GetVersionMajor (void)
 
uint16_t GetVersionMinor (void)
 
void Init (uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
 
void Open (std::string const &filename, std::ios::openmode mode)
 
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, 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 snapLen=SNAPLEN_DEFAULT)
 Compare two PCAP files packet-by-packet. More...
 

Static Public Attributes

static const uint32_t SNAPLEN_DEFAULT = 65535
 
static const int32_t ZONE_DEFAULT = 0
 

Private Member Functions

void ReadAndVerifyFileHeader (void)
 
uint8_t Swap (uint8_t val)
 
uint16_t Swap (uint16_t val)
 
uint32_t Swap (uint32_t val)
 
void Swap (PcapFileHeader *from, PcapFileHeader *to)
 
void Swap (PcapRecordHeader *from, PcapRecordHeader *to)
 
void WriteFileHeader (void)
 
uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
 

Private Attributes

std::fstream m_file
 
PcapFileHeader m_fileHeader
 
std::string m_filename
 
bool m_swapMode
 

Detailed Description

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

Member Function Documentation

void ns3::PcapFile::Clear ( void  )

Clear all state bits of the underlying iostream.

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

References NS_LOG_FUNCTION.

Referenced by ns3::PcapFileWrapper::Clear(), and ReadModeCreateTestCase::DoRun().

bool ns3::PcapFile::Diff ( std::string const &  f1,
std::string const &  f2,
uint32_t &  sec,
uint32_t &  usec,
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.
snapLenSnap length (if used)

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

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

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

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

References NS_LOG_FUNCTION.

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

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

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

References NS_LOG_FUNCTION.

Referenced by Diff(), WriteModeCreateTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), FileHeaderTestCase::DoRun(), RecordHeaderTestCase::DoRun(), ReadFileTestCase::DoRun(), DiffTestCase::DoRun(), and ns3::PcapFileWrapper::Fail().

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.

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

References NS_LOG_FUNCTION.

Referenced by FileHeaderTestCase::DoRun().

void ns3::PcapFile::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. 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 177. 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.
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 330 of file pcap-file.cc.

References ns3::MAGIC, NS_LOG_FUNCTION, ns3::VERSION_MAJOR, and ns3::VERSION_MINOR.

Referenced by WriteModeCreateTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), FileHeaderTestCase::DoRun(), RecordHeaderTestCase::DoRun(), DiffTestCase::DoRun(), Ns3TcpStateTestCase::DoSetup(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpInteroperabilityTestCase::DoSetup(), and ns3::PcapFileWrapper::Init().

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 311 of file pcap-file.cc.

References NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by Diff(), WriteModeCreateTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), FileHeaderTestCase::DoRun(), RecordHeaderTestCase::DoRun(), ReadFileTestCase::DoRun(), DiffTestCase::DoRun(), Ns3TcpStateTestCase::DoSetup(), Ns3TcpLossTestCase::DoSetup(), Ns3TcpInteroperabilityTestCase::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.

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 438 of file pcap-file.cc.

References NS_ASSERT, and NS_LOG_FUNCTION.

Referenced by Diff(), RecordHeaderTestCase::DoRun(), and ReadFileTestCase::DoRun().

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 405 of file pcap-file.cc.

References NS_LOG_FUNCTION.

Referenced by WriteModeCreateTestCase::DoRun(), ReadModeCreateTestCase::DoRun(), RecordHeaderTestCase::DoRun(), DiffTestCase::DoRun(), and ns3::PcapFileWrapper::Write().

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 413 of file pcap-file.cc.

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

void ns3::PcapFile::Write ( uint32_t  tsSec,
uint32_t  tsUsec,
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 421 of file pcap-file.cc.

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

Member Data Documentation

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

Default value for maximum octets to save per packet

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

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

Time zone offset for current location

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


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