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