A Discrete-Event Network Simulator
API
pcap-file.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Craig Dowell (craigdo@ee.washington.edu)
18  */
19 
20 #ifndef PCAP_FILE_H
21 #define PCAP_FILE_H
22 
23 #include "ns3/ptr.h"
24 
25 #include <fstream>
26 #include <stdint.h>
27 #include <string>
28 
29 namespace ns3
30 {
31 
32 class Packet;
33 class Header;
34 
42 class PcapFile
43 {
44  public:
45  static const int32_t ZONE_DEFAULT = 0;
46  static const uint32_t SNAPLEN_DEFAULT =
47  65535;
48 
49  public:
50  PcapFile();
51  ~PcapFile();
52 
56  bool Fail() const;
60  bool Eof() const;
64  void Clear();
65 
82  void Open(const std::string& filename, std::ios::openmode mode);
83 
87  void Close();
88 
118  void Init(uint32_t dataLinkType,
119  uint32_t snapLen = SNAPLEN_DEFAULT,
120  int32_t timeZoneCorrection = ZONE_DEFAULT,
121  bool swapMode = false,
122  bool nanosecMode = false);
123 
133  void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t* const data, uint32_t totalLen);
134 
143  void Write(uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
153  void Write(uint32_t tsSec, uint32_t tsUsec, const Header& header, Ptr<const Packet> p);
154 
167  void Read(uint8_t* const data,
168  uint32_t maxBytes,
169  uint32_t& tsSec,
170  uint32_t& tsUsec,
171  uint32_t& inclLen,
172  uint32_t& origLen,
173  uint32_t& readLen);
174 
194  bool GetSwapMode();
195 
202  bool IsNanoSecMode();
203 
212  uint32_t GetMagic();
213 
222  uint16_t GetVersionMajor();
223 
232  uint16_t GetVersionMinor();
233 
242  int32_t GetTimeZoneOffset();
243 
252  uint32_t GetSigFigs();
253 
262  uint32_t GetSnapLen();
263 
272  uint32_t GetDataLinkType();
273 
288  static bool Diff(const std::string& f1,
289  const std::string& f2,
290  uint32_t& sec,
291  uint32_t& usec,
292  uint32_t& packets,
293  uint32_t snapLen = SNAPLEN_DEFAULT);
294 
295  private:
300  {
301  uint32_t m_magicNumber;
302  uint16_t
304  uint16_t
306  int32_t m_zone;
307  uint32_t m_sigFigs;
308  uint32_t m_snapLen;
309  uint32_t m_type;
310  };
311 
316  {
317  uint32_t m_tsSec;
318  uint32_t m_tsUsec;
319  uint32_t m_inclLen;
320  uint32_t m_origLen;
321  };
322 
328  uint8_t Swap(uint8_t val);
334  uint16_t Swap(uint16_t val);
340  uint32_t Swap(uint32_t val);
346  void Swap(PcapFileHeader* from, PcapFileHeader* to);
352  void Swap(PcapRecordHeader* from, PcapRecordHeader* to);
353 
357  void WriteFileHeader();
369  uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
370 
375 
376  std::string m_filename;
377  std::fstream m_file;
379  bool m_swapMode;
381 };
382 
383 } // namespace ns3
384 
385 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:44
A class representing a pcap file.
Definition: pcap-file.h:43
bool IsNanoSecMode()
Get the nanosecond mode of the file.
Definition: pcap-file.cc:155
void Close()
Close the underlying file.
Definition: pcap-file.cc:92
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.
Definition: pcap-file.cc:537
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
Definition: pcap-file.cc:331
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
Definition: pcap-file.cc:141
bool m_swapMode
swap mode
Definition: pcap-file.h:379
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:479
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
Definition: pcap-file.cc:99
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
Definition: pcap-file.cc:106
std::string m_filename
file name
Definition: pcap-file.h:376
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
Definition: pcap-file.cc:113
PcapFileHeader m_fileHeader
file header
Definition: pcap-file.h:378
void Clear()
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:85
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
Write a Pcap packet header.
Definition: pcap-file.cc:413
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:351
void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t *const data, uint32_t totalLen)
Write next packet to file.
Definition: pcap-file.cc:444
bool Eof() const
Definition: pcap-file.cc:78
bool Fail() const
Definition: pcap-file.cc:71
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
Definition: pcap-file.cc:134
bool GetSwapMode()
Get the swap mode of the file.
Definition: pcap-file.cc:148
static const int32_t ZONE_DEFAULT
Time zone offset for current location.
Definition: pcap-file.h:45
bool m_nanosecMode
nanosecond timestamp mode
Definition: pcap-file.h:380
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition: pcap-file.h:46
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
Definition: pcap-file.cc:120
std::fstream m_file
file stream
Definition: pcap-file.h:377
void ReadAndVerifyFileHeader()
Read and verify a Pcap file header.
Definition: pcap-file.cc:252
void WriteFileHeader()
Write a Pcap file header.
Definition: pcap-file.cc:207
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
Definition: pcap-file.cc:127
uint8_t Swap(uint8_t val)
Swap a value byte order.
Definition: pcap-file.cc:162
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
uint32_t m_magicNumber
Magic number identifying this as a pcap file.
Definition: pcap-file.h:301
uint32_t m_sigFigs
Unused by pretty much everybody.
Definition: pcap-file.h:307
int32_t m_zone
Time zone correction to be applied to timestamps of packets.
Definition: pcap-file.h:306
uint16_t m_versionMajor
Major version identifying the version of pcap used in this file.
Definition: pcap-file.h:303
uint32_t m_type
Data link type of packet data.
Definition: pcap-file.h:309
uint32_t m_snapLen
Maximum length of packet data stored in records.
Definition: pcap-file.h:308
uint16_t m_versionMinor
Minor version identifying the version of pcap used in this file.
Definition: pcap-file.h:305
Pcap record header.
Definition: pcap-file.h:316
uint32_t m_inclLen
number of octets of packet saved in file
Definition: pcap-file.h:319
uint32_t m_tsUsec
microseconds part of timestamp (nsecs for PCAP_NSEC_MAGIC)
Definition: pcap-file.h:318
uint32_t m_tsSec
seconds part of timestamp
Definition: pcap-file.h:317
uint32_t m_origLen
actual length of original packet
Definition: pcap-file.h:320