A Discrete-Event Network Simulator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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);
116  void Init (uint32_t dataLinkType,
117  uint32_t snapLen = SNAPLEN_DEFAULT,
118  int32_t timeZoneCorrection = ZONE_DEFAULT,
119  bool swapMode = false);
130  void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
140  void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
150  void Write (uint32_t tsSec, uint32_t tsUsec, Header &header, Ptr<const Packet> p);
165  void Read (uint8_t * const data,
166  uint32_t maxBytes,
167  uint32_t &tsSec,
168  uint32_t &tsUsec,
169  uint32_t &inclLen,
170  uint32_t &origLen,
171  uint32_t &readLen);
192  bool GetSwapMode (void);
202  uint32_t GetMagic (void);
212  uint16_t GetVersionMajor (void);
222  uint16_t GetVersionMinor (void);
232  int32_t GetTimeZoneOffset (void);
242  uint32_t GetSigFigs (void);
252  uint32_t GetSnapLen (void);
262  uint32_t GetDataLinkType (void);
275  static bool Diff (std::string const & f1, std::string const & f2,
276  uint32_t & sec, uint32_t & usec,
277  uint32_t snapLen = SNAPLEN_DEFAULT);
279 private:
283  typedef struct {
284  uint32_t m_magicNumber;
285  uint16_t m_versionMajor;
286  uint16_t m_versionMinor;
287  int32_t m_zone;
288  uint32_t m_sigFigs;
289  uint32_t m_snapLen;
290  uint32_t m_type;
291  } PcapFileHeader;
296  typedef struct {
297  uint32_t m_tsSec;
298  uint32_t m_tsUsec;
299  uint32_t m_inclLen;
300  uint32_t m_origLen;
308  uint8_t Swap (uint8_t val);
314  uint16_t Swap (uint16_t val);
320  uint32_t Swap (uint32_t val);
326  void Swap (PcapFileHeader *from, PcapFileHeader *to);
332  void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
337  void WriteFileHeader (void);
345  uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
350  void ReadAndVerifyFileHeader (void);
352  std::string m_filename;
353  std::fstream m_file;
355  bool m_swapMode;
356 };
358 } // namespace ns3
360 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t m_tsSec
seconds part of timestamp
Definition: pcap-file.h:297
uint8_t Swap(uint8_t val)
Swap a value byte order.
Definition: pcap-file.cc:149
void WriteFileHeader(void)
Write a Pcap file header.
Definition: pcap-file.cc:193
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:107
Pcap file header.
Definition: pcap-file.h:283
uint16_t m_versionMajor
Major version identifying the version of pcap used in this file.
Definition: pcap-file.h:285
uint32_t m_origLen
actual length of original packet
Definition: pcap-file.h:300
bool Eof(void) const
Definition: pcap-file.cc:72
Pcap record header.
Definition: pcap-file.h:296
bool GetSwapMode(void)
Get the swap mode of the file.
Definition: pcap-file.cc:142
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:298
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:437
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:289
bool m_swapMode
swap mode
Definition: pcap-file.h:355
PcapFileHeader m_fileHeader
file header
Definition: pcap-file.h:354
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:93
uint32_t m_sigFigs
Unused by pretty much everybody.
Definition: pcap-file.h:288
void Clear(void)
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:78
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:238
uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen)
Write a Pcap packet header.
Definition: pcap-file.cc:374
void 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.
Definition: pcap-file.cc:329
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:121
void Close(void)
Close the underlying file.
Definition: pcap-file.cc:86
uint16_t m_versionMinor
Minor version identifying the version of pcap used in this file.
Definition: pcap-file.h:286
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:100
uint32_t m_inclLen
number of octets of packet saved in file
Definition: pcap-file.h:299
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.
Definition: pcap-file.cc:496
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:310
uint32_t m_magicNumber
Magic number identifying this as a pcap file.
Definition: pcap-file.h:284
uint32_t m_type
Data link type of packet data.
Definition: pcap-file.h:290
bool Fail(void) const
Definition: pcap-file.cc:66
std::fstream m_file
file stream
Definition: pcap-file.h:353
int32_t m_zone
Time zone correction to be applied to timestamps of packets.
Definition: pcap-file.h:287
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:135
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:404
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:128
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:114
std::string m_filename
file name
Definition: pcap-file.h:352