A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
29namespace ns3
30{
31
32class Packet;
33class Header;
34
35/**
36 * \brief A class representing a pcap file
37 *
38 * A class representing a pcap file. This allows easy creation, writing and
39 * reading of files composed of stored packets; which may be viewed using
40 * standard tools.
41 */
43{
44 public:
45 static const int32_t ZONE_DEFAULT = 0; //!< Time zone offset for current location
47 65535; //!< Default value for maximum octets to save per packet
48
49 public:
50 PcapFile();
51 ~PcapFile();
52
53 /**
54 * \return true if the 'fail' bit is set in the underlying iostream, false otherwise.
55 */
56 bool Fail() const;
57 /**
58 * \return true if the 'eof' bit is set in the underlying iostream, false otherwise.
59 */
60 bool Eof() const;
61 /**
62 * Clear all state bits of the underlying iostream.
63 */
64 void Clear();
65
66 /**
67 * Create a new pcap file or open an existing pcap file. Semantics are
68 * similar to the stdc++ io stream classes, but differ in that
69 * positions in the file are based on packets not characters. For example
70 * if the file is opened for reading, the file position indicator (seek
71 * position) points to the beginning of the first packet in the file, not
72 * zero (which would point to the start of the pcap header).
73 *
74 * Since a pcap file is always a binary file, the file type is automatically
75 * selected as a binary file (fstream::binary is automatically ored with the mode
76 * field).
77 *
78 * \param filename String containing the name of the file.
79 *
80 * \param mode the access mode for the file.
81 */
82 void Open(const std::string& filename, std::ios::openmode mode);
83
84 /**
85 * Close the underlying file.
86 */
87 void Close();
88
89 /**
90 * Initialize the pcap file associated with this object. This file must have
91 * been previously opened with write permissions.
92 *
93 * \param dataLinkType A data link type as defined in the pcap library. If
94 * you want to make resulting pcap files visible in existing tools, the
95 * data link type must match existing definitions, such as PCAP_ETHERNET,
96 * PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet
97 * data, such as naked TCP headers, you are at liberty to locally define your
98 * own data link types. According to the pcap-linktype man page, "well-known"
99 * pcap linktypes range from 0 to 263. If you use a large random number for
100 * your type, chances are small for a collision.
101 *
102 * \param snapLen An optional maximum size for packets written to the file.
103 * Defaults to 65535. If packets exceed this length they are truncated.
104 *
105 * \param timeZoneCorrection An integer describing the offset of your local
106 * time zone from UTC/GMT. For example, Pacific Standard Time in the US is
107 * GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC).
108 *
109 * \param swapMode Flag indicating a difference in endianness of the
110 * writing system. Defaults to false.
111 *
112 * \param nanosecMode Flag indicating the time resolution of the writing
113 * system. Default to false.
114 *
115 * \warning Calling this method on an existing file will result in the loss
116 * any existing data.
117 */
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
124 /**
125 * \brief Write next packet to file
126 *
127 * \param tsSec Packet timestamp, seconds
128 * \param tsUsec Packet timestamp, microseconds
129 * \param data Data buffer
130 * \param totalLen Total packet length
131 *
132 */
133 void Write(uint32_t tsSec, uint32_t tsUsec, const uint8_t* const data, uint32_t totalLen);
134
135 /**
136 * \brief Write next packet to file
137 *
138 * \param tsSec Packet timestamp, seconds
139 * \param tsUsec Packet timestamp, microseconds
140 * \param p Packet to write
141 *
142 */
143 void Write(uint32_t tsSec, uint32_t tsUsec, Ptr<const Packet> p);
144 /**
145 * \brief Write next packet to file
146 *
147 * \param tsSec Packet timestamp, seconds
148 * \param tsUsec Packet timestamp, microseconds
149 * \param header Header to write, in front of packet
150 * \param p Packet to write
151 *
152 */
153 void Write(uint32_t tsSec, uint32_t tsUsec, const Header& header, Ptr<const Packet> p);
154
155 /**
156 * \brief Read next packet from file
157 *
158 * \param data [out] Data buffer
159 * \param maxBytes Allocated data buffer size
160 * \param tsSec [out] Packet timestamp, seconds
161 * \param tsUsec [out] Packet timestamp, microseconds
162 * \param inclLen [out] Included length
163 * \param origLen [out] Original length
164 * \param readLen [out] Number of bytes read
165 *
166 */
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
175 /**
176 * \brief Get the swap mode of the file.
177 *
178 * Pcap files use a magic number that is overloaded to identify both the
179 * format of the file itself and the byte ordering of the file. The magic
180 * number (and all data) is written into the file according to the native
181 * byte ordering of the writing system. If a reading application reads
182 * the magic number identically (for example 0xa1b2c3d4) then no byte
183 * swapping is required to correctly interpret the file data. If the reading
184 * application sees the magic number is byte swapped (for example 0xd4c3b2a1)
185 * then it knows that it needs to byteswap appropriate fields in the format.
186 *
187 * GetSWapMode returns a value indicating whether or not the fields are being
188 * byteswapped. Used primarily for testing the class itself, but may be
189 * useful as a flag indicating a difference in endianness of the writing
190 * system.
191 *
192 * \returns swap mode of the file
193 */
194 bool GetSwapMode();
195
196 /**
197 * \brief Get the nanosecond mode of the file.
198 *
199 * \returns true if the packet timestamps in the PCAP
200 * file have nanosecond resolution.
201 */
202 bool IsNanoSecMode();
203
204 /**
205 * \brief Returns the magic number of the pcap file as defined by the magic_number
206 * field in the pcap global header.
207 *
208 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
209 *
210 * \returns magic number
211 */
213
214 /**
215 * \brief Returns the major version of the pcap file as defined by the version_major
216 * field in the pcap global header.
217 *
218 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
219 *
220 * \returns major version
221 */
222 uint16_t GetVersionMajor();
223
224 /**
225 * \brief Returns the minor version of the pcap file as defined by the version_minor
226 * field in the pcap global header.
227 *
228 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
229 *
230 * \returns minor version
231 */
232 uint16_t GetVersionMinor();
233
234 /**
235 * \brief Returns the time zone offset of the pcap file as defined by the thiszone
236 * field in the pcap global header.
237 *
238 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
239 *
240 * \returns time zone offset
241 */
243
244 /**
245 * \brief Returns the accuracy of timestamps field of the pcap file as defined
246 * by the sigfigs field in the pcap global header.
247 *
248 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
249 *
250 * \returns accuracy of timestamps
251 */
253
254 /**
255 * \brief Returns the max length of saved packets field of the pcap file as
256 * defined by the snaplen field in the pcap global header.
257 *
258 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
259 *
260 * \returns max length of saved packets field
261 */
263
264 /**
265 * \brief Returns the data link type field of the pcap file as defined by the
266 * network field in the pcap global header.
267 *
268 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
269 *
270 * \returns data link type field
271 */
273
274 /**
275 * \brief Compare two PCAP files packet-by-packet
276 *
277 * \param f1 First PCAP file name
278 * \param f2 Second PCAP file name
279 * \param sec [out] Time stamp of first different packet, seconds. Undefined if files don't
280 * differ.
281 * \param usec [out] Time stamp of first different packet, microseconds. Undefined if files
282 * don't differ.
283 * \param packets [out] Number of first different packet. Total number of parsed packets if
284 * files don't differ.
285 * \param snapLen Snap length (if used)
286 * \return true if files are different, false otherwise
287 */
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:
296 /**
297 * \brief Pcap file header
298 */
300 {
301 uint32_t m_magicNumber; //!< Magic number identifying this as a pcap file
302 uint16_t
303 m_versionMajor; //!< Major version identifying the version of pcap used in this file
304 uint16_t
305 m_versionMinor; //!< Minor version identifying the version of pcap used in this file
306 int32_t m_zone; //!< Time zone correction to be applied to timestamps of packets
307 uint32_t m_sigFigs; //!< Unused by pretty much everybody
308 uint32_t m_snapLen; //!< Maximum length of packet data stored in records
309 uint32_t m_type; //!< Data link type of packet data
310 };
311
312 /**
313 * \brief Pcap record header
314 */
316 {
317 uint32_t m_tsSec; //!< seconds part of timestamp
318 uint32_t m_tsUsec; //!< microseconds part of timestamp (nsecs for PCAP_NSEC_MAGIC)
319 uint32_t m_inclLen; //!< number of octets of packet saved in file
320 uint32_t m_origLen; //!< actual length of original packet
321 };
322
323 /**
324 * \brief Swap a value byte order
325 * \param val the value
326 * \returns the value with byte order swapped
327 */
328 uint8_t Swap(uint8_t val);
329 /**
330 * \brief Swap a value byte order
331 * \param val the value
332 * \returns the value with byte order swapped
333 */
334 uint16_t Swap(uint16_t val);
335 /**
336 * \brief Swap a value byte order
337 * \param val the value
338 * \returns the value with byte order swapped
339 */
341 /**
342 * \brief Swap the byte order of a Pcap file header
343 * \param from original file header
344 * \param to swapped file header
345 */
346 void Swap(PcapFileHeader* from, PcapFileHeader* to);
347 /**
348 * \brief Swap the byte order of a Pcap record header
349 * \param from original record header
350 * \param to swapped record header
351 */
352 void Swap(PcapRecordHeader* from, PcapRecordHeader* to);
353
354 /**
355 * \brief Write a Pcap file header
356 */
357 void WriteFileHeader();
358 /**
359 * \brief Write a Pcap packet header
360 *
361 * The pcap header has a fixed length of 24 bytes. The last 4 bytes
362 * represent the link-layer type
363 *
364 * \param tsSec Time stamp (seconds part)
365 * \param tsUsec Time stamp (microseconds part)
366 * \param totalLen total packet length
367 * \returns the length of the packet to write in the Pcap file
368 */
369 uint32_t WritePacketHeader(uint32_t tsSec, uint32_t tsUsec, uint32_t totalLen);
370
371 /**
372 * \brief Read and verify a Pcap file header
373 */
375
376 std::string m_filename; //!< file name
377 std::fstream m_file; //!< file stream
378 PcapFileHeader m_fileHeader; //!< file header
379 bool m_swapMode; //!< swap mode
380 bool m_nanosecMode; //!< nanosecond timestamp mode
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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