A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
pcap-file-wrapper.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
18#ifndef PCAP_FILE_WRAPPER_H
19#define PCAP_FILE_WRAPPER_H
20
21#include "pcap-file.h"
22
23#include "ns3/nstime.h"
24#include "ns3/object.h"
25#include "ns3/packet.h"
26#include "ns3/ptr.h"
27
28#include <cstring>
29#include <fstream>
30#include <limits>
31
32namespace ns3
33{
34
35/**
36 * A class that wraps a PcapFile as an ns3::Object and provides a higher-layer
37 * ns-3 interface to the low-level public methods of PcapFile. Users are
38 * encouraged to use this object instead of class ns3::PcapFile in ns-3
39 * public APIs.
40 */
41class PcapFileWrapper : public Object
42{
43 public:
44 /**
45 * \brief Get the type ID.
46 * \return the object TypeId
47 */
48 static TypeId GetTypeId();
49
51 ~PcapFileWrapper() override;
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.
69 *
70 * Since a pcap file is always a binary file, the file type is automatically
71 * selected as a binary file (fstream::binary is automatically ored with the mode
72 * field).
73 *
74 * \param filename String containing the name of the file.
75 *
76 * \param mode String containing the access mode for the file.
77 *
78 */
79 void Open(const std::string& filename, std::ios::openmode mode);
80
81 /**
82 * Close the underlying pcap file.
83 */
84 void Close();
85
86 /**
87 * Initialize the pcap file associated with this wrapper. This file must have
88 * been previously opened with write permissions.
89 *
90 * \param dataLinkType A data link type as defined in the pcap library. If
91 * you want to make resulting pcap files visible in existing tools, the
92 * data link type must match existing definitions, such as PCAP_ETHERNET,
93 * PCAP_PPP, PCAP_80211, etc. If you are storing different kinds of packet
94 * data, such as naked TCP headers, you are at liberty to locally define your
95 * own data link types. According to the pcap-linktype man page, "well-known"
96 * pcap linktypes range from 0 to 177. If you use a large random number for
97 * your type, chances are small for a collision.
98 *
99 * \param snapLen An optional maximum size for packets written to the file.
100 * Defaults to 65535. If packets exceed this length they are truncated.
101 *
102 * \param tzCorrection An integer describing the offset of your local
103 * time zone from UTC/GMT. For example, Pacific Standard Time in the US is
104 * GMT-8, so one would enter -8 for that correction. Defaults to 0 (UTC).
105 *
106 * \warning Calling this method on an existing file will result in the loss
107 * any existing data.
108 */
109 void Init(uint32_t dataLinkType,
110 uint32_t snapLen = std::numeric_limits<uint32_t>::max(),
111 int32_t tzCorrection = PcapFile::ZONE_DEFAULT);
112
113 /**
114 * \brief Write the next packet to file
115 *
116 * \param t Packet timestamp as ns3::Time.
117 * \param p Packet to write to the pcap file.
118 *
119 */
120 void Write(Time t, Ptr<const Packet> p);
121
122 /**
123 * \brief Write the provided header along with the packet to the pcap file.
124 *
125 * It is the case that adding a header to a packet prior to writing it to a
126 * file must trigger a deep copy in the Packet. By providing the header
127 * separately, we can avoid that copy.
128 *
129 * \param t Packet timestamp as ns3::Time.
130 * \param header The Header to prepend to the packet.
131 * \param p Packet to write to the pcap file.
132 *
133 */
134 void Write(Time t, const Header& header, Ptr<const Packet> p);
135
136 /**
137 * \brief Write the provided data buffer to the pcap file.
138 *
139 * \param t Packet timestamp as ns3::Time.
140 * \param buffer The buffer to write.
141 * \param length The size of the buffer.
142 *
143 */
144 void Write(Time t, const uint8_t* buffer, uint32_t length);
145
146 /**
147 * \brief Read the next packet from the file.
148 *
149 * \param t Reference to packet timestamp as ns3::Time.
150 * \returns a pointer to ns3::Packet.
151 */
153
154 /**
155 * \brief Returns the magic number of the pcap file as defined by the magic_number
156 * field in the pcap global header.
157 *
158 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
159 *
160 * \returns magic number
161 */
163
164 /**
165 * \brief Returns the major version of the pcap file as defined by the version_major
166 * field in the pcap global header.
167 *
168 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
169 *
170 * \returns major version
171 */
172 uint16_t GetVersionMajor();
173
174 /**
175 * \brief Returns the minor version of the pcap file as defined by the version_minor
176 * field in the pcap global header.
177 *
178 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
179 *
180 * \returns minor version
181 */
182 uint16_t GetVersionMinor();
183
184 /**
185 * \brief Returns the time zone offset of the pcap file as defined by the thiszone
186 * field in the pcap global header.
187 *
188 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
189 *
190 * \returns time zone offset
191 */
193
194 /**
195 * \brief Returns the accuracy of timestamps field of the pcap file as defined
196 * by the sigfigs field in the pcap global header.
197 *
198 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
199 *
200 * \returns accuracy of timestamps
201 */
203
204 /**
205 * \brief Returns the max length of saved packets field of the pcap file as
206 * defined by the snaplen field in the pcap global header.
207 *
208 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
209 *
210 * \returns max length of saved packets field
211 */
213
214 /**
215 * \brief Returns the data link type field of the pcap file as defined by the
216 * network field in the pcap global header.
217 *
218 * See http://wiki.wireshark.org/Development/LibpcapFileFormat
219 *
220 * \returns data link type field
221 */
223
224 private:
225 PcapFile m_file; //!< Pcap file
226 uint32_t m_snapLen; //!< max length of saved packets
227 bool m_nanosecMode; //!< Timestamps in nanosecond mode
228};
229
230} // namespace ns3
231
232#endif /* PCAP_FILE_WRAPPER_H */
Protocol header serialization and deserialization.
Definition: header.h:44
A base class which provides memory management and object aggregation.
Definition: object.h:89
A class representing a pcap file.
Definition: pcap-file.h:43
static const int32_t ZONE_DEFAULT
Time zone offset for current location.
Definition: pcap-file.h:45
A class that wraps a PcapFile as an ns3::Object and provides a higher-layer ns-3 interface to the low...
Ptr< Packet > Read(Time &t)
Read the next packet from the file.
void Write(Time t, Ptr< const Packet > p)
Write the next packet to file.
static TypeId GetTypeId()
Get the type ID.
PcapFile m_file
Pcap file.
void Open(const std::string &filename, std::ios::openmode mode)
Create a new pcap file or open an existing pcap file.
int32_t GetTimeZoneOffset()
Returns the time zone offset of the pcap file as defined by the thiszone field in the pcap global hea...
uint32_t m_snapLen
max length of saved packets
uint32_t GetSigFigs()
Returns the accuracy of timestamps field of the pcap file as defined by the sigfigs field in the pcap...
void Close()
Close the underlying pcap file.
void Init(uint32_t dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=PcapFile::ZONE_DEFAULT)
Initialize the pcap file associated with this wrapper.
void Clear()
Clear all state bits of the underlying iostream.
uint32_t GetMagic()
Returns the magic number of the pcap file as defined by the magic_number field in the pcap global hea...
uint16_t GetVersionMajor()
Returns the major version of the pcap file as defined by the version_major field in the pcap global h...
uint32_t GetSnapLen()
Returns the max length of saved packets field of the pcap file as defined by the snaplen field in the...
uint16_t GetVersionMinor()
Returns the minor version of the pcap file as defined by the version_minor field in the pcap global h...
uint32_t GetDataLinkType()
Returns the data link type field of the pcap file as defined by the network field in the pcap global ...
bool m_nanosecMode
Timestamps in nanosecond mode.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.