A Discrete-Event Network Simulator
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Craig Dowell (craigdo@ee.washington.edu)
19  */
21 #ifndef PCAP_FILE_H
22 #define PCAP_FILE_H
24 #include <string>
25 #include <fstream>
26 #include <stdint.h>
27 #include "ns3/ptr.h"
29 namespace ns3 {
31 class Packet;
32 class Header;
42 class PcapFile
43 {
44 public:
45  static const int32_t ZONE_DEFAULT = 0;
46  static const uint32_t SNAPLEN_DEFAULT = 65535;
48 public:
49  PcapFile ();
50  ~PcapFile ();
55  bool Fail (void) const;
59  bool Eof (void) const;
63  void Clear (void);
81  void Open (std::string const &filename, std::ios::openmode mode);
86  void Close (void);
119  void Init (uint32_t dataLinkType,
120  uint32_t snapLen = SNAPLEN_DEFAULT,
121  int32_t timeZoneCorrection = ZONE_DEFAULT,
122  bool swapMode = false,
123  bool nanosecMode = false);
134  void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
144  void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
154  void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p);
169  void Read (uint8_t * const data,
170  uint32_t maxBytes,
171  uint32_t &tsSec,
172  uint32_t &tsUsec,
173  uint32_t &inclLen,
174  uint32_t &origLen,
175  uint32_t &readLen);
196  bool GetSwapMode (void);
204  bool IsNanoSecMode (void);
214  uint32_t GetMagic (void);
224  uint16_t GetVersionMajor (void);
234  uint16_t GetVersionMinor (void);
244  int32_t GetTimeZoneOffset (void);
254  uint32_t GetSigFigs (void);
264  uint32_t GetSnapLen (void);
274  uint32_t GetDataLinkType (void);
288  static bool Diff (std::string const & f1, std::string const & f2,
289  uint32_t & sec, uint32_t & usec, uint32_t & packets,
290  uint32_t snapLen = SNAPLEN_DEFAULT);
292 private:
296  typedef struct {
297  uint32_t m_magicNumber;
298  uint16_t m_versionMajor;
299  uint16_t m_versionMinor;
300  int32_t m_zone;
301  uint32_t m_sigFigs;
302  uint32_t m_snapLen;
303  uint32_t m_type;
304  } PcapFileHeader;
309  typedef struct {
310  uint32_t m_tsSec;
311  uint32_t m_tsUsec;
312  uint32_t m_inclLen;
313  uint32_t m_origLen;
321  uint8_t Swap (uint8_t val);
327  uint16_t Swap (uint16_t val);
333  uint32_t Swap (uint32_t val);
339  void Swap (PcapFileHeader *from, PcapFileHeader *to);
345  void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
350  void WriteFileHeader (void);
362  uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
367  void ReadAndVerifyFileHeader (void);
369  std::string m_filename;
370  std::fstream m_file;
372  bool m_swapMode;
374 };
376 } // namespace ns3
378 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:42
bool m_nanosecMode
nanosecond timestamp mode
Definition: pcap-file.h:373
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.
Definition: pcap-file.cc:528
uint32_t m_tsSec
seconds part of timestamp
Definition: pcap-file.h:310
uint8_t Swap(uint8_t val)
Swap a value byte order.
Definition: pcap-file.cc:158
void WriteFileHeader(void)
Write a Pcap file header.
Definition: pcap-file.cc:202
uint16_t GetVersionMinor(void)
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
Definition: pcap-file.cc:109
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.
Definition: pcap-file.cc:345
Pcap file header.
Definition: pcap-file.h:296
uint16_t m_versionMajor
Major version identifying the version of pcap used in this file.
Definition: pcap-file.h:298
uint32_t m_origLen
actual length of original packet
Definition: pcap-file.h:313
Pcap record header.
Definition: pcap-file.h:309
bool Fail(void) const
Definition: pcap-file.cc:68
bool GetSwapMode(void)
Get the swap mode of the file.
Definition: pcap-file.cc:144
A class representing a pcap file.
Definition: pcap-file.h:42
uint32_t m_tsUsec
microseconds part of timestamp (nsecs for PCAP_NSEC_MAGIC)
Definition: pcap-file.h:311
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.
Definition: pcap-file.cc:469
uint8_t data[writeSize]
static const int32_t ZONE_DEFAULT
Time zone offset for current location.
Definition: pcap-file.h:45
uint32_t m_snapLen
Maximum length of packet data stored in records.
Definition: pcap-file.h:302
bool m_swapMode
swap mode
Definition: pcap-file.h:372
PcapFileHeader m_fileHeader
file header
Definition: pcap-file.h:371
uint32_t GetMagic(void)
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Definition: pcap-file.cc:95
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t m_sigFigs
Unused by pretty much everybody.
Definition: pcap-file.h:301
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:80
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition: pcap-file.h:46
void ReadAndVerifyFileHeader(void)
Read and verify a Pcap file header.
Definition: pcap-file.cc:247
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
Write a Pcap packet header.
Definition: pcap-file.cc:403
bool Eof(void) const
Definition: pcap-file.cc:74
uint32_t GetSigFigs(void)
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
Definition: pcap-file.cc:123
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:88
uint16_t m_versionMinor
Minor version identifying the version of pcap used in this file.
Definition: pcap-file.h:299
uint16_t GetVersionMajor(void)
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
Definition: pcap-file.cc:102
uint32_t m_inclLen
number of octets of packet saved in file
Definition: pcap-file.h:312
void Open(std::string const &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
Definition: pcap-file.cc:325
uint32_t m_magicNumber
Magic number identifying this as a pcap file.
Definition: pcap-file.h:297
uint32_t m_type
Data link type of packet data.
Definition: pcap-file.h:303
std::fstream m_file
file stream
Definition: pcap-file.h:370
int32_t m_zone
Time zone correction to be applied to timestamps of packets.
Definition: pcap-file.h:300
uint32_t GetDataLinkType(void)
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
Definition: pcap-file.cc:137
void Write(uint32_t tsSec, uint32_t tsUsec, uint8_t const *const data, uint32_t totalLen)
Write next packet to file.
Definition: pcap-file.cc:434
bool IsNanoSecMode(void)
Get the nanosecond mode of the file.
Definition: pcap-file.cc:151
uint32_t GetSnapLen(void)
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
Definition: pcap-file.cc:130
int32_t GetTimeZoneOffset(void)
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
Definition: pcap-file.cc:116
std::string m_filename
file name
Definition: pcap-file.h:369