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