A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pcap-file.h
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 /*
35  * A class representing a pcap file. This allows easy creation, writing and
36  * reading of files composed of stored packets; which may be viewed using
37  * standard tools.
38  */
39 
40 class PcapFile
41 {
42 public:
43  static const int32_t ZONE_DEFAULT = 0;
44  static const uint32_t SNAPLEN_DEFAULT = 65535;
46 public:
47  PcapFile ();
48  ~PcapFile ();
49 
53  bool Fail (void) const;
57  bool Eof (void) const;
61  void Clear (void);
62 
79  void Open (std::string const &filename, std::ios::openmode mode);
80 
84  void Close (void);
85 
114  void Init (uint32_t dataLinkType,
115  uint32_t snapLen = SNAPLEN_DEFAULT,
116  int32_t timeZoneCorrection = ZONE_DEFAULT,
117  bool swapMode = false);
118 
128  void Write (uint32_t tsSec, uint32_t tsUsec, uint8_t const * const data, uint32_t totalLen);
129 
138  void Write (uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
148  void Write (uint32_t tsSec, uint32_t tsUsec, Header &header, Ptr<const Packet> p);
149 
150 
163  void Read (uint8_t * const data,
164  uint32_t maxBytes,
165  uint32_t &tsSec,
166  uint32_t &tsUsec,
167  uint32_t &inclLen,
168  uint32_t &origLen,
169  uint32_t &readLen);
170 
188  bool GetSwapMode (void);
189 
190  /*
191  * \brief Returns the magic number of the pcap file as defined by the magic_number
192  * field in the pcap global header.
193  *
194  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
195  */
196  uint32_t GetMagic (void);
197 
198  /*
199  * \brief Returns the major version of the pcap file as defined by the version_major
200  * field in the pcap global header.
201  *
202  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
203  */
204  uint16_t GetVersionMajor (void);
205 
206  /*
207  * \brief Returns the minor version of the pcap file as defined by the version_minor
208  * field in the pcap global header.
209  *
210  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
211  */
212  uint16_t GetVersionMinor (void);
213 
214  /*
215  * \brief Returns the time zone offset of the pcap file as defined by the thiszone
216  * field in the pcap global header.
217  *
218  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
219  */
220  int32_t GetTimeZoneOffset (void);
221 
222  /*
223  * \brief Returns the accuracy of timestamps field of the pcap file as defined
224  * by the sigfigs field in the pcap global header.
225  *
226  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
227  */
228  uint32_t GetSigFigs (void);
229 
230  /*
231  * \brief Returns the max length of saved packets field of the pcap file as
232  * defined by the snaplen field in the pcap global header.
233  *
234  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
235  */
236  uint32_t GetSnapLen (void);
237 
238  /*
239  * \brief Returns the data link type field of the pcap file as defined by the
240  * network field in the pcap global header.
241  *
242  * See http://wiki.wireshark.org/Development/LibpcapFileFormat
243  */
244  uint32_t GetDataLinkType (void);
245 
257  static bool Diff (std::string const & f1, std::string const & f2,
258  uint32_t & sec, uint32_t & usec,
259  uint32_t snapLen = SNAPLEN_DEFAULT);
260 
261 private:
262  typedef struct {
263  uint32_t m_magicNumber;
264  uint16_t m_versionMajor;
265  uint16_t m_versionMinor;
266  int32_t m_zone;
267  uint32_t m_sigFigs;
268  uint32_t m_snapLen;
269  uint32_t m_type;
270  } PcapFileHeader;
271 
272  typedef struct {
273  uint32_t m_tsSec;
274  uint32_t m_tsUsec;
275  uint32_t m_inclLen;
276  uint32_t m_origLen;
278 
279  uint8_t Swap (uint8_t val);
280  uint16_t Swap (uint16_t val);
281  uint32_t Swap (uint32_t val);
282  void Swap (PcapFileHeader *from, PcapFileHeader *to);
283  void Swap (PcapRecordHeader *from, PcapRecordHeader *to);
284 
285  void WriteFileHeader (void);
286  uint32_t WritePacketHeader (uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
287  void ReadAndVerifyFileHeader (void);
288 
289  std::string m_filename;
290  std::fstream m_file;
291  PcapFileHeader m_fileHeader;
292  bool m_swapMode;
293 };
294 
295 } // namespace ns3
296 
297 #endif /* PCAP_FILE_H */
Protocol header serialization and deserialization.
Definition: header.h:42
bool Eof(void) const
Definition: pcap-file.cc:73
bool GetSwapMode(void)
Get the swap mode of the file.
Definition: pcap-file.cc:143
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:438
static const int32_t ZONE_DEFAULT
Definition: pcap-file.h:43
void Clear(void)
Definition: pcap-file.cc:79
static const uint32_t SNAPLEN_DEFAULT
Definition: pcap-file.h:44
void Init(uint32_t dataLinkType, uint32_t snapLen=SNAPLEN_DEFAULT, int32_t timeZoneCorrection=ZONE_DEFAULT, bool swapMode=false)
Definition: pcap-file.cc:330
void Close(void)
Definition: pcap-file.cc:87
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:497
void Open(std::string const &filename, std::ios::openmode mode)
Definition: pcap-file.cc:311
bool Fail(void) const
Definition: pcap-file.cc:67
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:405