A Discrete-Event Network Simulator
API
pcap-file-wrapper.cc
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 #include "pcap-file-wrapper.h"
19 
20 #include "ns3/boolean.h"
21 #include "ns3/buffer.h"
22 #include "ns3/header.h"
23 #include "ns3/log.h"
24 #include "ns3/uinteger.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("PcapFileWrapper");
30 
31 NS_OBJECT_ENSURE_REGISTERED(PcapFileWrapper);
32 
33 TypeId
35 {
36  static TypeId tid =
37  TypeId("ns3::PcapFileWrapper")
38  .SetParent<Object>()
39  .SetGroupName("Network")
40  .AddConstructor<PcapFileWrapper>()
41  .AddAttribute("CaptureSize",
42  "Maximum length of captured packets (cf. pcap snaplen)",
45  MakeUintegerChecker<uint32_t>(0, PcapFile::SNAPLEN_DEFAULT))
46  .AddAttribute("NanosecMode",
47  "Whether packet timestamps in the PCAP file are nanoseconds or "
48  "microseconds(default).",
49  BooleanValue(false),
52  return tid;
53 }
54 
56 {
57  NS_LOG_FUNCTION(this);
58 }
59 
61 {
62  NS_LOG_FUNCTION(this);
63  Close();
64 }
65 
66 bool
68 {
69  NS_LOG_FUNCTION(this);
70  return m_file.Fail();
71 }
72 
73 bool
75 {
76  NS_LOG_FUNCTION(this);
77  return m_file.Eof();
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION(this);
84  m_file.Clear();
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION(this);
91  m_file.Close();
92 }
93 
94 void
95 PcapFileWrapper::Open(const std::string& filename, std::ios::openmode mode)
96 {
97  NS_LOG_FUNCTION(this << filename << mode);
98  m_file.Open(filename, mode);
99 }
100 
101 void
102 PcapFileWrapper::Init(uint32_t dataLinkType, uint32_t snapLen, int32_t tzCorrection)
103 {
104  //
105  // If the user doesn't provide a snaplen, the default value will come in. If
106  // this happens, we use the "CaptureSize" Attribute. If the user does provide
107  // a snaplen, we use the one provided.
108  //
109  NS_LOG_FUNCTION(this << dataLinkType << snapLen << tzCorrection);
110  if (snapLen != std::numeric_limits<uint32_t>::max())
111  {
112  m_file.Init(dataLinkType, snapLen, tzCorrection, false, m_nanosecMode);
113  }
114  else
115  {
116  m_file.Init(dataLinkType, m_snapLen, tzCorrection, false, m_nanosecMode);
117  }
118 }
119 
120 void
122 {
123  NS_LOG_FUNCTION(this << t << p);
124  if (m_file.IsNanoSecMode())
125  {
126  uint64_t current = t.GetNanoSeconds();
127  uint64_t s = current / 1000000000;
128  uint64_t ns = current % 1000000000;
129  m_file.Write(s, ns, p);
130  }
131  else
132  {
133  uint64_t current = t.GetMicroSeconds();
134  uint64_t s = current / 1000000;
135  uint64_t us = current % 1000000;
136  m_file.Write(s, us, p);
137  }
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION(this << t << &header << p);
144  if (m_file.IsNanoSecMode())
145  {
146  uint64_t current = t.GetNanoSeconds();
147  uint64_t s = current / 1000000000;
148  uint64_t ns = current % 1000000000;
149  m_file.Write(s, ns, header, p);
150  }
151  else
152  {
153  uint64_t current = t.GetMicroSeconds();
154  uint64_t s = current / 1000000;
155  uint64_t us = current % 1000000;
156  m_file.Write(s, us, header, p);
157  }
158 }
159 
160 void
161 PcapFileWrapper::Write(Time t, const uint8_t* buffer, uint32_t length)
162 {
163  NS_LOG_FUNCTION(this << t << &buffer << length);
164  if (m_file.IsNanoSecMode())
165  {
166  uint64_t current = t.GetNanoSeconds();
167  uint64_t s = current / 1000000000;
168  uint64_t ns = current % 1000000000;
169  m_file.Write(s, ns, buffer, length);
170  }
171  else
172  {
173  uint64_t current = t.GetMicroSeconds();
174  uint64_t s = current / 1000000;
175  uint64_t us = current % 1000000;
176  m_file.Write(s, us, buffer, length);
177  }
178 }
179 
182 {
183  uint32_t tsSec;
184  uint32_t tsUsec;
185  uint32_t inclLen;
186  uint32_t origLen;
187  uint32_t readLen;
188 
189  uint8_t datbuf[65536];
190 
191  m_file.Read(datbuf, 65536, tsSec, tsUsec, inclLen, origLen, readLen);
192 
193  if (m_file.Fail())
194  {
195  return nullptr;
196  }
197 
198  if (m_file.IsNanoSecMode())
199  {
200  t = NanoSeconds(tsSec * 1000000000ULL + tsUsec);
201  }
202  else
203  {
204  t = MicroSeconds(tsSec * 1000000ULL + tsUsec);
205  }
206 
207  return Create<Packet>(datbuf, origLen);
208 }
209 
210 uint32_t
212 {
213  NS_LOG_FUNCTION(this);
214  return m_file.GetMagic();
215 }
216 
217 uint16_t
219 {
220  NS_LOG_FUNCTION(this);
221  return m_file.GetVersionMajor();
222 }
223 
224 uint16_t
226 {
227  NS_LOG_FUNCTION(this);
228  return m_file.GetVersionMinor();
229 }
230 
231 int32_t
233 {
234  NS_LOG_FUNCTION(this);
235  return m_file.GetTimeZoneOffset();
236 }
237 
238 uint32_t
240 {
241  NS_LOG_FUNCTION(this);
242  return m_file.GetSigFigs();
243 }
244 
245 uint32_t
247 {
248  NS_LOG_FUNCTION(this);
249  return m_file.GetSnapLen();
250 }
251 
252 uint32_t
254 {
255  NS_LOG_FUNCTION(this);
256  return m_file.GetDataLinkType();
257 }
258 
259 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
Protocol header serialization and deserialization.
Definition: header.h:44
A base class which provides memory management and object aggregation.
Definition: object.h:89
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
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
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
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
void Clear()
Clear all state bits of the underlying iostream.
Definition: pcap-file.cc:85
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
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
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
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.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46