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 
116  void Init (uint32_t dataLinkType,
117  uint32_t snapLen = SNAPLEN_DEFAULT,
118  int32_t timeZoneCorrection = ZONE_DEFAULT,
119  bool swapMode = false,
120  bool nanosecMode = false);
121 
131  void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
132 
141  void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
151  void Write (uint32_t tsSec, uint32_t tsUsec, const Header &header, Ptr<const Packet> p);
152 
153 
166  void Read (uint8_t * const data,
167  uint32_t maxBytes,
168  uint32_t &tsSec,
169  uint32_t &tsUsec,
170  uint32_t &inclLen,
171  uint32_t &origLen,
172  uint32_t &readLen);
173 
193  bool GetSwapMode (void);
194 
201  bool IsNanoSecMode (void);
202 
211  uint32_t GetMagic (void);
212 
221  uint16_t GetVersionMajor (void);
222 
231  uint16_t GetVersionMinor (void);
232 
241  int32_t GetTimeZoneOffset (void);
242 
251  uint32_t GetSigFigs (void);
252 
261  uint32_t GetSnapLen (void);
262 
271  uint32_t GetDataLinkType (void);
272 
284  static bool Diff (std::string const & f1, std::string const & f2,
285  uint32_t & sec, uint32_t & usec, uint32_t & packets,
286  uint32_t snapLen = SNAPLEN_DEFAULT);
287 
288 private:
292  typedef struct {
293  uint32_t m_magicNumber;
294  uint16_t m_versionMajor;
295  uint16_t m_versionMinor;
296  int32_t m_zone;
297  uint32_t m_sigFigs;
298  uint32_t m_snapLen;
299  uint32_t m_type;
300  } PcapFileHeader;
301 
305  typedef struct {
306  uint32_t m_tsSec;
307  uint32_t m_tsUsec;
308  uint32_t m_inclLen;
309  uint32_t m_origLen;
311 
317  uint8_t Swap (uint8_t val);
323  uint16_t Swap (uint16_t val);
329  uint32_t Swap (uint32_t val);
335  void Swap (PcapFileHeader *from, PcapFileHeader *to);
341  void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
342 
346  void WriteFileHeader (void);
358  uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
359 
363  void ReadAndVerifyFileHeader (void);
364 
365  std::string m_filename;
366  std::fstream m_file;
368  bool m_swapMode;
370 };
371 
372 } // namespace ns3
373 
374 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:42
bool m_nanosecMode
nanosecond timestamp mode
Definition: pcap-file.h:369
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:306
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:292
uint16_t m_versionMajor
Major version identifying the version of pcap used in this file.
Definition: pcap-file.h:294
uint32_t m_origLen
actual length of original packet
Definition: pcap-file.h:309
bool Eof(void) const
Definition: pcap-file.cc:74
Pcap record header.
Definition: pcap-file.h:305
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:307
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:298
bool m_swapMode
swap mode
Definition: pcap-file.h:368
PcapFileHeader m_fileHeader
file header
Definition: pcap-file.h:367
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:297
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
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:295
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:308
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:293
uint32_t m_type
Data link type of packet data.
Definition: pcap-file.h:299
bool Fail(void) const
Definition: pcap-file.cc:68
std::fstream m_file
file stream
Definition: pcap-file.h:366
int32_t m_zone
Time zone correction to be applied to timestamps of packets.
Definition: pcap-file.h:296
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:365