Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes

ns3::PcapFile Class Reference

Collaboration diagram for ns3::PcapFile:
Collaboration graph
[legend]

List of all members.

Classes

struct  PcapFileHeader
struct  PcapRecordHeader

Public Member Functions

bool Open (std::string const &filename, std::string const &mode)
void Close (void)
bool Init (uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
bool Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
 Write next packet to file.
bool 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.
bool GetSwapMode (void)
 Get the swap mode of the file.
uint32_t GetMagic (void)
uint16_t GetVersionMajor (void)
uint16_t GetVersionMinor (void)
int32_t GetTimeZoneOffset (void)
uint32_t GetSigFigs (void)
uint32_t GetSnapLen (void)
uint32_t GetDataLinkType (void)

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.

Static Public Attributes

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

Member Function Documentation

static 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:
f1 First PCAP file name
f2 Second PCAP file name
sec [out] Time stamp of first different packet, seconds. Undefined if files doesn't differ.
uses [out] Time stamp of first different packet, microseconds. Undefined if files doesn't differ.
snapLen Snap length (if used)
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.

bool 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:
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 177. 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).
Returns:
false if the open succeeds, true otherwise.
Warning:
Calling this method on an existing file will result in the loss any existing data.
bool ns3::PcapFile::Open ( std::string const &  filename,
std::string const &  mode 
)

Create a new pcap file or open an existing pcap file. Semantics are similar to the C standard library function fopen, 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).

Possible modes are:

   * "r":   Open a file for reading.  The file must exist.  The pcap header
   *        is assumed to exist in the file and will be read and checked.
   *        The file seek position indicator is set to point to the first 
   *        packet on exit.
   *
   * "w":   Create an empty file for writing. If a file with the same name 
   *        already exists its content is erased and the file is treated as a 
   *        new empty pcap file.  The file is assumed not to have a pcap 
   *        header and the caller is responsible for calling Init before saving
   *        any packet data.  The file seek position indicator is set to point 
   *        to the beginning of the file on exit since there will be no pcap
   *        header.
   *
   * "a":   Append to an existing file. This mode allows for adding packet data
   *        to the end of an existing pcap file.  The file must exist and have a
   *        valid pcap header written (N.B. this is different from standard fopen
   *        semantics).  The file seek position indicator is set to point 
   *        to the end of the file on exit.
   *
   * "r+":  Open a file for update -- both reading and writing. The file must 
   *        exist.  The pcap header is assumed to have been written to the 
   *        file and will be read and checked.  The file seek position indicator
   *        is set to point to the first packet on exit.
   *
   * "w+":  Create an empty file for both reading and writing.  If a file with
   *        the same name already exists, its content is erased and the file is 
   *        treated as a new empty pcap file.  Since this new file will not have
   *        a pcap header, the caller is responsible for calling Init before 
   *        saving any packet data.  On exit, the file seek position indicator is
   *        set to point to the beginning of the file.
   *
   * "a+"   Open a file for reading and appending.  The file must exist and have a
   *        valid pcap header written (N.B. this is different from standard fopen
   *        semantics).  The file seek position indicator is set to point 
   *        to the end of the file on exit.  Existing content is preserved.
   * 

Since a pcap file is always a binary file, the file type is automatically selected as a binary file. For example, providing a mode string "a+" results in the underlying OS file being opened in "a+b" mode.

Parameters:
filename String containing the name of the file.
mode String containing the access mode for the file.
Returns:
Error indication that should be interpreted as, "did an error happen"? That is, the method returns false if the open succeeds, true otherwise. The errno variable will be set by the OS to to provide a more descriptive failure indication.
bool 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
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
Returns:
true if read failed, false otherwise
bool ns3::PcapFile::Write ( uint32_t  tsSec,
uint32_t  tsUsec,
uint8_t const *const   data,
uint32_t  totalLen 
)

Write next packet to file.

Parameters:
tsSec Packet timestamp, seconds
tsUsec Packet timestamp, microseconds
data Data buffer
totalLen Total packet length
Returns:
true on error, false otherwise

Member Data Documentation

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

Default value for maximum octets to save per packet

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

Time zone offset for current location


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