A Discrete-Event Network Simulator
API
pcap-file.h
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
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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  */
20 
21 #ifndef PCAP_FILE_H
22 #define PCAP_FILE_H
23 
24 #include <string>
25 #include <fstream>
26 #include <stdint.h>
27 #include "ns3/ptr.h"
28 
29 namespace ns3 {
30 
31 class Packet;
32 class Header;
33 
34 
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 ();
51 
55  bool Fail (void) const;
59  bool Eof (void) const;
63  void Clear (void);
64 
81  void Open (std::string const &filename, std::ios::openmode mode);
82 
86  void Close (void);
87 
117  void Init (uint32_t dataLinkType,
118  uint32_t snapLen = SNAPLEN_DEFAULT,
119  int32_t timeZoneCorrection = ZONE_DEFAULT,
120  bool swapMode = false,
121  bool nanosecMode = false);
122 
132  void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
133 
142  void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
152  void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p);
153 
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 (void);
195 
202  bool IsNanoSecMode (void);
203 
212  uint32_t GetMagic (void);
213 
222  uint16_t GetVersionMajor (void);
223 
232  uint16_t GetVersionMinor (void);
233 
242  int32_t GetTimeZoneOffset (void);
243 
252  uint32_t GetSigFigs (void);
253 
262  uint32_t GetSnapLen (void);
263 
272  uint32_t GetDataLinkType (void);
273 
286  static bool Diff (std::string const & f1, std::string const & f2,
287  uint32_t & sec, uint32_t & usec, uint32_t & packets,
288  uint32_t snapLen = SNAPLEN_DEFAULT);
289 
290 private:
294  typedef struct {
295  uint32_t m_magicNumber;
296  uint16_t m_versionMajor;
297  uint16_t m_versionMinor;
298  int32_t m_zone;
299  uint32_t m_sigFigs;
300  uint32_t m_snapLen;
301  uint32_t m_type;
302  } PcapFileHeader;
303 
307  typedef struct {
308  uint32_t m_tsSec;
309  uint32_t m_tsUsec;
310  uint32_t m_inclLen;
311  uint32_t m_origLen;
313 
319  uint8_t Swap (uint8_t val);
325  uint16_t Swap (uint16_t val);
331  uint32_t Swap (uint32_t val);
337  void Swap (PcapFileHeader *from, PcapFileHeader *to);
343  void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
344 
348  void WriteFileHeader (void);
360  uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
361 
365  void ReadAndVerifyFileHeader (void);
366 
367  std::string m_filename;
368  std::fstream m_file;
370  bool m_swapMode;
372 };
373 
374 } // namespace ns3
375 
376 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:43
A class representing a pcap file.
Definition: pcap-file.h:43
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
bool m_swapMode
swap mode
Definition: pcap-file.h:370
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
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
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
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
std::string m_filename
file name
Definition: pcap-file.h:367
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
PcapFileHeader m_fileHeader
file header
Definition: pcap-file.h:369
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
Write a Pcap packet header.
Definition: pcap-file.cc:403
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:80
void WriteFileHeader(void)
Write a Pcap file header.
Definition: pcap-file.cc:202
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 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
bool Fail(void) const
Definition: pcap-file.cc:68
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 ReadAndVerifyFileHeader(void)
Read and verify a Pcap file header.
Definition: pcap-file.cc:247
bool IsNanoSecMode(void)
Get the nanosecond mode of the file.
Definition: pcap-file.cc:151
static const int32_t ZONE_DEFAULT
Time zone offset for current location.
Definition: pcap-file.h:45
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
bool m_nanosecMode
nanosecond timestamp mode
Definition: pcap-file.h:371
static const uint32_t SNAPLEN_DEFAULT
Default value for maximum octets to save per packet.
Definition: pcap-file.h:46
std::fstream m_file
file stream
Definition: pcap-file.h:368
bool Eof(void) const
Definition: pcap-file.cc:74
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:88
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
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
bool GetSwapMode(void)
Get the swap mode of the file.
Definition: pcap-file.cc:144
uint8_t Swap(uint8_t val)
Swap a value byte order.
Definition: pcap-file.cc:158
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:295
uint32_t m_sigFigs
Unused by pretty much everybody.
Definition: pcap-file.h:299
int32_t m_zone
Time zone correction to be applied to timestamps of packets.
Definition: pcap-file.h:298
uint16_t m_versionMajor
Major version identifying the version of pcap used in this file.
Definition: pcap-file.h:296
uint32_t m_type
Data link type of packet data.
Definition: pcap-file.h:301
uint32_t m_snapLen
Maximum length of packet data stored in records.
Definition: pcap-file.h:300
uint16_t m_versionMinor
Minor version identifying the version of pcap used in this file.
Definition: pcap-file.h:297
Pcap record header.
Definition: pcap-file.h:307
uint32_t m_inclLen
number of octets of packet saved in file
Definition: pcap-file.h:310
uint32_t m_tsUsec
microseconds part of timestamp (nsecs for PCAP_NSEC_MAGIC)
Definition: pcap-file.h:309
uint32_t m_tsSec
seconds part of timestamp
Definition: pcap-file.h:308
uint32_t m_origLen
actual length of original packet
Definition: pcap-file.h:311